2 * Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* TODO(3.0) Move this header into provider when dependencies are removed */
11 #include <openssl/modes.h>
13 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
15 typedef unsigned __int64 u64;
16 # define U64(C) C##UI64
17 #elif defined(__arch64__)
19 typedef unsigned long u64;
22 typedef long long i64;
23 typedef unsigned long long u64;
24 # define U64(C) C##ULL
27 typedef unsigned int u32;
28 typedef unsigned char u8;
30 #define STRICT_ALIGNMENT 1
32 # if defined(__i386) || defined(__i386__) || \
33 defined(__x86_64) || defined(__x86_64__) || \
34 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
35 defined(__aarch64__) || \
36 defined(__s390__) || defined(__s390x__)
37 # undef STRICT_ALIGNMENT
41 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
42 # if defined(__GNUC__) && __GNUC__>=2
43 # if defined(__x86_64) || defined(__x86_64__)
44 # define BSWAP8(x) ({ u64 ret_=(x); \
46 : "+r"(ret_)); ret_; })
47 # define BSWAP4(x) ({ u32 ret_=(x); \
49 : "+r"(ret_)); ret_; })
50 # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
51 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
52 asm ("bswapl %0; bswapl %1" \
53 : "+r"(hi_),"+r"(lo_)); \
55 # define BSWAP4(x) ({ u32 ret_=(x); \
57 : "+r"(ret_)); ret_; })
58 # elif defined(__aarch64__)
59 # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
60 __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
61 # define BSWAP8(x) ({ u64 ret_; \
63 : "=r"(ret_) : "r"(x)); ret_; })
64 # define BSWAP4(x) ({ u32 ret_; \
66 : "=r"(ret_) : "r"(x)); ret_; })
68 # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
69 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
70 asm ("rev %0,%0; rev %1,%1" \
71 : "+r"(hi_),"+r"(lo_)); \
73 # define BSWAP4(x) ({ u32 ret_; \
75 : "=r"(ret_) : "r"((u32)(x))); \
78 # elif defined(_MSC_VER)
81 # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
82 # define BSWAP8(x) _byteswap_uint64((u64)(x))
83 # define BSWAP4(x) _byteswap_ulong((u32)(x))
84 # elif defined(_M_IX86)
85 __inline u32 _bswap4(u32 val)
87 _asm mov eax, val _asm bswap eax}
88 # define BSWAP4(x) _bswap4(x)
92 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
93 # define GETU32(p) BSWAP4(*(const u32 *)(p))
94 # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
96 # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
97 # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
99 /*- GCM definitions */ typedef struct {
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.
112 struct gcm128_context {
113 /* Following 6 names follow names in GCM specification */
118 size_t t[16 / sizeof(size_t)];
119 } Yi, EKi, EK0, len, Xi, H;
121 * Relative position of Xi, H and pre-computed Htable is used in some
122 * assembler modules, i.e. don't change the order!
128 void (*gmult) (u64 Xi[2], const u128 Htable[16]);
129 void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
132 unsigned int mres, ares;
135 #if !defined(OPENSSL_SMALL_FOOTPRINT)
136 unsigned char Xn[48];
141 * The maximum permitted number of cipher blocks per data unit in XTS mode.
142 * Reference IEEE Std 1619-2018.
144 #define XTS_MAX_BLOCKS_PER_DATA_UNIT (1<<20)
146 struct xts128_context {
148 block128_f block1, block2;
151 struct ccm128_context {
161 #ifndef OPENSSL_NO_OCB
167 # define ocb_block16_xor(in1,in2,out) \
168 ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
169 (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
170 # if STRICT_ALIGNMENT
171 # define ocb_block16_xor_misaligned(in1,in2,out) \
172 ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
174 # define ocb_block16_xor_misaligned ocb_block16_xor
177 struct ocb128_context {
178 /* Need both encrypt and decrypt key schedules for decryption */
183 ocb128_f stream; /* direction dependent */
184 /* Key dependent variables. Can be reused if key remains the same */
190 /* Must be reset for each session */
193 u64 blocks_processed;
194 OCB_BLOCK offset_aad;
200 #endif /* OPENSSL_NO_OCB */
202 #ifndef OPENSSL_NO_SIV
206 typedef union siv_block_u {
207 uint64_t word[SIV_LEN/sizeof(uint64_t)];
208 unsigned char byte[SIV_LEN];
211 struct siv128_context {
212 /* d stores intermediate results of S2V; it corresponds to D from the
213 pseudocode in section 2.4 of RFC 5297. */
216 EVP_CIPHER_CTX *cipher_ctx;
218 EVP_MAC_CTX *mac_ctx_init;
223 #endif /* OPENSSL_NO_SIV */