/* * mgmt-timing.c * ------------- * Timing tests for RSA signing and sub-components thereof. * * Copyright (c) 2018-2019, NORDUnet A/S All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the NORDUnet nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #define HAL_OK CMSIS_HAL_OK #include "stm-init.h" #include "stm-uart.h" #include "mgmt-cli.h" #include "mgmt-timing.h" #undef HAL_OK /* Include the code we're going to be testing, since a lot of the * functions we want to test are static. */ #define HAL_OK LIBHAL_OK #define STATIC_PKEY_STATE_BLOCKS 256 #define HAL_STATIC_PKEY_STATE_BLOCKS STATIC_PKEY_STATE_BLOCKS #include "rpc_pkey.c" #include "rsa.c" static void hal_rsa_clear_blinding_cache(void) { memset(&bfc, 0, sizeof(bfc)); } #undef HAL_OK /* * Message and der-encoded key are derived from libhal/tests/parallel-signatures.py. */ /* * m = pkcs1_hash_and_pad("Hamsters'R'Us") */ static const uint8_t m[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20, 0xe6, 0xbe, 0x9e, 0x24, 0x79, 0xbc, 0xad, 0xc7, 0x22, 0x81, 0x72, 0xe2, 0xc3, 0x3a, 0xdb, 0x8b, 0x3f, 0xfe, 0xfb, 0x3b, 0x87, 0x23, 0x76, 0xbd, 0x60, 0xfb, 0xe5, 0x8f, 0x8d, 0x3c, 0xbf, 0x44 }; /* * d = key_table["rsa_2048"].exportKey(format = "DER", pkcs = 8) */ static const uint8_t d[] = { 0x30, 0x82, 0x04, 0xbe, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x04, 0xa8, 0x30, 0x82, 0x04, 0xa4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb1, 0xbb, 0xea, 0xea, 0xcc, 0xa1, 0x0d, 0x70, 0xff, 0x38, 0xc5, 0x40, 0xb8, 0xba, 0x0c, 0x71, 0xe1, 0x90, 0x2d, 0x42, 0x22, 0x7b, 0x37, 0xd2, 0x85, 0x5a, 0x85, 0x7f, 0x23, 0x2a, 0x00, 0x38, 0xea, 0x84, 0x54, 0x56, 0x59, 0xba, 0x27, 0x9f, 0x45, 0xd5, 0xf5, 0x99, 0x32, 0xa5, 0x4e, 0x08, 0x6f, 0xd9, 0xfa, 0x3e, 0xd2, 0xc0, 0x4c, 0xd6, 0x9e, 0x7e, 0x1a, 0x6c, 0x52, 0x52, 0x38, 0xb3, 0xed, 0xb4, 0x61, 0x39, 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 uint32_t start = HAL_GetTick() #define TIMER_STOP(op) \ uint32_t elapsed = HAL_GetTick() - start; \ uint32_t per = 1000 * elapsed / iterations; \ cli_print(cli, "%ld.%03lds total, %ld.%03ldms per " op, \ elapsed / 1000, elapsed % 1000, per / 1000, per % 1000) hal_pkey_handle_t pkey = { HAL_HANDLE_NONE }; hal_uuid_t name; static int cmd_timing_hal_rpc_pkey_sign(struct cli_def *cli, const char *command, char *argv[], int argc) { #define SETUP_hal_rpc_pkey_sign() \ hal_error_t err; \ int keep; \ \ if (argc < 2) { \ cli_print(cli, "Syntax: %s [keep]", command); \ return CLI_ERROR; \ } \ keep = (argc > 2); \ \ const int iterations = atoi(argv[1]); \ if (iterations == 0) { \ cli_print(cli, "invalid value of iterations: %s", argv[1]); \ return CLI_ERROR; \ } \ \ const hal_client_handle_t client = { 0 }; \ const hal_session_handle_t session = { 0 }; \ if (pkey.handle == HAL_HANDLE_NONE) { \ if ((err = hal_rpc_login(client, HAL_USER_NORMAL, argv[0], \ strlen(argv[0]))) != LIBHAL_OK) { \ cli_print(cli, "login: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ \ if ((err = hal_rpc_pkey_load(client, session, &pkey, &name, \ d, sizeof(d), \ HAL_KEY_FLAG_USAGE_DIGITALSIGNATURE)) \ != LIBHAL_OK) { \ cli_print(cli, "hal_rpc_pkey_load: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ } SETUP_hal_rpc_pkey_sign(); const hal_hash_handle_t hash = { 0 }; uint8_t sig[1024]; size_t sig_len; TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = hal_rpc_pkey_sign(pkey, hash, m, sizeof(m), sig, &sig_len, sizeof(sig))) != LIBHAL_OK) { cli_print(cli, "hal_rpc_pkey_sign: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("signature"); #define TEARDOWN() \ if (!keep) { \ if ((err = hal_rpc_pkey_delete(pkey)) != LIBHAL_OK) { \ cli_print(cli, "hal_rpc_pkey_delete: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ pkey.handle = HAL_HANDLE_NONE; \ \ if ((err = hal_rpc_logout(client)) != LIBHAL_OK) { \ cli_print(cli, "logout: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ } \ return CLI_OK TEARDOWN(); } static int cmd_timing_pkey_local_sign_rsa(struct cli_def *cli, const char *command, char *argv[], int argc) { /* do one signature to do the precalc, then read back the rewritten key */ #define SETUP_pkey_local_sign_rsa() \ SETUP_hal_rpc_pkey_sign(); \ \ hal_pkey_slot_t slot = { \ .client = client, \ .session = session, \ .pkey = pkey, \ .type = HAL_KEY_TYPE_RSA_PRIVATE, \ .curve = HAL_CURVE_NONE, \ .flags = HAL_KEY_FLAG_USAGE_DIGITALSIGNATURE, \ .name = name \ }; \ const hal_hash_handle_t hash = { 0 }; \ uint8_t sig[1024]; \ size_t sig_len; \ if ((err = hal_rpc_pkey_sign(pkey, hash, m, sizeof(m), \ sig, &sig_len, sizeof(sig))) != LIBHAL_OK) { \ cli_print(cli, "hal_rpc_pkey_sign: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ uint8_t der[HAL_KS_WRAPPED_KEYSIZE]; \ size_t der_len; \ if ((err = hal_ks_fetch(hal_ks_volatile, &slot, \ der, &der_len, sizeof(der))) != LIBHAL_OK) { \ cli_print(cli, "hal_ks_fetch: %s", hal_error_string(err)); \ return CLI_ERROR; \ } SETUP_pkey_local_sign_rsa(); uint8_t keybuf[hal_rsa_key_t_size]; TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = pkey_local_sign_rsa(&slot, keybuf, sizeof(keybuf), der, der_len, hash, m, sizeof(m), sig, &sig_len, sizeof(sig))) != LIBHAL_OK) { cli_print(cli, "pkey_local_sign_rsa: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("signature"); TEARDOWN(); } static int cmd_timing_hal_rsa_private_key_from_der(struct cli_def *cli, const char *command, char *argv[], int argc) { SETUP_pkey_local_sign_rsa(); hal_rsa_key_t *key = NULL; uint8_t keybuf[hal_rsa_key_t_size]; TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = hal_rsa_private_key_from_der(&key, keybuf, sizeof(keybuf), der, der_len)) != LIBHAL_OK) { cli_print(cli, "hal_rsa_private_key_from_der: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("read"); TEARDOWN(); } static int cmd_timing_hal_rsa_decrypt(struct cli_def *cli, const char *command, char *argv[], int argc) { #define SETUP_hal_rsa_decrypt() \ SETUP_pkey_local_sign_rsa(); \ \ hal_rsa_key_t *key = NULL; \ uint8_t keybuf[hal_rsa_key_t_size]; \ if ((err = hal_rsa_private_key_from_der(&key, keybuf, sizeof(keybuf), \ der, der_len)) != LIBHAL_OK) { \ cli_print(cli, "hal_rsa_private_key_from_der: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ if ((err = hal_rsa_key_get_modulus(key, NULL, &sig_len, 0)) != LIBHAL_OK) { \ cli_print(cli, "hal_rsa_key_get_modulus: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ \ uint8_t msg[sig_len]; \ if ((err = pkcs1_5_pad(m, sizeof(m), msg, sig_len, 0x01)) != LIBHAL_OK) { \ cli_print(cli, "pkcs1_5_pad: %s", hal_error_string(err)); \ return CLI_ERROR; \ } SETUP_hal_rsa_decrypt(); TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = hal_rsa_decrypt(NULL, NULL, key, msg, sig_len, sig, sig_len)) != LIBHAL_OK) { cli_print(cli, "hal_rsa_decrypt: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("signature"); TEARDOWN(); } static int cmd_timing_rsa_crt(struct cli_def *cli, const char *command, char *argv[], int argc) { #define SETUP_rsa_crt() \ SETUP_hal_rsa_decrypt(); \ \ fp_int in[1] = INIT_FP_INT; \ fp_read_unsigned_bin(in, msg, sig_len) SETUP_rsa_crt(); fp_int out[1] = INIT_FP_INT; TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = rsa_crt(NULL, NULL, key, in, out)) != LIBHAL_OK) { cli_print(cli, "rsa_crt: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("signature"); TEARDOWN(); } static int cmd_timing_modexp2(struct cli_def *cli, const char *command, char *argv[], int argc) { #define SETUP_modexp2() \ SETUP_rsa_crt(); \ \ fp_int bf[1] = INIT_FP_INT; \ fp_int ubf[1] = INIT_FP_INT; \ \ if ((err = create_blinding_factors(key, bf, ubf)) != LIBHAL_OK) { \ cli_print(cli, "create_blinding_factors: %s", hal_error_string(err)); \ return CLI_ERROR; \ } \ if (fp_mulmod(in, bf, (fp_int *)key->n, in) != FP_OKAY) { \ cli_print(cli, "fp_mulmod error"); \ return CLI_ERROR; \ } SETUP_modexp2(); fp_int m1[1] = INIT_FP_INT; fp_int m2[1] = INIT_FP_INT; TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = modexp2(0, in, NULL, key->dP, key->p, m1, key->pC, sizeof(key->pC), key->pF, sizeof(key->pF), NULL, key->dQ, key->q, m2, key->qC, sizeof(key->qC), key->qF, sizeof(key->qF))) != LIBHAL_OK) { cli_print(cli, "modexp2: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("signature"); TEARDOWN(); } static int cmd_timing_hal_modexp2(struct cli_def *cli, const char *command, char *argv[], int argc) { SETUP_modexp2(); const size_t msg_len = (fp_unsigned_bin_size(unconst_fp_int(in)) + 3) & ~3; const size_t exp1_len = (fp_unsigned_bin_size(unconst_fp_int(key->dP)) + 3) & ~3; const size_t mod1_len = (fp_unsigned_bin_size(unconst_fp_int(key->p)) + 3) & ~3; const size_t exp2_len = (fp_unsigned_bin_size(unconst_fp_int(key->dQ)) + 3) & ~3; const size_t mod2_len = (fp_unsigned_bin_size(unconst_fp_int(key->q)) + 3) & ~3; uint8_t msgbuf[msg_len]; uint8_t expbuf1[exp1_len], modbuf1[mod1_len], resbuf1[mod1_len]; uint8_t expbuf2[exp2_len], modbuf2[mod2_len], resbuf2[mod2_len]; hal_modexp_arg_t args1 = { .core = NULL, .msg = msgbuf, .msg_len = sizeof(msgbuf), .exp = expbuf1, .exp_len = sizeof(expbuf1), .mod = modbuf1, .mod_len = sizeof(modbuf1), .result = resbuf1, .result_len = sizeof(resbuf1), .coeff = key->pC, .coeff_len = sizeof(key->pC), .mont = key->pF, .mont_len = sizeof(key->pF) }; hal_modexp_arg_t args2 = { .core = NULL, .msg = msgbuf, .msg_len = sizeof(msgbuf), .exp = expbuf2, .exp_len = sizeof(expbuf2), .mod = modbuf2, .mod_len = sizeof(modbuf2), .result = resbuf2, .result_len = sizeof(resbuf2), .coeff = key->qC, .coeff_len = sizeof(key->qC), .mont = key->qF, .mont_len = sizeof(key->qF) }; if ((err = unpack_fp(in, msgbuf, sizeof(msgbuf))) != LIBHAL_OK || (err = unpack_fp(key->dP, expbuf1, sizeof(expbuf1))) != LIBHAL_OK || (err = unpack_fp(key->p, modbuf1, sizeof(modbuf1))) != LIBHAL_OK || (err = unpack_fp(key->dQ, expbuf2, sizeof(expbuf2))) != LIBHAL_OK || (err = unpack_fp(key->q, modbuf2, sizeof(modbuf2))) != LIBHAL_OK) { cli_print(cli, "unpack_fp: %s", hal_error_string(err)); return CLI_ERROR; } TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = hal_modexp2(0, &args1, &args2)) != LIBHAL_OK) { cli_print(cli, "hal_modexp2: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("signature"); TEARDOWN(); } static int cmd_timing_hal_ks_fetch(struct cli_def *cli, const char *command, char *argv[], int argc) { SETUP_pkey_local_sign_rsa(); TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = hal_ks_fetch(hal_ks_volatile, &slot, der, &der_len, sizeof(der))) != LIBHAL_OK) { cli_print(cli, "hal_ks_fetch: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("fetch"); TEARDOWN(); } static int cmd_timing_hal_mkm_get_kek(struct cli_def *cli, const char *command, char *argv[], int argc) { SETUP_hal_rpc_pkey_sign(); uint8_t kek[KEK_LENGTH]; size_t kek_len; TIMER_START(); for (int i = 0; i < iterations; ++i) { if ((err = hal_mkm_get_kek(kek, &kek_len, sizeof(kek))) != LIBHAL_OK) { cli_print(cli, "hal_mkm_get_kek: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("fetch"); TEARDOWN(); } static int cmd_timing_hal_aes_keyunwrap(struct cli_def *cli, const char *command, char *argv[], int argc) { SETUP_pkey_local_sign_rsa(); uint8_t kek[KEK_LENGTH]; size_t kek_len; if ((err = hal_mkm_get_kek(kek, &kek_len, sizeof(kek))) != LIBHAL_OK) { cli_print(cli, "hal_mkm_get_kek: %s", hal_error_string(err)); return CLI_ERROR; } uint8_t wrapped[HAL_KS_WRAPPED_KEYSIZE]; size_t wrapped_len = sizeof(wrapped); if ((err = hal_aes_keywrap(NULL, kek, kek_len, der, der_len, wrapped, &wrapped_len)) != LIBHAL_OK) { cli_print(cli, "hal_aes_keywrap: %s", hal_error_string(err)); return CLI_ERROR; } uint8_t unwrapped[HAL_KS_WRAPPED_KEYSIZE]; size_t unwrapped_len; TIMER_START(); for (int i = 0; i < iterations; ++i) { unwrapped_len = sizeof(unwrapped); if ((err = hal_aes_keyunwrap(NULL, kek, kek_len, wrapped, wrapped_len, unwrapped, &unwrapped_len)) != LIBHAL_OK) { cli_print(cli, "hal_aes_unkeywrap: %s", hal_error_string(err)); return CLI_ERROR; } } TIMER_STOP("unwrap"); TEARDOWN(); } static int cmd_blinding(struct cli_def *cli, const char *command, char *argv[], int argc) { extern void hal_rsa_clear_blinding_cache(void); command = command; if (argc == 1) { if (strcmp(argv[0], "on") == 0) return hal_rsa_set_blinding(1), CLI_OK; else if (strcmp(argv[0], "off") == 0) return hal_rsa_set_blinding(0), CLI_OK; else if (strcmp(argv[0], "clear") == 0) return hal_rsa_clear_blinding_cache(), CLI_OK; } cli_print(cli, "Syntax: blinding "); return CLI_ERROR; } void configure_cli_timing(struct cli_def *cli) { cli_register_command(cli, NULL, "blinding", cmd_blinding, 0, 0, "Toggle use of RSA blinding"); struct cli_command *c_timing = cli_register_command(cli, NULL, "timing", NULL, 0, 0, NULL); cli_register_command(cli, c_timing, "hal_rpc_pkey_sign", cmd_timing_hal_rpc_pkey_sign, 0, 0, "Timing test for hal_rpc_pkey_sign"); cli_register_command(cli, c_timing, "hal_ks_fetch", cmd_timing_hal_ks_fetch, 0, 0, "Timing test for hal_ks_fetch"); cli_register_command(cli, c_timing, "hal_mkm_get_kek", cmd_timing_hal_mkm_get_kek, 0, 0, "Timing test for hal_mkm_get_kek"); cli_register_command(cli, c_timing, "hal_aes_keyunwrap", cmd_timing_hal_aes_keyunwrap, 0, 0, "Timing test for hal_aes_keyunwrap"); cli_register_command(cli, c_timing, "pkey_local_sign_rsa", cmd_timing_pkey_local_sign_rsa, 0, 0, "Timing test for pkey_local_sign_rsa"); cli_register_command(cli, c_timing, "hal_rsa_private_key_from_der", cmd_timing_hal_rsa_private_key_from_der, 0, 0, "Timing test for hal_rsa_private_key_from_der"); cli_register_command(cli, c_timing, "hal_rsa_decrypt", cmd_timing_hal_rsa_decrypt, 0, 0, "Timing test for hal_rsa_decrypt"); cli_register_command(cli, c_timing, "rsa_crt", cmd_timing_rsa_crt, 0, 0, "Timing test for rsa_crt"); cli_register_command(cli, c_timing, "modexp2", cmd_timing_modexp2, 0, 0, "Timing test for modexp2"); cli_register_command(cli, c_timing, "hal_modexp2", cmd_timing_hal_modexp2, 0, 0, "Timing test for hal_modexp2"); }