constant_time.h 13 KB

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