Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

200 linhas
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 intrinsic(_byteswap_uint64, _byteswap_ulong)
  118. #define BSWAP8(x) _byteswap_uint64((uint64_t)(x))
  119. #define BSWAP4(x) _byteswap_ulong((uint32_t)(x))
  120. #elif defined(OPENSSL_X86)
  121. __inline uint32_t _bswap4(uint32_t val) {
  122. _asm mov eax, val
  123. _asm bswap eax
  124. }
  125. #define BSWAP4(x) _bswap4(x)
  126. #endif
  127. #endif
  128. #endif
  129. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  130. #define GETU32(p) BSWAP4(*(const uint32_t *)(p))
  131. #define PUTU32(p, v) *(uint32_t *)(p) = BSWAP4(v)
  132. #else
  133. #define GETU32(p) \
  134. ((uint32_t)(p)[0] << 24 | (uint32_t)(p)[1] << 16 | (uint32_t)(p)[2] << 8 | (uint32_t)(p)[3])
  135. #define PUTU32(p, v) \
  136. ((p)[0] = (uint8_t)((v) >> 24), (p)[1] = (uint8_t)((v) >> 16), \
  137. (p)[2] = (uint8_t)((v) >> 8), (p)[3] = (uint8_t)(v))
  138. #endif
  139. /* GCM definitions */
  140. typedef struct { uint64_t hi,lo; } u128;
  141. struct gcm128_context {
  142. /* Following 6 names follow names in GCM specification */
  143. union {
  144. uint64_t u[2];
  145. uint32_t d[4];
  146. uint8_t c[16];
  147. size_t t[16 / sizeof(size_t)];
  148. } Yi, EKi, EK0, len, Xi, H;
  149. /* Relative position of Xi, H and pre-computed Htable is used in some
  150. * assembler modules, i.e. don't change the order! */
  151. u128 Htable[16];
  152. void (*gmult)(uint64_t Xi[2], const u128 Htable[16]);
  153. void (*ghash)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  154. size_t len);
  155. unsigned int mres, ares;
  156. block128_f block;
  157. void *key;
  158. };
  159. struct xts128_context {
  160. void *key1, *key2;
  161. block128_f block1, block2;
  162. };
  163. struct ccm128_context {
  164. union {
  165. uint64_t u[2];
  166. uint8_t c[16];
  167. } nonce, cmac;
  168. uint64_t blocks;
  169. block128_f block;
  170. void *key;
  171. };
  172. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  173. /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
  174. * used. */
  175. int crypto_gcm_clmul_enabled(void);
  176. #endif
  177. #if defined(__cplusplus)
  178. } /* extern C */
  179. #endif
  180. #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */