LCOV - code coverage report
Current view: top level - src/crypto/x11 - shavite.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 204 228 89.5 %
Date: 2026-06-25 07:23:51 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /* $Id: shavite.c 227 2010-06-16 17:28:38Z tp $ */
       2             : /*
       3             :  * SHAvite-3 implementation.
       4             :  *
       5             :  * ==========================(LICENSE BEGIN)============================
       6             :  *
       7             :  * Copyright (c) 2007-2010  Projet RNRT SAPHIR
       8             :  *
       9             :  * Permission is hereby granted, free of charge, to any person obtaining
      10             :  * a copy of this software and associated documentation files (the
      11             :  * "Software"), to deal in the Software without restriction, including
      12             :  * without limitation the rights to use, copy, modify, merge, publish,
      13             :  * distribute, sublicense, and/or sell copies of the Software, and to
      14             :  * permit persons to whom the Software is furnished to do so, subject to
      15             :  * the following conditions:
      16             :  *
      17             :  * The above copyright notice and this permission notice shall be
      18             :  * included in all copies or substantial portions of the Software.
      19             :  *
      20             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      21             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      22             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      23             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      24             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      25             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      26             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      27             :  *
      28             :  * ===========================(LICENSE END)=============================
      29             :  *
      30             :  * @author   Thomas Pornin <thomas.pornin@cryptolog.com>
      31             :  */
      32             : 
      33             : #include <crypto/x11/aes.h>
      34             : #include <crypto/x11/dispatch.h>
      35             : 
      36             : #include <cstddef>
      37             : #include <cstring>
      38             : 
      39             : #include "sph_shavite.h"
      40             : 
      41             : #ifdef _MSC_VER
      42             : #pragma warning (disable: 4146)
      43             : #endif
      44             : 
      45             : #define C32   SPH_C32
      46             : 
      47             : /*
      48             :  * As of round 2 of the SHA-3 competition, the published reference
      49             :  * implementation and test vectors are wrong, because they use
      50             :  * big-endian AES tables while the internal decoding uses little-endian.
      51             :  * The code below follows the specification. To turn it into a code
      52             :  * which follows the reference implementation (the one called "BugFix"
      53             :  * on the SHAvite-3 web site, published on Nov 23rd, 2009), comment out
      54             :  * the code below (from the '#define AES_BIG_ENDIAN...' to the definition
      55             :  * of the AES_ROUND_NOKEY macro) and replace it with the version which
      56             :  * is commented out afterwards.
      57             :  */
      58             : 
      59             : static const sph_u32 IV512[] = {
      60             :         C32(0x72FCCDD8), C32(0x79CA4727), C32(0x128A077B), C32(0x40D55AEC),
      61             :         C32(0xD1901A06), C32(0x430AE307), C32(0xB29F5CD1), C32(0xDF07FBFC),
      62             :         C32(0x8E45D73D), C32(0x681AB538), C32(0xBDE86578), C32(0xDD577E47),
      63             :         C32(0xE275EADE), C32(0x502D9FCD), C32(0xB9357178), C32(0x022A4B9A)
      64             : };
      65             : 
      66             : #define AES_ROUND_NOKEY(x0, x1, x2, x3)   do { \
      67             :                 sapphire::soft_aes::RoundKeyless(x0, x1, x2, x3, x0, x1, x2, x3); \
      68             :         } while (0)
      69             : 
      70             : namespace sapphire {
      71             : namespace {
      72       31724 : void CompressElement(uint32_t& l0, uint32_t& l1, uint32_t& l2, uint32_t& l3,
      73             :                                          uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3, const uint32_t* rk)
      74             : {
      75       31724 :         uint32_t x0{r0 ^ rk[0]};
      76       31724 :         uint32_t x1{r1 ^ rk[1]};
      77       31724 :         uint32_t x2{r2 ^ rk[2]};
      78       31724 :         uint32_t x3{r3 ^ rk[3]};
      79       31724 :         soft_aes::RoundKeyless(x0, x1, x2, x3, x0, x1, x2, x3);
      80       31724 :         x0 ^= rk[4];
      81       31724 :         x1 ^= rk[5];
      82       31724 :         x2 ^= rk[6];
      83       31724 :         x3 ^= rk[7];
      84       31724 :         soft_aes::RoundKeyless(x0, x1, x2, x3, x0, x1, x2, x3);
      85       31724 :         x0 ^= rk[8];
      86       31724 :         x1 ^= rk[9];
      87       31724 :         x2 ^= rk[10];
      88       31724 :         x3 ^= rk[11];
      89       31724 :         soft_aes::RoundKeyless(x0, x1, x2, x3, x0, x1, x2, x3);
      90       31724 :         x0 ^= rk[12];
      91       31724 :         x1 ^= rk[13];
      92       31724 :         x2 ^= rk[14];
      93       31724 :         x3 ^= rk[15];
      94       31724 :         soft_aes::RoundKeyless(x0, x1, x2, x3, x0, x1, x2, x3);
      95       31724 :         l0 ^= x0;
      96       31724 :         l1 ^= x1;
      97       31724 :         l2 ^= x2;
      98       31724 :         l3 ^= x3;
      99       31724 : }
     100             : } // anonymous namespace
     101             : 
     102             : namespace soft_shavite {
     103             : /*
     104             :  * This function assumes that "msg" is aligned for 32-bit access.
     105             :  */
     106        1133 : void Compress(sph_shavite_big_context *sc, const void *msg)
     107             : {
     108        1133 :         sph_u32 p0, p1, p2, p3, p4, p5, p6, p7;
     109        1133 :         sph_u32 p8, p9, pA, pB, pC, pD, pE, pF;
     110        1133 :         alignas(16) sph_u32 rk[448];
     111        1133 :         size_t u;
     112        1133 :         int r, s;
     113             : 
     114             : #if SPH_LITTLE_ENDIAN
     115             :         memcpy(rk, msg, 128);
     116             : #else
     117       10197 :         for (u = 0; u < 32; u += 4) {
     118        9064 :                 rk[u + 0] = sph_dec32le_aligned(
     119        9064 :                         (const unsigned char *)msg + (u << 2) +  0);
     120        9064 :                 rk[u + 1] = sph_dec32le_aligned(
     121        9064 :                         (const unsigned char *)msg + (u << 2) +  4);
     122        9064 :                 rk[u + 2] = sph_dec32le_aligned(
     123        9064 :                         (const unsigned char *)msg + (u << 2) +  8);
     124        9064 :                 rk[u + 3] = sph_dec32le_aligned(
     125        9064 :                         (const unsigned char *)msg + (u << 2) + 12);
     126        9064 :         }
     127             : #endif
     128        1133 :         u = 32;
     129        7931 :         for (;;) {
     130       39655 :                 for (s = 0; s < 4; s ++) {
     131       31724 :                         sph_u32 x0, x1, x2, x3;
     132             : 
     133       31724 :                         x0 = rk[u - 31];
     134       31724 :                         x1 = rk[u - 30];
     135       31724 :                         x2 = rk[u - 29];
     136       31724 :                         x3 = rk[u - 32];
     137       31724 :                         AES_ROUND_NOKEY(x0, x1, x2, x3);
     138       31724 :                         rk[u + 0] = x0 ^ rk[u - 4];
     139       31724 :                         rk[u + 1] = x1 ^ rk[u - 3];
     140       31724 :                         rk[u + 2] = x2 ^ rk[u - 2];
     141       31724 :                         rk[u + 3] = x3 ^ rk[u - 1];
     142       31724 :                         if (u == 32) {
     143        1133 :                                 rk[ 32] ^= sc->count0;
     144        1133 :                                 rk[ 33] ^= sc->count1;
     145        1133 :                                 rk[ 34] ^= sc->count2;
     146        1133 :                                 rk[ 35] ^= SPH_T32(~sc->count3);
     147       31724 :                         } else if (u == 440) {
     148        1133 :                                 rk[440] ^= sc->count1;
     149        1133 :                                 rk[441] ^= sc->count0;
     150        1133 :                                 rk[442] ^= sc->count3;
     151        1133 :                                 rk[443] ^= SPH_T32(~sc->count2);
     152        1133 :                         }
     153       31724 :                         u += 4;
     154             : 
     155       31724 :                         x0 = rk[u - 31];
     156       31724 :                         x1 = rk[u - 30];
     157       31724 :                         x2 = rk[u - 29];
     158       31724 :                         x3 = rk[u - 32];
     159       31724 :                         AES_ROUND_NOKEY(x0, x1, x2, x3);
     160       31724 :                         rk[u + 0] = x0 ^ rk[u - 4];
     161       31724 :                         rk[u + 1] = x1 ^ rk[u - 3];
     162       31724 :                         rk[u + 2] = x2 ^ rk[u - 2];
     163       31724 :                         rk[u + 3] = x3 ^ rk[u - 1];
     164       31724 :                         if (u == 164) {
     165        1133 :                                 rk[164] ^= sc->count3;
     166        1133 :                                 rk[165] ^= sc->count2;
     167        1133 :                                 rk[166] ^= sc->count1;
     168        1133 :                                 rk[167] ^= SPH_T32(~sc->count0);
     169       31724 :                         } else if (u == 316) {
     170        1133 :                                 rk[316] ^= sc->count2;
     171        1133 :                                 rk[317] ^= sc->count3;
     172        1133 :                                 rk[318] ^= sc->count0;
     173        1133 :                                 rk[319] ^= SPH_T32(~sc->count1);
     174        1133 :                         }
     175       31724 :                         u += 4;
     176       31724 :                 }
     177        7931 :                 if (u == 448)
     178        1133 :                         break;
     179       61182 :                 for (s = 0; s < 8; s ++) {
     180       54384 :                         rk[u + 0] = rk[u - 32] ^ rk[u - 7];
     181       54384 :                         rk[u + 1] = rk[u - 31] ^ rk[u - 6];
     182       54384 :                         rk[u + 2] = rk[u - 30] ^ rk[u - 5];
     183       54384 :                         rk[u + 3] = rk[u - 29] ^ rk[u - 4];
     184       54384 :                         u += 4;
     185       54384 :                 }
     186             :         }
     187             : 
     188        1133 :         p0 = sc->h[0x0];
     189        1133 :         p1 = sc->h[0x1];
     190        1133 :         p2 = sc->h[0x2];
     191        1133 :         p3 = sc->h[0x3];
     192        1133 :         p4 = sc->h[0x4];
     193        1133 :         p5 = sc->h[0x5];
     194        1133 :         p6 = sc->h[0x6];
     195        1133 :         p7 = sc->h[0x7];
     196        1133 :         p8 = sc->h[0x8];
     197        1133 :         p9 = sc->h[0x9];
     198        1133 :         pA = sc->h[0xA];
     199        1133 :         pB = sc->h[0xB];
     200        1133 :         pC = sc->h[0xC];
     201        1133 :         pD = sc->h[0xD];
     202        1133 :         pE = sc->h[0xE];
     203        1133 :         pF = sc->h[0xF];
     204        1133 :         u = 0;
     205       16995 :         for (r = 0; r < 14; r ++) {
     206             : #define C512_ELT(l0, l1, l2, l3, r0, r1, r2, r3)   do { \
     207             :                 CompressElement(l0, l1, l2, l3, r0, r1, r2, r3, &rk[u]); \
     208             :                 u += 16; \
     209             :         } while (0)
     210             : 
     211             : #define WROT(a, b, c, d)   do { \
     212             :                 sph_u32 t = d; \
     213             :                 d = c; \
     214             :                 c = b; \
     215             :                 b = a; \
     216             :                 a = t; \
     217             :         } while (0)
     218             : 
     219       15862 :                 C512_ELT(p0, p1, p2, p3, p4, p5, p6, p7);
     220       15862 :                 C512_ELT(p8, p9, pA, pB, pC, pD, pE, pF);
     221             : 
     222       15862 :                 WROT(p0, p4, p8, pC);
     223       15862 :                 WROT(p1, p5, p9, pD);
     224       15862 :                 WROT(p2, p6, pA, pE);
     225       15862 :                 WROT(p3, p7, pB, pF);
     226             : 
     227             : #undef C512_ELT
     228             : #undef WROT
     229       15862 :         }
     230        1133 :         sc->h[0x0] ^= p0;
     231        1133 :         sc->h[0x1] ^= p1;
     232        1133 :         sc->h[0x2] ^= p2;
     233        1133 :         sc->h[0x3] ^= p3;
     234        1133 :         sc->h[0x4] ^= p4;
     235        1133 :         sc->h[0x5] ^= p5;
     236        1133 :         sc->h[0x6] ^= p6;
     237        1133 :         sc->h[0x7] ^= p7;
     238        1133 :         sc->h[0x8] ^= p8;
     239        1133 :         sc->h[0x9] ^= p9;
     240        1133 :         sc->h[0xA] ^= pA;
     241        1133 :         sc->h[0xB] ^= pB;
     242        1133 :         sc->h[0xC] ^= pC;
     243        1133 :         sc->h[0xD] ^= pD;
     244        1133 :         sc->h[0xE] ^= pE;
     245        1133 :         sc->h[0xF] ^= pF;
     246        1133 : }
     247             : } // namespace shavite_soft
     248             : } // namespace sapphire
     249             : 
     250             : sapphire::dispatch::ShaviteCompressFn shavite_c512 = sapphire::soft_shavite::Compress;
     251             : 
     252             : static void
     253     2040214 : shavite_big_init(sph_shavite_big_context *sc, const sph_u32 *iv)
     254             : {
     255     2040214 :         memcpy(sc->h, iv, sizeof sc->h);
     256     2040214 :         sc->ptr = 0;
     257     2040214 :         sc->count0 = 0;
     258     2040214 :         sc->count1 = 0;
     259     2040214 :         sc->count2 = 0;
     260     2040214 :         sc->count3 = 0;
     261     2040214 : }
     262             : 
     263             : static void
     264     1020110 : shavite_big_core(sph_shavite_big_context *sc, const void *data, size_t len)
     265             : {
     266     1020110 :         unsigned char *buf;
     267     1020110 :         size_t ptr;
     268             : 
     269     1020110 :         buf = sc->buf;
     270     1020110 :         ptr = sc->ptr;
     271     2040219 :         while (len > 0) {
     272     1020111 :                 size_t clen;
     273             : 
     274     1020111 :                 clen = (sizeof sc->buf) - ptr;
     275     1020111 :                 if (clen > len)
     276     1020110 :                         clen = len;
     277     1020111 :                 memcpy(buf + ptr, data, clen);
     278     1020111 :                 data = (const unsigned char *)data + clen;
     279     1020111 :                 ptr += clen;
     280     1020111 :                 len -= clen;
     281     1020111 :                 if (ptr == sizeof sc->buf) {
     282           0 :                         if ((sc->count0 = SPH_T32(sc->count0 + 1024)) == 0) {
     283           0 :                                 sc->count1 = SPH_T32(sc->count1 + 1);
     284           0 :                                 if (sc->count1 == 0) {
     285           0 :                                         sc->count2 = SPH_T32(sc->count2 + 1);
     286           0 :                                         if (sc->count2 == 0) {
     287           0 :                                                 sc->count3 = SPH_T32(
     288             :                                                         sc->count3 + 1);
     289           0 :                                         }
     290           0 :                                 }
     291           0 :                         }
     292           0 :                         shavite_c512(sc, buf);
     293           0 :                         ptr = 0;
     294           0 :                 }
     295     1020109 :         }
     296     1020108 :         sc->ptr = ptr;
     297     1020108 : }
     298             : 
     299             : static void
     300     1020110 : shavite_big_close(sph_shavite_big_context *sc,
     301             :         unsigned ub, unsigned n, void *dst, size_t out_size_w32)
     302             : {
     303     1020110 :         unsigned char *buf;
     304     1020110 :         size_t ptr, u;
     305     1020110 :         unsigned z;
     306     1020110 :         sph_u32 count0, count1, count2, count3;
     307             : 
     308     1020110 :         buf = sc->buf;
     309     1020110 :         ptr = sc->ptr;
     310     1020110 :         count0 = (sc->count0 += (ptr << 3) + n);
     311     1020110 :         count1 = sc->count1;
     312     1020110 :         count2 = sc->count2;
     313     1020110 :         count3 = sc->count3;
     314     1020110 :         z = 0x80 >> n;
     315     1020110 :         z = ((ub & -z) | z) & 0xFF;
     316     1020110 :         if (ptr == 0 && n == 0) {
     317           0 :                 buf[0] = 0x80;
     318           0 :                 memset(buf + 1, 0, 109);
     319           0 :                 sc->count0 = sc->count1 = sc->count2 = sc->count3 = 0;
     320     1020110 :         } else if (ptr < 110) {
     321     1020110 :                 buf[ptr ++] = z;
     322     1020110 :                 memset(buf + ptr, 0, 110 - ptr);
     323     1020110 :         } else {
     324           0 :                 buf[ptr ++] = z;
     325           0 :                 memset(buf + ptr, 0, 128 - ptr);
     326           0 :                 shavite_c512(sc, buf);
     327           0 :                 memset(buf, 0, 110);
     328           0 :                 sc->count0 = sc->count1 = sc->count2 = sc->count3 = 0;
     329             :         }
     330     1020110 :         sph_enc32le(buf + 110, count0);
     331     1020110 :         sph_enc32le(buf + 114, count1);
     332     1020110 :         sph_enc32le(buf + 118, count2);
     333     1020110 :         sph_enc32le(buf + 122, count3);
     334     1020110 :         buf[126] = out_size_w32 << 5;
     335     1020110 :         buf[127] = out_size_w32 >> 3;
     336     1020110 :         shavite_c512(sc, buf);
     337    17341878 :         for (u = 0; u < out_size_w32; u ++)
     338    16321768 :                 sph_enc32le((unsigned char *)dst + (u << 2), sc->h[u]);
     339     1020110 : }
     340             : 
     341             : /* see sph_shavite.h */
     342             : void
     343     1020111 : sph_shavite512_init(sph_shavite512_context *cc)
     344             : {
     345     1020111 :         shavite_big_init(cc, IV512);
     346     1020111 : }
     347             : 
     348             : /* see sph_shavite.h */
     349             : void
     350     1020111 : sph_shavite512(sph_shavite512_context *cc, const void *data, size_t len)
     351             : {
     352     1020111 :         shavite_big_core(cc, data, len);
     353     1020111 : }
     354             : 
     355             : /* see sph_shavite.h */
     356             : void
     357     1020110 : sph_shavite512_close(sph_shavite512_context *cc, void *dst)
     358             : {
     359     1020110 :         shavite_big_close(cc, 0, 0, dst, 16);
     360     1020110 :         shavite_big_init(cc, IV512);
     361     1020110 : }
     362             : 
     363             : /* see sph_shavite.h */
     364             : void
     365           0 : sph_shavite512_addbits_and_close(sph_shavite512_context *cc, unsigned ub, unsigned n, void *dst)
     366             : {
     367           0 :         shavite_big_close(cc, ub, n, dst, 16);
     368           0 :         shavite_big_init(cc, IV512);
     369           0 : }

Generated by: LCOV version 1.16