Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose callback API for replacing low-level cryptographic primitives #1832

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion docs/.Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
82 changes: 1 addition & 81 deletions src/common/aes/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <stdint.h>
#include <stdlib.h>

#include <oqs/common.h>
#include <oqs/aes_ops.h>

#if defined(__cplusplus)
extern "C" {
Expand Down Expand Up @@ -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
Expand Down
104 changes: 104 additions & 0 deletions src/common/aes/aes_ops.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <stdlib.h>

#include <oqs/common.h>

#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
Loading
Loading