constant_time_locl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2014-2018 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 HEADER_CONSTANT_TIME_LOCL_H
  10. # define HEADER_CONSTANT_TIME_LOCL_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. static ossl_inline unsigned int constant_time_select(unsigned int mask,
  181. unsigned int a,
  182. unsigned int b)
  183. {
  184. return (mask & a) | (~mask & b);
  185. }
  186. static ossl_inline size_t constant_time_select_s(size_t mask,
  187. size_t a,
  188. size_t b)
  189. {
  190. return (mask & a) | (~mask & b);
  191. }
  192. static ossl_inline unsigned char constant_time_select_8(unsigned char mask,
  193. unsigned char a,
  194. unsigned char b)
  195. {
  196. return (unsigned char)constant_time_select(mask, a, b);
  197. }
  198. static ossl_inline int constant_time_select_int(unsigned int mask, int a,
  199. int b)
  200. {
  201. return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b));
  202. }
  203. static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b)
  204. {
  205. return (int)constant_time_select((unsigned)mask, (unsigned)(a),
  206. (unsigned)(b));
  207. }
  208. static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a,
  209. uint32_t b)
  210. {
  211. return (mask & a) | (~mask & b);
  212. }
  213. static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a,
  214. uint64_t b)
  215. {
  216. return (mask & a) | (~mask & b);
  217. }
  218. /*
  219. * mask must be 0xFFFFFFFF or 0x00000000.
  220. *
  221. * if (mask) {
  222. * uint32_t tmp = *a;
  223. *
  224. * *a = *b;
  225. * *b = tmp;
  226. * }
  227. */
  228. static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a,
  229. uint32_t *b)
  230. {
  231. uint32_t xor = *a ^ *b;
  232. xor &= mask;
  233. *a ^= xor;
  234. *b ^= xor;
  235. }
  236. /*
  237. * mask must be 0xFFFFFFFF or 0x00000000.
  238. *
  239. * if (mask) {
  240. * uint64_t tmp = *a;
  241. *
  242. * *a = *b;
  243. * *b = tmp;
  244. * }
  245. */
  246. static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a,
  247. uint64_t *b)
  248. {
  249. uint64_t xor = *a ^ *b;
  250. xor &= mask;
  251. *a ^= xor;
  252. *b ^= xor;
  253. }
  254. /*
  255. * table is a two dimensional array of bytes. Each row has rowsize elements.
  256. * Copies row number idx into out. rowsize and numrows are not considered
  257. * private.
  258. */
  259. static ossl_inline void constant_time_lookup(void *out,
  260. const void *table,
  261. size_t rowsize,
  262. size_t numrows,
  263. size_t idx)
  264. {
  265. size_t i, j;
  266. const unsigned char *tablec = (const unsigned char *)table;
  267. unsigned char *outc = (unsigned char *)out;
  268. unsigned char mask;
  269. memset(out, 0, rowsize);
  270. /* Note idx may underflow - but that is well defined */
  271. for (i = 0; i < numrows; i++, idx--) {
  272. mask = (unsigned char)constant_time_is_zero_s(idx);
  273. for (j = 0; j < rowsize; j++)
  274. *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0);
  275. }
  276. }
  277. #endif /* HEADER_CONSTANT_TIME_LOCL_H */