Debian Codegraph @

crypto/sha3/shake.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 bytes;
6 use io;
7
8 // The rate or block size, in bytes, of a SHAKE128 XOF.
9 export def SHAKE_BLOCKSZ128: size = 168;
10
11 // The rate or block size, in bytes, of a SHAKE256 XOF.
12 export def SHAKE_BLOCKSZ256: size = 136;
13
14 // The domain separation byte **including** the first bit of padding for the
15 // SHAKE family of XOFs as specified in Appendix B.2 of FIPS 202.
16 def SHAKE_DS: u8 = 0x1f;
17
18 // Creates an [[io::stream]] that acts as a SHAKE128 eXtendable-Output Function
19 // (XOF). XOFs produce an output of unlimited size given some input data.
20 //
21 // let shake = sha3::shake128();
22 // // Absorb some input:
23 // io::write(&shake, strings::toutf8("hello "))!;
24 // io::write(&shake, strings::toutf8("hare!"))!;
25 //
26 // // Copying the XOF copies its state:
27 // let dup = shake;
28 //
29 // // Squeeze some output:
30 // let buf: []u8 = alloc([0...], 1337);
31 // io::read(&shake, buf[..42])!;
32 // io::read(&shake, buf[42..])!;
33 //
34 // // Absorbing (i.e. writing) more input is an error:
35 // assert(io::write(&shake, []) is errors::unsupported);
36 //
37 // All calls to [[io::write]] while absorbing and to [[io::read]] always
38 // write/read the full specified buffer and never fail. If this stream is used
39 // to write sensitive information, the caller should call [[io::close]] to erase
40 // sensitive data from memory after use; if not, the use of [[io::close]] is
41 // optional.
42 export fn shake128() xof = shake_init(SHAKE_BLOCKSZ128);
43
44 // Creates an [[io::stream]] that acts as a SHAKE256 eXtendable-Output Function
45 // (XOF). See [[shake128]] for more information.
46 export fn shake256() xof = shake_init(SHAKE_BLOCKSZ256);
47
48 export type xof = struct {
49 stream: io::stream,
50 e: sponge,
51 };
52
53 fn shake_init(rate: size) xof = {
54 return xof {
55 stream = &shake_absorb_vtable,
56 e = sponge {
57 rate = rate,
58 ds = SHAKE_DS,
59 s = [0...],
60 pos = 0,
61 },
62 };
63 };
64
65 const shake_absorb_vtable: io::vtable = io::vtable {
66 reader = &shake_first_read,
67 writer = &shake_write,
68 closer = &shake_close,
69 ...
70 };
71
72 const shake_squeeze_vtable: io::vtable = io::vtable {
73 reader = &shake_read,
74 closer = &shake_close,
75 ...
76 };
77
78 fn shake_first_read(st: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
79 let f = st: *xof;
80 // once a sponge starts being squeezed, it cannot absorb new data anymore.
81 f.stream = &shake_squeeze_vtable;
82 pad(&f.e);
83 squeeze(&f.e, buf);
84 return len(buf);
85 };
86
87 fn shake_read(st: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
88 let f = st: *xof;
89 squeeze(&f.e, buf);
90 return len(buf);
91 };
92
93 fn shake_write(st: *io::stream, buf: const []u8) (size | io::error) = {
94 let f = st: *xof;
95 absorb(&f.e, buf);
96 return len(buf);
97 };
98
99 fn shake_close(st: *io::stream) (void | io::error) = {
100 let f = st: *xof;
101 f.stream = &shake_absorb_vtable;
102 reset(&f.e);
103 };