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.
 
 
 
 
 
 

682 lines
23 KiB

  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; }