1
10
13
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
155
156
157
161
162
163
164
165
166
167
168
169
170
171
178
179
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
269
273
274
275
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
300
301
302
303
304
305
308
309
310
311
312
316
317
318
319
320
321
322
323
324
325
326
...
...
...
#define NX_CRYPTO_SOURCE_CODE
#include "nx_crypto_dh.h"
#ifndef NX_CRYPTO_SELF_TEST
static const HN_UBASE _nx_dh_group_2_modulus[] =
{
HN_ULONG_TO_UBASE(0xFFFFFFFF), HN_ULONG_TO_UBASE(0xFFFFFFFF),
HN_ULONG_TO_UBASE(0xECE65381), HN_ULONG_TO_UBASE(0x49286651),
HN_ULONG_TO_UBASE(0x7C4B1FE6), HN_ULONG_TO_UBASE(0xAE9F2411),
HN_ULONG_TO_UBASE(0x5A899FA5), HN_ULONG_TO_UBASE(0xEE386BFB),
HN_ULONG_TO_UBASE(0xF406B7ED), HN_ULONG_TO_UBASE(0x0BFF5CB6),
HN_ULONG_TO_UBASE(0xA637ED6B), HN_ULONG_TO_UBASE(0xF44C42E9),
HN_ULONG_TO_UBASE(0x625E7EC6), HN_ULONG_TO_UBASE(0xE485B576),
HN_ULONG_TO_UBASE(0x6D51C245), HN_ULONG_TO_UBASE(0x4FE1356D),
HN_ULONG_TO_UBASE(0xF25F1437), HN_ULONG_TO_UBASE(0x302B0A6D),
HN_ULONG_TO_UBASE(0xCD3A431B), HN_ULONG_TO_UBASE(0xEF9519B3),
HN_ULONG_TO_UBASE(0x8E3404DD), HN_ULONG_TO_UBASE(0x514A0879),
HN_ULONG_TO_UBASE(0x3B139B22), HN_ULONG_TO_UBASE(0x020BBEA6),
HN_ULONG_TO_UBASE(0x8A67CC74), HN_ULONG_TO_UBASE(0x29024E08),
HN_ULONG_TO_UBASE(0x80DC1CD1), HN_ULONG_TO_UBASE(0xC4C6628B),
HN_ULONG_TO_UBASE(0x2168C234), HN_ULONG_TO_UBASE(0xC90FDAA2),
HN_ULONG_TO_UBASE(0xFFFFFFFF), HN_ULONG_TO_UBASE(0xFFFFFFFF)
...};
...
NX_CRYPTO_KEEP UINT _nx_crypto_dh_setup(NX_CRYPTO_DH *dh_ptr,
UCHAR *local_public_key_ptr,
UINT *local_public_key_len_ptr,
ULONG dh_group_num,
HN_UBASE *scratch_buf_ptr)
{
UINT i;
NX_CRYPTO_HUGE_NUMBER modulus, public_key, private_key, generator;
HN_UBASE generator_value;
NX_CRYPTO_STATE_CHECK
switch (dh_group_num)
{
case NX_CRYPTO_DH_GROUP_2:
dh_ptr -> nx_crypto_dh_key_size = NX_CRYPTO_DIFFIE_HELLMAN_GROUP_2_KEY_SIZE;
dh_ptr -> nx_crypto_dh_modulus = (HN_UBASE *)_nx_dh_group_2_modulus;
NX_CRYPTO_HUGE_NUMBER_INITIALIZE_DIGIT(&generator, &generator_value,
NX_CRYPTO_DH_GROUP_2_GENERATOR)
modulus.nx_crypto_huge_number_data = dh_ptr -> nx_crypto_dh_modulus;
modulus.nx_crypto_huge_number_size = dh_ptr -> nx_crypto_dh_key_size >> HN_SIZE_SHIFT;
modulus.nx_crypto_huge_buffer_size = dh_ptr -> nx_crypto_dh_key_size;
modulus.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;
break;
case NX_CRYPTO_DH_GROUP_2:
default:
return(NX_CRYPTO_NOT_SUCCESSFUL);default
}switch (dh_group_num) { ... }
/* ... */
NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&public_key, scratch_buf_ptr, dh_ptr -> nx_crypto_dh_key_size << 1);
private_key.nx_crypto_huge_number_data = dh_ptr -> nx_crypto_dh_private_key_buffer;
private_key.nx_crypto_huge_number_size = dh_ptr -> nx_crypto_dh_key_size >> HN_SIZE_SHIFT;
private_key.nx_crypto_huge_buffer_size = sizeof(dh_ptr -> nx_crypto_dh_private_key_buffer);
private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;
for (i = 0; i < private_key.nx_crypto_huge_number_size; i++)
{
/* ... */
dh_ptr -> nx_crypto_dh_private_key_buffer[i] = (HN_UBASE)((HN_UBASE)(NX_CRYPTO_RAND()) & HN_MASK);
}for (i = 0; i < private_key.nx_crypto_huge_number_size; i++) { ... }
/* ... */
_nx_crypto_huge_number_mont_power_modulus(&generator, &private_key,
&modulus, &public_key, scratch_buf_ptr);
_nx_crypto_huge_number_extract(&public_key, local_public_key_ptr,
dh_ptr -> nx_crypto_dh_key_size, local_public_key_len_ptr);
return(NX_CRYPTO_SUCCESS);
}{ ... }
...
...
NX_CRYPTO_KEEP UINT _nx_crypto_dh_compute_secret(NX_CRYPTO_DH *dh_ptr,
UCHAR *share_secret_key_ptr,
ULONG *share_secret_key_len_ptr,
UCHAR *remote_public_key,
ULONG remote_public_key_len,
HN_UBASE *scratch_buf_ptr)
{
UINT key_size;
NX_CRYPTO_HUGE_NUMBER modulus, public_key, private_key, shared_secret;
NX_CRYPTO_STATE_CHECK
/* ... */
if (0 == dh_ptr -> nx_crypto_dh_key_size)
{
return(NX_CRYPTO_NOT_SUCCESSFUL);
}if (0 == dh_ptr -> nx_crypto_dh_key_size) { ... }
if (remote_public_key_len > dh_ptr -> nx_crypto_dh_key_size)
{
return(NX_CRYPTO_NOT_SUCCESSFUL);
}if (remote_public_key_len > dh_ptr -> nx_crypto_dh_key_size) { ... }
key_size = dh_ptr -> nx_crypto_dh_key_size;
NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&public_key, scratch_buf_ptr, key_size);
NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&shared_secret, scratch_buf_ptr, key_size << 1);
_nx_crypto_huge_number_setup(&public_key, remote_public_key, remote_public_key_len);
modulus.nx_crypto_huge_number_data = (HN_UBASE *)dh_ptr -> nx_crypto_dh_modulus;
modulus.nx_crypto_huge_number_size = key_size >> HN_SIZE_SHIFT;
modulus.nx_crypto_huge_buffer_size = key_size;
modulus.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;
/* ... */
private_key.nx_crypto_huge_number_data = (HN_UBASE *)dh_ptr -> nx_crypto_dh_private_key_buffer;
private_key.nx_crypto_huge_number_size = key_size >> HN_SIZE_SHIFT;
private_key.nx_crypto_huge_buffer_size = key_size;
private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;
/* ... */
_nx_crypto_huge_number_mont_power_modulus(&public_key, &private_key, &modulus,
&shared_secret, scratch_buf_ptr);
if (*share_secret_key_len_ptr < key_size)
{
return(NX_CRYPTO_NOT_SUCCESSFUL);
}if (*share_secret_key_len_ptr < key_size) { ... }
_nx_crypto_huge_number_extract(&shared_secret, share_secret_key_ptr,
key_size, (UINT *)share_secret_key_len_ptr);
return(NX_CRYPTO_SUCCESS);
}{ ... }
...#endif/* ... */