Skip to content

Commit

Permalink
Included changes from the TI PR project-chip#9479
Browse files Browse the repository at this point in the history
  • Loading branch information
tlykkeberg-grundfos committed Sep 23, 2021
1 parent 7732c52 commit 66075da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/crypto/CHIPCryptoPAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ constexpr size_t kEmitDerIntegerWithoutTagOverhead = 1; // 1 sign stuffer
constexpr size_t kEmitDerIntegerOverhead = 3; // Tag + Length byte + 1 sign stuffer

/*
* Worst case is OpenSSL, so let's use its worst case and let static assert tell us if
* we are wrong, since `typedef SHA_LONG unsigned int` is default.
* Size of a static intance of the SHA256 context.
*
* This must account for the worst case size for all platforms. Currently the
* worst case size is the TI hardware accelerated version of mbedtls. This is
* 76 words long for the driver context. The previous worst case was OpenSSL
* with the following sturuct sizing.
* SHA_LONG h[8];
* SHA_LONG Nl, Nh;
* SHA_LONG data[SHA_LBLOCK]; // SHA_LBLOCK is 16 for SHA256
* unsigned int num, md_len;
*
* We also have to account for possibly some custom extensions on some targets,
* especially for mbedTLS, so an extra sizeof(uint64_t) is added to account.
* Let the static assert tell us if we are wrong.
*/
constexpr size_t kMAX_Hash_SHA256_Context_Size =
((sizeof(unsigned int) * (8 + 2 + 16 + 2)) + sizeof(uint64_t) + (24 * sizeof(uint64_t)));
constexpr size_t kMAX_Hash_SHA256_Context_Size = (sizeof(unsigned int) * 76);

/*
* Overhead to encode a raw ECDSA signature in X9.62 format in ASN.1 DER
Expand Down
13 changes: 9 additions & 4 deletions src/platform/cc13x2_26x2/crypto/aes_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,28 @@ void mbedtls_aes_init(mbedtls_aes_context * ctx)
{
AESECB_Params AESECBParams;

if (ref_num++ == 0)
if (ref_num == 0)
{
AESECB_Params_init(&AESECBParams);
AESECBParams.returnBehavior = AESECB_RETURN_BEHAVIOR_POLLING;
AESECB_handle = AESECB_open(CONFIG_AESECB_1, &AESECBParams);

// handle will be NULL if open failed, subsequent calls will fail with a generic HW error
}
ref_num++;
}

void mbedtls_aes_free(mbedtls_aes_context * ctx)
{
if (--ref_num == 0)
if (ref_num > 0)
{
AESECB_close(AESECB_handle);
ref_num--;
if (ref_num == 0)
{
AESECB_close(AESECB_handle);

AESECB_handle = NULL;
AESECB_handle = NULL;
}
}

memset((void *) ctx, 0x00, sizeof(ctx));
Expand Down

0 comments on commit 66075da

Please sign in to comment.