2 * WPA Supplicant / wrapper functions for libcrypto
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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 <sys/types.h>
18 #include <openssl/md4.h>
19 #include <openssl/md5.h>
20 #include <openssl/sha.h>
21 #include <openssl/des.h>
22 #include <openssl/aes.h>
27 #if OPENSSL_VERSION_NUMBER < 0x00907000
28 #define DES_key_schedule des_key_schedule
29 #define DES_cblock des_cblock
30 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
31 #define DES_ecb_encrypt(input, output, ks, enc) \
32 des_ecb_encrypt((input), (output), *(ks), (enc))
33 #endif /* openssl < 0.9.7 */
36 void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
42 for (i = 0; i < num_elem; i++)
43 MD4_Update(&ctx, addr[i], len[i]);
48 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
50 u8 pkey[8], next, tmp;
54 /* Add parity bits to the key */
56 for (i = 0; i < 7; i++) {
58 pkey[i] = (tmp >> i) | next | 1;
59 next = tmp << (7 - i);
63 DES_set_key(&pkey, &ks);
64 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 void sha1_transform(u8 *state, const u8 data[64])
97 memset(&context, 0, sizeof(context));
98 memcpy(&context.h0, state, 5 * 4);
99 SHA1_Transform(&context, data);
100 memcpy(state, &context.h0, 5 * 4);
104 void * aes_encrypt_init(const u8 *key, size_t len)
107 ak = malloc(sizeof(*ak));
110 if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
118 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
120 AES_encrypt(plain, crypt, ctx);
124 void aes_encrypt_deinit(void *ctx)
130 void * aes_decrypt_init(const u8 *key, size_t len)
133 ak = malloc(sizeof(*ak));
136 if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
144 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
146 AES_decrypt(crypt, plain, ctx);
150 void aes_decrypt_deinit(void *ctx)
154 #endif /* EAP_TLS_FUNCS */