modes_local.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. # define BSWAP8(x) ({ u64 ret_; \
  61. asm ("rev %0,%1" \
  62. : "=r"(ret_) : "r"(x)); ret_; })
  63. # define BSWAP4(x) ({ u32 ret_; \
  64. asm ("rev %w0,%w1" \
  65. : "=r"(ret_) : "r"(x)); ret_; })
  66. # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
  67. # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
  68. asm ("rev %0,%0; rev %1,%1" \
  69. : "+r"(hi_),"+r"(lo_)); \
  70. (u64)hi_<<32|lo_; })
  71. # define BSWAP4(x) ({ u32 ret_; \
  72. asm ("rev %0,%1" \
  73. : "=r"(ret_) : "r"((u32)(x))); \
  74. ret_; })
  75. # endif
  76. # elif defined(_MSC_VER)
  77. # if _MSC_VER>=1300
  78. # include <stdlib.h>
  79. # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
  80. # define BSWAP8(x) _byteswap_uint64((u64)(x))
  81. # define BSWAP4(x) _byteswap_ulong((u32)(x))
  82. # elif defined(_M_IX86)
  83. __inline u32 _bswap4(u32 val)
  84. {
  85. _asm mov eax, val _asm bswap eax}
  86. # define BSWAP4(x) _bswap4(x)
  87. # endif
  88. # endif
  89. #endif
  90. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  91. # define GETU32(p) BSWAP4(*(const u32_a1 *)(p))
  92. # define PUTU32(p,v) *(u32_a1 *)(p) = BSWAP4(v)
  93. #else
  94. # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
  95. # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
  96. #endif
  97. /*- GCM definitions */ typedef struct {
  98. u64 hi, lo;
  99. } u128;
  100. #ifdef TABLE_BITS
  101. # undef TABLE_BITS
  102. #endif
  103. /*
  104. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  105. * never be set to 8 [or 1]. For further information see gcm128.c.
  106. */
  107. #define TABLE_BITS 4
  108. struct gcm128_context {
  109. /* Following 6 names follow names in GCM specification */
  110. union {
  111. u64 u[2];
  112. u32 d[4];
  113. u8 c[16];
  114. size_t t[16 / sizeof(size_t)];
  115. } Yi, EKi, EK0, len, Xi, H;
  116. /*
  117. * Relative position of Xi, H and pre-computed Htable is used in some
  118. * assembler modules, i.e. don't change the order!
  119. */
  120. #if TABLE_BITS==8
  121. u128 Htable[256];
  122. #else
  123. u128 Htable[16];
  124. void (*gmult) (u64 Xi[2], const u128 Htable[16]);
  125. void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
  126. size_t len);
  127. #endif
  128. unsigned int mres, ares;
  129. block128_f block;
  130. void *key;
  131. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  132. unsigned char Xn[48];
  133. #endif
  134. };
  135. struct xts128_context {
  136. void *key1, *key2;
  137. block128_f block1, block2;
  138. };
  139. struct ccm128_context {
  140. union {
  141. u64 u[2];
  142. u8 c[16];
  143. } nonce, cmac;
  144. u64 blocks;
  145. block128_f block;
  146. void *key;
  147. };
  148. #ifndef OPENSSL_NO_OCB
  149. typedef union {
  150. u64 a[2];
  151. unsigned char c[16];
  152. } OCB_BLOCK;
  153. # define ocb_block16_xor(in1,in2,out) \
  154. ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
  155. (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
  156. # if STRICT_ALIGNMENT
  157. # define ocb_block16_xor_misaligned(in1,in2,out) \
  158. ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
  159. # else
  160. # define ocb_block16_xor_misaligned ocb_block16_xor
  161. # endif
  162. struct ocb128_context {
  163. /* Need both encrypt and decrypt key schedules for decryption */
  164. block128_f encrypt;
  165. block128_f decrypt;
  166. void *keyenc;
  167. void *keydec;
  168. ocb128_f stream; /* direction dependent */
  169. /* Key dependent variables. Can be reused if key remains the same */
  170. size_t l_index;
  171. size_t max_l_index;
  172. OCB_BLOCK l_star;
  173. OCB_BLOCK l_dollar;
  174. OCB_BLOCK *l;
  175. /* Must be reset for each session */
  176. struct {
  177. u64 blocks_hashed;
  178. u64 blocks_processed;
  179. OCB_BLOCK offset_aad;
  180. OCB_BLOCK sum;
  181. OCB_BLOCK offset;
  182. OCB_BLOCK checksum;
  183. } sess;
  184. };
  185. #endif /* OPENSSL_NO_OCB */