STM32F437 Hardware Crypto functions integration with WolfSSL

2k Views Asked by At

I have a project which uses DTLS over connections. For now I have successfully managed to run WolfSSL on controllers using only software implementation but the time required to setup the initial connection is abhorrent and I'm now looking to utilize HW crypto in the MCU to accelerate performance. From the STM32F2 implementation provided by WolfSSL, I have only seen a few crypto algorithms being retargeted to the hardware crypto. In my DTLS implementation I am utilizing quite a few different algos provided by WolfCrypt. As I am quite inexperienced with very low level details of the SSL library, I need some help to retarget almost all the encryption algorithms present in WolfSSL from software implementation to STM32 Crypto Hardware.

According to ST's Cube HAL framework, the algos supported by HW Encrytion are

STM32F437x/439x
– AES: CFB, OFB, XTS, CCM, GCM, CMAC, KeyWrap
Key size: 128, 192, 256 bit Crypto accelerator
– ECC: Key generation, Scalar multiplication, ECDSA Random number
generator (RNG) – RSA encryption/decryption functions with PKCS#1v1.5

So I would need help changing these algos in WolfSSL such that they use STM32 HW Acc. If someone can provide sample of this, that would be most appreciated.

I have also asked this on the WolfSSL forums. In case I find anything interesting there, I will update here for other's benefit Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

Which version of wolfSSL are you working with? wolfSSL recently added support for CubeMX HAL. If you do not have wolfSSLv3.10.0 please download it from the download page here: https://wolfssl.com/wolfSSL/download/downloadForm.php or get it from the wolfSSL github repository: https://github.com/wolfSSL/wolfssl.git

There is a new define for WOLFSSL_STM32_CUBEMX in the STM32F2 section of /wolfssl/wolfcrypt/settings.h

#ifdef WOLFSSL_STM32F2
    #define SIZEOF_LONG_LONG 8
    #define NO_DEV_RANDOM
    #define NO_WOLFSSL_DIR
    #undef  NO_RABBIT
    #define NO_RABBIT
    #undef  NO_64BIT
    #define NO_64BIT
    #define STM32F2_RNG
    #define STM32F2_CRYPTO
    #if !defined(__GNUC__) && !defined(__ICCARM__)
        #define KEIL_INTRINSICS
    #endif
    #define NO_OLD_RNGNAME
    #ifdef WOLFSSL_STM32_CUBEMX   // <--- New section for HAL support in version 3.10.0
        #include "stm32f2xx_hal.h"
        #ifndef STM32_HAL_TIMEOUT
            #define STM32_HAL_TIMEOUT   0xFF
        #endif
    #else
        #include "stm32f2xx.h"
        #include "stm32f2xx_cryp.h"
        #include "stm32f2xx_hash.h"
    #endif /* WOLFSSL_STM32_CUBEMX */
#endif

This define adds support for offloading AES, DES3, and RANDOM operations to hardware. If you wish to port additional algorithms please use the sections in /wolfcrypt/src/aes.c surrounded by the define for WOLFSSL_STM32_CUBEMX as a reference. Here is a short sample of adding HAL API in the function wc_AesEncrypt. Source code was pulled from https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/aes.c#L208:

static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
{
    int ret = 0;
#ifdef WOLFSSL_STM32_CUBEMX
    CRYP_HandleTypeDef hcryp;

    /* load key into correct registers */
    switch(aes->rounds) {
        case 10: /* 128-bit key */
            hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
            break;
        case 12: /* 192-bit key */
            hcryp.Init.KeySize = CRYP_KEYSIZE_192B;
            break;
        case 14: /* 256-bit key */
            hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
            break;
        default:
            break;
    }

    XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
    hcryp.Instance = CRYP;
    hcryp.Init.DataType = CRYP_DATATYPE_8B;
    hcryp.Init.pKey = (uint8_t*)aes->key;

    HAL_CRYP_Init(&hcryp);

    if (HAL_CRYP_AESECB_Encrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
                                            outBlock, STM32_HAL_TIMEOUT) != HAL_OK) {
        ret = WC_TIMEOUT_E;
    }

    HAL_CRYP_DeInit(&hcryp);
#else
... other non-cubemx support implementations below