2018-05-03 15:04:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2017-05-30 22:36:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-08-04 16:15:53 +02:00
|
|
|
"crypto/subtle"
|
2017-05-30 22:36:49 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2017-06-28 23:45:45 +02:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
2017-05-30 22:36:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-28 23:45:45 +02:00
|
|
|
NoisePublicKeySize = 32
|
|
|
|
NoisePrivateKeySize = 32
|
2017-05-30 22:36:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2017-06-23 13:45:32 +02:00
|
|
|
NoisePublicKey [NoisePublicKeySize]byte
|
|
|
|
NoisePrivateKey [NoisePrivateKeySize]byte
|
2017-06-28 23:45:45 +02:00
|
|
|
NoiseSymmetricKey [chacha20poly1305.KeySize]byte
|
2017-06-23 13:45:32 +02:00
|
|
|
NoiseNonce uint64 // padded to 12-bytes
|
2017-05-30 22:36:49 +02:00
|
|
|
)
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
func loadExactHex(dst []byte, src string) error {
|
|
|
|
slice, err := hex.DecodeString(src)
|
2017-05-30 22:36:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-01 21:31:30 +02:00
|
|
|
if len(slice) != len(dst) {
|
|
|
|
return errors.New("Hex string does not fit the slice")
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
2017-06-01 21:31:30 +02:00
|
|
|
copy(dst, slice)
|
2017-05-30 22:36:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-28 23:45:45 +02:00
|
|
|
func (key NoisePrivateKey) IsZero() bool {
|
2017-08-04 16:15:53 +02:00
|
|
|
var zero NoisePrivateKey
|
|
|
|
return key.Equals(zero)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key NoisePrivateKey) Equals(tar NoisePrivateKey) bool {
|
|
|
|
return subtle.ConstantTimeCompare(key[:], tar[:]) == 1
|
2017-06-28 23:45:45 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
func (key *NoisePrivateKey) FromHex(src string) error {
|
|
|
|
return loadExactHex(key[:], src)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key NoisePrivateKey) ToHex() string {
|
2017-05-30 22:36:49 +02:00
|
|
|
return hex.EncodeToString(key[:])
|
|
|
|
}
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
func (key *NoisePublicKey) FromHex(src string) error {
|
|
|
|
return loadExactHex(key[:], src)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key NoisePublicKey) ToHex() string {
|
|
|
|
return hex.EncodeToString(key[:])
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:15:53 +02: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
|
|
|
|
}
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
func (key *NoiseSymmetricKey) FromHex(src string) error {
|
|
|
|
return loadExactHex(key[:], src)
|
2017-05-30 22:36:49 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 21:31:30 +02:00
|
|
|
func (key NoiseSymmetricKey) ToHex() string {
|
2017-05-30 22:36:49 +02:00
|
|
|
return hex.EncodeToString(key[:])
|
|
|
|
}
|