LCOV - code coverage report
Current view: top level - cipher - sha1.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 202 215 94.0 %
Date: 2017-03-02 16:44:37 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /* sha1.c - SHA1 hash function
       2             :  * Copyright (C) 1998, 2001, 2002, 2003, 2008 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             :  *  A999 3E36 4706 816A BA3E  2571 7850 C26C 9CD0 D89D
      25             :  *
      26             :  *  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
      27             :  *  8498 3E44 1C3B D26E BAAE  4AA1 F951 29E5 E546 70F1
      28             :  */
      29             : 
      30             : 
      31             : #include <config.h>
      32             : #include <stdio.h>
      33             : #include <stdlib.h>
      34             : #include <string.h>
      35             : #ifdef HAVE_STDINT_H
      36             : # include <stdint.h>
      37             : #endif
      38             : 
      39             : #include "g10lib.h"
      40             : #include "bithelp.h"
      41             : #include "bufhelp.h"
      42             : #include "cipher.h"
      43             : #include "sha1.h"
      44             : 
      45             : 
      46             : /* USE_SSSE3 indicates whether to compile with Intel SSSE3 code. */
      47             : #undef USE_SSSE3
      48             : #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_SSSE3) && \
      49             :     (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
      50             :      defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
      51             : # define USE_SSSE3 1
      52             : #endif
      53             : 
      54             : /* USE_AVX indicates whether to compile with Intel AVX code. */
      55             : #undef USE_AVX
      56             : #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \
      57             :     (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
      58             :      defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
      59             : # define USE_AVX 1
      60             : #endif
      61             : 
      62             : /* USE_BMI2 indicates whether to compile with Intel AVX/BMI2 code. */
      63             : #undef USE_BMI2
      64             : #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \
      65             :     defined(HAVE_GCC_INLINE_ASM_BMI2) && \
      66             :     (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
      67             :      defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
      68             : # define USE_BMI2 1
      69             : #endif
      70             : 
      71             : /* USE_NEON indicates whether to enable ARM NEON assembly code. */
      72             : #undef USE_NEON
      73             : #ifdef ENABLE_NEON_SUPPORT
      74             : # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
      75             :      && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
      76             :      && defined(HAVE_GCC_INLINE_ASM_NEON)
      77             : #  define USE_NEON 1
      78             : # endif
      79             : #endif
      80             : 
      81             : /* USE_ARM_CE indicates whether to enable ARMv8 Crypto Extension assembly
      82             :  * code. */
      83             : #undef USE_ARM_CE
      84             : #ifdef ENABLE_ARM_CRYPTO_SUPPORT
      85             : # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
      86             :      && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
      87             :      && defined(HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO)
      88             : #  define USE_ARM_CE 1
      89             : # elif defined(__AARCH64EL__) \
      90             :        && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \
      91             :        && defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO)
      92             : #  define USE_ARM_CE 1
      93             : # endif
      94             : #endif
      95             : 
      96             : /* A macro to test whether P is properly aligned for an u32 type.
      97             :    Note that config.h provides a suitable replacement for uintptr_t if
      98             :    it does not exist in stdint.h.  */
      99             : /* #if __GNUC__ >= 2 */
     100             : /* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (u32))) */
     101             : /* #else */
     102             : /* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (u32))) */
     103             : /* #endif */
     104             : 
     105             : 
     106             : static unsigned int
     107             : transform (void *c, const unsigned char *data, size_t nblks);
     108             : 
     109             : 
     110             : static void
     111      101325 : sha1_init (void *context, unsigned int flags)
     112             : {
     113      101325 :   SHA1_CONTEXT *hd = context;
     114      101325 :   unsigned int features = _gcry_get_hw_features ();
     115             : 
     116             :   (void)flags;
     117             : 
     118      101325 :   hd->h0 = 0x67452301;
     119      101325 :   hd->h1 = 0xefcdab89;
     120      101325 :   hd->h2 = 0x98badcfe;
     121      101325 :   hd->h3 = 0x10325476;
     122      101325 :   hd->h4 = 0xc3d2e1f0;
     123             : 
     124      101325 :   hd->bctx.nblocks = 0;
     125      101325 :   hd->bctx.nblocks_high = 0;
     126      101325 :   hd->bctx.count = 0;
     127      101325 :   hd->bctx.blocksize = 64;
     128      101325 :   hd->bctx.bwrite = transform;
     129             : 
     130             : #ifdef USE_SSSE3
     131      101325 :   hd->use_ssse3 = (features & HWF_INTEL_SSSE3) != 0;
     132             : #endif
     133             : #ifdef USE_AVX
     134             :   /* AVX implementation uses SHLD which is known to be slow on non-Intel CPUs.
     135             :    * Therefore use this implementation on Intel CPUs only. */
     136      101325 :   hd->use_avx = (features & HWF_INTEL_AVX) && (features & HWF_INTEL_FAST_SHLD);
     137             : #endif
     138             : #ifdef USE_BMI2
     139      101325 :   hd->use_bmi2 = (features & HWF_INTEL_AVX) && (features & HWF_INTEL_BMI2);
     140             : #endif
     141             : #ifdef USE_NEON
     142             :   hd->use_neon = (features & HWF_ARM_NEON) != 0;
     143             : #endif
     144             : #ifdef USE_ARM_CE
     145             :   hd->use_arm_ce = (features & HWF_ARM_SHA1) != 0;
     146             : #endif
     147             :   (void)features;
     148      101325 : }
     149             : 
     150             : /*
     151             :  * Initialize the context HD. This is used to prepare the use of
     152             :  * _gcry_sha1_mixblock.  WARNING: This is a special purpose function
     153             :  * for exclusive use by random-csprng.c.
     154             :  */
     155             : void
     156       13672 : _gcry_sha1_mixblock_init (SHA1_CONTEXT *hd)
     157             : {
     158       13672 :   sha1_init (hd, 0);
     159       13672 : }
     160             : 
     161             : 
     162             : /* Round function macros. */
     163             : #define K1  0x5A827999L
     164             : #define K2  0x6ED9EBA1L
     165             : #define K3  0x8F1BBCDCL
     166             : #define K4  0xCA62C1D6L
     167             : #define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )
     168             : #define F2(x,y,z)   ( x ^ y ^ z )
     169             : #define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )
     170             : #define F4(x,y,z)   ( x ^ y ^ z )
     171             : #define M(i) ( tm =    x[ i    &0x0f]  \
     172             :                      ^ x[(i-14)&0x0f]  \
     173             :                      ^ x[(i-8) &0x0f]  \
     174             :                      ^ x[(i-3) &0x0f], \
     175             :                      (x[i&0x0f] = rol(tm, 1)))
     176             : #define R(a,b,c,d,e,f,k,m)  do { e += rol( a, 5 )     \
     177             :                                       + f( b, c, d )  \
     178             :                                       + k             \
     179             :                                       + m;            \
     180             :                                  b = rol( b, 30 );    \
     181             :                                } while(0)
     182             : 
     183             : 
     184             : #ifdef USE_NEON
     185             : unsigned int
     186             : _gcry_sha1_transform_armv7_neon (void *state, const unsigned char *data,
     187             :                                  size_t nblks);
     188             : #endif
     189             : 
     190             : #ifdef USE_ARM_CE
     191             : unsigned int
     192             : _gcry_sha1_transform_armv8_ce (void *state, const unsigned char *data,
     193             :                                size_t nblks);
     194             : #endif
     195             : 
     196             : /*
     197             :  * Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
     198             :  */
     199             : static unsigned int
     200       71460 : transform_blk (void *ctx, const unsigned char *data)
     201             : {
     202       71460 :   SHA1_CONTEXT *hd = ctx;
     203       71460 :   const u32 *idata = (const void *)data;
     204             :   register u32 a, b, c, d, e; /* Local copies of the chaining variables.  */
     205             :   register u32 tm;            /* Helper.  */
     206             :   u32 x[16];                  /* The array we work on. */
     207             : 
     208             : #define I(i) (x[i] = buf_get_be32(idata + i))
     209             : 
     210             :       /* Get the values of the chaining variables. */
     211       71460 :       a = hd->h0;
     212       71460 :       b = hd->h1;
     213       71460 :       c = hd->h2;
     214       71460 :       d = hd->h3;
     215       71460 :       e = hd->h4;
     216             : 
     217             :       /* Transform. */
     218       71460 :       R( a, b, c, d, e, F1, K1, I( 0) );
     219       71460 :       R( e, a, b, c, d, F1, K1, I( 1) );
     220       71460 :       R( d, e, a, b, c, F1, K1, I( 2) );
     221       71460 :       R( c, d, e, a, b, F1, K1, I( 3) );
     222       71460 :       R( b, c, d, e, a, F1, K1, I( 4) );
     223       71460 :       R( a, b, c, d, e, F1, K1, I( 5) );
     224       71460 :       R( e, a, b, c, d, F1, K1, I( 6) );
     225       71460 :       R( d, e, a, b, c, F1, K1, I( 7) );
     226       71460 :       R( c, d, e, a, b, F1, K1, I( 8) );
     227       71460 :       R( b, c, d, e, a, F1, K1, I( 9) );
     228       71460 :       R( a, b, c, d, e, F1, K1, I(10) );
     229       71460 :       R( e, a, b, c, d, F1, K1, I(11) );
     230       71460 :       R( d, e, a, b, c, F1, K1, I(12) );
     231       71460 :       R( c, d, e, a, b, F1, K1, I(13) );
     232       71460 :       R( b, c, d, e, a, F1, K1, I(14) );
     233       71460 :       R( a, b, c, d, e, F1, K1, I(15) );
     234       71460 :       R( e, a, b, c, d, F1, K1, M(16) );
     235       71460 :       R( d, e, a, b, c, F1, K1, M(17) );
     236       71460 :       R( c, d, e, a, b, F1, K1, M(18) );
     237       71460 :       R( b, c, d, e, a, F1, K1, M(19) );
     238       71460 :       R( a, b, c, d, e, F2, K2, M(20) );
     239       71460 :       R( e, a, b, c, d, F2, K2, M(21) );
     240       71460 :       R( d, e, a, b, c, F2, K2, M(22) );
     241       71460 :       R( c, d, e, a, b, F2, K2, M(23) );
     242       71460 :       R( b, c, d, e, a, F2, K2, M(24) );
     243       71460 :       R( a, b, c, d, e, F2, K2, M(25) );
     244       71460 :       R( e, a, b, c, d, F2, K2, M(26) );
     245       71460 :       R( d, e, a, b, c, F2, K2, M(27) );
     246       71460 :       R( c, d, e, a, b, F2, K2, M(28) );
     247       71460 :       R( b, c, d, e, a, F2, K2, M(29) );
     248       71460 :       R( a, b, c, d, e, F2, K2, M(30) );
     249       71460 :       R( e, a, b, c, d, F2, K2, M(31) );
     250       71460 :       R( d, e, a, b, c, F2, K2, M(32) );
     251       71460 :       R( c, d, e, a, b, F2, K2, M(33) );
     252       71460 :       R( b, c, d, e, a, F2, K2, M(34) );
     253       71460 :       R( a, b, c, d, e, F2, K2, M(35) );
     254       71460 :       R( e, a, b, c, d, F2, K2, M(36) );
     255       71460 :       R( d, e, a, b, c, F2, K2, M(37) );
     256       71460 :       R( c, d, e, a, b, F2, K2, M(38) );
     257       71460 :       R( b, c, d, e, a, F2, K2, M(39) );
     258       71460 :       R( a, b, c, d, e, F3, K3, M(40) );
     259       71460 :       R( e, a, b, c, d, F3, K3, M(41) );
     260       71460 :       R( d, e, a, b, c, F3, K3, M(42) );
     261       71460 :       R( c, d, e, a, b, F3, K3, M(43) );
     262       71460 :       R( b, c, d, e, a, F3, K3, M(44) );
     263       71460 :       R( a, b, c, d, e, F3, K3, M(45) );
     264       71460 :       R( e, a, b, c, d, F3, K3, M(46) );
     265       71460 :       R( d, e, a, b, c, F3, K3, M(47) );
     266       71460 :       R( c, d, e, a, b, F3, K3, M(48) );
     267       71460 :       R( b, c, d, e, a, F3, K3, M(49) );
     268       71460 :       R( a, b, c, d, e, F3, K3, M(50) );
     269       71460 :       R( e, a, b, c, d, F3, K3, M(51) );
     270       71460 :       R( d, e, a, b, c, F3, K3, M(52) );
     271       71460 :       R( c, d, e, a, b, F3, K3, M(53) );
     272       71460 :       R( b, c, d, e, a, F3, K3, M(54) );
     273       71460 :       R( a, b, c, d, e, F3, K3, M(55) );
     274       71460 :       R( e, a, b, c, d, F3, K3, M(56) );
     275       71460 :       R( d, e, a, b, c, F3, K3, M(57) );
     276       71460 :       R( c, d, e, a, b, F3, K3, M(58) );
     277       71460 :       R( b, c, d, e, a, F3, K3, M(59) );
     278       71460 :       R( a, b, c, d, e, F4, K4, M(60) );
     279       71460 :       R( e, a, b, c, d, F4, K4, M(61) );
     280       71460 :       R( d, e, a, b, c, F4, K4, M(62) );
     281       71460 :       R( c, d, e, a, b, F4, K4, M(63) );
     282       71460 :       R( b, c, d, e, a, F4, K4, M(64) );
     283       71460 :       R( a, b, c, d, e, F4, K4, M(65) );
     284       71460 :       R( e, a, b, c, d, F4, K4, M(66) );
     285       71460 :       R( d, e, a, b, c, F4, K4, M(67) );
     286       71460 :       R( c, d, e, a, b, F4, K4, M(68) );
     287       71460 :       R( b, c, d, e, a, F4, K4, M(69) );
     288       71460 :       R( a, b, c, d, e, F4, K4, M(70) );
     289       71460 :       R( e, a, b, c, d, F4, K4, M(71) );
     290       71460 :       R( d, e, a, b, c, F4, K4, M(72) );
     291       71460 :       R( c, d, e, a, b, F4, K4, M(73) );
     292       71460 :       R( b, c, d, e, a, F4, K4, M(74) );
     293       71460 :       R( a, b, c, d, e, F4, K4, M(75) );
     294       71460 :       R( e, a, b, c, d, F4, K4, M(76) );
     295       71460 :       R( d, e, a, b, c, F4, K4, M(77) );
     296       71460 :       R( c, d, e, a, b, F4, K4, M(78) );
     297       71460 :       R( b, c, d, e, a, F4, K4, M(79) );
     298             : 
     299             :       /* Update the chaining variables. */
     300       71460 :       hd->h0 += a;
     301       71460 :       hd->h1 += b;
     302       71460 :       hd->h2 += c;
     303       71460 :       hd->h3 += d;
     304       71460 :       hd->h4 += e;
     305             : 
     306       71460 :   return /* burn_stack */ 88+4*sizeof(void*);
     307             : }
     308             : 
     309             : 
     310             : /* Assembly implementations use SystemV ABI, ABI conversion and additional
     311             :  * stack to store XMM6-XMM15 needed on Win64. */
     312             : #undef ASM_FUNC_ABI
     313             : #undef ASM_EXTRA_STACK
     314             : #if defined(USE_SSSE3) || defined(USE_AVX) || defined(USE_BMI2)
     315             : # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
     316             : #  define ASM_FUNC_ABI __attribute__((sysv_abi))
     317             : #  define ASM_EXTRA_STACK (10 * 16)
     318             : # else
     319             : #  define ASM_FUNC_ABI
     320             : #  define ASM_EXTRA_STACK 0
     321             : # endif
     322             : #endif
     323             : 
     324             : 
     325             : #ifdef USE_SSSE3
     326             : unsigned int
     327             : _gcry_sha1_transform_amd64_ssse3 (void *state, const unsigned char *data,
     328             :                                   size_t nblks) ASM_FUNC_ABI;
     329             : #endif
     330             : 
     331             : #ifdef USE_AVX
     332             : unsigned int
     333             : _gcry_sha1_transform_amd64_avx (void *state, const unsigned char *data,
     334             :                                  size_t nblks) ASM_FUNC_ABI;
     335             : #endif
     336             : 
     337             : #ifdef USE_BMI2
     338             : unsigned int
     339             : _gcry_sha1_transform_amd64_avx_bmi2 (void *state, const unsigned char *data,
     340             :                                      size_t nblks) ASM_FUNC_ABI;
     341             : #endif
     342             : 
     343             : 
     344             : static unsigned int
     345      662571 : transform (void *ctx, const unsigned char *data, size_t nblks)
     346             : {
     347      662571 :   SHA1_CONTEXT *hd = ctx;
     348             :   unsigned int burn;
     349             : 
     350             : #ifdef USE_BMI2
     351      662571 :   if (hd->use_bmi2)
     352           0 :     return _gcry_sha1_transform_amd64_avx_bmi2 (&hd->h0, data, nblks)
     353           0 :            + 4 * sizeof(void*) + ASM_EXTRA_STACK;
     354             : #endif
     355             : #ifdef USE_AVX
     356      662571 :   if (hd->use_avx)
     357      631974 :     return _gcry_sha1_transform_amd64_avx (&hd->h0, data, nblks)
     358      631974 :            + 4 * sizeof(void*) + ASM_EXTRA_STACK;
     359             : #endif
     360             : #ifdef USE_SSSE3
     361       30597 :   if (hd->use_ssse3)
     362           0 :     return _gcry_sha1_transform_amd64_ssse3 (&hd->h0, data, nblks)
     363           0 :            + 4 * sizeof(void*) + ASM_EXTRA_STACK;
     364             : #endif
     365             : #ifdef USE_ARM_CE
     366             :   if (hd->use_arm_ce)
     367             :     return _gcry_sha1_transform_armv8_ce (&hd->h0, data, nblks);
     368             : #endif
     369             : #ifdef USE_NEON
     370             :   if (hd->use_neon)
     371             :     return _gcry_sha1_transform_armv7_neon (&hd->h0, data, nblks)
     372             :            + 4 * sizeof(void*);
     373             : #endif
     374             : 
     375             :   do
     376             :     {
     377       71460 :       burn = transform_blk (hd, data);
     378       71460 :       data += 64;
     379             :     }
     380       71460 :   while (--nblks);
     381             : 
     382             : #ifdef ASM_EXTRA_STACK
     383             :   /* 'transform_blk' is typically inlined and XMM6-XMM15 are stored at
     384             :    *  the prologue of this function. Therefore need to add ASM_EXTRA_STACK to
     385             :    *  here too.
     386             :    */
     387       30597 :   burn += ASM_EXTRA_STACK;
     388             : #endif
     389             : 
     390       30597 :   return burn;
     391             : }
     392             : 
     393             : 
     394             : /*
     395             :  * Apply the SHA-1 transform function on the buffer BLOCKOF64BYTE
     396             :  * which must have a length 64 bytes.  BLOCKOF64BYTE must be 32-bit
     397             :  * aligned.  Updates the 20 bytes in BLOCKOF64BYTE with its mixed
     398             :  * content.  Returns the number of bytes which should be burned on the
     399             :  * stack.  You need to use _gcry_sha1_mixblock_init to initialize the
     400             :  * context.
     401             :  * WARNING: This is a special purpose function for exclusive use by
     402             :  * random-csprng.c.
     403             :  */
     404             : unsigned int
     405      410160 : _gcry_sha1_mixblock (SHA1_CONTEXT *hd, void *blockof64byte)
     406             : {
     407      410160 :   u32 *p = blockof64byte;
     408             :   unsigned int nburn;
     409             : 
     410      410160 :   nburn = transform (hd, blockof64byte, 1);
     411      410160 :   p[0] = hd->h0;
     412      410160 :   p[1] = hd->h1;
     413      410160 :   p[2] = hd->h2;
     414      410160 :   p[3] = hd->h3;
     415      410160 :   p[4] = hd->h4;
     416             : 
     417      410160 :   return nburn;
     418             : }
     419             : 
     420             : 
     421             : /* The routine final terminates the computation and
     422             :  * returns the digest.
     423             :  * The handle is prepared for a new cycle, but adding bytes to the
     424             :  * handle will the destroy the returned buffer.
     425             :  * Returns: 20 bytes representing the digest.
     426             :  */
     427             : 
     428             : static void
     429      141935 : sha1_final(void *context)
     430             : {
     431      141935 :   SHA1_CONTEXT *hd = context;
     432             :   u32 t, th, msb, lsb;
     433             :   unsigned char *p;
     434             :   unsigned int burn;
     435             : 
     436      141935 :   _gcry_md_block_write (hd, NULL, 0); /* flush */;
     437             : 
     438      141935 :   t = hd->bctx.nblocks;
     439             :   if (sizeof t == sizeof hd->bctx.nblocks)
     440             :     th = hd->bctx.nblocks_high;
     441             :   else
     442      141935 :     th = hd->bctx.nblocks >> 32;
     443             : 
     444             :   /* multiply by 64 to make a byte count */
     445      141935 :   lsb = t << 6;
     446      141935 :   msb = (th << 6) | (t >> 26);
     447             :   /* add the count */
     448      141935 :   t = lsb;
     449      141935 :   if( (lsb += hd->bctx.count) < t )
     450           0 :     msb++;
     451             :   /* multiply by 8 to make a bit count */
     452      141935 :   t = lsb;
     453      141935 :   lsb <<= 3;
     454      141935 :   msb <<= 3;
     455      141935 :   msb |= t >> 29;
     456             : 
     457      141935 :   if( hd->bctx.count < 56 )  /* enough room */
     458             :     {
     459      141887 :       hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad */
     460     4867052 :       while( hd->bctx.count < 56 )
     461     4583278 :         hd->bctx.buf[hd->bctx.count++] = 0;  /* pad */
     462             :     }
     463             :   else  /* need one extra block */
     464             :     {
     465          48 :       hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad character */
     466         304 :       while( hd->bctx.count < 64 )
     467         208 :         hd->bctx.buf[hd->bctx.count++] = 0;
     468          48 :       _gcry_md_block_write(hd, NULL, 0);  /* flush */;
     469          48 :       memset(hd->bctx.buf, 0, 56 ); /* fill next block with zeroes */
     470             :     }
     471             :   /* append the 64 bit count */
     472      141935 :   buf_put_be32(hd->bctx.buf + 56, msb);
     473      141935 :   buf_put_be32(hd->bctx.buf + 60, lsb);
     474      141935 :   burn = transform( hd, hd->bctx.buf, 1 );
     475      141935 :   _gcry_burn_stack (burn);
     476             : 
     477      141935 :   p = hd->bctx.buf;
     478             : #define X(a) do { buf_put_be32(p, hd->h##a); p += 4; } while(0)
     479      141935 :   X(0);
     480      141935 :   X(1);
     481      141935 :   X(2);
     482      141935 :   X(3);
     483      141935 :   X(4);
     484             : #undef X
     485             : 
     486      141935 : }
     487             : 
     488             : static unsigned char *
     489       59735 : sha1_read( void *context )
     490             : {
     491       59735 :   SHA1_CONTEXT *hd = context;
     492             : 
     493       59735 :   return hd->bctx.buf;
     494             : }
     495             : 
     496             : /****************
     497             :  * Shortcut functions which puts the hash value of the supplied buffer
     498             :  * into outbuf which must have a size of 20 bytes.
     499             :  */
     500             : void
     501       78109 : _gcry_sha1_hash_buffer (void *outbuf, const void *buffer, size_t length)
     502             : {
     503             :   SHA1_CONTEXT hd;
     504             : 
     505       78109 :   sha1_init (&hd, 0);
     506       78109 :   _gcry_md_block_write (&hd, buffer, length);
     507       78109 :   sha1_final (&hd);
     508       78109 :   memcpy (outbuf, hd.bctx.buf, 20);
     509       78109 : }
     510             : 
     511             : 
     512             : /* Variant of the above shortcut function using a multiple buffers.  */
     513             : void
     514           6 : _gcry_sha1_hash_buffers (void *outbuf, const gcry_buffer_t *iov, int iovcnt)
     515             : {
     516             :   SHA1_CONTEXT hd;
     517             : 
     518           6 :   sha1_init (&hd, 0);
     519          22 :   for (;iovcnt > 0; iov++, iovcnt--)
     520          32 :     _gcry_md_block_write (&hd,
     521          16 :                           (const char*)iov[0].data + iov[0].off, iov[0].len);
     522           6 :   sha1_final (&hd);
     523           6 :   memcpy (outbuf, hd.bctx.buf, 20);
     524           6 : }
     525             : 
     526             : 
     527             : 
     528             : /*
     529             :      Self-test section.
     530             :  */
     531             : 
     532             : 
     533             : static gpg_err_code_t
     534           3 : selftests_sha1 (int extended, selftest_report_func_t report)
     535             : {
     536             :   const char *what;
     537             :   const char *errtxt;
     538             : 
     539           3 :   what = "short string";
     540           3 :   errtxt = _gcry_hash_selftest_check_one
     541             :     (GCRY_MD_SHA1, 0,
     542             :      "abc", 3,
     543             :      "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
     544             :      "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
     545           3 :   if (errtxt)
     546           0 :     goto failed;
     547             : 
     548           3 :   if (extended)
     549             :     {
     550           3 :       what = "long string";
     551           3 :       errtxt = _gcry_hash_selftest_check_one
     552             :         (GCRY_MD_SHA1, 0,
     553             :          "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
     554             :          "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE"
     555             :          "\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1", 20);
     556           3 :       if (errtxt)
     557           0 :         goto failed;
     558             : 
     559           3 :       what = "one million \"a\"";
     560           3 :       errtxt = _gcry_hash_selftest_check_one
     561             :         (GCRY_MD_SHA1, 1,
     562             :          NULL, 0,
     563             :          "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E"
     564             :          "\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F", 20);
     565           3 :       if (errtxt)
     566           0 :         goto failed;
     567             :     }
     568             : 
     569           3 :   return 0; /* Succeeded. */
     570             : 
     571             :  failed:
     572           0 :   if (report)
     573           0 :     report ("digest", GCRY_MD_SHA1, what, errtxt);
     574           0 :   return GPG_ERR_SELFTEST_FAILED;
     575             : }
     576             : 
     577             : 
     578             : /* Run a full self-test for ALGO and return 0 on success.  */
     579             : static gpg_err_code_t
     580           3 : run_selftests (int algo, int extended, selftest_report_func_t report)
     581             : {
     582             :   gpg_err_code_t ec;
     583             : 
     584           3 :   switch (algo)
     585             :     {
     586             :     case GCRY_MD_SHA1:
     587           3 :       ec = selftests_sha1 (extended, report);
     588           3 :       break;
     589             :     default:
     590           0 :       ec = GPG_ERR_DIGEST_ALGO;
     591           0 :       break;
     592             : 
     593             :     }
     594           3 :   return ec;
     595             : }
     596             : 
     597             : 
     598             : 
     599             : 
     600             : static unsigned char asn[15] = /* Object ID is 1.3.14.3.2.26 */
     601             :   { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
     602             :     0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
     603             : 
     604             : static gcry_md_oid_spec_t oid_spec_sha1[] =
     605             :   {
     606             :     /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
     607             :     { "1.2.840.113549.1.1.5" },
     608             :     /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
     609             :     { "1.2.840.10040.4.3" },
     610             :     /* from NIST's OIW  (sha1) */
     611             :     { "1.3.14.3.2.26" },
     612             :     /* from NIST OIW (sha-1WithRSAEncryption) */
     613             :     { "1.3.14.3.2.29" },
     614             :     /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */
     615             :     { "1.2.840.10045.4.1" },
     616             :     { NULL },
     617             :   };
     618             : 
     619             : gcry_md_spec_t _gcry_digest_spec_sha1 =
     620             :   {
     621             :     GCRY_MD_SHA1, {0, 1},
     622             :     "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
     623             :     sha1_init, _gcry_md_block_write, sha1_final, sha1_read, NULL,
     624             :     sizeof (SHA1_CONTEXT),
     625             :     run_selftests
     626             :   };

Generated by: LCOV version 1.13