14 #include <openssl/bn.h> 15 #include <openssl/bio.h> 16 #include <openssl/pem.h> 17 #include <openssl/rsa.h> 18 #include <openssl/md5.h> 19 #include <openssl/sha.h> 20 #include <openssl/crypto.h> 21 #include <openssl/rand.h> 23 #define KEY_LENGTH_BITS 3072 61 MCL_DEBUG_ENTRY(
"const mcl_uint8_t *data = <%p>, mcl_size_t data_size = <%u>, mcl_uint8_t **hash = <%p>, mcl_size_t *hash_size = <%p>",
62 data, data_size, hash, hash_size);
74 MCL_DEBUG(
"Calculating SHA256 with OpenSSL.");
75 SHA256(data, data_size, *hash);
77 MCL_DEBUG(
"Calculated SHA256 *hash = <%p>, hash_size = <%u>", *hash, hash_size);
86 unsigned int sign_length = 0;
93 MCL_DEBUG_ENTRY(
"char *rsa_key = <%p>, char *data = <%p>, mcl_size_t data_size = <%u>, mcl_uint8_t **signature = <%p>, mcl_size_t *signature_size = <%p>",
94 rsa_key, data, data_size, signature, signature_size);
97 bio = BIO_new_mem_buf(rsa_key, (
int) length);
100 rsa = PEM_read_bio_RSAPrivateKey(bio, &rsa, 0,
MCL_NULL);
105 ok = (
MCL_TRUE == ok) && (1 == RSA_sign(NID_sha256, hash, (
unsigned int) hash_size, *signature, &sign_length, rsa));
106 *signature_size = sign_length;
129 MCL_DEBUG_ENTRY(
"char **public_key = <%p>, char **private_key = <%p>", public_key, private_key);
140 BN_set_word(exponent, RSA_F4);
181 MCL_DEBUG_ENTRY(
"char *public_key = <%s>, char **modulus = <%p>, char **exponent = <%p>", public_key, modulus, exponent);
184 bio = BIO_new_mem_buf(public_key, (
int) length);
187 rsa = PEM_read_bio_RSA_PUBKEY(bio, &rsa, 0,
MCL_NULL);
191 RSA_get0_key(rsa, &n, &e,
MCL_NULL);
217 MCL_DEBUG_ENTRY(
"unsigned char *buffer = <%p>, mcl_size_t size = <%u>", buffer, size);
219 if (1 == RAND_bytes(buffer, (
int) size))
235 MCL_DEBUG_ENTRY(
"const BIGNUM *big_number = <%p>, char **encoded = <%p>", big_number, encoded);
237 binary =
MCL_MALLOC(BN_num_bytes(big_number));
240 binary_length = BN_bn2bin(big_number, binary);
245 *encoded = binary_encoded;
260 MCL_DEBUG_ENTRY(
"RSA *rsa = <%p>, char **private_key = <%p>", rsa, private_key);
262 bio = BIO_new(BIO_s_mem());
265 if (0 == PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL))
271 key_length = BIO_pending(bio);
276 if (key_length != BIO_read(bio, *private_key, key_length))
278 MCL_DEBUG(
"Private key can not be read from BIO.");
299 MCL_DEBUG_ENTRY(
"RSA *rsa = <%p>, char **public_key = <%p>", rsa, public_key);
301 bio = BIO_new(BIO_s_mem());
304 if (0 == PEM_write_bio_RSA_PUBKEY(bio, rsa))
310 key_length = BIO_pending(bio);
315 if (key_length != BIO_read(bio, *public_key, key_length))
317 MCL_DEBUG(
"Public key can not be read from BIO.");
336 MCL_VERBOSE_ENTRY(
"size_t size = <%lu>, const char *file = <%s>, int line = <%d>", size, file, line);
338 #if !MCL_LOG_ENABLED_COMPILE_TIME(MCL_LOG_LEVEL_VERBOSE) 352 void *memory_reallocated;
354 MCL_VERBOSE_ENTRY(
"void *p = <%p>, size_t size = <%lu>, const char *file = <%s>, int line = <%d>", p, size, file, line);
356 #if !MCL_LOG_ENABLED_COMPILE_TIME(MCL_LOG_LEVEL_VERBOSE) 365 return memory_reallocated;
370 MCL_VERBOSE_ENTRY(
"void *p = <%p>, const char *file = <%s>, int line = <%d>", p, file, line);
372 #if !MCL_LOG_ENABLED_COMPILE_TIME(MCL_LOG_LEVEL_VERBOSE)
Assert module header file.
mcl_error_t security_rsa_sign(char *rsa_key, char *data, mcl_size_t data_size, mcl_uint8_t **signature, mcl_size_t *signature_size)
#define SHA256_DIGEST_LENGTH
#define MCL_CALLOC(count, bytes)
Security libcrypto module header file.
#define MCL_DEBUG_ENTRY(...)
String utility module header file.
MCL_CORE_EXPORT void * mcl_memory_realloc(void *p, mcl_size_t bytes)
static mcl_error_t _base64_encode_big_number(const BIGNUM *big_number, char **encoded)
#define MCL_ASSERT_CODE_MESSAGE(condition, return_code,...)
static void * _libcrypto_realloc(void *p, size_t size, const char *file, int line)
#define MCL_VERBOSE_LEAVE(...)
mcl_error_t security_rsa_get_modulus_and_exponent(char *public_key, char **modulus, char **exponent)
mcl_error_t security_initialize(void)
mcl_error_t security_generate_random_bytes(unsigned char *buffer, mcl_size_t size)
MCL_CORE_EXPORT void mcl_memory_free(void *p)
#define MCL_ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
mcl_error_t base64_url_encode(const mcl_uint8_t *data, mcl_size_t data_size, char **encoded_data)
static void _libcrypto_free(void *p, const char *file, int line)
#define MCL_ASSERT_CODE(condition, return_code)
#define MCL_ERROR_RETURN(return_value,...)
Base64 module header file.
static mcl_error_t _get_rsa_public_key(RSA *rsa, char **public_key)
static mcl_error_t _get_rsa_private_key(RSA *rsa, char **private_key)
mcl_error_t security_generate_rsa_key(char **public_key, char **private_key)
mcl_error_t security_hash_sha256(const mcl_uint8_t *data, mcl_size_t data_size, mcl_uint8_t **hash, mcl_size_t *hash_size)
#define MCL_MALLOC(bytes)
#define MCL_DEBUG_LEAVE(...)
MCL_CORE_EXPORT void * mcl_memory_malloc(mcl_size_t size)
#define MCL_VERBOSE_ENTRY(...)
mcl_size_t string_util_strlen(const char *buffer)
static void * _libcrypto_malloc(size_t size, const char *file, int line)
Memory module interface header file.