Line data Source code
1 : /*
2 : * Wrapper functions for OpenSSL libcrypto
3 : * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 : #include <openssl/opensslv.h>
11 : #include <openssl/err.h>
12 : #include <openssl/des.h>
13 : #include <openssl/aes.h>
14 : #include <openssl/bn.h>
15 : #include <openssl/evp.h>
16 : #include <openssl/dh.h>
17 : #include <openssl/hmac.h>
18 : #include <openssl/rand.h>
19 : #ifdef CONFIG_OPENSSL_CMAC
20 : #include <openssl/cmac.h>
21 : #endif /* CONFIG_OPENSSL_CMAC */
22 : #ifdef CONFIG_ECC
23 : #include <openssl/ec.h>
24 : #endif /* CONFIG_ECC */
25 :
26 : #include "common.h"
27 : #include "wpabuf.h"
28 : #include "dh_group5.h"
29 : #include "sha1.h"
30 : #include "sha256.h"
31 : #include "crypto.h"
32 :
33 : #if OPENSSL_VERSION_NUMBER < 0x00907000
34 : #define DES_key_schedule des_key_schedule
35 : #define DES_cblock des_cblock
36 : #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
37 : #define DES_ecb_encrypt(input, output, ks, enc) \
38 : des_ecb_encrypt((input), (output), *(ks), (enc))
39 : #endif /* openssl < 0.9.7 */
40 :
41 91 : static BIGNUM * get_group5_prime(void)
42 : {
43 : #if OPENSSL_VERSION_NUMBER < 0x00908000
44 : static const unsigned char RFC3526_PRIME_1536[] = {
45 : 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
46 : 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
47 : 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
48 : 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
49 : 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
50 : 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
51 : 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
52 : 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
53 : 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
54 : 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
55 : 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
56 : 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
57 : 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
58 : 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
59 : 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
60 : 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
61 : };
62 : return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
63 : #else /* openssl < 0.9.8 */
64 91 : return get_rfc3526_prime_1536(NULL);
65 : #endif /* openssl < 0.9.8 */
66 : }
67 :
68 : #if OPENSSL_VERSION_NUMBER < 0x00908000
69 : #ifndef OPENSSL_NO_SHA256
70 : #ifndef OPENSSL_FIPS
71 : #define NO_SHA256_WRAPPER
72 : #endif
73 : #endif
74 :
75 : #endif /* openssl < 0.9.8 */
76 :
77 : #ifdef OPENSSL_NO_SHA256
78 : #define NO_SHA256_WRAPPER
79 : #endif
80 :
81 12430 : static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
82 : const u8 *addr[], const size_t *len, u8 *mac)
83 : {
84 : EVP_MD_CTX ctx;
85 : size_t i;
86 : unsigned int mac_len;
87 :
88 12430 : EVP_MD_CTX_init(&ctx);
89 12430 : if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
90 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
91 : ERR_error_string(ERR_get_error(), NULL));
92 0 : return -1;
93 : }
94 44246 : for (i = 0; i < num_elem; i++) {
95 31816 : if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
96 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
97 : "failed: %s",
98 : ERR_error_string(ERR_get_error(), NULL));
99 0 : return -1;
100 : }
101 : }
102 12430 : if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
103 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
104 : ERR_error_string(ERR_get_error(), NULL));
105 0 : return -1;
106 : }
107 :
108 12430 : return 0;
109 : }
110 :
111 :
112 23 : int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
113 : {
114 23 : return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
115 : }
116 :
117 :
118 30 : void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
119 : {
120 : u8 pkey[8], next, tmp;
121 : int i;
122 : DES_key_schedule ks;
123 :
124 : /* Add parity bits to the key */
125 30 : next = 0;
126 240 : for (i = 0; i < 7; i++) {
127 210 : tmp = key[i];
128 210 : pkey[i] = (tmp >> i) | next | 1;
129 210 : next = tmp << (7 - i);
130 : }
131 30 : pkey[i] = next | 1;
132 :
133 30 : DES_set_key(&pkey, &ks);
134 30 : DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
135 : DES_ENCRYPT);
136 30 : }
137 :
138 :
139 25 : int rc4_skip(const u8 *key, size_t keylen, size_t skip,
140 : u8 *data, size_t data_len)
141 : {
142 : #ifdef OPENSSL_NO_RC4
143 : return -1;
144 : #else /* OPENSSL_NO_RC4 */
145 : EVP_CIPHER_CTX ctx;
146 : int outl;
147 25 : int res = -1;
148 : unsigned char skip_buf[16];
149 :
150 25 : EVP_CIPHER_CTX_init(&ctx);
151 50 : if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
152 50 : !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
153 50 : !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
154 25 : !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
155 : goto out;
156 :
157 354 : while (skip >= sizeof(skip_buf)) {
158 304 : size_t len = skip;
159 304 : if (len > sizeof(skip_buf))
160 285 : len = sizeof(skip_buf);
161 304 : if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
162 0 : goto out;
163 304 : skip -= len;
164 : }
165 :
166 25 : if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
167 25 : res = 0;
168 :
169 : out:
170 25 : EVP_CIPHER_CTX_cleanup(&ctx);
171 25 : return res;
172 : #endif /* OPENSSL_NO_RC4 */
173 : }
174 :
175 :
176 11701 : int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
177 : {
178 11701 : return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
179 : }
180 :
181 :
182 94 : int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
183 : {
184 94 : return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
185 : }
186 :
187 :
188 : #ifndef NO_SHA256_WRAPPER
189 612 : int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
190 : u8 *mac)
191 : {
192 612 : return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
193 : }
194 : #endif /* NO_SHA256_WRAPPER */
195 :
196 :
197 1627 : static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
198 : {
199 1627 : switch (keylen) {
200 : case 16:
201 1627 : return EVP_aes_128_ecb();
202 : case 24:
203 0 : return EVP_aes_192_ecb();
204 : case 32:
205 0 : return EVP_aes_256_ecb();
206 : }
207 :
208 0 : return NULL;
209 : }
210 :
211 :
212 1486 : void * aes_encrypt_init(const u8 *key, size_t len)
213 : {
214 : EVP_CIPHER_CTX *ctx;
215 : const EVP_CIPHER *type;
216 :
217 1486 : type = aes_get_evp_cipher(len);
218 1486 : if (type == NULL)
219 0 : return NULL;
220 :
221 1486 : ctx = os_malloc(sizeof(*ctx));
222 1486 : if (ctx == NULL)
223 0 : return NULL;
224 1486 : EVP_CIPHER_CTX_init(ctx);
225 1486 : if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
226 0 : os_free(ctx);
227 0 : return NULL;
228 : }
229 1486 : EVP_CIPHER_CTX_set_padding(ctx, 0);
230 1486 : return ctx;
231 : }
232 :
233 :
234 33822 : void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
235 : {
236 33822 : EVP_CIPHER_CTX *c = ctx;
237 33822 : int clen = 16;
238 33822 : if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
239 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
240 : ERR_error_string(ERR_get_error(), NULL));
241 : }
242 33822 : }
243 :
244 :
245 1486 : void aes_encrypt_deinit(void *ctx)
246 : {
247 1486 : EVP_CIPHER_CTX *c = ctx;
248 : u8 buf[16];
249 1486 : int len = sizeof(buf);
250 1486 : if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
251 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
252 : "%s", ERR_error_string(ERR_get_error(), NULL));
253 : }
254 1486 : if (len != 0) {
255 0 : wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
256 : "in AES encrypt", len);
257 : }
258 1486 : EVP_CIPHER_CTX_cleanup(c);
259 1486 : os_free(c);
260 1486 : }
261 :
262 :
263 141 : void * aes_decrypt_init(const u8 *key, size_t len)
264 : {
265 : EVP_CIPHER_CTX *ctx;
266 : const EVP_CIPHER *type;
267 :
268 141 : type = aes_get_evp_cipher(len);
269 141 : if (type == NULL)
270 0 : return NULL;
271 :
272 141 : ctx = os_malloc(sizeof(*ctx));
273 141 : if (ctx == NULL)
274 0 : return NULL;
275 141 : EVP_CIPHER_CTX_init(ctx);
276 141 : if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
277 0 : os_free(ctx);
278 0 : return NULL;
279 : }
280 141 : EVP_CIPHER_CTX_set_padding(ctx, 0);
281 141 : return ctx;
282 : }
283 :
284 :
285 1462 : void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
286 : {
287 1462 : EVP_CIPHER_CTX *c = ctx;
288 1462 : int plen = 16;
289 1462 : if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
290 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
291 : ERR_error_string(ERR_get_error(), NULL));
292 : }
293 1462 : }
294 :
295 :
296 141 : void aes_decrypt_deinit(void *ctx)
297 : {
298 141 : EVP_CIPHER_CTX *c = ctx;
299 : u8 buf[16];
300 141 : int len = sizeof(buf);
301 141 : if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
302 0 : wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
303 : "%s", ERR_error_string(ERR_get_error(), NULL));
304 : }
305 141 : if (len != 0) {
306 0 : wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
307 : "in AES decrypt", len);
308 : }
309 141 : EVP_CIPHER_CTX_cleanup(c);
310 141 : os_free(ctx);
311 141 : }
312 :
313 :
314 4 : int crypto_mod_exp(const u8 *base, size_t base_len,
315 : const u8 *power, size_t power_len,
316 : const u8 *modulus, size_t modulus_len,
317 : u8 *result, size_t *result_len)
318 : {
319 : BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
320 4 : int ret = -1;
321 : BN_CTX *ctx;
322 :
323 4 : ctx = BN_CTX_new();
324 4 : if (ctx == NULL)
325 0 : return -1;
326 :
327 4 : bn_base = BN_bin2bn(base, base_len, NULL);
328 4 : bn_exp = BN_bin2bn(power, power_len, NULL);
329 4 : bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
330 4 : bn_result = BN_new();
331 :
332 4 : if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
333 : bn_result == NULL)
334 : goto error;
335 :
336 4 : if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
337 0 : goto error;
338 :
339 4 : *result_len = BN_bn2bin(bn_result, result);
340 4 : ret = 0;
341 :
342 : error:
343 4 : BN_free(bn_base);
344 4 : BN_free(bn_exp);
345 4 : BN_free(bn_modulus);
346 4 : BN_free(bn_result);
347 4 : BN_CTX_free(ctx);
348 4 : return ret;
349 : }
350 :
351 :
352 : struct crypto_cipher {
353 : EVP_CIPHER_CTX enc;
354 : EVP_CIPHER_CTX dec;
355 : };
356 :
357 :
358 6 : struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
359 : const u8 *iv, const u8 *key,
360 : size_t key_len)
361 : {
362 : struct crypto_cipher *ctx;
363 : const EVP_CIPHER *cipher;
364 :
365 6 : ctx = os_zalloc(sizeof(*ctx));
366 6 : if (ctx == NULL)
367 0 : return NULL;
368 :
369 6 : switch (alg) {
370 : #ifndef OPENSSL_NO_RC4
371 : case CRYPTO_CIPHER_ALG_RC4:
372 0 : cipher = EVP_rc4();
373 0 : break;
374 : #endif /* OPENSSL_NO_RC4 */
375 : #ifndef OPENSSL_NO_AES
376 : case CRYPTO_CIPHER_ALG_AES:
377 6 : switch (key_len) {
378 : case 16:
379 6 : cipher = EVP_aes_128_cbc();
380 6 : break;
381 : case 24:
382 0 : cipher = EVP_aes_192_cbc();
383 0 : break;
384 : case 32:
385 0 : cipher = EVP_aes_256_cbc();
386 0 : break;
387 : default:
388 0 : os_free(ctx);
389 0 : return NULL;
390 : }
391 6 : break;
392 : #endif /* OPENSSL_NO_AES */
393 : #ifndef OPENSSL_NO_DES
394 : case CRYPTO_CIPHER_ALG_3DES:
395 0 : cipher = EVP_des_ede3_cbc();
396 0 : break;
397 : case CRYPTO_CIPHER_ALG_DES:
398 0 : cipher = EVP_des_cbc();
399 0 : break;
400 : #endif /* OPENSSL_NO_DES */
401 : #ifndef OPENSSL_NO_RC2
402 : case CRYPTO_CIPHER_ALG_RC2:
403 0 : cipher = EVP_rc2_ecb();
404 0 : break;
405 : #endif /* OPENSSL_NO_RC2 */
406 : default:
407 0 : os_free(ctx);
408 0 : return NULL;
409 : }
410 :
411 6 : EVP_CIPHER_CTX_init(&ctx->enc);
412 6 : EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
413 12 : if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
414 12 : !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
415 6 : !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
416 0 : EVP_CIPHER_CTX_cleanup(&ctx->enc);
417 0 : os_free(ctx);
418 0 : return NULL;
419 : }
420 :
421 6 : EVP_CIPHER_CTX_init(&ctx->dec);
422 6 : EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
423 12 : if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
424 12 : !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
425 6 : !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
426 0 : EVP_CIPHER_CTX_cleanup(&ctx->enc);
427 0 : EVP_CIPHER_CTX_cleanup(&ctx->dec);
428 0 : os_free(ctx);
429 0 : return NULL;
430 : }
431 :
432 6 : return ctx;
433 : }
434 :
435 :
436 2 : int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
437 : u8 *crypt, size_t len)
438 : {
439 : int outl;
440 2 : if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
441 0 : return -1;
442 2 : return 0;
443 : }
444 :
445 :
446 4 : int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
447 : u8 *plain, size_t len)
448 : {
449 : int outl;
450 4 : outl = len;
451 4 : if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
452 0 : return -1;
453 4 : return 0;
454 : }
455 :
456 :
457 6 : void crypto_cipher_deinit(struct crypto_cipher *ctx)
458 : {
459 6 : EVP_CIPHER_CTX_cleanup(&ctx->enc);
460 6 : EVP_CIPHER_CTX_cleanup(&ctx->dec);
461 6 : os_free(ctx);
462 6 : }
463 :
464 :
465 84 : void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
466 : {
467 : DH *dh;
468 84 : struct wpabuf *pubkey = NULL, *privkey = NULL;
469 : size_t publen, privlen;
470 :
471 84 : *priv = NULL;
472 84 : *publ = NULL;
473 :
474 84 : dh = DH_new();
475 84 : if (dh == NULL)
476 0 : return NULL;
477 :
478 84 : dh->g = BN_new();
479 84 : if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
480 : goto err;
481 :
482 84 : dh->p = get_group5_prime();
483 84 : if (dh->p == NULL)
484 0 : goto err;
485 :
486 84 : if (DH_generate_key(dh) != 1)
487 0 : goto err;
488 :
489 84 : publen = BN_num_bytes(dh->pub_key);
490 84 : pubkey = wpabuf_alloc(publen);
491 84 : if (pubkey == NULL)
492 0 : goto err;
493 84 : privlen = BN_num_bytes(dh->priv_key);
494 84 : privkey = wpabuf_alloc(privlen);
495 84 : if (privkey == NULL)
496 0 : goto err;
497 :
498 84 : BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
499 84 : BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
500 :
501 84 : *priv = privkey;
502 84 : *publ = pubkey;
503 84 : return dh;
504 :
505 : err:
506 0 : wpabuf_free(pubkey);
507 0 : wpabuf_free(privkey);
508 0 : DH_free(dh);
509 0 : return NULL;
510 : }
511 :
512 :
513 7 : void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
514 : {
515 : DH *dh;
516 :
517 7 : dh = DH_new();
518 7 : if (dh == NULL)
519 0 : return NULL;
520 :
521 7 : dh->g = BN_new();
522 7 : if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
523 : goto err;
524 :
525 7 : dh->p = get_group5_prime();
526 7 : if (dh->p == NULL)
527 0 : goto err;
528 :
529 7 : dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
530 7 : if (dh->priv_key == NULL)
531 0 : goto err;
532 :
533 7 : dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
534 7 : if (dh->pub_key == NULL)
535 0 : goto err;
536 :
537 7 : if (DH_generate_key(dh) != 1)
538 0 : goto err;
539 :
540 7 : return dh;
541 :
542 : err:
543 0 : DH_free(dh);
544 0 : return NULL;
545 : }
546 :
547 :
548 65 : struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
549 : const struct wpabuf *own_private)
550 : {
551 : BIGNUM *pub_key;
552 65 : struct wpabuf *res = NULL;
553 : size_t rlen;
554 65 : DH *dh = ctx;
555 : int keylen;
556 :
557 65 : if (ctx == NULL)
558 0 : return NULL;
559 :
560 65 : pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
561 : NULL);
562 65 : if (pub_key == NULL)
563 0 : return NULL;
564 :
565 65 : rlen = DH_size(dh);
566 65 : res = wpabuf_alloc(rlen);
567 65 : if (res == NULL)
568 0 : goto err;
569 :
570 65 : keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
571 65 : if (keylen < 0)
572 0 : goto err;
573 65 : wpabuf_put(res, keylen);
574 65 : BN_free(pub_key);
575 :
576 65 : return res;
577 :
578 : err:
579 0 : BN_free(pub_key);
580 0 : wpabuf_free(res);
581 0 : return NULL;
582 : }
583 :
584 :
585 244 : void dh5_free(void *ctx)
586 : {
587 : DH *dh;
588 244 : if (ctx == NULL)
589 397 : return;
590 91 : dh = ctx;
591 91 : DH_free(dh);
592 : }
593 :
594 :
595 : struct crypto_hash {
596 : HMAC_CTX ctx;
597 : };
598 :
599 :
600 73 : struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
601 : size_t key_len)
602 : {
603 : struct crypto_hash *ctx;
604 : const EVP_MD *md;
605 :
606 73 : switch (alg) {
607 : #ifndef OPENSSL_NO_MD5
608 : case CRYPTO_HASH_ALG_HMAC_MD5:
609 0 : md = EVP_md5();
610 0 : break;
611 : #endif /* OPENSSL_NO_MD5 */
612 : #ifndef OPENSSL_NO_SHA
613 : case CRYPTO_HASH_ALG_HMAC_SHA1:
614 0 : md = EVP_sha1();
615 0 : break;
616 : #endif /* OPENSSL_NO_SHA */
617 : #ifndef OPENSSL_NO_SHA256
618 : #ifdef CONFIG_SHA256
619 : case CRYPTO_HASH_ALG_HMAC_SHA256:
620 73 : md = EVP_sha256();
621 73 : break;
622 : #endif /* CONFIG_SHA256 */
623 : #endif /* OPENSSL_NO_SHA256 */
624 : default:
625 0 : return NULL;
626 : }
627 :
628 73 : ctx = os_zalloc(sizeof(*ctx));
629 73 : if (ctx == NULL)
630 0 : return NULL;
631 73 : HMAC_CTX_init(&ctx->ctx);
632 :
633 : #if OPENSSL_VERSION_NUMBER < 0x00909000
634 : HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
635 : #else /* openssl < 0.9.9 */
636 73 : if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
637 0 : os_free(ctx);
638 0 : return NULL;
639 : }
640 : #endif /* openssl < 0.9.9 */
641 :
642 73 : return ctx;
643 : }
644 :
645 :
646 322 : void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
647 : {
648 322 : if (ctx == NULL)
649 322 : return;
650 322 : HMAC_Update(&ctx->ctx, data, len);
651 : }
652 :
653 :
654 73 : int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
655 : {
656 : unsigned int mdlen;
657 : int res;
658 :
659 73 : if (ctx == NULL)
660 0 : return -2;
661 :
662 73 : if (mac == NULL || len == NULL) {
663 0 : os_free(ctx);
664 0 : return 0;
665 : }
666 :
667 73 : mdlen = *len;
668 : #if OPENSSL_VERSION_NUMBER < 0x00909000
669 : HMAC_Final(&ctx->ctx, mac, &mdlen);
670 : res = 1;
671 : #else /* openssl < 0.9.9 */
672 73 : res = HMAC_Final(&ctx->ctx, mac, &mdlen);
673 : #endif /* openssl < 0.9.9 */
674 73 : HMAC_CTX_cleanup(&ctx->ctx);
675 73 : os_free(ctx);
676 :
677 73 : if (res == 1) {
678 73 : *len = mdlen;
679 73 : return 0;
680 : }
681 :
682 0 : return -1;
683 : }
684 :
685 :
686 184 : int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
687 : int iterations, u8 *buf, size_t buflen)
688 : {
689 : #if OPENSSL_VERSION_NUMBER < 0x00908000
690 : if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
691 : (unsigned char *) ssid,
692 : ssid_len, 4096, buflen, buf) != 1)
693 : return -1;
694 : #else /* openssl < 0.9.8 */
695 184 : if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
696 : ssid_len, 4096, buflen, buf) != 1)
697 0 : return -1;
698 : #endif /* openssl < 0.9.8 */
699 184 : return 0;
700 : }
701 :
702 :
703 4999 : int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
704 : const u8 *addr[], const size_t *len, u8 *mac)
705 : {
706 : HMAC_CTX ctx;
707 : size_t i;
708 : unsigned int mdlen;
709 : int res;
710 :
711 4999 : HMAC_CTX_init(&ctx);
712 : #if OPENSSL_VERSION_NUMBER < 0x00909000
713 : HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
714 : #else /* openssl < 0.9.9 */
715 4999 : if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
716 0 : return -1;
717 : #endif /* openssl < 0.9.9 */
718 :
719 17180 : for (i = 0; i < num_elem; i++)
720 12181 : HMAC_Update(&ctx, addr[i], len[i]);
721 :
722 4999 : mdlen = 20;
723 : #if OPENSSL_VERSION_NUMBER < 0x00909000
724 : HMAC_Final(&ctx, mac, &mdlen);
725 : res = 1;
726 : #else /* openssl < 0.9.9 */
727 4999 : res = HMAC_Final(&ctx, mac, &mdlen);
728 : #endif /* openssl < 0.9.9 */
729 4999 : HMAC_CTX_cleanup(&ctx);
730 :
731 4999 : return res == 1 ? 0 : -1;
732 : }
733 :
734 :
735 1423 : int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
736 : u8 *mac)
737 : {
738 1423 : return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
739 : }
740 :
741 :
742 : #ifdef CONFIG_SHA256
743 :
744 3066 : int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
745 : const u8 *addr[], const size_t *len, u8 *mac)
746 : {
747 : HMAC_CTX ctx;
748 : size_t i;
749 : unsigned int mdlen;
750 : int res;
751 :
752 3066 : HMAC_CTX_init(&ctx);
753 : #if OPENSSL_VERSION_NUMBER < 0x00909000
754 : HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
755 : #else /* openssl < 0.9.9 */
756 3066 : if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
757 0 : return -1;
758 : #endif /* openssl < 0.9.9 */
759 :
760 13223 : for (i = 0; i < num_elem; i++)
761 10157 : HMAC_Update(&ctx, addr[i], len[i]);
762 :
763 3066 : mdlen = 32;
764 : #if OPENSSL_VERSION_NUMBER < 0x00909000
765 : HMAC_Final(&ctx, mac, &mdlen);
766 : res = 1;
767 : #else /* openssl < 0.9.9 */
768 3066 : res = HMAC_Final(&ctx, mac, &mdlen);
769 : #endif /* openssl < 0.9.9 */
770 3066 : HMAC_CTX_cleanup(&ctx);
771 :
772 3066 : return res == 1 ? 0 : -1;
773 : }
774 :
775 :
776 408 : int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
777 : size_t data_len, u8 *mac)
778 : {
779 408 : return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
780 : }
781 :
782 : #endif /* CONFIG_SHA256 */
783 :
784 :
785 0 : int crypto_get_random(void *buf, size_t len)
786 : {
787 0 : if (RAND_bytes(buf, len) != 1)
788 0 : return -1;
789 0 : return 0;
790 : }
791 :
792 :
793 : #ifdef CONFIG_OPENSSL_CMAC
794 : int omac1_aes_128_vector(const u8 *key, size_t num_elem,
795 : const u8 *addr[], const size_t *len, u8 *mac)
796 : {
797 : CMAC_CTX *ctx;
798 : int ret = -1;
799 : size_t outlen, i;
800 :
801 : ctx = CMAC_CTX_new();
802 : if (ctx == NULL)
803 : return -1;
804 :
805 : if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
806 : goto fail;
807 : for (i = 0; i < num_elem; i++) {
808 : if (!CMAC_Update(ctx, addr[i], len[i]))
809 : goto fail;
810 : }
811 : if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
812 : goto fail;
813 :
814 : ret = 0;
815 : fail:
816 : CMAC_CTX_free(ctx);
817 : return ret;
818 : }
819 :
820 :
821 : int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
822 : {
823 : return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
824 : }
825 : #endif /* CONFIG_OPENSSL_CMAC */
826 :
827 :
828 60 : struct crypto_bignum * crypto_bignum_init(void)
829 : {
830 60 : return (struct crypto_bignum *) BN_new();
831 : }
832 :
833 :
834 136 : struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
835 : {
836 136 : BIGNUM *bn = BN_bin2bn(buf, len, NULL);
837 136 : return (struct crypto_bignum *) bn;
838 : }
839 :
840 :
841 336 : void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
842 : {
843 336 : if (clear)
844 85 : BN_clear_free((BIGNUM *) n);
845 : else
846 251 : BN_free((BIGNUM *) n);
847 336 : }
848 :
849 :
850 380 : int crypto_bignum_to_bin(const struct crypto_bignum *a,
851 : u8 *buf, size_t buflen, size_t padlen)
852 : {
853 : int num_bytes, offset;
854 :
855 380 : if (padlen > buflen)
856 0 : return -1;
857 :
858 380 : num_bytes = BN_num_bytes((const BIGNUM *) a);
859 380 : if ((size_t) num_bytes > buflen)
860 0 : return -1;
861 380 : if (padlen > (size_t) num_bytes)
862 28 : offset = padlen - num_bytes;
863 : else
864 352 : offset = 0;
865 :
866 380 : os_memset(buf, 0, offset);
867 380 : BN_bn2bin((const BIGNUM *) a, buf + offset);
868 :
869 380 : return num_bytes + offset;
870 : }
871 :
872 :
873 40 : int crypto_bignum_add(const struct crypto_bignum *a,
874 : const struct crypto_bignum *b,
875 : struct crypto_bignum *c)
876 : {
877 80 : return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
878 40 : 0 : -1;
879 : }
880 :
881 :
882 40 : int crypto_bignum_mod(const struct crypto_bignum *a,
883 : const struct crypto_bignum *b,
884 : struct crypto_bignum *c)
885 : {
886 : int res;
887 : BN_CTX *bnctx;
888 :
889 40 : bnctx = BN_CTX_new();
890 40 : if (bnctx == NULL)
891 0 : return -1;
892 40 : res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
893 : bnctx);
894 40 : BN_CTX_free(bnctx);
895 :
896 40 : return res ? 0 : -1;
897 : }
898 :
899 :
900 25 : int crypto_bignum_exptmod(const struct crypto_bignum *a,
901 : const struct crypto_bignum *b,
902 : const struct crypto_bignum *c,
903 : struct crypto_bignum *d)
904 : {
905 : int res;
906 : BN_CTX *bnctx;
907 :
908 25 : bnctx = BN_CTX_new();
909 25 : if (bnctx == NULL)
910 0 : return -1;
911 25 : res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
912 : (const BIGNUM *) c, bnctx);
913 25 : BN_CTX_free(bnctx);
914 :
915 25 : return res ? 0 : -1;
916 : }
917 :
918 :
919 5 : int crypto_bignum_inverse(const struct crypto_bignum *a,
920 : const struct crypto_bignum *b,
921 : struct crypto_bignum *c)
922 : {
923 : BIGNUM *res;
924 : BN_CTX *bnctx;
925 :
926 5 : bnctx = BN_CTX_new();
927 5 : if (bnctx == NULL)
928 0 : return -1;
929 5 : res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
930 : (const BIGNUM *) b, bnctx);
931 5 : BN_CTX_free(bnctx);
932 :
933 5 : return res ? 0 : -1;
934 : }
935 :
936 :
937 3 : int crypto_bignum_sub(const struct crypto_bignum *a,
938 : const struct crypto_bignum *b,
939 : struct crypto_bignum *c)
940 : {
941 6 : return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
942 3 : 0 : -1;
943 : }
944 :
945 :
946 3 : int crypto_bignum_div(const struct crypto_bignum *a,
947 : const struct crypto_bignum *b,
948 : struct crypto_bignum *c)
949 : {
950 : int res;
951 :
952 : BN_CTX *bnctx;
953 :
954 3 : bnctx = BN_CTX_new();
955 3 : if (bnctx == NULL)
956 0 : return -1;
957 3 : res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
958 : (const BIGNUM *) b, bnctx);
959 3 : BN_CTX_free(bnctx);
960 :
961 3 : return res ? 0 : -1;
962 : }
963 :
964 :
965 5 : int crypto_bignum_mulmod(const struct crypto_bignum *a,
966 : const struct crypto_bignum *b,
967 : const struct crypto_bignum *c,
968 : struct crypto_bignum *d)
969 : {
970 : int res;
971 :
972 : BN_CTX *bnctx;
973 :
974 5 : bnctx = BN_CTX_new();
975 5 : if (bnctx == NULL)
976 0 : return -1;
977 5 : res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
978 : (const BIGNUM *) c, bnctx);
979 5 : BN_CTX_free(bnctx);
980 :
981 5 : return res ? 0 : -1;
982 : }
983 :
984 :
985 71 : int crypto_bignum_cmp(const struct crypto_bignum *a,
986 : const struct crypto_bignum *b)
987 : {
988 71 : return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
989 : }
990 :
991 :
992 40 : int crypto_bignum_bits(const struct crypto_bignum *a)
993 : {
994 40 : return BN_num_bits((const BIGNUM *) a);
995 : }
996 :
997 :
998 76 : int crypto_bignum_is_zero(const struct crypto_bignum *a)
999 : {
1000 76 : return BN_is_zero((const BIGNUM *) a);
1001 : }
1002 :
1003 :
1004 61 : int crypto_bignum_is_one(const struct crypto_bignum *a)
1005 : {
1006 61 : return BN_is_one((const BIGNUM *) a);
1007 : }
1008 :
1009 :
1010 : #ifdef CONFIG_ECC
1011 :
1012 : struct crypto_ec {
1013 : EC_GROUP *group;
1014 : BN_CTX *bnctx;
1015 : BIGNUM *prime;
1016 : BIGNUM *order;
1017 : };
1018 :
1019 20 : struct crypto_ec * crypto_ec_init(int group)
1020 : {
1021 : struct crypto_ec *e;
1022 : int nid;
1023 :
1024 : /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1025 20 : switch (group) {
1026 : case 19:
1027 11 : nid = NID_X9_62_prime256v1;
1028 11 : break;
1029 : case 20:
1030 1 : nid = NID_secp384r1;
1031 1 : break;
1032 : case 21:
1033 1 : nid = NID_secp521r1;
1034 1 : break;
1035 : case 25:
1036 1 : nid = NID_X9_62_prime192v1;
1037 1 : break;
1038 : case 26:
1039 1 : nid = NID_secp224r1;
1040 1 : break;
1041 : default:
1042 5 : return NULL;
1043 : }
1044 :
1045 15 : e = os_zalloc(sizeof(*e));
1046 15 : if (e == NULL)
1047 0 : return NULL;
1048 :
1049 15 : e->bnctx = BN_CTX_new();
1050 15 : e->group = EC_GROUP_new_by_curve_name(nid);
1051 15 : e->prime = BN_new();
1052 15 : e->order = BN_new();
1053 30 : if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1054 30 : e->order == NULL ||
1055 30 : !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
1056 15 : !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1057 0 : crypto_ec_deinit(e);
1058 0 : e = NULL;
1059 : }
1060 :
1061 15 : return e;
1062 : }
1063 :
1064 :
1065 20 : void crypto_ec_deinit(struct crypto_ec *e)
1066 : {
1067 20 : if (e == NULL)
1068 25 : return;
1069 15 : BN_free(e->order);
1070 15 : EC_GROUP_free(e->group);
1071 15 : BN_CTX_free(e->bnctx);
1072 15 : os_free(e);
1073 : }
1074 :
1075 :
1076 60 : struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1077 : {
1078 60 : if (e == NULL)
1079 0 : return NULL;
1080 60 : return (struct crypto_ec_point *) EC_POINT_new(e->group);
1081 : }
1082 :
1083 :
1084 15 : size_t crypto_ec_prime_len(struct crypto_ec *e)
1085 : {
1086 15 : return BN_num_bytes(e->prime);
1087 : }
1088 :
1089 :
1090 45 : size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1091 : {
1092 45 : return BN_num_bits(e->prime);
1093 : }
1094 :
1095 :
1096 15 : const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1097 : {
1098 15 : return (const struct crypto_bignum *) e->prime;
1099 : }
1100 :
1101 :
1102 15 : const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1103 : {
1104 15 : return (const struct crypto_bignum *) e->order;
1105 : }
1106 :
1107 :
1108 110 : void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1109 : {
1110 110 : if (clear)
1111 50 : EC_POINT_clear_free((EC_POINT *) p);
1112 : else
1113 60 : EC_POINT_free((EC_POINT *) p);
1114 110 : }
1115 :
1116 :
1117 90 : int crypto_ec_point_to_bin(struct crypto_ec *e,
1118 : const struct crypto_ec_point *point, u8 *x, u8 *y)
1119 : {
1120 : BIGNUM *x_bn, *y_bn;
1121 90 : int ret = -1;
1122 90 : int len = BN_num_bytes(e->prime);
1123 :
1124 90 : x_bn = BN_new();
1125 90 : y_bn = BN_new();
1126 :
1127 180 : if (x_bn && y_bn &&
1128 90 : EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1129 : x_bn, y_bn, e->bnctx)) {
1130 90 : if (x) {
1131 90 : crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1132 : x, len, len);
1133 : }
1134 90 : if (y) {
1135 75 : crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1136 : y, len, len);
1137 : }
1138 90 : ret = 0;
1139 : }
1140 :
1141 90 : BN_free(x_bn);
1142 90 : BN_free(y_bn);
1143 90 : return ret;
1144 : }
1145 :
1146 :
1147 20 : struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1148 : const u8 *val)
1149 : {
1150 : BIGNUM *x, *y;
1151 : EC_POINT *elem;
1152 20 : int len = BN_num_bytes(e->prime);
1153 :
1154 20 : x = BN_bin2bn(val, len, NULL);
1155 20 : y = BN_bin2bn(val + len, len, NULL);
1156 20 : elem = EC_POINT_new(e->group);
1157 20 : if (x == NULL || y == NULL || elem == NULL) {
1158 0 : BN_free(x);
1159 0 : BN_free(y);
1160 0 : EC_POINT_free(elem);
1161 0 : return NULL;
1162 : }
1163 :
1164 20 : if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1165 : e->bnctx)) {
1166 0 : EC_POINT_free(elem);
1167 0 : elem = NULL;
1168 : }
1169 :
1170 20 : BN_free(x);
1171 20 : BN_free(y);
1172 :
1173 20 : return (struct crypto_ec_point *) elem;
1174 : }
1175 :
1176 :
1177 15 : int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1178 : const struct crypto_ec_point *b,
1179 : struct crypto_ec_point *c)
1180 : {
1181 30 : return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1182 15 : (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1183 : }
1184 :
1185 :
1186 45 : int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1187 : const struct crypto_bignum *b,
1188 : struct crypto_ec_point *res)
1189 : {
1190 90 : return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1191 : (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1192 45 : ? 0 : -1;
1193 : }
1194 :
1195 :
1196 15 : int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1197 : {
1198 15 : return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1199 : }
1200 :
1201 :
1202 45 : int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1203 : struct crypto_ec_point *p,
1204 : const struct crypto_bignum *x, int y_bit)
1205 : {
1206 45 : if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1207 : (const BIGNUM *) x, y_bit,
1208 28 : e->bnctx) ||
1209 28 : !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1210 17 : return -1;
1211 28 : return 0;
1212 : }
1213 :
1214 :
1215 15 : int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1216 : const struct crypto_ec_point *p)
1217 : {
1218 15 : return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1219 : }
1220 :
1221 :
1222 20 : int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1223 : const struct crypto_ec_point *p)
1224 : {
1225 20 : return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
1226 : }
1227 :
1228 : #endif /* CONFIG_ECC */
|