LCOV - code coverage report
Current view: top level - src/crypto - aes.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 75 75
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 14 14
Branches: 83.3 % 30 25

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2016-2019 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/aes.h>
       6                 :             : 
       7                 :             : #include <string.h>
       8                 :             : 
       9                 :             : extern "C" {
      10                 :             : #include <crypto/ctaes/ctaes.c>
      11                 :             : }
      12                 :             : 
      13                 :        5349 : AES256Encrypt::AES256Encrypt(const unsigned char key[32])
      14                 :             : {
      15                 :        5349 :     AES256_init(&ctx, key);
      16                 :        5349 : }
      17                 :             : 
      18                 :        5349 : AES256Encrypt::~AES256Encrypt()
      19                 :             : {
      20                 :        5349 :     memset(&ctx, 0, sizeof(ctx));
      21                 :        5349 : }
      22                 :             : 
      23                 :       11451 : void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
      24                 :             : {
      25                 :       11451 :     AES256_encrypt(&ctx, 1, ciphertext, plaintext);
      26                 :       11451 : }
      27                 :             : 
      28                 :        6459 : AES256Decrypt::AES256Decrypt(const unsigned char key[32])
      29                 :             : {
      30                 :        6459 :     AES256_init(&ctx, key);
      31                 :        6459 : }
      32                 :             : 
      33                 :        6459 : AES256Decrypt::~AES256Decrypt()
      34                 :             : {
      35                 :        6459 :     memset(&ctx, 0, sizeof(ctx));
      36                 :        6459 : }
      37                 :             : 
      38                 :       14675 : void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
      39                 :             : {
      40                 :       14675 :     AES256_decrypt(&ctx, 1, plaintext, ciphertext);
      41                 :       14675 : }
      42                 :             : 
      43                 :             : 
      44                 :             : template <typename T>
      45                 :        5472 : static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
      46                 :             : {
      47                 :        5472 :     int written = 0;
      48                 :        5472 :     int padsize = size % AES_BLOCKSIZE;
      49                 :             :     unsigned char mixed[AES_BLOCKSIZE];
      50                 :             : 
      51   [ +  -  +  - ]:        5472 :     if (!data || !size || !out)
      52                 :             :         return 0;
      53                 :             : 
      54         [ +  + ]:        5472 :     if (!pad && padsize != 0)
      55                 :             :         return 0;
      56                 :             : 
      57                 :        5412 :     memcpy(mixed, iv, AES_BLOCKSIZE);
      58                 :             : 
      59                 :             :     // Write all but the last block
      60         [ +  + ]:       11454 :     while (written + AES_BLOCKSIZE <= size) {
      61         [ +  + ]:      102714 :         for (int i = 0; i != AES_BLOCKSIZE; i++)
      62                 :       96672 :             mixed[i] ^= *data++;
      63                 :        6042 :         enc.Encrypt(out + written, mixed);
      64                 :        6042 :         memcpy(mixed, out + written, AES_BLOCKSIZE);
      65                 :        6042 :         written += AES_BLOCKSIZE;
      66                 :             :     }
      67         [ +  + ]:        5412 :     if (pad) {
      68                 :             :         // For all that remains, pad each byte with the value of the remaining
      69                 :             :         // space. If there is none, pad by a full block.
      70         [ +  + ]:       30124 :         for (int i = 0; i != padsize; i++)
      71                 :       24720 :             mixed[i] ^= *data++;
      72         [ +  + ]:       67148 :         for (int i = padsize; i != AES_BLOCKSIZE; i++)
      73                 :       61744 :             mixed[i] ^= AES_BLOCKSIZE - padsize;
      74                 :        5404 :         enc.Encrypt(out + written, mixed);
      75                 :        5404 :         written += AES_BLOCKSIZE;
      76                 :             :     }
      77                 :             :     return written;
      78                 :             : }
      79                 :             : 
      80                 :             : template <typename T>
      81                 :        6522 : static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
      82                 :             : {
      83                 :        6522 :     int written = 0;
      84                 :        6522 :     bool fail = false;
      85                 :        6522 :     const unsigned char* prev = iv;
      86                 :             : 
      87   [ +  -  +  - ]:        6522 :     if (!data || !size || !out)
      88                 :             :         return 0;
      89                 :             : 
      90         [ +  - ]:        6522 :     if (size % AES_BLOCKSIZE != 0)
      91                 :             :         return 0;
      92                 :             : 
      93                 :             :     // Decrypt all data. Padding will be checked in the output.
      94         [ +  + ]:       21192 :     while (written != size) {
      95                 :       14670 :         dec.Decrypt(out, data + written);
      96         [ +  + ]:      249390 :         for (int i = 0; i != AES_BLOCKSIZE; i++)
      97                 :      234720 :             *out++ ^= prev[i];
      98                 :       14670 :         prev = data + written;
      99                 :       14670 :         written += AES_BLOCKSIZE;
     100                 :             :     }
     101                 :             : 
     102                 :             :     // When decrypting padding, attempt to run in constant-time
     103         [ +  + ]:        6522 :     if (pad) {
     104                 :             :         // If used, padding size is the value of the last decrypted byte. For
     105                 :             :         // it to be valid, It must be between 1 and AES_BLOCKSIZE.
     106                 :        6514 :         unsigned char padsize = *--out;
     107                 :        6514 :         fail = !padsize | (padsize > AES_BLOCKSIZE);
     108                 :             : 
     109                 :             :         // If not well-formed, treat it as though there's no padding.
     110                 :        6514 :         padsize *= !fail;
     111                 :             : 
     112                 :             :         // All padding must equal the last byte otherwise it's not well-formed
     113         [ +  + ]:      110738 :         for (int i = AES_BLOCKSIZE; i != 0; i--)
     114                 :      104224 :             fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
     115                 :             : 
     116                 :        6514 :         written -= padsize;
     117                 :             :     }
     118                 :        6522 :     return written * !fail;
     119                 :             : }
     120                 :             : 
     121                 :        5344 : AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
     122                 :        5344 :     : enc(key), pad(padIn)
     123                 :             : {
     124                 :        5344 :     memcpy(iv, ivIn, AES_BLOCKSIZE);
     125                 :        5344 : }
     126                 :             : 
     127                 :        5472 : int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
     128                 :             : {
     129                 :        5472 :     return CBCEncrypt(enc, iv, data, size, pad, out);
     130                 :             : }
     131                 :             : 
     132                 :        5344 : AES256CBCEncrypt::~AES256CBCEncrypt()
     133                 :             : {
     134                 :        5344 :     memset(iv, 0, sizeof(iv));
     135                 :        5344 : }
     136                 :             : 
     137                 :        6454 : AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
     138                 :        6454 :     : dec(key), pad(padIn)
     139                 :             : {
     140                 :        6454 :     memcpy(iv, ivIn, AES_BLOCKSIZE);
     141                 :        6454 : }
     142                 :             : 
     143                 :             : 
     144                 :        6522 : int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
     145                 :             : {
     146                 :        6522 :     return CBCDecrypt(dec, iv, data, size, pad, out);
     147                 :             : }
     148                 :             : 
     149                 :        6454 : AES256CBCDecrypt::~AES256CBCDecrypt()
     150                 :             : {
     151                 :        6454 :     memset(iv, 0, sizeof(iv));
     152                 :        6454 : }
        

Generated by: LCOV version 2.0-1