2024-05-31 12:59:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024 Lucas Gabriel Vuotto <lucas@lgv5.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2024-06-07 19:47:42 +02:00
|
|
|
#ifndef LILCRYPTO_H
|
|
|
|
#define LILCRYPTO_H
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2024-05-31 23:54:10 +02:00
|
|
|
#include <stdio.h>
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constants.
|
|
|
|
*/
|
|
|
|
|
2024-06-06 14:40:38 +02:00
|
|
|
/* Hashes. */
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_SHA224_BLOCKLEN 64
|
2024-06-07 05:14:21 +02:00
|
|
|
#define LC_SHA224_HASHLEN 28
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_SHA256_BLOCKLEN 64
|
2024-06-07 05:14:21 +02:00
|
|
|
#define LC_SHA256_HASHLEN 32
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_SHA384_BLOCKLEN 128
|
2024-06-06 14:40:38 +02:00
|
|
|
#define LC_SHA384_HASHLEN 48
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_SHA512_BLOCKLEN 128
|
2024-06-06 14:40:38 +02:00
|
|
|
#define LC_SHA512_HASHLEN 64
|
2024-06-17 16:41:20 +02:00
|
|
|
#define LC_SHA512_224_BLOCKLEN 128
|
|
|
|
#define LC_SHA512_224_HASHLEN 28
|
|
|
|
#define LC_SHA512_256_BLOCKLEN 128
|
|
|
|
#define LC_SHA512_256_HASHLEN 32
|
2024-06-06 14:40:38 +02:00
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
/* Authentitcation. */
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_POLY1305_BLOCKLEN 16
|
2024-05-31 12:59:58 +02:00
|
|
|
#define LC_POLY1305_KEYLEN 32
|
|
|
|
#define LC_POLY1305_TAGLEN 16
|
|
|
|
|
|
|
|
/* Ciphers. */
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_CHACHA20_BLOCKLEN 64
|
2024-05-31 12:59:58 +02:00
|
|
|
#define LC_CHACHA20_KEYLEN 32
|
2024-06-07 20:40:14 +02:00
|
|
|
#define LC_CHACHA20_NONCELEN 12
|
2024-06-09 16:16:18 +02:00
|
|
|
#define LC_XCHACHA20_BLOCKLEN 64
|
2024-06-07 04:18:50 +02:00
|
|
|
#define LC_XCHACHA20_KEYLEN 32
|
2024-06-07 20:40:14 +02:00
|
|
|
#define LC_XCHACHA20_NONCELEN 24
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
/*
|
2024-06-09 16:55:34 +02:00
|
|
|
* Structs.
|
2024-06-07 20:40:14 +02:00
|
|
|
*/
|
|
|
|
|
2024-06-09 16:55:34 +02:00
|
|
|
struct lc_aead_impl;
|
|
|
|
|
|
|
|
struct lc_auth_ctx;
|
|
|
|
struct lc_auth_impl;
|
|
|
|
|
|
|
|
struct lc_cipher_ctx;
|
|
|
|
struct lc_cipher_impl;
|
|
|
|
|
|
|
|
struct lc_hash_ctx;
|
|
|
|
struct lc_hash_impl;
|
|
|
|
|
2024-06-18 16:30:06 +02:00
|
|
|
struct lc_kdf_impl;
|
|
|
|
|
2024-06-09 16:55:34 +02:00
|
|
|
/*
|
|
|
|
* Parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Authentication. */
|
2024-06-07 20:40:14 +02:00
|
|
|
|
|
|
|
struct lc_hmac_params {
|
2024-06-10 19:29:29 +02:00
|
|
|
struct lc_hash_ctx *hash;
|
|
|
|
size_t keylen;
|
|
|
|
uint8_t *key;
|
2024-06-07 20:40:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lc_poly1305_params {
|
2024-06-07 23:09:26 +02:00
|
|
|
uint8_t key[LC_POLY1305_KEYLEN];
|
2024-06-07 20:40:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Ciphers. */
|
|
|
|
|
|
|
|
struct lc_chacha20_params {
|
2024-06-07 23:09:26 +02:00
|
|
|
uint8_t key[LC_CHACHA20_KEYLEN];
|
|
|
|
uint8_t nonce[LC_CHACHA20_NONCELEN];
|
2024-06-07 20:40:14 +02:00
|
|
|
uint32_t counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct lc_xchacha20_params {
|
2024-06-07 23:09:26 +02:00
|
|
|
uint8_t key[LC_XCHACHA20_KEYLEN];
|
|
|
|
uint8_t nonce[LC_XCHACHA20_NONCELEN];
|
2024-06-07 20:40:14 +02:00
|
|
|
uint32_t counter;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* AEAD. */
|
|
|
|
|
|
|
|
struct lc_chacha20_poly1305_params {
|
2024-06-07 23:09:26 +02:00
|
|
|
uint8_t key[LC_CHACHA20_KEYLEN];
|
|
|
|
uint8_t nonce[LC_CHACHA20_NONCELEN];
|
2024-06-07 20:40:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lc_xchacha20_poly1305_params {
|
2024-06-07 23:09:26 +02:00
|
|
|
uint8_t key[LC_XCHACHA20_KEYLEN];
|
|
|
|
uint8_t nonce[LC_XCHACHA20_NONCELEN];
|
2024-06-07 20:40:14 +02:00
|
|
|
};
|
|
|
|
|
2024-06-18 16:30:06 +02:00
|
|
|
/* KDF. */
|
|
|
|
|
|
|
|
struct lc_hkdf_params {
|
|
|
|
struct lc_hash_ctx *hash;
|
|
|
|
struct lc_auth_ctx *hmac;
|
|
|
|
uint8_t *ikm;
|
|
|
|
size_t ikmlen;
|
|
|
|
uint8_t *info;
|
|
|
|
size_t infolen;
|
|
|
|
uint8_t *salt;
|
|
|
|
size_t saltlen;
|
|
|
|
};
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
/*
|
|
|
|
* Constant-time operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint32_t lc_ct_cmp(const uint8_t *, const uint8_t *, size_t);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2024-06-06 14:40:38 +02:00
|
|
|
* Hashes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int lc_hash_init(struct lc_hash_ctx *);
|
|
|
|
int lc_hash_update(struct lc_hash_ctx *, const uint8_t *, size_t);
|
|
|
|
int lc_hash_final(struct lc_hash_ctx *, uint8_t *, size_t *);
|
|
|
|
int lc_hash(const struct lc_hash_impl *, uint8_t *, size_t *,
|
|
|
|
const uint8_t *, size_t);
|
|
|
|
|
|
|
|
struct lc_hash_ctx *lc_hash_ctx_new(const struct lc_hash_impl *);
|
|
|
|
void lc_hash_ctx_free(struct lc_hash_ctx *);
|
|
|
|
|
2024-06-07 05:14:21 +02:00
|
|
|
const struct lc_hash_impl *lc_hash_impl_sha224(void);
|
|
|
|
const struct lc_hash_impl *lc_hash_impl_sha256(void);
|
2024-06-06 14:40:38 +02:00
|
|
|
const struct lc_hash_impl *lc_hash_impl_sha384(void);
|
|
|
|
const struct lc_hash_impl *lc_hash_impl_sha512(void);
|
2024-06-17 16:41:20 +02:00
|
|
|
const struct lc_hash_impl *lc_hash_impl_sha512_224(void);
|
|
|
|
const struct lc_hash_impl *lc_hash_impl_sha512_256(void);
|
2024-06-06 14:40:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Authentication.
|
2024-05-31 12:59:58 +02:00
|
|
|
*/
|
|
|
|
|
2024-06-09 17:09:15 +02:00
|
|
|
int lc_auth_init(struct lc_auth_ctx *, void *);
|
2024-05-31 12:59:58 +02:00
|
|
|
int lc_auth_update(struct lc_auth_ctx *, const uint8_t *, size_t);
|
|
|
|
int lc_auth_final(struct lc_auth_ctx *, uint8_t *, size_t *);
|
2024-06-09 17:09:15 +02:00
|
|
|
int lc_auth(const struct lc_auth_impl *, uint8_t *, size_t *, void *,
|
|
|
|
const uint8_t *, size_t);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
struct lc_auth_ctx *lc_auth_ctx_new(const struct lc_auth_impl *);
|
|
|
|
void lc_auth_ctx_free(struct lc_auth_ctx *);
|
|
|
|
|
2024-06-10 19:29:29 +02:00
|
|
|
const struct lc_auth_impl *lc_auth_impl_hmac(void);
|
2024-05-31 12:59:58 +02:00
|
|
|
const struct lc_auth_impl *lc_auth_impl_poly1305(void);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ciphers.
|
|
|
|
*/
|
|
|
|
|
2024-06-09 17:09:15 +02:00
|
|
|
int lc_cipher_encrypt_init(struct lc_cipher_ctx *, void *);
|
2024-05-31 12:59:58 +02:00
|
|
|
int lc_cipher_encrypt_update(struct lc_cipher_ctx *, uint8_t *, size_t *,
|
|
|
|
const uint8_t *, size_t);
|
|
|
|
int lc_cipher_encrypt_final(struct lc_cipher_ctx *, uint8_t *, size_t *);
|
2024-06-07 19:47:42 +02:00
|
|
|
int lc_cipher_encrypt(const struct lc_cipher_impl *, uint8_t *, size_t *,
|
2024-06-09 17:09:15 +02:00
|
|
|
void *, const uint8_t *, size_t);
|
|
|
|
int lc_cipher_decrypt_init(struct lc_cipher_ctx *, void *);
|
2024-05-31 12:59:58 +02:00
|
|
|
int lc_cipher_decrypt_update(struct lc_cipher_ctx *, uint8_t *, size_t *,
|
|
|
|
const uint8_t *, size_t);
|
|
|
|
int lc_cipher_decrypt_final(struct lc_cipher_ctx *, uint8_t *, size_t *);
|
2024-06-07 19:47:42 +02:00
|
|
|
int lc_cipher_decrypt(const struct lc_cipher_impl *, uint8_t *, size_t *,
|
2024-06-09 17:09:15 +02:00
|
|
|
void *, const uint8_t *, size_t);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
struct lc_cipher_ctx *lc_cipher_ctx_new(const struct lc_cipher_impl *);
|
|
|
|
void lc_cipher_ctx_free(struct lc_cipher_ctx *);
|
|
|
|
|
|
|
|
const struct lc_cipher_impl *lc_cipher_impl_chacha20(void);
|
2024-06-07 04:18:50 +02:00
|
|
|
const struct lc_cipher_impl *lc_cipher_impl_xchacha20(void);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Authenticated encryption with additional data.
|
|
|
|
*/
|
|
|
|
|
2024-06-09 17:09:15 +02:00
|
|
|
int lc_aead_seal(const struct lc_aead_impl *, uint8_t *, size_t *, void *,
|
|
|
|
const uint8_t *, size_t, const uint8_t *, size_t);
|
|
|
|
int lc_aead_open(const struct lc_aead_impl *, uint8_t *, size_t *, void *,
|
|
|
|
const uint8_t *, size_t, const uint8_t *, size_t);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
const struct lc_aead_impl *lc_aead_impl_chacha20_poly1305(void);
|
2024-06-07 04:18:50 +02:00
|
|
|
const struct lc_aead_impl *lc_aead_impl_xchacha20_poly1305(void);
|
2024-05-31 23:51:37 +02:00
|
|
|
|
|
|
|
|
2024-06-18 16:30:06 +02:00
|
|
|
/*
|
|
|
|
* Key derivation functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int lc_kdf(const struct lc_kdf_impl *, uint8_t *, size_t *, void *,
|
|
|
|
size_t);
|
|
|
|
|
|
|
|
const struct lc_kdf_impl *lc_kdf_impl_hkdf(void);
|
|
|
|
|
|
|
|
|
2024-05-31 23:51:37 +02:00
|
|
|
/*
|
|
|
|
* Utilities.
|
|
|
|
*/
|
|
|
|
|
2024-05-31 23:54:10 +02:00
|
|
|
int lc_hexdump_fp(FILE *, const void *, size_t);
|
2024-05-31 23:51:37 +02:00
|
|
|
void lc_scrub(void *, size_t);
|
2024-06-07 19:47:42 +02:00
|
|
|
|
|
|
|
#endif /* LILCRYPTO_H */
|