1 /* ====================================================================
2 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
4 * Redistribution and use is governed by OpenSSL license.
5 * ====================================================================
8 #include <openssl/modes.h>
10 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
12 typedef unsigned __int64 u64;
13 # define U64(C) C##UI64
14 #elif defined(__arch64__)
16 typedef unsigned long u64;
19 typedef long long i64;
20 typedef unsigned long long u64;
21 # define U64(C) C##ULL
24 typedef unsigned int u32;
25 typedef unsigned char u8;
27 #define STRICT_ALIGNMENT 1
29 # if defined(__i386) || defined(__i386__) || \
30 defined(__x86_64) || defined(__x86_64__) || \
31 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
32 defined(__aarch64__) || \
33 defined(__s390__) || defined(__s390x__)
34 # undef STRICT_ALIGNMENT
38 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
39 # if defined(__GNUC__) && __GNUC__>=2
40 # if defined(__x86_64) || defined(__x86_64__)
41 # define BSWAP8(x) ({ u64 ret_=(x); \
43 : "+r"(ret_)); ret_; })
44 # define BSWAP4(x) ({ u32 ret_=(x); \
46 : "+r"(ret_)); ret_; })
47 # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
48 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
49 asm ("bswapl %0; bswapl %1" \
50 : "+r"(hi_),"+r"(lo_)); \
52 # define BSWAP4(x) ({ u32 ret_=(x); \
54 : "+r"(ret_)); ret_; })
55 # elif defined(__aarch64__)
56 # define BSWAP8(x) ({ u64 ret_; \
58 : "=r"(ret_) : "r"(x)); ret_; })
59 # define BSWAP4(x) ({ u32 ret_; \
61 : "=r"(ret_) : "r"(x)); ret_; })
62 # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
63 # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
64 asm ("rev %0,%0; rev %1,%1" \
65 : "+r"(hi_),"+r"(lo_)); \
67 # define BSWAP4(x) ({ u32 ret_; \
69 : "=r"(ret_) : "r"((u32)(x))); \
72 # elif defined(_MSC_VER)
74 # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
75 # define BSWAP8(x) _byteswap_uint64((u64)(x))
76 # define BSWAP4(x) _byteswap_ulong((u32)(x))
77 # elif defined(_M_IX86)
78 __inline u32 _bswap4(u32 val)
80 _asm mov eax, val _asm bswap eax}
81 # define BSWAP4(x) _bswap4(x)
85 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
86 # define GETU32(p) BSWAP4(*(const u32 *)(p))
87 # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
89 # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
90 # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
92 /*- GCM definitions */ typedef struct {
100 * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
101 * never be set to 8 [or 1]. For further information see gcm128.c.
105 struct gcm128_context {
106 /* Following 6 names follow names in GCM specification */
111 size_t t[16 / sizeof(size_t)];
112 } Yi, EKi, EK0, len, Xi, H;
114 * Relative position of Xi, H and pre-computed Htable is used in some
115 * assembler modules, i.e. don't change the order!
121 void (*gmult) (u64 Xi[2], const u128 Htable[16]);
122 void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
125 unsigned int mres, ares;
130 struct xts128_context {
132 block128_f block1, block2;
135 struct ccm128_context {
145 #ifndef OPENSSL_NO_OCB
147 # ifdef STRICT_ALIGNMENT
151 # define ocb_block16_xor(in1,in2,out) \
152 ocb_block_xor((in1)->a,(in2)->a,16,(out)->a)
153 # else /* STRICT_ALIGNMENT */
158 # define ocb_block16_xor(in1,in2,out) \
159 (out)->a=(in1)->a^(in2)->a; (out)->b=(in1)->b^(in2)->b;
160 # endif /* STRICT_ALIGNMENT */
162 struct ocb128_context {
163 /* Need both encrypt and decrypt key schedules for decryption */
168 /* Key dependent variables. Can be reused if key remains the same */
174 /* Must be reset for each session */
176 u64 blocks_processed;
178 OCB_BLOCK offset_aad;
183 #endif /* OPENSSL_NO_OCB */