2022-08-19 16:34:07 +00:00
|
|
|
// Copyright (c) 2015 The btcsuite developers
|
2023-06-30 13:41:32 +00:00
|
|
|
// Copyright (c) 2015-2023 The Decred developers
|
2022-08-19 16:34:07 +00:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package secp256k1
|
|
|
|
|
|
|
|
// GenerateSharedSecret generates a shared secret based on a private key and a
|
|
|
|
// public key using Diffie-Hellman key exchange (ECDH) (RFC 5903).
|
|
|
|
// RFC5903 Section 9 states we should only return x.
|
|
|
|
//
|
2023-06-30 13:41:32 +00:00
|
|
|
// It is recommended to securely hash the result before using as a cryptographic
|
2022-08-19 16:34:07 +00:00
|
|
|
// key.
|
|
|
|
func GenerateSharedSecret(privkey *PrivateKey, pubkey *PublicKey) []byte {
|
|
|
|
var point, result JacobianPoint
|
|
|
|
pubkey.AsJacobian(&point)
|
|
|
|
ScalarMultNonConst(&privkey.Key, &point, &result)
|
|
|
|
result.ToAffine()
|
|
|
|
xBytes := result.X.Bytes()
|
|
|
|
return xBytes[:]
|
|
|
|
}
|