2 * Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/modes.h>
12 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
14 typedef unsigned __int64 u64;
15 # define U64(C) C##UI64
16 #elif defined(__arch64__)
18 typedef unsigned long u64;
21 typedef long long i64;
22 typedef unsigned long long u64;
23 # define U64(C) C##ULL
26 typedef unsigned int u32;
27 typedef unsigned char u8;
29 #define STRICT_ALIGNMENT 1
31 # if defined(__i386) || defined(__i386__) || \
32 defined(__x86_64) || defined(__x86_64__) || \
33 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
34 defined(__aarch64__) || \
35 defined(__s390__) || defined(__s390x__)
36 # undef STRICT_ALIGNMENT
40 #ifndef STRICT_ALIGNMENT
42 typedef u32 u32_a1 __attribute((__aligned__(1)));
48 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
49 # if defined(__GNUC__) && __GNUC__>=2
50 # if defined(__x86_64) || defined(__x86_64__)
51 # define BSWAP8(x) ({ u64 ret_=(x); \
53 : "+r"(ret_)); ret_; })
54 # define BSWAP4(x) ({ u32 ret_=(x); \
56 : "+r"(ret_)); ret_; })
57 # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
58 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
59 asm ("bswapl %0; bswapl %1" \
60 : "+r"(hi_),"+r"(lo_)); \
62 # define BSWAP4(x) ({ u32 ret_=(x); \
64 : "+r"(ret_)); ret_; })
65 # elif defined(__aarch64__)
66 # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
67 __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
68 # define BSWAP8(x) ({ u64 ret_; \
70 : "=r"(ret_) : "r"(x)); ret_; })
71 # define BSWAP4(x) ({ u32 ret_; \
73 : "=r"(ret_) : "r"(x)); ret_; })
75 # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
76 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
77 asm ("rev %0,%0; rev %1,%1" \
78 : "+r"(hi_),"+r"(lo_)); \
80 # define BSWAP4(x) ({ u32 ret_; \
82 : "=r"(ret_) : "r"((u32)(x))); \
85 # elif defined(_MSC_VER)
88 # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
89 # define BSWAP8(x) _byteswap_uint64((u64)(x))
90 # define BSWAP4(x) _byteswap_ulong((u32)(x))
91 # elif defined(_M_IX86)
92 __inline u32 _bswap4(u32 val)
94 _asm mov eax, val _asm bswap eax}
95 # define BSWAP4(x) _bswap4(x)
99 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
100 # define GETU32(p) BSWAP4(*(const u32_a1 *)(p))
101 # define PUTU32(p,v) *(u32_a1 *)(p) = BSWAP4(v)
103 # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
104 # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
106 /*- GCM definitions */ typedef struct {
114 * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
115 * never be set to 8 [or 1]. For further information see gcm128.c.
119 struct gcm128_context {
120 /* Following 6 names follow names in GCM specification */
125 size_t t[16 / sizeof(size_t)];
126 } Yi, EKi, EK0, len, Xi, H;
128 * Relative position of Xi, H and pre-computed Htable is used in some
129 * assembler modules, i.e. don't change the order!
135 void (*gmult) (u64 Xi[2], const u128 Htable[16]);
136 void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
139 unsigned int mres, ares;
142 #if !defined(OPENSSL_SMALL_FOOTPRINT)
143 unsigned char Xn[48];
147 struct xts128_context {
149 block128_f block1, block2;
152 struct ccm128_context {
162 #ifndef OPENSSL_NO_OCB
168 # define ocb_block16_xor(in1,in2,out) \
169 ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
170 (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
171 # if STRICT_ALIGNMENT
172 # define ocb_block16_xor_misaligned(in1,in2,out) \
173 ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
175 # define ocb_block16_xor_misaligned ocb_block16_xor
178 struct ocb128_context {
179 /* Need both encrypt and decrypt key schedules for decryption */
184 ocb128_f stream; /* direction dependent */
185 /* Key dependent variables. Can be reused if key remains the same */
191 /* Must be reset for each session */
194 u64 blocks_processed;
195 OCB_BLOCK offset_aad;
201 #endif /* OPENSSL_NO_OCB */