B4R Question Inline C: Byte Array -> uint8_t

KMatle

Expert
Licensed User
Longtime User
In Globals I set a Byte Array to

B4X:
Public ivx(16) As Byte = Array As Byte(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

In the C section I can convert and use it (works by using the converted ivu variable)

B4X:
char *ivc = (char*)b4r_main::_ivx->data;

uint8_t ivu[16];
//Set IV
    int i;
    for( i = 0; i < 16; i++ )
    {
    ivu[i]=ivc[i];
    }

Now I would like to access ivx directly from globals:

B4X:
esp_aes_crypt_cbc( &ctx, ESP_AES_ENCRYPT, b4r_main::_dec_len, (uint8_t*)b4r_main::_ivx->data, (uint8_t*)b4r_main::_aesdecrypted->data, (uint8_t*)b4r_main::_aesencrypted->data );

Works fine for all other variables but not for (uint8_t*)b4r_main::_ivx->data which gives valid but incorrect values. Must be unsigned char iv[16]

Can someone please point me in the right direction?

Method (from the ESP32 docs):

B4X:
int esp_aes_crypt_cbc( esp_aes_context *ctx,
                       int mode,
                       size_t length,
                       unsigned char iv[16],
                       const unsigned char *input,
                       unsigned char *output )
{
...
 

KMatle

Expert
Licensed User
Longtime User
You aren't doing anything with ivu. Start with a test. Create arrays in the C code and fill them with data. Encrypt it and see whether it returns the expected result.

Sorry for the irritation... I tried a bit...

1. When I use ivu, everything is fine
2. I want to use (uint8_t*)b4r_main::_ivx->data (=ivx from globals) which doesn't work (should be all 1's but "some " bytes are used -> data conversion?)
3. The AES method needs it as unsigned char iv[16]

Q: I need to use ivx from globals directly (defined as a byte array). How can I do that? Which format do I have to use?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
1. Best if you upload the project.

Should be simple:

Just take a look at the esp_aes_crypt_cbc method. Here I use e.g. (uint8_t*)b4r_main::_aesdecrypted->data directly from Globals = fine

Now I want to use IV also from Globals. It's definded as

B4X:
Public iv(16) As Byte = Array As Byte(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1) 'just for a test with all one's

I don't get how to use it directly.

To help myself out, I created ivc inside the inline c code and managed it to get it working by converting it in the loop ("Set IV") but as I've said, I want to use IV directly from Globals (and I have no idea).

The docs of ESP32 say that it must be unsigned char iv[16]


B4X:
#if C
#
#include <mbedtls/aes.h>
#include "mbedtls/md.h"

char ivc[16];

void Encrypt (B4R::Object* o) {

char *pw = (char*)b4r_main::_pw->data;
char *ivc = (char*)b4r_main::_iv->data; //don't want that


//256Hash the PW (32 Bytes)
  byte shaResult[32];
  mbedtls_md_context_t hash;
  mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
  const size_t pwLength = strlen(pw);         
  mbedtls_md_init(&hash);
  mbedtls_md_setup(&hash, mbedtls_md_info_from_type(md_type), 0);
  mbedtls_md_starts(&hash);
  mbedtls_md_update(&hash, (const unsigned char *) pw, pwLength);
  mbedtls_md_finish(&hash, shaResult);
  mbedtls_md_free(&hash);
  uint8_t key[32];
  memcpy(key, shaResult, 32); 
 //*********************************************************************************** 
    
//Set IV don't want that, too
    uint8_t ivu[16];
    int i;
    for( i = 0; i < 16; i++ )
    {
    ivu[i]=ivc[i];
    }

esp_aes_context ctx;
esp_aes_init( &ctx );
esp_aes_setkey( &ctx, key, 256 );
printf("Before encryption...\n");
esp_aes_crypt_cbc( &ctx, ESP_AES_ENCRYPT, b4r_main::_dec_len, ivu, (uint8_t*)b4r_main::_aesdecrypted->data, (uint8_t*)b4r_main::_aesencrypted->data );
esp_aes_free( &ctx );

}
 
Upvote 0
Top