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 raw numbers
17 
18   @param p       DH's p (prime)
19   @param plen    DH's p's length
20   @param g       DH's g (group)
21   @param glen    DH's g's length
22   @param key     [out] the destination for the imported key
23   @return CRYPT_OK if successful
24 */
dh_set_pg(const unsigned char * p,unsigned long plen,const unsigned char * g,unsigned long glen,dh_key * key)25 int dh_set_pg(const unsigned char *p, unsigned long plen,
26               const unsigned char *g, unsigned long glen,
27               dh_key *key)
28 {
29    int err;
30 
31    LTC_ARGCHK(key         != NULL);
32    LTC_ARGCHK(p           != NULL);
33    LTC_ARGCHK(g           != NULL);
34    LTC_ARGCHK(ltc_mp.name != NULL);
35 
36    if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
37       return err;
38    }
39 
40    if ((err = mp_read_unsigned_bin(key->base, (unsigned char*)g, glen)) != CRYPT_OK)     { goto LBL_ERR; }
41    if ((err = mp_read_unsigned_bin(key->prime, (unsigned char*)p, plen)) != CRYPT_OK)  { goto LBL_ERR; }
42 
43    return CRYPT_OK;
44 
45 LBL_ERR:
46    dh_free(key);
47    return err;
48 }
49 
50 /**
51   Import DH key parts p and g from built-in DH groups
52 
53   @param groupsize  The size of the DH group to use
54   @param key        [out] Where the newly created DH key will be stored
55   @return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically.
56 */
dh_set_pg_groupsize(int groupsize,dh_key * key)57 int dh_set_pg_groupsize(int groupsize, dh_key *key)
58 {
59    int err, i;
60 
61    LTC_ARGCHK(key         != NULL);
62    LTC_ARGCHK(ltc_mp.name != NULL);
63    LTC_ARGCHK(groupsize   > 0);
64 
65    for (i = 0; (groupsize > ltc_dh_sets[i].size) && (ltc_dh_sets[i].size != 0); i++);
66    if (ltc_dh_sets[i].size == 0) return CRYPT_INVALID_KEYSIZE;
67 
68    if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
69       return err;
70    }
71    if ((err = mp_read_radix(key->base, ltc_dh_sets[i].base, 16)) != CRYPT_OK)  { goto LBL_ERR; }
72    if ((err = mp_read_radix(key->prime, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto LBL_ERR; }
73 
74    return CRYPT_OK;
75 
76 LBL_ERR:
77    dh_free(key);
78    return err;
79 }
80 
81 /**
82   Import DH public or private key part from raw numbers
83 
84      NB: The p & g parts must be set beforehand
85 
86   @param in      The key-part to import, either public or private.
87   @param inlen   The key-part's length
88   @param type    Which type of key (PK_PRIVATE or PK_PUBLIC)
89   @param key     [out] the destination for the imported key
90   @return CRYPT_OK if successful
91 */
dh_set_key(const unsigned char * in,unsigned long inlen,int type,dh_key * key)92 int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key)
93 {
94    int err;
95 
96    LTC_ARGCHK(key         != NULL);
97    LTC_ARGCHK(ltc_mp.name != NULL);
98 
99    if (type == PK_PRIVATE) {
100       key->type = PK_PRIVATE;
101       if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
102       if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK)       { goto LBL_ERR; }
103    }
104    else {
105       key->type = PK_PUBLIC;
106       if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
107    }
108 
109    /* check public key */
110    if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
111       goto LBL_ERR;
112    }
113 
114    return CRYPT_OK;
115 
116 LBL_ERR:
117    dh_free(key);
118    return err;
119 }
120 
121 #endif /* LTC_MDH */
122 
123 /* ref:         $Format:%D$ */
124 /* git commit:  $Format:%H$ */
125 /* commit time: $Format:%ai$ */
126