xref: /optee_os/core/lib/libtomcrypt/src/pk/dh/dh_shared_secret.c (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_MDH
7 
8 /**
9    Create a DH shared secret.
10    @param private_key     The private DH key in the pair
11    @param public_key      The public DH key in the pair
12    @param out             [out] The destination of the shared data
13    @param outlen          [in/out] The max size and resulting size of the shared data.
14    @return CRYPT_OK if successful
15 */
dh_shared_secret(const dh_key * private_key,const dh_key * public_key,unsigned char * out,unsigned long * outlen)16 int dh_shared_secret(const dh_key *private_key, const dh_key *public_key,
17                      unsigned char *out, unsigned long *outlen)
18 {
19    void *tmp;
20    unsigned long x;
21    int err;
22 
23    LTC_ARGCHK(private_key != NULL);
24    LTC_ARGCHK(public_key  != NULL);
25    LTC_ARGCHK(out         != NULL);
26    LTC_ARGCHK(outlen      != NULL);
27 
28    /* types valid? */
29    if (private_key->type != PK_PRIVATE) {
30       return CRYPT_PK_NOT_PRIVATE;
31    }
32 
33    /* same DH group? */
34    if (mp_cmp(private_key->prime, public_key->prime) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; }
35    if (mp_cmp(private_key->base, public_key->base) != LTC_MP_EQ)   { return CRYPT_PK_TYPE_MISMATCH; }
36 
37    /* init big numbers */
38    if ((err = mp_init(&tmp)) != CRYPT_OK) {
39       return err;
40    }
41 
42    /* check public key */
43    if ((err = dh_check_pubkey(public_key)) != CRYPT_OK) {
44       goto error;
45    }
46 
47    /* compute tmp = y^x mod p */
48    if ((err = mp_exptmod(public_key->y, private_key->x, private_key->prime, tmp)) != CRYPT_OK)  {
49       goto error;
50    }
51 
52    /* enough space for output? */
53    x = (unsigned long)mp_unsigned_bin_size(tmp);
54    if (*outlen < x) {
55       *outlen = x;
56       err = CRYPT_BUFFER_OVERFLOW;
57       goto error;
58    }
59    if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
60       goto error;
61    }
62    *outlen = x;
63    err = CRYPT_OK;
64 
65 error:
66    mp_clear(tmp);
67    return err;
68 }
69 
70 #endif /* LTC_MDH */
71