/* Initialize board. */ static void board_init(void) { init_pmu(); init_timers(); init_interrupts(); init_trng(); init_runlevel(PERMISSION_MEDIUM); /* TODO(crosbug.com/p/49959): For now, leave flash WP unlocked */ GREG32(RBOX, EC_WP_L) = 1; /* Indication that firmware is running, for debug purposes. */ GREG32(PMU, PWRDN_SCRATCH16) = 0xCAFECAFE; }
int main(void) { board_init(); board_console_init(BOARD_DEBUG_BAUD); SysTick_Config(BOARD_SYSTICK_100MS); PRINTF("ubirch #1 r0.2 RSA/ECC encryption/signature benchmark\r\n"); if (init_ltc() != 0) PRINTF("No LTC, may crash\r\n"); if (init_trng() != 0) error("failed to initialize TRNG"); if (init_board_key(2048) != 0) error("failed to generate key pair"); if (init_recipient_public_key(recipient_pubkey, recipient_pubkey_length)) error("failed to load recipient public key"); byte cipher[256]; // 256 bytes is large enough to store 2048 bit RSA ciphertext word32 plaintextLength = strlen(plaintext); word32 cipherLength = sizeof(cipher); PRINTF("- signing message with board private key\r\n"); uint32_t total = 0; int signatureLength = 0; byte *signature = NULL; for (int i = 0; i < BENCHMARK_LOOPS; i++) { const uint32_t start = timer_read(); signatureLength = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, &board_rsa_key, sizeof(board_rsa_key)); signature = malloc((size_t) signatureLength); if (wc_SignatureGenerate( WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, (const byte *) plaintext, plaintextLength, signature, (word32 *) &signatureLength, &board_rsa_key, sizeof(board_rsa_key), &rng) != 0) error("failed to sign plain text message"); const uint32_t elapsed = timer_read() - start; total += elapsed; char loop_str[64]; sprintf(loop_str, "%d", i); timestamp(loop_str, elapsed); } timestamp("Average:", total / BENCHMARK_LOOPS); PRINTF("-- SIGNATURE\r\n"); PRINTF("- encrypting message\r\n"); total = 0; int r = -1; for (int i = 0; i < BENCHMARK_LOOPS; i++) { const uint32_t start = timer_read(); r = wc_RsaPublicEncrypt((const byte *) plaintext, plaintextLength, cipher, cipherLength, &recipient_public_key, &rng); if (r < 0) error("failed to encrypt message"); const uint32_t elapsed = timer_read() - start; total += elapsed; char loop_str[64]; sprintf(loop_str, "%d", i); timestamp(loop_str, elapsed); } timestamp("Average:", total / BENCHMARK_LOOPS); PRINTF("-- CIPHER (%d bytes)\r\n", r); wc_FreeRsaKey(&board_rsa_key); wc_FreeRsaKey(&recipient_public_key); PRINTF("THE END\r\n"); while (true) { uint8_t ch = (uint8_t) GETCHAR(); if (ch == '\r') PUTCHAR('\n'); PUTCHAR(ch); } }