di_ops.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2011-2015, 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. const uint8_t *x = a;
  26. const uint8_t *y = b;
  27. size_t i = len;
  28. int retval = 0;
  29. /* This loop goes from the end of the arrays to the start. At the
  30. * start of every iteration, before we decrement i, we have set
  31. * "retval" equal to the result of memcmp(a+i,b+i,len-i). During the
  32. * loop, we update retval by leaving it unchanged if x[i]==y[i] and
  33. * setting it to x[i]-y[i] if x[i]!= y[i].
  34. *
  35. * The following assumes we are on a system with two's-complement
  36. * arithmetic. We check for this at configure-time with the check
  37. * that sets USING_TWOS_COMPLEMENT. If we aren't two's complement, then
  38. * torint.h will stop compilation with an error.
  39. */
  40. while (i--) {
  41. int v1 = x[i];
  42. int v2 = y[i];
  43. int equal_p = v1 ^ v2;
  44. /* The following sets bits 8 and above of equal_p to 'equal_p ==
  45. * 0', and thus to v1 == v2. (To see this, note that if v1 ==
  46. * v2, then v1^v2 == equal_p == 0, so equal_p-1 == -1, which is the
  47. * same as ~0 on a two's-complement machine. Then note that if
  48. * v1 != v2, then 0 < v1 ^ v2 < 256, so 0 <= equal_p - 1 < 255.)
  49. */
  50. --equal_p;
  51. equal_p >>= 8;
  52. /* Thanks to (sign-preserving) arithmetic shift, equal_p is now
  53. * equal to -(v1 == v2), which is exactly what we need below.
  54. * (Since we're assuming two's-complement arithmetic, -1 is the
  55. * same as ~0 (all bits set).)
  56. *
  57. * (The result of an arithmetic shift on a negative value is
  58. * actually implementation-defined in standard C. So how do we
  59. * get away with assuming it? Easy. We check.) */
  60. #if ((-60 >> 8) != -1)
  61. #error "According to cpp, right-shift doesn't perform sign-extension."
  62. #endif
  63. #ifndef RSHIFT_DOES_SIGN_EXTEND
  64. #error "According to configure, right-shift doesn't perform sign-extension."
  65. #endif
  66. /* If v1 == v2, equal_p is ~0, so this will leave retval
  67. * unchanged; otherwise, equal_p is 0, so this will zero it. */
  68. retval &= equal_p;
  69. /* If v1 == v2, then this adds 0, and leaves retval unchanged.
  70. * Otherwise, we just zeroed retval, so this sets it to v1 - v2. */
  71. retval += (v1 - v2);
  72. /* There. Now retval is equal to its previous value if v1 == v2, and
  73. * equal to v1 - v2 if v1 != v2. */
  74. }
  75. return retval;
  76. }
  77. /**
  78. * Timing-safe memory comparison. Return true if the <b>sz</b> bytes at
  79. * <b>a</b> are the same as the <b>sz</b> bytes at <b>b</b>, and 0 otherwise.
  80. *
  81. * This implementation differs from !memcmp(a,b,sz) in that its timing
  82. * behavior is not data-dependent: it should return in the same amount of time
  83. * regardless of the contents of <b>a</b> and <b>b</b>. It differs from
  84. * !tor_memcmp(a,b,sz) by being faster.
  85. */
  86. int
  87. tor_memeq(const void *a, const void *b, size_t sz)
  88. {
  89. /* Treat a and b as byte ranges. */
  90. const uint8_t *ba = a, *bb = b;
  91. uint32_t any_difference = 0;
  92. while (sz--) {
  93. /* Set byte_diff to all of those bits that are different in *ba and *bb,
  94. * and advance both ba and bb. */
  95. const uint8_t byte_diff = *ba++ ^ *bb++;
  96. /* Set bits in any_difference if they are set in byte_diff. */
  97. any_difference |= byte_diff;
  98. }
  99. /* Now any_difference is 0 if there are no bits different between
  100. * a and b, and is nonzero if there are bits different between a
  101. * and b. Now for paranoia's sake, let's convert it to 0 or 1.
  102. *
  103. * (If we say "!any_difference", the compiler might get smart enough
  104. * to optimize-out our data-independence stuff above.)
  105. *
  106. * To unpack:
  107. *
  108. * If any_difference == 0:
  109. * any_difference - 1 == ~0
  110. * (any_difference - 1) >> 8 == 0x00ffffff
  111. * 1 & ((any_difference - 1) >> 8) == 1
  112. *
  113. * If any_difference != 0:
  114. * 0 < any_difference < 256, so
  115. * 0 <= any_difference - 1 < 255
  116. * (any_difference - 1) >> 8 == 0
  117. * 1 & ((any_difference - 1) >> 8) == 0
  118. */
  119. /*coverity[overflow]*/
  120. return 1 & ((any_difference - 1) >> 8);
  121. }
  122. /* Implement di_digest256_map_t as a linked list of entries. */
  123. struct di_digest256_map_t {
  124. struct di_digest256_map_t *next;
  125. uint8_t key[32];
  126. void *val;
  127. };
  128. /** Release all storage held in <b>map</b>, calling free_fn on each value
  129. * as we go. */
  130. void
  131. dimap_free(di_digest256_map_t *map, dimap_free_fn free_fn)
  132. {
  133. while (map) {
  134. di_digest256_map_t *victim = map;
  135. map = map->next;
  136. if (free_fn)
  137. free_fn(victim->val);
  138. tor_free(victim);
  139. }
  140. }
  141. /** Adjust the map at *<b>map</b>, adding an entry for <b>key</b> ->
  142. * <b>val</b>, where <b>key</b> is a DIGEST256_LEN-byte key.
  143. *
  144. * The caller MUST NOT add a key that already appears in the map.
  145. */
  146. void
  147. dimap_add_entry(di_digest256_map_t **map,
  148. const uint8_t *key, void *val)
  149. {
  150. di_digest256_map_t *new_ent;
  151. {
  152. void *old_val = dimap_search(*map, key, NULL);
  153. tor_assert(! old_val);
  154. tor_assert(val);
  155. }
  156. new_ent = tor_malloc_zero(sizeof(di_digest256_map_t));
  157. new_ent->next = *map;
  158. memcpy(new_ent->key, key, 32);
  159. new_ent->val = val;
  160. *map = new_ent;
  161. }
  162. /** Search the map at <b>map</b> for an entry whose key is <b>key</b> (a
  163. * DIGEST256_LEN-byte key) returning the corresponding value if we found one,
  164. * and returning <b>dflt_val</b> if the key wasn't found.
  165. *
  166. * This operation takes an amount of time dependent only on the length of
  167. * <b>map</b>, not on the position or presence of <b>key</b> within <b>map</b>.
  168. */
  169. void *
  170. dimap_search(const di_digest256_map_t *map, const uint8_t *key,
  171. void *dflt_val)
  172. {
  173. uintptr_t result = (uintptr_t)dflt_val;
  174. while (map) {
  175. uintptr_t r = (uintptr_t) tor_memeq(map->key, key, 32);
  176. r -= 1; /* Now r is (uintptr_t)-1 if memeq returned false, and
  177. * 0 if memeq returned true. */
  178. result &= r;
  179. result |= ((uintptr_t)(map->val)) & ~r;
  180. map = map->next;
  181. }
  182. return (void *)result;
  183. }
  184. /**
  185. * Return true iff the <b>sz</b> bytes at <b>mem</b> are all zero. Runs in
  186. * time independent of the contents of <b>mem</b>.
  187. */
  188. int
  189. safe_mem_is_zero(const void *mem, size_t sz)
  190. {
  191. uint32_t total = 0;
  192. const uint8_t *ptr = mem;
  193. while (sz--) {
  194. total |= *ptr++;
  195. }
  196. /*coverity[overflow]*/
  197. return 1 & ((total - 1) >> 8);
  198. }