Line data Source code
1 : /**
2 : * The MIT License (MIT)
3 : *
4 : * Copyright (c) 2021 RSK Labs Ltd
5 : *
6 : * Permission is hereby granted, free of charge, to any person obtaining a copy
7 : * of this software and associated documentation files (the "Software"), to
8 : * deal in the Software without restriction, including without limitation the
9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 : * sell copies of the Software, and to permit persons to whom the Software is
11 : * furnished to do so, subject to the following conditions:
12 : *
13 : * The above copyright notice and this permission notice shall be included in
14 : * all copies or substantial portions of the Software.
15 : *
16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 : * IN THE SOFTWARE.
23 : */
24 :
25 : #include "hal/log.h"
26 : #include "hal/hash.h"
27 :
28 : // *** sha256 ***
29 2 : bool hash_sha256_init(hash_sha256_ctx_t* ctx) {
30 2 : mbedtls_sha256_init(ctx);
31 2 : if (mbedtls_sha256_starts_ret(ctx, 0) != 0) {
32 : LOG("Error initializing SHA256 context\n");
33 1 : mbedtls_sha256_free(ctx);
34 1 : return false;
35 : }
36 1 : return true;
37 : }
38 :
39 2 : bool hash_sha256_update(hash_sha256_ctx_t* ctx,
40 : const uint8_t* data,
41 : size_t len) {
42 2 : if (mbedtls_sha256_update_ret(ctx, data, len) != 0) {
43 : LOG("Error updating SHA256 context\n");
44 1 : mbedtls_sha256_free(ctx);
45 1 : return false;
46 : }
47 1 : return true;
48 : }
49 :
50 2 : bool hash_sha256_final(hash_sha256_ctx_t* ctx, uint8_t* out_hash) {
51 2 : if (mbedtls_sha256_finish_ret(ctx, out_hash) != 0) {
52 : LOG("Error finishing SHA256 computation\n");
53 1 : mbedtls_sha256_free(ctx);
54 1 : return false;
55 : }
56 1 : mbedtls_sha256_free(ctx);
57 1 : return true;
58 : }
59 :
60 : // *** sha256 with midstate support ***
61 1 : bool hash_sha256_ms_init(hash_sha256_ms_ctx_t* ctx) {
62 1 : sha256_init(ctx);
63 1 : return true;
64 : }
65 :
66 1 : bool hash_sha256_ms_midstate(hash_sha256_ms_ctx_t* ctx, uint8_t* midstate) {
67 1 : sha256_midstate(ctx, midstate);
68 1 : return true;
69 : }
70 :
71 1 : bool hash_sha256_ms_update(hash_sha256_ms_ctx_t* ctx,
72 : const uint8_t* data,
73 : size_t len) {
74 1 : sha256_update(ctx, data, len);
75 1 : return true;
76 : }
77 :
78 1 : bool hash_sha256_ms_final(hash_sha256_ms_ctx_t* ctx, uint8_t* out_hash) {
79 1 : sha256_final(ctx, out_hash);
80 1 : return true;
81 : }
82 :
83 : // *** keccak256 ***
84 18 : bool hash_keccak256_init(hash_keccak256_ctx_t* ctx) {
85 18 : keccak_init(ctx);
86 18 : return true;
87 : }
88 :
89 1157 : bool hash_keccak256_update(hash_keccak256_ctx_t* ctx,
90 : const uint8_t* data,
91 : size_t len) {
92 1157 : keccak_update(ctx, data, len);
93 1157 : return true;
94 : }
95 :
96 18 : bool hash_keccak256_final(hash_keccak256_ctx_t* ctx, uint8_t* out_hash) {
97 18 : keccak_final(ctx, out_hash);
98 18 : return true;
99 : }
|