2023-08-13 11:27:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
struct crypto_key;
|
|
|
|
struct crypto_cipher;
|
|
|
|
struct crypto_hash;
|
2023-08-15 21:41:11 +00:00
|
|
|
struct crypto_rsa_pub_key;
|
|
|
|
struct crypto_rsa_priv_key;
|
2023-08-15 22:56:42 +00:00
|
|
|
struct vec;
|
2023-08-13 11:27:16 +00:00
|
|
|
|
|
|
|
enum crypto_cipher_type {
|
|
|
|
CRYPTO_CIPHER_INVALID = 0,
|
|
|
|
CRYPTO_CIPHER_AES128_ECB,
|
2023-08-15 21:41:11 +00:00
|
|
|
CRYPTO_CIPHER_AES_EAX,
|
2023-09-04 22:15:40 +00:00
|
|
|
CRYPTO_CIPHER_AES256_EAX,
|
2023-08-13 11:27:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum crypto_hash_type {
|
|
|
|
CRYPTO_HASH_INVALID = 0,
|
|
|
|
CRYPTO_HASH_MD5,
|
2023-08-15 21:41:11 +00:00
|
|
|
CRYPTO_HASH_SHA1,
|
2023-08-13 11:27:16 +00:00
|
|
|
};
|
|
|
|
|
2023-09-04 21:40:02 +00:00
|
|
|
struct crypto_data_entry {
|
|
|
|
uint8_t* data;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
2023-08-15 21:41:11 +00:00
|
|
|
void crypto_dump_base16(const char* msg, const uint8_t* bytes, size_t len);
|
2023-08-13 11:27:16 +00:00
|
|
|
void crypto_dump_base64(const char* msg, const uint8_t* bytes, size_t len);
|
|
|
|
|
2023-08-15 21:41:11 +00:00
|
|
|
void crypto_random(uint8_t* dst, size_t len);
|
2023-08-13 11:27:16 +00:00
|
|
|
|
|
|
|
// Key generation
|
|
|
|
struct crypto_key* crypto_key_new(int g, const uint8_t *p, uint32_t p_len,
|
|
|
|
const uint8_t* q, uint32_t q_len);
|
|
|
|
void crypto_key_del(struct crypto_key* key);
|
|
|
|
|
|
|
|
int crypto_key_g(const struct crypto_key* key);
|
|
|
|
uint32_t crypto_key_p(const struct crypto_key* key, uint8_t* dst,
|
|
|
|
uint32_t dst_size);
|
|
|
|
uint32_t crypto_key_q(const struct crypto_key* key, uint8_t* dst,
|
|
|
|
uint32_t dst_size);
|
|
|
|
|
|
|
|
struct crypto_key* crypto_keygen(void);
|
|
|
|
|
|
|
|
// Diffie-Hellman
|
|
|
|
struct crypto_key* crypto_derive_public_key(const struct crypto_key* priv);
|
|
|
|
struct crypto_key* crypto_derive_shared_secret(
|
|
|
|
const struct crypto_key* own_secret,
|
|
|
|
const struct crypto_key* remote_public_key);
|
|
|
|
|
|
|
|
// Ciphers
|
2023-08-15 21:41:11 +00:00
|
|
|
struct crypto_cipher* crypto_cipher_new(const uint8_t* enc_key,
|
|
|
|
const uint8_t* dec_key, enum crypto_cipher_type type);
|
2023-08-13 11:27:16 +00:00
|
|
|
void crypto_cipher_del(struct crypto_cipher* self);
|
|
|
|
|
2023-08-15 22:56:42 +00:00
|
|
|
bool crypto_cipher_encrypt(struct crypto_cipher* self, struct vec* dst,
|
2023-09-03 22:30:10 +00:00
|
|
|
uint8_t* mac, const uint8_t* src, size_t len,
|
|
|
|
const uint8_t* ad, size_t ad_len);
|
2023-08-15 22:56:42 +00:00
|
|
|
ssize_t crypto_cipher_decrypt(struct crypto_cipher* self, uint8_t* dst,
|
2023-09-03 22:30:10 +00:00
|
|
|
uint8_t* mac, const uint8_t* src, size_t len,
|
|
|
|
const uint8_t* ad, size_t ad_len);
|
2023-08-13 11:27:16 +00:00
|
|
|
|
2023-08-15 21:41:11 +00:00
|
|
|
void crypto_cipher_set_ad(struct crypto_cipher* self, const uint8_t* ad,
|
|
|
|
size_t len);
|
|
|
|
void crypto_cipher_get_mac(struct crypto_cipher* self, uint8_t* dst,
|
|
|
|
size_t size);
|
|
|
|
|
2023-08-13 11:27:16 +00:00
|
|
|
// Hashing
|
|
|
|
struct crypto_hash* crypto_hash_new(enum crypto_hash_type type);
|
|
|
|
void crypto_hash_del(struct crypto_hash* self);
|
|
|
|
|
|
|
|
void crypto_hash_append(struct crypto_hash* self, const uint8_t* src,
|
|
|
|
size_t len);
|
|
|
|
void crypto_hash_digest(struct crypto_hash* self, uint8_t* dst,
|
|
|
|
size_t len);
|
2023-08-15 21:41:11 +00:00
|
|
|
|
2023-09-04 21:40:02 +00:00
|
|
|
void crypto_hash_one(uint8_t* dst, size_t dst_len, enum crypto_hash_type type,
|
|
|
|
const uint8_t* src, size_t src_len);
|
|
|
|
void crypto_hash_many(uint8_t* dst, size_t dst_len, enum crypto_hash_type type,
|
|
|
|
const struct crypto_data_entry *src);
|
|
|
|
|
2023-08-15 21:41:11 +00:00
|
|
|
// RSA
|
|
|
|
struct crypto_rsa_pub_key* crypto_rsa_pub_key_new(void);
|
|
|
|
void crypto_rsa_pub_key_del(struct crypto_rsa_pub_key*);
|
|
|
|
|
2023-08-15 22:56:42 +00:00
|
|
|
// Returns length in bytes
|
|
|
|
size_t crypto_rsa_pub_key_length(const struct crypto_rsa_pub_key* key);
|
|
|
|
|
2023-08-15 21:41:11 +00:00
|
|
|
struct crypto_rsa_pub_key* crypto_rsa_pub_key_import(const uint8_t* modulus,
|
|
|
|
const uint8_t* exponent, size_t size);
|
|
|
|
|
|
|
|
void crypto_rsa_pub_key_modulus(const struct crypto_rsa_pub_key* key,
|
|
|
|
uint8_t* dst, size_t dst_size);
|
|
|
|
void crypto_rsa_pub_key_exponent(const struct crypto_rsa_pub_key* key,
|
|
|
|
uint8_t* dst, size_t dst_size);
|
|
|
|
|
|
|
|
struct crypto_rsa_priv_key *crypto_rsa_priv_key_new(void);
|
|
|
|
void crypto_rsa_priv_key_del(struct crypto_rsa_priv_key*);
|
|
|
|
|
|
|
|
bool crypto_rsa_keygen(struct crypto_rsa_pub_key*, struct crypto_rsa_priv_key*);
|
|
|
|
|
|
|
|
ssize_t crypto_rsa_encrypt(struct crypto_rsa_pub_key* pub, uint8_t* dst,
|
|
|
|
size_t dst_size, const uint8_t* src, size_t src_size);
|
|
|
|
ssize_t crypto_rsa_decrypt(struct crypto_rsa_priv_key* priv, uint8_t* dst,
|
|
|
|
size_t dst_size, const uint8_t* src, size_t src_size);
|