hkdf_expand.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2015, Yawning Angel <yawning at schwanenlied dot me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package scramblesuit
  28. import (
  29. "crypto/hmac"
  30. "hash"
  31. )
  32. func hkdfExpand(hashFn func() hash.Hash, prk []byte, info []byte, l int) []byte {
  33. // Why, yes. golang.org/x/crypto/hkdf exists, and is a fine
  34. // implementation of HKDF. However it does both the extract
  35. // and expand, while ScrambleSuit only does extract, with no
  36. // way to separate the two steps.
  37. h := hmac.New(hashFn, prk)
  38. digestSz := h.Size()
  39. if l > 255*digestSz {
  40. panic("hkdf: requested OKM length > 255*HashLen")
  41. }
  42. var t []byte
  43. okm := make([]byte, 0, l)
  44. toAppend := l
  45. ctr := byte(1)
  46. for toAppend > 0 {
  47. h.Reset()
  48. _, _ = h.Write(t)
  49. _, _ = h.Write(info)
  50. _, _ = h.Write([]byte{ctr})
  51. t = h.Sum(nil)
  52. ctr++
  53. aLen := digestSz
  54. if toAppend < digestSz {
  55. aLen = toAppend
  56. }
  57. okm = append(okm, t[:aLen]...)
  58. toAppend -= aLen
  59. }
  60. return okm
  61. }