Fixed bug in nip44.c, was an error in ecdh_shared_secret. Added comments

This commit is contained in:
2025-08-17 11:29:07 -04:00
parent d8b342ca3f
commit 3ebfdc06c0
14 changed files with 15 additions and 69 deletions

View File

@@ -27,6 +27,7 @@ static void memory_clear(const void *p, size_t len) {
}
}
// =============================================================================
// NIP-44 UTILITY FUNCTIONS
// =============================================================================
@@ -85,13 +86,8 @@ static char* unpad_plaintext(const unsigned char* padded, size_t padded_len) {
size_t unpadded_len = (padded[0] << 8) | padded[1];
size_t expected_padded_len = calc_padded_len(unpadded_len);
printf("--- unpad_plaintext DEBUG ---\n");
printf("padded_len: %zu\n", padded_len);
printf("unpadded_len: %zu\n", unpadded_len);
printf("expected_padded_len: %zu\n", expected_padded_len);
printf("--- end unpad_plaintext DEBUG ---\n");
if (padded_len != expected_padded_len) {
if (padded_len != expected_padded_len + 2) {
return NULL;
}
@@ -130,6 +126,7 @@ int nostr_nip44_encrypt_with_nonce(const unsigned char* sender_private_key,
return NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL;
}
// Step 1: Compute ECDH shared secret
unsigned char shared_secret[32];
if (ecdh_shared_secret(sender_private_key, recipient_public_key, shared_secret) != 0) {
@@ -150,6 +147,7 @@ int nostr_nip44_encrypt_with_nonce(const unsigned char* sender_private_key,
unsigned char nonce_copy[32];
memcpy(nonce_copy, nonce, 32);
// Step 4: Derive message keys (HKDF-expand with nonce as info)
unsigned char message_keys[76]; // 32 chacha_key + 12 chacha_nonce + 32 hmac_key
if (nostr_hkdf_expand(conversation_key, 32, nonce_copy, 32, message_keys, 76) != 0) {
@@ -163,6 +161,7 @@ int nostr_nip44_encrypt_with_nonce(const unsigned char* sender_private_key,
unsigned char* chacha_nonce = message_keys + 32;
unsigned char* hmac_key = message_keys + 44;
// Step 5: Pad plaintext according to NIP-44 spec
size_t padded_len;
unsigned char* padded_plaintext = pad_plaintext(plaintext, &padded_len);