gitlab.com/SiaPrime/SiaPrime@v1.4.1/crypto/X25519.go (about)

     1  package crypto
     2  
     3  import (
     4  	"gitlab.com/NebulousLabs/fastrand"
     5  	"golang.org/x/crypto/blake2b"
     6  	"golang.org/x/crypto/curve25519"
     7  )
     8  
     9  type (
    10  	// An X25519SecretKey is the secret half of an X25519 key pair.
    11  	X25519SecretKey [32]byte
    12  
    13  	// An X25519PublicKey is the public half of an X25519 key pair.
    14  	X25519PublicKey [32]byte
    15  )
    16  
    17  // GenerateX25519KeyPair generates an ephemeral key pair for use in ECDH.
    18  func GenerateX25519KeyPair() (xsk X25519SecretKey, xpk X25519PublicKey) {
    19  	fastrand.Read(xsk[:])
    20  	curve25519.ScalarBaseMult((*[32]byte)(&xpk), (*[32]byte)(&xsk))
    21  	return
    22  }
    23  
    24  // DeriveSharedSecret derives 32 bytes of entropy from a secret key and public
    25  // key. Derivation is via ScalarMult of the private and public keys, followed
    26  // by a 256-bit unkeyed blake2b hash.
    27  func DeriveSharedSecret(xsk X25519SecretKey, xpk X25519PublicKey) (secret [32]byte) {
    28  	var dst [32]byte
    29  	curve25519.ScalarMult(&dst, (*[32]byte)(&xsk), (*[32]byte)(&xpk))
    30  	return blake2b.Sum256(dst[:])
    31  }