aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md4
-rw-r--r--include/noscrypt.h4
-rw-r--r--include/noscryptutil.h13
-rw-r--r--src/noscryptutil.c13
-rw-r--r--tests/test.c14
5 files changed, 43 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2414a2b..e519972 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- C# .NET 8.0 library wrapper for noscrypt
- NIP44 vector testing for encryption
+- `NCUtilContextAlloc()` and `NCUtilContextFree()` utilities for dynamic library context allocation
+
+### Changed
+- Public and Secret key structure definition names have been correctly namespaced __(no breaking changes)__
## [0.1.4]
diff --git a/include/noscrypt.h b/include/noscrypt.h
index 3702555..8ba71b0 100644
--- a/include/noscrypt.h
+++ b/include/noscrypt.h
@@ -156,7 +156,7 @@ typedef int64_t NCResult;
/*
An secp256k1 secret key (aka private key buffer)
*/
-typedef struct secret_key_struct {
+typedef struct nc_secret_key_struct {
uint8_t key[NC_SEC_KEY_SIZE];
@@ -165,7 +165,7 @@ typedef struct secret_key_struct {
/*
An x-only secp256k1 public key
*/
-typedef struct xonly_pubkey_struct {
+typedef struct nc_xonly_pubkey_struct {
uint8_t key[NC_PUBKEY_SIZE];
diff --git a/include/noscryptutil.h b/include/noscryptutil.h
index 63e08f8..ea5dad0 100644
--- a/include/noscryptutil.h
+++ b/include/noscryptutil.h
@@ -78,6 +78,19 @@ NC_EXPORT NCResult NC_CC NCUtilGetEncryptionPaddedSize(uint32_t encVersion, uint
NC_EXPORT NCResult NC_CC NCUtilGetEncryptionBufferSize(uint32_t encVersion, uint32_t plaintextSize);
/*
+* Allocates a new library context structure dynamically on the heap and returns a pointer to
+* it. The library context must be freed with NCUtilContextFree when it is no longer needed.
+* @return A valid pointer to a new library context or NULL if the operation failed
+*/
+NC_EXPORT NCContext* NC_CC NCUtilContextAlloc(void);
+
+/*
+* Frees the library context structure and clears the memory it points to.
+* @param ctx A valid pointer to a library context memory
+*/
+NC_EXPORT void NC_CC NCUtilContextFree(NCContext* ctx);
+
+/*
* Allocates a new encryption context and sets the encryption version and flags. The encryption context
* must be freed with NCUtilCipherFree when it is no longer needed.
* @param encVersion The encryption specification version to use
diff --git a/src/noscryptutil.c b/src/noscryptutil.c
index 115ad04..333ef74 100644
--- a/src/noscryptutil.c
+++ b/src/noscryptutil.c
@@ -672,6 +672,19 @@ NC_EXPORT NCResult NC_CC NCUtilGetEncryptionBufferSize(uint32_t encVersion, uint
}
+NC_EXPORT NCContext* NC_CC NCUtilContextAlloc(void)
+{
+ /* Dynamically allocate context aligned and zeroed */
+ return (NCContext*)_nc_mem_alloc(1, NCGetContextStructSize());
+}
+
+
+NC_EXPORT void NC_CC NCUtilContextFree(NCContext* ctx)
+{
+ _nc_mem_free(ctx);
+}
+
+
NC_EXPORT NCUtilCipherContext* NC_CC NCUtilCipherAlloc(uint32_t encVersion, uint32_t flags)
{
NCUtilCipherContext* encCtx;
diff --git a/tests/test.c b/tests/test.c
index e8b064b..20a0e5d 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -66,6 +66,10 @@
#include "hex.h"
+#ifdef NC_ENABLE_UTILS
+ #include <noscryptutil.h>
+#endif
+
/*Pre-computed constants for argument errors */
#define ARG_ERROR_POS_0 E_NULL_PTR
#define ARG_ERROR(pos) NCResultWithArgPosition(E_NULL_PTR, pos)
@@ -365,7 +369,13 @@ static int TestPublicApiArgumentValidation()
* Alloc context structure on the heap before use.
* THIS WILL LEAK IN THE CURRENT CONFIG ALWAYS FREE UNDER NORMAL CONDITIONS
*/
+
+#ifdef NOSCRYPTUTIL_H
+ ctx = NCUtilContextAlloc();
+#else
ctx = (NCContext*)malloc(NCGetContextStructSize());
+#endif
+
TASSERT(ctx != NULL)
/*Test null context*/
@@ -608,9 +618,7 @@ static int TestCorrectEncryption(const NCContext* context)
return 0;
}
-#ifdef NC_ENABLE_UTILS
-
-#include <noscryptutil.h>
+#ifdef NOSCRYPTUTIL_H
/* Padding tests taken from the nip44 repo vectors.json file */
static const uint32_t _padTestActual[24] = { 16, 32, 33, 37, 45, 49, 64, 65, 100, 111, 200, 250, 320, 383, 384, 400, 500, 512, 515, 700, 800, 900, 1020, 65536 };