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
Write MPI values in SPIFFS file
Re: Write MPI values in SPIFFS file
Can you post the incomplete code that you have working now?
Re: Write MPI values in SPIFFS file
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:
}
Re: Write MPI values in SPIFFS file
The additional code you need is something like this (this is uncompiled and untested, so may need some tweaking before it works 100%):
To read back:
(mpi_write_file appends a newline at the end, and mpi_read_file reads until a newline or EOF is reached.)
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);
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);
Re: Write MPI values in SPIFFS file
Thank you very much ESP_Angus!! It worked for me.
Re: Write MPI values in SPIFFS file
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
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: No registered users and 53 guests