Software APIs
fors_test.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 
5 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/fors.h"
6 
7 #include <stdint.h>
8 
11 #include "sw/device/lib/testing/test_framework/check.h"
13 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/hash.h"
14 
15 OTTF_DEFINE_TEST_CONFIG();
16 
17 // Test signature, message, and address. Populate before running test.
18 static uint32_t kTestSig[kSpxForsWords] = {0};
19 static uint8_t kTestMsg[kSpxForsMsgBytes] = {0};
20 static spx_addr_t kTestAddr = {.addr = {0}};
21 
22 // Test context.
23 static spx_ctx_t kTestCtx = {
24  .pub_seed =
25  {
26  0xf3f2f1f0,
27  0xf7f6f5f4,
28  0xfbfaf9f8,
29  0xfffefdfc,
30  },
31 };
32 
33 // Test data generated with a third-party implementation of SPHINCS+.
34 static uint32_t kExpectedPk[kSpxNWords] = {
35  0xa8862a36,
36  0x193a3511,
37  0x4e18a287,
38  0xe0006e36,
39 };
40 
42 static rom_error_t pk_from_sig_test(void) {
43  // Initialize the KMAC block.
44  RETURN_IF_ERROR(spx_hash_initialize(&kTestCtx));
45 
46  // Extract the public key from the signature.
47  uint32_t actual_pk[kSpxNWords];
48  fors_pk_from_sig(kTestSig, kTestMsg, &kTestCtx, &kTestAddr, actual_pk);
49 
50  // Check results.
51  CHECK_ARRAYS_EQ(actual_pk, kExpectedPk, kSpxNWords);
52  return kErrorOk;
53 }
54 
55 bool test_main(void) {
56  status_t result = OK_STATUS();
57 
58  // Populate signature with {0, 1, 2, 3, ... }.
59  unsigned char *test_sig_bytes = (unsigned char *)kTestSig;
60  for (size_t i = 0; i < kSpxForsBytes; i++) {
61  test_sig_bytes[i] = i & 255;
62  }
63 
64  // Populate message with { ..., 3, 2, 1, 0}.
65  for (size_t i = 0; i < kSpxForsMsgBytes; i++) {
66  kTestMsg[i] = (kSpxForsMsgBytes - i) & 255;
67  }
68 
69  // Populate address.
70  spx_addr_layer_set(&kTestAddr, 0xa3);
71  spx_addr_tree_set(&kTestAddr, 0xafaeadacabaaa9a8);
72  spx_addr_type_set(&kTestAddr, kSpxAddrTypeForsTree);
73  spx_addr_keypair_set(&kTestAddr, 0xb4b5b6b7);
74 
75  EXECUTE_TEST(result, pk_from_sig_test);
76 
77  return status_ok(result);
78 }