9 #include "sw/device/silicon_creator/lib/drivers/kmac.h"
10 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/address.h"
11 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/hash.h"
12 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/params.h"
13 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/utils.h"
19 kSpxTreeBits = kSpxTreeHeight * (kSpxD - 1),
23 kSpxTreeBytes = (kSpxTreeBits + 7) / 8,
27 kSpxLeafBits = kSpxTreeHeight,
31 kSpxLeafBytes = (kSpxLeafBits + 7) / 8,
35 kSpxDigestBytes = kSpxForsMsgBytes + kSpxTreeBytes + kSpxLeafBytes,
41 kSpxDigestWords = (kSpxDigestBytes +
sizeof(uint32_t) - 1) /
sizeof(uint32_t),
46 "For given height and depth, 64 bits cannot represent all subtrees.");
49 "For the given height, 32 bits is not large enough for a leaf index.");
51 rom_error_t spx_hash_initialize(
spx_ctx_t *ctx) {
52 return kmac_shake256_configure();
56 rom_error_t spx_hash_message(
const uint32_t *R,
const uint32_t *pk,
57 const uint8_t *msg_prefix_1,
58 size_t msg_prefix_1_len,
59 const uint8_t *msg_prefix_2,
60 size_t msg_prefix_2_len,
const uint8_t *msg,
61 size_t msg_len, uint8_t *digest, uint64_t *tree,
63 uint32_t buf[kSpxDigestWords] = {0};
64 unsigned char *bufp = (
unsigned char *)buf;
66 HARDENED_RETURN_IF_ERROR(kmac_shake256_start());
67 kmac_shake256_absorb_words(R, kSpxNWords);
68 kmac_shake256_absorb_words(pk, kSpxPkWords);
69 kmac_shake256_absorb(msg_prefix_1, msg_prefix_1_len);
70 kmac_shake256_absorb(msg_prefix_2, msg_prefix_2_len);
71 kmac_shake256_absorb(msg, msg_len);
72 kmac_shake256_squeeze_start();
73 HARDENED_RETURN_IF_ERROR(kmac_shake256_squeeze_end(buf, kSpxDigestWords));
75 memcpy(digest, bufp, kSpxForsMsgBytes);
76 bufp += kSpxForsMsgBytes;
78 if (kSpxTreeBits == 0) {
81 *tree = spx_utils_bytes_to_u64(bufp, kSpxTreeBytes);
82 *tree &= (~(uint64_t)0) >> (64 - kSpxTreeBits);
83 bufp += kSpxTreeBytes;
86 *leaf_idx = (uint32_t)spx_utils_bytes_to_u64(bufp, kSpxLeafBytes);
87 *leaf_idx &= (~(uint32_t)0) >> (32 - kSpxLeafBits);