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 година
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 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 = (block_size - (in_len % block_size)) % block_size;
  166. if (early_mac_len != 0) {
  167. assert(len + block_size - early_mac_len == in_len);
  168. uint8_t buf[EVP_MAX_BLOCK_LENGTH];
  169. int buf_len;
  170. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, buf, &buf_len, mac,
  171. (int)early_mac_len)) {
  172. return 0;
  173. }
  174. assert(buf_len == (int)block_size);
  175. OPENSSL_memcpy(out + len, buf, block_size - early_mac_len);
  176. OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len);
  177. }
  178. size_t tag_len = early_mac_len;
  179. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
  180. mac + tag_len, mac_len - tag_len)) {
  181. return 0;
  182. }
  183. tag_len += len;
  184. if (block_size > 1) {
  185. assert(block_size <= 256);
  186. assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
  187. // Compute padding and feed that into the cipher.
  188. uint8_t padding[256];
  189. unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
  190. OPENSSL_memset(padding, padding_len - 1, padding_len);
  191. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
  192. padding, (int)padding_len)) {
  193. return 0;
  194. }
  195. tag_len += len;
  196. }
  197. if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out_tag + tag_len, &len)) {
  198. return 0;
  199. }
  200. assert(len == 0); // Padding is explicit.
  201. assert(tag_len == aead_tls_tag_len(ctx, in_len, extra_in_len));
  202. *out_tag_len = tag_len;
  203. return 1;
  204. }
  205. static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  206. size_t max_out_len, const uint8_t *nonce,
  207. size_t nonce_len, const uint8_t *in, size_t in_len,
  208. const uint8_t *ad, size_t ad_len) {
  209. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  210. if (tls_ctx->cipher_ctx.encrypt) {
  211. // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
  212. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
  213. return 0;
  214. }
  215. if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) {
  216. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  217. return 0;
  218. }
  219. if (max_out_len < in_len) {
  220. // This requires that the caller provide space for the MAC, even though it
  221. // will always be removed on return.
  222. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  223. return 0;
  224. }
  225. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  226. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  227. return 0;
  228. }
  229. if (ad_len != 13 - 2 /* length bytes */) {
  230. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
  231. return 0;
  232. }
  233. if (in_len > INT_MAX) {
  234. // EVP_CIPHER takes int as input.
  235. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  236. return 0;
  237. }
  238. // Configure the explicit IV.
  239. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  240. !tls_ctx->implicit_iv &&
  241. !EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
  242. return 0;
  243. }
  244. // Decrypt to get the plaintext + MAC + padding.
  245. size_t total = 0;
  246. int len;
  247. if (!EVP_DecryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
  248. return 0;
  249. }
  250. total += len;
  251. if (!EVP_DecryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
  252. return 0;
  253. }
  254. total += len;
  255. assert(total == in_len);
  256. // Remove CBC padding. Code from here on is timing-sensitive with respect to
  257. // |padding_ok| and |data_plus_mac_len| for CBC ciphers.
  258. size_t data_plus_mac_len;
  259. crypto_word_t padding_ok;
  260. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
  261. if (!EVP_tls_cbc_remove_padding(
  262. &padding_ok, &data_plus_mac_len, out, total,
  263. EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
  264. HMAC_size(&tls_ctx->hmac_ctx))) {
  265. // Publicly invalid. This can be rejected in non-constant time.
  266. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  267. return 0;
  268. }
  269. } else {
  270. padding_ok = CONSTTIME_TRUE_W;
  271. data_plus_mac_len = total;
  272. // |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
  273. // already been checked against the MAC size at the top of the function.
  274. assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx));
  275. }
  276. size_t data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx);
  277. // At this point, if the padding is valid, the first |data_plus_mac_len| bytes
  278. // after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is
  279. // still large enough to extract a MAC, but it will be irrelevant.
  280. // To allow for CBC mode which changes cipher length, |ad| doesn't include the
  281. // length for legacy ciphers.
  282. uint8_t ad_fixed[13];
  283. OPENSSL_memcpy(ad_fixed, ad, 11);
  284. ad_fixed[11] = (uint8_t)(data_len >> 8);
  285. ad_fixed[12] = (uint8_t)(data_len & 0xff);
  286. ad_len += 2;
  287. // Compute the MAC and extract the one in the record.
  288. uint8_t mac[EVP_MAX_MD_SIZE];
  289. size_t mac_len;
  290. uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
  291. uint8_t *record_mac;
  292. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  293. EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
  294. if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
  295. ad_fixed, out, data_plus_mac_len, total,
  296. tls_ctx->mac_key, tls_ctx->mac_key_len)) {
  297. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  298. return 0;
  299. }
  300. assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
  301. record_mac = record_mac_tmp;
  302. EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
  303. } else {
  304. // We should support the constant-time path for all CBC-mode ciphers
  305. // implemented.
  306. assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
  307. unsigned mac_len_u;
  308. if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
  309. !HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
  310. !HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) ||
  311. !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) {
  312. return 0;
  313. }
  314. mac_len = mac_len_u;
  315. assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
  316. record_mac = &out[data_len];
  317. }
  318. // Perform the MAC check and the padding check in constant-time. It should be
  319. // safe to simply perform the padding check first, but it would not be under a
  320. // different choice of MAC location on padding failure. See
  321. // EVP_tls_cbc_remove_padding.
  322. crypto_word_t good =
  323. constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0);
  324. good &= padding_ok;
  325. if (!good) {
  326. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  327. return 0;
  328. }
  329. // End of timing-sensitive code.
  330. *out_len = data_len;
  331. return 1;
  332. }
  333. static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  334. size_t key_len, size_t tag_len,
  335. enum evp_aead_direction_t dir) {
  336. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  337. EVP_sha1(), 0);
  338. }
  339. static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
  340. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  341. enum evp_aead_direction_t dir) {
  342. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  343. EVP_sha1(), 1);
  344. }
  345. static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
  346. const uint8_t *key, size_t key_len,
  347. size_t tag_len,
  348. enum evp_aead_direction_t dir) {
  349. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  350. EVP_sha256(), 0);
  351. }
  352. static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  353. size_t key_len, size_t tag_len,
  354. enum evp_aead_direction_t dir) {
  355. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  356. EVP_sha1(), 0);
  357. }
  358. static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
  359. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  360. enum evp_aead_direction_t dir) {
  361. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  362. EVP_sha1(), 1);
  363. }
  364. static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
  365. const uint8_t *key, size_t key_len,
  366. size_t tag_len,
  367. enum evp_aead_direction_t dir) {
  368. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  369. EVP_sha256(), 0);
  370. }
  371. static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
  372. const uint8_t *key, size_t key_len,
  373. size_t tag_len,
  374. enum evp_aead_direction_t dir) {
  375. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  376. EVP_sha384(), 0);
  377. }
  378. static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
  379. const uint8_t *key, size_t key_len,
  380. size_t tag_len,
  381. enum evp_aead_direction_t dir) {
  382. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
  383. EVP_sha1(), 0);
  384. }
  385. static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
  386. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  387. enum evp_aead_direction_t dir) {
  388. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
  389. EVP_sha1(), 1);
  390. }
  391. static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
  392. size_t *out_iv_len) {
  393. const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state;
  394. const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
  395. if (iv_len <= 1) {
  396. return 0;
  397. }
  398. *out_iv = tls_ctx->cipher_ctx.iv;
  399. *out_iv_len = iv_len;
  400. return 1;
  401. }
  402. static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  403. size_t key_len, size_t tag_len,
  404. enum evp_aead_direction_t dir) {
  405. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
  406. EVP_sha1(), 1 /* implicit iv */);
  407. }
  408. static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
  409. SHA_DIGEST_LENGTH + 16, // key len (SHA1 + AES128)
  410. 16, // nonce len (IV)
  411. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  412. SHA_DIGEST_LENGTH, // max tag length
  413. 0, // seal_scatter_supports_extra_in
  414. NULL, // init
  415. aead_aes_128_cbc_sha1_tls_init,
  416. aead_tls_cleanup,
  417. aead_tls_open,
  418. aead_tls_seal_scatter,
  419. NULL, // open_gather
  420. NULL, // get_iv
  421. aead_tls_tag_len,
  422. };
  423. static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
  424. SHA_DIGEST_LENGTH + 16 + 16, // key len (SHA1 + AES128 + IV)
  425. 0, // nonce len
  426. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  427. SHA_DIGEST_LENGTH, // max tag length
  428. 0, // seal_scatter_supports_extra_in
  429. NULL, // init
  430. aead_aes_128_cbc_sha1_tls_implicit_iv_init,
  431. aead_tls_cleanup,
  432. aead_tls_open,
  433. aead_tls_seal_scatter,
  434. NULL, // open_gather
  435. aead_tls_get_iv, // get_iv
  436. aead_tls_tag_len,
  437. };
  438. static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
  439. SHA256_DIGEST_LENGTH + 16, // key len (SHA256 + AES128)
  440. 16, // nonce len (IV)
  441. 16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
  442. SHA256_DIGEST_LENGTH, // max tag length
  443. 0, // seal_scatter_supports_extra_in
  444. NULL, // init
  445. aead_aes_128_cbc_sha256_tls_init,
  446. aead_tls_cleanup,
  447. aead_tls_open,
  448. aead_tls_seal_scatter,
  449. NULL, // open_gather
  450. NULL, // get_iv
  451. aead_tls_tag_len,
  452. };
  453. static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
  454. SHA_DIGEST_LENGTH + 32, // key len (SHA1 + AES256)
  455. 16, // nonce len (IV)
  456. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  457. SHA_DIGEST_LENGTH, // max tag length
  458. 0, // seal_scatter_supports_extra_in
  459. NULL, // init
  460. aead_aes_256_cbc_sha1_tls_init,
  461. aead_tls_cleanup,
  462. aead_tls_open,
  463. aead_tls_seal_scatter,
  464. NULL, // open_gather
  465. NULL, // get_iv
  466. aead_tls_tag_len,
  467. };
  468. static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
  469. SHA_DIGEST_LENGTH + 32 + 16, // key len (SHA1 + AES256 + IV)
  470. 0, // nonce len
  471. 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  472. SHA_DIGEST_LENGTH, // max tag length
  473. 0, // seal_scatter_supports_extra_in
  474. NULL, // init
  475. aead_aes_256_cbc_sha1_tls_implicit_iv_init,
  476. aead_tls_cleanup,
  477. aead_tls_open,
  478. aead_tls_seal_scatter,
  479. NULL, // open_gather
  480. aead_tls_get_iv, // get_iv
  481. aead_tls_tag_len,
  482. };
  483. static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
  484. SHA256_DIGEST_LENGTH + 32, // key len (SHA256 + AES256)
  485. 16, // nonce len (IV)
  486. 16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
  487. SHA256_DIGEST_LENGTH, // max tag length
  488. 0, // seal_scatter_supports_extra_in
  489. NULL, // init
  490. aead_aes_256_cbc_sha256_tls_init,
  491. aead_tls_cleanup,
  492. aead_tls_open,
  493. aead_tls_seal_scatter,
  494. NULL, // open_gather
  495. NULL, // get_iv
  496. aead_tls_tag_len,
  497. };
  498. static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
  499. SHA384_DIGEST_LENGTH + 32, // key len (SHA384 + AES256)
  500. 16, // nonce len (IV)
  501. 16 + SHA384_DIGEST_LENGTH, // overhead (padding + SHA384)
  502. SHA384_DIGEST_LENGTH, // max tag length
  503. 0, // seal_scatter_supports_extra_in
  504. NULL, // init
  505. aead_aes_256_cbc_sha384_tls_init,
  506. aead_tls_cleanup,
  507. aead_tls_open,
  508. aead_tls_seal_scatter,
  509. NULL, // open_gather
  510. NULL, // get_iv
  511. aead_tls_tag_len,
  512. };
  513. static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
  514. SHA_DIGEST_LENGTH + 24, // key len (SHA1 + 3DES)
  515. 8, // nonce len (IV)
  516. 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  517. SHA_DIGEST_LENGTH, // max tag length
  518. 0, // seal_scatter_supports_extra_in
  519. NULL, // init
  520. aead_des_ede3_cbc_sha1_tls_init,
  521. aead_tls_cleanup,
  522. aead_tls_open,
  523. aead_tls_seal_scatter,
  524. NULL, // open_gather
  525. NULL, // get_iv
  526. aead_tls_tag_len,
  527. };
  528. static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
  529. SHA_DIGEST_LENGTH + 24 + 8, // key len (SHA1 + 3DES + IV)
  530. 0, // nonce len
  531. 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
  532. SHA_DIGEST_LENGTH, // max tag length
  533. 0, // seal_scatter_supports_extra_in
  534. NULL, // init
  535. aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
  536. aead_tls_cleanup,
  537. aead_tls_open,
  538. aead_tls_seal_scatter,
  539. NULL, // open_gather
  540. aead_tls_get_iv, // get_iv
  541. aead_tls_tag_len,
  542. };
  543. static const EVP_AEAD aead_null_sha1_tls = {
  544. SHA_DIGEST_LENGTH, // key len
  545. 0, // nonce len
  546. SHA_DIGEST_LENGTH, // overhead (SHA1)
  547. SHA_DIGEST_LENGTH, // max tag length
  548. 0, // seal_scatter_supports_extra_in
  549. NULL, // init
  550. aead_null_sha1_tls_init,
  551. aead_tls_cleanup,
  552. aead_tls_open,
  553. aead_tls_seal_scatter,
  554. NULL, // open_gather
  555. NULL, // get_iv
  556. aead_tls_tag_len,
  557. };
  558. const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
  559. return &aead_aes_128_cbc_sha1_tls;
  560. }
  561. const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
  562. return &aead_aes_128_cbc_sha1_tls_implicit_iv;
  563. }
  564. const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
  565. return &aead_aes_128_cbc_sha256_tls;
  566. }
  567. const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
  568. return &aead_aes_256_cbc_sha1_tls;
  569. }
  570. const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
  571. return &aead_aes_256_cbc_sha1_tls_implicit_iv;
  572. }
  573. const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) {
  574. return &aead_aes_256_cbc_sha256_tls;
  575. }
  576. const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) {
  577. return &aead_aes_256_cbc_sha384_tls;
  578. }
  579. const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
  580. return &aead_des_ede3_cbc_sha1_tls;
  581. }
  582. const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
  583. return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
  584. }
  585. const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; }