Skip to content

Commit

Permalink
Crypto: Config options and macros to enable/disable logging inside CA…
Browse files Browse the repository at this point in the history
…PIs (#1059)

-- Cmake argument ALCP_ENABLE_DEBUG_LOGGING to toggle debug prints from capi level
-- Defined debug log macro and log levels

Signed-off-by: Pranoy Jayaraj <[email protected]>
  • Loading branch information
pjayaraj-amd authored and GitHub Enterprise committed Nov 8, 2024
1 parent dea3fef commit 72ad402
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cmake/AlcpConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ FUNCTION(GEN_CONF)
# Set ALCP Release Version String
STRING(TIMESTAMP ALCP_RELEASE_VERSION_STRING "AOCL-Crypto ${AOCL_RELEASE_VERSION} Build %Y%m%d")

OPTION(ALCP_ENABLE_DEBUG_LOGGING "ENABLE DEBUG PRINTS INSIDE ALCP" OFF)
if(ALCP_ENABLE_DEBUG_LOGGING)
SET(ALCP_ENABLE_DEBUG_LOGGING ON)
endif()

# Set Build OS
IF(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
SET(ALCP_BUILD_OS_LINUX ON)
Expand Down
2 changes: 2 additions & 0 deletions include/alcp/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@
#cmakedefine ALCP_CPUID_FORCE_ZEN4
#cmakedefine ALCP_CPUID_FORCE_ZEN5

#cmakedefine ALCP_ENABLE_DEBUG_LOGGING

#endif /* _INCLUDE_CONFIG_H */
19 changes: 18 additions & 1 deletion lib/capi/c_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ Uint64
alcp_cipher_context_size()
{
Uint64 size = sizeof(Context);
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "CtxSize %6ld", size);
#endif
return size;
}

bool
validateKeys(const Uint8* tweakKey, const Uint8* encKey, Uint32 len)
{

for (Uint32 i = 0; i < len / 8; i++) {
if (tweakKey[i] != encKey[i]) {
return false;
Expand Down Expand Up @@ -95,6 +97,9 @@ alcp_cipher_request(const alc_cipher_mode_t mode,
const Uint64 keyLen,
alc_cipher_handle_p pCipherHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "KeyLen %6ld", keyLen);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand Down Expand Up @@ -125,6 +130,9 @@ alcp_cipher_encrypt(const alc_cipher_handle_p pCipherHandle,
Uint8* pCipherText,
Uint64 len)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "PTLen %6ld", len);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -148,6 +156,9 @@ alcp_cipher_decrypt(const alc_cipher_handle_p pCipherHandle,
Uint8* pPlainText,
Uint64 len)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "CTLen %6ld", len);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -172,6 +183,9 @@ alcp_cipher_init(const alc_cipher_handle_p pCipherHandle,
const Uint8* pIv,
Uint64 ivLen)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "KeyLen %6ld,IVLen %6ld", keyLen, ivLen);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -197,6 +211,9 @@ alcp_cipher_init(const alc_cipher_handle_p pCipherHandle,
void
alcp_cipher_finish(const alc_cipher_handle_p pCipherHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
if (pCipherHandle == nullptr || pCipherHandle->ch_context == nullptr)
return;

Expand Down
30 changes: 30 additions & 0 deletions lib/capi/c_cipher_aead.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Uint64
alcp_cipher_aead_context_size()
{
Uint64 size = sizeof(Context);
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "CtxSize %6ld", size);
#endif
return size;
}

Expand Down Expand Up @@ -79,6 +82,9 @@ alcp_cipher_aead_request(const alc_cipher_mode_t mode,
const Uint64 keyLen,
alc_cipher_handle_p pCipherHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "KeyLen %6ld", keyLen);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand Down Expand Up @@ -108,6 +114,9 @@ alcp_cipher_aead_encrypt(const alc_cipher_handle_p pCipherHandle,
Uint8* pOutput,
Uint64 len)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "EncLen %6ld", len);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -134,6 +143,9 @@ alcp_cipher_aead_decrypt(const alc_cipher_handle_p pCipherHandle,
Uint8* pOutput,
Uint64 len)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "DecLen %6ld", len);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand Down Expand Up @@ -162,6 +174,9 @@ alcp_cipher_aead_init(const alc_cipher_handle_p pCipherHandle,
const Uint8* pIv,
Uint64 ivLen)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "KeyLen %6ld,IVLen %6ld", keyLen, ivLen);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -188,6 +203,9 @@ alcp_cipher_aead_set_aad(const alc_cipher_handle_p pCipherHandle,
const Uint8* pInput,
Uint64 aadLen)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "ADLen %6ld", aadLen);
#endif
alc_error_t err = ALC_ERROR_NONE;
if (aadLen == 0) {
return err;
Expand Down Expand Up @@ -215,6 +233,9 @@ alcp_cipher_aead_get_tag(const alc_cipher_handle_p pCipherHandle,
Uint8* pOutput,
Uint64 tagLen)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "TagLen %6ld", tagLen);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -241,6 +262,9 @@ alc_error_t
alcp_cipher_aead_set_tag_length(const alc_cipher_handle_p pCipherHandle,
Uint64 tagLen)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "TagLen %ld", tagLen);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -265,6 +289,9 @@ alc_error_t
alcp_cipher_aead_set_ccm_plaintext_length(
const alc_cipher_handle_p pCipherHandle, Uint64 plaintextLength)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "PTLen %6ld", plaintextLength);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pCipherHandle, err);
Expand All @@ -280,6 +307,9 @@ alcp_cipher_aead_set_ccm_plaintext_length(
void
alcp_cipher_aead_finish(const alc_cipher_handle_p pCipherHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
if (nullptr == pCipherHandle)
return;
if (pCipherHandle->ch_context == nullptr) {
Expand Down
26 changes: 25 additions & 1 deletion lib/capi/c_digest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "alcp/digest.h"

#include "alcp/alcp.hh"
#include "alcp/capi/defs.hh"
#include "alcp/capi/digest/builder.hh"
#include "alcp/capi/digest/ctx.hh"

Expand All @@ -40,12 +41,18 @@ Uint64
alcp_digest_context_size()
{
Uint64 size = sizeof(digest::Context);
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "CtxSize %6ld", size);
#endif
return size;
}

alc_error_t
alcp_digest_request(alc_digest_mode_t mode, alc_digest_handle_p pDigestHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pDigestHandle, err);
Expand All @@ -63,6 +70,9 @@ alcp_digest_request(alc_digest_mode_t mode, alc_digest_handle_p pDigestHandle)
alc_error_t
alcp_digest_init(alc_digest_handle_p pDigestHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;
ALCP_BAD_PTR_ERR_RET(pDigestHandle, err);

Expand All @@ -79,6 +89,9 @@ alcp_digest_update(const alc_digest_handle_p pDigestHandle,
const Uint8* pMsgBuf,
Uint64 size)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "DigestSize %6ld", size);
#endif
alc_error_t err = ALC_ERROR_NONE;
ALCP_BAD_PTR_ERR_RET(pDigestHandle, err);

Expand All @@ -97,6 +110,9 @@ alcp_digest_finalize(const alc_digest_handle_p pDigestHandle,
Uint8* buf,
Uint64 size)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "DigestSize %6ld", size);
#endif
alc_error_t err;
ALCP_BAD_PTR_ERR_RET(pDigestHandle, err);

Expand All @@ -112,7 +128,9 @@ alcp_digest_finalize(const alc_digest_handle_p pDigestHandle,
void
alcp_digest_finish(const alc_digest_handle_p pDigestHandle)
{

#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
if (pDigestHandle && pDigestHandle->context) {
auto ctx = static_cast<digest::Context*>(pDigestHandle->context);
if (ctx->m_digest) {
Expand All @@ -129,6 +147,9 @@ alcp_digest_shake_squeeze(const alc_digest_handle_p pDigestHandle,
Uint64 size)

{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "DigestSize %6ld", size);
#endif
ALCP_BAD_PTR_ERR_RET(pDigestHandle, err);

auto ctx = static_cast<digest::Context*>(pDigestHandle->context);
Expand All @@ -150,6 +171,9 @@ alc_error_t
alcp_digest_context_copy(const alc_digest_handle_p pSrcHandle,
const alc_digest_handle_p pDestHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;
ALCP_BAD_PTR_ERR_RET(pSrcHandle, err);
ALCP_BAD_PTR_ERR_RET(pDestHandle, err);
Expand Down
24 changes: 24 additions & 0 deletions lib/capi/c_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ Uint64
alcp_ec_context_size(const alc_ec_info_p pEcInfo)
{
Uint64 size = sizeof(ec::Context) + ec::EcBuilder::getSize(*pEcInfo);
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_DBG, "CtxSize %6ld", size);
#endif
return size;
}

alc_error_t
alcp_ec_supported(const alc_ec_info_p pEcInfo)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;

if (pEcInfo->ecCurveId != ALCP_EC_CURVE25519) {
Expand All @@ -68,6 +74,9 @@ alcp_ec_supported(const alc_ec_info_p pEcInfo)
alc_error_t
alcp_ec_request(const alc_ec_info_p pEcInfo, alc_ec_handle_p pEcHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;

ALCP_BAD_PTR_ERR_RET(pEcHandle, err);
Expand All @@ -87,6 +96,9 @@ alc_error_t
alcp_ec_set_privatekey(const alc_ec_handle_p pEcHandle,
const Uint8* pPrivateKey)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;
ALCP_BAD_PTR_ERR_RET(pEcHandle, err);
ALCP_BAD_PTR_ERR_RET(pEcHandle->context, err);
Expand All @@ -105,6 +117,9 @@ alcp_ec_get_publickey(const alc_ec_handle_p pEcHandle,
Uint8* pPublicKey,
const Uint8* pPrivKey)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;
ALCP_BAD_PTR_ERR_RET(pEcHandle, err);
ALCP_BAD_PTR_ERR_RET(pEcHandle->context, err);
Expand All @@ -126,6 +141,9 @@ alcp_ec_get_secretkey(const alc_ec_handle_p pEcHandle,
const Uint8* pPublicKey,
Uint64* pKeyLength)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
alc_error_t err = ALC_ERROR_NONE;
ALCP_BAD_PTR_ERR_RET(pEcHandle, err);
ALCP_BAD_PTR_ERR_RET(pEcHandle->context, err);
Expand All @@ -144,6 +162,9 @@ alcp_ec_get_secretkey(const alc_ec_handle_p pEcHandle,
void
alcp_ec_finish(const alc_ec_handle_p pEcHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
auto ctx = static_cast<ec::Context*>(pEcHandle->context);

/* TODO: fix the argument */
Expand All @@ -157,6 +178,9 @@ alcp_ec_finish(const alc_ec_handle_p pEcHandle)
void
alcp_ec_reset(const alc_ec_handle_p pEcHandle)
{
#ifdef ALCP_ENABLE_DEBUG_LOGGING
ALCP_DEBUG_LOG(LOG_INFO);
#endif
auto ctx = static_cast<ec::Context*>(pEcHandle->context);
ctx->status = ctx->reset(ctx->m_ec);

Expand Down
Loading

0 comments on commit 72ad402

Please sign in to comment.