test_x25519_ecdh: 実際の SE050 ECDH 計算テストを追加

Test 5 を実装して、SE050 ハードウェア接続時の実際の ECDH 計算を検証:

実装内容:
- I2C HAL 初期化と SE050 接続
- Session/Keystore/RNG の初期化
- Alice と Bob の鍵ペア生成 (se050_x25519_generate_keypair)
- Alice: ECDH(Bob_pub, Alice_priv) 計算
- Bob: ECDH(Alice_pub, Bob_priv) 計算
- 共有秘密の一致確認

動作:
- SE050 未接続:SKIP (構造テストは Test 3 で完了)
- SE050 接続時:実際の ECDH 計算と共有秘密の一致を検証

エラーハンドリング:
- I2C 接続失敗: gracefully skip
- セッション作成失敗: gracefully skip
- 鍵生成失敗: fail
- ECDH 計算失敗: fail
- 共有秘密不一致: fail

テスト結果:
- SE050 未接続環境:6/6 PASS (Test 5 は SKIP)
- SE050 接続環境:実際の ECDH 計算を検証可能
This commit is contained in:
km
2026-03-26 14:00:02 +09:00
parent f7b9581428
commit ff32a1052f
+123 -7
View File
@@ -185,16 +185,132 @@ static int test_rfc7748_vectors(void)
return 1;
}
/* Test 5: Cross-compatibility */
/* Test 5: Cross-compatibility with actual SE050 ECDH */
static int test_cross_compatibility(void)
{
printf("\n=== Test 5: Cross-Compatibility Check ===\n");
printf("[INFO] Alice computes: ECDH(Bob_pub, Alice_priv)\n");
printf("[INFO] Bob computes: ECDH(Alice_pub, Bob_priv)\n");
printf("[INFO] Both should get same shared secret\n");
printf("[PASS] Cross-compatibility structure verified\n");
printf("[INFO] Actual computation requires SE050 hardware\n");
se050_i2c_hal_t hal;
se050_session_ctx_t *session;
se050_keystore_ctx_t *keystore;
se050_rng_ctx_t *rng;
se050_status_t status;
uint8_t shared_secret_alice[32];
uint8_t shared_secret_bob[32];
int result = 1;
printf("\n=== Test 5: Cross-Compatibility Check (SE050 ECDH) ===\n");
/* Check if hardware is available */
printf("[INFO] Attempting to connect to SE050...\n");
/* Initialize I2C HAL */
status = se050_i2c_init(&hal, "/dev/i2c-1", 0x48);
if (status != SE050_OK) {
printf("[SKIP] SE050 not available at /dev/i2c-1 (0x48)\n");
printf("[INFO] For hardware test, ensure SE050 is connected\n");
printf("[INFO] Structure verification passed (see Test 3)\n");
return 1; /* Skip, not fail */
}
printf("[INFO] I2C connection established\n");
/* Create session */
status = se050_session_create(&session, &hal);
if (status != SE050_OK) {
printf("[SKIP] Session creation failed (0x%04x)\n", status);
se050_i2c_close(&hal);
return 1; /* Skip, not fail */
}
printf("[INFO] Session created\n");
/* Initialize keystore */
status = se050_keystore_init(&keystore, session);
if (status != SE050_OK) {
printf("[SKIP] Keystore init failed (0x%04x)\n", status);
se050_session_delete(session);
se050_i2c_close(&hal);
return 1;
}
printf("[INFO] Keystore initialized\n");
/* Initialize RNG */
status = se050_rng_init(&rng, session);
if (status != SE050_OK) {
printf("[SKIP] RNG init failed (0x%04x)\n", status);
se050_keystore_free(keystore);
se050_session_delete(session);
se050_i2c_close(&hal);
return 1;
}
printf("[INFO] RNG initialized\n");
/* Generate Alice's keypair */
se050_x25519_keypair_t keypair_alice;
status = se050_x25519_generate_keypair(keystore, &keypair_alice, 0x1001);
if (status != SE050_OK) {
printf("[SKIP] Alice key generation failed (0x%04x)\n", status);
result = 0;
goto cleanup;
}
printf("[INFO] Alice's keypair generated (ID: 0x1001)\n");
/* Generate Bob's keypair */
se050_x25519_keypair_t keypair_bob;
status = se050_x25519_generate_keypair(keystore, &keypair_bob, 0x1002);
if (status != SE050_OK) {
printf("[SKIP] Bob key generation failed (0x%04x)\n", status);
result = 0;
goto cleanup;
}
printf("[INFO] Bob's keypair generated (ID: 0x1002)\n");
/* Alice computes shared secret using Bob's public key */
printf("[INFO] Alice computing ECDH with Bob's public key...\n");
status = se050_x25519_compute_shared_secret(keystore, 0x1001,
keypair_bob.public_key,
shared_secret_alice);
if (status != SE050_OK) {
printf("[FAIL] Alice ECDH computation failed (0x%04x)\n", status);
result = 0;
goto cleanup;
}
printf("[INFO] Alice's shared secret computed\n");
/* Bob computes shared secret using Alice's public key */
printf("[INFO] Bob computing ECDH with Alice's public key...\n");
status = se050_x25519_compute_shared_secret(keystore, 0x1002,
keypair_alice.public_key,
shared_secret_bob);
if (status != SE050_OK) {
printf("[FAIL] Bob ECDH computation failed (0x%04x)\n", status);
result = 0;
goto cleanup;
}
printf("[INFO] Bob's shared secret computed\n");
/* Compare shared secrets */
printf("[INFO] Comparing shared secrets...\n");
printf("[INFO] Alice's shared secret:\n");
print_hex(" ", shared_secret_alice, 32);
printf("[INFO] Bob's shared secret:\n");
print_hex(" ", shared_secret_bob, 32);
if (!buffers_equal(shared_secret_alice, shared_secret_bob, 32)) {
printf("[FAIL] Shared secrets DO NOT match!\n");
result = 0;
goto cleanup;
}
printf("[PASS] Shared secrets match! ECDH successful.\n");
printf("[INFO] Alice and Bob now share a common secret for WireGuard.\n");
cleanup:
/* Cleanup */
se050_rng_free(rng);
se050_keystore_free(keystore);
se050_session_delete(session);
se050_i2c_close(&hal);
return result;
}
/* Test 6: Key material security */