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

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