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
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(__s390__) || defined(__s390x__)
32 # undef STRICT_ALIGNMENT
35 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
36 # if defined(__GNUC__) && __GNUC__>=2
37 # if defined(__x86_64) || defined(__x86_64__)
38 # define BSWAP8(x) ({ u64 ret=(x); \
41 # define BSWAP4(x) ({ u32 ret=(x); \
44 # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
45 # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
46 asm ("bswapl %0; bswapl %1" \
47 : "+r"(hi),"+r"(lo)); \
49 # define BSWAP4(x) ({ u32 ret=(x); \
52 # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
53 # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
54 asm ("rev %0,%0; rev %1,%1" \
55 : "+r"(hi),"+r"(lo)); \
57 # define BSWAP4(x) ({ u32 ret; \
59 : "=r"(ret) : "r"((u32)(x))); \
62 # elif defined(_MSC_VER)
64 # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
65 # define BSWAP8(x) _byteswap_uint64((u64)(x))
66 # define BSWAP4(x) _byteswap_ulong((u32)(x))
67 # elif defined(_M_IX86)
68 __inline u32 _bswap4(u32 val)
70 _asm mov eax, val _asm bswap eax}
71 # define BSWAP4(x) _bswap4(x)
75 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
76 # define GETU32(p) BSWAP4(*(const u32 *)(p))
77 # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
79 # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
80 # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
82 /*- GCM definitions */ typedef struct {
90 * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
91 * never be set to 8 [or 1]. For further information see gcm128.c.
95 struct gcm128_context {
96 /* Following 6 names follow names in GCM specification */
101 size_t t[16 / sizeof(size_t)];
102 } Yi, EKi, EK0, len, Xi, H;
104 * Relative position of Xi, H and pre-computed Htable is used in some
105 * assembler modules, i.e. don't change the order!
111 void (*gmult) (u64 Xi[2], const u128 Htable[16]);
112 void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
115 unsigned int mres, ares;
120 struct xts128_context {
122 block128_f block1, block2;
125 struct ccm128_context {