Skip to content

NUCLEO_F756ZG / mbedTLS_SHA1 hw acceleration #3957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from

Conversation

adustm
Copy link
Member

@adustm adustm commented Mar 16, 2017

Description

mbedtls_SHA1 hw acceleration for NUCLEO_F756ZG

Status

READY

Related PRs

** This PR needs #3954 + #3947 to be merged first**

Deploy notes

Notes regarding the deployment of this PR. These should note any
required changes in the build environment, tools, compilers, etc.

Steps to test or reproduce

same as in #3947

@adustm
Copy link
Member Author

adustm commented Mar 16, 2017

+-------------------+---------------+------------------------+-----------------------------+--------+--------+--------+--------------------+
| target            | platform_name | test suite             | test case                   | passed | failed | result | elapsed_time (sec) |
+-------------------+---------------+------------------------+-----------------------------+--------+--------+--------+--------------------+
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_aes_self_test       | 1      | 0      | OK     | 0.47               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_base64_self_test    | 1      | 0      | OK     | 0.12               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_ccm_self_test       | 1      | 0      | OK     | 0.12               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_ctr_drbg_self_test  | 1      | 0      | OK     | 0.12               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_ecp_self_test       | 1      | 0      | OK     | 2.01               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_entropy_self_test   | 1      | 0      | OK     | 0.08               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_gcm_self_test       | 1      | 0      | OK     | 2.6                |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_hmac_drbg_self_test | 1      | 0      | OK     | 0.13               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_mpi_self_test       | 1      | 0      | OK     | 0.25               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_rsa_self_test       | 1      | 0      | OK     | 0.34               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_sha1_self_test      | 1      | 0      | OK     | 0.14               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_sha256_self_test    | 1      | 0      | OK     | 0.47               |
| NUCLEO_F756ZG-IAR | NUCLEO_F756ZG | tests-mbedtls-selftest | mbedtls_sha512_self_test    | 1      | 0      | OK     | 0.83               |
+-------------------+---------------+------------------------+-----------------------------+--------+--------+--------+--------------------+

Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It seems there are some memory leaks here
  2. user defined configuration should not be defined in mbedtls_device.h



#define MBEDTLS_SHA1_C
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an application specific definition, and should be defined by the end user. Please remove it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll put that into targets.json

#ifndef MBEDTLS_DEVICE_H
#define MBEDTLS_DEVICE_H

#define MBEDTLS_AES_ALT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds alternative AES support, but this PR is for SHA1 only.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as in PR for MD5. I based my PR on AES' one because I think it causes merge issues if I create new folders and new files with the same names in separate PRs, sorry for that.

#define MBEDTLS_AES_ALT
#define MBEDTLS_SHA1_ALT

#define MBEDTLS_SHA1_C
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an application specific definition, and should be defined by the end user. Please remove it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll put that into targets.json


if (ilen <4)
{
ctx->sbuf=malloc(ilen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a memory leak:

  1. where is this being released
  2. even if it is being released in an internal API, if a user is calling twice to mbedtls_sha1_update with size smaller than 4, the first allocated buffer will not be freed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for noticing this. I'll work on that

buf_len = ilen - modulus;
HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, buf_len);
ctx->sbuf_len=modulus;
ctx->sbuf=malloc(ctx->sbuf_len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a memory leak:

  1. where is this being released
  2. even if it is being released in an internal API, if a user is calling twice to mbedtls_sha1_update with size which modulus is not 4, the first allocated buffer will not be freed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for noticing this. I'll work on that

HAL_HASH_SHA1_Accumulate(&ctx->hhash_sha1, (uint8_t *)input, ilen);
else
{
intermediate_buf=malloc(ilen+ctx->sbuf_len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a memory leak - > where is it freed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for noticing this. I'll work on that

#if defined MBEDTLS_SHA1_ALT

#include "mbedtls/platform.h"
#include "mbedtls/config.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to include this file, as it should be included in the c file before this header file is included

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbedtls/config.h not needed. Correct


#if defined MBEDTLS_SHA1_ALT

#include "mbedtls/platform.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file needed to be included in this header file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed to avoid warnings that 'malloc' is declared implicitly. I move it to sha1_alt.c instead

* \param ilen length of the input data
* \param output SHA-1 checksum result
*/
void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to redefine this function, as it is defined in sha1.h, outside the #ifdef MBEDTLS_SHA1_ALT check

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

*
* \return 0 if successful, or 1 if the test failed
*/
int mbedtls_sha1_self_test( int verbose );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to redefine this function, as it is defined in sha1.h, outside the #ifdef MBEDTLS_SHA1_ALT check

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

@adustm
Copy link
Member Author

adustm commented Apr 7, 2017

Hello @RonEld
I think I reworked every comment you had (targets.json appart: discussion on going in #3956)
Could you check the result, please ?
Is it ok if I use realloc function in commit 97fc115 ?

kind regards

@sg-
Copy link
Contributor

sg- commented Apr 10, 2017

@adustm Should this be moved against the workshop branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants