1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 19:08:12 +00:00

bootloader: in self-test perform each step just once

This commit is contained in:
Pavol Rusnak 2017-07-19 11:39:04 +02:00
parent 6141782270
commit 980bae6cb5
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -344,87 +344,63 @@ static void hid_rx_callback(usbd_device *dev, uint8_t ep)
if (msg_id == 0x0020) { // SelfTest message (id 32) if (msg_id == 0x0020) { // SelfTest message (id 32)
// USB TEST // USB TEST
layoutProgress("TESTING USB ...", 0);
bool status_usb = (buf[9] == 0x0a) && (buf[10] == 53) && (0 == memcmp(buf + 11, "\x00\xFF\x55\xAA\x66\x99\x33\xCC" "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!" "\x00\xFF\x55\xAA\x66\x99\x33\xCC", 53)); bool status_usb = (buf[9] == 0x0a) && (buf[10] == 53) && (0 == memcmp(buf + 11, "\x00\xFF\x55\xAA\x66\x99\x33\xCC" "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!" "\x00\xFF\x55\xAA\x66\x99\x33\xCC", 53));
// RNG TEST // RNG TEST
layoutProgress("TESTING RNG ...", 250);
layoutProgress("TESTING RNG ...", 0);
uint32_t cnt[256]; uint32_t cnt[256];
memset(cnt, 0, sizeof(cnt)); memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < 10; i++) { for (int i = 0; i < (256 * 2000); i++) {
for (int j = 0; j < 256000; j++) { uint32_t r = random32();
uint32_t r = random32(); cnt[r & 0xFF]++;
cnt[r & 0xFF]++; cnt[(r >> 8) & 0xFF]++;
cnt[(r >> 8) & 0xFF]++; cnt[(r >> 16) & 0xFF]++;
cnt[(r >> 16) & 0xFF]++; cnt[(r >> 24) & 0xFF]++;
cnt[(r >> 24) & 0xFF]++;
}
layoutProgress("TESTING RNG ...", 100 + (i * 100));
} }
bool status_rng = true; bool status_rng = true;
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
status_rng = status_rng && (cnt[i] >= 39000) && (cnt[i] <= 41000); status_rng = status_rng && (cnt[i] >= 7600) && (cnt[i] <= 8400);
} }
// CPU TEST // CPU TEST
layoutProgress("TESTING CPU ...", 500);
layoutProgress("TESTING CPU ...", 0); // privkey : e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
// pubkey : 04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd
bool status_cpu = true; // 5b8dec5235a0fa8722476c7709c02559e3aa73aa03918ba2d492eea75abea235
// digest : c84a4cc264100070c8be2acf4072efaadaedfef3d6209c0fe26387e6b1262bbf
for (int i = 0; i < 10; i++) { // sig: : f7869c679bbed1817052affd0264ccc6486795f6d06d0c187651b8f3863670c8
// privkey : e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 // 2ccf89be32a53eb65ea7c007859783d46717986fead0833ec60c5729cdc4a9ee
// pubkey : 04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd bool status_cpu = (0 == ecdsa_verify_digest(&secp256k1,
// 5b8dec5235a0fa8722476c7709c02559e3aa73aa03918ba2d492eea75abea235 (const uint8_t *)"\x04\xa3\x4b\x99\xf2\x2c\x79\x0c\x4e\x36\xb2\xb3\xc2\xc3\x5a\x36\xdb\x06\x22\x6e\x41\xc6\x92\xfc\x82\xb8\xb5\x6a\xc1\xc5\x40\xc5\xbd\x5b\x8d\xec\x52\x35\xa0\xfa\x87\x22\x47\x6c\x77\x09\xc0\x25\x59\xe3\xaa\x73\xaa\x03\x91\x8b\xa2\xd4\x92\xee\xa7\x5a\xbe\xa2\x35",
// digest : c84a4cc264100070c8be2acf4072efaadaedfef3d6209c0fe26387e6b1262bbf (const uint8_t *)"\xf7\x86\x9c\x67\x9b\xbe\xd1\x81\x70\x52\xaf\xfd\x02\x64\xcc\xc6\x48\x67\x95\xf6\xd0\x6d\x0c\x18\x76\x51\xb8\xf3\x86\x36\x70\xc8\x2c\xcf\x89\xbe\x32\xa5\x3e\xb6\x5e\xa7\xc0\x07\x85\x97\x83\xd4\x67\x17\x98\x6f\xea\xd0\x83\x3e\xc6\x0c\x57\x29\xcd\xc4\xa9\xee",
// sig: : f7869c679bbed1817052affd0264ccc6486795f6d06d0c187651b8f3863670c8 (const uint8_t *)"\xc8\x4a\x4c\xc2\x64\x10\x00\x70\xc8\xbe\x2a\xcf\x40\x72\xef\xaa\xda\xed\xfe\xf3\xd6\x20\x9c\x0f\xe2\x63\x87\xe6\xb1\x26\x2b\xbf"));
// 2ccf89be32a53eb65ea7c007859783d46717986fead0833ec60c5729cdc4a9ee
status_cpu = status_cpu && (0 == ecdsa_verify_digest(&secp256k1,
(const uint8_t *)"\x04\xa3\x4b\x99\xf2\x2c\x79\x0c\x4e\x36\xb2\xb3\xc2\xc3\x5a\x36\xdb\x06\x22\x6e\x41\xc6\x92\xfc\x82\xb8\xb5\x6a\xc1\xc5\x40\xc5\xbd\x5b\x8d\xec\x52\x35\xa0\xfa\x87\x22\x47\x6c\x77\x09\xc0\x25\x59\xe3\xaa\x73\xaa\x03\x91\x8b\xa2\xd4\x92\xee\xa7\x5a\xbe\xa2\x35",
(const uint8_t *)"\xf7\x86\x9c\x67\x9b\xbe\xd1\x81\x70\x52\xaf\xfd\x02\x64\xcc\xc6\x48\x67\x95\xf6\xd0\x6d\x0c\x18\x76\x51\xb8\xf3\x86\x36\x70\xc8\x2c\xcf\x89\xbe\x32\xa5\x3e\xb6\x5e\xa7\xc0\x07\x85\x97\x83\xd4\x67\x17\x98\x6f\xea\xd0\x83\x3e\xc6\x0c\x57\x29\xcd\xc4\xa9\xee",
(const uint8_t *)"\xc8\x4a\x4c\xc2\x64\x10\x00\x70\xc8\xbe\x2a\xcf\x40\x72\xef\xaa\xda\xed\xfe\xf3\xd6\x20\x9c\x0f\xe2\x63\x87\xe6\xb1\x26\x2b\xbf"));
layoutProgress("TESTING CPU ...", 100 + (i * 100));
}
// FLASH TEST // FLASH TEST
layoutProgress("TESTING FLASH ...", 750);
const uint32_t patterns[] = { 0x00000000, 0xFFFFFFFF, 0x55555555, 0xAAAAAAAA, 0x66666666, 0x99999999, 0x33333333, 0xCCCCCCCC };
layoutProgress("TESTING FLASH ...", 0);
// backup metadata // backup metadata
backup_metadata(meta_backup); backup_metadata(meta_backup);
// write/read test patterns // write test pattern
sha256_Init(&ctx); erase_metadata_sectors();
flash_unlock();
for (int p = 0; p < 8; p++) { for (int i = 0; i < FLASH_META_LEN / 4; i++) {
erase_metadata_sectors(); flash_program_word(FLASH_META_START + i * 4, 0x3C695A0F);
flash_unlock();
for (int i = 0; i < FLASH_META_LEN / 4; i++) {
flash_program_word(FLASH_META_START + i * 4, patterns[p]);
}
flash_lock();
sha256_Update(&ctx, (unsigned char *)FLASH_META_START, FLASH_META_LEN);
layoutProgress("TESTING FLASH ...", 100 + (p * 100));
} }
flash_lock();
// compute hash of written test pattern
uint8_t hash[32];
sha256_Raw((unsigned char *)FLASH_META_START, FLASH_META_LEN, hash);
// copy metadata back // copy metadata back
erase_metadata_sectors(); erase_metadata_sectors();
restore_metadata(meta_backup); restore_metadata(meta_backup);
// compare against known hash // compare against known hash computed via the following Python3 script:
uint8_t hash[32]; // hashlib.sha256(binascii.unhexlify('0F5A693C' * 8192)).hexdigest()
sha256_Final(&ctx, hash); bool status_flash = (0 == memcmp(hash, "\xa6\xc2\x25\xa4\x76\xa1\xde\x76\x09\xe0\xb0\x07\xf8\xe2\x5a\xec\x1d\x75\x8d\x5c\x36\xc8\x4a\x6b\x75\x4e\xd5\x3d\xe6\x99\x97\x64", 32));
layoutProgress("TESTING FLASH ...", 1000);
// hash computed via the following Python3 script:
// hashlib.sha256(b''.join([binascii.unhexlify(c * 2 * 32768) for c in '0F5A693C'])).hexdigest()
bool status_flash = (0 == memcmp(hash, "\x49\x46\xe9\xa5\xf4\xc2\x57\xe9\xcf\xd1\x88\x78\xe9\x66\x9b\x0d\xcd\x4e\x82\x41\xb3\x9c\xee\xb7\x2c\x1d\x14\x4a\xe1\xe4\xcb\xd7", 32));
bool status_all = status_usb && status_rng && status_cpu && status_flash; bool status_all = status_usb && status_rng && status_cpu && status_flash;