constant_time.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright 2014-2021 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 value_barrier_bn(BN_ULONG a)
  123. {
  124. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  125. BN_ULONG r;
  126. __asm__("" : "=r"(r) : "0"(a));
  127. #else
  128. volatile BN_ULONG r = a;
  129. #endif
  130. return r;
  131. }
  132. static ossl_inline BN_ULONG constant_time_msb_bn(BN_ULONG a)
  133. {
  134. return 0 - (a >> (sizeof(a) * 8 - 1));
  135. }
  136. static ossl_inline BN_ULONG constant_time_lt_bn(BN_ULONG a, BN_ULONG b)
  137. {
  138. return constant_time_msb_bn(a ^ ((a ^ b) | ((a - b) ^ b)));
  139. }
  140. static ossl_inline BN_ULONG constant_time_is_zero_bn(BN_ULONG a)
  141. {
  142. return constant_time_msb_bn(~a & (a - 1));
  143. }
  144. static ossl_inline BN_ULONG constant_time_eq_bn(BN_ULONG a,
  145. BN_ULONG b)
  146. {
  147. return constant_time_is_zero_bn(a ^ b);
  148. }
  149. static ossl_inline BN_ULONG constant_time_select_bn(BN_ULONG mask,
  150. BN_ULONG a,
  151. BN_ULONG b)
  152. {
  153. return (value_barrier_bn(mask) & a) | (value_barrier_bn(~mask) & b);
  154. }
  155. #endif
  156. static ossl_inline unsigned int constant_time_ge(unsigned int a,
  157. unsigned int b)
  158. {
  159. return ~constant_time_lt(a, b);
  160. }
  161. static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
  162. {
  163. return ~constant_time_lt_s(a, b);
  164. }
  165. static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
  166. unsigned int b)
  167. {
  168. return (unsigned char)constant_time_ge(a, b);
  169. }
  170. static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
  171. {
  172. return (unsigned char)constant_time_ge_s(a, b);
  173. }
  174. static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
  175. {
  176. return constant_time_msb(~a & (a - 1));
  177. }
  178. static ossl_inline size_t constant_time_is_zero_s(size_t a)
  179. {
  180. return constant_time_msb_s(~a & (a - 1));
  181. }
  182. static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
  183. {
  184. return (unsigned char)constant_time_is_zero(a);
  185. }
  186. static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)
  187. {
  188. return constant_time_msb_32(~a & (a - 1));
  189. }
  190. static ossl_inline uint64_t constant_time_is_zero_64(uint64_t a)
  191. {
  192. return constant_time_msb_64(~a & (a - 1));
  193. }
  194. static ossl_inline unsigned int constant_time_eq(unsigned int a,
  195. unsigned int b)
  196. {
  197. return constant_time_is_zero(a ^ b);
  198. }
  199. static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
  200. {
  201. return constant_time_is_zero_s(a ^ b);
  202. }
  203. static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
  204. unsigned int b)
  205. {
  206. return (unsigned char)constant_time_eq(a, b);
  207. }
  208. static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
  209. {
  210. return (unsigned char)constant_time_eq_s(a, b);
  211. }
  212. static ossl_inline unsigned int constant_time_eq_int(int a, int b)
  213. {
  214. return constant_time_eq((unsigned)(a), (unsigned)(b));
  215. }
  216. static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
  217. {
  218. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  219. }
  220. /*
  221. * Returns the value unmodified, but avoids optimizations.
  222. * The barriers prevent the compiler from narrowing down the
  223. * possible value range of the mask and ~mask in the select
  224. * statements, which avoids the recognition of the select
  225. * and turning it into a conditional load or branch.
  226. */
  227. static ossl_inline unsigned int value_barrier(unsigned int a)
  228. {
  229. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  230. unsigned int r;
  231. __asm__("" : "=r"(r) : "0"(a));
  232. #else
  233. volatile unsigned int r = a;
  234. #endif
  235. return r;
  236. }
  237. /* Convenience method for uint32_t. */
  238. static ossl_inline uint32_t value_barrier_32(uint32_t a)
  239. {
  240. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  241. uint32_t r;
  242. __asm__("" : "=r"(r) : "0"(a));
  243. #else
  244. volatile uint32_t r = a;
  245. #endif
  246. return r;
  247. }
  248. /* Convenience method for uint64_t. */
  249. static ossl_inline uint64_t value_barrier_64(uint64_t a)
  250. {
  251. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  252. uint64_t r;
  253. __asm__("" : "=r"(r) : "0"(a));
  254. #else
  255. volatile uint64_t r = a;
  256. #endif
  257. return r;
  258. }
  259. /* Convenience method for size_t. */
  260. static ossl_inline size_t value_barrier_s(size_t a)
  261. {
  262. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  263. size_t r;
  264. __asm__("" : "=r"(r) : "0"(a));
  265. #else
  266. volatile size_t r = a;
  267. #endif
  268. return r;
  269. }
  270. static ossl_inline unsigned int constant_time_select(unsigned int mask,
  271. unsigned int a,
  272. unsigned int b)
  273. {
  274. return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
  275. }
  276. static ossl_inline size_t constant_time_select_s(size_t mask,
  277. size_t a,
  278. size_t b)
  279. {
  280. return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
  281. }
  282. static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
  283. unsigned char a,
  284. unsigned char b)
  285. {
  286. return (unsigned char)constant_time_select(mask, a, b);
  287. }
  288. static ossl_inline int constant_time_select_int(unsigned int mask, int a,
  289. int b)
  290. {
  291. return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
  292. }
  293. static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
  294. {
  295. return (int)constant_time_select((unsigned)mask, (unsigned)(a),
  296. (unsigned)(b));
  297. }
  298. static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
  299. uint32_t b)
  300. {
  301. return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
  302. }
  303. static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
  304. uint64_t b)
  305. {
  306. return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
  307. }
  308. /*
  309. * mask must be 0xFFFFFFFF or 0x00000000.
  310. *
  311. * if (mask) {
  312. * uint32_t tmp = *a;
  313. *
  314. * *a = *b;
  315. * *b = tmp;
  316. * }
  317. */
  318. static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
  319. uint32_t *b)
  320. {
  321. uint32_t xor = *a ^ *b;
  322. xor &= mask;
  323. *a ^= xor;
  324. *b ^= xor;
  325. }
  326. /*
  327. * mask must be 0xFFFFFFFF or 0x00000000.
  328. *
  329. * if (mask) {
  330. * uint64_t tmp = *a;
  331. *
  332. * *a = *b;
  333. * *b = tmp;
  334. * }
  335. */
  336. static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
  337. uint64_t *b)
  338. {
  339. uint64_t xor = *a ^ *b;
  340. xor &= mask;
  341. *a ^= xor;
  342. *b ^= xor;
  343. }
  344. /*
  345. * mask must be 0xFF or 0x00.
  346. * "constant time" is per len.
  347. *
  348. * if (mask) {
  349. * unsigned char tmp[len];
  350. *
  351. * memcpy(tmp, a, len);
  352. * memcpy(a, b);
  353. * memcpy(b, tmp);
  354. * }
  355. */
  356. static ossl_inline void constant_time_cond_swap_buff(unsigned char mask,
  357. unsigned char *a,
  358. unsigned char *b,
  359. size_t len)
  360. {
  361. size_t i;
  362. unsigned char tmp;
  363. for (i = 0; i < len; i++) {
  364. tmp = a[i] ^ b[i];
  365. tmp &= mask;
  366. a[i] ^= tmp;
  367. b[i] ^= tmp;
  368. }
  369. }
  370. /*
  371. * table is a two dimensional array of bytes. Each row has rowsize elements.
  372. * Copies row number idx into out. rowsize and numrows are not considered
  373. * private.
  374. */
  375. static ossl_inline void constant_time_lookup(void *out,
  376. const void *table,
  377. size_t rowsize,
  378. size_t numrows,
  379. size_t idx)
  380. {
  381. size_t i, j;
  382. const unsigned char *tablec = (const unsigned char *)table;
  383. unsigned char *outc = (unsigned char *)out;
  384. unsigned char mask;
  385. memset(out, 0, rowsize);
  386. /* Note idx may underflow - but that is well defined */
  387. for (i = 0; i < numrows; i++, idx--) {
  388. mask = (unsigned char)constant_time_is_zero_s(idx);
  389. for (j = 0; j < rowsize; j++)
  390. *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
  391. }
  392. }
  393. /*
  394. * Expected usage pattern is to unconditionally set error and then
  395. * wipe it if there was no actual error. |clear| is 1 or 0.
  396. */
  397. void err_clear_last_constant_time(int clear);
  398. #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */