# AES 256 - Options?

**URL:** <https://community.particle.io/t/aes-256-options/47437>\
**Category:** General\
**Created:** [February 1, 2019, 1:33am UTC](https://community.particle.io/t/aes-256-options/47437 "2019-02-01T01:33:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mblackler](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@mblackler](https://community.particle.io/u/mblackler)\
**Post date:** [February 1, 2019, 1:33am UTC](https://community.particle.io/t/aes-256-options/47437/1 "2019-02-01T01:33:05Z")

</div>

In order to be compliant with some client specifications on an RFP, they are asking for AES 256 for comms. Now, we know that we have AES 128 for comms (we are using an Electron, FWIW), but I need to provide assurance that we can use it.

There seems to have been some work done here:

> **[hirotakaster/TlsTcpClient](https://github.com/hirotakaster/TlsTcpClient)**
>
> Contribute to hirotakaster/TlsTcpClient development by creating an account on GitHub.

But this is for a TCP client. Particle uses UDP on the Electron, and I sure don’t want to be doing any TCP comms if I can help it.

I was thinking that we could encrypt our messages using AES 256, then throwing it across the wire as a publish. Is it even possible? Is this something I can do in software, or am I going to need to specify a new hardware encryption part to do it for me?

Interested in people’s take on this.

---

<div class="post-metadata">

**Author:** ![hirotakaster](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hirotakaster/32/2223_2.png) [@hirotakaster](https://community.particle.io/u/hirotakaster)\
**Post date:** [February 1, 2019, 5:44am UTC](https://community.particle.io/t/aes-256-options/47437/2 "2019-02-01T05:44:56Z")

</div>

Hi @mblackler  
Here is sample code for AES 256 bits tested on WebIDE&Photon.

```auto
#include "application.h"

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

void setup() {
    Serial.begin(9600);

    unsigned char key[32];
    unsigned char iv_enc[16] = {'i', 'n', 'i', 't', 'i', 'a', 'l', 'v', 'e', 'c', 't', 'o', 'r', ' ', ' ', ' '};
    unsigned char iv_dec[16] = {'i', 'n', 'i', 't', 'i', 'a', 'l', 'v', 'e', 'c', 't', 'o', 'r', ' ', ' ', ' '};
    unsigned char encbuff[128];
    unsigned char decbuff[128];
    unsigned char str[128];

    memset(str, 0, sizeof(str));
    memset(encbuff, 0, sizeof(encbuff));
    memset(decbuff, 0, sizeof(decbuff));
    
    for (int i = 0; i < 32; i++) key[i] = i;
    strcpy((char *)str, "Some text for AES 256 bytes :)");
    
    mbedtls_aes_context aes_ctx;
    
    mbedtls_aes_init(&aes_ctx);
    mbedtls_aes_setkey_enc(&aes_ctx, key, 256);
    mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, 128, iv_enc, str, encbuff);

    mbedtls_aes_setkey_dec(&aes_ctx, key, 256);
    mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, 128, iv_dec, encbuff, decbuff);
    mbedtls_aes_free(&aes_ctx);
    Serial.print("Dec:"); Serial.println((char *)decbuff);

}

void loop() {
    delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![mblackler](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@mblackler](https://community.particle.io/u/mblackler)\
**Post date:** [February 1, 2019, 1:25pm UTC](https://community.particle.io/t/aes-256-options/47437/3 "2019-02-01T13:25:32Z")

</div>

That’s excellent, thanks so much! Exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![developer\_bt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/developer_bt/32/1311_2.png) [@developer\_bt](https://community.particle.io/u/developer_bt)\
**Post date:** [February 4, 2019, 3:10pm UTC](https://community.particle.io/t/aes-256-options/47437/4 "2019-02-04T15:10:13Z")

</div>

Hi @hirotakaster!  
Do you have an example for mbedtls\_aes\_crypt\_cfb128 function.  
[https://tls.mbed.org/api/aes\_8h.html#a944946adabbc344f2c6cf6e6f51a21e3](https://tls.mbed.org/api/aes_8h.html#a944946adabbc344f2c6cf6e6f51a21e3)  
Which parameter need for iv\_off?

---

<div class="post-metadata">

**Author:** ![hirotakaster](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hirotakaster/32/2223_2.png) [@hirotakaster](https://community.particle.io/u/hirotakaster)\
**Post date:** [February 4, 2019, 3:30pm UTC](https://community.particle.io/t/aes-256-options/47437/5 "2019-02-04T15:30:09Z")

</div>

hi @developer_bt,  
sorry, I don’t have a mbedtls\_aes\_crypt\_cfb128 example, but maybe you could find iv\_off parameter requirements from AES source code.(0-15 int value)

> <https://github.com/ARMmbed/mbedtls/blob/development/library/aes.c>

---

<div class="post-metadata">

**Author:** ![developer\_bt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/developer_bt/32/1311_2.png) [@developer\_bt](https://community.particle.io/u/developer_bt)\
**Post date:** [February 6, 2019, 9:17am UTC](https://community.particle.io/t/aes-256-options/47437/6 "2019-02-06T09:17:10Z")

</div>

Thanks @hirotakaster!  
I tried with standard value for v\_off = 8 and work perfectly 🙂
