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