1/* ==================================================================== 2 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 3 * 4 * Redistribution and use is governed by OpenSSL license. 5 * ==================================================================== 6 */ 7 8#include <openssl/modes.h> 9 10#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) 11typedef __int64 i64; 12typedef unsigned __int64 u64; 13# define U64(C) C##UI64 14#elif defined(__arch64__) 15typedef long i64; 16typedef unsigned long u64; 17# define U64(C) C##UL 18#else 19typedef long long i64; 20typedef unsigned long long u64; 21# define U64(C) C##ULL 22#endif 23 24typedef unsigned int u32; 25typedef unsigned char u8; 26 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 33#endif 34 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); \ 39 asm ("bswapq %0" \ 40 : "+r"(ret)); ret; }) 41# define BSWAP4(x) ({ u32 ret=(x); \ 42 asm ("bswapl %0" \ 43 : "+r"(ret)); ret; }) 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)); \ 48 (u64)hi<<32|lo; }) 49# define BSWAP4(x) ({ u32 ret=(x); \ 50 asm ("bswapl %0" \ 51 : "+r"(ret)); ret; }) 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)); \ 56 (u64)hi<<32|lo; }) 57# define BSWAP4(x) ({ u32 ret; \ 58 asm ("rev %0,%1" \ 59 : "=r"(ret) : "r"((u32)(x))); \ 60 ret; }) 61# endif 62# elif defined(_MSC_VER) 63# if _MSC_VER>=1300 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) 69{ 70_asm mov eax, val _asm bswap eax} 71# define BSWAP4(x) _bswap4(x) 72# endif 73# endif 74#endif 75#if defined(BSWAP4) && !defined(STRICT_ALIGNMENT) 76# define GETU32(p) BSWAP4(*(const u32 *)(p)) 77# define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v) 78#else 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)) 81#endif 82/*- GCM definitions */ typedef struct { 83 u64 hi, lo; 84} u128; 85 86#ifdef TABLE_BITS 87# undef TABLE_BITS 88#endif 89/* 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. 92 */ 93#define TABLE_BITS 4 94 95struct gcm128_context { 96 /* Following 6 names follow names in GCM specification */ 97 union { 98 u64 u[2]; 99 u32 d[4]; 100 u8 c[16]; 101 size_t t[16 / sizeof(size_t)]; 102 } Yi, EKi, EK0, len, Xi, H; 103 /* 104 * Relative position of Xi, H and pre-computed Htable is used in some 105 * assembler modules, i.e. don't change the order! 106 */ 107#if TABLE_BITS==8 108 u128 Htable[256]; 109#else 110 u128 Htable[16]; 111 void (*gmult) (u64 Xi[2], const u128 Htable[16]); 112 void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp, 113 size_t len); 114#endif 115 unsigned int mres, ares; 116 block128_f block; 117 void *key; 118}; 119 120struct xts128_context { 121 void *key1, *key2; 122 block128_f block1, block2; 123}; 124 125struct ccm128_context { 126 union { 127 u64 u[2]; 128 u8 c[16]; 129 } nonce, cmac; 130 u64 blocks; 131 block128_f block; 132 void *key; 133}; 134