security_libcrypto.c File Reference

Security libcrypto module implementation file. More...

#include "security_libcrypto.h"
#include "base64.h"
#include "string_util.h"
#include "mcl_core/mcl_assert.h"
#include "mcl_core/mcl_memory.h"
#include <openssl/bn.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
Include dependency graph for security_libcrypto.c:

Go to the source code of this file.

Macros

#define KEY_LENGTH_BITS   3072
 

Functions

static mcl_error_t _base64_encode_big_number (const BIGNUM *big_number, char **encoded)
 
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)
 
static void * _libcrypto_malloc (size_t size, const char *file, int line)
 
static void * _libcrypto_realloc (void *p, size_t size, const char *file, int line)
 
static void _libcrypto_free (void *p, const char *file, int line)
 
mcl_error_t security_initialize (void)
 
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)
 
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)
 
mcl_error_t security_generate_rsa_key (char **public_key, char **private_key)
 
mcl_error_t security_rsa_get_modulus_and_exponent (char *public_key, char **modulus, char **exponent)
 
mcl_error_t security_generate_random_bytes (unsigned char *buffer, mcl_size_t size)
 

Detailed Description

Security libcrypto module implementation file.

Definition in file security_libcrypto.c.

Macro Definition Documentation

#define KEY_LENGTH_BITS   3072

Definition at line 23 of file security_libcrypto.c.

Referenced by security_generate_rsa_key().

Function Documentation

static mcl_error_t _base64_encode_big_number ( const BIGNUM *  big_number,
char **  encoded 
)
static

Definition at line 228 of file security_libcrypto.c.

References base64_url_encode(), MCL_ASSERT_CODE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FREE, MCL_MALLOC, MCL_NULL, MCL_OK, and MCL_OUT_OF_MEMORY.

Referenced by security_rsa_get_modulus_and_exponent().

Here is the call graph for this function:

Here is the caller graph for this function:

static mcl_error_t _get_rsa_private_key ( RSA *  rsa,
char **  private_key 
)
static
static mcl_error_t _get_rsa_public_key ( RSA *  rsa,
char **  public_key 
)
static
static void _libcrypto_free ( void *  p,
const char *  file,
int  line 
)
static

Definition at line 368 of file security_libcrypto.c.

References mcl_memory_free(), MCL_VERBOSE_ENTRY, and MCL_VERBOSE_LEAVE.

Referenced by security_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

static void * _libcrypto_malloc ( size_t  size,
const char *  file,
int  line 
)
static

Definition at line 332 of file security_libcrypto.c.

References mcl_memory_malloc(), MCL_NULL, MCL_VERBOSE_ENTRY, and MCL_VERBOSE_LEAVE.

Referenced by security_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

static void * _libcrypto_realloc ( void *  p,
size_t  size,
const char *  file,
int  line 
)
static

Definition at line 350 of file security_libcrypto.c.

References mcl_memory_realloc(), MCL_VERBOSE_ENTRY, and MCL_VERBOSE_LEAVE.

Referenced by security_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

mcl_error_t security_generate_random_bytes ( unsigned char *  buffer,
mcl_size_t  size 
)

This function is used to generate random bytes.

Parameters
[out]bufferBuffer which will be filled with random bytes.
[in]sizeSize of the buffer.
Returns
  • MCL_OK in case of success.
  • MCL_FAIL in case of an internal error in MCL.

Definition at line 213 of file security_libcrypto.c.

References ctr_drbg, is_initialized, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, and MCL_OK.

mcl_error_t security_generate_rsa_key ( char **  public_key,
char **  private_key 
)

This function is used to generate the RSA public/private keys.

Generated keys will be returned as out parameters.

Parameters
[out]public_keyGenerated public key. New memory space will be allocated for this parameter.
[out]private_keyGenerated private key. New memory space will be allocated for this parameter.
Returns

Definition at line 122 of file security_libcrypto.c.

References _get_rsa_private_key(), _get_rsa_public_key(), KEY_LENGTH_BITS, MCL_ASSERT_CODE_MESSAGE, MCL_ASSERT_STATEMENT_CODE_MESSAGE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_FREE, MCL_NULL, MCL_OK, and MCL_OUT_OF_MEMORY.

Here is the call graph for this function:

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 
)

This function is used to generate the sha256 hash of the given data.

See also
security_handler_hash_sha256.

Definition at line 59 of file security_libcrypto.c.

References MCL_ASSERT_CODE_MESSAGE, MCL_CALLOC, MCL_DEBUG, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, and SHA256_DIGEST_LENGTH.

Referenced by security_rsa_sign().

Here is the caller graph for this function:

mcl_error_t security_initialize ( void  )

This function is used to initialize security implementation.

Returns
  • MCL_OK in case of success.
  • MCL_FAIL in case of an internal error in MCL.

Definition at line 39 of file security_libcrypto.c.

References _libcrypto_free(), _libcrypto_malloc(), _libcrypto_realloc(), ctr_drbg, entropy, is_initialized, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_NULL, MCL_OK, and MCL_TRUE.

Here is the call graph for this function:

mcl_error_t security_rsa_get_modulus_and_exponent ( char *  public_key,
char **  modulus,
char **  exponent 
)

This function is used to get the modulus (n) and public exponent (e) parameters of RSA key in base64 format.

Parameters
public_keyPublic key in PEM format.
modulusBase64 encoded modulus of RSA key.
exponentBase64 encoded public exponent of RSA key.
Returns

Definition at line 172 of file security_libcrypto.c.

References _base64_encode_big_number(), MCL_ASSERT_CODE_MESSAGE, MCL_ASSERT_STATEMENT_CODE_MESSAGE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_FREE, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, and string_util_strlen().

Here is the call graph for this function:

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 
)

This function is used to sign data with RSA key.

Received key will be used to sign.

Parameters
[in]rsa_keyPrivate key to be used in signing.
[in]dataThe data to be signed.
[in]data_sizeSize of the data.
[out]signatureGenerated signature.
[out]signature_sizeSize of signature.
Returns

Definition at line 82 of file security_libcrypto.c.

References MCL_ASSERT_CODE_MESSAGE, MCL_ASSERT_STATEMENT_CODE_MESSAGE, MCL_DEBUG_ENTRY, MCL_DEBUG_LEAVE, MCL_FAIL, MCL_FALSE, MCL_FREE, MCL_MALLOC, MCL_NULL, MCL_OK, MCL_OUT_OF_MEMORY, MCL_TRUE, security_hash_sha256(), and string_util_strlen().

Here is the call graph for this function: