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 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 	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 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 	EVP_MD_CTX_init(&ctx);
89 	if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
90 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
91 			   ERR_error_string(ERR_get_error(), NULL));
92 		return -1;
93 	}
94 	for (i = 0; i < num_elem; i++) {
95 		if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
96 			wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
97 				   "failed: %s",
98 				   ERR_error_string(ERR_get_error(), NULL));
99 			return -1;
100 		}
101 	}
102 	if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
103 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
104 			   ERR_error_string(ERR_get_error(), NULL));
105 		return -1;
106 	}
107 
108 	return 0;
109 }
110 
111 
112 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
113 {
114 	return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
115 }
116 
117 
118 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 	next = 0;
126 	for (i = 0; i < 7; i++) {
127 		tmp = key[i];
128 		pkey[i] = (tmp >> i) | next | 1;
129 		next = tmp << (7 - i);
130 	}
131 	pkey[i] = next | 1;
132 
133 	DES_set_key(&pkey, &ks);
134 	DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
135 			DES_ENCRYPT);
136 }
137 
138 
139 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 	int res = -1;
148 	unsigned char skip_buf[16];
149 
150 	EVP_CIPHER_CTX_init(&ctx);
151 	if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
152 	    !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
153 	    !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
154 	    !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
155 		goto out;
156 
157 	while (skip >= sizeof(skip_buf)) {
158 		size_t len = skip;
159 		if (len > sizeof(skip_buf))
160 			len = sizeof(skip_buf);
161 		if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
162 			goto out;
163 		skip -= len;
164 	}
165 
166 	if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
167 		res = 0;
168 
169 out:
170 	EVP_CIPHER_CTX_cleanup(&ctx);
171 	return res;
172 #endif /* OPENSSL_NO_RC4 */
173 }
174 
175 
176 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
177 {
178 	return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
179 }
180 
181 
182 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
183 {
184 	return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
185 }
186 
187 
188 #ifndef NO_SHA256_WRAPPER
189 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
190 		  u8 *mac)
191 {
192 	return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
193 }
194 #endif /* NO_SHA256_WRAPPER */
195 
196 
197 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
198 {
199 	switch (keylen) {
200 	case 16:
201 		return EVP_aes_128_ecb();
202 	case 24:
203 		return EVP_aes_192_ecb();
204 	case 32:
205 		return EVP_aes_256_ecb();
206 	}
207 
208 	return NULL;
209 }
210 
211 
212 void * aes_encrypt_init(const u8 *key, size_t len)
213 {
214 	EVP_CIPHER_CTX *ctx;
215 	const EVP_CIPHER *type;
216 
217 	type = aes_get_evp_cipher(len);
218 	if (type == NULL)
219 		return NULL;
220 
221 	ctx = os_malloc(sizeof(*ctx));
222 	if (ctx == NULL)
223 		return NULL;
224 	EVP_CIPHER_CTX_init(ctx);
225 	if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
226 		os_free(ctx);
227 		return NULL;
228 	}
229 	EVP_CIPHER_CTX_set_padding(ctx, 0);
230 	return ctx;
231 }
232 
233 
234 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
235 {
236 	EVP_CIPHER_CTX *c = ctx;
237 	int clen = 16;
238 	if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
239 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
240 			   ERR_error_string(ERR_get_error(), NULL));
241 	}
242 }
243 
244 
245 void aes_encrypt_deinit(void *ctx)
246 {
247 	EVP_CIPHER_CTX *c = ctx;
248 	u8 buf[16];
249 	int len = sizeof(buf);
250 	if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
251 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
252 			   "%s", ERR_error_string(ERR_get_error(), NULL));
253 	}
254 	if (len != 0) {
255 		wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
256 			   "in AES encrypt", len);
257 	}
258 	EVP_CIPHER_CTX_cleanup(c);
259 	os_free(c);
260 }
261 
262 
263 void * aes_decrypt_init(const u8 *key, size_t len)
264 {
265 	EVP_CIPHER_CTX *ctx;
266 	const EVP_CIPHER *type;
267 
268 	type = aes_get_evp_cipher(len);
269 	if (type == NULL)
270 		return NULL;
271 
272 	ctx = os_malloc(sizeof(*ctx));
273 	if (ctx == NULL)
274 		return NULL;
275 	EVP_CIPHER_CTX_init(ctx);
276 	if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
277 		os_free(ctx);
278 		return NULL;
279 	}
280 	EVP_CIPHER_CTX_set_padding(ctx, 0);
281 	return ctx;
282 }
283 
284 
285 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
286 {
287 	EVP_CIPHER_CTX *c = ctx;
288 	int plen = 16;
289 	if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
290 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
291 			   ERR_error_string(ERR_get_error(), NULL));
292 	}
293 }
294 
295 
296 void aes_decrypt_deinit(void *ctx)
297 {
298 	EVP_CIPHER_CTX *c = ctx;
299 	u8 buf[16];
300 	int len = sizeof(buf);
301 	if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
302 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
303 			   "%s", ERR_error_string(ERR_get_error(), NULL));
304 	}
305 	if (len != 0) {
306 		wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
307 			   "in AES decrypt", len);
308 	}
309 	EVP_CIPHER_CTX_cleanup(c);
310 	os_free(ctx);
311 }
312 
313 
314 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 	int ret = -1;
321 	BN_CTX *ctx;
322 
323 	ctx = BN_CTX_new();
324 	if (ctx == NULL)
325 		return -1;
326 
327 	bn_base = BN_bin2bn(base, base_len, NULL);
328 	bn_exp = BN_bin2bn(power, power_len, NULL);
329 	bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
330 	bn_result = BN_new();
331 
332 	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
333 	    bn_result == NULL)
334 		goto error;
335 
336 	if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
337 		goto error;
338 
339 	*result_len = BN_bn2bin(bn_result, result);
340 	ret = 0;
341 
342 error:
343 	BN_free(bn_base);
344 	BN_free(bn_exp);
345 	BN_free(bn_modulus);
346 	BN_free(bn_result);
347 	BN_CTX_free(ctx);
348 	return ret;
349 }
350 
351 
352 struct crypto_cipher {
353 	EVP_CIPHER_CTX enc;
354 	EVP_CIPHER_CTX dec;
355 };
356 
357 
358 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 	ctx = os_zalloc(sizeof(*ctx));
366 	if (ctx == NULL)
367 		return NULL;
368 
369 	switch (alg) {
370 #ifndef OPENSSL_NO_RC4
371 	case CRYPTO_CIPHER_ALG_RC4:
372 		cipher = EVP_rc4();
373 		break;
374 #endif /* OPENSSL_NO_RC4 */
375 #ifndef OPENSSL_NO_AES
376 	case CRYPTO_CIPHER_ALG_AES:
377 		switch (key_len) {
378 		case 16:
379 			cipher = EVP_aes_128_cbc();
380 			break;
381 		case 24:
382 			cipher = EVP_aes_192_cbc();
383 			break;
384 		case 32:
385 			cipher = EVP_aes_256_cbc();
386 			break;
387 		default:
388 			os_free(ctx);
389 			return NULL;
390 		}
391 		break;
392 #endif /* OPENSSL_NO_AES */
393 #ifndef OPENSSL_NO_DES
394 	case CRYPTO_CIPHER_ALG_3DES:
395 		cipher = EVP_des_ede3_cbc();
396 		break;
397 	case CRYPTO_CIPHER_ALG_DES:
398 		cipher = EVP_des_cbc();
399 		break;
400 #endif /* OPENSSL_NO_DES */
401 #ifndef OPENSSL_NO_RC2
402 	case CRYPTO_CIPHER_ALG_RC2:
403 		cipher = EVP_rc2_ecb();
404 		break;
405 #endif /* OPENSSL_NO_RC2 */
406 	default:
407 		os_free(ctx);
408 		return NULL;
409 	}
410 
411 	EVP_CIPHER_CTX_init(&ctx->enc);
412 	EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
413 	if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
414 	    !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
415 	    !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
416 		EVP_CIPHER_CTX_cleanup(&ctx->enc);
417 		os_free(ctx);
418 		return NULL;
419 	}
420 
421 	EVP_CIPHER_CTX_init(&ctx->dec);
422 	EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
423 	if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
424 	    !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
425 	    !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
426 		EVP_CIPHER_CTX_cleanup(&ctx->enc);
427 		EVP_CIPHER_CTX_cleanup(&ctx->dec);
428 		os_free(ctx);
429 		return NULL;
430 	}
431 
432 	return ctx;
433 }
434 
435 
436 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
437 			  u8 *crypt, size_t len)
438 {
439 	int outl;
440 	if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
441 		return -1;
442 	return 0;
443 }
444 
445 
446 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
447 			  u8 *plain, size_t len)
448 {
449 	int outl;
450 	outl = len;
451 	if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
452 		return -1;
453 	return 0;
454 }
455 
456 
457 void crypto_cipher_deinit(struct crypto_cipher *ctx)
458 {
459 	EVP_CIPHER_CTX_cleanup(&ctx->enc);
460 	EVP_CIPHER_CTX_cleanup(&ctx->dec);
461 	os_free(ctx);
462 }
463 
464 
465 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
466 {
467 	DH *dh;
468 	struct wpabuf *pubkey = NULL, *privkey = NULL;
469 	size_t publen, privlen;
470 
471 	*priv = NULL;
472 	*publ = NULL;
473 
474 	dh = DH_new();
475 	if (dh == NULL)
476 		return NULL;
477 
478 	dh->g = BN_new();
479 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
480 		goto err;
481 
482 	dh->p = get_group5_prime();
483 	if (dh->p == NULL)
484 		goto err;
485 
486 	if (DH_generate_key(dh) != 1)
487 		goto err;
488 
489 	publen = BN_num_bytes(dh->pub_key);
490 	pubkey = wpabuf_alloc(publen);
491 	if (pubkey == NULL)
492 		goto err;
493 	privlen = BN_num_bytes(dh->priv_key);
494 	privkey = wpabuf_alloc(privlen);
495 	if (privkey == NULL)
496 		goto err;
497 
498 	BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
499 	BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
500 
501 	*priv = privkey;
502 	*publ = pubkey;
503 	return dh;
504 
505 err:
506 	wpabuf_free(pubkey);
507 	wpabuf_free(privkey);
508 	DH_free(dh);
509 	return NULL;
510 }
511 
512 
513 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
514 {
515 	DH *dh;
516 
517 	dh = DH_new();
518 	if (dh == NULL)
519 		return NULL;
520 
521 	dh->g = BN_new();
522 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
523 		goto err;
524 
525 	dh->p = get_group5_prime();
526 	if (dh->p == NULL)
527 		goto err;
528 
529 	dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
530 	if (dh->priv_key == NULL)
531 		goto err;
532 
533 	dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
534 	if (dh->pub_key == NULL)
535 		goto err;
536 
537 	if (DH_generate_key(dh) != 1)
538 		goto err;
539 
540 	return dh;
541 
542 err:
543 	DH_free(dh);
544 	return NULL;
545 }
546 
547 
548 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
549 				  const struct wpabuf *own_private)
550 {
551 	BIGNUM *pub_key;
552 	struct wpabuf *res = NULL;
553 	size_t rlen;
554 	DH *dh = ctx;
555 	int keylen;
556 
557 	if (ctx == NULL)
558 		return NULL;
559 
560 	pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
561 			    NULL);
562 	if (pub_key == NULL)
563 		return NULL;
564 
565 	rlen = DH_size(dh);
566 	res = wpabuf_alloc(rlen);
567 	if (res == NULL)
568 		goto err;
569 
570 	keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
571 	if (keylen < 0)
572 		goto err;
573 	wpabuf_put(res, keylen);
574 	BN_free(pub_key);
575 
576 	return res;
577 
578 err:
579 	BN_free(pub_key);
580 	wpabuf_free(res);
581 	return NULL;
582 }
583 
584 
585 void dh5_free(void *ctx)
586 {
587 	DH *dh;
588 	if (ctx == NULL)
589 		return;
590 	dh = ctx;
591 	DH_free(dh);
592 }
593 
594 
595 struct crypto_hash {
596 	HMAC_CTX ctx;
597 };
598 
599 
600 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 	switch (alg) {
607 #ifndef OPENSSL_NO_MD5
608 	case CRYPTO_HASH_ALG_HMAC_MD5:
609 		md = EVP_md5();
610 		break;
611 #endif /* OPENSSL_NO_MD5 */
612 #ifndef OPENSSL_NO_SHA
613 	case CRYPTO_HASH_ALG_HMAC_SHA1:
614 		md = EVP_sha1();
615 		break;
616 #endif /* OPENSSL_NO_SHA */
617 #ifndef OPENSSL_NO_SHA256
618 #ifdef CONFIG_SHA256
619 	case CRYPTO_HASH_ALG_HMAC_SHA256:
620 		md = EVP_sha256();
621 		break;
622 #endif /* CONFIG_SHA256 */
623 #endif /* OPENSSL_NO_SHA256 */
624 	default:
625 		return NULL;
626 	}
627 
628 	ctx = os_zalloc(sizeof(*ctx));
629 	if (ctx == NULL)
630 		return NULL;
631 	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 	if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
637 		os_free(ctx);
638 		return NULL;
639 	}
640 #endif /* openssl < 0.9.9 */
641 
642 	return ctx;
643 }
644 
645 
646 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
647 {
648 	if (ctx == NULL)
649 		return;
650 	HMAC_Update(&ctx->ctx, data, len);
651 }
652 
653 
654 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
655 {
656 	unsigned int mdlen;
657 	int res;
658 
659 	if (ctx == NULL)
660 		return -2;
661 
662 	if (mac == NULL || len == NULL) {
663 		os_free(ctx);
664 		return 0;
665 	}
666 
667 	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 	res = HMAC_Final(&ctx->ctx, mac, &mdlen);
673 #endif /* openssl < 0.9.9 */
674 	HMAC_CTX_cleanup(&ctx->ctx);
675 	os_free(ctx);
676 
677 	if (res == 1) {
678 		*len = mdlen;
679 		return 0;
680 	}
681 
682 	return -1;
683 }
684 
685 
686 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 	if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
696 				   ssid_len, 4096, buflen, buf) != 1)
697 		return -1;
698 #endif /* openssl < 0.9.8 */
699 	return 0;
700 }
701 
702 
703 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 	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 	if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
716 		return -1;
717 #endif /* openssl < 0.9.9 */
718 
719 	for (i = 0; i < num_elem; i++)
720 		HMAC_Update(&ctx, addr[i], len[i]);
721 
722 	mdlen = 20;
723 #if OPENSSL_VERSION_NUMBER < 0x00909000
724 	HMAC_Final(&ctx, mac, &mdlen);
725 	res = 1;
726 #else /* openssl < 0.9.9 */
727 	res = HMAC_Final(&ctx, mac, &mdlen);
728 #endif /* openssl < 0.9.9 */
729 	HMAC_CTX_cleanup(&ctx);
730 
731 	return res == 1 ? 0 : -1;
732 }
733 
734 
735 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
736 	       u8 *mac)
737 {
738 	return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
739 }
740 
741 
742 #ifdef CONFIG_SHA256
743 
744 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 	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 	if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
757 		return -1;
758 #endif /* openssl < 0.9.9 */
759 
760 	for (i = 0; i < num_elem; i++)
761 		HMAC_Update(&ctx, addr[i], len[i]);
762 
763 	mdlen = 32;
764 #if OPENSSL_VERSION_NUMBER < 0x00909000
765 	HMAC_Final(&ctx, mac, &mdlen);
766 	res = 1;
767 #else /* openssl < 0.9.9 */
768 	res = HMAC_Final(&ctx, mac, &mdlen);
769 #endif /* openssl < 0.9.9 */
770 	HMAC_CTX_cleanup(&ctx);
771 
772 	return res == 1 ? 0 : -1;
773 }
774 
775 
776 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
777 		size_t data_len, u8 *mac)
778 {
779 	return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
780 }
781 
782 #endif /* CONFIG_SHA256 */
783 
784 
785 int crypto_get_random(void *buf, size_t len)
786 {
787 	if (RAND_bytes(buf, len) != 1)
788 		return -1;
789 	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 struct crypto_bignum * crypto_bignum_init(void)
829 {
830 	return (struct crypto_bignum *) BN_new();
831 }
832 
833 
834 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
835 {
836 	BIGNUM *bn = BN_bin2bn(buf, len, NULL);
837 	return (struct crypto_bignum *) bn;
838 }
839 
840 
841 void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
842 {
843 	if (clear)
844 		BN_clear_free((BIGNUM *) n);
845 	else
846 		BN_free((BIGNUM *) n);
847 }
848 
849 
850 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 	if (padlen > buflen)
856 		return -1;
857 
858 	num_bytes = BN_num_bytes((const BIGNUM *) a);
859 	if ((size_t) num_bytes > buflen)
860 		return -1;
861 	if (padlen > (size_t) num_bytes)
862 		offset = padlen - num_bytes;
863 	else
864 		offset = 0;
865 
866 	os_memset(buf, 0, offset);
867 	BN_bn2bin((const BIGNUM *) a, buf + offset);
868 
869 	return num_bytes + offset;
870 }
871 
872 
873 int crypto_bignum_add(const struct crypto_bignum *a,
874 		      const struct crypto_bignum *b,
875 		      struct crypto_bignum *c)
876 {
877 	return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
878 		0 : -1;
879 }
880 
881 
882 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 	bnctx = BN_CTX_new();
890 	if (bnctx == NULL)
891 		return -1;
892 	res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
893 		     bnctx);
894 	BN_CTX_free(bnctx);
895 
896 	return res ? 0 : -1;
897 }
898 
899 
900 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 	bnctx = BN_CTX_new();
909 	if (bnctx == NULL)
910 		return -1;
911 	res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
912 			 (const BIGNUM *) c, bnctx);
913 	BN_CTX_free(bnctx);
914 
915 	return res ? 0 : -1;
916 }
917 
918 
919 int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
920 			 struct crypto_bignum *b)
921 {
922 	return BN_rshift((BIGNUM *) b, (const BIGNUM *) a, n) ? 0 : -1;
923 }
924 
925 
926 int crypto_bignum_inverse(const struct crypto_bignum *a,
927 			  const struct crypto_bignum *b,
928 			  struct crypto_bignum *c)
929 {
930 	BIGNUM *res;
931 	BN_CTX *bnctx;
932 
933 	bnctx = BN_CTX_new();
934 	if (bnctx == NULL)
935 		return -1;
936 	res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
937 			     (const BIGNUM *) b, bnctx);
938 	BN_CTX_free(bnctx);
939 
940 	return res ? 0 : -1;
941 }
942 
943 
944 int crypto_bignum_sub(const struct crypto_bignum *a,
945 		      const struct crypto_bignum *b,
946 		      struct crypto_bignum *c)
947 {
948 	return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
949 		0 : -1;
950 }
951 
952 
953 int crypto_bignum_div(const struct crypto_bignum *a,
954 		      const struct crypto_bignum *b,
955 		      struct crypto_bignum *c)
956 {
957 	int res;
958 
959 	BN_CTX *bnctx;
960 
961 	bnctx = BN_CTX_new();
962 	if (bnctx == NULL)
963 		return -1;
964 	res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
965 		     (const BIGNUM *) b, bnctx);
966 	BN_CTX_free(bnctx);
967 
968 	return res ? 0 : -1;
969 }
970 
971 
972 int crypto_bignum_mulmod(const struct crypto_bignum *a,
973 			 const struct crypto_bignum *b,
974 			 const struct crypto_bignum *c,
975 			 struct crypto_bignum *d)
976 {
977 	int res;
978 
979 	BN_CTX *bnctx;
980 
981 	bnctx = BN_CTX_new();
982 	if (bnctx == NULL)
983 		return -1;
984 	res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
985 			 (const BIGNUM *) c, bnctx);
986 	BN_CTX_free(bnctx);
987 
988 	return res ? 0 : -1;
989 }
990 
991 
992 int crypto_bignum_cmp(const struct crypto_bignum *a,
993 		      const struct crypto_bignum *b)
994 {
995 	return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
996 }
997 
998 
999 int crypto_bignum_bits(const struct crypto_bignum *a)
1000 {
1001 	return BN_num_bits((const BIGNUM *) a);
1002 }
1003 
1004 
1005 int crypto_bignum_is_zero(const struct crypto_bignum *a)
1006 {
1007 	return BN_is_zero((const BIGNUM *) a);
1008 }
1009 
1010 
1011 int crypto_bignum_is_one(const struct crypto_bignum *a)
1012 {
1013 	return BN_is_one((const BIGNUM *) a);
1014 }
1015 
1016 
1017 #ifdef CONFIG_ECC
1018 
1019 struct crypto_ec {
1020 	EC_GROUP *group;
1021 	BN_CTX *bnctx;
1022 	BIGNUM *prime;
1023 	BIGNUM *order;
1024 };
1025 
1026 struct crypto_ec * crypto_ec_init(int group)
1027 {
1028 	struct crypto_ec *e;
1029 	int nid;
1030 
1031 	/* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1032 	switch (group) {
1033 	case 19:
1034 		nid = NID_X9_62_prime256v1;
1035 		break;
1036 	case 20:
1037 		nid = NID_secp384r1;
1038 		break;
1039 	case 21:
1040 		nid = NID_secp521r1;
1041 		break;
1042 	case 25:
1043 		nid = NID_X9_62_prime192v1;
1044 		break;
1045 	case 26:
1046 		nid = NID_secp224r1;
1047 		break;
1048 	default:
1049 		return NULL;
1050 	}
1051 
1052 	e = os_zalloc(sizeof(*e));
1053 	if (e == NULL)
1054 		return NULL;
1055 
1056 	e->bnctx = BN_CTX_new();
1057 	e->group = EC_GROUP_new_by_curve_name(nid);
1058 	e->prime = BN_new();
1059 	e->order = BN_new();
1060 	if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1061 	    e->order == NULL ||
1062 	    !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
1063 	    !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1064 		crypto_ec_deinit(e);
1065 		e = NULL;
1066 	}
1067 
1068 	return e;
1069 }
1070 
1071 
1072 void crypto_ec_deinit(struct crypto_ec *e)
1073 {
1074 	if (e == NULL)
1075 		return;
1076 	BN_free(e->order);
1077 	EC_GROUP_free(e->group);
1078 	BN_CTX_free(e->bnctx);
1079 	os_free(e);
1080 }
1081 
1082 
1083 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1084 {
1085 	if (e == NULL)
1086 		return NULL;
1087 	return (struct crypto_ec_point *) EC_POINT_new(e->group);
1088 }
1089 
1090 
1091 size_t crypto_ec_prime_len(struct crypto_ec *e)
1092 {
1093 	return BN_num_bytes(e->prime);
1094 }
1095 
1096 
1097 size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1098 {
1099 	return BN_num_bits(e->prime);
1100 }
1101 
1102 
1103 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1104 {
1105 	return (const struct crypto_bignum *) e->prime;
1106 }
1107 
1108 
1109 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1110 {
1111 	return (const struct crypto_bignum *) e->order;
1112 }
1113 
1114 
1115 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1116 {
1117 	if (clear)
1118 		EC_POINT_clear_free((EC_POINT *) p);
1119 	else
1120 		EC_POINT_free((EC_POINT *) p);
1121 }
1122 
1123 
1124 int crypto_ec_point_to_bin(struct crypto_ec *e,
1125 			   const struct crypto_ec_point *point, u8 *x, u8 *y)
1126 {
1127 	BIGNUM *x_bn, *y_bn;
1128 	int ret = -1;
1129 	int len = BN_num_bytes(e->prime);
1130 
1131 	x_bn = BN_new();
1132 	y_bn = BN_new();
1133 
1134 	if (x_bn && y_bn &&
1135 	    EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1136 						x_bn, y_bn, e->bnctx)) {
1137 		if (x) {
1138 			crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1139 					     x, len, len);
1140 		}
1141 		if (y) {
1142 			crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1143 					     y, len, len);
1144 		}
1145 		ret = 0;
1146 	}
1147 
1148 	BN_free(x_bn);
1149 	BN_free(y_bn);
1150 	return ret;
1151 }
1152 
1153 
1154 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1155 						  const u8 *val)
1156 {
1157 	BIGNUM *x, *y;
1158 	EC_POINT *elem;
1159 	int len = BN_num_bytes(e->prime);
1160 
1161 	x = BN_bin2bn(val, len, NULL);
1162 	y = BN_bin2bn(val + len, len, NULL);
1163 	elem = EC_POINT_new(e->group);
1164 	if (x == NULL || y == NULL || elem == NULL) {
1165 		BN_free(x);
1166 		BN_free(y);
1167 		EC_POINT_free(elem);
1168 		return NULL;
1169 	}
1170 
1171 	if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1172 						 e->bnctx)) {
1173 		EC_POINT_free(elem);
1174 		elem = NULL;
1175 	}
1176 
1177 	BN_free(x);
1178 	BN_free(y);
1179 
1180 	return (struct crypto_ec_point *) elem;
1181 }
1182 
1183 
1184 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1185 			const struct crypto_ec_point *b,
1186 			struct crypto_ec_point *c)
1187 {
1188 	return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1189 			    (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1190 }
1191 
1192 
1193 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1194 			const struct crypto_bignum *b,
1195 			struct crypto_ec_point *res)
1196 {
1197 	return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1198 			    (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1199 		? 0 : -1;
1200 }
1201 
1202 
1203 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1204 {
1205 	return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1206 }
1207 
1208 
1209 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1210 				  struct crypto_ec_point *p,
1211 				  const struct crypto_bignum *x, int y_bit)
1212 {
1213 	if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1214 						     (const BIGNUM *) x, y_bit,
1215 						     e->bnctx) ||
1216 	    !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1217 		return -1;
1218 	return 0;
1219 }
1220 
1221 
1222 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1223 				   const struct crypto_ec_point *p)
1224 {
1225 	return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1226 }
1227 
1228 
1229 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1230 				const struct crypto_ec_point *p)
1231 {
1232 	return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
1233 }
1234 
1235 #endif /* CONFIG_ECC */
1236