modes_local.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/modes.h>
  10. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  11. typedef __int64 i64;
  12. typedef unsigned __int64 u64;
  13. # define U64(C) C##UI64
  14. #elif defined(__arch64__)
  15. typedef long i64;
  16. typedef unsigned long u64;
  17. # define U64(C) C##UL
  18. #else
  19. typedef long long i64;
  20. typedef unsigned long long u64;
  21. # define U64(C) C##ULL
  22. #endif
  23. typedef unsigned int u32;
  24. typedef unsigned char u8;
  25. #define STRICT_ALIGNMENT 1
  26. #ifndef PEDANTIC
  27. # if defined(__i386) || defined(__i386__) || \
  28. defined(__x86_64) || defined(__x86_64__) || \
  29. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
  30. defined(__aarch64__) || \
  31. defined(__s390__) || defined(__s390x__)
  32. # undef STRICT_ALIGNMENT
  33. # endif
  34. #endif
  35. #ifndef STRICT_ALIGNMENT
  36. # ifdef __GNUC__
  37. typedef u32 u32_a1 __attribute((__aligned__(1)));
  38. # else
  39. typedef u32 u32_a1;
  40. # endif
  41. #endif
  42. #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  43. # if defined(__GNUC__) && __GNUC__>=2
  44. # if defined(__x86_64) || defined(__x86_64__)
  45. # define BSWAP8(x) ({ u64 ret_=(x); \
  46. asm ("bswapq %0" \
  47. : "+r"(ret_)); ret_; })
  48. # define BSWAP4(x) ({ u32 ret_=(x); \
  49. asm ("bswapl %0" \
  50. : "+r"(ret_)); ret_; })
  51. # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
  52. # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
  53. asm ("bswapl %0; bswapl %1" \
  54. : "+r"(hi_),"+r"(lo_)); \
  55. (u64)hi_<<32|lo_; })
  56. # define BSWAP4(x) ({ u32 ret_=(x); \
  57. asm ("bswapl %0" \
  58. : "+r"(ret_)); ret_; })
  59. # elif defined(__aarch64__)
  60. # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  61. __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
  62. # define BSWAP8(x) ({ u64 ret_; \
  63. asm ("rev %0,%1" \
  64. : "=r"(ret_) : "r"(x)); ret_; })
  65. # define BSWAP4(x) ({ u32 ret_; \
  66. asm ("rev %w0,%w1" \
  67. : "=r"(ret_) : "r"(x)); ret_; })
  68. # endif
  69. # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
  70. # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
  71. asm ("rev %0,%0; rev %1,%1" \
  72. : "+r"(hi_),"+r"(lo_)); \
  73. (u64)hi_<<32|lo_; })
  74. # define BSWAP4(x) ({ u32 ret_; \
  75. asm ("rev %0,%1" \
  76. : "=r"(ret_) : "r"((u32)(x))); \
  77. ret_; })
  78. # endif
  79. # elif defined(_MSC_VER)
  80. # if _MSC_VER>=1300
  81. # include <stdlib.h>
  82. # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
  83. # define BSWAP8(x) _byteswap_uint64((u64)(x))
  84. # define BSWAP4(x) _byteswap_ulong((u32)(x))
  85. # elif defined(_M_IX86)
  86. __inline u32 _bswap4(u32 val)
  87. {
  88. _asm mov eax, val _asm bswap eax}
  89. # define BSWAP4(x) _bswap4(x)
  90. # endif
  91. # endif
  92. #endif
  93. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  94. # define GETU32(p) BSWAP4(*(const u32_a1 *)(p))
  95. # define PUTU32(p,v) *(u32_a1 *)(p) = BSWAP4(v)
  96. #else
  97. # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
  98. # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
  99. #endif
  100. /*- GCM definitions */ typedef struct {
  101. u64 hi, lo;
  102. } u128;
  103. #ifdef TABLE_BITS
  104. # undef TABLE_BITS
  105. #endif
  106. /*
  107. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  108. * never be set to 8 [or 1]. For further information see gcm128.c.
  109. */
  110. #define TABLE_BITS 4
  111. struct gcm128_context {
  112. /* Following 6 names follow names in GCM specification */
  113. union {
  114. u64 u[2];
  115. u32 d[4];
  116. u8 c[16];
  117. size_t t[16 / sizeof(size_t)];
  118. } Yi, EKi, EK0, len, Xi, H;
  119. /*
  120. * Relative position of Xi, H and pre-computed Htable is used in some
  121. * assembler modules, i.e. don't change the order!
  122. */
  123. #if TABLE_BITS==8
  124. u128 Htable[256];
  125. #else
  126. u128 Htable[16];
  127. void (*gmult) (u64 Xi[2], const u128 Htable[16]);
  128. void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
  129. size_t len);
  130. #endif
  131. unsigned int mres, ares;
  132. block128_f block;
  133. void *key;
  134. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  135. unsigned char Xn[48];
  136. #endif
  137. };
  138. struct xts128_context {
  139. void *key1, *key2;
  140. block128_f block1, block2;
  141. };
  142. struct ccm128_context {
  143. union {
  144. u64 u[2];
  145. u8 c[16];
  146. } nonce, cmac;
  147. u64 blocks;
  148. block128_f block;
  149. void *key;
  150. };
  151. #ifndef OPENSSL_NO_OCB
  152. typedef union {
  153. u64 a[2];
  154. unsigned char c[16];
  155. } OCB_BLOCK;
  156. # define ocb_block16_xor(in1,in2,out) \
  157. ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
  158. (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
  159. # if STRICT_ALIGNMENT
  160. # define ocb_block16_xor_misaligned(in1,in2,out) \
  161. ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
  162. # else
  163. # define ocb_block16_xor_misaligned ocb_block16_xor
  164. # endif
  165. struct ocb128_context {
  166. /* Need both encrypt and decrypt key schedules for decryption */
  167. block128_f encrypt;
  168. block128_f decrypt;
  169. void *keyenc;
  170. void *keydec;
  171. ocb128_f stream; /* direction dependent */
  172. /* Key dependent variables. Can be reused if key remains the same */
  173. size_t l_index;
  174. size_t max_l_index;
  175. OCB_BLOCK l_star;
  176. OCB_BLOCK l_dollar;
  177. OCB_BLOCK *l;
  178. /* Must be reset for each session */
  179. struct {
  180. u64 blocks_hashed;
  181. u64 blocks_processed;
  182. OCB_BLOCK offset_aad;
  183. OCB_BLOCK sum;
  184. OCB_BLOCK offset;
  185. OCB_BLOCK checksum;
  186. } sess;
  187. };
  188. #endif /* OPENSSL_NO_OCB */