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) {
285 struct crypto_cipher {
298 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
299 const u8 *iv, const u8 *key,
302 struct crypto_cipher *ctx;
303 int idx, res, rc4 = 0;
306 case CRYPTO_CIPHER_ALG_AES:
307 idx = find_cipher("aes");
309 case CRYPTO_CIPHER_ALG_3DES:
310 idx = find_cipher("3des");
312 case CRYPTO_CIPHER_ALG_DES:
313 idx = find_cipher("des");
315 case CRYPTO_CIPHER_ALG_RC2:
316 idx = find_cipher("rc2");
318 case CRYPTO_CIPHER_ALG_RC4:
326 ctx = os_zalloc(sizeof(*ctx));
332 if (key_len > sizeof(ctx->u.rc4.key)) {
336 ctx->u.rc4.keylen = key_len;
337 os_memcpy(ctx->u.rc4.key, key, key_len);
339 res = cbc_start(idx, iv, key, key_len, 0, &ctx->u.cbc);
340 if (res != CRYPT_OK) {
341 wpa_printf(MSG_DEBUG, "LibTomCrypt: Cipher start "
342 "failed: %s", error_to_string(res));
351 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
352 u8 *crypt, size_t len)
358 os_memcpy(crypt, plain, len);
359 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
360 ctx->u.rc4.used_bytes, crypt, len);
361 ctx->u.rc4.used_bytes += len;
365 res = cbc_encrypt(plain, crypt, len, &ctx->u.cbc);
366 if (res != CRYPT_OK) {
367 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC encryption "
368 "failed: %s", error_to_string(res));
375 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
376 u8 *plain, size_t len)
382 os_memcpy(plain, crypt, len);
383 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
384 ctx->u.rc4.used_bytes, plain, len);
385 ctx->u.rc4.used_bytes += len;
389 res = cbc_decrypt(crypt, plain, len, &ctx->u.cbc);
390 if (res != CRYPT_OK) {
391 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC decryption "
392 "failed: %s", error_to_string(res));
400 void crypto_cipher_deinit(struct crypto_cipher *ctx)
403 cbc_done(&ctx->u.cbc);
408 struct crypto_public_key {
412 struct crypto_private_key {
417 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
420 struct crypto_public_key *pk;
422 pk = os_zalloc(sizeof(*pk));
426 res = rsa_import(key, len, &pk->rsa);
427 if (res != CRYPT_OK) {
428 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import "
429 "public key (res=%d '%s')",
430 res, error_to_string(res));
435 if (pk->rsa.type != PK_PUBLIC) {
436 wpa_printf(MSG_ERROR, "LibTomCrypt: Public key was not of "
447 struct crypto_private_key * crypto_private_key_import(const u8 *key,
452 struct crypto_private_key *pk;
454 pk = os_zalloc(sizeof(*pk));
458 res = rsa_import(key, len, &pk->rsa);
459 if (res != CRYPT_OK) {
460 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import "
461 "private key (res=%d '%s')",
462 res, error_to_string(res));
467 if (pk->rsa.type != PK_PRIVATE) {
468 wpa_printf(MSG_ERROR, "LibTomCrypt: Private key was not of "
479 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
482 /* No X.509 support in LibTomCrypt */
487 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
488 const u8 *in, size_t inlen,
489 u8 *out, size_t *outlen)
497 * EB = 00 || BT || PS || 00 || D
498 * BT = 00 or 01 for private-key operation; 02 for public-key operation
499 * PS = k-3-||D||; at least eight octets
500 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
501 * k = length of modulus in octets (modlen)
504 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
505 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
506 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
507 __func__, (unsigned long) modlen,
508 (unsigned long) *outlen,
509 (unsigned long) inlen);
515 *pos++ = block_type; /* BT */
516 ps_len = modlen - inlen - 3;
517 switch (block_type) {
519 os_memset(pos, 0x00, ps_len);
523 os_memset(pos, 0xff, ps_len);
527 if (os_get_random(pos, ps_len) < 0) {
528 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
529 "random data for PS", __func__);
539 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
540 "%d", __func__, block_type);
544 os_memcpy(pos, in, inlen); /* D */
550 static int crypto_rsa_encrypt_pkcs1(int block_type, rsa_key *key, int key_type,
551 const u8 *in, size_t inlen,
552 u8 *out, size_t *outlen)
554 unsigned long len, modlen;
557 modlen = mp_unsigned_bin_size(key->N);
559 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
564 res = rsa_exptmod(out, modlen, out, &len, key_type, key);
565 if (res != CRYPT_OK) {
566 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s",
567 error_to_string(res));
576 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
577 const u8 *in, size_t inlen,
578 u8 *out, size_t *outlen)
580 return crypto_rsa_encrypt_pkcs1(2, &key->rsa, PK_PUBLIC, in, inlen,
585 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
586 const u8 *in, size_t inlen,
587 u8 *out, size_t *outlen)
589 return crypto_rsa_encrypt_pkcs1(1, &key->rsa, PK_PRIVATE, in, inlen,
594 void crypto_public_key_free(struct crypto_public_key *key)
603 void crypto_private_key_free(struct crypto_private_key *key)
612 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
613 const u8 *crypt, size_t crypt_len,
614 u8 *plain, size_t *plain_len)
621 res = rsa_exptmod(crypt, crypt_len, plain, &len, PK_PUBLIC,
623 if (res != CRYPT_OK) {
624 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s",
625 error_to_string(res));
632 * EB = 00 || BT || PS || 00 || D
634 * PS = k-3-||D|| times FF
635 * k = length of modulus in octets
638 if (len < 3 + 8 + 16 /* min hash len */ ||
639 plain[0] != 0x00 || plain[1] != 0x01 || plain[2] != 0xff) {
640 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
646 while (pos < plain + len && *pos == 0xff)
648 if (pos - plain - 2 < 8) {
649 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
650 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
655 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
656 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
663 /* Strip PKCS #1 header */
664 os_memmove(plain, pos, len);
671 int crypto_global_init(void)
674 /* TODO: only register algorithms that are really needed */
675 if (register_hash(&md4_desc) < 0 ||
676 register_hash(&md5_desc) < 0 ||
677 register_hash(&sha1_desc) < 0 ||
678 register_cipher(&aes_desc) < 0 ||
679 register_cipher(&des_desc) < 0 ||
680 register_cipher(&des3_desc) < 0) {
681 wpa_printf(MSG_ERROR, "TLSv1: Failed to register "
682 "hash/cipher functions");
690 void crypto_global_deinit(void)
697 int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
700 size_t pubkey_len, pad;
702 if (os_get_random(privkey, prime_len) < 0)
704 if (os_memcmp(privkey, prime, prime_len) > 0) {
705 /* Make sure private value is smaller than prime */
709 pubkey_len = prime_len;
710 if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
711 pubkey, &pubkey_len) < 0)
713 if (pubkey_len < prime_len) {
714 pad = prime_len - pubkey_len;
715 os_memmove(pubkey + pad, pubkey, pubkey_len);
716 os_memset(pubkey, 0, pad);
723 int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
724 const u8 *privkey, size_t privkey_len,
725 const u8 *pubkey, size_t pubkey_len,
726 u8 *secret, size_t *len)
728 return crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
729 prime, prime_len, secret, len);
733 int crypto_mod_exp(const u8 *base, size_t base_len,
734 const u8 *power, size_t power_len,
735 const u8 *modulus, size_t modulus_len,
736 u8 *result, size_t *result_len)
740 if (mp_init_multi(&b, &p, &m, &r, NULL) != CRYPT_OK)
743 if (mp_read_unsigned_bin(b, (u8 *) base, base_len) != CRYPT_OK ||
744 mp_read_unsigned_bin(p, (u8 *) power, power_len) != CRYPT_OK ||
745 mp_read_unsigned_bin(m, (u8 *) modulus, modulus_len) != CRYPT_OK)
748 if (mp_exptmod(b, p, m, r) != CRYPT_OK)
751 *result_len = mp_unsigned_bin_size(r);
752 if (mp_to_unsigned_bin(r, result) != CRYPT_OK)
755 mp_clear_multi(b, p, m, r, NULL);
759 mp_clear_multi(b, p, m, r, NULL);
763 #endif /* CONFIG_MODEXP */