Source file src/crypto/internal/impl/impl.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package impl is a registry of alternative implementations of cryptographic
     6  // primitives, to allow selecting them for testing.
     7  package impl
     8  
     9  import "strings"
    10  
    11  type implementation struct {
    12  	Package   string
    13  	Name      string
    14  	Available bool
    15  	Toggle    *bool
    16  }
    17  
    18  var allImplementations []implementation
    19  
    20  // Register records an alternative implementation of a cryptographic primitive.
    21  // The implementation might be available or not based on CPU support. If
    22  // available is false, the implementation is unavailable and can't be tested on
    23  // this machine. If available is true, it can be set to false to disable the
    24  // implementation. If all alternative implementations but one are disabled, the
    25  // remaining one must be used (i.e. disabling one implementation must not
    26  // implicitly disable any other). Each package has an implicit base
    27  // implementation that is selected when all alternatives are unavailable or
    28  // disabled. pkg must be the package name, not path (e.g. "aes" not "crypto/aes").
    29  func Register(pkg, name string, available *bool) {
    30  	if strings.Contains(pkg, "/") {
    31  		panic("impl: package name must not contain slashes")
    32  	}
    33  	allImplementations = append(allImplementations, implementation{
    34  		Package:   pkg,
    35  		Name:      name,
    36  		Available: *available,
    37  		Toggle:    available,
    38  	})
    39  }
    40  
    41  // List returns the names of all alternative implementations registered for the
    42  // given package, whether available or not. The implicit base implementation is
    43  // not included.
    44  func List(pkg string) []string {
    45  	var names []string
    46  	for _, i := range allImplementations {
    47  		if i.Package == pkg {
    48  			names = append(names, i.Name)
    49  		}
    50  	}
    51  	return names
    52  }
    53  
    54  func available(pkg, name string) bool {
    55  	for _, i := range allImplementations {
    56  		if i.Package == pkg && i.Name == name {
    57  			return i.Available
    58  		}
    59  	}
    60  	panic("unknown implementation")
    61  }
    62  
    63  // Select disables all implementations for the given package except the one
    64  // with the given name. If name is empty, the base implementation is selected.
    65  // It returns whether the selected implementation is available.
    66  func Select(pkg, name string) bool {
    67  	if name == "" {
    68  		for _, i := range allImplementations {
    69  			if i.Package == pkg {
    70  				*i.Toggle = false
    71  			}
    72  		}
    73  		return true
    74  	}
    75  	if !available(pkg, name) {
    76  		return false
    77  	}
    78  	for _, i := range allImplementations {
    79  		if i.Package == pkg {
    80  			*i.Toggle = i.Name == name
    81  		}
    82  	}
    83  	return true
    84  }
    85  
    86  func Reset(pkg string) {
    87  	for _, i := range allImplementations {
    88  		if i.Package == pkg {
    89  			*i.Toggle = i.Available
    90  			return
    91  		}
    92  	}
    93  }
    94  

View as plain text