utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package packet
  2. import (
  3. "crypto/hmac"
  4. "crypto/subtle"
  5. "hash"
  6. "github.com/gravitl/netmaker/nm-proxy/wg"
  7. "golang.org/x/crypto/blake2s"
  8. "golang.org/x/crypto/curve25519"
  9. )
  10. type MessageType uint32
  11. type ProxyActionType uint32
  12. const (
  13. MessageInitiationType MessageType = 1
  14. MessageMetricsType MessageType = 5
  15. MessageProxyType MessageType = 6
  16. MessageProxyUpdateType MessageType = 7
  17. )
  18. const (
  19. UpdateListenPort ProxyActionType = 1
  20. )
  21. const (
  22. NoisePublicKeySize = 32
  23. NoisePrivateKeySize = 32
  24. MessageMetricSize = 148
  25. MessageProxyUpdateSize = 148
  26. MessageProxySize = 36
  27. NoiseConstruction = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s"
  28. WGIdentifier = "WireGuard v1 zx2c4 [email protected]"
  29. WGLabelMAC1 = "mac1----"
  30. WGLabelCookie = "cookie--"
  31. )
  32. func mixKey(dst, c *[blake2s.Size]byte, data []byte) {
  33. KDF1(dst, c[:], data)
  34. }
  35. func mixHash(dst, h *[blake2s.Size]byte, data []byte) {
  36. hash, _ := blake2s.New256(nil)
  37. hash.Write(h[:])
  38. hash.Write(data)
  39. hash.Sum(dst[:0])
  40. hash.Reset()
  41. }
  42. func HMAC1(sum *[blake2s.Size]byte, key, in0 []byte) {
  43. mac := hmac.New(func() hash.Hash {
  44. h, _ := blake2s.New256(nil)
  45. return h
  46. }, key)
  47. mac.Write(in0)
  48. mac.Sum(sum[:0])
  49. }
  50. func HMAC2(sum *[blake2s.Size]byte, key, in0, in1 []byte) {
  51. mac := hmac.New(func() hash.Hash {
  52. h, _ := blake2s.New256(nil)
  53. return h
  54. }, key)
  55. mac.Write(in0)
  56. mac.Write(in1)
  57. mac.Sum(sum[:0])
  58. }
  59. func KDF1(t0 *[blake2s.Size]byte, key, input []byte) {
  60. HMAC1(t0, key, input)
  61. HMAC1(t0, t0[:], []byte{0x1})
  62. }
  63. func KDF2(t0, t1 *[blake2s.Size]byte, key, input []byte) {
  64. var prk [blake2s.Size]byte
  65. HMAC1(&prk, key, input)
  66. HMAC1(t0, prk[:], []byte{0x1})
  67. HMAC2(t1, prk[:], t0[:], []byte{0x2})
  68. setZero(prk[:])
  69. }
  70. func setZero(arr []byte) {
  71. for i := range arr {
  72. arr[i] = 0
  73. }
  74. }
  75. func isZero(val []byte) bool {
  76. acc := 1
  77. for _, b := range val {
  78. acc &= subtle.ConstantTimeByteEq(b, 0)
  79. }
  80. return acc == 1
  81. }
  82. func GetDeviceKeys(ifaceName string) (NoisePrivateKey, NoisePublicKey, error) {
  83. wgPrivKey := wg.GetWgIfacePrivKey(ifaceName)
  84. wgPubKey := wg.GetWgIfacePubKey(ifaceName)
  85. return wgPrivKey, wgPubKey, nil
  86. }
  87. type (
  88. NoisePublicKey [NoisePublicKeySize]byte
  89. NoisePrivateKey [NoisePrivateKeySize]byte
  90. )
  91. func sharedSecret(sk *NoisePrivateKey, pk NoisePublicKey) (ss [NoisePublicKeySize]byte) {
  92. apk := (*[NoisePublicKeySize]byte)(&pk)
  93. ask := (*[NoisePrivateKeySize]byte)(sk)
  94. //lint:ignore SA1019 no need of back and forth conversion between arrays and slices required by curve25519.X25519 function
  95. curve25519.ScalarMult(&ss, ask, apk)
  96. return ss
  97. }