2 * WPA Supplicant / wrapper functions for libcrypto
3 * Copyright (c) 2004-2009, 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/err.h>
18 #include <openssl/des.h>
19 #include <openssl/aes.h>
20 #include <openssl/bn.h>
21 #include <openssl/evp.h>
26 #if OPENSSL_VERSION_NUMBER < 0x00907000
27 #define DES_key_schedule des_key_schedule
28 #define DES_cblock des_cblock
29 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
30 #define DES_ecb_encrypt(input, output, ks, enc) \
31 des_ecb_encrypt((input), (output), *(ks), (enc))
32 #endif /* openssl < 0.9.7 */
35 int openssl_digest_vector(const EVP_MD *type, int non_fips, size_t num_elem,
36 const u8 *addr[], const size_t *len, u8 *mac)
42 EVP_MD_CTX_init(&ctx);
46 EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
47 #endif /* OPENSSL_FIPS */
48 #endif /* CONFIG_FIPS */
49 if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
50 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
51 ERR_error_string(ERR_get_error(), NULL));
54 for (i = 0; i < num_elem; i++) {
55 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
56 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
58 ERR_error_string(ERR_get_error(), NULL));
62 if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
63 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
64 ERR_error_string(ERR_get_error(), NULL));
72 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
74 return openssl_digest_vector(EVP_md4(), 0, num_elem, addr, len, mac);
78 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
80 u8 pkey[8], next, tmp;
84 /* Add parity bits to the key */
86 for (i = 0; i < 7; i++) {
88 pkey[i] = (tmp >> i) | next | 1;
89 next = tmp << (7 - i);
93 DES_set_key(&pkey, &ks);
94 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
99 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
100 u8 *data, size_t data_len)
102 #ifdef OPENSSL_NO_RC4
104 #else /* OPENSSL_NO_RC4 */
108 unsigned char skip_buf[16];
110 EVP_CIPHER_CTX_init(&ctx);
111 if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
112 !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
113 !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
114 !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
117 while (skip >= sizeof(skip_buf)) {
119 if (len > sizeof(skip_buf))
120 len = sizeof(skip_buf);
121 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
126 if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
130 EVP_CIPHER_CTX_cleanup(&ctx);
132 #endif /* OPENSSL_NO_RC4 */
136 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
138 return openssl_digest_vector(EVP_md5(), 0, num_elem, addr, len, mac);
143 int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
144 const size_t *len, u8 *mac)
146 return openssl_digest_vector(EVP_md5(), 1, num_elem, addr, len, mac);
148 #endif /* CONFIG_FIPS */
151 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
153 return openssl_digest_vector(EVP_sha1(), 0, num_elem, addr, len, mac);
157 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
160 return openssl_digest_vector(EVP_sha256(), 0, num_elem, addr, len,
165 void * aes_encrypt_init(const u8 *key, size_t len)
168 ak = os_malloc(sizeof(*ak));
171 if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
179 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
181 AES_encrypt(plain, crypt, ctx);
185 void aes_encrypt_deinit(void *ctx)
191 void * aes_decrypt_init(const u8 *key, size_t len)
194 ak = os_malloc(sizeof(*ak));
197 if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
205 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
207 AES_decrypt(crypt, plain, ctx);
211 void aes_decrypt_deinit(void *ctx)
217 int crypto_mod_exp(const u8 *base, size_t base_len,
218 const u8 *power, size_t power_len,
219 const u8 *modulus, size_t modulus_len,
220 u8 *result, size_t *result_len)
222 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
230 bn_base = BN_bin2bn(base, base_len, NULL);
231 bn_exp = BN_bin2bn(power, power_len, NULL);
232 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
233 bn_result = BN_new();
235 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
239 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
242 *result_len = BN_bn2bin(bn_result, result);
255 struct crypto_cipher {
261 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
262 const u8 *iv, const u8 *key,
265 struct crypto_cipher *ctx;
266 const EVP_CIPHER *cipher;
268 ctx = os_zalloc(sizeof(*ctx));
273 #ifndef OPENSSL_NO_RC4
274 case CRYPTO_CIPHER_ALG_RC4:
277 #endif /* OPENSSL_NO_RC4 */
278 #ifndef OPENSSL_NO_AES
279 case CRYPTO_CIPHER_ALG_AES:
282 cipher = EVP_aes_128_cbc();
285 cipher = EVP_aes_192_cbc();
288 cipher = EVP_aes_256_cbc();
295 #endif /* OPENSSL_NO_AES */
296 #ifndef OPENSSL_NO_DES
297 case CRYPTO_CIPHER_ALG_3DES:
298 cipher = EVP_des_ede3_cbc();
300 case CRYPTO_CIPHER_ALG_DES:
301 cipher = EVP_des_cbc();
303 #endif /* OPENSSL_NO_DES */
304 #ifndef OPENSSL_NO_RC2
305 case CRYPTO_CIPHER_ALG_RC2:
306 cipher = EVP_rc2_ecb();
308 #endif /* OPENSSL_NO_RC2 */
314 EVP_CIPHER_CTX_init(&ctx->enc);
315 EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
316 if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
317 !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
318 !EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, key, iv)) {
319 EVP_CIPHER_CTX_cleanup(&ctx->enc);
324 EVP_CIPHER_CTX_init(&ctx->dec);
325 EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
326 if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
327 !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
328 !EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, key, iv)) {
329 EVP_CIPHER_CTX_cleanup(&ctx->enc);
330 EVP_CIPHER_CTX_cleanup(&ctx->dec);
339 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
340 u8 *crypt, size_t len)
343 if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
349 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
350 u8 *plain, size_t len)
354 if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
360 void crypto_cipher_deinit(struct crypto_cipher *ctx)
362 EVP_CIPHER_CTX_cleanup(&ctx->enc);
363 EVP_CIPHER_CTX_cleanup(&ctx->dec);