constant_time.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright 2014-2025 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. /* Convenience method for unsigned char. */
  255. static ossl_inline unsigned char value_barrier_8(unsigned char a)
  256. {
  257. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  258. unsigned char r;
  259. __asm__("" : "=r"(r) : "0"(a));
  260. #else
  261. volatile unsigned char r = a;
  262. #endif
  263. return r;
  264. }
  265. static ossl_inline unsigned int constant_time_select(unsigned int mask,
  266. unsigned int a,
  267. unsigned int b)
  268. {
  269. return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
  270. }
  271. static ossl_inline size_t constant_time_select_s(size_t mask,
  272. size_t a,
  273. size_t b)
  274. {
  275. return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
  276. }
  277. static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
  278. unsigned char a,
  279. unsigned char b)
  280. {
  281. return (unsigned char)constant_time_select(mask, a, b);
  282. }
  283. static ossl_inline int constant_time_select_int(unsigned int mask, int a,
  284. int b)
  285. {
  286. return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
  287. }
  288. static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
  289. {
  290. return (int)constant_time_select((unsigned)mask, (unsigned)(a),
  291. (unsigned)(b));
  292. }
  293. static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
  294. uint32_t b)
  295. {
  296. return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
  297. }
  298. static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
  299. uint64_t b)
  300. {
  301. return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
  302. }
  303. /*
  304. * mask must be 0xFFFFFFFF or 0x00000000.
  305. *
  306. * if (mask) {
  307. * uint32_t tmp = *a;
  308. *
  309. * *a = *b;
  310. * *b = tmp;
  311. * }
  312. */
  313. static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
  314. uint32_t *b)
  315. {
  316. uint32_t xor = *a ^ *b;
  317. xor &= value_barrier_32(mask);
  318. *a ^= xor;
  319. *b ^= xor;
  320. }
  321. /*
  322. * mask must be 0xFFFFFFFF or 0x00000000.
  323. *
  324. * if (mask) {
  325. * uint64_t tmp = *a;
  326. *
  327. * *a = *b;
  328. * *b = tmp;
  329. * }
  330. */
  331. static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
  332. uint64_t *b)
  333. {
  334. uint64_t xor = *a ^ *b;
  335. xor &= value_barrier_64(mask);
  336. *a ^= xor;
  337. *b ^= xor;
  338. }
  339. /*
  340. * mask must be 0xFF or 0x00.
  341. * "constant time" is per len.
  342. *
  343. * if (mask) {
  344. * unsigned char tmp[len];
  345. *
  346. * memcpy(tmp, a, len);
  347. * memcpy(a, b);
  348. * memcpy(b, tmp);
  349. * }
  350. */
  351. static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
  352. unsigned char *a,
  353. unsigned char *b,
  354. size_t len)
  355. {
  356. size_t i;
  357. unsigned char tmp;
  358. for (i = 0; i < len; i++) {
  359. tmp = a[i] ^ b[i];
  360. tmp &= value_barrier_8(mask);
  361. a[i] ^= tmp;
  362. b[i] ^= tmp;
  363. }
  364. }
  365. /*
  366. * table is a two dimensional array of bytes. Each row has rowsize elements.
  367. * Copies row number idx into out. rowsize and numrows are not considered
  368. * private.
  369. */
  370. static ossl_inline void constant_time_lookup(void *out,
  371. const void *table,
  372. size_t rowsize,
  373. size_t numrows,
  374. size_t idx)
  375. {
  376. size_t i, j;
  377. const unsigned char *tablec = (const unsigned char *)table;
  378. unsigned char *outc = (unsigned char *)out;
  379. unsigned char mask;
  380. memset(out, 0, rowsize);
  381. /* Note idx may underflow - but that is well defined */
  382. for (i = 0; i < numrows; i++, idx--) {
  383. mask = (unsigned char)constant_time_is_zero_s(idx);
  384. for (j = 0; j < rowsize; j++)
  385. *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
  386. }
  387. }
  388. /*
  389. * Expected usage pattern is to unconditionally set error and then
  390. * wipe it if there was no actual error. |clear| is 1 or 0.
  391. */
  392. void err_clear_last_constant_time(int clear);
  393. #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */