aboutsummaryrefslogtreecommitdiff
path: root/src/noscrypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/noscrypt.c')
-rw-r--r--src/noscrypt.c116
1 files changed, 61 insertions, 55 deletions
diff --git a/src/noscrypt.c b/src/noscrypt.c
index 15d8da8..fb6dd4f 100644
--- a/src/noscrypt.c
+++ b/src/noscrypt.c
@@ -30,6 +30,16 @@
#include <mbedtls/chacha20.h>
#include <mbedtls/sha256.h>
+/* Non win platforms may need an inline override */
+#if !defined(_NC_IS_WINDOWS) && !defined(inline)
+ #define inline __inline__
+#endif // !IS_WINDOWS
+
+//NULL
+#ifndef NULL
+ #define NULL ((void*)0)
+#endif // !NULL
+
#define CHACHA_NONCE_SIZE 12 //Size of 12 is set by the cipher spec
#define CHACHA_KEY_SIZE 32
#define HMAC_KEY_SIZE 32
@@ -47,14 +57,12 @@
* Validation macros
*/
-#ifdef NC_INPUT_VALIDATION_OFF
- #define CHECK_NULL_PTR(ptr) if(ptr == NULL) return E_NULL_PTR;
- #define CHECK_INVALID_ARG(x) if(x == NULL) return E_INVALID_ARG;
+#ifndef NC_INPUT_VALIDATION_OFF
+ #define CHECK_INVALID_ARG(x, argPos) if(x == NULL) return NCResultWithArgPosition(E_INVALID_ARG, argPos);
#define CHECK_NULL_ARG(x, argPos) if(x == NULL) return NCResultWithArgPosition(E_NULL_PTR, argPos);
#define CHECK_ARG_RANGE(x, min, max, argPos) if(x < min || x > max) return NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, argPos);
#else
//empty macros
- #define CHECK_NULL_PTR(ptr)
#define CHECK_INVALID_ARG(x)
#define CHECK_NULL_ARG(x, argPos)
#define CHECK_ARG_RANGE(x, min, max, argPos)
@@ -461,8 +469,8 @@ NC_EXPORT NCResult NC_CC NCInitContext(
const uint8_t entropy[32]
)
{
- CHECK_NULL_PTR(ctx)
- CHECK_NULL_PTR(entropy)
+ CHECK_NULL_ARG(ctx, 0)
+ CHECK_NULL_ARG(entropy, 1)
ctx->secpCtx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
@@ -475,9 +483,9 @@ NC_EXPORT NCResult NC_CC NCReInitContext(
const uint8_t entropy[32]
)
{
- CHECK_NULL_PTR(ctx)
- CHECK_INVALID_ARG(ctx->secpCtx)
- CHECK_INVALID_ARG(entropy)
+ CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
+ CHECK_NULL_ARG(entropy, 1)
//Only randomize again
return secp256k1_context_randomize(ctx->secpCtx, entropy) ? NC_SUCCESS : E_INVALID_ARG;
@@ -486,7 +494,7 @@ NC_EXPORT NCResult NC_CC NCReInitContext(
NC_EXPORT NCResult NC_CC NCDestroyContext(NCContext* ctx)
{
CHECK_NULL_ARG(ctx, 0);
- CHECK_INVALID_ARG(ctx->secpCtx);
+ CHECK_INVALID_ARG(ctx->secpCtx, 0);
//Destroy secp256k1 context
secp256k1_context_destroy(ctx->secpCtx);
@@ -509,9 +517,9 @@ NC_EXPORT NCResult NC_CC NCGetPublicKey(
secp256k1_xonly_pubkey xonly;
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sk, 1)
CHECK_NULL_ARG(pk, 2)
- CHECK_INVALID_ARG(ctx->secpCtx)
if (secp256k1_keypair_create(ctx->secpCtx, &keyPair, sk->key) != 1)
{
@@ -538,9 +546,9 @@ NC_EXPORT NCResult NC_CC NCValidateSecretKey(
const NCSecretKey* sk
)
{
- CHECK_NULL_PTR(ctx)
- CHECK_NULL_PTR(sk)
- CHECK_INVALID_ARG(ctx->secpCtx)
+ CHECK_NULL_ARG(ctx, 0)
+ CHECK_NULL_ARG(sk, 1)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
//Validate the secret key
return secp256k1_ec_seckey_verify(ctx->secpCtx, sk->key);
@@ -562,11 +570,11 @@ NC_EXPORT NCResult NC_CC NCSignDigest(
//Validate arguments
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sk, 1)
CHECK_NULL_ARG(random32, 2)
CHECK_NULL_ARG(digest32, 3)
CHECK_NULL_ARG(sig64, 4)
- CHECK_INVALID_ARG(ctx->secpCtx)
//Generate the keypair
if (secp256k1_keypair_create(ctx->secpCtx, &keyPair, sk->key) != 1)
@@ -603,8 +611,13 @@ NC_EXPORT NCResult NC_CC NCSignData(
{
uint8_t digest[32];
- CHECK_NULL_ARG(data, 2)
- CHECK_ARG_RANGE(dataSize, 1, UINT32_MAX, 3)
+ //Double check is required because arg position differs
+ CHECK_NULL_ARG(ctx, 0)
+ CHECK_NULL_ARG(sk, 1)
+ CHECK_NULL_ARG(random32, 2)
+ CHECK_NULL_ARG(data, 3)
+ CHECK_ARG_RANGE(dataSize, 1, UINT32_MAX, 4)
+ CHECK_NULL_ARG(sig64, 5)
//Compute sha256 of the data before signing
if(_computeSha256Digest(data, dataSize, digest) != 0)
@@ -624,15 +637,13 @@ NC_EXPORT NCResult NC_CC NCVerifyDigest(
)
{
int result;
- secp256k1_xonly_pubkey xonly;
-
- DEBUG_ASSERT(&xonly != NULL)
+ secp256k1_xonly_pubkey xonly;
CHECK_NULL_ARG(ctx, 0)
- CHECK_NULL_ARG(sig64, 1)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
+ CHECK_NULL_ARG(pk, 1)
CHECK_NULL_ARG(digest32, 2)
- CHECK_NULL_ARG(pk, 3)
- CHECK_INVALID_ARG(ctx->secpCtx)
+ CHECK_NULL_ARG(sig64, 3)
//recover the x-only key from a compressed public key
if(_convertToXonly(ctx, pk, &xonly) != 1)
@@ -654,13 +665,16 @@ NC_EXPORT NCResult NC_CC NCVerifyData(
const NCPublicKey* pk,
const uint8_t* data,
const size_t dataSize,
- uint8_t sig64[64]
+ const uint8_t sig64[64]
)
{
uint8_t digest[32];
+ CHECK_NULL_ARG(ctx, 0)
+ CHECK_NULL_ARG(pk, 1)
CHECK_NULL_ARG(data, 2)
CHECK_ARG_RANGE(dataSize, 1, UINT32_MAX, 3)
+ CHECK_NULL_ARG(sig64, 4)
//Compute sha256 of the data before verifying
if (_computeSha256Digest(data, dataSize, digest) != 0)
@@ -681,10 +695,10 @@ NC_EXPORT NCResult NC_CC NCGetSharedSecret(
)
{
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sk, 1)
CHECK_NULL_ARG(otherPk, 2)
- CHECK_NULL_ARG(sharedPoint, 3)
- CHECK_INVALID_ARG(ctx->secpCtx)
+ CHECK_NULL_ARG(sharedPoint, 3)
return _computeSharedSecret(
ctx,
@@ -701,9 +715,9 @@ NC_EXPORT NCResult NC_CC NCGetConversationKeyEx(
)
{
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sharedPoint, 1)
- CHECK_NULL_ARG(conversationKey, 2)
- CHECK_INVALID_ARG(ctx->secpCtx)
+ CHECK_NULL_ARG(conversationKey, 2)
//Cast the shared point to the shared secret type
return _computeConversationKey(
@@ -726,10 +740,10 @@ NC_EXPORT NCResult NC_CC NCGetConversationKey(
const mbedtls_md_info_t* mdInfo;
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sk, 1)
CHECK_NULL_ARG(pk, 2)
- CHECK_NULL_ARG(conversationKey, 3)
- CHECK_INVALID_ARG(ctx->secpCtx)
+ CHECK_NULL_ARG(conversationKey, 3)
mdInfo = _getSha256MdInfo();
@@ -760,16 +774,14 @@ NC_EXPORT NCResult NC_CC NCEncryptEx(
)
{
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(conversationKey, 1)
CHECK_NULL_ARG(args, 2)
- //Validate the context
- CHECK_INVALID_ARG(ctx->secpCtx)
-
//Validte ciphertext/plaintext
- CHECK_INVALID_ARG(args->inputData)
- CHECK_INVALID_ARG(args->outputData)
- CHECK_ARG_RANGE(args->dataSize, NIP44_MIN_ENC_MESSAGE_SIZE, NIP44_MAX_ENC_MESSAGE_SIZE, 3)
+ CHECK_INVALID_ARG(args->inputData, 2)
+ CHECK_INVALID_ARG(args->outputData, 2)
+ CHECK_ARG_RANGE(args->dataSize, NIP44_MIN_ENC_MESSAGE_SIZE, NIP44_MAX_ENC_MESSAGE_SIZE, 2)
return _encryptEx(
ctx,
@@ -789,19 +801,17 @@ NC_EXPORT NCResult NC_CC NCEncrypt(
NCResult result;
const mbedtls_md_info_t* mdInfo;
struct shared_secret sharedSecret;
- struct conversation_key ck;
+ struct conversation_key conversationKey;
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sk, 1)
CHECK_NULL_ARG(pk, 2)
CHECK_NULL_ARG(args, 3)
- //Validate the context
- CHECK_INVALID_ARG(ctx->secpCtx)
-
//Validate input/output data
- CHECK_INVALID_ARG(args->inputData)
- CHECK_INVALID_ARG(args->outputData)
+ CHECK_INVALID_ARG(args->inputData, 3)
+ CHECK_INVALID_ARG(args->outputData, 3)
CHECK_ARG_RANGE(args->dataSize, NIP44_MIN_ENC_MESSAGE_SIZE, NIP44_MAX_ENC_MESSAGE_SIZE, 3)
mdInfo = _getSha256MdInfo();
@@ -813,17 +823,17 @@ NC_EXPORT NCResult NC_CC NCEncrypt(
}
//Compute the conversation key from secret and pubkic keys
- if ((result = _computeConversationKey(ctx, mdInfo, &sharedSecret, &ck)) != NC_SUCCESS)
+ if ((result = _computeConversationKey(ctx, mdInfo, &sharedSecret, &conversationKey)) != NC_SUCCESS)
{
goto Cleanup;
}
- result = _encryptEx(ctx, mdInfo, &ck, args);
+ result = _encryptEx(ctx, mdInfo, &conversationKey, args);
Cleanup:
//Clean up sensitive data
ZERO_FILL(&sharedSecret, sizeof(sharedSecret));
- ZERO_FILL(&ck, sizeof(ck));
+ ZERO_FILL(&conversationKey, sizeof(conversationKey));
return result;
}
@@ -836,15 +846,13 @@ NC_EXPORT NCResult NC_CC NCDecryptEx(
)
{
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(conversationKey, 1)
CHECK_NULL_ARG(args, 2)
- //Validate the context
- CHECK_INVALID_ARG(ctx->secpCtx)
-
//Validte ciphertext/plaintext
- CHECK_INVALID_ARG(args->inputData)
- CHECK_INVALID_ARG(args->outputData)
+ CHECK_INVALID_ARG(args->inputData, 2)
+ CHECK_INVALID_ARG(args->outputData, 2)
CHECK_ARG_RANGE(args->dataSize, NIP44_MIN_DEC_MESSAGE_SIZE, NIP44_MAX_DEC_MESSAGE_SIZE, 3)
return _decryptEx(
@@ -869,16 +877,14 @@ NC_EXPORT NCResult NC_CC NCDecrypt(
const mbedtls_md_info_t* mdInfo;
CHECK_NULL_ARG(ctx, 0)
+ CHECK_INVALID_ARG(ctx->secpCtx, 0)
CHECK_NULL_ARG(sk, 1)
CHECK_NULL_ARG(pk, 2)
CHECK_NULL_ARG(args, 3)
- //Validate the context
- CHECK_INVALID_ARG(ctx->secpCtx)
-
//Validte ciphertext/plaintext
- CHECK_INVALID_ARG(args->inputData)
- CHECK_INVALID_ARG(args->outputData)
+ CHECK_INVALID_ARG(args->inputData, 3)
+ CHECK_INVALID_ARG(args->outputData, 3)
CHECK_ARG_RANGE(args->dataSize, NIP44_MIN_DEC_MESSAGE_SIZE, NIP44_MAX_DEC_MESSAGE_SIZE, 3)
mdInfo = _getSha256MdInfo();