modes_lcl.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2010-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  125. unsigned char Xn[48];
  126. #endif
  127. };
  128. /*
  129. * The maximum permitted number of cipher blocks per data unit in XTS mode.
  130. * Reference IEEE Std 1619-2018.
  131. */
  132. #define XTS_MAX_BLOCKS_PER_DATA_UNIT (1<<20)
  133. struct xts128_context {
  134. void *key1, *key2;
  135. block128_f block1, block2;
  136. };
  137. struct ccm128_context {
  138. union {
  139. u64 u[2];
  140. u8 c[16];
  141. } nonce, cmac;
  142. u64 blocks;
  143. block128_f block;
  144. void *key;
  145. };
  146. #ifndef OPENSSL_NO_OCB
  147. typedef union {
  148. u64 a[2];
  149. unsigned char c[16];
  150. } OCB_BLOCK;
  151. # define ocb_block16_xor(in1,in2,out) \
  152. ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
  153. (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
  154. # if STRICT_ALIGNMENT
  155. # define ocb_block16_xor_misaligned(in1,in2,out) \
  156. ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
  157. # else
  158. # define ocb_block16_xor_misaligned ocb_block16_xor
  159. # endif
  160. struct ocb128_context {
  161. /* Need both encrypt and decrypt key schedules for decryption */
  162. block128_f encrypt;
  163. block128_f decrypt;
  164. void *keyenc;
  165. void *keydec;
  166. ocb128_f stream; /* direction dependent */
  167. /* Key dependent variables. Can be reused if key remains the same */
  168. size_t l_index;
  169. size_t max_l_index;
  170. OCB_BLOCK l_star;
  171. OCB_BLOCK l_dollar;
  172. OCB_BLOCK *l;
  173. /* Must be reset for each session */
  174. struct {
  175. u64 blocks_hashed;
  176. u64 blocks_processed;
  177. OCB_BLOCK offset_aad;
  178. OCB_BLOCK sum;
  179. OCB_BLOCK offset;
  180. OCB_BLOCK checksum;
  181. } sess;
  182. };
  183. #endif /* OPENSSL_NO_OCB */
  184. #ifndef OPENSSL_NO_SIV
  185. #include <openssl/cmac.h>
  186. #define SIV_LEN 16
  187. typedef union siv_block_u {
  188. uint64_t word[SIV_LEN/sizeof(uint64_t)];
  189. unsigned char byte[SIV_LEN];
  190. } SIV_BLOCK;
  191. struct siv128_context {
  192. /* d stores intermediate results of S2V; it corresponds to D from the
  193. pseudocode in section 2.4 of RFC 5297. */
  194. SIV_BLOCK d;
  195. SIV_BLOCK tag;
  196. EVP_CIPHER_CTX *cipher_ctx;
  197. EVP_MAC_CTX *mac_ctx_init;
  198. int final_ret;
  199. int crypto_ok;
  200. };
  201. #endif /* OPENSSL_NO_SIV */