You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Save some mallocs in computing the MAC for e_tls.c. We can reuse the HMAC_CTX that stores the key. The API is kind of unfortunate as, in principle, it should be possible to do an allocation-averse HMAC with a shared key on multiple threads at once (EVP_AEAD_CTX is normally logically const). At some point it may be worth rethinking those APIs somewhat. But these "stateful AEADs" are already stateful in their EVP_CIPHER_CTX, so this is fine. Each cipher was run individually to minimize the effect of other ciphers doing their mallocs. (Although the cost of a malloc is presumably going to depend a lot on the malloc implementation and what's happened before in the process, so take these numbers with a bucket of salt. They vary widely even with the same arguments.) Taking malloc out of seal/open also helps with the malloc tests. DTLS currently cannot distinguish a malloc failure (should be fatal) from a decryption failure (not fatal), so the malloc tests get stuck. But this doesn't completely get us there since tls_cbc.c mallocs. This also assumes EVP_CIPHER_CTX, EVP_MD_CTX, and HMAC_CTX are all clever about reusing their allocations when reset (which they are). Before: Did 1315000 AES-128-CBC-SHA1 (16 bytes) seal operations in 1000087us (1314885.6 ops/sec): 21.0 MB/s Did 181000 AES-128-CBC-SHA1 (1350 bytes) seal operations in 1004918us (180114.2 ops/sec): 243.2 MB/s Did 34000 AES-128-CBC-SHA1 (8192 bytes) seal operations in 1024250us (33195.0 ops/sec): 271.9 MB/s After: Did 1766000 AES-128-CBC-SHA1 (16 bytes) seal operations in 1000319us (1765436.8 ops/sec): 28.2 MB/s Did 187000 AES-128-CBC-SHA1 (1350 bytes) seal operations in 1004002us (186254.6 ops/sec): 251.4 MB/s Did 35000 AES-128-CBC-SHA1 (8192 bytes) seal operations in 1014885us (34486.7 ops/sec): 282.5 MB/s Before: Did 391000 DES-EDE3-CBC-SHA1 (16 bytes) seal operations in 1000038us (390985.1 ops/sec): 6.3 MB/s Did 16000 DES-EDE3-CBC-SHA1 (1350 bytes) seal operations in 1060226us (15091.1 ops/sec): 20.4 MB/s Did 2827 DES-EDE3-CBC-SHA1 (8192 bytes) seal operations in 1035971us (2728.8 ops/sec): 22.4 MB/s After: Did 444000 DES-EDE3-CBC-SHA1 (16 bytes) seal operations in 1001814us (443196.0 ops/sec): 7.1 MB/s Did 17000 DES-EDE3-CBC-SHA1 (1350 bytes) seal operations in 1042535us (16306.4 ops/sec): 22.0 MB/s Did 2590 DES-EDE3-CBC-SHA1 (8192 bytes) seal operations in 1012378us (2558.3 ops/sec): 21.0 MB/s Before: Did 1316000 AES-256-CBC-SHA1 (16 bytes) seal operations in 1000510us (1315329.2 ops/sec): 21.0 MB/s Did 157000 AES-256-CBC-SHA1 (1350 bytes) seal operations in 1002944us (156539.1 ops/sec): 211.3 MB/s Did 29000 AES-256-CBC-SHA1 (8192 bytes) seal operations in 1030284us (28147.6 ops/sec): 230.6 MB/s After: Did 1645000 AES-256-CBC-SHA1 (16 bytes) seal operations in 1000313us (1644485.3 ops/sec): 26.3 MB/s Did 162000 AES-256-CBC-SHA1 (1350 bytes) seal operations in 1003060us (161505.8 ops/sec): 218.0 MB/s Did 36000 AES-256-CBC-SHA1 (8192 bytes) seal operations in 1014819us (35474.3 ops/sec): 290.6 MB/s Before: Did 1435000 RC4-SHA1 (16 bytes) seal operations in 1000245us (1434648.5 ops/sec): 23.0 MB/s Did 207000 RC4-SHA1 (1350 bytes) seal operations in 1004675us (206036.8 ops/sec): 278.1 MB/s Did 38000 RC4-SHA1 (8192 bytes) seal operations in 1022712us (37156.1 ops/sec): 304.4 MB/s After: Did 1853000 RC4-SHA1 (16 bytes) seal operations in 1000433us (1852198.0 ops/sec): 29.6 MB/s Did 206000 RC4-SHA1 (1350 bytes) seal operations in 1002370us (205512.9 ops/sec): 277.4 MB/s Did 42000 RC4-SHA1 (8192 bytes) seal operations in 1024209us (41007.3 ops/sec): 335.9 MB/s Change-Id: I0edb89bddf146cf91a8e7a99c56b2278c8f38094 Reviewed-on: https://boringssl-review.googlesource.com/6751 Reviewed-by: Adam Langley <agl@google.com>
9 jaren geleden
Save some mallocs in computing the MAC for e_tls.c. We can reuse the HMAC_CTX that stores the key. The API is kind of unfortunate as, in principle, it should be possible to do an allocation-averse HMAC with a shared key on multiple threads at once (EVP_AEAD_CTX is normally logically const). At some point it may be worth rethinking those APIs somewhat. But these "stateful AEADs" are already stateful in their EVP_CIPHER_CTX, so this is fine. Each cipher was run individually to minimize the effect of other ciphers doing their mallocs. (Although the cost of a malloc is presumably going to depend a lot on the malloc implementation and what's happened before in the process, so take these numbers with a bucket of salt. They vary widely even with the same arguments.) Taking malloc out of seal/open also helps with the malloc tests. DTLS currently cannot distinguish a malloc failure (should be fatal) from a decryption failure (not fatal), so the malloc tests get stuck. But this doesn't completely get us there since tls_cbc.c mallocs. This also assumes EVP_CIPHER_CTX, EVP_MD_CTX, and HMAC_CTX are all clever about reusing their allocations when reset (which they are). Before: Did 1315000 AES-128-CBC-SHA1 (16 bytes) seal operations in 1000087us (1314885.6 ops/sec): 21.0 MB/s Did 181000 AES-128-CBC-SHA1 (1350 bytes) seal operations in 1004918us (180114.2 ops/sec): 243.2 MB/s Did 34000 AES-128-CBC-SHA1 (8192 bytes) seal operations in 1024250us (33195.0 ops/sec): 271.9 MB/s After: Did 1766000 AES-128-CBC-SHA1 (16 bytes) seal operations in 1000319us (1765436.8 ops/sec): 28.2 MB/s Did 187000 AES-128-CBC-SHA1 (1350 bytes) seal operations in 1004002us (186254.6 ops/sec): 251.4 MB/s Did 35000 AES-128-CBC-SHA1 (8192 bytes) seal operations in 1014885us (34486.7 ops/sec): 282.5 MB/s Before: Did 391000 DES-EDE3-CBC-SHA1 (16 bytes) seal operations in 1000038us (390985.1 ops/sec): 6.3 MB/s Did 16000 DES-EDE3-CBC-SHA1 (1350 bytes) seal operations in 1060226us (15091.1 ops/sec): 20.4 MB/s Did 2827 DES-EDE3-CBC-SHA1 (8192 bytes) seal operations in 1035971us (2728.8 ops/sec): 22.4 MB/s After: Did 444000 DES-EDE3-CBC-SHA1 (16 bytes) seal operations in 1001814us (443196.0 ops/sec): 7.1 MB/s Did 17000 DES-EDE3-CBC-SHA1 (1350 bytes) seal operations in 1042535us (16306.4 ops/sec): 22.0 MB/s Did 2590 DES-EDE3-CBC-SHA1 (8192 bytes) seal operations in 1012378us (2558.3 ops/sec): 21.0 MB/s Before: Did 1316000 AES-256-CBC-SHA1 (16 bytes) seal operations in 1000510us (1315329.2 ops/sec): 21.0 MB/s Did 157000 AES-256-CBC-SHA1 (1350 bytes) seal operations in 1002944us (156539.1 ops/sec): 211.3 MB/s Did 29000 AES-256-CBC-SHA1 (8192 bytes) seal operations in 1030284us (28147.6 ops/sec): 230.6 MB/s After: Did 1645000 AES-256-CBC-SHA1 (16 bytes) seal operations in 1000313us (1644485.3 ops/sec): 26.3 MB/s Did 162000 AES-256-CBC-SHA1 (1350 bytes) seal operations in 1003060us (161505.8 ops/sec): 218.0 MB/s Did 36000 AES-256-CBC-SHA1 (8192 bytes) seal operations in 1014819us (35474.3 ops/sec): 290.6 MB/s Before: Did 1435000 RC4-SHA1 (16 bytes) seal operations in 1000245us (1434648.5 ops/sec): 23.0 MB/s Did 207000 RC4-SHA1 (1350 bytes) seal operations in 1004675us (206036.8 ops/sec): 278.1 MB/s Did 38000 RC4-SHA1 (8192 bytes) seal operations in 1022712us (37156.1 ops/sec): 304.4 MB/s After: Did 1853000 RC4-SHA1 (16 bytes) seal operations in 1000433us (1852198.0 ops/sec): 29.6 MB/s Did 206000 RC4-SHA1 (1350 bytes) seal operations in 1002370us (205512.9 ops/sec): 277.4 MB/s Did 42000 RC4-SHA1 (8192 bytes) seal operations in 1024209us (41007.3 ops/sec): 335.9 MB/s Change-Id: I0edb89bddf146cf91a8e7a99c56b2278c8f38094 Reviewed-on: https://boringssl-review.googlesource.com/6751 Reviewed-by: Adam Langley <agl@google.com>
9 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <assert.h>
  15. #include <limits.h>
  16. #include <string.h>
  17. #include <openssl/aead.h>
  18. #include <openssl/cipher.h>
  19. #include <openssl/err.h>
  20. #include <openssl/hmac.h>
  21. #include <openssl/md5.h>
  22. #include <openssl/mem.h>
  23. #include <openssl/sha.h>
  24. #include <openssl/type_check.h>
  25. #include "../fipsmodule/cipher/internal.h"
  26. #include "../internal.h"
  27. #include "internal.h"
  28. typedef struct {
  29. EVP_CIPHER_CTX cipher_ctx;
  30. HMAC_CTX hmac_ctx;
  31. // mac_key is the portion of the key used for the MAC. It is retained
  32. // separately for the constant-time CBC code.
  33. uint8_t mac_key[EVP_MAX_MD_SIZE];
  34. uint8_t mac_key_len;
  35. // implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit
  36. // IV.
  37. char implicit_iv;
  38. } AEAD_TLS_CTX;
  39. OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE < 256, mac_key_len_fits_in_uint8_t);
  40. static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
  41. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  42. EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx);
  43. HMAC_CTX_cleanup(&tls_ctx->hmac_ctx);
  44. OPENSSL_free(tls_ctx);
  45. ctx->aead_state = NULL;
  46. }
  47. static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
  48. size_t tag_len, enum evp_aead_direction_t dir,
  49. const EVP_CIPHER *cipher, const EVP_MD *md,
  50. char implicit_iv) {
  51. if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
  52. tag_len != EVP_MD_size(md)) {
  53. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
  54. return 0;
  55. }
  56. if (key_len != EVP_AEAD_key_length(ctx->aead)) {
  57. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  58. return 0;
  59. }
  60. size_t mac_key_len = EVP_MD_size(md);
  61. size_t enc_key_len = EVP_CIPHER_key_length(cipher);
  62. assert(mac_key_len + enc_key_len +
  63. (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len);
  64. AEAD_TLS_CTX *tls_ctx = OPENSSL_malloc(sizeof(AEAD_TLS_CTX));
  65. if (tls_ctx == NULL) {
  66. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  67. return 0;
  68. }
  69. EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
  70. HMAC_CTX_init(&tls_ctx->hmac_ctx);
  71. assert(mac_key_len <= EVP_MAX_MD_SIZE);
  72. OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
  73. tls_ctx->mac_key_len = (uint8_t)mac_key_len;
  74. tls_ctx->implicit_iv = implicit_iv;
  75. ctx->aead_state = tls_ctx;
  76. if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
  77. implicit_iv ? &key[mac_key_len + enc_key_len] : NULL,
  78. dir == evp_aead_seal) ||
  79. !HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) {
  80. aead_tls_cleanup(ctx);
  81. ctx->aead_state = NULL;
  82. return 0;
  83. }
  84. EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
  85. return 1;
  86. }
  87. static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len,
  88. const size_t extra_in_len) {
  89. assert(extra_in_len == 0);
  90. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  91. const size_t hmac_len = HMAC_size(&tls_ctx->hmac_ctx);
  92. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) {
  93. // The NULL cipher.
  94. return hmac_len;
  95. }
  96. const size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
  97. // An overflow of |in_len + hmac_len| doesn't affect the result mod
  98. // |block_size|, provided that |block_size| is a smaller power of two.
  99. assert(block_size != 0 && (block_size & (block_size - 1)) == 0);
  100. const size_t pad_len = block_size - (in_len + hmac_len) % block_size;
  101. return hmac_len + pad_len;
  102. }
  103. static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
  104. uint8_t *out_tag, size_t *out_tag_len,
  105. const size_t max_out_tag_len,
  106. const uint8_t *nonce, const size_t nonce_len,
  107. const uint8_t *in, const size_t in_len,
  108. const uint8_t *extra_in,
  109. const size_t extra_in_len, const uint8_t *ad,
  110. const size_t ad_len) {
  111. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  112. if (!tls_ctx->cipher_ctx.encrypt) {
  113. // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
  114. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
  115. return 0;
  116. }
  117. if (in_len > INT_MAX) {
  118. // EVP_CIPHER takes int as input.
  119. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  120. return 0;
  121. }
  122. if (max_out_tag_len < aead_tls_tag_len(ctx, in_len, extra_in_len)) {
  123. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  124. return 0;
  125. }
  126. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  127. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  128. return 0;
  129. }
  130. if (ad_len != 13 - 2 /* length bytes */) {
  131. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
  132. return 0;
  133. }
  134. // To allow for CBC mode which changes cipher length, |ad| doesn't include the
  135. // length for legacy ciphers.
  136. uint8_t ad_extra[2];
  137. ad_extra[0] = (uint8_t)(in_len >> 8);
  138. ad_extra[1] = (uint8_t)(in_len & 0xff);
  139. // Compute the MAC. This must be first in case the operation is being done
  140. // in-place.
  141. uint8_t mac[EVP_MAX_MD_SIZE];
  142. unsigned mac_len;
  143. if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
  144. !HMAC_Update(&tls_ctx->hmac_ctx, ad, ad_len) ||
  145. !HMAC_Update(&tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) ||
  146. !HMAC_Update(&tls_ctx->hmac_ctx, in, in_len) ||
  147. !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len)) {
  148. return 0;
  149. }
  150. // Configure the explicit IV.
  151. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  152. !tls_ctx->implicit_iv &&
  153. !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
  154. return 0;
  155. }
  156. // Encrypt the input.
  157. int len;
  158. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
  159. return 0;
  160. }
  161. unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
  162. // Feed the MAC into the cipher in two steps. First complete the final partial
  163. // block from encrypting the input and split the result between |out| and
  164. // |out_tag|. Then feed the rest.
  165. const size_t early_mac_len =
  166. (block_size - (in_len % block_size) % block_size);
  167. if (early_mac_len != 0) {
  168. assert(len + block_size - early_mac_len == in_len);
  169. uint8_t buf[EVP_MAX_BLOCK_LENGTH];
  170. int buf_len;
  171. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, buf, &buf_len, mac,
  172. (int)early_mac_len)) {
  173. return 0;
  174. }
  175. assert(buf_len == (int)block_size);
  176. OPENSSL_memcpy(out + len, buf, block_size - early_mac_len);
  177. OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len);
  178. }
  179. size_t tag_len = early_mac_len;
  180. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
  181. mac + tag_len, mac_len - tag_len)) {
  182. return 0;
  183. }
  184. tag_len += len;
  185. if (block_size > 1) {
  186. assert(block_size <= 256);
  187. assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
  188. // Compute padding and feed that into the cipher.
  189. uint8_t padding[256];
  190. unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
  191. OPENSSL_memset(padding, padding_len - 1, padding_len);
  192. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
  193. padding, (int)padding_len)) {
  194. return 0;
  195. }
  196. tag_len += len;
  197. }
  198. if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out_tag + tag_len, &len)) {
  199. return 0;
  200. }
  201. assert(len == 0); // Padding is explicit.
  202. assert(tag_len == aead_tls_tag_len(ctx, in_len, extra_in_len));
  203. *out_tag_len = tag_len;
  204. return 1;
  205. }
  206. static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  207. size_t max_out_len, const uint8_t *nonce,
  208. size_t nonce_len, const uint8_t *in, size_t in_len,
  209. const uint8_t *ad, size_t ad_len) {
  210. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  211. if (tls_ctx->cipher_ctx.encrypt) {
  212. // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
  213. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
  214. return 0;
  215. }
  216. if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) {
  217. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  218. return 0;
  219. }
  220. if (max_out_len < in_len) {
  221. // This requires that the caller provide space for the MAC, even though it
  222. // will always be removed on return.
  223. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  224. return 0;
  225. }
  226. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  227. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  228. return 0;
  229. }
  230. if (ad_len != 13 - 2 /* length bytes */) {
  231. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
  232. return 0;
  233. }
  234. if (in_len > INT_MAX) {
  235. // EVP_CIPHER takes int as input.
  236. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  237. return 0;
  238. }
  239. // Configure the explicit IV.
  240. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  241. !tls_ctx->implicit_iv &&
  242. !EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
  243. return 0;
  244. }
  245. // Decrypt to get the plaintext + MAC + padding.
  246. size_t total = 0;
  247. int len;
  248. if (!EVP_DecryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
  249. return 0;
  250. }
  251. total += len;
  252. if (!EVP_DecryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
  253. return 0;
  254. }
  255. total += len;
  256. assert(total == in_len);
  257. // Remove CBC padding. Code from here on is timing-sensitive with respect to
  258. // |padding_ok| and |data_plus_mac_len| for CBC ciphers.
  259. size_t data_plus_mac_len;
  260. crypto_word_t padding_ok;
  261. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
  262. if (!EVP_tls_cbc_remove_padding(
  263. &padding_ok, &data_plus_mac_len, out, total,
  264. EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
  265. HMAC_size(&tls_ctx->hmac_ctx))) {
  266. // Publicly invalid. This can be rejected in non-constant time.
  267. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  268. return 0;
  269. }
  270. } else {
  271. padding_ok = CONSTTIME_TRUE_W;
  272. data_plus_mac_len = total;
  273. // |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
  274. // already been checked against the MAC size at the top of the function.
  275. assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx));
  276. }
  277. size_t data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx);
  278. // At this point, if the padding is valid, the first |data_plus_mac_len| bytes
  279. // after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is
  280. // still large enough to extract a MAC, but it will be irrelevant.
  281. // To allow for CBC mode which changes cipher length, |ad| doesn't include the
  282. // length for legacy ciphers.
  283. uint8_t ad_fixed[13];
  284. OPENSSL_memcpy(ad_fixed, ad, 11);
  285. ad_fixed[11] = (uint8_t)(data_len >> 8);
  286. ad_fixed[12] = (uint8_t)(data_len & 0xff);
  287. ad_len += 2;
  288. // Compute the MAC and extract the one in the record.
  289. uint8_t mac[EVP_MAX_MD_SIZE];
  290. size_t mac_len;
  291. uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
  292. uint8_t *record_mac;
  293. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  294. EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
  295. if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
  296. ad_fixed, out, data_plus_mac_len, total,
  297. tls_ctx->mac_key, tls_ctx->mac_key_len)) {
  298. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  299. return 0;
  300. }
  301. assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
  302. record_mac = record_mac_tmp;
  303. EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
  304. } else {
  305. // We should support the constant-time path for all CBC-mode ciphers
  306. // implemented.
  307. assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
  308. unsigned mac_len_u;
  309. if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
  310. !HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
  311. !HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) ||
  312. !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) {
  313. return 0;
  314. }
  315. mac_len = mac_len_u;
  316. assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
  317. record_mac = &out[data_len];
  318. }
  319. // Perform the MAC check and the padding check in constant-time. It should be
  320. // safe to simply perform the padding check first, but it would not be under a
  321. // different choice of MAC location on padding failure. See
  322. // EVP_tls_cbc_remove_padding.
  323. crypto_word_t good =
  324. constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0);
  325. good &= padding_ok;
  326. if (!good) {
  327. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  328. return 0;
  329. }
  330. // End of timing-sensitive code.
  331. *out_len = data_len;
  332. return 1;
  333. }
  334. static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  335. size_t key_len, size_t tag_len,
  336. enum evp_aead_direction_t dir) {
  337. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  338. EVP_sha1(), 0);
  339. }
  340. static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
  341. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  342. enum evp_aead_direction_t dir) {
  343. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  344. EVP_sha1(), 1);
  345. }
  346. static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
  347. const uint8_t *key, size_t key_len,
  348. size_t tag_len,
  349. enum evp_aead_direction_t dir) {
  350. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  351. EVP_sha256(), 0);
  352. }
  353. static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  354. size_t key_len, size_t tag_len,
  355. enum evp_aead_direction_t dir) {
  356. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  357. EVP_sha1(), 0);
  358. }
  359. static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
  360. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  361. enum evp_aead_direction_t dir) {
  362. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  363. EVP_sha1(), 1);
  364. }
  365. static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
  366. const uint8_t *key, size_t key_len,
  367. size_t tag_len,
  368. enum evp_aead_direction_t dir) {
  369. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  370. EVP_sha256(), 0);
  371. }
  372. static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
  373. const uint8_t *key, size_t key_len,
  374. size_t tag_len,
  375. enum evp_aead_direction_t dir) {
  376. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  377. EVP_sha384(), 0);
  378. }
  379. static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
  380. const uint8_t *key, size_t key_len,
  381. size_t tag_len,
  382. enum evp_aead_direction_t dir) {
  383. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
  384. EVP_sha1(), 0);
  385. }
  386. static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
  387. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  388. enum evp_aead_direction_t dir) {
  389. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
  390. EVP_sha1(), 1);
  391. }
  392. static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
  393. size_t *out_iv_len) {
  394. const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state;
  395. const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
  396. if (iv_len <= 1) {
  397. return 0;
  398. }
  399. *out_iv = tls_ctx->cipher_ctx.iv;
  400. *out_iv_len = iv_len;
  401. return 1;
  402. }
  403. static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  404. size_t key_len, size_t tag_len,
  405. enum evp_aead_direction_t dir) {
  406. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
  407. EVP_sha1(), 1 /* implicit iv */);
  408. }
  409. static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
  410. SHA_DIGEST_LENGTH + 16, // key len (SHA1 + AES128)
  411. 16, // nonce len (IV)
  412. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  413. SHA_DIGEST_LENGTH, // max tag length
  414. 0, // seal_scatter_supports_extra_in
  415. NULL, // init
  416. aead_aes_128_cbc_sha1_tls_init,
  417. aead_tls_cleanup,
  418. aead_tls_open,
  419. aead_tls_seal_scatter,
  420. NULL, // open_gather
  421. NULL, // get_iv
  422. aead_tls_tag_len,
  423. };
  424. static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
  425. SHA_DIGEST_LENGTH + 16 + 16, // key len (SHA1 + AES128 + IV)
  426. 0, // nonce len
  427. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  428. SHA_DIGEST_LENGTH, // max tag length
  429. 0, // seal_scatter_supports_extra_in
  430. NULL, // init
  431. aead_aes_128_cbc_sha1_tls_implicit_iv_init,
  432. aead_tls_cleanup,
  433. aead_tls_open,
  434. aead_tls_seal_scatter,
  435. NULL, // open_gather
  436. aead_tls_get_iv, // get_iv
  437. aead_tls_tag_len,
  438. };
  439. static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
  440. SHA256_DIGEST_LENGTH + 16, // key len (SHA256 + AES128)
  441. 16, // nonce len (IV)
  442. 16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
  443. SHA256_DIGEST_LENGTH, // max tag length
  444. 0, // seal_scatter_supports_extra_in
  445. NULL, // init
  446. aead_aes_128_cbc_sha256_tls_init,
  447. aead_tls_cleanup,
  448. aead_tls_open,
  449. aead_tls_seal_scatter,
  450. NULL, // open_gather
  451. NULL, // get_iv
  452. aead_tls_tag_len,
  453. };
  454. static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
  455. SHA_DIGEST_LENGTH + 32, // key len (SHA1 + AES256)
  456. 16, // nonce len (IV)
  457. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  458. SHA_DIGEST_LENGTH, // max tag length
  459. 0, // seal_scatter_supports_extra_in
  460. NULL, // init
  461. aead_aes_256_cbc_sha1_tls_init,
  462. aead_tls_cleanup,
  463. aead_tls_open,
  464. aead_tls_seal_scatter,
  465. NULL, // open_gather
  466. NULL, // get_iv
  467. aead_tls_tag_len,
  468. };
  469. static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
  470. SHA_DIGEST_LENGTH + 32 + 16, // key len (SHA1 + AES256 + IV)
  471. 0, // nonce len
  472. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  473. SHA_DIGEST_LENGTH, // max tag length
  474. 0, // seal_scatter_supports_extra_in
  475. NULL, // init
  476. aead_aes_256_cbc_sha1_tls_implicit_iv_init,
  477. aead_tls_cleanup,
  478. aead_tls_open,
  479. aead_tls_seal_scatter,
  480. NULL, // open_gather
  481. aead_tls_get_iv, // get_iv
  482. aead_tls_tag_len,
  483. };
  484. static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
  485. SHA256_DIGEST_LENGTH + 32, // key len (SHA256 + AES256)
  486. 16, // nonce len (IV)
  487. 16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
  488. SHA256_DIGEST_LENGTH, // max tag length
  489. 0, // seal_scatter_supports_extra_in
  490. NULL, // init
  491. aead_aes_256_cbc_sha256_tls_init,
  492. aead_tls_cleanup,
  493. aead_tls_open,
  494. aead_tls_seal_scatter,
  495. NULL, // open_gather
  496. NULL, // get_iv
  497. aead_tls_tag_len,
  498. };
  499. static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
  500. SHA384_DIGEST_LENGTH + 32, // key len (SHA384 + AES256)
  501. 16, // nonce len (IV)
  502. 16 + SHA384_DIGEST_LENGTH, // overhead (padding + SHA384)
  503. SHA384_DIGEST_LENGTH, // max tag length
  504. 0, // seal_scatter_supports_extra_in
  505. NULL, // init
  506. aead_aes_256_cbc_sha384_tls_init,
  507. aead_tls_cleanup,
  508. aead_tls_open,
  509. aead_tls_seal_scatter,
  510. NULL, // open_gather
  511. NULL, // get_iv
  512. aead_tls_tag_len,
  513. };
  514. static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
  515. SHA_DIGEST_LENGTH + 24, // key len (SHA1 + 3DES)
  516. 8, // nonce len (IV)
  517. 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  518. SHA_DIGEST_LENGTH, // max tag length
  519. 0, // seal_scatter_supports_extra_in
  520. NULL, // init
  521. aead_des_ede3_cbc_sha1_tls_init,
  522. aead_tls_cleanup,
  523. aead_tls_open,
  524. aead_tls_seal_scatter,
  525. NULL, // open_gather
  526. NULL, // get_iv
  527. aead_tls_tag_len,
  528. };
  529. static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
  530. SHA_DIGEST_LENGTH + 24 + 8, // key len (SHA1 + 3DES + IV)
  531. 0, // nonce len
  532. 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  533. SHA_DIGEST_LENGTH, // max tag length
  534. 0, // seal_scatter_supports_extra_in
  535. NULL, // init
  536. aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
  537. aead_tls_cleanup,
  538. aead_tls_open,
  539. aead_tls_seal_scatter,
  540. NULL, // open_gather
  541. aead_tls_get_iv, // get_iv
  542. aead_tls_tag_len,
  543. };
  544. static const EVP_AEAD aead_null_sha1_tls = {
  545. SHA_DIGEST_LENGTH, // key len
  546. 0, // nonce len
  547. SHA_DIGEST_LENGTH, // overhead (SHA1)
  548. SHA_DIGEST_LENGTH, // max tag length
  549. 0, // seal_scatter_supports_extra_in
  550. NULL, // init
  551. aead_null_sha1_tls_init,
  552. aead_tls_cleanup,
  553. aead_tls_open,
  554. aead_tls_seal_scatter,
  555. NULL, // open_gather
  556. NULL, // get_iv
  557. aead_tls_tag_len,
  558. };
  559. const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
  560. return &aead_aes_128_cbc_sha1_tls;
  561. }
  562. const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
  563. return &aead_aes_128_cbc_sha1_tls_implicit_iv;
  564. }
  565. const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
  566. return &aead_aes_128_cbc_sha256_tls;
  567. }
  568. const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
  569. return &aead_aes_256_cbc_sha1_tls;
  570. }
  571. const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
  572. return &aead_aes_256_cbc_sha1_tls_implicit_iv;
  573. }
  574. const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) {
  575. return &aead_aes_256_cbc_sha256_tls;
  576. }
  577. const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) {
  578. return &aead_aes_256_cbc_sha384_tls;
  579. }
  580. const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
  581. return &aead_des_ede3_cbc_sha1_tls;
  582. }
  583. const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
  584. return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
  585. }
  586. const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; }