Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Replace CBC_MAC_ROTATE_IN_PLACE with an N lg N rotation. Really the only thing we should be doing with these ciphers is hastening their demise, but it was the weekend and this seemed like fun. EVP_tls_cbc_copy_mac needs to rotate a buffer by a secret amount. (It extracts the MAC, but rotated.) We have two codepaths for this. If CBC_MAC_ROTATE_IN_PLACE is defined (always on), we make some assumptions abuot cache lines, play games with volatile, and hope that doesn't leak anything. Otherwise, we do O(N^2) work to constant-time select the rotation incidences. But we can do O(N lg N). Rotate by powers of two and constant-time select by the offset's bit positions. (Handwaivy lower-bound: an array position has N possible values, so, armed with only a constant-time select, we need O(lg N) work to resolve it. There's N array positions, so O(N lg N).) A microbenchmark of EVP_tls_cbc_copy_mac shows this is 27% faster than the old one, but still 32% slower than the in-place version. in-place: Did 15724000 CopyFromMAC operations in 20000744us (786170.8 ops/sec) N^2: Did 8443000 CopyFromMAC operations in 20001582us (422116.6 ops/sec) N lg N: Did 10718000 CopyFromMAC operations in 20000763us (535879.6 ops/sec) This results in the following the CBC ciphers. I measured AES-128-CBC-SHA1 and AES-256-CBC-SHA384 which are, respectively, the cipher where the other bits are the fastest and the cipher where N is largest. in-place: Did 2634000 AES-128-CBC-SHA1 (16 bytes) open operations in 10000739us (263380.5 ops/sec): 4.2 MB/s Did 1424000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10002782us (142360.4 ops/sec): 192.2 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002460us (53086.9 ops/sec): 434.9 MB/s N^2: Did 2529000 AES-128-CBC-SHA1 (16 bytes) open operations in 10001474us (252862.7 ops/sec): 4.0 MB/s Did 1392000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10006659us (139107.4 ops/sec): 187.8 MB/s Did 528000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10001276us (52793.3 ops/sec): 432.5 MB/s N lg N: Did 2531000 AES-128-CBC-SHA1 (16 bytes) open operations in 10003057us (253022.7 ops/sec): 4.0 MB/s Did 1390000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10003287us (138954.3 ops/sec): 187.6 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002448us (53087.0 ops/sec): 434.9 MB/s in-place: Did 1249000 AES-256-CBC-SHA384 (16 bytes) open operations in 10001767us (124877.9 ops/sec): 2.0 MB/s Did 879000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10009244us (87818.8 ops/sec): 118.6 MB/s Did 344000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10025897us (34311.1 ops/sec): 281.1 MB/s N^2: Did 1072000 AES-256-CBC-SHA384 (16 bytes) open operations in 10008090us (107113.3 ops/sec): 1.7 MB/s Did 780000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10007787us (77939.3 ops/sec): 105.2 MB/s Did 333000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10016332us (33245.7 ops/sec): 272.3 MB/s N lg N: Did 1168000 AES-256-CBC-SHA384 (16 bytes) open operations in 10007671us (116710.5 ops/sec): 1.9 MB/s Did 836000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10001536us (83587.2 ops/sec): 112.8 MB/s Did 339000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10018522us (33837.3 ops/sec): 277.2 MB/s TLS CBC performance isn't as important as it was before, and the costs aren't that high, so avoid making assumptions about cache lines. (If we care much about CBC open performance, we probably should get the malloc out of EVP_tls_cbc_digest_record at the end.) Change-Id: Ib8d8271be4b09e5635062cd3b039e1e96f0d9d3d Reviewed-on: https://boringssl-review.googlesource.com/11003 Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: Adam Langley <agl@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
vor 8 Jahren
Replace CBC_MAC_ROTATE_IN_PLACE with an N lg N rotation. Really the only thing we should be doing with these ciphers is hastening their demise, but it was the weekend and this seemed like fun. EVP_tls_cbc_copy_mac needs to rotate a buffer by a secret amount. (It extracts the MAC, but rotated.) We have two codepaths for this. If CBC_MAC_ROTATE_IN_PLACE is defined (always on), we make some assumptions abuot cache lines, play games with volatile, and hope that doesn't leak anything. Otherwise, we do O(N^2) work to constant-time select the rotation incidences. But we can do O(N lg N). Rotate by powers of two and constant-time select by the offset's bit positions. (Handwaivy lower-bound: an array position has N possible values, so, armed with only a constant-time select, we need O(lg N) work to resolve it. There's N array positions, so O(N lg N).) A microbenchmark of EVP_tls_cbc_copy_mac shows this is 27% faster than the old one, but still 32% slower than the in-place version. in-place: Did 15724000 CopyFromMAC operations in 20000744us (786170.8 ops/sec) N^2: Did 8443000 CopyFromMAC operations in 20001582us (422116.6 ops/sec) N lg N: Did 10718000 CopyFromMAC operations in 20000763us (535879.6 ops/sec) This results in the following the CBC ciphers. I measured AES-128-CBC-SHA1 and AES-256-CBC-SHA384 which are, respectively, the cipher where the other bits are the fastest and the cipher where N is largest. in-place: Did 2634000 AES-128-CBC-SHA1 (16 bytes) open operations in 10000739us (263380.5 ops/sec): 4.2 MB/s Did 1424000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10002782us (142360.4 ops/sec): 192.2 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002460us (53086.9 ops/sec): 434.9 MB/s N^2: Did 2529000 AES-128-CBC-SHA1 (16 bytes) open operations in 10001474us (252862.7 ops/sec): 4.0 MB/s Did 1392000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10006659us (139107.4 ops/sec): 187.8 MB/s Did 528000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10001276us (52793.3 ops/sec): 432.5 MB/s N lg N: Did 2531000 AES-128-CBC-SHA1 (16 bytes) open operations in 10003057us (253022.7 ops/sec): 4.0 MB/s Did 1390000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10003287us (138954.3 ops/sec): 187.6 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002448us (53087.0 ops/sec): 434.9 MB/s in-place: Did 1249000 AES-256-CBC-SHA384 (16 bytes) open operations in 10001767us (124877.9 ops/sec): 2.0 MB/s Did 879000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10009244us (87818.8 ops/sec): 118.6 MB/s Did 344000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10025897us (34311.1 ops/sec): 281.1 MB/s N^2: Did 1072000 AES-256-CBC-SHA384 (16 bytes) open operations in 10008090us (107113.3 ops/sec): 1.7 MB/s Did 780000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10007787us (77939.3 ops/sec): 105.2 MB/s Did 333000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10016332us (33245.7 ops/sec): 272.3 MB/s N lg N: Did 1168000 AES-256-CBC-SHA384 (16 bytes) open operations in 10007671us (116710.5 ops/sec): 1.9 MB/s Did 836000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10001536us (83587.2 ops/sec): 112.8 MB/s Did 339000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10018522us (33837.3 ops/sec): 277.2 MB/s TLS CBC performance isn't as important as it was before, and the costs aren't that high, so avoid making assumptions about cache lines. (If we care much about CBC open performance, we probably should get the malloc out of EVP_tls_cbc_digest_record at the end.) Change-Id: Ib8d8271be4b09e5635062cd3b039e1e96f0d9d3d Reviewed-on: https://boringssl-review.googlesource.com/11003 Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: Adam Langley <agl@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
vor 8 Jahren
Replace CBC_MAC_ROTATE_IN_PLACE with an N lg N rotation. Really the only thing we should be doing with these ciphers is hastening their demise, but it was the weekend and this seemed like fun. EVP_tls_cbc_copy_mac needs to rotate a buffer by a secret amount. (It extracts the MAC, but rotated.) We have two codepaths for this. If CBC_MAC_ROTATE_IN_PLACE is defined (always on), we make some assumptions abuot cache lines, play games with volatile, and hope that doesn't leak anything. Otherwise, we do O(N^2) work to constant-time select the rotation incidences. But we can do O(N lg N). Rotate by powers of two and constant-time select by the offset's bit positions. (Handwaivy lower-bound: an array position has N possible values, so, armed with only a constant-time select, we need O(lg N) work to resolve it. There's N array positions, so O(N lg N).) A microbenchmark of EVP_tls_cbc_copy_mac shows this is 27% faster than the old one, but still 32% slower than the in-place version. in-place: Did 15724000 CopyFromMAC operations in 20000744us (786170.8 ops/sec) N^2: Did 8443000 CopyFromMAC operations in 20001582us (422116.6 ops/sec) N lg N: Did 10718000 CopyFromMAC operations in 20000763us (535879.6 ops/sec) This results in the following the CBC ciphers. I measured AES-128-CBC-SHA1 and AES-256-CBC-SHA384 which are, respectively, the cipher where the other bits are the fastest and the cipher where N is largest. in-place: Did 2634000 AES-128-CBC-SHA1 (16 bytes) open operations in 10000739us (263380.5 ops/sec): 4.2 MB/s Did 1424000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10002782us (142360.4 ops/sec): 192.2 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002460us (53086.9 ops/sec): 434.9 MB/s N^2: Did 2529000 AES-128-CBC-SHA1 (16 bytes) open operations in 10001474us (252862.7 ops/sec): 4.0 MB/s Did 1392000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10006659us (139107.4 ops/sec): 187.8 MB/s Did 528000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10001276us (52793.3 ops/sec): 432.5 MB/s N lg N: Did 2531000 AES-128-CBC-SHA1 (16 bytes) open operations in 10003057us (253022.7 ops/sec): 4.0 MB/s Did 1390000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10003287us (138954.3 ops/sec): 187.6 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002448us (53087.0 ops/sec): 434.9 MB/s in-place: Did 1249000 AES-256-CBC-SHA384 (16 bytes) open operations in 10001767us (124877.9 ops/sec): 2.0 MB/s Did 879000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10009244us (87818.8 ops/sec): 118.6 MB/s Did 344000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10025897us (34311.1 ops/sec): 281.1 MB/s N^2: Did 1072000 AES-256-CBC-SHA384 (16 bytes) open operations in 10008090us (107113.3 ops/sec): 1.7 MB/s Did 780000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10007787us (77939.3 ops/sec): 105.2 MB/s Did 333000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10016332us (33245.7 ops/sec): 272.3 MB/s N lg N: Did 1168000 AES-256-CBC-SHA384 (16 bytes) open operations in 10007671us (116710.5 ops/sec): 1.9 MB/s Did 836000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10001536us (83587.2 ops/sec): 112.8 MB/s Did 339000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10018522us (33837.3 ops/sec): 277.2 MB/s TLS CBC performance isn't as important as it was before, and the costs aren't that high, so avoid making assumptions about cache lines. (If we care much about CBC open performance, we probably should get the malloc out of EVP_tls_cbc_digest_record at the end.) Change-Id: Ib8d8271be4b09e5635062cd3b039e1e96f0d9d3d Reviewed-on: https://boringssl-review.googlesource.com/11003 Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: Adam Langley <agl@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
vor 8 Jahren
Replace CBC_MAC_ROTATE_IN_PLACE with an N lg N rotation. Really the only thing we should be doing with these ciphers is hastening their demise, but it was the weekend and this seemed like fun. EVP_tls_cbc_copy_mac needs to rotate a buffer by a secret amount. (It extracts the MAC, but rotated.) We have two codepaths for this. If CBC_MAC_ROTATE_IN_PLACE is defined (always on), we make some assumptions abuot cache lines, play games with volatile, and hope that doesn't leak anything. Otherwise, we do O(N^2) work to constant-time select the rotation incidences. But we can do O(N lg N). Rotate by powers of two and constant-time select by the offset's bit positions. (Handwaivy lower-bound: an array position has N possible values, so, armed with only a constant-time select, we need O(lg N) work to resolve it. There's N array positions, so O(N lg N).) A microbenchmark of EVP_tls_cbc_copy_mac shows this is 27% faster than the old one, but still 32% slower than the in-place version. in-place: Did 15724000 CopyFromMAC operations in 20000744us (786170.8 ops/sec) N^2: Did 8443000 CopyFromMAC operations in 20001582us (422116.6 ops/sec) N lg N: Did 10718000 CopyFromMAC operations in 20000763us (535879.6 ops/sec) This results in the following the CBC ciphers. I measured AES-128-CBC-SHA1 and AES-256-CBC-SHA384 which are, respectively, the cipher where the other bits are the fastest and the cipher where N is largest. in-place: Did 2634000 AES-128-CBC-SHA1 (16 bytes) open operations in 10000739us (263380.5 ops/sec): 4.2 MB/s Did 1424000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10002782us (142360.4 ops/sec): 192.2 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002460us (53086.9 ops/sec): 434.9 MB/s N^2: Did 2529000 AES-128-CBC-SHA1 (16 bytes) open operations in 10001474us (252862.7 ops/sec): 4.0 MB/s Did 1392000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10006659us (139107.4 ops/sec): 187.8 MB/s Did 528000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10001276us (52793.3 ops/sec): 432.5 MB/s N lg N: Did 2531000 AES-128-CBC-SHA1 (16 bytes) open operations in 10003057us (253022.7 ops/sec): 4.0 MB/s Did 1390000 AES-128-CBC-SHA1 (1350 bytes) open operations in 10003287us (138954.3 ops/sec): 187.6 MB/s Did 531000 AES-128-CBC-SHA1 (8192 bytes) open operations in 10002448us (53087.0 ops/sec): 434.9 MB/s in-place: Did 1249000 AES-256-CBC-SHA384 (16 bytes) open operations in 10001767us (124877.9 ops/sec): 2.0 MB/s Did 879000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10009244us (87818.8 ops/sec): 118.6 MB/s Did 344000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10025897us (34311.1 ops/sec): 281.1 MB/s N^2: Did 1072000 AES-256-CBC-SHA384 (16 bytes) open operations in 10008090us (107113.3 ops/sec): 1.7 MB/s Did 780000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10007787us (77939.3 ops/sec): 105.2 MB/s Did 333000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10016332us (33245.7 ops/sec): 272.3 MB/s N lg N: Did 1168000 AES-256-CBC-SHA384 (16 bytes) open operations in 10007671us (116710.5 ops/sec): 1.9 MB/s Did 836000 AES-256-CBC-SHA384 (1350 bytes) open operations in 10001536us (83587.2 ops/sec): 112.8 MB/s Did 339000 AES-256-CBC-SHA384 (8192 bytes) open operations in 10018522us (33837.3 ops/sec): 277.2 MB/s TLS CBC performance isn't as important as it was before, and the costs aren't that high, so avoid making assumptions about cache lines. (If we care much about CBC open performance, we probably should get the malloc out of EVP_tls_cbc_digest_record at the end.) Change-Id: Ib8d8271be4b09e5635062cd3b039e1e96f0d9d3d Reviewed-on: https://boringssl-review.googlesource.com/11003 Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: Adam Langley <agl@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* ====================================================================
  2. * Copyright (c) 2012 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <assert.h>
  53. #include <string.h>
  54. #include <openssl/digest.h>
  55. #include <openssl/nid.h>
  56. #include <openssl/sha.h>
  57. #include "../internal.h"
  58. #include "internal.h"
  59. /* TODO(davidben): unsigned should be size_t. The various constant_time
  60. * functions need to be switched to size_t. */
  61. /* MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
  62. * field. (SHA-384/512 have 128-bit length.) */
  63. #define MAX_HASH_BIT_COUNT_BYTES 16
  64. /* MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
  65. * Currently SHA-384/512 has a 128-byte block size and that's the largest
  66. * supported by TLS.) */
  67. #define MAX_HASH_BLOCK_SIZE 128
  68. int EVP_tls_cbc_remove_padding(unsigned *out_padding_ok, unsigned *out_len,
  69. const uint8_t *in, unsigned in_len,
  70. unsigned block_size, unsigned mac_size) {
  71. unsigned padding_length, good, to_check, i;
  72. const unsigned overhead = 1 /* padding length byte */ + mac_size;
  73. /* These lengths are all public so we can test them in non-constant time. */
  74. if (overhead > in_len) {
  75. return 0;
  76. }
  77. padding_length = in[in_len - 1];
  78. good = constant_time_ge(in_len, overhead + padding_length);
  79. /* The padding consists of a length byte at the end of the record and
  80. * then that many bytes of padding, all with the same value as the
  81. * length byte. Thus, with the length byte included, there are i+1
  82. * bytes of padding.
  83. *
  84. * We can't check just |padding_length+1| bytes because that leaks
  85. * decrypted information. Therefore we always have to check the maximum
  86. * amount of padding possible. (Again, the length of the record is
  87. * public information so we can use it.) */
  88. to_check = 256; /* maximum amount of padding, inc length byte. */
  89. if (to_check > in_len) {
  90. to_check = in_len;
  91. }
  92. for (i = 0; i < to_check; i++) {
  93. uint8_t mask = constant_time_ge_8(padding_length, i);
  94. uint8_t b = in[in_len - 1 - i];
  95. /* The final |padding_length+1| bytes should all have the value
  96. * |padding_length|. Therefore the XOR should be zero. */
  97. good &= ~(mask & (padding_length ^ b));
  98. }
  99. /* If any of the final |padding_length+1| bytes had the wrong value,
  100. * one or more of the lower eight bits of |good| will be cleared. */
  101. good = constant_time_eq(0xff, good & 0xff);
  102. /* Always treat |padding_length| as zero on error. If, assuming block size of
  103. * 16, a padding of [<15 arbitrary bytes> 15] treated |padding_length| as 16
  104. * and returned -1, distinguishing good MAC and bad padding from bad MAC and
  105. * bad padding would give POODLE's padding oracle. */
  106. padding_length = good & (padding_length + 1);
  107. *out_len = in_len - padding_length;
  108. *out_padding_ok = good;
  109. return 1;
  110. }
  111. void EVP_tls_cbc_copy_mac(uint8_t *out, unsigned md_size,
  112. const uint8_t *in, unsigned in_len,
  113. unsigned orig_len) {
  114. uint8_t rotated_mac1[EVP_MAX_MD_SIZE], rotated_mac2[EVP_MAX_MD_SIZE];
  115. uint8_t *rotated_mac = rotated_mac1;
  116. uint8_t *rotated_mac_tmp = rotated_mac2;
  117. /* mac_end is the index of |in| just after the end of the MAC. */
  118. unsigned mac_end = in_len;
  119. unsigned mac_start = mac_end - md_size;
  120. /* scan_start contains the number of bytes that we can ignore because
  121. * the MAC's position can only vary by 255 bytes. */
  122. unsigned scan_start = 0;
  123. unsigned i, j;
  124. unsigned rotate_offset;
  125. assert(orig_len >= in_len);
  126. assert(in_len >= md_size);
  127. assert(md_size <= EVP_MAX_MD_SIZE);
  128. /* This information is public so it's safe to branch based on it. */
  129. if (orig_len > md_size + 255 + 1) {
  130. scan_start = orig_len - (md_size + 255 + 1);
  131. }
  132. /* Ideally the next statement would be:
  133. *
  134. * rotate_offset = (mac_start - scan_start) % md_size;
  135. *
  136. * However, division is not a constant-time operation (at least on Intel
  137. * chips). Thus we enumerate the possible values of md_size and handle each
  138. * separately. The value of |md_size| is public information (it's determined
  139. * by the cipher suite in the ServerHello) so our timing can vary based on
  140. * its value. */
  141. rotate_offset = mac_start - scan_start;
  142. /* rotate_offset can be, at most, 255 (bytes of padding) + 1 (padding length)
  143. * + md_size = 256 + 48 (since SHA-384 is the largest hash) = 304. */
  144. assert(rotate_offset <= 304);
  145. /* Below is an SMT-LIB2 verification that the Barrett reductions below are
  146. * correct within this range:
  147. *
  148. * (define-fun barrett (
  149. * (x (_ BitVec 32))
  150. * (mul (_ BitVec 32))
  151. * (shift (_ BitVec 32))
  152. * (divisor (_ BitVec 32)) ) (_ BitVec 32)
  153. * (let ((q (bvsub x (bvmul divisor (bvlshr (bvmul x mul) shift))) ))
  154. * (ite (bvuge q divisor)
  155. * (bvsub q divisor)
  156. * q)))
  157. *
  158. * (declare-fun x () (_ BitVec 32))
  159. *
  160. * (assert (or
  161. * (let (
  162. * (divisor (_ bv20 32))
  163. * (mul (_ bv25 32))
  164. * (shift (_ bv9 32))
  165. * (limit (_ bv853 32)))
  166. *
  167. * (and (bvule x limit) (not (= (bvurem x divisor)
  168. * (barrett x mul shift divisor)))))
  169. *
  170. * (let (
  171. * (divisor (_ bv48 32))
  172. * (mul (_ bv10 32))
  173. * (shift (_ bv9 32))
  174. * (limit (_ bv768 32)))
  175. *
  176. * (and (bvule x limit) (not (= (bvurem x divisor)
  177. * (barrett x mul shift divisor)))))
  178. * ))
  179. *
  180. * (check-sat)
  181. * (get-model)
  182. */
  183. if (md_size == 16) {
  184. rotate_offset &= 15;
  185. } else if (md_size == 20) {
  186. /* 1/20 is approximated as 25/512 and then Barrett reduction is used.
  187. * Analytically, this is correct for 0 <= rotate_offset <= 853. */
  188. unsigned q = (rotate_offset * 25) >> 9;
  189. rotate_offset -= q * 20;
  190. rotate_offset -=
  191. constant_time_select(constant_time_ge(rotate_offset, 20), 20, 0);
  192. } else if (md_size == 32) {
  193. rotate_offset &= 31;
  194. } else if (md_size == 48) {
  195. /* 1/48 is approximated as 10/512 and then Barrett reduction is used.
  196. * Analytically, this is correct for 0 <= rotate_offset <= 768. */
  197. unsigned q = (rotate_offset * 10) >> 9;
  198. rotate_offset -= q * 48;
  199. rotate_offset -=
  200. constant_time_select(constant_time_ge(rotate_offset, 48), 48, 0);
  201. } else {
  202. /* This should be impossible therefore this path doesn't run in constant
  203. * time. */
  204. assert(0);
  205. rotate_offset = rotate_offset % md_size;
  206. }
  207. memset(rotated_mac, 0, md_size);
  208. for (i = scan_start, j = 0; i < orig_len; i++) {
  209. uint8_t mac_started = constant_time_ge_8(i, mac_start);
  210. uint8_t mac_ended = constant_time_ge_8(i, mac_end);
  211. uint8_t b = in[i];
  212. rotated_mac[j++] |= b & mac_started & ~mac_ended;
  213. j &= constant_time_lt(j, md_size);
  214. }
  215. /* Now rotate the MAC. We rotate in log(md_size) steps, one for each bit
  216. * position. */
  217. for (unsigned offset = 1; offset < md_size;
  218. offset <<= 1, rotate_offset >>= 1) {
  219. /* Rotate by |offset| iff the corresponding bit is set in
  220. * |rotate_offset|, placing the result in |rotated_mac_tmp|. */
  221. const uint8_t skip_rotate = (rotate_offset & 1) - 1;
  222. for (i = 0, j = offset; i < md_size; i++, j++) {
  223. if (j >= md_size) {
  224. j -= md_size;
  225. }
  226. rotated_mac_tmp[i] =
  227. constant_time_select_8(skip_rotate, rotated_mac[i], rotated_mac[j]);
  228. }
  229. /* Swap pointers so |rotated_mac| contains the (possibly) rotated value.
  230. * Note the number of iterations and thus the identity of these pointers is
  231. * public information. */
  232. uint8_t *tmp = rotated_mac;
  233. rotated_mac = rotated_mac_tmp;
  234. rotated_mac_tmp = tmp;
  235. }
  236. memcpy(out, rotated_mac, md_size);
  237. }
  238. /* u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
  239. * big-endian order. The value of p is advanced by four. */
  240. #define u32toBE(n, p) \
  241. do { \
  242. *((p)++) = (uint8_t)((n) >> 24); \
  243. *((p)++) = (uint8_t)((n) >> 16); \
  244. *((p)++) = (uint8_t)((n) >> 8); \
  245. *((p)++) = (uint8_t)((n)); \
  246. } while (0)
  247. /* u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
  248. * big-endian order. The value of p is advanced by eight. */
  249. #define u64toBE(n, p) \
  250. do { \
  251. *((p)++) = (uint8_t)((n) >> 56); \
  252. *((p)++) = (uint8_t)((n) >> 48); \
  253. *((p)++) = (uint8_t)((n) >> 40); \
  254. *((p)++) = (uint8_t)((n) >> 32); \
  255. *((p)++) = (uint8_t)((n) >> 24); \
  256. *((p)++) = (uint8_t)((n) >> 16); \
  257. *((p)++) = (uint8_t)((n) >> 8); \
  258. *((p)++) = (uint8_t)((n)); \
  259. } while (0)
  260. /* These functions serialize the state of a hash and thus perform the standard
  261. * "final" operation without adding the padding and length that such a function
  262. * typically does. */
  263. static void tls1_sha1_final_raw(void *ctx, uint8_t *md_out) {
  264. SHA_CTX *sha1 = ctx;
  265. u32toBE(sha1->h[0], md_out);
  266. u32toBE(sha1->h[1], md_out);
  267. u32toBE(sha1->h[2], md_out);
  268. u32toBE(sha1->h[3], md_out);
  269. u32toBE(sha1->h[4], md_out);
  270. }
  271. #define LARGEST_DIGEST_CTX SHA_CTX
  272. static void tls1_sha256_final_raw(void *ctx, uint8_t *md_out) {
  273. SHA256_CTX *sha256 = ctx;
  274. unsigned i;
  275. for (i = 0; i < 8; i++) {
  276. u32toBE(sha256->h[i], md_out);
  277. }
  278. }
  279. #undef LARGEST_DIGEST_CTX
  280. #define LARGEST_DIGEST_CTX SHA256_CTX
  281. static void tls1_sha512_final_raw(void *ctx, uint8_t *md_out) {
  282. SHA512_CTX *sha512 = ctx;
  283. unsigned i;
  284. for (i = 0; i < 8; i++) {
  285. u64toBE(sha512->h[i], md_out);
  286. }
  287. }
  288. #undef LARGEST_DIGEST_CTX
  289. #define LARGEST_DIGEST_CTX SHA512_CTX
  290. int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
  291. switch (EVP_MD_type(md)) {
  292. case NID_sha1:
  293. case NID_sha256:
  294. case NID_sha384:
  295. return 1;
  296. default:
  297. return 0;
  298. }
  299. }
  300. int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
  301. size_t *md_out_size, const uint8_t header[13],
  302. const uint8_t *data, size_t data_plus_mac_size,
  303. size_t data_plus_mac_plus_padding_size,
  304. const uint8_t *mac_secret,
  305. unsigned mac_secret_length) {
  306. union {
  307. double align;
  308. uint8_t c[sizeof(LARGEST_DIGEST_CTX)];
  309. } md_state;
  310. void (*md_final_raw)(void *ctx, uint8_t *md_out);
  311. void (*md_transform)(void *ctx, const uint8_t *block);
  312. unsigned md_size, md_block_size = 64;
  313. unsigned len, max_mac_bytes, num_blocks, num_starting_blocks, k,
  314. mac_end_offset, c, index_a, index_b;
  315. unsigned int bits; /* at most 18 bits */
  316. uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
  317. /* hmac_pad is the masked HMAC key. */
  318. uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
  319. uint8_t first_block[MAX_HASH_BLOCK_SIZE];
  320. uint8_t mac_out[EVP_MAX_MD_SIZE];
  321. unsigned i, j, md_out_size_u;
  322. EVP_MD_CTX md_ctx;
  323. /* mdLengthSize is the number of bytes in the length field that terminates
  324. * the hash. */
  325. unsigned md_length_size = 8;
  326. /* This is a, hopefully redundant, check that allows us to forget about
  327. * many possible overflows later in this function. */
  328. assert(data_plus_mac_plus_padding_size < 1024 * 1024);
  329. switch (EVP_MD_type(md)) {
  330. case NID_sha1:
  331. SHA1_Init((SHA_CTX *)md_state.c);
  332. md_final_raw = tls1_sha1_final_raw;
  333. md_transform =
  334. (void (*)(void *ctx, const uint8_t *block))SHA1_Transform;
  335. md_size = 20;
  336. break;
  337. case NID_sha256:
  338. SHA256_Init((SHA256_CTX *)md_state.c);
  339. md_final_raw = tls1_sha256_final_raw;
  340. md_transform =
  341. (void (*)(void *ctx, const uint8_t *block))SHA256_Transform;
  342. md_size = 32;
  343. break;
  344. case NID_sha384:
  345. SHA384_Init((SHA512_CTX *)md_state.c);
  346. md_final_raw = tls1_sha512_final_raw;
  347. md_transform =
  348. (void (*)(void *ctx, const uint8_t *block))SHA512_Transform;
  349. md_size = 384 / 8;
  350. md_block_size = 128;
  351. md_length_size = 16;
  352. break;
  353. default:
  354. /* EVP_tls_cbc_record_digest_supported should have been called first to
  355. * check that the hash function is supported. */
  356. assert(0);
  357. *md_out_size = 0;
  358. return 0;
  359. }
  360. assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
  361. assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
  362. assert(md_size <= EVP_MAX_MD_SIZE);
  363. static const unsigned kHeaderLength = 13;
  364. /* kVarianceBlocks is the number of blocks of the hash that we have to
  365. * calculate in constant time because they could be altered by the
  366. * padding value.
  367. *
  368. * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
  369. * required to be minimal. Therefore we say that the final six blocks
  370. * can vary based on the padding. */
  371. static const unsigned kVarianceBlocks = 6;
  372. /* From now on we're dealing with the MAC, which conceptually has 13
  373. * bytes of `header' before the start of the data. */
  374. len = data_plus_mac_plus_padding_size + kHeaderLength;
  375. /* max_mac_bytes contains the maximum bytes of bytes in the MAC, including
  376. * |header|, assuming that there's no padding. */
  377. max_mac_bytes = len - md_size - 1;
  378. /* num_blocks is the maximum number of hash blocks. */
  379. num_blocks =
  380. (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
  381. /* In order to calculate the MAC in constant time we have to handle
  382. * the final blocks specially because the padding value could cause the
  383. * end to appear somewhere in the final |kVarianceBlocks| blocks and we
  384. * can't leak where. However, |num_starting_blocks| worth of data can
  385. * be hashed right away because no padding value can affect whether
  386. * they are plaintext. */
  387. num_starting_blocks = 0;
  388. /* k is the starting byte offset into the conceptual header||data where
  389. * we start processing. */
  390. k = 0;
  391. /* mac_end_offset is the index just past the end of the data to be
  392. * MACed. */
  393. mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
  394. /* c is the index of the 0x80 byte in the final hash block that
  395. * contains application data. */
  396. c = mac_end_offset % md_block_size;
  397. /* index_a is the hash block number that contains the 0x80 terminating
  398. * value. */
  399. index_a = mac_end_offset / md_block_size;
  400. /* index_b is the hash block number that contains the 64-bit hash
  401. * length, in bits. */
  402. index_b = (mac_end_offset + md_length_size) / md_block_size;
  403. /* bits is the hash-length in bits. It includes the additional hash
  404. * block for the masked HMAC key. */
  405. if (num_blocks > kVarianceBlocks) {
  406. num_starting_blocks = num_blocks - kVarianceBlocks;
  407. k = md_block_size * num_starting_blocks;
  408. }
  409. bits = 8 * mac_end_offset;
  410. /* Compute the initial HMAC block. */
  411. bits += 8 * md_block_size;
  412. memset(hmac_pad, 0, md_block_size);
  413. assert(mac_secret_length <= sizeof(hmac_pad));
  414. memcpy(hmac_pad, mac_secret, mac_secret_length);
  415. for (i = 0; i < md_block_size; i++) {
  416. hmac_pad[i] ^= 0x36;
  417. }
  418. md_transform(md_state.c, hmac_pad);
  419. memset(length_bytes, 0, md_length_size - 4);
  420. length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
  421. length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
  422. length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
  423. length_bytes[md_length_size - 1] = (uint8_t)bits;
  424. if (k > 0) {
  425. /* k is a multiple of md_block_size. */
  426. memcpy(first_block, header, 13);
  427. memcpy(first_block + 13, data, md_block_size - 13);
  428. md_transform(md_state.c, first_block);
  429. for (i = 1; i < k / md_block_size; i++) {
  430. md_transform(md_state.c, data + md_block_size * i - 13);
  431. }
  432. }
  433. memset(mac_out, 0, sizeof(mac_out));
  434. /* We now process the final hash blocks. For each block, we construct
  435. * it in constant time. If the |i==index_a| then we'll include the 0x80
  436. * bytes and zero pad etc. For each block we selectively copy it, in
  437. * constant time, to |mac_out|. */
  438. for (i = num_starting_blocks; i <= num_starting_blocks + kVarianceBlocks;
  439. i++) {
  440. uint8_t block[MAX_HASH_BLOCK_SIZE];
  441. uint8_t is_block_a = constant_time_eq_8(i, index_a);
  442. uint8_t is_block_b = constant_time_eq_8(i, index_b);
  443. for (j = 0; j < md_block_size; j++) {
  444. uint8_t b = 0, is_past_c, is_past_cp1;
  445. if (k < kHeaderLength) {
  446. b = header[k];
  447. } else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
  448. b = data[k - kHeaderLength];
  449. }
  450. k++;
  451. is_past_c = is_block_a & constant_time_ge_8(j, c);
  452. is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
  453. /* If this is the block containing the end of the
  454. * application data, and we are at the offset for the
  455. * 0x80 value, then overwrite b with 0x80. */
  456. b = constant_time_select_8(is_past_c, 0x80, b);
  457. /* If this the the block containing the end of the
  458. * application data and we're past the 0x80 value then
  459. * just write zero. */
  460. b = b & ~is_past_cp1;
  461. /* If this is index_b (the final block), but not
  462. * index_a (the end of the data), then the 64-bit
  463. * length didn't fit into index_a and we're having to
  464. * add an extra block of zeros. */
  465. b &= ~is_block_b | is_block_a;
  466. /* The final bytes of one of the blocks contains the
  467. * length. */
  468. if (j >= md_block_size - md_length_size) {
  469. /* If this is index_b, write a length byte. */
  470. b = constant_time_select_8(
  471. is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
  472. }
  473. block[j] = b;
  474. }
  475. md_transform(md_state.c, block);
  476. md_final_raw(md_state.c, block);
  477. /* If this is index_b, copy the hash value to |mac_out|. */
  478. for (j = 0; j < md_size; j++) {
  479. mac_out[j] |= block[j] & is_block_b;
  480. }
  481. }
  482. EVP_MD_CTX_init(&md_ctx);
  483. if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
  484. EVP_MD_CTX_cleanup(&md_ctx);
  485. return 0;
  486. }
  487. /* Complete the HMAC in the standard manner. */
  488. for (i = 0; i < md_block_size; i++) {
  489. hmac_pad[i] ^= 0x6a;
  490. }
  491. EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
  492. EVP_DigestUpdate(&md_ctx, mac_out, md_size);
  493. EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
  494. *md_out_size = md_out_size_u;
  495. EVP_MD_CTX_cleanup(&md_ctx);
  496. return 1;
  497. }