di_ops.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (c) 2011-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file di_ops.c
  5. * \brief Functions for data-independent operations.
  6. **/
  7. #include "orconfig.h"
  8. #include "di_ops.h"
  9. #include "torlog.h"
  10. #include "util.h"
  11. /**
  12. * Timing-safe version of memcmp. As memcmp, compare the <b>sz</b> bytes at
  13. * <b>a</b> with the <b>sz</b> bytes at <b>b</b>, and return less than 0 if
  14. * the bytes at <b>a</b> lexically precede those at <b>b</b>, 0 if the byte
  15. * ranges are equal, and greater than zero if the bytes at <b>a</b> lexically
  16. * follow those at <b>b</b>.
  17. *
  18. * This implementation differs from memcmp in that its timing behavior is not
  19. * data-dependent: it should return in the same amount of time regardless of
  20. * the contents of <b>a</b> and <b>b</b>.
  21. */
  22. int
  23. tor_memcmp(const void *a, const void *b, size_t len)
  24. {
  25. #ifdef HAVE_TIMINGSAFE_MEMCMP
  26. return timingsafe_memcmp(a, b, len);
  27. #else
  28. const uint8_t *x = a;
  29. const uint8_t *y = b;
  30. size_t i = len;
  31. int retval = 0;
  32. /* This loop goes from the end of the arrays to the start. At the
  33. * start of every iteration, before we decrement i, we have set
  34. * "retval" equal to the result of memcmp(a+i,b+i,len-i). During the
  35. * loop, we update retval by leaving it unchanged if x[i]==y[i] and
  36. * setting it to x[i]-y[i] if x[i]!= y[i].
  37. *
  38. * The following assumes we are on a system with two's-complement
  39. * arithmetic. We check for this at configure-time with the check
  40. * that sets USING_TWOS_COMPLEMENT. If we aren't two's complement, then
  41. * torint.h will stop compilation with an error.
  42. */
  43. while (i--) {
  44. int v1 = x[i];
  45. int v2 = y[i];
  46. int equal_p = v1 ^ v2;
  47. /* The following sets bits 8 and above of equal_p to 'equal_p ==
  48. * 0', and thus to v1 == v2. (To see this, note that if v1 ==
  49. * v2, then v1^v2 == equal_p == 0, so equal_p-1 == -1, which is the
  50. * same as ~0 on a two's-complement machine. Then note that if
  51. * v1 != v2, then 0 < v1 ^ v2 < 256, so 0 <= equal_p - 1 < 255.)
  52. */
  53. --equal_p;
  54. equal_p >>= 8;
  55. /* Thanks to (sign-preserving) arithmetic shift, equal_p is now
  56. * equal to -(v1 == v2), which is exactly what we need below.
  57. * (Since we're assuming two's-complement arithmetic, -1 is the
  58. * same as ~0 (all bits set).)
  59. *
  60. * (The result of an arithmetic shift on a negative value is
  61. * actually implementation-defined in standard C. So how do we
  62. * get away with assuming it? Easy. We check.) */
  63. #if ((-60 >> 8) != -1)
  64. #error "According to cpp, right-shift doesn't perform sign-extension."
  65. #endif
  66. #ifndef RSHIFT_DOES_SIGN_EXTEND
  67. #error "According to configure, right-shift doesn't perform sign-extension."
  68. #endif
  69. /* If v1 == v2, equal_p is ~0, so this will leave retval
  70. * unchanged; otherwise, equal_p is 0, so this will zero it. */
  71. retval &= equal_p;
  72. /* If v1 == v2, then this adds 0, and leaves retval unchanged.
  73. * Otherwise, we just zeroed retval, so this sets it to v1 - v2. */
  74. retval += (v1 - v2);
  75. /* There. Now retval is equal to its previous value if v1 == v2, and
  76. * equal to v1 - v2 if v1 != v2. */
  77. }
  78. return retval;
  79. #endif /* timingsafe_memcmp */
  80. }
  81. /**
  82. * Timing-safe memory comparison. Return true if the <b>sz</b> bytes at
  83. * <b>a</b> are the same as the <b>sz</b> bytes at <b>b</b>, and 0 otherwise.
  84. *
  85. * This implementation differs from !memcmp(a,b,sz) in that its timing
  86. * behavior is not data-dependent: it should return in the same amount of time
  87. * regardless of the contents of <b>a</b> and <b>b</b>. It differs from
  88. * !tor_memcmp(a,b,sz) by being faster.
  89. */
  90. int
  91. tor_memeq(const void *a, const void *b, size_t sz)
  92. {
  93. /* Treat a and b as byte ranges. */
  94. const uint8_t *ba = a, *bb = b;
  95. uint32_t any_difference = 0;
  96. while (sz--) {
  97. /* Set byte_diff to all of those bits that are different in *ba and *bb,
  98. * and advance both ba and bb. */
  99. const uint8_t byte_diff = *ba++ ^ *bb++;
  100. /* Set bits in any_difference if they are set in byte_diff. */
  101. any_difference |= byte_diff;
  102. }
  103. /* Now any_difference is 0 if there are no bits different between
  104. * a and b, and is nonzero if there are bits different between a
  105. * and b. Now for paranoia's sake, let's convert it to 0 or 1.
  106. *
  107. * (If we say "!any_difference", the compiler might get smart enough
  108. * to optimize-out our data-independence stuff above.)
  109. *
  110. * To unpack:
  111. *
  112. * If any_difference == 0:
  113. * any_difference - 1 == ~0
  114. * (any_difference - 1) >> 8 == 0x00ffffff
  115. * 1 & ((any_difference - 1) >> 8) == 1
  116. *
  117. * If any_difference != 0:
  118. * 0 < any_difference < 256, so
  119. * 0 <= any_difference - 1 < 255
  120. * (any_difference - 1) >> 8 == 0
  121. * 1 & ((any_difference - 1) >> 8) == 0
  122. */
  123. /*coverity[overflow]*/
  124. return 1 & ((any_difference - 1) >> 8);
  125. }
  126. /* Implement di_digest256_map_t as a linked list of entries. */
  127. struct di_digest256_map_t {
  128. struct di_digest256_map_t *next;
  129. uint8_t key[32];
  130. void *val;
  131. };
  132. /** Release all storage held in <b>map</b>, calling free_fn on each value
  133. * as we go. */
  134. void
  135. dimap_free(di_digest256_map_t *map, dimap_free_fn free_fn)
  136. {
  137. while (map) {
  138. di_digest256_map_t *victim = map;
  139. map = map->next;
  140. if (free_fn)
  141. free_fn(victim->val);
  142. tor_free(victim);
  143. }
  144. }
  145. /** Adjust the map at *<b>map</b>, adding an entry for <b>key</b> ->
  146. * <b>val</b>, where <b>key</b> is a DIGEST256_LEN-byte key.
  147. *
  148. * The caller MUST NOT add a key that already appears in the map.
  149. */
  150. void
  151. dimap_add_entry(di_digest256_map_t **map,
  152. const uint8_t *key, void *val)
  153. {
  154. di_digest256_map_t *new_ent;
  155. {
  156. void *old_val = dimap_search(*map, key, NULL);
  157. tor_assert(! old_val);
  158. tor_assert(val);
  159. }
  160. new_ent = tor_malloc_zero(sizeof(di_digest256_map_t));
  161. new_ent->next = *map;
  162. memcpy(new_ent->key, key, 32);
  163. new_ent->val = val;
  164. *map = new_ent;
  165. }
  166. /** Search the map at <b>map</b> for an entry whose key is <b>key</b> (a
  167. * DIGEST256_LEN-byte key) returning the corresponding value if we found one,
  168. * and returning <b>dflt_val</b> if the key wasn't found.
  169. *
  170. * This operation takes an amount of time dependent only on the length of
  171. * <b>map</b>, not on the position or presence of <b>key</b> within <b>map</b>.
  172. */
  173. void *
  174. dimap_search(const di_digest256_map_t *map, const uint8_t *key,
  175. void *dflt_val)
  176. {
  177. uintptr_t result = (uintptr_t)dflt_val;
  178. while (map) {
  179. uintptr_t r = (uintptr_t) tor_memeq(map->key, key, 32);
  180. r -= 1; /* Now r is (uintptr_t)-1 if memeq returned false, and
  181. * 0 if memeq returned true. */
  182. result &= r;
  183. result |= ((uintptr_t)(map->val)) & ~r;
  184. map = map->next;
  185. }
  186. return (void *)result;
  187. }
  188. /**
  189. * Return true iff the <b>sz</b> bytes at <b>mem</b> are all zero. Runs in
  190. * time independent of the contents of <b>mem</b>.
  191. */
  192. int
  193. safe_mem_is_zero(const void *mem, size_t sz)
  194. {
  195. uint32_t total = 0;
  196. const uint8_t *ptr = mem;
  197. while (sz--) {
  198. total |= *ptr++;
  199. }
  200. /*coverity[overflow]*/
  201. return 1 & ((total - 1) >> 8);
  202. }
  203. /** Time-invariant 64-bit greater-than; works on two integers in the range
  204. * (0,INT64_MAX). */
  205. #if SIZEOF_VOID_P == 8
  206. #define gt_i64_timei(a,b) ((a) > (b))
  207. #else
  208. static inline int
  209. gt_i64_timei(uint64_t a, uint64_t b)
  210. {
  211. int64_t diff = (int64_t) (b - a);
  212. int res = diff >> 63;
  213. return res & 1;
  214. }
  215. #endif
  216. /**
  217. * Given an array of list of <b>n_entries</b> uint64_t values, whose sum is
  218. * <b>total</b>, find the first i such that the total of all elements 0...i is
  219. * greater than rand_val.
  220. *
  221. * Try to perform this operation in a constant-time way.
  222. */
  223. int
  224. select_array_member_cumulative_timei(const uint64_t *entries, int n_entries,
  225. uint64_t total, uint64_t rand_val)
  226. {
  227. int i, i_chosen=-1, n_chosen=0;
  228. uint64_t total_so_far = 0;
  229. for (i = 0; i < n_entries; ++i) {
  230. total_so_far += entries[i];
  231. if (gt_i64_timei(total_so_far, rand_val)) {
  232. i_chosen = i;
  233. n_chosen++;
  234. /* Set rand_val to INT64_MAX rather than stopping the loop. This way,
  235. * the time we spend in the loop does not leak which element we chose. */
  236. rand_val = INT64_MAX;
  237. }
  238. }
  239. tor_assert(total_so_far == total);
  240. tor_assert(n_chosen == 1);
  241. tor_assert(i_chosen >= 0);
  242. tor_assert(i_chosen < n_entries);
  243. return i_chosen;
  244. }