/*
* Copyright (c) 2013-2025 Rony Shapiro <ronys@pwsafe.org>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
// AES.cpp
// C++ wrapper of AES for PasswordSafe, based on LibTomCrypt by
// Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
//-----------------------------------------------------------------------------

#include "AES.h"
#include "bitops.h"
#include "../Util.h"

#define LTC_CLEAN_STACK

enum class CryptStatus {
  OK = 0,           /* Result OK */
  GENERIC_ERROR,    /* Generic Error */
  NOP,              /* Not a failure but no operation was performed */

  INVALID_KEYSIZE,  /* Invalid key size given */
  INVALID_ROUNDS,   /* Invalid number of rounds */
  FAIL_TESTVECTOR,  /* Algorithm failed test vectors */

  BUFFER_OVERFLOW,  /* Not enough space for output */

  MEM,              /* Out of memory */

  INVALID_ARG      /* Generic invalid argument */
};

/** 
Implementation of AES (Rijndael) by Tom St Denis 
*/

#include "aes_tab.c"

static ulong32 setup_mix(ulong32 temp)
{
   return (Te4_3[byteN(temp, 2)]) ^
          (Te4_2[byteN(temp, 1)]) ^
          (Te4_1[byteN(temp, 0)]) ^
          (Te4_0[byteN(temp, 3)]);
}

#ifndef ENCRYPT_ONLY
#ifdef LTC_SMALL_CODE
static ulong32 setup_mix2(ulong32 temp)
{
   return Td0(255 & Te4[byteN(temp, 3)]) ^
          Td1(255 & Te4[byteN(temp, 2)]) ^
          Td2(255 & Te4[byteN(temp, 1)]) ^
          Td3(255 & Te4[byteN(temp, 0)]);
}
#endif
#endif

 /**
    Initialize the AES (Rijndael) block cipher
    @param key The symmetric key you wish to pass
    @param keylen The key length in bytes
    @param num_rounds The number of rounds desired (0 for default)
    @param skey The key in as scheduled by this function.
    @return CryptStatus::OK if successful
 */
static CryptStatus rijndael_setup(const unsigned char *key, int keylen,
                                  int num_rounds, rijndael_key *skey)
{
    int i, j;
    ulong32 temp, *rk;
#ifndef ENCRYPT_ONLY
    ulong32 *rrk;
#endif    
    ASSERT(key  != nullptr);
    ASSERT(skey != nullptr);
  
    if (keylen != 16 && keylen != 24 && keylen != 32) {
       return CryptStatus::INVALID_KEYSIZE;
    }
    
    if (num_rounds != 0 && num_rounds != (10 + ((keylen/8)-2)*2)) {
       return CryptStatus::INVALID_ROUNDS;
    }
    
    skey->Nr = 10 + ((keylen/8)-2)*2;
        
    /* setup the forward key */
    i                 = 0;
    rk                = skey->eK;
    LOAD32H(rk[0], key     );
    LOAD32H(rk[1], key +  4);
    LOAD32H(rk[2], key +  8);
    LOAD32H(rk[3], key + 12);
    if (keylen == 16) {
        j = 44;
        for (;;) {
            temp  = rk[3];
            rk[4] = rk[0] ^ setup_mix(temp) ^ rcon[i];
            rk[5] = rk[1] ^ rk[4];
            rk[6] = rk[2] ^ rk[5];
            rk[7] = rk[3] ^ rk[6];
            if (++i == 10) {
               break;
            }
            rk += 4;
        }
    } else if (keylen == 24) {
        j = 52;   
        LOAD32H(rk[4], key + 16);
        LOAD32H(rk[5], key + 20);
        for (;;) {
        #ifdef _MSC_VER
            temp = skey->eK[rk - skey->eK + 5]; 
        #else
            temp = rk[5];
        #endif
            rk[ 6] = rk[ 0] ^ setup_mix(temp) ^ rcon[i];
            rk[ 7] = rk[ 1] ^ rk[ 6];
            rk[ 8] = rk[ 2] ^ rk[ 7];
            rk[ 9] = rk[ 3] ^ rk[ 8];
            if (++i == 8) {
                break;
            }
            rk[10] = rk[ 4] ^ rk[ 9];
            rk[11] = rk[ 5] ^ rk[10];
            rk += 6;
        }
    } else if (keylen == 32) {
        j = 60;
        LOAD32H(rk[4], key + 16);
        LOAD32H(rk[5], key + 20);
        LOAD32H(rk[6], key + 24);
        LOAD32H(rk[7], key + 28);
        for (;;) {
        #ifdef _MSC_VER
            temp = skey->eK[rk - skey->eK + 7]; 
        #else
            temp = rk[7];
        #endif
            rk[ 8] = rk[ 0] ^ setup_mix(temp) ^ rcon[i];
            rk[ 9] = rk[ 1] ^ rk[ 8];
            rk[10] = rk[ 2] ^ rk[ 9];
            rk[11] = rk[ 3] ^ rk[10];
            if (++i == 7) {
                break;
            }
            temp = rk[11];
            rk[12] = rk[ 4] ^ setup_mix(RORc(temp, 8));
            rk[13] = rk[ 5] ^ rk[12];
            rk[14] = rk[ 6] ^ rk[13];
            rk[15] = rk[ 7] ^ rk[14];
            rk += 8;
        }
    } else {
       /* this can't happen */
       return CryptStatus::GENERIC_ERROR;
    }

#ifndef ENCRYPT_ONLY    
    /* setup the inverse key now */
    rk   = skey->dK;
    rrk  = skey->eK + j - 4; 
    
    /* apply the inverse MixColumn transform to all round keys but the first and the last: */
    /* copy first */
    *rk++ = *rrk++;
    *rk++ = *rrk++;
    *rk++ = *rrk++;
    *rk   = *rrk;
    rk -= 3; rrk -= 3;
    
    for (i = 1; i < skey->Nr; i++) {
        rrk -= 4;
        rk  += 4;
    #ifdef LTC_SMALL_CODE        
        temp = rrk[0];
        rk[0] = setup_mix2(temp);
        temp = rrk[1];
        rk[1] = setup_mix2(temp);
        temp = rrk[2];
        rk[2] = setup_mix2(temp);
        temp = rrk[3];
        rk[3] = setup_mix2(temp);
     #else
        temp = rrk[0];
        rk[0] =
            Tks0[byteN(temp, 3)] ^
            Tks1[byteN(temp, 2)] ^
            Tks2[byteN(temp, 1)] ^
            Tks3[byteN(temp, 0)];
        temp = rrk[1];
        rk[1] =
            Tks0[byteN(temp, 3)] ^
            Tks1[byteN(temp, 2)] ^
            Tks2[byteN(temp, 1)] ^
            Tks3[byteN(temp, 0)];
        temp = rrk[2];
        rk[2] =
            Tks0[byteN(temp, 3)] ^
            Tks1[byteN(temp, 2)] ^
            Tks2[byteN(temp, 1)] ^
            Tks3[byteN(temp, 0)];
        temp = rrk[3];
        rk[3] =
            Tks0[byteN(temp, 3)] ^
            Tks1[byteN(temp, 2)] ^
            Tks2[byteN(temp, 1)] ^
            Tks3[byteN(temp, 0)];
      #endif            
     
    }

    /* copy last */
    rrk -= 4;
    rk  += 4;
    *rk++ = *rrk++;
    *rk++ = *rrk++;
    *rk++ = *rrk++;
    *rk   = *rrk;
#endif /* ENCRYPT_ONLY */

    return CryptStatus::OK;   
}

/**
  Encrypts a block of text with AES
  @param pt The input plaintext (16 bytes)
  @param ct The output ciphertext (16 bytes)
  @param skey The key as scheduled
  @return CryptStatus::OK if successful
*/
#ifdef LTC_CLEAN_STACK
static CryptStatus _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
                                         const rijndael_key *skey) 
#else
static CryptStatus rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
                                        const rijndael_key *skey)
#endif
{
    ulong32 s0, s1, s2, s3, t0, t1, t2, t3;
    const ulong32 *rk;
    int Nr, r;
   
    ASSERT(pt != nullptr);
    ASSERT(ct != nullptr);
    ASSERT(skey != nullptr);
    
    Nr = skey->Nr;
    rk = skey->eK;
    
    /*
     * map byteN array block to cipher state
     * and add initial round key:
     */
    LOAD32H(s0, pt      ); s0 ^= rk[0];
    LOAD32H(s1, pt  +  4); s1 ^= rk[1];
    LOAD32H(s2, pt  +  8); s2 ^= rk[2];
    LOAD32H(s3, pt  + 12); s3 ^= rk[3];

#ifdef LTC_SMALL_CODE

    for (r = 0; ; r++) {
        rk += 4;
        t0 =
            Te0(byteN(s0, 3)) ^
            Te1(byteN(s1, 2)) ^
            Te2(byteN(s2, 1)) ^
            Te3(byteN(s3, 0)) ^
            rk[0];
        t1 =
            Te0(byteN(s1, 3)) ^
            Te1(byteN(s2, 2)) ^
            Te2(byteN(s3, 1)) ^
            Te3(byteN(s0, 0)) ^
            rk[1];
        t2 =
            Te0(byteN(s2, 3)) ^
            Te1(byteN(s3, 2)) ^
            Te2(byteN(s0, 1)) ^
            Te3(byteN(s1, 0)) ^
            rk[2];
        t3 =
            Te0(byteN(s3, 3)) ^
            Te1(byteN(s0, 2)) ^
            Te2(byteN(s1, 1)) ^
            Te3(byteN(s2, 0)) ^
            rk[3];
        if (r == Nr-2) { 
           break;
        }
        s0 = t0; s1 = t1; s2 = t2; s3 = t3;
    }
    rk += 4;

#else

    /*
     * Nr - 1 full rounds:
     */
    r = Nr >> 1;
    for (;;) {
        t0 =
            Te0(byteN(s0, 3)) ^
            Te1(byteN(s1, 2)) ^
            Te2(byteN(s2, 1)) ^
            Te3(byteN(s3, 0)) ^
            rk[4];
        t1 =
            Te0(byteN(s1, 3)) ^
            Te1(byteN(s2, 2)) ^
            Te2(byteN(s3, 1)) ^
            Te3(byteN(s0, 0)) ^
            rk[5];
        t2 =
            Te0(byteN(s2, 3)) ^
            Te1(byteN(s3, 2)) ^
            Te2(byteN(s0, 1)) ^
            Te3(byteN(s1, 0)) ^
            rk[6];
        t3 =
            Te0(byteN(s3, 3)) ^
            Te1(byteN(s0, 2)) ^
            Te2(byteN(s1, 1)) ^
            Te3(byteN(s2, 0)) ^
            rk[7];

        rk += 8;
        if (--r == 0) {
            break;
        }

        s0 =
            Te0(byteN(t0, 3)) ^
            Te1(byteN(t1, 2)) ^
            Te2(byteN(t2, 1)) ^
            Te3(byteN(t3, 0)) ^
            rk[0];
        s1 =
            Te0(byteN(t1, 3)) ^
            Te1(byteN(t2, 2)) ^
            Te2(byteN(t3, 1)) ^
            Te3(byteN(t0, 0)) ^
            rk[1];
        s2 =
            Te0(byteN(t2, 3)) ^
            Te1(byteN(t3, 2)) ^
            Te2(byteN(t0, 1)) ^
            Te3(byteN(t1, 0)) ^
            rk[2];
        s3 =
            Te0(byteN(t3, 3)) ^
            Te1(byteN(t0, 2)) ^
            Te2(byteN(t1, 1)) ^
            Te3(byteN(t2, 0)) ^
            rk[3];
    }

#endif

    /*
     * apply last round and
     * map cipher state to byte array block:
     */
    s0 =
        (Te4_3[byteN(t0, 3)]) ^
        (Te4_2[byteN(t1, 2)]) ^
        (Te4_1[byteN(t2, 1)]) ^
        (Te4_0[byteN(t3, 0)]) ^
        rk[0];
    STORE32H(s0, ct);
    s1 =
        (Te4_3[byteN(t1, 3)]) ^
        (Te4_2[byteN(t2, 2)]) ^
        (Te4_1[byteN(t3, 1)]) ^
        (Te4_0[byteN(t0, 0)]) ^
        rk[1];
    STORE32H(s1, ct+4);
    s2 =
        (Te4_3[byteN(t2, 3)]) ^
        (Te4_2[byteN(t3, 2)]) ^
        (Te4_1[byteN(t0, 1)]) ^
        (Te4_0[byteN(t1, 0)]) ^
        rk[2];
    STORE32H(s2, ct+8);
    s3 =
        (Te4_3[byteN(t3, 3)]) ^
        (Te4_2[byteN(t0, 2)]) ^
        (Te4_1[byteN(t1, 1)]) ^
        (Te4_0[byteN(t2, 0)]) ^ 
        rk[3];
    STORE32H(s3, ct+12);

    return CryptStatus::OK;
}

#ifdef LTC_CLEAN_STACK
CryptStatus rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
                                 const rijndael_key *skey) 
{
   CryptStatus err = _rijndael_ecb_encrypt(pt, ct, skey);
   burnStack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
   return err;
}
#endif

#ifndef ENCRYPT_ONLY 

/**
  Decrypts a block of text with AES
  @param ct The input ciphertext (16 bytes)
  @param pt The output plaintext (16 bytes)
  @param skey The key as scheduled 
  @return CryptStatus::OK if successful
*/
#ifdef LTC_CLEAN_STACK
static CryptStatus _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
                                         const rijndael_key *skey) 
#else
static CryptStatus rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
                                        const rijndael_key *skey)
#endif
{
    ulong32 s0, s1, s2, s3, t0, t1, t2, t3;
    const ulong32 *rk;
    int Nr, r;

    ASSERT(pt != nullptr);
    ASSERT(ct != nullptr);
    ASSERT(skey != nullptr);
    
    Nr = skey->Nr;
    rk = skey->dK;

    /*
     * map byte array block to cipher state
     * and add initial round key:
     */
    LOAD32H(s0, ct      ); s0 ^= rk[0];
    LOAD32H(s1, ct  +  4); s1 ^= rk[1];
    LOAD32H(s2, ct  +  8); s2 ^= rk[2];
    LOAD32H(s3, ct  + 12); s3 ^= rk[3];

#ifdef LTC_SMALL_CODE
    for (r = 0; ; r++) {
        rk += 4;
        t0 =
            Td0(byteN(s0, 3)) ^
            Td1(byteN(s3, 2)) ^
            Td2(byteN(s2, 1)) ^
            Td3(byteN(s1, 0)) ^
            rk[0];
        t1 =
            Td0(byteN(s1, 3)) ^
            Td1(byteN(s0, 2)) ^
            Td2(byteN(s3, 1)) ^
            Td3(byteN(s2, 0)) ^
            rk[1];
        t2 =
            Td0(byteN(s2, 3)) ^
            Td1(byteN(s1, 2)) ^
            Td2(byteN(s0, 1)) ^
            Td3(byteN(s3, 0)) ^
            rk[2];
        t3 =
            Td0(byteN(s3, 3)) ^
            Td1(byteN(s2, 2)) ^
            Td2(byteN(s1, 1)) ^
            Td3(byteN(s0, 0)) ^
            rk[3];
        if (r == Nr-2) {
           break; 
        }
        s0 = t0; s1 = t1; s2 = t2; s3 = t3;
    }
    rk += 4;

#else       

    /*
     * Nr - 1 full rounds:
     */
    r = Nr >> 1;
    for (;;) {

        t0 =
            Td0(byteN(s0, 3)) ^
            Td1(byteN(s3, 2)) ^
            Td2(byteN(s2, 1)) ^
            Td3(byteN(s1, 0)) ^
            rk[4];
        t1 =
            Td0(byteN(s1, 3)) ^
            Td1(byteN(s0, 2)) ^
            Td2(byteN(s3, 1)) ^
            Td3(byteN(s2, 0)) ^
            rk[5];
        t2 =
            Td0(byteN(s2, 3)) ^
            Td1(byteN(s1, 2)) ^
            Td2(byteN(s0, 1)) ^
            Td3(byteN(s3, 0)) ^
            rk[6];
        t3 =
            Td0(byteN(s3, 3)) ^
            Td1(byteN(s2, 2)) ^
            Td2(byteN(s1, 1)) ^
            Td3(byteN(s0, 0)) ^
            rk[7];

        rk += 8;
        if (--r == 0) {
            break;
        }

        s0 =
            Td0(byteN(t0, 3)) ^
            Td1(byteN(t3, 2)) ^
            Td2(byteN(t2, 1)) ^
            Td3(byteN(t1, 0)) ^
            rk[0];
        s1 =
            Td0(byteN(t1, 3)) ^
            Td1(byteN(t0, 2)) ^
            Td2(byteN(t3, 1)) ^
            Td3(byteN(t2, 0)) ^
            rk[1];
        s2 =
            Td0(byteN(t2, 3)) ^
            Td1(byteN(t1, 2)) ^
            Td2(byteN(t0, 1)) ^
            Td3(byteN(t3, 0)) ^
            rk[2];
        s3 =
            Td0(byteN(t3, 3)) ^
            Td1(byteN(t2, 2)) ^
            Td2(byteN(t1, 1)) ^
            Td3(byteN(t0, 0)) ^
            rk[3];
    }
#endif

    /*
     * apply last round and
     * map cipher state to byte array block:
     */
    s0 =
        (Td4[byteN(t0, 3)] & 0xff000000) ^
        (Td4[byteN(t3, 2)] & 0x00ff0000) ^
        (Td4[byteN(t2, 1)] & 0x0000ff00) ^
        (Td4[byteN(t1, 0)] & 0x000000ff) ^
        rk[0];
    STORE32H(s0, pt);
    s1 =
        (Td4[byteN(t1, 3)] & 0xff000000) ^
        (Td4[byteN(t0, 2)] & 0x00ff0000) ^
        (Td4[byteN(t3, 1)] & 0x0000ff00) ^
        (Td4[byteN(t2, 0)] & 0x000000ff) ^
        rk[1];
    STORE32H(s1, pt+4);
    s2 =
        (Td4[byteN(t2, 3)] & 0xff000000) ^
        (Td4[byteN(t1, 2)] & 0x00ff0000) ^
        (Td4[byteN(t0, 1)] & 0x0000ff00) ^
        (Td4[byteN(t3, 0)] & 0x000000ff) ^
        rk[2];
    STORE32H(s2, pt+8);
    s3 =
        (Td4[byteN(t3, 3)] & 0xff000000) ^
        (Td4[byteN(t2, 2)] & 0x00ff0000) ^
        (Td4[byteN(t1, 1)] & 0x0000ff00) ^
        (Td4[byteN(t0, 0)] & 0x000000ff) ^
        rk[3];
    STORE32H(s3, pt+12);

    return CryptStatus::OK;
}

#ifdef LTC_CLEAN_STACK
static CryptStatus rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
                                        const rijndael_key *skey) 
{
   CryptStatus err = _rijndael_ecb_decrypt(ct, pt, skey);
   burnStack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
   return err;
}
#endif
#endif /* ENCRYPT_ONLY */

AES::AES(const unsigned char* key, int keylen)
{
  CryptStatus status = rijndael_setup(key, keylen, 0, &key_schedule);

  ASSERT(status == CryptStatus::OK);
  if (status != CryptStatus::OK)
    throw status;
}

AES::~AES()
{
  trashMemory(&key_schedule, sizeof(key_schedule));
}

void AES::Encrypt(const unsigned char *in, unsigned char *out) const
{
  rijndael_ecb_encrypt(in, out, &key_schedule);
}

void AES::Decrypt(const unsigned char *in, unsigned char *out) const
{
  rijndael_ecb_decrypt(in, out, &key_schedule);
}
