sha2.c revision 1.19 1 1.19 skrll /* $NetBSD: sha2.c,v 1.19 2009/08/21 09:40:51 skrll Exp $ */
2 1.1 christos /* $KAME: sha2.c,v 1.9 2003/07/20 00:28:38 itojun Exp $ */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * sha2.c
6 1.1 christos *
7 1.1 christos * Version 1.0.0beta1
8 1.1 christos *
9 1.1 christos * Written by Aaron D. Gifford <me (at) aarongifford.com>
10 1.1 christos *
11 1.1 christos * Copyright 2000 Aaron D. Gifford. All rights reserved.
12 1.1 christos *
13 1.1 christos * Redistribution and use in source and binary forms, with or without
14 1.1 christos * modification, are permitted provided that the following conditions
15 1.1 christos * are met:
16 1.1 christos * 1. Redistributions of source code must retain the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer.
18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 christos * notice, this list of conditions and the following disclaimer in the
20 1.1 christos * documentation and/or other materials provided with the distribution.
21 1.1 christos * 3. Neither the name of the copyright holder nor the names of contributors
22 1.1 christos * may be used to endorse or promote products derived from this software
23 1.1 christos * without specific prior written permission.
24 1.1 christos *
25 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
26 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
29 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 christos * SUCH DAMAGE.
36 1.1 christos *
37 1.1 christos */
38 1.1 christos
39 1.14 joerg #if HAVE_NBTOOL_CONFIG_H
40 1.14 joerg #include "nbtool_config.h"
41 1.14 joerg #endif
42 1.14 joerg
43 1.1 christos #include <sys/cdefs.h>
44 1.1 christos
45 1.1 christos #if defined(_KERNEL) || defined(_STANDALONE)
46 1.19 skrll __KERNEL_RCSID(0, "$NetBSD: sha2.c,v 1.19 2009/08/21 09:40:51 skrll Exp $");
47 1.1 christos
48 1.17 tsutsui #include <sys/param.h> /* XXX: to pull <machine/macros.h> for vax memset(9) */
49 1.1 christos #include <lib/libkern/libkern.h>
50 1.1 christos
51 1.1 christos #else
52 1.1 christos
53 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
54 1.19 skrll __RCSID("$NetBSD: sha2.c,v 1.19 2009/08/21 09:40:51 skrll Exp $");
55 1.1 christos #endif /* LIBC_SCCS and not lint */
56 1.1 christos
57 1.1 christos #include "namespace.h"
58 1.1 christos #include <string.h>
59 1.1 christos
60 1.1 christos #endif
61 1.1 christos
62 1.1 christos #include <sys/types.h>
63 1.1 christos #include <sys/sha2.h>
64 1.14 joerg
65 1.14 joerg #if HAVE_NBTOOL_CONFIG_H
66 1.14 joerg # if HAVE_SYS_ENDIAN_H
67 1.14 joerg # include <sys/endian.h>
68 1.14 joerg # else
69 1.14 joerg # undef htobe32
70 1.14 joerg # undef htobe64
71 1.14 joerg # undef be32toh
72 1.14 joerg # undef be64toh
73 1.14 joerg
74 1.14 joerg static uint32_t
75 1.14 joerg htobe32(uint32_t x)
76 1.14 joerg {
77 1.14 joerg uint8_t p[4];
78 1.14 joerg memcpy(p, &x, 4);
79 1.14 joerg
80 1.14 joerg return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
81 1.14 joerg }
82 1.14 joerg
83 1.14 joerg static uint64_t
84 1.14 joerg htobe64(uint64_t x)
85 1.14 joerg {
86 1.14 joerg uint8_t p[8];
87 1.14 joerg uint32_t u, v;
88 1.14 joerg memcpy(p, &x, 8);
89 1.14 joerg
90 1.14 joerg u = ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
91 1.14 joerg v = ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]);
92 1.14 joerg
93 1.14 joerg return ((((uint64_t)u) << 32) | v);
94 1.14 joerg }
95 1.14 joerg
96 1.14 joerg static uint32_t
97 1.14 joerg be32toh(uint32_t x)
98 1.14 joerg {
99 1.14 joerg return htobe32(x);
100 1.14 joerg }
101 1.14 joerg
102 1.14 joerg static uint64_t
103 1.14 joerg be64toh(uint64_t x)
104 1.14 joerg {
105 1.14 joerg return htobe64(x);
106 1.14 joerg }
107 1.14 joerg # endif
108 1.14 joerg #endif
109 1.1 christos
110 1.1 christos /*** SHA-256/384/512 Various Length Definitions ***********************/
111 1.1 christos /* NOTE: Most of these are in sha2.h */
112 1.1 christos #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
113 1.1 christos #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
114 1.1 christos #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
115 1.1 christos
116 1.1 christos /*
117 1.1 christos * Macro for incrementally adding the unsigned 64-bit integer n to the
118 1.1 christos * unsigned 128-bit integer (represented using a two-element array of
119 1.1 christos * 64-bit words):
120 1.1 christos */
121 1.1 christos #define ADDINC128(w,n) { \
122 1.10 joerg (w)[0] += (uint64_t)(n); \
123 1.1 christos if ((w)[0] < (n)) { \
124 1.1 christos (w)[1]++; \
125 1.1 christos } \
126 1.1 christos }
127 1.1 christos
128 1.1 christos /*** THE SIX LOGICAL FUNCTIONS ****************************************/
129 1.1 christos /*
130 1.1 christos * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
131 1.1 christos *
132 1.1 christos * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
133 1.1 christos * S is a ROTATION) because the SHA-256/384/512 description document
134 1.1 christos * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
135 1.1 christos * same "backwards" definition.
136 1.1 christos */
137 1.1 christos /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
138 1.1 christos #define R(b,x) ((x) >> (b))
139 1.1 christos /* 32-bit Rotate-right (used in SHA-256): */
140 1.1 christos #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
141 1.1 christos /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
142 1.1 christos #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
143 1.1 christos
144 1.1 christos /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
145 1.1 christos #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
146 1.1 christos #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
147 1.1 christos
148 1.1 christos /* Four of six logical functions used in SHA-256: */
149 1.1 christos #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
150 1.1 christos #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
151 1.1 christos #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
152 1.1 christos #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
153 1.1 christos
154 1.1 christos /* Four of six logical functions used in SHA-384 and SHA-512: */
155 1.1 christos #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
156 1.1 christos #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
157 1.1 christos #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
158 1.1 christos #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
159 1.1 christos
160 1.1 christos /*** INTERNAL FUNCTION PROTOTYPES *************************************/
161 1.1 christos /* NOTE: These should not be accessed directly from outside this
162 1.1 christos * library -- they are intended for private internal visibility/use
163 1.1 christos * only.
164 1.1 christos */
165 1.12 christos static void SHA512_Last(SHA512_CTX *);
166 1.15 martin void SHA224_Transform(SHA224_CTX *, const uint32_t*);
167 1.12 christos void SHA256_Transform(SHA256_CTX *, const uint32_t*);
168 1.12 christos void SHA384_Transform(SHA384_CTX *, const uint64_t*);
169 1.12 christos void SHA512_Transform(SHA512_CTX *, const uint64_t*);
170 1.1 christos
171 1.1 christos
172 1.1 christos /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
173 1.1 christos /* Hash constant words K for SHA-256: */
174 1.10 joerg static const uint32_t K256[64] = {
175 1.1 christos 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
176 1.1 christos 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
177 1.1 christos 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
178 1.1 christos 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
179 1.1 christos 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
180 1.1 christos 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
181 1.1 christos 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
182 1.1 christos 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
183 1.1 christos 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
184 1.1 christos 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
185 1.1 christos 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
186 1.1 christos 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
187 1.1 christos 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
188 1.1 christos 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
189 1.1 christos 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
190 1.1 christos 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
191 1.1 christos };
192 1.1 christos
193 1.8 joerg /* Initial hash value H for SHA-224: */
194 1.10 joerg static const uint32_t sha224_initial_hash_value[8] = {
195 1.8 joerg 0xc1059ed8UL,
196 1.8 joerg 0x367cd507UL,
197 1.8 joerg 0x3070dd17UL,
198 1.8 joerg 0xf70e5939UL,
199 1.8 joerg 0xffc00b31UL,
200 1.8 joerg 0x68581511UL,
201 1.8 joerg 0x64f98fa7UL,
202 1.8 joerg 0xbefa4fa4UL
203 1.8 joerg };
204 1.8 joerg
205 1.1 christos /* Initial hash value H for SHA-256: */
206 1.10 joerg static const uint32_t sha256_initial_hash_value[8] = {
207 1.1 christos 0x6a09e667UL,
208 1.1 christos 0xbb67ae85UL,
209 1.1 christos 0x3c6ef372UL,
210 1.1 christos 0xa54ff53aUL,
211 1.1 christos 0x510e527fUL,
212 1.1 christos 0x9b05688cUL,
213 1.1 christos 0x1f83d9abUL,
214 1.1 christos 0x5be0cd19UL
215 1.1 christos };
216 1.1 christos
217 1.1 christos /* Hash constant words K for SHA-384 and SHA-512: */
218 1.10 joerg static const uint64_t K512[80] = {
219 1.1 christos 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
220 1.1 christos 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
221 1.1 christos 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
222 1.1 christos 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
223 1.1 christos 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
224 1.1 christos 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
225 1.1 christos 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
226 1.1 christos 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
227 1.1 christos 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
228 1.1 christos 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
229 1.1 christos 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
230 1.1 christos 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
231 1.1 christos 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
232 1.1 christos 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
233 1.1 christos 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
234 1.1 christos 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
235 1.1 christos 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
236 1.1 christos 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
237 1.1 christos 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
238 1.1 christos 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
239 1.1 christos 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
240 1.1 christos 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
241 1.1 christos 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
242 1.1 christos 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
243 1.1 christos 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
244 1.1 christos 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
245 1.1 christos 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
246 1.1 christos 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
247 1.1 christos 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
248 1.1 christos 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
249 1.1 christos 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
250 1.1 christos 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
251 1.1 christos 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
252 1.1 christos 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
253 1.1 christos 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
254 1.1 christos 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
255 1.1 christos 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
256 1.1 christos 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
257 1.1 christos 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
258 1.1 christos 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
259 1.1 christos };
260 1.1 christos
261 1.1 christos /* Initial hash value H for SHA-384 */
262 1.10 joerg static const uint64_t sha384_initial_hash_value[8] = {
263 1.1 christos 0xcbbb9d5dc1059ed8ULL,
264 1.1 christos 0x629a292a367cd507ULL,
265 1.1 christos 0x9159015a3070dd17ULL,
266 1.1 christos 0x152fecd8f70e5939ULL,
267 1.1 christos 0x67332667ffc00b31ULL,
268 1.1 christos 0x8eb44a8768581511ULL,
269 1.1 christos 0xdb0c2e0d64f98fa7ULL,
270 1.1 christos 0x47b5481dbefa4fa4ULL
271 1.1 christos };
272 1.1 christos
273 1.1 christos /* Initial hash value H for SHA-512 */
274 1.10 joerg static const uint64_t sha512_initial_hash_value[8] = {
275 1.1 christos 0x6a09e667f3bcc908ULL,
276 1.1 christos 0xbb67ae8584caa73bULL,
277 1.1 christos 0x3c6ef372fe94f82bULL,
278 1.1 christos 0xa54ff53a5f1d36f1ULL,
279 1.1 christos 0x510e527fade682d1ULL,
280 1.1 christos 0x9b05688c2b3e6c1fULL,
281 1.1 christos 0x1f83d9abfb41bd6bULL,
282 1.1 christos 0x5be0cd19137e2179ULL
283 1.1 christos };
284 1.1 christos
285 1.19 skrll #if !defined(_KERNEL) && !defined(_STANDALONE)
286 1.19 skrll #if defined(__weak_alias)
287 1.8 joerg __weak_alias(SHA224_Init,_SHA224_Init)
288 1.8 joerg __weak_alias(SHA224_Update,_SHA224_Update)
289 1.8 joerg __weak_alias(SHA224_Final,_SHA224_Final)
290 1.8 joerg __weak_alias(SHA224_Transform,_SHA224_Transform)
291 1.8 joerg
292 1.1 christos __weak_alias(SHA256_Init,_SHA256_Init)
293 1.1 christos __weak_alias(SHA256_Update,_SHA256_Update)
294 1.1 christos __weak_alias(SHA256_Final,_SHA256_Final)
295 1.1 christos __weak_alias(SHA256_Transform,_SHA256_Transform)
296 1.1 christos
297 1.1 christos __weak_alias(SHA384_Init,_SHA384_Init)
298 1.1 christos __weak_alias(SHA384_Update,_SHA384_Update)
299 1.1 christos __weak_alias(SHA384_Final,_SHA384_Final)
300 1.1 christos __weak_alias(SHA384_Transform,_SHA384_Transform)
301 1.1 christos
302 1.1 christos __weak_alias(SHA512_Init,_SHA512_Init)
303 1.1 christos __weak_alias(SHA512_Update,_SHA512_Update)
304 1.1 christos __weak_alias(SHA512_Final,_SHA512_Final)
305 1.1 christos __weak_alias(SHA512_Transform,_SHA512_Transform)
306 1.1 christos #endif
307 1.19 skrll #endif
308 1.1 christos
309 1.1 christos /*** SHA-256: *********************************************************/
310 1.11 joerg int
311 1.12 christos SHA256_Init(SHA256_CTX *context)
312 1.11 joerg {
313 1.12 christos if (context == NULL)
314 1.8 joerg return 1;
315 1.12 christos
316 1.12 christos memcpy(context->state, sha256_initial_hash_value,
317 1.12 christos (size_t)(SHA256_DIGEST_LENGTH));
318 1.1 christos memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH));
319 1.1 christos context->bitcount = 0;
320 1.8 joerg
321 1.8 joerg return 1;
322 1.8 joerg }
323 1.8 joerg
324 1.1 christos #ifdef SHA2_UNROLL_TRANSFORM
325 1.1 christos
326 1.1 christos /* Unrolled SHA-256 round macros: */
327 1.1 christos
328 1.1 christos #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
329 1.9 joerg W256[j] = be32toh(*data); \
330 1.9 joerg ++data; \
331 1.1 christos T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
332 1.1 christos K256[j] + W256[j]; \
333 1.1 christos (d) += T1; \
334 1.1 christos (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
335 1.1 christos j++
336 1.1 christos
337 1.1 christos #define ROUND256(a,b,c,d,e,f,g,h) \
338 1.1 christos s0 = W256[(j+1)&0x0f]; \
339 1.1 christos s0 = sigma0_256(s0); \
340 1.1 christos s1 = W256[(j+14)&0x0f]; \
341 1.1 christos s1 = sigma1_256(s1); \
342 1.1 christos T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
343 1.1 christos (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
344 1.1 christos (d) += T1; \
345 1.1 christos (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
346 1.1 christos j++
347 1.1 christos
348 1.11 joerg void
349 1.12 christos SHA256_Transform(SHA256_CTX *context, const uint32_t *data)
350 1.11 joerg {
351 1.10 joerg uint32_t a, b, c, d, e, f, g, h, s0, s1;
352 1.10 joerg uint32_t T1, *W256;
353 1.1 christos int j;
354 1.1 christos
355 1.10 joerg W256 = (uint32_t *)context->buffer;
356 1.1 christos
357 1.1 christos /* Initialize registers with the prev. intermediate value */
358 1.1 christos a = context->state[0];
359 1.1 christos b = context->state[1];
360 1.1 christos c = context->state[2];
361 1.1 christos d = context->state[3];
362 1.1 christos e = context->state[4];
363 1.1 christos f = context->state[5];
364 1.1 christos g = context->state[6];
365 1.1 christos h = context->state[7];
366 1.1 christos
367 1.1 christos j = 0;
368 1.1 christos do {
369 1.1 christos /* Rounds 0 to 15 (unrolled): */
370 1.1 christos ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
371 1.1 christos ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
372 1.1 christos ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
373 1.1 christos ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
374 1.1 christos ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
375 1.1 christos ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
376 1.1 christos ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
377 1.1 christos ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
378 1.1 christos } while (j < 16);
379 1.1 christos
380 1.1 christos /* Now for the remaining rounds to 64: */
381 1.1 christos do {
382 1.1 christos ROUND256(a,b,c,d,e,f,g,h);
383 1.1 christos ROUND256(h,a,b,c,d,e,f,g);
384 1.1 christos ROUND256(g,h,a,b,c,d,e,f);
385 1.1 christos ROUND256(f,g,h,a,b,c,d,e);
386 1.1 christos ROUND256(e,f,g,h,a,b,c,d);
387 1.1 christos ROUND256(d,e,f,g,h,a,b,c);
388 1.1 christos ROUND256(c,d,e,f,g,h,a,b);
389 1.1 christos ROUND256(b,c,d,e,f,g,h,a);
390 1.1 christos } while (j < 64);
391 1.1 christos
392 1.1 christos /* Compute the current intermediate hash value */
393 1.1 christos context->state[0] += a;
394 1.1 christos context->state[1] += b;
395 1.1 christos context->state[2] += c;
396 1.1 christos context->state[3] += d;
397 1.1 christos context->state[4] += e;
398 1.1 christos context->state[5] += f;
399 1.1 christos context->state[6] += g;
400 1.1 christos context->state[7] += h;
401 1.1 christos
402 1.1 christos /* Clean up */
403 1.1 christos a = b = c = d = e = f = g = h = T1 = 0;
404 1.1 christos }
405 1.1 christos
406 1.1 christos #else /* SHA2_UNROLL_TRANSFORM */
407 1.1 christos
408 1.11 joerg void
409 1.12 christos SHA256_Transform(SHA256_CTX *context, const uint32_t *data)
410 1.11 joerg {
411 1.10 joerg uint32_t a, b, c, d, e, f, g, h, s0, s1;
412 1.10 joerg uint32_t T1, T2, *W256;
413 1.1 christos int j;
414 1.1 christos
415 1.10 joerg W256 = (uint32_t *)(void *)context->buffer;
416 1.1 christos
417 1.1 christos /* Initialize registers with the prev. intermediate value */
418 1.1 christos a = context->state[0];
419 1.1 christos b = context->state[1];
420 1.1 christos c = context->state[2];
421 1.1 christos d = context->state[3];
422 1.1 christos e = context->state[4];
423 1.1 christos f = context->state[5];
424 1.1 christos g = context->state[6];
425 1.1 christos h = context->state[7];
426 1.1 christos
427 1.1 christos j = 0;
428 1.1 christos do {
429 1.9 joerg W256[j] = be32toh(*data);
430 1.9 joerg ++data;
431 1.1 christos /* Apply the SHA-256 compression function to update a..h */
432 1.1 christos T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
433 1.1 christos T2 = Sigma0_256(a) + Maj(a, b, c);
434 1.1 christos h = g;
435 1.1 christos g = f;
436 1.1 christos f = e;
437 1.1 christos e = d + T1;
438 1.1 christos d = c;
439 1.1 christos c = b;
440 1.1 christos b = a;
441 1.1 christos a = T1 + T2;
442 1.1 christos
443 1.1 christos j++;
444 1.1 christos } while (j < 16);
445 1.1 christos
446 1.1 christos do {
447 1.1 christos /* Part of the message block expansion: */
448 1.1 christos s0 = W256[(j+1)&0x0f];
449 1.1 christos s0 = sigma0_256(s0);
450 1.1 christos s1 = W256[(j+14)&0x0f];
451 1.1 christos s1 = sigma1_256(s1);
452 1.1 christos
453 1.1 christos /* Apply the SHA-256 compression function to update a..h */
454 1.1 christos T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
455 1.1 christos (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
456 1.1 christos T2 = Sigma0_256(a) + Maj(a, b, c);
457 1.1 christos h = g;
458 1.1 christos g = f;
459 1.1 christos f = e;
460 1.1 christos e = d + T1;
461 1.1 christos d = c;
462 1.1 christos c = b;
463 1.1 christos b = a;
464 1.1 christos a = T1 + T2;
465 1.1 christos
466 1.1 christos j++;
467 1.1 christos } while (j < 64);
468 1.1 christos
469 1.1 christos /* Compute the current intermediate hash value */
470 1.1 christos context->state[0] += a;
471 1.1 christos context->state[1] += b;
472 1.1 christos context->state[2] += c;
473 1.1 christos context->state[3] += d;
474 1.1 christos context->state[4] += e;
475 1.1 christos context->state[5] += f;
476 1.1 christos context->state[6] += g;
477 1.1 christos context->state[7] += h;
478 1.1 christos
479 1.1 christos /* Clean up */
480 1.1 christos a = b = c = d = e = f = g = h = T1 = T2 = 0;
481 1.1 christos }
482 1.1 christos
483 1.1 christos #endif /* SHA2_UNROLL_TRANSFORM */
484 1.1 christos
485 1.11 joerg int
486 1.12 christos SHA256_Update(SHA256_CTX *context, const uint8_t *data, size_t len)
487 1.11 joerg {
488 1.1 christos unsigned int freespace, usedspace;
489 1.1 christos
490 1.1 christos if (len == 0) {
491 1.1 christos /* Calling with no data is valid - we do nothing */
492 1.8 joerg return 1;
493 1.1 christos }
494 1.1 christos
495 1.1 christos usedspace = (unsigned int)((context->bitcount >> 3) %
496 1.1 christos SHA256_BLOCK_LENGTH);
497 1.1 christos if (usedspace > 0) {
498 1.1 christos /* Calculate how much free space is available in the buffer */
499 1.1 christos freespace = SHA256_BLOCK_LENGTH - usedspace;
500 1.1 christos
501 1.1 christos if (len >= freespace) {
502 1.1 christos /* Fill the buffer completely and process it */
503 1.12 christos memcpy(&context->buffer[usedspace], data,
504 1.12 christos (size_t)(freespace));
505 1.1 christos context->bitcount += freespace << 3;
506 1.1 christos len -= freespace;
507 1.1 christos data += freespace;
508 1.12 christos SHA256_Transform(context,
509 1.12 christos (uint32_t *)(void *)context->buffer);
510 1.1 christos } else {
511 1.1 christos /* The buffer is not yet full */
512 1.1 christos memcpy(&context->buffer[usedspace], data, len);
513 1.1 christos context->bitcount += len << 3;
514 1.1 christos /* Clean up: */
515 1.1 christos usedspace = freespace = 0;
516 1.8 joerg return 1;
517 1.1 christos }
518 1.1 christos }
519 1.5 joerg /*
520 1.5 joerg * Process as many complete blocks as possible.
521 1.5 joerg *
522 1.5 joerg * Check alignment of the data pointer. If it is 32bit aligned,
523 1.5 joerg * SHA256_Transform can be called directly on the data stream,
524 1.5 joerg * otherwise enforce the alignment by copy into the buffer.
525 1.5 joerg */
526 1.5 joerg if ((uintptr_t)data % 4 == 0) {
527 1.5 joerg while (len >= SHA256_BLOCK_LENGTH) {
528 1.5 joerg SHA256_Transform(context,
529 1.10 joerg (const uint32_t *)(const void *)data);
530 1.5 joerg context->bitcount += SHA256_BLOCK_LENGTH << 3;
531 1.5 joerg len -= SHA256_BLOCK_LENGTH;
532 1.5 joerg data += SHA256_BLOCK_LENGTH;
533 1.5 joerg }
534 1.5 joerg } else {
535 1.5 joerg while (len >= SHA256_BLOCK_LENGTH) {
536 1.5 joerg memcpy(context->buffer, data, SHA256_BLOCK_LENGTH);
537 1.5 joerg SHA256_Transform(context,
538 1.10 joerg (const uint32_t *)(const void *)context->buffer);
539 1.5 joerg context->bitcount += SHA256_BLOCK_LENGTH << 3;
540 1.5 joerg len -= SHA256_BLOCK_LENGTH;
541 1.5 joerg data += SHA256_BLOCK_LENGTH;
542 1.5 joerg }
543 1.1 christos }
544 1.1 christos if (len > 0) {
545 1.1 christos /* There's left-overs, so save 'em */
546 1.1 christos memcpy(context->buffer, data, len);
547 1.1 christos context->bitcount += len << 3;
548 1.1 christos }
549 1.1 christos /* Clean up: */
550 1.1 christos usedspace = freespace = 0;
551 1.8 joerg
552 1.8 joerg return 1;
553 1.1 christos }
554 1.1 christos
555 1.11 joerg static int
556 1.12 christos SHA224_256_Final(uint8_t digest[], SHA256_CTX *context, size_t len)
557 1.11 joerg {
558 1.10 joerg uint32_t *d = (void *)digest;
559 1.1 christos unsigned int usedspace;
560 1.9 joerg size_t i;
561 1.1 christos
562 1.1 christos /* If no digest buffer is passed, we don't bother doing this: */
563 1.10 joerg if (digest != NULL) {
564 1.12 christos usedspace = (unsigned int)((context->bitcount >> 3) %
565 1.12 christos SHA256_BLOCK_LENGTH);
566 1.9 joerg context->bitcount = htobe64(context->bitcount);
567 1.1 christos if (usedspace > 0) {
568 1.1 christos /* Begin padding with a 1 bit: */
569 1.1 christos context->buffer[usedspace++] = 0x80;
570 1.1 christos
571 1.1 christos if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
572 1.1 christos /* Set-up for the last transform: */
573 1.12 christos memset(&context->buffer[usedspace], 0,
574 1.12 christos (size_t)(SHA256_SHORT_BLOCK_LENGTH -
575 1.12 christos usedspace));
576 1.1 christos } else {
577 1.1 christos if (usedspace < SHA256_BLOCK_LENGTH) {
578 1.12 christos memset(&context->buffer[usedspace], 0,
579 1.12 christos (size_t)(SHA256_BLOCK_LENGTH -
580 1.12 christos usedspace));
581 1.1 christos }
582 1.1 christos /* Do second-to-last transform: */
583 1.12 christos SHA256_Transform(context,
584 1.12 christos (uint32_t *)(void *)context->buffer);
585 1.1 christos
586 1.1 christos /* And set-up for the last transform: */
587 1.12 christos memset(context->buffer, 0,
588 1.12 christos (size_t)(SHA256_SHORT_BLOCK_LENGTH));
589 1.1 christos }
590 1.1 christos } else {
591 1.1 christos /* Set-up for the last transform: */
592 1.12 christos memset(context->buffer, 0,
593 1.12 christos (size_t)(SHA256_SHORT_BLOCK_LENGTH));
594 1.1 christos
595 1.1 christos /* Begin padding with a 1 bit: */
596 1.1 christos *context->buffer = 0x80;
597 1.1 christos }
598 1.1 christos /* Set the bit count: */
599 1.13 christos memcpy(&context->buffer[SHA256_SHORT_BLOCK_LENGTH],
600 1.13 christos &context->bitcount, sizeof(context->bitcount));
601 1.1 christos
602 1.1 christos /* Final transform: */
603 1.10 joerg SHA256_Transform(context, (uint32_t *)(void *)context->buffer);
604 1.1 christos
605 1.9 joerg for (i = 0; i < len / 4; i++)
606 1.9 joerg d[i] = htobe32(context->state[i]);
607 1.1 christos }
608 1.1 christos
609 1.1 christos /* Clean up state data: */
610 1.1 christos memset(context, 0, sizeof(*context));
611 1.1 christos usedspace = 0;
612 1.8 joerg
613 1.8 joerg return 1;
614 1.8 joerg }
615 1.8 joerg
616 1.11 joerg int
617 1.12 christos SHA256_Final(uint8_t digest[], SHA256_CTX *context)
618 1.12 christos {
619 1.12 christos return SHA224_256_Final(digest, context, SHA256_DIGEST_LENGTH);
620 1.12 christos }
621 1.12 christos
622 1.12 christos /*** SHA-224: *********************************************************/
623 1.12 christos int
624 1.12 christos SHA224_Init(SHA224_CTX *context)
625 1.12 christos {
626 1.12 christos if (context == NULL)
627 1.12 christos return 1;
628 1.12 christos
629 1.18 joerg /* The state and buffer size are driven by SHA256, not by SHA224. */
630 1.12 christos memcpy(context->state, sha224_initial_hash_value,
631 1.18 joerg (size_t)(SHA256_DIGEST_LENGTH));
632 1.18 joerg memset(context->buffer, 0, (size_t)(SHA256_BLOCK_LENGTH));
633 1.12 christos context->bitcount = 0;
634 1.12 christos
635 1.12 christos return 1;
636 1.12 christos }
637 1.12 christos
638 1.12 christos int
639 1.12 christos SHA224_Update(SHA224_CTX *context, const uint8_t *data, size_t len)
640 1.12 christos {
641 1.12 christos return SHA256_Update((SHA256_CTX *)context, data, len);
642 1.12 christos }
643 1.12 christos
644 1.12 christos void
645 1.15 martin SHA224_Transform(SHA224_CTX *context, const uint32_t *data)
646 1.11 joerg {
647 1.15 martin SHA256_Transform((SHA256_CTX *)context, data);
648 1.8 joerg }
649 1.8 joerg
650 1.11 joerg int
651 1.12 christos SHA224_Final(uint8_t digest[], SHA224_CTX *context)
652 1.11 joerg {
653 1.12 christos return SHA224_256_Final(digest, (SHA256_CTX *)context,
654 1.12 christos SHA224_DIGEST_LENGTH);
655 1.1 christos }
656 1.1 christos
657 1.1 christos /*** SHA-512: *********************************************************/
658 1.11 joerg int
659 1.12 christos SHA512_Init(SHA512_CTX *context)
660 1.11 joerg {
661 1.10 joerg if (context == NULL)
662 1.8 joerg return 1;
663 1.10 joerg
664 1.12 christos memcpy(context->state, sha512_initial_hash_value,
665 1.12 christos (size_t)(SHA512_DIGEST_LENGTH));
666 1.1 christos memset(context->buffer, 0, (size_t)(SHA512_BLOCK_LENGTH));
667 1.1 christos context->bitcount[0] = context->bitcount[1] = 0;
668 1.8 joerg
669 1.8 joerg return 1;
670 1.1 christos }
671 1.1 christos
672 1.1 christos #ifdef SHA2_UNROLL_TRANSFORM
673 1.1 christos
674 1.1 christos /* Unrolled SHA-512 round macros: */
675 1.1 christos #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
676 1.9 joerg W512[j] = be64toh(*data); \
677 1.9 joerg ++data; \
678 1.1 christos T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
679 1.1 christos K512[j] + W512[j]; \
680 1.1 christos (d) += T1, \
681 1.1 christos (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
682 1.1 christos j++
683 1.1 christos
684 1.1 christos #define ROUND512(a,b,c,d,e,f,g,h) \
685 1.1 christos s0 = W512[(j+1)&0x0f]; \
686 1.1 christos s0 = sigma0_512(s0); \
687 1.1 christos s1 = W512[(j+14)&0x0f]; \
688 1.1 christos s1 = sigma1_512(s1); \
689 1.1 christos T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
690 1.1 christos (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
691 1.1 christos (d) += T1; \
692 1.1 christos (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
693 1.1 christos j++
694 1.1 christos
695 1.11 joerg void
696 1.12 christos SHA512_Transform(SHA512_CTX *context, const uint64_t *data)
697 1.11 joerg {
698 1.10 joerg uint64_t a, b, c, d, e, f, g, h, s0, s1;
699 1.10 joerg uint64_t T1, *W512 = (uint64_t *)context->buffer;
700 1.1 christos int j;
701 1.1 christos
702 1.1 christos /* Initialize registers with the prev. intermediate value */
703 1.1 christos a = context->state[0];
704 1.1 christos b = context->state[1];
705 1.1 christos c = context->state[2];
706 1.1 christos d = context->state[3];
707 1.1 christos e = context->state[4];
708 1.1 christos f = context->state[5];
709 1.1 christos g = context->state[6];
710 1.1 christos h = context->state[7];
711 1.1 christos
712 1.1 christos j = 0;
713 1.1 christos do {
714 1.1 christos ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
715 1.1 christos ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
716 1.1 christos ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
717 1.1 christos ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
718 1.1 christos ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
719 1.1 christos ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
720 1.1 christos ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
721 1.1 christos ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
722 1.1 christos } while (j < 16);
723 1.1 christos
724 1.1 christos /* Now for the remaining rounds up to 79: */
725 1.1 christos do {
726 1.1 christos ROUND512(a,b,c,d,e,f,g,h);
727 1.1 christos ROUND512(h,a,b,c,d,e,f,g);
728 1.1 christos ROUND512(g,h,a,b,c,d,e,f);
729 1.1 christos ROUND512(f,g,h,a,b,c,d,e);
730 1.1 christos ROUND512(e,f,g,h,a,b,c,d);
731 1.1 christos ROUND512(d,e,f,g,h,a,b,c);
732 1.1 christos ROUND512(c,d,e,f,g,h,a,b);
733 1.1 christos ROUND512(b,c,d,e,f,g,h,a);
734 1.1 christos } while (j < 80);
735 1.1 christos
736 1.1 christos /* Compute the current intermediate hash value */
737 1.1 christos context->state[0] += a;
738 1.1 christos context->state[1] += b;
739 1.1 christos context->state[2] += c;
740 1.1 christos context->state[3] += d;
741 1.1 christos context->state[4] += e;
742 1.1 christos context->state[5] += f;
743 1.1 christos context->state[6] += g;
744 1.1 christos context->state[7] += h;
745 1.1 christos
746 1.1 christos /* Clean up */
747 1.1 christos a = b = c = d = e = f = g = h = T1 = 0;
748 1.1 christos }
749 1.1 christos
750 1.1 christos #else /* SHA2_UNROLL_TRANSFORM */
751 1.1 christos
752 1.11 joerg void
753 1.12 christos SHA512_Transform(SHA512_CTX *context, const uint64_t *data)
754 1.11 joerg {
755 1.10 joerg uint64_t a, b, c, d, e, f, g, h, s0, s1;
756 1.10 joerg uint64_t T1, T2, *W512 = (void *)context->buffer;
757 1.1 christos int j;
758 1.1 christos
759 1.1 christos /* Initialize registers with the prev. intermediate value */
760 1.1 christos a = context->state[0];
761 1.1 christos b = context->state[1];
762 1.1 christos c = context->state[2];
763 1.1 christos d = context->state[3];
764 1.1 christos e = context->state[4];
765 1.1 christos f = context->state[5];
766 1.1 christos g = context->state[6];
767 1.1 christos h = context->state[7];
768 1.1 christos
769 1.1 christos j = 0;
770 1.1 christos do {
771 1.9 joerg W512[j] = be64toh(*data);
772 1.9 joerg ++data;
773 1.1 christos /* Apply the SHA-512 compression function to update a..h */
774 1.1 christos T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
775 1.1 christos T2 = Sigma0_512(a) + Maj(a, b, c);
776 1.1 christos h = g;
777 1.1 christos g = f;
778 1.1 christos f = e;
779 1.1 christos e = d + T1;
780 1.1 christos d = c;
781 1.1 christos c = b;
782 1.1 christos b = a;
783 1.1 christos a = T1 + T2;
784 1.1 christos
785 1.1 christos j++;
786 1.1 christos } while (j < 16);
787 1.1 christos
788 1.1 christos do {
789 1.1 christos /* Part of the message block expansion: */
790 1.1 christos s0 = W512[(j+1)&0x0f];
791 1.1 christos s0 = sigma0_512(s0);
792 1.1 christos s1 = W512[(j+14)&0x0f];
793 1.1 christos s1 = sigma1_512(s1);
794 1.1 christos
795 1.1 christos /* Apply the SHA-512 compression function to update a..h */
796 1.1 christos T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
797 1.1 christos (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
798 1.1 christos T2 = Sigma0_512(a) + Maj(a, b, c);
799 1.1 christos h = g;
800 1.1 christos g = f;
801 1.1 christos f = e;
802 1.1 christos e = d + T1;
803 1.1 christos d = c;
804 1.1 christos c = b;
805 1.1 christos b = a;
806 1.1 christos a = T1 + T2;
807 1.1 christos
808 1.1 christos j++;
809 1.1 christos } while (j < 80);
810 1.1 christos
811 1.1 christos /* Compute the current intermediate hash value */
812 1.1 christos context->state[0] += a;
813 1.1 christos context->state[1] += b;
814 1.1 christos context->state[2] += c;
815 1.1 christos context->state[3] += d;
816 1.1 christos context->state[4] += e;
817 1.1 christos context->state[5] += f;
818 1.1 christos context->state[6] += g;
819 1.1 christos context->state[7] += h;
820 1.1 christos
821 1.1 christos /* Clean up */
822 1.1 christos a = b = c = d = e = f = g = h = T1 = T2 = 0;
823 1.1 christos }
824 1.1 christos
825 1.1 christos #endif /* SHA2_UNROLL_TRANSFORM */
826 1.1 christos
827 1.11 joerg int
828 1.12 christos SHA512_Update(SHA512_CTX *context, const uint8_t *data, size_t len)
829 1.11 joerg {
830 1.1 christos unsigned int freespace, usedspace;
831 1.1 christos
832 1.1 christos if (len == 0) {
833 1.1 christos /* Calling with no data is valid - we do nothing */
834 1.8 joerg return 1;
835 1.1 christos }
836 1.1 christos
837 1.12 christos usedspace = (unsigned int)((context->bitcount[0] >> 3) %
838 1.12 christos SHA512_BLOCK_LENGTH);
839 1.1 christos if (usedspace > 0) {
840 1.1 christos /* Calculate how much free space is available in the buffer */
841 1.1 christos freespace = SHA512_BLOCK_LENGTH - usedspace;
842 1.1 christos
843 1.1 christos if (len >= freespace) {
844 1.1 christos /* Fill the buffer completely and process it */
845 1.12 christos memcpy(&context->buffer[usedspace], data,
846 1.12 christos (size_t)(freespace));
847 1.1 christos ADDINC128(context->bitcount, freespace << 3);
848 1.1 christos len -= freespace;
849 1.1 christos data += freespace;
850 1.12 christos SHA512_Transform(context,
851 1.12 christos (uint64_t *)(void *)context->buffer);
852 1.1 christos } else {
853 1.1 christos /* The buffer is not yet full */
854 1.1 christos memcpy(&context->buffer[usedspace], data, len);
855 1.1 christos ADDINC128(context->bitcount, len << 3);
856 1.1 christos /* Clean up: */
857 1.1 christos usedspace = freespace = 0;
858 1.8 joerg return 1;
859 1.1 christos }
860 1.1 christos }
861 1.5 joerg /*
862 1.5 joerg * Process as many complete blocks as possible.
863 1.5 joerg *
864 1.5 joerg * Check alignment of the data pointer. If it is 64bit aligned,
865 1.5 joerg * SHA512_Transform can be called directly on the data stream,
866 1.5 joerg * otherwise enforce the alignment by copy into the buffer.
867 1.5 joerg */
868 1.5 joerg if ((uintptr_t)data % 8 == 0) {
869 1.5 joerg while (len >= SHA512_BLOCK_LENGTH) {
870 1.5 joerg SHA512_Transform(context,
871 1.10 joerg (const uint64_t*)(const void *)data);
872 1.5 joerg ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
873 1.5 joerg len -= SHA512_BLOCK_LENGTH;
874 1.5 joerg data += SHA512_BLOCK_LENGTH;
875 1.5 joerg }
876 1.5 joerg } else {
877 1.5 joerg while (len >= SHA512_BLOCK_LENGTH) {
878 1.5 joerg memcpy(context->buffer, data, SHA512_BLOCK_LENGTH);
879 1.5 joerg SHA512_Transform(context,
880 1.6 drochner (const void *)context->buffer);
881 1.5 joerg ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
882 1.5 joerg len -= SHA512_BLOCK_LENGTH;
883 1.5 joerg data += SHA512_BLOCK_LENGTH;
884 1.5 joerg }
885 1.1 christos }
886 1.1 christos if (len > 0) {
887 1.1 christos /* There's left-overs, so save 'em */
888 1.1 christos memcpy(context->buffer, data, len);
889 1.1 christos ADDINC128(context->bitcount, len << 3);
890 1.1 christos }
891 1.1 christos /* Clean up: */
892 1.1 christos usedspace = freespace = 0;
893 1.8 joerg
894 1.8 joerg return 1;
895 1.1 christos }
896 1.1 christos
897 1.11 joerg static void
898 1.12 christos SHA512_Last(SHA512_CTX *context)
899 1.11 joerg {
900 1.1 christos unsigned int usedspace;
901 1.1 christos
902 1.1 christos usedspace = (unsigned int)((context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH);
903 1.9 joerg context->bitcount[0] = htobe64(context->bitcount[0]);
904 1.9 joerg context->bitcount[1] = htobe64(context->bitcount[1]);
905 1.1 christos if (usedspace > 0) {
906 1.1 christos /* Begin padding with a 1 bit: */
907 1.1 christos context->buffer[usedspace++] = 0x80;
908 1.1 christos
909 1.1 christos if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
910 1.1 christos /* Set-up for the last transform: */
911 1.12 christos memset(&context->buffer[usedspace], 0,
912 1.12 christos (size_t)(SHA512_SHORT_BLOCK_LENGTH - usedspace));
913 1.1 christos } else {
914 1.1 christos if (usedspace < SHA512_BLOCK_LENGTH) {
915 1.12 christos memset(&context->buffer[usedspace], 0,
916 1.12 christos (size_t)(SHA512_BLOCK_LENGTH - usedspace));
917 1.1 christos }
918 1.1 christos /* Do second-to-last transform: */
919 1.12 christos SHA512_Transform(context,
920 1.12 christos (uint64_t *)(void *)context->buffer);
921 1.1 christos
922 1.1 christos /* And set-up for the last transform: */
923 1.12 christos memset(context->buffer, 0,
924 1.12 christos (size_t)(SHA512_BLOCK_LENGTH - 2));
925 1.1 christos }
926 1.1 christos } else {
927 1.1 christos /* Prepare for final transform: */
928 1.1 christos memset(context->buffer, 0, (size_t)(SHA512_SHORT_BLOCK_LENGTH));
929 1.1 christos
930 1.1 christos /* Begin padding with a 1 bit: */
931 1.1 christos *context->buffer = 0x80;
932 1.1 christos }
933 1.1 christos /* Store the length of input data (in bits): */
934 1.13 christos memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH],
935 1.13 christos &context->bitcount[1], sizeof(context->bitcount[1]));
936 1.13 christos memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8],
937 1.13 christos &context->bitcount[0], sizeof(context->bitcount[0]));
938 1.1 christos
939 1.1 christos /* Final transform: */
940 1.10 joerg SHA512_Transform(context, (uint64_t *)(void *)context->buffer);
941 1.1 christos }
942 1.1 christos
943 1.11 joerg int
944 1.12 christos SHA512_Final(uint8_t digest[], SHA512_CTX *context)
945 1.11 joerg {
946 1.10 joerg uint64_t *d = (void *)digest;
947 1.9 joerg size_t i;
948 1.1 christos
949 1.1 christos /* If no digest buffer is passed, we don't bother doing this: */
950 1.10 joerg if (digest != NULL) {
951 1.1 christos SHA512_Last(context);
952 1.1 christos
953 1.1 christos /* Save the hash data for output: */
954 1.9 joerg for (i = 0; i < 8; ++i)
955 1.9 joerg d[i] = htobe64(context->state[i]);
956 1.1 christos }
957 1.1 christos
958 1.1 christos /* Zero out state data */
959 1.1 christos memset(context, 0, sizeof(*context));
960 1.8 joerg
961 1.8 joerg return 1;
962 1.1 christos }
963 1.1 christos
964 1.1 christos /*** SHA-384: *********************************************************/
965 1.11 joerg int
966 1.12 christos SHA384_Init(SHA384_CTX *context)
967 1.11 joerg {
968 1.12 christos if (context == NULL)
969 1.8 joerg return 1;
970 1.12 christos
971 1.12 christos memcpy(context->state, sha384_initial_hash_value,
972 1.12 christos (size_t)(SHA512_DIGEST_LENGTH));
973 1.1 christos memset(context->buffer, 0, (size_t)(SHA384_BLOCK_LENGTH));
974 1.1 christos context->bitcount[0] = context->bitcount[1] = 0;
975 1.8 joerg
976 1.8 joerg return 1;
977 1.1 christos }
978 1.1 christos
979 1.11 joerg int
980 1.12 christos SHA384_Update(SHA384_CTX *context, const uint8_t *data, size_t len)
981 1.11 joerg {
982 1.12 christos return SHA512_Update((SHA512_CTX *)context, data, len);
983 1.1 christos }
984 1.1 christos
985 1.11 joerg void
986 1.12 christos SHA384_Transform(SHA512_CTX *context, const uint64_t *data)
987 1.11 joerg {
988 1.12 christos SHA512_Transform((SHA512_CTX *)context, data);
989 1.1 christos }
990 1.1 christos
991 1.11 joerg int
992 1.12 christos SHA384_Final(uint8_t digest[], SHA384_CTX *context)
993 1.11 joerg {
994 1.10 joerg uint64_t *d = (void *)digest;
995 1.9 joerg size_t i;
996 1.1 christos
997 1.1 christos /* If no digest buffer is passed, we don't bother doing this: */
998 1.10 joerg if (digest != NULL) {
999 1.12 christos SHA512_Last((SHA512_CTX *)context);
1000 1.1 christos
1001 1.1 christos /* Save the hash data for output: */
1002 1.9 joerg for (i = 0; i < 6; ++i)
1003 1.9 joerg d[i] = be64toh(context->state[i]);
1004 1.1 christos }
1005 1.1 christos
1006 1.1 christos /* Zero out state data */
1007 1.1 christos memset(context, 0, sizeof(*context));
1008 1.8 joerg
1009 1.8 joerg return 1;
1010 1.1 christos }
1011