suites.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Package suites allows callers to look up Kyber suites by name.
  2. //
  3. // Currently, only the "ed25519" suite is available with a constant
  4. // time implementation and the other ones use variable time algorithms.
  5. package suites
  6. import (
  7. "errors"
  8. "strings"
  9. "go.dedis.ch/kyber/v3"
  10. )
  11. // Suite is the sum of all suites mix-ins in Kyber.
  12. type Suite interface {
  13. kyber.Encoding
  14. kyber.Group
  15. kyber.HashFactory
  16. kyber.XOFFactory
  17. kyber.Random
  18. }
  19. var suites = map[string]Suite{}
  20. var requireConstTime = false
  21. // register is called by suites to make themselves known to Kyber.
  22. //
  23. func register(s Suite) {
  24. suites[strings.ToLower(s.String())] = s
  25. }
  26. // ErrUnknownSuite indicates that the suite was not one of the
  27. // registered suites.
  28. var ErrUnknownSuite = errors.New("unknown suite")
  29. // Find looks up a suite by name.
  30. func Find(name string) (Suite, error) {
  31. if s, ok := suites[strings.ToLower(name)]; ok {
  32. if requireConstTime && strings.ToLower(s.String()) != "ed25519" {
  33. return nil, errors.New("requested suite exists but is not implemented with constant time algorithms as required by suites.RequireConstantTime")
  34. }
  35. return s, nil
  36. }
  37. return nil, ErrUnknownSuite
  38. }
  39. // MustFind looks up a suite by name and panics if it is not found.
  40. func MustFind(name string) Suite {
  41. s, err := Find(name)
  42. if err != nil {
  43. panic("Suite " + name + " not found.")
  44. }
  45. return s
  46. }
  47. // RequireConstantTime causes all future calls to Find and MustFind to only
  48. // search for suites where the implementation is constant time.
  49. // It should be called in an init() function for the main package
  50. // of users of Kyber who need to be sure to avoid variable time implementations.
  51. // Once constant time implementations are required, there is no way to
  52. // turn it back off (by design).
  53. //
  54. // At this time, the only constant time crypto suite is "Ed25519".
  55. func RequireConstantTime() {
  56. requireConstTime = true
  57. }