src/pdfmake_sha2.c

filter: Statementstmt 99.2% 139 / 140 Branchbran 94.7% 36 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 98.3% 175 / 178 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/PDF/Make/Builder/Form/Field/Checkbox.pm ↑ Summary blib/lib/PDF/Make.pm »
1 /*
2 * pdfmake_sha2.c — SHA-256, SHA-384, SHA-512 implementations
3 *
4 * Reference implementation per FIPS 180-4.
5 * Required for PDF encryption R6 key derivation.
6 */
7  
8 #include "pdfmake_sha2.h"
9 #include "pdfmake_internal.h"
10 #include <string.h>
11  
12 /*============================================================================
13 * SHA-256 constants
14 *==========================================================================*/
15  
16 static const uint32_t SHA256_K[64] = {
17 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
18 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
19 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
20 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
21 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
22 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
23 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
24 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
25 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
26 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
27 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
28 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
29 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
30 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
31 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
32 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
33 };
34  
35 static const uint32_t SHA256_H0[8] = {
36 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
37 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
38 };
39  
40 /*============================================================================
41 * SHA-512 constants
42 *==========================================================================*/
43  
44 static const uint64_t SHA512_K[80] = {
45 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
46 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
47 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
48 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
49 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
50 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
51 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
52 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
53 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
54 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
55 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
56 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
57 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
58 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
59 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
60 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
61 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
62 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
63 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
64 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
65 };
66  
67 static const uint64_t SHA512_H0[8] = {
68 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
69 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
70 };
71  
72 static const uint64_t SHA384_H0[8] = {
73 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
74 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL
75 };
76  
77 /*============================================================================
78 * Helper macros - 32-bit
79 *==========================================================================*/
80  
81 #define ROTR32(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
82 #define SHR32(x, n) ((x) >> (n))
83  
84 #define CH32(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
85 #define MAJ32(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
86  
87 #define SIGMA0_256(x) (ROTR32(x, 2) ^ ROTR32(x, 13) ^ ROTR32(x, 22))
88 #define SIGMA1_256(x) (ROTR32(x, 6) ^ ROTR32(x, 11) ^ ROTR32(x, 25))
89 #define sigma0_256(x) (ROTR32(x, 7) ^ ROTR32(x, 18) ^ SHR32(x, 3))
90 #define sigma1_256(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ SHR32(x, 10))
91  
92 /*============================================================================
93 * Helper macros - 64-bit
94 *==========================================================================*/
95  
96 #define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
97 #define SHR64(x, n) ((x) >> (n))
98  
99 #define CH64(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
100 #define MAJ64(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
101  
102 #define SIGMA0_512(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39))
103 #define SIGMA1_512(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41))
104 #define sigma0_512(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ SHR64(x, 7))
105 #define sigma1_512(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ SHR64(x, 6))
106  
107 /*============================================================================
108 * SHA-256 transform
109 *==========================================================================*/
110  
111 88437 static void sha256_transform(uint32_t state[8], const uint8_t block[64])
112 {
113 uint32_t W[64];
114 uint32_t a, b, c, d, e, f, g, h;
115 uint32_t T1, T2;
116 int t;
117  
118 /* Prepare message schedule */
119 1503429 ▶ for (t = 0; t < 16; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < 16; t++) { 1414992 88437 1503429
120 1414992 W[t] = pdfmake_read_be32(block + t * 4);
121 }
122 4333413 ▶ for (t = 16; t < 64; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 16; t < 64; t++) { 4244976 88437 4333413
123 4244976 W[t] = sigma1_256(W[t-2]) + W[t-7] + sigma0_256(W[t-15]) + W[t-16];
124 }
125
126 /* Initialize working variables */
127 88437 a = state[0]; b = state[1]; c = state[2]; d = state[3];
128 88437 e = state[4]; f = state[5]; g = state[6]; h = state[7];
129
130 /* 64 rounds */
131 5748405 ▶ for (t = 0; t < 64; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < 64; t++) { 5659968 88437 5748405
132 5659968 T1 = h + SIGMA1_256(e) + CH32(e, f, g) + SHA256_K[t] + W[t];
133 5659968 T2 = SIGMA0_256(a) + MAJ32(a, b, c);
134 5659968 h = g; g = f; f = e;
135 5659968 e = d + T1;
136 5659968 d = c; c = b; b = a;
137 5659968 a = T1 + T2;
138 }
139
140 88437 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
141 88437 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
142 88437 }
143  
144 /*============================================================================
145 * SHA-256 public API
146 *==========================================================================*/
147  
148 1208 void pdfmake_sha256_init(pdfmake_sha256_ctx_t *ctx)
149 {
150 1208 memcpy(ctx->state, SHA256_H0, sizeof(SHA256_H0));
151 1208 ctx->count = 0;
152 1208 memset(ctx->buffer, 0, sizeof(ctx->buffer));
153 1208 }
154  
155 3697 void pdfmake_sha256_update(pdfmake_sha256_ctx_t *ctx, const uint8_t *data, size_t len)
156 {
157 3697 size_t buf_pos = (ctx->count / 8) % 64;
158 3697 ctx->count += (uint64_t)len * 8;
159
160 3697 ▶ if (buf_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf_pos > 0) { 1313 2384 3697
161 1313 size_t space = 64 - buf_pos;
162 1313 ▶ if (len >= space) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= space) { 1232 81 1313
163 1232 memcpy(ctx->buffer + buf_pos, data, space);
164 1232 sha256_transform(ctx->state, ctx->buffer);
165 1232 data += space;
166 1232 len -= space;
167 1232 buf_pos = 0;
168 } else {
169 81 memcpy(ctx->buffer + buf_pos, data, len);
170 81 return;
171 }
172 }
173
174 90821 ▶ while (len >= 64) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 64) { 87205 3616 90821
175 87205 sha256_transform(ctx->state, data);
176 87205 data += 64;
177 87205 len -= 64;
178 }
179
180 3616 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 1232 2384 3616
181 1232 memcpy(ctx->buffer, data, len);
182 }
183 }
184  
185 1208 void pdfmake_sha256_final(pdfmake_sha256_ctx_t *ctx, uint8_t digest[32])
186 {
187 1208 uint64_t bits = ctx->count;
188 1208 size_t buf_pos = (bits / 8) % 64;
189 uint8_t pad[64];
190 size_t pad_len;
191 uint8_t len_bytes[8];
192 int i;
193  
194 1208 memset(pad, 0, sizeof(pad));
195 1208 pad[0] = 0x80;
196  
197 1208 ▶ pad_len = (buf_pos < 56) ? (56 - buf_pos) : (120 - buf_pos);
Branch: 1 branch
BranchTrueFalseTotal
pad_len = (buf_pos < 56) ? (56 - buf_pos) : (120 - buf_pos); 1192 16 1208
198 1208 pdfmake_sha256_update(ctx, pad, pad_len);
199  
200 1208 pdfmake_write_be64(len_bytes, bits);
201 1208 pdfmake_sha256_update(ctx, len_bytes, 8);
202  
203 10872 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 9664 1208 10872
204 9664 pdfmake_write_be32(digest + i * 4, ctx->state[i]);
205 }
206 1208 }
207  
208 1159 void pdfmake_sha256(const uint8_t *data, size_t len, uint8_t digest[32])
209 {
210 pdfmake_sha256_ctx_t ctx;
211 1159 pdfmake_sha256_init(&ctx);
212 1159 pdfmake_sha256_update(&ctx, data, len);
213 1159 pdfmake_sha256_final(&ctx, digest);
214 1159 }
215  
216 /*============================================================================
217 * SHA-512 transform
218 *==========================================================================*/
219  
220 90454 static void sha512_transform(uint64_t state[8], const uint8_t block[128])
221 {
222 uint64_t W[80];
223 uint64_t a, b, c, d, e, f, g, h;
224 uint64_t T1, T2;
225 int t;
226  
227 /* Prepare message schedule */
228 1537718 ▶ for (t = 0; t < 16; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < 16; t++) { 1447264 90454 1537718
229 1447264 W[t] = pdfmake_read_be64(block + t * 8);
230 }
231 5879510 ▶ for (t = 16; t < 80; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 16; t < 80; t++) { 5789056 90454 5879510
232 5789056 W[t] = sigma1_512(W[t-2]) + W[t-7] + sigma0_512(W[t-15]) + W[t-16];
233 }
234
235 /* Initialize working variables */
236 90454 a = state[0]; b = state[1]; c = state[2]; d = state[3];
237 90454 e = state[4]; f = state[5]; g = state[6]; h = state[7];
238
239 /* 80 rounds */
240 7326774 ▶ for (t = 0; t < 80; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; t < 80; t++) { 7236320 90454 7326774
241 7236320 T1 = h + SIGMA1_512(e) + CH64(e, f, g) + SHA512_K[t] + W[t];
242 7236320 T2 = SIGMA0_512(a) + MAJ64(a, b, c);
243 7236320 h = g; g = f; f = e;
244 7236320 e = d + T1;
245 7236320 d = c; c = b; b = a;
246 7236320 a = T1 + T2;
247 }
248
249 90454 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
250 90454 state[4] += e; state[5] += f; state[6] += g; state[7] += h;
251 90454 }
252  
253 /*============================================================================
254 * SHA-512 public API
255 *==========================================================================*/
256  
257 1166 void pdfmake_sha512_init(pdfmake_sha512_ctx_t *ctx)
258 {
259 1166 memcpy(ctx->state, SHA512_H0, sizeof(SHA512_H0));
260 1166 ctx->count[0] = 0;
261 1166 ctx->count[1] = 0;
262 1166 memset(ctx->buffer, 0, sizeof(ctx->buffer));
263 1166 }
264  
265 7014 void pdfmake_sha512_update(pdfmake_sha512_ctx_t *ctx, const uint8_t *data, size_t len)
266 {
267 7014 size_t buf_pos = (size_t)(ctx->count[0] / 8) % 128;
268
269 /* Update bit count */
270 7014 uint64_t add_bits = (uint64_t)len * 8;
271 7014 ctx->count[0] += add_bits;
272 7014 ▶ if (ctx->count[0] < add_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->count[0] < add_bits) { 0 7014 7014
273 0 ctx->count[1]++; /* Carry */
274 }
275
276 7014 ▶ if (buf_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf_pos > 0) { 2837 4177 7014
277 2837 size_t space = 128 - buf_pos;
278 2837 ▶ if (len >= space) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= space) { 2338 499 2837
279 2338 memcpy(ctx->buffer + buf_pos, data, space);
280 2338 sha512_transform(ctx->state, ctx->buffer);
281 2338 data += space;
282 2338 len -= space;
283 2338 buf_pos = 0;
284 } else {
285 499 memcpy(ctx->buffer + buf_pos, data, len);
286 499 return;
287 }
288 }
289
290 94631 ▶ while (len >= 128) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 128) { 88116 6515 94631
291 88116 sha512_transform(ctx->state, data);
292 88116 data += 128;
293 88116 len -= 128;
294 }
295
296 6515 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 2338 4177 6515
297 2338 memcpy(ctx->buffer, data, len);
298 }
299 }
300  
301 2338 void pdfmake_sha512_final(pdfmake_sha512_ctx_t *ctx, uint8_t digest[64])
302 {
303 /* Snapshot the bit count BEFORE padding (update() will mutate it). */
304 2338 uint64_t bits_lo = ctx->count[0];
305 2338 uint64_t bits_hi = ctx->count[1];
306 2338 size_t buf_pos = (size_t)(bits_lo / 8) % 128;
307 uint8_t pad[128];
308 size_t pad_len;
309 uint8_t len_bytes[16];
310 int i;
311  
312 2338 memset(pad, 0, sizeof(pad));
313 2338 pad[0] = 0x80;
314  
315 /* Pad to 112 mod 128 bytes (leaving 16 bytes for 128-bit length) */
316 2338 ▶ pad_len = (buf_pos < 112) ? (112 - buf_pos) : (240 - buf_pos);
Branch: 1 branch
BranchTrueFalseTotal
pad_len = (buf_pos < 112) ? (112 - buf_pos) : (240 - buf_pos); 2338 0 2338
317 2338 pdfmake_sha512_update(ctx, pad, pad_len);
318  
319 /* Append 128-bit length (high 64 bits, then low 64 bits) */
320 2338 pdfmake_write_be64(len_bytes, bits_hi);
321 2338 pdfmake_write_be64(len_bytes + 8, bits_lo);
322 2338 pdfmake_sha512_update(ctx, len_bytes, 16);
323  
324 21042 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 18704 2338 21042
325 18704 pdfmake_write_be64(digest + i * 8, ctx->state[i]);
326 }
327 2338 }
328  
329 1166 void pdfmake_sha512(const uint8_t *data, size_t len, uint8_t digest[64])
330 {
331 pdfmake_sha512_ctx_t ctx;
332 1166 pdfmake_sha512_init(&ctx);
333 1166 pdfmake_sha512_update(&ctx, data, len);
334 1166 pdfmake_sha512_final(&ctx, digest);
335 1166 }
336  
337 /*============================================================================
338 * SHA-384 (SHA-512 with different IV and truncated output)
339 *==========================================================================*/
340  
341 1172 void pdfmake_sha384_init(pdfmake_sha384_ctx_t *ctx)
342 {
343 1172 memcpy(ctx->state, SHA384_H0, sizeof(SHA384_H0));
344 1172 ctx->count[0] = 0;
345 1172 ctx->count[1] = 0;
346 1172 memset(ctx->buffer, 0, sizeof(ctx->buffer));
347 1172 }
348  
349 1172 void pdfmake_sha384_update(pdfmake_sha384_ctx_t *ctx, const uint8_t *data, size_t len)
350 {
351 /* SHA-384 uses same update logic as SHA-512 */
352 1172 pdfmake_sha512_update((pdfmake_sha512_ctx_t *)ctx, data, len);
353 1172 }
354  
355 1172 void pdfmake_sha384_final(pdfmake_sha384_ctx_t *ctx, uint8_t digest[48])
356 {
357 uint8_t full_digest[64];
358 1172 pdfmake_sha512_final((pdfmake_sha512_ctx_t *)ctx, full_digest);
359 1172 memcpy(digest, full_digest, 48); /* Truncate to 384 bits */
360 1172 }
361  
362 1172 void pdfmake_sha384(const uint8_t *data, size_t len, uint8_t digest[48])
363 {
364 pdfmake_sha384_ctx_t ctx;
365 1172 pdfmake_sha384_init(&ctx);
366 1172 pdfmake_sha384_update(&ctx, data, len);
367 1172 pdfmake_sha384_final(&ctx, digest);
368 1172 }
« blib/lib/PDF/Make/Builder/Form/Field/Checkbox.pm ↑ Summary blib/lib/PDF/Make.pm »