1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
15 Import DH key parts p and g from raw numbers
17 @param p DH's p (prime)
18 @param plen DH's p's length
19 @param g DH's g (group)
20 @param glen DH's g's length
21 @param key [out] the destination for the imported key
22 @return CRYPT_OK if successful
24 int dh_set_pg(const unsigned char *p, unsigned long plen,
25 const unsigned char *g, unsigned long glen,
30 LTC_ARGCHK(key != NULL);
31 LTC_ARGCHK(p != NULL);
32 LTC_ARGCHK(g != NULL);
33 LTC_ARGCHK(ltc_mp.name != NULL);
35 if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
39 if ((err = mp_read_unsigned_bin(key->base, (unsigned char*)g, glen)) != CRYPT_OK) { goto LBL_ERR; }
40 if ((err = mp_read_unsigned_bin(key->prime, (unsigned char*)p, plen)) != CRYPT_OK) { goto LBL_ERR; }
50 Import DH key parts p and g from built-in DH groups
52 @param groupsize The size of the DH group to use
53 @param key [out] Where the newly created DH key will be stored
54 @return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically.
56 int dh_set_pg_groupsize(int groupsize, dh_key *key)
60 LTC_ARGCHK(key != NULL);
61 LTC_ARGCHK(ltc_mp.name != NULL);
62 LTC_ARGCHK(groupsize > 0);
64 for (i = 0; (groupsize > ltc_dh_sets[i].size) && (ltc_dh_sets[i].size != 0); i++);
65 if (ltc_dh_sets[i].size == 0) return CRYPT_INVALID_KEYSIZE;
67 if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
70 if ((err = mp_read_radix(key->base, ltc_dh_sets[i].base, 16)) != CRYPT_OK) { goto LBL_ERR; }
71 if ((err = mp_read_radix(key->prime, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto LBL_ERR; }
81 Import DH public or private key part from raw numbers
83 NB: The p & g parts must be set beforehand
85 @param in The key-part to import, either public or private.
86 @param inlen The key-part's length
87 @param type Which type of key (PK_PRIVATE or PK_PUBLIC)
88 @param key [out] the destination for the imported key
89 @return CRYPT_OK if successful
91 int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key)
95 LTC_ARGCHK(key != NULL);
96 LTC_ARGCHK(ltc_mp.name != NULL);
98 if (type == PK_PRIVATE) {
99 key->type = PK_PRIVATE;
100 if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
101 if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; }
104 key->type = PK_PUBLIC;
105 if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
108 /* check public key */
109 if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
122 /* ref: $Format:%D$ */
123 /* git commit: $Format:%H$ */
124 /* commit time: $Format:%ai$ */