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.
18 #include <sys/types.h>
20 #include <openssl/opensslv.h>
21 #include <openssl/md4.h>
22 #include <openssl/md5.h>
23 #include <openssl/sha.h>
24 #include <openssl/des.h>
25 #include <openssl/aes.h>
30 #if OPENSSL_VERSION_NUMBER < 0x00907000
31 #define DES_key_schedule des_key_schedule
32 #define DES_cblock des_cblock
33 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
34 #define DES_ecb_encrypt(input, output, ks, enc) \
35 des_ecb_encrypt((input), (output), *(ks), (enc))
36 #endif /* openssl < 0.9.7 */
39 void md4_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
45 for (i
= 0; i
< num_elem
; i
++)
46 MD4_Update(&ctx
, addr
[i
], len
[i
]);
51 void des_encrypt(const u8
*clear
, const u8
*key
, u8
*cypher
)
53 u8 pkey
[8], next
, tmp
;
57 /* Add parity bits to the key */
59 for (i
= 0; i
< 7; i
++) {
61 pkey
[i
] = (tmp
>> i
) | next
| 1;
62 next
= tmp
<< (7 - i
);
66 DES_set_key(&pkey
, &ks
);
67 DES_ecb_encrypt((DES_cblock
*) clear
, (DES_cblock
*) cypher
, &ks
,
73 void md5_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
79 for (i
= 0; i
< num_elem
; i
++)
80 MD5_Update(&ctx
, addr
[i
], len
[i
]);
85 void sha1_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
91 for (i
= 0; i
< num_elem
; i
++)
92 SHA1_Update(&ctx
, addr
[i
], len
[i
]);
93 SHA1_Final(mac
, &ctx
);
97 void sha1_transform(u8
*state
, const u8 data
[64])
100 memset(&context
, 0, sizeof(context
));
101 memcpy(&context
.h0
, state
, 5 * 4);
102 SHA1_Transform(&context
, data
);
103 memcpy(state
, &context
.h0
, 5 * 4);
107 void * aes_encrypt_init(const u8
*key
, size_t len
)
110 ak
= malloc(sizeof(*ak
));
113 if (AES_set_encrypt_key(key
, 8 * len
, ak
) < 0) {
121 void aes_encrypt(void *ctx
, const u8
*plain
, u8
*crypt
)
123 AES_encrypt(plain
, crypt
, ctx
);
127 void aes_encrypt_deinit(void *ctx
)
133 void * aes_decrypt_init(const u8
*key
, size_t len
)
136 ak
= malloc(sizeof(*ak
));
139 if (AES_set_decrypt_key(key
, 8 * len
, ak
) < 0) {
147 void aes_decrypt(void *ctx
, const u8
*crypt
, u8
*plain
)
149 AES_decrypt(crypt
, plain
, ctx
);
153 void aes_decrypt_deinit(void *ctx
)
157 #endif /* EAP_TLS_FUNCS */