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 Import DH key parts p and g from dhparam
10
11 dhparam data: openssl dhparam -outform DER -out dhparam.der 2048
12
13 @param dhparam The DH param DER encoded data
14 @param dhparamlen The length of dhparam data
15 @param key [out] Where the newly created DH key will be stored
16 @return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically.
17 */
dh_set_pg_dhparam(const unsigned char * dhparam,unsigned long dhparamlen,dh_key * key)18 int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key)
19 {
20 int err;
21
22 LTC_ARGCHK(key != NULL);
23 LTC_ARGCHK(ltc_mp.name != NULL);
24 LTC_ARGCHK(dhparam != NULL);
25 LTC_ARGCHK(dhparamlen > 0);
26
27 if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, LTC_NULL)) != CRYPT_OK) {
28 return err;
29 }
30 if ((err = der_decode_sequence_multi(dhparam, dhparamlen,
31 LTC_ASN1_INTEGER, 1UL, key->prime,
32 LTC_ASN1_INTEGER, 1UL, key->base,
33 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
34 goto LBL_ERR;
35 }
36
37 return CRYPT_OK;
38
39 LBL_ERR:
40 dh_free(key);
41 return err;
42 }
43
44 #endif /* LTC_MDH */
45