constant_time.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright 2014-2024 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. #ifndef OSSL_INTERNAL_CONSTANT_TIME_H
  10. # define OSSL_INTERNAL_CONSTANT_TIME_H
  11. # pragma once
  12. # include <stdlib.h>
  13. # include <string.h>
  14. # include <openssl/e_os2.h> /* For 'ossl_inline' */
  15. /*-
  16. * The boolean methods return a bitmask of all ones (0xff...f) for true
  17. * and 0 for false. This is useful for choosing a value based on the result
  18. * of a conditional in constant time. For example,
  19. * if (a < b) {
  20. * c = a;
  21. * } else {
  22. * c = b;
  23. * }
  24. * can be written as
  25. * unsigned int lt = constant_time_lt(a, b);
  26. * c = constant_time_select(lt, a, b);
  27. */
  28. /* Returns the given value with the MSB copied to all the other bits. */
  29. static ossl_inline unsigned int constant_time_msb(unsigned int a);
  30. /* Convenience method for uint32_t. */
  31. static ossl_inline uint32_t constant_time_msb_32(uint32_t a);
  32. /* Convenience method for uint64_t. */
  33. static ossl_inline uint64_t constant_time_msb_64(uint64_t a);
  34. /* Returns 0xff..f if a < b and 0 otherwise. */
  35. static ossl_inline unsigned int constant_time_lt(unsigned int a,
  36. unsigned int b);
  37. /* Convenience method for getting an 8-bit mask. */
  38. static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
  39. unsigned int b);
  40. /* Convenience method for uint64_t. */
  41. static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b);
  42. /* Returns 0xff..f if a >= b and 0 otherwise. */
  43. static ossl_inline unsigned int constant_time_ge(unsigned int a,
  44. unsigned int b);
  45. /* Convenience method for getting an 8-bit mask. */
  46. static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
  47. unsigned int b);
  48. /* Returns 0xff..f if a == 0 and 0 otherwise. */
  49. static ossl_inline unsigned int constant_time_is_zero(unsigned int a);
  50. /* Convenience method for getting an 8-bit mask. */
  51. static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a);
  52. /* Convenience method for getting a 32-bit mask. */
  53. static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a);
  54. /* Returns 0xff..f if a == b and 0 otherwise. */
  55. static ossl_inline unsigned int constant_time_eq(unsigned int a,
  56. unsigned int b);
  57. /* Convenience method for getting an 8-bit mask. */
  58. static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
  59. unsigned int b);
  60. /* Signed integers. */
  61. static ossl_inline unsigned int constant_time_eq_int(int a, int b);
  62. /* Convenience method for getting an 8-bit mask. */
  63. static ossl_inline unsigned char constant_time_eq_int_8(int a, int b);
  64. /*-
  65. * Returns (mask & a) | (~mask & b).
  66. *
  67. * When |mask| is all 1s or all 0s (as returned by the methods above),
  68. * the select methods return either |a| (if |mask| is nonzero) or |b|
  69. * (if |mask| is zero).
  70. */
  71. static ossl_inline unsigned int constant_time_select(unsigned int mask,
  72. unsigned int a,
  73. unsigned int b);
  74. /* Convenience method for unsigned chars. */
  75. static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
  76. unsigned char a,
  77. unsigned char b);
  78. /* Convenience method for uint32_t. */
  79. static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
  80. uint32_t b);
  81. /* Convenience method for uint64_t. */
  82. static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
  83. uint64_t b);
  84. /* Convenience method for signed integers. */
  85. static ossl_inline int constant_time_select_int(unsigned int mask, int a,
  86. int b);
  87. static ossl_inline unsigned int constant_time_msb(unsigned int a)
  88. {
  89. return 0 - (a >> (sizeof(a) * 8 - 1));
  90. }
  91. static ossl_inline uint32_t constant_time_msb_32(uint32_t a)
  92. {
  93. return 0 - (a >> 31);
  94. }
  95. static ossl_inline uint64_t constant_time_msb_64(uint64_t a)
  96. {
  97. return 0 - (a >> 63);
  98. }
  99. static ossl_inline size_t constant_time_msb_s(size_t a)
  100. {
  101. return 0 - (a >> (sizeof(a) * 8 - 1));
  102. }
  103. static ossl_inline unsigned int constant_time_lt(unsigned int a,
  104. unsigned int b)
  105. {
  106. return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
  107. }
  108. static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
  109. {
  110. return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
  111. }
  112. static ossl_inline unsigned char constant_time_lt_8(unsigned int a,
  113. unsigned int b)
  114. {
  115. return (unsigned char)constant_time_lt(a, b);
  116. }
  117. static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b)
  118. {
  119. return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b)));
  120. }
  121. #ifdef BN_ULONG
  122. static ossl_inline BN_ULONG constant_time_msb_bn(BN_ULONG a)
  123. {
  124. return 0 - (a >> (sizeof(a) * 8 - 1));
  125. }
  126. static ossl_inline BN_ULONG constant_time_lt_bn(BN_ULONG a, BN_ULONG b)
  127. {
  128. return constant_time_msb_bn(a ^ ((a ^ b) | ((a - b) ^ b)));
  129. }
  130. static ossl_inline BN_ULONG constant_time_is_zero_bn(BN_ULONG a)
  131. {
  132. return constant_time_msb_bn(~a & (a - 1));
  133. }
  134. static ossl_inline BN_ULONG constant_time_eq_bn(BN_ULONG a,
  135. BN_ULONG b)
  136. {
  137. return constant_time_is_zero_bn(a ^ b);
  138. }
  139. #endif
  140. static ossl_inline unsigned int constant_time_ge(unsigned int a,
  141. unsigned int b)
  142. {
  143. return ~constant_time_lt(a, b);
  144. }
  145. static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
  146. {
  147. return ~constant_time_lt_s(a, b);
  148. }
  149. static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
  150. unsigned int b)
  151. {
  152. return (unsigned char)constant_time_ge(a, b);
  153. }
  154. static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
  155. {
  156. return (unsigned char)constant_time_ge_s(a, b);
  157. }
  158. static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
  159. {
  160. return constant_time_msb(~a & (a - 1));
  161. }
  162. static ossl_inline size_t constant_time_is_zero_s(size_t a)
  163. {
  164. return constant_time_msb_s(~a & (a - 1));
  165. }
  166. static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
  167. {
  168. return (unsigned char)constant_time_is_zero(a);
  169. }
  170. static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)
  171. {
  172. return constant_time_msb_32(~a & (a - 1));
  173. }
  174. static ossl_inline uint64_t constant_time_is_zero_64(uint64_t a)
  175. {
  176. return constant_time_msb_64(~a & (a - 1));
  177. }
  178. static ossl_inline unsigned int constant_time_eq(unsigned int a,
  179. unsigned int b)
  180. {
  181. return constant_time_is_zero(a ^ b);
  182. }
  183. static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
  184. {
  185. return constant_time_is_zero_s(a ^ b);
  186. }
  187. static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
  188. unsigned int b)
  189. {
  190. return (unsigned char)constant_time_eq(a, b);
  191. }
  192. static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
  193. {
  194. return (unsigned char)constant_time_eq_s(a, b);
  195. }
  196. static ossl_inline unsigned int constant_time_eq_int(int a, int b)
  197. {
  198. return constant_time_eq((unsigned)(a), (unsigned)(b));
  199. }
  200. static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
  201. {
  202. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  203. }
  204. /*
  205. * Returns the value unmodified, but avoids optimizations.
  206. * The barriers prevent the compiler from narrowing down the
  207. * possible value range of the mask and ~mask in the select
  208. * statements, which avoids the recognition of the select
  209. * and turning it into a conditional load or branch.
  210. */
  211. static ossl_inline unsigned int value_barrier(unsigned int a)
  212. {
  213. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  214. unsigned int r;
  215. __asm__("" : "=r"(r) : "0"(a));
  216. #else
  217. volatile unsigned int r = a;
  218. #endif
  219. return r;
  220. }
  221. /* Convenience method for uint32_t. */
  222. static ossl_inline uint32_t value_barrier_32(uint32_t a)
  223. {
  224. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  225. uint32_t r;
  226. __asm__("" : "=r"(r) : "0"(a));
  227. #else
  228. volatile uint32_t r = a;
  229. #endif
  230. return r;
  231. }
  232. /* Convenience method for uint64_t. */
  233. static ossl_inline uint64_t value_barrier_64(uint64_t a)
  234. {
  235. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  236. uint64_t r;
  237. __asm__("" : "=r"(r) : "0"(a));
  238. #else
  239. volatile uint64_t r = a;
  240. #endif
  241. return r;
  242. }
  243. /* Convenience method for size_t. */
  244. static ossl_inline size_t value_barrier_s(size_t a)
  245. {
  246. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  247. size_t r;
  248. __asm__("" : "=r"(r) : "0"(a));
  249. #else
  250. volatile size_t r = a;
  251. #endif
  252. return r;
  253. }
  254. static ossl_inline unsigned int constant_time_select(unsigned int mask,
  255. unsigned int a,
  256. unsigned int b)
  257. {
  258. return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
  259. }
  260. static ossl_inline size_t constant_time_select_s(size_t mask,
  261. size_t a,
  262. size_t b)
  263. {
  264. return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
  265. }
  266. static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
  267. unsigned char a,
  268. unsigned char b)
  269. {
  270. return (unsigned char)constant_time_select(mask, a, b);
  271. }
  272. static ossl_inline int constant_time_select_int(unsigned int mask, int a,
  273. int b)
  274. {
  275. return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
  276. }
  277. static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
  278. {
  279. return (int)constant_time_select((unsigned)mask, (unsigned)(a),
  280. (unsigned)(b));
  281. }
  282. static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
  283. uint32_t b)
  284. {
  285. return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
  286. }
  287. static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
  288. uint64_t b)
  289. {
  290. return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
  291. }
  292. /*
  293. * mask must be 0xFFFFFFFF or 0x00000000.
  294. *
  295. * if (mask) {
  296. * uint32_t tmp = *a;
  297. *
  298. * *a = *b;
  299. * *b = tmp;
  300. * }
  301. */
  302. static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
  303. uint32_t *b)
  304. {
  305. uint32_t xor = *a ^ *b;
  306. xor &= mask;
  307. *a ^= xor;
  308. *b ^= xor;
  309. }
  310. /*
  311. * mask must be 0xFFFFFFFF or 0x00000000.
  312. *
  313. * if (mask) {
  314. * uint64_t tmp = *a;
  315. *
  316. * *a = *b;
  317. * *b = tmp;
  318. * }
  319. */
  320. static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
  321. uint64_t *b)
  322. {
  323. uint64_t xor = *a ^ *b;
  324. xor &= mask;
  325. *a ^= xor;
  326. *b ^= xor;
  327. }
  328. /*
  329. * mask must be 0xFF or 0x00.
  330. * "constant time" is per len.
  331. *
  332. * if (mask) {
  333. * unsigned char tmp[len];
  334. *
  335. * memcpy(tmp, a, len);
  336. * memcpy(a, b);
  337. * memcpy(b, tmp);
  338. * }
  339. */
  340. static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
  341. unsigned char *a,
  342. unsigned char *b,
  343. size_t len)
  344. {
  345. size_t i;
  346. unsigned char tmp;
  347. for (i = 0; i < len; i++) {
  348. tmp = a[i] ^ b[i];
  349. tmp &= mask;
  350. a[i] ^= tmp;
  351. b[i] ^= tmp;
  352. }
  353. }
  354. /*
  355. * table is a two dimensional array of bytes. Each row has rowsize elements.
  356. * Copies row number idx into out. rowsize and numrows are not considered
  357. * private.
  358. */
  359. static ossl_inline void constant_time_lookup(void *out,
  360. const void *table,
  361. size_t rowsize,
  362. size_t numrows,
  363. size_t idx)
  364. {
  365. size_t i, j;
  366. const unsigned char *tablec = (const unsigned char *)table;
  367. unsigned char *outc = (unsigned char *)out;
  368. unsigned char mask;
  369. memset(out, 0, rowsize);
  370. /* Note idx may underflow - but that is well defined */
  371. for (i = 0; i < numrows; i++, idx--) {
  372. mask = (unsigned char)constant_time_is_zero_s(idx);
  373. for (j = 0; j < rowsize; j++)
  374. *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
  375. }
  376. }
  377. /*
  378. * Expected usage pattern is to unconditionally set error and then
  379. * wipe it if there was no actual error. |clear| is 1 or 0.
  380. */
  381. void err_clear_last_constant_time(int clear);
  382. #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */