From cef67c9f0910e1026a71ba1209c7b3aa9a8734df Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Vuotto Date: Mon, 17 Jun 2024 21:52:07 +0000 Subject: [PATCH] Merge most of internal headers into internal.h Fix the includes accordingly and get rid of some unused ones. --- aead.c | 3 +- aead.h | 29 ------ aead_chacha20_poly1305.c | 5 +- auth.c | 4 +- auth.h | 35 ------- auth_hmac.c | 8 +- auth_poly1305.c | 7 +- cipher.c | 3 +- cipher.h | 43 --------- cipher_chacha20.c | 6 +- hash.c | 4 +- hash.h | 35 ------- hash_sha224_sha256.c | 6 +- hash_sha384_sha512.c | 6 +- impl_chacha20.c | 5 +- impl_chacha20.h | 37 ------- impl_hmac.h | 26 ----- impl_poly1305.c | 5 +- impl_poly1305.h | 35 ------- impl_sha256.c | 5 +- impl_sha256.h | 32 ------ impl_sha512.c | 5 +- impl_sha512.h | 32 ------ internal.h | 204 +++++++++++++++++++++++++++++++++++++++ 24 files changed, 218 insertions(+), 362 deletions(-) delete mode 100644 aead.h delete mode 100644 auth.h delete mode 100644 cipher.h delete mode 100644 hash.h delete mode 100644 impl_chacha20.h delete mode 100644 impl_hmac.h delete mode 100644 impl_poly1305.h delete mode 100644 impl_sha256.h delete mode 100644 impl_sha512.h create mode 100644 internal.h diff --git a/aead.c b/aead.c index 7c7d689..28289da 100644 --- a/aead.c +++ b/aead.c @@ -14,8 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "lilcrypto.h" -#include "aead.h" +#include "internal.h" int diff --git a/aead.h b/aead.h deleted file mode 100644 index 06d51aa..0000000 --- a/aead.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include -#include - - -struct lc_aead_impl { - int (*seal)(uint8_t *, size_t *, void *, const uint8_t *, size_t, - const uint8_t *, size_t); - int (*open)(uint8_t *, size_t *, void *, const uint8_t *, size_t, - const uint8_t *, size_t); - - size_t argsz; - size_t blocklen; -}; diff --git a/aead_chacha20_poly1305.c b/aead_chacha20_poly1305.c index 6875e21..6f50554 100644 --- a/aead_chacha20_poly1305.c +++ b/aead_chacha20_poly1305.c @@ -14,10 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "lilcrypto.h" -#include "aead.h" -#include "impl_chacha20.h" - +#include "internal.h" #include "util.h" diff --git a/auth.c b/auth.c index 4d28657..b0e952d 100644 --- a/auth.c +++ b/auth.c @@ -15,10 +15,8 @@ */ #include -#include -#include "lilcrypto.h" -#include "auth.h" +#include "internal.h" int diff --git a/auth.h b/auth.h deleted file mode 100644 index 480daa9..0000000 --- a/auth.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include -#include - - -struct lc_auth_impl { - int (*init)(void *, void *); - int (*update)(void *, const uint8_t *, size_t); - int (*final)(void *, uint8_t *, size_t *); - int (*auth)(uint8_t *, size_t *, void *, const uint8_t *, size_t); - - size_t argsz; - size_t blocklen; - size_t taglen; -}; - -struct lc_auth_ctx { - const struct lc_auth_impl *impl; - void *arg; -}; diff --git a/auth_hmac.c b/auth_hmac.c index 44c9e6b..26fc2fc 100644 --- a/auth_hmac.c +++ b/auth_hmac.c @@ -14,13 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - -#include "lilcrypto.h" -#include "auth.h" -#include "hash.h" -#include "impl_hmac.h" - +#include "internal.h" #include "util.h" diff --git a/auth_poly1305.c b/auth_poly1305.c index ce705e4..f34fe30 100644 --- a/auth_poly1305.c +++ b/auth_poly1305.c @@ -14,12 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - -#include "lilcrypto.h" -#include "auth.h" -#include "impl_poly1305.h" - +#include "internal.h" #include "util.h" diff --git a/cipher.c b/cipher.c index 826dc08..79fa3e2 100644 --- a/cipher.c +++ b/cipher.c @@ -16,8 +16,7 @@ #include -#include "lilcrypto.h" -#include "cipher.h" +#include "internal.h" int diff --git a/cipher.h b/cipher.h deleted file mode 100644 index 3846fc7..0000000 --- a/cipher.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include -#include - - -struct lc_cipher_impl { - int (*encrypt_init)(void *, void *); - int (*encrypt_update)(void *, uint8_t *, size_t *, const uint8_t *, - size_t); - int (*encrypt_final)(void *, uint8_t *, size_t *); - int (*encrypt)(uint8_t *, size_t *, void *, const uint8_t *, - size_t); - - int (*decrypt_init)(void *, void *); - int (*decrypt_update)(void *, uint8_t *, size_t *, const uint8_t *, - size_t); - int (*decrypt_final)(void *, uint8_t *, size_t *); - int (*decrypt)(uint8_t *, size_t *, void *, const uint8_t *, - size_t); - - size_t argsz; - size_t blocklen; -}; - -struct lc_cipher_ctx { - const struct lc_cipher_impl *impl; - void *arg; -}; diff --git a/cipher_chacha20.c b/cipher_chacha20.c index 786e645..75c465d 100644 --- a/cipher_chacha20.c +++ b/cipher_chacha20.c @@ -15,12 +15,8 @@ */ #include -#include - -#include "lilcrypto.h" -#include "cipher.h" -#include "impl_chacha20.h" +#include "internal.h" #include "util.h" diff --git a/hash.c b/hash.c index 22aee66..ed04894 100644 --- a/hash.c +++ b/hash.c @@ -15,10 +15,8 @@ */ #include -#include -#include "lilcrypto.h" -#include "hash.h" +#include "internal.h" int diff --git a/hash.h b/hash.h deleted file mode 100644 index 7ef72de..0000000 --- a/hash.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include -#include - - -struct lc_hash_impl { - int (*init)(void *); - int (*update)(void *, const uint8_t *, size_t); - int (*final)(void *, uint8_t *, size_t *); - int (*hash)(uint8_t *, size_t *, const uint8_t *, size_t); - - size_t argsz; - size_t blocklen; - size_t hashlen; -}; - -struct lc_hash_ctx { - const struct lc_hash_impl *impl; - void *arg; -}; diff --git a/hash_sha224_sha256.c b/hash_sha224_sha256.c index 3ddf490..125a6f3 100644 --- a/hash_sha224_sha256.c +++ b/hash_sha224_sha256.c @@ -14,11 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - -#include "lilcrypto.h" -#include "hash.h" -#include "impl_sha256.h" +#include "internal.h" #include "util.h" diff --git a/hash_sha384_sha512.c b/hash_sha384_sha512.c index de64499..bf52e90 100644 --- a/hash_sha384_sha512.c +++ b/hash_sha384_sha512.c @@ -14,11 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - -#include "lilcrypto.h" -#include "hash.h" -#include "impl_sha512.h" +#include "internal.h" #include "util.h" diff --git a/impl_chacha20.c b/impl_chacha20.c index ca731ca..f6a0e97 100644 --- a/impl_chacha20.c +++ b/impl_chacha20.c @@ -14,10 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include - -#include "impl_chacha20.h" +#include "internal.h" #include "util.h" diff --git a/impl_chacha20.h b/impl_chacha20.h deleted file mode 100644 index a7eca8d..0000000 --- a/impl_chacha20.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include "lilcrypto.h" - - -#define CHACHA20_BLOCKLEN_WORDS (LC_CHACHA20_BLOCKLEN / sizeof(uint32_t)) -#define CHACHA20_CTRMAX 4294967295 /* 2^32 - 1 */ -#define CHACHA20_KEY_WORDS (LC_CHACHA20_KEYLEN / sizeof(uint32_t)) -#define CHACHA20_NONCE_WORDS 4 -#define CHACHA20_ROUNDS 10 - - -struct chacha20_state { - uint32_t s[CHACHA20_BLOCKLEN_WORDS]; - uint32_t k[CHACHA20_KEY_WORDS]; - uint32_t n[CHACHA20_NONCE_WORDS]; - size_t blen; - uint8_t b[LC_CHACHA20_BLOCKLEN]; -}; - - -void chacha20_block(struct chacha20_state *); -void hchacha20_block(struct chacha20_state *); diff --git a/impl_hmac.h b/impl_hmac.h deleted file mode 100644 index 506febb..0000000 --- a/impl_hmac.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include "lilcrypto.h" - - -#define HMAC_BLOCKLEN_MAX LC_SHA512_BLOCKLEN - - -struct hmac_state { - struct lc_hash_ctx *hash; - uint8_t key[HMAC_BLOCKLEN_MAX]; -}; diff --git a/impl_poly1305.c b/impl_poly1305.c index ece9f57..5c24223 100644 --- a/impl_poly1305.c +++ b/impl_poly1305.c @@ -14,10 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include - -#include "impl_poly1305.h" +#include "internal.h" #include "util.h" diff --git a/impl_poly1305.h b/impl_poly1305.h deleted file mode 100644 index 92a39d7..0000000 --- a/impl_poly1305.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include "lilcrypto.h" - - -#define POLY1305_TAGLEN_WORDS (LC_POLY1305_TAGLEN / sizeof(uint32_t)) - - -struct poly1305_state { - uint32_t h0, h1, h2, h3, h4; - uint32_t r0, r1, r2, r3, r4; - uint32_t x1, x2, x3, x4; - uint32_t s0, s1, s2, s3; - size_t blen; - uint8_t b[LC_POLY1305_BLOCKLEN]; -}; - - -void poly1305_block(struct poly1305_state *, uint32_t); -void poly1305_reduce(struct poly1305_state *, - uint32_t [POLY1305_TAGLEN_WORDS]); diff --git a/impl_sha256.c b/impl_sha256.c index 812f3fb..97d2914 100644 --- a/impl_sha256.c +++ b/impl_sha256.c @@ -14,10 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include - -#include "impl_sha256.h" +#include "internal.h" #include "util.h" diff --git a/impl_sha256.h b/impl_sha256.h deleted file mode 100644 index d109098..0000000 --- a/impl_sha256.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include "lilcrypto.h" - - -#define SHA256_BLOCKLEN_WORDS (LC_SHA256_BLOCKLEN / sizeof(uint32_t)) -#define SHA256_ROUNDS 64 - - -struct sha256_state { - uint32_t h0, h1, h2, h3, h4, h5, h6, h7; - uint64_t sz; - size_t blen; - uint8_t b[LC_SHA256_BLOCKLEN]; -}; - - -void sha256_block(struct sha256_state *); diff --git a/impl_sha512.c b/impl_sha512.c index 854a831..b47431d 100644 --- a/impl_sha512.c +++ b/impl_sha512.c @@ -14,10 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include - -#include "impl_sha512.h" +#include "internal.h" #include "util.h" diff --git a/impl_sha512.h b/impl_sha512.h deleted file mode 100644 index a33db47..0000000 --- a/impl_sha512.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2024 Lucas Gabriel Vuotto - * - * 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. - */ - -#include "lilcrypto.h" - - -#define SHA512_BLOCKLEN_WORDS (LC_SHA512_BLOCKLEN / sizeof(uint64_t)) -#define SHA512_ROUNDS 80 - - -struct sha512_state { - uint64_t h0, h1, h2, h3, h4, h5, h6, h7; - uint64_t szhi, szlo; - size_t blen; - uint8_t b[LC_SHA512_BLOCKLEN]; -}; - - -void sha512_block(struct sha512_state *); diff --git a/internal.h b/internal.h new file mode 100644 index 0000000..f8c53de --- /dev/null +++ b/internal.h @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2024 Lucas Gabriel Vuotto + * + * 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. + */ + +#ifndef LC_INTERNAL_H +#define LC_INTERNAL_H + +#include "lilcrypto.h" + + +/* + * CONSTANTS + */ + +/* Authentitcation. */ + +#define HMAC_BLOCKLEN_MAX LC_SHA512_BLOCKLEN + +#define POLY1305_TAGLEN_WORDS (LC_POLY1305_TAGLEN / sizeof(uint32_t)) + +/* Ciphers. */ + +#define CHACHA20_BLOCKLEN_WORDS (LC_CHACHA20_BLOCKLEN / sizeof(uint32_t)) +#define CHACHA20_CTRMAX 4294967295 /* 2^32 - 1 */ +#define CHACHA20_KEY_WORDS (LC_CHACHA20_KEYLEN / sizeof(uint32_t)) +#define CHACHA20_NONCE_WORDS 4 +#define CHACHA20_ROUNDS 10 + +/* Hashes. */ + +#define SHA256_BLOCKLEN_WORDS (LC_SHA256_BLOCKLEN / sizeof(uint32_t)) +#define SHA256_ROUNDS 64 + +#define SHA512_BLOCKLEN_WORDS (LC_SHA512_BLOCKLEN / sizeof(uint64_t)) +#define SHA512_ROUNDS 80 + + +/* + * STRUCTS + */ + + +/* + * *_impl provides the function pointers to the actual implementation of methods, + * serving as an interface to the cryptographic algorithms. + */ + +struct lc_aead_impl { + int (*seal)(uint8_t *, size_t *, void *, const uint8_t *, size_t, + const uint8_t *, size_t); + int (*open)(uint8_t *, size_t *, void *, const uint8_t *, size_t, + const uint8_t *, size_t); + + size_t argsz; + size_t blocklen; +}; + +struct lc_auth_impl { + int (*init)(void *, void *); + int (*update)(void *, const uint8_t *, size_t); + int (*final)(void *, uint8_t *, size_t *); + int (*auth)(uint8_t *, size_t *, void *, const uint8_t *, size_t); + + size_t argsz; + size_t blocklen; + size_t taglen; +}; + +struct lc_cipher_impl { + int (*encrypt_init)(void *, void *); + int (*encrypt_update)(void *, uint8_t *, size_t *, const uint8_t *, + size_t); + int (*encrypt_final)(void *, uint8_t *, size_t *); + int (*encrypt)(uint8_t *, size_t *, void *, const uint8_t *, + size_t); + + int (*decrypt_init)(void *, void *); + int (*decrypt_update)(void *, uint8_t *, size_t *, const uint8_t *, + size_t); + int (*decrypt_final)(void *, uint8_t *, size_t *); + int (*decrypt)(uint8_t *, size_t *, void *, const uint8_t *, + size_t); + + size_t argsz; + size_t blocklen; +}; + +struct lc_hash_impl { + int (*init)(void *); + int (*update)(void *, const uint8_t *, size_t); + int (*final)(void *, uint8_t *, size_t *); + int (*hash)(uint8_t *, size_t *, const uint8_t *, size_t); + + size_t argsz; + size_t blocklen; + size_t hashlen; +}; + + +/* + * *_ctx binds an *_impl with an state, effectively representing an instance of a + * cryptographic algorithm. + */ + +struct lc_auth_ctx { + const struct lc_auth_impl *impl; + void *arg; +}; + +struct lc_cipher_ctx { + const struct lc_cipher_impl *impl; + void *arg; +}; + +struct lc_hash_ctx { + const struct lc_hash_impl *impl; + void *arg; +}; + +/* + * *_state holds the internal state of the cryptographic algorithms. + */ + +/* Authentication. */ + +struct hmac_state { + struct lc_hash_ctx *hash; + uint8_t key[HMAC_BLOCKLEN_MAX]; +}; + +struct poly1305_state { + uint32_t h0, h1, h2, h3, h4; + uint32_t r0, r1, r2, r3, r4; + uint32_t x1, x2, x3, x4; + uint32_t s0, s1, s2, s3; + size_t blen; + uint8_t b[LC_POLY1305_BLOCKLEN]; +}; + + +/* Ciphers. */ + +struct chacha20_state { + uint32_t s[CHACHA20_BLOCKLEN_WORDS]; + uint32_t k[CHACHA20_KEY_WORDS]; + uint32_t n[CHACHA20_NONCE_WORDS]; + size_t blen; + uint8_t b[LC_CHACHA20_BLOCKLEN]; +}; + + +/* Hashes. */ + +struct sha256_state { + uint32_t h0, h1, h2, h3, h4, h5, h6, h7; + uint64_t sz; + size_t blen; + uint8_t b[LC_SHA256_BLOCKLEN]; +}; + +struct sha512_state { + uint64_t h0, h1, h2, h3, h4, h5, h6, h7; + uint64_t szhi, szlo; + size_t blen; + uint8_t b[LC_SHA512_BLOCKLEN]; +}; + + + +/* + * PROTOTYPES + */ + +/* Authentitcation. */ + +void poly1305_block(struct poly1305_state *, uint32_t); +void poly1305_reduce(struct poly1305_state *, uint32_t [POLY1305_TAGLEN_WORDS]); + + +/* Ciphers. */ + +void chacha20_block(struct chacha20_state *); +void hchacha20_block(struct chacha20_state *); + + +/* Hashes. */ + +void sha256_block(struct sha256_state *); + +void sha512_block(struct sha512_state *); + +#endif /* LC_INTERNAL_H */