231
struct crypto_cipher {
232
enum crypto_cipher_alg alg;
246
struct des3_key_s key;
253
struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
254
const u8 *iv, const u8 *key,
257
struct crypto_cipher *ctx;
259
ctx = os_zalloc(sizeof(*ctx));
266
case CRYPTO_CIPHER_ALG_RC4:
267
if (key_len > sizeof(ctx->u.rc4.key)) {
271
ctx->u.rc4.keylen = key_len;
272
os_memcpy(ctx->u.rc4.key, key, key_len);
274
case CRYPTO_CIPHER_ALG_AES:
275
if (key_len > sizeof(ctx->u.aes.cbc)) {
279
ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
280
if (ctx->u.aes.ctx_enc == NULL) {
284
ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
285
if (ctx->u.aes.ctx_dec == NULL) {
286
aes_encrypt_deinit(ctx->u.aes.ctx_enc);
290
ctx->u.aes.block_size = key_len;
291
os_memcpy(ctx->u.aes.cbc, iv, ctx->u.aes.block_size);
293
case CRYPTO_CIPHER_ALG_3DES:
298
des3_key_setup(key, &ctx->u.des3.key);
299
os_memcpy(ctx->u.des3.cbc, iv, 8);
310
int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
311
u8 *crypt, size_t len)
316
case CRYPTO_CIPHER_ALG_RC4:
318
os_memcpy(crypt, plain, len);
319
rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
320
ctx->u.rc4.used_bytes, crypt, len);
321
ctx->u.rc4.used_bytes += len;
323
case CRYPTO_CIPHER_ALG_AES:
324
if (len % ctx->u.aes.block_size)
326
blocks = len / ctx->u.aes.block_size;
327
for (i = 0; i < blocks; i++) {
328
for (j = 0; j < ctx->u.aes.block_size; j++)
329
ctx->u.aes.cbc[j] ^= plain[j];
330
aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
332
os_memcpy(crypt, ctx->u.aes.cbc,
333
ctx->u.aes.block_size);
334
plain += ctx->u.aes.block_size;
335
crypt += ctx->u.aes.block_size;
338
case CRYPTO_CIPHER_ALG_3DES:
342
for (i = 0; i < blocks; i++) {
343
for (j = 0; j < 8; j++)
344
ctx->u.des3.cbc[j] ^= plain[j];
345
des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
347
os_memcpy(crypt, ctx->u.des3.cbc, 8);
360
int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
361
u8 *plain, size_t len)
367
case CRYPTO_CIPHER_ALG_RC4:
369
os_memcpy(plain, crypt, len);
370
rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
371
ctx->u.rc4.used_bytes, plain, len);
372
ctx->u.rc4.used_bytes += len;
374
case CRYPTO_CIPHER_ALG_AES:
375
if (len % ctx->u.aes.block_size)
377
blocks = len / ctx->u.aes.block_size;
378
for (i = 0; i < blocks; i++) {
379
os_memcpy(tmp, crypt, ctx->u.aes.block_size);
380
aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
381
for (j = 0; j < ctx->u.aes.block_size; j++)
382
plain[j] ^= ctx->u.aes.cbc[j];
383
os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
384
plain += ctx->u.aes.block_size;
385
crypt += ctx->u.aes.block_size;
388
case CRYPTO_CIPHER_ALG_3DES:
392
for (i = 0; i < blocks; i++) {
393
os_memcpy(tmp, crypt, 8);
394
des3_decrypt(crypt, &ctx->u.des3.key, plain);
395
for (j = 0; j < 8; j++)
396
plain[j] ^= ctx->u.des3.cbc[j];
397
os_memcpy(ctx->u.des3.cbc, tmp, 8);
410
void crypto_cipher_deinit(struct crypto_cipher *ctx)
413
case CRYPTO_CIPHER_ALG_AES:
414
aes_encrypt_deinit(ctx->u.aes.ctx_enc);
415
aes_decrypt_deinit(ctx->u.aes.ctx_dec);
417
case CRYPTO_CIPHER_ALG_3DES:
426
/* Dummy structures; these are just typecast to struct crypto_rsa_key */
427
struct crypto_public_key;
428
struct crypto_private_key;
431
struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
433
return (struct crypto_public_key *)
434
crypto_rsa_import_public_key(key, len);
439
static struct crypto_private_key *
440
crypto_pkcs8_key_import(const u8 *buf, size_t len)
448
/* PKCS #8, Chapter 6 */
450
/* PrivateKeyInfo ::= SEQUENCE */
451
if (asn1_get_next(buf, len, &hdr) < 0 ||
452
hdr.class != ASN1_CLASS_UNIVERSAL ||
453
hdr.tag != ASN1_TAG_SEQUENCE) {
454
wpa_printf(MSG_DEBUG, "PKCS #8: Does not start with PKCS #8 "
455
"header (SEQUENCE); assume PKCS #8 not used");
459
end = pos + hdr.length;
461
/* version Version (Version ::= INTEGER) */
462
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
463
hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
464
wpa_printf(MSG_DEBUG, "PKCS #8: Expected INTEGER - found "
465
"class %d tag 0x%x; assume PKCS #8 not used",
470
zero = bignum_init();
474
if (bignum_set_unsigned_bin(zero, hdr.payload, hdr.length) < 0) {
475
wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse INTEGER");
479
pos = hdr.payload + hdr.length;
481
if (bignum_cmp_d(zero, 0) != 0) {
482
wpa_printf(MSG_DEBUG, "PKCS #8: Expected zero INTEGER in the "
483
"beginning of private key; not found; assume "
490
/* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier
491
* (PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier) */
492
if (asn1_get_next(pos, len, &hdr) < 0 ||
493
hdr.class != ASN1_CLASS_UNIVERSAL ||
494
hdr.tag != ASN1_TAG_SEQUENCE) {
495
wpa_printf(MSG_DEBUG, "PKCS #8: Expected SEQUENCE "
496
"(AlgorithmIdentifier) - found class %d tag 0x%x; "
497
"assume PKCS #8 not used",
502
if (asn1_get_oid(hdr.payload, hdr.length, &oid, &pos)) {
503
wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse OID "
504
"(algorithm); assume PKCS #8 not used");
508
asn1_oid_to_str(&oid, obuf, sizeof(obuf));
509
wpa_printf(MSG_DEBUG, "PKCS #8: algorithm=%s", obuf);
512
oid.oid[0] != 1 /* iso */ ||
513
oid.oid[1] != 2 /* member-body */ ||
514
oid.oid[2] != 840 /* us */ ||
515
oid.oid[3] != 113549 /* rsadsi */ ||
516
oid.oid[4] != 1 /* pkcs */ ||
517
oid.oid[5] != 1 /* pkcs-1 */ ||
518
oid.oid[6] != 1 /* rsaEncryption */) {
519
wpa_printf(MSG_DEBUG, "PKCS #8: Unsupported private key "
520
"algorithm %s", obuf);
524
pos = hdr.payload + hdr.length;
526
/* privateKey PrivateKey (PrivateKey ::= OCTET STRING) */
527
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
528
hdr.class != ASN1_CLASS_UNIVERSAL ||
529
hdr.tag != ASN1_TAG_OCTETSTRING) {
530
wpa_printf(MSG_DEBUG, "PKCS #8: Expected OCTETSTRING "
531
"(privateKey) - found class %d tag 0x%x",
535
wpa_printf(MSG_DEBUG, "PKCS #8: Try to parse RSAPrivateKey");
537
return (struct crypto_private_key *)
538
crypto_rsa_import_private_key(hdr.payload, hdr.length);
540
#endif /* EAP_TLS_FUNCS */
543
struct crypto_private_key * crypto_private_key_import(const u8 *key,
546
struct crypto_private_key *res;
548
/* First, check for possible PKCS #8 encoding */
549
res = crypto_pkcs8_key_import(key, len);
553
/* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
554
wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
556
return (struct crypto_private_key *)
557
crypto_rsa_import_private_key(key, len);
561
struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
564
/* No X.509 support in crypto_internal.c */
569
static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
570
const u8 *in, size_t inlen,
571
u8 *out, size_t *outlen)
579
* EB = 00 || BT || PS || 00 || D
580
* BT = 00 or 01 for private-key operation; 02 for public-key operation
581
* PS = k-3-||D||; at least eight octets
582
* (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
583
* k = length of modulus in octets (modlen)
586
if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
587
wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
588
"lengths (modlen=%lu outlen=%lu inlen=%lu)",
589
__func__, (unsigned long) modlen,
590
(unsigned long) *outlen,
591
(unsigned long) inlen);
597
*pos++ = block_type; /* BT */
598
ps_len = modlen - inlen - 3;
599
switch (block_type) {
601
os_memset(pos, 0x00, ps_len);
605
os_memset(pos, 0xff, ps_len);
609
if (os_get_random(pos, ps_len) < 0) {
610
wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
611
"random data for PS", __func__);
621
wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
622
"%d", __func__, block_type);
626
os_memcpy(pos, in, inlen); /* D */
632
static int crypto_rsa_encrypt_pkcs1(int block_type, struct crypto_rsa_key *key,
634
const u8 *in, size_t inlen,
635
u8 *out, size_t *outlen)
639
modlen = crypto_rsa_get_modulus_len(key);
641
if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
645
return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
649
int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
650
const u8 *in, size_t inlen,
651
u8 *out, size_t *outlen)
653
return crypto_rsa_encrypt_pkcs1(2, (struct crypto_rsa_key *) key,
654
0, in, inlen, out, outlen);
658
int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
659
const u8 *in, size_t inlen,
660
u8 *out, size_t *outlen)
662
struct crypto_rsa_key *rkey = (struct crypto_rsa_key *) key;
666
res = crypto_rsa_exptmod(in, inlen, out, outlen, rkey, 1);
670
if (*outlen < 2 || out[0] != 0 || out[1] != 2)
673
/* Skip PS (pseudorandom non-zero octets) */
676
while (*pos && pos < end)
682
*outlen -= pos - out;
684
/* Strip PKCS #1 header */
685
os_memmove(out, pos, *outlen);
691
int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
692
const u8 *in, size_t inlen,
693
u8 *out, size_t *outlen)
695
return crypto_rsa_encrypt_pkcs1(1, (struct crypto_rsa_key *) key,
696
1, in, inlen, out, outlen);
700
void crypto_public_key_free(struct crypto_public_key *key)
702
crypto_rsa_free((struct crypto_rsa_key *) key);
706
void crypto_private_key_free(struct crypto_private_key *key)
708
crypto_rsa_free((struct crypto_rsa_key *) key);
712
int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
713
const u8 *crypt, size_t crypt_len,
714
u8 *plain, size_t *plain_len)
720
if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len,
721
(struct crypto_rsa_key *) key, 0) < 0)
727
* EB = 00 || BT || PS || 00 || D
729
* PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
730
* k = length of modulus in octets
733
if (len < 3 + 8 + 16 /* min hash len */ ||
734
plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
735
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
741
if (plain[1] == 0x00) {
743
if (plain[2] != 0x00) {
744
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
748
while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
752
if (plain[2] != 0xff) {
753
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
757
while (pos < plain + len && *pos == 0xff)
761
if (pos - plain - 2 < 8) {
762
/* PKCS #1 v1.5, 8.1: At least eight octets long PS */
763
wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
768
if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
769
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
776
/* Strip PKCS #1 header */
777
os_memmove(plain, pos, len);
784
197
int crypto_global_init(void)