1 (edited by chrisj7903 2025-01-05 15:36:36)

Topic: Looking for example of AES-CTR decryption for Arduino IDE

I've been scouring the internet and relevant forums for info on decryption using AES-CTR mode.

I've installed wolfSSL in my Arduino IDE but unfortunately the provided examples focus on TLS not SSL or AES.

I've been searching for a suitable code examples. These two have been a bit helpful

a) wolfSSL aesctr-file-encryp example on GitHub
https://github.com/wolfSSL/wolfssl-exam … -encrypt.c

b) aes-ctr post on Stack Overflow (code in C#)
https://stackoverflow.com/questions/637 … 2#51188472

But neither are written for Arduino so only partly useful for my purposes.

After setting WOLFSSL_AES_COUNTER and WOLFSSL_AES_DIRECT in user_settings.h and some likely code tweaks I tried compiling item (a) but no luck.

I'm not even close to a professional software developer, so a good AES-CTR example in (C/C++) would be a huge help, ideally in a form I can easily get into the Arduino IDE.

help! :-) 
TIA

Share

Re: Looking for example of AES-CTR decryption for Arduino IDE

Hi,

Another great place to look is tests/api.c.  You can look at test_wc_AesCtrEncryptDecrypt() in there to see how to use the API.  When you compile, what errors are you seeing?

We'd love to know more about you and your project.  Where are you located?  What are you goals? Please do let us know!

Warm regards, Anthony

Share

3 (edited by chrisj7903 2025-01-06 15:00:02)

Re: Looking for example of AES-CTR decryption for Arduino IDE

Where exactly will I find tests/api.c? (it's not under sketches/libraries/wolfSSL)

I am aware of this AES-CTR section in the manual: https://www.wolfssl.com/documentation/m … ctrencrypt but it doesn't really help me.

If I'm given a working example in a similar context to my use case I can usually adapt it to my needs ...   

My compilation errors are simply because I'm not experienced enough as a programmer to convert the examples I found (like the C# AES-CTR decryption code) to C/C++ (the code I have kludged together so far is rubbish, no point in sharing)

Share

Re: Looking for example of AES-CTR decryption for Arduino IDE

You can find it online here: https://raw.githubusercontent.com/wolfS … ests/api.c

Note that it is a rather large file.  Can you let us know a bit more about yourself and your project?  If you would rather share information about yourself via a confidential channel, please send a message to support@wolfssl.com.

Warm regards, Anthony

Share

5 (edited by chrisj7903 2025-01-07 17:49:20)

Re: Looking for example of AES-CTR decryption for Arduino IDE

Thanks, I think :-) certainly is a large file. Searching for AES_CTR or AES_COUNTER revealed a few hits, but nothing jumped out at me.

My project is quite straight forward ... I'm building an instrumentation system for a small electric vehicle. To monitor battery condition I have a Smartshunt made by Victron Energy: https://www.victronenergy.com/battery-m … tery-shunt

My project uses a high powered Arduino relative (a Teensy 4.x https://www.pjrc.com/store/ ) to integrate information from several sensors around the vehicle (e.g temperatures, GPS, speed, battery voltage, current, % remaining etc) into a small display ... a dashboard if you like.

Victron have kindly made information on their Bluetooth Low Energy (BLE) Advertising protocol available to the public, for many of their bluetooth devices. So now people like me can continually receive data from a Smartshunt (or any 'smart' Victron product) over BLE. This is the same protocol and data the VictronConnect mobile phone app utilises to display device information.

BTW a neat aspect of the advertised data: it's broadcast and available without needing a BLE connection.

A portion of the data continually advertised by the Smartshunt (less than 16 bytes) is encrypted using AES-CTR. These few bytes include voltages, current etc. so I need to decrypt them. The data updates every 150ms or so.

From the unencypted portion I can see and extract the "salt" as two bytes (also called "Nonce/Data Counter")

I also have the encryption key (128 bits) specific to my SmartShunt (retreived via the VictronConnect app)

So I think have all the inputs, I just need to code the AES-CTR algorithm in C/C++ in the Arduino IDE. I am very experienced in that environment, so I don't need the whole solution, just the Algorithm, how to call it, with salt and key in hand.

As I see the algorithm simply has to process the encrypted byte array as an input stream using the salt and key, and output the decrypted bytes to a bytes array/variable that I can print or display. ON further thought I will also need code for the reverse, i.e. to encrypt 16 bytes, then I could generate specific data and test the encryption/decyprion cycle is working.

Searching on the Arduino forum I came across a few mentions of wolfSSL, but none were looking at AES-CTR. Hence why I am here ...

I was going to attach a rather detailed but very useful extract (in PDF) from the Victon forum, a user post that explains some of the finer points of decrypting the Victron data (e.g. watch out for the little endians!) :-)
but I keep getting a warning "The server was unable to save the uploaded file"

best regards

PS: If I can get this to work it would make a great additional example in the wolfSSL library for ArduinoIDE. I would be happy to contribute towards that ...

Share

Re: Looking for example of AES-CTR decryption for Arduino IDE

Hi ,

Excellent!  We here at wolfSSL would love to encourage your efforts.  Please, lets continue this conversation over on our technical support channel.  Please send a message to support@wolfssl.com.  This will get you started on the right note!

Warm regards, Anthony

Share

Re: Looking for example of AES-CTR decryption for Arduino IDE

For your immediate convenience, here is the specific code I was thinking that would interest you:

static int test_wc_AesCtrEncryptDecrypt(void)
{
    EXPECT_DECLS;
#if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
    Aes aesEnc;
    Aes aesDec;
    byte key32[] = {
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
    };
    byte vector[] = { /* Now is the time for all w/o trailing 0 */
        0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
        0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
        0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
    };
    byte iv[] = "1234567890abcdef";
    byte enc[AES_BLOCK_SIZE * 2];
    byte dec[AES_BLOCK_SIZE * 2];

    /* Init stack variables. */
    XMEMSET(&aesEnc, 0, sizeof(Aes));
    XMEMSET(&aesDec, 0, sizeof(Aes));
    XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
    XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);

    ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
    ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);

    ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
        AES_ENCRYPTION), 0);
    ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
        sizeof(vector)/sizeof(byte)), 0);
    /* Decrypt with wc_AesCtrEncrypt() */
    ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
        AES_ENCRYPTION), 0);
    ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
        0);
    ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);

    /* Test bad args. */
    ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
        WC_NO_ERR_TRACE(BAD_FUNC_ARG));
    ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
        WC_NO_ERR_TRACE(BAD_FUNC_ARG));
    ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
        WC_NO_ERR_TRACE(BAD_FUNC_ARG));

    wc_AesFree(&aesEnc);
    wc_AesFree(&aesDec);
#endif
    return EXPECT_RESULT();
}

Share

Re: Looking for example of AES-CTR decryption for Arduino IDE

Thank you anthony
I will follow up via support@wolfSSL.com but first I'll see what happens with the code you've generously offerred.
stand by :-)

Share