Kill nacl/box

This commit is contained in:
Frank Denis 2019-06-24 19:13:34 +02:00
parent d80e72365f
commit dd9cf5cc9a
23 changed files with 60 additions and 3085 deletions

View file

@ -4,21 +4,24 @@ import (
"errors"
"github.com/aead/chacha20/chacha"
"golang.org/x/crypto/curve25519"
"github.com/cloudflare/circl/dh/x25519"
)
// SharedKey computes a shared secret compatible with the one used by `crypto_box_xchacha20poly1305``
func SharedKey(secretKey [32]byte, publicKey [32]byte) ([32]byte, error) {
var sharedKey [32]byte
curve25519.ScalarMult(&sharedKey, &secretKey, &publicKey)
c := byte(0)
for i := 0; i < 32; i++ {
c |= sharedKey[i]
}
if c == 0 {
var cfSharedKey, cfSecretKey, cfPublicKey x25519.Key
copy(cfSecretKey[:], secretKey[:])
copy(cfPublicKey[:], publicKey[:])
if !x25519.Shared(&cfSharedKey, &cfSecretKey, &cfPublicKey) {
return sharedKey, errors.New("weak public key")
}
var nonce [16]byte
chacha.HChaCha20(&sharedKey, &nonce, &sharedKey)
HChaCha20(&sharedKey)
return sharedKey, nil
}
// HChaCha20 - Hash the result of an X25519 key exchange in order to get a box-compatible shared secret
func HChaCha20(sharedKey *[32]byte) {
var zeroNonce [16]byte
chacha.HChaCha20(sharedKey, &zeroNonce, sharedKey)
}