LCOV - code coverage report
Current view: top level - src/crypto - aes.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 84 87 96.6 %
Date: 2022-04-21 14:51:19 Functions: 14 14 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 29 36 80.6 %

           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                 :            : AES256Encrypt::AES256Encrypt(const unsigned char key[32])
#      14                 :       4682 : {
#      15                 :       4682 :     AES256_init(&ctx, key);
#      16                 :       4682 : }
#      17                 :            : 
#      18                 :            : AES256Encrypt::~AES256Encrypt()
#      19                 :       4682 : {
#      20                 :       4682 :     memset(&ctx, 0, sizeof(ctx));
#      21                 :       4682 : }
#      22                 :            : 
#      23                 :            : void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
#      24                 :       9500 : {
#      25                 :       9500 :     AES256_encrypt(&ctx, 1, ciphertext, plaintext);
#      26                 :       9500 : }
#      27                 :            : 
#      28                 :            : AES256Decrypt::AES256Decrypt(const unsigned char key[32])
#      29                 :       8737 : {
#      30                 :       8737 :     AES256_init(&ctx, key);
#      31                 :       8737 : }
#      32                 :            : 
#      33                 :            : AES256Decrypt::~AES256Decrypt()
#      34                 :       8737 : {
#      35                 :       8737 :     memset(&ctx, 0, sizeof(ctx));
#      36                 :       8737 : }
#      37                 :            : 
#      38                 :            : void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
#      39                 :      21559 : {
#      40                 :      21559 :     AES256_decrypt(&ctx, 1, plaintext, ciphertext);
#      41                 :      21559 : }
#      42                 :            : 
#      43                 :            : 
#      44                 :            : template <typename T>
#      45                 :            : static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
#      46                 :       4928 : {
#      47                 :       4928 :     int written = 0;
#      48                 :       4928 :     int padsize = size % AES_BLOCKSIZE;
#      49                 :       4928 :     unsigned char mixed[AES_BLOCKSIZE];
#      50                 :            : 
#      51 [ -  + ][ -  + ]:       4928 :     if (!data || !size || !out)
#                 [ -  + ]
#      52                 :          0 :         return 0;
#      53                 :            : 
#      54 [ +  + ][ +  + ]:       4928 :     if (!pad && padsize != 0)
#      55                 :        120 :         return 0;
#      56                 :            : 
#      57                 :       4808 :     memcpy(mixed, iv, AES_BLOCKSIZE);
#      58                 :            : 
#      59                 :            :     // Write all but the last block
#      60         [ +  + ]:       9506 :     while (written + AES_BLOCKSIZE <= size) {
#      61         [ +  + ]:      79866 :         for (int i = 0; i != AES_BLOCKSIZE; i++)
#      62                 :      75168 :             mixed[i] ^= *data++;
#      63                 :       4698 :         enc.Encrypt(out + written, mixed);
#      64                 :       4698 :         memcpy(mixed, out + written, AES_BLOCKSIZE);
#      65                 :       4698 :         written += AES_BLOCKSIZE;
#      66                 :       4698 :     }
#      67         [ +  + ]:       4808 :     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         [ +  + ]:      29992 :         for (int i = 0; i != padsize; i++)
#      71                 :      25200 :             mixed[i] ^= *data++;
#      72         [ +  + ]:      56264 :         for (int i = padsize; i != AES_BLOCKSIZE; i++)
#      73                 :      51472 :             mixed[i] ^= AES_BLOCKSIZE - padsize;
#      74                 :       4792 :         enc.Encrypt(out + written, mixed);
#      75                 :       4792 :         written += AES_BLOCKSIZE;
#      76                 :       4792 :     }
#      77                 :       4808 :     return written;
#      78                 :       4928 : }
#      79                 :            : 
#      80                 :            : template <typename T>
#      81                 :            : static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
#      82                 :       8863 : {
#      83                 :       8863 :     int written = 0;
#      84                 :       8863 :     bool fail = false;
#      85                 :       8863 :     const unsigned char* prev = iv;
#      86                 :            : 
#      87 [ -  + ][ -  + ]:       8863 :     if (!data || !size || !out)
#                 [ -  + ]
#      88                 :          0 :         return 0;
#      89                 :            : 
#      90         [ -  + ]:       8863 :     if (size % AES_BLOCKSIZE != 0)
#      91                 :          0 :         return 0;
#      92                 :            : 
#      93                 :            :     // Decrypt all data. Padding will be checked in the output.
#      94         [ +  + ]:      30412 :     while (written != size) {
#      95                 :      21549 :         dec.Decrypt(out, data + written);
#      96         [ +  + ]:     366333 :         for (int i = 0; i != AES_BLOCKSIZE; i++)
#      97                 :     344784 :             *out++ ^= prev[i];
#      98                 :      21549 :         prev = data + written;
#      99                 :      21549 :         written += AES_BLOCKSIZE;
#     100                 :      21549 :     }
#     101                 :            : 
#     102                 :            :     // When decrypting padding, attempt to run in constant-time
#     103         [ +  + ]:       8863 :     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                 :       8847 :         unsigned char padsize = *--out;
#     107                 :       8847 :         fail = !padsize | (padsize > AES_BLOCKSIZE);
#     108                 :            : 
#     109                 :            :         // If not well-formed, treat it as though there's no padding.
#     110                 :       8847 :         padsize *= !fail;
#     111                 :            : 
#     112                 :            :         // All padding must equal the last byte otherwise it's not well-formed
#     113         [ +  + ]:     150399 :         for (int i = AES_BLOCKSIZE; i != 0; i--)
#     114                 :     141552 :             fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
#     115                 :            : 
#     116                 :       8847 :         written -= padsize;
#     117                 :       8847 :     }
#     118                 :       8863 :     return written * !fail;
#     119                 :       8863 : }
#     120                 :            : 
#     121                 :            : AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
#     122                 :            :     : enc(key), pad(padIn)
#     123                 :       4672 : {
#     124                 :       4672 :     memcpy(iv, ivIn, AES_BLOCKSIZE);
#     125                 :       4672 : }
#     126                 :            : 
#     127                 :            : int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
#     128                 :       4928 : {
#     129                 :       4928 :     return CBCEncrypt(enc, iv, data, size, pad, out);
#     130                 :       4928 : }
#     131                 :            : 
#     132                 :            : AES256CBCEncrypt::~AES256CBCEncrypt()
#     133                 :       4672 : {
#     134                 :       4672 :     memset(iv, 0, sizeof(iv));
#     135                 :       4672 : }
#     136                 :            : 
#     137                 :            : AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
#     138                 :            :     : dec(key), pad(padIn)
#     139                 :       8727 : {
#     140                 :       8727 :     memcpy(iv, ivIn, AES_BLOCKSIZE);
#     141                 :       8727 : }
#     142                 :            : 
#     143                 :            : 
#     144                 :            : int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
#     145                 :       8863 : {
#     146                 :       8863 :     return CBCDecrypt(dec, iv, data, size, pad, out);
#     147                 :       8863 : }
#     148                 :            : 
#     149                 :            : AES256CBCDecrypt::~AES256CBCDecrypt()
#     150                 :       8727 : {
#     151                 :       8727 :     memset(iv, 0, sizeof(iv));
#     152                 :       8727 : }

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a