Added base16 encoding/decoding, as well as a wrapper around SHA256
This commit is contained in:
parent
9222925d49
commit
4222316a1a
14 changed files with 299 additions and 56 deletions
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ DEBUG = true
|
||||||
export DEBUG
|
export DEBUG
|
||||||
|
|
||||||
OBJS = crypto/rsa.o crypto/encoding/base58.o crypto/encoding/base64.o \
|
OBJS = crypto/rsa.o crypto/encoding/base58.o crypto/encoding/base64.o \
|
||||||
crypto/encoding/x509.o thirdparty/mbedtls/*.o
|
crypto/encoding/x509.o thirdparty/mbedtls/*.o crypto/encoding/base16.o
|
||||||
|
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
|
|
|
@ -2,8 +2,8 @@ CC = gcc
|
||||||
CFLAGS = -O0 -I../../include -g3
|
CFLAGS = -O0 -I../../include -g3
|
||||||
LFLAGS =
|
LFLAGS =
|
||||||
DEPS = ../../include/libp2p/crypto/encoding/base58.h ../../include/libp2p/crypto/encoding/base64.h \
|
DEPS = ../../include/libp2p/crypto/encoding/base58.h ../../include/libp2p/crypto/encoding/base64.h \
|
||||||
../../include/libp2p/crypto/encoding/x509.h
|
../../include/libp2p/crypto/encoding/x509.h ../../include/libp2p/crypto/encoding/base16.h
|
||||||
OBJS = base58.o base64.o x509.o
|
OBJS = base58.o base64.o x509.o base16.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
76
crypto/encoding/base16.c
Normal file
76
crypto/encoding/base16.c
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "libp2p/crypto/encoding/base16.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode in Base16 format
|
||||||
|
* @param incoming the incoming bytes
|
||||||
|
* @param incoming_length the length of the incoming bytes
|
||||||
|
* @param results where to put the results
|
||||||
|
* @param results_length the size of the buffer, and returns the actual length used
|
||||||
|
* @returns true(1) on success
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_encode(const unsigned char* incoming, size_t incoming_length, unsigned char* results, size_t* results_length) {
|
||||||
|
// the size will be 2x the size of incoming, so check that
|
||||||
|
if (*results_length < incoming_length * 2)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// clear out the results variable
|
||||||
|
memset(results, 0, *results_length);
|
||||||
|
|
||||||
|
*results_length = 0;
|
||||||
|
for(int i = 0; i < incoming_length; i++) {
|
||||||
|
unsigned char buf[3];
|
||||||
|
sprintf(buf, "%02x", incoming[i]);
|
||||||
|
results[i * 2] = buf[0];
|
||||||
|
results[i * 2 + 1] = buf[1];
|
||||||
|
*results_length += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the size of the buffer necessary to encode
|
||||||
|
* @param incoming_length the length of the incoming value
|
||||||
|
* @returns the size of the buffer necessary to hold the encoded bytes
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_encode_size(size_t incoming_length) {
|
||||||
|
return incoming_length * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode from Base16 format
|
||||||
|
* @param incoming the incoming base16 encoded string
|
||||||
|
* @param incoming_length the length of the incoming string (no need to include null)
|
||||||
|
* @param results where to put the results
|
||||||
|
* @param results_length the size of the buffer, and returns the actual length used
|
||||||
|
* @returns true(1) on success
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_decode(const unsigned char* incoming, size_t incoming_length, unsigned char* results, size_t* results_length) {
|
||||||
|
|
||||||
|
// buffer too small
|
||||||
|
if (*results_length < incoming_length / 2)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
memset(results, 0, *results_length);
|
||||||
|
|
||||||
|
unsigned char* pos = (char*)incoming;
|
||||||
|
|
||||||
|
for(int i = 0; i < incoming_length / 2; i++) {
|
||||||
|
sscanf(pos, "%2hhx", &results[i]);
|
||||||
|
pos += 2;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the size of the buffer necessary to decode
|
||||||
|
* @param incoming_length the length of the incoming value
|
||||||
|
* @returns the size of the buffer necessary to hold the decoded bytes
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_decode_size(size_t incoming_length) {
|
||||||
|
return incoming_length / 2;
|
||||||
|
}
|
||||||
|
|
|
@ -170,32 +170,27 @@ int libp2p_crypto_encoding_base58_encode(const unsigned char* data, size_t binsz
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/**
|
||||||
* calculate the size of the binary results based on an incoming base58 string
|
* calculate the max length in bytes of an encoding of n source bytes
|
||||||
* @param base58_string the string
|
* @param encoded_size the size of the encoded string
|
||||||
* @returns the size in bytes had the string been decoded
|
* @returns the maximum size in bytes had the string been decoded
|
||||||
*/
|
*/
|
||||||
size_t libp2p_crypto_encoding_base58_decode_size(const unsigned char* base58_string) {
|
size_t libp2p_crypto_encoding_base58_decode_size(size_t encoded_size) {
|
||||||
size_t string_length = strlen((char*)base58_string);
|
|
||||||
size_t decoded_length = 0;
|
|
||||||
size_t radix = strlen(b58digits_ordered);
|
size_t radix = strlen(b58digits_ordered);
|
||||||
double bits_per_digit = log2(radix);
|
double bits_per_digit = log2(radix); // each char represents about 6 bits
|
||||||
|
|
||||||
decoded_length = floor(string_length * bits_per_digit / 8);
|
return ceil(encoded_size * bits_per_digit / 8);
|
||||||
return decoded_length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* calculate the max length in bytes of an encoding of n source bits
|
* calculate the max length in bytes of a decoding of n source bytes
|
||||||
* @param base58_string the string
|
* @param decoded_size the size of the incoming string to be encoded
|
||||||
* @returns the maximum size in bytes had the string been decoded
|
* @returns the maximum size in bytes had the string been encoded
|
||||||
*/
|
*/
|
||||||
size_t libp2p_crypto_encoding_base58_decode_max_size(const unsigned char* base58_string) {
|
size_t libp2p_crypto_encoding_base58_encode_size(size_t decoded_size) {
|
||||||
size_t string_length = strlen((char*)base58_string);
|
|
||||||
size_t decoded_length = 0;
|
|
||||||
size_t radix = strlen(b58digits_ordered);
|
size_t radix = strlen(b58digits_ordered);
|
||||||
double bits_per_digit = log2(radix);
|
double bits_per_digit = log2(radix);
|
||||||
|
// each character
|
||||||
|
|
||||||
decoded_length = ceil(string_length * bits_per_digit / 8);
|
return ceil( 8 / bits_per_digit * decoded_size);
|
||||||
return decoded_length;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "mbedtls/base64.h"
|
#include "mbedtls/base64.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,12 +25,6 @@ int libp2p_crypto_encoding_base64_encode(const unsigned char* input_data, size_t
|
||||||
return retVal == 0;
|
return retVal == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t libp2p_crypto_encoding_base64_encode_length(const unsigned char* input_data, size_t input_length) {
|
|
||||||
size_t req_bytes;
|
|
||||||
mbedtls_base64_encode(NULL, 0, &req_bytes, input_data, input_length);
|
|
||||||
return req_bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* decode something that was encoded as base64
|
* decode something that was encoded as base64
|
||||||
* @param input_data the data to decode
|
* @param input_data the data to decode
|
||||||
|
@ -43,8 +39,27 @@ int libp2p_crypto_encoding_base64_decode(const unsigned char* input_data, size_t
|
||||||
return retVal == 0;
|
return retVal == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t libp2p_crypto_encoding_base64_decode_length(const unsigned char* input_data, size_t input_length) {
|
/**
|
||||||
size_t req_bytes;
|
* calculate the max length in bytes of an encoding of n source bytes
|
||||||
mbedtls_base64_decode(NULL, 0, &req_bytes, input_data, input_length);
|
* @param encoded_size the size of the encoded string
|
||||||
return req_bytes;
|
* @returns the maximum size in bytes had the string been decoded
|
||||||
|
*/
|
||||||
|
size_t libp2p_crypto_encoding_base64_decode_size(size_t encoded_size) {
|
||||||
|
size_t radix = 64;
|
||||||
|
double bits_per_digit = log2(radix); // each char represents about 6 bits
|
||||||
|
|
||||||
|
return ceil(encoded_size * bits_per_digit / 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* calculate the max length in bytes of a decoding of n source bytes
|
||||||
|
* @param decoded_size the size of the incoming string to be encoded
|
||||||
|
* @returns the maximum size in bytes had the string been encoded
|
||||||
|
*/
|
||||||
|
size_t libp2p_crypto_encoding_base64_encode_size(size_t decoded_size) {
|
||||||
|
size_t radix = 64;
|
||||||
|
double bits_per_digit = log2(radix);
|
||||||
|
|
||||||
|
return ceil( 8 / bits_per_digit * decoded_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
35
include/libp2p/crypto/encoding/base16.h
Normal file
35
include/libp2p/crypto/encoding/base16.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode in Base16 format
|
||||||
|
* @param incoming the incoming bytes
|
||||||
|
* @param incoming_length the length of the incoming bytes
|
||||||
|
* @param results where to put the results
|
||||||
|
* @param results_length the size of the buffer, and returns the actual length used
|
||||||
|
* @returns true(1) on success
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_encode(const unsigned char* incoming, size_t incoming_length, unsigned char* results, size_t* results_length);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the size of the buffer necessary to encode
|
||||||
|
* @param incoming_length the length of the incoming value
|
||||||
|
* @returns the size of the buffer necessary to hold the encoded bytes
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_encode_size(size_t incoming_length);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode from Base16 format
|
||||||
|
* @param incoming the incoming base16 encoded string
|
||||||
|
* @param incoming_length the length of the incoming string (no need to include null)
|
||||||
|
* @param results where to put the results
|
||||||
|
* @param results_length the size of the buffer, and returns the actual length used
|
||||||
|
* @returns true(1) on success
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_decode(const unsigned char* incoming, size_t incoming_length, unsigned char* results, size_t* results_length);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the size of the buffer necessary to decode
|
||||||
|
* @param incoming_length the length of the incoming value
|
||||||
|
* @returns the size of the buffer necessary to hold the decoded bytes
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_encoding_base16_decode_size(size_t incoming_length);
|
|
@ -30,18 +30,18 @@ int libp2p_crypto_encoding_base58_decode(const unsigned char* base58, size_t bas
|
||||||
int libp2p_crypto_encoding_base58_encode(const unsigned char* binary_data, size_t binary_data_size, unsigned char** base58, size_t* base58_size);
|
int libp2p_crypto_encoding_base58_encode(const unsigned char* binary_data, size_t binary_data_size, unsigned char** base58, size_t* base58_size);
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* calculate the size of the binary results based on an incoming base58 string with no initial padding
|
* calculate the size of the results based on an incoming string
|
||||||
* @param base58_string the string
|
* @param decoded_length the length of the string to be encoded
|
||||||
* @returns the size in bytes had the string been decoded
|
* @returns the size in bytes had the string been encoded
|
||||||
*/
|
*/
|
||||||
size_t libp2p_crypto_encoding_base58_decode_size(const unsigned char* base58_string);
|
size_t libp2p_crypto_encoding_base58_encode_size(size_t decoded_length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* calculate the max length in bytes of an encoding of n source bits
|
* calculate the max length in bytes of an encoding of n source bytes
|
||||||
* @param base58_string the string
|
* @param encoded_length the length of the string to be decoded
|
||||||
* @returns the maximum size in bytes had the string been decoded
|
* @returns the maximum size in bytes had the string been decoded
|
||||||
*/
|
*/
|
||||||
size_t libp2p_crypto_encoding_base58_decode_max_size(const unsigned char* base58_string);
|
size_t libp2p_crypto_encoding_base58_decode_size(size_t encoded_length);
|
||||||
|
|
||||||
|
|
||||||
#endif /* base58_h */
|
#endif /* base58_h */
|
||||||
|
|
|
@ -22,7 +22,12 @@
|
||||||
*/
|
*/
|
||||||
int libp2p_crypto_encoding_base64_encode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
int libp2p_crypto_encoding_base64_encode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
||||||
|
|
||||||
size_t base64_encode_length(const unsigned char* input_data, size_t input_length);
|
/**
|
||||||
|
* calculate the max length in bytes of an encoding of n source bytes
|
||||||
|
* @param encoded_size the size of the encoded string
|
||||||
|
* @returns the maximum size in bytes had the string been decoded
|
||||||
|
*/
|
||||||
|
size_t libp2p_crypto_encoding_base64_encode_size(size_t input_length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* decode something that was encoded as base64
|
* decode something that was encoded as base64
|
||||||
|
@ -35,6 +40,11 @@ size_t base64_encode_length(const unsigned char* input_data, size_t input_length
|
||||||
*/
|
*/
|
||||||
int libp2p_crypto_encoding_base64_decode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
int libp2p_crypto_encoding_base64_decode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
||||||
|
|
||||||
size_t libp2p_crypto_encoding_base64_decode_length(const unsigned char* input_data, size_t input_length);
|
/**
|
||||||
|
* calculate the max length in bytes of a decoding of n source bytes
|
||||||
|
* @param decoded_size the size of the incoming string to be encoded
|
||||||
|
* @returns the maximum size in bytes had the string been encoded
|
||||||
|
*/
|
||||||
|
size_t libp2p_crypto_encoding_base64_decode_size(size_t input_length);
|
||||||
|
|
||||||
#endif /* base64_h */
|
#endif /* base64_h */
|
||||||
|
|
18
include/libp2p/crypto/hashing/sha256.h
Normal file
18
include/libp2p/crypto/hashing/sha256.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef __CRYPTO_HASHING_SHA256_H__
|
||||||
|
#define __CRYPTO_HASHING_SHA256_H__
|
||||||
|
|
||||||
|
#include "mbedtls/sha256.h"
|
||||||
|
|
||||||
|
/***
|
||||||
|
* hash a string using SHA256
|
||||||
|
* @param input the input string
|
||||||
|
* @param input_length the length of the input string
|
||||||
|
* @param output where to place the results
|
||||||
|
* @returns 1
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_hashing_sha256(const unsigned char* input, size_t input_length, unsigned char output[32]) {
|
||||||
|
mbedtls_sha256(input, input_length, output, 0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -6,7 +6,7 @@ CFLAGS += -g3
|
||||||
endif
|
endif
|
||||||
|
|
||||||
LFLAGS = -L../
|
LFLAGS = -L../
|
||||||
DEPS = crypto/test_base58.h crypto/test_rsa.h
|
DEPS = crypto/test_base58.h crypto/test_rsa.h test_mbedtls.h
|
||||||
OBJS = testit.o
|
OBJS = testit.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
|
|
|
@ -43,30 +43,34 @@ int test_base58_encode_decode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_base58_size() {
|
int test_base58_size() {
|
||||||
unsigned char* unencoded = (unsigned char*)"Hello, World!";
|
unsigned char* unencoded = (unsigned char*)"Hello, World!"; // 13 chars + 1 null
|
||||||
size_t string_length = strlen((char*)unencoded) + 1;
|
size_t string_length = strlen((char*)unencoded) + 1;
|
||||||
|
|
||||||
size_t encoded_length = 100;
|
size_t encoded_length = libp2p_crypto_encoding_base58_encode_size(string_length);
|
||||||
unsigned char encoded[encoded_length];
|
|
||||||
|
if (encoded_length != 20)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
size_t encoded_max_length = 100;
|
||||||
|
unsigned char encoded[encoded_max_length];
|
||||||
unsigned char* ptr = encoded;
|
unsigned char* ptr = encoded;
|
||||||
|
|
||||||
// now encode it
|
// now encode it
|
||||||
libp2p_crypto_encoding_base58_encode(unencoded, string_length, &ptr, &encoded_length);
|
libp2p_crypto_encoding_base58_encode(unencoded, string_length, &ptr, &encoded_max_length);
|
||||||
|
|
||||||
size_t size_enc = libp2p_crypto_encoding_base58_decode_size(ptr);
|
size_t decoded_length = libp2p_crypto_encoding_base58_decode_size(encoded_max_length);
|
||||||
size_t max_size_enc = libp2p_crypto_encoding_base58_decode_max_size(ptr);
|
|
||||||
|
|
||||||
if (size_enc <= string_length && max_size_enc >= string_length)
|
if (decoded_length != string_length)
|
||||||
return 1;
|
return 0;
|
||||||
|
|
||||||
return 0;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_base58_max_size() {
|
int test_base58_max_size() {
|
||||||
unsigned char hash[5] = {'S', 'D', 'Y', 'h', 'd' };
|
unsigned char hash[5] = {'S', 'D', 'Y', 'h', 'd' };
|
||||||
|
|
||||||
size_t results = libp2p_crypto_encoding_base58_decode_max_size(hash);
|
size_t results = libp2p_crypto_encoding_base58_decode_size(5);
|
||||||
if (results != 4)
|
if (results != 4)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -76,7 +80,7 @@ int test_base58_max_size() {
|
||||||
int test_base58_peer_address() {
|
int test_base58_peer_address() {
|
||||||
char* x_data = "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB";
|
char* x_data = "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB";
|
||||||
size_t x_data_length = strlen(x_data);
|
size_t x_data_length = strlen(x_data);
|
||||||
size_t result_buffer_length = libp2p_crypto_encoding_base58_decode_max_size(x_data);
|
size_t result_buffer_length = libp2p_crypto_encoding_base58_decode_size(x_data_length);
|
||||||
unsigned char result_buffer[result_buffer_length];
|
unsigned char result_buffer[result_buffer_length];
|
||||||
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);
|
||||||
|
|
|
@ -41,7 +41,7 @@ int test_crypto_rsa_private_key_der() {
|
||||||
int test_crypto_x509_der_to_private2() {
|
int test_crypto_x509_der_to_private2() {
|
||||||
// this is an example private key. The type is not added. Therefore, it is not compatible with the go version
|
// this is an example private key. The type is not added. Therefore, it is not compatible with the go version
|
||||||
char* der = "MIIEogIBAAKCAQEAmpDhRGecWN+MwfxW9UqsyEsZwnROHb2hCRBUATh7e0thgyi5Te6lT9x2++89x1sC7B9Egma1AJvY7BinIJYeFiOwzkKDe6/JxCle6TmLtCO1qmb8aGOIlu8NzO3L8EjcriYcgp0J5sZn5I3B4iU6q78u5jJQFpi2V7wsasanmfFwfF9ZUSxAnuwkcRkuhGnp5sBauHsQ4dn3IaiutiZDxPsAdxQyXX0SdrB4ew72UVAI2UsMAj5fRYy3plrsV/zZsGpeT6SDJI28weqOoSGvLzYaowf583MrH8O8dFoOWfXHq8hXy6qCDFOa7UYICCkb4QU9SLP7embmtfnB5/bVKwIDAQABAoIBADAwGwsIgmXyzB9uXG386gFH6LAHMpNzG1GIFaiLw3Oc/Lm3aI4zaLaNNUs2Ozx7011qIiHFg4i9DdQNm409QAQG/IhRlExrcawGeeCcYEG5IFoP4YFqBpuHy/Wn7XzsOmDQ4PKXow6frKREzb2Dfdcts6Fw7icdVTvlHrPrWzVS4azROsKy1spaCG7gYGd968f14Q2NBJmmlO75+dhjliI+cmMq5TaEvuCilXfSK4v6RWRGx+Mjl+ATSN1V7ksgzYsjNE15fdguxVWEIW5x5IsrpVviZdfGsDNWGsW9upoHoP118i2HKgrHRgbNcvTvWN/vCqJ/wDGz2FkYaa/3sYECgYEA9mBkSMW/Uc6m9/FNqlsEqCKLryI3ksxW9Ea6pHQ2p79gXYnlKuBLs7VdOA4XBIbV1vQdxjwJfojOdG7DmjklXYf+AYL/x26NSTszVV5feIIfR1jko67izBje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JnxcDy1AoGAMVmdK7mTMBtayUXRpthNCUmRAubaUKAjhKYm0j7ciPKWmo+mBHITbUshti07HqYeqxZE3LAIw/j0nowPsf4HIOxc69E+ZQqvETQwGyCwyHKJNYSFJi3tkHzCGEoP0/k9l6Fu8Qqcs1xhNQu8AcjQ1QL17rs/r9N44GKS2iXI+PY=";
|
||||||
size_t b64_length = libp2p_crypto_encoding_base64_decode_length((unsigned char*)der, strlen(der));
|
size_t b64_length = libp2p_crypto_encoding_base64_decode_size(strlen(der));
|
||||||
unsigned char buffer[b64_length];
|
unsigned char buffer[b64_length];
|
||||||
unsigned char* b = buffer;
|
unsigned char* b = buffer;
|
||||||
size_t ultimate_length;
|
size_t ultimate_length;
|
||||||
|
@ -59,7 +59,7 @@ int test_crypto_x509_der_to_private2() {
|
||||||
int test_crypto_x509_der_to_private() {
|
int test_crypto_x509_der_to_private() {
|
||||||
// this is a base64 encoded string from the go version of ipfs
|
// this is a base64 encoded string from the go version of ipfs
|
||||||
char* der = "CAASpwkwggSjAgEAAoIBAQDTDJBWjDzS/HxDNOHazvzH2bu9CPMVHUrrvKRdBUM5ansL6/CC3MVZ6HVm4O6QHRapN6EF2CbrTgI4KBOXIL125Xo8MlROnyfXYk3O5q2tgwL/MbW8kXjtkyCfBak7MUoLOdLU7Svg0gkl3l+uDAiDcCLnwJVcFfq9ch6z4wMOhYJqE5dtx0uXxn6IuKWl1B69FTvBXCc0thw8Rw54b941FDcsBH5ttV9mRNCJym3poZ5qalNgXlxoIIB+PUx5QD+aq7KMJdpAX8HkapBntCOahP/QUceRmma0grlZLeYkH6/oi/hIrM6se3KUZ+F6tBuDFys8UAZy/X2BCUbKjbxtAgMBAAECggEANWfQfpYuLhXGPBt9q6kFPm1SnJtPJ+CpvM2XqhJS2IyhZnrl+bd0GTRBwS7aL42s1lVFYf04nAK5fQxnKK8YQqX/MIxr2RldM5ukpN6qxGWKtJkXrAgD2dqJPrRoBpqKahzPxSHfIJ0Fw5dqDtjsrpYJvyt0o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Xso5J0CQ43W/U9+QIyjwKBgHLL2hw3o+wXaRO3WMUPUmVM2zdRgR0suybp5a7Vqb0H5NZrohUw4NulIzJ8H6Q2VjMzJL6Q9sGu2HepF6ecTtBa7ErqtiVlG4Dr1aCOs5XhYEWBMlwxX+JKSt4Cn+UVoTB7Cy5lEhn7JurX0Xuy0ylXMWoIKKv89cs5eg6quzTBAoGAaq9eEztLjKCWXOE9SetBdYnG8aunb9cqaJlwgu/h0bfXPVDYBbAUSEyLURY4MQI7Q1tM3Pu9iqfEmUZj7/LoIV5mg6X9RX/alT6etk3+dF+9nlqN1OU9U9cCtZ/rTcb2y5EptJcidRH/eCFY/pTV/PcttOJPx/S4kHcroC+N8MUCgYEA6DA5QHxHfNN6Nxv+pEzy2DIxFe9RrBxS+KPBsra1C8jgdeMf4EmfU0Nox92V0q0bRrD5ztqQwSONI0hSRb1iiMWR6MuFnAFajUJfASjjIlZ6nIQjQslI7vjlvYyyHS/p/Codxap+yJlTLWwVEOXp2D9pWwiMq1xEyf0TH1BosvM=";
|
||||||
size_t b64_length = libp2p_crypto_encoding_base64_decode_length((unsigned char*)der, strlen(der));
|
size_t b64_length = libp2p_crypto_encoding_base64_decode_size(strlen(der));
|
||||||
unsigned char buffer[b64_length];
|
unsigned char buffer[b64_length];
|
||||||
unsigned char* b = buffer;
|
unsigned char* b = buffer;
|
||||||
size_t ultimate_length;
|
size_t ultimate_length;
|
||||||
|
|
89
test/test_mbedtls.h
Normal file
89
test/test_mbedtls.h
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
/**
|
||||||
|
* A playground for testing mbedtls functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mbedtls/bignum.h"
|
||||||
|
|
||||||
|
int convertToString(unsigned char* string, int len) {
|
||||||
|
// load up the mpi
|
||||||
|
mbedtls_mpi mpi;
|
||||||
|
mbedtls_mpi_init(&mpi);
|
||||||
|
int retVal = mbedtls_mpi_read_binary(&mpi, string, len);
|
||||||
|
if (retVal < 0) {
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// put it in string form so we can see it
|
||||||
|
size_t buffer_len = 50;
|
||||||
|
size_t output_len = 0;
|
||||||
|
char buffer[buffer_len];
|
||||||
|
retVal = mbedtls_mpi_write_string(&mpi, 10, buffer, buffer_len, &output_len);
|
||||||
|
if (retVal < 0) {
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < output_len; i++) {
|
||||||
|
printf("%02x", buffer[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_mbedtls_varint_128_binary() {
|
||||||
|
// the values I'm working with 0x08, 0x00, 0x18, 0xa7, 0x09
|
||||||
|
unsigned char starting_val[5] = { 0x08, 0x00, 0x18, 0x87, 0x09 };
|
||||||
|
size_t starting_len = 5;
|
||||||
|
|
||||||
|
for(int i = 0; i < 5; i++) {
|
||||||
|
if (convertToString(&starting_val[i], 5 - i) != 1)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(" 3 and 4\n");
|
||||||
|
if (convertToString(&starting_val[2], 2) != 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("And now the other way...\n");
|
||||||
|
|
||||||
|
for(int i = 1; i <= 5; i--) {
|
||||||
|
if (convertToString(starting_val, i) != 1)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_mbedtls_varint_128_string() {
|
||||||
|
// go from string to uint_128 and back again
|
||||||
|
char* bigint_string = "47942806932686753431";
|
||||||
|
|
||||||
|
mbedtls_mpi mpi;
|
||||||
|
|
||||||
|
mbedtls_mpi_init(&mpi);
|
||||||
|
|
||||||
|
int retVal = mbedtls_mpi_read_string( &mpi, 10, bigint_string );
|
||||||
|
if (retVal < 0) {
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// now go back again
|
||||||
|
size_t buffer_len = 50;
|
||||||
|
size_t output_len = 0;
|
||||||
|
char buffer[buffer_len];
|
||||||
|
retVal = mbedtls_mpi_write_string(&mpi, 10, buffer, buffer_len, &output_len);
|
||||||
|
if (retVal < 0) {
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (strcmp(buffer, bigint_string) != 0) {
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
mbedtls_mpi_free(&mpi);
|
||||||
|
return 1;
|
||||||
|
}
|
|
@ -2,9 +2,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "crypto/test_rsa.h"
|
#include "crypto/test_rsa.h"
|
||||||
//#include "multihash/test_multihash.h"
|
|
||||||
#include "crypto/test_base58.h"
|
#include "crypto/test_base58.h"
|
||||||
//#include "crypto/test_mbedtls.h"
|
#include "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);
|
||||||
|
@ -17,6 +16,8 @@ int testit(const char* name, int (*func)(void)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
testit("test_mbedtls_varint_128_binary", test_mbedtls_varint_128_binary);
|
||||||
|
testit("test_mbedtls_varint_128_string", test_mbedtls_varint_128_string);
|
||||||
testit("test_crypto_rsa_private_key_der", test_crypto_rsa_private_key_der);
|
testit("test_crypto_rsa_private_key_der", test_crypto_rsa_private_key_der);
|
||||||
//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_private2", test_crypto_x509_der_to_private2);
|
testit("test_crypto_x509_der_to_private2", test_crypto_x509_der_to_private2);
|
||||||
|
|
Loading…
Reference in a new issue