2 * WPA Supplicant / Crypto wrapper for LibTomCrypt (for internal TLSv1)
3 * Copyright (c) 2005-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.
22 #define mp_init_multi ltc_init_multi
23 #define mp_clear_multi ltc_deinit_multi
24 #define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
25 #define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
26 #define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
27 #define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
31 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
37 for (i = 0; i < num_elem; i++)
38 md4_process(&md, addr[i], len[i]);
44 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
46 u8 pkey[8], next, tmp;
50 /* Add parity bits to the key */
52 for (i = 0; i < 7; i++) {
54 pkey[i] = (tmp >> i) | next | 1;
55 next = tmp << (7 - i);
59 des_setup(pkey, 8, 0, &skey);
60 des_ecb_encrypt(clear, cypher, &skey);
66 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
72 for (i = 0; i < num_elem; i++)
73 md5_process(&md, addr[i], len[i]);
79 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
85 for (i = 0; i < num_elem; i++)
86 sha1_process(&md, addr[i], len[i]);
92 void * aes_encrypt_init(const u8 *key, size_t len)
95 skey = os_malloc(sizeof(*skey));
98 if (aes_setup(key, len, 0, skey) != CRYPT_OK) {
106 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
108 symmetric_key *skey = ctx;
109 aes_ecb_encrypt(plain, crypt, skey);
113 void aes_encrypt_deinit(void *ctx)
115 symmetric_key *skey = ctx;
121 void * aes_decrypt_init(const u8 *key, size_t len)
124 skey = os_malloc(sizeof(*skey));
127 if (aes_setup(key, len, 0, skey) != CRYPT_OK) {
135 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
137 symmetric_key *skey = ctx;
138 aes_ecb_encrypt(plain, (u8 *) crypt, skey);
142 void aes_decrypt_deinit(void *ctx)
144 symmetric_key *skey = ctx;
150 #ifdef CONFIG_TLS_INTERNAL
153 enum crypto_hash_alg alg;
162 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
165 struct crypto_hash *ctx;
167 ctx = os_zalloc(sizeof(*ctx));
174 case CRYPTO_HASH_ALG_MD5:
175 if (md5_init(&ctx->u.md) != CRYPT_OK)
178 case CRYPTO_HASH_ALG_SHA1:
179 if (sha1_init(&ctx->u.md) != CRYPT_OK)
182 case CRYPTO_HASH_ALG_HMAC_MD5:
183 if (hmac_init(&ctx->u.hmac, find_hash("md5"), key, key_len) !=
187 case CRYPTO_HASH_ALG_HMAC_SHA1:
188 if (hmac_init(&ctx->u.hmac, find_hash("sha1"), key, key_len) !=
203 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
205 if (ctx == NULL || ctx->error)
209 case CRYPTO_HASH_ALG_MD5:
210 ctx->error = md5_process(&ctx->u.md, data, len) != CRYPT_OK;
212 case CRYPTO_HASH_ALG_SHA1:
213 ctx->error = sha1_process(&ctx->u.md, data, len) != CRYPT_OK;
215 case CRYPTO_HASH_ALG_HMAC_MD5:
216 case CRYPTO_HASH_ALG_HMAC_SHA1:
217 ctx->error = hmac_process(&ctx->u.hmac, data, len) != CRYPT_OK;
223 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
231 if (mac == NULL || len == NULL) {
242 case CRYPTO_HASH_ALG_MD5:
249 if (md5_done(&ctx->u.md, mac) != CRYPT_OK)
252 case CRYPTO_HASH_ALG_SHA1:
259 if (sha1_done(&ctx->u.md, mac) != CRYPT_OK)
262 case CRYPTO_HASH_ALG_HMAC_SHA1:
269 case CRYPTO_HASH_ALG_HMAC_MD5:
276 if (hmac_done(&ctx->u.hmac, mac, &clen) != CRYPT_OK) {
293 struct crypto_cipher {
306 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
307 const u8 *iv, const u8 *key,
310 struct crypto_cipher *ctx;
311 int idx, res, rc4 = 0;
314 case CRYPTO_CIPHER_ALG_AES:
315 idx = find_cipher("aes");
317 case CRYPTO_CIPHER_ALG_3DES:
318 idx = find_cipher("3des");
320 case CRYPTO_CIPHER_ALG_DES:
321 idx = find_cipher("des");
323 case CRYPTO_CIPHER_ALG_RC2:
324 idx = find_cipher("rc2");
326 case CRYPTO_CIPHER_ALG_RC4:
334 ctx = os_zalloc(sizeof(*ctx));
340 if (key_len > sizeof(ctx->u.rc4.key)) {
344 ctx->u.rc4.keylen = key_len;
345 os_memcpy(ctx->u.rc4.key, key, key_len);
347 res = cbc_start(idx, iv, key, key_len, 0, &ctx->u.cbc);
348 if (res != CRYPT_OK) {
349 wpa_printf(MSG_DEBUG, "LibTomCrypt: Cipher start "
350 "failed: %s", error_to_string(res));
359 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
360 u8 *crypt, size_t len)
366 os_memcpy(crypt, plain, len);
367 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
368 ctx->u.rc4.used_bytes, crypt, len);
369 ctx->u.rc4.used_bytes += len;
373 res = cbc_encrypt(plain, crypt, len, &ctx->u.cbc);
374 if (res != CRYPT_OK) {
375 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC encryption "
376 "failed: %s", error_to_string(res));
383 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
384 u8 *plain, size_t len)
390 os_memcpy(plain, crypt, len);
391 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
392 ctx->u.rc4.used_bytes, plain, len);
393 ctx->u.rc4.used_bytes += len;
397 res = cbc_decrypt(crypt, plain, len, &ctx->u.cbc);
398 if (res != CRYPT_OK) {
399 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC decryption "
400 "failed: %s", error_to_string(res));
408 void crypto_cipher_deinit(struct crypto_cipher *ctx)
411 cbc_done(&ctx->u.cbc);
416 struct crypto_public_key {
420 struct crypto_private_key {
425 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
428 struct crypto_public_key *pk;
430 pk = os_zalloc(sizeof(*pk));
434 res = rsa_import(key, len, &pk->rsa);
435 if (res != CRYPT_OK) {
436 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import "
437 "public key (res=%d '%s')",
438 res, error_to_string(res));
443 if (pk->rsa.type != PK_PUBLIC) {
444 wpa_printf(MSG_ERROR, "LibTomCrypt: Public key was not of "
455 struct crypto_private_key * crypto_private_key_import(const u8 *key,
459 struct crypto_private_key *pk;
461 pk = os_zalloc(sizeof(*pk));
465 res = rsa_import(key, len, &pk->rsa);
466 if (res != CRYPT_OK) {
467 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import "
468 "private key (res=%d '%s')",
469 res, error_to_string(res));
474 if (pk->rsa.type != PK_PRIVATE) {
475 wpa_printf(MSG_ERROR, "LibTomCrypt: Private key was not of "
486 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
489 /* No X.509 support in LibTomCrypt */
494 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
495 const u8 *in, size_t inlen,
496 u8 *out, size_t *outlen)
504 * EB = 00 || BT || PS || 00 || D
505 * BT = 00 or 01 for private-key operation; 02 for public-key operation
506 * PS = k-3-||D||; at least eight octets
507 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
508 * k = length of modulus in octets (modlen)
511 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
512 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
513 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
514 __func__, (unsigned long) modlen,
515 (unsigned long) *outlen,
516 (unsigned long) inlen);
522 *pos++ = block_type; /* BT */
523 ps_len = modlen - inlen - 3;
524 switch (block_type) {
526 os_memset(pos, 0x00, ps_len);
530 os_memset(pos, 0xff, ps_len);
534 if (os_get_random(pos, ps_len) < 0) {
535 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
536 "random data for PS", __func__);
546 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
547 "%d", __func__, block_type);
551 os_memcpy(pos, in, inlen); /* D */
557 static int crypto_rsa_encrypt_pkcs1(int block_type, rsa_key *key, int key_type,
558 const u8 *in, size_t inlen,
559 u8 *out, size_t *outlen)
561 unsigned long len, modlen;
564 modlen = mp_unsigned_bin_size(key->N);
566 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
571 res = rsa_exptmod(out, modlen, out, &len, key_type, key);
572 if (res != CRYPT_OK) {
573 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s",
574 error_to_string(res));
583 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
584 const u8 *in, size_t inlen,
585 u8 *out, size_t *outlen)
587 return crypto_rsa_encrypt_pkcs1(2, &key->rsa, PK_PUBLIC, in, inlen,
592 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
593 const u8 *in, size_t inlen,
594 u8 *out, size_t *outlen)
596 return crypto_rsa_encrypt_pkcs1(1, &key->rsa, PK_PRIVATE, in, inlen,
601 void crypto_public_key_free(struct crypto_public_key *key)
610 void crypto_private_key_free(struct crypto_private_key *key)
619 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
620 const u8 *crypt, size_t crypt_len,
621 u8 *plain, size_t *plain_len)
628 res = rsa_exptmod(crypt, crypt_len, plain, &len, PK_PUBLIC,
630 if (res != CRYPT_OK) {
631 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s",
632 error_to_string(res));
639 * EB = 00 || BT || PS || 00 || D
641 * PS = k-3-||D|| times FF
642 * k = length of modulus in octets
645 if (len < 3 + 8 + 16 /* min hash len */ ||
646 plain[0] != 0x00 || plain[1] != 0x01 || plain[2] != 0xff) {
647 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
653 while (pos < plain + len && *pos == 0xff)
655 if (pos - plain - 2 < 8) {
656 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
657 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
662 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
663 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
670 /* Strip PKCS #1 header */
671 os_memmove(plain, pos, len);
678 int crypto_global_init(void)
681 /* TODO: only register algorithms that are really needed */
682 if (register_hash(&md4_desc) < 0 ||
683 register_hash(&md5_desc) < 0 ||
684 register_hash(&sha1_desc) < 0 ||
685 register_cipher(&aes_desc) < 0 ||
686 register_cipher(&des_desc) < 0 ||
687 register_cipher(&des3_desc) < 0) {
688 wpa_printf(MSG_ERROR, "TLSv1: Failed to register "
689 "hash/cipher functions");
697 void crypto_global_deinit(void)
702 #if defined(EAP_FAST) || defined(EAP_SERVER_FAST)
704 int crypto_mod_exp(const u8 *base, size_t base_len,
705 const u8 *power, size_t power_len,
706 const u8 *modulus, size_t modulus_len,
707 u8 *result, size_t *result_len)
711 if (mp_init_multi(&b, &p, &m, &r, NULL) != CRYPT_OK)
714 if (mp_read_unsigned_bin(b, (u8 *) base, base_len) != CRYPT_OK ||
715 mp_read_unsigned_bin(p, (u8 *) power, power_len) != CRYPT_OK ||
716 mp_read_unsigned_bin(m, (u8 *) modulus, modulus_len) != CRYPT_OK)
719 if (mp_exptmod(b, p, m, r) != CRYPT_OK)
722 *result_len = mp_unsigned_bin_size(r);
723 if (mp_to_unsigned_bin(r, result) != CRYPT_OK)
726 mp_clear_multi(b, p, m, r, NULL);
730 mp_clear_multi(b, p, m, r, NULL);
734 #endif /* EAP_FAST || EAP_SERVER_FAST */
736 #endif /* CONFIG_TLS_INTERNAL */
738 #endif /* EAP_TLS_FUNCS */