/*
 * =====================================================================================
 *
 *       Filename:  logEncryptor.c
 *
 *    Description:  log encryption using RSA and AES. 
 *
 *        Version:  1.1
 *        Created:  08/23/2016 12:08:02 AM
 *       Compiler:  armcc
 *
 *         Author:  Dongwook Shim (), dw.shim@samsung.com
 *        Company:  Samsung Electronics
 *
 *        Copyright (c) 2016 by Samsung Electronics, All rights reserved. 
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "commonConfig.h"
#include "teeCryptoApi.h"
#include "log.h"
#include "keyManager.h"
#include "secMemoryManager.h"
#include "circularQueue.h"
#include "logEncryptor.h"

const uint8_t logEncPubKey[] = 
{
	// ASN.1 header.
	0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,	0xF7, 0x0D, 0x01, 0x01, 
	0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00,	0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01,
	0x00,

	// Modulus.
	0xB7, 0xA9, 0x75, 0x32, 0xA6, 0xAC, 0x2C, 0x2F, 0xAF, 0xAB, 0x4D, 0xC2, 0x2B, 0xC5, 0x6B, 0x58,
	0xB7, 0xB0, 0x64, 0x5C, 0x54, 0x1E, 0xF2, 0xCD, 0x0D, 0xD0, 0xF1, 0xD3, 0x16, 0xCE, 0xB5, 0x68,
	0x23, 0x04, 0x06, 0x09, 0x89, 0xD0, 0x5E, 0x22, 0x7F, 0x59, 0x56, 0x11, 0x56, 0x94, 0xAA, 0xD8,
	0xAA, 0x23, 0xCB, 0x93, 0xEF, 0xFF, 0x68, 0x16, 0xE0, 0x6F, 0x61, 0x36, 0xAE, 0xF0, 0xFC, 0xE6,
	0x60, 0x28, 0x10, 0x76, 0x3E, 0x36, 0xA4, 0xA2, 0x62, 0x7B, 0x24, 0x3D, 0x31, 0x70, 0xB1, 0x1D,
	0xD5, 0x89, 0xF3, 0x84, 0x7F, 0xC4, 0x80, 0x21, 0x09, 0xA8, 0xB9, 0xC5, 0xF9, 0x1D, 0x44, 0x19,
	0x77, 0x6F, 0x53, 0x88, 0x6A, 0xFB, 0x05, 0x08, 0x44, 0x0D, 0x9E, 0x51, 0xB5, 0x02, 0xB0, 0x90,
	0x0F, 0x4D, 0xEC, 0x2A, 0x9E, 0x47, 0x7C, 0x38, 0x25, 0x0B, 0xE6, 0x65, 0xB4, 0x79, 0xD9, 0x52,
	0x64, 0xEF, 0x0F, 0x38, 0x69, 0x8E, 0x4A, 0x1E, 0x32, 0x7C, 0xEC, 0x5E, 0xC0, 0x3B, 0x39, 0x90,
	0xBD, 0x12, 0x92, 0xDC, 0x8A, 0xAC, 0x1D, 0xA8, 0x07, 0xBD, 0x64, 0x93, 0xEB, 0x40, 0x6E, 0xE6,
	0x13, 0x05, 0x2A, 0x0C, 0x27, 0x1E, 0x4D, 0xFA, 0x6E, 0x6A, 0xEA, 0xB3, 0x7C, 0x82, 0x06, 0x6C,
	0xA8, 0xBB, 0x2B, 0xF1, 0x0E, 0x79, 0x9A, 0x9B, 0x7D, 0x17, 0x67, 0x9D, 0x77, 0x47, 0xC7, 0xAF,
	0x50, 0x25, 0x37, 0xA4, 0xF9, 0xD3, 0x40, 0xBA, 0x39, 0xAB, 0x9F, 0x54, 0x7E, 0x66, 0x87, 0x71,
	0x44, 0xCB, 0xEA, 0x07, 0xCB, 0xB4, 0xA0, 0xDF, 0x38, 0x8A, 0x4E, 0x3A, 0xE2, 0x8B, 0x11, 0x49,
	0x7F, 0x59, 0x0B, 0x92, 0x22, 0x7E, 0x6D, 0xBC, 0x5F, 0x5B, 0x53, 0xF0, 0x4B, 0xFB, 0xD7, 0x7D,
	0x95, 0x7A, 0x74, 0x5F, 0xEA, 0x04, 0x0E, 0x8E, 0x28, 0x11, 0x42, 0x39, 0xCE, 0x69, 0xED, 0x55,

	// ASN.1 public exponent.
	0x02,0x03, 0x01, 0x00, 0x01
};

int32_t logEncrypt(uint8_t *cipherText, uint32_t cipherTextLen)
{
	int32_t ret = NOT_ERROR, len = 0;
	char buf[QUEUE_MAX_SIZE];
	uint8_t *pCipherText = cipherText, *plainText = NULL, *pPlainText = NULL;
	uint32_t *pCipherTextLen = NULL, plainTextLen = 0;
	AesKeyData keyData;
	KEY *key = NULL;

	plainTextLen = getQueueSize() + 1;	// Include NULL.
	
	if((plainText = secMemoryManagerMalloc(plainTextLen)) == NULL)
		return ERR_TA_NOT_ENOUGH_MEMORY;

	pPlainText = plainText;

	while(!isQueueEmpty() || pPlainText - plainText < plainTextLen - 1)
	{
		memset(buf, 0, sizeof(buf));
		ret = dequeueData(buf, sizeof(buf));
		if((len = snprintf((char *)pPlainText, plainTextLen - (pPlainText - plainText), "%s", buf)) < 0 || ret != len)
		{
			TEE_LOG("Failed to print buffer with error %d.", len);
			ret = ERR_TA_BUFFER_OVERFLOW;
			goto cleanup;
		}
		pPlainText += ret;
	}

	if(cipherText == NULL || plainTextLen == 0 || plainTextLen + strlen(MAGIC_CODE) +
			sizeof(uint32_t) + AES_BLOCK_SIZE + ENCRYPTED_KEY_SIZE > cipherTextLen)
	{
		TEE_LOG("Invalid parameters(%d %d).", (int)plainTextLen, cipherTextLen);
		ret = ERR_TA_INVALID_ARGUMENT;
		goto cleanup;
	}

	memset(&keyData, 0, sizeof(keyData));
	memset(cipherText, 0, cipherTextLen);

	memcpy(pCipherText, MAGIC_CODE, strlen(MAGIC_CODE));
	pCipherText += strlen(MAGIC_CODE);

	pCipherTextLen = (uint32_t *)pCipherText;
	pCipherText += sizeof(cipherTextLen);

	if((ret = getRandBlock(keyData.aesRawKey, sizeof(keyData.aesRawKey))) != sizeof(keyData.aesRawKey))
	{
		TEE_LOG("Failed to generate aes key.");
		goto cleanup;
	}

	if((ret = getRandBlock(keyData.iv, sizeof(keyData.iv))) != sizeof(keyData.iv))
	{
		TEE_LOG("Failed to generate iv.");
		goto cleanup;
	}

	if((ret = aes256CbcEncrypt((const uint8_t *)plainText, plainTextLen, keyData.iv, pCipherText, &cipherTextLen,
					keyData.aesRawKey, sizeof(keyData.aesRawKey), AES_MODE_ENCRYPT)) != NOT_ERROR)
	{
		TEE_LOG("Failed to set key with error %d.", ret);
		ret = ERR_TA_CRYPTO_API_ERROR;
		goto cleanup;
	}

	*pCipherTextLen = cipherTextLen;
	pCipherText += cipherTextLen;

	if((key = KEY_new(RSA_KEY)) == NULL)
	{
		TEE_LOG("Failed to allocate key.");
		ret = ERR_TA_NOT_ENOUGH_MEMORY;
		goto cleanup;
	}

	if((ret = KEY_populate_keys(key, logEncPubKey, sizeof(logEncPubKey), NULL, 0)) != NOT_ERROR)
	{
		TEE_LOG("Failed to populate key for encryptor.");
		goto cleanup;
	}

	if((ret = KEY_public_encrypt(key, sizeof(keyData), (uint8_t *)&keyData, pCipherText, RSA_PKCS1_PADDING)) != NOT_ERROR)
	{
		TEE_LOG("Failed to encrypt log with error %d.", ret);
		goto cleanup;
	}

	ret = strlen(MAGIC_CODE) + sizeof(cipherTextLen) + cipherTextLen + ENCRYPTED_KEY_SIZE;

cleanup:
	if(key)
		KEY_free(key);

	if(plainText)
		secMemoryManagerFree(plainText);

	return ret;
}
