/*
 * (c) Copyright 2021 by Coinkite Inc. This file is covered by license found in COPYING-CC.
 */
#include "basics.h"
#include "console.h"
#include "aes.h"
#include "stm32l4xx_hal.h"
#include "constant_time.h"
#include <string.h>

// aes_init()
//
    void
aes_init(AES_CTX *ctx)
{
    memset(ctx, 0, sizeof(AES_CTX));
}

// aes_add()
//
// - capture more data to be encrypted/decrypted
//
    void
aes_add(AES_CTX *ctx, const uint8_t data_in[], uint32_t len)
{
    memcpy(ctx->pending+ctx->num_pending, data_in, len);
    ctx->num_pending += len;

    ASSERT(ctx->num_pending < sizeof(ctx->pending));
}

// word_pump_bytes()
//
// Return bytes as words. Handles misaligned inputs. Also byte-reverses.
//
    static inline uint32_t
word_pump_bytes(const uint8_t **src)
{
    uint32_t rv;

    if(((uint32_t)src) & 0x3) {
        memcpy(&rv, *src, 4);
    } else {
        rv = *(uint32_t *)(*src);
    }
    (*src) += 4;

    return __REV(rv);
}

// aes_done()
//
// Do the decryption.
//
    void
aes_done(AES_CTX *ctx, uint8_t data_out[], uint32_t len, const uint8_t key[32], const uint8_t nonce[AES_BLOCK_SIZE])
{
    ASSERT(len <= ctx->num_pending);

    // enable clock to block
    __HAL_RCC_AES_CLK_ENABLE();

    // most changes have to be made w/ module disabled
    AES->CR &= ~AES_CR_EN;

    // set the key size and operation mode
    MODIFY_REG(AES->CR, AES_CR_KEYSIZE, CRYP_KEYSIZE_256B);
    MODIFY_REG(AES->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, 
            CRYP_DATATYPE_8B | CRYP_ALGOMODE_ENCRYPT | CRYP_CHAINMODE_AES_CTR);

    // load key and IV values
    const uint8_t *K = key;
    AES->KEYR7 = word_pump_bytes(&K);
    AES->KEYR6 = word_pump_bytes(&K);
    AES->KEYR5 = word_pump_bytes(&K);
    AES->KEYR4 = word_pump_bytes(&K);
    AES->KEYR3 = word_pump_bytes(&K);
    AES->KEYR2 = word_pump_bytes(&K);
    AES->KEYR1 = word_pump_bytes(&K);
    AES->KEYR0 = word_pump_bytes(&K);

    if(nonce) {
        const uint8_t *N = nonce;
        AES->IVR3 = word_pump_bytes(&N);
        AES->IVR2 = word_pump_bytes(&N);
        AES->IVR1 = word_pump_bytes(&N);
        AES->IVR0 = word_pump_bytes(&N);
    } else {
        AES->IVR3 = 0;
        AES->IVR2 = 0;
        AES->IVR1 = 0;
        AES->IVR0 = 0;          // maybe should be byte-swapped one, but whatever
    }

    // Enable the Peripheral
    AES->CR |= AES_CR_EN;

    ASSERT((((uint32_t)&ctx->pending) & 3) == 0);      // safe because of special attr

    uint32_t    *p = (uint32_t *)ctx->pending;
    for(int i=0; i < ctx->num_pending; i += 16) {
        // Write the block to the AES engine
        AES->DINR = *p; p++;
        AES->DINR = *p; p++;
        AES->DINR = *p; p++;
        AES->DINR = *p; p++;

        // Wait for CCF flag to be raised
        while(HAL_IS_BIT_CLR(AES->SR, AES_SR_CCF)) {
            // no timeout -- just 75 cycles?
        }

        // clear CCF flag
        SET_BIT(AES->CR, CRYP_CCF_CLEAR);

        // work in place, overwrite what we just wrote
        uint32_t    *out = p - 4;
        *out = AES->DOUTR; out++;
        *out = AES->DOUTR; out++;
        *out = AES->DOUTR; out++;
        *out = AES->DOUTR;
    }

    memcpy(data_out, ctx->pending, len);

    memset(ctx, 0, sizeof(AES_CTX));

    // reset state of chip block, and leave clock off as well
    __HAL_RCC_AES_CLK_ENABLE();
    __HAL_RCC_AES_FORCE_RESET();
    __HAL_RCC_AES_RELEASE_RESET();
    __HAL_RCC_AES_CLK_DISABLE();
}


#ifndef RELEASE
// aes_selftest()
//
    void
aes_selftest(void)
{
    puts2("AES selftest: ");

/*
    >>> import pyaes
    >>> pyaes.AESModeOfOperationCTR(bytes(32), pyaes.Counter(0)).encrypt(b'Zack')
    b'\x86\xf4\xa3\x13'
*/

    AES_CTX ctx;
    static const uint8_t key[32] = { };
    static const uint8_t nonce[16] = { };
    static const uint8_t msg[4] = "Zack";
    static const uint8_t expect[4] = { 0x86, 0xf4, 0xa3, 0x13 };

    uint8_t tmp[4];

    aes_init(&ctx);
    aes_add(&ctx, msg, 4);
    aes_done(&ctx, tmp, 4, key, nonce);
    ASSERT(check_equal(tmp, expect, 4));

    aes_init(&ctx);
    aes_add(&ctx, expect, 4);
    aes_done(&ctx, tmp, 4, key, nonce);
    ASSERT(check_equal(tmp, msg, 4));

#if 0
    // passes, but big
/*
    >>> pyaes.Counter(0x102030405060708090a0b0c0d0e0f0).value
    [0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240]
    >>> pyaes.AESModeOfOperationCTR(bytes(32), pyaes.Counter(0x102030405060708090a0b0c0d0e0f0)).encrypt(bytes(512)).hex()
*/
    static const uint8_t long_nonce[16] = { 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240 };
    static const uint8_t long_exp[512] = {
0x88, 0x6f, 0x2f, 0x0c, 0xa7, 0x1f, 0x00, 0xed, 0xfd, 0xf8, 0x2d, 0x62, 0xf7, 0xf2, 0xc9,
0xd3, 0x0e, 0xb3, 0xc4, 0xf6, 0xff, 0x3e, 0x3d, 0xc9, 0x6d, 0x26, 0x09, 0x70, 0x97, 0x6a,
0xef, 0x60, 0x59, 0x20, 0xb2, 0xb1, 0x61, 0x92, 0xe1, 0x19, 0xa5, 0xcb, 0x24, 0xf3, 0x1e,
0x9a, 0x82, 0x71, 0x4f, 0x73, 0x21, 0x1e, 0x51, 0x4f, 0xed, 0x05, 0x77, 0xd8, 0x8b, 0x11,
0x5b, 0x38, 0x64, 0xb8, 0xf8, 0x6c, 0xf3, 0xb0, 0xa8, 0x3f, 0xe2, 0x17, 0xd5, 0x6f, 0xc2,
0xc1, 0x2b, 0x9e, 0x9d, 0x0c, 0x89, 0xc1, 0x65, 0x8e, 0xce, 0xb3, 0x21, 0x6d, 0xbb, 0x23,
0x5f, 0x5e, 0x3d, 0x52, 0x61, 0x7f, 0x61, 0x37, 0xd9, 0xea, 0x3a, 0x27, 0xce, 0x80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 0x9c, 0xa7, 0xa1,
0x2f, 0xdb, 0x28, 0x6c, 0xda, 0x16, 0x2f, 0x25, 0x9d, 0x81, 0x44, 0xd7, 0xee, 0x8e, 0x7b,
0x59, 0x8a, 0x24, 0x59, 0x03, 0x4e, 0x48, 0x7a, 0xfd, 0x3c, 0x76, 0x97, 0xa5, 0xac, 0xbb,
0xea, 0x83 };

    aes_init(&ctx);
    ctx.num_pending = 512;
    uint8_t tmp2[512];
    aes_done(&ctx, tmp2, 512, key, long_nonce);
    ASSERT(check_equal(tmp2, long_exp, 512));
#endif


    puts("PASS");
}
#endif

// EOF
