tls_pad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/rand.h>
  10. #include <openssl/evp.h>
  11. #include "internal/constant_time.h"
  12. #include "internal/cryptlib.h"
  13. #include "internal/ssl3_cbc.h"
  14. /*
  15. * This file has no dependencies on the rest of libssl because it is shared
  16. * with the providers. It contains functions for low level CBC TLS padding
  17. * removal. Responsibility for this lies with the cipher implementations in the
  18. * providers. However there are legacy code paths in libssl which also need to
  19. * do this. In time those legacy code paths can be removed and this file can be
  20. * moved out of libssl.
  21. */
  22. static int ssl3_cbc_copy_mac(size_t *reclen,
  23. size_t origreclen,
  24. unsigned char *recdata,
  25. unsigned char **mac,
  26. int *alloced,
  27. size_t block_size,
  28. size_t mac_size,
  29. size_t good,
  30. OSSL_LIB_CTX *libctx);
  31. /*-
  32. * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
  33. * record in |recdata| by updating |reclen| in constant time. It also extracts
  34. * the MAC from the underlying record and places a pointer to it in |mac|. The
  35. * MAC data can either be newly allocated memory, or a pointer inside the
  36. * |recdata| buffer. If allocated then |*alloced| is set to 1, otherwise it is
  37. * set to 0.
  38. *
  39. * origreclen: the original record length before any changes were made
  40. * block_size: the block size of the cipher used to encrypt the record.
  41. * mac_size: the size of the MAC to be extracted
  42. * aead: 1 if an AEAD cipher is in use, or 0 otherwise
  43. * returns:
  44. * 0: if the record is publicly invalid.
  45. * 1: if the record is publicly valid. If the padding removal fails then the
  46. * MAC returned is random.
  47. */
  48. int ssl3_cbc_remove_padding_and_mac(size_t *reclen,
  49. size_t origreclen,
  50. unsigned char *recdata,
  51. unsigned char **mac,
  52. int *alloced,
  53. size_t block_size, size_t mac_size,
  54. OSSL_LIB_CTX *libctx)
  55. {
  56. size_t padding_length;
  57. size_t good;
  58. const size_t overhead = 1 /* padding length byte */ + mac_size;
  59. /*
  60. * These lengths are all public so we can test them in non-constant time.
  61. */
  62. if (overhead > *reclen)
  63. return 0;
  64. padding_length = recdata[*reclen - 1];
  65. good = constant_time_ge_s(*reclen, padding_length + overhead);
  66. /* SSLv3 requires that the padding is minimal. */
  67. good &= constant_time_ge_s(block_size, padding_length + 1);
  68. *reclen -= good & (padding_length + 1);
  69. return ssl3_cbc_copy_mac(reclen, origreclen, recdata, mac, alloced,
  70. block_size, mac_size, good, libctx);
  71. }
  72. /*-
  73. * tls1_cbc_remove_padding_and_mac removes padding from the decrypted, TLS, CBC
  74. * record in |recdata| by updating |reclen| in constant time. It also extracts
  75. * the MAC from the underlying record and places a pointer to it in |mac|. The
  76. * MAC data can either be newly allocated memory, or a pointer inside the
  77. * |recdata| buffer. If allocated then |*alloced| is set to 1, otherwise it is
  78. * set to 0.
  79. *
  80. * origreclen: the original record length before any changes were made
  81. * block_size: the block size of the cipher used to encrypt the record.
  82. * mac_size: the size of the MAC to be extracted
  83. * aead: 1 if an AEAD cipher is in use, or 0 otherwise
  84. * returns:
  85. * 0: if the record is publicly invalid.
  86. * 1: if the record is publicly valid. If the padding removal fails then the
  87. * MAC returned is random.
  88. */
  89. int tls1_cbc_remove_padding_and_mac(size_t *reclen,
  90. size_t origreclen,
  91. unsigned char *recdata,
  92. unsigned char **mac,
  93. int *alloced,
  94. size_t block_size, size_t mac_size,
  95. int aead,
  96. OSSL_LIB_CTX *libctx)
  97. {
  98. size_t good = -1;
  99. size_t padding_length, to_check, i;
  100. size_t overhead = ((block_size == 1) ? 0 : 1) /* padding length byte */
  101. + mac_size;
  102. /*
  103. * These lengths are all public so we can test them in non-constant
  104. * time.
  105. */
  106. if (overhead > *reclen)
  107. return 0;
  108. if (block_size != 1) {
  109. padding_length = recdata[*reclen - 1];
  110. if (aead) {
  111. /* padding is already verified and we don't need to check the MAC */
  112. *reclen -= padding_length + 1 + mac_size;
  113. return 1;
  114. }
  115. good = constant_time_ge_s(*reclen, overhead + padding_length);
  116. /*
  117. * The padding consists of a length byte at the end of the record and
  118. * then that many bytes of padding, all with the same value as the
  119. * length byte. Thus, with the length byte included, there are i+1 bytes
  120. * of padding. We can't check just |padding_length+1| bytes because that
  121. * leaks decrypted information. Therefore we always have to check the
  122. * maximum amount of padding possible. (Again, the length of the record
  123. * is public information so we can use it.)
  124. */
  125. to_check = 256; /* maximum amount of padding, inc length byte. */
  126. if (to_check > *reclen)
  127. to_check = *reclen;
  128. for (i = 0; i < to_check; i++) {
  129. unsigned char mask = constant_time_ge_8_s(padding_length, i);
  130. unsigned char b = recdata[*reclen - 1 - i];
  131. /*
  132. * The final |padding_length+1| bytes should all have the value
  133. * |padding_length|. Therefore the XOR should be zero.
  134. */
  135. good &= ~(mask & (padding_length ^ b));
  136. }
  137. /*
  138. * If any of the final |padding_length+1| bytes had the wrong value, one
  139. * or more of the lower eight bits of |good| will be cleared.
  140. */
  141. good = constant_time_eq_s(0xff, good & 0xff);
  142. *reclen -= good & (padding_length + 1);
  143. }
  144. return ssl3_cbc_copy_mac(reclen, origreclen, recdata, mac, alloced,
  145. block_size, mac_size, good, libctx);
  146. }
  147. /*-
  148. * ssl3_cbc_copy_mac copies |md_size| bytes from the end of the record in
  149. * |recdata| to |*mac| in constant time (independent of the concrete value of
  150. * the record length |reclen|, which may vary within a 256-byte window).
  151. *
  152. * On entry:
  153. * origreclen >= mac_size
  154. * mac_size <= EVP_MAX_MD_SIZE
  155. *
  156. * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
  157. * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
  158. * a single or pair of cache-lines, then the variable memory accesses don't
  159. * actually affect the timing. CPUs with smaller cache-lines [if any] are
  160. * not multi-core and are not considered vulnerable to cache-timing attacks.
  161. */
  162. #define CBC_MAC_ROTATE_IN_PLACE
  163. static int ssl3_cbc_copy_mac(size_t *reclen,
  164. size_t origreclen,
  165. unsigned char *recdata,
  166. unsigned char **mac,
  167. int *alloced,
  168. size_t block_size,
  169. size_t mac_size,
  170. size_t good,
  171. OSSL_LIB_CTX *libctx)
  172. {
  173. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  174. unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
  175. unsigned char *rotated_mac;
  176. char aux1, aux2, aux3, mask;
  177. #else
  178. unsigned char rotated_mac[EVP_MAX_MD_SIZE];
  179. #endif
  180. unsigned char randmac[EVP_MAX_MD_SIZE];
  181. unsigned char *out;
  182. /*
  183. * mac_end is the index of |recdata| just after the end of the MAC.
  184. */
  185. size_t mac_end = *reclen;
  186. size_t mac_start = mac_end - mac_size;
  187. size_t in_mac;
  188. /*
  189. * scan_start contains the number of bytes that we can ignore because the
  190. * MAC's position can only vary by 255 bytes.
  191. */
  192. size_t scan_start = 0;
  193. size_t i, j;
  194. size_t rotate_offset;
  195. if (!ossl_assert(origreclen >= mac_size
  196. && mac_size <= EVP_MAX_MD_SIZE))
  197. return 0;
  198. /* If no MAC then nothing to be done */
  199. if (mac_size == 0) {
  200. /* No MAC so we can do this in non-constant time */
  201. if (good == 0)
  202. return 0;
  203. return 1;
  204. }
  205. *reclen -= mac_size;
  206. if (block_size == 1) {
  207. /* There's no padding so the position of the MAC is fixed */
  208. if (mac != NULL)
  209. *mac = &recdata[*reclen];
  210. if (alloced != NULL)
  211. *alloced = 0;
  212. return 1;
  213. }
  214. /* Create the random MAC we will emit if padding is bad */
  215. if (RAND_bytes_ex(libctx, randmac, mac_size, 0) <= 0)
  216. return 0;
  217. if (!ossl_assert(mac != NULL && alloced != NULL))
  218. return 0;
  219. *mac = out = OPENSSL_malloc(mac_size);
  220. if (*mac == NULL)
  221. return 0;
  222. *alloced = 1;
  223. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  224. rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
  225. #endif
  226. /* This information is public so it's safe to branch based on it. */
  227. if (origreclen > mac_size + 255 + 1)
  228. scan_start = origreclen - (mac_size + 255 + 1);
  229. in_mac = 0;
  230. rotate_offset = 0;
  231. memset(rotated_mac, 0, mac_size);
  232. for (i = scan_start, j = 0; i < origreclen; i++) {
  233. size_t mac_started = constant_time_eq_s(i, mac_start);
  234. size_t mac_ended = constant_time_lt_s(i, mac_end);
  235. unsigned char b = recdata[i];
  236. in_mac |= mac_started;
  237. in_mac &= mac_ended;
  238. rotate_offset |= j & mac_started;
  239. rotated_mac[j++] |= b & in_mac;
  240. j &= constant_time_lt_s(j, mac_size);
  241. }
  242. /* Now rotate the MAC */
  243. #if defined(CBC_MAC_ROTATE_IN_PLACE)
  244. j = 0;
  245. for (i = 0; i < mac_size; i++) {
  246. /*
  247. * in case cache-line is 32 bytes,
  248. * load from both lines and select appropriately
  249. */
  250. aux1 = rotated_mac[rotate_offset & ~32];
  251. aux2 = rotated_mac[rotate_offset | 32];
  252. mask = constant_time_eq_8(rotate_offset & ~32, rotate_offset);
  253. aux3 = constant_time_select_8(mask, aux1, aux2);
  254. rotate_offset++;
  255. /* If the padding wasn't good we emit a random MAC */
  256. out[j++] = constant_time_select_8((unsigned char)(good & 0xff),
  257. aux3,
  258. randmac[i]);
  259. rotate_offset &= constant_time_lt_s(rotate_offset, mac_size);
  260. }
  261. #else
  262. memset(out, 0, mac_size);
  263. rotate_offset = mac_size - rotate_offset;
  264. rotate_offset &= constant_time_lt_s(rotate_offset, mac_size);
  265. for (i = 0; i < mac_size; i++) {
  266. for (j = 0; j < mac_size; j++)
  267. out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
  268. rotate_offset++;
  269. rotate_offset &= constant_time_lt_s(rotate_offset, mac_size);
  270. /* If the padding wasn't good we emit a random MAC */
  271. out[i] = constant_time_select_8((unsigned char)(good & 0xff), out[i],
  272. randmac[i]);
  273. }
  274. #endif
  275. return 1;
  276. }