2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2021-01-28 16:52:15 +00:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-04 14:15:53 +00:00
|
|
|
"crypto/subtle"
|
2017-05-30 20:36:49 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-28 13:44:51 +00:00
|
|
|
NoisePublicKeySize = 32
|
|
|
|
NoisePrivateKeySize = 32
|
|
|
|
NoisePresharedKeySize = 32
|
2017-05-30 20:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2017-06-23 11:45:32 +00:00
|
|
|
NoisePublicKey [NoisePublicKeySize]byte
|
|
|
|
NoisePrivateKey [NoisePrivateKeySize]byte
|
2021-01-28 13:44:51 +00:00
|
|
|
NoisePresharedKey [NoisePresharedKeySize]byte
|
2017-06-23 11:45:32 +00:00
|
|
|
NoiseNonce uint64 // padded to 12-bytes
|
2017-05-30 20:36:49 +00:00
|
|
|
)
|
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
func loadExactHex(dst []byte, src string) error {
|
|
|
|
slice, err := hex.DecodeString(src)
|
2017-05-30 20:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-01 19:31:30 +00:00
|
|
|
if len(slice) != len(dst) {
|
2018-05-13 17:33:41 +00:00
|
|
|
return errors.New("hex string does not fit the slice")
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-06-01 19:31:30 +00:00
|
|
|
copy(dst, slice)
|
2017-05-30 20:36:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:45:45 +00:00
|
|
|
func (key NoisePrivateKey) IsZero() bool {
|
2017-08-04 14:15:53 +00:00
|
|
|
var zero NoisePrivateKey
|
|
|
|
return key.Equals(zero)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key NoisePrivateKey) Equals(tar NoisePrivateKey) bool {
|
|
|
|
return subtle.ConstantTimeCompare(key[:], tar[:]) == 1
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 21:00:36 +00:00
|
|
|
func (key *NoisePrivateKey) FromHex(src string) (err error) {
|
|
|
|
err = loadExactHex(key[:], src)
|
|
|
|
key.clamp()
|
|
|
|
return
|
2017-06-01 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 17:03:31 +00:00
|
|
|
func (key *NoisePrivateKey) FromMaybeZeroHex(src string) (err error) {
|
|
|
|
err = loadExactHex(key[:], src)
|
|
|
|
if key.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
key.clamp()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
func (key *NoisePublicKey) FromHex(src string) error {
|
|
|
|
return loadExactHex(key[:], src)
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:15:53 +00:00
|
|
|
func (key NoisePublicKey) IsZero() bool {
|
|
|
|
var zero NoisePublicKey
|
|
|
|
return key.Equals(zero)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key NoisePublicKey) Equals(tar NoisePublicKey) bool {
|
|
|
|
return subtle.ConstantTimeCompare(key[:], tar[:]) == 1
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:44:51 +00:00
|
|
|
func (key *NoisePresharedKey) FromHex(src string) error {
|
2017-06-01 19:31:30 +00:00
|
|
|
return loadExactHex(key[:], src)
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|