c-libp2p/crypto/rsa.c

100 lines
2.4 KiB
C
Raw Normal View History

2016-11-03 14:18:32 +00:00
//
// rsa.c
// c-libp2p
//
// Created by John Jones on 11/3/16.
// Copyright © 2016 JMJAtlanta. All rights reserved.
//
#include <stdio.h>
2016-11-03 16:15:18 +00:00
#include <string.h>
#include "libp2p/crypto/rsa.h"
// mbedtls stuff
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/bignum.h"
#include "mbedtls/x509.h"
#include "mbedtls/rsa.h"
int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair) {
mbedtls_rsa_context rsa;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
int exponent = 65537;
int retVal = 1;
const char *pers = "rsa_genkey";
// initialize mbedtls structs
mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_entropy_init( &entropy );
// seed the routines
if( ( retVal = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
{
retVal = 0;
goto exit;
}
// initialize the rsa struct
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
// finally, generate the key
if( ( retVal = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, (unsigned int)num_bits_for_keypair,
exponent ) ) != 0 )
{
retVal = 0;
goto exit;
}
retVal = 1;
// fill in values of structures
2016-11-09 16:22:25 +00:00
private_key->D = *(rsa.D.p);
private_key->DP = *(rsa.DP.p);
private_key->DQ = *(rsa.DQ.p);
private_key->E = *(rsa.E.p);
private_key->N = *(rsa.N.p);
private_key->P = *(rsa.P.p);
private_key->Q = *(rsa.Q.p);
private_key->QP = *(rsa.QP.p);
/*
2016-11-03 16:15:18 +00:00
private_key->public_key.modulus = *(rsa.N.p);
private_key->public_key.exponent = *(rsa.E.p);
private_key->prime1 = *(rsa.DP.p);
private_key->prime2 = *(rsa.Q.p);
private_key->private_exponent = *(rsa.D.p);
//TODO: fill in the rest of the precomputed values
private_key->precomputed_values.dp = *(rsa.DP.p);
private_key->precomputed_values.dq = *(rsa.DQ.p);
2016-11-09 16:22:25 +00:00
*/
2016-11-03 16:15:18 +00:00
retVal = 1;
exit:
mbedtls_rsa_free( &rsa );
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );
return retVal;
}
2016-11-07 20:11:58 +00:00
/**
* convert a struct RsaPublicKey into a stream of bytes compatible with x509 PKI
* @param public_key the public key struct
* @param bytes the results
* @param bytes_size the length of the results
*/
int crypto_rsa_public_key_bytes(struct RsaPublicKey* public_key, char** bytes, unsigned long long* bytes_size) {
// marshal to x509
return 0;
}