1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2010-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos /* This header can move into provider when legacy support is removed */
11*b0d17251Schristos #include <openssl/modes.h>
12*b0d17251Schristos
13*b0d17251Schristos #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
14*b0d17251Schristos typedef __int64 i64;
15*b0d17251Schristos typedef unsigned __int64 u64;
16*b0d17251Schristos # define U64(C) C##UI64
17*b0d17251Schristos #elif defined(__arch64__)
18*b0d17251Schristos typedef long i64;
19*b0d17251Schristos typedef unsigned long u64;
20*b0d17251Schristos # define U64(C) C##UL
21*b0d17251Schristos #else
22*b0d17251Schristos typedef long long i64;
23*b0d17251Schristos typedef unsigned long long u64;
24*b0d17251Schristos # define U64(C) C##ULL
25*b0d17251Schristos #endif
26*b0d17251Schristos
27*b0d17251Schristos typedef unsigned int u32;
28*b0d17251Schristos typedef unsigned char u8;
29*b0d17251Schristos
30*b0d17251Schristos #define STRICT_ALIGNMENT 1
31*b0d17251Schristos #ifndef PEDANTIC
32*b0d17251Schristos # if defined(__i386) || defined(__i386__) || \
33*b0d17251Schristos defined(__x86_64) || defined(__x86_64__) || \
34*b0d17251Schristos defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
35*b0d17251Schristos defined(__aarch64__) || \
36*b0d17251Schristos defined(__s390__) || defined(__s390x__)
37*b0d17251Schristos # undef STRICT_ALIGNMENT
38*b0d17251Schristos # endif
39*b0d17251Schristos #endif
40*b0d17251Schristos
41*b0d17251Schristos #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
42*b0d17251Schristos # if defined(__GNUC__) && __GNUC__>=2
43*b0d17251Schristos # if defined(__x86_64) || defined(__x86_64__)
44*b0d17251Schristos # define BSWAP8(x) ({ u64 ret_=(x); \
45*b0d17251Schristos asm ("bswapq %0" \
46*b0d17251Schristos : "+r"(ret_)); ret_; })
47*b0d17251Schristos # define BSWAP4(x) ({ u32 ret_=(x); \
48*b0d17251Schristos asm ("bswapl %0" \
49*b0d17251Schristos : "+r"(ret_)); ret_; })
50*b0d17251Schristos # elif (defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)
51*b0d17251Schristos # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
52*b0d17251Schristos asm ("bswapl %0; bswapl %1" \
53*b0d17251Schristos : "+r"(hi_),"+r"(lo_)); \
54*b0d17251Schristos (u64)hi_<<32|lo_; })
55*b0d17251Schristos # define BSWAP4(x) ({ u32 ret_=(x); \
56*b0d17251Schristos asm ("bswapl %0" \
57*b0d17251Schristos : "+r"(ret_)); ret_; })
58*b0d17251Schristos # elif defined(__aarch64__)
59*b0d17251Schristos # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
60*b0d17251Schristos __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
61*b0d17251Schristos # define BSWAP8(x) ({ u64 ret_; \
62*b0d17251Schristos asm ("rev %0,%1" \
63*b0d17251Schristos : "=r"(ret_) : "r"(x)); ret_; })
64*b0d17251Schristos # define BSWAP4(x) ({ u32 ret_; \
65*b0d17251Schristos asm ("rev %w0,%w1" \
66*b0d17251Schristos : "=r"(ret_) : "r"(x)); ret_; })
67*b0d17251Schristos # endif
68*b0d17251Schristos # elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
69*b0d17251Schristos # define BSWAP8(x) ({ u32 lo_=(u64)(x)>>32,hi_=(x); \
70*b0d17251Schristos asm ("rev %0,%0; rev %1,%1" \
71*b0d17251Schristos : "+r"(hi_),"+r"(lo_)); \
72*b0d17251Schristos (u64)hi_<<32|lo_; })
73*b0d17251Schristos # define BSWAP4(x) ({ u32 ret_; \
74*b0d17251Schristos asm ("rev %0,%1" \
75*b0d17251Schristos : "=r"(ret_) : "r"((u32)(x))); \
76*b0d17251Schristos ret_; })
77*b0d17251Schristos # endif
78*b0d17251Schristos # elif defined(_MSC_VER)
79*b0d17251Schristos # if _MSC_VER>=1300
80*b0d17251Schristos # include <stdlib.h>
81*b0d17251Schristos # pragma intrinsic(_byteswap_uint64,_byteswap_ulong)
82*b0d17251Schristos # define BSWAP8(x) _byteswap_uint64((u64)(x))
83*b0d17251Schristos # define BSWAP4(x) _byteswap_ulong((u32)(x))
84*b0d17251Schristos # elif defined(_M_IX86)
_bswap4(u32 val)85*b0d17251Schristos __inline u32 _bswap4(u32 val)
86*b0d17251Schristos {
87*b0d17251Schristos _asm mov eax, val _asm bswap eax}
88*b0d17251Schristos # define BSWAP4(x) _bswap4(x)
89*b0d17251Schristos # endif
90*b0d17251Schristos # endif
91*b0d17251Schristos #endif
92*b0d17251Schristos #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
93*b0d17251Schristos # define GETU32(p) BSWAP4(*(const u32 *)(p))
94*b0d17251Schristos # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
95*b0d17251Schristos #else
96*b0d17251Schristos # define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
97*b0d17251Schristos # define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
98*b0d17251Schristos #endif
99*b0d17251Schristos /*- GCM definitions */ typedef struct {
100*b0d17251Schristos u64 hi, lo;
101*b0d17251Schristos } u128;
102*b0d17251Schristos
103*b0d17251Schristos #ifdef TABLE_BITS
104*b0d17251Schristos # undef TABLE_BITS
105*b0d17251Schristos #endif
106*b0d17251Schristos /*
107*b0d17251Schristos * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
108*b0d17251Schristos * never be set to 8 [or 1]. For further information see gcm128.c.
109*b0d17251Schristos */
110*b0d17251Schristos #define TABLE_BITS 4
111*b0d17251Schristos
112*b0d17251Schristos struct gcm128_context {
113*b0d17251Schristos /* Following 6 names follow names in GCM specification */
114*b0d17251Schristos union {
115*b0d17251Schristos u64 u[2];
116*b0d17251Schristos u32 d[4];
117*b0d17251Schristos u8 c[16];
118*b0d17251Schristos size_t t[16 / sizeof(size_t)];
119*b0d17251Schristos } Yi, EKi, EK0, len, Xi, H;
120*b0d17251Schristos /*
121*b0d17251Schristos * Relative position of Xi, H and pre-computed Htable is used in some
122*b0d17251Schristos * assembler modules, i.e. don't change the order!
123*b0d17251Schristos */
124*b0d17251Schristos #if TABLE_BITS==8
125*b0d17251Schristos u128 Htable[256];
126*b0d17251Schristos #else
127*b0d17251Schristos u128 Htable[16];
128*b0d17251Schristos void (*gmult) (u64 Xi[2], const u128 Htable[16]);
129*b0d17251Schristos void (*ghash) (u64 Xi[2], const u128 Htable[16], const u8 *inp,
130*b0d17251Schristos size_t len);
131*b0d17251Schristos #endif
132*b0d17251Schristos unsigned int mres, ares;
133*b0d17251Schristos block128_f block;
134*b0d17251Schristos void *key;
135*b0d17251Schristos #if !defined(OPENSSL_SMALL_FOOTPRINT)
136*b0d17251Schristos unsigned char Xn[48];
137*b0d17251Schristos #endif
138*b0d17251Schristos };
139*b0d17251Schristos
140*b0d17251Schristos /*
141*b0d17251Schristos * The maximum permitted number of cipher blocks per data unit in XTS mode.
142*b0d17251Schristos * Reference IEEE Std 1619-2018.
143*b0d17251Schristos */
144*b0d17251Schristos #define XTS_MAX_BLOCKS_PER_DATA_UNIT (1<<20)
145*b0d17251Schristos
146*b0d17251Schristos struct xts128_context {
147*b0d17251Schristos void *key1, *key2;
148*b0d17251Schristos block128_f block1, block2;
149*b0d17251Schristos };
150*b0d17251Schristos
151*b0d17251Schristos struct ccm128_context {
152*b0d17251Schristos union {
153*b0d17251Schristos u64 u[2];
154*b0d17251Schristos u8 c[16];
155*b0d17251Schristos } nonce, cmac;
156*b0d17251Schristos u64 blocks;
157*b0d17251Schristos block128_f block;
158*b0d17251Schristos void *key;
159*b0d17251Schristos };
160*b0d17251Schristos
161*b0d17251Schristos #ifndef OPENSSL_NO_OCB
162*b0d17251Schristos
163*b0d17251Schristos typedef union {
164*b0d17251Schristos u64 a[2];
165*b0d17251Schristos unsigned char c[16];
166*b0d17251Schristos } OCB_BLOCK;
167*b0d17251Schristos # define ocb_block16_xor(in1,in2,out) \
168*b0d17251Schristos ( (out)->a[0]=(in1)->a[0]^(in2)->a[0], \
169*b0d17251Schristos (out)->a[1]=(in1)->a[1]^(in2)->a[1] )
170*b0d17251Schristos # if STRICT_ALIGNMENT
171*b0d17251Schristos # define ocb_block16_xor_misaligned(in1,in2,out) \
172*b0d17251Schristos ocb_block_xor((in1)->c,(in2)->c,16,(out)->c)
173*b0d17251Schristos # else
174*b0d17251Schristos # define ocb_block16_xor_misaligned ocb_block16_xor
175*b0d17251Schristos # endif
176*b0d17251Schristos
177*b0d17251Schristos struct ocb128_context {
178*b0d17251Schristos /* Need both encrypt and decrypt key schedules for decryption */
179*b0d17251Schristos block128_f encrypt;
180*b0d17251Schristos block128_f decrypt;
181*b0d17251Schristos void *keyenc;
182*b0d17251Schristos void *keydec;
183*b0d17251Schristos ocb128_f stream; /* direction dependent */
184*b0d17251Schristos /* Key dependent variables. Can be reused if key remains the same */
185*b0d17251Schristos size_t l_index;
186*b0d17251Schristos size_t max_l_index;
187*b0d17251Schristos OCB_BLOCK l_star;
188*b0d17251Schristos OCB_BLOCK l_dollar;
189*b0d17251Schristos OCB_BLOCK *l;
190*b0d17251Schristos /* Must be reset for each session */
191*b0d17251Schristos struct {
192*b0d17251Schristos u64 blocks_hashed;
193*b0d17251Schristos u64 blocks_processed;
194*b0d17251Schristos OCB_BLOCK offset_aad;
195*b0d17251Schristos OCB_BLOCK sum;
196*b0d17251Schristos OCB_BLOCK offset;
197*b0d17251Schristos OCB_BLOCK checksum;
198*b0d17251Schristos } sess;
199*b0d17251Schristos };
200*b0d17251Schristos #endif /* OPENSSL_NO_OCB */
201*b0d17251Schristos
202*b0d17251Schristos #ifndef OPENSSL_NO_SIV
203*b0d17251Schristos
204*b0d17251Schristos #define SIV_LEN 16
205*b0d17251Schristos
206*b0d17251Schristos typedef union siv_block_u {
207*b0d17251Schristos uint64_t word[SIV_LEN/sizeof(uint64_t)];
208*b0d17251Schristos unsigned char byte[SIV_LEN];
209*b0d17251Schristos } SIV_BLOCK;
210*b0d17251Schristos
211*b0d17251Schristos struct siv128_context {
212*b0d17251Schristos /* d stores intermediate results of S2V; it corresponds to D from the
213*b0d17251Schristos pseudocode in section 2.4 of RFC 5297. */
214*b0d17251Schristos SIV_BLOCK d;
215*b0d17251Schristos SIV_BLOCK tag;
216*b0d17251Schristos EVP_CIPHER_CTX *cipher_ctx;
217*b0d17251Schristos EVP_MAC *mac;
218*b0d17251Schristos EVP_MAC_CTX *mac_ctx_init;
219*b0d17251Schristos int final_ret;
220*b0d17251Schristos int crypto_ok;
221*b0d17251Schristos };
222*b0d17251Schristos
223*b0d17251Schristos #endif /* OPENSSL_NO_SIV */
224