Added method to rsa sign a message, using pkcs 1.15
This commit is contained in:
parent
7e7a4e0712
commit
9a7c494436
6 changed files with 48 additions and 10 deletions
|
@ -22,7 +22,7 @@ int libp2p_crypto_encoding_base16_encode(const unsigned char* incoming, size_t i
|
||||||
*results_length = 0;
|
*results_length = 0;
|
||||||
for(int i = 0; i < incoming_length; i++) {
|
for(int i = 0; i < incoming_length; i++) {
|
||||||
unsigned char buf[3];
|
unsigned char buf[3];
|
||||||
sprintf(buf, "%02x", incoming[i]);
|
sprintf((char*)buf, "%02x", incoming[i]);
|
||||||
results[i * 2] = buf[0];
|
results[i * 2] = buf[0];
|
||||||
results[i * 2 + 1] = buf[1];
|
results[i * 2 + 1] = buf[1];
|
||||||
*results_length += 2;
|
*results_length += 2;
|
||||||
|
@ -56,7 +56,7 @@ int libp2p_crypto_encoding_base16_decode(const unsigned char* incoming, size_t i
|
||||||
|
|
||||||
memset(results, 0, *results_length);
|
memset(results, 0, *results_length);
|
||||||
|
|
||||||
unsigned char* pos = (char*)incoming;
|
char* pos = (char*)incoming;
|
||||||
|
|
||||||
for(int i = 0; i < incoming_length / 2; i++) {
|
for(int i = 0; i < incoming_length / 2; i++) {
|
||||||
sscanf(pos, "%2hhx", &results[i]);
|
sscanf(pos, "%2hhx", &results[i]);
|
||||||
|
|
|
@ -637,9 +637,9 @@ base32_decode_alloc_ctx (struct base32_decode_context *ctx,
|
||||||
int libp2p_crypto_encoding_base32_encode(const unsigned char* incoming, size_t incoming_length,
|
int libp2p_crypto_encoding_base32_encode(const unsigned char* incoming, size_t incoming_length,
|
||||||
unsigned char* results, size_t* results_length) {
|
unsigned char* results, size_t* results_length) {
|
||||||
memset(results, 0, *results_length);
|
memset(results, 0, *results_length);
|
||||||
base32_encode(incoming, incoming_length, results, *results_length);
|
base32_encode((char*)incoming, incoming_length, (char*)results, *results_length);
|
||||||
if (results[ (*results_length) -1] == 0)
|
if (results[ (*results_length) -1] == 0)
|
||||||
*results_length = strlen(results);
|
*results_length = strlen((char*)results);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -664,7 +664,7 @@ size_t libp2p_crypto_encoding_base32_encode_size(size_t incoming_length) {
|
||||||
*/
|
*/
|
||||||
int libp2p_crypto_encoding_base32_decode(const unsigned char* incoming, size_t incoming_length,
|
int libp2p_crypto_encoding_base32_decode(const unsigned char* incoming, size_t incoming_length,
|
||||||
unsigned char* results, size_t* results_length) {
|
unsigned char* results, size_t* results_length) {
|
||||||
int retVal = base32_decode_ctx(NULL, incoming, incoming_length, results, results_length);
|
int retVal = base32_decode_ctx(NULL, (char*)incoming, incoming_length, (char*)results, results_length);
|
||||||
|
|
||||||
// unknown error
|
// unknown error
|
||||||
if (results == NULL)
|
if (results == NULL)
|
||||||
|
|
30
crypto/rsa.c
30
crypto/rsa.c
|
@ -2,6 +2,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "libp2p/crypto/rsa.h"
|
#include "libp2p/crypto/rsa.h"
|
||||||
|
#include "libp2p/crypto/sha256.h"
|
||||||
|
|
||||||
// mbedtls stuff
|
// mbedtls stuff
|
||||||
#include "mbedtls/config.h"
|
#include "mbedtls/config.h"
|
||||||
|
@ -187,7 +188,7 @@ int libp2p_crypto_rsa_private_key_fill_public_key(struct RsaPrivateKey* private_
|
||||||
// first build the rsa context
|
// first build the rsa context
|
||||||
mbedtls_pk_context ctx;
|
mbedtls_pk_context ctx;
|
||||||
mbedtls_pk_init(&ctx);
|
mbedtls_pk_init(&ctx);
|
||||||
mbedtls_pk_parse_key(&ctx, private_key->der, private_key->der_length, NULL, 0);
|
mbedtls_pk_parse_key(&ctx, (unsigned char*)private_key->der, private_key->der_length, NULL, 0);
|
||||||
|
|
||||||
// buffer
|
// buffer
|
||||||
size_t buffer_size = 1600;
|
size_t buffer_size = 1600;
|
||||||
|
@ -227,3 +228,30 @@ int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sign a message
|
||||||
|
* @param private_key the private key
|
||||||
|
* @param message the message to be signed
|
||||||
|
* @param message_length the length of message
|
||||||
|
* @param result the resultant signature. Note: should be pre-allocated and be the size of the private key (i.e. 2048)
|
||||||
|
* @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 );
|
||||||
|
return retVal == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,4 +45,14 @@ int libp2p_crypto_rsa_private_key_fill_public_key(struct RsaPrivateKey* private_
|
||||||
*/
|
*/
|
||||||
int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key);
|
int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sign a message
|
||||||
|
* @param private_key the private key
|
||||||
|
* @param message the message to be signed
|
||||||
|
* @param message_length the length of message
|
||||||
|
* @param result the resultant signature. Note: should be pre-allocated and be the size of the private key (i.e. 2048)
|
||||||
|
* @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);
|
||||||
|
|
||||||
#endif /* rsa_h */
|
#endif /* rsa_h */
|
||||||
|
|
|
@ -77,7 +77,7 @@ int test_base58_peer_address() {
|
||||||
unsigned char* ptr_to_result = result_buffer;
|
unsigned char* ptr_to_result = result_buffer;
|
||||||
memset(result_buffer, 0, result_buffer_length);
|
memset(result_buffer, 0, result_buffer_length);
|
||||||
// now get the decoded address
|
// now get the decoded address
|
||||||
int return_value = libp2p_crypto_encoding_base58_decode(x_data, x_data_length, &ptr_to_result, &result_buffer_length);
|
int return_value = libp2p_crypto_encoding_base58_decode((unsigned char*)x_data, x_data_length, &ptr_to_result, &result_buffer_length);
|
||||||
if (return_value == 0)
|
if (return_value == 0)
|
||||||
return 0;
|
return 0;
|
||||||
// add 2 bytes to the front for the varint
|
// add 2 bytes to the front for the varint
|
||||||
|
|
|
@ -135,7 +135,7 @@ int test_crypto_rsa_public_key_to_peer_id() {
|
||||||
memset(decode_base64, 0, decode_base64_size);
|
memset(decode_base64, 0, decode_base64_size);
|
||||||
unsigned char* ptr = decode_base64;
|
unsigned char* ptr = decode_base64;
|
||||||
|
|
||||||
int retVal = libp2p_crypto_encoding_base64_decode(orig_priv_key, strlen(orig_priv_key), ptr, decode_base64_size, &decode_base64_size);
|
int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)orig_priv_key, strlen(orig_priv_key), ptr, decode_base64_size, &decode_base64_size);
|
||||||
if (retVal == 0)
|
if (retVal == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -158,7 +158,7 @@ int test_crypto_rsa_public_key_to_peer_id() {
|
||||||
|
|
||||||
// 3) grab the public key, hash it, then base58 it
|
// 3) grab the public key, hash it, then base58 it
|
||||||
unsigned char hashed[32];
|
unsigned char hashed[32];
|
||||||
ID_FromPK_non_null_terminated(hashed, private_key.public_key_der, private_key.public_key_length);
|
ID_FromPK_non_null_terminated((char*)hashed, (unsigned char*)private_key.public_key_der, private_key.public_key_length);
|
||||||
size_t final_id_size = 1600;
|
size_t final_id_size = 1600;
|
||||||
unsigned char final_id[final_id_size];
|
unsigned char final_id[final_id_size];
|
||||||
memset(final_id, 0, final_id_size);
|
memset(final_id, 0, final_id_size);
|
||||||
|
@ -170,7 +170,7 @@ int test_crypto_rsa_public_key_to_peer_id() {
|
||||||
if (orig_peer_id_size != final_id_size)
|
if (orig_peer_id_size != final_id_size)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (strncmp(orig_peer_id, final_id, final_id_size) != 0)
|
if (strncmp(orig_peer_id, (char*)final_id, final_id_size) != 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue