Skip to content

Commit db0c311

Browse files
committed
enable tests
1 parent 2a5ca58 commit db0c311

File tree

1 file changed

+47
-176
lines changed

1 file changed

+47
-176
lines changed

test/embedded/test_id/id_test.cpp

Lines changed: 47 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,53 @@ constexpr Sha256TestVector sha256_test_vectors[] = {
165165
constexpr uint8_t sha256_a1000000_result[] = {0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, 0x81, 0xA1, 0xC7,
166166
0xE2, 0x84, 0xD7, 0x3E, 0x67, 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97,
167167
0x20, 0x0E, 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0};
168+
169+
template <typename T, typename U>
170+
void test_random(UnitATECC608B_TNGTLS* u, const U l, const U h)
171+
{
172+
const T lower = static_cast<T>(l);
173+
const T higher = static_cast<T>(h);
174+
175+
uint32_t count{100};
176+
std::vector<T> result;
177+
178+
while (count--) {
179+
T value{};
180+
EXPECT_TRUE(u->readRandom(value, lower, higher)); // [lower ... higher)
181+
182+
EXPECT_LT(value, higher);
183+
EXPECT_GE(value, lower);
184+
result.push_back(value);
185+
}
186+
EXPECT_EQ(result.size(), 100);
187+
EXPECT_FALSE(std::all_of(result.cbegin() + 1, result.cend(), [&result](const uint8_t v) { return v == result[0]; }))
188+
<< "low:" << l << " high:" << h;
189+
}
190+
191+
template <typename T, typename U>
192+
void test_random_float(UnitATECC608B_TNGTLS* u, const U l, const U h)
193+
{
194+
const T lower = static_cast<T>(l);
195+
const T higher = static_cast<T>(h);
196+
197+
uint32_t count{100};
198+
std::vector<T> result;
199+
200+
while (count--) {
201+
T value{};
202+
EXPECT_TRUE(u->readRandom(value, lower, higher)); // [lower ... higher)
203+
204+
EXPECT_TRUE(value < higher) << "actual=" << value << ", expected=" << higher;
205+
EXPECT_TRUE(value >= lower) << "actual=" << value << ", expected=" << higher;
206+
result.push_back(value);
207+
}
208+
EXPECT_EQ(result.size(), 100);
209+
EXPECT_FALSE(std::all_of(result.cbegin() + 1, result.cend(), [&result](const uint8_t v) { return v == result[0]; }))
210+
<< "low:" << l << " high:" << h;
211+
}
212+
168213
} // namespace
169214

170-
#if 0
171215
TEST_P(TestATECC608B_TNGTLS, serialNumber)
172216
{
173217
SCOPED_TRACE(ustr);
@@ -230,11 +274,8 @@ TEST_P(TestATECC608B_TNGTLS, Info)
230274
}
231275
}
232276
}
233-
234277
}
235-
#endif
236278

237-
#if 0
238279
TEST_P(TestATECC608B_TNGTLS, Nonce)
239280
{
240281
uint16_t state{};
@@ -309,7 +350,6 @@ TEST_P(TestATECC608B_TNGTLS, Nonce)
309350
EXPECT_TRUE(clear_tempkey(unit.get()));
310351
EXPECT_FALSE(unit->writeNonce32(Destination::ExternalBuffer, nonce32));
311352

312-
313353
// Write 64-byte nonce (TempKey)
314354
EXPECT_TRUE(clear_tempkey(unit.get()));
315355
EXPECT_TRUE(unit->writeNonce64(Destination::TempKey, nonce64));
@@ -329,53 +369,6 @@ TEST_P(TestATECC608B_TNGTLS, Nonce)
329369
EXPECT_FALSE(unit->writeNonce64(Destination::ExternalBuffer, nonce64));
330370
}
331371

332-
namespace {
333-
template <typename T, typename U>
334-
void test_random(UnitATECC608B_TNGTLS* u, const U l, const U h)
335-
{
336-
const T lower = static_cast<T>(l);
337-
const T higher = static_cast<T>(h);
338-
339-
uint32_t count{100};
340-
std::vector<T> result;
341-
342-
while (count--) {
343-
T value{};
344-
EXPECT_TRUE(u->readRandom(value, lower, higher)); // [lower ... higher)
345-
346-
EXPECT_LT(value, higher);
347-
EXPECT_GE(value, lower);
348-
result.push_back(value);
349-
}
350-
EXPECT_EQ(result.size(), 100);
351-
EXPECT_FALSE(std::all_of(result.cbegin() + 1, result.cend(), [&result](const uint8_t v) { return v == result[0]; }))
352-
<< "low:" << l << " high:" << h;
353-
}
354-
355-
template <typename T, typename U>
356-
void test_random_float(UnitATECC608B_TNGTLS* u, const U l, const U h)
357-
{
358-
const T lower = static_cast<T>(l);
359-
const T higher = static_cast<T>(h);
360-
361-
uint32_t count{100};
362-
std::vector<T> result;
363-
364-
while (count--) {
365-
T value{};
366-
EXPECT_TRUE(u->readRandom(value, lower, higher)); // [lower ... higher)
367-
368-
EXPECT_TRUE(value < higher) << "actual=" << value << ", expected=" << higher;
369-
EXPECT_TRUE(value >= lower) << "actual=" << value << ", expected=" << higher;
370-
result.push_back(value);
371-
}
372-
EXPECT_EQ(result.size(), 100);
373-
EXPECT_FALSE(std::all_of(result.cbegin() + 1, result.cend(), [&result](const uint8_t v) { return v == result[0]; }))
374-
<< "low:" << l << " high:" << h;
375-
}
376-
377-
} // namespace
378-
379372
TEST_P(TestATECC608B_TNGTLS, Random)
380373
{
381374
SCOPED_TRACE(ustr);
@@ -439,7 +432,7 @@ TEST_P(TestATECC608B_TNGTLS, Read)
439432
uint8_t data[416]{};
440433
for (uint8_t s = 0; s < 16; ++s) {
441434
// Can read as clear text
442-
if (s==5 || s == 8 || s == 10 || s == 11 || s == 12) {
435+
if (s == 5 || s == 8 || s == 10 || s == 11 || s == 12) {
443436
EXPECT_TRUE(unit->readDataZone(data, unit->getSlotSize((Slot)s), (Slot)s));
444437
} else {
445438
EXPECT_FALSE(unit->readDataZone(data, unit->getSlotSize((Slot)s), (Slot)s));
@@ -457,7 +450,7 @@ TEST_P(TestATECC608B_TNGTLS, SHA256)
457450
uint16_t state{};
458451

459452
// 'a' String repeated 1000000 times
460-
constexpr uint32_t ilen{1000000};
453+
constexpr uint32_t ilen{1000000};
461454
// uint8_t* in = (uint8_t*)malloc(1000000);
462455
uint8_t* in = nullptr;
463456
if (in) {
@@ -576,126 +569,6 @@ TEST_P(TestATECC608B_TNGTLS, SHA256)
576569

577570
free(in);
578571
}
579-
#endif
580-
581-
#if 0
582-
TEST_P(TestATECC608B_TNGTLS, ECDH)
583-
{
584-
SCOPED_TRACE(ustr);
585-
586-
// Make private key at TempKey
587-
uint8_t pubKey[64]{};
588-
EXPECT_TRUE(unit->generateKey(pubKey);
589-
590-
// 2. ソフトウェア側で鍵ペア作成(OpenSSLや mbedTLS を使うが今回は固定ベクター)
591-
const uint8_t software_privkey[32] = {
592-
0xC5, 0xAA, 0x8D, 0x6A, 0x58, 0x3E, 0xE6, 0xCD,
593-
0xB4, 0x64, 0x05, 0x69, 0x8C, 0xF1, 0x6B, 0x07,
594-
0x60, 0x19, 0x9C, 0x17, 0x6D, 0x46, 0xA0, 0x8F,
595-
0x77, 0xD7, 0x04, 0x2F, 0x7C, 0x43, 0x76, 0x30,
596-
};
597-
const uint8_t software_pubkey[64] = {
598-
0x04,
599-
0x3A, 0xD0, 0xFD, 0xF2, 0x8D, 0x73, 0x5A, 0x7B,
600-
0x8F, 0x62, 0xB0, 0xF5, 0xE7, 0x3B, 0xCE, 0x44,
601-
0xD3, 0x3E, 0xC2, 0x60, 0x30, 0xF7, 0x9C, 0x45,
602-
0x2A, 0x3A, 0x90, 0x13, 0xD3, 0x41, 0x7E, 0x19,
603-
0xF8, 0xB0, 0x68, 0x70, 0xDE, 0xA4, 0x02, 0x03,
604-
0xD5, 0x67, 0x8A, 0x16, 0x7F, 0x9D, 0x0E, 0x5F,
605-
0x5B, 0x35, 0xAD, 0x69, 0x65, 0x6C, 0x26, 0x60,
606-
0x62, 0x30, 0x67, 0x26, 0x8B, 0x89, 0xE7, 0xAF
607-
};
608-
609-
// 3. ATECC608B による ECDH
610-
uint8_t shared_secret_atecc[32]{};
611-
ASSERT_TRUE(atecc.ecdh(shared_secret_atecc, Slot::PrimaryPrivateKey, software_pubkey));
612-
613-
// 4. ソフト側でも同じ shared secret を生成(例:mbedTLSなどを想定、ここでは固定ベクターとの一致確認)
614-
const uint8_t expected_shared[32] = {
615-
0x6A, 0x27, 0x2B, 0x4F, 0x12, 0xC2, 0x88, 0xB3,
616-
0x68, 0xA3, 0x2D, 0xD2, 0xFC, 0xD3, 0x66, 0x6A,
617-
0x96, 0x6D, 0x69, 0x0C, 0x7C, 0xE9, 0xD6, 0x0A,
618-
0x48, 0xAB, 0x4D, 0xA4, 0x23, 0xB3, 0xB5, 0x44,
619-
};
620-
621-
EXPECT_EQ(memcmp(shared_secret_atecc, expected_shared, 32), 0) << "ECDH output mismatch!";
622-
}
623-
#endif
624-
625-
#if 0
626-
typedef struct {
627-
const char* name;
628-
const uint8_t private_key[32];
629-
const uint8_t public_key_x[32];
630-
const uint8_t public_key_y[32];
631-
const uint8_t expected_shared_secret[32];
632-
} ECDHTestVector;
633-
634-
const ECDHTestVector ecdh_test_vectors[] = {
635-
{"secp256r1 test vector 1",
636-
{0x38, 0xf6, 0x5d, 0x6d, 0xce, 0x47, 0x67, 0x60, 0x44, 0xd5, 0x8c, 0xe5, 0x13, 0x95, 0x82, 0xd5,
637-
0x68, 0xf6, 0x4b, 0xb1, 0x60, 0x98, 0xd1, 0x79, 0xdb, 0xab, 0x07, 0x74, 0x1d, 0xd5, 0xca, 0xf5},
638-
{0x80, 0x9f, 0x04, 0x28, 0x9c, 0x64, 0x34, 0x8c, 0x01, 0x51, 0x5e, 0xb0, 0x3d, 0x5c, 0xe7, 0xac,
639-
0x1a, 0x8c, 0xb9, 0x49, 0x8f, 0x5c, 0xaa, 0x50, 0x19, 0x7e, 0x58, 0xd4, 0x3a, 0x86, 0xa7, 0xae},
640-
{0xb2, 0x9d, 0x84, 0xe8, 0x11, 0x19, 0x7f, 0x25, 0xeb, 0xa8, 0xf5, 0x19, 0x40, 0x92, 0xcb, 0x6f,
641-
0xf4, 0x40, 0xe2, 0x6d, 0x44, 0x21, 0x01, 0x13, 0x72, 0x46, 0x1f, 0x57, 0x92, 0x71, 0xcd, 0xa3},
642-
{0x05, 0x7d, 0x63, 0x60, 0x96, 0xcb, 0x80, 0xb6, 0x7a, 0x8c, 0x03, 0x8c, 0x89, 0x0e, 0x88, 0x7d,
643-
0x1a, 0xdf, 0xa4, 0x19, 0x5e, 0x9b, 0x3c, 0xe2, 0x41, 0xc8, 0xa7, 0x78, 0xc5, 0x9c, 0xda, 0x67}}
644-
// 他のベクターも同様に追加可能
645-
};
646-
647-
TEST_P(TestATECC608B_TNGTLS, ECDH)
648-
{
649-
auto& e = ecdh_test_vectors[0];
650-
651-
// 公開鍵の組み立て(x || y)
652-
uint8_t pubkey[64];
653-
memcpy(pubkey, e.public_key_x, 32);
654-
memcpy(pubkey + 32, e.public_key_y, 32);
655-
656-
// TempKey に秘密鍵をセット
657-
uint8_t key64[64]{};
658-
memcpy(key64, e.private_key, 32);
659-
EXPECT_TRUE(unit->writeNonce64(Destination::TempKey, key64));
660-
661-
// ECDH 実行
662-
uint8_t out[32]{};
663-
EXPECT_TRUE(unit->ECDHTempKey(out, pubkey));
664-
665-
// 結果照合
666-
EXPECT_EQ(memcmp(out, e.expected_shared_secret, 32), 0) << e.name;
667-
}
668-
669-
670-
TEST_P(TestATECC608B_TNGTLS, ECDH)
671-
{
672-
SCOPED_TRACE(ustr);
673-
674-
// StoredKey
675-
uint8_t device_pubkey[64]{};
676-
EXPECT_TRUE(unit->generateKey(device_pubkey, Slot::PrimaryPrivateKey)); // Slot 0
677-
678-
uint8_t shared_secret[32]{};
679-
EXPECT_TRUE(atecc.ECDHStoredKey(shared_secret, device_pubkey, Slot::PrimaryPrivateKey));
680-
// EXPECT_NE(shared_secret[0], 0); // dummy check
681-
682-
// TempKey
683-
uint8_t private_key[32]{};
684-
ASSERT_TRUE(unit->readRandomArray(private_key)); // ランダムな秘密鍵(仮)
685-
writeDataZone(8, private_key);
686-
uint8_t pubkey[64]{};
687-
ASSERT_TRUE(SHA256::generatePublicKey(pubkey, slot8); // 外部で公開鍵を生成(ソフト)
688-
689-
// TempKey に秘密鍵を渡す(64バイト: priv || zero)
690-
uint8_t key_buf[64]{};
691-
memcpy(key_buf, private_key, 32);
692-
ASSERT_TRUE(atecc.writeNonce64(Destination::TempKey, key_buf));
693-
694-
uint8_t shared_secret[32]{};
695-
ASSERT_TRUE(atecc.ECDHTempKey(shared_secret, pubkey));
696-
EXPECT_NE(shared_secret[0], 0);
697-
}
698-
699572

700573
TEST_P(TestATECC608B_TNGTLS, ECDHStoredKey)
701574
{
@@ -826,7 +699,6 @@ TEST_P(TestATECC608B_TNGTLS, ECDHTempKey)
826699
}
827700
}
828701

829-
830702
TEST_P(TestATECC608B_TNGTLS, GenKey)
831703
{
832704
SCOPED_TRACE(ustr);
@@ -973,7 +845,6 @@ TEST_P(TestATECC608B_TNGTLS, SignExternal)
973845
}
974846
}
975847
}
976-
#endif
977848

978849
TEST_P(TestATECC608B_TNGTLS, SignInternal)
979850
{

0 commit comments

Comments
 (0)