2 * WPA Supplicant / Crypto wrapper for LibTomCrypt (for internal TLSv1)
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
16 #define mp_init_multi ltc_init_multi
17 #define mp_clear_multi ltc_deinit_multi
18 #define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
19 #define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
20 #define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
21 #define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
25 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
31 for (i = 0; i < num_elem; i++)
32 md4_process(&md, addr[i], len[i]);
38 int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
40 u8 pkey[8], next, tmp;
44 /* Add parity bits to the key */
46 for (i = 0; i < 7; i++) {
48 pkey[i] = (tmp >> i) | next | 1;
49 next = tmp << (7 - i);
53 des_setup(pkey, 8, 0, &skey);
54 des_ecb_encrypt(clear, cypher, &skey);
60 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
66 for (i = 0; i < num_elem; i++)
67 md5_process(&md, addr[i], len[i]);
73 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
79 for (i = 0; i < num_elem; i++)
80 sha1_process(&md, addr[i], len[i]);
86 void * aes_encrypt_init(const u8 *key, size_t len)
89 skey = os_malloc(sizeof(*skey));
92 if (aes_setup(key, len, 0, skey) != CRYPT_OK) {
100 int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
102 symmetric_key *skey = ctx;
103 return aes_ecb_encrypt(plain, crypt, skey) == CRYPT_OK ? 0 : -1;
107 void aes_encrypt_deinit(void *ctx)
109 symmetric_key *skey = ctx;
115 void * aes_decrypt_init(const u8 *key, size_t len)
118 skey = os_malloc(sizeof(*skey));
121 if (aes_setup(key, len, 0, skey) != CRYPT_OK) {
129 int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
131 symmetric_key *skey = ctx;
132 return aes_ecb_encrypt(plain, (u8 *) crypt, skey) == CRYPT_OK ? 0 : -1;
136 void aes_decrypt_deinit(void *ctx)
138 symmetric_key *skey = ctx;
145 enum crypto_hash_alg alg;
154 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
157 struct crypto_hash *ctx;
159 ctx = os_zalloc(sizeof(*ctx));
166 case CRYPTO_HASH_ALG_MD5:
167 if (md5_init(&ctx->u.md) != CRYPT_OK)
170 case CRYPTO_HASH_ALG_SHA1:
171 if (sha1_init(&ctx->u.md) != CRYPT_OK)
174 case CRYPTO_HASH_ALG_HMAC_MD5:
175 if (hmac_init(&ctx->u.hmac, find_hash("md5"), key, key_len) !=
179 case CRYPTO_HASH_ALG_HMAC_SHA1:
180 if (hmac_init(&ctx->u.hmac, find_hash("sha1"), key, key_len) !=
195 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
197 if (ctx == NULL || ctx->error)
201 case CRYPTO_HASH_ALG_MD5:
202 ctx->error = md5_process(&ctx->u.md, data, len) != CRYPT_OK;
204 case CRYPTO_HASH_ALG_SHA1:
205 ctx->error = sha1_process(&ctx->u.md, data, len) != CRYPT_OK;
207 case CRYPTO_HASH_ALG_HMAC_MD5:
208 case CRYPTO_HASH_ALG_HMAC_SHA1:
209 ctx->error = hmac_process(&ctx->u.hmac, data, len) != CRYPT_OK;
215 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
223 if (mac == NULL || len == NULL) {
234 case CRYPTO_HASH_ALG_MD5:
241 if (md5_done(&ctx->u.md, mac) != CRYPT_OK)
244 case CRYPTO_HASH_ALG_SHA1:
251 if (sha1_done(&ctx->u.md, mac) != CRYPT_OK)
254 case CRYPTO_HASH_ALG_HMAC_SHA1:
261 case CRYPTO_HASH_ALG_HMAC_MD5:
268 if (hmac_done(&ctx->u.hmac, mac, &clen) != CRYPT_OK) {
288 struct crypto_cipher {
301 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
302 const u8 *iv, const u8 *key,
305 struct crypto_cipher *ctx;
306 int idx, res, rc4 = 0;
309 case CRYPTO_CIPHER_ALG_AES:
310 idx = find_cipher("aes");
312 case CRYPTO_CIPHER_ALG_3DES:
313 idx = find_cipher("3des");
315 case CRYPTO_CIPHER_ALG_DES:
316 idx = find_cipher("des");
318 case CRYPTO_CIPHER_ALG_RC2:
319 idx = find_cipher("rc2");
321 case CRYPTO_CIPHER_ALG_RC4:
329 ctx = os_zalloc(sizeof(*ctx));
335 if (key_len > sizeof(ctx->u.rc4.key)) {
339 ctx->u.rc4.keylen = key_len;
340 os_memcpy(ctx->u.rc4.key, key, key_len);
342 res = cbc_start(idx, iv, key, key_len, 0, &ctx->u.cbc);
343 if (res != CRYPT_OK) {
344 wpa_printf(MSG_DEBUG, "LibTomCrypt: Cipher start "
345 "failed: %s", error_to_string(res));
354 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
355 u8 *crypt, size_t len)
361 os_memcpy(crypt, plain, len);
362 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
363 ctx->u.rc4.used_bytes, crypt, len);
364 ctx->u.rc4.used_bytes += len;
368 res = cbc_encrypt(plain, crypt, len, &ctx->u.cbc);
369 if (res != CRYPT_OK) {
370 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC encryption "
371 "failed: %s", error_to_string(res));
378 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
379 u8 *plain, size_t len)
385 os_memcpy(plain, crypt, len);
386 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
387 ctx->u.rc4.used_bytes, plain, len);
388 ctx->u.rc4.used_bytes += len;
392 res = cbc_decrypt(crypt, plain, len, &ctx->u.cbc);
393 if (res != CRYPT_OK) {
394 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC decryption "
395 "failed: %s", error_to_string(res));
403 void crypto_cipher_deinit(struct crypto_cipher *ctx)
406 cbc_done(&ctx->u.cbc);
411 struct crypto_public_key {
415 struct crypto_private_key {
420 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
423 struct crypto_public_key *pk;
425 pk = os_zalloc(sizeof(*pk));
429 res = rsa_import(key, len, &pk->rsa);
430 if (res != CRYPT_OK) {
431 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import "
432 "public key (res=%d '%s')",
433 res, error_to_string(res));
438 if (pk->rsa.type != PK_PUBLIC) {
439 wpa_printf(MSG_ERROR, "LibTomCrypt: Public key was not of "
450 struct crypto_private_key * crypto_private_key_import(const u8 *key,
455 struct crypto_private_key *pk;
457 pk = os_zalloc(sizeof(*pk));
461 res = rsa_import(key, len, &pk->rsa);
462 if (res != CRYPT_OK) {
463 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import "
464 "private key (res=%d '%s')",
465 res, error_to_string(res));
470 if (pk->rsa.type != PK_PRIVATE) {
471 wpa_printf(MSG_ERROR, "LibTomCrypt: Private key was not of "
482 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
485 /* No X.509 support in LibTomCrypt */
490 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
491 const u8 *in, size_t inlen,
492 u8 *out, size_t *outlen)
500 * EB = 00 || BT || PS || 00 || D
501 * BT = 00 or 01 for private-key operation; 02 for public-key operation
502 * PS = k-3-||D||; at least eight octets
503 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
504 * k = length of modulus in octets (modlen)
507 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
508 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
509 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
510 __func__, (unsigned long) modlen,
511 (unsigned long) *outlen,
512 (unsigned long) inlen);
518 *pos++ = block_type; /* BT */
519 ps_len = modlen - inlen - 3;
520 switch (block_type) {
522 os_memset(pos, 0x00, ps_len);
526 os_memset(pos, 0xff, ps_len);
530 if (os_get_random(pos, ps_len) < 0) {
531 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
532 "random data for PS", __func__);
542 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
543 "%d", __func__, block_type);
547 os_memcpy(pos, in, inlen); /* D */
553 static int crypto_rsa_encrypt_pkcs1(int block_type, rsa_key *key, int key_type,
554 const u8 *in, size_t inlen,
555 u8 *out, size_t *outlen)
557 unsigned long len, modlen;
560 modlen = mp_unsigned_bin_size(key->N);
562 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
567 res = rsa_exptmod(out, modlen, out, &len, key_type, key);
568 if (res != CRYPT_OK) {
569 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s",
570 error_to_string(res));
579 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
580 const u8 *in, size_t inlen,
581 u8 *out, size_t *outlen)
583 return crypto_rsa_encrypt_pkcs1(2, &key->rsa, PK_PUBLIC, in, inlen,
588 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
589 const u8 *in, size_t inlen,
590 u8 *out, size_t *outlen)
592 return crypto_rsa_encrypt_pkcs1(1, &key->rsa, PK_PRIVATE, in, inlen,
597 void crypto_public_key_free(struct crypto_public_key *key)
606 void crypto_private_key_free(struct crypto_private_key *key)
615 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
616 const u8 *crypt, size_t crypt_len,
617 u8 *plain, size_t *plain_len)
624 res = rsa_exptmod(crypt, crypt_len, plain, &len, PK_PUBLIC,
626 if (res != CRYPT_OK) {
627 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s",
628 error_to_string(res));
635 * EB = 00 || BT || PS || 00 || D
637 * PS = k-3-||D|| times FF
638 * k = length of modulus in octets
641 if (len < 3 + 8 + 16 /* min hash len */ ||
642 plain[0] != 0x00 || plain[1] != 0x01 || plain[2] != 0xff) {
643 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
649 while (pos < plain + len && *pos == 0xff)
651 if (pos - plain - 2 < 8) {
652 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
653 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
658 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
659 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
666 /* Strip PKCS #1 header */
667 os_memmove(plain, pos, len);
674 int crypto_global_init(void)
677 /* TODO: only register algorithms that are really needed */
678 if (register_hash(&md4_desc) < 0 ||
679 register_hash(&md5_desc) < 0 ||
680 register_hash(&sha1_desc) < 0 ||
681 register_cipher(&aes_desc) < 0 ||
682 register_cipher(&des_desc) < 0 ||
683 register_cipher(&des3_desc) < 0) {
684 wpa_printf(MSG_ERROR, "TLSv1: Failed to register "
685 "hash/cipher functions");
693 void crypto_global_deinit(void)
700 int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
703 size_t pubkey_len, pad;
705 if (os_get_random(privkey, prime_len) < 0)
707 if (os_memcmp(privkey, prime, prime_len) > 0) {
708 /* Make sure private value is smaller than prime */
712 pubkey_len = prime_len;
713 if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
714 pubkey, &pubkey_len) < 0)
716 if (pubkey_len < prime_len) {
717 pad = prime_len - pubkey_len;
718 os_memmove(pubkey + pad, pubkey, pubkey_len);
719 os_memset(pubkey, 0, pad);
726 int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
727 const u8 *order, size_t order_len,
728 const u8 *privkey, size_t privkey_len,
729 const u8 *pubkey, size_t pubkey_len,
730 u8 *secret, size_t *len)
732 /* TODO: check pubkey */
733 return crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
734 prime, prime_len, secret, len);
738 int crypto_mod_exp(const u8 *base, size_t base_len,
739 const u8 *power, size_t power_len,
740 const u8 *modulus, size_t modulus_len,
741 u8 *result, size_t *result_len)
745 if (mp_init_multi(&b, &p, &m, &r, NULL) != CRYPT_OK)
748 if (mp_read_unsigned_bin(b, (u8 *) base, base_len) != CRYPT_OK ||
749 mp_read_unsigned_bin(p, (u8 *) power, power_len) != CRYPT_OK ||
750 mp_read_unsigned_bin(m, (u8 *) modulus, modulus_len) != CRYPT_OK)
753 if (mp_exptmod(b, p, m, r) != CRYPT_OK)
756 *result_len = mp_unsigned_bin_size(r);
757 if (mp_to_unsigned_bin(r, result) != CRYPT_OK)
760 mp_clear_multi(b, p, m, r, NULL);
764 mp_clear_multi(b, p, m, r, NULL);
768 #endif /* CONFIG_MODEXP */