421
static void dummy_wait(gnutls_session_t session, gnutls_datum_t* plaintext,
422
unsigned pad_failed, unsigned int pad, unsigned total, int ver)
424
/* this hack is only needed on CBC ciphers */
425
if (_gnutls_cipher_is_block (session->security_parameters.read_bulk_cipher_algorithm) == CIPHER_BLOCK)
427
uint8_t MAC[MAX_HASH_SIZE];
432
ret = mac_init (&td, session->security_parameters.read_mac_algorithm,
433
session->connection_state.read_mac_secret.data,
434
session->connection_state.read_mac_secret.size, ver);
439
/* force an additional hash compression function evaluation to prevent timing
440
* attacks that distinguish between wrong-mac + correct pad, from wrong-mac + incorrect pad.
442
if (pad_failed == 0 && pad > 0)
444
len = _gnutls_get_hash_block_len(session->security_parameters.read_mac_algorithm);
447
/* This is really specific to the current hash functions.
448
* It should be removed once a protocol fix is in place.
450
if ((pad+total) % len > len-9 && total % len <= len-9)
452
if (len < plaintext->size)
453
_gnutls_hmac (&td, plaintext->data, len);
455
_gnutls_hmac (&td, plaintext->data, plaintext->size);
460
mac_deinit (&td, MAC, ver);
421
464
/* Deciphers the ciphertext packet, and puts the result to compress_data, of compress_size.
422
465
* Returns the actual compressed packet size.
508
552
gnutls_assert ();
509
553
return GNUTLS_E_DECRYPTION_FAILED;
511
pad = ciphertext.data[ciphertext.size - 1] + 1; /* pad */
513
if ((int) pad > (int) ciphertext.size - hash_size)
517
("REC[%p]: Short record length %d > %d - %d (under attack?)\n",
518
session, pad, ciphertext.size, hash_size);
519
/* We do not fail here. We check below for the
520
* the pad_failed. If zero means success.
522
pad_failed = GNUTLS_E_DECRYPTION_FAILED;
525
length = ciphertext.size - hash_size - pad;
527
/* Check the pading bytes (TLS 1.x)
555
pad = ciphertext.data[ciphertext.size - 1]; /* pad */
557
/* Check the pading bytes (TLS 1.x).
558
* Note that we access all 256 bytes of ciphertext for padding check
559
* because there is a timing channel in that memory access (in certain CPUs).
529
561
if (ver >= GNUTLS_TLS1 && pad_failed == 0)
530
for (i = 2; i < pad; i++)
562
for (i = 2; i <= pad; i++)
532
if (ciphertext.data[ciphertext.size - i] !=
533
ciphertext.data[ciphertext.size - 1])
564
if (ciphertext.data[ciphertext.size - i] != pad)
534
565
pad_failed = GNUTLS_E_DECRYPTION_FAILED;
570
length = ciphertext.size - hash_size - pad - 1;
538
574
gnutls_assert ();
551
587
_gnutls_hmac (&td,
552
588
UINT64DATA (session->connection_state.
553
589
read_sequence_number), 8);
555
592
_gnutls_hmac (&td, &type, 1);
556
594
if (ver >= GNUTLS_TLS1)
558
596
_gnutls_hmac (&td, &major, 1);
559
598
_gnutls_hmac (&td, &minor, 1);
561
601
_gnutls_hmac (&td, &c_length, 2);
564
605
_gnutls_hmac (&td, ciphertext.data, length);
566
607
mac_deinit (&td, MAC, ver);
569
/* This one was introduced to avoid a timing attack against the TLS
575
610
/* HMAC was not the same.
577
if (memcmp (MAC, &ciphertext.data[length], hash_size) != 0)
612
if (memcmp (MAC, &ciphertext.data[length], hash_size) != 0 || pad_failed != 0)
614
gnutls_datum_t compressed = {compress_data, compress_size};
615
/* HMAC was not the same. */
616
dummy_wait(session, &compressed, pad_failed, pad, length+preamble_size, ver);
579
618
gnutls_assert ();
580
619
return GNUTLS_E_DECRYPTION_FAILED;
583
/* copy the decrypted stuff to compress_data.
622
/* copy the decrypted stuff to compressed_data.
585
624
if (compress_size < length)