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.
 
 
 
 
 
 

558 lines
20 KiB

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