- Add shared ksync_crypto module: SHA-256, HMAC-SHA256, PBKDF2-SHA256, ChaCha20-Poly1305 (RFC 8439), hex helpers, random bytes; all verified against RFC test vectors in tests/ksync_crypto_test.c - Encrypted account exports (v2): PBKDF2 (600k iterations) + ChaCha20-Poly1305, passphrase-protected import/export APIs; the v1 plaintext format remains importable - DeleteKsyncSyncAccount no longer transmits the private key: it signs a challenge against /api/v1/account/delete and falls back to the legacy delete-with-key endpoint on 404/405 - JSON writer escapes control characters (including \b, \f, \uXXXX); reader now decodes escapes, rejects substring keys and keys embedded in string values - Token expiry accounts for server clock skew (server_time in the login response, clamped to +/-24h) - Optional retry with exponential backoff for transport errors, 429, and 5xx via retry_max/retry_delay_ms/sleep_ms config fields - Opt-in end-to-end payload encryption (encrypt_payload flag plus Wrap/UnwrapKsyncSyncPayload) derived from the account private key
144 lines
5.4 KiB
C
144 lines
5.4 KiB
C
#include "ksync_account.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static int failures = 0;
|
|
|
|
static void
|
|
check_true(const char *label, int value)
|
|
{
|
|
if(!value) {
|
|
fprintf(stderr, "FAIL: %s\n", label);
|
|
failures++;
|
|
}
|
|
}
|
|
|
|
static void
|
|
bytes_to_hex_local(const unsigned char *bytes, size_t len, char *out, size_t out_size)
|
|
{
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
if(out == NULL || out_size < len * 2 + 1)
|
|
return;
|
|
for(size_t i = 0; i < len; i++) {
|
|
out[i * 2] = hex[bytes[i] >> 4];
|
|
out[i * 2 + 1] = hex[bytes[i] & 0x0f];
|
|
}
|
|
out[len * 2] = '\0';
|
|
}
|
|
|
|
static void
|
|
make_account(KsyncAccount *account)
|
|
{
|
|
unsigned char public_key[1312];
|
|
unsigned char private_key[2560];
|
|
|
|
memset(account, 0, sizeof(*account));
|
|
for(size_t i = 0; i < sizeof(public_key); i++)
|
|
public_key[i] = (unsigned char)(i * 7U + 11U);
|
|
for(size_t i = 0; i < sizeof(private_key); i++)
|
|
private_key[i] = (unsigned char)(i * 17U + 3U);
|
|
KsyncSha256Hex(public_key, sizeof(public_key), account->public_id);
|
|
bytes_to_hex_local(public_key, sizeof(public_key), account->public_key_hex,
|
|
sizeof(account->public_key_hex));
|
|
bytes_to_hex_local(private_key, sizeof(private_key), account->private_key_hex,
|
|
sizeof(account->private_key_hex));
|
|
}
|
|
|
|
static void
|
|
test_export_parse_roundtrip(void)
|
|
{
|
|
KsyncAccount account;
|
|
KsyncAccount parsed;
|
|
char text[KSYNC_ACCOUNT_EXPORT_TEXT_SIZE];
|
|
|
|
make_account(&account);
|
|
check_true("export text", ExportKsyncAccountText(&account, text, sizeof(text)));
|
|
check_true("generic header", strstr(text, "ksync-account-key-v1\n") == text);
|
|
check_true("parse exported text", ParseKsyncAccountText(text, &parsed));
|
|
check_true("roundtrip public id", strcmp(parsed.public_id, account.public_id) == 0);
|
|
check_true("roundtrip public key", strcmp(parsed.public_key_hex, account.public_key_hex) == 0);
|
|
check_true("roundtrip private key",
|
|
strcmp(parsed.private_key_hex, account.private_key_hex) == 0);
|
|
}
|
|
|
|
static void
|
|
test_old_imports(void)
|
|
{
|
|
KsyncAccount account;
|
|
KsyncAccount parsed;
|
|
char text[KSYNC_ACCOUNT_EXPORT_TEXT_SIZE];
|
|
char json[KSYNC_ACCOUNT_EXPORT_TEXT_SIZE + 128];
|
|
|
|
make_account(&account);
|
|
snprintf(text, sizeof(text),
|
|
"inbe-sync-key-v1\nalgorithm=ML-DSA-44\npublic_key=%s\nsecret_key=%s\n",
|
|
account.public_key_hex, account.private_key_hex);
|
|
check_true("old secret_key import", ParseKsyncAccountText(text, &parsed));
|
|
check_true("old derived public id", strcmp(parsed.public_id, account.public_id) == 0);
|
|
|
|
snprintf(text, sizeof(text),
|
|
"account-key-v1\nalgorithm=ML-DSA-44\npublic_id=%s\npublic_key=%s\nprivate_key=%s\n",
|
|
account.public_id, account.public_key_hex, account.private_key_hex);
|
|
check_true("old account-key import", ParseKsyncAccountText(text, &parsed));
|
|
|
|
snprintf(text, sizeof(text),
|
|
"lyra-account-key-v1\nalgorithm=ML-DSA-44\npublic_id=%s\npublic_key=%s\nprivate_key=%s\n",
|
|
account.public_id, account.public_key_hex, account.private_key_hex);
|
|
check_true("old lyra account-key import", ParseKsyncAccountText(text, &parsed));
|
|
|
|
snprintf(json, sizeof(json),
|
|
"{\"exported_key\":\"ksync-account-key-v1\\nalgorithm=ML-DSA-44\\npublic_id=%s\\npublic_key=%s\\nprivate_key=%s\\n\"}",
|
|
account.public_id, account.public_key_hex, account.private_key_hex);
|
|
check_true("json exported_key import", ParseKsyncAccountText(json, &parsed));
|
|
}
|
|
|
|
static void
|
|
test_reject_mismatch(void)
|
|
{
|
|
KsyncAccount account;
|
|
char text[KSYNC_ACCOUNT_EXPORT_TEXT_SIZE];
|
|
|
|
make_account(&account);
|
|
account.public_id[0] = account.public_id[0] == '0' ? '1' : '0';
|
|
snprintf(text, sizeof(text),
|
|
"ksync-account-key-v1\nalgorithm=ML-DSA-44\npublic_id=%s\npublic_key=%s\nprivate_key=%s\n",
|
|
account.public_id, account.public_key_hex, account.private_key_hex);
|
|
check_true("reject mismatched public id", !ParseKsyncAccountText(text, &account));
|
|
}
|
|
|
|
static void
|
|
test_encrypted_roundtrip(void)
|
|
{
|
|
KsyncAccount account;
|
|
KsyncAccount parsed;
|
|
char text[KSYNC_ACCOUNT_EXPORT_ENCRYPTED_TEXT_SIZE];
|
|
|
|
make_account(&account);
|
|
check_true("encrypted export", ExportKsyncAccountTextEncrypted(&account, "hunter2", text, sizeof(text)));
|
|
check_true("encrypted export marks v2 header", strncmp(text, "ksync-account-key-v2", 20) == 0);
|
|
check_true("encrypted export hides private key", strstr(text, account.private_key_hex) == NULL);
|
|
check_true("encrypted import", ParseKsyncAccountTextEncrypted(text, "hunter2", &parsed));
|
|
check_true("encrypted roundtrip id", strcmp(parsed.public_id, account.public_id) == 0);
|
|
check_true("encrypted roundtrip public key", strcmp(parsed.public_key_hex, account.public_key_hex) == 0);
|
|
check_true("encrypted roundtrip private key", strcmp(parsed.private_key_hex, account.private_key_hex) == 0);
|
|
check_true("wrong passphrase rejected", !ParseKsyncAccountTextEncrypted(text, "wrong", &parsed));
|
|
check_true("tampered ciphertext rejected", !ParseKsyncAccountTextEncrypted(text, "hunter2", &parsed) ||
|
|
(text[strlen(text) - 1] ^= 1, !ParseKsyncAccountTextEncrypted(text, "hunter2", &parsed)));
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_export_parse_roundtrip();
|
|
test_old_imports();
|
|
test_reject_mismatch();
|
|
test_encrypted_roundtrip();
|
|
if(failures != 0)
|
|
return 1;
|
|
printf("ksync account tests passed\n");
|
|
return 0;
|
|
}
|