src/sha3nist.c

filter: Statementstmt 83.7% 72 / 86 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 80 / 100 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« JH.xs ↑ Summary src/jh.c »
1 /* $Id: sha3nist.c 154 2010-04-26 17:00:24Z tp $ */
2 /*
3 * NIST SHA-3 competition compatibility code.
4 *
5 * ==========================(LICENSE BEGIN)============================
6 *
7 * Copyright (c) 2007-2010 Projet RNRT SAPHIR
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * ===========================(LICENSE END)=============================
29 *
30 * @author Thomas Pornin <thomas.pornin@cryptolog.com>
31 */
32  
33 #include <stddef.h>
34 #include <string.h>
35  
36 #include "sha3nist.h"
37  
38 /*
39 * Compute the output, using the provided extra bits (0 to 7 bits).
40 * If the output is already computed, then this function returns 0.
41 * On error (invalid saved output length), -1 is returned.
42 */
43 static int
44 477 compute_output(hashState *state, unsigned ub, unsigned n)
45 {
46 477 ▶ if (state->output_computed)
Branch: 1 branch
BranchTrueFalseTotal
if (state->output_computed) 173 304 477
47 173 return 0;
48 304 switch (state->hashbitlen) {
49 79 case 224:
50 79 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 224_addbits_and_close)(
51 79 &state->u.ctx224, ub, n, state->output);
52 79 break;
53 75 case 256:
54 75 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 256_addbits_and_close)(
55 75 &state->u.ctx256, ub, n, state->output);
56 75 break;
57 75 case 384:
58 75 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 384_addbits_and_close)(
59 75 &state->u.ctx384, ub, n, state->output);
60 75 break;
61 75 case 512:
62 75 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 512_addbits_and_close)(
63 75 &state->u.ctx512, ub, n, state->output);
64 75 break;
65 0 default:
66 0 return -1;
67 }
68 304 state->output_computed = 1;
69 304 return 0;
70 }
71  
72 /* see sha3nist.h */
73 HashReturn
74 557 Init(hashState *state, int hashbitlen)
75 {
76 557 switch (hashbitlen) {
77 145 case 224:
78 145 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 224_init)(&state->u.ctx224);
79 145 break;
80 137 case 256:
81 137 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 256_init)(&state->u.ctx256);
82 137 break;
83 137 case 384:
84 137 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 384_init)(&state->u.ctx384);
85 137 break;
86 137 case 512:
87 137 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 512_init)(&state->u.ctx512);
88 137 break;
89 1 default:
90 1 return BAD_HASHBITLEN;
91 }
92 556 state->hashbitlen = hashbitlen;
93 556 state->output_computed = 0;
94 556 return SUCCESS;
95 }
96  
97 /* see sha3nist.h */
98 HashReturn
99 293 Update(hashState *state, const BitSequence *data, DataLength databitlen)
100 {
101 unsigned extra;
102 size_t len;
103  
104 293 ▶ if (state->output_computed)
Branch: 1 branch
BranchTrueFalseTotal
if (state->output_computed) 1 292 293
105 1 return FAIL;
106 292 extra = (unsigned)databitlen & 0x7U;
107 292 len = (size_t)(databitlen >> 3);
108 292 switch (state->hashbitlen) {
109 76 case 224:
110 76 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 224)(
111 76 &state->u.ctx224, data, len);
112 76 break;
113 72 case 256:
114 72 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 256)(
115 72 &state->u.ctx256, data, len);
116 72 break;
117 72 case 384:
118 72 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 384)(
119 72 &state->u.ctx384, data, len);
120 72 break;
121 72 case 512:
122 72 SPH_CAT(SPH_CAT(sph_, SPH_NIST), 512)(
123 72 &state->u.ctx512, data, len);
124 72 break;
125 0 default:
126 0 return FAIL;
127 }
128 292 ▶ if (extra > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (extra > 0) 174 118 292
129 174 compute_output(state, data[len], extra);
130 292 return SUCCESS;
131 }
132  
133 /* see sha3nist.h */
134 HashReturn
135 303 Final(hashState *state, BitSequence *hashval)
136 {
137 size_t len;
138  
139 303 ▶ if (compute_output(state, 0, 0) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (compute_output(state, 0, 0) < 0) 0 303 303
140 0 return FAIL;
141 303 len = state->hashbitlen >> 3;
142 303 ▶ if (len > 64)
Branch: 1 branch
BranchTrueFalseTotal
if (len > 64) 0 303 303
143 0 return FAIL;
144 303 memcpy(hashval, state->output, len);
145 303 return SUCCESS;
146 }
147  
148 /* see sha3nist.h */
149 HashReturn
150 0 Hash(int hashbitlen, const BitSequence *data,
151 DataLength databitlen, BitSequence *hashval)
152 {
153 hashState st;
154 HashReturn r;
155  
156 0 r = Init(&st, hashbitlen);
157 0 ▶ if (r != SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (r != SUCCESS) 0 0 0
158 0 return r;
159 0 r = Update(&st, data, databitlen);
160 0 ▶ if (r != SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (r != SUCCESS) 0 0 0
161 0 return r;
162 0 return Final(&st, hashval);
163 }
« JH.xs ↑ Summary src/jh.c »