Debian Codegraph @

crypto/sha3/sha3.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3 // (c) 2023 Nicholas Rodrigues Lordello <[email protected]>
4
5 use hash;
6 use io;
7
8 // The size, in bytes, of a SHA3-224 digest.
9 export def SZ224: size = 28;
10
11 // The rate or block size, in bytes, of a SHA3-224 hash.
12 export def BLOCKSZ224: size = 144;
13
14 // The size, in bytes, of a SHA3-256 digest.
15 export def SZ256: size = 32;
16
17 // The rate or block size, in bytes, of a SHA3-256 hash.
18 export def BLOCKSZ256: size = 136;
19
20 // The size, in bytes, of a SHA3-384 digest.
21 export def SZ384: size = 48;
22
23 // The rate or block size, in bytes, of a SHA3-384 hash.
24 export def BLOCKSZ384: size = 104;
25
26 // The size, in bytes, of a SHA3-512 digest.
27 export def SZ512: size = 64;
28
29 // The rate or block size, in bytes, of a SHA3-512 hash.
30 export def BLOCKSZ512: size = 72;
31
32 // The domain separation byte **including** the first bit of padding for SHA3
33 // family of hashes as specified in Appendix B.2 of FIPS 202.
34 def SHA3_DS: u8 = 0x06;
35
36 // Creates a [[hash::hash]] which computes a SHA3-224 hash. If this function is
37 // used to hash sensitive information, the caller should call [[hash::close]] to
38 // erase sensitive data from memory after use; if not, the use of
39 // [[hash::close]] is optional.
40 export fn sha3_224() hash = sha3_init(SZ224, BLOCKSZ224);
41
42 // Creates a [[hash::hash]] which computes a SHA3-256 hash. If this function is
43 // used to hash sensitive information, the caller should call [[hash::close]] to
44 // erase sensitive data from memory after use; if not, the use of
45 // [[hash::close]] is optional.
46 export fn sha3_256() hash = sha3_init(SZ256, BLOCKSZ256);
47
48 // Creates a [[hash::hash]] which computes a SHA3-384 hash. If this function is
49 // used to hash sensitive information, the caller should call [[hash::close]] to
50 // erase sensitive data from memory after use; if not, the use of
51 // [[hash::close]] is optional.
52 export fn sha3_384() hash = sha3_init(SZ384, BLOCKSZ384);
53
54 // Creates a [[hash::hash]] which computes a SHA3-512 hash. If this function is
55 // used to hash sensitive information, the caller should call [[hash::close]] to
56 // erase sensitive data from memory after use; if not, the use of
57 // [[hash::close]] is optional.
58 export fn sha3_512() hash = sha3_init(SZ512, BLOCKSZ512);
59
60 export type hash = struct {
61 hash::hash,
62 e: sponge,
63 };
64
65 fn sha3_init(sz: size, rate: size) hash = {
66 return hash {
67 stream = &sha3_vtable,
68 sum = &sha3_sum,
69 reset = &sha3_reset,
70 sz = sz,
71 // The rate is an approximation of the block size as defined in
72 // the [[hash::hash]] type. In fact, because of the domain
73 // separation bits, there is no possible value that fits the
74 // description, as there will **always** be padding required.
75 bsz = rate,
76 e = sponge {
77 rate = rate,
78 ds = SHA3_DS,
79 s = [0...],
80 pos = 0,
81 },
82 };
83 };
84
85 const sha3_vtable: io::vtable = io::vtable {
86 writer = &sha3_write,
87 closer = &sha3_close,
88 ...
89 };
90
91 fn sha3_write(st: *io::stream, buf: const []u8) (size | io::error) = {
92 let h = st: *hash;
93 absorb(&h.e, buf);
94 return len(buf);
95 };
96
97 fn sha3_close(st: *io::stream) (void | io::error) = {
98 let h = st: *hash;
99 reset(&h.e);
100 };
101
102 fn sha3_sum(h: *hash::hash, buf: []u8) void = {
103 let h = h: *hash;
104 // Make a copy of the sponge; this way, the caller can request the
105 // current sum but also continue updating the state of the hash with new
106 // data afterwards.
107 let e = h.e;
108 pad(&e);
109 squeeze(&e, buf[..h.sz]);
110 };
111
112 fn sha3_reset(h: *hash::hash) void = {
113 let h = h: *hash;
114 reset(&h.e);
115 };