diff --git a/crypto/encoding/base32.c b/crypto/encoding/base32.c index 25b9bdd..b11a35f 100644 --- a/crypto/encoding/base32.c +++ b/crypto/encoding/base32.c @@ -636,8 +636,10 @@ 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); - *results_length = strlen(results); + if (results[ (*results_length) -1] == 0) + *results_length = strlen(results); return 1; } @@ -649,7 +651,7 @@ int libp2p_crypto_encoding_base32_encode(const unsigned char* incoming, size_t i size_t libp2p_crypto_encoding_base32_encode_size(size_t incoming_length) { incoming_length -= 5; incoming_length /= 5; - return incoming_length * 8; + return incoming_length * 8 + 1; } /** diff --git a/crypto/encoding/x509.c b/crypto/encoding/x509.c index 647ccaf..cf374bb 100644 --- a/crypto/encoding/x509.c +++ b/crypto/encoding/x509.c @@ -7,6 +7,7 @@ // #include +#include #include #include "mbedtls/rsa.h" @@ -92,7 +93,10 @@ int libp2p_crypto_encoding_x509_der_to_private_key(unsigned char* der, size_t de } // now put the public DER format in. - private_key->der = der; + private_key->der = malloc(sizeof(char) * der_length); + if (private_key->der == NULL) + return 0; + memcpy(private_key->der, der, der_length); private_key->der_length = der_length; //NOTE: the public DER stuff is done in rsa.c diff --git a/crypto/rsa.c b/crypto/rsa.c index ed35136..b5d1929 100644 --- a/crypto/rsa.c +++ b/crypto/rsa.c @@ -204,18 +204,20 @@ int libp2p_crypto_rsa_private_key_fill_public_key(struct RsaPrivateKey* private_ // generate public key der int retVal = libp2p_crypto_rsa_write_public_key_der(&ctx, buffer, &buffer_size); + mbedtls_pk_free(&ctx); if (retVal == 0) { - mbedtls_pk_free(&ctx); 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); + 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); - mbedtls_pk_free(&ctx); return 1; } diff --git a/test/Makefile b/test/Makefile index ba0629c..37300d6 100644 --- a/test/Makefile +++ b/test/Makefile @@ -24,3 +24,5 @@ all: all_others testit_libp2p clean: rm -f *.o rm -f testit_libp2p + +test: clean testit_libp2p \ No newline at end of file diff --git a/test/crypto/test_base32.h b/test/crypto/test_base32.h new file mode 100644 index 0000000..06a6e1a --- /dev/null +++ b/test/crypto/test_base32.h @@ -0,0 +1,21 @@ +#include "libp2p/crypto/encoding/base32.h" + +int test_crypto_encoding_base32_encode() { + size_t input_size = 1000; + unsigned char input[input_size]; + int minus = 0; + for(int i = 0; i < input_size; i++) { + if (input_size > 0 && input_size % 255 == 0) { + minus += 255; + } + input[i] = input_size - minus; + } + + size_t results_size = libp2p_crypto_encoding_base32_encode_size(input_size); + unsigned char results[results_size]; + int retVal = libp2p_crypto_encoding_base32_encode(input, input_size, &results[0], &results_size); + if (retVal == 0) + return 0; + return 1; + +} diff --git a/test/testit.c b/test/testit.c index 0561429..30f8815 100644 --- a/test/testit.c +++ b/test/testit.c @@ -4,6 +4,7 @@ #include "crypto/test_rsa.h" #include "crypto/test_base58.h" +#include "crypto/test_base32.h" #include "test_mbedtls.h" int testit(const char* name, int (*func)(void)) { @@ -17,6 +18,7 @@ int testit(const char* name, int (*func)(void)) { } int main(int argc, char** argv) { + /* testit("test_public_der_to_private_der", test_public_der_to_private_der); testit("test_mbedtls_varint_128_binary", test_mbedtls_varint_128_binary); testit("test_mbedtls_varint_128_string", test_mbedtls_varint_128_string); @@ -34,6 +36,8 @@ int main(int argc, char** argv) { testit("test_base58_max_size", test_base58_max_size); testit("test_base58_peer_address", test_base58_peer_address); //testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der); + */ + testit("test_crypto_encoding_base32_encode", test_crypto_encoding_base32_encode); return 1; }