diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-26 10:33:23 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-26 10:33:23 +0100 |
commit | d4681c7293da6aeb901101b5bc239229f4963926 (patch) | |
tree | 119847e7b90981996c72362fcb8cd5f21c9dce15 | |
parent | ca7cdd4b0350d47445f9c02adc161ebcb41b4c20 (diff) | |
download | busybox-w32-d4681c7293da6aeb901101b5bc239229f4963926.tar.gz busybox-w32-d4681c7293da6aeb901101b5bc239229f4963926.tar.bz2 busybox-w32-d4681c7293da6aeb901101b5bc239229f4963926.zip |
tls: simplify hmac_begin()
function old new delta
hmac_begin 196 158 -38
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/tls.c | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/networking/tls.c b/networking/tls.c index e76a78585..002983273 100644 --- a/networking/tls.c +++ b/networking/tls.c | |||
@@ -224,7 +224,7 @@ enum { | |||
224 | OUTBUF_PFX = 8 + AES_BLOCK_SIZE, /* header + IV */ | 224 | OUTBUF_PFX = 8 + AES_BLOCK_SIZE, /* header + IV */ |
225 | OUTBUF_SFX = TLS_MAX_MAC_SIZE + TLS_MAX_CRYPTBLOCK_SIZE, /* MAC + padding */ | 225 | OUTBUF_SFX = TLS_MAX_MAC_SIZE + TLS_MAX_CRYPTBLOCK_SIZE, /* MAC + padding */ |
226 | 226 | ||
227 | // RFC 5246 | 227 | // RFC 5246: |
228 | // | 6.2.1. Fragmentation | 228 | // | 6.2.1. Fragmentation |
229 | // | The record layer fragments information blocks into TLSPlaintext | 229 | // | The record layer fragments information blocks into TLSPlaintext |
230 | // | records carrying data in chunks of 2^14 bytes or less. Client | 230 | // | records carrying data in chunks of 2^14 bytes or less. Client |
@@ -405,7 +405,7 @@ static void hash_handshake(tls_state_t *tls, const char *fmt, const void *buffer | |||
405 | #endif | 405 | #endif |
406 | } | 406 | } |
407 | 407 | ||
408 | // RFC 2104 | 408 | // RFC 2104: |
409 | // HMAC(key, text) based on a hash H (say, sha256) is: | 409 | // HMAC(key, text) based on a hash H (say, sha256) is: |
410 | // ipad = [0x36 x INSIZE] | 410 | // ipad = [0x36 x INSIZE] |
411 | // opad = [0x5c x INSIZE] | 411 | // opad = [0x5c x INSIZE] |
@@ -448,7 +448,7 @@ static void hmac_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size, | |||
448 | { | 448 | { |
449 | uint8_t key_xor_ipad[SHA_INSIZE]; | 449 | uint8_t key_xor_ipad[SHA_INSIZE]; |
450 | uint8_t key_xor_opad[SHA_INSIZE]; | 450 | uint8_t key_xor_opad[SHA_INSIZE]; |
451 | uint8_t tempkey[SHA1_OUTSIZE < SHA256_OUTSIZE ? SHA256_OUTSIZE : SHA1_OUTSIZE]; | 451 | // uint8_t tempkey[SHA1_OUTSIZE < SHA256_OUTSIZE ? SHA256_OUTSIZE : SHA1_OUTSIZE]; |
452 | unsigned i; | 452 | unsigned i; |
453 | 453 | ||
454 | // "The authentication key can be of any length up to INSIZE, the | 454 | // "The authentication key can be of any length up to INSIZE, the |
@@ -456,10 +456,18 @@ static void hmac_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size, | |||
456 | // than INSIZE bytes will first hash the key using H and then use the | 456 | // than INSIZE bytes will first hash the key using H and then use the |
457 | // resultant OUTSIZE byte string as the actual key to HMAC." | 457 | // resultant OUTSIZE byte string as the actual key to HMAC." |
458 | if (key_size > SHA_INSIZE) { | 458 | if (key_size > SHA_INSIZE) { |
459 | md5sha_ctx_t ctx; | 459 | bb_error_msg_and_die("HMAC key>64"); //does not happen (yet?) |
460 | begin(&ctx); | 460 | // md5sha_ctx_t ctx; |
461 | md5sha_hash(&ctx, key, key_size); | 461 | // begin(&ctx); |
462 | key_size = sha_end(&ctx, tempkey); | 462 | // md5sha_hash(&ctx, key, key_size); |
463 | // key_size = sha_end(&ctx, tempkey); | ||
464 | // //key = tempkey; - right? RIGHT? why does it work without this? | ||
465 | // // because SHA_INSIZE is 64, but hmac() is always called with | ||
466 | // // key_size = tls->MAC_size = SHA1/256_OUTSIZE (20 or 32), | ||
467 | // // and prf_hmac_sha256() -> hmac_sha256() key sizes are: | ||
468 | // // - RSA_PREMASTER_SIZE is 48 | ||
469 | // // - CURVE25519_KEYSIZE is 32 | ||
470 | // // - master_secret[] is 48 | ||
463 | } | 471 | } |
464 | 472 | ||
465 | for (i = 0; i < key_size; i++) { | 473 | for (i = 0; i < key_size; i++) { |
@@ -519,8 +527,9 @@ static unsigned hmac_sha256(/*tls_state_t *tls,*/ uint8_t *out, uint8_t *key, un | |||
519 | // document and in TLS documents published prior to this document when | 527 | // document and in TLS documents published prior to this document when |
520 | // TLS 1.2 is negotiated. | 528 | // TLS 1.2 is negotiated. |
521 | // ^^^^^^^^^^^^^ IMPORTANT! | 529 | // ^^^^^^^^^^^^^ IMPORTANT! |
522 | // PRF uses sha256 regardless of cipher (at least for all ciphers | 530 | // PRF uses sha256 regardless of cipher for all ciphers |
523 | // defined by RFC5246). It's not sha1 for AES_128_CBC_SHA! | 531 | // defined by RFC 5246. It's not sha1 for AES_128_CBC_SHA! |
532 | // However, for _SHA384 ciphers, it's sha384. See RFC 5288,5289. | ||
524 | //... | 533 | //... |
525 | // P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + | 534 | // P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + |
526 | // HMAC_hash(secret, A(2) + seed) + | 535 | // HMAC_hash(secret, A(2) + seed) + |
@@ -542,6 +551,12 @@ static unsigned hmac_sha256(/*tls_state_t *tls,*/ uint8_t *out, uint8_t *key, un | |||
542 | // PRF(secret, label, seed) = P_<hash>(secret, label + seed) | 551 | // PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
543 | // | 552 | // |
544 | // The label is an ASCII string. | 553 | // The label is an ASCII string. |
554 | // | ||
555 | // RFC 5288: | ||
556 | // For cipher suites ending with _SHA256, the PRF is the TLS PRF | ||
557 | // with SHA-256 as the hash function. | ||
558 | // For cipher suites ending with _SHA384, the PRF is the TLS PRF | ||
559 | // with SHA-384 as the hash function. | ||
545 | static void prf_hmac_sha256(/*tls_state_t *tls,*/ | 560 | static void prf_hmac_sha256(/*tls_state_t *tls,*/ |
546 | uint8_t *outbuf, unsigned outbuf_size, | 561 | uint8_t *outbuf, unsigned outbuf_size, |
547 | uint8_t *secret, unsigned secret_size, | 562 | uint8_t *secret, unsigned secret_size, |
@@ -675,7 +690,7 @@ static void xwrite_encrypted_and_hmac_signed(tls_state_t *tls, unsigned size, un | |||
675 | 690 | ||
676 | size += tls->MAC_size; | 691 | size += tls->MAC_size; |
677 | 692 | ||
678 | // RFC 5246 | 693 | // RFC 5246: |
679 | // 6.2.3.1. Null or Standard Stream Cipher | 694 | // 6.2.3.1. Null or Standard Stream Cipher |
680 | // | 695 | // |
681 | // Stream ciphers (including BulkCipherAlgorithm.null; see Appendix A.6) | 696 | // Stream ciphers (including BulkCipherAlgorithm.null; see Appendix A.6) |
@@ -1467,6 +1482,7 @@ static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) | |||
1467 | // 0xC0,0x28, // TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet | 1482 | // 0xC0,0x28, // TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet |
1468 | 0xC0,0x2B, // 6 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 - ok: wget https://is.gd/ | 1483 | 0xC0,0x2B, // 6 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 - ok: wget https://is.gd/ |
1469 | // 0xC0,0x2C, // TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 - wget https://is.gd/: "TLS error from peer (alert code 20): bad MAC" | 1484 | // 0xC0,0x2C, // TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 - wget https://is.gd/: "TLS error from peer (alert code 20): bad MAC" |
1485 | //TODO: GCM_SHA384 ciphers can be supported, only need sha384-based PRF? | ||
1470 | 0xC0,0x2F, // 7 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-GCM-SHA256 | 1486 | 0xC0,0x2F, // 7 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-GCM-SHA256 |
1471 | // 0xC0,0x30, // TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 - openssl s_server ... -cipher ECDHE-RSA-AES256-GCM-SHA384: "decryption failed or bad record mac" | 1487 | // 0xC0,0x30, // TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 - openssl s_server ... -cipher ECDHE-RSA-AES256-GCM-SHA384: "decryption failed or bad record mac" |
1472 | //possibly these too: | 1488 | //possibly these too: |
@@ -1489,7 +1505,7 @@ static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) | |||
1489 | 0x00,0x0a, //extension_type: "supported_groups" | 1505 | 0x00,0x0a, //extension_type: "supported_groups" |
1490 | 0x00,0x04, //ext len | 1506 | 0x00,0x04, //ext len |
1491 | 0x00,0x02, //list len | 1507 | 0x00,0x02, //list len |
1492 | 0x00,0x1d, //curve_x25519 (rfc7748) | 1508 | 0x00,0x1d, //curve_x25519 (RFC 7748) |
1493 | //0x00,0x17, //curve_secp256r1 | 1509 | //0x00,0x17, //curve_secp256r1 |
1494 | //0x00,0x18, //curve_secp384r1 | 1510 | //0x00,0x18, //curve_secp384r1 |
1495 | //0x00,0x19, //curve_secp521r1 | 1511 | //0x00,0x19, //curve_secp521r1 |