constant_time.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include <contrib/libs/openssl/redef.h>
  2. /*
  3. * Copyright 2014-2019 The OpenSSL Project Authors. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #ifndef OSSL_INTERNAL_CONSTANT_TIME_H
  11. # define OSSL_INTERNAL_CONSTANT_TIME_H
  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. static ossl_inline unsigned int constant_time_ge(unsigned int a,
  122. unsigned int b)
  123. {
  124. return ~constant_time_lt(a, b);
  125. }
  126. static ossl_inline size_t constant_time_ge_s(size_t a, size_t b)
  127. {
  128. return ~constant_time_lt_s(a, b);
  129. }
  130. static ossl_inline unsigned char constant_time_ge_8(unsigned int a,
  131. unsigned int b)
  132. {
  133. return (unsigned char)constant_time_ge(a, b);
  134. }
  135. static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b)
  136. {
  137. return (unsigned char)constant_time_ge_s(a, b);
  138. }
  139. static ossl_inline unsigned int constant_time_is_zero(unsigned int a)
  140. {
  141. return constant_time_msb(~a & (a - 1));
  142. }
  143. static ossl_inline size_t constant_time_is_zero_s(size_t a)
  144. {
  145. return constant_time_msb_s(~a & (a - 1));
  146. }
  147. static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)
  148. {
  149. return (unsigned char)constant_time_is_zero(a);
  150. }
  151. static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)
  152. {
  153. return constant_time_msb_32(~a & (a - 1));
  154. }
  155. static ossl_inline unsigned int constant_time_eq(unsigned int a,
  156. unsigned int b)
  157. {
  158. return constant_time_is_zero(a ^ b);
  159. }
  160. static ossl_inline size_t constant_time_eq_s(size_t a, size_t b)
  161. {
  162. return constant_time_is_zero_s(a ^ b);
  163. }
  164. static ossl_inline unsigned char constant_time_eq_8(unsigned int a,
  165. unsigned int b)
  166. {
  167. return (unsigned char)constant_time_eq(a, b);
  168. }
  169. static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b)
  170. {
  171. return (unsigned char)constant_time_eq_s(a, b);
  172. }
  173. static ossl_inline unsigned int constant_time_eq_int(int a, int b)
  174. {
  175. return constant_time_eq((unsigned)(a), (unsigned)(b));
  176. }
  177. static ossl_inline unsigned char constant_time_eq_int_8(int a, int b)
  178. {
  179. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  180. }
  181. /*
  182. * Returns the value unmodified, but avoids optimizations.
  183. * The barriers prevent the compiler from narrowing down the
  184. * possible value range of the mask and ~mask in the select
  185. * statements, which avoids the recognition of the select
  186. * and turning it into a conditional load or branch.
  187. */
  188. static ossl_inline unsigned int value_barrier(unsigned int a)
  189. {
  190. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  191. unsigned int r;
  192. __asm__("" : "=r"(r) : "0"(a));
  193. #else
  194. volatile unsigned int r = a;
  195. #endif
  196. return r;
  197. }
  198. /* Convenience method for uint32_t. */
  199. static ossl_inline uint32_t value_barrier_32(uint32_t a)
  200. {
  201. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  202. uint32_t r;
  203. __asm__("" : "=r"(r) : "0"(a));
  204. #else
  205. volatile uint32_t r = a;
  206. #endif
  207. return r;
  208. }
  209. /* Convenience method for uint64_t. */
  210. static ossl_inline uint64_t value_barrier_64(uint64_t a)
  211. {
  212. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  213. uint64_t r;
  214. __asm__("" : "=r"(r) : "0"(a));
  215. #else
  216. volatile uint64_t r = a;
  217. #endif
  218. return r;
  219. }
  220. /* Convenience method for size_t. */
  221. static ossl_inline size_t value_barrier_s(size_t a)
  222. {
  223. #if !defined(OPENSSL_NO_ASM) && defined(__GNUC__)
  224. size_t r;
  225. __asm__("" : "=r"(r) : "0"(a));
  226. #else
  227. volatile size_t r = a;
  228. #endif
  229. return r;
  230. }
  231. static ossl_inline unsigned int constant_time_select(unsigned int mask,
  232. unsigned int a,
  233. unsigned int b)
  234. {
  235. return (value_barrier(mask) & a) | (value_barrier(~mask) & b);
  236. }
  237. static ossl_inline size_t constant_time_select_s(size_t mask,
  238. size_t a,
  239. size_t b)
  240. {
  241. return (value_barrier_s(mask) & a) | (value_barrier_s(~mask) & b);
  242. }
  243. static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
  244. unsigned char a,
  245. unsigned char b)
  246. {
  247. return (unsigned char)constant_time_select(mask, a, b);
  248. }
  249. static ossl_inline int constant_time_select_int(unsigned int mask, int a,
  250. int b)
  251. {
  252. return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
  253. }
  254. static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
  255. {
  256. return (int)constant_time_select((unsigned)mask, (unsigned)(a),
  257. (unsigned)(b));
  258. }
  259. static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
  260. uint32_t b)
  261. {
  262. return (value_barrier_32(mask) & a) | (value_barrier_32(~mask) & b);
  263. }
  264. static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
  265. uint64_t b)
  266. {
  267. return (value_barrier_64(mask) & a) | (value_barrier_64(~mask) & b);
  268. }
  269. /*
  270. * mask must be 0xFFFFFFFF or 0x00000000.
  271. *
  272. * if (mask) {
  273. * uint32_t tmp = *a;
  274. *
  275. * *a = *b;
  276. * *b = tmp;
  277. * }
  278. */
  279. static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
  280. uint32_t *b)
  281. {
  282. uint32_t xor = *a ^ *b;
  283. xor &= mask;
  284. *a ^= xor;
  285. *b ^= xor;
  286. }
  287. /*
  288. * mask must be 0xFFFFFFFF or 0x00000000.
  289. *
  290. * if (mask) {
  291. * uint64_t tmp = *a;
  292. *
  293. * *a = *b;
  294. * *b = tmp;
  295. * }
  296. */
  297. static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
  298. uint64_t *b)
  299. {
  300. uint64_t xor = *a ^ *b;
  301. xor &= mask;
  302. *a ^= xor;
  303. *b ^= xor;
  304. }
  305. /*
  306. * table is a two dimensional array of bytes. Each row has rowsize elements.
  307. * Copies row number idx into out. rowsize and numrows are not considered
  308. * private.
  309. */
  310. static ossl_inline void constant_time_lookup(void *out,
  311. const void *table,
  312. size_t rowsize,
  313. size_t numrows,
  314. size_t idx)
  315. {
  316. size_t i, j;
  317. const unsigned char *tablec = (const unsigned char *)table;
  318. unsigned char *outc = (unsigned char *)out;
  319. unsigned char mask;
  320. memset(out, 0, rowsize);
  321. /* Note idx may underflow - but that is well defined */
  322. for (i = 0; i < numrows; i++, idx--) {
  323. mask = (unsigned char)constant_time_is_zero_s(idx);
  324. for (j = 0; j < rowsize; j++)
  325. *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
  326. }
  327. }
  328. /*
  329. * Expected usage pattern is to unconditionally set error and then
  330. * wipe it if there was no actual error. |clear| is 1 or 0.
  331. */
  332. void err_clear_last_constant_time(int clear);
  333. #endif /* OSSL_INTERNAL_CONSTANT_TIME_H */