diff --git a/Makefile b/Makefile index 89ea424..ff4daae 100644 --- a/Makefile +++ b/Makefile @@ -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 -all: +compile: cd crypto; make all; cd thirdparty; make all; ar rcs libp2p.a $(OBJS) +test: compile + cd test; make all; + +all: test + clean: cd crypto; make clean; cd thirdparty; make clean + cd test; make clean; rm -rf libp2p.a \ No newline at end of file diff --git a/crypto/encoding/x509.c b/crypto/encoding/x509.c index 457c140..fdea373 100644 --- a/crypto/encoding/x509.c +++ b/crypto/encoding/x509.c @@ -38,14 +38,19 @@ int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey* private // set the values in the context mbedtls_mpi N; mbedtls_mpi_init(&N); - mbedtls_mpi_lset(&N, private_key->prime1); + mbedtls_mpi_lset(&N, private_key->N); mbedtls_mpi 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->E = E; + rsa->Q = Q; rsa->len = ( mbedtls_mpi_bitlen(&rsa->N ) + 7) >> 3; // 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(&E); + mbedtls_mpi_free(&Q); mbedtls_rsa_free(rsa); mbedtls_pk_free(&ctx); diff --git a/crypto/rsa.c b/crypto/rsa.c index 02130a6..91f2c1e 100644 --- a/crypto/rsa.c +++ b/crypto/rsa.c @@ -57,6 +57,15 @@ int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long retVal = 1; // 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.exponent = *(rsa.E.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 private_key->precomputed_values.dp = *(rsa.DP.p); private_key->precomputed_values.dq = *(rsa.DQ.p); - + */ retVal = 1; exit: diff --git a/include/libp2p/crypto/rsa.h b/include/libp2p/crypto/rsa.h index f96e40c..2f3d8bc 100644 --- a/include/libp2p/crypto/rsa.h +++ b/include/libp2p/crypto/rsa.h @@ -28,11 +28,21 @@ struct PrecomputedValues { }; struct RsaPrivateKey { + /* The old struct RsaPublicKey public_key; unsigned long long private_exponent; unsigned long long prime1; unsigned long long prime2; 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; }; /** diff --git a/multihash/multihash.c b/multihash_old/multihash.c similarity index 100% rename from multihash/multihash.c rename to multihash_old/multihash.c diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..6c212ce --- /dev/null +++ b/test/Makefile @@ -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 diff --git a/test/crypto/test_base58.h b/test/crypto/test_base58.h index 6830e24..f0e56dc 100644 --- a/test/crypto/test_base58.h +++ b/test/crypto/test_base58.h @@ -73,4 +73,21 @@ int test_base58_max_size() { 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 */ diff --git a/test/crypto/test_mbedtls.h b/test/crypto/test_mbedtls.h new file mode 100644 index 0000000..1eab9cf --- /dev/null +++ b/test/crypto/test_mbedtls.h @@ -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 + +#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; +} diff --git a/test/crypto/test_rsa.h b/test/crypto/test_rsa.h index 110ad7a..6196ed5 100644 --- a/test/crypto/test_rsa.h +++ b/test/crypto/test_rsa.h @@ -27,8 +27,9 @@ int test_crypto_rsa_public_key_bytes() { crypto_rsa_generate_keypair(&private_key, 2048); // save it to a text file (converting the primes to text) FILE* temp_file = fopen("/tmp/public_key.txt", "w"); - fprintf(temp_file, "%llu\n", private_key.public_key.modulus); - fprintf(temp_file, "%llu", private_key.public_key.exponent); + fprintf(temp_file, "%llu\n", private_key.P); + fprintf(temp_file, "%llu\n", private_key.E); + fprintf(temp_file, "%llu", private_key.Q); fclose(temp_file); // TODO: get what go produces // compare what go produces to ours. @@ -38,8 +39,8 @@ int test_crypto_rsa_public_key_bytes() { int test_crypto_x509_private_to_der() { struct RsaPrivateKey private_key; - private_key.prime1 = 6908992579533823845; - private_key.private_exponent = 65537; + private_key.P = 6908992579533823845; + private_key.E = 65537; //crypto_rsa_generate_keypair(&private_key, 2048); unsigned char buffer[1600]; @@ -62,4 +63,6 @@ int test_crypto_x509_der_to_private() { return 1; } + + #endif /* test_rsa_h */ diff --git a/test/testit.c b/test/testit.c index 0c2795f..28d6a42 100644 --- a/test/testit.c +++ b/test/testit.c @@ -2,8 +2,9 @@ #include #include "crypto/test_rsa.h" -#include "multihash/test_multihash.h" +//#include "multihash/test_multihash.h" #include "crypto/test_base58.h" +#include "crypto/test_mbedtls.h" int testit(const char* name, int (*func)(void)) { 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_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_multihash_encode", test_multihash_encode); - testit("test_multihash_decode", test_multihash_decode); - testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_decode); - testit("test_multihash_base58_decode", test_multihash_base58_decode); + //testit("test_multihash_encode", test_multihash_encode); + //testit("test_multihash_decode", test_multihash_decode); + //testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_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_size", test_base58_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; }