xof.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Package xof provides an interface for eXtendable-Output Functions.
  2. //
  3. // # Available Functions
  4. //
  5. // SHAKE functions are defined in FIPS-202, see https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf.
  6. // BLAKE2Xb and BLAKE2Xs are defined in https://www.blake2.net/blake2x.pdf.
  7. package xof
  8. import (
  9. "io"
  10. "github.com/cloudflare/circl/internal/sha3"
  11. "github.com/cloudflare/circl/xof/k12"
  12. "golang.org/x/crypto/blake2b"
  13. "golang.org/x/crypto/blake2s"
  14. )
  15. // XOF defines the interface to hash functions that support arbitrary-length output.
  16. type XOF interface {
  17. // Write absorbs more data into the XOF's state. It panics if called
  18. // after Read.
  19. io.Writer
  20. // Read reads more output from the XOF. It returns io.EOF if the limit
  21. // has been reached.
  22. io.Reader
  23. // Clone returns a copy of the XOF in its current state.
  24. Clone() XOF
  25. // Reset restores the XOF to its initial state and discards all data appended by Write.
  26. Reset()
  27. }
  28. type ID uint
  29. const (
  30. SHAKE128 ID = iota + 1
  31. SHAKE256
  32. BLAKE2XB
  33. BLAKE2XS
  34. K12D10
  35. )
  36. func (x ID) New() XOF {
  37. switch x {
  38. case SHAKE128:
  39. s := sha3.NewShake128()
  40. return shakeBody{&s}
  41. case SHAKE256:
  42. s := sha3.NewShake256()
  43. return shakeBody{&s}
  44. case BLAKE2XB:
  45. x, _ := blake2b.NewXOF(blake2b.OutputLengthUnknown, nil)
  46. return blake2xb{x}
  47. case BLAKE2XS:
  48. x, _ := blake2s.NewXOF(blake2s.OutputLengthUnknown, nil)
  49. return blake2xs{x}
  50. case K12D10:
  51. x := k12.NewDraft10([]byte{})
  52. return k12d10{&x}
  53. default:
  54. panic("crypto: requested unavailable XOF function")
  55. }
  56. }
  57. type shakeBody struct{ sha3.ShakeHash }
  58. func (s shakeBody) Clone() XOF { return shakeBody{s.ShakeHash.Clone()} }
  59. type blake2xb struct{ blake2b.XOF }
  60. func (s blake2xb) Clone() XOF { return blake2xb{s.XOF.Clone()} }
  61. type blake2xs struct{ blake2s.XOF }
  62. func (s blake2xs) Clone() XOF { return blake2xs{s.XOF.Clone()} }
  63. type k12d10 struct{ *k12.State }
  64. func (s k12d10) Clone() XOF {
  65. x := s.State.Clone()
  66. return k12d10{&x}
  67. }