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