Coverage Report

Created: 2025-03-18 06:55

/src/nettle/ghash-update.c
Line
Count
Source (jump to first uncovered line)
1
/* ghash-update.c
2
3
   Galois counter mode, specified by NIST,
4
   http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
5
6
   See also the gcm paper at
7
   http://www.cryptobarn.com/papers/gcm-spec.pdf.
8
9
   Copyright (C) 2011 Katholieke Universiteit Leuven
10
   Copyright (C) 2011, 2013, 2018, 2022 Niels Möller
11
   Copyright (C) 2018 Red Hat, Inc.
12
13
   This file is part of GNU Nettle.
14
15
   GNU Nettle is free software: you can redistribute it and/or
16
   modify it under the terms of either:
17
18
     * the GNU Lesser General Public License as published by the Free
19
       Software Foundation; either version 3 of the License, or (at your
20
       option) any later version.
21
22
   or
23
24
     * the GNU General Public License as published by the Free
25
       Software Foundation; either version 2 of the License, or (at your
26
       option) any later version.
27
28
   or both in parallel, as here.
29
30
   GNU Nettle is distributed in the hope that it will be useful,
31
   but WITHOUT ANY WARRANTY; without even the implied warranty of
32
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33
   General Public License for more details.
34
35
   You should have received copies of the GNU General Public License and
36
   the GNU Lesser General Public License along with this program.  If
37
   not, see http://www.gnu.org/licenses/.
38
*/
39
40
#if HAVE_CONFIG_H
41
# include "config.h"
42
#endif
43
44
#include "ghash-internal.h"
45
#include "block-internal.h"
46
47
#if GCM_TABLE_BITS < 7
48
# error Unsupported table size.
49
#endif
50
51
/* For fat builds */
52
#if HAVE_NATIVE_ghash_update
53
const uint8_t *
54
_nettle_ghash_update_c (const struct gcm_key *ctx, union nettle_block16 *state,
55
      size_t blocks, const uint8_t *data);
56
#define _nettle_ghash_update _nettle_ghash_update_c
57
#endif
58
59
static void
60
gcm_gf_mul (union nettle_block16 *x, const union nettle_block16 *table)
61
0
{
62
0
  uint64_t x0 = x->u64[0];
63
0
  uint64_t x1 = x->u64[1];
64
0
  uint64_t r0 = 0;
65
0
  uint64_t r1 = 0;
66
0
  unsigned i;
67
0
  for (i = 0; i < 64; i++, x0 >>= 1, x1 >>= 1)
68
0
    {
69
0
      uint64_t m0 = -(x0 & 1);
70
0
      uint64_t m1 = -(x1 & 1);
71
0
      r0 ^= m0 & table[2*i].u64[0];
72
0
      r1 ^= m0 & table[2*i].u64[1];
73
0
      r0 ^= m1 & table[2*i+1].u64[0];
74
0
      r1 ^= m1 & table[2*i+1].u64[1];
75
0
    }
76
0
  x->u64[0] = r0; x->u64[1] = r1;
77
0
}
78
79
const uint8_t *
80
_ghash_update (const struct gcm_key *ctx, union nettle_block16 *state,
81
         size_t blocks, const uint8_t *data)
82
0
{
83
0
  for (; blocks-- > 0; data += GCM_BLOCK_SIZE)
84
0
    {
85
0
      memxor (state->b, data, GCM_BLOCK_SIZE);
86
0
      gcm_gf_mul (state, ctx->h);
87
0
    }
88
0
  return data;
89
0
}