1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file x25519_shared_secret.c
7   Create a X25519 shared secret, Steffen Jaeckel
8 */
9 
10 #ifdef LTC_CURVE25519
11 
12 /**
13    Create a X25519 shared secret.
14    @param private_key     The private X25519 key in the pair
15    @param public_key      The public X25519 key in the pair
16    @param out             [out] The destination of the shared data
17    @param outlen          [in/out] The max size and resulting size of the shared data.
18    @return CRYPT_OK if successful
19 */
x25519_shared_secret(const curve25519_key * private_key,const curve25519_key * public_key,unsigned char * out,unsigned long * outlen)20 int x25519_shared_secret(const    curve25519_key *private_key,
21                          const    curve25519_key *public_key,
22                                unsigned char *out, unsigned long *outlen)
23 {
24    LTC_ARGCHK(private_key        != NULL);
25    LTC_ARGCHK(public_key         != NULL);
26    LTC_ARGCHK(out                != NULL);
27    LTC_ARGCHK(outlen             != NULL);
28 
29    if(private_key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
30 
31    if(*outlen < 32uL) {
32       *outlen = 32uL;
33       return CRYPT_BUFFER_OVERFLOW;
34    }
35 
36    tweetnacl_crypto_scalarmult(out, private_key->priv, public_key->pub);
37    *outlen = 32uL;
38 
39    return CRYPT_OK;
40 }
41 
42 #endif
43