/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 | | /* Implements a lookup table for processors without carryless-mul |
55 | | instruction. */ |
56 | | void |
57 | | _ghash_set_key (struct gcm_key *ctx, const union nettle_block16 *key) |
58 | 0 | { |
59 | | /* Middle element if GCM_TABLE_BITS > 0, otherwise the first |
60 | | element */ |
61 | 0 | unsigned i = (1<<GCM_TABLE_BITS)/2; |
62 | 0 | block16_zero (&ctx->h[0]); |
63 | 0 | ctx->h[i] = *key; |
64 | | |
65 | | /* Algorithm 3 from the gcm paper. First do powers of two, then do |
66 | | the rest by adding. */ |
67 | 0 | while (i /= 2) |
68 | 0 | block16_mulx_ghash (&ctx->h[i], &ctx->h[2*i]); |
69 | 0 | for (i = 2; i < 1<<GCM_TABLE_BITS; i *= 2) |
70 | 0 | { |
71 | 0 | unsigned j; |
72 | 0 | for (j = 1; j < i; j++) |
73 | 0 | block16_xor3 (&ctx->h[i+j], &ctx->h[i], &ctx->h[j]); |
74 | 0 | } |
75 | 0 | } |