2 * Crypto wrapper for internal crypto implementation
3 * Copyright (c) 2006-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
20 enum crypto_hash_alg alg;
22 struct MD5Context md5;
23 struct SHA1Context sha1;
25 struct sha256_state sha256;
26 #endif /* CONFIG_SHA256 */
27 #ifdef CONFIG_INTERNAL_SHA384
28 struct sha384_state sha384;
29 #endif /* CONFIG_INTERNAL_SHA384 */
30 #ifdef CONFIG_INTERNAL_SHA512
31 struct sha512_state sha512;
32 #endif /* CONFIG_INTERNAL_SHA512 */
39 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
42 struct crypto_hash *ctx;
47 ctx = os_zalloc(sizeof(*ctx));
54 case CRYPTO_HASH_ALG_MD5:
57 case CRYPTO_HASH_ALG_SHA1:
58 SHA1Init(&ctx->u.sha1);
61 case CRYPTO_HASH_ALG_SHA256:
62 sha256_init(&ctx->u.sha256);
64 #endif /* CONFIG_SHA256 */
65 #ifdef CONFIG_INTERNAL_SHA384
66 case CRYPTO_HASH_ALG_SHA384:
67 sha384_init(&ctx->u.sha384);
69 #endif /* CONFIG_INTERNAL_SHA384 */
70 #ifdef CONFIG_INTERNAL_SHA512
71 case CRYPTO_HASH_ALG_SHA512:
72 sha512_init(&ctx->u.sha512);
74 #endif /* CONFIG_INTERNAL_SHA512 */
75 case CRYPTO_HASH_ALG_HMAC_MD5:
76 if (key_len > sizeof(k_pad)) {
78 MD5Update(&ctx->u.md5, key, key_len);
79 MD5Final(tk, &ctx->u.md5);
83 os_memcpy(ctx->key, key, key_len);
84 ctx->key_len = key_len;
86 os_memcpy(k_pad, key, key_len);
87 if (key_len < sizeof(k_pad))
88 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
89 for (i = 0; i < sizeof(k_pad); i++)
92 MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
94 case CRYPTO_HASH_ALG_HMAC_SHA1:
95 if (key_len > sizeof(k_pad)) {
96 SHA1Init(&ctx->u.sha1);
97 SHA1Update(&ctx->u.sha1, key, key_len);
98 SHA1Final(tk, &ctx->u.sha1);
102 os_memcpy(ctx->key, key, key_len);
103 ctx->key_len = key_len;
105 os_memcpy(k_pad, key, key_len);
106 if (key_len < sizeof(k_pad))
107 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
108 for (i = 0; i < sizeof(k_pad); i++)
110 SHA1Init(&ctx->u.sha1);
111 SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
114 case CRYPTO_HASH_ALG_HMAC_SHA256:
115 if (key_len > sizeof(k_pad)) {
116 sha256_init(&ctx->u.sha256);
117 sha256_process(&ctx->u.sha256, key, key_len);
118 sha256_done(&ctx->u.sha256, tk);
122 os_memcpy(ctx->key, key, key_len);
123 ctx->key_len = key_len;
125 os_memcpy(k_pad, key, key_len);
126 if (key_len < sizeof(k_pad))
127 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
128 for (i = 0; i < sizeof(k_pad); i++)
130 sha256_init(&ctx->u.sha256);
131 sha256_process(&ctx->u.sha256, k_pad, sizeof(k_pad));
133 #endif /* CONFIG_SHA256 */
143 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
149 case CRYPTO_HASH_ALG_MD5:
150 case CRYPTO_HASH_ALG_HMAC_MD5:
151 MD5Update(&ctx->u.md5, data, len);
153 case CRYPTO_HASH_ALG_SHA1:
154 case CRYPTO_HASH_ALG_HMAC_SHA1:
155 SHA1Update(&ctx->u.sha1, data, len);
158 case CRYPTO_HASH_ALG_SHA256:
159 case CRYPTO_HASH_ALG_HMAC_SHA256:
160 sha256_process(&ctx->u.sha256, data, len);
162 #endif /* CONFIG_SHA256 */
163 #ifdef CONFIG_INTERNAL_SHA384
164 case CRYPTO_HASH_ALG_SHA384:
165 sha384_process(&ctx->u.sha384, data, len);
167 #endif /* CONFIG_INTERNAL_SHA384 */
168 #ifdef CONFIG_INTERNAL_SHA512
169 case CRYPTO_HASH_ALG_SHA512:
170 sha512_process(&ctx->u.sha512, data, len);
172 #endif /* CONFIG_INTERNAL_SHA512 */
179 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
187 if (mac == NULL || len == NULL) {
193 case CRYPTO_HASH_ALG_MD5:
200 MD5Final(mac, &ctx->u.md5);
202 case CRYPTO_HASH_ALG_SHA1:
209 SHA1Final(mac, &ctx->u.sha1);
212 case CRYPTO_HASH_ALG_SHA256:
219 sha256_done(&ctx->u.sha256, mac);
221 #endif /* CONFIG_SHA256 */
222 #ifdef CONFIG_INTERNAL_SHA384
223 case CRYPTO_HASH_ALG_SHA384:
230 sha384_done(&ctx->u.sha384, mac);
232 #endif /* CONFIG_INTERNAL_SHA384 */
233 #ifdef CONFIG_INTERNAL_SHA512
234 case CRYPTO_HASH_ALG_SHA512:
241 sha512_done(&ctx->u.sha512, mac);
243 #endif /* CONFIG_INTERNAL_SHA512 */
244 case CRYPTO_HASH_ALG_HMAC_MD5:
252 MD5Final(mac, &ctx->u.md5);
254 os_memcpy(k_pad, ctx->key, ctx->key_len);
255 os_memset(k_pad + ctx->key_len, 0,
256 sizeof(k_pad) - ctx->key_len);
257 for (i = 0; i < sizeof(k_pad); i++)
259 MD5Init(&ctx->u.md5);
260 MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
261 MD5Update(&ctx->u.md5, mac, 16);
262 MD5Final(mac, &ctx->u.md5);
264 case CRYPTO_HASH_ALG_HMAC_SHA1:
272 SHA1Final(mac, &ctx->u.sha1);
274 os_memcpy(k_pad, ctx->key, ctx->key_len);
275 os_memset(k_pad + ctx->key_len, 0,
276 sizeof(k_pad) - ctx->key_len);
277 for (i = 0; i < sizeof(k_pad); i++)
279 SHA1Init(&ctx->u.sha1);
280 SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
281 SHA1Update(&ctx->u.sha1, mac, 20);
282 SHA1Final(mac, &ctx->u.sha1);
285 case CRYPTO_HASH_ALG_HMAC_SHA256:
293 sha256_done(&ctx->u.sha256, mac);
295 os_memcpy(k_pad, ctx->key, ctx->key_len);
296 os_memset(k_pad + ctx->key_len, 0,
297 sizeof(k_pad) - ctx->key_len);
298 for (i = 0; i < sizeof(k_pad); i++)
300 sha256_init(&ctx->u.sha256);
301 sha256_process(&ctx->u.sha256, k_pad, sizeof(k_pad));
302 sha256_process(&ctx->u.sha256, mac, 32);
303 sha256_done(&ctx->u.sha256, mac);
305 #endif /* CONFIG_SHA256 */
320 int crypto_global_init(void)
326 void crypto_global_deinit(void)