devF: AES256 Single Operation Demo ( beta ) with esp-idf

User avatar
rudi ;-)
Posts: 1729
Joined: Fri Nov 13, 2015 3:25 pm

devF: AES256 Single Operation Demo ( beta ) with esp-idf

Postby rudi ;-) » Sun Aug 28, 2016 2:52 am

Hi guys,

had a try in the AES Accelerator. just in time only in code, compile and link. we waiting for the release modul.
Using Espressif IoT Development Framework with the ESP32. For more Information about read in developer preview thread.

because its preview state, it can be, there are small mistake in this demo code, not sure how to handle the AES_IDLE_REG,
so if i know more, i will then update here. my try workaround you can read here i changed here in this demo to uint32_t

what was done:
we have one task for simple "ping"
and we have one plaintext/cleartext cyphering and one decyphering crypttext

read in the esp32 technical reference manual there is a AES Accelerator chapter.

the functions was found in components folder for esp32, there are headers for ROM, here used ROM/aes256.h
be sure you have change the line for AES_ENDIAN_REG from "ets_set_endian" to "ets_aes_set_endian"

i used for this demo devF .
devF is a Flowchart design programm, that can do flowchart to code, its a flowchart to code generator.
it is a small application for creating program flowchart, source code generation, and compilation.
Its main goal is to be straightforward, educational tool for use during structural programming classes.
devF is freeware and the src code is on github. there comes an own webside for devF and demos for esp asap,
also the links to repo, help, programm leader and so on. btw: devF was written in delphi ;) you can use d7 personal example.

now the AES256 demo:
start with main_app, task, Vars and Function:

here is it:
main_task_vars_funcs.png
main_task_vars_funcs.png (63.52 KiB) Viewed 13057 times

the enc function:
aesdevF_demo_enc.png
aesdevF_demo_enc.png (28.35 KiB) Viewed 13057 times
the dec function:
aesdevF_demo_dec.png
aesdevF_demo_dec.png (26.21 KiB) Viewed 13057 times
after we call the code editor and press code generat, we get this code:

Code: Select all


#include <stdint.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "rom/aes.h"


const uint8_t key[32]       = "ola_not_public_viewed_here_;-)_!";
const uint8_t cryptText[16] = "esp32 aes done !";
uint8_t cryptOuttext[16]    = "";
uint8_t clearOuttext[16]    = "";
uint8_t i;
bool key_word_swap;
bool key_byte_swap;
bool in_word_swap;
bool in_byte_swap;
bool out_word_swap;
bool out_byte_swap;
extern uint32_t AES_IDLE_REG;


/******************************************************************************
 * FunctionName : pingTask
 * Description  : Task for outprint "ping" as example 
 * Parameters   : void* pvParameters
 * Returns      : void
*******************************************************************************/
void pingTask(void* pvParameters)
{
  while (1)
  {
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    printf("ping\n");
  }
}

/******************************************************************************
 * FunctionName : decr_ciphertext
 * Description  : decrypt the cipher text to cleartext
 * Parameters   : 
 * Returns      : void
*******************************************************************************/
void decr_ciphertext()
{
  ets_aes_setkey_dec( (uint8_t *) key, AES256);
  
  key_word_swap = false;
  key_byte_swap = false;
  in_word_swap  = false;
  in_byte_swap  = false;
  out_word_swap = false;
  out_byte_swap = false;
  
  ets_aes_set_endian( key_word_swap, key_byte_swap,
                      in_word_swap, in_byte_swap,
                      out_word_swap, out_byte_swap);
  ets_aes_enable();
  ets_aes_crypt( (uint8_t *) cryptOuttext, (uint8_t *)  clearOuttext);
  while (AES_IDLE_REG !=1)
  {
    
    // we wait for AES_IDLE
    
  }
  ets_aes_disable();
  printf(" our cleartext as string : ");
  printf("%s", clearOuttext);
  printf("\n done!\n");
}

/******************************************************************************
 * FunctionName : enc_plaintext
 * Description  : encrypt cleartext to ciphertext
 * Parameters   : 
 * Returns      : void
*******************************************************************************/
void enc_plaintext()
{
  ets_aes_setkey_enc( (uint8_t *) key, AES256);
  
  key_word_swap = false;
  key_byte_swap = false;
  in_word_swap  = false;
  in_byte_swap  = false;
  out_word_swap = false;
  out_byte_swap = false;
  
  ets_aes_set_endian( key_word_swap, key_byte_swap,
                      in_word_swap, in_byte_swap,
                      out_word_swap, out_byte_swap);

  ets_aes_enable();
  ets_aes_crypt( (uint8_t *) cryptText, (uint8_t *)  cryptOuttext);
  while (AES_IDLE_REG !=1)
  {
    
    // we wait for AES_IDLE
    
  }
  ets_aes_disable();
  printf(" our ciphertext in hex: ");
  for (i = 0; i < 16; i++)
  {
    printf("%2x ", cryptOuttext[i]);
  }
  printf("\n done!\n");
}

/******************************************************************************
 * FunctionName : app_main
 * Description  : ESP32 app main - first called function
 * Parameters   : 
 * Returns      : void
*******************************************************************************/
void app_main()
{
  enc_plaintext();
  decr_ciphertext();
  xTaskCreatePinnedToCore(&pingTask, "pingTask", 2048, NULL, 5, NULL, 0);
}


you can open the code in browser hereas html side


after press compile, devF calls the "Windows script" from esp-idf, compile and link it.
i used in this demo - single app and bootloader. the menuconfig in esp-idf is amazing!

To flash all build output, must run 'make flash' in devF or on console:
( for your folder / paths - you must use your folder / paths )

python c:/sdk32/esp-idf/bin/esptool.py --chip esp32 --port COM20 --baud 230400 write_flash
0x1000 /c/sdk32devF/esp-devF/build/bootloader/bootloader.bin
0x10000 /c/sdk32devF/esp-devF/build/app-template.bin
0x4000 /c/sdk32devF/esp-devF/build/partitions_singleapp.bin

perhabs someone has a esp32 release, he can use the code and have a try.
because the limit is 3 attachment in this - i will push one thread with bin files.
let me know, if something wrong - because i can not flash just in time,
i am waiting for the released modul.

have phun!

best wishes
rudi ;-)
Last edited by rudi ;-) on Sun Aug 28, 2016 3:27 am, edited 2 times in total.
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

User avatar
rudi ;-)
Posts: 1729
Joined: Fri Nov 13, 2015 3:25 pm

Re: beta: AES256 Single Operation Demo with esp-idf and devF

Postby rudi ;-) » Sun Aug 28, 2016 2:58 am

push - bin file attached
generated with the new Espressif IoT Development Framework for the ESP32 Release
build.zip
0x1000 bootloader.bin,
0x10000 app-template.bin,
0x4000 partitions_singleapp.bin,
(210.56 KiB) Downloaded 827 times
best wishes
rudi ;-)

done!
:mrgreen:
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

User avatar
rudi ;-)
Posts: 1729
Joined: Fri Nov 13, 2015 3:25 pm

Re: devF: AES256 Single Operation Demo ( beta ) with esp-idf

Postby rudi ;-) » Fri Sep 16, 2016 8:26 am

FYI:

There is a chance ,

Add comment to ROM crypto functions recommending they not be used directly

Code: Select all

/*
 ROM functions for hardware AES support.

  It is not recommended to use these functions directly,
  use the wrapper functions in hwcrypto/aes.h instead.

 */
see here too
Match API completely to mbedTLS naming conventions

After i get my ESP32 Modul, i will update the beta code .

best wishes
rudi ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: No registered users and 20 guests