Software APIs
sha2.h
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 // Derived from code in the SPHINCS+ reference implementation (CC0 license):
6 // https://github.com/sphincs/sphincsplus/blob/7ec789ace6874d875f4bb84cb61b81155398167e/ref/sha2.h
7 
8 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_SIGVERIFY_SPHINCSPLUS_SHA2_H_
9 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_SIGVERIFY_SPHINCSPLUS_SHA2_H_
10 
12 #include "sw/device/silicon_creator/lib/error.h"
13 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/params.h"
14 
15 static_assert(kSpxSha512 == 0,
16  "Parameter sets requiring SHA-512 are not supported.");
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 enum {
23  /**
24  * Number of bytes in the SHA256 block size (512 bits).
25  */
26  kSpxSha2BlockNumBytes = 512 / 8,
27  /**
28  * Number of words in the SHA256 block size.
29  */
30  kSpxSha2BlockNumWords = kSpxSha2BlockNumBytes / sizeof(uint32_t),
31  /**
32  * Total number of bytes in the SHA256 address representation.
33  */
34  kSpxSha256AddrBytes = 22,
35 };
36 
37 /**
38  * Mask-generation function (MGF1) using SHA256.
39  *
40  * MGF1 is defined in IETF RFC 8017, section B.2.1. Its inputs are a seed and a
41  * length, and it produces a mask value of that length from the seed using an
42  * underlying hash function (SHA256 in this case).
43  *
44  * The input and output buffers are allowed to overlap in any configuration.
45  *
46  * @param in Input buffer.
47  * @param in_len Input buffer length in 32-bit words.
48  * @param out_len Requested output length in 32-bit words.
49  * @param[out] out Output buffer.
50  */
51 void mgf1_sha256(const uint32_t *in, size_t in_len, size_t out_len,
52  uint32_t *out);
53 
54 #ifdef __cplusplus
55 }
56 #endif
57 
58 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_SIGVERIFY_SPHINCSPLUS_SHA2_H_