IMXRT学习记录 – DCP加密

/ 0评 / 0

这个家族最神秘,因为他没有文档,几乎没有,只描述了一下主要特征:

使用DCP的几个优点,不用占用CPU,最多可以4个一起并行来,执行速度也比较快,用MbedTLS在500MHz CPU下AES-128加解密上不了1MByte/s速度,但是DCP可以跑到几MByte/s速度,至于能否满足实时加密片外Flash,这个后续讨论.

库函数提供三个方式:

总体初始化:
dcp_config_t dcpConfig;
/*!
* dcpConfig->gatherResidualWrites = true;
* dcpConfig->enableContextCaching = true;
* dcpConfig->enableContextSwitching = true;
* dcpConfig->enableChannnel = kDCP_chEnableAll;
* dcpConfig->enableChannelInterrupt = kDCP_chIntDisable;
*/
DCP_GetDefaultConfig(&dcpConfig);
DCP_Init(DCP, &dcpConfig);
HASH模式:
static const uint8_t message[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
unsigned int length = sizeof(message) - 1;
size_t outLength = sizeof(output);
unsigned char output[32]; //CRC32 => 4 , SHA1 => 20 , SHA256 => 32
dcp_handle_t m_handle;

m_handle.channel = kDCP_Channel0;
m_handle.keySlot = kDCP_KeySlot0;
m_handle.swapConfig = kDCP_NoSwap;

DCP_HASH(DCP, &m_handle, kDCP_Sha1, message, length, output, &outLength);
DCP_HASH(DCP, &m_handle, kDCP_Sha256, message, length, output, &outLength);
DCP_HASH(DCP, &m_handle, kDCP_Crc32, message, length, output, &outLength);
加密模式:
static const uint8_t keyAes128[] __attribute__((aligned)) = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
static const uint8_t plainAes128[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
static const uint8_t ive[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

uint8_t cipher[16];
uint8_t output[16];
dcp_handle_t m_handle;

m_handle.channel = kDCP_Channel0;
m_handle.keySlot = kDCP_KeySlot0;
m_handle.swapConfig = kDCP_NoSwap;

// ECB 模式是没有IVE的.
DCP_AES_SetKey(DCP, &m_handle, keyAes128, 16);
DCP_AES_EncryptEcb(DCP, &m_handle, plainAes128, cipher, 16);
DCP_AES_DecryptEcb(DCP, &m_handle, cipher, output, 16);

// CBC 模式就是有IVE的.
DCP_AES_SetKey(DCP, &m_handle, keyAes128, 16);
DCP_AES_EncryptCbc(DCP, &m_handle, plainAes128, cipher, 16, ive);
DCP_AES_DecryptCbc(DCP, &m_handle, cipher, output, 16, ive);
测试速度(使用PIT测量):
CLOCK_EnableClock(kCLOCK_Pit);

PIT->MCR = 0x00;
PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF;
PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_CHN(1);
PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_TEN(1);

PIT->CHANNEL[0].LDVAL = 0xFFFFFFFF;
PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TEN(1);

for (;;)
{
    current_uptime = 0xFFFFFFFFFFFFFFFF - (((uint64_t)PIT->LTMR64H << 32) + PIT->LTMR64L);
    vTaskDelay(pdMS_TO_TICKS(1000));
}
DCP 测速代码示例:
for (;;)
{
	current_uptime = 0xFFFFFFFFFFFFFFFF - (((uint64_t)PIT->LTMR64H << 32) + PIT->LTMR64L);
	diff_uptime_before = current_uptime;
	DCP_AES_SetKey(DCP, &m_handle, keyAes128, 16);
	for(i = 0;i < 0xFFFF;i++){ DCP_AES_EncryptCbc(DCP, &m_handle, plainAes128, cipher, 16, ive); } current_uptime = 0xFFFFFFFFFFFFFFFF - (((uint64_t)PIT->LTMR64H << 32) + PIT->LTMR64L);
	diff_uptime_after = current_uptime;
	
	diff_uptime = diff_uptime_after - diff_uptime_before;
	
	diff_uptime_milliseconds = ((float)diff_uptime/(float)CLOCK_GetPerClkFreq())*1000;
	__NOP();
}

MbedTLS测试结果:https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-tls-benchmark/ (注意:他的测试结果是KBit.)

DCP 测试结果(62.5MHz IPG):

DCP 1MB AES-128-CBC加密(diff_uptime_milliseconds = 241.15ms)
DCP 1MB AES-128-CBC解密(diff_uptime_milliseconds = 190.84ms)
DCP 1MB AES-128-ECB加密(diff_uptime_milliseconds = 184.00ms)
DCP 1MB AES-128-ECB解密(diff_uptime_milliseconds = 89.65ms)

DCP 1MB SHA1摘要(diff_uptime_milliseconds = 330.55ms)
DCP 1MB SHA256摘要(diff_uptime_milliseconds = 321.13ms)
DCP 1MB CRC32摘要(diff_uptime_milliseconds = 243.01ms)

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注