Coverage Report

Created: 2023-09-25 06:33

/src/nettle-with-libgmp/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
1.47k
{
49
1.47k
  static const union nettle_block16 zero_block;
50
1.47k
  *r = zero_block;
51
1.47k
}
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
1.35k
{
49
1.35k
  static const union nettle_block16 zero_block;
50
1.35k
  *r = zero_block;
51
1.35k
}
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
116
{
49
116
  static const union nettle_block16 zero_block;
50
116
  *r = zero_block;
51
116
}
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
775
{
57
775
  r->u64[0] = x->u64[0];
58
775
  r->u64[1] = x->u64[1];
59
775
}
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
775
{
57
775
  r->u64[0] = x->u64[0];
58
775
  r->u64[1] = x->u64[1];
59
775
}
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
8.77k
{
65
8.77k
  r->u64[0] ^= x->u64[0];
66
8.77k
  r->u64[1] ^= x->u64[1];
67
8.77k
}
cmac.c:block16_xor
Line
Count
Source
64
216
{
65
216
  r->u64[0] ^= x->u64[0];
66
216
  r->u64[1] ^= x->u64[1];
67
216
}
Unexecuted instantiation: cmac64.c:block16_xor
eax.c:block16_xor
Line
Count
Source
64
756
{
65
756
  r->u64[0] ^= x->u64[0];
66
756
  r->u64[1] ^= x->u64[1];
67
756
}
gcm.c:block16_xor
Line
Count
Source
64
361
{
65
361
  r->u64[0] ^= x->u64[0];
66
361
  r->u64[1] ^= x->u64[1];
67
361
}
Unexecuted instantiation: ghash-set-key.c:block16_xor
ocb.c:block16_xor
Line
Count
Source
64
7.31k
{
65
7.31k
  r->u64[0] ^= x->u64[0];
66
7.31k
  r->u64[1] ^= x->u64[1];
67
7.31k
}
siv-cmac.c:block16_xor
Line
Count
Source
64
126
{
65
126
  r->u64[0] ^= x->u64[0];
66
126
  r->u64[1] ^= x->u64[1];
67
126
}
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
2.78k
{
74
2.78k
  r->u64[0] = x->u64[0] ^ y->u64[0];
75
2.78k
  r->u64[1] = x->u64[1] ^ y->u64[1];
76
2.78k
}
cmac.c:block16_xor3
Line
Count
Source
73
260
{
74
260
  r->u64[0] = x->u64[0] ^ y->u64[0];
75
260
  r->u64[1] = x->u64[1] ^ y->u64[1];
76
260
}
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.52k
{
74
2.52k
  r->u64[0] = x->u64[0] ^ y->u64[0];
75
2.52k
  r->u64[1] = x->u64[1] ^ y->u64[1];
76
2.52k
}
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
876
{
83
876
  memxor3 (r->b, x->b, bytes, 16);
84
876
}
cmac.c:block16_xor_bytes
Line
Count
Source
82
849
{
83
849
  memxor3 (r->b, x->b, bytes, 16);
84
849
}
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
27
{
83
27
  memxor3 (r->b, x->b, bytes, 16);
84
27
}
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
40
{
90
40
  r->u64 ^= x->u64;
91
40
}
Unexecuted instantiation: cmac.c:block8_xor
cmac64.c:block8_xor
Line
Count
Source
89
40
{
90
40
  r->u64 ^= x->u64;
91
40
}
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
130
{
98
130
  r->u64 = x->u64 ^ y->u64;
99
130
}
Unexecuted instantiation: cmac.c:block8_xor3
cmac64.c:block8_xor3
Line
Count
Source
97
130
{
98
130
  r->u64 = x->u64 ^ y->u64;
99
130
}
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
15
{
106
15
  memxor3 (r->b, x->b, bytes, 8);
107
15
}
Unexecuted instantiation: cmac.c:block8_xor_bytes
cmac64.c:block8_xor_bytes
Line
Count
Source
105
15
{
106
15
  memxor3 (r->b, x->b, bytes, 8);
107
15
}
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
11.0k
  ((((x) & UINT64_C(0x7f7f7f7f7f7f7f7f)) << 1) | \
113
11.0k
   (((x) & UINT64_C(0x8080808080808080)) >> 15))
114
#define RSHIFT_ALIEN_UINT64(x) \
115
116
  ((((x) & UINT64_C(0xfefefefefefefefe)) >> 1) | \
116
116
   (((x) & UINT64_C(0x0001010101010101)) << 15))
117
118
/* Two typical defining polynoms */
119
120
5.56k
#define BLOCK16_POLY (UINT64_C(0x87))
121
80
#define BLOCK8_POLY (UINT64_C(0x1b))
122
58
#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
5.50k
{
173
5.50k
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
5.50k
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
5.50k
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
5.50k
}
cmac.c:block16_mulx_be
Line
Count
Source
172
126
{
173
126
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
126
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
126
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
126
}
Unexecuted instantiation: cmac64.c:block16_mulx_be
eax.c:block16_mulx_be
Line
Count
Source
172
226
{
173
226
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
226
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
226
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
226
}
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.03k
{
173
5.03k
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
5.03k
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
5.03k
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
5.03k
}
siv-cmac.c:block16_mulx_be
Line
Count
Source
172
126
{
173
126
  uint64_t carry = (src->u64[0] & 0x80) >> 7;
174
126
  dst->u64[0] = LSHIFT_ALIEN_UINT64(src->u64[0]) | ((src->u64[1] & 0x80) << 49);
175
126
  dst->u64[1] = LSHIFT_ALIEN_UINT64(src->u64[1]) ^ ((BLOCK16_POLY << 56) & -carry);
176
126
}
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
60
{
182
60
  uint64_t carry = src->u64[1] >> 63;
183
60
  dst->u64[1] = (src->u64[1] << 1) | (src->u64[0] >> 63);
184
60
  dst->u64[0] = (src->u64[0] << 1) ^ (BLOCK16_POLY & -carry);
185
60
}
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
60
{
182
60
  uint64_t carry = src->u64[1] >> 63;
183
60
  dst->u64[1] = (src->u64[1] << 1) | (src->u64[0] >> 63);
184
60
  dst->u64[0] = (src->u64[0] << 1) ^ (BLOCK16_POLY & -carry);
185
60
}
186
187
static inline void
188
block8_mulx_be (union nettle_block8 *dst,
189
    const union nettle_block8 *src)
190
80
{
191
80
  uint64_t carry = (src->u64 & 0x80) >> 7;
192
193
80
  dst->u64 = LSHIFT_ALIEN_UINT64(src->u64) ^ ((BLOCK8_POLY << 56) & -carry);
194
80
}
Unexecuted instantiation: cmac.c:block8_mulx_be
cmac64.c:block8_mulx_be
Line
Count
Source
190
80
{
191
80
  uint64_t carry = (src->u64 & 0x80) >> 7;
192
193
80
  dst->u64 = LSHIFT_ALIEN_UINT64(src->u64) ^ ((BLOCK8_POLY << 56) & -carry);
194
80
}
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
58
{
200
58
  uint64_t mask;
201
202
  /* Shift uses big-endian representation. */
203
58
  mask = - ((x->u64[1] >> 56) & 1);
204
58
  r->u64[1] = RSHIFT_ALIEN_UINT64(x->u64[1]) | ((x->u64[0] >> 49) & 0x80);
205
58
  r->u64[0] = RSHIFT_ALIEN_UINT64(x->u64[0]) ^ (mask & GHASH_POLY);
206
58
}
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
58
{
200
58
  uint64_t mask;
201
202
  /* Shift uses big-endian representation. */
203
58
  mask = - ((x->u64[1] >> 56) & 1);
204
58
  r->u64[1] = RSHIFT_ALIEN_UINT64(x->u64[1]) | ((x->u64[0] >> 49) & 0x80);
205
58
  r->u64[0] = RSHIFT_ALIEN_UINT64(x->u64[0]) ^ (mask & GHASH_POLY);
206
58
}
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
116
{
215
116
  uint64_t t = nettle_bswap64 (x->u64[0]);
216
116
  r->u64[0] = nettle_bswap64 (x->u64[1]);
217
116
  r->u64[1] = t;
218
116
}
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
58
{
215
58
  uint64_t t = nettle_bswap64 (x->u64[0]);
216
58
  r->u64[0] = nettle_bswap64 (x->u64[1]);
217
58
  r->u64[1] = t;
218
58
}
siv-ghash-set-key.c:block16_bswap
Line
Count
Source
214
58
{
215
58
  uint64_t t = nettle_bswap64 (x->u64[0]);
216
58
  r->u64[0] = nettle_bswap64 (x->u64[1]);
217
58
  r->u64[1] = t;
218
58
}
Unexecuted instantiation: siv-ghash-update.c:block16_bswap
Unexecuted instantiation: xts.c:block16_bswap
219
220
#endif /* NETTLE_BLOCK_INTERNAL_H_INCLUDED */