/src/openssl/crypto/modes/gcm128.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2010-2023 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 | | #include <string.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include "internal/endian.h" |
14 | | #include "crypto/modes.h" |
15 | | |
16 | | #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) |
17 | | typedef size_t size_t_aX __attribute((__aligned__(1))); |
18 | | #else |
19 | | typedef size_t size_t_aX; |
20 | | #endif |
21 | | |
22 | | #if defined(BSWAP4) && defined(STRICT_ALIGNMENT) |
23 | | /* redefine, because alignment is ensured */ |
24 | | # undef GETU32 |
25 | | # define GETU32(p) BSWAP4(*(const u32 *)(p)) |
26 | | # undef PUTU32 |
27 | | # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v) |
28 | | #endif |
29 | | |
30 | | /* RISC-V uses C implementation as a fallback. */ |
31 | | #if defined(__riscv) |
32 | | # define INCLUDE_C_GMULT_4BIT |
33 | | # define INCLUDE_C_GHASH_4BIT |
34 | | #endif |
35 | | |
36 | | #define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16)) |
37 | 0 | #define REDUCE1BIT(V) do { \ |
38 | 0 | if (sizeof(size_t)==8) { \ |
39 | 0 | u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \ |
40 | 0 | V.lo = (V.hi<<63)|(V.lo>>1); \ |
41 | 0 | V.hi = (V.hi>>1 )^T; \ |
42 | 0 | } \ |
43 | 0 | else { \ |
44 | 0 | u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \ |
45 | 0 | V.lo = (V.hi<<63)|(V.lo>>1); \ |
46 | 0 | V.hi = (V.hi>>1 )^((u64)T<<32); \ |
47 | 0 | } \ |
48 | 0 | } while(0) |
49 | | |
50 | | /*- |
51 | | * |
52 | | * NOTE: TABLE_BITS and all non-4bit implementations have been removed in 3.1. |
53 | | * |
54 | | * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should |
55 | | * never be set to 8. 8 is effectively reserved for testing purposes. |
56 | | * TABLE_BITS>1 are lookup-table-driven implementations referred to as |
57 | | * "Shoup's" in GCM specification. In other words OpenSSL does not cover |
58 | | * whole spectrum of possible table driven implementations. Why? In |
59 | | * non-"Shoup's" case memory access pattern is segmented in such manner, |
60 | | * that it's trivial to see that cache timing information can reveal |
61 | | * fair portion of intermediate hash value. Given that ciphertext is |
62 | | * always available to attacker, it's possible for him to attempt to |
63 | | * deduce secret parameter H and if successful, tamper with messages |
64 | | * [which is nothing but trivial in CTR mode]. In "Shoup's" case it's |
65 | | * not as trivial, but there is no reason to believe that it's resistant |
66 | | * to cache-timing attack. And the thing about "8-bit" implementation is |
67 | | * that it consumes 16 (sixteen) times more memory, 4KB per individual |
68 | | * key + 1KB shared. Well, on pros side it should be twice as fast as |
69 | | * "4-bit" version. And for gcc-generated x86[_64] code, "8-bit" version |
70 | | * was observed to run ~75% faster, closer to 100% for commercial |
71 | | * compilers... Yet "4-bit" procedure is preferred, because it's |
72 | | * believed to provide better security-performance balance and adequate |
73 | | * all-round performance. "All-round" refers to things like: |
74 | | * |
75 | | * - shorter setup time effectively improves overall timing for |
76 | | * handling short messages; |
77 | | * - larger table allocation can become unbearable because of VM |
78 | | * subsystem penalties (for example on Windows large enough free |
79 | | * results in VM working set trimming, meaning that consequent |
80 | | * malloc would immediately incur working set expansion); |
81 | | * - larger table has larger cache footprint, which can affect |
82 | | * performance of other code paths (not necessarily even from same |
83 | | * thread in Hyper-Threading world); |
84 | | * |
85 | | * Value of 1 is not appropriate for performance reasons. |
86 | | */ |
87 | | |
88 | | static void gcm_init_4bit(u128 Htable[16], const u64 H[2]) |
89 | 0 | { |
90 | 0 | u128 V; |
91 | | # if defined(OPENSSL_SMALL_FOOTPRINT) |
92 | | int i; |
93 | | # endif |
94 | |
|
95 | 0 | Htable[0].hi = 0; |
96 | 0 | Htable[0].lo = 0; |
97 | 0 | V.hi = H[0]; |
98 | 0 | V.lo = H[1]; |
99 | |
|
100 | | # if defined(OPENSSL_SMALL_FOOTPRINT) |
101 | | for (Htable[8] = V, i = 4; i > 0; i >>= 1) { |
102 | | REDUCE1BIT(V); |
103 | | Htable[i] = V; |
104 | | } |
105 | | |
106 | | for (i = 2; i < 16; i <<= 1) { |
107 | | u128 *Hi = Htable + i; |
108 | | int j; |
109 | | for (V = *Hi, j = 1; j < i; ++j) { |
110 | | Hi[j].hi = V.hi ^ Htable[j].hi; |
111 | | Hi[j].lo = V.lo ^ Htable[j].lo; |
112 | | } |
113 | | } |
114 | | # else |
115 | 0 | Htable[8] = V; |
116 | 0 | REDUCE1BIT(V); |
117 | 0 | Htable[4] = V; |
118 | 0 | REDUCE1BIT(V); |
119 | 0 | Htable[2] = V; |
120 | 0 | REDUCE1BIT(V); |
121 | 0 | Htable[1] = V; |
122 | 0 | Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo; |
123 | 0 | V = Htable[4]; |
124 | 0 | Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo; |
125 | 0 | Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo; |
126 | 0 | Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo; |
127 | 0 | V = Htable[8]; |
128 | 0 | Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo; |
129 | 0 | Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo; |
130 | 0 | Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo; |
131 | 0 | Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo; |
132 | 0 | Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo; |
133 | 0 | Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo; |
134 | 0 | Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo; |
135 | 0 | # endif |
136 | | # if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm)) |
137 | | /* |
138 | | * ARM assembler expects specific dword order in Htable. |
139 | | */ |
140 | | { |
141 | | int j; |
142 | | DECLARE_IS_ENDIAN; |
143 | | |
144 | | if (IS_LITTLE_ENDIAN) |
145 | | for (j = 0; j < 16; ++j) { |
146 | | V = Htable[j]; |
147 | | Htable[j].hi = V.lo; |
148 | | Htable[j].lo = V.hi; |
149 | | } else |
150 | | for (j = 0; j < 16; ++j) { |
151 | | V = Htable[j]; |
152 | | Htable[j].hi = V.lo << 32 | V.lo >> 32; |
153 | | Htable[j].lo = V.hi << 32 | V.hi >> 32; |
154 | | } |
155 | | } |
156 | | # endif |
157 | 0 | } |
158 | | |
159 | | # if !defined(GHASH_ASM) || defined(INCLUDE_C_GMULT_4BIT) |
160 | | static const size_t rem_4bit[16] = { |
161 | | PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460), |
162 | | PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0), |
163 | | PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560), |
164 | | PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0) |
165 | | }; |
166 | | |
167 | | static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]) |
168 | | { |
169 | | u128 Z; |
170 | | int cnt = 15; |
171 | | size_t rem, nlo, nhi; |
172 | | DECLARE_IS_ENDIAN; |
173 | | |
174 | | nlo = ((const u8 *)Xi)[15]; |
175 | | nhi = nlo >> 4; |
176 | | nlo &= 0xf; |
177 | | |
178 | | Z.hi = Htable[nlo].hi; |
179 | | Z.lo = Htable[nlo].lo; |
180 | | |
181 | | while (1) { |
182 | | rem = (size_t)Z.lo & 0xf; |
183 | | Z.lo = (Z.hi << 60) | (Z.lo >> 4); |
184 | | Z.hi = (Z.hi >> 4); |
185 | | if (sizeof(size_t) == 8) |
186 | | Z.hi ^= rem_4bit[rem]; |
187 | | else |
188 | | Z.hi ^= (u64)rem_4bit[rem] << 32; |
189 | | |
190 | | Z.hi ^= Htable[nhi].hi; |
191 | | Z.lo ^= Htable[nhi].lo; |
192 | | |
193 | | if (--cnt < 0) |
194 | | break; |
195 | | |
196 | | nlo = ((const u8 *)Xi)[cnt]; |
197 | | nhi = nlo >> 4; |
198 | | nlo &= 0xf; |
199 | | |
200 | | rem = (size_t)Z.lo & 0xf; |
201 | | Z.lo = (Z.hi << 60) | (Z.lo >> 4); |
202 | | Z.hi = (Z.hi >> 4); |
203 | | if (sizeof(size_t) == 8) |
204 | | Z.hi ^= rem_4bit[rem]; |
205 | | else |
206 | | Z.hi ^= (u64)rem_4bit[rem] << 32; |
207 | | |
208 | | Z.hi ^= Htable[nlo].hi; |
209 | | Z.lo ^= Htable[nlo].lo; |
210 | | } |
211 | | |
212 | | if (IS_LITTLE_ENDIAN) { |
213 | | # ifdef BSWAP8 |
214 | | Xi[0] = BSWAP8(Z.hi); |
215 | | Xi[1] = BSWAP8(Z.lo); |
216 | | # else |
217 | | u8 *p = (u8 *)Xi; |
218 | | u32 v; |
219 | | v = (u32)(Z.hi >> 32); |
220 | | PUTU32(p, v); |
221 | | v = (u32)(Z.hi); |
222 | | PUTU32(p + 4, v); |
223 | | v = (u32)(Z.lo >> 32); |
224 | | PUTU32(p + 8, v); |
225 | | v = (u32)(Z.lo); |
226 | | PUTU32(p + 12, v); |
227 | | # endif |
228 | | } else { |
229 | | Xi[0] = Z.hi; |
230 | | Xi[1] = Z.lo; |
231 | | } |
232 | | } |
233 | | |
234 | | # endif |
235 | | |
236 | | # if !defined(GHASH_ASM) || defined(INCLUDE_C_GHASH_4BIT) |
237 | | # if !defined(OPENSSL_SMALL_FOOTPRINT) |
238 | | /* |
239 | | * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for |
240 | | * details... Compiler-generated code doesn't seem to give any |
241 | | * performance improvement, at least not on x86[_64]. It's here |
242 | | * mostly as reference and a placeholder for possible future |
243 | | * non-trivial optimization[s]... |
244 | | */ |
245 | | static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], |
246 | | const u8 *inp, size_t len) |
247 | | { |
248 | | u128 Z; |
249 | | int cnt; |
250 | | size_t rem, nlo, nhi; |
251 | | DECLARE_IS_ENDIAN; |
252 | | |
253 | | do { |
254 | | cnt = 15; |
255 | | nlo = ((const u8 *)Xi)[15]; |
256 | | nlo ^= inp[15]; |
257 | | nhi = nlo >> 4; |
258 | | nlo &= 0xf; |
259 | | |
260 | | Z.hi = Htable[nlo].hi; |
261 | | Z.lo = Htable[nlo].lo; |
262 | | |
263 | | while (1) { |
264 | | rem = (size_t)Z.lo & 0xf; |
265 | | Z.lo = (Z.hi << 60) | (Z.lo >> 4); |
266 | | Z.hi = (Z.hi >> 4); |
267 | | if (sizeof(size_t) == 8) |
268 | | Z.hi ^= rem_4bit[rem]; |
269 | | else |
270 | | Z.hi ^= (u64)rem_4bit[rem] << 32; |
271 | | |
272 | | Z.hi ^= Htable[nhi].hi; |
273 | | Z.lo ^= Htable[nhi].lo; |
274 | | |
275 | | if (--cnt < 0) |
276 | | break; |
277 | | |
278 | | nlo = ((const u8 *)Xi)[cnt]; |
279 | | nlo ^= inp[cnt]; |
280 | | nhi = nlo >> 4; |
281 | | nlo &= 0xf; |
282 | | |
283 | | rem = (size_t)Z.lo & 0xf; |
284 | | Z.lo = (Z.hi << 60) | (Z.lo >> 4); |
285 | | Z.hi = (Z.hi >> 4); |
286 | | if (sizeof(size_t) == 8) |
287 | | Z.hi ^= rem_4bit[rem]; |
288 | | else |
289 | | Z.hi ^= (u64)rem_4bit[rem] << 32; |
290 | | |
291 | | Z.hi ^= Htable[nlo].hi; |
292 | | Z.lo ^= Htable[nlo].lo; |
293 | | } |
294 | | |
295 | | if (IS_LITTLE_ENDIAN) { |
296 | | # ifdef BSWAP8 |
297 | | Xi[0] = BSWAP8(Z.hi); |
298 | | Xi[1] = BSWAP8(Z.lo); |
299 | | # else |
300 | | u8 *p = (u8 *)Xi; |
301 | | u32 v; |
302 | | v = (u32)(Z.hi >> 32); |
303 | | PUTU32(p, v); |
304 | | v = (u32)(Z.hi); |
305 | | PUTU32(p + 4, v); |
306 | | v = (u32)(Z.lo >> 32); |
307 | | PUTU32(p + 8, v); |
308 | | v = (u32)(Z.lo); |
309 | | PUTU32(p + 12, v); |
310 | | # endif |
311 | | } else { |
312 | | Xi[0] = Z.hi; |
313 | | Xi[1] = Z.lo; |
314 | | } |
315 | | |
316 | | inp += 16; |
317 | | /* Block size is 128 bits so len is a multiple of 16 */ |
318 | | len -= 16; |
319 | | } while (len > 0); |
320 | | } |
321 | | # endif |
322 | | # else |
323 | | void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]); |
324 | | void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
325 | | size_t len); |
326 | | # endif |
327 | | |
328 | 0 | # define GCM_MUL(ctx) ctx->funcs.gmult(ctx->Xi.u,ctx->Htable) |
329 | | # if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT) |
330 | 0 | # define GHASH(ctx,in,len) ctx->funcs.ghash((ctx)->Xi.u,(ctx)->Htable,in,len) |
331 | | /* |
332 | | * GHASH_CHUNK is "stride parameter" missioned to mitigate cache trashing |
333 | | * effect. In other words idea is to hash data while it's still in L1 cache |
334 | | * after encryption pass... |
335 | | */ |
336 | 0 | # define GHASH_CHUNK (3*1024) |
337 | | # endif |
338 | | |
339 | | #if (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ)) |
340 | | # if !defined(I386_ONLY) && \ |
341 | | (defined(__i386) || defined(__i386__) || \ |
342 | | defined(__x86_64) || defined(__x86_64__) || \ |
343 | | defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)) |
344 | | # define GHASH_ASM_X86_OR_64 |
345 | | |
346 | | void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]); |
347 | | void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]); |
348 | | void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
349 | | size_t len); |
350 | | |
351 | | # if defined(__i386) || defined(__i386__) || defined(_M_IX86) |
352 | | # define gcm_init_avx gcm_init_clmul |
353 | | # define gcm_gmult_avx gcm_gmult_clmul |
354 | | # define gcm_ghash_avx gcm_ghash_clmul |
355 | | # else |
356 | | void gcm_init_avx(u128 Htable[16], const u64 Xi[2]); |
357 | | void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]); |
358 | | void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
359 | | size_t len); |
360 | | # endif |
361 | | |
362 | | # if defined(__i386) || defined(__i386__) || defined(_M_IX86) |
363 | | # define GHASH_ASM_X86 |
364 | | void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]); |
365 | | void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
366 | | size_t len); |
367 | | |
368 | | void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]); |
369 | | void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
370 | | size_t len); |
371 | | # endif |
372 | | # elif defined(__arm__) || defined(__arm) || defined(__aarch64__) |
373 | | # include "arm_arch.h" |
374 | | # if __ARM_MAX_ARCH__>=7 |
375 | | # define GHASH_ASM_ARM |
376 | | # define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL) |
377 | | # if defined(__arm__) || defined(__arm) |
378 | | # define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON) |
379 | | # endif |
380 | | void gcm_init_neon(u128 Htable[16], const u64 Xi[2]); |
381 | | void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]); |
382 | | void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
383 | | size_t len); |
384 | | void gcm_init_v8(u128 Htable[16], const u64 Xi[2]); |
385 | | void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]); |
386 | | void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
387 | | size_t len); |
388 | | # endif |
389 | | # elif defined(__sparc__) || defined(__sparc) |
390 | | # include "crypto/sparc_arch.h" |
391 | | # define GHASH_ASM_SPARC |
392 | | void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]); |
393 | | void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]); |
394 | | void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
395 | | size_t len); |
396 | | # elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC)) |
397 | | # include "crypto/ppc_arch.h" |
398 | | # define GHASH_ASM_PPC |
399 | | void gcm_init_p8(u128 Htable[16], const u64 Xi[2]); |
400 | | void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]); |
401 | | void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp, |
402 | | size_t len); |
403 | | # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64 |
404 | | # include "crypto/riscv_arch.h" |
405 | | # define GHASH_ASM_RV64I |
406 | | /* Zbc/Zbkc (scalar crypto with clmul) based routines. */ |
407 | | void gcm_init_rv64i_zbc(u128 Htable[16], const u64 Xi[2]); |
408 | | void gcm_init_rv64i_zbc__zbb(u128 Htable[16], const u64 Xi[2]); |
409 | | void gcm_init_rv64i_zbc__zbkb(u128 Htable[16], const u64 Xi[2]); |
410 | | void gcm_gmult_rv64i_zbc(u64 Xi[2], const u128 Htable[16]); |
411 | | void gcm_gmult_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16]); |
412 | | void gcm_ghash_rv64i_zbc(u64 Xi[2], const u128 Htable[16], |
413 | | const u8 *inp, size_t len); |
414 | | void gcm_ghash_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16], |
415 | | const u8 *inp, size_t len); |
416 | | # endif |
417 | | #endif |
418 | | |
419 | | static void gcm_get_funcs(struct gcm_funcs_st *ctx) |
420 | 0 | { |
421 | | /* set defaults -- overridden below as needed */ |
422 | 0 | ctx->ginit = gcm_init_4bit; |
423 | | #if !defined(GHASH_ASM) |
424 | | ctx->gmult = gcm_gmult_4bit; |
425 | | #else |
426 | 0 | ctx->gmult = NULL; |
427 | 0 | #endif |
428 | | #if !defined(GHASH_ASM) && !defined(OPENSSL_SMALL_FOOTPRINT) |
429 | | ctx->ghash = gcm_ghash_4bit; |
430 | | #else |
431 | 0 | ctx->ghash = NULL; |
432 | 0 | #endif |
433 | |
|
434 | 0 | #if defined(GHASH_ASM_X86_OR_64) |
435 | 0 | # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2) |
436 | | /* x86_64 */ |
437 | 0 | if (OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */ |
438 | 0 | if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */ |
439 | 0 | ctx->ginit = gcm_init_avx; |
440 | 0 | ctx->gmult = gcm_gmult_avx; |
441 | 0 | ctx->ghash = gcm_ghash_avx; |
442 | 0 | } else { |
443 | 0 | ctx->ginit = gcm_init_clmul; |
444 | 0 | ctx->gmult = gcm_gmult_clmul; |
445 | 0 | ctx->ghash = gcm_ghash_clmul; |
446 | 0 | } |
447 | 0 | return; |
448 | 0 | } |
449 | 0 | # endif |
450 | | # if defined(GHASH_ASM_X86) |
451 | | /* x86 only */ |
452 | | # if defined(OPENSSL_IA32_SSE2) |
453 | | if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */ |
454 | | ctx->gmult = gcm_gmult_4bit_mmx; |
455 | | ctx->ghash = gcm_ghash_4bit_mmx; |
456 | | return; |
457 | | } |
458 | | # else |
459 | | if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */ |
460 | | ctx->gmult = gcm_gmult_4bit_mmx; |
461 | | ctx->ghash = gcm_ghash_4bit_mmx; |
462 | | return; |
463 | | } |
464 | | # endif |
465 | | ctx->gmult = gcm_gmult_4bit_x86; |
466 | | ctx->ghash = gcm_ghash_4bit_x86; |
467 | | return; |
468 | | # else |
469 | | /* x86_64 fallback defaults */ |
470 | 0 | ctx->gmult = gcm_gmult_4bit; |
471 | 0 | ctx->ghash = gcm_ghash_4bit; |
472 | 0 | return; |
473 | 0 | # endif |
474 | | #elif defined(GHASH_ASM_ARM) |
475 | | /* ARM defaults */ |
476 | | ctx->gmult = gcm_gmult_4bit; |
477 | | ctx->ghash = gcm_ghash_4bit; |
478 | | # ifdef PMULL_CAPABLE |
479 | | if (PMULL_CAPABLE) { |
480 | | ctx->ginit = (gcm_init_fn)gcm_init_v8; |
481 | | ctx->gmult = gcm_gmult_v8; |
482 | | ctx->ghash = gcm_ghash_v8; |
483 | | } |
484 | | # elif defined(NEON_CAPABLE) |
485 | | if (NEON_CAPABLE) { |
486 | | ctx->ginit = gcm_init_neon; |
487 | | ctx->gmult = gcm_gmult_neon; |
488 | | ctx->ghash = gcm_ghash_neon; |
489 | | } |
490 | | # endif |
491 | | return; |
492 | | #elif defined(GHASH_ASM_SPARC) |
493 | | /* SPARC defaults */ |
494 | | ctx->gmult = gcm_gmult_4bit; |
495 | | ctx->ghash = gcm_ghash_4bit; |
496 | | if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) { |
497 | | ctx->ginit = gcm_init_vis3; |
498 | | ctx->gmult = gcm_gmult_vis3; |
499 | | ctx->ghash = gcm_ghash_vis3; |
500 | | } |
501 | | return; |
502 | | #elif defined(GHASH_ASM_PPC) |
503 | | /* PowerPC does not define GHASH_ASM; defaults set above */ |
504 | | if (OPENSSL_ppccap_P & PPC_CRYPTO207) { |
505 | | ctx->ginit = gcm_init_p8; |
506 | | ctx->gmult = gcm_gmult_p8; |
507 | | ctx->ghash = gcm_ghash_p8; |
508 | | } |
509 | | return; |
510 | | #elif defined(GHASH_ASM_RV64I) |
511 | | /* RISCV defaults */ |
512 | | ctx->gmult = gcm_gmult_4bit; |
513 | | ctx->ghash = gcm_ghash_4bit; |
514 | | |
515 | | if (RISCV_HAS_ZBC()) { |
516 | | if (RISCV_HAS_ZBKB()) { |
517 | | ctx->ginit = gcm_init_rv64i_zbc__zbkb; |
518 | | ctx->gmult = gcm_gmult_rv64i_zbc__zbkb; |
519 | | ctx->ghash = gcm_ghash_rv64i_zbc__zbkb; |
520 | | } else if (RISCV_HAS_ZBB()) { |
521 | | ctx->ginit = gcm_init_rv64i_zbc__zbb; |
522 | | ctx->gmult = gcm_gmult_rv64i_zbc; |
523 | | ctx->ghash = gcm_ghash_rv64i_zbc; |
524 | | } else { |
525 | | ctx->ginit = gcm_init_rv64i_zbc; |
526 | | ctx->gmult = gcm_gmult_rv64i_zbc; |
527 | | ctx->ghash = gcm_ghash_rv64i_zbc; |
528 | | } |
529 | | } |
530 | | return; |
531 | | #elif defined(GHASH_ASM) |
532 | | /* all other architectures use the generic names */ |
533 | | ctx->gmult = gcm_gmult_4bit; |
534 | | ctx->ghash = gcm_ghash_4bit; |
535 | | return; |
536 | | #endif |
537 | 0 | } |
538 | | |
539 | | void ossl_gcm_init_4bit(u128 Htable[16], const u64 H[2]) |
540 | 0 | { |
541 | 0 | struct gcm_funcs_st funcs; |
542 | |
|
543 | 0 | gcm_get_funcs(&funcs); |
544 | 0 | funcs.ginit(Htable, H); |
545 | 0 | } |
546 | | |
547 | | void ossl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]) |
548 | 0 | { |
549 | 0 | struct gcm_funcs_st funcs; |
550 | |
|
551 | 0 | gcm_get_funcs(&funcs); |
552 | 0 | funcs.gmult(Xi, Htable); |
553 | 0 | } |
554 | | |
555 | | void ossl_gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], |
556 | | const u8 *inp, size_t len) |
557 | 0 | { |
558 | 0 | struct gcm_funcs_st funcs; |
559 | 0 | u64 tmp[2]; |
560 | 0 | size_t i; |
561 | |
|
562 | 0 | gcm_get_funcs(&funcs); |
563 | 0 | if (funcs.ghash != NULL) { |
564 | 0 | funcs.ghash(Xi, Htable, inp, len); |
565 | 0 | } else { |
566 | | /* Emulate ghash if needed */ |
567 | 0 | for (i = 0; i < len; i += 16) { |
568 | 0 | memcpy(tmp, &inp[i], sizeof(tmp)); |
569 | 0 | Xi[0] ^= tmp[0]; |
570 | 0 | Xi[1] ^= tmp[1]; |
571 | 0 | funcs.gmult(Xi, Htable); |
572 | 0 | } |
573 | 0 | } |
574 | 0 | } |
575 | | |
576 | | void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block) |
577 | 0 | { |
578 | 0 | DECLARE_IS_ENDIAN; |
579 | |
|
580 | 0 | memset(ctx, 0, sizeof(*ctx)); |
581 | 0 | ctx->block = block; |
582 | 0 | ctx->key = key; |
583 | |
|
584 | 0 | (*block) (ctx->H.c, ctx->H.c, key); |
585 | |
|
586 | 0 | if (IS_LITTLE_ENDIAN) { |
587 | | /* H is stored in host byte order */ |
588 | | #ifdef BSWAP8 |
589 | | ctx->H.u[0] = BSWAP8(ctx->H.u[0]); |
590 | | ctx->H.u[1] = BSWAP8(ctx->H.u[1]); |
591 | | #else |
592 | 0 | u8 *p = ctx->H.c; |
593 | 0 | u64 hi, lo; |
594 | 0 | hi = (u64)GETU32(p) << 32 | GETU32(p + 4); |
595 | 0 | lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); |
596 | 0 | ctx->H.u[0] = hi; |
597 | 0 | ctx->H.u[1] = lo; |
598 | 0 | #endif |
599 | 0 | } |
600 | |
|
601 | 0 | gcm_get_funcs(&ctx->funcs); |
602 | 0 | ctx->funcs.ginit(ctx->Htable, ctx->H.u); |
603 | 0 | } |
604 | | |
605 | | void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv, |
606 | | size_t len) |
607 | 0 | { |
608 | 0 | DECLARE_IS_ENDIAN; |
609 | 0 | unsigned int ctr; |
610 | |
|
611 | 0 | ctx->len.u[0] = 0; /* AAD length */ |
612 | 0 | ctx->len.u[1] = 0; /* message length */ |
613 | 0 | ctx->ares = 0; |
614 | 0 | ctx->mres = 0; |
615 | |
|
616 | 0 | if (len == 12) { |
617 | 0 | memcpy(ctx->Yi.c, iv, 12); |
618 | 0 | ctx->Yi.c[12] = 0; |
619 | 0 | ctx->Yi.c[13] = 0; |
620 | 0 | ctx->Yi.c[14] = 0; |
621 | 0 | ctx->Yi.c[15] = 1; |
622 | 0 | ctr = 1; |
623 | 0 | } else { |
624 | 0 | size_t i; |
625 | 0 | u64 len0 = len; |
626 | | |
627 | | /* Borrow ctx->Xi to calculate initial Yi */ |
628 | 0 | ctx->Xi.u[0] = 0; |
629 | 0 | ctx->Xi.u[1] = 0; |
630 | |
|
631 | 0 | while (len >= 16) { |
632 | 0 | for (i = 0; i < 16; ++i) |
633 | 0 | ctx->Xi.c[i] ^= iv[i]; |
634 | 0 | GCM_MUL(ctx); |
635 | 0 | iv += 16; |
636 | 0 | len -= 16; |
637 | 0 | } |
638 | 0 | if (len) { |
639 | 0 | for (i = 0; i < len; ++i) |
640 | 0 | ctx->Xi.c[i] ^= iv[i]; |
641 | 0 | GCM_MUL(ctx); |
642 | 0 | } |
643 | 0 | len0 <<= 3; |
644 | 0 | if (IS_LITTLE_ENDIAN) { |
645 | | #ifdef BSWAP8 |
646 | | ctx->Xi.u[1] ^= BSWAP8(len0); |
647 | | #else |
648 | 0 | ctx->Xi.c[8] ^= (u8)(len0 >> 56); |
649 | 0 | ctx->Xi.c[9] ^= (u8)(len0 >> 48); |
650 | 0 | ctx->Xi.c[10] ^= (u8)(len0 >> 40); |
651 | 0 | ctx->Xi.c[11] ^= (u8)(len0 >> 32); |
652 | 0 | ctx->Xi.c[12] ^= (u8)(len0 >> 24); |
653 | 0 | ctx->Xi.c[13] ^= (u8)(len0 >> 16); |
654 | 0 | ctx->Xi.c[14] ^= (u8)(len0 >> 8); |
655 | 0 | ctx->Xi.c[15] ^= (u8)(len0); |
656 | 0 | #endif |
657 | 0 | } else { |
658 | 0 | ctx->Xi.u[1] ^= len0; |
659 | 0 | } |
660 | |
|
661 | 0 | GCM_MUL(ctx); |
662 | |
|
663 | 0 | if (IS_LITTLE_ENDIAN) |
664 | | #ifdef BSWAP4 |
665 | | ctr = BSWAP4(ctx->Xi.d[3]); |
666 | | #else |
667 | 0 | ctr = GETU32(ctx->Xi.c + 12); |
668 | 0 | #endif |
669 | 0 | else |
670 | 0 | ctr = ctx->Xi.d[3]; |
671 | | |
672 | | /* Copy borrowed Xi to Yi */ |
673 | 0 | ctx->Yi.u[0] = ctx->Xi.u[0]; |
674 | 0 | ctx->Yi.u[1] = ctx->Xi.u[1]; |
675 | 0 | } |
676 | |
|
677 | 0 | ctx->Xi.u[0] = 0; |
678 | 0 | ctx->Xi.u[1] = 0; |
679 | |
|
680 | 0 | (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key); |
681 | 0 | ++ctr; |
682 | 0 | if (IS_LITTLE_ENDIAN) |
683 | | #ifdef BSWAP4 |
684 | | ctx->Yi.d[3] = BSWAP4(ctr); |
685 | | #else |
686 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
687 | 0 | #endif |
688 | 0 | else |
689 | 0 | ctx->Yi.d[3] = ctr; |
690 | 0 | } |
691 | | |
692 | | int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad, |
693 | | size_t len) |
694 | 0 | { |
695 | 0 | size_t i; |
696 | 0 | unsigned int n; |
697 | 0 | u64 alen = ctx->len.u[0]; |
698 | |
|
699 | 0 | if (ctx->len.u[1]) |
700 | 0 | return -2; |
701 | | |
702 | 0 | alen += len; |
703 | 0 | if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len)) |
704 | 0 | return -1; |
705 | 0 | ctx->len.u[0] = alen; |
706 | |
|
707 | 0 | n = ctx->ares; |
708 | 0 | if (n) { |
709 | 0 | while (n && len) { |
710 | 0 | ctx->Xi.c[n] ^= *(aad++); |
711 | 0 | --len; |
712 | 0 | n = (n + 1) % 16; |
713 | 0 | } |
714 | 0 | if (n == 0) |
715 | 0 | GCM_MUL(ctx); |
716 | 0 | else { |
717 | 0 | ctx->ares = n; |
718 | 0 | return 0; |
719 | 0 | } |
720 | 0 | } |
721 | 0 | #ifdef GHASH |
722 | 0 | if ((i = (len & (size_t)-16))) { |
723 | 0 | GHASH(ctx, aad, i); |
724 | 0 | aad += i; |
725 | 0 | len -= i; |
726 | 0 | } |
727 | | #else |
728 | | while (len >= 16) { |
729 | | for (i = 0; i < 16; ++i) |
730 | | ctx->Xi.c[i] ^= aad[i]; |
731 | | GCM_MUL(ctx); |
732 | | aad += 16; |
733 | | len -= 16; |
734 | | } |
735 | | #endif |
736 | 0 | if (len) { |
737 | 0 | n = (unsigned int)len; |
738 | 0 | for (i = 0; i < len; ++i) |
739 | 0 | ctx->Xi.c[i] ^= aad[i]; |
740 | 0 | } |
741 | |
|
742 | 0 | ctx->ares = n; |
743 | 0 | return 0; |
744 | 0 | } |
745 | | |
746 | | int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, |
747 | | const unsigned char *in, unsigned char *out, |
748 | | size_t len) |
749 | 0 | { |
750 | 0 | DECLARE_IS_ENDIAN; |
751 | 0 | unsigned int n, ctr, mres; |
752 | 0 | size_t i; |
753 | 0 | u64 mlen = ctx->len.u[1]; |
754 | 0 | block128_f block = ctx->block; |
755 | 0 | void *key = ctx->key; |
756 | |
|
757 | 0 | mlen += len; |
758 | 0 | if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) |
759 | 0 | return -1; |
760 | 0 | ctx->len.u[1] = mlen; |
761 | |
|
762 | 0 | mres = ctx->mres; |
763 | |
|
764 | 0 | if (ctx->ares) { |
765 | | /* First call to encrypt finalizes GHASH(AAD) */ |
766 | 0 | #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) |
767 | 0 | if (len == 0) { |
768 | 0 | GCM_MUL(ctx); |
769 | 0 | ctx->ares = 0; |
770 | 0 | return 0; |
771 | 0 | } |
772 | 0 | memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); |
773 | 0 | ctx->Xi.u[0] = 0; |
774 | 0 | ctx->Xi.u[1] = 0; |
775 | 0 | mres = sizeof(ctx->Xi); |
776 | | #else |
777 | | GCM_MUL(ctx); |
778 | | #endif |
779 | 0 | ctx->ares = 0; |
780 | 0 | } |
781 | | |
782 | 0 | if (IS_LITTLE_ENDIAN) |
783 | | #ifdef BSWAP4 |
784 | | ctr = BSWAP4(ctx->Yi.d[3]); |
785 | | #else |
786 | 0 | ctr = GETU32(ctx->Yi.c + 12); |
787 | 0 | #endif |
788 | 0 | else |
789 | 0 | ctr = ctx->Yi.d[3]; |
790 | |
|
791 | 0 | n = mres % 16; |
792 | 0 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
793 | 0 | if (16 % sizeof(size_t) == 0) { /* always true actually */ |
794 | 0 | do { |
795 | 0 | if (n) { |
796 | 0 | # if defined(GHASH) |
797 | 0 | while (n && len) { |
798 | 0 | ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n]; |
799 | 0 | --len; |
800 | 0 | n = (n + 1) % 16; |
801 | 0 | } |
802 | 0 | if (n == 0) { |
803 | 0 | GHASH(ctx, ctx->Xn, mres); |
804 | 0 | mres = 0; |
805 | 0 | } else { |
806 | 0 | ctx->mres = mres; |
807 | 0 | return 0; |
808 | 0 | } |
809 | | # else |
810 | | while (n && len) { |
811 | | ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n]; |
812 | | --len; |
813 | | n = (n + 1) % 16; |
814 | | } |
815 | | if (n == 0) { |
816 | | GCM_MUL(ctx); |
817 | | mres = 0; |
818 | | } else { |
819 | | ctx->mres = n; |
820 | | return 0; |
821 | | } |
822 | | # endif |
823 | 0 | } |
824 | 0 | # if defined(STRICT_ALIGNMENT) |
825 | 0 | if (((size_t)in | (size_t)out) % sizeof(size_t) != 0) |
826 | 0 | break; |
827 | 0 | # endif |
828 | 0 | # if defined(GHASH) |
829 | 0 | if (len >= 16 && mres) { |
830 | 0 | GHASH(ctx, ctx->Xn, mres); |
831 | 0 | mres = 0; |
832 | 0 | } |
833 | 0 | # if defined(GHASH_CHUNK) |
834 | 0 | while (len >= GHASH_CHUNK) { |
835 | 0 | size_t j = GHASH_CHUNK; |
836 | |
|
837 | 0 | while (j) { |
838 | 0 | size_t_aX *out_t = (size_t_aX *)out; |
839 | 0 | const size_t_aX *in_t = (const size_t_aX *)in; |
840 | |
|
841 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
842 | 0 | ++ctr; |
843 | 0 | if (IS_LITTLE_ENDIAN) |
844 | | # ifdef BSWAP4 |
845 | | ctx->Yi.d[3] = BSWAP4(ctr); |
846 | | # else |
847 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
848 | 0 | # endif |
849 | 0 | else |
850 | 0 | ctx->Yi.d[3] = ctr; |
851 | 0 | for (i = 0; i < 16 / sizeof(size_t); ++i) |
852 | 0 | out_t[i] = in_t[i] ^ ctx->EKi.t[i]; |
853 | 0 | out += 16; |
854 | 0 | in += 16; |
855 | 0 | j -= 16; |
856 | 0 | } |
857 | 0 | GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK); |
858 | 0 | len -= GHASH_CHUNK; |
859 | 0 | } |
860 | 0 | # endif |
861 | 0 | if ((i = (len & (size_t)-16))) { |
862 | 0 | size_t j = i; |
863 | |
|
864 | 0 | while (len >= 16) { |
865 | 0 | size_t_aX *out_t = (size_t_aX *)out; |
866 | 0 | const size_t_aX *in_t = (const size_t_aX *)in; |
867 | |
|
868 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
869 | 0 | ++ctr; |
870 | 0 | if (IS_LITTLE_ENDIAN) |
871 | | # ifdef BSWAP4 |
872 | | ctx->Yi.d[3] = BSWAP4(ctr); |
873 | | # else |
874 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
875 | 0 | # endif |
876 | 0 | else |
877 | 0 | ctx->Yi.d[3] = ctr; |
878 | 0 | for (i = 0; i < 16 / sizeof(size_t); ++i) |
879 | 0 | out_t[i] = in_t[i] ^ ctx->EKi.t[i]; |
880 | 0 | out += 16; |
881 | 0 | in += 16; |
882 | 0 | len -= 16; |
883 | 0 | } |
884 | 0 | GHASH(ctx, out - j, j); |
885 | 0 | } |
886 | | # else |
887 | | while (len >= 16) { |
888 | | size_t *out_t = (size_t *)out; |
889 | | const size_t *in_t = (const size_t *)in; |
890 | | |
891 | | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
892 | | ++ctr; |
893 | | if (IS_LITTLE_ENDIAN) |
894 | | # ifdef BSWAP4 |
895 | | ctx->Yi.d[3] = BSWAP4(ctr); |
896 | | # else |
897 | | PUTU32(ctx->Yi.c + 12, ctr); |
898 | | # endif |
899 | | else |
900 | | ctx->Yi.d[3] = ctr; |
901 | | for (i = 0; i < 16 / sizeof(size_t); ++i) |
902 | | ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i]; |
903 | | GCM_MUL(ctx); |
904 | | out += 16; |
905 | | in += 16; |
906 | | len -= 16; |
907 | | } |
908 | | # endif |
909 | 0 | if (len) { |
910 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
911 | 0 | ++ctr; |
912 | 0 | if (IS_LITTLE_ENDIAN) |
913 | | # ifdef BSWAP4 |
914 | | ctx->Yi.d[3] = BSWAP4(ctr); |
915 | | # else |
916 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
917 | 0 | # endif |
918 | 0 | else |
919 | 0 | ctx->Yi.d[3] = ctr; |
920 | 0 | # if defined(GHASH) |
921 | 0 | while (len--) { |
922 | 0 | ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n]; |
923 | 0 | ++n; |
924 | 0 | } |
925 | | # else |
926 | | while (len--) { |
927 | | ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n]; |
928 | | ++n; |
929 | | } |
930 | | mres = n; |
931 | | # endif |
932 | 0 | } |
933 | |
|
934 | 0 | ctx->mres = mres; |
935 | 0 | return 0; |
936 | 0 | } while (0); |
937 | 0 | } |
938 | 0 | #endif |
939 | 0 | for (i = 0; i < len; ++i) { |
940 | 0 | if (n == 0) { |
941 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
942 | 0 | ++ctr; |
943 | 0 | if (IS_LITTLE_ENDIAN) |
944 | | #ifdef BSWAP4 |
945 | | ctx->Yi.d[3] = BSWAP4(ctr); |
946 | | #else |
947 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
948 | 0 | #endif |
949 | 0 | else |
950 | 0 | ctx->Yi.d[3] = ctr; |
951 | 0 | } |
952 | 0 | #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) |
953 | 0 | ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n]; |
954 | 0 | n = (n + 1) % 16; |
955 | 0 | if (mres == sizeof(ctx->Xn)) { |
956 | 0 | GHASH(ctx,ctx->Xn,sizeof(ctx->Xn)); |
957 | 0 | mres = 0; |
958 | 0 | } |
959 | | #else |
960 | | ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n]; |
961 | | mres = n = (n + 1) % 16; |
962 | | if (n == 0) |
963 | | GCM_MUL(ctx); |
964 | | #endif |
965 | 0 | } |
966 | |
|
967 | 0 | ctx->mres = mres; |
968 | 0 | return 0; |
969 | 0 | } |
970 | | |
971 | | int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, |
972 | | const unsigned char *in, unsigned char *out, |
973 | | size_t len) |
974 | 0 | { |
975 | 0 | DECLARE_IS_ENDIAN; |
976 | 0 | unsigned int n, ctr, mres; |
977 | 0 | size_t i; |
978 | 0 | u64 mlen = ctx->len.u[1]; |
979 | 0 | block128_f block = ctx->block; |
980 | 0 | void *key = ctx->key; |
981 | |
|
982 | 0 | mlen += len; |
983 | 0 | if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) |
984 | 0 | return -1; |
985 | 0 | ctx->len.u[1] = mlen; |
986 | |
|
987 | 0 | mres = ctx->mres; |
988 | |
|
989 | 0 | if (ctx->ares) { |
990 | | /* First call to decrypt finalizes GHASH(AAD) */ |
991 | 0 | #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) |
992 | 0 | if (len == 0) { |
993 | 0 | GCM_MUL(ctx); |
994 | 0 | ctx->ares = 0; |
995 | 0 | return 0; |
996 | 0 | } |
997 | 0 | memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); |
998 | 0 | ctx->Xi.u[0] = 0; |
999 | 0 | ctx->Xi.u[1] = 0; |
1000 | 0 | mres = sizeof(ctx->Xi); |
1001 | | #else |
1002 | | GCM_MUL(ctx); |
1003 | | #endif |
1004 | 0 | ctx->ares = 0; |
1005 | 0 | } |
1006 | | |
1007 | 0 | if (IS_LITTLE_ENDIAN) |
1008 | | #ifdef BSWAP4 |
1009 | | ctr = BSWAP4(ctx->Yi.d[3]); |
1010 | | #else |
1011 | 0 | ctr = GETU32(ctx->Yi.c + 12); |
1012 | 0 | #endif |
1013 | 0 | else |
1014 | 0 | ctr = ctx->Yi.d[3]; |
1015 | |
|
1016 | 0 | n = mres % 16; |
1017 | 0 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
1018 | 0 | if (16 % sizeof(size_t) == 0) { /* always true actually */ |
1019 | 0 | do { |
1020 | 0 | if (n) { |
1021 | 0 | # if defined(GHASH) |
1022 | 0 | while (n && len) { |
1023 | 0 | *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n]; |
1024 | 0 | --len; |
1025 | 0 | n = (n + 1) % 16; |
1026 | 0 | } |
1027 | 0 | if (n == 0) { |
1028 | 0 | GHASH(ctx, ctx->Xn, mres); |
1029 | 0 | mres = 0; |
1030 | 0 | } else { |
1031 | 0 | ctx->mres = mres; |
1032 | 0 | return 0; |
1033 | 0 | } |
1034 | | # else |
1035 | | while (n && len) { |
1036 | | u8 c = *(in++); |
1037 | | *(out++) = c ^ ctx->EKi.c[n]; |
1038 | | ctx->Xi.c[n] ^= c; |
1039 | | --len; |
1040 | | n = (n + 1) % 16; |
1041 | | } |
1042 | | if (n == 0) { |
1043 | | GCM_MUL(ctx); |
1044 | | mres = 0; |
1045 | | } else { |
1046 | | ctx->mres = n; |
1047 | | return 0; |
1048 | | } |
1049 | | # endif |
1050 | 0 | } |
1051 | 0 | # if defined(STRICT_ALIGNMENT) |
1052 | 0 | if (((size_t)in | (size_t)out) % sizeof(size_t) != 0) |
1053 | 0 | break; |
1054 | 0 | # endif |
1055 | 0 | # if defined(GHASH) |
1056 | 0 | if (len >= 16 && mres) { |
1057 | 0 | GHASH(ctx, ctx->Xn, mres); |
1058 | 0 | mres = 0; |
1059 | 0 | } |
1060 | 0 | # if defined(GHASH_CHUNK) |
1061 | 0 | while (len >= GHASH_CHUNK) { |
1062 | 0 | size_t j = GHASH_CHUNK; |
1063 | |
|
1064 | 0 | GHASH(ctx, in, GHASH_CHUNK); |
1065 | 0 | while (j) { |
1066 | 0 | size_t_aX *out_t = (size_t_aX *)out; |
1067 | 0 | const size_t_aX *in_t = (const size_t_aX *)in; |
1068 | |
|
1069 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
1070 | 0 | ++ctr; |
1071 | 0 | if (IS_LITTLE_ENDIAN) |
1072 | | # ifdef BSWAP4 |
1073 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1074 | | # else |
1075 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1076 | 0 | # endif |
1077 | 0 | else |
1078 | 0 | ctx->Yi.d[3] = ctr; |
1079 | 0 | for (i = 0; i < 16 / sizeof(size_t); ++i) |
1080 | 0 | out_t[i] = in_t[i] ^ ctx->EKi.t[i]; |
1081 | 0 | out += 16; |
1082 | 0 | in += 16; |
1083 | 0 | j -= 16; |
1084 | 0 | } |
1085 | 0 | len -= GHASH_CHUNK; |
1086 | 0 | } |
1087 | 0 | # endif |
1088 | 0 | if ((i = (len & (size_t)-16))) { |
1089 | 0 | GHASH(ctx, in, i); |
1090 | 0 | while (len >= 16) { |
1091 | 0 | size_t_aX *out_t = (size_t_aX *)out; |
1092 | 0 | const size_t_aX *in_t = (const size_t_aX *)in; |
1093 | |
|
1094 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
1095 | 0 | ++ctr; |
1096 | 0 | if (IS_LITTLE_ENDIAN) |
1097 | | # ifdef BSWAP4 |
1098 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1099 | | # else |
1100 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1101 | 0 | # endif |
1102 | 0 | else |
1103 | 0 | ctx->Yi.d[3] = ctr; |
1104 | 0 | for (i = 0; i < 16 / sizeof(size_t); ++i) |
1105 | 0 | out_t[i] = in_t[i] ^ ctx->EKi.t[i]; |
1106 | 0 | out += 16; |
1107 | 0 | in += 16; |
1108 | 0 | len -= 16; |
1109 | 0 | } |
1110 | 0 | } |
1111 | | # else |
1112 | | while (len >= 16) { |
1113 | | size_t *out_t = (size_t *)out; |
1114 | | const size_t *in_t = (const size_t *)in; |
1115 | | |
1116 | | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
1117 | | ++ctr; |
1118 | | if (IS_LITTLE_ENDIAN) |
1119 | | # ifdef BSWAP4 |
1120 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1121 | | # else |
1122 | | PUTU32(ctx->Yi.c + 12, ctr); |
1123 | | # endif |
1124 | | else |
1125 | | ctx->Yi.d[3] = ctr; |
1126 | | for (i = 0; i < 16 / sizeof(size_t); ++i) { |
1127 | | size_t c = in_t[i]; |
1128 | | out_t[i] = c ^ ctx->EKi.t[i]; |
1129 | | ctx->Xi.t[i] ^= c; |
1130 | | } |
1131 | | GCM_MUL(ctx); |
1132 | | out += 16; |
1133 | | in += 16; |
1134 | | len -= 16; |
1135 | | } |
1136 | | # endif |
1137 | 0 | if (len) { |
1138 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
1139 | 0 | ++ctr; |
1140 | 0 | if (IS_LITTLE_ENDIAN) |
1141 | | # ifdef BSWAP4 |
1142 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1143 | | # else |
1144 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1145 | 0 | # endif |
1146 | 0 | else |
1147 | 0 | ctx->Yi.d[3] = ctr; |
1148 | 0 | # if defined(GHASH) |
1149 | 0 | while (len--) { |
1150 | 0 | out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n]; |
1151 | 0 | ++n; |
1152 | 0 | } |
1153 | | # else |
1154 | | while (len--) { |
1155 | | u8 c = in[n]; |
1156 | | ctx->Xi.c[n] ^= c; |
1157 | | out[n] = c ^ ctx->EKi.c[n]; |
1158 | | ++n; |
1159 | | } |
1160 | | mres = n; |
1161 | | # endif |
1162 | 0 | } |
1163 | |
|
1164 | 0 | ctx->mres = mres; |
1165 | 0 | return 0; |
1166 | 0 | } while (0); |
1167 | 0 | } |
1168 | 0 | #endif |
1169 | 0 | for (i = 0; i < len; ++i) { |
1170 | 0 | u8 c; |
1171 | 0 | if (n == 0) { |
1172 | 0 | (*block) (ctx->Yi.c, ctx->EKi.c, key); |
1173 | 0 | ++ctr; |
1174 | 0 | if (IS_LITTLE_ENDIAN) |
1175 | | #ifdef BSWAP4 |
1176 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1177 | | #else |
1178 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1179 | 0 | #endif |
1180 | 0 | else |
1181 | 0 | ctx->Yi.d[3] = ctr; |
1182 | 0 | } |
1183 | 0 | #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) |
1184 | 0 | out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n]; |
1185 | 0 | n = (n + 1) % 16; |
1186 | 0 | if (mres == sizeof(ctx->Xn)) { |
1187 | 0 | GHASH(ctx,ctx->Xn,sizeof(ctx->Xn)); |
1188 | 0 | mres = 0; |
1189 | 0 | } |
1190 | | #else |
1191 | | c = in[i]; |
1192 | | out[i] = c ^ ctx->EKi.c[n]; |
1193 | | ctx->Xi.c[n] ^= c; |
1194 | | mres = n = (n + 1) % 16; |
1195 | | if (n == 0) |
1196 | | GCM_MUL(ctx); |
1197 | | #endif |
1198 | 0 | } |
1199 | |
|
1200 | 0 | ctx->mres = mres; |
1201 | 0 | return 0; |
1202 | 0 | } |
1203 | | |
1204 | | int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, |
1205 | | const unsigned char *in, unsigned char *out, |
1206 | | size_t len, ctr128_f stream) |
1207 | 0 | { |
1208 | | #if defined(OPENSSL_SMALL_FOOTPRINT) |
1209 | | return CRYPTO_gcm128_encrypt(ctx, in, out, len); |
1210 | | #else |
1211 | 0 | DECLARE_IS_ENDIAN; |
1212 | 0 | unsigned int n, ctr, mres; |
1213 | 0 | size_t i; |
1214 | 0 | u64 mlen = ctx->len.u[1]; |
1215 | 0 | void *key = ctx->key; |
1216 | |
|
1217 | 0 | mlen += len; |
1218 | 0 | if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) |
1219 | 0 | return -1; |
1220 | 0 | ctx->len.u[1] = mlen; |
1221 | |
|
1222 | 0 | mres = ctx->mres; |
1223 | |
|
1224 | 0 | if (ctx->ares) { |
1225 | | /* First call to encrypt finalizes GHASH(AAD) */ |
1226 | 0 | #if defined(GHASH) |
1227 | 0 | if (len == 0) { |
1228 | 0 | GCM_MUL(ctx); |
1229 | 0 | ctx->ares = 0; |
1230 | 0 | return 0; |
1231 | 0 | } |
1232 | 0 | memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); |
1233 | 0 | ctx->Xi.u[0] = 0; |
1234 | 0 | ctx->Xi.u[1] = 0; |
1235 | 0 | mres = sizeof(ctx->Xi); |
1236 | | #else |
1237 | | GCM_MUL(ctx); |
1238 | | #endif |
1239 | 0 | ctx->ares = 0; |
1240 | 0 | } |
1241 | | |
1242 | 0 | if (IS_LITTLE_ENDIAN) |
1243 | | # ifdef BSWAP4 |
1244 | | ctr = BSWAP4(ctx->Yi.d[3]); |
1245 | | # else |
1246 | 0 | ctr = GETU32(ctx->Yi.c + 12); |
1247 | 0 | # endif |
1248 | 0 | else |
1249 | 0 | ctr = ctx->Yi.d[3]; |
1250 | |
|
1251 | 0 | n = mres % 16; |
1252 | 0 | if (n) { |
1253 | 0 | # if defined(GHASH) |
1254 | 0 | while (n && len) { |
1255 | 0 | ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n]; |
1256 | 0 | --len; |
1257 | 0 | n = (n + 1) % 16; |
1258 | 0 | } |
1259 | 0 | if (n == 0) { |
1260 | 0 | GHASH(ctx, ctx->Xn, mres); |
1261 | 0 | mres = 0; |
1262 | 0 | } else { |
1263 | 0 | ctx->mres = mres; |
1264 | 0 | return 0; |
1265 | 0 | } |
1266 | | # else |
1267 | | while (n && len) { |
1268 | | ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n]; |
1269 | | --len; |
1270 | | n = (n + 1) % 16; |
1271 | | } |
1272 | | if (n == 0) { |
1273 | | GCM_MUL(ctx); |
1274 | | mres = 0; |
1275 | | } else { |
1276 | | ctx->mres = n; |
1277 | | return 0; |
1278 | | } |
1279 | | # endif |
1280 | 0 | } |
1281 | 0 | # if defined(GHASH) |
1282 | 0 | if (len >= 16 && mres) { |
1283 | 0 | GHASH(ctx, ctx->Xn, mres); |
1284 | 0 | mres = 0; |
1285 | 0 | } |
1286 | 0 | # if defined(GHASH_CHUNK) |
1287 | 0 | while (len >= GHASH_CHUNK) { |
1288 | 0 | (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c); |
1289 | 0 | ctr += GHASH_CHUNK / 16; |
1290 | 0 | if (IS_LITTLE_ENDIAN) |
1291 | | # ifdef BSWAP4 |
1292 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1293 | | # else |
1294 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1295 | 0 | # endif |
1296 | 0 | else |
1297 | 0 | ctx->Yi.d[3] = ctr; |
1298 | 0 | GHASH(ctx, out, GHASH_CHUNK); |
1299 | 0 | out += GHASH_CHUNK; |
1300 | 0 | in += GHASH_CHUNK; |
1301 | 0 | len -= GHASH_CHUNK; |
1302 | 0 | } |
1303 | 0 | # endif |
1304 | 0 | # endif |
1305 | 0 | if ((i = (len & (size_t)-16))) { |
1306 | 0 | size_t j = i / 16; |
1307 | |
|
1308 | 0 | (*stream) (in, out, j, key, ctx->Yi.c); |
1309 | 0 | ctr += (unsigned int)j; |
1310 | 0 | if (IS_LITTLE_ENDIAN) |
1311 | | # ifdef BSWAP4 |
1312 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1313 | | # else |
1314 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1315 | 0 | # endif |
1316 | 0 | else |
1317 | 0 | ctx->Yi.d[3] = ctr; |
1318 | 0 | in += i; |
1319 | 0 | len -= i; |
1320 | 0 | # if defined(GHASH) |
1321 | 0 | GHASH(ctx, out, i); |
1322 | 0 | out += i; |
1323 | | # else |
1324 | | while (j--) { |
1325 | | for (i = 0; i < 16; ++i) |
1326 | | ctx->Xi.c[i] ^= out[i]; |
1327 | | GCM_MUL(ctx); |
1328 | | out += 16; |
1329 | | } |
1330 | | # endif |
1331 | 0 | } |
1332 | 0 | if (len) { |
1333 | 0 | (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key); |
1334 | 0 | ++ctr; |
1335 | 0 | if (IS_LITTLE_ENDIAN) |
1336 | | # ifdef BSWAP4 |
1337 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1338 | | # else |
1339 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1340 | 0 | # endif |
1341 | 0 | else |
1342 | 0 | ctx->Yi.d[3] = ctr; |
1343 | 0 | while (len--) { |
1344 | 0 | # if defined(GHASH) |
1345 | 0 | ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n]; |
1346 | | # else |
1347 | | ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n]; |
1348 | | # endif |
1349 | 0 | ++n; |
1350 | 0 | } |
1351 | 0 | } |
1352 | |
|
1353 | 0 | ctx->mres = mres; |
1354 | 0 | return 0; |
1355 | 0 | #endif |
1356 | 0 | } |
1357 | | |
1358 | | int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, |
1359 | | const unsigned char *in, unsigned char *out, |
1360 | | size_t len, ctr128_f stream) |
1361 | 0 | { |
1362 | | #if defined(OPENSSL_SMALL_FOOTPRINT) |
1363 | | return CRYPTO_gcm128_decrypt(ctx, in, out, len); |
1364 | | #else |
1365 | 0 | DECLARE_IS_ENDIAN; |
1366 | 0 | unsigned int n, ctr, mres; |
1367 | 0 | size_t i; |
1368 | 0 | u64 mlen = ctx->len.u[1]; |
1369 | 0 | void *key = ctx->key; |
1370 | |
|
1371 | 0 | mlen += len; |
1372 | 0 | if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) |
1373 | 0 | return -1; |
1374 | 0 | ctx->len.u[1] = mlen; |
1375 | |
|
1376 | 0 | mres = ctx->mres; |
1377 | |
|
1378 | 0 | if (ctx->ares) { |
1379 | | /* First call to decrypt finalizes GHASH(AAD) */ |
1380 | 0 | # if defined(GHASH) |
1381 | 0 | if (len == 0) { |
1382 | 0 | GCM_MUL(ctx); |
1383 | 0 | ctx->ares = 0; |
1384 | 0 | return 0; |
1385 | 0 | } |
1386 | 0 | memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); |
1387 | 0 | ctx->Xi.u[0] = 0; |
1388 | 0 | ctx->Xi.u[1] = 0; |
1389 | 0 | mres = sizeof(ctx->Xi); |
1390 | | # else |
1391 | | GCM_MUL(ctx); |
1392 | | # endif |
1393 | 0 | ctx->ares = 0; |
1394 | 0 | } |
1395 | | |
1396 | 0 | if (IS_LITTLE_ENDIAN) |
1397 | | # ifdef BSWAP4 |
1398 | | ctr = BSWAP4(ctx->Yi.d[3]); |
1399 | | # else |
1400 | 0 | ctr = GETU32(ctx->Yi.c + 12); |
1401 | 0 | # endif |
1402 | 0 | else |
1403 | 0 | ctr = ctx->Yi.d[3]; |
1404 | |
|
1405 | 0 | n = mres % 16; |
1406 | 0 | if (n) { |
1407 | 0 | # if defined(GHASH) |
1408 | 0 | while (n && len) { |
1409 | 0 | *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n]; |
1410 | 0 | --len; |
1411 | 0 | n = (n + 1) % 16; |
1412 | 0 | } |
1413 | 0 | if (n == 0) { |
1414 | 0 | GHASH(ctx, ctx->Xn, mres); |
1415 | 0 | mres = 0; |
1416 | 0 | } else { |
1417 | 0 | ctx->mres = mres; |
1418 | 0 | return 0; |
1419 | 0 | } |
1420 | | # else |
1421 | | while (n && len) { |
1422 | | u8 c = *(in++); |
1423 | | *(out++) = c ^ ctx->EKi.c[n]; |
1424 | | ctx->Xi.c[n] ^= c; |
1425 | | --len; |
1426 | | n = (n + 1) % 16; |
1427 | | } |
1428 | | if (n == 0) { |
1429 | | GCM_MUL(ctx); |
1430 | | mres = 0; |
1431 | | } else { |
1432 | | ctx->mres = n; |
1433 | | return 0; |
1434 | | } |
1435 | | # endif |
1436 | 0 | } |
1437 | 0 | # if defined(GHASH) |
1438 | 0 | if (len >= 16 && mres) { |
1439 | 0 | GHASH(ctx, ctx->Xn, mres); |
1440 | 0 | mres = 0; |
1441 | 0 | } |
1442 | 0 | # if defined(GHASH_CHUNK) |
1443 | 0 | while (len >= GHASH_CHUNK) { |
1444 | 0 | GHASH(ctx, in, GHASH_CHUNK); |
1445 | 0 | (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c); |
1446 | 0 | ctr += GHASH_CHUNK / 16; |
1447 | 0 | if (IS_LITTLE_ENDIAN) |
1448 | | # ifdef BSWAP4 |
1449 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1450 | | # else |
1451 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1452 | 0 | # endif |
1453 | 0 | else |
1454 | 0 | ctx->Yi.d[3] = ctr; |
1455 | 0 | out += GHASH_CHUNK; |
1456 | 0 | in += GHASH_CHUNK; |
1457 | 0 | len -= GHASH_CHUNK; |
1458 | 0 | } |
1459 | 0 | # endif |
1460 | 0 | # endif |
1461 | 0 | if ((i = (len & (size_t)-16))) { |
1462 | 0 | size_t j = i / 16; |
1463 | |
|
1464 | 0 | # if defined(GHASH) |
1465 | 0 | GHASH(ctx, in, i); |
1466 | | # else |
1467 | | while (j--) { |
1468 | | size_t k; |
1469 | | for (k = 0; k < 16; ++k) |
1470 | | ctx->Xi.c[k] ^= in[k]; |
1471 | | GCM_MUL(ctx); |
1472 | | in += 16; |
1473 | | } |
1474 | | j = i / 16; |
1475 | | in -= i; |
1476 | | # endif |
1477 | 0 | (*stream) (in, out, j, key, ctx->Yi.c); |
1478 | 0 | ctr += (unsigned int)j; |
1479 | 0 | if (IS_LITTLE_ENDIAN) |
1480 | | # ifdef BSWAP4 |
1481 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1482 | | # else |
1483 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1484 | 0 | # endif |
1485 | 0 | else |
1486 | 0 | ctx->Yi.d[3] = ctr; |
1487 | 0 | out += i; |
1488 | 0 | in += i; |
1489 | 0 | len -= i; |
1490 | 0 | } |
1491 | 0 | if (len) { |
1492 | 0 | (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key); |
1493 | 0 | ++ctr; |
1494 | 0 | if (IS_LITTLE_ENDIAN) |
1495 | | # ifdef BSWAP4 |
1496 | | ctx->Yi.d[3] = BSWAP4(ctr); |
1497 | | # else |
1498 | 0 | PUTU32(ctx->Yi.c + 12, ctr); |
1499 | 0 | # endif |
1500 | 0 | else |
1501 | 0 | ctx->Yi.d[3] = ctr; |
1502 | 0 | while (len--) { |
1503 | 0 | # if defined(GHASH) |
1504 | 0 | out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n]; |
1505 | | # else |
1506 | | u8 c = in[n]; |
1507 | | ctx->Xi.c[mres++] ^= c; |
1508 | | out[n] = c ^ ctx->EKi.c[n]; |
1509 | | # endif |
1510 | 0 | ++n; |
1511 | 0 | } |
1512 | 0 | } |
1513 | |
|
1514 | 0 | ctx->mres = mres; |
1515 | 0 | return 0; |
1516 | 0 | #endif |
1517 | 0 | } |
1518 | | |
1519 | | int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag, |
1520 | | size_t len) |
1521 | 0 | { |
1522 | 0 | DECLARE_IS_ENDIAN; |
1523 | 0 | u64 alen = ctx->len.u[0] << 3; |
1524 | 0 | u64 clen = ctx->len.u[1] << 3; |
1525 | |
|
1526 | 0 | #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) |
1527 | 0 | u128 bitlen; |
1528 | 0 | unsigned int mres = ctx->mres; |
1529 | |
|
1530 | 0 | if (mres) { |
1531 | 0 | unsigned blocks = (mres + 15) & -16; |
1532 | |
|
1533 | 0 | memset(ctx->Xn + mres, 0, blocks - mres); |
1534 | 0 | mres = blocks; |
1535 | 0 | if (mres == sizeof(ctx->Xn)) { |
1536 | 0 | GHASH(ctx, ctx->Xn, mres); |
1537 | 0 | mres = 0; |
1538 | 0 | } |
1539 | 0 | } else if (ctx->ares) { |
1540 | 0 | GCM_MUL(ctx); |
1541 | 0 | } |
1542 | | #else |
1543 | | if (ctx->mres || ctx->ares) |
1544 | | GCM_MUL(ctx); |
1545 | | #endif |
1546 | |
|
1547 | 0 | if (IS_LITTLE_ENDIAN) { |
1548 | | #ifdef BSWAP8 |
1549 | | alen = BSWAP8(alen); |
1550 | | clen = BSWAP8(clen); |
1551 | | #else |
1552 | 0 | u8 *p = ctx->len.c; |
1553 | |
|
1554 | 0 | ctx->len.u[0] = alen; |
1555 | 0 | ctx->len.u[1] = clen; |
1556 | |
|
1557 | 0 | alen = (u64)GETU32(p) << 32 | GETU32(p + 4); |
1558 | 0 | clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); |
1559 | 0 | #endif |
1560 | 0 | } |
1561 | |
|
1562 | 0 | #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) |
1563 | 0 | bitlen.hi = alen; |
1564 | 0 | bitlen.lo = clen; |
1565 | 0 | memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen)); |
1566 | 0 | mres += sizeof(bitlen); |
1567 | 0 | GHASH(ctx, ctx->Xn, mres); |
1568 | | #else |
1569 | | ctx->Xi.u[0] ^= alen; |
1570 | | ctx->Xi.u[1] ^= clen; |
1571 | | GCM_MUL(ctx); |
1572 | | #endif |
1573 | |
|
1574 | 0 | ctx->Xi.u[0] ^= ctx->EK0.u[0]; |
1575 | 0 | ctx->Xi.u[1] ^= ctx->EK0.u[1]; |
1576 | |
|
1577 | 0 | if (tag && len <= sizeof(ctx->Xi)) |
1578 | 0 | return CRYPTO_memcmp(ctx->Xi.c, tag, len); |
1579 | 0 | else |
1580 | 0 | return -1; |
1581 | 0 | } |
1582 | | |
1583 | | void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) |
1584 | 0 | { |
1585 | 0 | CRYPTO_gcm128_finish(ctx, NULL, 0); |
1586 | 0 | memcpy(tag, ctx->Xi.c, |
1587 | 0 | len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c)); |
1588 | 0 | } |
1589 | | |
1590 | | GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block) |
1591 | 0 | { |
1592 | 0 | GCM128_CONTEXT *ret; |
1593 | |
|
1594 | 0 | if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) |
1595 | 0 | CRYPTO_gcm128_init(ret, key, block); |
1596 | |
|
1597 | 0 | return ret; |
1598 | 0 | } |
1599 | | |
1600 | | void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx) |
1601 | 0 | { |
1602 | 0 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
1603 | 0 | } |