Libu2f-emu  0.0.0
Universal 2nd Factor (U2F) Emulation C Library
Data Structures | Macros | Functions
crypto.h File Reference
#include <stdbool.h>
#include <stdint.h>
#include <openssl/ec.h>
#include <openssl/x509.h>
Include dependency graph for crypto.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  crypto_core
 Crypto core of the U2F device. More...
 

Macros

#define CRYPTO_CERT_FILENAME   "certificate.pem"
 
#define CRYPTO_PRIVKEY_FILENAME   "private-key.pem"
 
#define CRYPTO_ENTROPY_FILENAME   "entropy"
 

Functions

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 *core, const unsigned char *data, int size, unsigned char **buffer)
 Decrypt data using AES. More...
 
size_t crypto_aes_encrypt (struct crypto_core *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 *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 *core, unsigned char **buffer)
 Get the x509 certificate bytes. More...
 
EC_KEY * crypto_ec_pubkey_from_priv (EC_KEY *privkey)
 Get the ec public key from its private key. More...
 
EC_KEY * crypto_ec_generate_key (void)
 Generate an ec pair key. More...
 
bool crypto_new_from_dir (const char *dirpath, struct crypto_core **core_ref)
 Setup a crypto core from a dir. More...
 
bool crypto_new_ephemeral (struct crypto_core **core_ref)
 Instantiate a new ephemeral crypto core. 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...
 
void crypto_free (struct crypto_core *core)
 Release the memory allocated by the crypto_core. More...
 

Macro Definition Documentation

#define CRYPTO_CERT_FILENAME   "certificate.pem"
#define CRYPTO_ENTROPY_FILENAME   "entropy"
#define CRYPTO_PRIVKEY_FILENAME   "private-key.pem"

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.
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.
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.
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.