ikalchev / kpabe-yct14-cpp

A lightweight Key-Policy Attribute-Based Encryption scheme in C++
Apache License 2.0
19 stars 4 forks source link

kp abe on ESP32 #2

Closed a91Vidal closed 5 years ago

a91Vidal commented 5 years ago

Hello, I am interested on testing the kp abe on an esp32 device. I would like to know how to load the dependencies (GMP and PBC are not part of the FreeRT-OS from espressif idf). Can you make a branch with the modifications for the kp abe to run on the esp32 please? Thanks in advance

ikalchev commented 5 years ago

Hey!

Certainly, I just need to dig out the commands/changes - will need a few days for that.

I remember I had to implement the entropy source to use the ESP32 true random number generator and maybe a few other nits. Will keep posted.

Best, Ivan

a91Vidal commented 5 years ago

Hello, Thanks for your answer. Maybe if this is too complicated to have a good entropy on the esp32, it can be possible to generate the key pairs on a laptop and then load the public key / private key when flashing the esp32? My main issue is to get GMP (and PBC) library running on the esp32. Best regards

ikalchev commented 5 years ago

Actually this is more complicated I think. You can find the change below. BTW you can just return the same number every time as well (if you don't care for security and want to just test something).

Here is some info before I get my stuff together and do a branch for this. It is possible the commands could have been shorter though (this was for a master's thesis after all, who has the time).

First, you will need the xtensa toolchain. You can get it with platformio for example (arduino IDE may also have it). Let me know if you have trouble with this.

Then you need to cross-compile gmp and pbc for esp32. I am giving you wrappers around the ./configure, you need to do the usual make && make install after that.

Building gmp (tested with 6.1.2; you have to change the paths where necessary)

#!/bin/bash
# Don't forget:
# - Change HAS_OBSTACK_VNPRITNF and HAVE_NL_LANGINFO to 0 from config.h
# - (optional) Change HAS_ALLOCA and HAS_ALLOCA_H to 0 (we want to avoid stack allocation on the esp)
# - Change HAVE_RAISE to 0 (see invalid.c)
./configure \
  --enable-static \
  --disable-shared \
  --host=xtensa \
  --prefix=/Users/ivankalchev/Dev/xtensa-esp32-elf \
  AR=/Users/ivankalchev/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-ar \
  ARFLAGS=rcs \
  RANLIB=/Users/ivankalchev/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-ranlib \
  CC=~/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-gcc \
  CFLAGS="-Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -Os -g3 -nostdlib -Wpointer-arith -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -mlongcalls -ffunction-sections -fdata-sections -fstrict-volatile-bitfields" \
  CXX=~/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-g++ \
  CXXFLAGS="-fno-rtti -fno-exceptions -std=gnu++11" \
  LIBS="" \
  LDFLAGS="-L/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/lib -L/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/ld -nostdlib -Wl,-static -Wl,-gc-sections -Wl,-EL -Wl,--undefined=uxTopUsedPriority"

Building PBC (tested with 0.5.14):

Modify arith/init_random.c - pbc_init_random

#include <stdio.h>
#include <stdint.h> // for intptr_t
#include <stdlib.h>
#include <gmp.h>
#include "pbc_utils.h"
#include "pbc_random.h"

#include <esp_system.h>

void esp32_randfunc(mpz_t num, mpz_t limit, void* data)
{
  UNUSED_VAR(data);
  UNUSED_VAR(limit);
  uint32_t n = esp_random();
  mpz_init_set_ui(num, n);
}

void pbc_init_random(void)
{
  pbc_random_set_function(esp32_randfunc, NULL);
}
#!/bin/bash
# The many includes in CPPFLAGS are all because of arith/init_random.c, as it is modified to use the random number generator from the esp platform.
# Don't forget:
# - Change HAVE_ALLOCA and HAVE_ALLOCA_H to 0 (we don't want to use stack allocation on the esp)

TOOLCHAIN="/Users/ivankalchev/.platformio/packages/toolchain-xtensa32/bin/xtensa-esp32-elf-"

./configure \
  --enable-static \
  --disable-shared \
  --host=xtensa \
  --prefix=/Users/ivankalchev/Dev/xtensa-esp32-elf \
  AR="$TOOLCHAIN"ar \
  ARFLAGS=rcs \
  AS="$TOOLCHAIN"as \
  RANLIB="$TOOLCHAIN"ranlib \
  NM="$TOOLCHAIN"nm \
  CC="$TOOLCHAIN"gcc \
  CFLAGS="-Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -Os -g3 -nostdlib -Wpointer-arith -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -mlongcalls -ffunction-sections -fdata-sections -fstrict-volatile-bitfields" \
  CPPFLAGS="-mlongcalls -fno-exceptions -I/Users/ivankalchev/Dev/xtensa-esp32-elf/include -I/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/esp32/ -I/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/driver/ -I/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/soc/ -I/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/freertos/" \
  LIBS="" \
  LDFLAGS="-L/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/lib -L/Users/ivankalchev/.platformio/packages/framework-arduinoespressif32/tools/sdk/ld -L/Users/ivankalchev/Dev/xtensa-esp32-elf/lib -nostdlib -Wl,--gc-sections -Wl,-EL -Wl,--undefined=uxTopUsedPriority"

Now you can use the two libs and link them in an esp32 image. You may want to increase the stack size of the free RTOS that is running on the esp32, as these crypto libs pass some rather big chunks around (otherwise you will get a non-deterministic stack overflow).

It's been a while since I tried this, so I may have forgotten something. Let me know how it goes, I will try to refresh my memory and do it myself as well.

Best, Ivan

a91Vidal commented 5 years ago

Awesome, I managed to install it thanks to your help. I also had to define _HAVE_LOCALEINFO 1 in both config.h (pbc and gmp) Many thanks

ikalchev commented 5 years ago

Happy to hear that :)

WueSePrantl commented 4 years ago

Hi,

i am dealing with the same problem, only that i want to use the pbc library on an esp8266. I tried to follow your description but i am not sure how to integrate the generate libs and if i have done everything right.

Some background details: I am using Ubuntu 16.04.6 LTS, PlatformIO with core 4.3.3 and Home 3.2.2, gmp in version 6.1.2 and pbc in version 0.5.14. I have created an new project in PlatformIO for NodeMCU 1.0 (ESP-12E-Module) and Arduino. I also create the folder crossCompile on the desktop. Also i use gcc in version 5.4.0.

I tried to modify your scripts so that they fit to my paths and for the eps8266. Below are the text version of the used scripts (so that i can upload them here).

gmp-script.txt pbc-script.txt

I put them in the gmp/pbc folder and executed them and then changed the parts in the config.h files. For pbc also the arith/init_random.c - pbc_init_random should be modfied, but there i had the problem, that #include could not be found and i had to leave it out, in order that it can be compiled.

I think that i modifed the scripts in the wrong way, since the esp_system could not be found. And how can i use the generate libraries? I copied them in the lib directory of the PlatformIO directory and tried to compile the following simple script:

#include <stdbool.h>
#include "pbc.h"
 #include "pbc_test.h"

// #define A_TYPE_PARAMS "type a\nq 8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791\nh 12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776\nr 730750818665451621361119245571504901405976559617\nexp2 159\nexp1 107\nsign1 1\nsign0 1"

void setup() {
  pairing_t pp;
  pairing_init_set_str(pp, A_TYPE_PARAMS);
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

The script failed compiling, since it could not finde pbc.h. So i also added the headers from the crossCompile folder to the PlatformIO so that pbc.h could be founded, but then i got the error, that the function pairing_init_set_str could not be found.

I hope you can help me, even if the topic is older and thanks for your help :)