LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hcrypto - sha256.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 102 103 99.0 %
Date: 2024-04-13 12:30:31 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2006 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #include <config.h>
      35             : #include <roken.h>
      36             : 
      37             : #include "hash.h"
      38             : #include "sha.h"
      39             : 
      40             : #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
      41             : #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
      42             : 
      43             : #define ROTR(x,n)   (((x)>>(n)) | ((x) << (32 - (n))))
      44             : 
      45             : #define Sigma0(x)       (ROTR(x,2)  ^ ROTR(x,13) ^ ROTR(x,22))
      46             : #define Sigma1(x)       (ROTR(x,6)  ^ ROTR(x,11) ^ ROTR(x,25))
      47             : #define sigma0(x)       (ROTR(x,7)  ^ ROTR(x,18) ^ ((x)>>3))
      48             : #define sigma1(x)       (ROTR(x,17) ^ ROTR(x,19) ^ ((x)>>10))
      49             : 
      50             : #define A m->counter[0]
      51             : #define B m->counter[1]
      52             : #define C m->counter[2]
      53             : #define D m->counter[3]
      54             : #define E m->counter[4]
      55             : #define F m->counter[5]
      56             : #define G m->counter[6]
      57             : #define H m->counter[7]
      58             : 
      59             : static const uint32_t constant_256[64] = {
      60             :     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
      61             :     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
      62             :     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
      63             :     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
      64             :     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
      65             :     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
      66             :     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
      67             :     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
      68             :     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
      69             :     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
      70             :     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
      71             :     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
      72             :     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
      73             :     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
      74             :     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
      75             :     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
      76             : };
      77             : 
      78             : int
      79     1214512 : SHA256_Init (SHA256_CTX *m)
      80             : {
      81     1214512 :     m->sz[0] = 0;
      82     1214512 :     m->sz[1] = 0;
      83     1214512 :     A = 0x6a09e667;
      84     1214512 :     B = 0xbb67ae85;
      85     1214512 :     C = 0x3c6ef372;
      86     1214512 :     D = 0xa54ff53a;
      87     1214512 :     E = 0x510e527f;
      88     1214512 :     F = 0x9b05688c;
      89     1214512 :     G = 0x1f83d9ab;
      90     1214512 :     H = 0x5be0cd19;
      91     1214512 :     return 1;
      92             : }
      93             : 
      94             : static void
      95      331686 : calc (SHA256_CTX *m, uint32_t *in)
      96             : {
      97       11684 :     uint32_t AA, BB, CC, DD, EE, FF, GG, HH;
      98       11684 :     uint32_t data[64];
      99       11684 :     int i;
     100             : 
     101      331686 :     AA = A;
     102      331686 :     BB = B;
     103      331686 :     CC = C;
     104      331686 :     DD = D;
     105      331686 :     EE = E;
     106      331686 :     FF = F;
     107      331686 :     GG = G;
     108      331686 :     HH = H;
     109             : 
     110     5638662 :     for (i = 0; i < 16; ++i)
     111     5306976 :         data[i] = in[i];
     112    16252614 :     for (i = 16; i < 64; ++i)
     113    15920928 :         data[i] = sigma1(data[i-2]) + data[i-7] +
     114    15920928 :             sigma0(data[i-15]) + data[i - 16];
     115             : 
     116    21559590 :     for (i = 0; i < 64; i++) {
     117      747776 :         uint32_t T1, T2;
     118             : 
     119    21227904 :         T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_256[i] + data[i];
     120    21227904 :         T2 = Sigma0(AA) + Maj(AA,BB,CC);
     121             : 
     122    21227904 :         HH = GG;
     123    21227904 :         GG = FF;
     124    21227904 :         FF = EE;
     125    21227904 :         EE = DD + T1;
     126    21227904 :         DD = CC;
     127    21227904 :         CC = BB;
     128    21227904 :         BB = AA;
     129    21227904 :         AA = T1 + T2;
     130             :     }
     131             : 
     132      331686 :     A += AA;
     133      331686 :     B += BB;
     134      331686 :     C += CC;
     135      331686 :     D += DD;
     136      331686 :     E += EE;
     137      331686 :     F += FF;
     138      331686 :     G += GG;
     139      331686 :     H += HH;
     140      331686 : }
     141             : 
     142             : /*
     143             :  * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
     144             :  */
     145             : 
     146             : #if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
     147             : static inline uint32_t
     148     5213504 : swap_uint32_t (uint32_t t)
     149             : {
     150             : #define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
     151       93472 :     uint32_t temp1, temp2;
     152             : 
     153     5213504 :     temp1   = cshift(t, 16);
     154     5213504 :     temp2   = temp1 >> 8;
     155     5213504 :     temp1  &= 0x00ff00ff;
     156     5213504 :     temp2  &= 0x00ff00ff;
     157     5213504 :     temp1 <<= 8;
     158     5213504 :     return temp1 | temp2;
     159             : }
     160             : #endif
     161             : 
     162             : struct x32{
     163             :     unsigned int a:32;
     164             :     unsigned int b:32;
     165             : };
     166             : 
     167             : int
     168     1539802 : SHA256_Update (SHA256_CTX *m, const void *v, size_t len)
     169             : {
     170     1539802 :     const unsigned char *p = v;
     171     1539802 :     size_t old_sz = m->sz[0];
     172       57425 :     size_t offset;
     173             : 
     174     1539802 :     m->sz[0] += len * 8;
     175     1539802 :     if (m->sz[0] < old_sz)
     176           0 :         ++m->sz[1];
     177     1539802 :     offset = (old_sz / 8) % 64;
     178     3119939 :     while(len > 0){
     179     1580137 :         size_t l = min(len, 64 - offset);
     180     1580137 :         memcpy(m->save + offset, p, l);
     181     1580137 :         offset += l;
     182     1580137 :         p += l;
     183     1580137 :         len -= l;
     184     1580137 :         if(offset == 64){
     185             : #if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
     186             :             int i;
     187             :             uint32_t current[16];
     188      320002 :             struct x32 *us = (struct x32*)m->save;
     189     2985174 :             for(i = 0; i < 8; i++){
     190     2653488 :                 current[2*i+0] = swap_uint32_t(us[i].a);
     191     2653488 :                 current[2*i+1] = swap_uint32_t(us[i].b);
     192             :             }
     193      331686 :             calc(m, current);
     194             : #else
     195             :             calc(m, (uint32_t*)m->save);
     196             : #endif
     197      331686 :             offset = 0;
     198             :         }
     199             :     }
     200     1539802 :     return 1;
     201             : }
     202             : 
     203             : int
     204      179432 : SHA256_Final (void *res, SHA256_CTX *m)
     205             : {
     206        6453 :     unsigned char zeros[72];
     207      179432 :     unsigned offset = (m->sz[0] / 8) % 64;
     208      179432 :     unsigned int dstart = (120 - offset - 1) % 64 + 1;
     209             : 
     210      179432 :     *zeros = 0x80;
     211      179432 :     memset (zeros + 1, 0, sizeof(zeros) - 1);
     212      179432 :     zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
     213      179432 :     zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
     214      179432 :     zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
     215      179432 :     zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
     216      179432 :     zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
     217      179432 :     zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
     218      179432 :     zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
     219      179432 :     zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
     220      179432 :     SHA256_Update (m, zeros, dstart + 8);
     221             :     {
     222        6453 :         int i;
     223      179432 :         unsigned char *r = (unsigned char*)res;
     224             : 
     225     1621341 :         for (i = 0; i < 8; ++i) {
     226     1435456 :             r[4*i+3] = m->counter[i] & 0xFF;
     227     1435456 :             r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
     228     1435456 :             r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
     229     1435456 :             r[4*i]   = (m->counter[i] >> 24) & 0xFF;
     230             :         }
     231             :     }
     232      179432 :     return 1;
     233             : }

Generated by: LCOV version 1.14