IMXRT学习记录 – DCP加密

栏目: IT技术 · 发布时间: 6年前

内容简介:这个家族最神秘,因为他没有文档,几乎没有,只描述了一下主要特征:使用DCP的几个优点,不用占用CPU,最多可以4个一起并行来,执行速度也比较快,用MbedTLS在500MHz CPU下AES-128加解密上不了1MByte/s速度,但是DCP可以跑到几MByte/s速度,至于能否满足实时加密片外Flash,这个后续讨论.库函数提供三个方式:

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

  • 加密算法:AES-128(ECB/CBC)
  • 摘要算法:SHA-1/SHA-256/CRC-32
  • 从SNVS(OTP)/DCP内部密钥存储槽/常规存储器中选择密钥
  • DCP内部密钥存储槽最多可存储四个AES-128密钥,只能由DCP AES-128引擎读取.
  • DMA
  • 可以配合其他外设实现片外Flash实时解密,这个不需要软件代码,官方发布了专门 工具 和应用手册,所以不会单独说了.

使用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)


以上所述就是小编给大家介绍的《IMXRT学习记录 – DCP加密》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security, Privacy and Commerce, 2nd Edition

Web Security, Privacy and Commerce, 2nd Edition

Simson Garfinkel / O'Reilly Media / 2002-01-15 / USD 44.95

Since the first edition of this classic reference was published, World Wide Web use has exploded and e-commerce has become a daily part of business and personal life. As Web use has grown, so have ......一起来看看 《Web Security, Privacy and Commerce, 2nd Edition》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码