Coverage Report

Created: 2026-08-17 07:16

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