modes.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2010-2020 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. /* TODO(3.0) Move this header into provider when dependencies are removed */
  10. #include <openssl/modes.h>
  11. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  12. typedef __int64 i64;
  13. typedef unsigned __int64 u64;
  14. # define U64(C) C##UI64
  15. #elif defined(__arch64__)
  16. typedef long i64;
  17. typedef unsigned long u64;
  18. # define U64(C) C##UL
  19. #else
  20. typedef long long i64;
  21. typedef unsigned long long u64;
  22. # define U64(C) C##ULL
  23. #endif
  24. typedef unsigned int u32;
  25. typedef unsigned char u8;
  26. #define STRICT_ALIGNMENT 1
  27. #ifndef PEDANTIC
  28. # if defined(__i386) || defined(__i386__) || \
  29. defined(__x86_64) || defined(__x86_64__) || \
  30. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
  31. defined(__aarch64__) || \
  32. defined(__s390__) || defined(__s390x__)
  33. # undef STRICT_ALIGNMENT
  34. # endif
  35. #endif
  36. #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  37. # if defined(__GNUC__) && __GNUC__>=2
  38. # if defined(__x86_64) || defined(__x86_64__)
  39. # define BSWAP8(x) ({ u64 ret_=(x); \
  40. asm ("bswapq %0" \
  41. : "+r"(ret_)); ret_; })
  42. # define BSWAP4(x) ({ u32 ret_=(x); \
  43. asm ("bswapl %0" \
  44. : "+r"(ret_)); ret_; })
  45. # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
  46. # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
  47. asm ("bswapl %0; bswapl %1" \
  48. : "+r"(hi_),"+r"(lo_)); \
  49. (u64)hi_<<32|lo_; })
  50. # define BSWAP4(x) ({ u32 ret_=(x); \
  51. asm ("bswapl %0" \
  52. : "+r"(ret_)); ret_; })
  53. # elif defined(__aarch64__)
  54. # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  55. __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
  56. # define BSWAP8(x) ({ u64 ret_; \
  57. asm ("rev %0,%1" \
  58. : "=r"(ret_) : "r"(x)); ret_; })
  59. # define BSWAP4(x) ({ u32 ret_; \
  60. asm ("rev %w0,%w1" \
  61. : "=r"(ret_) : "r"(x)); ret_; })
  62. # endif
  63. # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
  64. # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
  65. asm ("rev %0,%0; rev %1,%1" \
  66. : "+r"(hi_),"+r"(lo_)); \
  67. (u64)hi_<<32|lo_; })
  68. # define BSWAP4(x) ({ u32 ret_; \
  69. asm ("rev %0,%1" \
  70. : "=r"(ret_) : "r"((u32)(x))); \
  71. ret_; })
  72. # endif
  73. # elif defined(_MSC_VER)
  74. # if _MSC_VER>=1300
  75. # include <stdlib.h>
  76. # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
  77. # define BSWAP8(x) _byteswap_uint64((u64)(x))
  78. # define BSWAP4(x) _byteswap_ulong((u32)(x))
  79. # elif defined(_M_IX86)
  80. __inline u32 _bswap4(u32 val)
  81. {
  82. _asm mov eax, val _asm bswap eax}
  83. # define BSWAP4(x) _bswap4(x)
  84. # endif
  85. # endif
  86. #endif
  87. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  88. # define GETU32(p) BSWAP4(*(const u32 *)(p))
  89. # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
  90. #else
  91. # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
  92. # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
  93. #endif
  94. /*- GCM definitions */ typedef struct {
  95. u64 hi, lo;
  96. } u128;
  97. #ifdef TABLE_BITS
  98. # undef TABLE_BITS
  99. #endif
  100. /*
  101. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  102. * never be set to 8 [or 1]. For further information see gcm128.c.
  103. */
  104. #define TABLE_BITS 4
  105. struct gcm128_context {
  106. /* Following 6 names follow names in GCM specification */
  107. union {
  108. u64 u[2];
  109. u32 d[4];
  110. u8 c[16];
  111. size_t t[16 / sizeof(size_t)];
  112. } Yi, EKi, EK0, len, Xi, H;
  113. /*
  114. * Relative position of Xi, H and pre-computed Htable is used in some
  115. * assembler modules, i.e. don't change the order!
  116. */
  117. #if TABLE_BITS==8
  118. u128 Htable[256];
  119. #else
  120. u128 Htable[16];
  121. void (*gmult) (u64 Xi[2], const u128 Htable[16]);
  122. void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
  123. size_t len);
  124. #endif
  125. unsigned int mres, ares;
  126. block128_f block;
  127. void *key;
  128. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  129. unsigned char Xn[48];
  130. #endif
  131. };
  132. /*
  133. * The maximum permitted number of cipher blocks per data unit in XTS mode.
  134. * Reference IEEE Std 1619-2018.
  135. */
  136. #define XTS_MAX_BLOCKS_PER_DATA_UNIT (1<<20)
  137. struct xts128_context {
  138. void *key1, *key2;
  139. block128_f block1, block2;
  140. };
  141. struct ccm128_context {
  142. union {
  143. u64 u[2];
  144. u8 c[16];
  145. } nonce, cmac;
  146. u64 blocks;
  147. block128_f block;
  148. void *key;
  149. };
  150. #ifndef OPENSSL_NO_OCB
  151. typedef union {
  152. u64 a[2];
  153. unsigned char c[16];
  154. } OCB_BLOCK;
  155. # define ocb_block16_xor(in1,in2,out) \
  156. ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
  157. (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
  158. # if STRICT_ALIGNMENT
  159. # define ocb_block16_xor_misaligned(in1,in2,out) \
  160. ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
  161. # else
  162. # define ocb_block16_xor_misaligned ocb_block16_xor
  163. # endif
  164. struct ocb128_context {
  165. /* Need both encrypt and decrypt key schedules for decryption */
  166. block128_f encrypt;
  167. block128_f decrypt;
  168. void *keyenc;
  169. void *keydec;
  170. ocb128_f stream; /* direction dependent */
  171. /* Key dependent variables. Can be reused if key remains the same */
  172. size_t l_index;
  173. size_t max_l_index;
  174. OCB_BLOCK l_star;
  175. OCB_BLOCK l_dollar;
  176. OCB_BLOCK *l;
  177. /* Must be reset for each session */
  178. struct {
  179. u64 blocks_hashed;
  180. u64 blocks_processed;
  181. OCB_BLOCK offset_aad;
  182. OCB_BLOCK sum;
  183. OCB_BLOCK offset;
  184. OCB_BLOCK checksum;
  185. } sess;
  186. };
  187. #endif /* OPENSSL_NO_OCB */
  188. #ifndef OPENSSL_NO_SIV
  189. #define SIV_LEN 16
  190. typedef union siv_block_u {
  191. uint64_t word[SIV_LEN/sizeof(uint64_t)];
  192. unsigned char byte[SIV_LEN];
  193. } SIV_BLOCK;
  194. struct siv128_context {
  195. /* d stores intermediate results of S2V; it corresponds to D from the
  196. pseudocode in section 2.4 of RFC 5297. */
  197. SIV_BLOCK d;
  198. SIV_BLOCK tag;
  199. EVP_CIPHER_CTX *cipher_ctx;
  200. EVP_MAC *mac;
  201. EVP_MAC_CTX *mac_ctx_init;
  202. int final_ret;
  203. int crypto_ok;
  204. };
  205. #endif /* OPENSSL_NO_SIV */