hkdf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2014 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. // Package hkdf implements the HMAC-based Extract-and-Expand Key Derivation
  5. // Function (HKDF) as defined in RFC 5869.
  6. //
  7. // HKDF is a cryptographic key derivation function (KDF) with the goal of
  8. // expanding limited input keying material into one or more cryptographically
  9. // strong secret keys.
  10. package hkdf // import "golang.org/x/crypto/hkdf"
  11. import (
  12. "crypto/hmac"
  13. "errors"
  14. "hash"
  15. "io"
  16. )
  17. // Extract generates a pseudorandom key for use with Expand from an input secret
  18. // and an optional independent salt.
  19. //
  20. // Only use this function if you need to reuse the extracted key with multiple
  21. // Expand invocations and different context values. Most common scenarios,
  22. // including the generation of multiple keys, should use New instead.
  23. func Extract(hash func() hash.Hash, secret, salt []byte) []byte {
  24. if salt == nil {
  25. salt = make([]byte, hash().Size())
  26. }
  27. extractor := hmac.New(hash, salt)
  28. extractor.Write(secret)
  29. return extractor.Sum(nil)
  30. }
  31. type hkdf struct {
  32. expander hash.Hash
  33. size int
  34. info []byte
  35. counter byte
  36. prev []byte
  37. buf []byte
  38. }
  39. func (f *hkdf) Read(p []byte) (int, error) {
  40. // Check whether enough data can be generated
  41. need := len(p)
  42. remains := len(f.buf) + int(255-f.counter+1)*f.size
  43. if remains < need {
  44. return 0, errors.New("hkdf: entropy limit reached")
  45. }
  46. // Read any leftover from the buffer
  47. n := copy(p, f.buf)
  48. p = p[n:]
  49. // Fill the rest of the buffer
  50. for len(p) > 0 {
  51. f.expander.Reset()
  52. f.expander.Write(f.prev)
  53. f.expander.Write(f.info)
  54. f.expander.Write([]byte{f.counter})
  55. f.prev = f.expander.Sum(f.prev[:0])
  56. f.counter++
  57. // Copy the new batch into p
  58. f.buf = f.prev
  59. n = copy(p, f.buf)
  60. p = p[n:]
  61. }
  62. // Save leftovers for next run
  63. f.buf = f.buf[n:]
  64. return need, nil
  65. }
  66. // Expand returns a Reader, from which keys can be read, using the given
  67. // pseudorandom key and optional context info, skipping the extraction step.
  68. //
  69. // The pseudorandomKey should have been generated by Extract, or be a uniformly
  70. // random or pseudorandom cryptographically strong key. See RFC 5869, Section
  71. // 3.3. Most common scenarios will want to use New instead.
  72. func Expand(hash func() hash.Hash, pseudorandomKey, info []byte) io.Reader {
  73. expander := hmac.New(hash, pseudorandomKey)
  74. return &hkdf{expander, expander.Size(), info, 1, nil, nil}
  75. }
  76. // New returns a Reader, from which keys can be read, using the given hash,
  77. // secret, salt and context info. Salt and info can be nil.
  78. func New(hash func() hash.Hash, secret, salt, info []byte) io.Reader {
  79. prk := Extract(hash, secret, salt)
  80. return Expand(hash, prk, info)
  81. }