2 * WPA Supplicant / Crypto wrapper for internal crypto implementation
3 * Copyright (c) 2006, 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.
29 #ifdef CONFIG_TLS_INTERNAL
37 void des3_key_setup(const u8
*key
, struct des3_key_s
*dkey
);
38 void des3_encrypt(const u8
*plain
, const struct des3_key_s
*key
, u8
*crypt
);
39 void des3_decrypt(const u8
*crypt
, const struct des3_key_s
*key
, u8
*plain
);
51 unsigned char buffer
[64];
56 enum crypto_hash_alg alg
;
58 struct MD5Context md5
;
59 struct SHA1Context sha1
;
66 struct crypto_hash
* crypto_hash_init(enum crypto_hash_alg alg
, const u8
*key
,
69 struct crypto_hash
*ctx
;
74 ctx
= os_zalloc(sizeof(*ctx
));
81 case CRYPTO_HASH_ALG_MD5
:
84 case CRYPTO_HASH_ALG_SHA1
:
85 SHA1Init(&ctx
->u
.sha1
);
87 case CRYPTO_HASH_ALG_HMAC_MD5
:
88 if (key_len
> sizeof(k_pad
)) {
90 MD5Update(&ctx
->u
.md5
, key
, key_len
);
91 MD5Final(tk
, &ctx
->u
.md5
);
95 os_memcpy(ctx
->key
, key
, key_len
);
96 ctx
->key_len
= key_len
;
98 os_memcpy(k_pad
, key
, key_len
);
99 os_memset(k_pad
+ key_len
, 0, sizeof(k_pad
) - key_len
);
100 for (i
= 0; i
< sizeof(k_pad
); i
++)
102 MD5Init(&ctx
->u
.md5
);
103 MD5Update(&ctx
->u
.md5
, k_pad
, sizeof(k_pad
));
105 case CRYPTO_HASH_ALG_HMAC_SHA1
:
106 if (key_len
> sizeof(k_pad
)) {
107 SHA1Init(&ctx
->u
.sha1
);
108 SHA1Update(&ctx
->u
.sha1
, key
, key_len
);
109 SHA1Final(tk
, &ctx
->u
.sha1
);
113 os_memcpy(ctx
->key
, key
, key_len
);
114 ctx
->key_len
= key_len
;
116 os_memcpy(k_pad
, key
, key_len
);
117 os_memset(k_pad
+ key_len
, 0, sizeof(k_pad
) - key_len
);
118 for (i
= 0; i
< sizeof(k_pad
); i
++)
120 SHA1Init(&ctx
->u
.sha1
);
121 SHA1Update(&ctx
->u
.sha1
, k_pad
, sizeof(k_pad
));
132 void crypto_hash_update(struct crypto_hash
*ctx
, const u8
*data
, size_t len
)
138 case CRYPTO_HASH_ALG_MD5
:
139 case CRYPTO_HASH_ALG_HMAC_MD5
:
140 MD5Update(&ctx
->u
.md5
, data
, len
);
142 case CRYPTO_HASH_ALG_SHA1
:
143 case CRYPTO_HASH_ALG_HMAC_SHA1
:
144 SHA1Update(&ctx
->u
.sha1
, data
, len
);
150 int crypto_hash_finish(struct crypto_hash
*ctx
, u8
*mac
, size_t *len
)
158 if (mac
== NULL
|| len
== NULL
) {
164 case CRYPTO_HASH_ALG_MD5
:
171 MD5Final(mac
, &ctx
->u
.md5
);
173 case CRYPTO_HASH_ALG_SHA1
:
180 SHA1Final(mac
, &ctx
->u
.sha1
);
182 case CRYPTO_HASH_ALG_HMAC_MD5
:
190 MD5Final(mac
, &ctx
->u
.md5
);
192 os_memcpy(k_pad
, ctx
->key
, ctx
->key_len
);
193 os_memset(k_pad
+ ctx
->key_len
, 0,
194 sizeof(k_pad
) - ctx
->key_len
);
195 for (i
= 0; i
< sizeof(k_pad
); i
++)
197 MD5Init(&ctx
->u
.md5
);
198 MD5Update(&ctx
->u
.md5
, k_pad
, sizeof(k_pad
));
199 MD5Update(&ctx
->u
.md5
, mac
, 16);
200 MD5Final(mac
, &ctx
->u
.md5
);
202 case CRYPTO_HASH_ALG_HMAC_SHA1
:
210 SHA1Final(mac
, &ctx
->u
.sha1
);
212 os_memcpy(k_pad
, ctx
->key
, ctx
->key_len
);
213 os_memset(k_pad
+ ctx
->key_len
, 0,
214 sizeof(k_pad
) - ctx
->key_len
);
215 for (i
= 0; i
< sizeof(k_pad
); i
++)
217 SHA1Init(&ctx
->u
.sha1
);
218 SHA1Update(&ctx
->u
.sha1
, k_pad
, sizeof(k_pad
));
219 SHA1Update(&ctx
->u
.sha1
, mac
, 20);
220 SHA1Final(mac
, &ctx
->u
.sha1
);
230 struct crypto_cipher
{
231 enum crypto_cipher_alg alg
;
245 struct des3_key_s key
;
252 struct crypto_cipher
* crypto_cipher_init(enum crypto_cipher_alg alg
,
253 const u8
*iv
, const u8
*key
,
256 struct crypto_cipher
*ctx
;
258 ctx
= os_zalloc(sizeof(*ctx
));
265 case CRYPTO_CIPHER_ALG_RC4
:
266 if (key_len
> sizeof(ctx
->u
.rc4
.key
)) {
270 ctx
->u
.rc4
.keylen
= key_len
;
271 os_memcpy(ctx
->u
.rc4
.key
, key
, key_len
);
273 case CRYPTO_CIPHER_ALG_AES
:
274 if (key_len
> sizeof(ctx
->u
.aes
.cbc
)) {
278 ctx
->u
.aes
.ctx_enc
= aes_encrypt_init(key
, key_len
);
279 if (ctx
->u
.aes
.ctx_enc
== NULL
) {
283 ctx
->u
.aes
.ctx_dec
= aes_decrypt_init(key
, key_len
);
284 if (ctx
->u
.aes
.ctx_dec
== NULL
) {
285 aes_encrypt_deinit(ctx
->u
.aes
.ctx_enc
);
289 ctx
->u
.aes
.block_size
= key_len
;
290 os_memcpy(ctx
->u
.aes
.cbc
, iv
, ctx
->u
.aes
.block_size
);
292 case CRYPTO_CIPHER_ALG_3DES
:
297 des3_key_setup(key
, &ctx
->u
.des3
.key
);
298 os_memcpy(ctx
->u
.des3
.cbc
, iv
, 8);
309 int crypto_cipher_encrypt(struct crypto_cipher
*ctx
, const u8
*plain
,
310 u8
*crypt
, size_t len
)
315 case CRYPTO_CIPHER_ALG_RC4
:
317 os_memcpy(crypt
, plain
, len
);
318 rc4_skip(ctx
->u
.rc4
.key
, ctx
->u
.rc4
.keylen
,
319 ctx
->u
.rc4
.used_bytes
, crypt
, len
);
320 ctx
->u
.rc4
.used_bytes
+= len
;
322 case CRYPTO_CIPHER_ALG_AES
:
323 if (len
% ctx
->u
.aes
.block_size
)
325 blocks
= len
/ ctx
->u
.aes
.block_size
;
326 for (i
= 0; i
< blocks
; i
++) {
327 for (j
= 0; j
< ctx
->u
.aes
.block_size
; j
++)
328 ctx
->u
.aes
.cbc
[j
] ^= plain
[j
];
329 aes_encrypt(ctx
->u
.aes
.ctx_enc
, ctx
->u
.aes
.cbc
,
331 os_memcpy(crypt
, ctx
->u
.aes
.cbc
,
332 ctx
->u
.aes
.block_size
);
333 plain
+= ctx
->u
.aes
.block_size
;
334 crypt
+= ctx
->u
.aes
.block_size
;
337 case CRYPTO_CIPHER_ALG_3DES
:
341 for (i
= 0; i
< blocks
; i
++) {
342 for (j
= 0; j
< 8; j
++)
343 ctx
->u
.des3
.cbc
[j
] ^= plain
[j
];
344 des3_encrypt(ctx
->u
.des3
.cbc
, &ctx
->u
.des3
.key
,
346 os_memcpy(crypt
, ctx
->u
.des3
.cbc
, 8);
359 int crypto_cipher_decrypt(struct crypto_cipher
*ctx
, const u8
*crypt
,
360 u8
*plain
, size_t len
)
366 case CRYPTO_CIPHER_ALG_RC4
:
368 os_memcpy(plain
, crypt
, len
);
369 rc4_skip(ctx
->u
.rc4
.key
, ctx
->u
.rc4
.keylen
,
370 ctx
->u
.rc4
.used_bytes
, plain
, len
);
371 ctx
->u
.rc4
.used_bytes
+= len
;
373 case CRYPTO_CIPHER_ALG_AES
:
374 if (len
% ctx
->u
.aes
.block_size
)
376 blocks
= len
/ ctx
->u
.aes
.block_size
;
377 for (i
= 0; i
< blocks
; i
++) {
378 os_memcpy(tmp
, crypt
, ctx
->u
.aes
.block_size
);
379 aes_decrypt(ctx
->u
.aes
.ctx_dec
, crypt
, plain
);
380 for (j
= 0; j
< ctx
->u
.aes
.block_size
; j
++)
381 plain
[j
] ^= ctx
->u
.aes
.cbc
[j
];
382 os_memcpy(ctx
->u
.aes
.cbc
, tmp
, ctx
->u
.aes
.block_size
);
383 plain
+= ctx
->u
.aes
.block_size
;
384 crypt
+= ctx
->u
.aes
.block_size
;
387 case CRYPTO_CIPHER_ALG_3DES
:
391 for (i
= 0; i
< blocks
; i
++) {
392 os_memcpy(tmp
, crypt
, 8);
393 des3_decrypt(crypt
, &ctx
->u
.des3
.key
, plain
);
394 for (j
= 0; j
< 8; j
++)
395 plain
[j
] ^= ctx
->u
.des3
.cbc
[j
];
396 os_memcpy(ctx
->u
.des3
.cbc
, tmp
, 8);
409 void crypto_cipher_deinit(struct crypto_cipher
*ctx
)
412 case CRYPTO_CIPHER_ALG_AES
:
413 aes_encrypt_deinit(ctx
->u
.aes
.ctx_enc
);
414 aes_decrypt_deinit(ctx
->u
.aes
.ctx_dec
);
416 case CRYPTO_CIPHER_ALG_3DES
:
425 /* Dummy structures; these are just typecast to struct crypto_rsa_key */
426 struct crypto_public_key
;
427 struct crypto_private_key
;
430 struct crypto_public_key
* crypto_public_key_import(const u8
*key
, size_t len
)
432 return (struct crypto_public_key
*)
433 crypto_rsa_import_public_key(key
, len
);
437 struct crypto_private_key
* crypto_private_key_import(const u8
*key
,
440 return (struct crypto_private_key
*)
441 crypto_rsa_import_private_key(key
, len
);
445 struct crypto_public_key
* crypto_public_key_from_cert(const u8
*buf
,
448 /* No X.509 support in crypto_internal.c */
453 static int pkcs1_generate_encryption_block(u8 block_type
, size_t modlen
,
454 const u8
*in
, size_t inlen
,
455 u8
*out
, size_t *outlen
)
463 * EB = 00 || BT || PS || 00 || D
464 * BT = 00 or 01 for private-key operation; 02 for public-key operation
465 * PS = k-3-||D||; at least eight octets
466 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
467 * k = length of modulus in octets (modlen)
470 if (modlen
< 12 || modlen
> *outlen
|| inlen
> modlen
- 11) {
471 wpa_printf(MSG_DEBUG
, "PKCS #1: %s - Invalid buffer "
472 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
473 __func__
, (unsigned long) modlen
,
474 (unsigned long) *outlen
,
475 (unsigned long) inlen
);
481 *pos
++ = block_type
; /* BT */
482 ps_len
= modlen
- inlen
- 3;
483 switch (block_type
) {
485 os_memset(pos
, 0x00, ps_len
);
489 os_memset(pos
, 0xff, ps_len
);
493 if (os_get_random(pos
, ps_len
) < 0) {
494 wpa_printf(MSG_DEBUG
, "PKCS #1: %s - Failed to get "
495 "random data for PS", __func__
);
505 wpa_printf(MSG_DEBUG
, "PKCS #1: %s - Unsupported block type "
506 "%d", __func__
, block_type
);
510 os_memcpy(pos
, in
, inlen
); /* D */
516 static int crypto_rsa_encrypt_pkcs1(int block_type
, struct crypto_rsa_key
*key
,
518 const u8
*in
, size_t inlen
,
519 u8
*out
, size_t *outlen
)
523 modlen
= crypto_rsa_get_modulus_len(key
);
525 if (pkcs1_generate_encryption_block(block_type
, modlen
, in
, inlen
,
529 return crypto_rsa_exptmod(out
, modlen
, out
, outlen
, key
, use_private
);
533 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key
*key
,
534 const u8
*in
, size_t inlen
,
535 u8
*out
, size_t *outlen
)
537 return crypto_rsa_encrypt_pkcs1(2, (struct crypto_rsa_key
*) key
,
538 0, in
, inlen
, out
, outlen
);
542 int crypto_private_key_sign_pkcs1(struct crypto_private_key
*key
,
543 const u8
*in
, size_t inlen
,
544 u8
*out
, size_t *outlen
)
546 return crypto_rsa_encrypt_pkcs1(1, (struct crypto_rsa_key
*) key
,
547 1, in
, inlen
, out
, outlen
);
551 void crypto_public_key_free(struct crypto_public_key
*key
)
553 crypto_rsa_free((struct crypto_rsa_key
*) key
);
557 void crypto_private_key_free(struct crypto_private_key
*key
)
559 crypto_rsa_free((struct crypto_rsa_key
*) key
);
563 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key
*key
,
564 const u8
*crypt
, size_t crypt_len
,
565 u8
*plain
, size_t *plain_len
)
571 if (crypto_rsa_exptmod(crypt
, crypt_len
, plain
, &len
,
572 (struct crypto_rsa_key
*) key
, 0) < 0)
578 * EB = 00 || BT || PS || 00 || D
580 * PS = k-3-||D|| times FF
581 * k = length of modulus in octets
584 if (len
< 3 + 8 + 16 /* min hash len */ ||
585 plain
[0] != 0x00 || plain
[1] != 0x01 || plain
[2] != 0xff) {
586 wpa_printf(MSG_INFO
, "LibTomCrypt: Invalid signature EB "
592 while (pos
< plain
+ len
&& *pos
== 0xff)
594 if (pos
- plain
- 2 < 8) {
595 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
596 wpa_printf(MSG_INFO
, "LibTomCrypt: Too short signature "
601 if (pos
+ 16 /* min hash len */ >= plain
+ len
|| *pos
!= 0x00) {
602 wpa_printf(MSG_INFO
, "LibTomCrypt: Invalid signature EB "
609 /* Strip PKCS #1 header */
610 os_memmove(plain
, pos
, len
);
617 int crypto_global_init(void)
623 void crypto_global_deinit(void)
630 int crypto_mod_exp(const u8
*base
, size_t base_len
,
631 const u8
*power
, size_t power_len
,
632 const u8
*modulus
, size_t modulus_len
,
633 u8
*result
, size_t *result_len
)
635 struct bignum
*bn_base
, *bn_exp
, *bn_modulus
, *bn_result
;
638 bn_base
= bignum_init();
639 bn_exp
= bignum_init();
640 bn_modulus
= bignum_init();
641 bn_result
= bignum_init();
643 if (bn_base
== NULL
|| bn_exp
== NULL
|| bn_modulus
== NULL
||
647 if (bignum_set_unsigned_bin(bn_base
, base
, base_len
) < 0 ||
648 bignum_set_unsigned_bin(bn_exp
, power
, power_len
) < 0 ||
649 bignum_set_unsigned_bin(bn_modulus
, modulus
, modulus_len
) < 0)
652 if (bignum_exptmod(bn_base
, bn_exp
, bn_modulus
, bn_result
) < 0)
655 ret
= bignum_get_unsigned_bin(bn_result
, result
, result_len
);
658 bignum_deinit(bn_base
);
659 bignum_deinit(bn_exp
);
660 bignum_deinit(bn_modulus
);
661 bignum_deinit(bn_result
);
665 #endif /* EAP_FAST */
668 #endif /* CONFIG_TLS_INTERNAL */
670 #endif /* EAP_TLS_FUNCS */