From 120022aa349f5e4cac28da74d568373c49245884 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 4 Feb 2024 21:09:52 -0500 Subject: merge latest patches and tests --- src/noscrypt.h | 83 ++++++++++++++++++---------------------------------------- 1 file changed, 26 insertions(+), 57 deletions(-) (limited to 'src/noscrypt.h') diff --git a/src/noscrypt.h b/src/noscrypt.h index 8a43743..f6bfe76 100644 --- a/src/noscrypt.h +++ b/src/noscrypt.h @@ -1,22 +1,21 @@ /* * Copyright (c) 2024 Vaughn Nugent * -* Library: noscrypt * Package: noscrypt * File: noscrypt.h -* -* noscrypt is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published -* by the Free Software Foundation, either version 2 of the License, -* or (at your option) any later version. * -* noscrypt is distributed in the hope that it will be useful, +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public License +* as published by the Free Software Foundation; either version 2.1 +* of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. +* Lesser General Public License for more details. * -* You should have received a copy of the GNU General Public License -* along with noscrypt. If not, see http://www.gnu.org/licenses/. +* You should have received a copy of the GNU Lesser General Public License +* along with NativeHeapApi. If not, see http://www.gnu.org/licenses/. */ /* @@ -34,12 +33,12 @@ #include #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) - #define IS_WINDOWS + #define _NC_IS_WINDOWS #endif //Set api export calling convention (allow used to override) #ifndef NC_CC - #ifdef IS_WINDOWS + #ifdef _NC_IS_WINDOWS //STD for importing to other languages such as .NET #define NC_CC __stdcall #else @@ -49,32 +48,20 @@ #ifndef NC_EXPORT //Allow users to disable the export/impoty macro if using source code directly #ifdef NOSCRYPT_EXPORTING - #ifdef IS_WINDOWS + #ifdef _NC_IS_WINDOWS #define NC_EXPORT __declspec(dllexport) #else #define NC_EXPORT __attribute__((visibility("default"))) - #endif // IS_WINDOWS + #endif // _NC_IS_WINDOWS #else - #ifdef IS_WINDOWS + #ifdef _NC_IS_WINDOWS #define NC_EXPORT __declspec(dllimport) #else #define NC_EXPORT - #endif // IS_WINDOWS + #endif // _NC_IS_WINDOWS #endif // !NOSCRYPT_EXPORTING #endif // !NC_EXPORT - -#ifndef IS_WINDOWS - #ifndef inline - #define inline __inline__ - #endif // !inline -#endif // !IS_WINDOWS - -//NULL -#ifndef NULL - #define NULL ((void*)0) -#endif // !NULL - /* * CONSTANTS */ @@ -113,7 +100,7 @@ static const uint8_t Nip44ConstantSalt[8] = { 0x6e, 0x69, 0x70, 0x34, 0x34, 0x2d * operations that return a value count. */ -#define ARG_POSITION_OFFSET 8 +#define NC_ARG_POSITION_OFFSET 8 #define NC_ERROR_CODE_MASK 0xFF #define NC_SUCCESS 0 @@ -121,27 +108,7 @@ static const uint8_t Nip44ConstantSalt[8] = { 0x6e, 0x69, 0x70, 0x34, 0x34, 0x2d #define E_INVALID_ARG -2 #define E_INVALID_CONTEXT -3 #define E_ARGUMENT_OUT_OF_RANGE -4 - -/* -* Validation macros -*/ - -#define CHECK_NULL_PTR(ptr) if(ptr == NULL) return E_NULL_PTR; -#define CHECK_INVALID_ARG(x) if(x == NULL) return E_INVALID_ARG; -#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); - -#ifdef DEBUG - -//Must include assert.h for assertions -#include - -#define DEBUG_ASSERT(x) assert(x); -#define DEBUG_ASSERT2(x, message) assert(x && message); -#else -#define DEBUG_ASSERT(x) -#define DEBUG_ASSERT2(x, message) -#endif +#define E_OPERATION_FAILED -5 /* A compressed resul/return value, negative values are failure, 0 is success and positive values are @@ -217,8 +184,7 @@ static inline NCPublicKey* NCToPubKey(uint8_t key[NC_PUBKEY_SIZE]) static inline NCResult NCResultWithArgPosition(NCResult err, uint8_t argPosition) { - //Store the error code in the lower 8 bits and the argument position in the upper 24 bits - return (err & NC_ERROR_CODE_MASK) | (((uint32_t)argPosition) << ARG_POSITION_OFFSET); + return -(((NCResult)argPosition << NC_ARG_POSITION_OFFSET) | -err); } /* @@ -228,11 +194,14 @@ that caused the error. * @param code A pointer to the error code to write to * @param argPosition A pointer to the argument position to write to */ -NC_EXPORT void NC_CC NCParseErrorCode(NCResult result, int* code, int* argPosition) +NC_EXPORT void NC_CC NCParseErrorCode(NCResult result, int* code, uint8_t* argPosition) { + //convert result to a positive value + NCResult asPositive = -result; + //Get the error code from the lower 8 bits and the argument position from the upper 8 bits - *code = result & NC_ERROR_CODE_MASK; - *argPosition = (result >> ARG_POSITION_OFFSET) & 0xFF; + *code = -(asPositive & NC_ERROR_CODE_MASK); + *argPosition = (asPositive >> NC_ARG_POSITION_OFFSET) & 0xFF; } /*-------------------------------------- @@ -296,7 +265,7 @@ NC_EXPORT NCResult NC_CC NCGetPublicKey( is functionally the same as calling secp256k1_ec_seckey_verify. * @param ctx A pointer to the existing library context * @param sk A pointer to the secret key to verify -* @return NC_SUCCESS if the secret key is in a valid format, otherwise an error code +* @return 1 if the secret key is valid, 0 if it is not, otherwise an error code */ NC_EXPORT NCResult NC_CC NCValidateSecretKey( const NCContext* ctx, @@ -338,7 +307,7 @@ 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] ); /*-------------------------------------- -- cgit