#pragma once

#include <stddef.h>
#include <switch.h>

namespace Crypto
{
    #define RSA_2048_BYTES 0x100
    #define RSA_2048_BITS (RSA_2048_BYTES*8)
    static const unsigned char NCAHeaderSignature[0x100] = { /* Fixed RSA key used to validate NCA signature 0. */
        0xBF, 0xBE, 0x40, 0x6C, 0xF4, 0xA7, 0x80, 0xE9, 0xF0, 0x7D, 0x0C, 0x99, 0x61, 0x1D, 0x77, 0x2F,
        0x96, 0xBC, 0x4B, 0x9E, 0x58, 0x38, 0x1B, 0x03, 0xAB, 0xB1, 0x75, 0x49, 0x9F, 0x2B, 0x4D, 0x58,
        0x34, 0xB0, 0x05, 0xA3, 0x75, 0x22, 0xBE, 0x1A, 0x3F, 0x03, 0x73, 0xAC, 0x70, 0x68, 0xD1, 0x16,
        0xB9, 0x04, 0x46, 0x5E, 0xB7, 0x07, 0x91, 0x2F, 0x07, 0x8B, 0x26, 0xDE, 0xF6, 0x00, 0x07, 0xB2,
        0xB4, 0x51, 0xF8, 0x0D, 0x0A, 0x5E, 0x58, 0xAD, 0xEB, 0xBC, 0x9A, 0xD6, 0x49, 0xB9, 0x64, 0xEF,
        0xA7, 0x82, 0xB5, 0xCF, 0x6D, 0x70, 0x13, 0xB0, 0x0F, 0x85, 0xF6, 0xA9, 0x08, 0xAA, 0x4D, 0x67,
        0x66, 0x87, 0xFA, 0x89, 0xFF, 0x75, 0x90, 0x18, 0x1E, 0x6B, 0x3D, 0xE9, 0x8A, 0x68, 0xC9, 0x26,
        0x04, 0xD9, 0x80, 0xCE, 0x3F, 0x5E, 0x92, 0xCE, 0x01, 0xFF, 0x06, 0x3B, 0xF2, 0xC1, 0xA9, 0x0C,
        0xCE, 0x02, 0x6F, 0x16, 0xBC, 0x92, 0x42, 0x0A, 0x41, 0x64, 0xCD, 0x52, 0xB6, 0x34, 0x4D, 0xAE,
        0xC0, 0x2E, 0xDE, 0xA4, 0xDF, 0x27, 0x68, 0x3C, 0xC1, 0xA0, 0x60, 0xAD, 0x43, 0xF3, 0xFC, 0x86,
        0xC1, 0x3E, 0x6C, 0x46, 0xF7, 0x7C, 0x29, 0x9F, 0xFA, 0xFD, 0xF0, 0xE3, 0xCE, 0x64, 0xE7, 0x35,
        0xF2, 0xF6, 0x56, 0x56, 0x6F, 0x6D, 0xF1, 0xE2, 0x42, 0xB0, 0x83, 0x40, 0xA5, 0xC3, 0x20, 0x2B,
        0xCC, 0x9A, 0xAE, 0xCA, 0xED, 0x4D, 0x70, 0x30, 0xA8, 0x70, 0x1C, 0x70, 0xFD, 0x13, 0x63, 0x29,
        0x02, 0x79, 0xEA, 0xD2, 0xA7, 0xAF, 0x35, 0x28, 0x32, 0x1C, 0x7B, 0xE6, 0x2F, 0x1A, 0xAA, 0x40,
        0x7E, 0x32, 0x8C, 0x27, 0x42, 0xFE, 0x82, 0x78, 0xEC, 0x0D, 0xEB, 0xE6, 0x83, 0x4B, 0x6D, 0x81,
        0x04, 0x40, 0x1A, 0x9E, 0x9A, 0x67, 0xF6, 0x72, 0x29, 0xFA, 0x04, 0xF0, 0x9D, 0xE4, 0xF4, 0x03    
    };

    class Keys
    {
    public:
        Keys()
        {
            u8 kek[0x10] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            splCryptoGenerateAesKek(headerKekSource, 0, 0, kek);
            splCryptoGenerateAesKey(kek, headerKeySource, headerKey);
            splCryptoGenerateAesKey(kek, headerKeySource + 0x10, headerKey + 0x10);
        }

        u8 headerKekSource[0x10] = { 0x1F, 0x12, 0x91, 0x3A, 0x4A, 0xCB, 0xF0, 0x0D, 0x4C, 0xDE, 0x3A, 0xF6, 0xD5, 0x23, 0x88, 0x2A };
        u8 headerKeySource[0x20] = { 0x5A, 0x3E, 0xD8, 0x4F, 0xDE, 0xC0, 0xD8, 0x26, 0x31, 0xF7, 0xE2, 0x5D, 0x19, 0x7B, 0xF5, 0xD0, 0x1C, 0x9B, 0x7B, 0xFA, 0xF6, 0x28, 0x18, 0x3D, 0x71, 0xF6, 0x4D, 0x73, 0xF1, 0x50, 0xB9, 0xD2 };

        u8 headerKey[0x20] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    };

    void calculateMGF1andXOR(unsigned char* data, size_t data_size, const void* source, size_t source_size);
    bool rsa2048PssVerify(const void *data, size_t len, const unsigned char *signature, const unsigned char *modulus);

    template<class T>
    T swapEndian(T s)
    {
        T result;
        u8* dest = (u8*)&result;
        u8* src = (u8*)&s;
        for (unsigned int i = 0; i < sizeof(s); i++)
        {
            dest[i] = src[sizeof(s) - i - 1];
        }
        return result;
    }
    class AesCtr
    {
    public:
        AesCtr() : m_high(0), m_low(0)
        {
        }

        AesCtr(u64 iv) : m_high(swapEndian(iv)), m_low(0)
        {
        }

        u64& high() { return m_high; }
        u64& low() { return m_low; }
    private:
        u64 m_high;
        u64 m_low;
    };


    class Aes128Ctr
    {
    public:
        Aes128Ctr(const u8* key, const AesCtr& iv)
        {
            counter = iv;
            aes128CtrContextCreate(&ctx, key, &iv);
            seek(0);
        }

        virtual ~Aes128Ctr()
        {
        }

        void seek(u64 offset)
        {
            counter.low() = swapEndian(offset >> 4);
            aes128CtrContextResetCtr(&ctx, &counter);
        }

        void encrypt(void *dst, const void *src, size_t l)
        {
            aes128CtrCrypt(&ctx, dst, src, l);
        }

        void decrypt(void *dst, const void *src, size_t l)
        {
            encrypt(dst, src, l);
        }
    protected:
        AesCtr counter;

        Aes128CtrContext ctx;
    };

    class AesXtr
    {
    public:
        AesXtr(const u8* key, bool is_encryptor)
        {
            aes128XtsContextCreate(&ctx, key, key + 0x10, is_encryptor);
        }

        virtual ~AesXtr()
        {
        }

        void encrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size)
        {
            for (size_t i = 0; i < l; i += sector_size)
            {
                aes128XtsContextResetSector(&ctx, sector++, true);
                aes128XtsEncrypt(&ctx, dst, src, sector_size);

                dst = (u8*)dst + sector_size;
                src = (const u8*)src + sector_size;
            }
        }

        void decrypt(void *dst, const void *src, size_t l, size_t sector, size_t sector_size)
        {
            for (size_t i = 0; i < l; i += sector_size)
            {
                aes128XtsContextResetSector(&ctx, sector++, true);
                aes128XtsDecrypt(&ctx, dst, src, sector_size);

                dst = (u8*)dst + sector_size;
                src = (const u8*)src + sector_size;
            }
        }
    protected:
        Aes128XtsContext ctx;
    };
}
