2019-01-02 01:55:51 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 15:04:00 +02:00
|
|
|
*
|
2021-01-28 17:52:15 +01:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-05-03 15:04:00 +02:00
|
|
|
*/
|
|
|
|
|
2019-03-03 04:04:41 +01:00
|
|
|
package device
|
2017-06-23 13:41:59 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
2018-02-02 16:40:14 +01:00
|
|
|
"crypto/subtle"
|
2019-05-14 09:09:52 +02:00
|
|
|
"hash"
|
|
|
|
|
2017-06-23 13:41:59 +02:00
|
|
|
"golang.org/x/crypto/blake2s"
|
|
|
|
"golang.org/x/crypto/curve25519"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* KDF related functions.
|
|
|
|
* HMAC-based Key Derivation Function (HKDF)
|
|
|
|
* https://tools.ietf.org/html/rfc5869
|
|
|
|
*/
|
|
|
|
|
2017-09-01 14:21:53 +02:00
|
|
|
func HMAC1(sum *[blake2s.Size]byte, key, in0 []byte) {
|
2017-06-23 13:41:59 +02:00
|
|
|
mac := hmac.New(func() hash.Hash {
|
|
|
|
h, _ := blake2s.New256(nil)
|
|
|
|
return h
|
|
|
|
}, key)
|
2017-09-01 14:21:53 +02:00
|
|
|
mac.Write(in0)
|
2017-06-23 13:41:59 +02:00
|
|
|
mac.Sum(sum[:0])
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:21:53 +02:00
|
|
|
func HMAC2(sum *[blake2s.Size]byte, key, in0, in1 []byte) {
|
|
|
|
mac := hmac.New(func() hash.Hash {
|
|
|
|
h, _ := blake2s.New256(nil)
|
|
|
|
return h
|
|
|
|
}, key)
|
|
|
|
mac.Write(in0)
|
|
|
|
mac.Write(in1)
|
|
|
|
mac.Sum(sum[:0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func KDF1(t0 *[blake2s.Size]byte, key, input []byte) {
|
|
|
|
HMAC1(t0, key, input)
|
|
|
|
HMAC1(t0, t0[:], []byte{0x1})
|
2017-06-23 13:41:59 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 14:21:53 +02:00
|
|
|
func KDF2(t0, t1 *[blake2s.Size]byte, key, input []byte) {
|
2017-06-23 13:41:59 +02:00
|
|
|
var prk [blake2s.Size]byte
|
2017-09-01 14:21:53 +02:00
|
|
|
HMAC1(&prk, key, input)
|
|
|
|
HMAC1(t0, prk[:], []byte{0x1})
|
|
|
|
HMAC2(t1, prk[:], t0[:], []byte{0x2})
|
|
|
|
setZero(prk[:])
|
2017-06-23 13:41:59 +02:00
|
|
|
}
|
|
|
|
|
2017-09-01 14:21:53 +02:00
|
|
|
func KDF3(t0, t1, t2 *[blake2s.Size]byte, key, input []byte) {
|
2017-06-23 13:41:59 +02:00
|
|
|
var prk [blake2s.Size]byte
|
2017-09-01 14:21:53 +02:00
|
|
|
HMAC1(&prk, key, input)
|
|
|
|
HMAC1(t0, prk[:], []byte{0x1})
|
|
|
|
HMAC2(t1, prk[:], t0[:], []byte{0x2})
|
|
|
|
HMAC2(t2, prk[:], t1[:], []byte{0x3})
|
|
|
|
setZero(prk[:])
|
2017-06-23 13:41:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:15:53 +02:00
|
|
|
func isZero(val []byte) bool {
|
2018-02-02 16:40:14 +01:00
|
|
|
acc := 1
|
2017-08-04 16:15:53 +02:00
|
|
|
for _, b := range val {
|
2018-02-02 16:40:14 +01:00
|
|
|
acc &= subtle.ConstantTimeByteEq(b, 0)
|
2017-08-04 16:15:53 +02:00
|
|
|
}
|
2018-02-02 16:40:14 +01:00
|
|
|
return acc == 1
|
2017-08-04 16:15:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:33:41 +02:00
|
|
|
/* This function is not used as pervasively as it should because this is mostly impossible in Go at the moment */
|
2017-09-01 14:21:53 +02:00
|
|
|
func setZero(arr []byte) {
|
|
|
|
for i := range arr {
|
|
|
|
arr[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:00:36 +01:00
|
|
|
func (sk *NoisePrivateKey) clamp() {
|
|
|
|
sk[0] &= 248
|
|
|
|
sk[31] = (sk[31] & 127) | 64
|
|
|
|
}
|
|
|
|
|
2017-06-23 13:41:59 +02:00
|
|
|
func newPrivateKey() (sk NoisePrivateKey, err error) {
|
|
|
|
_, err = rand.Read(sk[:])
|
2019-02-03 22:00:36 +01:00
|
|
|
sk.clamp()
|
2017-06-23 13:41:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:03:11 +02:00
|
|
|
func (sk *NoisePrivateKey) PublicKey() (pk NoisePublicKey) {
|
2017-06-23 13:41:59 +02:00
|
|
|
apk := (*[NoisePublicKeySize]byte)(&pk)
|
|
|
|
ask := (*[NoisePrivateKeySize]byte)(sk)
|
|
|
|
curve25519.ScalarBaseMult(apk, ask)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sk *NoisePrivateKey) sharedSecret(pk NoisePublicKey) (ss [NoisePublicKeySize]byte) {
|
|
|
|
apk := (*[NoisePublicKeySize]byte)(&pk)
|
|
|
|
ask := (*[NoisePrivateKeySize]byte)(sk)
|
2017-06-24 15:34:17 +02:00
|
|
|
curve25519.ScalarMult(&ss, ask, apk)
|
2017-06-23 13:41:59 +02:00
|
|
|
return ss
|
|
|
|
}
|