Debian Codegraph @

cipher/sha256.c

raw
1 /* sha256.c - SHA256 hash function
2 * Copyright (C) 2003, 2006, 2008, 2009 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 /* Test vectors:
22
23 "abc"
24 SHA224: 23097d22 3405d822 8642a477 bda255b3 2aadbce4 bda0b3f7 e36c9da7
25 SHA256: ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad
26
27 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
28 SHA224: 75388b16 512776cc 5dba5da1 fd890150 b0c6455c b4f58b19 52522525
29 SHA256: 248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1
30
31 "a" one million times
32 SHA224: 20794655 980c91d8 bbb4c1ea 97618a4b f03f4258 1948b2ee 4ee7ad67
33 SHA256: cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0
34
35 */
36
37
38 #include <config.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "g10lib.h"
44 #include "bithelp.h"
45 #include "bufhelp.h"
46 #include "cipher.h"
47 #include "hash-common.h"
48
49
50 /* USE_SSSE3 indicates whether to compile with Intel SSSE3 code. */
51 #undef USE_SSSE3
52 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_SSSE3) && \
53 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
54 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
55 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
56 # define USE_SSSE3 1
57 #endif
58
59 /* USE_AVX indicates whether to compile with Intel AVX code. */
60 #undef USE_AVX
61 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \
62 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
63 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
64 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
65 # define USE_AVX 1
66 #endif
67
68 /* USE_AVX2 indicates whether to compile with Intel AVX2/BMI2 code. */
69 #undef USE_AVX2
70 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX2) && \
71 defined(HAVE_GCC_INLINE_ASM_BMI2) && \
72 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
73 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
74 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
75 # define USE_AVX2 1
76 #endif
77
78 /* USE_SHAEXT indicates whether to compile with Intel SHA Extension code. */
79 #undef USE_SHAEXT
80 #if defined(HAVE_GCC_INLINE_ASM_SHAEXT) && \
81 defined(HAVE_GCC_INLINE_ASM_SSE41) && \
82 defined(ENABLE_SHAEXT_SUPPORT)
83 # define USE_SHAEXT 1
84 #endif
85
86 /* USE_ARM_CE indicates whether to enable ARMv8 Crypto Extension assembly
87 * code. */
88 #undef USE_ARM_CE
89 #ifdef ENABLE_ARM_CRYPTO_SUPPORT
90 # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
91 && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
92 && defined(HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO)
93 # define USE_ARM_CE 1
94 # elif defined(__AARCH64EL__) \
95 && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \
96 && defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO)
97 # define USE_ARM_CE 1
98 # endif
99 #endif
100
101 /* USE_PPC_CRYPTO indicates whether to enable PowerPC vector crypto
102 * accelerated code. */
103 #undef USE_PPC_CRYPTO
104 #ifdef ENABLE_PPC_CRYPTO_SUPPORT
105 # if defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
106 defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC)
107 # if __GNUC__ >= 4
108 # define USE_PPC_CRYPTO 1
109 # endif
110 # endif
111 #endif
112
113 /* USE_RISCV_V_CRYPTO indicates whether to enable RISC-V vector cryptography
114 * extension code. */
115 #undef USE_RISCV_V_CRYPTO
116 #if defined (__riscv) && \
117 defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS) && \
118 defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_CRYPTO_INTRINSICS)
119 # define USE_RISCV_V_CRYPTO 1
120 #endif
121
122 /* USE_S390X_CRYPTO indicates whether to enable zSeries code. */
123 #undef USE_S390X_CRYPTO
124 #if defined(HAVE_GCC_INLINE_ASM_S390X)
125 # define USE_S390X_CRYPTO 1
126 #endif /* USE_S390X_CRYPTO */
127
128
129 typedef struct {
130 gcry_md_block_ctx_t bctx;
131 u32 h[8];
132 #ifdef USE_S390X_CRYPTO
133 u32 final_len_msb, final_len_lsb; /* needs to be right after h[7]. */
134 int use_s390x_crypto;
135 #endif
136 } SHA256_CONTEXT;
137
138
139 /* Assembly implementations use SystemV ABI, ABI conversion and additional
140 * stack to store XMM6-XMM15 needed on Win64. */
141 #undef ASM_FUNC_ABI
142 #undef ASM_EXTRA_STACK
143 #if defined(USE_SSSE3) || defined(USE_AVX) || defined(USE_AVX2) || \
144 defined(USE_SHAEXT)
145 # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
146 # define ASM_FUNC_ABI __attribute__((sysv_abi))
147 # define ASM_EXTRA_STACK (10 * 16 + sizeof(void *) * 4)
148 # else
149 # define ASM_FUNC_ABI
150 # define ASM_EXTRA_STACK 0
151 # endif
152 #endif
153
154
155 #ifdef USE_SSSE3
156 unsigned int _gcry_sha256_transform_amd64_ssse3(const void *input_data,
157 u32 state[8],
158 size_t num_blks) ASM_FUNC_ABI;
159
160 static unsigned int
161 do_sha256_transform_amd64_ssse3(void *ctx, const unsigned char *data,
162 size_t nblks)
163 {
164 SHA256_CONTEXT *hd = ctx;
165 return _gcry_sha256_transform_amd64_ssse3 (data, hd->h, nblks)
166 + ASM_EXTRA_STACK;
167 }
168 #endif
169
170 #ifdef USE_AVX
171 unsigned int _gcry_sha256_transform_amd64_avx(const void *input_data,
172 u32 state[8],
173 size_t num_blks) ASM_FUNC_ABI;
174
175 static unsigned int
176 do_sha256_transform_amd64_avx(void *ctx, const unsigned char *data,
177 size_t nblks)
178 {
179 SHA256_CONTEXT *hd = ctx;
180 return _gcry_sha256_transform_amd64_avx (data, hd->h, nblks)
181 + ASM_EXTRA_STACK;
182 }
183 #endif
184
185 #ifdef USE_AVX2
186 unsigned int _gcry_sha256_transform_amd64_avx2(const void *input_data,
187 u32 state[8],
188 size_t num_blks) ASM_FUNC_ABI;
189
190 static unsigned int
191 do_sha256_transform_amd64_avx2(void *ctx, const unsigned char *data,
192 size_t nblks)
193 {
194 SHA256_CONTEXT *hd = ctx;
195 return _gcry_sha256_transform_amd64_avx2 (data, hd->h, nblks)
196 + ASM_EXTRA_STACK;
197 }
198 #endif
199
200 #ifdef USE_SHAEXT
201 /* Does not need ASM_FUNC_ABI */
202 unsigned int
203 _gcry_sha256_transform_intel_shaext(u32 state[8],
204 const unsigned char *input_data,
205 size_t num_blks);
206
207 static unsigned int
208 do_sha256_transform_intel_shaext(void *ctx, const unsigned char *data,
209 size_t nblks)
210 {
211 SHA256_CONTEXT *hd = ctx;
212 return _gcry_sha256_transform_intel_shaext (hd->h, data, nblks);
213 }
214 #endif
215
216 #ifdef USE_ARM_CE
217 unsigned int _gcry_sha256_transform_armv8_ce(u32 state[8],
218 const void *input_data,
219 size_t num_blks);
220
221 static unsigned int
222 do_sha256_transform_armv8_ce(void *ctx, const unsigned char *data,
223 size_t nblks)
224 {
225 SHA256_CONTEXT *hd = ctx;
226 return _gcry_sha256_transform_armv8_ce (hd->h, data, nblks);
227 }
228 #endif
229
230 #ifdef USE_PPC_CRYPTO
231 unsigned int _gcry_sha256_transform_ppc8(u32 state[8],
232 const unsigned char *input_data,
233 size_t num_blks);
234
235 unsigned int _gcry_sha256_transform_ppc9(u32 state[8],
236 const unsigned char *input_data,
237 size_t num_blks);
238
239 static unsigned int
240 do_sha256_transform_ppc8(void *ctx, const unsigned char *data, size_t nblks)
241 {
242 SHA256_CONTEXT *hd = ctx;
243 return _gcry_sha256_transform_ppc8 (hd->h, data, nblks);
244 }
245
246 static unsigned int
247 do_sha256_transform_ppc9(void *ctx, const unsigned char *data, size_t nblks)
248 {
249 SHA256_CONTEXT *hd = ctx;
250 return _gcry_sha256_transform_ppc9 (hd->h, data, nblks);
251 }
252 #endif
253
254 #ifdef USE_RISCV_V_CRYPTO
255 unsigned int _gcry_sha256_riscv_v_check_hw(void);
256
257 unsigned int
258 _gcry_sha256_transform_riscv_zvknha_zvkb(u32 state[8],
259 const unsigned char *input_data,
260 size_t num_blks);
261
262 static unsigned int
263 do_sha256_transform_riscv_zvknha(void *ctx, const unsigned char *data,
264 size_t nblks)
265 {
266 SHA256_CONTEXT *hd = ctx;
267 return _gcry_sha256_transform_riscv_zvknha_zvkb (hd->h, data, nblks);
268 }
269 #endif
270
271 #ifdef USE_S390X_CRYPTO
272 #include "asm-inline-s390x.h"
273
274 static unsigned int
275 do_sha256_transform_s390x (void *ctx, const unsigned char *data, size_t nblks)
276 {
277 SHA256_CONTEXT *hd = ctx;
278
279 kimd_execute (KMID_FUNCTION_SHA256, hd->h, data, nblks * 64);
280 return 0;
281 }
282
283 static unsigned int
284 do_sha256_final_s390x (void *ctx, const unsigned char *data, size_t datalen,
285 u32 len_msb, u32 len_lsb)
286 {
287 SHA256_CONTEXT *hd = ctx;
288
289 /* Make sure that 'final_len' is positioned at correct offset relative
290 * to 'h[0]'. This is because we are passing 'h[0]' pointer as start of
291 * parameter block to 'klmd' instruction. */
292
293 gcry_assert (offsetof (SHA256_CONTEXT, final_len_msb)
294 - offsetof (SHA256_CONTEXT, h[0]) == 8 * sizeof(u32));
295 gcry_assert (offsetof (SHA256_CONTEXT, final_len_lsb)
296 - offsetof (SHA256_CONTEXT, final_len_msb) == 1 * sizeof(u32));
297
298 hd->final_len_msb = len_msb;
299 hd->final_len_lsb = len_lsb;
300
301 klmd_execute (KMID_FUNCTION_SHA256, hd->h, data, datalen);
302 return 0;
303 }
304 #endif
305
306
307 static unsigned int
308 do_transform_generic (void *ctx, const unsigned char *data, size_t nblks);
309
310
311 static void
312 sha256_common_init (SHA256_CONTEXT *hd)
313 {
314 unsigned int features = _gcry_get_hw_features ();
315
316 hd->bctx.nblocks = 0;
317 hd->bctx.nblocks_high = 0;
318 hd->bctx.count = 0;
319 hd->bctx.blocksize_shift = _gcry_ctz(64);
320
321 /* Order of feature checks is important here; last match will be
322 * selected. Keep slower implementations at the top and faster at
323 * the bottom. */
324 hd->bctx.bwrite = do_transform_generic;
325 #ifdef USE_SSSE3
326 if ((features & HWF_INTEL_SSSE3) != 0)
327 hd->bctx.bwrite = do_sha256_transform_amd64_ssse3;
328 #endif
329 #ifdef USE_AVX
330 /* AVX implementation uses SHLD which is known to be slow on non-Intel CPUs.
331 * Therefore use this implementation on Intel CPUs only. */
332 if ((features & HWF_INTEL_AVX) && (features & HWF_INTEL_FAST_SHLD))
333 hd->bctx.bwrite = do_sha256_transform_amd64_avx;
334 #endif
335 #ifdef USE_AVX2
336 if ((features & HWF_INTEL_AVX2) && (features & HWF_INTEL_BMI2))
337 hd->bctx.bwrite = do_sha256_transform_amd64_avx2;
338 #endif
339 #ifdef USE_SHAEXT
340 if ((features & HWF_INTEL_SHAEXT) && (features & HWF_INTEL_SSE4_1))
341 hd->bctx.bwrite = do_sha256_transform_intel_shaext;
342 #endif
343 #ifdef USE_ARM_CE
344 if ((features & HWF_ARM_SHA2) != 0)
345 hd->bctx.bwrite = do_sha256_transform_armv8_ce;
346 #endif
347 #ifdef USE_PPC_CRYPTO
348 if ((features & HWF_PPC_VCRYPTO) != 0)
349 hd->bctx.bwrite = do_sha256_transform_ppc8;
350 if ((features & HWF_PPC_VCRYPTO) != 0 && (features & HWF_PPC_ARCH_3_00) != 0)
351 hd->bctx.bwrite = do_sha256_transform_ppc9;
352 #endif
353 #ifdef USE_RISCV_V_CRYPTO
354 if ((features & HWF_RISCV_IMAFDC)
355 && (features & HWF_RISCV_B) /* Mandatory in RVA23U64 */
356 && (features & HWF_RISCV_V) /* Mandatory in RVA23U64 */
357 && (features & HWF_RISCV_ZVKB) /* Mandatory in RVA23U64 (Zvbb) */
358 && ((features & HWF_RISCV_ZVKNHA) /* Optional in RVA23U64 (Zvkng) */
359 || (features & HWF_RISCV_ZVKNHB)) /* Optional in RVA23U64 (Zvkng) */
360 && _gcry_sha256_riscv_v_check_hw())
361 hd->bctx.bwrite = do_sha256_transform_riscv_zvknha;
362 #endif
363 #ifdef USE_S390X_CRYPTO
364 hd->use_s390x_crypto = 0;
365 if ((features & HWF_S390X_MSA) != 0)
366 {
367 if ((kimd_query () & km_function_to_mask (KMID_FUNCTION_SHA256)) &&
368 (klmd_query () & km_function_to_mask (KMID_FUNCTION_SHA256)))
369 {
370 hd->bctx.bwrite = do_sha256_transform_s390x;
371 hd->use_s390x_crypto = 1;
372 }
373 }
374 #endif
375 (void)features;
376 }
377
378
379 static void
380 sha256_init (void *context, unsigned int flags)
381 {
382 SHA256_CONTEXT *hd = context;
383
384 (void)flags;
385
386 hd->h[0] = 0x6a09e667;
387 hd->h[1] = 0xbb67ae85;
388 hd->h[2] = 0x3c6ef372;
389 hd->h[3] = 0xa54ff53a;
390 hd->h[4] = 0x510e527f;
391 hd->h[5] = 0x9b05688c;
392 hd->h[6] = 0x1f83d9ab;
393 hd->h[7] = 0x5be0cd19;
394
395 sha256_common_init (hd);
396 }
397
398
399 static void
400 sha224_init (void *context, unsigned int flags)
401 {
402 SHA256_CONTEXT *hd = context;
403
404 (void)flags;
405
406 hd->h[0] = 0xc1059ed8;
407 hd->h[1] = 0x367cd507;
408 hd->h[2] = 0x3070dd17;
409 hd->h[3] = 0xf70e5939;
410 hd->h[4] = 0xffc00b31;
411 hd->h[5] = 0x68581511;
412 hd->h[6] = 0x64f98fa7;
413 hd->h[7] = 0xbefa4fa4;
414
415 sha256_common_init (hd);
416 }
417
418
419 /*
420 Transform the message X which consists of 16 32-bit-words. See FIPS
421 180-2 for details. */
422 #define R(a,b,c,d,e,f,g,h,k,w) do \
423 { \
424 t1 = (h) + Sum1((e)) + Cho((e),(f),(g)) + (k) + (w); \
425 t2 = Sum0((a)) + Maj((a),(b),(c)); \
426 d += t1; \
427 h = t1 + t2; \
428 } while (0)
429
430 /* (4.2) same as SHA-1's F1. */
431 #define Cho(x, y, z) (z ^ (x & (y ^ z)))
432
433 /* (4.3) same as SHA-1's F3 */
434 #define Maj(x, y, z) ((x & y) + (z & (x ^ y)))
435
436 /* (4.4) */
437 #define Sum0(x) (ror (x, 2) ^ ror (x, 13) ^ ror (x, 22))
438
439 /* (4.5) */
440 #define Sum1(x) (ror (x, 6) ^ ror (x, 11) ^ ror (x, 25))
441
442 /* Message expansion */
443 #define S0(x) (ror ((x), 7) ^ ror ((x), 18) ^ ((x) >> 3)) /* (4.6) */
444 #define S1(x) (ror ((x), 17) ^ ror ((x), 19) ^ ((x) >> 10)) /* (4.7) */
445 #define I(i) ( w[i] = buf_get_be32(data + i * 4) )
446 #define W(i) ( w[i&0x0f] = S1(w[(i-2) &0x0f]) \
447 + w[(i-7) &0x0f] \
448 + S0(w[(i-15)&0x0f]) \
449 + w[(i-16)&0x0f] )
450
451 static unsigned int
452 do_transform_generic (void *ctx, const unsigned char *data, size_t nblks)
453 {
454 SHA256_CONTEXT *hd = ctx;
455 static const u32 K[64] = {
456 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
457 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
458 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
459 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
460 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
461 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
462 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
463 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
464 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
465 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
466 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
467 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
468 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
469 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
470 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
471 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
472 };
473
474 do
475 {
476
477 u32 a,b,c,d,e,f,g,h,t1,t2;
478 u32 w[16];
479
480 a = hd->h[0];
481 b = hd->h[1];
482 c = hd->h[2];
483 d = hd->h[3];
484 e = hd->h[4];
485 f = hd->h[5];
486 g = hd->h[6];
487 h = hd->h[7];
488
489 R(a, b, c, d, e, f, g, h, K[0], I(0));
490 R(h, a, b, c, d, e, f, g, K[1], I(1));
491 R(g, h, a, b, c, d, e, f, K[2], I(2));
492 R(f, g, h, a, b, c, d, e, K[3], I(3));
493 R(e, f, g, h, a, b, c, d, K[4], I(4));
494 R(d, e, f, g, h, a, b, c, K[5], I(5));
495 R(c, d, e, f, g, h, a, b, K[6], I(6));
496 R(b, c, d, e, f, g, h, a, K[7], I(7));
497 R(a, b, c, d, e, f, g, h, K[8], I(8));
498 R(h, a, b, c, d, e, f, g, K[9], I(9));
499 R(g, h, a, b, c, d, e, f, K[10], I(10));
500 R(f, g, h, a, b, c, d, e, K[11], I(11));
501 R(e, f, g, h, a, b, c, d, K[12], I(12));
502 R(d, e, f, g, h, a, b, c, K[13], I(13));
503 R(c, d, e, f, g, h, a, b, K[14], I(14));
504 R(b, c, d, e, f, g, h, a, K[15], I(15));
505
506 R(a, b, c, d, e, f, g, h, K[16], W(16));
507 R(h, a, b, c, d, e, f, g, K[17], W(17));
508 R(g, h, a, b, c, d, e, f, K[18], W(18));
509 R(f, g, h, a, b, c, d, e, K[19], W(19));
510 R(e, f, g, h, a, b, c, d, K[20], W(20));
511 R(d, e, f, g, h, a, b, c, K[21], W(21));
512 R(c, d, e, f, g, h, a, b, K[22], W(22));
513 R(b, c, d, e, f, g, h, a, K[23], W(23));
514 R(a, b, c, d, e, f, g, h, K[24], W(24));
515 R(h, a, b, c, d, e, f, g, K[25], W(25));
516 R(g, h, a, b, c, d, e, f, K[26], W(26));
517 R(f, g, h, a, b, c, d, e, K[27], W(27));
518 R(e, f, g, h, a, b, c, d, K[28], W(28));
519 R(d, e, f, g, h, a, b, c, K[29], W(29));
520 R(c, d, e, f, g, h, a, b, K[30], W(30));
521 R(b, c, d, e, f, g, h, a, K[31], W(31));
522
523 R(a, b, c, d, e, f, g, h, K[32], W(32));
524 R(h, a, b, c, d, e, f, g, K[33], W(33));
525 R(g, h, a, b, c, d, e, f, K[34], W(34));
526 R(f, g, h, a, b, c, d, e, K[35], W(35));
527 R(e, f, g, h, a, b, c, d, K[36], W(36));
528 R(d, e, f, g, h, a, b, c, K[37], W(37));
529 R(c, d, e, f, g, h, a, b, K[38], W(38));
530 R(b, c, d, e, f, g, h, a, K[39], W(39));
531 R(a, b, c, d, e, f, g, h, K[40], W(40));
532 R(h, a, b, c, d, e, f, g, K[41], W(41));
533 R(g, h, a, b, c, d, e, f, K[42], W(42));
534 R(f, g, h, a, b, c, d, e, K[43], W(43));
535 R(e, f, g, h, a, b, c, d, K[44], W(44));
536 R(d, e, f, g, h, a, b, c, K[45], W(45));
537 R(c, d, e, f, g, h, a, b, K[46], W(46));
538 R(b, c, d, e, f, g, h, a, K[47], W(47));
539
540 R(a, b, c, d, e, f, g, h, K[48], W(48));
541 R(h, a, b, c, d, e, f, g, K[49], W(49));
542 R(g, h, a, b, c, d, e, f, K[50], W(50));
543 R(f, g, h, a, b, c, d, e, K[51], W(51));
544 R(e, f, g, h, a, b, c, d, K[52], W(52));
545 R(d, e, f, g, h, a, b, c, K[53], W(53));
546 R(c, d, e, f, g, h, a, b, K[54], W(54));
547 R(b, c, d, e, f, g, h, a, K[55], W(55));
548 R(a, b, c, d, e, f, g, h, K[56], W(56));
549 R(h, a, b, c, d, e, f, g, K[57], W(57));
550 R(g, h, a, b, c, d, e, f, K[58], W(58));
551 R(f, g, h, a, b, c, d, e, K[59], W(59));
552 R(e, f, g, h, a, b, c, d, K[60], W(60));
553 R(d, e, f, g, h, a, b, c, K[61], W(61));
554 R(c, d, e, f, g, h, a, b, K[62], W(62));
555 R(b, c, d, e, f, g, h, a, K[63], W(63));
556
557 hd->h[0] += a;
558 hd->h[1] += b;
559 hd->h[2] += c;
560 hd->h[3] += d;
561 hd->h[4] += e;
562 hd->h[5] += f;
563 hd->h[6] += g;
564 hd->h[7] += h;
565
566 data += 64;
567 }
568 while (--nblks);
569
570 return 26*4 + 32 + 3 * sizeof(void*);
571 }
572
573 #undef S0
574 #undef S1
575 #undef R
576
577
578 /*
579 The routine finally terminates the computation and returns the
580 digest. The handle is prepared for a new cycle, but adding bytes
581 to the handle will the destroy the returned buffer. Returns: 32
582 bytes with the message the digest. */
583 static void
584 sha256_final(void *context)
585 {
586 SHA256_CONTEXT *hd = context;
587 u32 t, th, msb, lsb;
588 byte *p;
589 unsigned int burn;
590
591 t = hd->bctx.nblocks;
592 if (sizeof t == sizeof hd->bctx.nblocks)
593 th = hd->bctx.nblocks_high;
594 else
595 th = hd->bctx.nblocks >> 32;
596
597 /* multiply by 64 to make a byte count */
598 lsb = t << 6;
599 msb = (th << 6) | (t >> 26);
600 /* add the count */
601 t = lsb;
602 if ((lsb += hd->bctx.count) < t)
603 msb++;
604 /* multiply by 8 to make a bit count */
605 t = lsb;
606 lsb <<= 3;
607 msb <<= 3;
608 msb |= t >> 29;
609
610 if (0)
611 { }
612 #ifdef USE_S390X_CRYPTO
613 else if (hd->use_s390x_crypto)
614 {
615 burn = do_sha256_final_s390x (hd, hd->bctx.buf, hd->bctx.count, msb, lsb);
616 }
617 #endif
618 else if (hd->bctx.count < 56) /* enough room */
619 {
620 hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad */
621 if (hd->bctx.count < 56)
622 memset (&hd->bctx.buf[hd->bctx.count], 0, 56 - hd->bctx.count);
623
624 /* append the 64 bit count */
625 buf_put_be32(hd->bctx.buf + 56, msb);
626 buf_put_be32(hd->bctx.buf + 60, lsb);
627 burn = (*hd->bctx.bwrite) (hd, hd->bctx.buf, 1);
628 }
629 else /* need one extra block */
630 {
631 hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad character */
632 /* fill pad and next block with zeroes */
633 memset (&hd->bctx.buf[hd->bctx.count], 0, 64 - hd->bctx.count + 56);
634
635 /* append the 64 bit count */
636 buf_put_be32(hd->bctx.buf + 64 + 56, msb);
637 buf_put_be32(hd->bctx.buf + 64 + 60, lsb);
638 burn = (*hd->bctx.bwrite) (hd, hd->bctx.buf, 2);
639 }
640
641 p = hd->bctx.buf;
642 #define X(a) do { buf_put_be32(p, hd->h[a]); p += 4; } while(0)
643 X(0);
644 X(1);
645 X(2);
646 X(3);
647 X(4);
648 X(5);
649 X(6);
650 X(7);
651 #undef X
652
653 hd->bctx.count = 0;
654
655 _gcry_burn_stack (burn);
656 }
657
658 static byte *
659 sha256_read (void *context)
660 {
661 SHA256_CONTEXT *hd = context;
662
663 return hd->bctx.buf;
664 }
665
666
667 /* Shortcut functions which puts the hash value of the supplied buffer iov
668 * into outbuf which must have a size of 32 bytes. */
669 static void
670 _gcry_sha256_hash_buffers (void *outbuf, size_t nbytes,
671 const gcry_buffer_t *iov, int iovcnt)
672 {
673 SHA256_CONTEXT hd;
674
675 (void)nbytes;
676
677 sha256_init (&hd, 0);
678 for (;iovcnt > 0; iov++, iovcnt--)
679 _gcry_md_block_write (&hd,
680 (const char*)iov[0].data + iov[0].off, iov[0].len);
681 sha256_final (&hd);
682 memcpy (outbuf, hd.bctx.buf, 32);
683 }
684
685
686 /* Shortcut functions which puts the hash value of the supplied buffer iov
687 * into outbuf which must have a size of 28 bytes. */
688 static void
689 _gcry_sha224_hash_buffers (void *outbuf, size_t nbytes,
690 const gcry_buffer_t *iov, int iovcnt)
691 {
692 SHA256_CONTEXT hd;
693
694 (void)nbytes;
695
696 sha224_init (&hd, 0);
697 for (;iovcnt > 0; iov++, iovcnt--)
698 _gcry_md_block_write (&hd,
699 (const char*)iov[0].data + iov[0].off, iov[0].len);
700 sha256_final (&hd);
701 memcpy (outbuf, hd.bctx.buf, 28);
702 }
703
704
705
706 /*
707 Self-test section.
708 */
709
710
711 static gpg_err_code_t
712 selftests_sha224 (int extended, selftest_report_func_t report)
713 {
714 const char *what;
715 const char *errtxt;
716
717 what = "short string";
718 errtxt = _gcry_hash_selftest_check_one
719 (GCRY_MD_SHA224, 0,
720 "abc", 3,
721 "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55\xb3"
722 "\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7", 28);
723 if (errtxt)
724 goto failed;
725
726 if (extended)
727 {
728 what = "long string";
729 errtxt = _gcry_hash_selftest_check_one
730 (GCRY_MD_SHA224, 0,
731 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
732 "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01\x50"
733 "\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25", 28);
734 if (errtxt)
735 goto failed;
736
737 what = "one million \"a\"";
738 errtxt = _gcry_hash_selftest_check_one
739 (GCRY_MD_SHA224, 1,
740 NULL, 0,
741 "\x20\x79\x46\x55\x98\x0c\x91\xd8\xbb\xb4\xc1\xea\x97\x61\x8a\x4b"
742 "\xf0\x3f\x42\x58\x19\x48\xb2\xee\x4e\xe7\xad\x67", 28);
743 if (errtxt)
744 goto failed;
745 }
746
747 return 0; /* Succeeded. */
748
749 failed:
750 if (report)
751 report ("digest", GCRY_MD_SHA224, what, errtxt);
752 return GPG_ERR_SELFTEST_FAILED;
753 }
754
755 static gpg_err_code_t
756 selftests_sha256 (int extended, selftest_report_func_t report)
757 {
758 const char *what;
759 const char *errtxt;
760
761 what = "short string";
762 errtxt = _gcry_hash_selftest_check_one
763 (GCRY_MD_SHA256, 0,
764 "abc", 3,
765 "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
766 "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad", 32);
767 if (errtxt)
768 goto failed;
769
770 if (extended)
771 {
772 what = "long string";
773 errtxt = _gcry_hash_selftest_check_one
774 (GCRY_MD_SHA256, 0,
775 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
776 "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
777 "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1",
778 32);
779 if (errtxt)
780 goto failed;
781
782 what = "one million \"a\"";
783 errtxt = _gcry_hash_selftest_check_one
784 (GCRY_MD_SHA256, 1,
785 NULL, 0,
786 "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67"
787 "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0",
788 32);
789 if (errtxt)
790 goto failed;
791 }
792
793 return 0; /* Succeeded. */
794
795 failed:
796 if (report)
797 report ("digest", GCRY_MD_SHA256, what, errtxt);
798 return GPG_ERR_SELFTEST_FAILED;
799 }
800
801
802 /* Run a full self-test for ALGO and return 0 on success. */
803 static gpg_err_code_t
804 run_selftests (int algo, int extended, selftest_report_func_t report)
805 {
806 gpg_err_code_t ec;
807
808 switch (algo)
809 {
810 case GCRY_MD_SHA224:
811 ec = selftests_sha224 (extended, report);
812 break;
813 case GCRY_MD_SHA256:
814 ec = selftests_sha256 (extended, report);
815 break;
816 default:
817 ec = GPG_ERR_DIGEST_ALGO;
818 break;
819
820 }
821 return ec;
822 }
823
824
825
826
827 static const byte asn224[19] = /* Object ID is 2.16.840.1.101.3.4.2.4 */
828 { 0x30, 0x2D, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
829 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04,
830 0x1C
831 };
832
833 static const gcry_md_oid_spec_t oid_spec_sha224[] =
834 {
835 /* From RFC3874, Section 4 */
836 { "2.16.840.1.101.3.4.2.4" },
837 /* ANSI X9.62 ecdsaWithSHA224 */
838 { "1.2.840.10045.4.3.1" },
839 { NULL },
840 };
841
842 static const byte asn256[19] = /* Object ID is 2.16.840.1.101.3.4.2.1 */
843 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
844 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
845 0x00, 0x04, 0x20 };
846
847 static const gcry_md_oid_spec_t oid_spec_sha256[] =
848 {
849 /* According to the OpenPGP draft rfc2440-bis06 */
850 { "2.16.840.1.101.3.4.2.1" },
851 /* PKCS#1 sha256WithRSAEncryption */
852 { "1.2.840.113549.1.1.11" },
853 /* ANSI X9.62 ecdsaWithSHA256 */
854 { "1.2.840.10045.4.3.2" },
855
856 { NULL },
857 };
858
859 const gcry_md_spec_t _gcry_digest_spec_sha224 =
860 {
861 GCRY_MD_SHA224, {0, 1},
862 "SHA224", asn224, DIM (asn224), oid_spec_sha224, 28,
863 sha224_init, _gcry_md_block_write, sha256_final, sha256_read, NULL,
864 _gcry_sha224_hash_buffers,
865 sizeof (SHA256_CONTEXT),
866 run_selftests
867 };
868
869 const gcry_md_spec_t _gcry_digest_spec_sha256 =
870 {
871 GCRY_MD_SHA256, {0, 1},
872 "SHA256", asn256, DIM (asn256), oid_spec_sha256, 32,
873 sha256_init, _gcry_md_block_write, sha256_final, sha256_read, NULL,
874 _gcry_sha256_hash_buffers,
875 sizeof (SHA256_CONTEXT),
876 run_selftests
877 };