/*---------------------------------------------------------------------------*
  Project:  Lotus AES-CCM

  Copyright (C)Nintendo Co., Ltd.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

 *---------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "aes.h"

#include "aes_ccm_oneblock.h"

static const u8 FLAGS_SIZE = 1;

u8    zero[BLOCK_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
u8    ctrBuffer[ BLOCK_SIZE ];
u8    cbcBuffer[ BLOCK_SIZE ];
u8    temp[BLOCK_SIZE];

const u32   REVERSED_BLOCK_SIZE =   ((BLOCK_SIZE << 24) & 0xFF000000)
                            | ((BLOCK_SIZE <<  8) & 0x00FF0000)
                            | ((BLOCK_SIZE >>  8) & 0x0000FF00)
                            | ((BLOCK_SIZE >> 24) & 0x000000FF);


void ExclusiveOr(u8 dst[BLOCK_SIZE], const u8 srcA[BLOCK_SIZE], const u8 srcB[BLOCK_SIZE])
{
    const u32* pSrcA32 = (u32*)srcA;
    const u32* pSrcB32 = (u32*)srcB;
    u32* pDst32  = (u32*)dst;
    int i;

    for( i = 0; i < (BLOCK_SIZE / 4); ++i )
    {
        pDst32[i] = (pSrcA32[i] ^ pSrcB32[i]);
    }
}

void Increment(u8 counter[BLOCK_SIZE])
{
    int i;
    for( i = BLOCK_SIZE - 1; i > 0; --i )
    {
        if( ++counter[i] != 0 )
        {
            break;
        }
    }
}

void ProcessCbcOneBlock(u8 iv[BLOCK_SIZE], const u8 src[BLOCK_SIZE], const u8 key[KEY_SIZE])
{
    AES_cbc_encrypt(src, iv, BLOCK_SIZE, key, iv, AES_ENCRYPT);
}

void ProcessCtrOneBlock(u8 dst[BLOCK_SIZE], u8 counter[BLOCK_SIZE], const u8 src[BLOCK_SIZE], const u8 key[KEY_SIZE])
{
    // We have to use AES-CBC due to the restriction that HW only supports AES-CBC.
    // By setting zero-filled block, AES-CBC acts as just a simple AES encryption.
    u8 iv[BLOCK_SIZE];
    memset(zero, 0, BLOCK_SIZE);
    memcpy(iv, counter, BLOCK_SIZE);
    
    AES_cbc_encrypt(zero, temp, BLOCK_SIZE, key, iv, AES_ENCRYPT);
    ExclusiveOr(dst, src ,temp);

    // This function only processes one block, so it doesn't increment counter
}
    
void SimpleCcmDecryptOneBlock(const u8 key[KEY_SIZE], const u8 nonce[NONCE_SIZE],
                              u8 pdata[BLOCK_SIZE], const u8 cdata[BLOCK_SIZE], u8 mac[MAC_SIZE] )
{
    const size_t q = BLOCK_SIZE - FLAGS_SIZE - NONCE_SIZE;

    // Make counter
    memset(ctrBuffer, 0, BLOCK_SIZE);
    ctrBuffer[0] = ((q - 1) & 0x07); // flags
    memcpy(ctrBuffer + FLAGS_SIZE, nonce, NONCE_SIZE);

    // increment counter
    Increment(ctrBuffer);

    // Make header
    memset(temp, 0, BLOCK_SIZE);

    temp[0] = ( (0x00) // adata size
                | ((((MAC_SIZE - 2) / 2) & 0x07) << 3)
                | ((q - 1) & 0x07) );

    memcpy(temp + BLOCK_SIZE - sizeof(REVERSED_BLOCK_SIZE), &REVERSED_BLOCK_SIZE, sizeof(REVERSED_BLOCK_SIZE));
    memcpy(temp + FLAGS_SIZE, nonce, NONCE_SIZE);

    memset(cbcBuffer, 0, BLOCK_SIZE);

    // process cbc block to generate initial CBC data
    ProcessCbcOneBlock(cbcBuffer, (const u8*)temp, key);

    // Decrypt one block (Process Cdata)
    ProcessCtrOneBlock(pdata, ctrBuffer, cdata, key);

    ProcessCbcOneBlock(cbcBuffer, (const u8*)pdata, key);

    // Generate mac
    memset(ctrBuffer + BLOCK_SIZE - q, 0, q);
    ProcessCtrOneBlock(mac, ctrBuffer, cbcBuffer, key);

}

