-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrypttools.c
More file actions
348 lines (278 loc) · 7.37 KB
/
crypttools.c
File metadata and controls
348 lines (278 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "crypttools.h"
#include "ciplustools.h"
#include <string.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
{
/* If the fields p and g in d are NULL, the corresponding input
* parameters MUST be non-NULL. q may remain NULL.
*/
if ((dh->p == NULL && p == NULL)
|| (dh->g == NULL && g == NULL))
return 0;
if (p != NULL) {
BN_free(dh->p);
dh->p = p;
}
if (q != NULL) {
BN_free(dh->q);
dh->q = q;
}
if (g != NULL) {
BN_free(dh->g);
dh->g = g;
}
if (q != NULL) {
dh->length = BN_num_bits(q);
}
return 1;
}
void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
{
if (pub_key != NULL)
*pub_key = dh->pub_key;
if (priv_key != NULL)
*priv_key = dh->priv_key;
}
void DH_set_flags(DH *dh, int flags)
{
dh->flags |= flags;
}
#endif
int verify_cb(int ok, X509_STORE_CTX *ctx)
{
if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_CERT_NOT_YET_VALID) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
if (t->tm_year < 2015)
return 1;
}
if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_CERT_HAS_EXPIRED)
return 1;
return 0;
}
static int pkcs_1_mgf1(const uint8_t *seed, unsigned long seedlen, uint8_t *mask, unsigned long masklen)
{
unsigned long hLen, x;
uint32_t counter;
uint8_t *buf;
/* get hash output size */
hLen = 20; /* SHA1 */
/* allocate memory */
buf = (uint8_t *)malloc(hLen);
if (buf == NULL) {
return -1;
}
/* start counter */
counter = 0;
while (masklen > 0) {
/* handle counter */
BYTE32(buf, counter);
++counter;
/* get hash of seed || counter */
unsigned char buffer[0x18];
memcpy(buffer, seed, seedlen);
memcpy(buffer + 0x14, buf, 4);
SHA1(buffer, 0x18, buf);
/* store it */
for (x = 0; x < hLen && masklen > 0; x++, masklen--)
*mask++ = buf[x];
}
free(buf);
return 0;
}
static int pkcs_1_pss_encode(const uint8_t *msghash, unsigned int msghashlen,
unsigned long saltlen, unsigned long modulus_bitlen,
uint8_t *out, unsigned int outlen)
{
unsigned char *DB, *mask, *salt, *hash;
unsigned long x, y, hLen, modulus_len;
int err = -1;
unsigned char *hashbuf;
unsigned int hashbuflen;
hLen = 20; /* SHA1 */
modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
/* allocate ram for DB/mask/salt/hash of size modulus_len */
DB = (unsigned char *)malloc(modulus_len);
mask = (unsigned char *)malloc(modulus_len);
salt = (unsigned char *)malloc(modulus_len);
hash = (unsigned char *)malloc(modulus_len);
hashbuflen = 8 + msghashlen + saltlen;
hashbuf = (unsigned char *)malloc(hashbuflen);
if (!(DB && mask && salt && hash && hashbuf)) {
goto LBL_ERR;
}
/* generate random salt */
if (saltlen > 0) {
if (get_random(salt, saltlen) != (long)saltlen) {
goto LBL_ERR;
}
}
/* M = (eight) 0x00 || msghash || salt, hash = H(M) */
memset(hashbuf, 0, 8);
memcpy(hashbuf + 8, msghash, msghashlen);
memcpy(hashbuf + 8 + msghashlen, salt, saltlen);
SHA1(hashbuf, hashbuflen, hash);
/* generate DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */
x = 0;
memset(DB + x, 0, modulus_len - saltlen - hLen - 2);
x += modulus_len - saltlen - hLen - 2;
DB[x++] = 0x01;
memcpy(DB + x, salt, saltlen);
x += saltlen;
err = pkcs_1_mgf1(hash, hLen, mask, modulus_len - hLen - 1);
if (err)
goto LBL_ERR;
/* xor against DB */
for (y = 0; y < (modulus_len - hLen - 1); y++)
DB[y] ^= mask[y];
/* output is DB || hash || 0xBC */
if (outlen < modulus_len) {
err = -1;
goto LBL_ERR;
}
/* DB len = modulus_len - hLen - 1 */
y = 0;
memcpy(out + y, DB, modulus_len - hLen - 1);
y += modulus_len - hLen - 1;
/* hash */
memcpy(out + y, hash, hLen);
y += hLen;
/* 0xBC */
out[y] = 0xBC;
/* now clear the 8*modulus_len - modulus_bitlen most significant bits */
out[0] &= 0xFF >> ((modulus_len << 3) - (modulus_bitlen - 1));
err = 0;
LBL_ERR:
free(hashbuf);
free(hash);
free(salt);
free(mask);
free(DB);
return err;
}
/* DH */
int dh_gen_exp(uint8_t *dest, int dest_len, uint8_t *dh_g, int dh_g_len, uint8_t *dh_p, int dh_p_len)
{
DH *dh;
BIGNUM *p, *g;
const BIGNUM *priv_key;
int len;
unsigned int gap;
dh = DH_new();
p = BN_bin2bn(dh_p, dh_p_len, 0);
g = BN_bin2bn(dh_g, dh_g_len, 0);
DH_set0_pqg(dh, p, NULL, g);
DH_set_flags(dh, DH_FLAG_NO_EXP_CONSTTIME);
DH_generate_key(dh);
DH_get0_key(dh, NULL, &priv_key);
len = BN_num_bytes(priv_key);
if (len > dest_len) {
return -1;
}
gap = dest_len - len;
memset(dest, 0, gap);
BN_bn2bin(priv_key, &dest[gap]);
DH_free(dh);
return 0;
}
/* dest = base ^ exp % mod */
int dh_mod_exp(uint8_t *dest, int dest_len, uint8_t *base, int base_len, uint8_t *mod, int mod_len, uint8_t *exp, int exp_len)
{
BIGNUM *bn_dest, *bn_base, *bn_exp, *bn_mod;
BN_CTX *ctx;
int len;
unsigned int gap;
bn_base = BN_bin2bn(base, base_len, NULL);
bn_exp = BN_bin2bn(exp, exp_len, NULL);
bn_mod = BN_bin2bn(mod, mod_len, NULL);
ctx = BN_CTX_new();
bn_dest = BN_new();
BN_mod_exp(bn_dest, bn_base, bn_exp, bn_mod, ctx);
BN_CTX_free(ctx);
len = BN_num_bytes(bn_dest);
if (len > dest_len) {
return -1;
}
gap = dest_len - len;
memset(dest, 0, gap);
BN_bn2bin(bn_dest, &dest[gap]);
BN_free(bn_dest);
BN_free(bn_mod);
BN_free(bn_exp);
BN_free(bn_base);
return 0;
}
int dh_dhph_signature(uint8_t *out, uint8_t *nonce, uint8_t *dhph, RSA *r)
{
unsigned char dest[302];
uint8_t hash[20];
unsigned char dbuf[256];
dest[0x00] = 0x00; /* version */
dest[0x01] = 0x00;
dest[0x02] = 0x08; /* len (bits) */
dest[0x03] = 0x01; /* version data */
dest[0x04] = 0x01; /* msg_label */
dest[0x05] = 0x00;
dest[0x06] = 0x08; /* len (bits) */
dest[0x07] = 0x02; /* message data */
dest[0x08] = 0x02; /* auth_nonce */
dest[0x09] = 0x01;
dest[0x0a] = 0x00; /* len (bits) */
memcpy(&dest[0x0b], nonce, 32);
dest[0x2b] = 0x04; /* DHPH - DH public key host */
dest[0x2c] = 0x08;
dest[0x2d] = 0x00; /* len (bits) */
memcpy(&dest[0x2e], dhph, 256);
SHA1(dest, 0x12e, hash);
if (pkcs_1_pss_encode(hash, 20, 20, 0x800, dbuf, sizeof(dbuf))) {;
return -1;
}
RSA_private_encrypt(sizeof(dbuf), dbuf, out, r, RSA_NO_PADDING);
return 0;
}
int aes_xcbc_mac_init(struct aes_xcbc_mac_ctx *ctx, const uint8_t *key)
{
AES_KEY aes_key;
int y, x;
AES_set_encrypt_key(key, 128, &aes_key);
for (y = 0; y < 3; y++) {
for (x = 0; x < 16; x++)
ctx->K[y][x] = y + 1;
AES_ecb_encrypt(ctx->K[y], ctx->K[y], &aes_key, 1);
}
/* setup K1 */
AES_set_encrypt_key(ctx->K[0], 128, &ctx->key);
memset(ctx->IV, 0, 16);
ctx->buflen = 0;
return 0;
}
int aes_xcbc_mac_process(struct aes_xcbc_mac_ctx *ctx, const uint8_t *in, unsigned int len)
{
while (len) {
if (ctx->buflen == 16) {
AES_ecb_encrypt(ctx->IV, ctx->IV, &ctx->key, 1);
ctx->buflen = 0;
}
ctx->IV[ctx->buflen++] ^= *in++;
--len;
}
return 0;
}
int aes_xcbc_mac_done(struct aes_xcbc_mac_ctx *ctx, uint8_t *out)
{
int i;
if (ctx->buflen == 16) {
/* K2 */
for (i = 0; i < 16; i++)
ctx->IV[i] ^= ctx->K[1][i];
} else {
ctx->IV[ctx->buflen] ^= 0x80;
/* K3 */
for (i = 0; i < 16; i++)
ctx->IV[i] ^= ctx->K[2][i];
}
AES_ecb_encrypt(ctx->IV, ctx->IV, &ctx->key, 1);
memcpy(out, ctx->IV, 16);
return 0;
}