Libu2f-emu  0.0.0
Universal 2nd Factor (U2F) Emulation C Library
crypto.h
Go to the documentation of this file.
1 #ifndef CRYPTO
2 #define CRYPTO
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 
7 #include <openssl/ec.h>
8 #include <openssl/x509.h>
9 
10 /* Filenames */
11 #define CRYPTO_CERT_FILENAME "certificate.pem"
12 #define CRYPTO_PRIVKEY_FILENAME "private-key.pem"
13 #define CRYPTO_ENTROPY_FILENAME "entropy"
14 
15 
20 {
21  X509 *cert;
22  EC_KEY *privkey;
23  EC_KEY *pubkey;
24  uint8_t entropy[48];
25 };
26 
34 EC_KEY *crypto_ec_bytes_to_key(const unsigned char *buffer,
35  long size);
36 
46 size_t crypto_aes_decrypt(struct crypto_core* core,
47  const unsigned char *data, int size,
48  unsigned char **buffer);
49 
59 size_t crypto_aes_encrypt(struct crypto_core* core,
60  const unsigned char *data, int data_len,
61  unsigned char **buffer);
62 
70 int crypto_ec_key_to_bytes(EC_KEY *key, unsigned char **buffer);
71 
81 unsigned int crypto_ec_sign_with_key(EC_KEY *key,
82  const unsigned char *digest,
83  int digest_len,
84  unsigned char **signature);
85 
95 unsigned int crypto_ec_sign(struct crypto_core *core,
96  const unsigned char *digest,
97  int digest_len,
98  unsigned char **signature);
99 
108 size_t crypto_hash(const void *data, size_t data_len,
109  unsigned char **hash);
110 
118 size_t crypto_ec_pubkey_to_bytes(const EC_KEY *key,
119  unsigned char **buffer);
120 
128 int crypto_x509_get_bytes(struct crypto_core *core,
129  unsigned char **buffer);
130 
137 EC_KEY *crypto_ec_pubkey_from_priv(EC_KEY *privkey);
138 
145 EC_KEY *crypto_ec_generate_key(void);
146 
155 bool crypto_new_from_dir(const char *dirpath,
156  struct crypto_core **core_ref);
157 
165 bool crypto_new_ephemeral(struct crypto_core **core_ref);
166 
177 bool crypto_new(const char *certificate,
178  const char *private_key, const uint8_t entropy[48],
179  struct crypto_core **core_ref);
180 
186 void crypto_free(struct crypto_core *core);
187 
188 
189 #endif
size_t crypto_aes_decrypt(struct crypto_core *core, const unsigned char *data, int size, unsigned char **buffer)
Decrypt data using AES.
Definition: crypto.c:59
int crypto_ec_key_to_bytes(EC_KEY *key, unsigned char **buffer)
Get the ec key bytes.
Definition: crypto.c:188
EC_KEY * crypto_ec_generate_key(void)
Generate an ec pair key.
Definition: crypto.c:401
bool crypto_new_from_dir(const char *dirpath, struct crypto_core **core_ref)
Setup a crypto core from a dir.
Definition: crypto.c:652
void crypto_free(struct crypto_core *core)
Release the memory allocated by the crypto_core.
Definition: crypto.c:696
EC_KEY * crypto_ec_pubkey_from_priv(EC_KEY *privkey)
Get the ec public key from its private key.
Definition: crypto.c:554
EC_KEY * crypto_ec_bytes_to_key(const unsigned char *buffer, long size)
Get the ec key from ec key bytes.
Definition: crypto.c:48
EC_KEY * privkey
Definition: crypto.h:22
int crypto_x509_get_bytes(struct crypto_core *core, unsigned char **buffer)
Get the x509 certificate bytes.
Definition: crypto.c:309
uint8_t data[(64-7)]
Definition: packet.h:45
size_t crypto_hash(const void *data, size_t data_len, unsigned char **hash)
Hash data using sha256.
Definition: crypto.c:260
size_t crypto_aes_encrypt(struct crypto_core *core, const unsigned char *data, int data_len, unsigned char **buffer)
Encrypt data using AES.
Definition: crypto.c:125
uint8_t entropy[48]
Definition: crypto.h:24
X509 * cert
Definition: crypto.h:21
Crypto core of the U2F device.
Definition: crypto.h:19
unsigned int crypto_ec_sign(struct crypto_core *core, const unsigned char *digest, int digest_len, unsigned char **signature)
Sign a digest.
Definition: crypto.c:251
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.
Definition: crypto.c:578
bool crypto_new_ephemeral(struct crypto_core **core_ref)
Instantiate a new ephemeral crypto core.
Definition: crypto.c:614
EC_KEY * pubkey
Definition: crypto.h:23
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.
Definition: crypto.c:197
size_t crypto_ec_pubkey_to_bytes(const EC_KEY *key, unsigned char **buffer)
Get the ec key bytes.
Definition: crypto.c:291