crypto_hmac_sha256 not working

⚠️
Hi there.. thanks for coming to the forums. Exciting news! we’re now in the process of moving to our new forum platform that will offer better functionality and is contained within the main Dialog website. All posts and accounts have been migrated. We’re now accepting traffic on the new forum only - please POST any new threads at//www.xmece.com/support. We’ll be fixing bugs / optimising the searching and tagging over the coming days.
4 posts / 0 new
Last post
Thomas Donhauser
Offline
Last seen:2 months 3 weeks ago
加入:2019-10-20 10:45
crypto_hmac_sha256 not working

Hello Support!

The code as shown in documentation does not work.

uint8_t txt[] = "what do ya want for nothing?" 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_status == -1) { // Failed to acquire the AES/HASH engine within 100 ticks } else { // Everything is fine }

Execution stops on line 64 in crypto_hmac.c at ad_crypto_acquire_aes_hash(timeout).

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();

我找不到任何其他tha代码示例t show the right usage and I ask you to help me. I very much appreciate if you could show me how to calculate a SHA256 resp. HMAC.

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

hw_aes_hash_init(&s); // s : hw_aes_hash_setup

but had no luck so far.

Thank you,

Thomas

Device:
PM_Dialog
Offline
Last seen:3 days 16 hours ago
Staff
加入:2018-02-08 11:03
Hi Thomas,

Hi Thomas,

Thanks for your question on our public Bluetooth forums. I have followed the Doxygen SDK files in order to use the crypto_hmac library, but I am not able to reproduce the issue that you mentioned. I’ve tested in both non-BLE (freertos_retarger) and BLE (ble_peripheral) SDK examples. Please make sure that you are following the correct steps. Attached you will find some screenshots for your convenience.

A] HW/SW Setup

- 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.Open the SmartSnippets Studio (SSS) and import the ble_peripheral. I would recommend using a clean SDK install or delete the .metadata from the existing one.

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. In the pop-up window expand the advanced option. Please refer to B_2.png. You should add the crypto SDK folder as linked resources.

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

5. Right-click in the ble_peripheral from the “Project Explorer” window and go to the “Properties”

6. The crypto driver might be set in absolute path location (B_6.png)

7. Change the path location as B_7.png

8. Add the crypto driver in the includes as shown in B_8_i.png, B_8_ii.png and B_8_iii.png

C] SW modifications

1.包括crypto_hmac.h ble_peripheral_task.c

#include "crypto_hmac.h"

2. In custom_config_qspi.h :

#define dg_configUSE_HW_AES_HASH (1)

3. In the ble_peripheral_task before the for(;;) loop add the following code snippet.

uint8_t txt[] = "what do ya want for nothing?"; 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_status == -1) { printf("Failed"); } else { printf("Succeeded"); }

4. Build the project for DA14683-00-Debug-QSPI configuration.

5. Initiate a debug session and the add a break point into the “Succeeded”

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

Please follow the procedure described and let me know with your feedback.

Thanks, PM_Dialog

Attachment:
Thomas Donhauser
Offline
Last seen:2 months 3 weeks ago
加入:2019-10-20 10:45
Hi PM_Dialog,

Hi PM_Dialog,

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

I do some AES-256 decryption before hmac calculation anddid notcallhw_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!

Thomas

PM_Dialog
Offline
Last seen:3 days 16 hours ago
Staff
加入:2018-02-08 11:03
Hi Thomas,

Hi Thomas,

Glad it is working and thank you for accepting my answer. I saw that you created a new forum thread (link is provided below). I will respond you in the new one.

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

Thanks, PM_Dialog