Libu2f-emu  0.0.0
Universal 2nd Factor (U2F) Emulation C Library
Functions
crypto.c File Reference
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "crypto.h"
#include "utils.h"
Include dependency graph for crypto.c:

Functions

static size_t crypto_pem_length (const char *pem)
 Compute the PEM length. More...
 
EC_KEY * crypto_ec_bytes_to_key (const unsigned char *buffer, long size)
 Get the ec key from ec key bytes. More...
 
size_t crypto_aes_decrypt (struct crypto_core *crypto_core, const unsigned char *data, int size, unsigned char **buffer)
 Decrypt data using AES. More...
 
size_t crypto_aes_encrypt (struct crypto_core *crypto_core, const unsigned char *data, int data_len, unsigned char **buffer)
 Encrypt data using AES. More...
 
int crypto_ec_key_to_bytes (EC_KEY *key, unsigned char **buffer)
 Get the ec key bytes. More...
 
unsigned int crypto_ec_sign_with_key (EC_KEY *key, const unsigned char *digest, int digest_len, unsigned char **signature)
 Sign a digest with a specific key. More...
 
unsigned int crypto_ec_sign (struct crypto_core *crypto_core, const unsigned char *digest, int digest_len, unsigned char **signature)
 Sign a digest. More...
 
size_t crypto_hash (const void *data, size_t data_len, unsigned char **hash)
 Hash data using sha256. More...
 
size_t crypto_ec_pubkey_to_bytes (const EC_KEY *key, unsigned char **buffer)
 Get the ec key bytes. More...
 
int crypto_x509_get_bytes (struct crypto_core *crypto_core, unsigned char **buffer)
 Get the x509 certificate bytes. More...
 
static bool crypto_x509_add_ext (X509 *cert, int nid, const char *value, bool critical)
 Add an extension to a x509 certificate. More...
 
static X509 * crypto_x509_generate (EC_KEY *key)
 Generate a new x509 certificate from an ec key. More...
 
EC_KEY * crypto_ec_generate_key (void)
 Generate an ec pair key. More...
 
static X509 * crypto_x509_from_file (const char *dirpath, const char *filename)
 Get the x509 from file. More...
 
static X509 * crypto_x509_from_pem (const char *x509_pem)
 Get the x509 from PEM. More...
 
static EC_KEY * crypto_ec_privkey_from_file (const char *dirpath, const char *filename)
 Get the ec private key from file. More...
 
static EC_KEY * crypto_ec_privkey_from_pem (const char *private_key_pem)
 Get the ec private key from PEM. More...
 
static bool crypto_entropy_from_file (const char *dirpath, const char *filename, uint8_t entropy[48])
 Get the entropy from file. More...
 
EC_KEY * crypto_ec_pubkey_from_priv (EC_KEY *privkey)
 Get the ec public key from its private key. More...
 
bool crypto_new (const char *certificate, const char *private_key, const uint8_t entropy[48], struct crypto_core **core_ref)
 Instantiate a new crypto core. More...
 
bool crypto_new_ephemeral (struct crypto_core **core_ref)
 Instantiate a new ephemeral crypto core. More...
 
bool crypto_new_from_dir (const char *dirpath, struct crypto_core **core_ref)
 Setup a crypto core from a dir. More...
 
void crypto_free (struct crypto_core *crypto_core)
 Release the memory allocated by the crypto_core. More...
 

Function Documentation

size_t crypto_aes_decrypt ( struct crypto_core core,
const unsigned char *  data,
int  size,
unsigned char **  buffer 
)

Decrypt data using AES.

Parameters
coreThe crypto core.
dataThe data to decrypt.
sizeThe data size.
bufferThe resulting buffer where clear data is put.
Returns
The size of the buffer.
size_t crypto_aes_encrypt ( struct crypto_core core,
const unsigned char *  data,
int  data_len,
unsigned char **  buffer 
)

Encrypt data using AES.

Parameters
coreThe crypto core.
dataThe data to encrypt.
data_lenThe data size.
bufferThe resulting buffer where cipher data is put.
Returns
The size of the buffer.
EC_KEY* crypto_ec_bytes_to_key ( const unsigned char *  buffer,
long  size 
)

Get the ec key from ec key bytes.

Parameters
bufferThe buffer containing the ec key bytes.
sizeThe size of the buffer.
Returns
The ec key.
EC_KEY* crypto_ec_generate_key ( void  )

Generate an ec pair key.

Returns
Success: The generated ec pair key. Failure: NULL.
int crypto_ec_key_to_bytes ( EC_KEY *  key,
unsigned char **  buffer 
)

Get the ec key bytes.

Parameters
keyThe ec key.
bufferThe buffer use to put the bytes
Returns
The size of the buffer.
static EC_KEY* crypto_ec_privkey_from_file ( const char *  dirpath,
const char *  filename 
)
static

Get the ec private key from file.

Parameters
dirpathThe path of the directory.
filenameThe filename.
Returns
Success: The private key. Failure: NULL.
static EC_KEY* crypto_ec_privkey_from_pem ( const char *  private_key_pem)
static

Get the ec private key from PEM.

Parameters
private_key_pemThe ec private key PEM.
Returns
Success: The private key. Failure: NULL.
EC_KEY* crypto_ec_pubkey_from_priv ( EC_KEY *  privkey)

Get the ec public key from its private key.

Parameters
privkeythe ec privkey.
Returns
The ec public key
size_t crypto_ec_pubkey_to_bytes ( const EC_KEY *  key,
unsigned char **  buffer 
)

Get the ec key bytes.

Parameters
keyThe ec key.
bufferThe buffer use to put the bytes.
Returns
The size of the buffer.
unsigned int crypto_ec_sign ( struct crypto_core core,
const unsigned char *  digest,
int  digest_len,
unsigned char **  signature 
)

Sign a digest.

Parameters
coreThe crypto core.
digestThe digest.
digest_lenThe digest length?
signatureThe ref buffer to put the signature.
Returns
The size of the signature.
unsigned int crypto_ec_sign_with_key ( EC_KEY *  key,
const unsigned char *  digest,
int  digest_len,
unsigned char **  signature 
)

Sign a digest with a specific key.

Parameters
keyThe ec key.
digestThe digest.
digest_lenThe digest len.
signatureThe ref buffer to put the signature.
Returns
The size of the signature.
static bool crypto_entropy_from_file ( const char *  dirpath,
const char *  filename,
uint8_t  entropy[48] 
)
static

Get the entropy from file.

Parameters
dirpathThe path of the directory.
filenameThe filename.
entropyThe entropy to setu.
void crypto_free ( struct crypto_core core)

Release the memory allocated by the crypto_core.

Parameters
coreThe crypto core to release.
size_t crypto_hash ( const void *  data,
size_t  data_len,
unsigned char **  hash 
)

Hash data using sha256.

Parameters
dataThe data.
data_lenThe data length.
hashThe ref buffer to put the hash.
Returns
The size of the hash.
bool crypto_new ( const char *  certificate,
const char *  private_key,
const uint8_t  entropy[48],
struct crypto_core **  core_ref 
)

Instantiate a new crypto core.

Parameters
certificatePEM ec certificate.
private_keyPEM ec Private key of the certificate.
entropyRandom bits used in encryption.
core_refThe crypto core reference.
Returns
Sucess: true. Failure: false.
bool crypto_new_ephemeral ( struct crypto_core **  core_ref)

Instantiate a new ephemeral crypto core.

Parameters
core_refThe crypto core reference.
Returns
Sucess: true. Failure: false.
bool crypto_new_from_dir ( const char *  dirpath,
struct crypto_core **  core_ref 
)

Setup a crypto core from a dir.

Parameters
dirpathThe path of the setup directory.
core_refThe crypto core to setup.
Returns
Sucess: true. Failure: false.
static size_t crypto_pem_length ( const char *  pem)
static

Compute the PEM length.

Parameters
pemThe PEM.
Returns
Success: PEM length. Failure: 0.
static bool crypto_x509_add_ext ( X509 *  cert,
int  nid,
const char *  value,
bool  critical 
)
static

Add an extension to a x509 certificate.

Parameters
certThe cert to add the extension.
nidThe nid of the extenstion.
valueThe value of the extension.
criticalThe extension critism.
Returns
Success: true. Failure: false.
static X509* crypto_x509_from_file ( const char *  dirpath,
const char *  filename 
)
static

Get the x509 from file.

Parameters
dirpathThe path of the directory.
filenameThe filename.
Returns
Success: The x509. Failure: NULL.
static X509* crypto_x509_from_pem ( const char *  x509_pem)
static

Get the x509 from PEM.

Parameters
x509_pemThe x509 PEM.
Returns
Success: The x509. Failure: NULL.
static X509* crypto_x509_generate ( EC_KEY *  key)
static

Generate a new x509 certificate from an ec key.

Parameters
keyThe ec key.
Returns
The new x509 certificate.
int crypto_x509_get_bytes ( struct crypto_core core,
unsigned char **  buffer 
)

Get the x509 certificate bytes.

Parameters
coreThe crypto core.
bufferThe buffer to put the bytes.
Returns
The buffer length.