1 /* 2 * WPA Supplicant / Crypto wrapper for LibTomCrypt (for internal TLSv1) 3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 #include <tomcrypt.h> 11 12 #include "common.h" 13 #include "crypto.h" 14 15 #ifndef mp_init_multi 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) 22 #endif 23 24 25 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 26 { 27 hash_state md; 28 size_t i; 29 30 md4_init(&md); 31 for (i = 0; i < num_elem; i++) 32 md4_process(&md, addr[i], len[i]); 33 md4_done(&md, mac); 34 return 0; 35 } 36 37 38 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher) 39 { 40 u8 pkey[8], next, tmp; 41 int i; 42 symmetric_key skey; 43 44 /* Add parity bits to the key */ 45 next = 0; 46 for (i = 0; i < 7; i++) { 47 tmp = key[i]; 48 pkey[i] = (tmp >> i) | next | 1; 49 next = tmp << (7 - i); 50 } 51 pkey[i] = next | 1; 52 53 des_setup(pkey, 8, 0, &skey); 54 des_ecb_encrypt(clear, cypher, &skey); 55 des_done(&skey); 56 } 57 58 59 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 60 { 61 hash_state md; 62 size_t i; 63 64 md5_init(&md); 65 for (i = 0; i < num_elem; i++) 66 md5_process(&md, addr[i], len[i]); 67 md5_done(&md, mac); 68 return 0; 69 } 70 71 72 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 73 { 74 hash_state md; 75 size_t i; 76 77 sha1_init(&md); 78 for (i = 0; i < num_elem; i++) 79 sha1_process(&md, addr[i], len[i]); 80 sha1_done(&md, mac); 81 return 0; 82 } 83 84 85 void * aes_encrypt_init(const u8 *key, size_t len) 86 { 87 symmetric_key *skey; 88 skey = os_malloc(sizeof(*skey)); 89 if (skey == NULL) 90 return NULL; 91 if (aes_setup(key, len, 0, skey) != CRYPT_OK) { 92 os_free(skey); 93 return NULL; 94 } 95 return skey; 96 } 97 98 99 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt) 100 { 101 symmetric_key *skey = ctx; 102 aes_ecb_encrypt(plain, crypt, skey); 103 } 104 105 106 void aes_encrypt_deinit(void *ctx) 107 { 108 symmetric_key *skey = ctx; 109 aes_done(skey); 110 os_free(skey); 111 } 112 113 114 void * aes_decrypt_init(const u8 *key, size_t len) 115 { 116 symmetric_key *skey; 117 skey = os_malloc(sizeof(*skey)); 118 if (skey == NULL) 119 return NULL; 120 if (aes_setup(key, len, 0, skey) != CRYPT_OK) { 121 os_free(skey); 122 return NULL; 123 } 124 return skey; 125 } 126 127 128 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) 129 { 130 symmetric_key *skey = ctx; 131 aes_ecb_encrypt(plain, (u8 *) crypt, skey); 132 } 133 134 135 void aes_decrypt_deinit(void *ctx) 136 { 137 symmetric_key *skey = ctx; 138 aes_done(skey); 139 os_free(skey); 140 } 141 142 143 struct crypto_hash { 144 enum crypto_hash_alg alg; 145 int error; 146 union { 147 hash_state md; 148 hmac_state hmac; 149 } u; 150 }; 151 152 153 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, 154 size_t key_len) 155 { 156 struct crypto_hash *ctx; 157 158 ctx = os_zalloc(sizeof(*ctx)); 159 if (ctx == NULL) 160 return NULL; 161 162 ctx->alg = alg; 163 164 switch (alg) { 165 case CRYPTO_HASH_ALG_MD5: 166 if (md5_init(&ctx->u.md) != CRYPT_OK) 167 goto fail; 168 break; 169 case CRYPTO_HASH_ALG_SHA1: 170 if (sha1_init(&ctx->u.md) != CRYPT_OK) 171 goto fail; 172 break; 173 case CRYPTO_HASH_ALG_HMAC_MD5: 174 if (hmac_init(&ctx->u.hmac, find_hash("md5"), key, key_len) != 175 CRYPT_OK) 176 goto fail; 177 break; 178 case CRYPTO_HASH_ALG_HMAC_SHA1: 179 if (hmac_init(&ctx->u.hmac, find_hash("sha1"), key, key_len) != 180 CRYPT_OK) 181 goto fail; 182 break; 183 default: 184 goto fail; 185 } 186 187 return ctx; 188 189 fail: 190 os_free(ctx); 191 return NULL; 192 } 193 194 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len) 195 { 196 if (ctx == NULL || ctx->error) 197 return; 198 199 switch (ctx->alg) { 200 case CRYPTO_HASH_ALG_MD5: 201 ctx->error = md5_process(&ctx->u.md, data, len) != CRYPT_OK; 202 break; 203 case CRYPTO_HASH_ALG_SHA1: 204 ctx->error = sha1_process(&ctx->u.md, data, len) != CRYPT_OK; 205 break; 206 case CRYPTO_HASH_ALG_HMAC_MD5: 207 case CRYPTO_HASH_ALG_HMAC_SHA1: 208 ctx->error = hmac_process(&ctx->u.hmac, data, len) != CRYPT_OK; 209 break; 210 } 211 } 212 213 214 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len) 215 { 216 int ret = 0; 217 unsigned long clen; 218 219 if (ctx == NULL) 220 return -2; 221 222 if (mac == NULL || len == NULL) { 223 os_free(ctx); 224 return 0; 225 } 226 227 if (ctx->error) { 228 os_free(ctx); 229 return -2; 230 } 231 232 switch (ctx->alg) { 233 case CRYPTO_HASH_ALG_MD5: 234 if (*len < 16) { 235 *len = 16; 236 os_free(ctx); 237 return -1; 238 } 239 *len = 16; 240 if (md5_done(&ctx->u.md, mac) != CRYPT_OK) 241 ret = -2; 242 break; 243 case CRYPTO_HASH_ALG_SHA1: 244 if (*len < 20) { 245 *len = 20; 246 os_free(ctx); 247 return -1; 248 } 249 *len = 20; 250 if (sha1_done(&ctx->u.md, mac) != CRYPT_OK) 251 ret = -2; 252 break; 253 case CRYPTO_HASH_ALG_HMAC_SHA1: 254 if (*len < 20) { 255 *len = 20; 256 os_free(ctx); 257 return -1; 258 } 259 /* continue */ 260 case CRYPTO_HASH_ALG_HMAC_MD5: 261 if (*len < 16) { 262 *len = 16; 263 os_free(ctx); 264 return -1; 265 } 266 clen = *len; 267 if (hmac_done(&ctx->u.hmac, mac, &clen) != CRYPT_OK) { 268 os_free(ctx); 269 return -1; 270 } 271 *len = clen; 272 break; 273 default: 274 ret = -2; 275 break; 276 } 277 278 os_free(ctx); 279 280 return ret; 281 } 282 283 284 struct crypto_cipher { 285 int rc4; 286 union { 287 symmetric_CBC cbc; 288 struct { 289 size_t used_bytes; 290 u8 key[16]; 291 size_t keylen; 292 } rc4; 293 } u; 294 }; 295 296 297 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg, 298 const u8 *iv, const u8 *key, 299 size_t key_len) 300 { 301 struct crypto_cipher *ctx; 302 int idx, res, rc4 = 0; 303 304 switch (alg) { 305 case CRYPTO_CIPHER_ALG_AES: 306 idx = find_cipher("aes"); 307 break; 308 case CRYPTO_CIPHER_ALG_3DES: 309 idx = find_cipher("3des"); 310 break; 311 case CRYPTO_CIPHER_ALG_DES: 312 idx = find_cipher("des"); 313 break; 314 case CRYPTO_CIPHER_ALG_RC2: 315 idx = find_cipher("rc2"); 316 break; 317 case CRYPTO_CIPHER_ALG_RC4: 318 idx = -1; 319 rc4 = 1; 320 break; 321 default: 322 return NULL; 323 } 324 325 ctx = os_zalloc(sizeof(*ctx)); 326 if (ctx == NULL) 327 return NULL; 328 329 if (rc4) { 330 ctx->rc4 = 1; 331 if (key_len > sizeof(ctx->u.rc4.key)) { 332 os_free(ctx); 333 return NULL; 334 } 335 ctx->u.rc4.keylen = key_len; 336 os_memcpy(ctx->u.rc4.key, key, key_len); 337 } else { 338 res = cbc_start(idx, iv, key, key_len, 0, &ctx->u.cbc); 339 if (res != CRYPT_OK) { 340 wpa_printf(MSG_DEBUG, "LibTomCrypt: Cipher start " 341 "failed: %s", error_to_string(res)); 342 os_free(ctx); 343 return NULL; 344 } 345 } 346 347 return ctx; 348 } 349 350 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, 351 u8 *crypt, size_t len) 352 { 353 int res; 354 355 if (ctx->rc4) { 356 if (plain != crypt) 357 os_memcpy(crypt, plain, len); 358 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen, 359 ctx->u.rc4.used_bytes, crypt, len); 360 ctx->u.rc4.used_bytes += len; 361 return 0; 362 } 363 364 res = cbc_encrypt(plain, crypt, len, &ctx->u.cbc); 365 if (res != CRYPT_OK) { 366 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC encryption " 367 "failed: %s", error_to_string(res)); 368 return -1; 369 } 370 return 0; 371 } 372 373 374 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, 375 u8 *plain, size_t len) 376 { 377 int res; 378 379 if (ctx->rc4) { 380 if (plain != crypt) 381 os_memcpy(plain, crypt, len); 382 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen, 383 ctx->u.rc4.used_bytes, plain, len); 384 ctx->u.rc4.used_bytes += len; 385 return 0; 386 } 387 388 res = cbc_decrypt(crypt, plain, len, &ctx->u.cbc); 389 if (res != CRYPT_OK) { 390 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC decryption " 391 "failed: %s", error_to_string(res)); 392 return -1; 393 } 394 395 return 0; 396 } 397 398 399 void crypto_cipher_deinit(struct crypto_cipher *ctx) 400 { 401 if (!ctx->rc4) 402 cbc_done(&ctx->u.cbc); 403 os_free(ctx); 404 } 405 406 407 struct crypto_public_key { 408 rsa_key rsa; 409 }; 410 411 struct crypto_private_key { 412 rsa_key rsa; 413 }; 414 415 416 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) 417 { 418 int res; 419 struct crypto_public_key *pk; 420 421 pk = os_zalloc(sizeof(*pk)); 422 if (pk == NULL) 423 return NULL; 424 425 res = rsa_import(key, len, &pk->rsa); 426 if (res != CRYPT_OK) { 427 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import " 428 "public key (res=%d '%s')", 429 res, error_to_string(res)); 430 os_free(pk); 431 return NULL; 432 } 433 434 if (pk->rsa.type != PK_PUBLIC) { 435 wpa_printf(MSG_ERROR, "LibTomCrypt: Public key was not of " 436 "correct type"); 437 rsa_free(&pk->rsa); 438 os_free(pk); 439 return NULL; 440 } 441 442 return pk; 443 } 444 445 446 struct crypto_private_key * crypto_private_key_import(const u8 *key, 447 size_t len, 448 const char *passwd) 449 { 450 int res; 451 struct crypto_private_key *pk; 452 453 pk = os_zalloc(sizeof(*pk)); 454 if (pk == NULL) 455 return NULL; 456 457 res = rsa_import(key, len, &pk->rsa); 458 if (res != CRYPT_OK) { 459 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import " 460 "private key (res=%d '%s')", 461 res, error_to_string(res)); 462 os_free(pk); 463 return NULL; 464 } 465 466 if (pk->rsa.type != PK_PRIVATE) { 467 wpa_printf(MSG_ERROR, "LibTomCrypt: Private key was not of " 468 "correct type"); 469 rsa_free(&pk->rsa); 470 os_free(pk); 471 return NULL; 472 } 473 474 return pk; 475 } 476 477 478 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf, 479 size_t len) 480 { 481 /* No X.509 support in LibTomCrypt */ 482 return NULL; 483 } 484 485 486 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen, 487 const u8 *in, size_t inlen, 488 u8 *out, size_t *outlen) 489 { 490 size_t ps_len; 491 u8 *pos; 492 493 /* 494 * PKCS #1 v1.5, 8.1: 495 * 496 * EB = 00 || BT || PS || 00 || D 497 * BT = 00 or 01 for private-key operation; 02 for public-key operation 498 * PS = k-3-||D||; at least eight octets 499 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero) 500 * k = length of modulus in octets (modlen) 501 */ 502 503 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) { 504 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer " 505 "lengths (modlen=%lu outlen=%lu inlen=%lu)", 506 __func__, (unsigned long) modlen, 507 (unsigned long) *outlen, 508 (unsigned long) inlen); 509 return -1; 510 } 511 512 pos = out; 513 *pos++ = 0x00; 514 *pos++ = block_type; /* BT */ 515 ps_len = modlen - inlen - 3; 516 switch (block_type) { 517 case 0: 518 os_memset(pos, 0x00, ps_len); 519 pos += ps_len; 520 break; 521 case 1: 522 os_memset(pos, 0xff, ps_len); 523 pos += ps_len; 524 break; 525 case 2: 526 if (os_get_random(pos, ps_len) < 0) { 527 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get " 528 "random data for PS", __func__); 529 return -1; 530 } 531 while (ps_len--) { 532 if (*pos == 0x00) 533 *pos = 0x01; 534 pos++; 535 } 536 break; 537 default: 538 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type " 539 "%d", __func__, block_type); 540 return -1; 541 } 542 *pos++ = 0x00; 543 os_memcpy(pos, in, inlen); /* D */ 544 545 return 0; 546 } 547 548 549 static int crypto_rsa_encrypt_pkcs1(int block_type, rsa_key *key, int key_type, 550 const u8 *in, size_t inlen, 551 u8 *out, size_t *outlen) 552 { 553 unsigned long len, modlen; 554 int res; 555 556 modlen = mp_unsigned_bin_size(key->N); 557 558 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen, 559 out, outlen) < 0) 560 return -1; 561 562 len = *outlen; 563 res = rsa_exptmod(out, modlen, out, &len, key_type, key); 564 if (res != CRYPT_OK) { 565 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s", 566 error_to_string(res)); 567 return -1; 568 } 569 *outlen = len; 570 571 return 0; 572 } 573 574 575 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, 576 const u8 *in, size_t inlen, 577 u8 *out, size_t *outlen) 578 { 579 return crypto_rsa_encrypt_pkcs1(2, &key->rsa, PK_PUBLIC, in, inlen, 580 out, outlen); 581 } 582 583 584 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key, 585 const u8 *in, size_t inlen, 586 u8 *out, size_t *outlen) 587 { 588 return crypto_rsa_encrypt_pkcs1(1, &key->rsa, PK_PRIVATE, in, inlen, 589 out, outlen); 590 } 591 592 593 void crypto_public_key_free(struct crypto_public_key *key) 594 { 595 if (key) { 596 rsa_free(&key->rsa); 597 os_free(key); 598 } 599 } 600 601 602 void crypto_private_key_free(struct crypto_private_key *key) 603 { 604 if (key) { 605 rsa_free(&key->rsa); 606 os_free(key); 607 } 608 } 609 610 611 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, 612 const u8 *crypt, size_t crypt_len, 613 u8 *plain, size_t *plain_len) 614 { 615 int res; 616 unsigned long len; 617 u8 *pos; 618 619 len = *plain_len; 620 res = rsa_exptmod(crypt, crypt_len, plain, &len, PK_PUBLIC, 621 &key->rsa); 622 if (res != CRYPT_OK) { 623 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s", 624 error_to_string(res)); 625 return -1; 626 } 627 628 /* 629 * PKCS #1 v1.5, 8.1: 630 * 631 * EB = 00 || BT || PS || 00 || D 632 * BT = 01 633 * PS = k-3-||D|| times FF 634 * k = length of modulus in octets 635 */ 636 637 if (len < 3 + 8 + 16 /* min hash len */ || 638 plain[0] != 0x00 || plain[1] != 0x01 || plain[2] != 0xff) { 639 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " 640 "structure"); 641 return -1; 642 } 643 644 pos = plain + 3; 645 while (pos < plain + len && *pos == 0xff) 646 pos++; 647 if (pos - plain - 2 < 8) { 648 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */ 649 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature " 650 "padding"); 651 return -1; 652 } 653 654 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) { 655 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " 656 "structure (2)"); 657 return -1; 658 } 659 pos++; 660 len -= pos - plain; 661 662 /* Strip PKCS #1 header */ 663 os_memmove(plain, pos, len); 664 *plain_len = len; 665 666 return 0; 667 } 668 669 670 int crypto_global_init(void) 671 { 672 ltc_mp = tfm_desc; 673 /* TODO: only register algorithms that are really needed */ 674 if (register_hash(&md4_desc) < 0 || 675 register_hash(&md5_desc) < 0 || 676 register_hash(&sha1_desc) < 0 || 677 register_cipher(&aes_desc) < 0 || 678 register_cipher(&des_desc) < 0 || 679 register_cipher(&des3_desc) < 0) { 680 wpa_printf(MSG_ERROR, "TLSv1: Failed to register " 681 "hash/cipher functions"); 682 return -1; 683 } 684 685 return 0; 686 } 687 688 689 void crypto_global_deinit(void) 690 { 691 } 692 693 694 #ifdef CONFIG_MODEXP 695 696 int crypto_mod_exp(const u8 *base, size_t base_len, 697 const u8 *power, size_t power_len, 698 const u8 *modulus, size_t modulus_len, 699 u8 *result, size_t *result_len) 700 { 701 void *b, *p, *m, *r; 702 703 if (mp_init_multi(&b, &p, &m, &r, NULL) != CRYPT_OK) 704 return -1; 705 706 if (mp_read_unsigned_bin(b, (u8 *) base, base_len) != CRYPT_OK || 707 mp_read_unsigned_bin(p, (u8 *) power, power_len) != CRYPT_OK || 708 mp_read_unsigned_bin(m, (u8 *) modulus, modulus_len) != CRYPT_OK) 709 goto fail; 710 711 if (mp_exptmod(b, p, m, r) != CRYPT_OK) 712 goto fail; 713 714 *result_len = mp_unsigned_bin_size(r); 715 if (mp_to_unsigned_bin(r, result) != CRYPT_OK) 716 goto fail; 717 718 mp_clear_multi(b, p, m, r, NULL); 719 return 0; 720 721 fail: 722 mp_clear_multi(b, p, m, r, NULL); 723 return -1; 724 } 725 726 #endif /* CONFIG_MODEXP */ 727