21 #if (1 == HAVE_OPENSSL_BN_H_) 22 #include <openssl/bn.h> 25 #if (1 == HAVE_OPENSSL_BIO_H_) 26 #include <openssl/bio.h> 29 #if (1 == HAVE_OPENSSL_PEM_H_) 30 #include <openssl/pem.h> 33 #if (1 == HAVE_OPENSSL_RSA_H_) 34 #include <openssl/rsa.h> 37 #if (1 == HAVE_OPENSSL_MD5_H_) 38 #include <openssl/md5.h> 41 #if (1 == HAVE_OPENSSL_SHA_H_) 42 #include <openssl/sha.h> 45 #if (1 == HAVE_OPENSSL_CRYPTO_H_) 46 #include <openssl/crypto.h> 49 #if (1 == HAVE_OPENSSL_RAND_H_) 50 #include <openssl/rand.h> 53 #define KEY_LENGTH_BITS 3072 80 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>", data, data_size, hash, hash_size)
88 *hash_size = SHA256_DIGEST_LENGTH;
91 MCL_DEBUG(
"Calculating SHA256 with OpenSSL.");
92 SHA256(data, data_size, *hash);
94 MCL_DEBUG(
"Calculated SHA256 *hash = <%p>, hash_size = <%u>", *hash, hash_size);
101 DEBUG_ENTRY(
"char *rsa_key = <%s>, char *data = <%s>, mcl_size_t data_size = <%u>, mcl_uint8_t **signature = <%p>, mcl_size_t *signature_size = <%p>", rsa_key, data, data_size, signature, signature_size)
104 unsigned int sign_length = 0;
112 bio = BIO_new_mem_buf(rsa_key, (
int)length);
115 rsa = PEM_read_bio_RSAPrivateKey(bio, &rsa, 0,
MCL_NULL);
120 ok = ok && (1 == RSA_sign(NID_sha256, hash, (
unsigned int)hash_size, *signature, &sign_length, rsa));
121 *signature_size = sign_length;
139 DEBUG_ENTRY(
"char **public_key = <%p>, char **private_key = <%p>", public_key, private_key)
155 BN_set_word(exponent, RSA_F4);
182 DEBUG_ENTRY(
"char *public_key = <%s>, char **modulus = <%p>, char **exponent = <%p>", public_key, modulus, exponent)
189 BIO *bio = BIO_new_mem_buf(public_key, (
int)length);
192 rsa = PEM_read_bio_RSA_PUBKEY(bio, &rsa, 0,
MCL_NULL);
196 RSA_get0_key(rsa, &n, &e,
MCL_NULL);
216 DEBUG_ENTRY(
"unsigned char *buffer = <%p>, mcl_size_t size = <%u>", buffer, size)
220 #if (1 == HAVE_OPENSSL_RAND_H_) 221 if(1 == RAND_bytes(buffer, (
int)size))
233 DEBUG_ENTRY(
"BIGNUM *big_number = <%p>, char **encoded = <%p>", big_number, encoded)
240 binary =
MCL_MALLOC(BN_num_bytes(big_number));
243 binary_length = BN_bn2bin(big_number, (
unsigned char *)binary);
249 *encoded = binary_encoded->
buffer;
261 DEBUG_ENTRY(
"RSA *rsa = <%p>, char **private_key = <%p>", rsa, private_key)
267 bio = BIO_new(BIO_s_mem());
270 if (!PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL))
275 key_length = BIO_pending(bio);
280 if (key_length != BIO_read(bio, *private_key, key_length))
282 MCL_DEBUG(
"Private key can not be read from BIO.");
296 DEBUG_ENTRY(
"RSA *rsa = <%p>, char **public_key = <%p>", rsa, public_key)
302 bio = BIO_new(BIO_s_mem());
305 if (!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.");
333 VERBOSE_ENTRY(
"size_t size = <%lu>, const char *file = <%s>, int line = <%d>", size, file, line);
335 #if (MCL_LOG_UTIL_LEVEL < MCL_LOG_UTIL_LEVEL_VERBOSE) 349 void *memory_reallocated;
351 VERBOSE_ENTRY(
"void *p = <%p>, size_t size = <%lu>, const char *file = <%s>, int line = <%d>", p, size, file, line);
353 #if (MCL_LOG_UTIL_LEVEL < MCL_LOG_UTIL_LEVEL_VERBOSE) 362 return memory_reallocated;
367 VERBOSE_ENTRY(
"void *p = <%p>, const char *file = <%s>, int line = <%d>", p, file, line);
369 #if (MCL_LOG_UTIL_LEVEL < MCL_LOG_UTIL_LEVEL_VERBOSE) void security_initialize(void)
Memory module header file.
static E_MCL_ERROR_CODE _get_rsa_public_key(RSA *rsa, char **public_key)
#define VERBOSE_LEAVE(...)
E_MCL_ERROR_CODE base64_url_encode(const mcl_uint8_t *data, mcl_size_t data_size, string_t **encoded_data)
E_MCL_ERROR_CODE security_generate_random_bytes(unsigned char *buffer, mcl_size_t size)
To be used to generate random bytes.
char * buffer
Buffer of string handle.
Security libcrypto module header file.
static E_MCL_ERROR_CODE _get_rsa_private_key(RSA *rsa, char **private_key)
#define MCL_FALSE
MCL bool type.
#define MCL_MALLOC(bytes)
Log utility module header file.
String utility module implementation file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
void * memory_malloc(mcl_size_t size)
malloc wrapper
static E_MCL_ERROR_CODE _base64_encode_big_number(BIGNUM *big_number, char **encoded)
#define VERBOSE_ENTRY(...)
#define ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
static void * _libcrypto_realloc(void *p, size_t size, const char *file, int line)
#define MCL_CALLOC(count, bytes)
E_MCL_ERROR_CODE security_hash_sha256(const mcl_uint8_t *data, mcl_size_t data_size, mcl_uint8_t **hash, mcl_size_t *hash_size)
#define ASSERT_CODE_MESSAGE(condition, return_code,...)
Definitions module header file.
E_MCL_ERROR_CODE security_rsa_get_modulus_and_exponent(char *public_key, char **modulus, char **exponent)
To be used to get the modulus (n) and public exponent (e) parameters of RSA key in Base64 format...
void memory_free(void *p)
free wrapper
static void _libcrypto_free(void *p, const char *file, int line)
#define ASSERT_CODE(condition, return_code)
Base64 module header file.
E_MCL_ERROR_CODE security_generate_rsa_key(char **public_key, char **private_key)
To be used to generate the RSA public/private keys.
void * memory_realloc(void *p, mcl_size_t bytes)
realloc wrapper
mcl_size_t string_util_strlen(const char *buffer)
Standard library strlen wrapper.
E_MCL_ERROR_CODE security_rsa_sign(char *rsa_key, char *data, mcl_size_t data_size, mcl_uint8_t **signature, mcl_size_t *signature_size)
To be used to sign data with RSA key.
static void * _libcrypto_malloc(size_t size, const char *file, int line)
#define MCL_ERROR_RETURN(return_value,...)