Skip to main content

加密\u hmac \u sha256不工作

1年前

加密\u hmac \u sha256不工作

Posted by托马斯Donhauser0 points 3个答复
0票以上

Hello Support!

文档中显示的代码不起作用。

uint8_t txt[]=“你什么都不想要?”uint8_t key[]=“Jefe”uint8_t hmac[32];crypto_hmac_ctx_t hmac_status=crypto_hmac_sha256(txt,28,key,4,hmac,crypto_hmac_NO_OPTION,100);if(hmac\u status==-1){//在100个滴答声内获取AES/哈希引擎失败}else{//一切正常}

执行在crypto\u hmac.c的第64行ad\u crypto\u acquire\u aes\u hash处停止(超时)。

crypto_hmac_ctx_t crypto_hmac_sha256 (const uint8_t*text, size_t text_sz, const uint8_t *key, size_t key_sz, uint8_t *hmac, unsigned int flags, OS_TICK_TIME timeout) { int i; uint8_t k0[CRYPTO_HMAC_SHA256_BLOCK_SZ]; uint8_t h_k0_ipad[CRYPTO_HMAC_SHA256_BLOCK_SZ]; uint8_t *h_k0_opad = NULL; if (ad_crypto_acquire_aes_hash(timeout) != OS_MUTEX_TAKEN) { return -1; } /* Enable engine clock and adapter event handling */ ad_crypto_enable_aes_hash_event();

我找不到任何其他显示正确用法的代码示例,请您帮助我。如果您能告诉我如何计算SHA256 resp,我将不胜感激。HMAC公司。

I also tried to solve it be some different pre-configs executed by

hw_aes_hash_init(&s);//s:hw\u aes\u hash\u设置

but had no luck so far.

谢谢您,

托马斯

accepted answer!

1年前

PM\U对话框

Hi Thomas,

感谢您在我们的公共蓝牙论坛上提出的问题。为了使用crypto\u hmac库,我遵循了Doxygen SDK文件,但是我无法重现您提到的问题。我已经在non-BLE(freertos\u retarger)和BLE(BLE\u peripheral)SDK示例中进行了测试。请确保您遵循了正确的步骤。附上你会发现一些屏幕截图为您提供方便。

A] 硬件/软件设置

- DA1468x Pro-DK with DA14683 daughterboard are used

- DA1468x_DA15xxx_SDK_1.0.14.1081

- ble_peripheral is used which is under projects\dk_apps\features\ble_peripheral SDK path

B] Importing the crypto_hmac library

1.打开SmartSnippets Studio(SSS)并导入外围设备。我建议使用干净的SDK安装或从现有SDK中删除.metadata。

2. Expand the ble_peripheral folder from the “Project Explorer” window and right-click to the ble_peripheral\sdk. Add a new sub-folder under this folder. Refer to B_1.png photo.

3.在弹出窗口中展开“高级”选项。请参考Bèu 2.png。您应该将crypto-SDK文件夹添加为链接资源。

4. The project tree should be like B_3.png and the crypto driver should be have added.

5.右键单击“项目资源管理器”窗口中的ble\u外围设备并转到“属性”

6.加密驱动程序可能设置在绝对路径位置(B_6.png)

7. Change the path location as B_7.png

8.在includes中添加加密驱动程序,如B_8_i.png、B_8_ii.png和B_8_iii.png所示

C] SW modifications

1.将crypto\u hmac.h包含到ble\u peripheral\u task.c中

#包括“crypto\u hmac.h”

2.在custom\u config\u qspi.h中:

#定义dg\u configUSE\u HW\u AES\u HASH(1)

3.在for(;)之前的ble_peripheral_任务中循环添加以下代码段。

uint8_t txt[]=“你什么都不想要?”;uint8\u t key[]=“Jefe”;uint8_t hmac[32];crypto_hmac_ctx_t hmac_status=crypto_hmac_sha256(txt,28,key,4,hmac,crypto_hmac_NO_OPTION,100);if(hmac_status==-1){printf(“Failed”);}else{printf(“Succeeded”);}

4.构建DA14683-00-Debug-QSPI配置项目。

5.启动调试会话,并在“Succeeded”中添加断点

6. The breakpoint should be hit and the crypto_hmac_sha256() should be executed successfully.

请遵循所描述的程序,并让我知道你的反馈。

谢谢,下午好

附件 大小
加密.zip 379.21千字节

1年前

托马斯Donhauser 0 points

Hi PM_Dialog,

You are perfektly right and it works as described and expected. And finally it works in my App. What was wrong?

我做了一些AES-256解密前hmac计算和分析没有呼叫hw_aes_hash_disable_clock();afterwards. This leads to the crash. After adding this function call everthing works fine.

The only thing I'm not sure is if my parameters setting is right and if the order of hw_aes_hash...() functions is ok too?

s.mode = HW_AES_CBC; s.aesKeySize = HW_AES_256; s.aesKeyExpand = HW_AES_DO_NOT_PERFORM_KEY_EXPANSION; s.aesKeys = (uint32)&aesKey; s.aesIvCtrblk_0_31 = 0x01020304; s.aesIvCtrblk_32_63 = 0x05060708; s.aesIvCtrblk_64_95 = 0x090A0B0C; s.aesIvCtrblk_96_127 = 0x0D0E0FA1; s.aesWriteBackAll = true; s.moreDataToCome = false; s.sourceAddress = 0; s.destinationAddress = 0; s.dataSize = 0; s.enableInterrupt = false; s.callback = 0; uint8_t e2[] = { 0x26,0x9d,0xf3,0x2b,0x94,0xe9,0xcd,0xde, 0x7a,0xd2,0x6f,0xe8,0x7a,0x7e,0x8d,0xa8, 0x1f,0xe1,0xca,0xb7,0xbf,0xa7,0x4f,0xc7, 0x17,0xf3,0xd4,0x2f,0xbb,0xe6,0xc3,0xc7, 0x39,0x7a,0x92,0xfe,0x54,0x98,0xc7,0xf8, 0x2f,0x13,0x93,0x15,0x3a,0x43,0xb0,0x3e }; s.aesDirection = HW_AES_DECRYPT; s.sourceAddress = (uint32)&e2; s.destinationAddress = (uint32)&d; s.dataSize = 32; hw_aes_hash_init(&s); hw_aes_hash_start(); while(hw_aes_hash_is_active()){}; hw_aes_hash_disable_clock(); // *************** HMAC on SHA256 ***************** uint8_t txt[] = "what do ya want for nothing?"; uint8_t key[] = "Jefe"; uint8_t hma2c[32]; memset(hma2c,0,sizeof(hma2c)); crypto_hmac_ctx_t hmac_status = crypto_hmac_sha256(txt, 28, key, 4, hma2c, CRYPTO_HMAC_NO_OPTION, 100); if (hmac_status == -1) { printf("Failed"); } else { printf("Succeeded"); }

Thank you!

托马斯

1年前

PM\U对话框

Hi Thomas,

很高兴这是工作,并感谢您接受我的回答。我看到你创建了一个新的论坛线程(链接如下)。我会用新的回答你。

https://support.dialog-semiconductor.com/forums/post/dialog-smartbond-bluetooth-low-energy-%E2%80%93-software/aes-cbc-no-padding

谢谢,下午好