/* A TLS CBC record looks like .. * * [ Payload data ] [ HMAC ] [ Padding ] [ Padding length byte ] * * Each byte in the padding is expected to be set to the same value * as the padding length byte. So if the padding length byte is '2' * then the padding will be [ '2', '2' ] (there'll be three bytes * set to that value if you include the padding length byte). * * The goal of s2n_verify_cbc() is to verify that the padding and hmac * are correct, without leaking (via timing) how much padding there * actually is: as this is considered secret. * * In addition to our efforts here though, s2n also wraps any CBC * verification error (or record parsing error in general) with * a randomized delay of between 1ms and 10 seconds. See s2n_connection.c. * This amount of delay randomization is sufficient to increase the * complexity of attack for even a 1 microsecond timing leak (which * is quite large) by a factor of around 83 trillion. */ int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted) { struct s2n_hmac_state copy; int mac_digest_size = s2n_hmac_digest_size(hmac->alg); /* The record has to be at least big enough to contain the MAC, * plus the padding length byte */ gt_check(decrypted->size, mac_digest_size); int payload_and_padding_size = decrypted->size - mac_digest_size; /* Determine what the padding length is */ uint8_t padding_length = decrypted->data[decrypted->size - 1]; int payload_length = MAX(payload_and_padding_size - padding_length - 1, 0); /* Update the MAC */ GUARD(s2n_hmac_update(hmac, decrypted->data, payload_length)); GUARD(s2n_hmac_copy(©, hmac)); /* Check the MAC */ uint8_t check_digest[S2N_MAX_DIGEST_LEN]; lte_check(mac_digest_size, sizeof(check_digest)); GUARD(s2n_hmac_digest_two_compression_rounds(hmac, check_digest, mac_digest_size)); int mismatches = s2n_constant_time_equals(decrypted->data + payload_length, check_digest, mac_digest_size) ^ 1; /* Compute a MAC on the rest of the data so that we perform the same number of hash operations */ GUARD(s2n_hmac_update(©, decrypted->data + payload_length + mac_digest_size, decrypted->size - payload_length - mac_digest_size - 1)); /* SSLv3 doesn't specify what the padding should actually be */ if (conn->actual_protocol_version == S2N_SSLv3) { return 0 - mismatches; } /* Check the maximum amount that could theoritically be padding */ int check = MIN(255, (payload_and_padding_size - 1)); int cutoff = check - padding_length; for (int i = 0, j = decrypted->size - 1 - check; i < check && j < decrypted->size; i++, j++) { uint8_t mask = ~(0xff << ((i >= cutoff) * 8)); mismatches |= (decrypted->data[j] ^ padding_length) & mask; } if (mismatches) { S2N_ERROR(S2N_ERR_CBC_VERIFY); } return 0; }
int s2n_client_finished_recv(struct s2n_connection *conn) { uint8_t *our_version; our_version = conn->handshake.client_finished; uint8_t *their_version = s2n_stuffer_raw_read(&conn->handshake.io, S2N_TLS_FINISHED_LEN); notnull_check(their_version); if (!s2n_constant_time_equals(our_version, their_version, S2N_TLS_FINISHED_LEN)) { S2N_ERROR(S2N_ERR_BAD_MESSAGE); } conn->handshake.next_state = SERVER_CHANGE_CIPHER_SPEC; return 0; }
int s2n_hmac_digest_verify(const void *a, uint32_t alen, const void *b, uint32_t blen) { return 0 - (!s2n_constant_time_equals(a, b, alen) | !!(alen - blen)); }