Write MPI values in SPIFFS file

16aa00
Posts: 11
Joined: Tue Sep 04, 2018 12:03 pm

Write MPI values in SPIFFS file

Postby 16aa00 » Tue Sep 18, 2018 7:31 am

Hello,

I have generated RSA key pairs in ESP32 using mbedtls APIs. The key values are exported to MPIs. I want to copy the values of MPI in the SPIFFS file. I tried to follow this to copy values of MPI in SPIFFS file
https://tls.mbed.org/api/bignum_8h.html ... c9328e1324
I don't know how to pass the output file handle(4th argument) to the mbedtls_mpi_write_file. Could anyone of you help me how to create or pass the output file handle of a SPIFFS file? Or is there any other way to copy the values of MPI in file in ESP32?
I already created and opened a SPIFFS file with write permission. I don't want to use external SD card to save the file.

Any help or suggestion will be appreciated.

Thank You

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Write MPI values in SPIFFS file

Postby ESP_Angus » Tue Sep 18, 2018 7:50 am

Can you post the incomplete code that you have working now?

16aa00
Posts: 11
Joined: Tue Sep 04, 2018 12:03 pm

Re: Write MPI values in SPIFFS file

Postby 16aa00 » Tue Sep 18, 2018 7:56 am

Yes sure

Code: Select all

#include "mbedtls/rsa.h"
#include "mbedtls/pk.h"
#include "mbedtls/sha1.h"
#include "mbedtls/platform.h"
#include "mbedtls/config.h"
#include "mbedtls/oid.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/x509.h"
#include "mbedtls/error.h"
#include<string.h>
#include "mbedtls/md.h"
#include "mbedtls/entropy.h"
#include "mbedtls/bignum.h"
#include "SPIFFS.h"
#include "FS.h"
#include <SD.h>

#include <WiFi.h>
#include <SPI.h>
#include <ESP32WebServer.h>

#define KEY_SIZE 2048
#define EXPONENT 65537
#define mbedtls_printf printf

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  
  int ret = 1;
  int exit_code = MBEDTLS_EXIT_FAILURE;
  mbedtls_rsa_context rsa;
  mbedtls_entropy_context entropy;
  mbedtls_ctr_drbg_context ctr_drbg;
  mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  const char *pers = "rsa_genkey";

  mbedtls_ctr_drbg_init( &ctr_drbg );
  mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );

  mbedtls_printf( "\n  . Seeding the random number generator..." );

  mbedtls_entropy_init( &entropy );
  if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
                             (const unsigned char *) pers,
                             strlen( pers ) ) ) != 0 )
  {
      mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
      //goto exit;
  }

  mbedtls_printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
                                     EXPONENT ) ) != 0 )
  {
      mbedtls_printf( " failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret );
  }

  mbedtls_printf( " ok\n  . Exporting the public key in pub-key.txt...." );

  if( ( ret = mbedtls_rsa_export    ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
        ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) )      != 0 )
  {
      mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" );
      //goto exit;
  }

  mbedtls_printf("done export\n");
  
  //write MPI in file

}

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

}

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Write MPI values in SPIFFS file

Postby ESP_Angus » Wed Sep 19, 2018 12:28 am

The additional code you need is something like this (this is uncompiled and untested, so may need some tweaking before it works 100%):

Code: Select all

   SPIFFS.begin();
   
   FILE *mpi_file = fopen("/spiffs/numbers.txt", "w");
   mbedtls_mpi_write_file(NULL, &N, 16, mpi_file);
   mbedtls_mpi_write_file(NULL, &P, 16, mpi_file);
   mbedtls_mpi_write_file(NULL, &Q, 16, mpi_file);
   // etc, etc
   fclose(mpi_file);
To read back:

Code: Select all

   FILE *mpi_file = fopen("/spiffs/numbers.txt", "r");
   mbedtls_mpi_read_file(&N, 16, mpi_file);
   mbedtls_mpi_read_file(&P, 16, mpi_file);
   mbedtls_mpi_read_file(&Q, 16, mpi_file);
   // etc, etc
   fclose(mpi_file);
(mpi_write_file appends a newline at the end, and mpi_read_file reads until a newline or EOF is reached.)

16aa00
Posts: 11
Joined: Tue Sep 04, 2018 12:03 pm

Re: Write MPI values in SPIFFS file

Postby 16aa00 » Wed Sep 19, 2018 2:54 am

Thank you very much ESP_Angus!! It worked for me.

16aa00
Posts: 11
Joined: Tue Sep 04, 2018 12:03 pm

Re: Write MPI values in SPIFFS file

Postby 16aa00 » Sat Sep 22, 2018 2:54 am

Hi,

The method to read and write in SPIFFS file worked me but now I am facing an error while loading the saved file. I need to load the saved file to generate Certificate Signing Request using mbedtls API's. I am following this example
https://github.com/ARMmbed/mbedtls/blob ... cert_req.c

While trying to parse the key file using mbedtls_pk_parse_keyfile(), error -15616 is returned. Could you please help me know what that error means and how to solve?

Any help is appreciated.

Thank you

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 64 guests