LCOV - code coverage report
Current view: top level - lib - sha2.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 210 228 92.1 %
Date: 2016-06-07 Functions: 12 12 100.0 %
Branches: 52 76 68.4 %

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

Generated by: LCOV version 1.10