Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

198 righe
6.7 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #ifndef OPENSSL_HEADER_MODES_INTERNAL_H
  49. #define OPENSSL_HEADER_MODES_INTERNAL_H
  50. #include <openssl/base.h>
  51. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. #define asm __asm__
  55. #define STRICT_ALIGNMENT 1
  56. #if defined(OPENSSL_X86_64) || defined(OPENSSL_X86) || defined(OPENSSL_AARCH64)
  57. #undef STRICT_ALIGNMENT
  58. #define STRICT_ALIGNMENT 0
  59. #endif
  60. #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM)
  61. #if defined(__GNUC__) && __GNUC__ >= 2
  62. #if defined(OPENSSL_X86_64)
  63. #define BSWAP8(x) \
  64. ({ \
  65. uint64_t ret = (x); \
  66. asm("bswapq %0" : "+r"(ret)); \
  67. ret; \
  68. })
  69. #define BSWAP4(x) \
  70. ({ \
  71. uint32_t ret = (x); \
  72. asm("bswapl %0" : "+r"(ret)); \
  73. ret; \
  74. })
  75. #elif defined(OPENSSL_X86)
  76. #define BSWAP8(x) \
  77. ({ \
  78. uint32_t lo = (uint64_t)(x) >> 32, hi = (x); \
  79. asm("bswapl %0; bswapl %1" : "+r"(hi), "+r"(lo)); \
  80. (uint64_t) hi << 32 | lo; \
  81. })
  82. #define BSWAP4(x) \
  83. ({ \
  84. uint32_t ret = (x); \
  85. asm("bswapl %0" : "+r"(ret)); \
  86. ret; \
  87. })
  88. #elif defined(OPENSSL_AARCH64)
  89. #define BSWAP8(x) \
  90. ({ \
  91. uint64_t ret; \
  92. asm("rev %0,%1" : "=r"(ret) : "r"(x)); \
  93. ret; \
  94. })
  95. #define BSWAP4(x) \
  96. ({ \
  97. uint32_t ret; \
  98. asm("rev %w0,%w1" : "=r"(ret) : "r"(x)); \
  99. ret; \
  100. })
  101. #elif defined(OPENSSL_ARM) && !defined(STRICT_ALIGNMENT)
  102. #define BSWAP8(x) \
  103. ({ \
  104. uint32_t lo = (uint64_t)(x) >> 32, hi = (x); \
  105. asm("rev %0,%0; rev %1,%1" : "+r"(hi), "+r"(lo)); \
  106. (uint64_t) hi << 32 | lo; \
  107. })
  108. #define BSWAP4(x) \
  109. ({ \
  110. uint32_t ret; \
  111. asm("rev %0,%1" : "=r"(ret) : "r"((uint32_t)(x))); \
  112. ret; \
  113. })
  114. #endif
  115. #elif defined(_MSC_VER)
  116. #if _MSC_VER >= 1300
  117. #pragma warning(push, 3)
  118. #include <intrin.h>
  119. #pragma warning(pop)
  120. #pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
  121. #define BSWAP8(x) _byteswap_uint64((uint64_t)(x))
  122. #define BSWAP4(x) _byteswap_ulong((uint32_t)(x))
  123. #elif defined(OPENSSL_X86)
  124. __inline uint32_t _bswap4(uint32_t val) {
  125. _asm mov eax, val
  126. _asm bswap eax
  127. }
  128. #define BSWAP4(x) _bswap4(x)
  129. #endif
  130. #endif
  131. #endif
  132. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  133. #define GETU32(p) BSWAP4(*(const uint32_t *)(p))
  134. #define PUTU32(p, v) *(uint32_t *)(p) = BSWAP4(v)
  135. #else
  136. #define GETU32(p) \
  137. ((uint32_t)(p)[0] << 24 | (uint32_t)(p)[1] << 16 | (uint32_t)(p)[2] << 8 | (uint32_t)(p)[3])
  138. #define PUTU32(p, v) \
  139. ((p)[0] = (uint8_t)((v) >> 24), (p)[1] = (uint8_t)((v) >> 16), \
  140. (p)[2] = (uint8_t)((v) >> 8), (p)[3] = (uint8_t)(v))
  141. #endif
  142. /* GCM definitions */
  143. typedef struct { uint64_t hi,lo; } u128;
  144. struct gcm128_context {
  145. /* Following 6 names follow names in GCM specification */
  146. union {
  147. uint64_t u[2];
  148. uint32_t d[4];
  149. uint8_t c[16];
  150. size_t t[16 / sizeof(size_t)];
  151. } Yi, EKi, EK0, len, Xi, H;
  152. /* Relative position of Xi, H and pre-computed Htable is used in some
  153. * assembler modules, i.e. don't change the order! */
  154. u128 Htable[16];
  155. void (*gmult)(uint64_t Xi[2], const u128 Htable[16]);
  156. void (*ghash)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  157. size_t len);
  158. unsigned int mres, ares;
  159. block128_f block;
  160. void *key;
  161. };
  162. struct ccm128_context {
  163. union {
  164. uint64_t u[2];
  165. uint8_t c[16];
  166. } nonce, cmac;
  167. uint64_t blocks;
  168. block128_f block;
  169. void *key;
  170. };
  171. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  172. /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
  173. * used. */
  174. int crypto_gcm_clmul_enabled(void);
  175. #endif
  176. #if defined(__cplusplus)
  177. } /* extern C */
  178. #endif
  179. #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */