From b929e79dba5a8189d5e311b10a3b372aab078774 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 9 Jul 2024 09:21:48 +0900 Subject: [PATCH] Expose callback API for replacing low-level cryptographic primitives This makes the callback API to replace low-level cryptographic implementation public again after open-quantum-safe#1667. Signed-off-by: Daiki Ueno --- CMakeLists.txt | 4 + docs/.Doxyfile | 6 +- src/common/aes/aes.h | 82 +---------- src/common/aes/aes_ops.h | 104 ++++++++++++++ src/common/sha2/sha2.h | 152 +-------------------- src/common/sha2/sha2_ops.h | 176 ++++++++++++++++++++++++ src/common/sha3/sha3.h | 232 +------------------------------ src/common/sha3/sha3_ops.h | 256 +++++++++++++++++++++++++++++++++++ src/common/sha3/sha3x4.h | 162 +--------------------- src/common/sha3/sha3x4_ops.h | 182 +++++++++++++++++++++++++ src/oqs.h | 4 + 11 files changed, 737 insertions(+), 623 deletions(-) create mode 100644 src/common/aes/aes_ops.h create mode 100644 src/common/sha2/sha2_ops.h create mode 100644 src/common/sha3/sha3_ops.h create mode 100644 src/common/sha3/sha3x4_ops.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 654ab86340..0524a07c5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,8 +150,12 @@ if(${OQS_USE_OPENSSL}) endif() set(PUBLIC_HEADERS ${PROJECT_SOURCE_DIR}/src/oqs.h + ${PROJECT_SOURCE_DIR}/src/common/aes/aes_ops.h ${PROJECT_SOURCE_DIR}/src/common/common.h ${PROJECT_SOURCE_DIR}/src/common/rand/rand.h + ${PROJECT_SOURCE_DIR}/src/common/sha2/sha2_ops.h + ${PROJECT_SOURCE_DIR}/src/common/sha3/sha3_ops.h + ${PROJECT_SOURCE_DIR}/src/common/sha3/sha3x4_ops.h ${PROJECT_SOURCE_DIR}/src/kem/kem.h ${PROJECT_SOURCE_DIR}/src/sig/sig.h ${PROJECT_SOURCE_DIR}/src/sig_stfl/sig_stfl.h) diff --git a/docs/.Doxyfile b/docs/.Doxyfile index 5401ab9851..1e7dbd33b2 100644 --- a/docs/.Doxyfile +++ b/docs/.Doxyfile @@ -949,8 +949,12 @@ WARN_LOGFILE = # spaces. See also FILE_PATTERNS and EXTENSION_MAPPING # Note: If this tag is empty the current directory is searched. -INPUT = src/common/common.h \ +INPUT = src/common/aes/aes_ops.h \ + src/common/common.h \ src/common/rand/rand.h \ + src/common/sha2/sha2_ops.h \ + src/common/sha3/sha3_ops.h \ + src/common/sha3/sha3x4_ops.h \ src/kem/kem.h \ src/sig/sig.h \ README.md \ diff --git a/src/common/aes/aes.h b/src/common/aes/aes.h index 0194b907a6..011686b3e9 100644 --- a/src/common/aes/aes.h +++ b/src/common/aes/aes.h @@ -14,7 +14,7 @@ #include #include -#include +#include #if defined(__cplusplus) extern "C" { @@ -149,86 +149,6 @@ void OQS_AES256_CTR_inc_stream_iv(const uint8_t *iv, size_t iv_len, const void * */ void OQS_AES256_CTR_inc_stream_blks(void *ctx, uint8_t *out, size_t out_blks); -/** Data structure implemented by cryptographic provider for AES operations. - */ -struct OQS_AES_callbacks { - /** - * Implementation of function OQS_AES128_ECB_load_schedule. - */ - void (*AES128_ECB_load_schedule)(const uint8_t *key, void **ctx); - - /** - * Implementation of function OQS_AES128_free_schedule. - */ - void (*AES128_free_schedule)(void *ctx); - - /** - * Implementation of function OQS_AES128_ECB_enc. - */ - void (*AES128_ECB_enc)(const uint8_t *plaintext, const size_t plaintext_len, const uint8_t *key, uint8_t *ciphertext); - - /** - * Implementation of function OQS_AES128_ECB_enc_sch. - */ - void (*AES128_ECB_enc_sch)(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext); - - /** - * Implementation of function OQS_AES256_ECB_load_schedule. - */ - void (*AES256_ECB_load_schedule)(const uint8_t *key, void **ctx); - - /** - * Implementation of function OQS_AES256_CTR_inc_init. - */ - void (*AES256_CTR_inc_init)(const uint8_t *key, void **ctx); - - /** - * Implementation of function OQS_AES256_CTR_inc_iv. - */ - void (*AES256_CTR_inc_iv)(const uint8_t *iv, size_t iv_len, void *ctx); - - /** - * Implementation of function OQS_AES256_CTR_inc_ivu64. - */ - void (*AES256_CTR_inc_ivu64)(uint64_t iv, void *ctx); - - /** - * Implementation of function OQS_AES256_free_schedule. - */ - void (*AES256_free_schedule)(void *ctx); - - /** - * Implementation of function OQS_AES256_ECB_enc. - */ - void (*AES256_ECB_enc)(const uint8_t *plaintext, const size_t plaintext_len, const uint8_t *key, uint8_t *ciphertext); - - /** - * Implementation of function OQS_AES256_ECB_enc_sch. - */ - void (*AES256_ECB_enc_sch)(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext); - - /** - * Implementation of function OQS_AES256_CTR_inc_stream_iv. - */ - void (*AES256_CTR_inc_stream_iv)(const uint8_t *iv, size_t iv_len, const void *ctx, uint8_t *out, size_t out_len); - - /** - * Implementation of function OQS_AES256_CTR_inc_stream_blks. - */ - void (*AES256_CTR_inc_stream_blks)(void *ctx, uint8_t *out, size_t out_blks); -}; - -/** - * Set callback functions for AES operations. - * - * This function may be called before OQS_init to switch the - * cryptographic provider for AES operations. If it is not called, the - * default provider determined at build time will be used. - * - * @param[in] new_callbacks Callback functions defined in OQS_AES_callbacks - */ -OQS_API void OQS_AES_set_callbacks(struct OQS_AES_callbacks *new_callbacks); - #if defined(__cplusplus) } // extern "C" #endif diff --git a/src/common/aes/aes_ops.h b/src/common/aes/aes_ops.h new file mode 100644 index 0000000000..5a26f75764 --- /dev/null +++ b/src/common/aes/aes_ops.h @@ -0,0 +1,104 @@ +/** + * \file aes_ops.h + * \brief Header defining the callback API for OQS AES + * + * SPDX-License-Identifier: MIT + */ + +#ifndef OQS_AES_OPS_H +#define OQS_AES_OPS_H + +#include +#include + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +/** Data structure implemented by cryptographic provider for AES operations. + */ +struct OQS_AES_callbacks { + /** + * Implementation of function OQS_AES128_ECB_load_schedule. + */ + void (*AES128_ECB_load_schedule)(const uint8_t *key, void **ctx); + + /** + * Implementation of function OQS_AES128_free_schedule. + */ + void (*AES128_free_schedule)(void *ctx); + + /** + * Implementation of function OQS_AES128_ECB_enc. + */ + void (*AES128_ECB_enc)(const uint8_t *plaintext, const size_t plaintext_len, const uint8_t *key, uint8_t *ciphertext); + + /** + * Implementation of function OQS_AES128_ECB_enc_sch. + */ + void (*AES128_ECB_enc_sch)(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext); + + /** + * Implementation of function OQS_AES256_ECB_load_schedule. + */ + void (*AES256_ECB_load_schedule)(const uint8_t *key, void **ctx); + + /** + * Implementation of function OQS_AES256_CTR_inc_init. + */ + void (*AES256_CTR_inc_init)(const uint8_t *key, void **ctx); + + /** + * Implementation of function OQS_AES256_CTR_inc_iv. + */ + void (*AES256_CTR_inc_iv)(const uint8_t *iv, size_t iv_len, void *ctx); + + /** + * Implementation of function OQS_AES256_CTR_inc_ivu64. + */ + void (*AES256_CTR_inc_ivu64)(uint64_t iv, void *ctx); + + /** + * Implementation of function OQS_AES256_free_schedule. + */ + void (*AES256_free_schedule)(void *ctx); + + /** + * Implementation of function OQS_AES256_ECB_enc. + */ + void (*AES256_ECB_enc)(const uint8_t *plaintext, const size_t plaintext_len, const uint8_t *key, uint8_t *ciphertext); + + /** + * Implementation of function OQS_AES256_ECB_enc_sch. + */ + void (*AES256_ECB_enc_sch)(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext); + + /** + * Implementation of function OQS_AES256_CTR_inc_stream_iv. + */ + void (*AES256_CTR_inc_stream_iv)(const uint8_t *iv, size_t iv_len, const void *ctx, uint8_t *out, size_t out_len); + + /** + * Implementation of function OQS_AES256_CTR_inc_stream_blks. + */ + void (*AES256_CTR_inc_stream_blks)(void *ctx, uint8_t *out, size_t out_blks); +}; + +/** + * Set callback functions for AES operations. + * + * This function may be called before OQS_init to switch the + * cryptographic provider for AES operations. If it is not called, the + * default provider determined at build time will be used. + * + * @param[in] new_callbacks Callback functions defined in OQS_AES_callbacks + */ +OQS_API void OQS_AES_set_callbacks(struct OQS_AES_callbacks *new_callbacks); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // OQS_AES_OPS_H diff --git a/src/common/sha2/sha2.h b/src/common/sha2/sha2.h index cd993e69c8..e7aaf54d54 100644 --- a/src/common/sha2/sha2.h +++ b/src/common/sha2/sha2.h @@ -18,22 +18,12 @@ #include #include -#include +#include #if defined(__cplusplus) extern "C" { #endif -/** Data structure for the state of the SHA-224 incremental hashing API. */ -typedef struct { - /** Internal state */ - void *ctx; - /** current number of bytes in data */ - size_t data_len; - /** unprocessed data buffer */ - uint8_t data[128]; -} OQS_SHA2_sha224_ctx; - /** * \brief Process a message with SHA-256 and return the hash code in the output byte array. * @@ -45,16 +35,6 @@ typedef struct { */ void OQS_SHA2_sha256(uint8_t *output, const uint8_t *input, size_t inplen); -/** Data structure for the state of the SHA-256 incremental hashing API. */ -typedef struct { - /** Internal state */ - void *ctx; - /** current number of bytes in data */ - size_t data_len; - /** unprocessed data buffer */ - uint8_t data[128]; -} OQS_SHA2_sha256_ctx; - /** * \brief Allocate and initialize the state for the SHA-256 incremental hashing API. * @@ -134,16 +114,6 @@ void OQS_SHA2_sha256_inc_ctx_release(OQS_SHA2_sha256_ctx *state); */ void OQS_SHA2_sha384(uint8_t *output, const uint8_t *input, size_t inplen); -/** Data structure for the state of the SHA-384 incremental hashing API. */ -typedef struct { - /** Internal state. */ - void *ctx; - /** current number of bytes in data */ - size_t data_len; - /** unprocessed data buffer */ - uint8_t data[128]; -} OQS_SHA2_sha384_ctx; - /** * \brief Allocate and initialize the state for the SHA-384 incremental hashing API. * @@ -212,16 +182,6 @@ void OQS_SHA2_sha384_inc_ctx_release(OQS_SHA2_sha384_ctx *state); */ void OQS_SHA2_sha512(uint8_t *output, const uint8_t *input, size_t inplen); -/** Data structure for the state of the SHA-512 incremental hashing API. */ -typedef struct { - /** Internal state. */ - void *ctx; - /** current number of bytes in data */ - size_t data_len; - /** unprocessed data buffer */ - uint8_t data[128]; -} OQS_SHA2_sha512_ctx; - /** * \brief Allocate and initialize the state for the SHA-512 incremental hashing API. * @@ -279,116 +239,6 @@ void OQS_SHA2_sha512_inc_finalize(uint8_t *out, OQS_SHA2_sha512_ctx *state, cons */ void OQS_SHA2_sha512_inc_ctx_release(OQS_SHA2_sha512_ctx *state); -/** Data structure implemented by cryptographic provider for SHA-2 operations. - */ -struct OQS_SHA2_callbacks { - /** - * Implementation of function OQS_SHA2_sha256. - */ - void (*SHA2_sha256)(uint8_t *output, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA2_sha256_inc_init. - */ - void (*SHA2_sha256_inc_init)(OQS_SHA2_sha256_ctx *state); - - /** - * Implementation of function OQS_SHA2_sha256_inc_ctx_clone. - */ - void (*SHA2_sha256_inc_ctx_clone)(OQS_SHA2_sha256_ctx *dest, const OQS_SHA2_sha256_ctx *src); - - /** - * Implementation of function OQS_SHA2_sha256_inc. - */ - void (*SHA2_sha256_inc)(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t len); - - /** - * Implementation of function OQS_SHA2_sha256_inc_blocks. - */ - void (*SHA2_sha256_inc_blocks)(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inblocks); - - /** - * Implementation of function OQS_SHA2_sha256_inc_finalize. - */ - void (*SHA2_sha256_inc_finalize)(uint8_t *out, OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inlen); - - /** - * Implementation of function OQS_SHA2_sha256_inc_ctx_release. - */ - void (*SHA2_sha256_inc_ctx_release)(OQS_SHA2_sha256_ctx *state); - - /** - * Implementation of function OQS_SHA2_sha384. - */ - void (*SHA2_sha384)(uint8_t *output, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA2_sha384_inc_init. - */ - void (*SHA2_sha384_inc_init)(OQS_SHA2_sha384_ctx *state); - - /** - * Implementation of function OQS_SHA2_sha384_inc_ctx_clone. - */ - void (*SHA2_sha384_inc_ctx_clone)(OQS_SHA2_sha384_ctx *dest, const OQS_SHA2_sha384_ctx *src); - - /** - * Implementation of function OQS_SHA2_sha384_inc_blocks. - */ - void (*SHA2_sha384_inc_blocks)(OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inblocks); - - /** - * Implementation of function OQS_SHA2_sha384_inc_finalize. - */ - void (*SHA2_sha384_inc_finalize)(uint8_t *out, OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inlen); - - /** - * Implementation of function OQS_SHA2_sha384_inc_ctx_release. - */ - void (*SHA2_sha384_inc_ctx_release)(OQS_SHA2_sha384_ctx *state); - - /** - * Implementation of function OQS_SHA2_sha512. - */ - void (*SHA2_sha512)(uint8_t *output, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA2_sha512_inc_init. - */ - void (*SHA2_sha512_inc_init)(OQS_SHA2_sha512_ctx *state); - - /** - * Implementation of function OQS_SHA2_sha512_inc_ctx_clone. - */ - void (*SHA2_sha512_inc_ctx_clone)(OQS_SHA2_sha512_ctx *dest, const OQS_SHA2_sha512_ctx *src); - - /** - * Implementation of function OQS_SHA2_sha512_inc_blocks. - */ - void (*SHA2_sha512_inc_blocks)(OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inblocks); - - /** - * Implementation of function OQS_SHA2_sha512_inc_finalize. - */ - void (*SHA2_sha512_inc_finalize)(uint8_t *out, OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inlen); - - /** - * Implementation of function OQS_SHA2_sha512_inc_ctx_release. - */ - void (*SHA2_sha512_inc_ctx_release)(OQS_SHA2_sha512_ctx *state); -}; - -/** - * Set callback functions for SHA2 operations. - * - * This function may be called before OQS_init to switch the - * cryptographic provider for SHA2 operations. If it is not called, - * the default provider determined at build time will be used. - * - * @param[in] new_callbacks Callback functions defined in OQS_SHA2_callbacks - */ -OQS_API void OQS_SHA2_set_callbacks(struct OQS_SHA2_callbacks *new_callbacks); - #if defined(__cplusplus) } // extern "C" #endif diff --git a/src/common/sha2/sha2_ops.h b/src/common/sha2/sha2_ops.h new file mode 100644 index 0000000000..7b8ae8c992 --- /dev/null +++ b/src/common/sha2/sha2_ops.h @@ -0,0 +1,176 @@ +/** + * \file sha2_ops.h + * \brief Header defining the callback API for OQS SHA2 + * + * \author Douglas Stebila + * + * SPDX-License-Identifier: MIT + */ + +#ifndef OQS_SHA2_OPS_H +#define OQS_SHA2_OPS_H + +#include +#include + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +/** Data structure for the state of the SHA-224 incremental hashing API. */ +typedef struct { + /** Internal state */ + void *ctx; + /** current number of bytes in data */ + size_t data_len; + /** unprocessed data buffer */ + uint8_t data[128]; +} OQS_SHA2_sha224_ctx; + +/** Data structure for the state of the SHA-256 incremental hashing API. */ +typedef struct { + /** Internal state */ + void *ctx; + /** current number of bytes in data */ + size_t data_len; + /** unprocessed data buffer */ + uint8_t data[128]; +} OQS_SHA2_sha256_ctx; + +/** Data structure for the state of the SHA-384 incremental hashing API. */ +typedef struct { + /** Internal state. */ + void *ctx; + /** current number of bytes in data */ + size_t data_len; + /** unprocessed data buffer */ + uint8_t data[128]; +} OQS_SHA2_sha384_ctx; + +/** Data structure for the state of the SHA-512 incremental hashing API. */ +typedef struct { + /** Internal state. */ + void *ctx; + /** current number of bytes in data */ + size_t data_len; + /** unprocessed data buffer */ + uint8_t data[128]; +} OQS_SHA2_sha512_ctx; + +/** Data structure implemented by cryptographic provider for SHA-2 operations. + */ +struct OQS_SHA2_callbacks { + /** + * Implementation of function OQS_SHA2_sha256. + */ + void (*SHA2_sha256)(uint8_t *output, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA2_sha256_inc_init. + */ + void (*SHA2_sha256_inc_init)(OQS_SHA2_sha256_ctx *state); + + /** + * Implementation of function OQS_SHA2_sha256_inc_ctx_clone. + */ + void (*SHA2_sha256_inc_ctx_clone)(OQS_SHA2_sha256_ctx *dest, const OQS_SHA2_sha256_ctx *src); + + /** + * Implementation of function OQS_SHA2_sha256_inc. + */ + void (*SHA2_sha256_inc)(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t len); + + /** + * Implementation of function OQS_SHA2_sha256_inc_blocks. + */ + void (*SHA2_sha256_inc_blocks)(OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inblocks); + + /** + * Implementation of function OQS_SHA2_sha256_inc_finalize. + */ + void (*SHA2_sha256_inc_finalize)(uint8_t *out, OQS_SHA2_sha256_ctx *state, const uint8_t *in, size_t inlen); + + /** + * Implementation of function OQS_SHA2_sha256_inc_ctx_release. + */ + void (*SHA2_sha256_inc_ctx_release)(OQS_SHA2_sha256_ctx *state); + + /** + * Implementation of function OQS_SHA2_sha384. + */ + void (*SHA2_sha384)(uint8_t *output, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA2_sha384_inc_init. + */ + void (*SHA2_sha384_inc_init)(OQS_SHA2_sha384_ctx *state); + + /** + * Implementation of function OQS_SHA2_sha384_inc_ctx_clone. + */ + void (*SHA2_sha384_inc_ctx_clone)(OQS_SHA2_sha384_ctx *dest, const OQS_SHA2_sha384_ctx *src); + + /** + * Implementation of function OQS_SHA2_sha384_inc_blocks. + */ + void (*SHA2_sha384_inc_blocks)(OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inblocks); + + /** + * Implementation of function OQS_SHA2_sha384_inc_finalize. + */ + void (*SHA2_sha384_inc_finalize)(uint8_t *out, OQS_SHA2_sha384_ctx *state, const uint8_t *in, size_t inlen); + + /** + * Implementation of function OQS_SHA2_sha384_inc_ctx_release. + */ + void (*SHA2_sha384_inc_ctx_release)(OQS_SHA2_sha384_ctx *state); + + /** + * Implementation of function OQS_SHA2_sha512. + */ + void (*SHA2_sha512)(uint8_t *output, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA2_sha512_inc_init. + */ + void (*SHA2_sha512_inc_init)(OQS_SHA2_sha512_ctx *state); + + /** + * Implementation of function OQS_SHA2_sha512_inc_ctx_clone. + */ + void (*SHA2_sha512_inc_ctx_clone)(OQS_SHA2_sha512_ctx *dest, const OQS_SHA2_sha512_ctx *src); + + /** + * Implementation of function OQS_SHA2_sha512_inc_blocks. + */ + void (*SHA2_sha512_inc_blocks)(OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inblocks); + + /** + * Implementation of function OQS_SHA2_sha512_inc_finalize. + */ + void (*SHA2_sha512_inc_finalize)(uint8_t *out, OQS_SHA2_sha512_ctx *state, const uint8_t *in, size_t inlen); + + /** + * Implementation of function OQS_SHA2_sha512_inc_ctx_release. + */ + void (*SHA2_sha512_inc_ctx_release)(OQS_SHA2_sha512_ctx *state); +}; + +/** + * Set callback functions for SHA2 operations. + * + * This function may be called before OQS_init to switch the + * cryptographic provider for SHA2 operations. If it is not called, + * the default provider determined at build time will be used. + * + * @param[in] new_callbacks Callback functions defined in OQS_SHA2_callbacks + */ +OQS_API void OQS_SHA2_set_callbacks(struct OQS_SHA2_callbacks *new_callbacks); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // OQS_SHA2_OPS_H diff --git a/src/common/sha3/sha3.h b/src/common/sha3/sha3.h index ef259f428b..7fa9c9a9eb 100644 --- a/src/common/sha3/sha3.h +++ b/src/common/sha3/sha3.h @@ -18,7 +18,7 @@ #include #include -#include +#include #if defined(__cplusplus) extern "C" { @@ -40,12 +40,6 @@ extern "C" { */ void OQS_SHA3_sha3_256(uint8_t *output, const uint8_t *input, size_t inplen); -/** Data structure for the state of the incremental SHA3-256 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_sha3_256_inc_ctx; - /** * \brief Initialize the state for the incremental SHA3-256 API. * @@ -115,12 +109,6 @@ void OQS_SHA3_sha3_256_inc_ctx_clone(OQS_SHA3_sha3_256_inc_ctx *dest, const OQS_ */ void OQS_SHA3_sha3_384(uint8_t *output, const uint8_t *input, size_t inplen); -/** Data structure for the state of the incremental SHA3-384 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_sha3_384_inc_ctx; - /** * \brief Initialize the state for the incremental SHA3-384 API. * @@ -190,12 +178,6 @@ void OQS_SHA3_sha3_384_inc_ctx_clone(OQS_SHA3_sha3_384_inc_ctx *dest, const OQS_ */ void OQS_SHA3_sha3_512(uint8_t *output, const uint8_t *input, size_t inplen); -/** Data structure for the state of the incremental SHA3-512 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_sha3_512_inc_ctx; - /** * \brief Initialize the state for the incremental SHA3-512 API. * @@ -268,12 +250,6 @@ void OQS_SHA3_sha3_512_inc_ctx_clone(OQS_SHA3_sha3_512_inc_ctx *dest, const OQS_ */ void OQS_SHA3_shake128(uint8_t *output, size_t outlen, const uint8_t *input, size_t inplen); -/** Data structure for the state of the incremental SHAKE-128 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_shake128_inc_ctx; - /** * \brief Initialize the state for the incremental SHAKE-128 API. * @@ -355,12 +331,6 @@ void OQS_SHA3_shake128_inc_ctx_reset(OQS_SHA3_shake128_inc_ctx *state); */ void OQS_SHA3_shake256(uint8_t *output, size_t outlen, const uint8_t *input, size_t inplen); -/** Data structure for the state of the incremental SHAKE-256 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_shake256_inc_ctx; - /** * \brief Initialize the state for the incremental SHAKE-256 API. * @@ -423,206 +393,6 @@ void OQS_SHA3_shake256_inc_ctx_clone(OQS_SHA3_shake256_inc_ctx *dest, const OQS_ */ void OQS_SHA3_shake256_inc_ctx_reset(OQS_SHA3_shake256_inc_ctx *state); -/** Data structure implemented by cryptographic provider for SHA-3 operations. - */ -struct OQS_SHA3_callbacks { - /** - * Implementation of function OQS_SHA3_sha3_256. - */ - void (*SHA3_sha3_256)(uint8_t *output, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA3_sha3_256_inc_init. - */ - void (*SHA3_sha3_256_inc_init)(OQS_SHA3_sha3_256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_256_inc_absorb. - */ - void (*SHA3_sha3_256_inc_absorb)(OQS_SHA3_sha3_256_inc_ctx *state, const uint8_t *input, size_t inlen); - - /** - * Implementation of function OQS_SHA3_sha3_256_inc_finalize. - */ - void (*SHA3_sha3_256_inc_finalize)(uint8_t *output, OQS_SHA3_sha3_256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_256_inc_ctx_release. - */ - void (*SHA3_sha3_256_inc_ctx_release)(OQS_SHA3_sha3_256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_256_inc_ctx_reset. - */ - void (*SHA3_sha3_256_inc_ctx_reset)(OQS_SHA3_sha3_256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_256_inc_ctx_clone. - */ - void (*SHA3_sha3_256_inc_ctx_clone)(OQS_SHA3_sha3_256_inc_ctx *dest, const OQS_SHA3_sha3_256_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_sha3_384. - */ - void (*SHA3_sha3_384)(uint8_t *output, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA3_sha3_384_inc_init. - */ - void (*SHA3_sha3_384_inc_init)(OQS_SHA3_sha3_384_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_384_inc_absorb. - */ - void (*SHA3_sha3_384_inc_absorb)(OQS_SHA3_sha3_384_inc_ctx *state, const uint8_t *input, size_t inlen); - - /** - * Implementation of function OQS_SHA3_sha3_384_inc_finalize. - */ - void (*SHA3_sha3_384_inc_finalize)(uint8_t *output, OQS_SHA3_sha3_384_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_384_inc_ctx_release. - */ - void (*SHA3_sha3_384_inc_ctx_release)(OQS_SHA3_sha3_384_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_384_inc_ctx_reset. - */ - void (*SHA3_sha3_384_inc_ctx_reset)(OQS_SHA3_sha3_384_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_384_inc_ctx_clone. - */ - void (*SHA3_sha3_384_inc_ctx_clone)(OQS_SHA3_sha3_384_inc_ctx *dest, const OQS_SHA3_sha3_384_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_sha3_512. - */ - void (*SHA3_sha3_512)(uint8_t *output, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA3_sha3_512_inc_init. - */ - void (*SHA3_sha3_512_inc_init)(OQS_SHA3_sha3_512_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_512_inc_absorb. - */ - void (*SHA3_sha3_512_inc_absorb)(OQS_SHA3_sha3_512_inc_ctx *state, const uint8_t *input, size_t inlen); - - /** - * Implementation of function OQS_SHA3_sha3_512_inc_finalize. - */ - void (*SHA3_sha3_512_inc_finalize)(uint8_t *output, OQS_SHA3_sha3_512_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_512_inc_ctx_release. - */ - void (*SHA3_sha3_512_inc_ctx_release)(OQS_SHA3_sha3_512_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_512_inc_ctx_reset. - */ - void (*SHA3_sha3_512_inc_ctx_reset)(OQS_SHA3_sha3_512_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_sha3_512_inc_ctx_clone. - */ - void (*SHA3_sha3_512_inc_ctx_clone)(OQS_SHA3_sha3_512_inc_ctx *dest, const OQS_SHA3_sha3_512_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_shake128. - */ - void (*SHA3_shake128)(uint8_t *output, size_t outlen, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA3_shake128_inc_init. - */ - void (*SHA3_shake128_inc_init)(OQS_SHA3_shake128_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_inc_absorb. - */ - void (*SHA3_shake128_inc_absorb)(OQS_SHA3_shake128_inc_ctx *state, const uint8_t *input, size_t inlen); - - /** - * Implementation of function OQS_SHA3_shake128_inc_finalize. - */ - void (*SHA3_shake128_inc_finalize)(OQS_SHA3_shake128_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_inc_squeeze. - */ - void (*SHA3_shake128_inc_squeeze)(uint8_t *output, size_t outlen, OQS_SHA3_shake128_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_inc_ctx_release. - */ - void (*SHA3_shake128_inc_ctx_release)(OQS_SHA3_shake128_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_inc_ctx_clone. - */ - void (*SHA3_shake128_inc_ctx_clone)(OQS_SHA3_shake128_inc_ctx *dest, const OQS_SHA3_shake128_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_shake128_inc_ctx_reset. - */ - void (*SHA3_shake128_inc_ctx_reset)(OQS_SHA3_shake128_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256. - */ - void (*SHA3_shake256)(uint8_t *output, size_t outlen, const uint8_t *input, size_t inplen); - - /** - * Implementation of function OQS_SHA3_shake256_inc_init. - */ - void (*SHA3_shake256_inc_init)(OQS_SHA3_shake256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_inc_absorb. - */ - void (*SHA3_shake256_inc_absorb)(OQS_SHA3_shake256_inc_ctx *state, const uint8_t *input, size_t inlen); - - /** - * Implementation of function OQS_SHA3_shake256_inc_finalize. - */ - void (*SHA3_shake256_inc_finalize)(OQS_SHA3_shake256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_inc_squeeze. - */ - void (*SHA3_shake256_inc_squeeze)(uint8_t *output, size_t outlen, OQS_SHA3_shake256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_inc_ctx_release. - */ - void (*SHA3_shake256_inc_ctx_release)(OQS_SHA3_shake256_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_inc_ctx_clone. - */ - void (*SHA3_shake256_inc_ctx_clone)(OQS_SHA3_shake256_inc_ctx *dest, const OQS_SHA3_shake256_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_shake256_inc_ctx_reset. - */ - void (*SHA3_shake256_inc_ctx_reset)(OQS_SHA3_shake256_inc_ctx *state); -}; - -/** - * Set callback functions for SHA3 operations. - * - * This function may be called before OQS_init to switch the - * cryptographic provider for SHA3 operations. If it is not called, - * the default provider determined at build time will be used. - * - * @param new_callbacks Callback functions defined in OQS_SHA3_callbacks struct - */ -OQS_API void OQS_SHA3_set_callbacks(struct OQS_SHA3_callbacks *new_callbacks); - #if defined(__cplusplus) } // extern "C" #endif diff --git a/src/common/sha3/sha3_ops.h b/src/common/sha3/sha3_ops.h new file mode 100644 index 0000000000..694fc21873 --- /dev/null +++ b/src/common/sha3/sha3_ops.h @@ -0,0 +1,256 @@ +/** + * \file sha3_ops.h + * \brief Header defining the callback API for OQS SHA3 and SHAKE + * + * \author John Underhill, Douglas Stebila + * + * SPDX-License-Identifier: MIT + */ + +#ifndef OQS_SHA3_OPS_H +#define OQS_SHA3_OPS_H + +#include +#include + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +/** Data structure for the state of the incremental SHA3-256 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_sha3_256_inc_ctx; + +/** Data structure for the state of the incremental SHA3-384 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_sha3_384_inc_ctx; + +/** Data structure for the state of the incremental SHA3-512 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_sha3_512_inc_ctx; + +/** Data structure for the state of the incremental SHAKE-128 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_shake128_inc_ctx; + +/** Data structure for the state of the incremental SHAKE-256 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_shake256_inc_ctx; + +/** Data structure implemented by cryptographic provider for SHA-3 operations. + */ +struct OQS_SHA3_callbacks { + /** + * Implementation of function OQS_SHA3_sha3_256. + */ + void (*SHA3_sha3_256)(uint8_t *output, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA3_sha3_256_inc_init. + */ + void (*SHA3_sha3_256_inc_init)(OQS_SHA3_sha3_256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_256_inc_absorb. + */ + void (*SHA3_sha3_256_inc_absorb)(OQS_SHA3_sha3_256_inc_ctx *state, const uint8_t *input, size_t inlen); + + /** + * Implementation of function OQS_SHA3_sha3_256_inc_finalize. + */ + void (*SHA3_sha3_256_inc_finalize)(uint8_t *output, OQS_SHA3_sha3_256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_256_inc_ctx_release. + */ + void (*SHA3_sha3_256_inc_ctx_release)(OQS_SHA3_sha3_256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_256_inc_ctx_reset. + */ + void (*SHA3_sha3_256_inc_ctx_reset)(OQS_SHA3_sha3_256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_256_inc_ctx_clone. + */ + void (*SHA3_sha3_256_inc_ctx_clone)(OQS_SHA3_sha3_256_inc_ctx *dest, const OQS_SHA3_sha3_256_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_sha3_384. + */ + void (*SHA3_sha3_384)(uint8_t *output, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA3_sha3_384_inc_init. + */ + void (*SHA3_sha3_384_inc_init)(OQS_SHA3_sha3_384_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_384_inc_absorb. + */ + void (*SHA3_sha3_384_inc_absorb)(OQS_SHA3_sha3_384_inc_ctx *state, const uint8_t *input, size_t inlen); + + /** + * Implementation of function OQS_SHA3_sha3_384_inc_finalize. + */ + void (*SHA3_sha3_384_inc_finalize)(uint8_t *output, OQS_SHA3_sha3_384_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_384_inc_ctx_release. + */ + void (*SHA3_sha3_384_inc_ctx_release)(OQS_SHA3_sha3_384_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_384_inc_ctx_reset. + */ + void (*SHA3_sha3_384_inc_ctx_reset)(OQS_SHA3_sha3_384_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_384_inc_ctx_clone. + */ + void (*SHA3_sha3_384_inc_ctx_clone)(OQS_SHA3_sha3_384_inc_ctx *dest, const OQS_SHA3_sha3_384_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_sha3_512. + */ + void (*SHA3_sha3_512)(uint8_t *output, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA3_sha3_512_inc_init. + */ + void (*SHA3_sha3_512_inc_init)(OQS_SHA3_sha3_512_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_512_inc_absorb. + */ + void (*SHA3_sha3_512_inc_absorb)(OQS_SHA3_sha3_512_inc_ctx *state, const uint8_t *input, size_t inlen); + + /** + * Implementation of function OQS_SHA3_sha3_512_inc_finalize. + */ + void (*SHA3_sha3_512_inc_finalize)(uint8_t *output, OQS_SHA3_sha3_512_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_512_inc_ctx_release. + */ + void (*SHA3_sha3_512_inc_ctx_release)(OQS_SHA3_sha3_512_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_512_inc_ctx_reset. + */ + void (*SHA3_sha3_512_inc_ctx_reset)(OQS_SHA3_sha3_512_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_sha3_512_inc_ctx_clone. + */ + void (*SHA3_sha3_512_inc_ctx_clone)(OQS_SHA3_sha3_512_inc_ctx *dest, const OQS_SHA3_sha3_512_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_shake128. + */ + void (*SHA3_shake128)(uint8_t *output, size_t outlen, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA3_shake128_inc_init. + */ + void (*SHA3_shake128_inc_init)(OQS_SHA3_shake128_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_inc_absorb. + */ + void (*SHA3_shake128_inc_absorb)(OQS_SHA3_shake128_inc_ctx *state, const uint8_t *input, size_t inlen); + + /** + * Implementation of function OQS_SHA3_shake128_inc_finalize. + */ + void (*SHA3_shake128_inc_finalize)(OQS_SHA3_shake128_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_inc_squeeze. + */ + void (*SHA3_shake128_inc_squeeze)(uint8_t *output, size_t outlen, OQS_SHA3_shake128_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_inc_ctx_release. + */ + void (*SHA3_shake128_inc_ctx_release)(OQS_SHA3_shake128_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_inc_ctx_clone. + */ + void (*SHA3_shake128_inc_ctx_clone)(OQS_SHA3_shake128_inc_ctx *dest, const OQS_SHA3_shake128_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_shake128_inc_ctx_reset. + */ + void (*SHA3_shake128_inc_ctx_reset)(OQS_SHA3_shake128_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256. + */ + void (*SHA3_shake256)(uint8_t *output, size_t outlen, const uint8_t *input, size_t inplen); + + /** + * Implementation of function OQS_SHA3_shake256_inc_init. + */ + void (*SHA3_shake256_inc_init)(OQS_SHA3_shake256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_inc_absorb. + */ + void (*SHA3_shake256_inc_absorb)(OQS_SHA3_shake256_inc_ctx *state, const uint8_t *input, size_t inlen); + + /** + * Implementation of function OQS_SHA3_shake256_inc_finalize. + */ + void (*SHA3_shake256_inc_finalize)(OQS_SHA3_shake256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_inc_squeeze. + */ + void (*SHA3_shake256_inc_squeeze)(uint8_t *output, size_t outlen, OQS_SHA3_shake256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_inc_ctx_release. + */ + void (*SHA3_shake256_inc_ctx_release)(OQS_SHA3_shake256_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_inc_ctx_clone. + */ + void (*SHA3_shake256_inc_ctx_clone)(OQS_SHA3_shake256_inc_ctx *dest, const OQS_SHA3_shake256_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_shake256_inc_ctx_reset. + */ + void (*SHA3_shake256_inc_ctx_reset)(OQS_SHA3_shake256_inc_ctx *state); +}; + +/** + * Set callback functions for SHA3 operations. + * + * This function may be called before OQS_init to switch the + * cryptographic provider for SHA3 operations. If it is not called, + * the default provider determined at build time will be used. + * + * @param new_callbacks Callback functions defined in OQS_SHA3_callbacks struct + */ +OQS_API void OQS_SHA3_set_callbacks(struct OQS_SHA3_callbacks *new_callbacks); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // OQS_SHA3_OPS_H diff --git a/src/common/sha3/sha3x4.h b/src/common/sha3/sha3x4.h index 1dc3c79ed2..aabea5b19e 100644 --- a/src/common/sha3/sha3x4.h +++ b/src/common/sha3/sha3x4.h @@ -1,5 +1,5 @@ /** - * \file shakex4.h + * \file sha3x4.h * \brief SHA3, SHAKE, and cSHAKE functions; not part of the OQS public API * * Contains the API and documentation for SHA3 digest and SHAKE implementations. @@ -18,7 +18,7 @@ #include #include -#include +#include #if defined(__cplusplus) extern "C" { @@ -52,12 +52,6 @@ void OQS_SHA3_shake128_x4( const uint8_t *in3, size_t inlen); -/** Data structure for the state of the four-way parallel incremental SHAKE-128 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_shake128_x4_inc_ctx; - /** * \brief Initialize the state for four-way parallel incremental SHAKE-128 API. * @@ -169,12 +163,6 @@ void OQS_SHA3_shake256_x4( const uint8_t *in3, size_t inlen); -/** Data structure for the state of the four-way parallel incremental SHAKE-256 API. */ -typedef struct { - /** Internal state. */ - void *ctx; -} OQS_SHA3_shake256_x4_inc_ctx; - /** * \brief Initialize the state for four-way parallel incremental SHAKE-256 API. * @@ -257,152 +245,8 @@ void OQS_SHA3_shake256_x4_inc_ctx_clone( */ void OQS_SHA3_shake256_x4_inc_ctx_reset(OQS_SHA3_shake256_x4_inc_ctx *state); -/** Data structure implemented by cryptographic provider for the - * four-way parallel incremental SHAKE-256 operations. - */ -struct OQS_SHA3_x4_callbacks { - /** - * Implementation of function OQS_SHA3_shake128_x4. - */ - void (*SHA3_shake128_x4)( - uint8_t *out0, - uint8_t *out1, - uint8_t *out2, - uint8_t *out3, - size_t outlen, - const uint8_t *in0, - const uint8_t *in1, - const uint8_t *in2, - const uint8_t *in3, - size_t inlen); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_init. - */ - void (*SHA3_shake128_x4_inc_init)(OQS_SHA3_shake128_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_absorb. - */ - void (*SHA3_shake128_x4_inc_absorb)( - OQS_SHA3_shake128_x4_inc_ctx *state, - const uint8_t *in0, - const uint8_t *in1, - const uint8_t *in2, - const uint8_t *in3, - size_t inlen); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_finalize. - */ - void (*SHA3_shake128_x4_inc_finalize)(OQS_SHA3_shake128_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_squeeze. - */ - void (*SHA3_shake128_x4_inc_squeeze)( - uint8_t *out0, - uint8_t *out1, - uint8_t *out2, - uint8_t *out3, - size_t outlen, - OQS_SHA3_shake128_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_ctx_release. - */ - void (*SHA3_shake128_x4_inc_ctx_release)(OQS_SHA3_shake128_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_ctx_clone. - */ - void (*SHA3_shake128_x4_inc_ctx_clone)( - OQS_SHA3_shake128_x4_inc_ctx *dest, - const OQS_SHA3_shake128_x4_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_shake128_x4_inc_ctx_reset. - */ - void (*SHA3_shake128_x4_inc_ctx_reset)(OQS_SHA3_shake128_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_x4. - */ - void (*SHA3_shake256_x4)( - uint8_t *out0, - uint8_t *out1, - uint8_t *out2, - uint8_t *out3, - size_t outlen, - const uint8_t *in0, - const uint8_t *in1, - const uint8_t *in2, - const uint8_t *in3, - size_t inlen); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_init. - */ - void (*SHA3_shake256_x4_inc_init)(OQS_SHA3_shake256_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_absorb. - */ - void (*SHA3_shake256_x4_inc_absorb)( - OQS_SHA3_shake256_x4_inc_ctx *state, - const uint8_t *in0, - const uint8_t *in1, - const uint8_t *in2, - const uint8_t *in3, - size_t inlen); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_finalize. - */ - void (*SHA3_shake256_x4_inc_finalize)(OQS_SHA3_shake256_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_squeeze. - */ - void (*SHA3_shake256_x4_inc_squeeze)( - uint8_t *out0, - uint8_t *out1, - uint8_t *out2, - uint8_t *out3, - size_t outlen, - OQS_SHA3_shake256_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_ctx_release. - */ - void (*SHA3_shake256_x4_inc_ctx_release)(OQS_SHA3_shake256_x4_inc_ctx *state); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_ctx_clone. - */ - void (*SHA3_shake256_x4_inc_ctx_clone)( - OQS_SHA3_shake256_x4_inc_ctx *dest, - const OQS_SHA3_shake256_x4_inc_ctx *src); - - /** - * Implementation of function OQS_SHA3_shake256_x4_inc_ctx_reset. - */ - void (*SHA3_shake256_x4_inc_ctx_reset)(OQS_SHA3_shake256_x4_inc_ctx *state); -}; - -/** - * Set callback functions for 4-parallel SHA3 operations. - * - * This function may be called before OQS_init to switch the - * cryptographic provider for 4-parallel SHA3 operations. If it is not - * called, the default provider determined at build time will be used. - * - * @param new_callbacks Callback functions defined in OQS_SHA3_x4_callbacks struct - */ -OQS_API void OQS_SHA3_x4_set_callbacks(struct OQS_SHA3_x4_callbacks *new_callbacks); - #if defined(__cplusplus) } // extern "C" #endif -#endif // OQS_SHA3_H +#endif // OQS_SHA3X4_H diff --git a/src/common/sha3/sha3x4_ops.h b/src/common/sha3/sha3x4_ops.h new file mode 100644 index 0000000000..32e4cc377c --- /dev/null +++ b/src/common/sha3/sha3x4_ops.h @@ -0,0 +1,182 @@ +/** + * \file sha3x4_ops.h + * \brief Header defining the callback API for OQS SHA3 and SHAKE + * + * \author John Underhill, Douglas Stebila + * + * SPDX-License-Identifier: MIT + */ + +#ifndef OQS_SHA3X4_OPS_H +#define OQS_SHA3X4_OPS_H + +#include +#include + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +/** Data structure for the state of the four-way parallel incremental SHAKE-128 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_shake128_x4_inc_ctx; + +/** Data structure for the state of the four-way parallel incremental SHAKE-256 API. */ +typedef struct { + /** Internal state. */ + void *ctx; +} OQS_SHA3_shake256_x4_inc_ctx; + +/** Data structure implemented by cryptographic provider for the + * four-way parallel incremental SHAKE-256 operations. + */ +struct OQS_SHA3_x4_callbacks { + /** + * Implementation of function OQS_SHA3_shake128_x4. + */ + void (*SHA3_shake128_x4)( + uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_init. + */ + void (*SHA3_shake128_x4_inc_init)(OQS_SHA3_shake128_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_absorb. + */ + void (*SHA3_shake128_x4_inc_absorb)( + OQS_SHA3_shake128_x4_inc_ctx *state, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_finalize. + */ + void (*SHA3_shake128_x4_inc_finalize)(OQS_SHA3_shake128_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_squeeze. + */ + void (*SHA3_shake128_x4_inc_squeeze)( + uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + OQS_SHA3_shake128_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_ctx_release. + */ + void (*SHA3_shake128_x4_inc_ctx_release)(OQS_SHA3_shake128_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_ctx_clone. + */ + void (*SHA3_shake128_x4_inc_ctx_clone)( + OQS_SHA3_shake128_x4_inc_ctx *dest, + const OQS_SHA3_shake128_x4_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_shake128_x4_inc_ctx_reset. + */ + void (*SHA3_shake128_x4_inc_ctx_reset)(OQS_SHA3_shake128_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_x4. + */ + void (*SHA3_shake256_x4)( + uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_init. + */ + void (*SHA3_shake256_x4_inc_init)(OQS_SHA3_shake256_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_absorb. + */ + void (*SHA3_shake256_x4_inc_absorb)( + OQS_SHA3_shake256_x4_inc_ctx *state, + const uint8_t *in0, + const uint8_t *in1, + const uint8_t *in2, + const uint8_t *in3, + size_t inlen); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_finalize. + */ + void (*SHA3_shake256_x4_inc_finalize)(OQS_SHA3_shake256_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_squeeze. + */ + void (*SHA3_shake256_x4_inc_squeeze)( + uint8_t *out0, + uint8_t *out1, + uint8_t *out2, + uint8_t *out3, + size_t outlen, + OQS_SHA3_shake256_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_ctx_release. + */ + void (*SHA3_shake256_x4_inc_ctx_release)(OQS_SHA3_shake256_x4_inc_ctx *state); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_ctx_clone. + */ + void (*SHA3_shake256_x4_inc_ctx_clone)( + OQS_SHA3_shake256_x4_inc_ctx *dest, + const OQS_SHA3_shake256_x4_inc_ctx *src); + + /** + * Implementation of function OQS_SHA3_shake256_x4_inc_ctx_reset. + */ + void (*SHA3_shake256_x4_inc_ctx_reset)(OQS_SHA3_shake256_x4_inc_ctx *state); +}; + +/** + * Set callback functions for 4-parallel SHA3 operations. + * + * This function may be called before OQS_init to switch the + * cryptographic provider for 4-parallel SHA3 operations. If it is not + * called, the default provider determined at build time will be used. + * + * @param new_callbacks Callback functions defined in OQS_SHA3_x4_callbacks struct + */ +OQS_API void OQS_SHA3_x4_set_callbacks(struct OQS_SHA3_x4_callbacks *new_callbacks); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // OQS_SHA3X4_OPS_H diff --git a/src/oqs.h b/src/oqs.h index 6d1923c78b..d6833bcb70 100644 --- a/src/oqs.h +++ b/src/oqs.h @@ -18,5 +18,9 @@ #include #include #include +#include +#include +#include +#include #endif // OQS_H