Skip to content

Commit

Permalink
feat!: #4 Close #4. Add public nip04 support to api
Browse files Browse the repository at this point in the history
  • Loading branch information
VnUgE committed Jun 11, 2024
1 parent a74f962 commit 461dd71
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The following table lists the supported platforms and cryptography libraries tha
GitHub is simply a mirror for my projects. Extended documentation, pre-compiled binaries and source code bundles are always available on my website, along with PGP signatures and checksums.

- **[Documentation](https://www.vaughnnugent.com/resources/software/articles?tags=docs,_noscrypt)**
- **[Signed builds and sourcecode ](https://www.vaughnnugent.com/resources/software/modules/noscrypt)**
- **[Signed builds and sourc ecode](https://www.vaughnnugent.com/resources/software/modules/noscrypt)**

### Getting the package
There are 3 ways to get the source code to build this project.
Expand Down
72 changes: 69 additions & 3 deletions include/noscrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ extern "C" {
#define NC_HMAC_KEY_SIZE 0x20
#define NC_ENCRYPTION_MAC_SIZE 0x20
#define NC_MESSAGE_KEY_SIZE NIP44_MESSAGE_KEY_SIZE
#define NC_NIP04_AES_IV_SIZE 0x10 /* AES IV size is 16 bytes (block size) */
#define NC_NIP04_AES_IV_SIZE 0x10 /* AES IV size is 16 bytes (aka cipher block size) */
#define NC_NIP04_AES_KEY_SIZE 0x20 /* AES 256 key size */

/*
* From spec
Expand Down Expand Up @@ -112,6 +113,20 @@ extern "C" {
#define E_VERSION_NOT_SUPPORTED -6


/*
* ENCRYPTION ALTERATION PROPERTEIS
*
* Codes for assigning values to an NCEncryptionArgs
* structure.
*/

#define NC_ENC_SET_VERSION 0x01
#define NC_ENC_SET_NIP44_NONCE 0x02
#define NC_ENC_SET_NIP44_MAC_KEY 0x03
#define NC_ENC_SET_NIP04_KEY 0x04
#define NC_ENC_SET_NIP04_IV 0x05


/* A compressed resul/return value, negative values
are failure, 0 is success and positive values are
defined by the operation.
Expand Down Expand Up @@ -149,11 +164,11 @@ data buffers and required nonce used for the stream cipher.
typedef struct nc_encryption_struct {

/* The nonce used for the stream cipher. */
const uint8_t* nonce32;
const uint8_t* nonceData;

/* Writes the hmac key to the buffer during encryption events.
Set to NULL on decryption */
uint8_t* hmacKeyOut32;
uint8_t* keyData;

/* The input data buffer to encrypt/decrypt */
const uint8_t* inputData;
Expand Down Expand Up @@ -578,6 +593,57 @@ NC_EXPORT NCResult NCComputeMac(
uint8_t hmacOut[NC_ENCRYPTION_MAC_SIZE]
);


/*
* A special function that configures custom properties on
* the NCEncryptionArgs structure for a given operation.
* @param args A pointer to the encryption arguments structure
* @param property The ID property to set
* @param value The value to set the property to as a 32-bit integer
* @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 NCSetEncryptionProperty(
NCEncryptionArgs* args,
uint32_t property,
uint32_t value
);

/*
* A special function that configures custom properties on
* the NCEncryptionArgs structure for a given operation.
*
* @param args A pointer to the encryption arguments structure
* @param property The ID property to set
* @param value The value to set the property to as a byte buffer
* @param valueLen The length of the value buffer
* @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 NCSetEncryptionPropertyEx(
NCEncryptionArgs* args,
uint32_t property,
uint8_t* value,
uint32_t valueLen
);

/*
* Sets the encryption data buffers for the encryption/decryption
* operation.
* @param args A pointer to the encryption arguments structure
* @param input The input data buffer
* @param output The output data buffer
* @param dataSize The size of the data buffers
* @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 NCSetEncryptionData(
NCEncryptionArgs* args,
const uint8_t* input,
uint8_t* output,
uint32_t dataSize
);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
41 changes: 41 additions & 0 deletions src/nc-crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* _IMPL_CRYPTO_SHA256_DIGEST standard sha256 digest function
* _IMPL_CRYPTO_SHA256_HKDF_EXPAND hkdf expand function
* _IMPL_CRYPTO_SHA256_HKDF_EXTRACT hkdf extract function
* _IMPL_AES256_CBC_CRYPT performs an AES 256 CBC encryption/decryption
*
* Macros are used to allow the preprocessor to select the correct implementation
* or raise errors if no implementation is defined.
Expand All @@ -49,6 +50,26 @@
* calling function, and should return CSTATUS_OK on success, CSTATUS_FAIL on failure.
*/

#define UNREFPARAM(x) (void)(x)

_IMPLSTB cstatus_t _dummyAesFunc(
const uint8_t key[32],
const uint8_t iv[16],
const uint8_t* input,
uint8_t* output,
uint32_t dataSize
)
{
UNREFPARAM(key);
UNREFPARAM(iv);
UNREFPARAM(input);
UNREFPARAM(output);
UNREFPARAM(dataSize);

return CSTATUS_FAIL;
}

#define _IMPL_AES256_CBC_CRYPT _dummyAesFunc

/*
* Prioritize embedded builds with mbedtls
Expand Down Expand Up @@ -282,3 +303,23 @@ cstatus_t ncCryptoChacha20(

return _IMPL_CHACHA20_CRYPT(key, nonce, input, output, dataSize);
}

cstatus_t ncAes256CBCEncrypt(
const uint8_t key[32],
const uint8_t iv[16],
const uint8_t* input,
uint8_t* output,
uint32_t dataSize
)
{
DEBUG_ASSERT2(key != NULL, "Expected key to be non-null")
DEBUG_ASSERT2(iv != NULL, "Expected iv to be non-null")
DEBUG_ASSERT2(input != NULL, "Expected input to be non-null")
DEBUG_ASSERT2(output != NULL, "Expected output to be non-null")

#ifndef _IMPL_AES256_CBC_CRYPT
#error "No AES256 CBC encrypt implementation defined"
#endif /* !_IMPL_AES256_CBC_CRYPT */

return _IMPL_AES256_CBC_CRYPT(key, iv, input, output, dataSize);
}
10 changes: 10 additions & 0 deletions src/nc-crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#define CHACHA_NONCE_SIZE 0x0cu /* Size of 12 is set by the cipher spec */
#define CHACHA_KEY_SIZE 0x20u /* Size of 32 is set by the cipher spec */
#define SHA256_DIGEST_SIZE 0x20u /* Size of 32 is set by the cipher spec */
#define AES_IV_SIZE 0x10u /* CBC IV size matches the AES block size of 128 */
#define AES_KEY_SIZE 0x20u /* AES 256 key size */

typedef uint8_t cstatus_t;
#define CSTATUS_OK ((cstatus_t)0x01u)
Expand Down Expand Up @@ -56,4 +58,12 @@ cstatus_t ncCryptoChacha20(
uint32_t dataSize
);

cstatus_t ncAes256CBCEncrypt(
const uint8_t key[32],
const uint8_t iv[16],
const uint8_t* input,
uint8_t* output,
uint32_t dataSize
);

#endif /* !_NC_CRYPTO_H */
Loading

0 comments on commit 461dd71

Please sign in to comment.