e_tls.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 "../crypto/internal.h"
  26. #include "internal.h"
  27. typedef struct {
  28. EVP_CIPHER_CTX cipher_ctx;
  29. HMAC_CTX hmac_ctx;
  30. /* mac_key is the portion of the key used for the MAC. It is retained
  31. * separately for the constant-time CBC code. */
  32. uint8_t mac_key[EVP_MAX_MD_SIZE];
  33. uint8_t mac_key_len;
  34. /* implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit
  35. * IV. */
  36. char implicit_iv;
  37. } AEAD_TLS_CTX;
  38. OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE < 256, mac_key_len_fits_in_uint8_t);
  39. static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
  40. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  41. EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx);
  42. HMAC_CTX_cleanup(&tls_ctx->hmac_ctx);
  43. OPENSSL_cleanse(&tls_ctx->mac_key, sizeof(tls_ctx->mac_key));
  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. /* Although EVP_rc4() is a variable-length cipher, the default key size is
  65. * correct for TLS. */
  66. AEAD_TLS_CTX *tls_ctx = OPENSSL_malloc(sizeof(AEAD_TLS_CTX));
  67. if (tls_ctx == NULL) {
  68. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  69. return 0;
  70. }
  71. EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
  72. HMAC_CTX_init(&tls_ctx->hmac_ctx);
  73. assert(mac_key_len <= EVP_MAX_MD_SIZE);
  74. memcpy(tls_ctx->mac_key, key, mac_key_len);
  75. tls_ctx->mac_key_len = (uint8_t)mac_key_len;
  76. tls_ctx->implicit_iv = implicit_iv;
  77. ctx->aead_state = tls_ctx;
  78. if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
  79. implicit_iv ? &key[mac_key_len + enc_key_len] : NULL,
  80. dir == evp_aead_seal) ||
  81. !HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) {
  82. aead_tls_cleanup(ctx);
  83. ctx->aead_state = NULL;
  84. return 0;
  85. }
  86. EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
  87. return 1;
  88. }
  89. static int aead_tls_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  90. size_t *out_len, size_t max_out_len,
  91. const uint8_t *nonce, size_t nonce_len,
  92. const uint8_t *in, size_t in_len,
  93. const uint8_t *ad, size_t ad_len) {
  94. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  95. size_t total = 0;
  96. if (!tls_ctx->cipher_ctx.encrypt) {
  97. /* Unlike a normal AEAD, a TLS AEAD may only be used in one direction. */
  98. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
  99. return 0;
  100. }
  101. if (in_len + EVP_AEAD_max_overhead(ctx->aead) < in_len ||
  102. in_len > INT_MAX) {
  103. /* EVP_CIPHER takes int as input. */
  104. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  105. return 0;
  106. }
  107. if (max_out_len < in_len + EVP_AEAD_max_overhead(ctx->aead)) {
  108. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  109. return 0;
  110. }
  111. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  112. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  113. return 0;
  114. }
  115. if (ad_len != 13 - 2 /* length bytes */) {
  116. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
  117. return 0;
  118. }
  119. /* To allow for CBC mode which changes cipher length, |ad| doesn't include the
  120. * length for legacy ciphers. */
  121. uint8_t ad_extra[2];
  122. ad_extra[0] = (uint8_t)(in_len >> 8);
  123. ad_extra[1] = (uint8_t)(in_len & 0xff);
  124. /* Compute the MAC. This must be first in case the operation is being done
  125. * in-place. */
  126. uint8_t mac[EVP_MAX_MD_SIZE];
  127. unsigned mac_len;
  128. if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
  129. !HMAC_Update(&tls_ctx->hmac_ctx, ad, ad_len) ||
  130. !HMAC_Update(&tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) ||
  131. !HMAC_Update(&tls_ctx->hmac_ctx, in, in_len) ||
  132. !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len)) {
  133. return 0;
  134. }
  135. /* Configure the explicit IV. */
  136. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  137. !tls_ctx->implicit_iv &&
  138. !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
  139. return 0;
  140. }
  141. /* Encrypt the input. */
  142. int len;
  143. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in,
  144. (int)in_len)) {
  145. return 0;
  146. }
  147. total = len;
  148. /* Feed the MAC into the cipher. */
  149. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out + total, &len, mac,
  150. (int)mac_len)) {
  151. return 0;
  152. }
  153. total += len;
  154. unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
  155. if (block_size > 1) {
  156. assert(block_size <= 256);
  157. assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
  158. /* Compute padding and feed that into the cipher. */
  159. uint8_t padding[256];
  160. unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
  161. memset(padding, padding_len - 1, padding_len);
  162. if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out + total, &len, padding,
  163. (int)padding_len)) {
  164. return 0;
  165. }
  166. total += len;
  167. }
  168. if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
  169. return 0;
  170. }
  171. total += len;
  172. *out_len = total;
  173. return 1;
  174. }
  175. static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  176. size_t *out_len, size_t max_out_len,
  177. const uint8_t *nonce, size_t nonce_len,
  178. const uint8_t *in, size_t in_len,
  179. const uint8_t *ad, size_t ad_len) {
  180. AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
  181. if (tls_ctx->cipher_ctx.encrypt) {
  182. /* Unlike a normal AEAD, a TLS AEAD may only be used in one direction. */
  183. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
  184. return 0;
  185. }
  186. if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) {
  187. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  188. return 0;
  189. }
  190. if (max_out_len < in_len) {
  191. /* This requires that the caller provide space for the MAC, even though it
  192. * will always be removed on return. */
  193. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  194. return 0;
  195. }
  196. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  197. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  198. return 0;
  199. }
  200. if (ad_len != 13 - 2 /* length bytes */) {
  201. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
  202. return 0;
  203. }
  204. if (in_len > INT_MAX) {
  205. /* EVP_CIPHER takes int as input. */
  206. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  207. return 0;
  208. }
  209. /* Configure the explicit IV. */
  210. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  211. !tls_ctx->implicit_iv &&
  212. !EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
  213. return 0;
  214. }
  215. /* Decrypt to get the plaintext + MAC + padding. */
  216. size_t total = 0;
  217. int len;
  218. if (!EVP_DecryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
  219. return 0;
  220. }
  221. total += len;
  222. if (!EVP_DecryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
  223. return 0;
  224. }
  225. total += len;
  226. assert(total == in_len);
  227. /* Remove CBC padding. Code from here on is timing-sensitive with respect to
  228. * |padding_ok| and |data_plus_mac_len| for CBC ciphers. */
  229. int padding_ok;
  230. unsigned data_plus_mac_len, data_len;
  231. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
  232. padding_ok = EVP_tls_cbc_remove_padding(
  233. &data_plus_mac_len, out, total,
  234. EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
  235. (unsigned)HMAC_size(&tls_ctx->hmac_ctx));
  236. /* Publicly invalid. This can be rejected in non-constant time. */
  237. if (padding_ok == 0) {
  238. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  239. return 0;
  240. }
  241. } else {
  242. padding_ok = 1;
  243. data_plus_mac_len = total;
  244. /* |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
  245. * already been checked against the MAC size at the top of the function. */
  246. assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx));
  247. }
  248. data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx);
  249. /* At this point, |padding_ok| is 1 or -1. If 1, the padding is valid and the
  250. * first |data_plus_mac_size| bytes after |out| are the plaintext and
  251. * MAC. Either way, |data_plus_mac_size| is large enough to extract a MAC. */
  252. /* To allow for CBC mode which changes cipher length, |ad| doesn't include the
  253. * length for legacy ciphers. */
  254. uint8_t ad_fixed[13];
  255. memcpy(ad_fixed, ad, 11);
  256. ad_fixed[11] = (uint8_t)(data_len >> 8);
  257. ad_fixed[12] = (uint8_t)(data_len & 0xff);
  258. ad_len += 2;
  259. /* Compute the MAC and extract the one in the record. */
  260. uint8_t mac[EVP_MAX_MD_SIZE];
  261. size_t mac_len;
  262. uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
  263. uint8_t *record_mac;
  264. if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
  265. EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
  266. if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
  267. ad_fixed, out, data_plus_mac_len, total,
  268. tls_ctx->mac_key, tls_ctx->mac_key_len)) {
  269. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  270. return 0;
  271. }
  272. assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
  273. record_mac = record_mac_tmp;
  274. EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
  275. } else {
  276. /* We should support the constant-time path for all CBC-mode ciphers
  277. * implemented. */
  278. assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
  279. unsigned mac_len_u;
  280. if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
  281. !HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
  282. !HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) ||
  283. !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) {
  284. return 0;
  285. }
  286. mac_len = mac_len_u;
  287. assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
  288. record_mac = &out[data_len];
  289. }
  290. /* Perform the MAC check and the padding check in constant-time. It should be
  291. * safe to simply perform the padding check first, but it would not be under a
  292. * different choice of MAC location on padding failure. See
  293. * EVP_tls_cbc_remove_padding. */
  294. unsigned good = constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len),
  295. 0);
  296. good &= constant_time_eq_int(padding_ok, 1);
  297. if (!good) {
  298. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  299. return 0;
  300. }
  301. /* End of timing-sensitive code. */
  302. *out_len = data_len;
  303. return 1;
  304. }
  305. static int aead_rc4_md5_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  306. size_t key_len, size_t tag_len,
  307. enum evp_aead_direction_t dir) {
  308. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_rc4(), EVP_md5(),
  309. 0);
  310. }
  311. static int aead_rc4_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  312. size_t key_len, size_t tag_len,
  313. enum evp_aead_direction_t dir) {
  314. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_rc4(), EVP_sha1(),
  315. 0);
  316. }
  317. static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  318. size_t key_len, size_t tag_len,
  319. enum evp_aead_direction_t dir) {
  320. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  321. EVP_sha1(), 0);
  322. }
  323. static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
  324. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  325. enum evp_aead_direction_t dir) {
  326. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  327. EVP_sha1(), 1);
  328. }
  329. static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
  330. const uint8_t *key, size_t key_len,
  331. size_t tag_len,
  332. enum evp_aead_direction_t dir) {
  333. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
  334. EVP_sha256(), 0);
  335. }
  336. static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  337. size_t key_len, size_t tag_len,
  338. enum evp_aead_direction_t dir) {
  339. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  340. EVP_sha1(), 0);
  341. }
  342. static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
  343. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  344. enum evp_aead_direction_t dir) {
  345. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  346. EVP_sha1(), 1);
  347. }
  348. static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
  349. const uint8_t *key, size_t key_len,
  350. size_t tag_len,
  351. enum evp_aead_direction_t dir) {
  352. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  353. EVP_sha256(), 0);
  354. }
  355. static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
  356. const uint8_t *key, size_t key_len,
  357. size_t tag_len,
  358. enum evp_aead_direction_t dir) {
  359. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
  360. EVP_sha384(), 0);
  361. }
  362. static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
  363. const uint8_t *key, size_t key_len,
  364. size_t tag_len,
  365. enum evp_aead_direction_t dir) {
  366. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
  367. EVP_sha1(), 0);
  368. }
  369. static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
  370. EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
  371. enum evp_aead_direction_t dir) {
  372. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
  373. EVP_sha1(), 1);
  374. }
  375. static int aead_rc4_tls_get_rc4_state(const EVP_AEAD_CTX *ctx,
  376. const RC4_KEY **out_key) {
  377. const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state;
  378. if (EVP_CIPHER_CTX_cipher(&tls_ctx->cipher_ctx) != EVP_rc4()) {
  379. return 0;
  380. }
  381. *out_key = (const RC4_KEY*) tls_ctx->cipher_ctx.cipher_data;
  382. return 1;
  383. }
  384. static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
  385. size_t *out_iv_len) {
  386. const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state;
  387. const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
  388. if (iv_len <= 1) {
  389. return 0;
  390. }
  391. *out_iv = tls_ctx->cipher_ctx.iv;
  392. *out_iv_len = iv_len;
  393. return 1;
  394. }
  395. static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  396. size_t key_len, size_t tag_len,
  397. enum evp_aead_direction_t dir) {
  398. return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
  399. EVP_sha1(), 1 /* implicit iv */);
  400. }
  401. static const EVP_AEAD aead_rc4_md5_tls = {
  402. MD5_DIGEST_LENGTH + 16, /* key len (MD5 + RC4) */
  403. 0, /* nonce len */
  404. MD5_DIGEST_LENGTH, /* overhead */
  405. MD5_DIGEST_LENGTH, /* max tag length */
  406. NULL, /* init */
  407. aead_rc4_md5_tls_init,
  408. aead_tls_cleanup,
  409. aead_tls_seal,
  410. aead_tls_open,
  411. aead_rc4_tls_get_rc4_state, /* get_rc4_state */
  412. NULL, /* get_iv */
  413. };
  414. static const EVP_AEAD aead_rc4_sha1_tls = {
  415. SHA_DIGEST_LENGTH + 16, /* key len (SHA1 + RC4) */
  416. 0, /* nonce len */
  417. SHA_DIGEST_LENGTH, /* overhead */
  418. SHA_DIGEST_LENGTH, /* max tag length */
  419. NULL, /* init */
  420. aead_rc4_sha1_tls_init,
  421. aead_tls_cleanup,
  422. aead_tls_seal,
  423. aead_tls_open,
  424. aead_rc4_tls_get_rc4_state, /* get_rc4_state */
  425. NULL, /* get_iv */
  426. };
  427. static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
  428. SHA_DIGEST_LENGTH + 16, /* key len (SHA1 + AES128) */
  429. 16, /* nonce len (IV) */
  430. 16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
  431. SHA_DIGEST_LENGTH, /* max tag length */
  432. NULL, /* init */
  433. aead_aes_128_cbc_sha1_tls_init,
  434. aead_tls_cleanup,
  435. aead_tls_seal,
  436. aead_tls_open,
  437. NULL, /* get_rc4_state */
  438. NULL, /* get_iv */
  439. };
  440. static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
  441. SHA_DIGEST_LENGTH + 16 + 16, /* key len (SHA1 + AES128 + IV) */
  442. 0, /* nonce len */
  443. 16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
  444. SHA_DIGEST_LENGTH, /* max tag length */
  445. NULL, /* init */
  446. aead_aes_128_cbc_sha1_tls_implicit_iv_init,
  447. aead_tls_cleanup,
  448. aead_tls_seal,
  449. aead_tls_open,
  450. NULL, /* get_rc4_state */
  451. aead_tls_get_iv, /* get_iv */
  452. };
  453. static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
  454. SHA256_DIGEST_LENGTH + 16, /* key len (SHA256 + AES128) */
  455. 16, /* nonce len (IV) */
  456. 16 + SHA256_DIGEST_LENGTH, /* overhead (padding + SHA256) */
  457. SHA256_DIGEST_LENGTH, /* max tag length */
  458. NULL, /* init */
  459. aead_aes_128_cbc_sha256_tls_init,
  460. aead_tls_cleanup,
  461. aead_tls_seal,
  462. aead_tls_open,
  463. NULL, /* get_rc4_state */
  464. NULL, /* get_iv */
  465. };
  466. static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
  467. SHA_DIGEST_LENGTH + 32, /* key len (SHA1 + AES256) */
  468. 16, /* nonce len (IV) */
  469. 16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
  470. SHA_DIGEST_LENGTH, /* max tag length */
  471. NULL, /* init */
  472. aead_aes_256_cbc_sha1_tls_init,
  473. aead_tls_cleanup,
  474. aead_tls_seal,
  475. aead_tls_open,
  476. NULL, /* get_rc4_state */
  477. NULL, /* get_iv */
  478. };
  479. static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
  480. SHA_DIGEST_LENGTH + 32 + 16, /* key len (SHA1 + AES256 + IV) */
  481. 0, /* nonce len */
  482. 16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
  483. SHA_DIGEST_LENGTH, /* max tag length */
  484. NULL, /* init */
  485. aead_aes_256_cbc_sha1_tls_implicit_iv_init,
  486. aead_tls_cleanup,
  487. aead_tls_seal,
  488. aead_tls_open,
  489. NULL, /* get_rc4_state */
  490. aead_tls_get_iv, /* get_iv */
  491. };
  492. static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
  493. SHA256_DIGEST_LENGTH + 32, /* key len (SHA256 + AES256) */
  494. 16, /* nonce len (IV) */
  495. 16 + SHA256_DIGEST_LENGTH, /* overhead (padding + SHA256) */
  496. SHA256_DIGEST_LENGTH, /* max tag length */
  497. NULL, /* init */
  498. aead_aes_256_cbc_sha256_tls_init,
  499. aead_tls_cleanup,
  500. aead_tls_seal,
  501. aead_tls_open,
  502. NULL, /* get_rc4_state */
  503. NULL, /* get_iv */
  504. };
  505. static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
  506. SHA384_DIGEST_LENGTH + 32, /* key len (SHA384 + AES256) */
  507. 16, /* nonce len (IV) */
  508. 16 + SHA384_DIGEST_LENGTH, /* overhead (padding + SHA384) */
  509. SHA384_DIGEST_LENGTH, /* max tag length */
  510. NULL, /* init */
  511. aead_aes_256_cbc_sha384_tls_init,
  512. aead_tls_cleanup,
  513. aead_tls_seal,
  514. aead_tls_open,
  515. NULL, /* get_rc4_state */
  516. NULL, /* get_iv */
  517. };
  518. static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
  519. SHA_DIGEST_LENGTH + 24, /* key len (SHA1 + 3DES) */
  520. 8, /* nonce len (IV) */
  521. 8 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
  522. SHA_DIGEST_LENGTH, /* max tag length */
  523. NULL, /* init */
  524. aead_des_ede3_cbc_sha1_tls_init,
  525. aead_tls_cleanup,
  526. aead_tls_seal,
  527. aead_tls_open,
  528. NULL, /* get_rc4_state */
  529. NULL, /* get_iv */
  530. };
  531. static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
  532. SHA_DIGEST_LENGTH + 24 + 8, /* key len (SHA1 + 3DES + IV) */
  533. 0, /* nonce len */
  534. 8 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
  535. SHA_DIGEST_LENGTH, /* max tag length */
  536. NULL, /* init */
  537. aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
  538. aead_tls_cleanup,
  539. aead_tls_seal,
  540. aead_tls_open,
  541. NULL, /* get_rc4_state */
  542. aead_tls_get_iv, /* get_iv */
  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. NULL, /* init */
  550. aead_null_sha1_tls_init,
  551. aead_tls_cleanup,
  552. aead_tls_seal,
  553. aead_tls_open,
  554. NULL, /* get_rc4_state */
  555. NULL, /* get_iv */
  556. };
  557. const EVP_AEAD *EVP_aead_rc4_md5_tls(void) { return &aead_rc4_md5_tls; }
  558. const EVP_AEAD *EVP_aead_rc4_sha1_tls(void) { return &aead_rc4_sha1_tls; }
  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; }