modes_lcl.h 5.9 KB

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