Software APIs
otcrypto_hash_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 <stdbool.h>
6 #include <stdint.h>
7 
9 #include "sw/device/lib/base/status.h"
13 
14 // This test checks that the static-linked `otcrypto` library is usable.
15 
16 // From: http://www.abrahamlincolnonline.org/lincoln/speeches/gettysburg.htm
17 static const char kGettysburgPrelude[] =
18  "Four score and seven years ago our fathers brought forth on this "
19  "continent, a new nation, conceived in Liberty, and dedicated to the "
20  "proposition that all men are created equal.";
21 
22 // The following shell command will produce the sha256sum and convert the
23 // digest into valid C hexadecimal constants:
24 //
25 // $ echo -n "Four score and seven years ago our fathers brought forth on this
26 // continent, a new nation, conceived in Liberty, and dedicated to the
27 // proposition that all men are created equal." |
28 // sha256sum - | cut -f1 -d' ' | sed -e "s/../0x&, /g"
29 //
30 static const uint8_t kGettysburgDigest[] = {
31  0x1e, 0x6f, 0xd4, 0x03, 0x0f, 0x90, 0x34, 0xcd, 0x77, 0x57, 0x08,
32  0xa3, 0x96, 0xc3, 0x24, 0xed, 0x42, 0x0e, 0xc5, 0x87, 0xeb, 0x3d,
33  0xd4, 0x33, 0xe2, 0x9f, 0x6a, 0xc0, 0x8b, 0x8c, 0xc7, 0xba,
34 };
35 
36 status_t hash_test(void) {
37  uint32_t digest_content[8];
39  otcrypto_hash_digest_t digest = {
40  .mode = kOtcryptoHashModeSha256,
41  .len = ARRAYSIZE(digest_content),
42  .data = digest_content,
43  };
45  .len = sizeof(kGettysburgPrelude) - 1,
46  .data = (const uint8_t *)kGettysburgPrelude,
47  };
48 
49  TRY(otcrypto_hash_init(&ctx, kOtcryptoHashModeSha256));
50  TRY(otcrypto_hash_update(&ctx, buf));
51  TRY(otcrypto_hash_final(&ctx, digest));
52 
53  TRY_CHECK_ARRAYS_EQ((const uint8_t *)digest.data, kGettysburgDigest,
54  ARRAYSIZE(kGettysburgDigest));
55  return OK_STATUS();
56 }
57 
58 OTTF_DEFINE_TEST_CONFIG();
59 
60 bool test_main(void) {
61  status_t result = OK_STATUS();
62  result = hash_test();
63  return status_ok(result);
64 }