s2n_cbc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdint.h>
  16. #include <sys/param.h>
  17. #include "crypto/s2n_hmac.h"
  18. #include "error/s2n_errno.h"
  19. #include "tls/s2n_connection.h"
  20. #include "tls/s2n_record.h"
  21. #include "utils/s2n_mem.h"
  22. #include "utils/s2n_safety.h"
  23. /* A TLS CBC record looks like ..
  24. *
  25. * [ Payload data ] [ HMAC ] [ Padding ] [ Padding length byte ]
  26. *
  27. * Each byte in the padding is expected to be set to the same value
  28. * as the padding length byte. So if the padding length byte is '2'
  29. * then the padding will be [ '2', '2' ] (there'll be three bytes
  30. * set to that value if you include the padding length byte).
  31. *
  32. * The goal of s2n_verify_cbc() is to verify that the padding and hmac
  33. * are correct, without leaking (via timing) how much padding there
  34. * actually is: as this is considered secret.
  35. *
  36. * In addition to our efforts here though, s2n also wraps any CBC
  37. * verification error (or record parsing error in general) with
  38. * a randomized delay of between 1ms and 10 seconds. See s2n_connection.c.
  39. * This amount of delay randomization is sufficient to increase the
  40. * complexity of attack for even a 1 microsecond timing leak (which
  41. * is quite large) by a factor of around 83 trillion.
  42. */
  43. int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted)
  44. {
  45. uint8_t mac_digest_size;
  46. POSIX_GUARD(s2n_hmac_digest_size(hmac->alg, &mac_digest_size));
  47. /* The record has to be at least big enough to contain the MAC,
  48. * plus the padding length byte */
  49. POSIX_ENSURE_GT(decrypted->size, mac_digest_size);
  50. int payload_and_padding_size = decrypted->size - mac_digest_size;
  51. /* Determine what the padding length is */
  52. uint8_t padding_length = decrypted->data[decrypted->size - 1];
  53. int payload_length = MAX(payload_and_padding_size - padding_length - 1, 0);
  54. /* Update the MAC */
  55. POSIX_GUARD(s2n_hmac_update(hmac, decrypted->data, payload_length));
  56. int currently_in_hash_block = hmac->currently_in_hash_block;
  57. /* Check the MAC */
  58. uint8_t check_digest[S2N_MAX_DIGEST_LEN];
  59. POSIX_ENSURE_LTE(mac_digest_size, sizeof(check_digest));
  60. POSIX_GUARD(s2n_hmac_digest_two_compression_rounds(hmac, check_digest, mac_digest_size));
  61. int mismatches = s2n_constant_time_equals(decrypted->data + payload_length, check_digest, mac_digest_size) ^ 1;
  62. /* Compute a MAC on the rest of the data so that we perform the same number of hash operations.
  63. * Include the partial hash block from the first MAC to ensure we use the same number of blocks.
  64. */
  65. POSIX_GUARD(s2n_hmac_reset(hmac));
  66. POSIX_GUARD(s2n_hmac_update(hmac, decrypted->data, currently_in_hash_block));
  67. POSIX_GUARD(s2n_hmac_update(hmac, decrypted->data + payload_length + mac_digest_size, decrypted->size - payload_length - mac_digest_size - 1));
  68. /* SSLv3 doesn't specify what the padding should actually be */
  69. if (conn->actual_protocol_version == S2N_SSLv3) {
  70. return 0 - mismatches;
  71. }
  72. /* Check the maximum amount that could theoretically be padding */
  73. uint32_t check = MIN(255, (payload_and_padding_size - 1));
  74. POSIX_ENSURE_GTE(check, padding_length);
  75. uint32_t cutoff = check - padding_length;
  76. for (size_t i = 0, j = decrypted->size - 1 - check; i < check && j < decrypted->size; i++, j++) {
  77. uint8_t mask = ~(0xff << ((i >= cutoff) * 8));
  78. mismatches |= (decrypted->data[j] ^ padding_length) & mask;
  79. }
  80. S2N_ERROR_IF(mismatches, S2N_ERR_CBC_VERIFY);
  81. return 0;
  82. }