Software APIs
thash_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/thash.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 message. Size should always be a multiple of kSpxNWords.
18 static uint32_t kTestMsg[2 * kSpxNWords] = {
19  0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
20  0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c,
21 };
22 
23 // Test context.
24 static spx_ctx_t kTestCtx = {
25  .pub_seed =
26  {
27  0x23222120,
28  0x27262524,
29  0x2b2a2928,
30  0x2f2e2d2c,
31  },
32 };
33 
34 // Test address. Populate before using.
35 static spx_addr_t kTestAddr = {.addr = {0}};
36 
37 // This test data was generated using a third-party implementation of SPHINCS+
38 // (slh-dsa-py).
39 static uint32_t kExpectedResult[kSpxNWords] = {
40  0x3ea2808a,
41  0x090d2a51,
42  0x9193afcc,
43  0x93a58ed8,
44 };
45 
47 static rom_error_t thash_test(void) {
48  RETURN_IF_ERROR(spx_hash_initialize(&kTestCtx));
49 
50  size_t msg_blocks = sizeof(kTestMsg) / kSpxN;
51  uint32_t result[kSpxNWords];
52  thash(kTestMsg, msg_blocks, &kTestCtx, &kTestAddr, result);
53 
54  CHECK_ARRAYS_EQ(result, kExpectedResult, kSpxNWords);
55  return kErrorOk;
56 }
57 
58 bool test_main(void) {
59  status_t result = OK_STATUS();
60 
61  spx_addr_layer_set(&kTestAddr, 0xa3);
62  spx_addr_tree_set(&kTestAddr, 0xafaeadacabaaa9a8);
63  spx_addr_type_set(&kTestAddr, kSpxAddrTypeForsTree);
64  spx_addr_keypair_set(&kTestAddr, 0xb4b5b6b7);
65 
66  EXECUTE_TEST(result, thash_test);
67  return status_ok(result);
68 }