crypto_openssl.c revision 04949598a23f501be6eec21697465fd46a28840a
1/*
2 * WPA Supplicant / wrapper functions for libcrypto
3 * Copyright (c) 2004-2012, 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
19#include "common.h"
20#include "wpabuf.h"
21#include "dh_group5.h"
22#include "crypto.h"
23
24#if OPENSSL_VERSION_NUMBER < 0x00907000
25#define DES_key_schedule des_key_schedule
26#define DES_cblock des_cblock
27#define DES_set_key(key, schedule) des_set_key((key), *(schedule))
28#define DES_ecb_encrypt(input, output, ks, enc) \
29	des_ecb_encrypt((input), (output), *(ks), (enc))
30#endif /* openssl < 0.9.7 */
31
32static BIGNUM * get_group5_prime(void)
33{
34#if OPENSSL_VERSION_NUMBER < 0x00908000
35	static const unsigned char RFC3526_PRIME_1536[] = {
36		0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
37		0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
38		0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
39		0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
40		0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
41		0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
42		0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
43		0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
44		0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
45		0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
46		0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
47		0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
48		0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
49		0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
50		0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
51		0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
52	};
53        return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
54#else /* openssl < 0.9.8 */
55	return get_rfc3526_prime_1536(NULL);
56#endif /* openssl < 0.9.8 */
57}
58
59#if OPENSSL_VERSION_NUMBER < 0x00908000
60#ifndef OPENSSL_NO_SHA256
61#ifndef OPENSSL_FIPS
62#define NO_SHA256_WRAPPER
63#endif
64#endif
65
66#endif /* openssl < 0.9.8 */
67
68#ifdef OPENSSL_NO_SHA256
69#define NO_SHA256_WRAPPER
70#endif
71
72static int openssl_digest_vector(const EVP_MD *type, int non_fips,
73				 size_t num_elem, const u8 *addr[],
74				 const size_t *len, u8 *mac)
75{
76	EVP_MD_CTX ctx;
77	size_t i;
78	unsigned int mac_len;
79
80	EVP_MD_CTX_init(&ctx);
81#ifdef CONFIG_FIPS
82#ifdef OPENSSL_FIPS
83	if (non_fips)
84		EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
85#endif /* OPENSSL_FIPS */
86#endif /* CONFIG_FIPS */
87	if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
88		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
89			   ERR_error_string(ERR_get_error(), NULL));
90		return -1;
91	}
92	for (i = 0; i < num_elem; i++) {
93		if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
94			wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
95				   "failed: %s",
96				   ERR_error_string(ERR_get_error(), NULL));
97			return -1;
98		}
99	}
100	if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
101		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
102			   ERR_error_string(ERR_get_error(), NULL));
103		return -1;
104	}
105
106	return 0;
107}
108
109
110int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
111{
112	return openssl_digest_vector(EVP_md4(), 0, num_elem, addr, len, mac);
113}
114
115
116void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
117{
118	u8 pkey[8], next, tmp;
119	int i;
120	DES_key_schedule ks;
121
122	/* Add parity bits to the key */
123	next = 0;
124	for (i = 0; i < 7; i++) {
125		tmp = key[i];
126		pkey[i] = (tmp >> i) | next | 1;
127		next = tmp << (7 - i);
128	}
129	pkey[i] = next | 1;
130
131	DES_set_key(&pkey, &ks);
132	DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
133			DES_ENCRYPT);
134}
135
136
137int rc4_skip(const u8 *key, size_t keylen, size_t skip,
138	     u8 *data, size_t data_len)
139{
140#ifdef OPENSSL_NO_RC4
141	return -1;
142#else /* OPENSSL_NO_RC4 */
143	EVP_CIPHER_CTX ctx;
144	int outl;
145	int res = -1;
146	unsigned char skip_buf[16];
147
148	EVP_CIPHER_CTX_init(&ctx);
149	if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
150	    !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
151	    !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
152	    !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
153		goto out;
154
155	while (skip >= sizeof(skip_buf)) {
156		size_t len = skip;
157		if (len > sizeof(skip_buf))
158			len = sizeof(skip_buf);
159		if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
160			goto out;
161		skip -= len;
162	}
163
164	if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
165		res = 0;
166
167out:
168	EVP_CIPHER_CTX_cleanup(&ctx);
169	return res;
170#endif /* OPENSSL_NO_RC4 */
171}
172
173
174int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
175{
176	return openssl_digest_vector(EVP_md5(), 0, num_elem, addr, len, mac);
177}
178
179
180#ifdef CONFIG_FIPS
181int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
182			      const size_t *len, u8 *mac)
183{
184	return openssl_digest_vector(EVP_md5(), 1, num_elem, addr, len, mac);
185}
186#endif /* CONFIG_FIPS */
187
188
189int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
190{
191	return openssl_digest_vector(EVP_sha1(), 0, num_elem, addr, len, mac);
192}
193
194
195#ifndef NO_SHA256_WRAPPER
196int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
197		  u8 *mac)
198{
199	return openssl_digest_vector(EVP_sha256(), 0, num_elem, addr, len,
200				     mac);
201}
202#endif /* NO_SHA256_WRAPPER */
203
204
205void * aes_encrypt_init(const u8 *key, size_t len)
206{
207	AES_KEY *ak;
208	ak = os_malloc(sizeof(*ak));
209	if (ak == NULL)
210		return NULL;
211	if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
212		os_free(ak);
213		return NULL;
214	}
215	return ak;
216}
217
218
219void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
220{
221	AES_encrypt(plain, crypt, ctx);
222}
223
224
225void aes_encrypt_deinit(void *ctx)
226{
227	os_free(ctx);
228}
229
230
231void * aes_decrypt_init(const u8 *key, size_t len)
232{
233	AES_KEY *ak;
234	ak = os_malloc(sizeof(*ak));
235	if (ak == NULL)
236		return NULL;
237	if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
238		os_free(ak);
239		return NULL;
240	}
241	return ak;
242}
243
244
245void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
246{
247	AES_decrypt(crypt, plain, ctx);
248}
249
250
251void aes_decrypt_deinit(void *ctx)
252{
253	os_free(ctx);
254}
255
256
257int crypto_mod_exp(const u8 *base, size_t base_len,
258		   const u8 *power, size_t power_len,
259		   const u8 *modulus, size_t modulus_len,
260		   u8 *result, size_t *result_len)
261{
262	BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
263	int ret = -1;
264	BN_CTX *ctx;
265
266	ctx = BN_CTX_new();
267	if (ctx == NULL)
268		return -1;
269
270	bn_base = BN_bin2bn(base, base_len, NULL);
271	bn_exp = BN_bin2bn(power, power_len, NULL);
272	bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
273	bn_result = BN_new();
274
275	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
276	    bn_result == NULL)
277		goto error;
278
279	if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
280		goto error;
281
282	*result_len = BN_bn2bin(bn_result, result);
283	ret = 0;
284
285error:
286	BN_free(bn_base);
287	BN_free(bn_exp);
288	BN_free(bn_modulus);
289	BN_free(bn_result);
290	BN_CTX_free(ctx);
291	return ret;
292}
293
294
295struct crypto_cipher {
296	EVP_CIPHER_CTX enc;
297	EVP_CIPHER_CTX dec;
298};
299
300
301struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
302					  const u8 *iv, const u8 *key,
303					  size_t key_len)
304{
305	struct crypto_cipher *ctx;
306	const EVP_CIPHER *cipher;
307
308	ctx = os_zalloc(sizeof(*ctx));
309	if (ctx == NULL)
310		return NULL;
311
312	switch (alg) {
313#ifndef OPENSSL_NO_RC4
314	case CRYPTO_CIPHER_ALG_RC4:
315		cipher = EVP_rc4();
316		break;
317#endif /* OPENSSL_NO_RC4 */
318#ifndef OPENSSL_NO_AES
319	case CRYPTO_CIPHER_ALG_AES:
320		switch (key_len) {
321		case 16:
322			cipher = EVP_aes_128_cbc();
323			break;
324		case 24:
325			cipher = EVP_aes_192_cbc();
326			break;
327		case 32:
328			cipher = EVP_aes_256_cbc();
329			break;
330		default:
331			os_free(ctx);
332			return NULL;
333		}
334		break;
335#endif /* OPENSSL_NO_AES */
336#ifndef OPENSSL_NO_DES
337	case CRYPTO_CIPHER_ALG_3DES:
338		cipher = EVP_des_ede3_cbc();
339		break;
340	case CRYPTO_CIPHER_ALG_DES:
341		cipher = EVP_des_cbc();
342		break;
343#endif /* OPENSSL_NO_DES */
344#ifndef OPENSSL_NO_RC2
345	case CRYPTO_CIPHER_ALG_RC2:
346		cipher = EVP_rc2_ecb();
347		break;
348#endif /* OPENSSL_NO_RC2 */
349	default:
350		os_free(ctx);
351		return NULL;
352	}
353
354	EVP_CIPHER_CTX_init(&ctx->enc);
355	EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
356	if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
357	    !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
358	    !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
359		EVP_CIPHER_CTX_cleanup(&ctx->enc);
360		os_free(ctx);
361		return NULL;
362	}
363
364	EVP_CIPHER_CTX_init(&ctx->dec);
365	EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
366	if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
367	    !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
368	    !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
369		EVP_CIPHER_CTX_cleanup(&ctx->enc);
370		EVP_CIPHER_CTX_cleanup(&ctx->dec);
371		os_free(ctx);
372		return NULL;
373	}
374
375	return ctx;
376}
377
378
379int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
380			  u8 *crypt, size_t len)
381{
382	int outl;
383	if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
384		return -1;
385	return 0;
386}
387
388
389int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
390			  u8 *plain, size_t len)
391{
392	int outl;
393	outl = len;
394	if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
395		return -1;
396	return 0;
397}
398
399
400void crypto_cipher_deinit(struct crypto_cipher *ctx)
401{
402	EVP_CIPHER_CTX_cleanup(&ctx->enc);
403	EVP_CIPHER_CTX_cleanup(&ctx->dec);
404	os_free(ctx);
405}
406
407
408void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
409{
410	DH *dh;
411	struct wpabuf *pubkey = NULL, *privkey = NULL;
412	size_t publen, privlen;
413
414	*priv = NULL;
415	*publ = NULL;
416
417	dh = DH_new();
418	if (dh == NULL)
419		return NULL;
420
421	dh->g = BN_new();
422	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
423		goto err;
424
425	dh->p = get_group5_prime();
426	if (dh->p == NULL)
427		goto err;
428
429	if (DH_generate_key(dh) != 1)
430		goto err;
431
432	publen = BN_num_bytes(dh->pub_key);
433	pubkey = wpabuf_alloc(publen);
434	if (pubkey == NULL)
435		goto err;
436	privlen = BN_num_bytes(dh->priv_key);
437	privkey = wpabuf_alloc(privlen);
438	if (privkey == NULL)
439		goto err;
440
441	BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
442	BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
443
444	*priv = privkey;
445	*publ = pubkey;
446	return dh;
447
448err:
449	wpabuf_free(pubkey);
450	wpabuf_free(privkey);
451	DH_free(dh);
452	return NULL;
453}
454
455
456void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
457{
458	DH *dh;
459
460	dh = DH_new();
461	if (dh == NULL)
462		return NULL;
463
464	dh->g = BN_new();
465	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
466		goto err;
467
468	dh->p = get_group5_prime();
469	if (dh->p == NULL)
470		goto err;
471
472	dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
473	if (dh->priv_key == NULL)
474		goto err;
475
476	dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
477	if (dh->pub_key == NULL)
478		goto err;
479
480	if (DH_generate_key(dh) != 1)
481		goto err;
482
483	return dh;
484
485err:
486	DH_free(dh);
487	return NULL;
488}
489
490
491struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
492				  const struct wpabuf *own_private)
493{
494	BIGNUM *pub_key;
495	struct wpabuf *res = NULL;
496	size_t rlen;
497	DH *dh = ctx;
498	int keylen;
499
500	if (ctx == NULL)
501		return NULL;
502
503	pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
504			    NULL);
505	if (pub_key == NULL)
506		return NULL;
507
508	rlen = DH_size(dh);
509	res = wpabuf_alloc(rlen);
510	if (res == NULL)
511		goto err;
512
513	keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
514	if (keylen < 0)
515		goto err;
516	wpabuf_put(res, keylen);
517	BN_free(pub_key);
518
519	return res;
520
521err:
522	BN_free(pub_key);
523	wpabuf_free(res);
524	return NULL;
525}
526
527
528void dh5_free(void *ctx)
529{
530	DH *dh;
531	if (ctx == NULL)
532		return;
533	dh = ctx;
534	DH_free(dh);
535}
536
537
538struct crypto_hash {
539	HMAC_CTX ctx;
540};
541
542
543struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
544				      size_t key_len)
545{
546	struct crypto_hash *ctx;
547	const EVP_MD *md;
548
549	switch (alg) {
550#ifndef OPENSSL_NO_MD5
551	case CRYPTO_HASH_ALG_HMAC_MD5:
552		md = EVP_md5();
553		break;
554#endif /* OPENSSL_NO_MD5 */
555#ifndef OPENSSL_NO_SHA
556	case CRYPTO_HASH_ALG_HMAC_SHA1:
557		md = EVP_sha1();
558		break;
559#endif /* OPENSSL_NO_SHA */
560#ifndef OPENSSL_NO_SHA256
561#ifdef CONFIG_SHA256
562	case CRYPTO_HASH_ALG_HMAC_SHA256:
563		md = EVP_sha256();
564		break;
565#endif /* CONFIG_SHA256 */
566#endif /* OPENSSL_NO_SHA256 */
567	default:
568		return NULL;
569	}
570
571	ctx = os_zalloc(sizeof(*ctx));
572	if (ctx == NULL)
573		return NULL;
574
575#if OPENSSL_VERSION_NUMBER < 0x00909000
576	HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
577#else /* openssl < 0.9.9 */
578	if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
579		os_free(ctx);
580		return NULL;
581	}
582#endif /* openssl < 0.9.9 */
583
584	return ctx;
585}
586
587
588void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
589{
590	if (ctx == NULL)
591		return;
592	HMAC_Update(&ctx->ctx, data, len);
593}
594
595
596int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
597{
598	unsigned int mdlen;
599	int res;
600
601	if (ctx == NULL)
602		return -2;
603
604	if (mac == NULL || len == NULL) {
605		os_free(ctx);
606		return 0;
607	}
608
609	mdlen = *len;
610#if OPENSSL_VERSION_NUMBER < 0x00909000
611	HMAC_Final(&ctx->ctx, mac, &mdlen);
612	res = 1;
613#else /* openssl < 0.9.9 */
614	res = HMAC_Final(&ctx->ctx, mac, &mdlen);
615#endif /* openssl < 0.9.9 */
616	HMAC_CTX_cleanup(&ctx->ctx);
617	os_free(ctx);
618
619	if (res == 1) {
620		*len = mdlen;
621		return 0;
622	}
623
624	return -1;
625}
626