Software APIs
mgf1_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 <stdint.h>
6 
8 #include "sw/device/lib/testing/test_framework/check.h"
10 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/hash.h"
11 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/params.h"
12 #include "sw/device/silicon_creator/lib/sigverify/sphincsplus/sha2.h"
13 
14 OTTF_DEFINE_TEST_CONFIG();
15 
16 // Length of runtime input and output for SPX+ verify with sha2-128s parameter
17 // set.
18 enum {
19  kMgf1OutputWords = 8,
20  kMgf1InputWords = 16,
21 };
22 
23 // Test input, same length as runtime input for SPX+ verify.
24 static const uint32_t kTestInput[kMgf1InputWords] = {
25  0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, 0x13121110, 0x17161514,
26  0x1b1a1918, 0x1f1e1d1c, 0x23222120, 0x27262524, 0x2b2a2928, 0x2f2e2d2c,
27  0x33323130, 0x37363534, 0x3b3a3938, 0x3f3e3d3c,
28 };
29 
30 // Expected result, generated with PyCryptoDome as follows:
31 // >>> from Crypto.Signature.pss import MGF1
32 // >>> from Crypto.Hash import SHA256
33 // >>> x = bytearray([i for i in range(0x40)])
34 // >>> MGF1(x, 32, SHA256).hex()
35 // '926a33148745e412d1b93cfd3829786181935e93ebabb9ae5d42222fd2dfc8a2'
36 static const uint32_t kExpectedOutput[kMgf1OutputWords] = {
37  0x14336a92, 0x12e44587, 0xfd3cb9d1, 0x61782938,
38  0x935e9381, 0xaeb9abeb, 0x2f22425d, 0xa2c8dfd2,
39 };
40 
42 static rom_error_t mgf1_test(void) {
43  // Run spx_hash_initialize() to configure the HMAC block.
44  spx_ctx_t ctx;
45  HARDENED_RETURN_IF_ERROR(spx_hash_initialize(&ctx));
46 
47  uint32_t actual_output[kMgf1OutputWords];
48  mgf1_sha256(kTestInput, ARRAYSIZE(kTestInput), ARRAYSIZE(actual_output),
49  actual_output);
50  CHECK_ARRAYS_EQ(actual_output, kExpectedOutput, ARRAYSIZE(kExpectedOutput));
51  return kErrorOk;
52 }
53 
54 bool test_main(void) {
55  status_t result = OK_STATUS();
56  EXECUTE_TEST(result, mgf1_test);
57  return status_ok(result);
58 }