Debian Codegraph @

cipher/sha512.c

raw
1 /* sha512.c - SHA384 and SHA512 hash functions
2 * Copyright (C) 2003, 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 from FIPS-180-2:
22 *
23 * "abc"
24 * 384:
25 * CB00753F 45A35E8B B5A03D69 9AC65007 272C32AB 0EDED163
26 * 1A8B605A 43FF5BED 8086072B A1E7CC23 58BAECA1 34C825A7
27 * 512:
28 * DDAF35A1 93617ABA CC417349 AE204131 12E6FA4E 89A97EA2 0A9EEEE6 4B55D39A
29 * 2192992A 274FC1A8 36BA3C23 A3FEEBBD 454D4423 643CE80E 2A9AC94F A54CA49F
30 *
31 * "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
32 * 384:
33 * 09330C33 F71147E8 3D192FC7 82CD1B47 53111B17 3B3B05D2
34 * 2FA08086 E3B0F712 FCC7C71A 557E2DB9 66C3E9FA 91746039
35 * 512:
36 * 8E959B75 DAE313DA 8CF4F728 14FC143F 8F7779C6 EB9F7FA1 7299AEAD B6889018
37 * 501D289E 4900F7E4 331B99DE C4B5433A C7D329EE B6DD2654 5E96E55B 874BE909
38 *
39 * "a" x 1000000
40 * 384:
41 * 9D0E1809 716474CB 086E834E 310A4A1C ED149E9C 00F24852
42 * 7972CEC5 704C2A5B 07B8B3DC 38ECC4EB AE97DDD8 7F3D8985
43 * 512:
44 * E718483D 0CE76964 4E2E42C7 BC15B463 8E1F98B1 3B204428 5632A803 AFA973EB
45 * DE0FF244 877EA60A 4CB0432C E577C31B EB009C5C 2C49AA2E 4EADB217 AD8CC09B
46 */
47
48
49 #include <config.h>
50 #include <string.h>
51 #include "g10lib.h"
52 #include "bithelp.h"
53 #include "bufhelp.h"
54 #include "cipher.h"
55 #include "hash-common.h"
56
57
58 /* Helper macro to force alignment to 64 bytes. */
59 #ifdef HAVE_GCC_ATTRIBUTE_ALIGNED
60 # define ATTR_ALIGNED_64 __attribute__ ((aligned (64)))
61 #else
62 # define ATTR_ALIGNED_64
63 #endif
64
65
66 /* USE_ARM_NEON_ASM indicates whether to enable ARM NEON assembly code. */
67 #undef USE_ARM_NEON_ASM
68 #ifdef ENABLE_NEON_SUPPORT
69 # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
70 && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
71 && defined(HAVE_GCC_INLINE_ASM_NEON)
72 # define USE_ARM_NEON_ASM 1
73 # endif
74 #endif /*ENABLE_NEON_SUPPORT*/
75
76
77 /* USE_ARM_ASM indicates whether to enable ARM assembly code. */
78 #undef USE_ARM_ASM
79 #if defined(__ARMEL__) && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS)
80 # define USE_ARM_ASM 1
81 #endif
82
83 /* USE_ARM64_SHA512 indicates whether to enable ARMv8 SHA512 extension assembly
84 * code. */
85 #undef USE_ARM64_SHA512
86 #ifdef ENABLE_ARM_CRYPTO_SUPPORT
87 # if defined(__AARCH64EL__) \
88 && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \
89 && defined(HAVE_GCC_INLINE_ASM_AARCH64_SHA3_SHA512_SM3_SM4)
90 # define USE_ARM64_SHA512 1
91 # endif
92 #endif
93
94
95 /* USE_SSSE3 indicates whether to compile with Intel SSSE3 code. */
96 #undef USE_SSSE3
97 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_SSSE3) && \
98 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
99 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
100 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
101 # define USE_SSSE3 1
102 #endif
103
104
105 /* USE_AVX indicates whether to compile with Intel AVX code. */
106 #undef USE_AVX
107 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \
108 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
109 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
110 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
111 # define USE_AVX 1
112 #endif
113
114
115 /* USE_AVX2 indicates whether to compile with Intel AVX2/rorx code. */
116 #undef USE_AVX2
117 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX2) && \
118 defined(HAVE_GCC_INLINE_ASM_BMI2) && \
119 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
120 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
121 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
122 # define USE_AVX2 1
123 #endif
124
125
126 /* USE_AVX512 indicates whether to compile with Intel AVX512 code. */
127 #undef USE_AVX512
128 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX512) && \
129 defined(HAVE_INTEL_SYNTAX_PLATFORM_AS) && \
130 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
131 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
132 # define USE_AVX512 1
133 #endif
134
135
136 /* USE_SSSE3_I386 indicates whether to compile with Intel SSSE3/i386 code. */
137 #undef USE_SSSE3_I386
138 #if defined(__i386__) && SIZEOF_UNSIGNED_LONG == 4 && __GNUC__ >= 4 && \
139 defined(HAVE_GCC_INLINE_ASM_SSSE3)
140 # define USE_SSSE3_I386 1
141 #endif
142
143
144 /* USE_PPC_CRYPTO indicates whether to enable PowerPC vector crypto
145 * accelerated code. */
146 #undef USE_PPC_CRYPTO
147 #ifdef ENABLE_PPC_CRYPTO_SUPPORT
148 # if defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
149 defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC)
150 # if __GNUC__ >= 4
151 # define USE_PPC_CRYPTO 1
152 # endif
153 # endif
154 #endif
155
156
157 /* USE_RISCV_V_CRYPTO indicates whether to enable RISC-V vector cryptography
158 * extension code. */
159 #undef USE_RISCV_V_CRYPTO
160 #if defined (__riscv) && \
161 defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS) && \
162 defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_CRYPTO_INTRINSICS)
163 # define USE_RISCV_V_CRYPTO 1
164 #endif
165
166
167 /* USE_S390X_CRYPTO indicates whether to enable zSeries code. */
168 #undef USE_S390X_CRYPTO
169 #if defined(HAVE_GCC_INLINE_ASM_S390X)
170 # define USE_S390X_CRYPTO 1
171 #endif /* USE_S390X_CRYPTO */
172
173
174 typedef struct
175 {
176 u64 h[8];
177 } SHA512_STATE;
178
179 typedef struct
180 {
181 gcry_md_block_ctx_t bctx;
182 SHA512_STATE state;
183 #ifdef USE_S390X_CRYPTO
184 u64 final_len_msb, final_len_lsb; /* needs to be right after state.h[7]. */
185 int use_s390x_crypto;
186 #endif
187 } SHA512_CONTEXT;
188
189
190 static ATTR_ALIGNED_64 const u64 k[] =
191 {
192 U64_C(0x428a2f98d728ae22), U64_C(0x7137449123ef65cd),
193 U64_C(0xb5c0fbcfec4d3b2f), U64_C(0xe9b5dba58189dbbc),
194 U64_C(0x3956c25bf348b538), U64_C(0x59f111f1b605d019),
195 U64_C(0x923f82a4af194f9b), U64_C(0xab1c5ed5da6d8118),
196 U64_C(0xd807aa98a3030242), U64_C(0x12835b0145706fbe),
197 U64_C(0x243185be4ee4b28c), U64_C(0x550c7dc3d5ffb4e2),
198 U64_C(0x72be5d74f27b896f), U64_C(0x80deb1fe3b1696b1),
199 U64_C(0x9bdc06a725c71235), U64_C(0xc19bf174cf692694),
200 U64_C(0xe49b69c19ef14ad2), U64_C(0xefbe4786384f25e3),
201 U64_C(0x0fc19dc68b8cd5b5), U64_C(0x240ca1cc77ac9c65),
202 U64_C(0x2de92c6f592b0275), U64_C(0x4a7484aa6ea6e483),
203 U64_C(0x5cb0a9dcbd41fbd4), U64_C(0x76f988da831153b5),
204 U64_C(0x983e5152ee66dfab), U64_C(0xa831c66d2db43210),
205 U64_C(0xb00327c898fb213f), U64_C(0xbf597fc7beef0ee4),
206 U64_C(0xc6e00bf33da88fc2), U64_C(0xd5a79147930aa725),
207 U64_C(0x06ca6351e003826f), U64_C(0x142929670a0e6e70),
208 U64_C(0x27b70a8546d22ffc), U64_C(0x2e1b21385c26c926),
209 U64_C(0x4d2c6dfc5ac42aed), U64_C(0x53380d139d95b3df),
210 U64_C(0x650a73548baf63de), U64_C(0x766a0abb3c77b2a8),
211 U64_C(0x81c2c92e47edaee6), U64_C(0x92722c851482353b),
212 U64_C(0xa2bfe8a14cf10364), U64_C(0xa81a664bbc423001),
213 U64_C(0xc24b8b70d0f89791), U64_C(0xc76c51a30654be30),
214 U64_C(0xd192e819d6ef5218), U64_C(0xd69906245565a910),
215 U64_C(0xf40e35855771202a), U64_C(0x106aa07032bbd1b8),
216 U64_C(0x19a4c116b8d2d0c8), U64_C(0x1e376c085141ab53),
217 U64_C(0x2748774cdf8eeb99), U64_C(0x34b0bcb5e19b48a8),
218 U64_C(0x391c0cb3c5c95a63), U64_C(0x4ed8aa4ae3418acb),
219 U64_C(0x5b9cca4f7763e373), U64_C(0x682e6ff3d6b2b8a3),
220 U64_C(0x748f82ee5defb2fc), U64_C(0x78a5636f43172f60),
221 U64_C(0x84c87814a1f0ab72), U64_C(0x8cc702081a6439ec),
222 U64_C(0x90befffa23631e28), U64_C(0xa4506cebde82bde9),
223 U64_C(0xbef9a3f7b2c67915), U64_C(0xc67178f2e372532b),
224 U64_C(0xca273eceea26619c), U64_C(0xd186b8c721c0c207),
225 U64_C(0xeada7dd6cde0eb1e), U64_C(0xf57d4f7fee6ed178),
226 U64_C(0x06f067aa72176fba), U64_C(0x0a637dc5a2c898a6),
227 U64_C(0x113f9804bef90dae), U64_C(0x1b710b35131c471b),
228 U64_C(0x28db77f523047d84), U64_C(0x32caab7b40c72493),
229 U64_C(0x3c9ebe0a15c9bebc), U64_C(0x431d67c49c100d4c),
230 U64_C(0x4cc5d4becb3e42b6), U64_C(0x597f299cfc657e2a),
231 U64_C(0x5fcb6fab3ad6faec), U64_C(0x6c44198c4a475817)
232 };
233
234
235 /* AMD64 assembly implementations use SystemV ABI, ABI conversion and additional
236 * stack to store XMM6-XMM15 needed on Win64. */
237 #undef ASM_FUNC_ABI
238 #undef ASM_EXTRA_STACK
239 #if defined(USE_SSSE3) || defined(USE_AVX) || defined(USE_AVX2) \
240 || defined(USE_AVX512)
241 # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
242 # define ASM_FUNC_ABI __attribute__((sysv_abi))
243 # define ASM_EXTRA_STACK (10 * 16 + 4 * sizeof(void *))
244 # else
245 # define ASM_FUNC_ABI
246 # define ASM_EXTRA_STACK 0
247 # endif
248 #endif
249
250
251 #ifdef USE_ARM64_SHA512
252 unsigned int _gcry_sha512_transform_armv8_ce (u64 state[8],
253 const unsigned char *data,
254 size_t num_blks,
255 const u64 k[]);
256
257 static unsigned int
258 do_sha512_transform_armv8_ce(void *ctx, const unsigned char *data,
259 size_t nblks)
260 {
261 SHA512_CONTEXT *hd = ctx;
262 return _gcry_sha512_transform_armv8_ce (hd->state.h, data, nblks, k);
263 }
264 #endif
265
266 #ifdef USE_ARM_NEON_ASM
267 unsigned int _gcry_sha512_transform_armv7_neon (SHA512_STATE *hd,
268 const unsigned char *data,
269 const u64 k[], size_t num_blks);
270
271 static unsigned int
272 do_sha512_transform_armv7_neon(void *ctx, const unsigned char *data,
273 size_t nblks)
274 {
275 SHA512_CONTEXT *hd = ctx;
276 return _gcry_sha512_transform_armv7_neon (&hd->state, data, k, nblks);
277 }
278 #endif
279
280 #ifdef USE_SSSE3
281 unsigned int _gcry_sha512_transform_amd64_ssse3(const void *input_data,
282 void *state,
283 size_t num_blks) ASM_FUNC_ABI;
284
285 static unsigned int
286 do_sha512_transform_amd64_ssse3(void *ctx, const unsigned char *data,
287 size_t nblks)
288 {
289 SHA512_CONTEXT *hd = ctx;
290 unsigned int burn;
291 burn = _gcry_sha512_transform_amd64_ssse3 (data, &hd->state, nblks);
292 burn += burn > 0 ? ASM_EXTRA_STACK : 0;
293 return burn;
294 }
295 #endif
296
297 #ifdef USE_AVX
298 unsigned int _gcry_sha512_transform_amd64_avx(const void *input_data,
299 void *state,
300 size_t num_blks) ASM_FUNC_ABI;
301
302 static unsigned int
303 do_sha512_transform_amd64_avx(void *ctx, const unsigned char *data,
304 size_t nblks)
305 {
306 SHA512_CONTEXT *hd = ctx;
307 unsigned int burn;
308 burn = _gcry_sha512_transform_amd64_avx (data, &hd->state, nblks);
309 burn += burn > 0 ? ASM_EXTRA_STACK : 0;
310 return burn;
311 }
312 #endif
313
314 #ifdef USE_AVX2
315 unsigned int _gcry_sha512_transform_amd64_avx2(const void *input_data,
316 void *state,
317 size_t num_blks) ASM_FUNC_ABI;
318
319 static unsigned int
320 do_sha512_transform_amd64_avx2(void *ctx, const unsigned char *data,
321 size_t nblks)
322 {
323 SHA512_CONTEXT *hd = ctx;
324 unsigned int burn;
325 burn = _gcry_sha512_transform_amd64_avx2 (data, &hd->state, nblks);
326 burn += burn > 0 ? ASM_EXTRA_STACK : 0;
327 return burn;
328 }
329 #endif
330
331 #ifdef USE_AVX512
332 unsigned int _gcry_sha512_transform_amd64_avx512(const void *input_data,
333 void *state,
334 size_t num_blks) ASM_FUNC_ABI;
335
336 static unsigned int
337 do_sha512_transform_amd64_avx512(void *ctx, const unsigned char *data,
338 size_t nblks)
339 {
340 SHA512_CONTEXT *hd = ctx;
341 unsigned int burn;
342 burn = _gcry_sha512_transform_amd64_avx512 (data, &hd->state, nblks);
343 burn += burn > 0 ? ASM_EXTRA_STACK : 0;
344 return burn;
345 }
346 #endif
347
348 #ifdef USE_SSSE3_I386
349 unsigned int _gcry_sha512_transform_i386_ssse3(u64 state[8],
350 const unsigned char *input_data,
351 size_t num_blks);
352
353 static unsigned int
354 do_sha512_transform_i386_ssse3(void *ctx, const unsigned char *data,
355 size_t nblks)
356 {
357 SHA512_CONTEXT *hd = ctx;
358 return _gcry_sha512_transform_i386_ssse3 (hd->state.h, data, nblks);
359 }
360 #endif
361
362
363 #ifdef USE_ARM_ASM
364 unsigned int _gcry_sha512_transform_arm (SHA512_STATE *hd,
365 const unsigned char *data,
366 const u64 k[], size_t num_blks);
367
368 static unsigned int
369 do_transform_generic (void *context, const unsigned char *data, size_t nblks)
370 {
371 SHA512_CONTEXT *hd = context;
372 return _gcry_sha512_transform_arm (&hd->state, data, k, nblks);
373 }
374 #else
375 static unsigned int
376 do_transform_generic (void *context, const unsigned char *data, size_t nblks);
377 #endif
378
379
380 #ifdef USE_PPC_CRYPTO
381 unsigned int _gcry_sha512_transform_ppc8(u64 state[8],
382 const unsigned char *input_data,
383 size_t num_blks);
384
385 unsigned int _gcry_sha512_transform_ppc9(u64 state[8],
386 const unsigned char *input_data,
387 size_t num_blks);
388
389 static unsigned int
390 do_sha512_transform_ppc8(void *ctx, const unsigned char *data, size_t nblks)
391 {
392 SHA512_CONTEXT *hd = ctx;
393 return _gcry_sha512_transform_ppc8 (hd->state.h, data, nblks);
394 }
395
396 static unsigned int
397 do_sha512_transform_ppc9(void *ctx, const unsigned char *data, size_t nblks)
398 {
399 SHA512_CONTEXT *hd = ctx;
400 return _gcry_sha512_transform_ppc9 (hd->state.h, data, nblks);
401 }
402 #endif
403
404
405 #ifdef USE_RISCV_V_CRYPTO
406 unsigned int _gcry_sha512_riscv_v_check_hw(void);
407
408 unsigned int
409 _gcry_sha512_transform_riscv_zvknhb_zvkb(u64 state[8],
410 const unsigned char *input_data,
411 size_t num_blks,
412 const u64 k[80]);
413
414 static unsigned int
415 do_sha512_transform_riscv_zvknhb(void *ctx, const unsigned char *data,
416 size_t nblks)
417 {
418 SHA512_CONTEXT *hd = ctx;
419 return _gcry_sha512_transform_riscv_zvknhb_zvkb (hd->state.h, data, nblks, k);
420 }
421 #endif
422
423
424 #ifdef USE_S390X_CRYPTO
425 #include "asm-inline-s390x.h"
426
427 static unsigned int
428 do_sha512_transform_s390x (void *ctx, const unsigned char *data, size_t nblks)
429 {
430 SHA512_CONTEXT *hd = ctx;
431
432 kimd_execute (KMID_FUNCTION_SHA512, hd->state.h, data, nblks * 128);
433 return 0;
434 }
435
436 static unsigned int
437 do_sha512_final_s390x (void *ctx, const unsigned char *data, size_t datalen,
438 u64 len_msb, u64 len_lsb)
439 {
440 SHA512_CONTEXT *hd = ctx;
441
442 /* Make sure that 'final_len' is positioned at correct offset relative
443 * to 'state.h[0]'. This is because we are passing 'state.h[0]' pointer as
444 * start of parameter block to 'klmd' instruction. */
445
446 gcry_assert (offsetof (SHA512_CONTEXT, final_len_msb)
447 - offsetof (SHA512_CONTEXT, state.h[0]) == 8 * sizeof(u64));
448 gcry_assert (offsetof (SHA512_CONTEXT, final_len_lsb)
449 - offsetof (SHA512_CONTEXT, final_len_msb) == 1 * sizeof(u64));
450
451 hd->final_len_msb = len_msb;
452 hd->final_len_lsb = len_lsb;
453
454 klmd_execute (KMID_FUNCTION_SHA512, hd->state.h, data, datalen);
455 return 0;
456 }
457 #endif
458
459
460 static void
461 sha512_init_common (SHA512_CONTEXT *ctx, unsigned int flags)
462 {
463 unsigned int features = _gcry_get_hw_features ();
464
465 (void)flags;
466 (void)k;
467
468 ctx->bctx.nblocks = 0;
469 ctx->bctx.nblocks_high = 0;
470 ctx->bctx.count = 0;
471 ctx->bctx.blocksize_shift = _gcry_ctz(128);
472
473 /* Order of feature checks is important here; last match will be
474 * selected. Keep slower implementations at the top and faster at
475 * the bottom. */
476 ctx->bctx.bwrite = do_transform_generic;
477 #ifdef USE_ARM_NEON_ASM
478 if ((features & HWF_ARM_NEON) != 0)
479 ctx->bctx.bwrite = do_sha512_transform_armv7_neon;
480 #endif
481 #ifdef USE_ARM64_SHA512
482 if ((features & HWF_ARM_NEON) && (features & HWF_ARM_SHA512))
483 ctx->bctx.bwrite = do_sha512_transform_armv8_ce;
484 #endif
485 #ifdef USE_SSSE3
486 if ((features & HWF_INTEL_SSSE3) != 0)
487 ctx->bctx.bwrite = do_sha512_transform_amd64_ssse3;
488 #endif
489 #ifdef USE_AVX
490 if ((features & HWF_INTEL_AVX) && (features & HWF_INTEL_FAST_SHLD))
491 ctx->bctx.bwrite = do_sha512_transform_amd64_avx;
492 #endif
493 #ifdef USE_AVX2
494 if ((features & HWF_INTEL_AVX2) && (features & HWF_INTEL_BMI2))
495 ctx->bctx.bwrite = do_sha512_transform_amd64_avx2;
496 #endif
497 #ifdef USE_AVX512
498 if ((features & HWF_INTEL_AVX512) && (features & HWF_INTEL_CPU))
499 ctx->bctx.bwrite = do_sha512_transform_amd64_avx512;
500 #endif
501 #ifdef USE_PPC_CRYPTO
502 if ((features & HWF_PPC_VCRYPTO) != 0)
503 ctx->bctx.bwrite = do_sha512_transform_ppc8;
504 if ((features & HWF_PPC_VCRYPTO) != 0 && (features & HWF_PPC_ARCH_3_00) != 0)
505 ctx->bctx.bwrite = do_sha512_transform_ppc9;
506 #endif
507 #ifdef USE_SSSE3_I386
508 if ((features & HWF_INTEL_SSSE3) != 0)
509 ctx->bctx.bwrite = do_sha512_transform_i386_ssse3;
510 #endif
511 #ifdef USE_RISCV_V_CRYPTO
512 if ((features & HWF_RISCV_IMAFDC)
513 && (features & HWF_RISCV_B) /* Mandatory in RVA23U64 */
514 && (features & HWF_RISCV_V) /* Mandatory in RVA23U64 */
515 && (features & HWF_RISCV_ZVKB) /* Mandatory in RVA23U64 (Zvbb) */
516 && (features & HWF_RISCV_ZVKNHB) /* Optional in RVA23U64 (Zvkng) */
517 && _gcry_sha512_riscv_v_check_hw())
518 ctx->bctx.bwrite = do_sha512_transform_riscv_zvknhb;
519 #endif
520 #ifdef USE_S390X_CRYPTO
521 ctx->use_s390x_crypto = 0;
522 if ((features & HWF_S390X_MSA) != 0)
523 {
524 if ((kimd_query () & km_function_to_mask (KMID_FUNCTION_SHA512)) &&
525 (klmd_query () & km_function_to_mask (KMID_FUNCTION_SHA512)))
526 {
527 ctx->bctx.bwrite = do_sha512_transform_s390x;
528 ctx->use_s390x_crypto = 1;
529 }
530 }
531 #endif
532 (void)features;
533 }
534
535
536 static void
537 sha512_init (void *context, unsigned int flags)
538 {
539 SHA512_CONTEXT *ctx = context;
540 SHA512_STATE *hd = &ctx->state;
541
542 hd->h[0] = U64_C(0x6a09e667f3bcc908);
543 hd->h[1] = U64_C(0xbb67ae8584caa73b);
544 hd->h[2] = U64_C(0x3c6ef372fe94f82b);
545 hd->h[3] = U64_C(0xa54ff53a5f1d36f1);
546 hd->h[4] = U64_C(0x510e527fade682d1);
547 hd->h[5] = U64_C(0x9b05688c2b3e6c1f);
548 hd->h[6] = U64_C(0x1f83d9abfb41bd6b);
549 hd->h[7] = U64_C(0x5be0cd19137e2179);
550
551 sha512_init_common (ctx, flags);
552 }
553
554 static void
555 sha384_init (void *context, unsigned int flags)
556 {
557 SHA512_CONTEXT *ctx = context;
558 SHA512_STATE *hd = &ctx->state;
559
560 hd->h[0] = U64_C(0xcbbb9d5dc1059ed8);
561 hd->h[1] = U64_C(0x629a292a367cd507);
562 hd->h[2] = U64_C(0x9159015a3070dd17);
563 hd->h[3] = U64_C(0x152fecd8f70e5939);
564 hd->h[4] = U64_C(0x67332667ffc00b31);
565 hd->h[5] = U64_C(0x8eb44a8768581511);
566 hd->h[6] = U64_C(0xdb0c2e0d64f98fa7);
567 hd->h[7] = U64_C(0x47b5481dbefa4fa4);
568
569 sha512_init_common (ctx, flags);
570 }
571
572
573 static void
574 sha512_256_init (void *context, unsigned int flags)
575 {
576 SHA512_CONTEXT *ctx = context;
577 SHA512_STATE *hd = &ctx->state;
578
579 hd->h[0] = U64_C(0x22312194fc2bf72c);
580 hd->h[1] = U64_C(0x9f555fa3c84c64c2);
581 hd->h[2] = U64_C(0x2393b86b6f53b151);
582 hd->h[3] = U64_C(0x963877195940eabd);
583 hd->h[4] = U64_C(0x96283ee2a88effe3);
584 hd->h[5] = U64_C(0xbe5e1e2553863992);
585 hd->h[6] = U64_C(0x2b0199fc2c85b8aa);
586 hd->h[7] = U64_C(0x0eb72ddc81c52ca2);
587
588 sha512_init_common (ctx, flags);
589 }
590
591
592 static void
593 sha512_224_init (void *context, unsigned int flags)
594 {
595 SHA512_CONTEXT *ctx = context;
596 SHA512_STATE *hd = &ctx->state;
597
598 hd->h[0] = U64_C(0x8c3d37c819544da2);
599 hd->h[1] = U64_C(0x73e1996689dcd4d6);
600 hd->h[2] = U64_C(0x1dfab7ae32ff9c82);
601 hd->h[3] = U64_C(0x679dd514582f9fcf);
602 hd->h[4] = U64_C(0x0f6d2b697bd44da8);
603 hd->h[5] = U64_C(0x77e36f7304c48942);
604 hd->h[6] = U64_C(0x3f9d85a86a1d36c8);
605 hd->h[7] = U64_C(0x1112e6ad91d692a1);
606
607 sha512_init_common (ctx, flags);
608 }
609
610
611
612 #ifndef USE_ARM_ASM
613
614 static inline u64
615 ROTR (u64 x, u64 n)
616 {
617 return ((x >> n) | (x << (64 - n)));
618 }
619
620 static inline u64
621 Ch (u64 x, u64 y, u64 z)
622 {
623 return ((x & y) ^ ( ~x & z));
624 }
625
626 static inline u64
627 Maj (u64 x, u64 y, u64 z)
628 {
629 return ((x & y) ^ (x & z) ^ (y & z));
630 }
631
632 static inline u64
633 Sum0 (u64 x)
634 {
635 return (ROTR (x, 28) ^ ROTR (x, 34) ^ ROTR (x, 39));
636 }
637
638 static inline u64
639 Sum1 (u64 x)
640 {
641 return (ROTR (x, 14) ^ ROTR (x, 18) ^ ROTR (x, 41));
642 }
643
644 /****************
645 * Transform the message W which consists of 16 64-bit-words
646 */
647 static unsigned int
648 do_transform_generic (void *context, const unsigned char *data, size_t nblks)
649 {
650 SHA512_CONTEXT *ctx = context;
651 SHA512_STATE *hd = &ctx->state;
652
653 do
654 {
655 u64 a, b, c, d, e, f, g, h;
656 u64 w[16];
657 int t;
658
659 /* get values from the chaining vars */
660 a = hd->h[0];
661 b = hd->h[1];
662 c = hd->h[2];
663 d = hd->h[3];
664 e = hd->h[4];
665 f = hd->h[5];
666 g = hd->h[6];
667 h = hd->h[7];
668
669 for ( t = 0; t < 16; t++ )
670 w[t] = buf_get_be64(data + t * 8);
671
672 #define S0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
673 #define S1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
674
675 for (t = 0; t < 80 - 16; )
676 {
677 u64 t1, t2;
678
679 t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[0];
680 w[0] += S1 (w[14]) + w[9] + S0 (w[1]);
681 t2 = Sum0 (a) + Maj (a, b, c);
682 d += t1;
683 h = t1 + t2;
684
685 t1 = g + Sum1 (d) + Ch (d, e, f) + k[t+1] + w[1];
686 w[1] += S1 (w[15]) + w[10] + S0 (w[2]);
687 t2 = Sum0 (h) + Maj (h, a, b);
688 c += t1;
689 g = t1 + t2;
690
691 t1 = f + Sum1 (c) + Ch (c, d, e) + k[t+2] + w[2];
692 w[2] += S1 (w[0]) + w[11] + S0 (w[3]);
693 t2 = Sum0 (g) + Maj (g, h, a);
694 b += t1;
695 f = t1 + t2;
696
697 t1 = e + Sum1 (b) + Ch (b, c, d) + k[t+3] + w[3];
698 w[3] += S1 (w[1]) + w[12] + S0 (w[4]);
699 t2 = Sum0 (f) + Maj (f, g, h);
700 a += t1;
701 e = t1 + t2;
702
703 t1 = d + Sum1 (a) + Ch (a, b, c) + k[t+4] + w[4];
704 w[4] += S1 (w[2]) + w[13] + S0 (w[5]);
705 t2 = Sum0 (e) + Maj (e, f, g);
706 h += t1;
707 d = t1 + t2;
708
709 t1 = c + Sum1 (h) + Ch (h, a, b) + k[t+5] + w[5];
710 w[5] += S1 (w[3]) + w[14] + S0 (w[6]);
711 t2 = Sum0 (d) + Maj (d, e, f);
712 g += t1;
713 c = t1 + t2;
714
715 t1 = b + Sum1 (g) + Ch (g, h, a) + k[t+6] + w[6];
716 w[6] += S1 (w[4]) + w[15] + S0 (w[7]);
717 t2 = Sum0 (c) + Maj (c, d, e);
718 f += t1;
719 b = t1 + t2;
720
721 t1 = a + Sum1 (f) + Ch (f, g, h) + k[t+7] + w[7];
722 w[7] += S1 (w[5]) + w[0] + S0 (w[8]);
723 t2 = Sum0 (b) + Maj (b, c, d);
724 e += t1;
725 a = t1 + t2;
726
727 t1 = h + Sum1 (e) + Ch (e, f, g) + k[t+8] + w[8];
728 w[8] += S1 (w[6]) + w[1] + S0 (w[9]);
729 t2 = Sum0 (a) + Maj (a, b, c);
730 d += t1;
731 h = t1 + t2;
732
733 t1 = g + Sum1 (d) + Ch (d, e, f) + k[t+9] + w[9];
734 w[9] += S1 (w[7]) + w[2] + S0 (w[10]);
735 t2 = Sum0 (h) + Maj (h, a, b);
736 c += t1;
737 g = t1 + t2;
738
739 t1 = f + Sum1 (c) + Ch (c, d, e) + k[t+10] + w[10];
740 w[10] += S1 (w[8]) + w[3] + S0 (w[11]);
741 t2 = Sum0 (g) + Maj (g, h, a);
742 b += t1;
743 f = t1 + t2;
744
745 t1 = e + Sum1 (b) + Ch (b, c, d) + k[t+11] + w[11];
746 w[11] += S1 (w[9]) + w[4] + S0 (w[12]);
747 t2 = Sum0 (f) + Maj (f, g, h);
748 a += t1;
749 e = t1 + t2;
750
751 t1 = d + Sum1 (a) + Ch (a, b, c) + k[t+12] + w[12];
752 w[12] += S1 (w[10]) + w[5] + S0 (w[13]);
753 t2 = Sum0 (e) + Maj (e, f, g);
754 h += t1;
755 d = t1 + t2;
756
757 t1 = c + Sum1 (h) + Ch (h, a, b) + k[t+13] + w[13];
758 w[13] += S1 (w[11]) + w[6] + S0 (w[14]);
759 t2 = Sum0 (d) + Maj (d, e, f);
760 g += t1;
761 c = t1 + t2;
762
763 t1 = b + Sum1 (g) + Ch (g, h, a) + k[t+14] + w[14];
764 w[14] += S1 (w[12]) + w[7] + S0 (w[15]);
765 t2 = Sum0 (c) + Maj (c, d, e);
766 f += t1;
767 b = t1 + t2;
768
769 t1 = a + Sum1 (f) + Ch (f, g, h) + k[t+15] + w[15];
770 w[15] += S1 (w[13]) + w[8] + S0 (w[0]);
771 t2 = Sum0 (b) + Maj (b, c, d);
772 e += t1;
773 a = t1 + t2;
774
775 t += 16;
776 }
777
778 for (; t < 80; )
779 {
780 u64 t1, t2;
781
782 t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[0];
783 t2 = Sum0 (a) + Maj (a, b, c);
784 d += t1;
785 h = t1 + t2;
786
787 t1 = g + Sum1 (d) + Ch (d, e, f) + k[t+1] + w[1];
788 t2 = Sum0 (h) + Maj (h, a, b);
789 c += t1;
790 g = t1 + t2;
791
792 t1 = f + Sum1 (c) + Ch (c, d, e) + k[t+2] + w[2];
793 t2 = Sum0 (g) + Maj (g, h, a);
794 b += t1;
795 f = t1 + t2;
796
797 t1 = e + Sum1 (b) + Ch (b, c, d) + k[t+3] + w[3];
798 t2 = Sum0 (f) + Maj (f, g, h);
799 a += t1;
800 e = t1 + t2;
801
802 t1 = d + Sum1 (a) + Ch (a, b, c) + k[t+4] + w[4];
803 t2 = Sum0 (e) + Maj (e, f, g);
804 h += t1;
805 d = t1 + t2;
806
807 t1 = c + Sum1 (h) + Ch (h, a, b) + k[t+5] + w[5];
808 t2 = Sum0 (d) + Maj (d, e, f);
809 g += t1;
810 c = t1 + t2;
811
812 t1 = b + Sum1 (g) + Ch (g, h, a) + k[t+6] + w[6];
813 t2 = Sum0 (c) + Maj (c, d, e);
814 f += t1;
815 b = t1 + t2;
816
817 t1 = a + Sum1 (f) + Ch (f, g, h) + k[t+7] + w[7];
818 t2 = Sum0 (b) + Maj (b, c, d);
819 e += t1;
820 a = t1 + t2;
821
822 t1 = h + Sum1 (e) + Ch (e, f, g) + k[t+8] + w[8];
823 t2 = Sum0 (a) + Maj (a, b, c);
824 d += t1;
825 h = t1 + t2;
826
827 t1 = g + Sum1 (d) + Ch (d, e, f) + k[t+9] + w[9];
828 t2 = Sum0 (h) + Maj (h, a, b);
829 c += t1;
830 g = t1 + t2;
831
832 t1 = f + Sum1 (c) + Ch (c, d, e) + k[t+10] + w[10];
833 t2 = Sum0 (g) + Maj (g, h, a);
834 b += t1;
835 f = t1 + t2;
836
837 t1 = e + Sum1 (b) + Ch (b, c, d) + k[t+11] + w[11];
838 t2 = Sum0 (f) + Maj (f, g, h);
839 a += t1;
840 e = t1 + t2;
841
842 t1 = d + Sum1 (a) + Ch (a, b, c) + k[t+12] + w[12];
843 t2 = Sum0 (e) + Maj (e, f, g);
844 h += t1;
845 d = t1 + t2;
846
847 t1 = c + Sum1 (h) + Ch (h, a, b) + k[t+13] + w[13];
848 t2 = Sum0 (d) + Maj (d, e, f);
849 g += t1;
850 c = t1 + t2;
851
852 t1 = b + Sum1 (g) + Ch (g, h, a) + k[t+14] + w[14];
853 t2 = Sum0 (c) + Maj (c, d, e);
854 f += t1;
855 b = t1 + t2;
856
857 t1 = a + Sum1 (f) + Ch (f, g, h) + k[t+15] + w[15];
858 t2 = Sum0 (b) + Maj (b, c, d);
859 e += t1;
860 a = t1 + t2;
861
862 t += 16;
863 }
864
865 /* Update chaining vars. */
866 hd->h[0] += a;
867 hd->h[1] += b;
868 hd->h[2] += c;
869 hd->h[3] += d;
870 hd->h[4] += e;
871 hd->h[5] += f;
872 hd->h[6] += g;
873 hd->h[7] += h;
874
875 data += 128;
876 }
877 while (--nblks);
878
879 return (8 + 16) * sizeof(u64) + sizeof(u32) + 3 * sizeof(void*);
880 }
881 #endif /*!USE_ARM_ASM*/
882
883
884 /* The routine final terminates the computation and
885 * returns the digest.
886 * The handle is prepared for a new cycle, but adding bytes to the
887 * handle will the destroy the returned buffer.
888 * Returns: 64 bytes representing the digest. When used for sha384,
889 * we take the leftmost 48 of those bytes.
890 */
891
892 static void
893 sha512_final (void *context)
894 {
895 SHA512_CONTEXT *hd = context;
896 unsigned int burn;
897 u64 t, th, msb, lsb;
898 byte *p;
899
900 t = hd->bctx.nblocks;
901 /* if (sizeof t == sizeof hd->bctx.nblocks) */
902 th = hd->bctx.nblocks_high;
903 /* else */
904 /* th = hd->bctx.nblocks >> 64; In case we ever use u128 */
905
906 /* multiply by 128 to make a byte count */
907 lsb = t << 7;
908 msb = (th << 7) | (t >> 57);
909 /* add the count */
910 t = lsb;
911 if ((lsb += hd->bctx.count) < t)
912 msb++;
913 /* multiply by 8 to make a bit count */
914 t = lsb;
915 lsb <<= 3;
916 msb <<= 3;
917 msb |= t >> 61;
918
919 if (0)
920 { }
921 #ifdef USE_S390X_CRYPTO
922 else if (hd->use_s390x_crypto)
923 {
924 burn = do_sha512_final_s390x (hd, hd->bctx.buf, hd->bctx.count, msb, lsb);
925 }
926 #endif
927 else
928 {
929 if (hd->bctx.count < 112)
930 {
931 /* enough room */
932 hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad */
933 if (hd->bctx.count < 112)
934 memset (&hd->bctx.buf[hd->bctx.count], 0, 112 - hd->bctx.count);
935 }
936 else
937 {
938 /* need one extra block */
939 hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad character */
940 if (hd->bctx.count < 128)
941 memset (&hd->bctx.buf[hd->bctx.count], 0, 128 - hd->bctx.count);
942 hd->bctx.count = 128;
943 _gcry_md_block_write (context, NULL, 0); /* flush */
944 memset (hd->bctx.buf, 0, 112); /* fill next block with zeroes */
945 }
946 /* append the 128 bit count */
947 buf_put_be64(hd->bctx.buf + 112, msb);
948 buf_put_be64(hd->bctx.buf + 120, lsb);
949 burn = (*hd->bctx.bwrite) (hd, hd->bctx.buf, 1);
950 }
951
952 p = hd->bctx.buf;
953 #define X(a) do { buf_put_be64(p, hd->state.h[a]); p += 8; } while (0)
954 X (0);
955 X (1);
956 X (2);
957 X (3);
958 X (4);
959 X (5);
960 /* Note that these last two chunks are included even for SHA384.
961 We just ignore them. */
962 X (6);
963 X (7);
964 #undef X
965
966 hd->bctx.count = 0;
967
968 _gcry_burn_stack (burn);
969 }
970
971 static byte *
972 sha512_read (void *context)
973 {
974 SHA512_CONTEXT *hd = (SHA512_CONTEXT *) context;
975 return hd->bctx.buf;
976 }
977
978
979 /* Shortcut functions which puts the hash value of the supplied buffer iov
980 * into outbuf which must have a size of 64 bytes. */
981 static void
982 _gcry_sha512_hash_buffers (void *outbuf, size_t nbytes,
983 const gcry_buffer_t *iov, int iovcnt)
984 {
985 SHA512_CONTEXT hd;
986
987 (void)nbytes;
988
989 sha512_init (&hd, 0);
990 for (;iovcnt > 0; iov++, iovcnt--)
991 _gcry_md_block_write (&hd,
992 (const char*)iov[0].data + iov[0].off, iov[0].len);
993 sha512_final (&hd);
994 memcpy (outbuf, hd.bctx.buf, 64);
995 }
996
997
998
999 /* Shortcut functions which puts the hash value of the supplied buffer iov
1000 * into outbuf which must have a size of 48 bytes. */
1001 static void
1002 _gcry_sha384_hash_buffers (void *outbuf, size_t nbytes,
1003 const gcry_buffer_t *iov, int iovcnt)
1004 {
1005 SHA512_CONTEXT hd;
1006
1007 (void)nbytes;
1008
1009 sha384_init (&hd, 0);
1010 for (;iovcnt > 0; iov++, iovcnt--)
1011 _gcry_md_block_write (&hd,
1012 (const char*)iov[0].data + iov[0].off, iov[0].len);
1013 sha512_final (&hd);
1014 memcpy (outbuf, hd.bctx.buf, 48);
1015 }
1016
1017
1018
1019 /* Shortcut functions which puts the hash value of the supplied buffer iov
1020 * into outbuf which must have a size of 32 bytes. */
1021 static void
1022 _gcry_sha512_256_hash_buffers (void *outbuf, size_t nbytes,
1023 const gcry_buffer_t *iov, int iovcnt)
1024 {
1025 SHA512_CONTEXT hd;
1026
1027 (void)nbytes;
1028
1029 sha512_256_init (&hd, 0);
1030 for (;iovcnt > 0; iov++, iovcnt--)
1031 _gcry_md_block_write (&hd,
1032 (const char*)iov[0].data + iov[0].off, iov[0].len);
1033 sha512_final (&hd);
1034 memcpy (outbuf, hd.bctx.buf, 32);
1035 }
1036
1037
1038
1039 /* Shortcut functions which puts the hash value of the supplied buffer iov
1040 * into outbuf which must have a size of 28 bytes. */
1041 static void
1042 _gcry_sha512_224_hash_buffers (void *outbuf, size_t nbytes,
1043 const gcry_buffer_t *iov, int iovcnt)
1044 {
1045 SHA512_CONTEXT hd;
1046
1047 (void)nbytes;
1048
1049 sha512_224_init (&hd, 0);
1050 for (;iovcnt > 0; iov++, iovcnt--)
1051 _gcry_md_block_write (&hd,
1052 (const char*)iov[0].data + iov[0].off, iov[0].len);
1053 sha512_final (&hd);
1054 memcpy (outbuf, hd.bctx.buf, 28);
1055 }
1056
1057
1058
1059 /*
1060 Self-test section.
1061 */
1062
1063
1064 static gpg_err_code_t
1065 selftests_sha384 (int extended, selftest_report_func_t report)
1066 {
1067 const char *what;
1068 const char *errtxt;
1069
1070 what = "short string";
1071 errtxt = _gcry_hash_selftest_check_one
1072 (GCRY_MD_SHA384, 0,
1073 "abc", 3,
1074 "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50\x07"
1075 "\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff\x5b\xed"
1076 "\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34\xc8\x25\xa7", 48);
1077 if (errtxt)
1078 goto failed;
1079
1080 if (extended)
1081 {
1082 what = "long string";
1083 errtxt = _gcry_hash_selftest_check_one
1084 (GCRY_MD_SHA384, 0,
1085 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
1086 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112,
1087 "\x09\x33\x0C\x33\xF7\x11\x47\xE8\x3D\x19\x2F\xC7\x82\xCD\x1B\x47"
1088 "\x53\x11\x1B\x17\x3B\x3B\x05\xD2\x2F\xA0\x80\x86\xE3\xB0\xF7\x12"
1089 "\xFC\xC7\xC7\x1A\x55\x7E\x2D\xB9\x66\xC3\xE9\xFA\x91\x74\x60\x39",
1090 48);
1091 if (errtxt)
1092 goto failed;
1093
1094 what = "one million \"a\"";
1095 errtxt = _gcry_hash_selftest_check_one
1096 (GCRY_MD_SHA384, 1,
1097 NULL, 0,
1098 "\x9D\x0E\x18\x09\x71\x64\x74\xCB\x08\x6E\x83\x4E\x31\x0A\x4A\x1C"
1099 "\xED\x14\x9E\x9C\x00\xF2\x48\x52\x79\x72\xCE\xC5\x70\x4C\x2A\x5B"
1100 "\x07\xB8\xB3\xDC\x38\xEC\xC4\xEB\xAE\x97\xDD\xD8\x7F\x3D\x89\x85",
1101 48);
1102 if (errtxt)
1103 goto failed;
1104 }
1105
1106 return 0; /* Succeeded. */
1107
1108 failed:
1109 if (report)
1110 report ("digest", GCRY_MD_SHA384, what, errtxt);
1111 return GPG_ERR_SELFTEST_FAILED;
1112 }
1113
1114 static gpg_err_code_t
1115 selftests_sha512 (int extended, selftest_report_func_t report)
1116 {
1117 const char *what;
1118 const char *errtxt;
1119
1120 what = "short string";
1121 errtxt = _gcry_hash_selftest_check_one
1122 (GCRY_MD_SHA512, 0,
1123 "abc", 3,
1124 "\xDD\xAF\x35\xA1\x93\x61\x7A\xBA\xCC\x41\x73\x49\xAE\x20\x41\x31"
1125 "\x12\xE6\xFA\x4E\x89\xA9\x7E\xA2\x0A\x9E\xEE\xE6\x4B\x55\xD3\x9A"
1126 "\x21\x92\x99\x2A\x27\x4F\xC1\xA8\x36\xBA\x3C\x23\xA3\xFE\xEB\xBD"
1127 "\x45\x4D\x44\x23\x64\x3C\xE8\x0E\x2A\x9A\xC9\x4F\xA5\x4C\xA4\x9F", 64);
1128 if (errtxt)
1129 goto failed;
1130
1131 if (extended)
1132 {
1133 what = "long string";
1134 errtxt = _gcry_hash_selftest_check_one
1135 (GCRY_MD_SHA512, 0,
1136 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
1137 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112,
1138 "\x8E\x95\x9B\x75\xDA\xE3\x13\xDA\x8C\xF4\xF7\x28\x14\xFC\x14\x3F"
1139 "\x8F\x77\x79\xC6\xEB\x9F\x7F\xA1\x72\x99\xAE\xAD\xB6\x88\x90\x18"
1140 "\x50\x1D\x28\x9E\x49\x00\xF7\xE4\x33\x1B\x99\xDE\xC4\xB5\x43\x3A"
1141 "\xC7\xD3\x29\xEE\xB6\xDD\x26\x54\x5E\x96\xE5\x5B\x87\x4B\xE9\x09",
1142 64);
1143 if (errtxt)
1144 goto failed;
1145
1146 what = "one million \"a\"";
1147 errtxt = _gcry_hash_selftest_check_one
1148 (GCRY_MD_SHA512, 1,
1149 NULL, 0,
1150 "\xE7\x18\x48\x3D\x0C\xE7\x69\x64\x4E\x2E\x42\xC7\xBC\x15\xB4\x63"
1151 "\x8E\x1F\x98\xB1\x3B\x20\x44\x28\x56\x32\xA8\x03\xAF\xA9\x73\xEB"
1152 "\xDE\x0F\xF2\x44\x87\x7E\xA6\x0A\x4C\xB0\x43\x2C\xE5\x77\xC3\x1B"
1153 "\xEB\x00\x9C\x5C\x2C\x49\xAA\x2E\x4E\xAD\xB2\x17\xAD\x8C\xC0\x9B",
1154 64);
1155 if (errtxt)
1156 goto failed;
1157 }
1158
1159 return 0; /* Succeeded. */
1160
1161 failed:
1162 if (report)
1163 report ("digest", GCRY_MD_SHA512, what, errtxt);
1164 return GPG_ERR_SELFTEST_FAILED;
1165 }
1166
1167 static gpg_err_code_t
1168 selftests_sha512_224 (int extended, selftest_report_func_t report)
1169 {
1170 const char *what;
1171 const char *errtxt;
1172
1173 what = "short string";
1174 errtxt = _gcry_hash_selftest_check_one
1175 (GCRY_MD_SHA512_224, 0,
1176 "abc", 3,
1177 "\x46\x34\x27\x0F\x70\x7B\x6A\x54\xDA\xAE\x75\x30\x46\x08\x42\xE2"
1178 "\x0E\x37\xED\x26\x5C\xEE\xE9\xA4\x3E\x89\x24\xAA",
1179 28);
1180 if (errtxt)
1181 goto failed;
1182
1183 if (extended)
1184 {
1185 what = "long string";
1186 errtxt = _gcry_hash_selftest_check_one
1187 (GCRY_MD_SHA512_224, 0,
1188 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
1189 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112,
1190 "\x23\xFE\xC5\xBB\x94\xD6\x0B\x23\x30\x81\x92\x64\x0B\x0C\x45\x33"
1191 "\x35\xD6\x64\x73\x4F\xE4\x0E\x72\x68\x67\x4A\xF9",
1192 28);
1193 if (errtxt)
1194 goto failed;
1195
1196 what = "one million \"a\"";
1197 errtxt = _gcry_hash_selftest_check_one
1198 (GCRY_MD_SHA512_224, 1,
1199 NULL, 0,
1200 "\x37\xab\x33\x1d\x76\xf0\xd3\x6d\xe4\x22\xbd\x0e\xde\xb2\x2a\x28"
1201 "\xac\xcd\x48\x7b\x7a\x84\x53\xae\x96\x5d\xd2\x87",
1202 28);
1203 if (errtxt)
1204 goto failed;
1205 }
1206
1207 return 0; /* Succeeded. */
1208
1209 failed:
1210 if (report)
1211 report ("digest", GCRY_MD_SHA512_224, what, errtxt);
1212 return GPG_ERR_SELFTEST_FAILED;
1213 }
1214
1215 static gpg_err_code_t
1216 selftests_sha512_256 (int extended, selftest_report_func_t report)
1217 {
1218 const char *what;
1219 const char *errtxt;
1220
1221 what = "short string";
1222 errtxt = _gcry_hash_selftest_check_one
1223 (GCRY_MD_SHA512_256, 0,
1224 "abc", 3,
1225 "\x53\x04\x8E\x26\x81\x94\x1E\xF9\x9B\x2E\x29\xB7\x6B\x4C\x7D\xAB"
1226 "\xE4\xC2\xD0\xC6\x34\xFC\x6D\x46\xE0\xE2\xF1\x31\x07\xE7\xAF\x23",
1227 32);
1228 if (errtxt)
1229 goto failed;
1230
1231 if (extended)
1232 {
1233 what = "long string";
1234 errtxt = _gcry_hash_selftest_check_one
1235 (GCRY_MD_SHA512_256, 0,
1236 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
1237 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112,
1238 "\x39\x28\xE1\x84\xFB\x86\x90\xF8\x40\xDA\x39\x88\x12\x1D\x31\xBE"
1239 "\x65\xCB\x9D\x3E\xF8\x3E\xE6\x14\x6F\xEA\xC8\x61\xE1\x9B\x56\x3A",
1240 32);
1241 if (errtxt)
1242 goto failed;
1243
1244 what = "one million \"a\"";
1245 errtxt = _gcry_hash_selftest_check_one
1246 (GCRY_MD_SHA512_256, 1,
1247 NULL, 0,
1248 "\x9a\x59\xa0\x52\x93\x01\x87\xa9\x70\x38\xca\xe6\x92\xf3\x07\x08"
1249 "\xaa\x64\x91\x92\x3e\xf5\x19\x43\x94\xdc\x68\xd5\x6c\x74\xfb\x21",
1250 32);
1251 if (errtxt)
1252 goto failed;
1253 }
1254
1255 return 0; /* Succeeded. */
1256
1257 failed:
1258 if (report)
1259 report ("digest", GCRY_MD_SHA512_256, what, errtxt);
1260 return GPG_ERR_SELFTEST_FAILED;
1261 }
1262
1263
1264 /* Run a full self-test for ALGO and return 0 on success. */
1265 static gpg_err_code_t
1266 run_selftests (int algo, int extended, selftest_report_func_t report)
1267 {
1268 gpg_err_code_t ec;
1269
1270 switch (algo)
1271 {
1272 case GCRY_MD_SHA384:
1273 ec = selftests_sha384 (extended, report);
1274 break;
1275 case GCRY_MD_SHA512:
1276 ec = selftests_sha512 (extended, report);
1277 break;
1278 case GCRY_MD_SHA512_224:
1279 ec = selftests_sha512_224 (extended, report);
1280 break;
1281 case GCRY_MD_SHA512_256:
1282 ec = selftests_sha512_256 (extended, report);
1283 break;
1284 default:
1285 ec = GPG_ERR_DIGEST_ALGO;
1286 break;
1287
1288 }
1289 return ec;
1290 }
1291
1292
1293
1294
1295 static const byte sha512_asn[] = /* Object ID is 2.16.840.1.101.3.4.2.3 */
1296 {
1297 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1298 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
1299 0x00, 0x04, 0x40
1300 };
1301
1302 static const gcry_md_oid_spec_t oid_spec_sha512[] =
1303 {
1304 { "2.16.840.1.101.3.4.2.3" },
1305
1306 /* PKCS#1 sha512WithRSAEncryption */
1307 { "1.2.840.113549.1.1.13" },
1308 /* ANSI X9.62 ecdsaWithSHA512 */
1309 { "1.2.840.10045.4.3.4" },
1310
1311 { NULL }
1312 };
1313
1314 const gcry_md_spec_t _gcry_digest_spec_sha512 =
1315 {
1316 GCRY_MD_SHA512, {0, 1},
1317 "SHA512", sha512_asn, DIM (sha512_asn), oid_spec_sha512, 64,
1318 sha512_init, _gcry_md_block_write, sha512_final, sha512_read, NULL,
1319 _gcry_sha512_hash_buffers,
1320 sizeof (SHA512_CONTEXT),
1321 run_selftests
1322 };
1323
1324 static const byte sha384_asn[] = /* Object ID is 2.16.840.1.101.3.4.2.2 */
1325 {
1326 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1327 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
1328 0x00, 0x04, 0x30
1329 };
1330
1331 static const gcry_md_oid_spec_t oid_spec_sha384[] =
1332 {
1333 { "2.16.840.1.101.3.4.2.2" },
1334
1335 /* PKCS#1 sha384WithRSAEncryption */
1336 { "1.2.840.113549.1.1.12" },
1337
1338 /* SHA384WithECDSA: RFC 7427 (A.3.3.) */
1339 { "1.2.840.10045.4.3.3" },
1340
1341 /* ANSI X9.62 ecdsaWithSHA384 */
1342 { "1.2.840.10045.4.3.3" },
1343
1344 { NULL },
1345 };
1346
1347 const gcry_md_spec_t _gcry_digest_spec_sha384 =
1348 {
1349 GCRY_MD_SHA384, {0, 1},
1350 "SHA384", sha384_asn, DIM (sha384_asn), oid_spec_sha384, 48,
1351 sha384_init, _gcry_md_block_write, sha512_final, sha512_read, NULL,
1352 _gcry_sha384_hash_buffers,
1353 sizeof (SHA512_CONTEXT),
1354 run_selftests
1355 };
1356
1357 static const byte sha512_256_asn[] =
1358 {
1359 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1360 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06, 0x05,
1361 0x00, 0x04, 0x20
1362 };
1363
1364 static const gcry_md_oid_spec_t oid_spec_sha512_256[] =
1365 {
1366 { "2.16.840.1.101.3.4.2.6" },
1367
1368 { NULL },
1369 };
1370
1371 const gcry_md_spec_t _gcry_digest_spec_sha512_256 =
1372 {
1373 GCRY_MD_SHA512_256, {0, 1},
1374 "SHA512_256", sha512_256_asn, DIM (sha512_256_asn), oid_spec_sha512_256, 32,
1375 sha512_256_init, _gcry_md_block_write, sha512_final, sha512_read, NULL,
1376 _gcry_sha512_256_hash_buffers,
1377 sizeof (SHA512_CONTEXT),
1378 run_selftests
1379 };
1380
1381 static const byte sha512_224_asn[] =
1382 {
1383 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1384 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05, 0x05,
1385 0x00, 0x04, 0x1c
1386 };
1387
1388 static const gcry_md_oid_spec_t oid_spec_sha512_224[] =
1389 {
1390 { "2.16.840.1.101.3.4.2.5" },
1391
1392 { NULL },
1393 };
1394
1395 const gcry_md_spec_t _gcry_digest_spec_sha512_224 =
1396 {
1397 GCRY_MD_SHA512_224, {0, 1},
1398 "SHA512_224", sha512_224_asn, DIM (sha512_224_asn), oid_spec_sha512_224, 28,
1399 sha512_224_init, _gcry_md_block_write, sha512_final, sha512_read, NULL,
1400 _gcry_sha512_224_hash_buffers,
1401 sizeof (SHA512_CONTEXT),
1402 run_selftests
1403 };