Coverage Report

Created: 2026-10-16 07:31

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-2026 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
/*
728
 * Returns:
729
 * -  0: success
730
 * - -1: error, AAD length overflow
731
 * - -2: error, AAD follows the payload -> out of order
732
 */
733
int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
734
    size_t len)
735
0
{
736
0
    size_t i;
737
0
    unsigned int n;
738
0
    uint64_t alen = ctx->len.u[0];
739
740
0
    if (ctx->len.u[1])
741
0
        return -2;
742
743
0
    alen += len;
744
0
    if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
745
0
        return -1;
746
0
    ctx->len.u[0] = alen;
747
748
0
    n = ctx->ares;
749
0
    if (n) {
750
0
        while (n && len) {
751
0
            ctx->Xi.c[n] ^= *(aad++);
752
0
            --len;
753
0
            n = (n + 1) % 16;
754
0
        }
755
0
        if (n == 0)
756
0
            GCM_MUL(ctx);
757
0
        else {
758
0
            ctx->ares = n;
759
0
            return 0;
760
0
        }
761
0
    }
762
0
#ifdef GHASH
763
0
    if ((i = (len & (size_t)-16))) {
764
0
        GHASH(ctx, aad, i);
765
0
        aad += i;
766
0
        len -= i;
767
0
    }
768
#else
769
    while (len >= 16) {
770
        for (i = 0; i < 16; ++i)
771
            ctx->Xi.c[i] ^= aad[i];
772
        GCM_MUL(ctx);
773
        aad += 16;
774
        len -= 16;
775
    }
776
#endif
777
0
    if (len) {
778
0
        n = (unsigned int)len;
779
0
        for (i = 0; i < len; ++i)
780
0
            ctx->Xi.c[i] ^= aad[i];
781
0
    }
782
783
0
    ctx->ares = n;
784
0
    return 0;
785
0
}
786
787
int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
788
    const unsigned char *in, unsigned char *out,
789
    size_t len)
790
0
{
791
0
    DECLARE_IS_ENDIAN;
792
0
    unsigned int n, ctr, mres;
793
0
    size_t i;
794
0
    uint64_t mlen = ctx->len.u[1];
795
0
    block128_f block = ctx->block;
796
0
    void *key = ctx->key;
797
798
0
    mlen += len;
799
0
    if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
800
0
        return -1;
801
0
    ctx->len.u[1] = mlen;
802
803
0
    mres = ctx->mres;
804
805
0
    if (ctx->ares) {
806
        /* First call to encrypt finalizes GHASH(AAD) */
807
0
#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
808
0
        if (len == 0) {
809
0
            GCM_MUL(ctx);
810
0
            ctx->ares = 0;
811
0
            return 0;
812
0
        }
813
0
        memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
814
0
        ctx->Xi.u[0] = 0;
815
0
        ctx->Xi.u[1] = 0;
816
0
        mres = sizeof(ctx->Xi);
817
#else
818
        GCM_MUL(ctx);
819
#endif
820
0
        ctx->ares = 0;
821
0
    }
822
823
0
    if (IS_LITTLE_ENDIAN)
824
0
#ifdef BSWAP4
825
0
        ctr = BSWAP4(ctx->Yi.d[3]);
826
#else
827
        ctr = GETU32(ctx->Yi.c + 12);
828
#endif
829
0
    else
830
0
        ctr = ctx->Yi.d[3];
831
832
0
    n = mres % 16;
833
0
#if !defined(OPENSSL_SMALL_FOOTPRINT)
834
0
    if (16 % sizeof(size_t) == 0) { /* always true actually */
835
0
        do {
836
0
            if (n) {
837
0
#if defined(GHASH)
838
0
                while (n && len) {
839
0
                    ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
840
0
                    --len;
841
0
                    n = (n + 1) % 16;
842
0
                }
843
0
                if (n == 0) {
844
0
                    GHASH(ctx, ctx->Xn, mres);
845
0
                    mres = 0;
846
0
                } else {
847
0
                    ctx->mres = mres;
848
0
                    return 0;
849
0
                }
850
#else
851
                while (n && len) {
852
                    ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
853
                    --len;
854
                    n = (n + 1) % 16;
855
                }
856
                if (n == 0) {
857
                    GCM_MUL(ctx);
858
                    mres = 0;
859
                } else {
860
                    ctx->mres = n;
861
                    return 0;
862
                }
863
#endif
864
0
            }
865
#if defined(STRICT_ALIGNMENT)
866
            if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
867
                break;
868
#endif
869
0
#if defined(GHASH)
870
0
            if (len >= 16 && mres) {
871
0
                GHASH(ctx, ctx->Xn, mres);
872
0
                mres = 0;
873
0
            }
874
0
#if defined(GHASH_CHUNK)
875
0
            while (len >= GHASH_CHUNK) {
876
0
                size_t j = GHASH_CHUNK;
877
878
0
                while (j) {
879
0
                    size_t_aX *out_t = (size_t_aX *)out;
880
0
                    const size_t_aX *in_t = (const size_t_aX *)in;
881
882
0
                    (*block)(ctx->Yi.c, ctx->EKi.c, key);
883
0
                    ++ctr;
884
0
                    if (IS_LITTLE_ENDIAN)
885
0
#ifdef BSWAP4
886
0
                        ctx->Yi.d[3] = BSWAP4(ctr);
887
#else
888
                        PUTU32(ctx->Yi.c + 12, ctr);
889
#endif
890
0
                    else
891
0
                        ctx->Yi.d[3] = ctr;
892
0
                    for (i = 0; i < 16 / sizeof(size_t); ++i)
893
0
                        out_t[i] = in_t[i] ^ ctx->EKi.t[i];
894
0
                    out += 16;
895
0
                    in += 16;
896
0
                    j -= 16;
897
0
                }
898
0
                GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
899
0
                len -= GHASH_CHUNK;
900
0
            }
901
0
#endif
902
0
            if ((i = (len & (size_t)-16))) {
903
0
                size_t j = i;
904
905
0
                while (len >= 16) {
906
0
                    size_t_aX *out_t = (size_t_aX *)out;
907
0
                    const size_t_aX *in_t = (const size_t_aX *)in;
908
909
0
                    (*block)(ctx->Yi.c, ctx->EKi.c, key);
910
0
                    ++ctr;
911
0
                    if (IS_LITTLE_ENDIAN)
912
0
#ifdef BSWAP4
913
0
                        ctx->Yi.d[3] = BSWAP4(ctr);
914
#else
915
                        PUTU32(ctx->Yi.c + 12, ctr);
916
#endif
917
0
                    else
918
0
                        ctx->Yi.d[3] = ctr;
919
0
                    for (i = 0; i < 16 / sizeof(size_t); ++i)
920
0
                        out_t[i] = in_t[i] ^ ctx->EKi.t[i];
921
0
                    out += 16;
922
0
                    in += 16;
923
0
                    len -= 16;
924
0
                }
925
0
                GHASH(ctx, out - j, j);
926
0
            }
927
#else
928
            while (len >= 16) {
929
                size_t *out_t = (size_t *)out;
930
                const size_t *in_t = (const size_t *)in;
931
932
                (*block)(ctx->Yi.c, ctx->EKi.c, key);
933
                ++ctr;
934
                if (IS_LITTLE_ENDIAN)
935
#ifdef BSWAP4
936
                    ctx->Yi.d[3] = BSWAP4(ctr);
937
#else
938
                    PUTU32(ctx->Yi.c + 12, ctr);
939
#endif
940
                else
941
                    ctx->Yi.d[3] = ctr;
942
                for (i = 0; i < 16 / sizeof(size_t); ++i)
943
                    ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
944
                GCM_MUL(ctx);
945
                out += 16;
946
                in += 16;
947
                len -= 16;
948
            }
949
#endif
950
0
            if (len) {
951
0
                (*block)(ctx->Yi.c, ctx->EKi.c, key);
952
0
                ++ctr;
953
0
                if (IS_LITTLE_ENDIAN)
954
0
#ifdef BSWAP4
955
0
                    ctx->Yi.d[3] = BSWAP4(ctr);
956
#else
957
                    PUTU32(ctx->Yi.c + 12, ctr);
958
#endif
959
0
                else
960
0
                    ctx->Yi.d[3] = ctr;
961
0
#if defined(GHASH)
962
0
                while (len--) {
963
0
                    ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
964
0
                    ++n;
965
0
                }
966
#else
967
                while (len--) {
968
                    ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
969
                    ++n;
970
                }
971
                mres = n;
972
#endif
973
0
            }
974
975
0
            ctx->mres = mres;
976
0
            return 0;
977
0
        } while (0);
978
0
    }
979
0
#endif
980
0
    for (i = 0; i < len; ++i) {
981
0
        if (n == 0) {
982
0
            (*block)(ctx->Yi.c, ctx->EKi.c, key);
983
0
            ++ctr;
984
0
            if (IS_LITTLE_ENDIAN)
985
0
#ifdef BSWAP4
986
0
                ctx->Yi.d[3] = BSWAP4(ctr);
987
#else
988
                PUTU32(ctx->Yi.c + 12, ctr);
989
#endif
990
0
            else
991
0
                ctx->Yi.d[3] = ctr;
992
0
        }
993
0
#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
994
0
        ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
995
0
        n = (n + 1) % 16;
996
0
        if (mres == sizeof(ctx->Xn)) {
997
0
            GHASH(ctx, ctx->Xn, sizeof(ctx->Xn));
998
0
            mres = 0;
999
0
        }
1000
#else
1001
        ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
1002
        mres = n = (n + 1) % 16;
1003
        if (n == 0)
1004
            GCM_MUL(ctx);
1005
#endif
1006
0
    }
1007
1008
0
    ctx->mres = mres;
1009
0
    return 0;
1010
0
}
1011
1012
int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1013
    const unsigned char *in, unsigned char *out,
1014
    size_t len)
1015
0
{
1016
0
    DECLARE_IS_ENDIAN;
1017
0
    unsigned int n, ctr, mres;
1018
0
    size_t i;
1019
0
    uint64_t mlen = ctx->len.u[1];
1020
0
    block128_f block = ctx->block;
1021
0
    void *key = ctx->key;
1022
1023
0
    mlen += len;
1024
0
    if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1025
0
        return -1;
1026
0
    ctx->len.u[1] = mlen;
1027
1028
0
    mres = ctx->mres;
1029
1030
0
    if (ctx->ares) {
1031
        /* First call to decrypt finalizes GHASH(AAD) */
1032
0
#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1033
0
        if (len == 0) {
1034
0
            GCM_MUL(ctx);
1035
0
            ctx->ares = 0;
1036
0
            return 0;
1037
0
        }
1038
0
        memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
1039
0
        ctx->Xi.u[0] = 0;
1040
0
        ctx->Xi.u[1] = 0;
1041
0
        mres = sizeof(ctx->Xi);
1042
#else
1043
        GCM_MUL(ctx);
1044
#endif
1045
0
        ctx->ares = 0;
1046
0
    }
1047
1048
0
    if (IS_LITTLE_ENDIAN)
1049
0
#ifdef BSWAP4
1050
0
        ctr = BSWAP4(ctx->Yi.d[3]);
1051
#else
1052
        ctr = GETU32(ctx->Yi.c + 12);
1053
#endif
1054
0
    else
1055
0
        ctr = ctx->Yi.d[3];
1056
1057
0
    n = mres % 16;
1058
0
#if !defined(OPENSSL_SMALL_FOOTPRINT)
1059
0
    if (16 % sizeof(size_t) == 0) { /* always true actually */
1060
0
        do {
1061
0
            if (n) {
1062
0
#if defined(GHASH)
1063
0
                while (n && len) {
1064
0
                    *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
1065
0
                    --len;
1066
0
                    n = (n + 1) % 16;
1067
0
                }
1068
0
                if (n == 0) {
1069
0
                    GHASH(ctx, ctx->Xn, mres);
1070
0
                    mres = 0;
1071
0
                } else {
1072
0
                    ctx->mres = mres;
1073
0
                    return 0;
1074
0
                }
1075
#else
1076
                while (n && len) {
1077
                    uint8_t c = *(in++);
1078
                    *(out++) = c ^ ctx->EKi.c[n];
1079
                    ctx->Xi.c[n] ^= c;
1080
                    --len;
1081
                    n = (n + 1) % 16;
1082
                }
1083
                if (n == 0) {
1084
                    GCM_MUL(ctx);
1085
                    mres = 0;
1086
                } else {
1087
                    ctx->mres = n;
1088
                    return 0;
1089
                }
1090
#endif
1091
0
            }
1092
#if defined(STRICT_ALIGNMENT)
1093
            if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
1094
                break;
1095
#endif
1096
0
#if defined(GHASH)
1097
0
            if (len >= 16 && mres) {
1098
0
                GHASH(ctx, ctx->Xn, mres);
1099
0
                mres = 0;
1100
0
            }
1101
0
#if defined(GHASH_CHUNK)
1102
0
            while (len >= GHASH_CHUNK) {
1103
0
                size_t j = GHASH_CHUNK;
1104
1105
0
                GHASH(ctx, in, GHASH_CHUNK);
1106
0
                while (j) {
1107
0
                    size_t_aX *out_t = (size_t_aX *)out;
1108
0
                    const size_t_aX *in_t = (const size_t_aX *)in;
1109
1110
0
                    (*block)(ctx->Yi.c, ctx->EKi.c, key);
1111
0
                    ++ctr;
1112
0
                    if (IS_LITTLE_ENDIAN)
1113
0
#ifdef BSWAP4
1114
0
                        ctx->Yi.d[3] = BSWAP4(ctr);
1115
#else
1116
                        PUTU32(ctx->Yi.c + 12, ctr);
1117
#endif
1118
0
                    else
1119
0
                        ctx->Yi.d[3] = ctr;
1120
0
                    for (i = 0; i < 16 / sizeof(size_t); ++i)
1121
0
                        out_t[i] = in_t[i] ^ ctx->EKi.t[i];
1122
0
                    out += 16;
1123
0
                    in += 16;
1124
0
                    j -= 16;
1125
0
                }
1126
0
                len -= GHASH_CHUNK;
1127
0
            }
1128
0
#endif
1129
0
            if ((i = (len & (size_t)-16))) {
1130
0
                GHASH(ctx, in, i);
1131
0
                while (len >= 16) {
1132
0
                    size_t_aX *out_t = (size_t_aX *)out;
1133
0
                    const size_t_aX *in_t = (const size_t_aX *)in;
1134
1135
0
                    (*block)(ctx->Yi.c, ctx->EKi.c, key);
1136
0
                    ++ctr;
1137
0
                    if (IS_LITTLE_ENDIAN)
1138
0
#ifdef BSWAP4
1139
0
                        ctx->Yi.d[3] = BSWAP4(ctr);
1140
#else
1141
                        PUTU32(ctx->Yi.c + 12, ctr);
1142
#endif
1143
0
                    else
1144
0
                        ctx->Yi.d[3] = ctr;
1145
0
                    for (i = 0; i < 16 / sizeof(size_t); ++i)
1146
0
                        out_t[i] = in_t[i] ^ ctx->EKi.t[i];
1147
0
                    out += 16;
1148
0
                    in += 16;
1149
0
                    len -= 16;
1150
0
                }
1151
0
            }
1152
#else
1153
            while (len >= 16) {
1154
                size_t *out_t = (size_t *)out;
1155
                const size_t *in_t = (const size_t *)in;
1156
1157
                (*block)(ctx->Yi.c, ctx->EKi.c, key);
1158
                ++ctr;
1159
                if (IS_LITTLE_ENDIAN)
1160
#ifdef BSWAP4
1161
                    ctx->Yi.d[3] = BSWAP4(ctr);
1162
#else
1163
                    PUTU32(ctx->Yi.c + 12, ctr);
1164
#endif
1165
                else
1166
                    ctx->Yi.d[3] = ctr;
1167
                for (i = 0; i < 16 / sizeof(size_t); ++i) {
1168
                    size_t c = in_t[i];
1169
                    out_t[i] = c ^ ctx->EKi.t[i];
1170
                    ctx->Xi.t[i] ^= c;
1171
                }
1172
                GCM_MUL(ctx);
1173
                out += 16;
1174
                in += 16;
1175
                len -= 16;
1176
            }
1177
#endif
1178
0
            if (len) {
1179
0
                (*block)(ctx->Yi.c, ctx->EKi.c, key);
1180
0
                ++ctr;
1181
0
                if (IS_LITTLE_ENDIAN)
1182
0
#ifdef BSWAP4
1183
0
                    ctx->Yi.d[3] = BSWAP4(ctr);
1184
#else
1185
                    PUTU32(ctx->Yi.c + 12, ctr);
1186
#endif
1187
0
                else
1188
0
                    ctx->Yi.d[3] = ctr;
1189
0
#if defined(GHASH)
1190
0
                while (len--) {
1191
0
                    out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
1192
0
                    ++n;
1193
0
                }
1194
#else
1195
                while (len--) {
1196
                    uint8_t c = in[n];
1197
                    ctx->Xi.c[n] ^= c;
1198
                    out[n] = c ^ ctx->EKi.c[n];
1199
                    ++n;
1200
                }
1201
                mres = n;
1202
#endif
1203
0
            }
1204
1205
0
            ctx->mres = mres;
1206
0
            return 0;
1207
0
        } while (0);
1208
0
    }
1209
0
#endif
1210
0
    for (i = 0; i < len; ++i) {
1211
0
        uint8_t c;
1212
0
        if (n == 0) {
1213
0
            (*block)(ctx->Yi.c, ctx->EKi.c, key);
1214
0
            ++ctr;
1215
0
            if (IS_LITTLE_ENDIAN)
1216
0
#ifdef BSWAP4
1217
0
                ctx->Yi.d[3] = BSWAP4(ctr);
1218
#else
1219
                PUTU32(ctx->Yi.c + 12, ctr);
1220
#endif
1221
0
            else
1222
0
                ctx->Yi.d[3] = ctr;
1223
0
        }
1224
0
#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1225
0
        out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
1226
0
        n = (n + 1) % 16;
1227
0
        if (mres == sizeof(ctx->Xn)) {
1228
0
            GHASH(ctx, ctx->Xn, sizeof(ctx->Xn));
1229
0
            mres = 0;
1230
0
        }
1231
#else
1232
        c = in[i];
1233
        out[i] = c ^ ctx->EKi.c[n];
1234
        ctx->Xi.c[n] ^= c;
1235
        mres = n = (n + 1) % 16;
1236
        if (n == 0)
1237
            GCM_MUL(ctx);
1238
#endif
1239
0
    }
1240
1241
0
    ctx->mres = mres;
1242
0
    return 0;
1243
0
}
1244
1245
int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1246
    const unsigned char *in, unsigned char *out,
1247
    size_t len, ctr128_f stream)
1248
0
{
1249
#if defined(OPENSSL_SMALL_FOOTPRINT)
1250
    return CRYPTO_gcm128_encrypt(ctx, in, out, len);
1251
#else
1252
0
    DECLARE_IS_ENDIAN;
1253
0
    unsigned int n, ctr, mres;
1254
0
    size_t i;
1255
0
    uint64_t mlen = ctx->len.u[1];
1256
0
    void *key = ctx->key;
1257
1258
0
    mlen += len;
1259
0
    if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1260
0
        return -1;
1261
0
    ctx->len.u[1] = mlen;
1262
1263
0
    mres = ctx->mres;
1264
1265
0
    if (ctx->ares) {
1266
        /* First call to encrypt finalizes GHASH(AAD) */
1267
0
#if defined(GHASH)
1268
0
        if (len == 0) {
1269
0
            GCM_MUL(ctx);
1270
0
            ctx->ares = 0;
1271
0
            return 0;
1272
0
        }
1273
0
        memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
1274
0
        ctx->Xi.u[0] = 0;
1275
0
        ctx->Xi.u[1] = 0;
1276
0
        mres = sizeof(ctx->Xi);
1277
#else
1278
        GCM_MUL(ctx);
1279
#endif
1280
0
        ctx->ares = 0;
1281
0
    }
1282
1283
0
    if (IS_LITTLE_ENDIAN)
1284
0
#ifdef BSWAP4
1285
0
        ctr = BSWAP4(ctx->Yi.d[3]);
1286
#else
1287
        ctr = GETU32(ctx->Yi.c + 12);
1288
#endif
1289
0
    else
1290
0
        ctr = ctx->Yi.d[3];
1291
1292
0
    n = mres % 16;
1293
0
    if (n) {
1294
0
#if defined(GHASH)
1295
0
        while (n && len) {
1296
0
            ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
1297
0
            --len;
1298
0
            n = (n + 1) % 16;
1299
0
        }
1300
0
        if (n == 0) {
1301
0
            GHASH(ctx, ctx->Xn, mres);
1302
0
            mres = 0;
1303
0
        } else {
1304
0
            ctx->mres = mres;
1305
0
            return 0;
1306
0
        }
1307
#else
1308
        while (n && len) {
1309
            ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
1310
            --len;
1311
            n = (n + 1) % 16;
1312
        }
1313
        if (n == 0) {
1314
            GCM_MUL(ctx);
1315
            mres = 0;
1316
        } else {
1317
            ctx->mres = n;
1318
            return 0;
1319
        }
1320
#endif
1321
0
    }
1322
0
#if defined(GHASH)
1323
0
    if (len >= 16 && mres) {
1324
0
        GHASH(ctx, ctx->Xn, mres);
1325
0
        mres = 0;
1326
0
    }
1327
0
#if defined(GHASH_CHUNK)
1328
0
    while (len >= GHASH_CHUNK) {
1329
0
        (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
1330
0
        ctr += GHASH_CHUNK / 16;
1331
0
        if (IS_LITTLE_ENDIAN)
1332
0
#ifdef BSWAP4
1333
0
            ctx->Yi.d[3] = BSWAP4(ctr);
1334
#else
1335
            PUTU32(ctx->Yi.c + 12, ctr);
1336
#endif
1337
0
        else
1338
0
            ctx->Yi.d[3] = ctr;
1339
0
        GHASH(ctx, out, GHASH_CHUNK);
1340
0
        out += GHASH_CHUNK;
1341
0
        in += GHASH_CHUNK;
1342
0
        len -= GHASH_CHUNK;
1343
0
    }
1344
0
#endif
1345
0
#endif
1346
0
    if ((i = (len & (size_t)-16))) {
1347
0
        size_t j = i / 16;
1348
1349
0
        (*stream)(in, out, j, key, ctx->Yi.c);
1350
0
        ctr += (unsigned int)j;
1351
0
        if (IS_LITTLE_ENDIAN)
1352
0
#ifdef BSWAP4
1353
0
            ctx->Yi.d[3] = BSWAP4(ctr);
1354
#else
1355
            PUTU32(ctx->Yi.c + 12, ctr);
1356
#endif
1357
0
        else
1358
0
            ctx->Yi.d[3] = ctr;
1359
0
        in += i;
1360
0
        len -= i;
1361
0
#if defined(GHASH)
1362
0
        GHASH(ctx, out, i);
1363
0
        out += i;
1364
#else
1365
        while (j--) {
1366
            for (i = 0; i < 16; ++i)
1367
                ctx->Xi.c[i] ^= out[i];
1368
            GCM_MUL(ctx);
1369
            out += 16;
1370
        }
1371
#endif
1372
0
    }
1373
0
    if (len) {
1374
0
        (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
1375
0
        ++ctr;
1376
0
        if (IS_LITTLE_ENDIAN)
1377
0
#ifdef BSWAP4
1378
0
            ctx->Yi.d[3] = BSWAP4(ctr);
1379
#else
1380
            PUTU32(ctx->Yi.c + 12, ctr);
1381
#endif
1382
0
        else
1383
0
            ctx->Yi.d[3] = ctr;
1384
0
        while (len--) {
1385
0
#if defined(GHASH)
1386
0
            ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
1387
#else
1388
            ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n];
1389
#endif
1390
0
            ++n;
1391
0
        }
1392
0
    }
1393
1394
0
    ctx->mres = mres;
1395
0
    return 0;
1396
0
#endif
1397
0
}
1398
1399
int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1400
    const unsigned char *in, unsigned char *out,
1401
    size_t len, ctr128_f stream)
1402
0
{
1403
#if defined(OPENSSL_SMALL_FOOTPRINT)
1404
    return CRYPTO_gcm128_decrypt(ctx, in, out, len);
1405
#else
1406
0
    DECLARE_IS_ENDIAN;
1407
0
    unsigned int n, ctr, mres;
1408
0
    size_t i;
1409
0
    uint64_t mlen = ctx->len.u[1];
1410
0
    void *key = ctx->key;
1411
1412
0
    mlen += len;
1413
0
    if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1414
0
        return -1;
1415
0
    ctx->len.u[1] = mlen;
1416
1417
0
    mres = ctx->mres;
1418
1419
0
    if (ctx->ares) {
1420
        /* First call to decrypt finalizes GHASH(AAD) */
1421
0
#if defined(GHASH)
1422
0
        if (len == 0) {
1423
0
            GCM_MUL(ctx);
1424
0
            ctx->ares = 0;
1425
0
            return 0;
1426
0
        }
1427
0
        memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
1428
0
        ctx->Xi.u[0] = 0;
1429
0
        ctx->Xi.u[1] = 0;
1430
0
        mres = sizeof(ctx->Xi);
1431
#else
1432
        GCM_MUL(ctx);
1433
#endif
1434
0
        ctx->ares = 0;
1435
0
    }
1436
1437
0
    if (IS_LITTLE_ENDIAN)
1438
0
#ifdef BSWAP4
1439
0
        ctr = BSWAP4(ctx->Yi.d[3]);
1440
#else
1441
        ctr = GETU32(ctx->Yi.c + 12);
1442
#endif
1443
0
    else
1444
0
        ctr = ctx->Yi.d[3];
1445
1446
0
    n = mres % 16;
1447
0
    if (n) {
1448
0
#if defined(GHASH)
1449
0
        while (n && len) {
1450
0
            *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
1451
0
            --len;
1452
0
            n = (n + 1) % 16;
1453
0
        }
1454
0
        if (n == 0) {
1455
0
            GHASH(ctx, ctx->Xn, mres);
1456
0
            mres = 0;
1457
0
        } else {
1458
0
            ctx->mres = mres;
1459
0
            return 0;
1460
0
        }
1461
#else
1462
        while (n && len) {
1463
            uint8_t c = *(in++);
1464
            *(out++) = c ^ ctx->EKi.c[n];
1465
            ctx->Xi.c[n] ^= c;
1466
            --len;
1467
            n = (n + 1) % 16;
1468
        }
1469
        if (n == 0) {
1470
            GCM_MUL(ctx);
1471
            mres = 0;
1472
        } else {
1473
            ctx->mres = n;
1474
            return 0;
1475
        }
1476
#endif
1477
0
    }
1478
0
#if defined(GHASH)
1479
0
    if (len >= 16 && mres) {
1480
0
        GHASH(ctx, ctx->Xn, mres);
1481
0
        mres = 0;
1482
0
    }
1483
0
#if defined(GHASH_CHUNK)
1484
0
    while (len >= GHASH_CHUNK) {
1485
0
        GHASH(ctx, in, GHASH_CHUNK);
1486
0
        (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
1487
0
        ctr += GHASH_CHUNK / 16;
1488
0
        if (IS_LITTLE_ENDIAN)
1489
0
#ifdef BSWAP4
1490
0
            ctx->Yi.d[3] = BSWAP4(ctr);
1491
#else
1492
            PUTU32(ctx->Yi.c + 12, ctr);
1493
#endif
1494
0
        else
1495
0
            ctx->Yi.d[3] = ctr;
1496
0
        out += GHASH_CHUNK;
1497
0
        in += GHASH_CHUNK;
1498
0
        len -= GHASH_CHUNK;
1499
0
    }
1500
0
#endif
1501
0
#endif
1502
0
    if ((i = (len & (size_t)-16))) {
1503
0
        size_t j = i / 16;
1504
1505
0
#if defined(GHASH)
1506
0
        GHASH(ctx, in, i);
1507
#else
1508
        while (j--) {
1509
            size_t k;
1510
            for (k = 0; k < 16; ++k)
1511
                ctx->Xi.c[k] ^= in[k];
1512
            GCM_MUL(ctx);
1513
            in += 16;
1514
        }
1515
        j = i / 16;
1516
        in -= i;
1517
#endif
1518
0
        (*stream)(in, out, j, key, ctx->Yi.c);
1519
0
        ctr += (unsigned int)j;
1520
0
        if (IS_LITTLE_ENDIAN)
1521
0
#ifdef BSWAP4
1522
0
            ctx->Yi.d[3] = BSWAP4(ctr);
1523
#else
1524
            PUTU32(ctx->Yi.c + 12, ctr);
1525
#endif
1526
0
        else
1527
0
            ctx->Yi.d[3] = ctr;
1528
0
        out += i;
1529
0
        in += i;
1530
0
        len -= i;
1531
0
    }
1532
0
    if (len) {
1533
0
        (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
1534
0
        ++ctr;
1535
0
        if (IS_LITTLE_ENDIAN)
1536
0
#ifdef BSWAP4
1537
0
            ctx->Yi.d[3] = BSWAP4(ctr);
1538
#else
1539
            PUTU32(ctx->Yi.c + 12, ctr);
1540
#endif
1541
0
        else
1542
0
            ctx->Yi.d[3] = ctr;
1543
0
        while (len--) {
1544
0
#if defined(GHASH)
1545
0
            out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
1546
#else
1547
            uint8_t c = in[n];
1548
            ctx->Xi.c[mres++] ^= c;
1549
            out[n] = c ^ ctx->EKi.c[n];
1550
#endif
1551
0
            ++n;
1552
0
        }
1553
0
    }
1554
1555
0
    ctx->mres = mres;
1556
0
    return 0;
1557
0
#endif
1558
0
}
1559
1560
/*
1561
 * Calculate the tag and verify it against the supplied tag.
1562
 * Returns:
1563
 * -1: invalid tag length
1564
 *  0: tag verified
1565
 * >0: tag mismatch
1566
 */
1567
int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
1568
    size_t len)
1569
0
{
1570
0
    DECLARE_IS_ENDIAN;
1571
0
    uint64_t alen = ctx->len.u[0] << 3;
1572
0
    uint64_t clen = ctx->len.u[1] << 3;
1573
1574
0
#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1575
0
    u128 bitlen;
1576
0
    unsigned int mres = ctx->mres;
1577
1578
0
    if (mres) {
1579
0
        unsigned blocks = (mres + 15) & -16;
1580
1581
0
        memset(ctx->Xn + mres, 0, blocks - mres);
1582
0
        mres = blocks;
1583
0
        if (mres == sizeof(ctx->Xn)) {
1584
0
            GHASH(ctx, ctx->Xn, mres);
1585
0
            mres = 0;
1586
0
        }
1587
0
    } else if (ctx->ares) {
1588
0
        GCM_MUL(ctx);
1589
0
    }
1590
#else
1591
    if (ctx->mres || ctx->ares)
1592
        GCM_MUL(ctx);
1593
#endif
1594
1595
0
    if (IS_LITTLE_ENDIAN) {
1596
0
#ifdef BSWAP8
1597
0
        alen = BSWAP8(alen);
1598
0
        clen = BSWAP8(clen);
1599
#else
1600
        uint8_t *p = ctx->len.c;
1601
1602
        ctx->len.u[0] = alen;
1603
        ctx->len.u[1] = clen;
1604
1605
        alen = (uint64_t)GETU32(p) << 32 | GETU32(p + 4);
1606
        clen = (uint64_t)GETU32(p + 8) << 32 | GETU32(p + 12);
1607
#endif
1608
0
    }
1609
1610
0
#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1611
0
    bitlen.hi = alen;
1612
0
    bitlen.lo = clen;
1613
0
    memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen));
1614
0
    mres += sizeof(bitlen);
1615
0
    GHASH(ctx, ctx->Xn, mres);
1616
#else
1617
    ctx->Xi.u[0] ^= alen;
1618
    ctx->Xi.u[1] ^= clen;
1619
    GCM_MUL(ctx);
1620
#endif
1621
1622
0
    ctx->Xi.u[0] ^= ctx->EK0.u[0];
1623
0
    ctx->Xi.u[1] ^= ctx->EK0.u[1];
1624
1625
0
    if (tag && len <= sizeof(ctx->Xi))
1626
0
        return CRYPTO_memcmp(ctx->Xi.c, tag, len);
1627
0
    else
1628
0
        return -1;
1629
0
}
1630
1631
void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
1632
0
{
1633
0
    CRYPTO_gcm128_finish(ctx, NULL, 0);
1634
0
    memcpy(tag, ctx->Xi.c,
1635
0
        len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
1636
0
}
1637
1638
GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
1639
0
{
1640
0
    GCM128_CONTEXT *ret;
1641
1642
0
    if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
1643
0
        CRYPTO_gcm128_init(ret, key, block);
1644
1645
0
    return ret;
1646
0
}
1647
1648
void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
1649
0
{
1650
0
    OPENSSL_clear_free(ctx, sizeof(*ctx));
1651
0
}