Zum Inhalt

ESP-IDF with WolfSSL

Using WolfSSL with ESP32 in ESP-IDF is not magic. In this post I will show you how to setup WolfSSL with ESP-IDF and provide an example for encryption and decryption. Also with splitted bytes[] input.

How to install

There are two options to install WolfSSL for ESP-IDF:

  1. Install WolfSSL for ESP-IDF:
    https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/esp_tls.html#how-to-use-wolfssl-with-esp-idf
    1. Clone repository
    2. Execute setup.sh from IDE/Espressif/ESP-IDF
    3. Modify EXTRA_COMPONENT_DIRS in CMakeLists.txt with the component path. For example ${ESP_INSTALL}/esp/esp-idf/components/wolfssl/
  2. Install WolfSSL for ESP-IDF from WolfSSL:
    https://github.com/wolfSSL/wolfssl/tree/master/IDE/Espressif/ESP-IDF
    1. Clone repository
    2. Execute setup.sh from IDE/Espressif/ESP-IDF
    3. Modify EXTRA_COMPONENT_DIRS in CMakeLists.txt with the component path. For example ${ESP_INSTALL}/esp/esp-idf/components/wolfssl/

Example code

#include <stdio.h>
#include <inttypes.h>
#include <wolfssl/wolfcrypt/aes.h>

void app_main(void)
{

    Aes enc;
    Aes dec;

    /**
     * Plain: 72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72:72
     * Cipher: 85:82:76:E5:5C:30:E1:F4:90:07:C8:72:A6:9E:3C:63:CD:02:B5:15:F3:2E:2A:47:E4:B6:06:B4:78:9B:28:E8
    */

    // IMPORTANT DO NOT USE THIS INSECURE KEY AN IV!
    const byte key_b[] = {  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00  };
    const byte iv_b[]  = {  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00  };

    byte plain_b[32]   =  {  0x72, 0x73, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
                             0x72, 0x74, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
                             0x72, 0x75, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
                             0x72, 0x76  };

    byte cipher_b[32];
    byte decrypted_b[32];

    byte plain11_b[16];
    byte plain12_b[16];
    byte cipher11_b[16];
    byte cipher12_b[16];
    byte decrypted11_b[16];
    byte decrypted12_b[16];

    wc_AesSetKey(&enc, key_b, sizeof(key_b), iv_b, AES_ENCRYPTION);
    wc_AesCbcEncrypt(&enc, cipher_b, plain_b, sizeof(plain_b));

    memcpy(plain11_b, plain_b, sizeof(plain_b[0])*(16));
    memcpy(plain12_b, &plain_b[16], sizeof(plain_b[0])*(16));

    wc_AesSetKey(&enc, key_b, sizeof(key_b), iv_b, AES_ENCRYPTION);
    wc_AesCbcEncrypt(&enc, cipher11_b, plain11_b, sizeof(plain11_b));
    wc_AesCbcEncrypt(&enc, cipher12_b, plain12_b, sizeof(plain12_b));

    wc_AesSetKey(&dec, key_b, sizeof(key_b), iv_b, AES_DECRYPTION);
    wc_AesCbcDecrypt(&dec, decrypted_b, cipher_b, sizeof(cipher_b));

    wc_AesSetKey(&dec, key_b, sizeof(key_b), iv_b, AES_DECRYPTION);
    wc_AesCbcDecrypt(&dec, decrypted11_b, cipher11_b, sizeof(cipher11_b));
    wc_AesCbcDecrypt(&dec, decrypted12_b, cipher12_b, sizeof(cipher12_b));

    int i;
    printf("Plain         : ");
    for (i = 0; i < sizeof(plain_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", plain_b[i]);
    }
    printf("\n");

    printf("Plain1112     : ");
    for (i = 0; i < sizeof(plain11_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", plain11_b[i]);
    }
    printf("|");
    for (i = 0; i < sizeof(plain12_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", plain12_b[i]);
    }
    printf("\n");

    printf("Cipher        : ");
    for (i = 0; i < sizeof(cipher_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", cipher_b[i]);
    }
    printf("\n");

    printf("Cipher1112    : ");
    for (i = 0; i < sizeof(cipher11_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", cipher11_b[i]);
    }
    printf("|");
    for (i = 0; i < sizeof(cipher12_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", cipher12_b[i]);
    }
    printf("\n");

    printf("Decrypted     : ");
    for (i = 0; i < sizeof(decrypted_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", decrypted_b[i]);
    }
    printf("\n");

    printf("Decrypted1112 : ");
    for (i = 0; i < sizeof(decrypted11_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", decrypted11_b[i]);
    }
    printf("|");
    for (i = 0; i < sizeof(decrypted12_b); i++)
    {
        if (i > 0) printf(":");
        printf("%02X", decrypted12_b[i]);
    }
    printf("\n");
}