2 * Crypto wrapper for internal crypto implementation
3 * Copyright (c) 2006-2009, 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.
23 enum crypto_hash_alg alg
;
25 struct MD5Context md5
;
26 struct SHA1Context sha1
;
33 struct crypto_hash
* crypto_hash_init(enum crypto_hash_alg alg
, const u8
*key
,
36 struct crypto_hash
*ctx
;
41 ctx
= os_zalloc(sizeof(*ctx
));
48 case CRYPTO_HASH_ALG_MD5
:
51 case CRYPTO_HASH_ALG_SHA1
:
52 SHA1Init(&ctx
->u
.sha1
);
54 case CRYPTO_HASH_ALG_HMAC_MD5
:
55 if (key_len
> sizeof(k_pad
)) {
57 MD5Update(&ctx
->u
.md5
, key
, key_len
);
58 MD5Final(tk
, &ctx
->u
.md5
);
62 os_memcpy(ctx
->key
, key
, key_len
);
63 ctx
->key_len
= key_len
;
65 os_memcpy(k_pad
, key
, key_len
);
66 os_memset(k_pad
+ key_len
, 0, sizeof(k_pad
) - key_len
);
67 for (i
= 0; i
< sizeof(k_pad
); i
++)
70 MD5Update(&ctx
->u
.md5
, k_pad
, sizeof(k_pad
));
72 case CRYPTO_HASH_ALG_HMAC_SHA1
:
73 if (key_len
> sizeof(k_pad
)) {
74 SHA1Init(&ctx
->u
.sha1
);
75 SHA1Update(&ctx
->u
.sha1
, key
, key_len
);
76 SHA1Final(tk
, &ctx
->u
.sha1
);
80 os_memcpy(ctx
->key
, key
, key_len
);
81 ctx
->key_len
= key_len
;
83 os_memcpy(k_pad
, key
, key_len
);
84 os_memset(k_pad
+ key_len
, 0, sizeof(k_pad
) - key_len
);
85 for (i
= 0; i
< sizeof(k_pad
); i
++)
87 SHA1Init(&ctx
->u
.sha1
);
88 SHA1Update(&ctx
->u
.sha1
, k_pad
, sizeof(k_pad
));
99 void crypto_hash_update(struct crypto_hash
*ctx
, const u8
*data
, size_t len
)
105 case CRYPTO_HASH_ALG_MD5
:
106 case CRYPTO_HASH_ALG_HMAC_MD5
:
107 MD5Update(&ctx
->u
.md5
, data
, len
);
109 case CRYPTO_HASH_ALG_SHA1
:
110 case CRYPTO_HASH_ALG_HMAC_SHA1
:
111 SHA1Update(&ctx
->u
.sha1
, data
, len
);
117 int crypto_hash_finish(struct crypto_hash
*ctx
, u8
*mac
, size_t *len
)
125 if (mac
== NULL
|| len
== NULL
) {
131 case CRYPTO_HASH_ALG_MD5
:
138 MD5Final(mac
, &ctx
->u
.md5
);
140 case CRYPTO_HASH_ALG_SHA1
:
147 SHA1Final(mac
, &ctx
->u
.sha1
);
149 case CRYPTO_HASH_ALG_HMAC_MD5
:
157 MD5Final(mac
, &ctx
->u
.md5
);
159 os_memcpy(k_pad
, ctx
->key
, ctx
->key_len
);
160 os_memset(k_pad
+ ctx
->key_len
, 0,
161 sizeof(k_pad
) - ctx
->key_len
);
162 for (i
= 0; i
< sizeof(k_pad
); i
++)
164 MD5Init(&ctx
->u
.md5
);
165 MD5Update(&ctx
->u
.md5
, k_pad
, sizeof(k_pad
));
166 MD5Update(&ctx
->u
.md5
, mac
, 16);
167 MD5Final(mac
, &ctx
->u
.md5
);
169 case CRYPTO_HASH_ALG_HMAC_SHA1
:
177 SHA1Final(mac
, &ctx
->u
.sha1
);
179 os_memcpy(k_pad
, ctx
->key
, ctx
->key_len
);
180 os_memset(k_pad
+ ctx
->key_len
, 0,
181 sizeof(k_pad
) - ctx
->key_len
);
182 for (i
= 0; i
< sizeof(k_pad
); i
++)
184 SHA1Init(&ctx
->u
.sha1
);
185 SHA1Update(&ctx
->u
.sha1
, k_pad
, sizeof(k_pad
));
186 SHA1Update(&ctx
->u
.sha1
, mac
, 20);
187 SHA1Final(mac
, &ctx
->u
.sha1
);
197 int crypto_global_init(void)
203 void crypto_global_deinit(void)