Coverage Report

Created: 2024-06-28 06:39

/src/nettle-with-mini-gmp/block-internal.h
Line
Count
Source
1
/* block-internal.h
2
3
   Internal implementations of nettle_blockZ-related functions.
4
5
   Copyright (C) 2011 Katholieke Universiteit Leuven
6
   Copyright (C) 2011, 2013, 2018 Niels Möller
7
   Copyright (C) 2018 Red Hat, Inc.
8
   Copyright (C) 2019 Dmitry Eremin-Solenikov
9
10
   This file is part of GNU Nettle.
11
12
   GNU Nettle is free software: you can redistribute it and/or
13
   modify it under the terms of either:
14
15
     * the GNU Lesser General Public License as published by the Free
16
       Software Foundation; either version 3 of the License, or (at your
17
       option) any later version.
18
19
   or
20
21
     * the GNU General Public License as published by the Free
22
       Software Foundation; either version 2 of the License, or (at your
23
       option) any later version.
24
25
   or both in parallel, as here.
26
27
   GNU Nettle is distributed in the hope that it will be useful,
28
   but WITHOUT ANY WARRANTY; without even the implied warranty of
29
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30
   General Public License for more details.
31
32
   You should have received copies of the GNU General Public License and
33
   the GNU Lesser General Public License along with this program.  If
34
   not, see http://www.gnu.org/licenses/.
35
*/
36
37
#ifndef NETTLE_BLOCK_INTERNAL_H_INCLUDED
38
#define NETTLE_BLOCK_INTERNAL_H_INCLUDED
39
40
#include <assert.h>
41
42
#include "nettle-types.h"
43
#include "bswap-internal.h"
44
#include "memxor.h"
45
46
static inline void
47
block16_zero (union nettle_block16 *r)
48
4.26k
{
49
4.26k
  static const union nettle_block16 zero_block;
50
4.26k
  *r = zero_block;
51
4.26k
}
Unexecuted instantiation: cmac.c:block16_zero
Unexecuted instantiation: cmac64.c:block16_zero
Unexecuted instantiation: eax.c:block16_zero
gcm.c:block16_zero
Line
Count
Source
48
3.95k
{
49
3.95k
  static const union nettle_block16 zero_block;
50
3.95k
  *r = zero_block;
51
3.95k
}
Unexecuted instantiation: ghash-set-key.c:block16_zero
Unexecuted instantiation: ocb.c:block16_zero
Unexecuted instantiation: siv-cmac.c:block16_zero
siv-gcm.c:block16_zero
Line
Count
Source
48
318
{
49
318
  static const union nettle_block16 zero_block;
50
318
  *r = zero_block;
51
318
}
Unexecuted instantiation: siv-ghash-set-key.c:block16_zero
Unexecuted instantiation: siv-ghash-update.c:block16_zero
Unexecuted instantiation: xts.c:block16_zero
52
53
static inline void
54
block16_set (union nettle_block16 *r,
55
       const union nettle_block16 *x)
56
1.88k
{
57
1.88k
  r->u64[0] = x->u64[0];
58
1.88k
  r->u64[1] = x->u64[1];
59
1.88k
}
Unexecuted instantiation: cmac.c:block16_set
Unexecuted instantiation: cmac64.c:block16_set
Unexecuted instantiation: eax.c:block16_set
Unexecuted instantiation: gcm.c:block16_set
Unexecuted instantiation: ghash-set-key.c:block16_set
ocb.c:block16_set
Line
Count
Source
56
1.88k
{
57
1.88k
  r->u64[0] = x->u64[0];
58
1.88k
  r->u64[1] = x->u64[1];
59
1.88k
}
Unexecuted instantiation: siv-cmac.c:block16_set
Unexecuted instantiation: siv-gcm.c:block16_set
Unexecuted instantiation: siv-ghash-set-key.c:block16_set
Unexecuted instantiation: siv-ghash-update.c:block16_set
Unexecuted instantiation: xts.c:block16_set
60
61
static inline void
62
block16_xor (union nettle_block16 *r,
63
       const union nettle_block16 *x)
64
12.1k
{
65
12.1k
  r->u64[0] ^= x->u64[0];
66
12.1k
  r->u64[1] ^= x->u64[1];
67
12.1k
}
cmac.c:block16_xor
Line
Count
Source
64
915
{
65
915
  r->u64[0] ^= x->u64[0];
66
915
  r->u64[1] ^= x->u64[1];
67
915
}
Unexecuted instantiation: cmac64.c:block16_xor
eax.c:block16_xor
Line
Count
Source
64
3.06k
{
65
3.06k
  r->u64[0] ^= x->u64[0];
66
3.06k
  r->u64[1] ^= x->u64[1];
67
3.06k
}
gcm.c:block16_xor
Line
Count
Source
64
1.07k
{
65
1.07k
  r->u64[0] ^= x->u64[0];
66
1.07k
  r->u64[1] ^= x->u64[1];
67
1.07k
}
Unexecuted instantiation: ghash-set-key.c:block16_xor
ocb.c:block16_xor
Line
Count
Source
64
6.51k
{
65
6.51k
  r->u64[0] ^= x->u64[0];
66
6.51k
  r->u64[1] ^= x->u64[1];
67
6.51k
}
siv-cmac.c:block16_xor
Line
Count
Source
64
546
{
65
546
  r->u64[0] ^= x->u64[0];
66
546
  r->u64[1] ^= x->u64[1];
67
546
}
Unexecuted instantiation: siv-gcm.c:block16_xor
Unexecuted instantiation: siv-ghash-set-key.c:block16_xor
Unexecuted instantiation: siv-ghash-update.c:block16_xor
Unexecuted instantiation: xts.c:block16_xor
68
69
static inline void
70
block16_xor3 (union nettle_block16 *r,
71
        const union nettle_block16 *x,
72
        const union nettle_block16 *y)
73
3.87k
{
74
3.87k
  r->u64[0] = x->u64[0] ^ y->u64[0];
75
3.87k
  r->u64[1] = x->u64[1] ^ y->u64[1];
76
3.87k
}
cmac.c:block16_xor3
Line
Count
Source
73
1.17k
{
74
1.17k
  r->u64[0] = x->u64[0] ^ y->u64[0];
75
1.17k
  r->u64[1] = x->u64[1] ^ y->u64[1];
76
1.17k
}
Unexecuted instantiation: cmac64.c:block16_xor3
Unexecuted instantiation: eax.c:block16_xor3
Unexecuted instantiation: gcm.c:block16_xor3
Unexecuted instantiation: ghash-set-key.c:block16_xor3
ocb.c:block16_xor3
Line
Count
Source
73
2.69k
{
74
2.69k
  r->u64[0] = x->u64[0] ^ y->u64[0];
75
2.69k
  r->u64[1] = x->u64[1] ^ y->u64[1];
76
2.69k
}
Unexecuted instantiation: siv-cmac.c:block16_xor3
Unexecuted instantiation: siv-gcm.c:block16_xor3
Unexecuted instantiation: siv-ghash-set-key.c:block16_xor3
Unexecuted instantiation: siv-ghash-update.c:block16_xor3
Unexecuted instantiation: xts.c:block16_xor3
77
78
static inline void
79
block16_xor_bytes (union nettle_block16 *r,
80
       const union nettle_block16 *x,
81
       const uint8_t *bytes)
82
2.29k
{
83
2.29k
  memxor3 (r->b, x->b, bytes, 16);
84
2.29k
}
cmac.c:block16_xor_bytes
Line
Count
Source
82
2.20k
{
83
2.20k
  memxor3 (r->b, x->b, bytes, 16);
84
2.20k
}
Unexecuted instantiation: cmac64.c:block16_xor_bytes
Unexecuted instantiation: eax.c:block16_xor_bytes
Unexecuted instantiation: gcm.c:block16_xor_bytes
Unexecuted instantiation: ghash-set-key.c:block16_xor_bytes
Unexecuted instantiation: ocb.c:block16_xor_bytes
siv-cmac.c:block16_xor_bytes
Line
Count
Source
82
84
{
83
84
  memxor3 (r->b, x->b, bytes, 16);
84
84
}
Unexecuted instantiation: siv-gcm.c:block16_xor_bytes
Unexecuted instantiation: siv-ghash-set-key.c:block16_xor_bytes
Unexecuted instantiation: siv-ghash-update.c:block16_xor_bytes
Unexecuted instantiation: xts.c:block16_xor_bytes
85
86
static inline void
87
block8_xor (union nettle_block8 *r,
88
      const union nettle_block8 *x)
89
285
{
90
285
  r->u64 ^= x->u64;
91
285
}
Unexecuted instantiation: cmac.c:block8_xor
cmac64.c:block8_xor
Line
Count
Source
89
285
{
90
285
  r->u64 ^= x->u64;
91
285
}
Unexecuted instantiation: eax.c:block8_xor
Unexecuted instantiation: gcm.c:block8_xor
Unexecuted instantiation: ghash-set-key.c:block8_xor
Unexecuted instantiation: ocb.c:block8_xor
Unexecuted instantiation: siv-cmac.c:block8_xor
Unexecuted instantiation: siv-gcm.c:block8_xor
Unexecuted instantiation: siv-ghash-set-key.c:block8_xor
Unexecuted instantiation: siv-ghash-update.c:block8_xor
Unexecuted instantiation: xts.c:block8_xor
92
93
static inline void
94
block8_xor3 (union nettle_block8 *r,
95
       const union nettle_block8 *x,
96
       const union nettle_block8 *y)
97
578
{
98
578
  r->u64 = x->u64 ^ y->u64;
99
578
}
Unexecuted instantiation: cmac.c:block8_xor3
cmac64.c:block8_xor3
Line
Count
Source
97
578
{
98
578
  r->u64 = x->u64 ^ y->u64;
99
578
}
Unexecuted instantiation: eax.c:block8_xor3
Unexecuted instantiation: gcm.c:block8_xor3
Unexecuted instantiation: ghash-set-key.c:block8_xor3
Unexecuted instantiation: ocb.c:block8_xor3
Unexecuted instantiation: siv-cmac.c:block8_xor3
Unexecuted instantiation: siv-gcm.c:block8_xor3
Unexecuted instantiation: siv-ghash-set-key.c:block8_xor3
Unexecuted instantiation: siv-ghash-update.c:block8_xor3
Unexecuted instantiation: xts.c:block8_xor3
100
101
static inline void
102
block8_xor_bytes (union nettle_block8 *r,
103
      const union nettle_block8 *x,
104
      const uint8_t *bytes)
105
3.18k
{
106
3.18k
  memxor3 (r->b, x->b, bytes, 8);
107
3.18k
}
Unexecuted instantiation: cmac.c:block8_xor_bytes
cmac64.c:block8_xor_bytes
Line
Count
Source
105
3.18k
{
106
3.18k
  memxor3 (r->b, x->b, bytes, 8);
107
3.18k
}
Unexecuted instantiation: eax.c:block8_xor_bytes
Unexecuted instantiation: gcm.c:block8_xor_bytes
Unexecuted instantiation: ghash-set-key.c:block8_xor_bytes
Unexecuted instantiation: ocb.c:block8_xor_bytes
Unexecuted instantiation: siv-cmac.c:block8_xor_bytes
Unexecuted instantiation: siv-gcm.c:block8_xor_bytes
Unexecuted instantiation: siv-ghash-set-key.c:block8_xor_bytes
Unexecuted instantiation: siv-ghash-update.c:block8_xor_bytes
Unexecuted instantiation: xts.c:block8_xor_bytes
108
109
/* Do a foreign-endianness shift of data */
110
111
#define LSHIFT_ALIEN_UINT64(x) \
112
15.6k
  ((((x) & UINT64_C(0x7f7f7f7f7f7f7f7f)) << 1) | \
113
15.6k
   (((x) & UINT64_C(0x8080808080808080)) >> 15))
114
#define RSHIFT_ALIEN_UINT64(x) \
115
318
  ((((x) & UINT64_C(0xfefefefefefefefe)) >> 1) | \
116
318
   (((x) & UINT64_C(0x0001010101010101)) << 15))
117
118
/* Two typical defining polynoms */
119
120
7.80k
#define BLOCK16_POLY (UINT64_C(0x87))
121
570
#define BLOCK8_POLY (UINT64_C(0x1b))
122
159
#define GHASH_POLY (UINT64_C(0xE1))
123
124
/* Galois multiplications by 2:
125
 * functions differ in shifting right or left, big- or little- endianness
126
 * and by defining polynom.
127
 * r == x is allowed. */
128
129
#if WORDS_BIGENDIAN
130
static inline void
131
block16_mulx_be (union nettle_block16 *dst,
132
     const union nettle_block16 *src)
133
{
134
  uint64_t carry = src->u64[0] >> 63;
135
  dst->u64[0] = (src->u64[0] << 1) | (src->u64[1] >> 63);
136
  dst->u64[1] = (src->u64[1] << 1) ^ (BLOCK16_POLY & -carry);
137
}
138
139
static inline void
140
block16_mulx_le (union nettle_block16 *dst,
141
     const union nettle_block16 *src)
142
{
143
  uint64_t carry = (src->u64[1] & 0x80) >> 7;
144
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) | ((src->u64[0] & 0x80) << 49);
145
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) ^ ((BLOCK16_POLY << 56) & -carry);
146
}
147
148
static inline void
149
block8_mulx_be (union nettle_block8 *dst,
150
    const union nettle_block8 *src)
151
{
152
  uint64_t carry = src->u64 >> 63;
153
154
  dst->u64 = (src->u64 << 1) ^ (BLOCK8_POLY & -carry);
155
}
156
157
static inline void
158
block16_mulx_ghash (union nettle_block16 *r,
159
        const union nettle_block16 *x)
160
{
161
  uint64_t mask;
162
163
  /* Shift uses big-endian representation. */
164
  mask = - (x->u64[1] & 1);
165
  r->u64[1] = (x->u64[1] >> 1) | ((x->u64[0] & 1) << 63);
166
  r->u64[0] = (x->u64[0] >> 1) ^ (mask & (GHASH_POLY << 56));
167
}
168
#else /* !WORDS_BIGENDIAN */
169
static inline void
170
block16_mulx_be (union nettle_block16 *dst,
171
     const union nettle_block16 *src)
172
7.55k
{
173
7.55k
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
7.55k
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
7.55k
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
7.55k
}
cmac.c:block16_mulx_be
Line
Count
Source
172
570
{
173
570
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
570
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
570
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
570
}
Unexecuted instantiation: cmac64.c:block16_mulx_be
eax.c:block16_mulx_be
Line
Count
Source
172
902
{
173
902
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
902
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
902
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
902
}
Unexecuted instantiation: gcm.c:block16_mulx_be
Unexecuted instantiation: ghash-set-key.c:block16_mulx_be
ocb.c:block16_mulx_be
Line
Count
Source
172
5.53k
{
173
5.53k
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
5.53k
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
5.53k
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
5.53k
}
siv-cmac.c:block16_mulx_be
Line
Count
Source
172
546
{
173
546
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
546
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
546
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
546
}
Unexecuted instantiation: siv-gcm.c:block16_mulx_be
Unexecuted instantiation: siv-ghash-set-key.c:block16_mulx_be
Unexecuted instantiation: siv-ghash-update.c:block16_mulx_be
Unexecuted instantiation: xts.c:block16_mulx_be
177
178
static inline void
179
block16_mulx_le (union nettle_block16 *dst,
180
     const union nettle_block16 *src)
181
255
{
182
255
  uint64_t carry = src->u64[1] >> 63;
183
255
  dst->u64[1] = (src->u64[1] << 1) | (src->u64[0] >> 63);
184
255
  dst->u64[0] = (src->u64[0] << 1) ^ (BLOCK16_POLY & -carry);
185
255
}
Unexecuted instantiation: cmac.c:block16_mulx_le
Unexecuted instantiation: cmac64.c:block16_mulx_le
Unexecuted instantiation: eax.c:block16_mulx_le
Unexecuted instantiation: gcm.c:block16_mulx_le
Unexecuted instantiation: ghash-set-key.c:block16_mulx_le
Unexecuted instantiation: ocb.c:block16_mulx_le
Unexecuted instantiation: siv-cmac.c:block16_mulx_le
Unexecuted instantiation: siv-gcm.c:block16_mulx_le
Unexecuted instantiation: siv-ghash-set-key.c:block16_mulx_le
Unexecuted instantiation: siv-ghash-update.c:block16_mulx_le
xts.c:block16_mulx_le
Line
Count
Source
181
255
{
182
255
  uint64_t carry = src->u64[1] >> 63;
183
255
  dst->u64[1] = (src->u64[1] << 1) | (src->u64[0] >> 63);
184
255
  dst->u64[0] = (src->u64[0] << 1) ^ (BLOCK16_POLY & -carry);
185
255
}
186
187
static inline void
188
block8_mulx_be (union nettle_block8 *dst,
189
    const union nettle_block8 *src)
190
570
{
191
570
  uint64_t carry = (src->u64 & 0x80) >> 7;
192
193
570
  dst->u64 = LSHIFT_ALIEN_UINT64(src->u64) ^ ((BLOCK8_POLY << 56) & -carry);
194
570
}
Unexecuted instantiation: cmac.c:block8_mulx_be
cmac64.c:block8_mulx_be
Line
Count
Source
190
570
{
191
570
  uint64_t carry = (src->u64 & 0x80) >> 7;
192
193
570
  dst->u64 = LSHIFT_ALIEN_UINT64(src->u64) ^ ((BLOCK8_POLY << 56) & -carry);
194
570
}
Unexecuted instantiation: eax.c:block8_mulx_be
Unexecuted instantiation: gcm.c:block8_mulx_be
Unexecuted instantiation: ghash-set-key.c:block8_mulx_be
Unexecuted instantiation: ocb.c:block8_mulx_be
Unexecuted instantiation: siv-cmac.c:block8_mulx_be
Unexecuted instantiation: siv-gcm.c:block8_mulx_be
Unexecuted instantiation: siv-ghash-set-key.c:block8_mulx_be
Unexecuted instantiation: siv-ghash-update.c:block8_mulx_be
Unexecuted instantiation: xts.c:block8_mulx_be
195
196
static inline void
197
block16_mulx_ghash (union nettle_block16 *r,
198
        const union nettle_block16 *x)
199
159
{
200
159
  uint64_t mask;
201
202
  /* Shift uses big-endian representation. */
203
159
  mask = - ((x->u64[1] >> 56) & 1);
204
159
  r->u64[1] = RSHIFT_ALIEN_UINT64(x->u64[1]) | ((x->u64[0] >> 49) & 0x80);
205
159
  r->u64[0] = RSHIFT_ALIEN_UINT64(x->u64[0]) ^ (mask & GHASH_POLY);
206
159
}
Unexecuted instantiation: cmac.c:block16_mulx_ghash
Unexecuted instantiation: cmac64.c:block16_mulx_ghash
Unexecuted instantiation: eax.c:block16_mulx_ghash
Unexecuted instantiation: gcm.c:block16_mulx_ghash
Unexecuted instantiation: ghash-set-key.c:block16_mulx_ghash
Unexecuted instantiation: ocb.c:block16_mulx_ghash
Unexecuted instantiation: siv-cmac.c:block16_mulx_ghash
Unexecuted instantiation: siv-gcm.c:block16_mulx_ghash
siv-ghash-set-key.c:block16_mulx_ghash
Line
Count
Source
199
159
{
200
159
  uint64_t mask;
201
202
  /* Shift uses big-endian representation. */
203
159
  mask = - ((x->u64[1] >> 56) & 1);
204
159
  r->u64[1] = RSHIFT_ALIEN_UINT64(x->u64[1]) | ((x->u64[0] >> 49) & 0x80);
205
159
  r->u64[0] = RSHIFT_ALIEN_UINT64(x->u64[0]) ^ (mask & GHASH_POLY);
206
159
}
Unexecuted instantiation: siv-ghash-update.c:block16_mulx_ghash
Unexecuted instantiation: xts.c:block16_mulx_ghash
207
#endif /* ! WORDS_BIGENDIAN */
208
209
/* Reverse bytes in X and store the result in R.  This supports
210
   in-place operation (R and X can overlap).  */
211
static inline void
212
block16_bswap (union nettle_block16 *r,
213
         const union nettle_block16 *x)
214
318
{
215
318
  uint64_t t = nettle_bswap64 (x->u64[0]);
216
318
  r->u64[0] = nettle_bswap64 (x->u64[1]);
217
318
  r->u64[1] = t;
218
318
}
Unexecuted instantiation: cmac.c:block16_bswap
Unexecuted instantiation: cmac64.c:block16_bswap
Unexecuted instantiation: eax.c:block16_bswap
Unexecuted instantiation: gcm.c:block16_bswap
Unexecuted instantiation: ghash-set-key.c:block16_bswap
Unexecuted instantiation: ocb.c:block16_bswap
Unexecuted instantiation: siv-cmac.c:block16_bswap
siv-gcm.c:block16_bswap
Line
Count
Source
214
159
{
215
159
  uint64_t t = nettle_bswap64 (x->u64[0]);
216
159
  r->u64[0] = nettle_bswap64 (x->u64[1]);
217
159
  r->u64[1] = t;
218
159
}
siv-ghash-set-key.c:block16_bswap
Line
Count
Source
214
159
{
215
159
  uint64_t t = nettle_bswap64 (x->u64[0]);
216
159
  r->u64[0] = nettle_bswap64 (x->u64[1]);
217
159
  r->u64[1] = t;
218
159
}
Unexecuted instantiation: siv-ghash-update.c:block16_bswap
Unexecuted instantiation: xts.c:block16_bswap
219
220
#endif /* NETTLE_BLOCK_INTERNAL_H_INCLUDED */