You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

internal.h 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #ifndef OPENSSL_HEADER_CRYPTO_INTERNAL_H
  109. #define OPENSSL_HEADER_CRYPTO_INTERNAL_H
  110. #include <openssl/ex_data.h>
  111. #if defined(__cplusplus)
  112. extern "C" {
  113. #endif
  114. /* st_CRYPTO_EX_DATA_IMPL contains an ex_data implementation. See the comments
  115. * in ex_data.h for details of the behaviour of each of the functions. */
  116. struct st_CRYPTO_EX_DATA_IMPL {
  117. int (*new_class)(void);
  118. void (*cleanup)(void);
  119. int (*get_new_index)(int class_value, long argl, void *argp,
  120. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  121. CRYPTO_EX_free *free_func);
  122. int (*new_ex_data)(int class_value, void *obj, CRYPTO_EX_DATA *ad);
  123. int (*dup_ex_data)(int class_value, CRYPTO_EX_DATA *to,
  124. const CRYPTO_EX_DATA *from);
  125. void (*free_ex_data)(int class_value, void *obj, CRYPTO_EX_DATA *ad);
  126. };
  127. #if defined(_MSC_VER)
  128. #define OPENSSL_U64(x) x##UI64
  129. #else
  130. #if defined(OPENSSL_64_BIT)
  131. #define OPENSSL_U64(x) x##UL
  132. #else
  133. #define OPENSSL_U64(x) x##ULL
  134. #endif
  135. #endif /* defined(_MSC_VER) */
  136. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \
  137. defined(OPENSSL_AARCH64)
  138. /* OPENSSL_cpuid_setup initializes OPENSSL_ia32cap_P. */
  139. void OPENSSL_cpuid_setup(void);
  140. #endif
  141. #if !defined(inline)
  142. #define inline __inline
  143. #endif
  144. /* Constant-time utility functions.
  145. *
  146. * The following methods return a bitmask of all ones (0xff...f) for true and 0
  147. * for false. This is useful for choosing a value based on the result of a
  148. * conditional in constant time. For example,
  149. *
  150. * if (a < b) {
  151. * c = a;
  152. * } else {
  153. * c = b;
  154. * }
  155. *
  156. * can be written as
  157. *
  158. * unsigned int lt = constant_time_lt(a, b);
  159. * c = constant_time_select(lt, a, b); */
  160. /* constant_time_msb returns the given value with the MSB copied to all the
  161. * other bits. */
  162. static inline unsigned int constant_time_msb(unsigned int a) {
  163. return (unsigned int)((int)(a) >> (sizeof(int) * 8 - 1));
  164. }
  165. /* constant_time_lt returns 0xff..f if a < b and 0 otherwise. */
  166. static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) {
  167. /* Consider the two cases of the problem:
  168. * msb(a) == msb(b): a < b iff the MSB of a - b is set.
  169. * msb(a) != msb(b): a < b iff the MSB of b is set.
  170. *
  171. * If msb(a) == msb(b) then the following evaluates as:
  172. * msb(a^((a^b)|((a-b)^a))) ==
  173. * msb(a^((a-b) ^ a)) == (because msb(a^b) == 0)
  174. * msb(a^a^(a-b)) == (rearranging)
  175. * msb(a-b) (because ∀x. x^x == 0)
  176. *
  177. * Else, if msb(a) != msb(b) then the following evaluates as:
  178. * msb(a^((a^b)|((a-b)^a))) ==
  179. * msb(a^(𝟙 | ((a-b)^a))) == (because msb(a^b) == 1 and 𝟙
  180. * represents a value s.t. msb(𝟙) = 1)
  181. * msb(a^𝟙) == (because ORing with 1 results in 1)
  182. * msb(b)
  183. *
  184. *
  185. * Here is an SMT-LIB verification of this formula:
  186. *
  187. * (define-fun lt ((a (_ BitVec 32)) (b (_ BitVec 32))) (_ BitVec 32)
  188. * (bvxor a (bvor (bvxor a b) (bvxor (bvsub a b) a)))
  189. * )
  190. *
  191. * (declare-fun a () (_ BitVec 32))
  192. * (declare-fun b () (_ BitVec 32))
  193. *
  194. * (assert (not (= (= #x00000001 (bvlshr (lt a b) #x0000001f)) (bvult a b))))
  195. * (check-sat)
  196. * (get-model)
  197. */
  198. return constant_time_msb(a^((a^b)|((a-b)^a)));
  199. }
  200. /* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */
  201. static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) {
  202. return (uint8_t)(constant_time_lt(a, b));
  203. }
  204. /* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */
  205. static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) {
  206. return ~constant_time_lt(a, b);
  207. }
  208. /* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */
  209. static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) {
  210. return (uint8_t)(constant_time_ge(a, b));
  211. }
  212. /* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
  213. static inline unsigned int constant_time_is_zero(unsigned int a) {
  214. /* Here is an SMT-LIB verification of this formula:
  215. *
  216. * (define-fun is_zero ((a (_ BitVec 32))) (_ BitVec 32)
  217. * (bvand (bvnot a) (bvsub a #x00000001))
  218. * )
  219. *
  220. * (declare-fun a () (_ BitVec 32))
  221. *
  222. * (assert (not (= (= #x00000001 (bvlshr (is_zero a) #x0000001f)) (= a #x00000000))))
  223. * (check-sat)
  224. * (get-model)
  225. */
  226. return constant_time_msb(~a & (a - 1));
  227. }
  228. /* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit
  229. * mask. */
  230. static inline uint8_t constant_time_is_zero_8(unsigned int a) {
  231. return (uint8_t)(constant_time_is_zero(a));
  232. }
  233. /* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */
  234. static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) {
  235. return constant_time_is_zero(a ^ b);
  236. }
  237. /* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */
  238. static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) {
  239. return (uint8_t)(constant_time_eq(a, b));
  240. }
  241. /* constant_time_eq_int acts like |constant_time_eq| but works on int values. */
  242. static inline unsigned int constant_time_eq_int(int a, int b) {
  243. return constant_time_eq((unsigned)(a), (unsigned)(b));
  244. }
  245. /* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
  246. * mask. */
  247. static inline uint8_t constant_time_eq_int_8(int a, int b) {
  248. return constant_time_eq_8((unsigned)(a), (unsigned)(b));
  249. }
  250. /* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s
  251. * or all 0s (as returned by the methods above), the select methods return
  252. * either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
  253. static inline unsigned int constant_time_select(unsigned int mask,
  254. unsigned int a, unsigned int b) {
  255. return (mask & a) | (~mask & b);
  256. }
  257. /* constant_time_select_8 acts like |constant_time_select| but operates on
  258. * 8-bit values. */
  259. static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
  260. uint8_t b) {
  261. return (uint8_t)(constant_time_select(mask, a, b));
  262. }
  263. /* constant_time_select_int acts like |constant_time_select| but operates on
  264. * ints. */
  265. static inline int constant_time_select_int(unsigned int mask, int a, int b) {
  266. return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
  267. }
  268. #if defined(__cplusplus)
  269. } /* extern C */
  270. #endif
  271. #endif /* OPENSSL_HEADER_CRYPTO_INTERNAL_H */