0002-Add-helper-functions-for-constant-time-operations.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. From 6e34f618d37ddbb5854c42e2ad4fca83492fa7b7 Mon Sep 17 00:00:00 2001
  2. From: Jouni Malinen <jouni@codeaurora.org>
  3. Date: Wed, 27 Feb 2019 18:38:30 +0200
  4. Subject: [PATCH 02/14] Add helper functions for constant time operations
  5. These functions can be used to help implement constant time operations
  6. for various cryptographic operations that must minimize externally
  7. observable differences in processing (both in timing and also in
  8. internal cache use, etc.).
  9. This is related to CVE-2019-9494 and CVE-2019-9495.
  10. Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
  11. ---
  12. src/utils/const_time.h | 191 +++++++++++++++++++++++++++++++++++++++++++++++++
  13. 1 file changed, 191 insertions(+)
  14. create mode 100644 src/utils/const_time.h
  15. diff --git a/src/utils/const_time.h b/src/utils/const_time.h
  16. new file mode 100644
  17. index 0000000..ab8f611
  18. --- /dev/null
  19. +++ b/src/utils/const_time.h
  20. @@ -0,0 +1,191 @@
  21. +/*
  22. + * Helper functions for constant time operations
  23. + * Copyright (c) 2019, The Linux Foundation
  24. + *
  25. + * This software may be distributed under the terms of the BSD license.
  26. + * See README for more details.
  27. + *
  28. + * These helper functions can be used to implement logic that needs to minimize
  29. + * externally visible differences in execution path by avoiding use of branches,
  30. + * avoiding early termination or other time differences, and forcing same memory
  31. + * access pattern regardless of values.
  32. + */
  33. +
  34. +#ifndef CONST_TIME_H
  35. +#define CONST_TIME_H
  36. +
  37. +
  38. +#if defined(__clang__)
  39. +#define NO_UBSAN_UINT_OVERFLOW \
  40. + __attribute__((no_sanitize("unsigned-integer-overflow")))
  41. +#else
  42. +#define NO_UBSAN_UINT_OVERFLOW
  43. +#endif
  44. +
  45. +
  46. +/**
  47. + * const_time_fill_msb - Fill all bits with MSB value
  48. + * @val: Input value
  49. + * Returns: Value with all the bits set to the MSB of the input val
  50. + */
  51. +static inline unsigned int const_time_fill_msb(unsigned int val)
  52. +{
  53. + /* Move the MSB to LSB and multiple by -1 to fill in all bits. */
  54. + return (val >> (sizeof(val) * 8 - 1)) * ~0U;
  55. +}
  56. +
  57. +
  58. +/* Returns: -1 if val is zero; 0 if val is not zero */
  59. +static inline unsigned int const_time_is_zero(unsigned int val)
  60. + NO_UBSAN_UINT_OVERFLOW
  61. +{
  62. + /* Set MSB to 1 for 0 and fill rest of bits with the MSB value */
  63. + return const_time_fill_msb(~val & (val - 1));
  64. +}
  65. +
  66. +
  67. +/* Returns: -1 if a == b; 0 if a != b */
  68. +static inline unsigned int const_time_eq(unsigned int a, unsigned int b)
  69. +{
  70. + return const_time_is_zero(a ^ b);
  71. +}
  72. +
  73. +
  74. +/* Returns: -1 if a == b; 0 if a != b */
  75. +static inline u8 const_time_eq_u8(unsigned int a, unsigned int b)
  76. +{
  77. + return (u8) const_time_eq(a, b);
  78. +}
  79. +
  80. +
  81. +/**
  82. + * const_time_eq_bin - Constant time memory comparison
  83. + * @a: First buffer to compare
  84. + * @b: Second buffer to compare
  85. + * @len: Number of octets to compare
  86. + * Returns: -1 if buffers are equal, 0 if not
  87. + *
  88. + * This function is meant for comparing passwords or hash values where
  89. + * difference in execution time or memory access pattern could provide external
  90. + * observer information about the location of the difference in the memory
  91. + * buffers. The return value does not behave like memcmp(), i.e.,
  92. + * const_time_eq_bin() cannot be used to sort items into a defined order. Unlike
  93. + * memcmp(), the execution time of const_time_eq_bin() does not depend on the
  94. + * contents of the compared memory buffers, but only on the total compared
  95. + * length.
  96. + */
  97. +static inline unsigned int const_time_eq_bin(const void *a, const void *b,
  98. + size_t len)
  99. +{
  100. + const u8 *aa = a;
  101. + const u8 *bb = b;
  102. + size_t i;
  103. + u8 res = 0;
  104. +
  105. + for (i = 0; i < len; i++)
  106. + res |= aa[i] ^ bb[i];
  107. +
  108. + return const_time_is_zero(res);
  109. +}
  110. +
  111. +
  112. +/**
  113. + * const_time_select - Constant time unsigned int selection
  114. + * @mask: 0 (false) or -1 (true) to identify which value to select
  115. + * @true_val: Value to select for the true case
  116. + * @false_val: Value to select for the false case
  117. + * Returns: true_val if mask == -1, false_val if mask == 0
  118. + */
  119. +static inline unsigned int const_time_select(unsigned int mask,
  120. + unsigned int true_val,
  121. + unsigned int false_val)
  122. +{
  123. + return (mask & true_val) | (~mask & false_val);
  124. +}
  125. +
  126. +
  127. +/**
  128. + * const_time_select_int - Constant time int selection
  129. + * @mask: 0 (false) or -1 (true) to identify which value to select
  130. + * @true_val: Value to select for the true case
  131. + * @false_val: Value to select for the false case
  132. + * Returns: true_val if mask == -1, false_val if mask == 0
  133. + */
  134. +static inline int const_time_select_int(unsigned int mask, int true_val,
  135. + int false_val)
  136. +{
  137. + return (int) const_time_select(mask, (unsigned int) true_val,
  138. + (unsigned int) false_val);
  139. +}
  140. +
  141. +
  142. +/**
  143. + * const_time_select_u8 - Constant time u8 selection
  144. + * @mask: 0 (false) or -1 (true) to identify which value to select
  145. + * @true_val: Value to select for the true case
  146. + * @false_val: Value to select for the false case
  147. + * Returns: true_val if mask == -1, false_val if mask == 0
  148. + */
  149. +static inline u8 const_time_select_u8(u8 mask, u8 true_val, u8 false_val)
  150. +{
  151. + return (u8) const_time_select(mask, true_val, false_val);
  152. +}
  153. +
  154. +
  155. +/**
  156. + * const_time_select_s8 - Constant time s8 selection
  157. + * @mask: 0 (false) or -1 (true) to identify which value to select
  158. + * @true_val: Value to select for the true case
  159. + * @false_val: Value to select for the false case
  160. + * Returns: true_val if mask == -1, false_val if mask == 0
  161. + */
  162. +static inline s8 const_time_select_s8(u8 mask, s8 true_val, s8 false_val)
  163. +{
  164. + return (s8) const_time_select(mask, (unsigned int) true_val,
  165. + (unsigned int) false_val);
  166. +}
  167. +
  168. +
  169. +/**
  170. + * const_time_select_bin - Constant time binary buffer selection copy
  171. + * @mask: 0 (false) or -1 (true) to identify which value to copy
  172. + * @true_val: Buffer to copy for the true case
  173. + * @false_val: Buffer to copy for the false case
  174. + * @len: Number of octets to copy
  175. + * @dst: Destination buffer for the copy
  176. + *
  177. + * This function copies the specified buffer into the destination buffer using
  178. + * operations with identical memory access pattern regardless of which buffer
  179. + * is being copied.
  180. + */
  181. +static inline void const_time_select_bin(u8 mask, const u8 *true_val,
  182. + const u8 *false_val, size_t len,
  183. + u8 *dst)
  184. +{
  185. + size_t i;
  186. +
  187. + for (i = 0; i < len; i++)
  188. + dst[i] = const_time_select_u8(mask, true_val[i], false_val[i]);
  189. +}
  190. +
  191. +
  192. +static inline int const_time_memcmp(const void *a, const void *b, size_t len)
  193. +{
  194. + const u8 *aa = a;
  195. + const u8 *bb = b;
  196. + int diff, res = 0;
  197. + unsigned int mask;
  198. +
  199. + if (len == 0)
  200. + return 0;
  201. + do {
  202. + len--;
  203. + diff = (int) aa[len] - (int) bb[len];
  204. + mask = const_time_is_zero((unsigned int) diff);
  205. + res = const_time_select_int(mask, res, diff);
  206. + } while (len);
  207. +
  208. + return res;
  209. +}
  210. +
  211. +#endif /* CONST_TIME_H */
  212. --
  213. 2.7.4