LCOV - code coverage report
Current view: top level - src/crypto - aes.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 79 79
Test Date: 2026-04-07 04:59:12 Functions: 100.0 % 14 14
Branches: 79.4 % 34 27

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

Generated by: LCOV version 2.0-1