constant_time.h 14 KB

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