Coverage Report

Created: 2025-03-18 06:55

/src/nettle/ghash-set-key.c
Line
Count
Source (jump to first uncovered line)
1
/* ghash-set-key.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
/* For fat builds */
48
#if HAVE_NATIVE_ghash_set_key
49
void
50
_nettle_ghash_set_key_c (struct gcm_key *ctx, const union nettle_block16 *key);
51
#define _nettle_ghash_set_key _nettle_ghash_set_key_c
52
#endif
53
54
#if GCM_TABLE_BITS < 7
55
# error Unsupported table size.
56
#endif
57
58
/* Implements a lookup table for processors without carryless-mul
59
   instruction. */
60
void
61
_ghash_set_key (struct gcm_key *ctx, const union nettle_block16 *key)
62
0
{
63
  /* Table elements hold the key, premultiplied by all needed powers
64
     of x. Element ordering follows the order bits are processed in
65
     _ghash_update, alternating u64[0] and u64[1] bits, starting from
66
     the least significant end. In the gcm bit order, bits (left to
67
     right) correspond to x powers (the numbers) like
68
69
       |0...7|8...15|...|56...63|64...71|72...79|...|120...127|
70
71
     where | indicates the byte boundaries. On little endian, these
72
     bits are in u64 like
73
74
       u64[0]: | 56...63   48...55   40...47  32...39  24...31 16...23  8...15  0...7|
75
       u64[1]: |120...127 112...129 104...111 96...103 88...95 80...87 72...79 64...71|
76
77
     With big-endian, we instead get
78
79
       u64[0]:  |0...63|
80
       u64[1]: |64...127|
81
  */
82
#if WORDS_BIGENDIAN
83
# define INDEX_PERMUTE 63
84
#else
85
0
# define INDEX_PERMUTE 7
86
0
#endif
87
0
  unsigned i;
88
89
0
  block16_set (&ctx->h[2*INDEX_PERMUTE], key);
90
0
  for (i = 1; i < 64; i++)
91
0
    block16_mulx_ghash(&ctx->h[2*(i ^ INDEX_PERMUTE)], &ctx->h[2*((i-1) ^ INDEX_PERMUTE)]);
92
93
0
  block16_mulx_ghash(&ctx->h[2*INDEX_PERMUTE + 1], &ctx->h[2*(63^INDEX_PERMUTE)]);
94
0
  for (i = 1; i < 64; i++)
95
0
    block16_mulx_ghash(&ctx->h[2*(i ^ INDEX_PERMUTE)+1], &ctx->h[2*((i-1) ^ INDEX_PERMUTE)+1]);
96
0
}