LCOV - code coverage report
Current view: top level - src/test/fuzz - crypto_diff_fuzz_chacha20.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.5 % 217 216
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 10 10
Branches: 75.0 % 60 45

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-2021 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <crypto/chacha20.h>
       6                 :             : #include <test/fuzz/FuzzedDataProvider.h>
       7                 :             : #include <test/fuzz/fuzz.h>
       8                 :             : #include <test/fuzz/util.h>
       9                 :             : 
      10                 :             : #include <cstdint>
      11                 :             : #include <vector>
      12                 :             : 
      13                 :             : /*
      14                 :             : From https://cr.yp.to/chacha.html
      15                 :             : chacha-merged.c version 20080118
      16                 :             : D. J. Bernstein
      17                 :             : Public domain.
      18                 :             : */
      19                 :             : 
      20                 :             : typedef unsigned int u32;
      21                 :             : typedef unsigned char u8;
      22                 :             : 
      23                 :             : #define U8C(v) (v##U)
      24                 :             : #define U32C(v) (v##U)
      25                 :             : 
      26                 :             : #define U8V(v) ((u8)(v)&U8C(0xFF))
      27                 :             : #define U32V(v) ((u32)(v)&U32C(0xFFFFFFFF))
      28                 :             : 
      29                 :             : #define ROTL32(v, n) (U32V((v) << (n)) | ((v) >> (32 - (n))))
      30                 :             : 
      31                 :             : #define U8TO32_LITTLE(p)                                              \
      32                 :             :     (((u32)((p)[0])) | ((u32)((p)[1]) << 8) | ((u32)((p)[2]) << 16) | \
      33                 :             :      ((u32)((p)[3]) << 24))
      34                 :             : 
      35                 :             : #define U32TO8_LITTLE(p, v)      \
      36                 :             :     do {                         \
      37                 :             :         (p)[0] = U8V((v));       \
      38                 :             :         (p)[1] = U8V((v) >> 8);  \
      39                 :             :         (p)[2] = U8V((v) >> 16); \
      40                 :             :         (p)[3] = U8V((v) >> 24); \
      41                 :             :     } while (0)
      42                 :             : 
      43                 :             : /* ------------------------------------------------------------------------- */
      44                 :             : /* Data structures */
      45                 :             : 
      46                 :             : typedef struct
      47                 :             : {
      48                 :             :     u32 input[16];
      49                 :             : } ECRYPT_ctx;
      50                 :             : 
      51                 :             : /* ------------------------------------------------------------------------- */
      52                 :             : /* Mandatory functions */
      53                 :             : 
      54                 :             : void ECRYPT_keysetup(
      55                 :             :     ECRYPT_ctx* ctx,
      56                 :             :     const u8* key,
      57                 :             :     u32 keysize, /* Key size in bits. */
      58                 :             :     u32 ivsize); /* IV size in bits. */
      59                 :             : 
      60                 :             : void ECRYPT_ivsetup(
      61                 :             :     ECRYPT_ctx* ctx,
      62                 :             :     const u8* iv);
      63                 :             : 
      64                 :             : void ECRYPT_encrypt_bytes(
      65                 :             :     ECRYPT_ctx* ctx,
      66                 :             :     const u8* plaintext,
      67                 :             :     u8* ciphertext,
      68                 :             :     u32 msglen); /* Message length in bytes. */
      69                 :             : 
      70                 :             : /* ------------------------------------------------------------------------- */
      71                 :             : 
      72                 :             : /* Optional features */
      73                 :             : 
      74                 :             : void ECRYPT_keystream_bytes(
      75                 :             :     ECRYPT_ctx* ctx,
      76                 :             :     u8* keystream,
      77                 :             :     u32 length); /* Length of keystream in bytes. */
      78                 :             : 
      79                 :             : /* ------------------------------------------------------------------------- */
      80                 :             : 
      81                 :             : #define ROTATE(v, c) (ROTL32(v, c))
      82                 :             : #define XOR(v, w) ((v) ^ (w))
      83                 :             : #define PLUS(v, w) (U32V((v) + (w)))
      84                 :             : #define PLUSONE(v) (PLUS((v), 1))
      85                 :             : 
      86                 :             : #define QUARTERROUND(a, b, c, d) \
      87                 :             :     a = PLUS(a, b); d = ROTATE(XOR(d, a), 16);   \
      88                 :             :     c = PLUS(c, d); b = ROTATE(XOR(b, c), 12);   \
      89                 :             :     a = PLUS(a, b); d = ROTATE(XOR(d, a), 8);    \
      90                 :             :     c = PLUS(c, d); b = ROTATE(XOR(b, c), 7);
      91                 :             : 
      92                 :             : static const char sigma[] = "expand 32-byte k";
      93                 :             : static const char tau[] = "expand 16-byte k";
      94                 :             : 
      95                 :        2815 : void ECRYPT_keysetup(ECRYPT_ctx* x, const u8* k, u32 kbits, u32 ivbits)
      96                 :             : {
      97                 :        2815 :     const char* constants;
      98                 :             : 
      99                 :        2815 :     x->input[4] = U8TO32_LITTLE(k + 0);
     100                 :        2815 :     x->input[5] = U8TO32_LITTLE(k + 4);
     101                 :        2815 :     x->input[6] = U8TO32_LITTLE(k + 8);
     102                 :        2815 :     x->input[7] = U8TO32_LITTLE(k + 12);
     103         [ +  - ]:        2815 :     if (kbits == 256) { /* recommended */
     104                 :        2815 :         k += 16;
     105                 :        2815 :         constants = sigma;
     106                 :        2815 :     } else { /* kbits == 128 */
     107                 :           0 :         constants = tau;
     108                 :             :     }
     109                 :        2815 :     x->input[8] = U8TO32_LITTLE(k + 0);
     110                 :        2815 :     x->input[9] = U8TO32_LITTLE(k + 4);
     111                 :        2815 :     x->input[10] = U8TO32_LITTLE(k + 8);
     112                 :        2815 :     x->input[11] = U8TO32_LITTLE(k + 12);
     113                 :        2815 :     x->input[0] = U8TO32_LITTLE(constants + 0);
     114                 :        2815 :     x->input[1] = U8TO32_LITTLE(constants + 4);
     115                 :        2815 :     x->input[2] = U8TO32_LITTLE(constants + 8);
     116                 :        2815 :     x->input[3] = U8TO32_LITTLE(constants + 12);
     117                 :        2815 : }
     118                 :             : 
     119                 :        2815 : void ECRYPT_ivsetup(ECRYPT_ctx* x, const u8* iv)
     120                 :             : {
     121                 :        2815 :     x->input[12] = 0;
     122                 :        2815 :     x->input[13] = 0;
     123                 :        2815 :     x->input[14] = U8TO32_LITTLE(iv + 0);
     124                 :        2815 :     x->input[15] = U8TO32_LITTLE(iv + 4);
     125                 :        2815 : }
     126                 :             : 
     127                 :       60448 : void ECRYPT_encrypt_bytes(ECRYPT_ctx* x, const u8* m, u8* c, u32 bytes)
     128                 :             : {
     129                 :       60448 :     u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
     130                 :       60448 :     u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
     131                 :       60448 :     u8* ctarget = nullptr;
     132                 :       60448 :     u8 tmp[64];
     133                 :       60448 :     uint32_t i;
     134                 :             : 
     135         [ +  + ]:       60448 :     if (!bytes) return;
     136                 :             : 
     137                 :       42104 :     j0 = x->input[0];
     138                 :       42104 :     j1 = x->input[1];
     139                 :       42104 :     j2 = x->input[2];
     140                 :       42104 :     j3 = x->input[3];
     141                 :       42104 :     j4 = x->input[4];
     142                 :       42104 :     j5 = x->input[5];
     143                 :       42104 :     j6 = x->input[6];
     144                 :       42104 :     j7 = x->input[7];
     145                 :       42104 :     j8 = x->input[8];
     146                 :       42104 :     j9 = x->input[9];
     147                 :       42104 :     j10 = x->input[10];
     148                 :       42104 :     j11 = x->input[11];
     149                 :       42104 :     j12 = x->input[12];
     150                 :       42104 :     j13 = x->input[13];
     151                 :       42104 :     j14 = x->input[14];
     152                 :       42104 :     j15 = x->input[15];
     153                 :             : 
     154                 :      863366 :     for (;;) {
     155         [ +  + ]:      863366 :         if (bytes < 64) {
     156         [ +  + ]:      866632 :             for (i = 0; i < bytes; ++i)
     157                 :      831513 :                 tmp[i] = m[i];
     158                 :       35119 :             m = tmp;
     159                 :       35119 :             ctarget = c;
     160                 :       35119 :             c = tmp;
     161                 :       35119 :         }
     162                 :      863366 :         x0 = j0;
     163                 :      863366 :         x1 = j1;
     164                 :      863366 :         x2 = j2;
     165                 :      863366 :         x3 = j3;
     166                 :      863366 :         x4 = j4;
     167                 :      863366 :         x5 = j5;
     168                 :      863366 :         x6 = j6;
     169                 :      863366 :         x7 = j7;
     170                 :      863366 :         x8 = j8;
     171                 :      863366 :         x9 = j9;
     172                 :      863366 :         x10 = j10;
     173                 :      863366 :         x11 = j11;
     174                 :      863366 :         x12 = j12;
     175                 :      863366 :         x13 = j13;
     176                 :      863366 :         x14 = j14;
     177                 :      863366 :         x15 = j15;
     178         [ +  + ]:     9497026 :         for (i = 20; i > 0; i -= 2) {
     179                 :     8633660 :             QUARTERROUND(x0, x4, x8, x12)
     180                 :     8633660 :             QUARTERROUND(x1, x5, x9, x13)
     181                 :     8633660 :             QUARTERROUND(x2, x6, x10, x14)
     182                 :     8633660 :             QUARTERROUND(x3, x7, x11, x15)
     183                 :     8633660 :             QUARTERROUND(x0, x5, x10, x15)
     184                 :     8633660 :             QUARTERROUND(x1, x6, x11, x12)
     185                 :     8633660 :             QUARTERROUND(x2, x7, x8, x13)
     186                 :     8633660 :             QUARTERROUND(x3, x4, x9, x14)
     187                 :     8633660 :         }
     188                 :      863366 :         x0 = PLUS(x0, j0);
     189                 :      863366 :         x1 = PLUS(x1, j1);
     190                 :      863366 :         x2 = PLUS(x2, j2);
     191                 :      863366 :         x3 = PLUS(x3, j3);
     192                 :      863366 :         x4 = PLUS(x4, j4);
     193                 :      863366 :         x5 = PLUS(x5, j5);
     194                 :      863366 :         x6 = PLUS(x6, j6);
     195                 :      863366 :         x7 = PLUS(x7, j7);
     196                 :      863366 :         x8 = PLUS(x8, j8);
     197                 :      863366 :         x9 = PLUS(x9, j9);
     198                 :      863366 :         x10 = PLUS(x10, j10);
     199                 :      863366 :         x11 = PLUS(x11, j11);
     200                 :      863366 :         x12 = PLUS(x12, j12);
     201                 :      863366 :         x13 = PLUS(x13, j13);
     202                 :      863366 :         x14 = PLUS(x14, j14);
     203                 :      863366 :         x15 = PLUS(x15, j15);
     204                 :             : 
     205                 :      863366 :         x0 = XOR(x0, U8TO32_LITTLE(m + 0));
     206                 :      863366 :         x1 = XOR(x1, U8TO32_LITTLE(m + 4));
     207                 :      863366 :         x2 = XOR(x2, U8TO32_LITTLE(m + 8));
     208                 :      863366 :         x3 = XOR(x3, U8TO32_LITTLE(m + 12));
     209                 :      863366 :         x4 = XOR(x4, U8TO32_LITTLE(m + 16));
     210                 :      863366 :         x5 = XOR(x5, U8TO32_LITTLE(m + 20));
     211                 :      863366 :         x6 = XOR(x6, U8TO32_LITTLE(m + 24));
     212                 :      863366 :         x7 = XOR(x7, U8TO32_LITTLE(m + 28));
     213                 :      863366 :         x8 = XOR(x8, U8TO32_LITTLE(m + 32));
     214                 :      863366 :         x9 = XOR(x9, U8TO32_LITTLE(m + 36));
     215                 :      863366 :         x10 = XOR(x10, U8TO32_LITTLE(m + 40));
     216                 :      863366 :         x11 = XOR(x11, U8TO32_LITTLE(m + 44));
     217                 :      863366 :         x12 = XOR(x12, U8TO32_LITTLE(m + 48));
     218                 :      863366 :         x13 = XOR(x13, U8TO32_LITTLE(m + 52));
     219                 :      863366 :         x14 = XOR(x14, U8TO32_LITTLE(m + 56));
     220                 :      863366 :         x15 = XOR(x15, U8TO32_LITTLE(m + 60));
     221                 :             : 
     222                 :      863366 :         j12 = PLUSONE(j12);
     223         [ +  + ]:      863366 :         if (!j12) {
     224                 :        2494 :             j13 = PLUSONE(j13);
     225                 :             :             /* stopping at 2^70 bytes per nonce is user's responsibility */
     226                 :        2494 :         }
     227                 :             : 
     228                 :      863366 :         U32TO8_LITTLE(c + 0, x0);
     229                 :      863366 :         U32TO8_LITTLE(c + 4, x1);
     230                 :      863366 :         U32TO8_LITTLE(c + 8, x2);
     231                 :      863366 :         U32TO8_LITTLE(c + 12, x3);
     232                 :      863366 :         U32TO8_LITTLE(c + 16, x4);
     233                 :      863366 :         U32TO8_LITTLE(c + 20, x5);
     234                 :      863366 :         U32TO8_LITTLE(c + 24, x6);
     235                 :      863366 :         U32TO8_LITTLE(c + 28, x7);
     236                 :      863366 :         U32TO8_LITTLE(c + 32, x8);
     237                 :      863366 :         U32TO8_LITTLE(c + 36, x9);
     238                 :      863366 :         U32TO8_LITTLE(c + 40, x10);
     239                 :      863366 :         U32TO8_LITTLE(c + 44, x11);
     240                 :      863366 :         U32TO8_LITTLE(c + 48, x12);
     241                 :      863366 :         U32TO8_LITTLE(c + 52, x13);
     242                 :      863366 :         U32TO8_LITTLE(c + 56, x14);
     243                 :      863366 :         U32TO8_LITTLE(c + 60, x15);
     244                 :             : 
     245         [ +  + ]:      863366 :         if (bytes <= 64) {
     246         [ +  + ]:       42104 :             if (bytes < 64) {
     247         [ +  + ]:      866632 :                 for (i = 0; i < bytes; ++i)
     248                 :      831513 :                     ctarget[i] = c[i];
     249                 :       35119 :             }
     250                 :       42104 :             x->input[12] = j12;
     251                 :       42104 :             x->input[13] = j13;
     252                 :       42104 :             return;
     253                 :             :         }
     254                 :      821262 :         bytes -= 64;
     255                 :      821262 :         c += 64;
     256                 :      821262 :         m += 64;
     257                 :             :     }
     258                 :       60448 : }
     259                 :             : 
     260                 :       44008 : void ECRYPT_keystream_bytes(ECRYPT_ctx* x, u8* stream, u32 bytes)
     261                 :             : {
     262                 :       44008 :     u32 i;
     263         [ +  + ]:    24194585 :     for (i = 0; i < bytes; ++i)
     264                 :    24150577 :         stream[i] = 0;
     265                 :       44008 :     ECRYPT_encrypt_bytes(x, stream, stream, bytes);
     266                 :       44008 : }
     267                 :             : 
     268   [ +  -  +  - ]:         374 : FUZZ_TARGET(crypto_diff_fuzz_chacha20)
     269                 :             : {
     270                 :         371 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     271                 :             : 
     272                 :         371 :     ECRYPT_ctx ctx;
     273                 :             : 
     274                 :         371 :     const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
     275                 :         371 :     ChaCha20 chacha20{MakeByteSpan(key)};
     276                 :         371 :     ECRYPT_keysetup(&ctx, key.data(), key.size() * 8, 0);
     277                 :             : 
     278                 :             :     // ECRYPT_keysetup() doesn't set the counter and nonce to 0 while SetKey() does
     279                 :             :     static const uint8_t iv[8] = {0, 0, 0, 0, 0, 0, 0, 0};
     280                 :         371 :     ChaCha20::Nonce96 nonce{0, 0};
     281                 :         371 :     uint32_t counter{0};
     282                 :         371 :     ECRYPT_ivsetup(&ctx, iv);
     283                 :             : 
     284   [ +  -  +  +  :       89977 :     LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 3000) {
                   +  + ]
     285         [ +  - ]:       89606 :         CallOneOf(
     286                 :             :             fuzzed_data_provider,
     287                 :       92050 :             [&] {
     288                 :        2444 :                 const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
     289                 :        2444 :                 chacha20.SetKey(MakeByteSpan(key));
     290                 :        2444 :                 nonce = {0, 0};
     291                 :        2444 :                 counter = 0;
     292                 :        2444 :                 ECRYPT_keysetup(&ctx, key.data(), key.size() * 8, 0);
     293                 :             :                 // ECRYPT_keysetup() doesn't set the counter and nonce to 0 while SetKey() does
     294                 :        2444 :                 uint8_t iv[8] = {0, 0, 0, 0, 0, 0, 0, 0};
     295                 :        2444 :                 ECRYPT_ivsetup(&ctx, iv);
     296                 :        2444 :             },
     297                 :      116320 :             [&] {
     298                 :       26714 :                 uint32_t iv_prefix = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     299                 :       26714 :                 uint64_t iv = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
     300                 :       26714 :                 nonce = {iv_prefix, iv};
     301                 :       26714 :                 counter = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     302                 :       26714 :                 chacha20.Seek(nonce, counter);
     303                 :       26714 :                 ctx.input[12] = counter;
     304                 :       26714 :                 ctx.input[13] = iv_prefix;
     305                 :       26714 :                 ctx.input[14] = iv;
     306                 :       26714 :                 ctx.input[15] = iv >> 32;
     307                 :       26714 :             },
     308                 :      133614 :             [&] {
     309                 :       44008 :                 uint32_t integralInRange = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
     310         [ +  - ]:       44008 :                 std::vector<uint8_t> output(integralInRange);
     311                 :       44008 :                 chacha20.Keystream(MakeWritableByteSpan(output));
     312         [ +  - ]:       44008 :                 std::vector<uint8_t> djb_output(integralInRange);
     313                 :       44008 :                 ECRYPT_keystream_bytes(&ctx, djb_output.data(), djb_output.size());
     314   [ +  -  +  - ]:       44008 :                 assert(output == djb_output);
     315                 :             :                 // DJB's version seeks forward to a multiple of 64 bytes after every operation. Correct for that.
     316                 :       44008 :                 uint32_t old_counter = counter;
     317                 :       44008 :                 counter += (integralInRange + 63) >> 6;
     318         [ +  + ]:       44008 :                 if (counter < old_counter) ++nonce.first;
     319         [ +  + ]:       44008 :                 if (integralInRange & 63) {
     320                 :       23873 :                     chacha20.Seek(nonce, counter);
     321                 :       23873 :                 }
     322         [ +  - ]:       44008 :                 assert(counter == ctx.input[12]);
     323                 :       44008 :             },
     324                 :      106046 :             [&] {
     325                 :       16440 :                 uint32_t integralInRange = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
     326         [ +  - ]:       16440 :                 std::vector<uint8_t> output(integralInRange);
     327                 :       16440 :                 const std::vector<uint8_t> input = ConsumeFixedLengthByteVector(fuzzed_data_provider, output.size());
     328                 :       16440 :                 chacha20.Crypt(MakeByteSpan(input), MakeWritableByteSpan(output));
     329         [ +  - ]:       16440 :                 std::vector<uint8_t> djb_output(integralInRange);
     330                 :       16440 :                 ECRYPT_encrypt_bytes(&ctx, input.data(), djb_output.data(), input.size());
     331   [ +  -  +  - ]:       16440 :                 assert(output == djb_output);
     332                 :             :                 // DJB's version seeks forward to a multiple of 64 bytes after every operation. Correct for that.
     333                 :       16440 :                 uint32_t old_counter = counter;
     334                 :       16440 :                 counter += (integralInRange + 63) >> 6;
     335         [ +  + ]:       16440 :                 if (counter < old_counter) ++nonce.first;
     336         [ +  + ]:       16440 :                 if (integralInRange & 63) {
     337                 :       11246 :                     chacha20.Seek(nonce, counter);
     338                 :       11246 :                 }
     339         [ +  - ]:       16440 :                 assert(counter == ctx.input[12]);
     340                 :       16440 :             });
     341                 :       89606 :     }
     342                 :         371 : }
        

Generated by: LCOV version 2.0-1