2 * Copyright 2010-2016 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 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
41 # if defined(__GNUC__) && __GNUC__>=2
42 # if defined(__x86_64) || defined(__x86_64__)
43 # define BSWAP8(x) ({ u64 ret_=(x); \
45 : "+r"(ret_)); ret_; })
46 # define BSWAP4(x) ({ u32 ret_=(x); \
48 : "+r"(ret_)); ret_; })
49 # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
50 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
51 asm ("bswapl %0; bswapl %1" \
52 : "+r"(hi_),"+r"(lo_)); \
54 # define BSWAP4(x) ({ u32 ret_=(x); \
56 : "+r"(ret_)); ret_; })
57 # elif defined(__aarch64__)
58 # define BSWAP8(x) ({ u64 ret_; \
60 : "=r"(ret_) : "r"(x)); ret_; })
61 # define BSWAP4(x) ({ u32 ret_; \
63 : "=r"(ret_) : "r"(x)); ret_; })
64 # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
65 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
66 asm ("rev %0,%0; rev %1,%1" \
67 : "+r"(hi_),"+r"(lo_)); \
69 # define BSWAP4(x) ({ u32 ret_; \
71 : "=r"(ret_) : "r"((u32)(x))); \
74 # elif defined(_MSC_VER)
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)
82 _asm mov eax, val _asm bswap eax}
83 # define BSWAP4(x) _bswap4(x)
87 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
88 # define GETU32(p) BSWAP4(*(const u32 *)(p))
89 # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
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))
94 /*- GCM definitions */ typedef struct {
102 * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
103 * never be set to 8 [or 1]. For further information see gcm128.c.
107 struct gcm128_context {
108 /* Following 6 names follow names in GCM specification */
113 size_t t[16 / sizeof(size_t)];
114 } Yi, EKi, EK0, len, Xi, H;
116 * Relative position of Xi, H and pre-computed Htable is used in some
117 * assembler modules, i.e. don't change the order!
123 void (*gmult) (u64 Xi[2], const u128 Htable[16]);
124 void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
127 unsigned int mres, ares;
132 struct xts128_context {
134 block128_f block1, block2;
137 struct ccm128_context {
147 #ifndef OPENSSL_NO_OCB
153 # define ocb_block16_xor(in1,in2,out) \
154 ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
155 (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
156 # if STRICT_ALIGNMENT
157 # define ocb_block16_xor_misaligned(in1,in2,out) \
158 ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
160 # define ocb_block16_xor_misaligned ocb_block16_xor
163 struct ocb128_context {
164 /* Need both encrypt and decrypt key schedules for decryption */
169 ocb128_f stream; /* direction dependent */
170 /* Key dependent variables. Can be reused if key remains the same */
176 /* Must be reset for each session */
178 u64 blocks_processed;
180 OCB_BLOCK offset_aad;
185 #endif /* OPENSSL_NO_OCB */