EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
Macros | Functions
cryptou.c File Reference

File containing the implementation of the cryptographic utilities. More...

#include <stdint.h>
#include <openssl/conf.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
#include <openssl/x509v3.h>
#include <openssl/engine.h>
#include "cryptou.h"
#include "../utils/allocs.h"
#include "../utils/log.h"
#include "../utils/os.h"
Include dependency graph for cryptou.c:

Macros

#define OPENSSL_API_COMPAT   10101
 
#define OPENSSL_NO_DEPRECATED   1
 

Functions

int crypto_geniv (uint8_t *buf, int iv_size)
 Generate IV. More...
 
int crypto_gensalt (uint8_t *buf, int salt_size)
 Generate salt. More...
 
int crypto_genkey (uint8_t *buf, int key_size)
 Generate a random key. More...
 
int crypto_buf2key (const uint8_t *buf, int buf_size, const uint8_t *salt, int salt_size, uint8_t *key, int key_size)
 Transforms a secret buf into a key. More...
 
ssize_t crypto_encrypt (const uint8_t *in, int in_size, const uint8_t *key, const uint8_t *iv, uint8_t *out)
 Encrypts a buffer with AES CBC 256. More...
 
ssize_t crypto_decrypt (uint8_t *in, int in_size, uint8_t *key, uint8_t *iv, uint8_t *out)
 Decrypts a buffer with AES CBC 256. More...
 
EVP_PKEY * crypto_generate_rsa_key (int bits)
 
EVP_PKEY * crypto_generate_ec_key (void)
 
X509 * crypto_generate_cert (EVP_PKEY *pkey, struct certificate_meta *meta)
 
EVP_PKEY * crypto_key2evp (uint8_t *key, size_t key_size)
 
EVP_PKEY * crypto_priv2pub (EVP_PKEY *key)
 
char * crypto_get_key_str (bool private, EVP_PKEY *pkey)
 
int crypto_generate_privkey_str (enum CRYPTO_KEY_TYPE type, int bits, char **key)
 Generate a private RSA key string. More...
 
int crypto_generate_pubkey_str (uint8_t *key, size_t key_size, char **pub)
 Generates a public key string from a private key. More...
 
int crypto_generate_cert_str (struct certificate_meta *meta, uint8_t *key, size_t key_size, char **cert)
 Generates a pair of private key and certificate strings. More...
 
int crypto_verify_data (uint8_t *key, size_t key_size, uint8_t *in, size_t in_size, uint8_t *sig, size_t sig_size)
 
ssize_t crypto_sign_data (uint8_t *key, size_t key_size, uint8_t *in, size_t in_size, uint8_t **out)
 Signs a buffer with a private key string. More...
 

Detailed Description

File containing the implementation of the cryptographic utilities.

Author
Alexandru Mereacre
Date
2021

Macro Definition Documentation

◆ OPENSSL_API_COMPAT

#define OPENSSL_API_COMPAT   10101

◆ OPENSSL_NO_DEPRECATED

#define OPENSSL_NO_DEPRECATED   1

Function Documentation

◆ crypto_buf2key()

int crypto_buf2key ( const uint8_t *  buf,
int  buf_size,
const uint8_t *  salt,
int  salt_size,
uint8_t *  key,
int  key_size 
)

Transforms a secret buf into a key.

Parameters
bufThe secret buffer
buf_sizeThe secret buf size
saltThe salt buffer
salt_sizeThe salt buffer size
[out]keyThe ouput buffer to store the key.
key_sizeThe size of the key buffer.
Return values
0on success
-1on failure

◆ crypto_decrypt()

ssize_t crypto_decrypt ( uint8_t *  in,
int  in_size,
uint8_t *  key,
uint8_t *  iv,
uint8_t *  out 
)

Decrypts a buffer with AES CBC 256.

Parameters
inThe input buffer
in_sizeThe input buffer size
keyThe 256 bit key
ivThe 128 bit key
outThe output buffer
Returns
The output size, -1 on error

◆ crypto_encrypt()

ssize_t crypto_encrypt ( const uint8_t *  in,
int  in_size,
const uint8_t *  key,
const uint8_t *  iv,
uint8_t *  out 
)

Encrypts a buffer with AES CBC 256.

Parameters
inThe input buffer
in_sizeThe input buffer size
keyThe 256 bit key
ivThe 128 bit key
[out]outThe output buffer
Returns
The output size, -1 on error

◆ crypto_generate_cert()

X509* crypto_generate_cert ( EVP_PKEY *  pkey,
struct certificate_meta meta 
)

◆ crypto_generate_cert_str()

int crypto_generate_cert_str ( struct certificate_meta meta,
uint8_t *  key,
size_t  key_size,
char **  cert 
)

Generates a pair of private key and certificate strings.

Parameters
metaCertificate metadata
keyThe private key buffer
key_sizeThe private key buffer size
certThe certificate string
Returns
int 0 on success, -1 on failure

◆ crypto_generate_ec_key()

EVP_PKEY* crypto_generate_ec_key ( void  )

◆ crypto_generate_privkey_str()

int crypto_generate_privkey_str ( enum CRYPTO_KEY_TYPE  type,
int  bits,
char **  key 
)

Generate a private RSA key string.

Parameters
typeThe key type
bitsNumber of key bits
keyThe output key string
Returns
int 0 on success, -1 on failure

◆ crypto_generate_pubkey_str()

int crypto_generate_pubkey_str ( uint8_t *  key,
size_t  key_size,
char **  pub 
)

Generates a public key string from a private key.

Parameters
keyThe private key buffer
key_sizeThe private key buffer size
pubThe public key string
Returns
int 0 on success, -1 on failure

◆ crypto_generate_rsa_key()

EVP_PKEY* crypto_generate_rsa_key ( int  bits)

◆ crypto_geniv()

int crypto_geniv ( uint8_t *  buf,
int  iv_size 
)

Generate IV.

Parameters
bufThe output buffer
iv_sizeThe IV size
Returns
1 on success, 0 on failure

◆ crypto_genkey()

int crypto_genkey ( uint8_t *  buf,
int  key_size 
)

Generate a random key.

Parameters
bufThe output buffer
key_sizeThe key size in bytes
Returns
1 on success, 0 on failure

◆ crypto_gensalt()

int crypto_gensalt ( uint8_t *  buf,
int  salt_size 
)

Generate salt.

Parameters
bufThe output buffer
salt_sizeThe salt size in bytes
Returns
1 on success, 0 on failure

◆ crypto_get_key_str()

char* crypto_get_key_str ( bool  private,
EVP_PKEY *  pkey 
)

◆ crypto_key2evp()

EVP_PKEY* crypto_key2evp ( uint8_t *  key,
size_t  key_size 
)

◆ crypto_priv2pub()

EVP_PKEY* crypto_priv2pub ( EVP_PKEY *  key)

◆ crypto_sign_data()

ssize_t crypto_sign_data ( uint8_t *  key,
size_t  key_size,
uint8_t *  in,
size_t  in_size,
uint8_t **  out 
)

Signs a buffer with a private key string.

Parameters
keyThe private key buffer
key_sizeThe private key buffer size
inThe input buffer
in_sizeThe input buffer size
outThe output signature
Returns
ssize_t the length of the signature, -1 on failure

◆ crypto_verify_data()

int crypto_verify_data ( uint8_t *  key,
size_t  key_size,
uint8_t *  in,
size_t  in_size,
uint8_t *  sig,
size_t  sig_size 
)