Coverage Report

Created: 2025-06-22 06:32

/src/qpdf/libqpdf/sha2big.c
Line
Count
Source (jump to first uncovered line)
1
/* clang-format off */
2
/* $Id: sha2big.c 216 2010-06-08 09:46:57Z tp $ */
3
/*
4
 * SHA-384 / SHA-512 implementation.
5
 *
6
 * ==========================(LICENSE BEGIN)============================
7
 *
8
 * Copyright (c) 2007-2010  Projet RNRT SAPHIR
9
 * 
10
 * Permission is hereby granted, free of charge, to any person obtaining
11
 * a copy of this software and associated documentation files (the
12
 * "Software"), to deal in the Software without restriction, including
13
 * without limitation the rights to use, copy, modify, merge, publish,
14
 * distribute, sublicense, and/or sell copies of the Software, and to
15
 * permit persons to whom the Software is furnished to do so, subject to
16
 * the following conditions:
17
 * 
18
 * The above copyright notice and this permission notice shall be
19
 * included in all copies or substantial portions of the Software.
20
 * 
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
 *
29
 * ===========================(LICENSE END)=============================
30
 *
31
 * @author   Thomas Pornin <thomas.pornin@cryptolog.com>
32
 */
33
34
#include <stddef.h>
35
#include <string.h>
36
37
#include "sph/sph_sha2.h"
38
39
#if SPH_64
40
41
#define CH(X, Y, Z)    ((((Y) ^ (Z)) & (X)) ^ (Z))
42
#define MAJ(X, Y, Z)   (((X) & (Y)) | (((X) | (Y)) & (Z)))
43
44
#define ROTR64    SPH_ROTR64
45
46
#define BSG5_0(x)      (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39))
47
#define BSG5_1(x)      (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41))
48
#define SSG5_0(x)      (ROTR64(x, 1) ^ ROTR64(x, 8) ^ SPH_T64((x) >> 7))
49
#define SSG5_1(x)      (ROTR64(x, 19) ^ ROTR64(x, 61) ^ SPH_T64((x) >> 6))
50
51
static const sph_u64 K512[80] = {
52
        SPH_C64(0x428A2F98D728AE22), SPH_C64(0x7137449123EF65CD),
53
        SPH_C64(0xB5C0FBCFEC4D3B2F), SPH_C64(0xE9B5DBA58189DBBC),
54
        SPH_C64(0x3956C25BF348B538), SPH_C64(0x59F111F1B605D019),
55
        SPH_C64(0x923F82A4AF194F9B), SPH_C64(0xAB1C5ED5DA6D8118),
56
        SPH_C64(0xD807AA98A3030242), SPH_C64(0x12835B0145706FBE),
57
        SPH_C64(0x243185BE4EE4B28C), SPH_C64(0x550C7DC3D5FFB4E2),
58
        SPH_C64(0x72BE5D74F27B896F), SPH_C64(0x80DEB1FE3B1696B1),
59
        SPH_C64(0x9BDC06A725C71235), SPH_C64(0xC19BF174CF692694),
60
        SPH_C64(0xE49B69C19EF14AD2), SPH_C64(0xEFBE4786384F25E3),
61
        SPH_C64(0x0FC19DC68B8CD5B5), SPH_C64(0x240CA1CC77AC9C65),
62
        SPH_C64(0x2DE92C6F592B0275), SPH_C64(0x4A7484AA6EA6E483),
63
        SPH_C64(0x5CB0A9DCBD41FBD4), SPH_C64(0x76F988DA831153B5),
64
        SPH_C64(0x983E5152EE66DFAB), SPH_C64(0xA831C66D2DB43210),
65
        SPH_C64(0xB00327C898FB213F), SPH_C64(0xBF597FC7BEEF0EE4),
66
        SPH_C64(0xC6E00BF33DA88FC2), SPH_C64(0xD5A79147930AA725),
67
        SPH_C64(0x06CA6351E003826F), SPH_C64(0x142929670A0E6E70),
68
        SPH_C64(0x27B70A8546D22FFC), SPH_C64(0x2E1B21385C26C926),
69
        SPH_C64(0x4D2C6DFC5AC42AED), SPH_C64(0x53380D139D95B3DF),
70
        SPH_C64(0x650A73548BAF63DE), SPH_C64(0x766A0ABB3C77B2A8),
71
        SPH_C64(0x81C2C92E47EDAEE6), SPH_C64(0x92722C851482353B),
72
        SPH_C64(0xA2BFE8A14CF10364), SPH_C64(0xA81A664BBC423001),
73
        SPH_C64(0xC24B8B70D0F89791), SPH_C64(0xC76C51A30654BE30),
74
        SPH_C64(0xD192E819D6EF5218), SPH_C64(0xD69906245565A910),
75
        SPH_C64(0xF40E35855771202A), SPH_C64(0x106AA07032BBD1B8),
76
        SPH_C64(0x19A4C116B8D2D0C8), SPH_C64(0x1E376C085141AB53),
77
        SPH_C64(0x2748774CDF8EEB99), SPH_C64(0x34B0BCB5E19B48A8),
78
        SPH_C64(0x391C0CB3C5C95A63), SPH_C64(0x4ED8AA4AE3418ACB),
79
        SPH_C64(0x5B9CCA4F7763E373), SPH_C64(0x682E6FF3D6B2B8A3),
80
        SPH_C64(0x748F82EE5DEFB2FC), SPH_C64(0x78A5636F43172F60),
81
        SPH_C64(0x84C87814A1F0AB72), SPH_C64(0x8CC702081A6439EC),
82
        SPH_C64(0x90BEFFFA23631E28), SPH_C64(0xA4506CEBDE82BDE9),
83
        SPH_C64(0xBEF9A3F7B2C67915), SPH_C64(0xC67178F2E372532B),
84
        SPH_C64(0xCA273ECEEA26619C), SPH_C64(0xD186B8C721C0C207),
85
        SPH_C64(0xEADA7DD6CDE0EB1E), SPH_C64(0xF57D4F7FEE6ED178),
86
        SPH_C64(0x06F067AA72176FBA), SPH_C64(0x0A637DC5A2C898A6),
87
        SPH_C64(0x113F9804BEF90DAE), SPH_C64(0x1B710B35131C471B),
88
        SPH_C64(0x28DB77F523047D84), SPH_C64(0x32CAAB7B40C72493),
89
        SPH_C64(0x3C9EBE0A15C9BEBC), SPH_C64(0x431D67C49C100D4C),
90
        SPH_C64(0x4CC5D4BECB3E42B6), SPH_C64(0x597F299CFC657E2A),
91
        SPH_C64(0x5FCB6FAB3AD6FAEC), SPH_C64(0x6C44198C4A475817)
92
};
93
94
static const sph_u64 H384[8] = {
95
        SPH_C64(0xCBBB9D5DC1059ED8), SPH_C64(0x629A292A367CD507),
96
        SPH_C64(0x9159015A3070DD17), SPH_C64(0x152FECD8F70E5939),
97
        SPH_C64(0x67332667FFC00B31), SPH_C64(0x8EB44A8768581511),
98
        SPH_C64(0xDB0C2E0D64F98FA7), SPH_C64(0x47B5481DBEFA4FA4)
99
};
100
101
static const sph_u64 H512[8] = {
102
        SPH_C64(0x6A09E667F3BCC908), SPH_C64(0xBB67AE8584CAA73B),
103
        SPH_C64(0x3C6EF372FE94F82B), SPH_C64(0xA54FF53A5F1D36F1),
104
        SPH_C64(0x510E527FADE682D1), SPH_C64(0x9B05688C2B3E6C1F),
105
        SPH_C64(0x1F83D9ABFB41BD6B), SPH_C64(0x5BE0CD19137E2179)
106
};
107
108
/*
109
 * This macro defines the body for a SHA-384 / SHA-512 compression function
110
 * implementation. The "in" parameter should evaluate, when applied to a
111
 * numerical input parameter from 0 to 15, to an expression which yields
112
 * the corresponding input block. The "r" parameter should evaluate to
113
 * an array or pointer expression designating the array of 8 words which
114
 * contains the input and output of the compression function.
115
 *
116
 * SHA-512 is hard for the compiler. If the loop is completely unrolled,
117
 * then the code will be quite huge (possibly more than 100 kB), and the
118
 * performance will be degraded due to cache misses on the code. We
119
 * unroll only eight steps, which avoids all needless copies when
120
 * 64-bit registers are swapped.
121
 */
122
123
148M
#define SHA3_STEP(A, B, C, D, E, F, G, H, i)   do { \
124
148M
                sph_u64 T1, T2; \
125
148M
                T1 = SPH_T64(H + BSG5_1(E) + CH(E, F, G) + K512[i] + W[i]); \
126
148M
                T2 = SPH_T64(BSG5_0(A) + MAJ(A, B, C)); \
127
148M
                D = SPH_T64(D + T1); \
128
148M
                H = SPH_T64(T1 + T2); \
129
148M
        } while (0)
130
131
1.85M
#define SHA3_ROUND_BODY(in, r)   do { \
132
1.85M
                int i; \
133
1.85M
                sph_u64 A, B, C, D, E, F, G, H; \
134
1.85M
                sph_u64 W[80]; \
135
1.85M
 \
136
31.5M
                for (i = 0; i < 16; i ++) \
137
29.6M
                        W[i] = in(i); \
138
120M
                for (i = 16; i < 80; i ++) \
139
118M
                        W[i] = SPH_T64(SSG5_1(W[i - 2]) + W[i - 7] \
140
1.85M
                                + SSG5_0(W[i - 15]) + W[i - 16]); \
141
1.85M
                A = (r)[0]; \
142
1.85M
                B = (r)[1]; \
143
1.85M
                C = (r)[2]; \
144
1.85M
                D = (r)[3]; \
145
1.85M
                E = (r)[4]; \
146
1.85M
                F = (r)[5]; \
147
1.85M
                G = (r)[6]; \
148
1.85M
                H = (r)[7]; \
149
20.3M
                for (i = 0; i < 80; i += 8) { \
150
18.5M
                        SHA3_STEP(A, B, C, D, E, F, G, H, i + 0); \
151
18.5M
                        SHA3_STEP(H, A, B, C, D, E, F, G, i + 1); \
152
18.5M
                        SHA3_STEP(G, H, A, B, C, D, E, F, i + 2); \
153
18.5M
                        SHA3_STEP(F, G, H, A, B, C, D, E, i + 3); \
154
18.5M
                        SHA3_STEP(E, F, G, H, A, B, C, D, i + 4); \
155
18.5M
                        SHA3_STEP(D, E, F, G, H, A, B, C, i + 5); \
156
18.5M
                        SHA3_STEP(C, D, E, F, G, H, A, B, i + 6); \
157
18.5M
                        SHA3_STEP(B, C, D, E, F, G, H, A, i + 7); \
158
18.5M
                } \
159
1.85M
                (r)[0] = SPH_T64((r)[0] + A); \
160
1.85M
                (r)[1] = SPH_T64((r)[1] + B); \
161
1.85M
                (r)[2] = SPH_T64((r)[2] + C); \
162
1.85M
                (r)[3] = SPH_T64((r)[3] + D); \
163
1.85M
                (r)[4] = SPH_T64((r)[4] + E); \
164
1.85M
                (r)[5] = SPH_T64((r)[5] + F); \
165
1.85M
                (r)[6] = SPH_T64((r)[6] + G); \
166
1.85M
                (r)[7] = SPH_T64((r)[7] + H); \
167
1.85M
        } while (0)
168
169
/*
170
 * One round of SHA-384 / SHA-512. The data must be aligned for 64-bit access.
171
 */
172
static void
173
sha3_round(const unsigned char *data, sph_u64 r[8])
174
1.85M
{
175
29.6M
#define SHA3_IN(x)   sph_dec64be_aligned(data + (8 * (x)))
176
29.6M
        SHA3_ROUND_BODY(SHA3_IN, r);
177
1.85M
#undef SHA3_IN
178
1.85M
}
179
180
/* see sph_sha3.h */
181
void
182
sph_sha384_init(void *cc)
183
48.0k
{
184
48.0k
        sph_sha384_context *sc;
185
186
48.0k
        sc = cc;
187
48.0k
        memcpy(sc->val, H384, sizeof H384);
188
48.0k
        sc->count = 0;
189
48.0k
}
190
191
/* see sph_sha3.h */
192
void
193
sph_sha512_init(void *cc)
194
55.4k
{
195
55.4k
        sph_sha512_context *sc;
196
197
55.4k
        sc = cc;
198
55.4k
        memcpy(sc->val, H512, sizeof H512);
199
55.4k
        sc->count = 0;
200
55.4k
}
201
202
1.85M
#define RFUN   sha3_round
203
#define HASH   sha384
204
#define BE64   1
205
#include "sph/md_helper.c"
206
207
/* see sph_sha3.h */
208
void
209
sph_sha384_close(void *cc, void *dst)
210
24.0k
{
211
24.0k
        sha384_close(cc, dst, 6);
212
24.0k
        sph_sha384_init(cc);
213
24.0k
}
214
215
/* see sph_sha3.h */
216
void
217
sph_sha384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
218
0
{
219
0
        sha384_addbits_and_close(cc, ub, n, dst, 6);
220
0
        sph_sha384_init(cc);
221
0
}
222
223
/* see sph_sha3.h */
224
void
225
sph_sha512_close(void *cc, void *dst)
226
27.7k
{
227
27.7k
        sha384_close(cc, dst, 8);
228
27.7k
        sph_sha512_init(cc);
229
27.7k
}
230
231
/* see sph_sha3.h */
232
void
233
sph_sha512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
234
0
{
235
0
        sha384_addbits_and_close(cc, ub, n, dst, 8);
236
0
        sph_sha512_init(cc);
237
0
}
238
239
/* see sph_sha3.h */
240
void
241
sph_sha384_comp(const sph_u64 msg[16], sph_u64 val[8])
242
0
{
243
0
#define SHA3_IN(x)   msg[x]
244
0
        SHA3_ROUND_BODY(SHA3_IN, val);
245
0
#undef SHA3_IN
246
0
}
247
248
#endif