Coverage Report

Created: 2022-12-08 06:10

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