c-libp2p/crypto/rsa.c

260 lines
8 KiB
C
Raw Normal View History

2016-11-03 14:18:32 +00:00
#include <stdio.h>
2016-11-03 16:15:18 +00:00
#include <string.h>
#include "libp2p/crypto/rsa.h"
#include "libp2p/crypto/sha256.h"
2016-11-03 16:15:18 +00:00
// 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"
#include "mbedtls/asn1write.h"
2016-11-10 17:04:48 +00:00
#include "mbedtls/oid.h"
#include "mbedtls/pk.h"
2016-11-03 16:15:18 +00:00
/**
* Take an rsa context and turn it into a der formatted byte stream.
* NOTE: the stream starts from the right. So there could be a lot of padding in front.
* Pay attention to the returned size to cut the padding.
* @param rsa the rsa key to encode
* @param buf where to put the bytes
* @param size the max size of the buffer. The actual size used is returned in this value
* @returns true(1) on success, else 0
*/
2016-11-10 17:04:48 +00:00
int libp2p_crypto_rsa_write_private_key_der( mbedtls_rsa_context *rsa, unsigned char *buf, size_t* size )
{
int ret;
unsigned char *c = buf + *size;
size_t len = 0;
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->QP ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DQ ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DP ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->Q ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->P ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->D ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->E ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->N ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
MBEDTLS_ASN1_SEQUENCE ) );
*size = len;
return 1;
}
2016-11-10 17:04:48 +00:00
/**
* Take a context and turn it into a der formatted byte stream.
* @param key the key
* @param buf the buffer to be filled
* @param size the max size of the buffer. The actual size used is returned in this value
* @returns true(1) on success, else false(0)
*/
int libp2p_crypto_rsa_write_public_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t* size )
{
int ret;
unsigned char *c;
size_t len = 0, par_len = 0, oid_len;
const char *oid;
c = buf + *size;
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
if( c - buf < 1 ) // buffer is too small
return 0;
/*
* SubjectPublicKeyInfo ::= SEQUENCE {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT STRING }
*/
*--c = 0;
len += 1;
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
&oid, &oid_len ) ) != 0 )
{
return 0;
}
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
par_len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
MBEDTLS_ASN1_SEQUENCE ) );
*size = len;
return 1;
}
/***
* Generate an RSA keypair of a certain size, and place the results in the struct
* @param private_key where to put the results
* @param num_bits_for_keypair the number of bits for the key, 1024 is the minimum
* @returns true(1) on success
*/
2016-11-10 17:04:48 +00:00
int libp2p_crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair) {
2016-11-03 16:15:18 +00:00
mbedtls_rsa_context rsa;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
int exponent = 65537;
int retVal = 1;
unsigned char* buffer;
2016-11-03 16:15:18 +00:00
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);
size_t buffer_size = 1600;
buffer = malloc(sizeof(char) * buffer_size);
2016-11-10 17:04:48 +00:00
retVal = libp2p_crypto_rsa_write_private_key_der(&rsa, buffer, &buffer_size);
if (retVal == 0)
return 0;
2016-11-10 17:04:48 +00:00
// allocate memory for the private key der
private_key->der_length = buffer_size;
2016-11-10 17:04:48 +00:00
private_key->der = malloc(sizeof(char) * buffer_size);
// add in the der to the buffer
memcpy(private_key->der, &buffer[1600-buffer_size], buffer_size);
2016-11-03 16:15:18 +00:00
exit:
if (buffer != NULL)
free(buffer);
2016-11-03 16:15:18 +00:00
mbedtls_rsa_free( &rsa );
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );
if (retVal != 0) {
// now do the public key.
retVal = libp2p_crypto_rsa_private_key_fill_public_key(private_key);
}
2016-11-03 16:15:18 +00:00
return retVal;
}
2016-11-07 20:11:58 +00:00
/**
* Use the private key DER to fill in the public key DER
* @param private_key the private key to use
* @reutrns true(1) on success
*/
int libp2p_crypto_rsa_private_key_fill_public_key(struct RsaPrivateKey* private_key) {
// first build the rsa context
mbedtls_pk_context ctx;
mbedtls_pk_init(&ctx);
mbedtls_pk_parse_key(&ctx, (unsigned char*)private_key->der, private_key->der_length, NULL, 0);
// buffer
size_t buffer_size = 1600;
unsigned char buffer[buffer_size];
memset(buffer, 0, buffer_size);
// generate public key der
int retVal = libp2p_crypto_rsa_write_public_key_der(&ctx, buffer, &buffer_size);
2016-12-01 18:05:14 +00:00
mbedtls_pk_free(&ctx);
if (retVal == 0) {
return 0;
}
// allocate memory for the public key der
private_key->public_key_length = buffer_size;
private_key->public_key_der = malloc(sizeof(char) * buffer_size);
2016-12-01 18:05:14 +00:00
if (private_key->public_key_der == NULL) {
return 0;
}
//copy it into the struct
memcpy(private_key->public_key_der, &buffer[1600-buffer_size], buffer_size);
return 1;
}
/***
* Free resources used by RsaPrivateKey
* @param private_key the resources
2016-11-10 17:04:48 +00:00
* @returns true(1)
2016-11-07 20:11:58 +00:00
*/
2016-11-10 17:04:48 +00:00
int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key) {
if (private_key->der != NULL)
free(private_key->der);
2016-11-10 17:04:48 +00:00
if (private_key->public_key_der != NULL)
free(private_key->public_key_der);
return 1;
2016-11-07 20:11:58 +00:00
}
/**
* sign a message
* @param private_key the private key
* @param message the message to be signed
* @param message_length the length of message
2017-01-20 09:53:55 +00:00
* @param result the resultant signature. Note: should be pre-allocated and be the size of the private key (i.e. 2048 bit key can store a sig in 256 bytes)
* @returns true(1) on successs, otherwise false(0)
*/
int libp2p_crypto_rsa_sign(struct RsaPrivateKey* private_key, unsigned char* message, size_t message_length, unsigned char* result) {
unsigned char output[32];
libp2p_crypto_hashing_sha256(message, message_length, output);
mbedtls_rsa_context ctx;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V15, 0);
mbedtls_ctr_drbg_init(&ctr_drbg);
int retVal = mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx,
mbedtls_ctr_drbg_random,
&ctr_drbg,
MBEDTLS_RSA_PRIVATE,
MBEDTLS_MD_SHA256,
32,
output,
result );
mbedtls_ctr_drbg_free(&ctr_drbg);
//mbetdls_rsa_free(ctx);
return retVal == 0;
}