sha.c

filter: Statementstmt 0.0% 0 / 308 Branchbran 0.0% 0 / 206 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 514 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _rijndael.c ↑ Summary sha64bit.c »
1 /*
2 * sha.c: routines to compute SHA-1/224/256/384/512 digests
3 *
4 * Ref: NIST FIPS PUB 180-2 Secure Hash Standard
5 *
6 * Copyright (C) 2003-2011 Mark Shelor, All Rights Reserved
7 *
8 * Version: 5.70
9 * Wed Dec 14 02:32:10 MST 2011
10 *
11 */
12  
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include "sha.h"
19 #include "sha64bit.h"
20  
21 #define W32 SHA32 /* useful abbreviations */
22 #define C32 SHA32_CONST
23 #define SR32 SHA32_SHR
24 #define SL32 SHA32_SHL
25 #define LO32 SHA_LO32
26 #define UCHR unsigned char
27 #define UINT unsigned int
28 #define ULNG unsigned long
29 #define VP void *
30  
31 #define ROTR(x, n) (SR32(x, n) | SL32(x, 32-(n)))
32 #define ROTL(x, n) (SL32(x, n) | SR32(x, 32-(n)))
33  
34 #define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
35 #define Pa(x, y, z) ((x) ^ (y) ^ (z))
36 #define Ma(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
37  
38 #define SIGMA0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
39 #define SIGMA1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
40 #define sigma0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SR32(x, 3))
41 #define sigma1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SR32(x, 10))
42  
43 #define K1 C32(0x5a827999) /* SHA-1 constants */
44 #define K2 C32(0x6ed9eba1)
45 #define K3 C32(0x8f1bbcdc)
46 #define K4 C32(0xca62c1d6)
47  
48 static W32 K256[64] = /* SHA-224/256 constants */
49 {
50 C32(0x428a2f98), C32(0x71374491), C32(0xb5c0fbcf), C32(0xe9b5dba5),
51 C32(0x3956c25b), C32(0x59f111f1), C32(0x923f82a4), C32(0xab1c5ed5),
52 C32(0xd807aa98), C32(0x12835b01), C32(0x243185be), C32(0x550c7dc3),
53 C32(0x72be5d74), C32(0x80deb1fe), C32(0x9bdc06a7), C32(0xc19bf174),
54 C32(0xe49b69c1), C32(0xefbe4786), C32(0x0fc19dc6), C32(0x240ca1cc),
55 C32(0x2de92c6f), C32(0x4a7484aa), C32(0x5cb0a9dc), C32(0x76f988da),
56 C32(0x983e5152), C32(0xa831c66d), C32(0xb00327c8), C32(0xbf597fc7),
57 C32(0xc6e00bf3), C32(0xd5a79147), C32(0x06ca6351), C32(0x14292967),
58 C32(0x27b70a85), C32(0x2e1b2138), C32(0x4d2c6dfc), C32(0x53380d13),
59 C32(0x650a7354), C32(0x766a0abb), C32(0x81c2c92e), C32(0x92722c85),
60 C32(0xa2bfe8a1), C32(0xa81a664b), C32(0xc24b8b70), C32(0xc76c51a3),
61 C32(0xd192e819), C32(0xd6990624), C32(0xf40e3585), C32(0x106aa070),
62 C32(0x19a4c116), C32(0x1e376c08), C32(0x2748774c), C32(0x34b0bcb5),
63 C32(0x391c0cb3), C32(0x4ed8aa4a), C32(0x5b9cca4f), C32(0x682e6ff3),
64 C32(0x748f82ee), C32(0x78a5636f), C32(0x84c87814), C32(0x8cc70208),
65 C32(0x90befffa), C32(0xa4506ceb), C32(0xbef9a3f7), C32(0xc67178f2)
66 };
67  
68 static W32 H01[5] = /* SHA-1 initial hash value */
69 {
70 C32(0x67452301), C32(0xefcdab89), C32(0x98badcfe),
71 C32(0x10325476), C32(0xc3d2e1f0)
72 };
73  
74 static W32 H0224[8] = /* SHA-224 initial hash value */
75 {
76 C32(0xc1059ed8), C32(0x367cd507), C32(0x3070dd17), C32(0xf70e5939),
77 C32(0xffc00b31), C32(0x68581511), C32(0x64f98fa7), C32(0xbefa4fa4)
78 };
79  
80 static W32 H0256[8] = /* SHA-256 initial hash value */
81 {
82 C32(0x6a09e667), C32(0xbb67ae85), C32(0x3c6ef372), C32(0xa54ff53a),
83 C32(0x510e527f), C32(0x9b05688c), C32(0x1f83d9ab), C32(0x5be0cd19)
84 };
85  
86 0 static void sha1(SHA *s, UCHR *block) /* SHA-1 transform */
87 {
88 W32 a, b, c, d, e;
89 SHA_STO_CLASS W32 W[16];
90 0 W32 *wp = W;
91 0 W32 *H = (W32 *) s->H;
92  
93 0 ▶ SHA32_SCHED(W, block);
Branch: 1 branch
BranchTrueFalseTotal
SHA32_SCHED(W, block); 0 0 0
94  
95 /*
96 * Use SHA-1 alternate method from FIPS PUB 180-2 (ref. 6.1.3)
97 *
98 * To improve performance, unroll the loop and consolidate assignments
99 * by changing the roles of variables "a" through "e" at each step.
100 * Note that the variable "T" is no longer needed.
101 */
102  
103 #define M1(a, b, c, d, e, f, k, w) \
104 e += ROTL(a, 5) + f(b, c, d) + k + w; \
105 b = ROTL(b, 30)
106  
107 #define M11(f, k, w) M1(a, b, c, d, e, f, k, w);
108 #define M12(f, k, w) M1(e, a, b, c, d, f, k, w);
109 #define M13(f, k, w) M1(d, e, a, b, c, f, k, w);
110 #define M14(f, k, w) M1(c, d, e, a, b, f, k, w);
111 #define M15(f, k, w) M1(b, c, d, e, a, f, k, w);
112  
113 #define W11(s) W[(s+ 0) & 0xf]
114 #define W12(s) W[(s+13) & 0xf]
115 #define W13(s) W[(s+ 8) & 0xf]
116 #define W14(s) W[(s+ 2) & 0xf]
117  
118 #define A1(s) (W11(s) = ROTL(W11(s) ^ W12(s) ^ W13(s) ^ W14(s), 1))
119  
120 0 a = H[0]; b = H[1]; c = H[2]; d = H[3]; e = H[4];
121  
122 0 M11(Ch, K1, *wp++); M12(Ch, K1, *wp++); M13(Ch, K1, *wp++);
123 0 M14(Ch, K1, *wp++); M15(Ch, K1, *wp++); M11(Ch, K1, *wp++);
124 0 M12(Ch, K1, *wp++); M13(Ch, K1, *wp++); M14(Ch, K1, *wp++);
125 0 M15(Ch, K1, *wp++); M11(Ch, K1, *wp++); M12(Ch, K1, *wp++);
126 0 M13(Ch, K1, *wp++); M14(Ch, K1, *wp++); M15(Ch, K1, *wp++);
127 0 M11(Ch, K1, *wp ); M12(Ch, K1, A1( 0)); M13(Ch, K1, A1( 1));
128 0 M14(Ch, K1, A1( 2)); M15(Ch, K1, A1( 3)); M11(Pa, K2, A1( 4));
129 0 M12(Pa, K2, A1( 5)); M13(Pa, K2, A1( 6)); M14(Pa, K2, A1( 7));
130 0 M15(Pa, K2, A1( 8)); M11(Pa, K2, A1( 9)); M12(Pa, K2, A1(10));
131 0 M13(Pa, K2, A1(11)); M14(Pa, K2, A1(12)); M15(Pa, K2, A1(13));
132 0 M11(Pa, K2, A1(14)); M12(Pa, K2, A1(15)); M13(Pa, K2, A1( 0));
133 0 M14(Pa, K2, A1( 1)); M15(Pa, K2, A1( 2)); M11(Pa, K2, A1( 3));
134 0 M12(Pa, K2, A1( 4)); M13(Pa, K2, A1( 5)); M14(Pa, K2, A1( 6));
135 0 M15(Pa, K2, A1( 7)); M11(Ma, K3, A1( 8)); M12(Ma, K3, A1( 9));
136 0 M13(Ma, K3, A1(10)); M14(Ma, K3, A1(11)); M15(Ma, K3, A1(12));
137 0 M11(Ma, K3, A1(13)); M12(Ma, K3, A1(14)); M13(Ma, K3, A1(15));
138 0 M14(Ma, K3, A1( 0)); M15(Ma, K3, A1( 1)); M11(Ma, K3, A1( 2));
139 0 M12(Ma, K3, A1( 3)); M13(Ma, K3, A1( 4)); M14(Ma, K3, A1( 5));
140 0 M15(Ma, K3, A1( 6)); M11(Ma, K3, A1( 7)); M12(Ma, K3, A1( 8));
141 0 M13(Ma, K3, A1( 9)); M14(Ma, K3, A1(10)); M15(Ma, K3, A1(11));
142 0 M11(Pa, K4, A1(12)); M12(Pa, K4, A1(13)); M13(Pa, K4, A1(14));
143 0 M14(Pa, K4, A1(15)); M15(Pa, K4, A1( 0)); M11(Pa, K4, A1( 1));
144 0 M12(Pa, K4, A1( 2)); M13(Pa, K4, A1( 3)); M14(Pa, K4, A1( 4));
145 0 M15(Pa, K4, A1( 5)); M11(Pa, K4, A1( 6)); M12(Pa, K4, A1( 7));
146 0 M13(Pa, K4, A1( 8)); M14(Pa, K4, A1( 9)); M15(Pa, K4, A1(10));
147 0 M11(Pa, K4, A1(11)); M12(Pa, K4, A1(12)); M13(Pa, K4, A1(13));
148 0 M14(Pa, K4, A1(14)); M15(Pa, K4, A1(15));
149  
150 0 H[0] += a; H[1] += b; H[2] += c; H[3] += d; H[4] += e;
151 0 }
152  
153 0 static void sha256(SHA *s, UCHR *block) /* SHA-224/256 transform */
154 {
155 W32 a, b, c, d, e, f, g, h, T1;
156 SHA_STO_CLASS W32 W[16];
157 0 W32 *kp = K256;
158 0 W32 *wp = W;
159 0 W32 *H = (W32 *) s->H;
160  
161 0 ▶ SHA32_SCHED(W, block);
Branch: 1 branch
BranchTrueFalseTotal
SHA32_SCHED(W, block); 0 0 0
162  
163 /*
164 * Use same technique as in sha1()
165 *
166 * To improve performance, unroll the loop and consolidate assignments
167 * by changing the roles of variables "a" through "h" at each step.
168 * Note that the variable "T2" is no longer needed.
169 */
170  
171 #define M2(a, b, c, d, e, f, g, h, w) \
172 T1 = h + SIGMA1(e) + Ch(e, f, g) + (*kp++) + w; \
173 h = T1 + SIGMA0(a) + Ma(a, b, c); d += T1;
174  
175 #define W21(s) W[(s+ 0) & 0xf]
176 #define W22(s) W[(s+14) & 0xf]
177 #define W23(s) W[(s+ 9) & 0xf]
178 #define W24(s) W[(s+ 1) & 0xf]
179  
180 #define A2(s) (W21(s) += sigma1(W22(s)) + W23(s) + sigma0(W24(s)))
181  
182 #define M21(w) M2(a, b, c, d, e, f, g, h, w)
183 #define M22(w) M2(h, a, b, c, d, e, f, g, w)
184 #define M23(w) M2(g, h, a, b, c, d, e, f, w)
185 #define M24(w) M2(f, g, h, a, b, c, d, e, w)
186 #define M25(w) M2(e, f, g, h, a, b, c, d, w)
187 #define M26(w) M2(d, e, f, g, h, a, b, c, w)
188 #define M27(w) M2(c, d, e, f, g, h, a, b, w)
189 #define M28(w) M2(b, c, d, e, f, g, h, a, w)
190  
191 0 a = H[0]; b = H[1]; c = H[2]; d = H[3];
192 0 e = H[4]; f = H[5]; g = H[6]; h = H[7];
193  
194 0 M21( *wp++); M22( *wp++); M23( *wp++); M24( *wp++);
195 0 M25( *wp++); M26( *wp++); M27( *wp++); M28( *wp++);
196 0 M21( *wp++); M22( *wp++); M23( *wp++); M24( *wp++);
197 0 M25( *wp++); M26( *wp++); M27( *wp++); M28( *wp );
198 0 M21(A2( 0)); M22(A2( 1)); M23(A2( 2)); M24(A2( 3));
199 0 M25(A2( 4)); M26(A2( 5)); M27(A2( 6)); M28(A2( 7));
200 0 M21(A2( 8)); M22(A2( 9)); M23(A2(10)); M24(A2(11));
201 0 M25(A2(12)); M26(A2(13)); M27(A2(14)); M28(A2(15));
202 0 M21(A2( 0)); M22(A2( 1)); M23(A2( 2)); M24(A2( 3));
203 0 M25(A2( 4)); M26(A2( 5)); M27(A2( 6)); M28(A2( 7));
204 0 M21(A2( 8)); M22(A2( 9)); M23(A2(10)); M24(A2(11));
205 0 M25(A2(12)); M26(A2(13)); M27(A2(14)); M28(A2(15));
206 0 M21(A2( 0)); M22(A2( 1)); M23(A2( 2)); M24(A2( 3));
207 0 M25(A2( 4)); M26(A2( 5)); M27(A2( 6)); M28(A2( 7));
208 0 M21(A2( 8)); M22(A2( 9)); M23(A2(10)); M24(A2(11));
209 0 M25(A2(12)); M26(A2(13)); M27(A2(14)); M28(A2(15));
210  
211 0 H[0] += a; H[1] += b; H[2] += c; H[3] += d;
212 0 H[4] += e; H[5] += f; H[6] += g; H[7] += h;
213 0 }
214  
215 #include "sha64bit.c"
216  
217 #define SETBIT(s, pos) s[(pos) >> 3] |= (0x01 << (7 - (pos) % 8))
218 #define CLRBIT(s, pos) s[(pos) >> 3] &= ~(0x01 << (7 - (pos) % 8))
219 #define NBYTES(nbits) ((nbits) > 0 ? 1 + (((nbits) - 1) >> 3) : 0)
220 #define HEXLEN(nbytes) ((nbytes) << 1)
221 #define B64LEN(nbytes) (((nbytes) % 3 == 0) ? ((nbytes) / 3) * 4 \
222 : ((nbytes) / 3) * 4 + ((nbytes) % 3) + 1)
223  
224 /* w32mem: writes 32-bit word to memory in big-endian order */
225 0 static void w32mem(UCHR *mem, W32 w32)
226 {
227 int i;
228  
229 0 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 0 0 0
230 0 *mem++ = (UCHR) (SR32(w32, 24-i*8) & 0xff);
231 0 }
232  
233 /* digcpy: writes current state to digest buffer */
234 0 static void digcpy(SHA *s)
235 {
236 UINT i;
237 0 UCHR *d = s->digest;
238 0 W32 *p32 = (W32 *) s->H;
239 0 W64 *p64 = (W64 *) s->H;
240  
241 0 ▶ if (s->alg <= SHA256)
Branch: 1 branch
BranchTrueFalseTotal
if (s->alg <= SHA256) 0 0 0
242 0 ▶ for (i = 0; i < 8; i++, d += 4)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++, d += 4) 0 0 0
243 0 w32mem(d, *p32++);
244 else
245 0 ▶ for (i = 0; i < 8; i++, d += 8) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++, d += 8) { 0 0 0
246 0 w32mem(d, (W32) ((*p64 >> 16) >> 16));
247 0 w32mem(d+4, (W32) (*p64++ & SHA32_MAX));
248 }
249 0 }
250  
251 #define SHA_INIT(algo, transform) \
252 do { \
253 memset(s, 0, sizeof(SHA)); \
254 s->alg = algo; s->sha = sha ## transform; \
255 memcpy(s->H, H0 ## algo, sizeof(H0 ## algo)); \
256 s->blocksize = SHA ## algo ## _BLOCK_BITS; \
257 s->digestlen = SHA ## algo ## _DIGEST_BITS >> 3; \
258 } while (0)
259  
260 /* sharewind: re-initializes the digest object */
261 0 void sharewind(SHA *s)
262 {
263 0 ▶ if (s->alg == SHA1) SHA_INIT(1, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (s->alg == SHA1) SHA_INIT(1, 1); 0 0 0
264 0 ▶ else if (s->alg == SHA224) SHA_INIT(224, 256);
Branch: 1 branch
BranchTrueFalseTotal
else if (s->alg == SHA224) SHA_INIT(224, 256); 0 0 0
265 0 ▶ else if (s->alg == SHA256) SHA_INIT(256, 256);
Branch: 1 branch
BranchTrueFalseTotal
else if (s->alg == SHA256) SHA_INIT(256, 256); 0 0 0
266 0 ▶ else if (s->alg == SHA384) SHA_INIT(384, 512);
Branch: 1 branch
BranchTrueFalseTotal
else if (s->alg == SHA384) SHA_INIT(384, 512); 0 0 0
267 0 ▶ else if (s->alg == SHA512) SHA_INIT(512, 512);
Branch: 1 branch
BranchTrueFalseTotal
else if (s->alg == SHA512) SHA_INIT(512, 512); 0 0 0
268 0 ▶ else if (s->alg == SHA512224) SHA_INIT(512224, 512);
Branch: 1 branch
BranchTrueFalseTotal
else if (s->alg == SHA512224) SHA_INIT(512224, 512); 0 0 0
269 0 ▶ else if (s->alg == SHA512256) SHA_INIT(512256, 512);
Branch: 1 branch
BranchTrueFalseTotal
else if (s->alg == SHA512256) SHA_INIT(512256, 512); 0 0 0
270 0 }
271  
272 /* shaopen: creates a new digest object */
273 0 SHA *shaopen(int alg)
274 {
275 SHA *s;
276  
277 0 ▶ if (alg != SHA1 && alg != SHA224 && alg != SHA256 &&
Branch: 4 branches
BranchTrueFalseTotal
if (alg != SHA1 && alg != SHA224 && alg != SHA256 && 0 0 0
if (alg != SHA1 && alg != SHA224 && alg != SHA256 && 0 0 0
if (alg != SHA1 && alg != SHA224 && alg != SHA256 && 0 0 0
if (alg != SHA1 && alg != SHA224 && alg != SHA256 && 0 0 0
278 0 ▶ alg != SHA384 && alg != SHA512 &&
Branch: 2 branches
BranchTrueFalseTotal
alg != SHA384 && alg != SHA512 && 0 0 0
alg != SHA384 && alg != SHA512 && 0 0 0
279 0 ▶ alg != SHA512224 && alg != SHA512256)
Branch: 1 branch
BranchTrueFalseTotal
alg != SHA512224 && alg != SHA512256) 0 0 0
280 0 return(NULL);
281 if (alg >= SHA384 && !sha_384_512)
282 return(NULL);
283 0 SHA_newz(0, s, 1, SHA);
284 0 ▶ if (s == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (s == NULL) 0 0 0
285 0 return(NULL);
286 0 s->alg = alg;
287 0 sharewind(s);
288 0 return(s);
289 }
290  
291 /* shadirect: updates state directly (w/o going through s->block) */
292 0 static ULNG shadirect(UCHR *bitstr, ULNG bitcnt, SHA *s)
293 {
294 0 ULNG savecnt = bitcnt;
295  
296 0 ▶ while (bitcnt >= s->blocksize) {
Branch: 1 branch
BranchTrueFalseTotal
while (bitcnt >= s->blocksize) { 0 0 0
297 0 s->sha(s, bitstr);
298 0 bitstr += (s->blocksize >> 3);
299 0 bitcnt -= s->blocksize;
300 }
301 0 ▶ if (bitcnt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bitcnt > 0) { 0 0 0
302 0 ▶ memcpy(s->block, bitstr, NBYTES(bitcnt));
Branch: 1 branch
BranchTrueFalseTotal
memcpy(s->block, bitstr, NBYTES(bitcnt)); 0 0 0
303 0 s->blockcnt = bitcnt;
304 }
305 0 return(savecnt);
306 }
307  
308 /* shabytes: updates state for byte-aligned input data */
309 0 static ULNG shabytes(UCHR *bitstr, ULNG bitcnt, SHA *s)
310 {
311 UINT offset;
312 UINT nbits;
313 0 ULNG savecnt = bitcnt;
314  
315 0 offset = s->blockcnt >> 3;
316 0 ▶ if (s->blockcnt + bitcnt >= s->blocksize) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->blockcnt + bitcnt >= s->blocksize) { 0 0 0
317 0 nbits = s->blocksize - s->blockcnt;
318 0 memcpy(s->block+offset, bitstr, nbits>>3);
319 0 bitcnt -= nbits;
320 0 bitstr += (nbits >> 3);
321 0 s->sha(s, s->block), s->blockcnt = 0;
322 0 shadirect(bitstr, bitcnt, s);
323 }
324 else {
325 0 ▶ memcpy(s->block+offset, bitstr, NBYTES(bitcnt));
Branch: 1 branch
BranchTrueFalseTotal
memcpy(s->block+offset, bitstr, NBYTES(bitcnt)); 0 0 0
326 0 s->blockcnt += bitcnt;
327 }
328 0 return(savecnt);
329 }
330  
331 /* shabits: updates state for bit-aligned input data */
332 0 static ULNG shabits(UCHR *bitstr, ULNG bitcnt, SHA *s)
333 {
334 UINT i;
335 UINT gap;
336 ULNG nbits;
337 UCHR buf[1<<9];
338 0 UINT bufsize = sizeof(buf);
339 0 ULNG bufbits = (ULNG) bufsize << 3;
340 0 ▶ UINT nbytes = NBYTES(bitcnt);
Branch: 1 branch
BranchTrueFalseTotal
UINT nbytes = NBYTES(bitcnt); 0 0 0
341 0 ULNG savecnt = bitcnt;
342  
343 0 gap = 8 - s->blockcnt % 8;
344 0 s->block[s->blockcnt>>3] &= ~0 << gap;
345 0 s->block[s->blockcnt>>3] |= *bitstr >> (8 - gap);
346 0 s->blockcnt += bitcnt < gap ? bitcnt : gap;
347 0 ▶ if (bitcnt < gap)
Branch: 1 branch
BranchTrueFalseTotal
if (bitcnt < gap) 0 0 0
348 0 return(savecnt);
349 0 ▶ if (s->blockcnt == s->blocksize)
Branch: 1 branch
BranchTrueFalseTotal
if (s->blockcnt == s->blocksize) 0 0 0
350 0 s->sha(s, s->block), s->blockcnt = 0;
351 0 ▶ if ((bitcnt -= gap) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((bitcnt -= gap) == 0) 0 0 0
352 0 return(savecnt);
353 0 ▶ while (nbytes > bufsize) {
Branch: 1 branch
BranchTrueFalseTotal
while (nbytes > bufsize) { 0 0 0
354 0 ▶ for (i = 0; i < bufsize; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bufsize; i++) 0 0 0
355 0 buf[i] = bitstr[i] << gap | bitstr[i+1] >> (8-gap);
356 0 nbits = bitcnt < bufbits ? bitcnt : bufbits;
357 0 shabytes(buf, nbits, s);
358 0 bitcnt -= nbits, bitstr += bufsize, nbytes -= bufsize;
359 }
360 0 ▶ for (i = 0; i < nbytes - 1; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nbytes - 1; i++) 0 0 0
361 0 buf[i] = bitstr[i] << gap | bitstr[i+1] >> (8-gap);
362 0 buf[nbytes-1] = bitstr[nbytes-1] << gap;
363 0 shabytes(buf, bitcnt, s);
364 0 return(savecnt);
365 }
366  
367 /* shawrite: triggers a state update using data in bitstr/bitcnt */
368 0 ULNG shawrite(UCHR *bitstr, ULNG bitcnt, SHA *s)
369 {
370 0 ▶ if (bitcnt < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (bitcnt < 1) 0 0 0
371 0 return(0);
372 0 ▶ if (SHA_LO32(s->lenll += bitcnt) < bitcnt)
Branch: 1 branch
BranchTrueFalseTotal
if (SHA_LO32(s->lenll += bitcnt) < bitcnt) 0 0 0
373 0 ▶ if (SHA_LO32(++s->lenlh) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SHA_LO32(++s->lenlh) == 0) 0 0 0
374 0 ▶ if (SHA_LO32(++s->lenhl) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SHA_LO32(++s->lenhl) == 0) 0 0 0
375 0 s->lenhh++;
376 0 ▶ if (s->blockcnt == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s->blockcnt == 0) 0 0 0
377 0 return(shadirect(bitstr, bitcnt, s));
378 0 ▶ else if (s->blockcnt % 8 == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (s->blockcnt % 8 == 0) 0 0 0
379 0 return(shabytes(bitstr, bitcnt, s));
380 else
381 0 return(shabits(bitstr, bitcnt, s));
382 }
383  
384 /* shafinish: pads remaining block(s) and computes final digest state */
385 0 void shafinish(SHA *s)
386 {
387 UINT lenpos, lhpos, llpos;
388  
389 0 ▶ lenpos = s->blocksize == SHA1_BLOCK_BITS ? 448 : 896;
Branch: 1 branch
BranchTrueFalseTotal
lenpos = s->blocksize == SHA1_BLOCK_BITS ? 448 : 896; 0 0 0
390 0 ▶ lhpos = s->blocksize == SHA1_BLOCK_BITS ? 56 : 120;
Branch: 1 branch
BranchTrueFalseTotal
lhpos = s->blocksize == SHA1_BLOCK_BITS ? 56 : 120; 0 0 0
391 0 ▶ llpos = s->blocksize == SHA1_BLOCK_BITS ? 60 : 124;
Branch: 1 branch
BranchTrueFalseTotal
llpos = s->blocksize == SHA1_BLOCK_BITS ? 60 : 124; 0 0 0
392 0 SETBIT(s->block, s->blockcnt), s->blockcnt++;
393 0 ▶ while (s->blockcnt > lenpos)
Branch: 1 branch
BranchTrueFalseTotal
while (s->blockcnt > lenpos) 0 0 0
394 0 ▶ if (s->blockcnt < s->blocksize)
Branch: 1 branch
BranchTrueFalseTotal
if (s->blockcnt < s->blocksize) 0 0 0
395 0 CLRBIT(s->block, s->blockcnt), s->blockcnt++;
396 else
397 0 s->sha(s, s->block), s->blockcnt = 0;
398 0 ▶ while (s->blockcnt < lenpos)
Branch: 1 branch
BranchTrueFalseTotal
while (s->blockcnt < lenpos) 0 0 0
399 0 CLRBIT(s->block, s->blockcnt), s->blockcnt++;
400 0 ▶ if (s->blocksize > SHA1_BLOCK_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->blocksize > SHA1_BLOCK_BITS) { 0 0 0
401 0 w32mem(s->block + 112, s->lenhh);
402 0 w32mem(s->block + 116, s->lenhl);
403 }
404 0 w32mem(s->block + lhpos, s->lenlh);
405 0 w32mem(s->block + llpos, s->lenll);
406 0 s->sha(s, s->block);
407 0 }
408  
409 /* shadigest: returns pointer to current digest (binary) */
410 0 UCHR *shadigest(SHA *s)
411 {
412 0 digcpy(s);
413 0 return(s->digest);
414 }
415  
416 /* shahex: returns pointer to current digest (hexadecimal) */
417 0 char *shahex(SHA *s)
418 {
419 int i;
420  
421 0 digcpy(s);
422 0 s->hex[0] = '\0';
423 0 ▶ if (HEXLEN((size_t) s->digestlen) >= sizeof(s->hex))
Branch: 1 branch
BranchTrueFalseTotal
if (HEXLEN((size_t) s->digestlen) >= sizeof(s->hex)) 0 0 0
424 0 return(s->hex);
425 0 ▶ for (i = 0; i < s->digestlen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->digestlen; i++) 0 0 0
426 0 sprintf(s->hex+i*2, "%02x", s->digest[i]);
427 0 return(s->hex);
428 }
429  
430 /* map: translation map for Base 64 encoding */
431 static char map[] =
432 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
433  
434 /* encbase64: encodes input (0 to 3 bytes) into Base 64 */
435 0 static void encbase64(UCHR *in, int n, char *out)
436 {
437 0 UCHR byte[3] = {0, 0, 0};
438  
439 0 out[0] = '\0';
440 0 ▶ if (n < 1 || n > 3)
Branch: 2 branches
BranchTrueFalseTotal
if (n < 1 || n > 3) 0 0 0
if (n < 1 || n > 3) 0 0 0
441 0 return;
442 0 memcpy(byte, in, n);
443 0 out[0] = map[byte[0] >> 2];
444 0 out[1] = map[((byte[0] & 0x03) << 4) | (byte[1] >> 4)];
445 0 out[2] = map[((byte[1] & 0x0f) << 2) | (byte[2] >> 6)];
446 0 out[3] = map[byte[2] & 0x3f];
447 0 out[n+1] = '\0';
448 }
449  
450 /* shabase64: returns pointer to current digest (Base 64) */
451 0 char *shabase64(SHA *s)
452 {
453 int n;
454 UCHR *q;
455 char out[5];
456  
457 0 digcpy(s);
458 0 s->base64[0] = '\0';
459 0 ▶ if (B64LEN(s->digestlen) >= sizeof(s->base64))
Branch: 2 branches
BranchTrueFalseTotal
if (B64LEN(s->digestlen) >= sizeof(s->base64)) 0 0 0
if (B64LEN(s->digestlen) >= sizeof(s->base64)) 0 0 0
460 0 return(s->base64);
461 0 ▶ for (n = s->digestlen, q = s->digest; n > 3; n -= 3, q += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = s->digestlen, q = s->digest; n > 3; n -= 3, q += 3) { 0 0 0
462 0 encbase64(q, 3, out);
463 0 strcat(s->base64, out);
464 }
465 0 encbase64(q, n, out);
466 0 strcat(s->base64, out);
467 0 return(s->base64);
468 }
469  
470 /* shadsize: returns length of digest in bytes */
471 0 int shadsize(SHA *s)
472 {
473 0 return(s->digestlen);
474 }
475  
476 /* shaalg: returns which SHA algorithm is being used */
477 0 int shaalg(SHA *s)
478 {
479 0 return(s->alg);
480 }
481  
482 /* shadup: duplicates current digest object */
483 0 SHA *shadup(SHA *s)
484 {
485 SHA *p;
486  
487 0 SHA_new(0, p, 1, SHA);
488 0 ▶ if (p == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p == NULL) 0 0 0
489 0 return(NULL);
490 0 memcpy(p, s, sizeof(SHA));
491 0 return(p);
492 }
493  
494 /* shadump: dumps digest object to a human-readable ASCII file */
495 0 int shadump(char *file, SHA *s)
496 {
497 int i, j;
498 SHA_FILE *f;
499 0 UCHR *p = shadigest(s);
500  
501 0 ▶ if (file == NULL || strlen(file) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (file == NULL || strlen(file) == 0) 0 0 0
if (file == NULL || strlen(file) == 0) 0 0 0
502 0 f = SHA_stdout();
503 0 ▶ else if ((f = SHA_open(file, "w")) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
else if ((f = SHA_open(file, "w")) == NULL) 0 0 0
504 0 return(0);
505 0 SHA_fprintf(f, "alg:%d\nH", s->alg);
506 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
507 0 ▶ for (j = 0; j < (s->alg <= 256 ? 4 : 8); j++)
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < (s->alg <= 256 ? 4 : 8); j++) 0 0 0
for (j = 0; j < (s->alg <= 256 ? 4 : 8); j++) 0 0 0
508 0 ▶ SHA_fprintf(f, "%s%02x", j==0 ? ":" : "", *p++);
Branch: 1 branch
BranchTrueFalseTotal
SHA_fprintf(f, "%s%02x", j==0 ? ":" : "", *p++); 0 0 0
509 0 SHA_fprintf(f, "\nblock");
510 0 ▶ for (i = 0; i < (int) (s->blocksize >> 3); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (int) (s->blocksize >> 3); i++) 0 0 0
511 0 SHA_fprintf(f, ":%02x", s->block[i]);
512 0 SHA_fprintf(f, "\nblockcnt:%u\n", s->blockcnt);
513 0 SHA_fprintf(f, "lenhh:%lu\nlenhl:%lu\nlenlh:%lu\nlenll:%lu\n",
514 0 (ULNG) LO32(s->lenhh), (ULNG) LO32(s->lenhl),
515 0 (ULNG) LO32(s->lenlh), (ULNG) LO32(s->lenll));
516 0 ▶ if (f != SHA_stdout())
Branch: 1 branch
BranchTrueFalseTotal
if (f != SHA_stdout()) 0 0 0
517 0 SHA_close(f);
518 0 return(1);
519 }
520  
521 /* fgetstr: reads (and returns pointer to) next line of file */
522 0 static char *fgetstr(char *line, UINT maxsize, SHA_FILE *f)
523 {
524 char *p;
525  
526 0 ▶ if (SHA_feof(f) || maxsize == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (SHA_feof(f) || maxsize == 0) 0 0 0
if (SHA_feof(f) || maxsize == 0) 0 0 0
527 0 return(NULL);
528 0 ▶ for (p = line; !SHA_feof(f) && maxsize > 1; maxsize--)
Branch: 2 branches
BranchTrueFalseTotal
for (p = line; !SHA_feof(f) && maxsize > 1; maxsize--) 0 0 0
for (p = line; !SHA_feof(f) && maxsize > 1; maxsize--) 0 0 0
529 0 ▶ if ((*p++ = SHA_getc(f)) == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if ((*p++ = SHA_getc(f)) == '\n') 0 0 0
530 0 break;
531 0 *p = '\0';
532 0 return(line);
533 }
534  
535 /* empty: returns true if line contains only whitespace characters */
536 0 static int empty(char *line)
537 {
538 char *p;
539  
540 0 ▶ for (p = line; *p; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = line; *p; p++) 0 0 0
541 0 ▶ if (!isspace(*p))
Branch: 1 branch
BranchTrueFalseTotal
if (!isspace(*p)) 0 0 0
542 0 return(0);
543 0 return(1);
544 }
545  
546 /* getval: null-terminates field value, and sets pointer to rest of line */
547 0 static char *getval(char *line, char **pprest)
548 {
549 char *p, *v;
550  
551 0 ▶ for (v = line; *v == ':' || isspace(*v); v++)
Branch: 2 branches
BranchTrueFalseTotal
for (v = line; *v == ':' || isspace(*v); v++) 0 0 0
for (v = line; *v == ':' || isspace(*v); v++) 0 0 0
552 ;
553 0 ▶ for (p = v; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = v; *p; p++) { 0 0 0
554 0 ▶ if (*p == ':' || isspace(*p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == ':' || isspace(*p)) { 0 0 0
if (*p == ':' || isspace(*p)) { 0 0 0
555 0 *p++ = '\0';
556 0 break;
557 }
558 }
559 0 *pprest = p;
560 0 ▶ return(p == v ? NULL : v);
Branch: 1 branch
BranchTrueFalseTotal
return(p == v ? NULL : v); 0 0 0
561 }
562  
563 /* types of values present in dump file */
564 #define T_C 1 /* character */
565 #define T_I 2 /* normal integer */
566 #define T_L 3 /* 32-bit value */
567 #define T_Q 4 /* 64-bit value */
568  
569 /* ldvals: checks next line in dump file against tag, and loads values */
570 0 static int ldvals(
571 SHA_FILE *f,
572 const char *tag,
573 int type,
574 void *pval,
575 int reps,
576 int base)
577 {
578 char *p, *pr, line[512];
579 0 UCHR *pc = (UCHR *) pval; UINT *pi = (UINT *) pval;
580 0 W32 *pl = (W32 *) pval; W64 *pq = (W64 *) pval;
581  
582 0 ▶ while ((p = fgetstr(line, sizeof(line), f)) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
while ((p = fgetstr(line, sizeof(line), f)) != NULL) 0 0 0
583 0 ▶ if (line[0] != '#' && !empty(line))
Branch: 2 branches
BranchTrueFalseTotal
if (line[0] != '#' && !empty(line)) 0 0 0
if (line[0] != '#' && !empty(line)) 0 0 0
584 0 break;
585 0 ▶ if (p == NULL || strcmp(getval(line, &pr), tag) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (p == NULL || strcmp(getval(line, &pr), tag) != 0) 0 0 0
if (p == NULL || strcmp(getval(line, &pr), tag) != 0) 0 0 0
586 0 return(0);
587 0 ▶ while (reps-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (reps-- > 0) { 0 0 0
588 0 ▶ if ((p = getval(pr, &pr)) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((p = getval(pr, &pr)) == NULL) 0 0 0
589 0 return(1);
590 0 switch (type) {
591 0 case T_C: *pc++ = (UCHR) strtoul(p, NULL, base); break;
592 0 case T_I: *pi++ = (UINT) strtoul(p, NULL, base); break;
593 0 case T_L: *pl++ = (W32 ) strtoul(p, NULL, base); break;
594 0 case T_Q: *pq++ = (W64 ) strto64(p ); break;
595 }
596 }
597 0 return(1);
598 }
599  
600 /* closeall: closes dump file and de-allocates digest object */
601 0 static SHA *closeall(SHA_FILE *f, SHA *s)
602 {
603 0 ▶ if (f != NULL && f != SHA_stdin())
Branch: 2 branches
BranchTrueFalseTotal
if (f != NULL && f != SHA_stdin()) 0 0 0
if (f != NULL && f != SHA_stdin()) 0 0 0
604 0 SHA_close(f);
605 0 ▶ if (s != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (s != NULL) 0 0 0
606 0 shaclose(s);
607 0 return(NULL);
608 }
609  
610 /* shaload: creates digest object corresponding to contents of dump file */
611 0 SHA *shaload(char *file)
612 {
613 int alg;
614 0 SHA *s = NULL;
615 SHA_FILE *f;
616  
617 0 ▶ if (file == NULL || strlen(file) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (file == NULL || strlen(file) == 0) 0 0 0
if (file == NULL || strlen(file) == 0) 0 0 0
618 0 f = SHA_stdin();
619 0 ▶ else if ((f = SHA_open(file, "r")) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
else if ((f = SHA_open(file, "r")) == NULL) 0 0 0
620 0 return(NULL);
621 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 0 0
622 /* avoid parens by exploiting precedence of (type)&-> */
623 0 ▶ !ldvals(f,"alg",T_I,(VP)&alg,1,10) ||
Branch: 1 branch
BranchTrueFalseTotal
!ldvals(f,"alg",T_I,(VP)&alg,1,10) || 0 0 0
624 0 ▶ ((s = shaopen(alg)) == NULL) ||
Branch: 1 branch
BranchTrueFalseTotal
((s = shaopen(alg)) == NULL) || 0 0 0
625 0 ▶ !ldvals(f,"H",alg<=SHA256?T_L:T_Q,(VP)s->H,8,16) ||
Branch: 2 branches
BranchTrueFalseTotal
!ldvals(f,"H",alg<=SHA256?T_L:T_Q,(VP)s->H,8,16) || 0 0 0
!ldvals(f,"H",alg<=SHA256?T_L:T_Q,(VP)s->H,8,16) || 0 0 0
626 0 ▶ !ldvals(f,"block",T_C,(VP)s->block,s->blocksize/8,16) ||
Branch: 1 branch
BranchTrueFalseTotal
!ldvals(f,"block",T_C,(VP)s->block,s->blocksize/8,16) || 0 0 0
627 0 !ldvals(f,"blockcnt",T_I,(VP)&s->blockcnt,1,10) ||
628 0 ▶ (alg <= SHA256 && s->blockcnt >= SHA1_BLOCK_BITS) ||
Branch: 2 branches
BranchTrueFalseTotal
(alg <= SHA256 && s->blockcnt >= SHA1_BLOCK_BITS) || 0 0 0
(alg <= SHA256 && s->blockcnt >= SHA1_BLOCK_BITS) || 0 0 0
629 0 ▶ (alg >= SHA384 && s->blockcnt >= SHA384_BLOCK_BITS) ||
Branch: 1 branch
BranchTrueFalseTotal
(alg >= SHA384 && s->blockcnt >= SHA384_BLOCK_BITS) || 0 0 0
630 0 ▶ !ldvals(f,"lenhh",T_L,(VP)&s->lenhh,1,10) ||
Branch: 1 branch
BranchTrueFalseTotal
!ldvals(f,"lenhh",T_L,(VP)&s->lenhh,1,10) || 0 0 0
631 0 ▶ !ldvals(f,"lenhl",T_L,(VP)&s->lenhl,1,10) ||
Branch: 1 branch
BranchTrueFalseTotal
!ldvals(f,"lenhl",T_L,(VP)&s->lenhl,1,10) || 0 0 0
632 0 ▶ !ldvals(f,"lenlh",T_L,(VP)&s->lenlh,1,10) ||
Branch: 1 branch
BranchTrueFalseTotal
!ldvals(f,"lenlh",T_L,(VP)&s->lenlh,1,10) || 0 0 0
633 0 !ldvals(f,"lenll",T_L,(VP)&s->lenll,1,10)
634 )
635 0 return(closeall(f, s));
636 0 ▶ if (f != SHA_stdin())
Branch: 1 branch
BranchTrueFalseTotal
if (f != SHA_stdin()) 0 0 0
637 0 SHA_close(f);
638 0 return(s);
639 }
640  
641 /* shaclose: de-allocates digest object */
642 0 int shaclose(SHA *s)
643 {
644 0 ▶ if (s != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (s != NULL) { 0 0 0
645 0 memset(s, 0, sizeof(SHA));
646 0 SHA_free(s);
647 }
648 0 return(0);
649 }
« _rijndael.c ↑ Summary sha64bit.c »