2 * WPA Supplicant / wrapper functions for libcrypto
3 * Copyright (c) 2004-2005, 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/md4.h>
18 #include <openssl/md5.h>
19 #include <openssl/sha.h>
20 #include <openssl/des.h>
21 #include <openssl/aes.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 void md4_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
41 for (i
= 0; i
< num_elem
; i
++)
42 MD4_Update(&ctx
, addr
[i
], len
[i
]);
47 void des_encrypt(const u8
*clear
, const u8
*key
, u8
*cypher
)
49 u8 pkey
[8], next
, tmp
;
53 /* Add parity bits to the key */
55 for (i
= 0; i
< 7; i
++) {
57 pkey
[i
] = (tmp
>> i
) | next
| 1;
58 next
= tmp
<< (7 - i
);
62 DES_set_key(&pkey
, &ks
);
63 DES_ecb_encrypt((DES_cblock
*) clear
, (DES_cblock
*) cypher
, &ks
,
69 void md5_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
75 for (i
= 0; i
< num_elem
; i
++)
76 MD5_Update(&ctx
, addr
[i
], len
[i
]);
81 void sha1_vector(size_t num_elem
, const u8
*addr
[], const size_t *len
, u8
*mac
)
87 for (i
= 0; i
< num_elem
; i
++)
88 SHA1_Update(&ctx
, addr
[i
], len
[i
]);
89 SHA1_Final(mac
, &ctx
);
93 static void sha1_transform(u8
*state
, const u8 data
[64])
96 os_memset(&context
, 0, sizeof(context
));
97 os_memcpy(&context
.h0
, state
, 5 * 4);
98 SHA1_Transform(&context
, data
);
99 os_memcpy(state
, &context
.h0
, 5 * 4);
103 int fips186_2_prf(const u8
*seed
, size_t seed_len
, u8
*x
, size_t xlen
)
111 if (seed_len
> sizeof(xkey
))
112 seed_len
= sizeof(xkey
);
114 /* FIPS 186-2 + change notice 1 */
116 os_memcpy(xkey
, seed
, seed_len
);
117 os_memset(xkey
+ seed_len
, 0, 64 - seed_len
);
125 for (j
= 0; j
< m
; j
++) {
127 for (i
= 0; i
< 2; i
++) {
128 /* XVAL = (XKEY + XSEED_j) mod 2^b */
130 /* w_i = G(t, XVAL) */
131 os_memcpy(_t
, t
, 20);
132 sha1_transform((u8
*) _t
, xkey
);
133 _t
[0] = host_to_be32(_t
[0]);
134 _t
[1] = host_to_be32(_t
[1]);
135 _t
[2] = host_to_be32(_t
[2]);
136 _t
[3] = host_to_be32(_t
[3]);
137 _t
[4] = host_to_be32(_t
[4]);
138 os_memcpy(xpos
, _t
, 20);
140 /* XKEY = (1 + XKEY + w_i) mod 2^b */
142 for (k
= 19; k
>= 0; k
--) {
143 carry
+= xkey
[k
] + xpos
[k
];
144 xkey
[k
] = carry
& 0xff;
157 void * aes_encrypt_init(const u8
*key
, size_t len
)
160 ak
= os_malloc(sizeof(*ak
));
163 if (AES_set_encrypt_key(key
, 8 * len
, ak
) < 0) {
171 void aes_encrypt(void *ctx
, const u8
*plain
, u8
*crypt
)
173 AES_encrypt(plain
, crypt
, ctx
);
177 void aes_encrypt_deinit(void *ctx
)
183 void * aes_decrypt_init(const u8
*key
, size_t len
)
186 ak
= os_malloc(sizeof(*ak
));
189 if (AES_set_decrypt_key(key
, 8 * len
, ak
) < 0) {
197 void aes_decrypt(void *ctx
, const u8
*crypt
, u8
*plain
)
199 AES_decrypt(crypt
, plain
, ctx
);
203 void aes_decrypt_deinit(void *ctx
)
207 #endif /* EAP_TLS_FUNCS */