1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file x25519_shared_secret.c
14 Create a X25519 shared secret, Steffen Jaeckel
15 */
16
17 #ifdef LTC_CURVE25519
18
19 /**
20 Create a X25519 shared secret.
21 @param private_key The private X25519 key in the pair
22 @param public_key The public X25519 key in the pair
23 @param out [out] The destination of the shared data
24 @param outlen [in/out] The max size and resulting size of the shared data.
25 @return CRYPT_OK if successful
26 */
x25519_shared_secret(const curve25519_key * private_key,const curve25519_key * public_key,unsigned char * out,unsigned long * outlen)27 int x25519_shared_secret(const curve25519_key *private_key,
28 const curve25519_key *public_key,
29 unsigned char *out, unsigned long *outlen)
30 {
31 LTC_ARGCHK(private_key != NULL);
32 LTC_ARGCHK(public_key != NULL);
33 LTC_ARGCHK(out != NULL);
34 LTC_ARGCHK(outlen != NULL);
35
36 if(private_key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
37
38 if(*outlen < 32uL) {
39 *outlen = 32uL;
40 return CRYPT_BUFFER_OVERFLOW;
41 }
42
43 tweetnacl_crypto_scalarmult(out, private_key->priv, public_key->pub);
44 *outlen = 32uL;
45
46 return CRYPT_OK;
47 }
48
49 #endif
50
51 /* ref: $Format:%D$ */
52 /* git commit: $Format:%H$ */
53 /* commit time: $Format:%ai$ */
54