Skip to content
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

Move TI hardware acceleration into Matter repo #9579

Merged
merged 17 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/crypto/CHIPCryptoPAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,10 @@ 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.
* 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.
* Worst case is TI mbedtls hardware accelerated context, so let's use its
srickardti marked this conversation as resolved.
Show resolved Hide resolved
* worst case and let 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));
constexpr size_t kMAX_Hash_SHA256_Context_Size = (sizeof(unsigned int) * 76);
srickardti marked this conversation as resolved.
Show resolved Hide resolved

/*
* Overhead to encode a raw ECDSA signature in X9.62 format in ASN.1 DER
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/CHIPCryptoPALmbedTLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ CHIP_ERROR Hash_SHA256_stream::GetDigest(MutableByteSpan & out_buffer)
CHIP_ERROR result = Finish(out_buffer);

// Restore context prior to finalization.
*context = previous_ctx;
mbedtls_sha256_clone(context, &previous_ctx);

return result;
}
Expand Down
8 changes: 8 additions & 0 deletions src/platform/OpenThread/OpenThreadUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ void LogOpenThreadStateChange(otInstance * otInst, uint32_t flags)
ChipLogDetail(DeviceLayer, " Mesh Prefix: %s/64", strBuf);
}
#if CHIP_CONFIG_SECURITY_TEST_MODE

tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
{
#if OPENTHREAD_API_VERSION >= 126
const otNetworkKey * otKey = otThreadGetNetworkKey(otInst);
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++)
snprintf(&strBuf[i * 2], 3, "%02X", otKey->m8[i]);
ChipLogDetail(DeviceLayer, " Network Key: %s", strBuf);
#else
const otMasterKey * otKey = otThreadGetMasterKey(otInst);
for (int i = 0; i < OT_MASTER_KEY_SIZE; i++)
snprintf(&strBuf[i * 2], 3, "%02X", otKey->m8[i]);
ChipLogDetail(DeviceLayer, " Master Key: %s", strBuf);
#endif // OPENTHREAD_API_VERSION
}
#endif // CHIP_CONFIG_SECURITY_TEST_MODE
}
Expand Down
145 changes: 145 additions & 0 deletions src/platform/cc13x2_26x2/crypto/aes_alt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2020 Texas Instruments Incorporated
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "aes_alt.h"
#include "mbedtls/aes.h"

#if defined(MBEDTLS_AES_ALT)

#include <assert.h>
#include <string.h>

#include "ti_drivers_config.h"

#include <ti/devices/DeviceFamily.h>
#include <ti/drivers/AESECB.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>

/**
* number of active contexts, used for power on/off of the crypto core
*/
static unsigned int ref_num = 0;

static AESECB_Handle AESECB_handle = NULL;

/**
* @brief Initialize AES context
srickardti marked this conversation as resolved.
Show resolved Hide resolved
*
* @param [in,out] ctx AES context to be initialized
*/
void mbedtls_aes_init(mbedtls_aes_context * ctx)
{
AESECB_Params AESECBParams;

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

/**
* @brief Clear AES context
*
* \param ctx AES context to be cleared
*/
void mbedtls_aes_free(mbedtls_aes_context * ctx)
{
srickardti marked this conversation as resolved.
Show resolved Hide resolved
if (--ref_num == 0)
{
AESECB_close(AESECB_handle);

AESECB_handle = NULL;
}

memset((void *) ctx, 0x00, sizeof(ctx));
}

/**
* \brief AES key schedule (encryption)
*
* \param ctx AES context to be initialized
* \param key encryption key
* \param keybits must be 128, 192 or 256
*
* \return 0 if successful
*/
int mbedtls_aes_setkey_enc(mbedtls_aes_context * ctx, const unsigned char * key, unsigned int keybits)
{
int_fast16_t statusCrypto = 0;

/* Initialize AES key */
memcpy(ctx->keyMaterial, key, (keybits >> 3));
statusCrypto = CryptoKeyPlaintext_initKey(&ctx->cryptoKey, (uint8_t *) ctx->keyMaterial, (keybits >> 3));
assert(statusCrypto == 0);
srickardti marked this conversation as resolved.
Show resolved Hide resolved

return (int) statusCrypto;
}

/**
* \brief AES key schedule (decryption)
*
* \param ctx AES context to be initialized
* \param key decryption key
* \param keybits must be 128, 192 or 256
*
* \return 0 if successful
*/
int mbedtls_aes_setkey_dec(mbedtls_aes_context * ctx, const unsigned char * key, unsigned int keybits)
{
int_fast16_t statusCrypto;

/* Initialize AES key */
statusCrypto = CryptoKeyPlaintext_initKey(&ctx->cryptoKey, (uint8_t *) key, (keybits >> 3));
assert(statusCrypto == 0);

return (int) statusCrypto;
}

/**
* \brief AES-ECB block encryption/decryption
*
* \param ctx AES context
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
* \param input 16-byte input block
* \param output 16-byte output block
*
* \return 0 if successful
*/

int mbedtls_aes_crypt_ecb(mbedtls_aes_context * ctx, int mode, const unsigned char input[16], unsigned char output[16])
{
int statusCrypto;
AESECB_Operation operationOneStepEncrypt;

/* run it through the authentication + encryption, pass the ccmLVal = 2 */
AESECB_Operation_init(&operationOneStepEncrypt);

operationOneStepEncrypt.key = &ctx->cryptoKey;
operationOneStepEncrypt.inputLength = 16;
operationOneStepEncrypt.input = (uint8_t *) input;
operationOneStepEncrypt.output = (uint8_t *) output;

statusCrypto = AESECB_oneStepEncrypt(AESECB_handle, &operationOneStepEncrypt);
assert(statusCrypto == 0);

return statusCrypto;
}
#endif
102 changes: 102 additions & 0 deletions src/platform/cc13x2_26x2/crypto/aes_alt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2020 Texas Instruments Incorporated
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBEDTLS_AES_ALT_H
srickardti marked this conversation as resolved.
Show resolved Hide resolved
#define MBEDTLS_AES_ALT_H

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls-config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#if defined(MBEDTLS_AES_ALT)

#include <ti/drivers/AESECB.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKey.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* \brief AES context structure
*
* \note
srickardti marked this conversation as resolved.
Show resolved Hide resolved
*/
typedef struct
{
CryptoKey cryptoKey; /*!< input to the crypto driver */
uint32_t keyMaterial[16]; /*!< storage for the key */
} mbedtls_aes_context;

/**
* @brief Initialize AES context
*
* @param [in,out] ctx AES context to be initialized
*/
void mbedtls_aes_init(mbedtls_aes_context * ctx);

/**
* @brief Clear AES context
*
* \param ctx AES context to be cleared
*/
void mbedtls_aes_free(mbedtls_aes_context * ctx);

/**
* \brief AES key schedule (encryption)
*
* \param ctx AES context to be initialized
* \param key encryption key
* \param keybits must be 128, 192 or 256
*
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
*/
int mbedtls_aes_setkey_enc(mbedtls_aes_context * ctx, const unsigned char * key, unsigned int keybits);

/**
* \brief AES key schedule (decryption)
*
* \param ctx AES context to be initialized
* \param key decryption key
* \param keybits must be 128, 192 or 256
*
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
*/
int mbedtls_aes_setkey_dec(mbedtls_aes_context * ctx, const unsigned char * key, unsigned int keybits);

/**
* \brief AES-ECB block encryption/decryption
*
* \param ctx AES context
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
* \param input 16-byte input block
* \param output 16-byte output block
*
* \return 0 if successful
*/
int mbedtls_aes_crypt_ecb(mbedtls_aes_context * ctx, int mode, const unsigned char input[16], unsigned char output[16]);

#ifdef __cplusplus
}
#endif

#endif /* MBEDTLS_AES_ALT */

#endif /* MBEDTLS_AES_ALT_H */
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define MBEDTLS_ECJPAKE_ALT
#define MBEDTLS_AES_ALT
#define MBEDTLS_SHA256_ALT
//#define MBEDTLS_ENTROPY_HARDWARE_ALT
#define MBEDTLS_ENTROPY_HARDWARE_ALT

/**
* Enable Crypto and Entropy modules
Expand Down
Loading