hkdf.go 897 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package noise
  2. import (
  3. "crypto/hmac"
  4. "hash"
  5. )
  6. func hkdf(h func() hash.Hash, outputs int, out1, out2, out3, chainingKey, inputKeyMaterial []byte) ([]byte, []byte, []byte) {
  7. if len(out1) > 0 {
  8. panic("len(out1) > 0")
  9. }
  10. if len(out2) > 0 {
  11. panic("len(out2) > 0")
  12. }
  13. if len(out3) > 0 {
  14. panic("len(out3) > 0")
  15. }
  16. if outputs > 3 {
  17. panic("outputs > 3")
  18. }
  19. tempMAC := hmac.New(h, chainingKey)
  20. tempMAC.Write(inputKeyMaterial)
  21. tempKey := tempMAC.Sum(out2)
  22. out1MAC := hmac.New(h, tempKey)
  23. out1MAC.Write([]byte{0x01})
  24. out1 = out1MAC.Sum(out1)
  25. if outputs == 1 {
  26. return out1, nil, nil
  27. }
  28. out2MAC := hmac.New(h, tempKey)
  29. out2MAC.Write(out1)
  30. out2MAC.Write([]byte{0x02})
  31. out2 = out2MAC.Sum(out2)
  32. if outputs == 2 {
  33. return out1, out2, nil
  34. }
  35. out3MAC := hmac.New(h, tempKey)
  36. out3MAC.Write(out2)
  37. out3MAC.Write([]byte{0x03})
  38. out3 = out3MAC.Sum(out3)
  39. return out1, out2, out3
  40. }