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