line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/** |
2
|
|
|
|
|
|
|
* @file dh.c |
3
|
|
|
|
|
|
|
* @version 950bba4 (HEAD -> master) |
4
|
|
|
|
|
|
|
* |
5
|
|
|
|
|
|
|
* Diffie-Hellman. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
/* |
8
|
|
|
|
|
|
|
* Copyright (c) 2013-2017 INSIDE Secure Corporation |
9
|
|
|
|
|
|
|
* Copyright (c) PeerSec Networks, 2002-2011 |
10
|
|
|
|
|
|
|
* All Rights Reserved |
11
|
|
|
|
|
|
|
* |
12
|
|
|
|
|
|
|
* The latest version of this code is available at http://www.matrixssl.org |
13
|
|
|
|
|
|
|
* |
14
|
|
|
|
|
|
|
* This software is open source; you can redistribute it and/or modify |
15
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by |
16
|
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
17
|
|
|
|
|
|
|
* (at your option) any later version. |
18
|
|
|
|
|
|
|
* |
19
|
|
|
|
|
|
|
* This General Public License does NOT permit incorporating this software |
20
|
|
|
|
|
|
|
* into proprietary programs. If you are unable to comply with the GPL, a |
21
|
|
|
|
|
|
|
* commercial license for this software may be purchased from INSIDE at |
22
|
|
|
|
|
|
|
* http://www.insidesecure.com/ |
23
|
|
|
|
|
|
|
* |
24
|
|
|
|
|
|
|
* This program is distributed in WITHOUT ANY WARRANTY; without even the |
25
|
|
|
|
|
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
26
|
|
|
|
|
|
|
* See the GNU General Public License for more details. |
27
|
|
|
|
|
|
|
* |
28
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
29
|
|
|
|
|
|
|
* along with this program; if not, write to the Free Software |
30
|
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
31
|
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
32
|
|
|
|
|
|
|
*/ |
33
|
|
|
|
|
|
|
/******************************************************************************/ |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#include "../cryptoImpl.h" |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#ifdef USE_MATRIX_DH |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
/******************************************************************************/ |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
void psDhClearKey(psDhKey_t *key) |
42
|
|
|
|
|
|
|
{ |
43
|
0
|
0
|
|
|
|
|
psAssert(key); |
44
|
0
|
|
|
|
|
|
pstm_clear(&key->priv); |
45
|
0
|
|
|
|
|
|
pstm_clear(&key->pub); |
46
|
0
|
|
|
|
|
|
key->type = 0; |
47
|
0
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
psSize_t psDhSize(const psDhKey_t *key) |
50
|
|
|
|
|
|
|
{ |
51
|
0
|
|
|
|
|
|
return key->size; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
/******************************************************************************/ |
55
|
|
|
|
|
|
|
/** |
56
|
|
|
|
|
|
|
Parse ASN.1 encoded DH parameters. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
DHParameter ::= SEQUENCE { |
59
|
|
|
|
|
|
|
prime INTEGER, -- p |
60
|
|
|
|
|
|
|
base INTEGER, -- g |
61
|
|
|
|
|
|
|
privateValueLength INTEGER OPTIONAL |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
@note privateValueLength field unsupported |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
@param pool Memory pool |
66
|
|
|
|
|
|
|
@param[in] dhBin Pointer to buffer containing ASN.1 format parameters |
67
|
|
|
|
|
|
|
@param[in] dhBinLen Length in bytes of 'dhBin' |
68
|
|
|
|
|
|
|
@param[in,out] params Allocated parameter structure to receive parsed |
69
|
|
|
|
|
|
|
params. |
70
|
|
|
|
|
|
|
@return < on error. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
*/ |
73
|
0
|
|
|
|
|
|
int32_t psPkcs3ParseDhParamBin(psPool_t *pool, const unsigned char *dhBin, |
74
|
|
|
|
|
|
|
psSize_t dhBinLen, psDhParams_t *params) |
75
|
|
|
|
|
|
|
{ |
76
|
|
|
|
|
|
|
const unsigned char *c, *end; |
77
|
|
|
|
|
|
|
psSize_t baseLen; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (!params || !dhBin) |
|
|
0
|
|
|
|
|
|
80
|
|
|
|
|
|
|
{ |
81
|
0
|
|
|
|
|
|
return PS_ARG_FAIL; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
|
|
|
|
end = dhBin + dhBinLen; |
84
|
0
|
|
|
|
|
|
c = dhBin; |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if (getAsnSequence(&c, (uint16_t) (end - c), &baseLen) < 0) |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
|
|
|
|
|
return PS_PARSE_FAIL; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
/* Parse the DH prime value and validate against minimum length */ |
91
|
0
|
0
|
|
|
|
|
if (pstm_read_asn(pool, &c, (uint16_t) (end - c), ¶ms->p) < 0) |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
|
|
|
goto L_ERR; |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
|
params->size = pstm_unsigned_bin_size(¶ms->p); |
96
|
0
|
0
|
|
|
|
|
if (params->size < (MIN_DH_BITS / 8)) |
97
|
|
|
|
|
|
|
{ |
98
|
|
|
|
|
|
|
psTraceIntCrypto("Unsupported DH prime size %hu\n", params->size); |
99
|
0
|
|
|
|
|
|
goto L_ERR; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
/* The DH base parameter is typically small (usually value 2 or 5), |
102
|
|
|
|
|
|
|
so we don't validate against a minimum length */ |
103
|
0
|
0
|
|
|
|
|
if (pstm_read_asn(pool, &c, (uint16_t) (end - c), ¶ms->g) < 0) |
104
|
|
|
|
|
|
|
{ |
105
|
0
|
|
|
|
|
|
goto L_ERR; |
106
|
|
|
|
|
|
|
} |
107
|
0
|
0
|
|
|
|
|
if (end != c) |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
psTraceCrypto("Unsupported DHParameter Format\n"); |
110
|
0
|
|
|
|
|
|
goto L_ERR; |
111
|
|
|
|
|
|
|
} |
112
|
0
|
|
|
|
|
|
params->pool = pool; |
113
|
0
|
|
|
|
|
|
return PS_SUCCESS; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
L_ERR: |
116
|
0
|
|
|
|
|
|
pstm_clear(¶ms->g); |
117
|
0
|
|
|
|
|
|
pstm_clear(¶ms->p); |
118
|
0
|
|
|
|
|
|
params->pool = NULL; |
119
|
0
|
|
|
|
|
|
params->size = 0; |
120
|
0
|
|
|
|
|
|
return PS_PARSE_FAIL; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
/** |
124
|
|
|
|
|
|
|
Clear DH params. |
125
|
|
|
|
|
|
|
@param[out] params Pointer to allocated DH params to clear. |
126
|
|
|
|
|
|
|
@note Caller is responsible for freeing memory associated with 'params', |
127
|
|
|
|
|
|
|
if appropriate. |
128
|
|
|
|
|
|
|
*/ |
129
|
100808
|
|
|
|
|
|
void psPkcs3ClearDhParams(psDhParams_t *params) |
130
|
|
|
|
|
|
|
{ |
131
|
100808
|
50
|
|
|
|
|
if (params == NULL) |
132
|
|
|
|
|
|
|
{ |
133
|
0
|
|
|
|
|
|
return; |
134
|
|
|
|
|
|
|
} |
135
|
100808
|
|
|
|
|
|
pstm_clear(¶ms->g); |
136
|
100808
|
|
|
|
|
|
pstm_clear(¶ms->p); |
137
|
100808
|
|
|
|
|
|
params->size = 0; |
138
|
100808
|
|
|
|
|
|
params->pool = NULL; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
/** |
142
|
|
|
|
|
|
|
Allocate and populate buffers for DH prime and base values. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
@param pool Memory pool |
145
|
|
|
|
|
|
|
@param[in] params DH params to export |
146
|
|
|
|
|
|
|
@param[out] pp On success, will point to an allocated memory buffer |
147
|
|
|
|
|
|
|
containing the DH params prime value. |
148
|
|
|
|
|
|
|
@param[out] pLen Pointer to value to receive length of 'pp' in bytes |
149
|
|
|
|
|
|
|
@param[out] pg On success, will point to an allocated memory buffer |
150
|
|
|
|
|
|
|
containing the DH params generator/base value. |
151
|
|
|
|
|
|
|
@param[out] gLen Pointer to value to receive length of 'pg' in bytes |
152
|
|
|
|
|
|
|
@return < 0 on failure |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
@post On success, the buffers pointed to by 'pp' and 'pg' are allocated |
155
|
|
|
|
|
|
|
by this API and must be freed by the caller. |
156
|
|
|
|
|
|
|
*/ |
157
|
0
|
|
|
|
|
|
int32_t psDhExportParameters(psPool_t *pool, |
158
|
|
|
|
|
|
|
const psDhParams_t *params, |
159
|
|
|
|
|
|
|
unsigned char **pp, psSize_t *pLen, |
160
|
|
|
|
|
|
|
unsigned char **pg, psSize_t *gLen) |
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
uint16_t pl, gl; |
163
|
|
|
|
|
|
|
unsigned char *p, *g; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
pl = pstm_unsigned_bin_size(¶ms->p); |
166
|
0
|
|
|
|
|
|
gl = pstm_unsigned_bin_size(¶ms->g); |
167
|
0
|
0
|
|
|
|
|
if ((p = psMalloc(pool, pl)) == NULL) |
168
|
|
|
|
|
|
|
{ |
169
|
0
|
|
|
|
|
|
psError("Memory allocation error in psDhExportParameters\n"); |
170
|
0
|
|
|
|
|
|
return PS_MEM_FAIL; |
171
|
|
|
|
|
|
|
} |
172
|
0
|
0
|
|
|
|
|
if ((g = psMalloc(pool, gl)) == NULL) |
173
|
|
|
|
|
|
|
{ |
174
|
0
|
|
|
|
|
|
psError("Memory allocation error in psDhExportParameters\n"); |
175
|
0
|
|
|
|
|
|
psFree(p, pool); |
176
|
0
|
|
|
|
|
|
return PS_MEM_FAIL; |
177
|
|
|
|
|
|
|
} |
178
|
0
|
|
|
|
|
|
if (pstm_to_unsigned_bin(pool, ¶ms->p, p) < 0 || |
179
|
0
|
|
|
|
|
|
pstm_to_unsigned_bin(pool, ¶ms->g, g) < 0) |
180
|
|
|
|
|
|
|
{ |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
psFree(p, pool); |
183
|
0
|
|
|
|
|
|
psFree(g, pool); |
184
|
0
|
|
|
|
|
|
return PS_FAIL; |
185
|
|
|
|
|
|
|
} |
186
|
0
|
|
|
|
|
|
*pLen = pl; |
187
|
0
|
|
|
|
|
|
*gLen = gl; |
188
|
0
|
|
|
|
|
|
*pp = p; |
189
|
0
|
|
|
|
|
|
*pg = g; |
190
|
0
|
|
|
|
|
|
return PS_SUCCESS; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
/******************************************************************************/ |
194
|
|
|
|
|
|
|
/** |
195
|
|
|
|
|
|
|
Import a public DH key in raw (wire) format to a psDhKey_t struct. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
@param pool Memory pool |
198
|
|
|
|
|
|
|
@param[in] in Pointer to buffer containing raw public DH key |
199
|
|
|
|
|
|
|
@param[in] inlen Length in bytes of 'in' |
200
|
|
|
|
|
|
|
@param[out] key Pointer to allocated key to be initialized with raw |
201
|
|
|
|
|
|
|
DH value from 'in'. |
202
|
|
|
|
|
|
|
@return < on failure |
203
|
|
|
|
|
|
|
*/ |
204
|
0
|
|
|
|
|
|
int32_t psDhImportPubKey(psPool_t *pool, |
205
|
|
|
|
|
|
|
const unsigned char *in, psSize_t inlen, |
206
|
|
|
|
|
|
|
psDhKey_t *key) |
207
|
|
|
|
|
|
|
{ |
208
|
|
|
|
|
|
|
int32_t rc; |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
memset(&key->priv, 0, sizeof(key->priv)); |
211
|
0
|
0
|
|
|
|
|
if ((rc = pstm_init_for_read_unsigned_bin(pool, &key->pub, inlen)) < 0) |
212
|
|
|
|
|
|
|
{ |
213
|
0
|
|
|
|
|
|
return rc; |
214
|
|
|
|
|
|
|
} |
215
|
0
|
0
|
|
|
|
|
if ((rc = pstm_read_unsigned_bin(&key->pub, in, inlen)) < 0) |
216
|
|
|
|
|
|
|
{ |
217
|
0
|
|
|
|
|
|
pstm_clear(&key->pub); |
218
|
0
|
|
|
|
|
|
return rc; |
219
|
|
|
|
|
|
|
} |
220
|
0
|
|
|
|
|
|
key->size = inlen; |
221
|
0
|
|
|
|
|
|
key->type = PS_PUBKEY; |
222
|
0
|
|
|
|
|
|
return PS_SUCCESS; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
/** |
226
|
|
|
|
|
|
|
Export a public psDhKey_t struct to a raw binary format. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
@param pool Memory pool |
229
|
|
|
|
|
|
|
@param[in] key Pointer to DH key to export |
230
|
|
|
|
|
|
|
@param[out] out Pointer to buffer to write raw public DH key |
231
|
|
|
|
|
|
|
@param[in,out] outlen On input, the number of bytes available in 'out', |
232
|
|
|
|
|
|
|
on successful return, the number of bytes written to 'out'. |
233
|
|
|
|
|
|
|
@return < on failure |
234
|
|
|
|
|
|
|
*/ |
235
|
0
|
|
|
|
|
|
int32_t psDhExportPubKey(psPool_t *pool, const psDhKey_t *key, |
236
|
|
|
|
|
|
|
unsigned char *out, psSize_t *outlen) |
237
|
|
|
|
|
|
|
{ |
238
|
|
|
|
|
|
|
unsigned char *c; |
239
|
|
|
|
|
|
|
int16_t pad; |
240
|
|
|
|
|
|
|
int32_t rc; |
241
|
|
|
|
|
|
|
|
242
|
0
|
0
|
|
|
|
|
if (*outlen < key->size) |
243
|
|
|
|
|
|
|
{ |
244
|
0
|
|
|
|
|
|
return PS_ARG_FAIL; |
245
|
|
|
|
|
|
|
} |
246
|
0
|
|
|
|
|
|
c = out; |
247
|
0
|
|
|
|
|
|
pad = key->size - pstm_unsigned_bin_size(&key->pub); |
248
|
0
|
0
|
|
|
|
|
if (pad > 0) |
249
|
|
|
|
|
|
|
{ |
250
|
0
|
|
|
|
|
|
memset(c, 0x0, pad); |
251
|
0
|
|
|
|
|
|
c += pad; |
252
|
|
|
|
|
|
|
} |
253
|
0
|
0
|
|
|
|
|
else if (pad < 0) |
254
|
|
|
|
|
|
|
{ |
255
|
0
|
|
|
|
|
|
return PS_FAIL; |
256
|
|
|
|
|
|
|
} |
257
|
0
|
0
|
|
|
|
|
if ((rc = pstm_to_unsigned_bin(pool, &key->pub, c)) < 0) |
258
|
|
|
|
|
|
|
{ |
259
|
0
|
|
|
|
|
|
return rc; |
260
|
|
|
|
|
|
|
} |
261
|
0
|
|
|
|
|
|
*outlen = key->size; |
262
|
0
|
|
|
|
|
|
return PS_SUCCESS; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
/******************************************************************************/ |
267
|
|
|
|
|
|
|
/** |
268
|
|
|
|
|
|
|
Generate a DH key given the parameters. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
*/ |
271
|
0
|
|
|
|
|
|
int32_t psDhGenKey(psPool_t *pool, psSize_t keysize, |
272
|
|
|
|
|
|
|
const unsigned char *pBin, psSize_t pLen, |
273
|
|
|
|
|
|
|
const unsigned char *gBin, psSize_t gLen, |
274
|
|
|
|
|
|
|
psDhKey_t *key, void *usrData) |
275
|
|
|
|
|
|
|
{ |
276
|
|
|
|
|
|
|
int32_t rc; |
277
|
|
|
|
|
|
|
pstm_int p, g; |
278
|
|
|
|
|
|
|
|
279
|
0
|
0
|
|
|
|
|
if (keysize > pLen) |
280
|
|
|
|
|
|
|
{ |
281
|
|
|
|
|
|
|
psTraceCrypto("psDhGenKey: keysize > pLen\n"); |
282
|
0
|
|
|
|
|
|
return PS_FAIL; |
283
|
|
|
|
|
|
|
} |
284
|
0
|
0
|
|
|
|
|
switch (pLen) |
285
|
|
|
|
|
|
|
{ |
286
|
|
|
|
|
|
|
case 128: |
287
|
|
|
|
|
|
|
case 192: |
288
|
|
|
|
|
|
|
case 256: |
289
|
|
|
|
|
|
|
case 384: |
290
|
|
|
|
|
|
|
case 512: |
291
|
0
|
|
|
|
|
|
break; |
292
|
|
|
|
|
|
|
default: |
293
|
|
|
|
|
|
|
psTraceCrypto("psDhGenKey: invalid keysize\n"); |
294
|
0
|
|
|
|
|
|
return PS_FAIL; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
/* Convert the p and g into ints and make keys */ |
297
|
0
|
0
|
|
|
|
|
if ((rc = pstm_init_for_read_unsigned_bin(pool, &p, pLen)) != PS_SUCCESS) |
298
|
|
|
|
|
|
|
{ |
299
|
0
|
|
|
|
|
|
return rc; |
300
|
|
|
|
|
|
|
} |
301
|
0
|
0
|
|
|
|
|
if ((rc = pstm_init_for_read_unsigned_bin(pool, &g, gLen)) != PS_SUCCESS) |
302
|
|
|
|
|
|
|
{ |
303
|
0
|
|
|
|
|
|
pstm_clear(&p); |
304
|
0
|
|
|
|
|
|
return rc; |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
|
307
|
0
|
0
|
|
|
|
|
if ((rc = pstm_read_unsigned_bin(&p, pBin, pLen)) != PS_SUCCESS) |
308
|
|
|
|
|
|
|
{ |
309
|
0
|
|
|
|
|
|
goto error; |
310
|
|
|
|
|
|
|
} |
311
|
0
|
0
|
|
|
|
|
if ((rc = pstm_read_unsigned_bin(&g, gBin, gLen)) != PS_SUCCESS) |
312
|
|
|
|
|
|
|
{ |
313
|
0
|
|
|
|
|
|
goto error; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
0
|
|
|
|
|
|
rc = psDhGenKeyInts(pool, keysize, &p, &g, key, usrData); |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
error: |
319
|
0
|
|
|
|
|
|
pstm_clear(&p); |
320
|
0
|
|
|
|
|
|
pstm_clear(&g); |
321
|
0
|
|
|
|
|
|
return rc; |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
/******************************************************************************/ |
325
|
|
|
|
|
|
|
/** |
326
|
|
|
|
|
|
|
Does the actual key generation given p and g. |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
*/ |
329
|
|
|
|
|
|
|
# define DH_KEYGEN_SANITY 256 |
330
|
0
|
|
|
|
|
|
int32_t psDhGenKeyInts(psPool_t *pool, psSize_t keysize, |
331
|
|
|
|
|
|
|
const pstm_int *p, const pstm_int *g, |
332
|
|
|
|
|
|
|
psDhKey_t *key, void *usrData) |
333
|
|
|
|
|
|
|
{ |
334
|
0
|
|
|
|
|
|
unsigned char *buf = NULL; |
335
|
|
|
|
|
|
|
int32_t err, i; |
336
|
|
|
|
|
|
|
psSize_t privsize; |
337
|
|
|
|
|
|
|
|
338
|
0
|
0
|
|
|
|
|
if (key == NULL) |
339
|
|
|
|
|
|
|
{ |
340
|
0
|
|
|
|
|
|
return PS_ARG_FAIL; |
341
|
|
|
|
|
|
|
} |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
/* Detect parameters with too small g. */ |
344
|
0
|
0
|
|
|
|
|
if (pstm_count_bits(g) < 2) |
345
|
|
|
|
|
|
|
{ |
346
|
0
|
|
|
|
|
|
return PS_ARG_FAIL; |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
0
|
|
|
|
|
|
privsize = keysize; |
350
|
|
|
|
|
|
|
# ifndef USE_LARGE_DH_PRIVATE_KEYS |
351
|
|
|
|
|
|
|
/* |
352
|
|
|
|
|
|
|
The mapping between DH prime field size and key size follows |
353
|
|
|
|
|
|
|
NIST SP 800-57 Part 1 for the common sizes, and for unusual |
354
|
|
|
|
|
|
|
sizes the key size is intentionally rounded up. |
355
|
|
|
|
|
|
|
The private key size must never be larger than prime size. |
356
|
|
|
|
|
|
|
*/ |
357
|
0
|
0
|
|
|
|
|
if (keysize >= 160 / 8 && keysize <= 1024 / 8) |
|
|
0
|
|
|
|
|
|
358
|
|
|
|
|
|
|
{ |
359
|
0
|
|
|
|
|
|
privsize = 160 / 8; |
360
|
|
|
|
|
|
|
} |
361
|
0
|
0
|
|
|
|
|
else if (keysize > 1024 / 8 && keysize <= 2048 / 8) |
|
|
0
|
|
|
|
|
|
362
|
|
|
|
|
|
|
{ |
363
|
0
|
|
|
|
|
|
privsize = 224 / 8; |
364
|
|
|
|
|
|
|
} |
365
|
0
|
0
|
|
|
|
|
else if (keysize > 2048 / 8 && keysize <= 3072 / 8) |
|
|
0
|
|
|
|
|
|
366
|
|
|
|
|
|
|
{ |
367
|
0
|
|
|
|
|
|
privsize = 256 / 8; |
368
|
|
|
|
|
|
|
} |
369
|
0
|
0
|
|
|
|
|
else if (keysize > 3072 / 8 && keysize <= 7680 / 8) |
|
|
0
|
|
|
|
|
|
370
|
|
|
|
|
|
|
{ |
371
|
0
|
|
|
|
|
|
privsize = 384 / 8; |
372
|
|
|
|
|
|
|
} |
373
|
0
|
0
|
|
|
|
|
else if (keysize > 7680 / 8 && keysize <= 15360 / 8) |
|
|
0
|
|
|
|
|
|
374
|
|
|
|
|
|
|
{ |
375
|
0
|
|
|
|
|
|
privsize = 256 / 8; |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
# endif /* USE_LARGE_DH_PRIVATE_KEYS */ |
378
|
|
|
|
|
|
|
|
379
|
0
|
|
|
|
|
|
key->size = keysize; |
380
|
|
|
|
|
|
|
|
381
|
0
|
|
|
|
|
|
buf = psMalloc(pool, privsize); |
382
|
0
|
0
|
|
|
|
|
if (buf == NULL) |
383
|
|
|
|
|
|
|
{ |
384
|
0
|
|
|
|
|
|
psError("malloc error in psDhMakeKey\n"); |
385
|
0
|
|
|
|
|
|
return PS_MEM_FAIL; |
386
|
|
|
|
|
|
|
} |
387
|
0
|
0
|
|
|
|
|
if ((err = pstm_init_for_read_unsigned_bin(pool, &key->priv, privsize)) |
388
|
|
|
|
|
|
|
!= PS_SUCCESS) |
389
|
|
|
|
|
|
|
{ |
390
|
0
|
|
|
|
|
|
goto error; |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
|
393
|
0
|
0
|
|
|
|
|
for (i = 0; i < DH_KEYGEN_SANITY; i++) |
394
|
|
|
|
|
|
|
{ |
395
|
0
|
0
|
|
|
|
|
if ((err = psGetPrngLocked(buf, privsize, usrData)) < 0) |
396
|
|
|
|
|
|
|
{ |
397
|
0
|
|
|
|
|
|
goto error; |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
/* Load the random bytes as the private key */ |
400
|
0
|
0
|
|
|
|
|
if ((err = pstm_read_unsigned_bin(&key->priv, buf, privsize)) |
401
|
|
|
|
|
|
|
!= PS_SUCCESS) |
402
|
|
|
|
|
|
|
{ |
403
|
0
|
|
|
|
|
|
goto error; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
/* Test (1 < key < p), usually succeeds right away */ |
406
|
0
|
|
|
|
|
|
if (pstm_cmp_d(&key->priv, 1) == PSTM_GT && |
407
|
0
|
|
|
|
|
|
pstm_cmp(&key->priv, p) == PSTM_LT) |
408
|
|
|
|
|
|
|
{ |
409
|
0
|
|
|
|
|
|
break; /* found one */ |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
} |
412
|
0
|
0
|
|
|
|
|
if (i == DH_KEYGEN_SANITY) |
413
|
|
|
|
|
|
|
{ |
414
|
|
|
|
|
|
|
psTraceCrypto("DH private key could not be generated\n"); |
415
|
0
|
|
|
|
|
|
err = PS_PLATFORM_FAIL; |
416
|
0
|
|
|
|
|
|
goto error; |
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
/* Have the private key, now calculate the public part */ |
419
|
0
|
0
|
|
|
|
|
if ((err = pstm_init_size(pool, &key->pub, (p->used * 2) + 1)) |
420
|
|
|
|
|
|
|
!= PS_SUCCESS) |
421
|
|
|
|
|
|
|
{ |
422
|
0
|
|
|
|
|
|
pstm_clear(&key->priv); |
423
|
0
|
|
|
|
|
|
goto error; |
424
|
|
|
|
|
|
|
} |
425
|
0
|
0
|
|
|
|
|
if ((err = pstm_exptmod(pool, g, &key->priv, p, &key->pub)) != |
426
|
|
|
|
|
|
|
PS_SUCCESS) |
427
|
|
|
|
|
|
|
{ |
428
|
0
|
|
|
|
|
|
goto error; |
429
|
|
|
|
|
|
|
} |
430
|
0
|
|
|
|
|
|
key->type = PS_PRIVKEY; |
431
|
0
|
|
|
|
|
|
err = PS_SUCCESS; |
432
|
0
|
|
|
|
|
|
goto done; |
433
|
|
|
|
|
|
|
error: |
434
|
0
|
|
|
|
|
|
pstm_clear(&key->priv); |
435
|
0
|
|
|
|
|
|
pstm_clear(&key->pub); |
436
|
|
|
|
|
|
|
done: |
437
|
0
|
0
|
|
|
|
|
if (buf) |
438
|
|
|
|
|
|
|
{ |
439
|
0
|
|
|
|
|
|
memzero_s(buf, privsize); |
440
|
0
|
|
|
|
|
|
psFree(buf, pool); |
441
|
|
|
|
|
|
|
} |
442
|
0
|
|
|
|
|
|
return err; |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
/******************************************************************************/ |
446
|
|
|
|
|
|
|
/** |
447
|
|
|
|
|
|
|
Create the DH premaster secret. |
448
|
|
|
|
|
|
|
@param[in] privKey The private DH key in the pair |
449
|
|
|
|
|
|
|
@param[in] pubKey The public DH key in the pair |
450
|
|
|
|
|
|
|
@param[in] pBin The DH Param Prime value |
451
|
|
|
|
|
|
|
@param[in] pBinLen The length in bytes if 'pBin' |
452
|
|
|
|
|
|
|
@param[out] out Buffer to write the shared secret |
453
|
|
|
|
|
|
|
@param[in,out] outlen On input, the available space in 'out', on |
454
|
|
|
|
|
|
|
successful return, the number of bytes written to 'out'. |
455
|
|
|
|
|
|
|
*/ |
456
|
0
|
|
|
|
|
|
int32_t psDhGenSharedSecret(psPool_t *pool, |
457
|
|
|
|
|
|
|
const psDhKey_t *privKey, const psDhKey_t *pubKey, |
458
|
|
|
|
|
|
|
const unsigned char *pBin, psSize_t pBinLen, |
459
|
|
|
|
|
|
|
unsigned char *out, psSize_t *outlen, void *usrData) |
460
|
|
|
|
|
|
|
{ |
461
|
|
|
|
|
|
|
pstm_int tmp, p; |
462
|
|
|
|
|
|
|
uint16_t x; |
463
|
|
|
|
|
|
|
int32_t err; |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
/* Verify the privKey is a private type. pubKey param can be either */ |
466
|
0
|
0
|
|
|
|
|
if (privKey->type != PS_PRIVKEY) |
467
|
|
|
|
|
|
|
{ |
468
|
|
|
|
|
|
|
psTraceCrypto("Bad private key format for DH premaster\n"); |
469
|
0
|
|
|
|
|
|
return PS_ARG_FAIL; |
470
|
|
|
|
|
|
|
} |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
/* compute y^x mod p */ |
473
|
0
|
0
|
|
|
|
|
if ((err = pstm_init(pool, &tmp)) != PS_SUCCESS) |
474
|
|
|
|
|
|
|
{ |
475
|
0
|
|
|
|
|
|
return err; |
476
|
|
|
|
|
|
|
} |
477
|
0
|
0
|
|
|
|
|
if ((err = pstm_init_for_read_unsigned_bin(pool, &p, pBinLen)) != PS_SUCCESS) |
478
|
|
|
|
|
|
|
{ |
479
|
0
|
|
|
|
|
|
return err; |
480
|
|
|
|
|
|
|
} |
481
|
|
|
|
|
|
|
|
482
|
0
|
0
|
|
|
|
|
if ((err = pstm_read_unsigned_bin(&p, pBin, pBinLen)) != PS_SUCCESS) |
483
|
|
|
|
|
|
|
{ |
484
|
0
|
|
|
|
|
|
goto error; |
485
|
|
|
|
|
|
|
} |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
/* Check key->pub is within correct range 2 <= pub < p - 1. */ |
488
|
0
|
0
|
|
|
|
|
if (pstm_count_bits(&pubKey->pub) < 2) |
489
|
|
|
|
|
|
|
{ |
490
|
0
|
|
|
|
|
|
err = PS_FAILURE; |
491
|
0
|
|
|
|
|
|
goto error; |
492
|
|
|
|
|
|
|
} |
493
|
0
|
0
|
|
|
|
|
if ((err = pstm_add_d(pool, &pubKey->pub, 1, &tmp)) != PSTM_OKAY) |
494
|
|
|
|
|
|
|
{ |
495
|
0
|
|
|
|
|
|
goto error; |
496
|
|
|
|
|
|
|
} |
497
|
0
|
0
|
|
|
|
|
if (pstm_cmp(&p, &tmp) != PSTM_GT) |
498
|
|
|
|
|
|
|
{ |
499
|
0
|
|
|
|
|
|
err = PS_FAILURE; |
500
|
0
|
|
|
|
|
|
goto error; |
501
|
|
|
|
|
|
|
} |
502
|
|
|
|
|
|
|
|
503
|
0
|
0
|
|
|
|
|
if ((err = pstm_exptmod(pool, &pubKey->pub, &privKey->priv, &p, |
504
|
|
|
|
|
|
|
&tmp)) != PS_SUCCESS) |
505
|
|
|
|
|
|
|
{ |
506
|
0
|
|
|
|
|
|
goto error; |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
/* enough space for output? */ |
510
|
0
|
|
|
|
|
|
x = (unsigned long) pstm_unsigned_bin_size(&tmp); |
511
|
0
|
0
|
|
|
|
|
if (*outlen < x) |
512
|
|
|
|
|
|
|
{ |
513
|
|
|
|
|
|
|
psTraceCrypto("Overflow in DH premaster generation\n"); |
514
|
0
|
|
|
|
|
|
err = PS_LIMIT_FAIL; |
515
|
0
|
|
|
|
|
|
goto error; |
516
|
|
|
|
|
|
|
} |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
/* It is possible to have a key size smaller than we expect */ |
519
|
0
|
|
|
|
|
|
*outlen = x; |
520
|
0
|
0
|
|
|
|
|
if ((err = pstm_to_unsigned_bin(pool, &tmp, out)) < 0) |
521
|
|
|
|
|
|
|
{ |
522
|
0
|
|
|
|
|
|
goto error; |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
|
525
|
0
|
|
|
|
|
|
err = PS_SUCCESS; |
526
|
|
|
|
|
|
|
error: |
527
|
0
|
|
|
|
|
|
pstm_clear(&p); |
528
|
0
|
|
|
|
|
|
pstm_clear(&tmp); |
529
|
0
|
|
|
|
|
|
return err; |
530
|
|
|
|
|
|
|
} |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
#endif /* USE_MATRIX_DH */ |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
/******************************************************************************/ |
535
|
|
|
|
|
|
|
|