base58 and x509 certificate testing

This commit is contained in:
John Jones 2016-11-09 11:22:25 -05:00
parent 6f3be52801
commit e0714cc76d
10 changed files with 184 additions and 14 deletions

View file

@ -2,13 +2,19 @@ OBJS = crypto/rsa.o crypto/encoding/asn1.o crypto/encoding/base58.o crypto/encod
crypto/encoding/x509.o thirdparty/mbedtls/*.o crypto/encoding/x509.o thirdparty/mbedtls/*.o
all: compile:
cd crypto; make all; cd crypto; make all;
cd thirdparty; make all; cd thirdparty; make all;
ar rcs libp2p.a $(OBJS) ar rcs libp2p.a $(OBJS)
test: compile
cd test; make all;
all: test
clean: clean:
cd crypto; make clean; cd crypto; make clean;
cd thirdparty; make clean cd thirdparty; make clean
cd test; make clean;
rm -rf libp2p.a rm -rf libp2p.a

View file

@ -38,14 +38,19 @@ int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey* private
// set the values in the context // set the values in the context
mbedtls_mpi N; mbedtls_mpi N;
mbedtls_mpi_init(&N); mbedtls_mpi_init(&N);
mbedtls_mpi_lset(&N, private_key->prime1); mbedtls_mpi_lset(&N, private_key->N);
mbedtls_mpi E; mbedtls_mpi E;
mbedtls_mpi_init(&E); mbedtls_mpi_init(&E);
mbedtls_mpi_lset(&E, private_key->private_exponent); mbedtls_mpi_lset(&E, private_key->E);
mbedtls_mpi Q;
mbedtls_mpi_init(&Q);
mbedtls_mpi_lset(&Q, private_key->Q);
rsa->N = N; rsa->N = N;
rsa->E = E; rsa->E = E;
rsa->Q = Q;
rsa->len = ( mbedtls_mpi_bitlen(&rsa->N ) + 7) >> 3; rsa->len = ( mbedtls_mpi_bitlen(&rsa->N ) + 7) >> 3;
// now write to DER // now write to DER
@ -53,6 +58,7 @@ int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey* private
mbedtls_mpi_free(&N); mbedtls_mpi_free(&N);
mbedtls_mpi_free(&E); mbedtls_mpi_free(&E);
mbedtls_mpi_free(&Q);
mbedtls_rsa_free(rsa); mbedtls_rsa_free(rsa);
mbedtls_pk_free(&ctx); mbedtls_pk_free(&ctx);

View file

@ -57,6 +57,15 @@ int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long
retVal = 1; retVal = 1;
// fill in values of structures // fill in values of structures
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);
/*
private_key->public_key.modulus = *(rsa.N.p); private_key->public_key.modulus = *(rsa.N.p);
private_key->public_key.exponent = *(rsa.E.p); private_key->public_key.exponent = *(rsa.E.p);
private_key->prime1 = *(rsa.DP.p); private_key->prime1 = *(rsa.DP.p);
@ -66,7 +75,7 @@ int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long
//TODO: fill in the rest of the precomputed values //TODO: fill in the rest of the precomputed values
private_key->precomputed_values.dp = *(rsa.DP.p); private_key->precomputed_values.dp = *(rsa.DP.p);
private_key->precomputed_values.dq = *(rsa.DQ.p); private_key->precomputed_values.dq = *(rsa.DQ.p);
*/
retVal = 1; retVal = 1;
exit: exit:

View file

@ -28,11 +28,21 @@ struct PrecomputedValues {
}; };
struct RsaPrivateKey { struct RsaPrivateKey {
/* The old
struct RsaPublicKey public_key; struct RsaPublicKey public_key;
unsigned long long private_exponent; unsigned long long private_exponent;
unsigned long long prime1; unsigned long long prime1;
unsigned long long prime2; unsigned long long prime2;
struct PrecomputedValues precomputed_values; struct PrecomputedValues precomputed_values;
*/
unsigned long long QP;
unsigned long long DQ;
unsigned long long DP;
unsigned long long Q;
unsigned long long P;
unsigned long long D;
unsigned long long E;
unsigned long long N;
}; };
/** /**

17
test/Makefile Normal file
View file

@ -0,0 +1,17 @@
CC = gcc
CFLAGS = -O0 -I../include -I. -g3
LFLAGS = -L../
DEPS = crypto/test_base58.h crypto/test_mbedtls.h crypto/test_rsa.h
OBJS = testit.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
testit_libp2p: $(OBJS)
$(CC) -o $@ $(OBJS) $(LFLAGS) -lp2p -lm
all: testit_libp2p
clean:
rm -f *.o
rm testit_libp2p

View file

@ -73,4 +73,21 @@ int test_base58_max_size() {
return 1; return 1;
} }
int test_base58_peer_address() {
char* x_data = "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB";
size_t x_data_length = strlen(x_data);
size_t result_buffer_length = libp2p_crypto_encoding_base58_decode_max_size(x_data);
unsigned char result_buffer[result_buffer_length];
unsigned char* ptr_to_result = result_buffer;
// now get the decoded address
int return_value = libp2p_crypto_encoding_base58_decode(x_data, x_data_length, &ptr_to_result, &result_buffer_length);
if (return_value == 0)
return 0;
// throw everything in a hex string so we can debug the results
//for(int i = 0; i < x_data_length; i++)
// printf("%02x", result_buffer[i]);
//printf("\n");
return 1;
}
#endif /* test_base58_h */ #endif /* test_base58_h */

View file

@ -0,0 +1,99 @@
/**
* These are mainly functions to reverse engineer some of the
* mbedtls stuff to make sure we're storing and retrieving
* something that mbedtls can use
*/
#include <string.h>
#include "mbedtls/asn1write.h"
#include "mbedtls/rsa.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
// taken from mbedtls/programs/pkwrite.c
int mbedtls_pk_write_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 ) );
return( (int) len );
}
int mbedtls_generate_key(mbedtls_rsa_context* ctx) {
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;
}
// finally, generate the key
if( ( retVal = mbedtls_rsa_gen_key( ctx, mbedtls_ctr_drbg_random, &ctr_drbg, (unsigned int)2046,
exponent ) ) != 0 )
{
retVal = 0;
goto exit;
}
retVal = 1;
exit:
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );
return retVal;
}
int test_mbedtls_pk_write_key_der() {
// generate private key
mbedtls_rsa_context key;
mbedtls_rsa_init( &key, MBEDTLS_RSA_PKCS_V15, 0 );
mbedtls_generate_key(&key);
// write it out in a section of memory in der format
size_t size = 1600;
unsigned char buf[size];
memset(buf, 0, size);
int retVal = mbedtls_pk_write_key_der(&key, buf, size);
// examine it
printf("Size: %d\n", retVal);
for(int i = retVal-1; i < size; i++)
printf("%02x", buf[i]);
printf("\n");
// use it
// free it
mbedtls_rsa_free( &key );
return 1;
}

View file

@ -27,8 +27,9 @@ int test_crypto_rsa_public_key_bytes() {
crypto_rsa_generate_keypair(&private_key, 2048); crypto_rsa_generate_keypair(&private_key, 2048);
// save it to a text file (converting the primes to text) // save it to a text file (converting the primes to text)
FILE* temp_file = fopen("/tmp/public_key.txt", "w"); FILE* temp_file = fopen("/tmp/public_key.txt", "w");
fprintf(temp_file, "%llu\n", private_key.public_key.modulus); fprintf(temp_file, "%llu\n", private_key.P);
fprintf(temp_file, "%llu", private_key.public_key.exponent); fprintf(temp_file, "%llu\n", private_key.E);
fprintf(temp_file, "%llu", private_key.Q);
fclose(temp_file); fclose(temp_file);
// TODO: get what go produces // TODO: get what go produces
// compare what go produces to ours. // compare what go produces to ours.
@ -38,8 +39,8 @@ int test_crypto_rsa_public_key_bytes() {
int test_crypto_x509_private_to_der() { int test_crypto_x509_private_to_der() {
struct RsaPrivateKey private_key; struct RsaPrivateKey private_key;
private_key.prime1 = 6908992579533823845; private_key.P = 6908992579533823845;
private_key.private_exponent = 65537; private_key.E = 65537;
//crypto_rsa_generate_keypair(&private_key, 2048); //crypto_rsa_generate_keypair(&private_key, 2048);
unsigned char buffer[1600]; unsigned char buffer[1600];
@ -62,4 +63,6 @@ int test_crypto_x509_der_to_private() {
return 1; return 1;
} }
#endif /* test_rsa_h */ #endif /* test_rsa_h */

View file

@ -2,8 +2,9 @@
#include <stdio.h> #include <stdio.h>
#include "crypto/test_rsa.h" #include "crypto/test_rsa.h"
#include "multihash/test_multihash.h" //#include "multihash/test_multihash.h"
#include "crypto/test_base58.h" #include "crypto/test_base58.h"
#include "crypto/test_mbedtls.h"
int testit(const char* name, int (*func)(void)) { int testit(const char* name, int (*func)(void)) {
printf("Testing %s...\n", name); printf("Testing %s...\n", name);
@ -19,14 +20,16 @@ int main(int argc, char** argv) {
//testit("test_crypto_rsa_public_key_bytes", test_crypto_rsa_public_key_bytes); //testit("test_crypto_rsa_public_key_bytes", test_crypto_rsa_public_key_bytes);
//testit("test_crypto_x509_private_to_der", test_crypto_x509_private_to_der); //testit("test_crypto_x509_private_to_der", test_crypto_x509_private_to_der);
//testit("test_crypto_x509_der_to_private", test_crypto_x509_der_to_private); //testit("test_crypto_x509_der_to_private", test_crypto_x509_der_to_private);
testit("test_multihash_encode", test_multihash_encode); //testit("test_multihash_encode", test_multihash_encode);
testit("test_multihash_decode", test_multihash_decode); //testit("test_multihash_decode", test_multihash_decode);
testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_decode); //testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_decode);
testit("test_multihash_base58_decode", test_multihash_base58_decode); //testit("test_multihash_base58_decode", test_multihash_base58_decode);
//testit("test_multihash_size", test_multihash_size);
testit("test_base58_encode_decode", test_base58_encode_decode); testit("test_base58_encode_decode", test_base58_encode_decode);
testit("test_base58_size", test_base58_size); testit("test_base58_size", test_base58_size);
testit("test_base58_max_size", test_base58_max_size); testit("test_base58_max_size", test_base58_max_size);
testit("test_multihash_size", test_multihash_size); testit("test_base58_peer_address", test_base58_peer_address);
testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der);
return 1; return 1;
} }