From 7ec20facaff2e38b2cdb2f445bda1d5a237e1ea7 Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 4 Nov 2024 11:21:47 -0500 Subject: refactor: #9 Light cleanup & prep for release --- include/noscrypt.h | 2 +- src/nc-crypto.c | 1 - src/noscrypt.c | 3 +-- src/noscryptutil.c | 46 ++++++++++++++++++++++++++-------------------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/include/noscrypt.h b/include/noscrypt.h index 8ba71b0..758d942 100644 --- a/include/noscrypt.h +++ b/include/noscrypt.h @@ -147,7 +147,7 @@ extern "C" { * least, so I'm using unsgined integers. Sorry, not sorry. */ -/* A compressed resul/return value, negative values +/* A compressed result/return value, negative values are failure, 0 is success and positive values are defined by the operation. */ diff --git a/src/nc-crypto.c b/src/nc-crypto.c index 56bdf75..c78296a 100644 --- a/src/nc-crypto.c +++ b/src/nc-crypto.c @@ -101,7 +101,6 @@ _IMPLSTB cstatus_t _dummyAesFunc( * * https://sourceware.org/glibc/manual/2.39/html_mono/libc.html#Erasing-Sensitive-Data */ - #include extern void explicit_bzero(void* block, size_t len); #define _IMPL_SECURE_ZERO_MEMSET explicit_bzero #endif diff --git a/src/noscrypt.c b/src/noscrypt.c index fededaf..1e19337 100644 --- a/src/noscrypt.c +++ b/src/noscrypt.c @@ -216,7 +216,7 @@ static NCResult _computeSharedSecret( */ result = secp256k1_ecdh( ctx->secpCtx, - (uint8_t*)sharedPoint, + sharedPoint->value, &pubKey, sk->key, &_edhHashFuncInternal, @@ -876,7 +876,6 @@ NC_EXPORT NCResult NC_CC NCEncrypt( result = _encryptNip44Ex(ctx, &conversationKey, args->keyData, args); } - break; /* At the moment nip04 compatability is not supported */ diff --git a/src/noscryptutil.c b/src/noscryptutil.c index 333ef74..dc3c104 100644 --- a/src/noscryptutil.c +++ b/src/noscryptutil.c @@ -82,11 +82,15 @@ /* performs a log2 on integer types */ #define _math_int_log2(x) (uint32_t)log2((double)x) -#else +#elif defined(__has_builtin) /* + * Only avaialable with builtins + * * GCC/clang does not expose log2 so we can use the __builtin_clz * to find leading zeros of an integer and subtract that from 31 * (bit positions) for int32 + * + * This file is really only meant for non-embedded systems */ static _nc_fn_inline uint32_t _math_int_log2(uint32_t val) { @@ -94,48 +98,50 @@ return 31 - __builtin_clz(val); } +#else + #error "Utilities library is not supported on this platform. Must support GCC/Glang builtin functions" #endif /* Currently were on nip44 version 2 */ static const uint8_t Nip44VersionValue[1] = { 0x02u }; -struct cipher_buffer_state { - - cspan_t input; - span_t output; - - cspan_t actualOutput; -}; - struct nc_util_enc_struct { uint32_t _flags; NCEncryptionArgs encArgs; - struct cipher_buffer_state buffer; + struct cipher_buffer_state { + + cspan_t input; + span_t output; + + cspan_t actualOutput; + + } buffer; }; -static _nc_fn_inline span_t _ncUtilAllocSpan(uint32_t count, size_t size) +static _nc_fn_inline int _ncUtilAllocSpan(span_t* span, uint32_t count, size_t size) { - span_t span; #if SIZE_MAX < UINT32_MAX if (count > SIZE_MAX) { - return span; + /* Return empty span */ + ncSpanInit(&span, NULL, 0); + return 0; } #endif ncSpanInit( - &span, + span, _nc_mem_alloc((size_t)count, size), (uint32_t)count ); - return span; + return ncSpanIsValid(*span); } static _nc_fn_inline void _ncUtilZeroSpan(span_t span) @@ -365,7 +371,7 @@ static NCResult _nip44EncryptCompleteCore( * slice has debug guards to ensure output is large enough */ message = ncSpanSlice( - state->buffer.output, + state->buffer.output, 0, _calcNip44TotalOutSize(plainText.size) ); @@ -761,7 +767,7 @@ NC_EXPORT NCResult NC_CC NCUtilCipherInit( return E_CIPHER_BAD_INPUT_SIZE; } - /* Ensure the first byte is a valid version */ + /* Ensure the first byte is a valid nip44 version */ if (inputData[0] != Nip44VersionValue[0]) { return E_VERSION_NOT_SUPPORTED; @@ -811,6 +817,8 @@ NC_EXPORT NCResult NC_CC NCUtilCipherInit( /* * if the existing buffer is large enough to hold the new * data reuse it, otherwise free it and allocate a new buffer + * + * TODO: Consider re-alloc to resize */ if (outputSize <= ncSpanGetSize(encCtx->buffer.output)) @@ -826,9 +834,7 @@ NC_EXPORT NCResult NC_CC NCUtilCipherInit( } /* Alloc output buffer within the struct */ - encCtx->buffer.output = _ncUtilAllocSpan((uint32_t)outputSize, sizeof(uint8_t)); - - if (!ncSpanIsValid(encCtx->buffer.output)) + if (!_ncUtilAllocSpan(&encCtx->buffer.output, (uint32_t)outputSize, sizeof(uint8_t))) { return E_OUT_OF_MEMORY; } -- cgit