aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-11-04 11:21:47 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-11-04 11:21:47 -0500
commit7ec20facaff2e38b2cdb2f445bda1d5a237e1ea7 (patch)
tree09a36061e8303a50a6f3609153685eaab37744ec /src
parente0d30c1d8f407bfef05a9cc36398bb0894a96c39 (diff)
refactor: #9 Light cleanup & prep for release
Diffstat (limited to 'src')
-rw-r--r--src/nc-crypto.c1
-rw-r--r--src/noscrypt.c3
-rw-r--r--src/noscryptutil.c46
3 files changed, 27 insertions, 23 deletions
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 <string.h>
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;
}