2020-07-28 05:19:17 +03:00
|
|
|
#ifndef URCRYPT_H
|
|
|
|
#define URCRYPT_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2020-08-01 03:30:09 +03:00
|
|
|
#include <stdbool.h>
|
2020-08-07 22:28:50 +03:00
|
|
|
#include <stdlib.h>
|
2020-08-11 01:19:32 +03:00
|
|
|
|
2020-08-07 22:28:50 +03:00
|
|
|
typedef void *(*urcrypt_malloc_t)(size_t);
|
|
|
|
typedef void *(*urcrypt_realloc_t)(void*, size_t);
|
|
|
|
typedef void (*urcrypt_free_t)(void*);
|
|
|
|
|
2020-08-18 20:31:50 +03:00
|
|
|
/* We depend on OpenSSL for various reasons, which doesn't promise not to
|
|
|
|
* allocate memory and has the annoying CRYPTO_set_mem_functions api. We
|
2020-08-18 22:35:07 +03:00
|
|
|
* are therefore forced to support it in some fashion.
|
2020-08-18 20:31:50 +03:00
|
|
|
*
|
2020-08-18 22:35:07 +03:00
|
|
|
* If you need to control urcrypt's internal OpenSSL allocation, you can call
|
2020-08-19 01:42:22 +03:00
|
|
|
* this function. It wraps the OpenSSL function, returning 0 on success.
|
2020-08-18 20:31:50 +03:00
|
|
|
*
|
2020-08-18 22:35:07 +03:00
|
|
|
* urcrypt will not use these functions directly.
|
2020-08-18 20:31:50 +03:00
|
|
|
*/
|
2020-08-19 01:42:22 +03:00
|
|
|
int urcrypt_set_openssl_mem_functions(urcrypt_malloc_t malloc_ptr,
|
|
|
|
urcrypt_realloc_t realloc_ptr,
|
|
|
|
urcrypt_free_t free_ptr);
|
2020-08-07 22:28:50 +03:00
|
|
|
|
2020-08-18 22:35:07 +03:00
|
|
|
// const arguments are not written to, non-const arguments may be
|
|
|
|
// array sizes[64] are purely documentary
|
2020-08-07 22:28:50 +03:00
|
|
|
|
2020-08-18 22:35:07 +03:00
|
|
|
// 0 on success, result in out
|
2020-08-07 22:28:50 +03:00
|
|
|
int urcrypt_ed_point_add(const uint8_t a[32],
|
|
|
|
const uint8_t b[32],
|
|
|
|
uint8_t out[32]);
|
|
|
|
int urcrypt_ed_scalarmult(const uint8_t a[32],
|
|
|
|
const uint8_t b[32],
|
|
|
|
uint8_t out[32]);
|
2020-08-18 22:35:07 +03:00
|
|
|
// void functions have no failure mode
|
2020-08-07 22:28:50 +03:00
|
|
|
void urcrypt_ed_scalarmult_base(const uint8_t a[32],
|
|
|
|
uint8_t out[32]);
|
|
|
|
int urcrypt_ed_add_scalarmult_scalarmult_base(const uint8_t a[32],
|
|
|
|
const uint8_t a_point[32],
|
|
|
|
const uint8_t b[32],
|
2020-08-01 00:21:02 +03:00
|
|
|
uint8_t out[32]);
|
2020-08-07 22:28:50 +03:00
|
|
|
int urcrypt_ed_add_double_scalarmult(const uint8_t a[32],
|
|
|
|
const uint8_t a_point[32],
|
|
|
|
const uint8_t b[32],
|
|
|
|
const uint8_t b_point[32],
|
2020-08-01 00:46:19 +03:00
|
|
|
uint8_t out[32]);
|
2020-08-06 02:06:04 +03:00
|
|
|
|
2020-08-07 22:28:50 +03:00
|
|
|
void urcrypt_ed_puck(const uint8_t seed[32],
|
|
|
|
uint8_t out[32]);
|
|
|
|
void urcrypt_ed_shar(const uint8_t public[32],
|
|
|
|
const uint8_t seed[32],
|
|
|
|
uint8_t out[32]);
|
|
|
|
void urcrypt_ed_sign(const uint8_t *message,
|
2020-07-31 00:40:47 +03:00
|
|
|
size_t length,
|
2020-08-07 22:28:50 +03:00
|
|
|
const uint8_t seed[32],
|
2020-08-01 02:31:56 +03:00
|
|
|
uint8_t out[64]);
|
2020-08-18 22:35:07 +03:00
|
|
|
// return value means the signature was (not) verified
|
2020-08-07 22:28:50 +03:00
|
|
|
bool urcrypt_ed_veri(const uint8_t *message,
|
|
|
|
size_t length,
|
|
|
|
const uint8_t signature[64],
|
|
|
|
const uint8_t public[32]);
|
|
|
|
|
2020-08-18 22:35:07 +03:00
|
|
|
int urcrypt_aes_ecba_en(uint8_t key[16], uint8_t block[16], uint8_t out[16]);
|
|
|
|
int urcrypt_aes_ecba_de(uint8_t key[16], uint8_t block[16], uint8_t out[16]);
|
|
|
|
int urcrypt_aes_ecbb_en(uint8_t key[24], uint8_t block[16], uint8_t out[16]);
|
|
|
|
int urcrypt_aes_ecbb_de(uint8_t key[24], uint8_t block[16], uint8_t out[16]);
|
|
|
|
int urcrypt_aes_ecbc_en(uint8_t key[32], uint8_t block[16], uint8_t out[16]);
|
|
|
|
int urcrypt_aes_ecbc_de(uint8_t key[32], uint8_t block[16], uint8_t out[16]);
|
|
|
|
|
|
|
|
// message and length are read/write so
|
|
|
|
// realloc_ptr can be used as realloc to pad message
|
|
|
|
int urcrypt_aes_cbca_en(uint8_t **message_ptr,
|
|
|
|
size_t *length_ptr,
|
|
|
|
uint8_t key[16],
|
|
|
|
uint8_t ivec[16],
|
|
|
|
urcrypt_realloc_t realloc_ptr);
|
|
|
|
int urcrypt_aes_cbca_de(uint8_t **message_ptr,
|
|
|
|
size_t *length_ptr,
|
|
|
|
uint8_t key[16],
|
|
|
|
uint8_t ivec[16],
|
|
|
|
urcrypt_realloc_t realloc_ptr);
|
|
|
|
int urcrypt_aes_cbcb_en(uint8_t **message_ptr,
|
|
|
|
size_t *length_ptr,
|
|
|
|
uint8_t key[24],
|
|
|
|
uint8_t ivec[16],
|
|
|
|
urcrypt_realloc_t realloc_ptr);
|
|
|
|
int urcrypt_aes_cbcb_de(uint8_t **message_ptr,
|
|
|
|
size_t *length_ptr,
|
|
|
|
uint8_t key[24],
|
|
|
|
uint8_t ivec[16],
|
|
|
|
urcrypt_realloc_t realloc_ptr);
|
|
|
|
int urcrypt_aes_cbcc_en(uint8_t **message_ptr,
|
|
|
|
size_t *length_ptr,
|
|
|
|
uint8_t key[32],
|
|
|
|
uint8_t ivec[16],
|
|
|
|
urcrypt_realloc_t realloc_ptr);
|
|
|
|
int urcrypt_aes_cbcc_de(uint8_t **message_ptr,
|
|
|
|
size_t *length_ptr,
|
|
|
|
uint8_t key[32],
|
|
|
|
uint8_t ivec[16],
|
|
|
|
urcrypt_realloc_t realloc_ptr);
|
2020-08-06 02:06:04 +03:00
|
|
|
|
2020-08-25 00:33:57 +03:00
|
|
|
typedef struct {
|
|
|
|
size_t length;
|
|
|
|
uint8_t *bytes;
|
|
|
|
} urcrypt_aes_siv_data;
|
|
|
|
|
|
|
|
int urcrypt_aes_siva_en(uint8_t *message,
|
|
|
|
size_t message_length,
|
|
|
|
urcrypt_aes_siv_data *data,
|
|
|
|
size_t data_length,
|
|
|
|
uint8_t key[32],
|
|
|
|
uint8_t iv[16],
|
|
|
|
uint8_t *out);
|
|
|
|
|
2020-08-21 00:43:22 +03:00
|
|
|
int urcrypt_ripemd160(uint8_t *message, size_t length, uint8_t out[20]);
|
|
|
|
|
2020-08-21 01:25:11 +03:00
|
|
|
void urcrypt_sha1(uint8_t *message, size_t length, uint8_t out[20]);
|
2020-08-21 22:32:48 +03:00
|
|
|
void urcrypt_shay(const uint8_t *message, size_t length, uint8_t out[32]);
|
|
|
|
void urcrypt_shal(const uint8_t *message, size_t length, uint8_t out[64]);
|
2020-08-21 02:01:16 +03:00
|
|
|
void urcrypt_shas(uint8_t *salt, size_t salt_length,
|
|
|
|
const uint8_t *message, size_t message_length,
|
|
|
|
uint8_t out[32]);
|
2020-08-21 01:25:11 +03:00
|
|
|
|
2020-08-11 01:19:32 +03:00
|
|
|
typedef enum urcrypt_argon2_type {
|
|
|
|
urcrypt_argon2_d = 0,
|
|
|
|
urcrypt_argon2_i = 1,
|
|
|
|
urcrypt_argon2_id = 2,
|
|
|
|
urcrypt_argon2_u = 10,
|
|
|
|
} urcrypt_argon2_type;
|
|
|
|
|
2020-08-18 20:31:50 +03:00
|
|
|
/* returns a constant error message string or NULL for success */
|
2020-08-11 01:19:32 +03:00
|
|
|
const char* urcrypt_argon2(urcrypt_argon2_type type,
|
|
|
|
uint32_t version,
|
|
|
|
uint32_t threads,
|
|
|
|
uint32_t memory_cost,
|
|
|
|
uint32_t time_cost,
|
|
|
|
size_t secret_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t *secret,
|
2020-08-11 01:19:32 +03:00
|
|
|
size_t associated_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t *associated,
|
2020-08-11 01:19:32 +03:00
|
|
|
size_t password_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t *password,
|
2020-08-11 01:19:32 +03:00
|
|
|
size_t salt_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t *salt,
|
2020-08-11 01:19:32 +03:00
|
|
|
size_t out_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t *out,
|
|
|
|
urcrypt_malloc_t malloc_ptr,
|
|
|
|
urcrypt_free_t free_ptr);
|
2020-08-11 01:19:32 +03:00
|
|
|
|
2020-08-12 01:01:24 +03:00
|
|
|
int urcrypt_blake2(size_t message_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t *message,
|
2020-08-12 01:01:24 +03:00
|
|
|
size_t key_length,
|
2020-08-18 22:35:07 +03:00
|
|
|
uint8_t key[64],
|
2020-08-12 01:01:24 +03:00
|
|
|
size_t out_length,
|
|
|
|
uint8_t *out);
|
|
|
|
|
2020-07-28 05:19:17 +03:00
|
|
|
#endif
|