This commit is contained in:
John Jones 2017-01-23 11:31:43 -05:00
commit 8bdc9332d4
10 changed files with 139 additions and 11 deletions

View file

@ -2,13 +2,14 @@
DEBUG = true
export DEBUG
OBJS = crypto/*.o crypto/encoding/*.o thirdparty/mbedtls/*.o hashmap/hashmap.o
OBJS = crypto/*.o crypto/encoding/*.o thirdparty/mbedtls/*.o hashmap/hashmap.o record/*.o
compile:
cd crypto; make all;
cd thirdparty; make all;
cd hashmap; make all;
cd record; make all;
ar rcs libp2p.a $(OBJS)
test: compile
@ -21,5 +22,6 @@ clean:
cd hashmap; make clean;
cd thirdparty; make clean
cd test; make clean;
cd record; make clean;
rm -rf libp2p.a

View file

@ -22,7 +22,7 @@ int libp2p_crypto_encoding_base16_encode(const unsigned char* incoming, size_t i
*results_length = 0;
for(int i = 0; i < incoming_length; i++) {
unsigned char buf[3];
sprintf(buf, "%02x", incoming[i]);
sprintf((char*)buf, "%02x", incoming[i]);
results[i * 2] = buf[0];
results[i * 2 + 1] = buf[1];
*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);
unsigned char* pos = (char*)incoming;
char* pos = (char*)incoming;
for(int i = 0; i < incoming_length / 2; i++) {
sscanf(pos, "%2hhx", &results[i]);

View file

@ -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,
unsigned char* results, size_t* 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)
*results_length = strlen(results);
*results_length = strlen((char*)results);
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,
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
if (results == NULL)

View file

@ -2,6 +2,7 @@
#include <string.h>
#include "libp2p/crypto/rsa.h"
#include "libp2p/crypto/sha256.h"
// mbedtls stuff
#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
mbedtls_pk_context 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
size_t buffer_size = 1600;
@ -227,3 +228,30 @@ int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key) {
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 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 );
return retVal == 0;
}

View file

@ -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);
/**
* 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 */

View file

@ -0,0 +1,7 @@
#ifndef LIBP2P_RECORD_H
#define LIBP2P_RECORD_H
#define RECORD_BUFSIZE 1024
int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign);
#endif // LIBP2P_RECORD_H

14
record/Makefile Normal file
View file

@ -0,0 +1,14 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -g3
LFLAGS =
DEPS =
OBJS = record.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
all: $(OBJS)
clean:
rm -f $(OBJS)

67
record/record.c Normal file
View file

@ -0,0 +1,67 @@
#include <string.h>
#include <stdlib.h>
#include "libp2p/crypto/rsa.h"
#include "libp2p/record/record.h"
#include "protobuf.h"
#include "mh/hashes.h"
#include "mh/multihash.h"
// libp2p_record_make_put_record creates and signs a dht record for the given key/value pair
int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign)
{
char *pkh, *p;
int pkh_len;
size_t len = 0, l;
*record = NULL; *rec_size = 0;
p = malloc(RECORD_BUFSIZE);
if (p) {
memset (p, '\0', len);
if (!protobuf_encode_string (1, WIRETYPE_LENGTH_DELIMITED, key, p, RECORD_BUFSIZE, &l)) {
free (p);
return -1;
}
len += l;
if (!protobuf_encode_length_delimited (2, WIRETYPE_LENGTH_DELIMITED, value, vlen, p+len, RECORD_BUFSIZE-len, &l)) {
free (p);
return -1;
}
len += l;
pkh_len = mh_new_length(MH_H_SHA2_256, sk->public_key_length);
pkh = malloc(pkh_len);
if (!pkh) {
free (p);
return -1;
}
if (mh_new(pkh, MH_H_SHA2_256, sk->public_key_der, sk->public_key_length)) {
free (pkh);
free (p);
return -1;
}
if (!protobuf_encode_length_delimited (3, WIRETYPE_LENGTH_DELIMITED, pkh, pkh_len, p+len, RECORD_BUFSIZE-len, &l)) {
free (pkh);
free (p);
return -1;
}
free (pkh);
len += l;
if (sign) {
//TODO: missing signature function at libp2p-crypto ?
//sign(sk, signature, p, len);
//proto encode signature.
free (pkh);
free (p);
return -1; // not implemented.
}
}
*record = realloc(p, len); // Reduces memory used for just what is needed.
if (*record) {
*rec_size = len;
} else {
free (p);
return -1;
}
return 0; // sucess.
}

View file

@ -77,7 +77,7 @@ int test_base58_peer_address() {
unsigned char* ptr_to_result = result_buffer;
memset(result_buffer, 0, result_buffer_length);
// 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)
return 0;
// add 2 bytes to the front for the varint

View file

@ -135,7 +135,7 @@ int test_crypto_rsa_public_key_to_peer_id() {
memset(decode_base64, 0, decode_base64_size);
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)
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
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;
unsigned char final_id[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)
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 1;