Software APIs
sigverify_dynamic_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 
6 #include "sw/device/lib/testing/test_framework/check.h"
9 #include "sw/device/silicon_creator/lib/drivers/hmac.h"
10 #include "sw/device/silicon_creator/lib/sigverify/rsa_verify.h"
11 #include "sw/device/silicon_creator/lib/sigverify/sigverify.h"
12 
13 // The autogen rule that creates this header creates it in a directory named
14 // after the rule, then manipulates the include path in the
15 // cc_compilation_context to include that directory, so the compiler will find
16 // the version of this file matching the Bazel rule under test.
17 #include "sigverify_testvectors.h"
18 
19 // Index of the test vector currently under test
20 static uint32_t test_index;
21 
22 rom_error_t sigverify_test(void) {
23  sigverify_test_vector_t testvec = sigverify_tests[test_index];
24 
25  // Extract the digest from the encoded message
26  hmac_digest_t digest;
27  memcpy(digest.digest, testvec.encoded_msg,
28  kHmacDigestNumWords * sizeof(uint32_t));
29 
30  uint32_t flash_exec = 0;
31  rom_error_t result = sigverify_rsa_verify(&testvec.sig, &testvec.key, &digest,
32  kLcStateRma, &flash_exec);
33 
34  rom_error_t test_result;
35  if (testvec.valid) {
36  CHECK(flash_exec == kSigverifyRsaSuccess);
37  if (result == kErrorOk) {
38  test_result = kErrorOk;
39  } else {
40  // Signature verification failed when it was expected to pass.
41  LOG_ERROR("Error while attempting to verify a valid signature.");
42  LOG_INFO("Test notes: %s", testvec.comment);
43  test_result = result;
44  }
45  } else {
46  CHECK(flash_exec == UINT32_MAX);
47  if (result == kErrorOk) {
48  // Signature verification passed when it was expected to fail.
49  LOG_ERROR("Invalid signature passed verification.");
50  LOG_INFO("Test notes: %s", testvec.comment);
51  test_result = kErrorUnknown;
52  } else {
53  test_result = kErrorOk;
54  }
55  }
56 
57  return test_result;
58 }
59 
60 OTTF_DEFINE_TEST_CONFIG();
61 
62 bool test_main(void) {
63  status_t result = OK_STATUS();
64 
65  // The definition of `RULE_NAME` comes from the autogen Bazel rule.
66  LOG_INFO("Starting sigverify_dynamic_functest:%s", RULE_NAME);
67  for (uint32_t i = 0; i < SIGVERIFY_NUM_TESTS; i++) {
68  LOG_INFO("Starting test vector %d of %d...", i + 1, SIGVERIFY_NUM_TESTS);
69  test_index = i;
70  EXECUTE_TEST(result, sigverify_test);
71  }
72  LOG_INFO("Finished sigverify_dynamic_functest:%s", RULE_NAME);
73  return status_ok(result);
74 }