2
0

constant_time.h 12 KB

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