| 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__) |
| 11 |
typedef __int64 i64; |
| 12 |
typedef unsigned __int64 u64; |
| 13 |
# define U64(C) C##UI64 |
| 14 |
#elif defined(__arch64__) |
| 15 |
typedef long i64; |
| 16 |
typedef unsigned long u64; |
| 17 |
# define U64(C) C##UL |
| 18 |
#else |
| 19 |
typedef long long i64; |
| 20 |
typedef unsigned long long u64; |
| 21 |
# define U64(C) C##ULL |
| 22 |
#endif |
| 23 |
|
| 24 |
typedef unsigned int u32; |
| 25 |
typedef unsigned char u8; |
| 26 |
|
| 27 |
#define STRICT_ALIGNMENT 1 |
| 28 |
#ifndef PEDANTIC |
| 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 |
| 35 |
# endif |
| 36 |
#endif |
| 37 |
|
| 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); \ |
| 42 |
asm ("bswapq %0" \ |
| 43 |
: "+r"(ret_)); ret_; }) |
| 44 |
# define BSWAP4(x) ({ u32 ret_=(x); \ |
| 45 |
asm ("bswapl %0" \ |
| 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_)); \ |
| 51 |
(u64)hi_<<32|lo_; }) |
| 52 |
# define BSWAP4(x) ({ u32 ret_=(x); \ |
| 53 |
asm ("bswapl %0" \ |
| 54 |
: "+r"(ret_)); ret_; }) |
| 55 |
# elif defined(__aarch64__) |
| 56 |
# define BSWAP8(x) ({ u64 ret_; \ |
| 57 |
asm ("rev %0,%1" \ |
| 58 |
: "=r"(ret_) : "r"(x)); ret_; }) |
| 59 |
# define BSWAP4(x) ({ u32 ret_; \ |
| 60 |
asm ("rev %w0,%w1" \ |
| 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_)); \ |
| 66 |
(u64)hi_<<32|lo_; }) |
| 67 |
# define BSWAP4(x) ({ u32 ret_; \ |
| 68 |
asm ("rev %0,%1" \ |
| 69 |
: "=r"(ret_) : "r"((u32)(x))); \ |
| 70 |
ret_; }) |
| 71 |
# endif |
| 72 |
# elif defined(_MSC_VER) |
| 73 |
# if _MSC_VER>=1300 |
| 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) |
| 79 |
{ |
| 80 |
_asm mov eax, val _asm bswap eax} |
| 81 |
# define BSWAP4(x) _bswap4(x) |
| 82 |
# endif |
| 83 |
# endif |
| 84 |
#endif |
| 85 |
#if defined(BSWAP4) && !defined(STRICT_ALIGNMENT) |
| 86 |
# define GETU32(p) BSWAP4(*(const u32 *)(p)) |
| 87 |
# define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v) |
| 88 |
#else |
| 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)) |
| 91 |
#endif |
| 92 |
/*- GCM definitions */ typedef struct { |
| 93 |
u64 hi, lo; |
| 94 |
} u128; |
| 95 |
|
| 96 |
#ifdef TABLE_BITS |
| 97 |
# undef TABLE_BITS |
| 98 |
#endif |
| 99 |
/* |
| 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. |
| 102 |
*/ |
| 103 |
#define TABLE_BITS 4 |
| 104 |
|
| 105 |
struct gcm128_context { |
| 106 |
/* Following 6 names follow names in GCM specification */ |
| 107 |
union { |
| 108 |
u64 u[2]; |
| 109 |
u32 d[4]; |
| 110 |
u8 c[16]; |
| 111 |
size_t t[16 / sizeof(size_t)]; |
| 112 |
} Yi, EKi, EK0, len, Xi, H; |
| 113 |
/* |
| 114 |
* Relative position of Xi, H and pre-computed Htable is used in some |
| 115 |
* assembler modules, i.e. don't change the order! |
| 116 |
*/ |
| 117 |
#if TABLE_BITS==8 |
| 118 |
u128 Htable[256]; |
| 119 |
#else |
| 120 |
u128 Htable[16]; |
| 121 |
void (*gmult) (u64 Xi[2], const u128 Htable[16]); |
| 122 |
void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp, |
| 123 |
size_t len); |
| 124 |
#endif |
| 125 |
unsigned int mres, ares; |
| 126 |
block128_f block; |
| 127 |
void *key; |
| 128 |
}; |
| 129 |
|
| 130 |
struct xts128_context { |
| 131 |
void *key1, *key2; |
| 132 |
block128_f block1, block2; |
| 133 |
}; |
| 134 |
|
| 135 |
struct ccm128_context { |
| 136 |
union { |
| 137 |
u64 u[2]; |
| 138 |
u8 c[16]; |
| 139 |
} nonce, cmac; |
| 140 |
u64 blocks; |
| 141 |
block128_f block; |
| 142 |
void *key; |
| 143 |
}; |