2 * WPA Supplicant / wrapper functions for libcrypto
3 * Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <openssl/opensslv.h>
17 #include <openssl/md4.h>
18 #include <openssl/md5.h>
19 #include <openssl/sha.h>
20 #include <openssl/des.h>
21 #include <openssl/aes.h>
22 #include <openssl/bn.h>
23 #include <openssl/evp.h>
28 #if OPENSSL_VERSION_NUMBER < 0x00907000
29 #define DES_key_schedule des_key_schedule
30 #define DES_cblock des_cblock
31 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
32 #define DES_ecb_encrypt(input, output, ks, enc) \
33 des_ecb_encrypt((input), (output), *(ks), (enc))
34 #endif /* openssl < 0.9.7 */
37 void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
43 for (i = 0; i < num_elem; i++)
44 MD4_Update(&ctx, addr[i], len[i]);
49 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
51 u8 pkey[8], next, tmp;
55 /* Add parity bits to the key */
57 for (i = 0; i < 7; i++) {
59 pkey[i] = (tmp >> i) | next | 1;
60 next = tmp << (7 - i);
64 DES_set_key(&pkey, &ks);
65 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
70 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
76 for (i = 0; i < num_elem; i++)
77 MD5_Update(&ctx, addr[i], len[i]);
82 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
88 for (i = 0; i < num_elem; i++)
89 SHA1_Update(&ctx, addr[i], len[i]);
90 SHA1_Final(mac, &ctx);
94 #ifndef CONFIG_NO_FIPS186_2_PRF
95 static void sha1_transform(u8 *state, const u8 data[64])
98 os_memset(&context, 0, sizeof(context));
99 os_memcpy(&context.h0, state, 5 * 4);
100 SHA1_Transform(&context, data);
101 os_memcpy(state, &context.h0, 5 * 4);
105 int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
113 if (seed_len > sizeof(xkey))
114 seed_len = sizeof(xkey);
116 /* FIPS 186-2 + change notice 1 */
118 os_memcpy(xkey, seed, seed_len);
119 os_memset(xkey + seed_len, 0, 64 - seed_len);
127 for (j = 0; j < m; j++) {
129 for (i = 0; i < 2; i++) {
130 /* XVAL = (XKEY + XSEED_j) mod 2^b */
132 /* w_i = G(t, XVAL) */
133 os_memcpy(_t, t, 20);
134 sha1_transform((u8 *) _t, xkey);
135 _t[0] = host_to_be32(_t[0]);
136 _t[1] = host_to_be32(_t[1]);
137 _t[2] = host_to_be32(_t[2]);
138 _t[3] = host_to_be32(_t[3]);
139 _t[4] = host_to_be32(_t[4]);
140 os_memcpy(xpos, _t, 20);
142 /* XKEY = (1 + XKEY + w_i) mod 2^b */
144 for (k = 19; k >= 0; k--) {
145 carry += xkey[k] + xpos[k];
146 xkey[k] = carry & 0xff;
157 #endif /* CONFIG_NO_FIPS186_2_PRF */
160 void * aes_encrypt_init(const u8 *key, size_t len)
163 ak = os_malloc(sizeof(*ak));
166 if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
174 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
176 AES_encrypt(plain, crypt, ctx);
180 void aes_encrypt_deinit(void *ctx)
186 void * aes_decrypt_init(const u8 *key, size_t len)
189 ak = os_malloc(sizeof(*ak));
192 if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
200 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
202 AES_decrypt(crypt, plain, ctx);
206 void aes_decrypt_deinit(void *ctx)
212 int crypto_mod_exp(const u8 *base, size_t base_len,
213 const u8 *power, size_t power_len,
214 const u8 *modulus, size_t modulus_len,
215 u8 *result, size_t *result_len)
217 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
225 bn_base = BN_bin2bn(base, base_len, NULL);
226 bn_exp = BN_bin2bn(power, power_len, NULL);
227 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
228 bn_result = BN_new();
230 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
234 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
237 *result_len = BN_bn2bin(bn_result, result);
250 struct crypto_cipher {
256 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
257 const u8 *iv, const u8 *key,
260 struct crypto_cipher *ctx;
261 const EVP_CIPHER *cipher;
263 ctx = os_zalloc(sizeof(*ctx));
268 #ifndef OPENSSL_NO_RC4
269 case CRYPTO_CIPHER_ALG_RC4:
272 #endif /* OPENSSL_NO_RC4 */
273 #ifndef OPENSSL_NO_AES
274 case CRYPTO_CIPHER_ALG_AES:
277 cipher = EVP_aes_128_cbc();
280 cipher = EVP_aes_192_cbc();
283 cipher = EVP_aes_256_cbc();
290 #endif /* OPENSSL_NO_AES */
291 #ifndef OPENSSL_NO_DES
292 case CRYPTO_CIPHER_ALG_3DES:
293 cipher = EVP_des_ede3_cbc();
295 case CRYPTO_CIPHER_ALG_DES:
296 cipher = EVP_des_cbc();
298 #endif /* OPENSSL_NO_DES */
299 #ifndef OPENSSL_NO_RC2
300 case CRYPTO_CIPHER_ALG_RC2:
301 cipher = EVP_rc2_ecb();
303 #endif /* OPENSSL_NO_RC2 */
309 EVP_CIPHER_CTX_init(&ctx->enc);
310 EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
311 if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
312 !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
313 !EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, key, iv)) {
314 EVP_CIPHER_CTX_cleanup(&ctx->enc);
319 EVP_CIPHER_CTX_init(&ctx->dec);
320 EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
321 if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
322 !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
323 !EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, key, iv)) {
324 EVP_CIPHER_CTX_cleanup(&ctx->enc);
325 EVP_CIPHER_CTX_cleanup(&ctx->dec);
334 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
335 u8 *crypt, size_t len)
338 if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
344 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
345 u8 *plain, size_t len)
349 if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
355 void crypto_cipher_deinit(struct crypto_cipher *ctx)
357 EVP_CIPHER_CTX_cleanup(&ctx->enc);
358 EVP_CIPHER_CTX_cleanup(&ctx->dec);