Software APIs
mod_exp_ibex_functest.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
8 #include "sw/device/silicon_creator/lib/sigverify/mod_exp_ibex.h"
9 
10 // The autogen rule that creates this header creates it in a directory named
11 // after the rule, then manipulates the include path in the
12 // cc_compilation_context to include that directory, so the compiler will find
13 // the version of this file matching the Bazel rule under test.
14 #include "sigverify_testvectors.h"
15 
16 // Index of the test vector currently under test
17 static uint32_t test_index;
18 
19 rom_error_t sigverify_mod_exp_ibex_test(void) {
20  sigverify_test_vector_t testvec = sigverify_tests[test_index];
21 
22  sigverify_rsa_buffer_t recovered_message;
23  rom_error_t err =
24  sigverify_mod_exp_ibex(&testvec.key, &testvec.sig, &recovered_message);
25  if (err != kErrorOk) {
26  if (testvec.valid) {
27  LOG_ERROR("Error on a valid signature.");
28  LOG_INFO("Test notes: %s", testvec.comment);
29  return err;
30  } else {
31  // If a test vector is marked invalid, it's okay to return an error
32  return kErrorOk;
33  }
34  }
35 
36  bool passed = memcmp(testvec.encoded_msg, recovered_message.data,
37  sizeof(testvec.encoded_msg)) == 0;
38  if (testvec.valid && !passed) {
39  // Signature verification failed when it was expected to pass.
40  LOG_ERROR("Failed to verify a valid signature.");
41  LOG_INFO("Test notes: %s", testvec.comment);
42  return kErrorUnknown;
43  } else if (!testvec.valid && passed) {
44  // Signature verification passed when it was expected to fail.
45  LOG_ERROR("Invalid signature passed verification.");
46  LOG_INFO("Test notes: %s", testvec.comment);
47  return kErrorUnknown;
48  }
49 
50  return kErrorOk;
51 }
52 
53 OTTF_DEFINE_TEST_CONFIG();
54 
55 bool test_main(void) {
56  status_t result = OK_STATUS();
57 
58  // The definition of `RULE_NAME` comes from the autogen Bazel rule.
59  LOG_INFO("Starting mod_exp_ibex_functest:%s", RULE_NAME);
60  for (uint32_t i = 0; i < SIGVERIFY_NUM_TESTS; i++) {
61  LOG_INFO("Starting test vector %d of %d...", i + 1, SIGVERIFY_NUM_TESTS);
62  test_index = i;
63  EXECUTE_TEST(result, sigverify_mod_exp_ibex_test);
64  }
65  LOG_INFO("Finished mod_exp_ibex_functest:%s", RULE_NAME);
66  return status_ok(result);
67 }