File Coverage

inc/matrixssl-3-9-3-open/crypto/digest/sha512.c
Criterion Covered Total %
statement 92 92 100.0
branch 28 30 93.3
condition n/a
subroutine n/a
pod n/a
total 120 122 98.3


line stmt bran cond sub pod time code
1             /**
2             * @file sha512.c
3             * @version 950bba4 (HEAD -> master)
4             *
5             * SHA256 hash implementation.
6             */
7             /*
8             * Copyright (c) 2013-2017 INSIDE Secure Corporation
9             * Copyright (c) PeerSec Networks, 2002-2011
10             * All Rights Reserved
11             *
12             * The latest version of this code is available at http://www.matrixssl.org
13             *
14             * This software is open source; you can redistribute it and/or modify
15             * it under the terms of the GNU General Public License as published by
16             * the Free Software Foundation; either version 2 of the License, or
17             * (at your option) any later version.
18             *
19             * This General Public License does NOT permit incorporating this software
20             * into proprietary programs. If you are unable to comply with the GPL, a
21             * commercial license for this software may be purchased from INSIDE at
22             * http://www.insidesecure.com/
23             *
24             * This program is distributed in WITHOUT ANY WARRANTY; without even the
25             * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26             * See the GNU General Public License for more details.
27             *
28             * You should have received a copy of the GNU General Public License
29             * along with this program; if not, write to the Free Software
30             * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31             * http://www.gnu.org/copyleft/gpl.html
32             */
33             /******************************************************************************/
34              
35             #include "../cryptoImpl.h"
36              
37             #if defined(USE_MATRIX_SHA384) || defined(USE_MATRIX_SHA512)
38              
39             # ifndef HAVE_NATIVE_INT64
40             # error Must have NATIVE_INT64 support
41             # endif
42              
43             /******************************************************************************/
44              
45             /* the K array */
46             static const uint64_t K[80] = {
47             CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
48             CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
49             CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
50             CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118),
51             CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
52             CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2),
53             CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
54             CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694),
55             CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
56             CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65),
57             CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
58             CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5),
59             CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
60             CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4),
61             CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
62             CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70),
63             CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
64             CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df),
65             CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
66             CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b),
67             CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001),
68             CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30),
69             CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
70             CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8),
71             CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
72             CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8),
73             CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
74             CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3),
75             CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
76             CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec),
77             CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
78             CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b),
79             CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
80             CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178),
81             CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
82             CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b),
83             CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
84             CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c),
85             CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
86             CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817)
87             };
88              
89             /* Various logical functions */
90             # define Ch(x, y, z) (z ^ (x & (y ^ z)))
91             # define Maj(x, y, z) (((x | y) & z) | (x & y))
92             # define S(x, n) ROR64c(x, n)
93             # define R(x, n) (((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((uint64) n))
94             # define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
95             # define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
96             # define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
97             # define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
98              
99             /* compress 1024-bits */
100             # ifdef USE_BURN_STACK
101 156811           static void _sha512_compress(psSha512_t *sha512, const unsigned char *buf)
102             # else
103             static void sha512_compress(psSha512_t *sha512, const unsigned char *buf)
104             # endif
105             {
106             uint64 S[8], W[80], t0, t1;
107             int i;
108              
109             /* copy state into S */
110 1411299 100         for (i = 0; i < 8; i++)
111             {
112 1254488           S[i] = sha512->state[i];
113             }
114              
115             /* copy the state into 1024-bits into W[0..15] */
116 2665787 100         for (i = 0; i < 16; i++)
117             {
118 2508976           LOAD64H(W[i], buf + (8 * i));
119             }
120              
121             /* fill W[16..79] */
122 10192715 100         for (i = 16; i < 80; i++)
123             {
124 10035904           W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
125             }
126              
127             /* Compress */
128             # ifndef PS_SHA512_IMPROVE_PERF_INCREASE_CODESIZE
129 12701691 100         for (i = 0; i < 80; i++)
130             {
131 12544880           t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
132 12544880           t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
133 12544880           S[7] = S[6];
134 12544880           S[6] = S[5];
135 12544880           S[5] = S[4];
136 12544880           S[4] = S[3] + t0;
137 12544880           S[3] = S[2];
138 12544880           S[2] = S[1];
139 12544880           S[1] = S[0];
140 12544880           S[0] = t0 + t1;
141             }
142             # else
143             # define RND(a, b, c, d, e, f, g, h, i) \
144             t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
145             t1 = Sigma0(a) + Maj(a, b, c); \
146             d += t0; \
147             h = t0 + t1;
148              
149             for (i = 0; i < 80; i += 8)
150             {
151             RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i + 0);
152             RND(S[7], S[0], S[1], S[2], S[3], S[4], S[5], S[6], i + 1);
153             RND(S[6], S[7], S[0], S[1], S[2], S[3], S[4], S[5], i + 2);
154             RND(S[5], S[6], S[7], S[0], S[1], S[2], S[3], S[4], i + 3);
155             RND(S[4], S[5], S[6], S[7], S[0], S[1], S[2], S[3], i + 4);
156             RND(S[3], S[4], S[5], S[6], S[7], S[0], S[1], S[2], i + 5);
157             RND(S[2], S[3], S[4], S[5], S[6], S[7], S[0], S[1], i + 6);
158             RND(S[1], S[2], S[3], S[4], S[5], S[6], S[7], S[0], i + 7);
159             }
160             # endif /* PS_SHA512_IMPROVE_PERF_INCREASE_CODESIZE */
161              
162             /* feedback */
163 1411299 100         for (i = 0; i < 8; i++)
164             {
165 1254488           sha512->state[i] = sha512->state[i] + S[i];
166             }
167 156811           }
168              
169             /* compress 1024-bits */
170             # ifdef USE_BURN_STACK
171 156811           static void sha512_compress(psSha512_t *sha512, const unsigned char *buf)
172             {
173 156811           _sha512_compress(sha512, buf);
174 156811           psBurnStack(sizeof(uint64) * 90 + sizeof(int));
175 156811           }
176             # endif
177              
178             /******************************************************************************/
179             # ifdef USE_MATRIX_SHA512
180             /* Other 512 functions are used by 384 */
181 12328           int32_t psSha512Init(psSha512_t *sha512)
182             {
183             # ifdef CRYPTO_ASSERT
184             psAssert(sha512 != NULL);
185             # endif
186 12328           sha512->curlen = 0;
187 12328           sha512->length = 0;
188 12328           sha512->state[0] = CONST64(0x6a09e667f3bcc908);
189 12328           sha512->state[1] = CONST64(0xbb67ae8584caa73b);
190 12328           sha512->state[2] = CONST64(0x3c6ef372fe94f82b);
191 12328           sha512->state[3] = CONST64(0xa54ff53a5f1d36f1);
192 12328           sha512->state[4] = CONST64(0x510e527fade682d1);
193 12328           sha512->state[5] = CONST64(0x9b05688c2b3e6c1f);
194 12328           sha512->state[6] = CONST64(0x1f83d9abfb41bd6b);
195 12328           sha512->state[7] = CONST64(0x5be0cd19137e2179);
196 12328           return PS_SUCCESS;
197             }
198             # endif
199             /******************************************************************************/
200              
201             # ifdef USE_MATRIX_SHA512
202 150643           void psSha512Update(psSha512_t *sha512, const unsigned char *buf, uint32_t len)
203             # else
204             static void psSha512Update(psSha512_t *sha512, const unsigned char *buf, uint32_t len)
205             # endif
206             {
207             uint32_t n;
208              
209 150643 50         psAssert(sha512 != NULL);
210 150643 50         psAssert(buf != NULL);
211              
212 360661 100         while (len > 0)
213             {
214 210018 100         if (sha512->curlen == 0 && len >= 128)
    100          
215             {
216 83857           sha512_compress(sha512, (unsigned char *) buf);
217 83857           sha512->length += 1024;
218 83857           buf += 128;
219 83857           len -= 128;
220             }
221             else
222             {
223 126161           n = min(len, (128 - sha512->curlen));
224 126161           memcpy(sha512->buf + sha512->curlen, buf, (size_t) n);
225 126161           sha512->curlen += n;
226 126161           buf += n;
227 126161           len -= n;
228              
229 126161 100         if (sha512->curlen == 128)
230             {
231 17822           sha512_compress(sha512, sha512->buf);
232 17822           sha512->length += 1024;
233 17822           sha512->curlen = 0;
234             }
235             }
236             }
237 150643           }
238              
239             /******************************************************************************/
240              
241             # ifdef USE_MATRIX_SHA512
242 48773           void psSha512Final(psSha512_t *sha512, unsigned char out[SHA512_HASHLEN])
243             # else
244             static void psSha512Final(psSha512_t *sha512, unsigned char out[SHA512_HASHLEN])
245             # endif
246             {
247             int i;
248              
249             # ifdef CRYPTO_ASSERT
250             psAssert(sha512 != NULL);
251             psAssert(out != NULL);
252             if (sha512->curlen >= sizeof(sha512->buf))
253             {
254             return;
255             }
256             # endif
257             /* increase the length of the message */
258 48773           sha512->length += sha512->curlen * CONST64(8);
259              
260             /* append the '1' bit */
261 48773           sha512->buf[sha512->curlen++] = (unsigned char) 0x80;
262              
263             /* if the length is currently above 112 bytes we append zeros
264             * then compress. Then we can fall back to padding zeros and length
265             * encoding like normal.
266             */
267 48773 100         if (sha512->curlen > 112)
268             {
269 33921 100         while (sha512->curlen < 128)
270             {
271 27562           sha512->buf[sha512->curlen++] = (unsigned char) 0;
272             }
273 6359           sha512_compress(sha512, sha512->buf);
274 6359           sha512->curlen = 0;
275             }
276              
277             /* pad upto 120 bytes of zeroes
278             @note from 112 to 120 is the 64 MSB of the length.
279             We assume that you won't hash > 2^64 bits of data... :-)
280             */
281 3369414 100         while (sha512->curlen < 120)
282             {
283 3320641           sha512->buf[sha512->curlen++] = (unsigned char) 0;
284             }
285              
286             /* store length */
287 48773           STORE64H(sha512->length, sha512->buf + 120);
288 48773           sha512_compress(sha512, sha512->buf);
289              
290             /* copy output */
291 438957 100         for (i = 0; i < 8; i++)
292             {
293 390184           STORE64H(sha512->state[i], out + (8 * i));
294             }
295             # ifdef USE_BURN_STACK
296 48773           psBurnStack(sizeof(psSha512_t));
297             # endif
298 48773           }
299              
300             # ifdef USE_MATRIX_SHA384
301             /******************************************************************************/
302              
303 54727           int32_t psSha384Init(psSha384_t *sha384)
304             {
305             # ifdef CRYPTO_ASSERT
306             psAssert(sha384 != NULL);
307             # endif
308 54727           sha384->curlen = 0;
309 54727           sha384->length = 0;
310 54727           sha384->state[0] = CONST64(0xcbbb9d5dc1059ed8);
311 54727           sha384->state[1] = CONST64(0x629a292a367cd507);
312 54727           sha384->state[2] = CONST64(0x9159015a3070dd17);
313 54727           sha384->state[3] = CONST64(0x152fecd8f70e5939);
314 54727           sha384->state[4] = CONST64(0x67332667ffc00b31);
315 54727           sha384->state[5] = CONST64(0x8eb44a8768581511);
316 54727           sha384->state[6] = CONST64(0xdb0c2e0d64f98fa7);
317 54727           sha384->state[7] = CONST64(0x47b5481dbefa4fa4);
318 54727           return PS_SUCCESS;
319             }
320              
321             /******************************************************************************/
322              
323 122961           void psSha384Update(psSha384_t *sha384, const unsigned char *buf, uint32_t len)
324             {
325 122961           psSha512Update(sha384, buf, len);
326 122961           }
327              
328             /******************************************************************************/
329              
330 48758           void psSha384Final(psSha384_t *sha384, unsigned char out[SHA384_HASHLEN])
331             {
332             unsigned char buf[SHA512_HASHLEN];
333              
334             # ifdef CRYPTO_ASSERT
335             psAssert(sha384 != NULL);
336             psAssert(out != NULL);
337             if (sha384->curlen >= sizeof(sha384->buf))
338             {
339             return;
340             }
341             # endif
342 48758           psSha512Final(sha384, buf);
343 48758           memcpy(out, buf, SHA384_HASHLEN);
344             # ifdef USE_BURN_STACK
345 48758           psBurnStack(sizeof(buf));
346             # endif
347 48758           }
348              
349             # endif /* USE_MATRIX_SHA384 */
350              
351             #endif /* USE_MATRIX_SHA384 || USE_MATRIX_SHA512 */
352              
353             /******************************************************************************/
354