1 /* $OpenBSD: modes_lcl.h,v 1.10 2016/12/21 15:49:29 jsing Exp $ */ 2 /* ==================================================================== 3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 4 * 5 * Redistribution and use is governed by OpenSSL license. 6 * ==================================================================== 7 */ 8 9 #include <machine/endian.h> 10 11 #include <openssl/opensslconf.h> 12 13 #include <openssl/modes.h> 14 15 __BEGIN_HIDDEN_DECLS 16 17 #if defined(_LP64) 18 typedef long i64; 19 typedef unsigned long u64; 20 #define U64(C) C##UL 21 #else 22 typedef long long i64; 23 typedef unsigned long long u64; 24 #define U64(C) C##ULL 25 #endif 26 27 typedef unsigned int u32; 28 typedef unsigned char u8; 29 30 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) 31 #if defined(__GNUC__) && __GNUC__>=2 32 # if defined(__x86_64) || defined(__x86_64__) 33 # define BSWAP8(x) ({ u64 ret=(x); \ 34 asm ("bswapq %0" \ 35 : "+r"(ret)); ret; }) 36 # define BSWAP4(x) ({ u32 ret=(x); \ 37 asm ("bswapl %0" \ 38 : "+r"(ret)); ret; }) 39 # elif (defined(__i386) || defined(__i386__)) 40 # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \ 41 asm ("bswapl %0; bswapl %1" \ 42 : "+r"(hi),"+r"(lo)); \ 43 (u64)hi<<32|lo; }) 44 # define BSWAP4(x) ({ u32 ret=(x); \ 45 asm ("bswapl %0" \ 46 : "+r"(ret)); ret; }) 47 # elif (defined(__arm__) || defined(__arm)) && !defined(__STRICT_ALIGNMENT) 48 # if (__ARM_ARCH >= 6) 49 # define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \ 50 asm ("rev %0,%0; rev %1,%1" \ 51 : "+r"(hi),"+r"(lo)); \ 52 (u64)hi<<32|lo; }) 53 # define BSWAP4(x) ({ u32 ret; \ 54 asm ("rev %0,%1" \ 55 : "=r"(ret) : "r"((u32)(x))); \ 56 ret; }) 57 # endif 58 # endif 59 #endif 60 #endif 61 62 #if defined(BSWAP4) && !defined(__STRICT_ALIGNMENT) 63 #define GETU32(p) BSWAP4(*(const u32 *)(p)) 64 #define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v) 65 #else 66 #define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3]) 67 #define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v)) 68 #endif 69 70 /* GCM definitions */ 71 72 typedef struct { u64 hi,lo; } u128; 73 74 #ifdef TABLE_BITS 75 #undef TABLE_BITS 76 #endif 77 /* 78 * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should 79 * never be set to 8 [or 1]. For further information see gcm128.c. 80 */ 81 #define TABLE_BITS 4 82 83 struct gcm128_context { 84 /* Following 6 names follow names in GCM specification */ 85 union { u64 u[2]; u32 d[4]; u8 c[16]; size_t t[16/sizeof(size_t)]; } 86 Yi,EKi,EK0,len,Xi,H; 87 /* Relative position of Xi, H and pre-computed Htable is used 88 * in some assembler modules, i.e. don't change the order! */ 89 #if TABLE_BITS==8 90 u128 Htable[256]; 91 #else 92 u128 Htable[16]; 93 void (*gmult)(u64 Xi[2],const u128 Htable[16]); 94 void (*ghash)(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len); 95 #endif 96 unsigned int mres, ares; 97 block128_f block; 98 void *key; 99 }; 100 101 struct xts128_context { 102 void *key1, *key2; 103 block128_f block1,block2; 104 }; 105 106 struct ccm128_context { 107 union { u64 u[2]; u8 c[16]; } nonce, cmac; 108 u64 blocks; 109 block128_f block; 110 void *key; 111 }; 112 113 __END_HIDDEN_DECLS 114