Use OpenSSL for RC4 instead of internal implementation
[mech_eap.git] / src / crypto / crypto_openssl.c
1 /*
2  * WPA Supplicant / wrapper functions for libcrypto
3  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
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>
22
23 #include "common.h"
24 #include "crypto.h"
25
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 */
33
34
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)
37 {
38         EVP_MD_CTX ctx;
39         size_t i;
40         unsigned int mac_len;
41
42         EVP_MD_CTX_init(&ctx);
43 #ifdef CONFIG_FIPS
44 #ifdef OPENSSL_FIPS
45         if (non_fips)
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));
52                 return -1;
53         }
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 "
57                                    "failed: %s",
58                                    ERR_error_string(ERR_get_error(), NULL));
59                         return -1;
60                 }
61         }
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));
65                 return -1;
66         }
67
68         return 0;
69 }
70
71
72 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
73 {
74         return openssl_digest_vector(EVP_md4(), 0, num_elem, addr, len, mac);
75 }
76
77
78 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
79 {
80         u8 pkey[8], next, tmp;
81         int i;
82         DES_key_schedule ks;
83
84         /* Add parity bits to the key */
85         next = 0;
86         for (i = 0; i < 7; i++) {
87                 tmp = key[i];
88                 pkey[i] = (tmp >> i) | next | 1;
89                 next = tmp << (7 - i);
90         }
91         pkey[i] = next | 1;
92
93         DES_set_key(&pkey, &ks);
94         DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
95                         DES_ENCRYPT);
96 }
97
98
99 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
100              u8 *data, size_t data_len)
101 {
102 #ifdef OPENSSL_NO_RC4
103         return -1;
104 #else /* OPENSSL_NO_RC4 */
105         EVP_CIPHER_CTX ctx;
106         int outl;
107         int res = -1;
108         unsigned char skip_buf[16];
109
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))
115                 goto out;
116
117         while (skip >= sizeof(skip_buf)) {
118                 size_t len = skip;
119                 if (len > sizeof(skip_buf))
120                         len = sizeof(skip_buf);
121                 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
122                         goto out;
123                 skip -= len;
124         }
125
126         if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
127                 res = 0;
128
129 out:
130         EVP_CIPHER_CTX_cleanup(&ctx);
131         return res;
132 #endif /* OPENSSL_NO_RC4 */
133 }
134
135
136 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
137 {
138         return openssl_digest_vector(EVP_md5(), 0, num_elem, addr, len, mac);
139 }
140
141
142 #ifdef CONFIG_FIPS
143 int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
144                               const size_t *len, u8 *mac)
145 {
146         return openssl_digest_vector(EVP_md5(), 1, num_elem, addr, len, mac);
147 }
148 #endif /* CONFIG_FIPS */
149
150
151 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
152 {
153         return openssl_digest_vector(EVP_sha1(), 0, num_elem, addr, len, mac);
154 }
155
156
157 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
158                   u8 *mac)
159 {
160         return openssl_digest_vector(EVP_sha256(), 0, num_elem, addr, len,
161                                      mac);
162 }
163
164
165 void * aes_encrypt_init(const u8 *key, size_t len)
166 {
167         AES_KEY *ak;
168         ak = os_malloc(sizeof(*ak));
169         if (ak == NULL)
170                 return NULL;
171         if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
172                 os_free(ak);
173                 return NULL;
174         }
175         return ak;
176 }
177
178
179 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
180 {
181         AES_encrypt(plain, crypt, ctx);
182 }
183
184
185 void aes_encrypt_deinit(void *ctx)
186 {
187         os_free(ctx);
188 }
189
190
191 void * aes_decrypt_init(const u8 *key, size_t len)
192 {
193         AES_KEY *ak;
194         ak = os_malloc(sizeof(*ak));
195         if (ak == NULL)
196                 return NULL;
197         if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
198                 os_free(ak);
199                 return NULL;
200         }
201         return ak;
202 }
203
204
205 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
206 {
207         AES_decrypt(crypt, plain, ctx);
208 }
209
210
211 void aes_decrypt_deinit(void *ctx)
212 {
213         os_free(ctx);
214 }
215
216
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)
221 {
222         BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
223         int ret = -1;
224         BN_CTX *ctx;
225
226         ctx = BN_CTX_new();
227         if (ctx == NULL)
228                 return -1;
229
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();
234
235         if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
236             bn_result == NULL)
237                 goto error;
238
239         if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
240                 goto error;
241
242         *result_len = BN_bn2bin(bn_result, result);
243         ret = 0;
244
245 error:
246         BN_free(bn_base);
247         BN_free(bn_exp);
248         BN_free(bn_modulus);
249         BN_free(bn_result);
250         BN_CTX_free(ctx);
251         return ret;
252 }
253
254
255 struct crypto_cipher {
256         EVP_CIPHER_CTX enc;
257         EVP_CIPHER_CTX dec;
258 };
259
260
261 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
262                                           const u8 *iv, const u8 *key,
263                                           size_t key_len)
264 {
265         struct crypto_cipher *ctx;
266         const EVP_CIPHER *cipher;
267
268         ctx = os_zalloc(sizeof(*ctx));
269         if (ctx == NULL)
270                 return NULL;
271
272         switch (alg) {
273 #ifndef OPENSSL_NO_RC4
274         case CRYPTO_CIPHER_ALG_RC4:
275                 cipher = EVP_rc4();
276                 break;
277 #endif /* OPENSSL_NO_RC4 */
278 #ifndef OPENSSL_NO_AES
279         case CRYPTO_CIPHER_ALG_AES:
280                 switch (key_len) {
281                 case 16:
282                         cipher = EVP_aes_128_cbc();
283                         break;
284                 case 24:
285                         cipher = EVP_aes_192_cbc();
286                         break;
287                 case 32:
288                         cipher = EVP_aes_256_cbc();
289                         break;
290                 default:
291                         os_free(ctx);
292                         return NULL;
293                 }
294                 break;
295 #endif /* OPENSSL_NO_AES */
296 #ifndef OPENSSL_NO_DES
297         case CRYPTO_CIPHER_ALG_3DES:
298                 cipher = EVP_des_ede3_cbc();
299                 break;
300         case CRYPTO_CIPHER_ALG_DES:
301                 cipher = EVP_des_cbc();
302                 break;
303 #endif /* OPENSSL_NO_DES */
304 #ifndef OPENSSL_NO_RC2
305         case CRYPTO_CIPHER_ALG_RC2:
306                 cipher = EVP_rc2_ecb();
307                 break;
308 #endif /* OPENSSL_NO_RC2 */
309         default:
310                 os_free(ctx);
311                 return NULL;
312         }
313
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);
320                 os_free(ctx);
321                 return NULL;
322         }
323
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);
331                 os_free(ctx);
332                 return NULL;
333         }
334
335         return ctx;
336 }
337
338
339 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
340                           u8 *crypt, size_t len)
341 {
342         int outl;
343         if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
344                 return -1;
345         return 0;
346 }
347
348
349 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
350                           u8 *plain, size_t len)
351 {
352         int outl;
353         outl = len;
354         if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
355                 return -1;
356         return 0;
357 }
358
359
360 void crypto_cipher_deinit(struct crypto_cipher *ctx)
361 {
362         EVP_CIPHER_CTX_cleanup(&ctx->enc);
363         EVP_CIPHER_CTX_cleanup(&ctx->dec);
364         os_free(ctx);
365 }