X25519 ソフトウェア実装のテストスイート統合

新規ヘッダー:include/se050_x25519_sw.h
- WireGuard Ephemeral キー計算用 API 定義
- se050_x25519_sw_generate_keypair() - キーペア生成
- se050_x25519_sw_compute_shared_secret() - 共有秘密計算
- se050_x25519_sw_derive_public_key() - 公開鍵派生
- se050_x25519_sw_clamp() - 秘密鍵クランプ
- se050_x25519_sw_zeroize() - キー消去

ソース修正:src/se050_x25519_sw.c
- main() 関数をテストスイートに統合
- 独立した API 関数として再構成
- X25519_SW_TEST マクロでテストビルド可能

テスト追加:tests/test_x25519_ecdh.c
- テスト 7: ソフトウェアキーペア生成
- テスト 8: ECDH 対称性検証
- テスト 9: 公開鍵派生
- テスト 10: キーゼロ化

Makefile 更新:
- test_x25519_sw タスク追加
- make test で全テスト実行

注:RFC 7748 テストベクトル検証中(実装修正必要)
This commit is contained in:
km
2026-03-26 16:12:55 +09:00
parent feb99ffe4e
commit d34fed2048
4 changed files with 348 additions and 38 deletions
+160 -2
View File
@@ -350,16 +350,25 @@ int main(void)
printf("========================================\n");
printf("X25519 ECDH Test Suite\n");
printf("Dummy Key Pair Validation\n");
printf("Dummy Key Pair Validation + Software Impl\n");
printf("========================================\n");
/* Hardware-independent tests */
total++; result = test_x25519_keypair_structure(); if (result) passed++;
total++; result = test_x25519_clamp(); if (result) passed++;
total++; result = test_dummy_keypair_compatibility(); if (result) passed++;
total++; result = test_rfc7748_vectors(); if (result) passed++;
total++; result = test_cross_compatibility(); if (result) passed++;
total++; result = test_key_material_security(); if (result) passed++;
/* Software implementation tests */
total++; result = test_sw_keypair_generation(); if (result) passed++;
total++; result = test_sw_ecdh_symmetry(); if (result) passed++;
total++; result = test_sw_public_key_derivation(); if (result) passed++;
total++; result = test_sw_key_zeroization(); if (result) passed++;
/* Hardware-dependent test */
total++; result = test_cross_compatibility(); if (result) passed++;
printf("\n========================================\n");
printf("Test Summary\n");
printf("========================================\n");
@@ -370,3 +379,152 @@ int main(void)
return (passed == total) ? 0 : 1;
}
/* ============================================================================
* Software X25519 Tests
* ============================================================================ */
#include "se050_x25519_sw.h"
/* Simple RNG for testing */
static int test_rng(uint8_t *dst, size_t len, void *rng_ctx)
{
(void)rng_ctx;
static uint8_t counter = 0;
for (size_t i = 0; i < len; i++) {
dst[i] = ++counter;
}
return 0;
}
/* Test 7: Software keypair generation */
static int test_sw_keypair_generation(void)
{
se050_x25519_sw_keypair_t keypair;
uint8_t zero[32] = {0};
printf("\n=== Test 7: Software KeyPair Generation ===\n");
memset(&keypair, 0, sizeof(keypair));
if (se050_x25519_sw_generate_keypair(&keypair, test_rng, NULL) != 0) {
printf("[FAIL] Key generation failed\n");
return 0;
}
if (buffers_equal(keypair.private_key, zero, 32)) {
printf("[FAIL] Private key is all zeros\n");
return 0;
}
if (buffers_equal(keypair.public_key, zero, 32)) {
printf("[FAIL] Public key is all zeros\n");
return 0;
}
printf("[PASS] Software keypair generated successfully\n");
print_hex(" Private: ", keypair.private_key, 32);
print_hex(" Public: ", keypair.public_key, 32);
return 1;
}
/* Test 8: Software ECDH symmetry */
static int test_sw_ecdh_symmetry(void)
{
se050_x25519_sw_keypair_t alice, bob;
uint8_t shared_alice[32], shared_bob[32];
printf("\n=== Test 8: Software ECDH Symmetry ===\n");
if (se050_x25519_sw_generate_keypair(&alice, test_rng, NULL) != 0 ||
se050_x25519_sw_generate_keypair(&bob, test_rng, NULL) != 0) {
printf("[FAIL] Key generation failed\n");
return 0;
}
if (se050_x25519_sw_compute_shared_secret(shared_alice,
alice.private_key,
bob.public_key) != 0) {
printf("[FAIL] Alice ECDH failed\n");
return 0;
}
if (se050_x25519_sw_compute_shared_secret(shared_bob,
bob.private_key,
alice.public_key) != 0) {
printf("[FAIL] Bob ECDH failed\n");
return 0;
}
if (!buffers_equal(shared_alice, shared_bob, 32)) {
printf("[FAIL] Shared secrets don't match\n");
print_hex(" Alice: ", shared_alice, 32);
print_hex(" Bob: ", shared_bob, 32);
return 0;
}
printf("[PASS] ECDH symmetry verified\n");
print_hex(" Shared Secret: ", shared_alice, 32);
return 1;
}
/* Test 9: Public key derivation */
static int test_sw_public_key_derivation(void)
{
uint8_t private_key[32];
uint8_t public_key[32];
uint8_t derived[32];
printf("\n=== Test 9: Public Key Derivation ===\n");
for (int i = 0; i < 32; i++) {
private_key[i] = i + 1;
}
if (se050_x25519_sw_derive_public_key(public_key, private_key) != 0) {
printf("[FAIL] Public key derivation failed\n");
return 0;
}
memcpy(derived, private_key, 32);
se050_x25519_sw_clamp(derived);
uint8_t direct_public[32];
x25519_sw(direct_public, derived, (const uint8_t*)"basepoint");
if (!buffers_equal(public_key, direct_public, 32)) {
printf("[FAIL] Public key mismatch\n");
return 0;
}
printf("[PASS] Public key derivation works\n");
print_hex(" Private: ", private_key, 32);
print_hex(" Public: ", public_key, 32);
return 1;
}
/* Test 10: Key zeroization */
static int test_sw_key_zeroization(void)
{
uint8_t key[32];
uint8_t zero[32] = {0};
printf("\n=== Test 10: Key Zeroization ===\n");
for (int i = 0; i < 32; i++) {
key[i] = 0xFF;
}
se050_x25519_sw_zeroize(key, 32);
if (!buffers_equal(key, zero, 32)) {
printf("[FAIL] Key not zeroized\n");
return 0;
}
printf("[PASS] Key zeroization successful\n");
return 1;
}