2015-11-15 17:49:59 +00:00
|
|
|
package noise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/binary"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
|
2017-09-22 13:18:33 +00:00
|
|
|
"golang.org/x/crypto/blake2b"
|
2017-01-08 08:49:27 +00:00
|
|
|
"golang.org/x/crypto/blake2s"
|
2017-01-08 08:55:28 +00:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
2015-11-15 17:49:59 +00:00
|
|
|
"golang.org/x/crypto/curve25519"
|
|
|
|
)
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// A DHKey is a keypair used for Diffie-Hellman key agreement.
|
2015-11-15 17:49:59 +00:00
|
|
|
type DHKey struct {
|
|
|
|
Private []byte
|
|
|
|
Public []byte
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// A DHFunc implements Diffie-Hellman key agreement.
|
2015-11-15 17:49:59 +00:00
|
|
|
type DHFunc interface {
|
2016-07-13 02:20:06 +00:00
|
|
|
// GenerateKeypair generates a new keypair using random as a source of
|
|
|
|
// entropy.
|
2018-01-10 02:48:36 +00:00
|
|
|
GenerateKeypair(random io.Reader) (DHKey, error)
|
2016-07-13 02:20:06 +00:00
|
|
|
|
2022-12-14 16:11:33 +00:00
|
|
|
// GenerateKeypairFromPrivateKEy generates a keypair from a private key
|
|
|
|
GenerateKeyPairFromPrivateKey(privkey []byte) (DHKey, error)
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// DH performs a Diffie-Hellman calculation between the provided private and
|
|
|
|
// public keys and returns the result.
|
2021-04-22 17:00:17 +00:00
|
|
|
DH(privkey, pubkey []byte) ([]byte, error)
|
2016-07-13 02:20:06 +00:00
|
|
|
|
|
|
|
// DHLen is the number of bytes returned by DH.
|
2015-11-15 17:49:59 +00:00
|
|
|
DHLen() int
|
2016-07-13 02:20:06 +00:00
|
|
|
|
|
|
|
// DHName is the name of the DH function.
|
2015-11-15 17:49:59 +00:00
|
|
|
DHName() string
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// A HashFunc implements a cryptographic hash function.
|
2015-11-15 17:49:59 +00:00
|
|
|
type HashFunc interface {
|
2016-07-13 02:20:06 +00:00
|
|
|
// Hash returns a hash state.
|
2015-11-15 17:49:59 +00:00
|
|
|
Hash() hash.Hash
|
2016-07-13 02:20:06 +00:00
|
|
|
|
|
|
|
// HashName is the name of the hash function.
|
2015-11-15 17:49:59 +00:00
|
|
|
HashName() string
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// A CipherFunc implements an AEAD symmetric cipher.
|
2015-11-15 17:49:59 +00:00
|
|
|
type CipherFunc interface {
|
2016-07-13 02:20:06 +00:00
|
|
|
// Cipher initializes the algorithm with the provided key and returns a Cipher.
|
2015-11-15 17:49:59 +00:00
|
|
|
Cipher(k [32]byte) Cipher
|
2016-07-13 02:20:06 +00:00
|
|
|
|
|
|
|
// CipherName is the name of the cipher.
|
2015-11-15 17:49:59 +00:00
|
|
|
CipherName() string
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// A Cipher is a AEAD cipher that has been initialized with a key.
|
2015-11-15 17:49:59 +00:00
|
|
|
type Cipher interface {
|
2016-07-13 02:20:06 +00:00
|
|
|
// Encrypt encrypts the provided plaintext with a nonce and then appends the
|
|
|
|
// ciphertext to out along with an authentication tag over the ciphertext
|
|
|
|
// and optional authenticated data.
|
2015-11-15 17:49:59 +00:00
|
|
|
Encrypt(out []byte, n uint64, ad, plaintext []byte) []byte
|
2016-07-13 02:20:06 +00:00
|
|
|
|
|
|
|
// Decrypt authenticates the ciphertext and optional authenticated data and
|
|
|
|
// then decrypts the provided ciphertext using the provided nonce and
|
|
|
|
// appends it to out.
|
2015-11-15 17:49:59 +00:00
|
|
|
Decrypt(out []byte, n uint64, ad, ciphertext []byte) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// A CipherSuite is a set of cryptographic primitives used in a Noise protocol.
|
|
|
|
// It should be constructed with NewCipherSuite.
|
2015-11-15 17:49:59 +00:00
|
|
|
type CipherSuite interface {
|
|
|
|
DHFunc
|
|
|
|
CipherFunc
|
|
|
|
HashFunc
|
|
|
|
Name() []byte
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// NewCipherSuite returns a CipherSuite constructed from the specified
|
|
|
|
// primitives.
|
2015-11-15 17:49:59 +00:00
|
|
|
func NewCipherSuite(dh DHFunc, c CipherFunc, h HashFunc) CipherSuite {
|
|
|
|
return ciphersuite{
|
|
|
|
DHFunc: dh,
|
|
|
|
CipherFunc: c,
|
|
|
|
HashFunc: h,
|
|
|
|
name: []byte(dh.DHName() + "_" + c.CipherName() + "_" + h.HashName()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ciphersuite struct {
|
|
|
|
DHFunc
|
|
|
|
CipherFunc
|
|
|
|
HashFunc
|
|
|
|
name []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ciphersuite) Name() []byte { return s.name }
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// DH25519 is the Curve25519 ECDH function.
|
2015-11-15 17:49:59 +00:00
|
|
|
var DH25519 DHFunc = dh25519{}
|
|
|
|
|
|
|
|
type dh25519 struct{}
|
|
|
|
|
2022-12-14 16:11:33 +00:00
|
|
|
func (d dh25519) GenerateKeypair(rng io.Reader) (DHKey, error) {
|
2021-04-22 17:00:17 +00:00
|
|
|
privkey := make([]byte, 32)
|
2015-11-15 17:49:59 +00:00
|
|
|
if rng == nil {
|
|
|
|
rng = rand.Reader
|
|
|
|
}
|
2021-04-22 17:00:17 +00:00
|
|
|
if _, err := io.ReadFull(rng, privkey); err != nil {
|
2018-01-10 02:48:36 +00:00
|
|
|
return DHKey{}, err
|
2015-11-15 17:49:59 +00:00
|
|
|
}
|
2022-12-14 16:11:33 +00:00
|
|
|
|
|
|
|
return d.GenerateKeyPairFromPrivateKey(privkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dh25519) GenerateKeyPairFromPrivateKey(privkey []byte) (DHKey, error) {
|
2021-04-22 17:00:17 +00:00
|
|
|
pubkey, err := curve25519.X25519(privkey, curve25519.Basepoint)
|
|
|
|
if err != nil {
|
|
|
|
return DHKey{}, err
|
|
|
|
}
|
|
|
|
return DHKey{Private: privkey, Public: pubkey}, nil
|
2015-11-15 17:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 17:00:17 +00:00
|
|
|
func (dh25519) DH(privkey, pubkey []byte) ([]byte, error) {
|
|
|
|
return curve25519.X25519(privkey, pubkey)
|
2015-11-15 17:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dh25519) DHLen() int { return 32 }
|
|
|
|
func (dh25519) DHName() string { return "25519" }
|
|
|
|
|
|
|
|
type cipherFn struct {
|
|
|
|
fn func([32]byte) Cipher
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c cipherFn) Cipher(k [32]byte) Cipher { return c.fn(k) }
|
|
|
|
func (c cipherFn) CipherName() string { return c.name }
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// CipherAESGCM is the AES256-GCM AEAD cipher.
|
2016-07-13 02:56:22 +00:00
|
|
|
var CipherAESGCM CipherFunc = cipherFn{cipherAESGCM, "AESGCM"}
|
|
|
|
|
|
|
|
func cipherAESGCM(k [32]byte) Cipher {
|
|
|
|
c, err := aes.NewCipher(k[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return aeadCipher{
|
|
|
|
gcm,
|
|
|
|
func(n uint64) []byte {
|
|
|
|
var nonce [12]byte
|
|
|
|
binary.BigEndian.PutUint64(nonce[4:], n)
|
|
|
|
return nonce[:]
|
|
|
|
},
|
|
|
|
}
|
2015-11-15 17:49:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// CipherChaChaPoly is the ChaCha20-Poly1305 AEAD cipher construction.
|
2016-07-13 02:56:22 +00:00
|
|
|
var CipherChaChaPoly CipherFunc = cipherFn{cipherChaChaPoly, "ChaChaPoly"}
|
|
|
|
|
|
|
|
func cipherChaChaPoly(k [32]byte) Cipher {
|
2017-01-08 08:55:28 +00:00
|
|
|
c, err := chacha20poly1305.New(k[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-07-13 02:56:22 +00:00
|
|
|
return aeadCipher{
|
2017-01-08 08:55:28 +00:00
|
|
|
c,
|
2016-07-13 02:56:22 +00:00
|
|
|
func(n uint64) []byte {
|
|
|
|
var nonce [12]byte
|
|
|
|
binary.LittleEndian.PutUint64(nonce[4:], n)
|
|
|
|
return nonce[:]
|
|
|
|
},
|
|
|
|
}
|
2015-11-15 17:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type aeadCipher struct {
|
|
|
|
cipher.AEAD
|
|
|
|
nonce func(uint64) []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c aeadCipher) Encrypt(out []byte, n uint64, ad, plaintext []byte) []byte {
|
|
|
|
return c.Seal(out, c.nonce(n), plaintext, ad)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c aeadCipher) Decrypt(out []byte, n uint64, ad, ciphertext []byte) ([]byte, error) {
|
|
|
|
return c.Open(out, c.nonce(n), ciphertext, ad)
|
|
|
|
}
|
|
|
|
|
|
|
|
type hashFn struct {
|
|
|
|
fn func() hash.Hash
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h hashFn) Hash() hash.Hash { return h.fn() }
|
|
|
|
func (h hashFn) HashName() string { return h.name }
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// HashSHA256 is the SHA-256 hash function.
|
2015-11-15 17:49:59 +00:00
|
|
|
var HashSHA256 HashFunc = hashFn{sha256.New, "SHA256"}
|
2016-07-13 02:20:06 +00:00
|
|
|
|
|
|
|
// HashSHA512 is the SHA-512 hash function.
|
2015-11-15 17:49:59 +00:00
|
|
|
var HashSHA512 HashFunc = hashFn{sha512.New, "SHA512"}
|
2016-07-13 02:20:06 +00:00
|
|
|
|
2017-09-22 13:18:33 +00:00
|
|
|
func blake2bNew() hash.Hash {
|
|
|
|
h, err := blake2b.New512(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// HashBLAKE2b is the BLAKE2b hash function.
|
2017-09-22 13:18:33 +00:00
|
|
|
var HashBLAKE2b HashFunc = hashFn{blake2bNew, "BLAKE2b"}
|
2016-07-13 02:20:06 +00:00
|
|
|
|
2017-01-08 08:49:27 +00:00
|
|
|
func blake2sNew() hash.Hash {
|
|
|
|
h, err := blake2s.New256(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2016-07-13 02:20:06 +00:00
|
|
|
// HashBLAKE2s is the BLAKE2s hash function.
|
2017-01-08 08:49:27 +00:00
|
|
|
var HashBLAKE2s HashFunc = hashFn{blake2sNew, "BLAKE2s"}
|