9 #include "sw/device/silicon_creator/lib/drivers/hmac.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/sha2.h"
14 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/utils.h"
20 kSpxTreeBits = kSpxTreeHeight * (kSpxD - 1),
24 kSpxTreeBytes = (kSpxTreeBits + 7) / 8,
28 kSpxLeafBits = kSpxTreeHeight,
32 kSpxLeafBytes = (kSpxLeafBits + 7) / 8,
36 kSpxDigestBytes = kSpxForsMsgBytes + kSpxTreeBytes + kSpxLeafBytes,
42 kSpxDigestWords = (kSpxDigestBytes +
sizeof(uint32_t) - 1) /
sizeof(uint32_t),
47 "For given height and depth, 64 bits cannot represent all subtrees.");
50 "For the given height, 32 bits is not large enough for a leaf index.");
52 inline rom_error_t spx_hash_initialize(
spx_ctx_t *ctx) {
53 hmac_sha256_configure(
true);
57 hmac_sha256_update_words(ctx->
pub_seed, kSpxNWords);
58 uint32_t padding[kSpxSha2BlockNumWords - kSpxNWords];
59 memset(padding, 0,
sizeof(padding));
60 hmac_sha256_update_words(padding,
ARRAYSIZE(padding));
65 rom_error_t spx_hash_message(
66 const uint32_t *R,
const uint32_t *pk,
const uint8_t *msg_prefix_1,
67 size_t msg_prefix_1_len,
const uint8_t *msg_prefix_2,
68 size_t msg_prefix_2_len,
const uint8_t *msg_prefix_3,
69 size_t msg_prefix_3_len,
const uint8_t *msg,
size_t msg_len,
70 uint8_t *digest, uint64_t *tree, uint32_t *leaf_idx) {
71 uint32_t seed[kSpxDigestWords + (2 * kSpxNWords)] = {0};
74 memcpy(&seed[kSpxNWords], pk, kSpxN);
76 hmac_sha256_update_words(R, kSpxNWords);
77 hmac_sha256_update_words(pk, kSpxPkWords);
78 hmac_sha256_update(msg_prefix_1, msg_prefix_1_len);
79 hmac_sha256_update(msg_prefix_2, msg_prefix_2_len);
80 hmac_sha256_update(msg_prefix_3, msg_prefix_3_len);
81 hmac_sha256_update(msg, msg_len);
82 hmac_sha256_process();
83 hmac_sha256_final_truncated(&seed[2 * kSpxNWords], kSpxDigestWords);
85 uint32_t buf[kSpxDigestWords] = {0};
88 unsigned char *bufp = (
unsigned char *)buf;
89 memcpy(digest, bufp, kSpxForsMsgBytes);
90 bufp += kSpxForsMsgBytes;
92 if (kSpxTreeBits == 0) {
95 *tree = spx_utils_bytes_to_u64(bufp, kSpxTreeBytes);
96 *tree &= (~(uint64_t)0) >> (64 - kSpxTreeBits);
97 bufp += kSpxTreeBytes;
100 *leaf_idx = (uint32_t)spx_utils_bytes_to_u64(bufp, kSpxLeafBytes);
101 *leaf_idx &= (~(uint32_t)0) >> (32 - kSpxLeafBits);