crypto_openssl.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant / wrapper functions for libcrypto
00003  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 #include <openssl/opensslv.h>
00017 #include <openssl/err.h>
00018 #include <openssl/des.h>
00019 #include <openssl/aes.h>
00020 #include <openssl/bn.h>
00021 #include <openssl/evp.h>
00022 #include <openssl/dh.h>
00023 
00024 #include "common.h"
00025 #include "wpabuf.h"
00026 #include "dh_group5.h"
00027 #include "crypto.h"
00028 
00029 #if OPENSSL_VERSION_NUMBER < 0x00907000
00030 #define DES_key_schedule des_key_schedule
00031 #define DES_cblock des_cblock
00032 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
00033 #define DES_ecb_encrypt(input, output, ks, enc) \
00034         des_ecb_encrypt((input), (output), *(ks), (enc))
00035 #endif /* openssl < 0.9.7 */
00036 
00037 static BIGNUM * get_group5_prime(void)
00038 {
00039 #if OPENSSL_VERSION_NUMBER < 0x00908000
00040         static const unsigned char RFC3526_PRIME_1536[] = {
00041                 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
00042                 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
00043                 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
00044                 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
00045                 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
00046                 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
00047                 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
00048                 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
00049                 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
00050                 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
00051                 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
00052                 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
00053                 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
00054                 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
00055                 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
00056                 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
00057         };
00058         return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
00059 #else /* openssl < 0.9.8 */
00060         return get_rfc3526_prime_1536(NULL);
00061 #endif /* openssl < 0.9.8 */
00062 }
00063 
00064 #if OPENSSL_VERSION_NUMBER < 0x00908000
00065 #ifndef OPENSSL_NO_SHA256
00066 #ifndef OPENSSL_FIPS
00067 #define NO_SHA256_WRAPPER
00068 #endif
00069 #endif
00070 
00071 #endif /* openssl < 0.9.8 */
00072 
00073 #ifdef OPENSSL_NO_SHA256
00074 #define NO_SHA256_WRAPPER
00075 #endif
00076 
00077 static int openssl_digest_vector(const EVP_MD *type, int non_fips,
00078                                  size_t num_elem, const u8 *addr[],
00079                                  const size_t *len, u8 *mac)
00080 {
00081         EVP_MD_CTX ctx;
00082         size_t i;
00083         unsigned int mac_len;
00084 
00085         EVP_MD_CTX_init(&ctx);
00086 #ifdef CONFIG_FIPS
00087 #ifdef OPENSSL_FIPS
00088         if (non_fips)
00089                 EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
00090 #endif /* OPENSSL_FIPS */
00091 #endif /* CONFIG_FIPS */
00092         if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
00093                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
00094                            ERR_error_string(ERR_get_error(), NULL));
00095                 return -1;
00096         }
00097         for (i = 0; i < num_elem; i++) {
00098                 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
00099                         wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
00100                                    "failed: %s",
00101                                    ERR_error_string(ERR_get_error(), NULL));
00102                         return -1;
00103                 }
00104         }
00105         if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
00106                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
00107                            ERR_error_string(ERR_get_error(), NULL));
00108                 return -1;
00109         }
00110 
00111         return 0;
00112 }
00113 
00114 
00115 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00116 {
00117         return openssl_digest_vector(EVP_md4(), 0, num_elem, addr, len, mac);
00118 }
00119 
00120 
00121 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
00122 {
00123         u8 pkey[8], next, tmp;
00124         int i;
00125         DES_key_schedule ks;
00126 
00127         /* Add parity bits to the key */
00128         next = 0;
00129         for (i = 0; i < 7; i++) {
00130                 tmp = key[i];
00131                 pkey[i] = (tmp >> i) | next | 1;
00132                 next = tmp << (7 - i);
00133         }
00134         pkey[i] = next | 1;
00135 
00136         DES_set_key(&pkey, &ks);
00137         DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
00138                         DES_ENCRYPT);
00139 }
00140 
00141 
00142 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
00143              u8 *data, size_t data_len)
00144 {
00145 #ifdef OPENSSL_NO_RC4
00146         return -1;
00147 #else /* OPENSSL_NO_RC4 */
00148         EVP_CIPHER_CTX ctx;
00149         int outl;
00150         int res = -1;
00151         unsigned char skip_buf[16];
00152 
00153         EVP_CIPHER_CTX_init(&ctx);
00154         if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
00155             !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
00156             !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
00157             !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
00158                 goto out;
00159 
00160         while (skip >= sizeof(skip_buf)) {
00161                 size_t len = skip;
00162                 if (len > sizeof(skip_buf))
00163                         len = sizeof(skip_buf);
00164                 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
00165                         goto out;
00166                 skip -= len;
00167         }
00168 
00169         if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
00170                 res = 0;
00171 
00172 out:
00173         EVP_CIPHER_CTX_cleanup(&ctx);
00174         return res;
00175 #endif /* OPENSSL_NO_RC4 */
00176 }
00177 
00178 
00179 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00180 {
00181         return openssl_digest_vector(EVP_md5(), 0, num_elem, addr, len, mac);
00182 }
00183 
00184 
00185 #ifdef CONFIG_FIPS
00186 int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
00187                               const size_t *len, u8 *mac)
00188 {
00189         return openssl_digest_vector(EVP_md5(), 1, num_elem, addr, len, mac);
00190 }
00191 #endif /* CONFIG_FIPS */
00192 
00193 
00194 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
00195 {
00196         return openssl_digest_vector(EVP_sha1(), 0, num_elem, addr, len, mac);
00197 }
00198 
00199 
00200 #ifndef NO_SHA256_WRAPPER
00201 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
00202                   u8 *mac)
00203 {
00204         return openssl_digest_vector(EVP_sha256(), 0, num_elem, addr, len,
00205                                      mac);
00206 }
00207 #endif /* NO_SHA256_WRAPPER */
00208 
00209 
00210 void * aes_encrypt_init(const u8 *key, size_t len)
00211 {
00212         AES_KEY *ak;
00213         ak = os_malloc(sizeof(*ak));
00214         if (ak == NULL)
00215                 return NULL;
00216         if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
00217                 os_free(ak);
00218                 return NULL;
00219         }
00220         return ak;
00221 }
00222 
00223 
00224 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
00225 {
00226         AES_encrypt(plain, crypt, ctx);
00227 }
00228 
00229 
00230 void aes_encrypt_deinit(void *ctx)
00231 {
00232         os_free(ctx);
00233 }
00234 
00235 
00236 void * aes_decrypt_init(const u8 *key, size_t len)
00237 {
00238         AES_KEY *ak;
00239         ak = os_malloc(sizeof(*ak));
00240         if (ak == NULL)
00241                 return NULL;
00242         if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
00243                 os_free(ak);
00244                 return NULL;
00245         }
00246         return ak;
00247 }
00248 
00249 
00250 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
00251 {
00252         AES_decrypt(crypt, plain, ctx);
00253 }
00254 
00255 
00256 void aes_decrypt_deinit(void *ctx)
00257 {
00258         os_free(ctx);
00259 }
00260 
00261 
00262 int crypto_mod_exp(const u8 *base, size_t base_len,
00263                    const u8 *power, size_t power_len,
00264                    const u8 *modulus, size_t modulus_len,
00265                    u8 *result, size_t *result_len)
00266 {
00267         BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
00268         int ret = -1;
00269         BN_CTX *ctx;
00270 
00271         ctx = BN_CTX_new();
00272         if (ctx == NULL)
00273                 return -1;
00274 
00275         bn_base = BN_bin2bn(base, base_len, NULL);
00276         bn_exp = BN_bin2bn(power, power_len, NULL);
00277         bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
00278         bn_result = BN_new();
00279 
00280         if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
00281             bn_result == NULL)
00282                 goto error;
00283 
00284         if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
00285                 goto error;
00286 
00287         *result_len = BN_bn2bin(bn_result, result);
00288         ret = 0;
00289 
00290 error:
00291         BN_free(bn_base);
00292         BN_free(bn_exp);
00293         BN_free(bn_modulus);
00294         BN_free(bn_result);
00295         BN_CTX_free(ctx);
00296         return ret;
00297 }
00298 
00299 
00300 struct crypto_cipher {
00301         EVP_CIPHER_CTX enc;
00302         EVP_CIPHER_CTX dec;
00303 };
00304 
00305 
00306 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
00307                                           const u8 *iv, const u8 *key,
00308                                           size_t key_len)
00309 {
00310         struct crypto_cipher *ctx;
00311         const EVP_CIPHER *cipher;
00312 
00313         ctx = os_zalloc(sizeof(*ctx));
00314         if (ctx == NULL)
00315                 return NULL;
00316 
00317         switch (alg) {
00318 #ifndef OPENSSL_NO_RC4
00319         case CRYPTO_CIPHER_ALG_RC4:
00320                 cipher = EVP_rc4();
00321                 break;
00322 #endif /* OPENSSL_NO_RC4 */
00323 #ifndef OPENSSL_NO_AES
00324         case CRYPTO_CIPHER_ALG_AES:
00325                 switch (key_len) {
00326                 case 16:
00327                         cipher = EVP_aes_128_cbc();
00328                         break;
00329                 case 24:
00330                         cipher = EVP_aes_192_cbc();
00331                         break;
00332                 case 32:
00333                         cipher = EVP_aes_256_cbc();
00334                         break;
00335                 default:
00336                         os_free(ctx);
00337                         return NULL;
00338                 }
00339                 break;
00340 #endif /* OPENSSL_NO_AES */
00341 #ifndef OPENSSL_NO_DES
00342         case CRYPTO_CIPHER_ALG_3DES:
00343                 cipher = EVP_des_ede3_cbc();
00344                 break;
00345         case CRYPTO_CIPHER_ALG_DES:
00346                 cipher = EVP_des_cbc();
00347                 break;
00348 #endif /* OPENSSL_NO_DES */
00349 #ifndef OPENSSL_NO_RC2
00350         case CRYPTO_CIPHER_ALG_RC2:
00351                 cipher = EVP_rc2_ecb();
00352                 break;
00353 #endif /* OPENSSL_NO_RC2 */
00354         default:
00355                 os_free(ctx);
00356                 return NULL;
00357         }
00358 
00359         EVP_CIPHER_CTX_init(&ctx->enc);
00360         EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
00361         if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
00362             !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
00363             !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
00364                 EVP_CIPHER_CTX_cleanup(&ctx->enc);
00365                 os_free(ctx);
00366                 return NULL;
00367         }
00368 
00369         EVP_CIPHER_CTX_init(&ctx->dec);
00370         EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
00371         if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
00372             !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
00373             !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
00374                 EVP_CIPHER_CTX_cleanup(&ctx->enc);
00375                 EVP_CIPHER_CTX_cleanup(&ctx->dec);
00376                 os_free(ctx);
00377                 return NULL;
00378         }
00379 
00380         return ctx;
00381 }
00382 
00383 
00384 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
00385                           u8 *crypt, size_t len)
00386 {
00387         int outl;
00388         if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
00389                 return -1;
00390         return 0;
00391 }
00392 
00393 
00394 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
00395                           u8 *plain, size_t len)
00396 {
00397         int outl;
00398         outl = len;
00399         if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
00400                 return -1;
00401         return 0;
00402 }
00403 
00404 
00405 void crypto_cipher_deinit(struct crypto_cipher *ctx)
00406 {
00407         EVP_CIPHER_CTX_cleanup(&ctx->enc);
00408         EVP_CIPHER_CTX_cleanup(&ctx->dec);
00409         os_free(ctx);
00410 }
00411 
00412 
00413 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
00414 {
00415         DH *dh;
00416         struct wpabuf *pubkey = NULL, *privkey = NULL;
00417         size_t publen, privlen;
00418 
00419         *priv = NULL;
00420         *publ = NULL;
00421 
00422         dh = DH_new();
00423         if (dh == NULL)
00424                 return NULL;
00425 
00426         dh->g = BN_new();
00427         if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
00428                 goto err;
00429 
00430         dh->p = get_group5_prime();
00431         if (dh->p == NULL)
00432                 goto err;
00433 
00434         if (DH_generate_key(dh) != 1)
00435                 goto err;
00436 
00437         publen = BN_num_bytes(dh->p);
00438         pubkey = wpabuf_alloc(publen);
00439         if (pubkey == NULL)
00440                 goto err;
00441         privlen = BN_num_bytes(dh->priv_key);
00442         privkey = wpabuf_alloc(privlen);
00443         if (privkey == NULL)
00444                 goto err;
00445 
00446         BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
00447         BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
00448 
00449         *priv = privkey;
00450         *publ = pubkey;
00451         return dh;
00452 
00453 err:
00454         wpabuf_free(pubkey);
00455         wpabuf_free(privkey);
00456         DH_free(dh);
00457         return NULL;
00458 }
00459 
00460 
00461 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
00462                                   const struct wpabuf *own_private)
00463 {
00464         BIGNUM *pub_key;
00465         struct wpabuf *res = NULL;
00466         size_t rlen;
00467         DH *dh = ctx;
00468         int keylen;
00469 
00470         if (ctx == NULL)
00471                 return NULL;
00472 
00473         pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
00474                             NULL);
00475         if (pub_key == NULL)
00476                 return NULL;
00477 
00478         rlen = DH_size(dh);
00479         res = wpabuf_alloc(rlen);
00480         if (res == NULL)
00481                 goto err;
00482 
00483         keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
00484         if (keylen < 0)
00485                 goto err;
00486         wpabuf_put(res, keylen);
00487         BN_free(pub_key);
00488 
00489         return res;
00490 
00491 err:
00492         BN_free(pub_key);
00493         wpabuf_free(res);
00494         return NULL;
00495 }
00496 
00497 
00498 void dh5_free(void *ctx)
00499 {
00500         DH *dh;
00501         if (ctx == NULL)
00502                 return;
00503         dh = ctx;
00504         DH_free(dh);
00505 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:25:12