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