Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/dh/dh_set.c
4396 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
10
#include "tomcrypt.h"
11
12
#ifdef LTC_MDH
13
14
/**
15
Import DH key parts p and g from raw numbers
16
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
23
*/
24
int dh_set_pg(const unsigned char *p, unsigned long plen,
25
const unsigned char *g, unsigned long glen,
26
dh_key *key)
27
{
28
int err;
29
30
LTC_ARGCHK(key != NULL);
31
LTC_ARGCHK(p != NULL);
32
LTC_ARGCHK(g != NULL);
33
LTC_ARGCHK(ltc_mp.name != NULL);
34
35
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
36
return err;
37
}
38
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; }
41
42
return CRYPT_OK;
43
44
LBL_ERR:
45
dh_free(key);
46
return err;
47
}
48
49
/**
50
Import DH key parts p and g from built-in DH groups
51
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.
55
*/
56
int dh_set_pg_groupsize(int groupsize, dh_key *key)
57
{
58
int err, i;
59
60
LTC_ARGCHK(key != NULL);
61
LTC_ARGCHK(ltc_mp.name != NULL);
62
LTC_ARGCHK(groupsize > 0);
63
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;
66
67
if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
68
return err;
69
}
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; }
72
73
return CRYPT_OK;
74
75
LBL_ERR:
76
dh_free(key);
77
return err;
78
}
79
80
/**
81
Import DH public or private key part from raw numbers
82
83
NB: The p & g parts must be set beforehand
84
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
90
*/
91
int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key)
92
{
93
int err;
94
95
LTC_ARGCHK(key != NULL);
96
LTC_ARGCHK(ltc_mp.name != NULL);
97
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; }
102
}
103
else {
104
key->type = PK_PUBLIC;
105
if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
106
}
107
108
/* check public key */
109
if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
110
goto LBL_ERR;
111
}
112
113
return CRYPT_OK;
114
115
LBL_ERR:
116
dh_free(key);
117
return err;
118
}
119
120
#endif /* LTC_MDH */
121
122