HWCrypto VS MbedTLS

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: HWCrypto VS MbedTLS

Postby ESP_Angus » Thu Sep 21, 2017 10:52 pm

urbanze wrote:Well... Sucess!! Before, I put only "mbedtls\include\mbedtls\aes.h". Now, I tried put more one "mbedtls\library\aes.c" and WORKED!
I'm glad you got this working. Can you explain exactly what you needed to change?
urbanze wrote: See both libraries: https://i.imgur.com/d3liT0L.png

However, with HWCrypto only, takes ~129uS. MbedTLS with Acell ON takes ~150uS.... List bellow show my tests.
That doesn't seem right. With the code you posted mbedtls_aes_crypt_ecb() is remapped via macro directly to your comparison function esp_aes_crypt_ecb(). So they should be the exact same function when acceleration is enabled.

See here: https://github.com/espressif/esp-idf/bl ... _alt.h#L39

You may need to run a lot more than 4 iterations to even out small differences and "jitter" due to interrupts, task switching, etc.


Angus

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: HWCrypto VS MbedTLS

Postby urbanze » Fri Sep 22, 2017 1:12 pm

ESP_Angus wrote:
urbanze wrote:Well... Sucess!! Before, I put only "mbedtls\include\mbedtls\aes.h". Now, I tried put more one "mbedtls\library\aes.c" and WORKED!
I'm glad you got this working. Can you explain exactly what you needed to change?
urbanze wrote: See both libraries: https://i.imgur.com/d3liT0L.png

However, with HWCrypto only, takes ~129uS. MbedTLS with Acell ON takes ~150uS.... List bellow show my tests.
That doesn't seem right. With the code you posted mbedtls_aes_crypt_ecb() is remapped via macro directly to your comparison function esp_aes_crypt_ecb(). So they should be the exact same function when acceleration is enabled.

See here: https://github.com/espressif/esp-idf/bl ... _alt.h#L39

You may need to run a lot more than 4 iterations to even out small differences and "jitter" due to interrupts, task switching, etc.


Angus
Well angus, problems! LOL.

1-) "Can you explain exactly what you needed to change?"

-I need to include this both libraries :
#include <esp-idf\components\mbedtls\include\mbedtls\aes.h>
#include <esp-idf\components\mbedtls\library\aes.c>

Without aes.c, doesn't work. Error: Undefined reference to all functions of mbed.

2-) I made some test and benchmark's with 4 and 250 encryptations 128bits. The results were surprising and inverse, see:

2.1-) HWCrypto and 4 encrypts. Total time: 125uS
Image



2.2-) HWCrypto and 250 encrypts. Total time: 6519uS
Image



2.3-) MbedTLS and 4 encrypts. Total time: 143uS
Image



2.4-) MbedTLS and 250 encrypts. Total time: 4164uS
Image


Strangely, as you said, they are both identical. The results with 250 encryptions were quite different, can you explain why? :?: :x


Another thing, explain to me why they put "mode" inside the encryption function, for example:

esp_aes_crypt_ecb(ctx, MODE, input, output);

If the function already says it is encryption, it is necessary to put "AES_ENCRYPT". Unlike the decryption function, you do not need to enter the mode.

JimmyPedersen
Posts: 21
Joined: Sun Nov 15, 2015 4:14 am

Re: HWCrypto VS MbedTLS

Postby JimmyPedersen » Fri Sep 22, 2017 3:34 pm

Could it be that there is some over head associated with setting up the hw crypto unit that causes these poor numbers. What happens if you use bigger input and output blocks? For example En/De-crypt 256 bytes instead of 16.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: HWCrypto VS MbedTLS

Postby urbanze » Fri Sep 22, 2017 5:31 pm

JimmyPedersen wrote:Could it be that there is some over head associated with setting up the hw crypto unit that causes these poor numbers. What happens if you use bigger input and output blocks? For example En/De-crypt 256 bytes instead of 16.
You say, divide 256B into 16 16B blocks and encrypt one by one? Input and Output parameters are 16B, by block chipter size I think

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: HWCrypto VS MbedTLS

Postby ESP_Angus » Mon Sep 25, 2017 3:08 am

1-) "Can you explain exactly what you needed to change?"

-I need to include this both libraries :
#include <esp-idf\components\mbedtls\include\mbedtls\aes.h>
#include <esp-idf\components\mbedtls\library\aes.c>

Without aes.c, doesn't work. Error: Undefined reference to all functions of mbed.
You shouldn't have to do this, #include "mbedtls/aes.h" should be enough.

If you delete the "build" directory for your project and rebuild and it still fails, there's something wrong with your IDF installation (if deleting the build directory and rebuilding from scratch causes it to pass, this is a recent bug that will be fixed in the next couple of days.)
2-) I made some test and benchmark's with 4 and 250 encryptations 128bits. The results were surprising and inverse, see:
That is a bit odd. If you use the CCOUNT register instead of micros() (and avoid any floating point division), do you get the same thing?

ie

Code: Select all

#include "esp_cpu.h"

uint32_t before, after;
RSR(CCOUNT, before);

// do the thing

RSR(CCOUNT, after);
printf("Took %d instructions\n", after - before);
Otherwise it may have something to do with including aes.c directly in the same source file. I'm not sure.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: HWCrypto VS MbedTLS

Postby urbanze » Mon Sep 25, 2017 1:18 pm

ESP_Angus wrote:
That is a bit odd. If you use the CCOUNT register instead of micros() (and avoid any floating point division), do you get the same thing?
I did the RSR () tests and the results were the same with 250 encryptions.
Why? The files indicate the same operations, there is something wrong... :?:


MbedTLS, 332897 instructions, ~4161uS
Image



HWCrypto, 521166 instructions, ~6514uS
Image

_chris_
Posts: 2
Joined: Fri Oct 20, 2017 6:46 pm

Re: HWCrypto VS MbedTLS

Postby _chris_ » Sat Oct 21, 2017 6:32 pm

I have the very same problem concerning the missing references... how can I find out what went wrong? As soon as I add aes.c (!) as include, it works

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: HWCrypto VS MbedTLS

Postby urbanze » Sun Oct 22, 2017 12:47 pm

_chris_ wrote:I have the very same problem concerning the missing references... how can I find out what went wrong? As soon as I add aes.c (!) as include, it works
I just put the ".c" of each encryption I use in the code. Why this happens, I do not know. :lol:

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: HWCrypto VS MbedTLS

Postby chegewara » Sun Sep 16, 2018 8:53 am

I know its old topic, but i just found it and i am doing some test with aes cbc encrypt/decrypt. Basically i am trying to encrypt some string and then decrypt output array to get back initial string. I have no issue with encrypting data, output value is consistent with online encryption tools, but decrypted value is not what i am expecting.

Code: Select all

I (167) text: 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00
I (173) encrypt: cc c6 d5 a3 2e df d2 7d 31 27 c8 17 54 b4 54 d3
I (180) decrypt: 22 0b b1 60 70 51 0c ae ec 33 af ac 2b fd 4b 24
This is test code (thanks urbanza):

Code: Select all

	esp_aes_context aes;
	esp_aes_context aes2;
	const unsigned char key[] = "12345678901234561234567890123456";
	// char _tmp[] = "testtesttesttesttest";
	char _tmp[] = "test";
	int i = strlen(_tmp)%16 == 0 ? strlen(_tmp) : (strlen(_tmp)/16 + 1) * 16;
	char *inp;
	inp = (char*)calloc(i, sizeof(char));
	strcpy(inp, _tmp);
	unsigned char iv[] = {0x9a, 0xa8, 0x17, 0xb7, 0x5e, 0x8e, 0xde, 0xd3, 0xdd, 0x14, 0x67, 0xbb, 0x7f, 0x49, 0x1f, 0xf7}; // 9a, a8, 17, b7, 5e, 8e, de, d3, dd, 14, 67, bb, 7f, 49, 1f, f7
	unsigned char outp[128] = {0};
	unsigned char outp2[128] = {0};
ESP_LOGI("LOG_TAG", "string: %s, length: %d", inp, strlen(inp));
ESP_LOGI("LOG_TAG", "string: %s, length: %d", iv, 16);
	ESP_LOG_BUFFER_HEX("text", inp, i);
	esp_aes_init(&aes);
	esp_aes_setkey(&aes, key, 256);
	esp_aes_crypt_cbc(&aes, ESP_AES_ENCRYPT, i, iv, (const unsigned char*)inp, outp);
	esp_aes_free(&aes);

	esp_aes_init(&aes2);
	esp_aes_setkey(&aes2, key, 256);
	esp_aes_crypt_cbc(&aes2, ESP_AES_DECRYPT, i, iv, (const unsigned char*)outp, outp2);
	esp_aes_free(&aes2);


User avatar
billiam
Posts: 5
Joined: Tue Jul 24, 2018 8:20 pm

Re: HWCrypto VS MbedTLS

Postby billiam » Fri Oct 09, 2020 10:01 pm

FWIW 2 years too late, the iv is modified in place when the crypto runs. To decrypt it, you need to reset it to its original value.

Who is online

Users browsing this forum: No registered users and 88 guests