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/hash.h"
26 : #include "os.h"
27 :
28 : // *** sha256 ***
29 1 : bool hash_sha256_init(hash_sha256_ctx_t* ctx) {
30 1 : cx_sha256_init(ctx);
31 1 : return true;
32 : }
33 :
34 1 : bool hash_sha256_update(hash_sha256_ctx_t* ctx,
35 : const uint8_t* data,
36 : size_t len) {
37 1 : cx_hash((cx_hash_t*)(ctx), 0, (unsigned char*)data, len, NULL);
38 1 : return true;
39 : }
40 :
41 1 : bool hash_sha256_final(hash_sha256_ctx_t* ctx, uint8_t* out_hash) {
42 1 : cx_hash((cx_hash_t*)(ctx), CX_LAST, NULL, 0, out_hash);
43 1 : return true;
44 : }
45 :
46 : // *** sha256 with midstate support ***
47 1 : bool hash_sha256_ms_init(hash_sha256_ms_ctx_t* ctx) {
48 1 : sha256_init(ctx);
49 1 : return true;
50 : }
51 :
52 1 : bool hash_sha256_ms_midstate(hash_sha256_ms_ctx_t* ctx, uint8_t* midstate) {
53 1 : sha256_midstate(ctx, midstate);
54 1 : return true;
55 : }
56 :
57 1 : bool hash_sha256_ms_update(hash_sha256_ms_ctx_t* ctx,
58 : const uint8_t* data,
59 : size_t len) {
60 1 : sha256_update(ctx, data, len);
61 1 : return true;
62 : }
63 :
64 1 : bool hash_sha256_ms_final(hash_sha256_ms_ctx_t* ctx, uint8_t* out_hash) {
65 1 : sha256_final(ctx, out_hash);
66 1 : return true;
67 : }
68 :
69 : // *** keccak256 ***
70 1 : bool hash_keccak256_init(hash_keccak256_ctx_t* ctx) {
71 1 : cx_keccak_init(ctx, 256);
72 1 : return true;
73 : }
74 :
75 1 : bool hash_keccak256_update(hash_keccak256_ctx_t* ctx,
76 : const uint8_t* data,
77 : size_t len) {
78 1 : cx_hash((cx_hash_t*)(ctx), 0, (unsigned char*)data, len, NULL);
79 1 : return true;
80 : }
81 :
82 1 : bool hash_keccak256_final(hash_keccak256_ctx_t* ctx, uint8_t* out_hash) {
83 1 : cx_hash((cx_hash_t*)(ctx), CX_LAST, NULL, 0, out_hash);
84 1 : return true;
85 : }
|