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  type implementation struct {
    10  	Package   string
    11  	Name      string
    12  	Available bool
    13  	Toggle    *bool
    14  }
    15  
    16  var allImplementations []implementation
    17  
    18  // Register records an alternative implementation of a cryptographic primitive.
    19  // The implementation might be available or not based on CPU support. If
    20  // available is false, the implementation is unavailable and can't be tested on
    21  // this machine. If available is true, it can be set to false to disable the
    22  // implementation. If all alternative implementations but one are disabled, the
    23  // remaining one must be used (i.e. disabling one implementation must not
    24  // implicitly disable any other). Each package has an implicit base
    25  // implementation that is selected when all alternatives are unavailable or
    26  // disabled.
    27  func Register(pkg, name string, available *bool) {
    28  	allImplementations = append(allImplementations, implementation{
    29  		Package:   pkg,
    30  		Name:      name,
    31  		Available: *available,
    32  		Toggle:    available,
    33  	})
    34  }
    35  
    36  // List returns the names of all alternative implementations registered for the
    37  // given package, whether available or not. The implicit base implementation is
    38  // not included.
    39  func List(pkg string) []string {
    40  	var names []string
    41  	for _, i := range allImplementations {
    42  		if i.Package == pkg {
    43  			names = append(names, i.Name)
    44  		}
    45  	}
    46  	return names
    47  }
    48  
    49  func available(pkg, name string) bool {
    50  	for _, i := range allImplementations {
    51  		if i.Package == pkg && i.Name == name {
    52  			return i.Available
    53  		}
    54  	}
    55  	panic("unknown implementation")
    56  }
    57  
    58  // Select disables all implementations for the given package except the one
    59  // with the given name. If name is empty, the base implementation is selected.
    60  // It returns whether the selected implementation is available.
    61  func Select(pkg, name string) bool {
    62  	if name == "" {
    63  		for _, i := range allImplementations {
    64  			if i.Package == pkg {
    65  				*i.Toggle = false
    66  			}
    67  		}
    68  		return true
    69  	}
    70  	if !available(pkg, name) {
    71  		return false
    72  	}
    73  	for _, i := range allImplementations {
    74  		if i.Package == pkg {
    75  			*i.Toggle = i.Name == name
    76  		}
    77  	}
    78  	return true
    79  }
    80  
    81  func Reset(pkg string) {
    82  	for _, i := range allImplementations {
    83  		if i.Package == pkg {
    84  			*i.Toggle = i.Available
    85  			return
    86  		}
    87  	}
    88  }
    89  

View as plain text