aboutsummaryrefslogtreecommitdiff
path: root/include/noscryptutil.h
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-07-11 21:39:39 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-07-11 21:39:39 -0400
commit8df8c5aed4ac626171b451b5422c3b207e88000b (patch)
tree01615787210e0f54027296b4bb6db689134ff2e2 /include/noscryptutil.h
parent23fe6e8c8596333c2183f0f4389817087442c551 (diff)
feat: Update sidecar utils library
Diffstat (limited to 'include/noscryptutil.h')
-rw-r--r--include/noscryptutil.h125
1 files changed, 107 insertions, 18 deletions
diff --git a/include/noscryptutil.h b/include/noscryptutil.h
index 1a98698..7db5738 100644
--- a/include/noscryptutil.h
+++ b/include/noscryptutil.h
@@ -19,9 +19,8 @@
*/
/*
-* noscrypt is a an open-source, strict C89 library that performs the basic
-* cryptographic operations found in the Nostr protocol. It is designed to be
-* portable and easy to use in any C89 compatible environment. It is also designed
+* This header includes some optional high-level nostr crypto utility functions
+* for much easer app development.
*/
#pragma once
@@ -33,42 +32,132 @@
extern "C" {
#endif
-#include <stdlib.h>
#include "noscrypt.h"
#define E_OUT_OF_MEMORY -10
-typedef struct nc_util_enc_struct NCUtilEncryptionContext;
+#define NC_UTIL_CIPHER_MODE_ENCRYPT 0x00ui32
+#define NC_UTIL_CIPHER_MODE_DECRYPT 0x01ui32
+#define NC_UTIL_CIPHER_ZERO_ON_FREE 0x02ui32
-NC_EXPORT NCResult NC_CC NCUtilGetEncryptionPaddedSize(uint32_t encVersion, int32_t plaintextSize);
+/*
+* The encryption context structure. This structure is used to store the state
+* of the encryption operation. The structure is opaque and should not be accessed
+* directly.
+*/
+typedef struct nc_util_enc_struct NCUtilCipherContext;
+
+/*
+* Gets the size of the padded buffer required for an encryption operation.
+* @param encVersion The encryption specification version to use
+* @param plaintextSize The size of the plaintext buffer in bytes
+* @return The size of the padded buffer in bytes
+*/
+NC_EXPORT NCResult NC_CC NCUtilGetEncryptionPaddedSize(uint32_t encVersion, uint32_t plaintextSize);
-NC_EXPORT NCResult NC_CC NCUtilGetEncryptionBufferSize(uint32_t encVersion, int32_t plaintextSize);
+/*
+* Gets the size of the payload buffer required for an encryption operation.
+* @param encVersion The encryption specification version to use
+* @param plaintextSize The size of the plaintext buffer in bytes
+* @return The size of the payload buffer in bytes
+* @note The payload buffer is the final buffer to be sent to a nostr user. For nip04 this
+* is a raw AES message, for nip44 this is a mucher lager buffer. See the nostr specifications
+* for more information.
+*/
+NC_EXPORT NCResult NC_CC NCUtilGetEncryptionBufferSize(uint32_t encVersion, uint32_t plaintextSize);
-NC_EXPORT NCUtilEncryptionContext* NC_CC NCUtilAllocEncryptionContext(uint32_t encVersion);
+/*
+* 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
+* @param flags The flags to set on the encryption context
+* @return A valid pointer to an encryption context or NULL if the operation failed
+*/
+NC_EXPORT NCUtilCipherContext* NC_CC NCUtilCipherAlloc(uint32_t encVersion, uint32_t flags);
-NC_EXPORT NCResult NC_CC NCUtilInitEncryptionContext(
- NCUtilEncryptionContext* encCtx,
- const uint8_t* plainText,
- uint32_t plainTextSize
+/*
+* Initializes the encryption context with the input data and size. This function will
+ internally allocate a the required output buffer for the ciper operation. You may only call
+ this function once.
+* @param encCtx A valid pointer to an allocated encryption context
+* @param inputData A pointer to the input data for the ciper
+* @param inputSize The size of the input data
+* @return NC_SUCCESS if the operation was successful, otherwise an error code. Use NCParseErrorCode to
+the error code and positional argument that caused the error
+*/
+NC_EXPORT NCResult NC_CC NCUtilCipherInit(
+ NCUtilCipherContext* encCtx,
+ const uint8_t* inputData,
+ uint32_t inputSize
);
-NC_EXPORT void NC_CC NCUtilFreeEncryptionContext(NCUtilEncryptionContext* encCtx);
+/*
+* Frees the encryption context and clears the memory if the NC_UTIL_CIPHER_ZERO_ON_FREE
+* flag is set.
+* @param encCtx A valid pointer to an allocated encryption context to free
+*/
+NC_EXPORT void NC_CC NCUtilCipherFree(NCUtilCipherContext* encCtx);
-NC_EXPORT NCResult NC_CC NCUtilGetEncryptedSize(const NCUtilEncryptionContext* encCtx);
+/*
+* Gets the output size of the encryption context. This function will return the size of
+* the output buffer that will be written to when calling NCUtilCipherReadOutput.
+* @param encCtx A valid pointer to an allocated encryption context
+* @return The size of the output buffer in bytes
+*/
+NC_EXPORT NCResult NC_CC NCUtilCipherGetOutputSize(const NCUtilCipherContext* encCtx);
-NC_EXPORT NCResult NC_CC NCUtilReadEncryptedData(
- const NCUtilEncryptionContext* encCtx,
+/*
+* Reads the output buffer from the encryption context. This function will copy the output
+* buffer to the output buffer provided. The output buffer must be at least the size of the
+* output buffer returned by NCUtilCipherGetOutputSize.
+* @param encCtx A valid pointer to an initialized encryption context
+* @param output A pointer to the output buffer to copy the output to
+* @param outputSize The size of the output buffer in bytes
+* @returns The number of bytes written to the output buffer or an error code. Use NCParseErrorCode
+* to get the error code and positional argument that caused the error
+*/
+NC_EXPORT NCResult NC_CC NCUtilCipherReadOutput(
+ const NCUtilCipherContext* encCtx,
uint8_t* output,
uint32_t outputSize
);
-NC_EXPORT NCResult NCUtilSetEncryptionProperty(
- NCUtilEncryptionContext* ctx,
+/*
+* Sets a property on the encryption context. Equivalent to calling NCSetEncryptionPropertyEx
+* @param ctx A valid pointer to an encryption context
+* @param property The property to set
+* @param value A pointer to the value to set
+* @param valueLen The length of the value
+* @return NC_SUCCESS if the operation was successful, otherwise an error code. Use NCParseErrorCode to
+* get the error code and positional argument that caused the error
+*/
+NC_EXPORT NCResult NCUtilCipherSetProperty(
+ NCUtilCipherContext* ctx,
uint32_t property,
uint8_t* value,
uint32_t valueLen
);
+/*
+* Performs the desired ciper option once. This may either cause an encryption
+* or decryption operation to be performed. Regardless of the operation, input data
+* is consumed and output data is produced.
+* @param encCtx A valid pointer to an initialized encryption context
+* @param libContext A valid pointer to an NCContext structure
+* @param sk A valid pointer to the sender's private key
+* @param pk A valid pointer to the receivers public key
+* @return NC_SUCCESS if the operation was successful, otherwise an error code. Use NCParseErrorCode to
+* get the error code and positional argument that caused the error.
+* @note This function should only be called once. However it is indempotent and deterministic
+* so the exact same operation should happen if called again.
+*/
+NC_EXPORT NCResult NC_CC NCUtilCipherUpdate(
+ const NCUtilCipherContext* encCtx,
+ const NCContext* libContext,
+ const NCSecretKey* sk,
+ const NCPublicKey* pk
+);
+
#ifdef __cplusplus
}
#endif