1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#![no_std]
#![warn(missing_docs)]
use core::marker::PhantomData;
use ascon_core::{pad, State};
pub use digest::{self, Digest, ExtendableOutput, Reset, Update, XofReader};
use digest::{
block_buffer::Eager,
consts::{U32, U8},
core_api::{
AlgorithmName, Block, Buffer, BufferKindUser, CoreWrapper, ExtendableOutputCore,
FixedOutputCore, UpdateCore, XofReaderCore, XofReaderCoreWrapper,
},
crypto_common::BlockSizeUser,
HashMarker, Output, OutputSizeUser,
};
trait HashParameters {
const ROUNDS: usize;
const IV0: u64;
const IV1: u64;
const IV2: u64;
const IV3: u64;
const IV4: u64;
}
#[derive(Clone, Debug)]
struct Parameters;
impl HashParameters for Parameters {
const ROUNDS: usize = 12;
const IV0: u64 = 0xee9398aadb67f03d;
const IV1: u64 = 0x8bb21831c60f1002;
const IV2: u64 = 0xb48a92db98d5da62;
const IV3: u64 = 0x43189921b8f8e3e8;
const IV4: u64 = 0x348fa5c9d525e140;
}
#[derive(Clone, Debug)]
struct ParametersA;
impl HashParameters for ParametersA {
const ROUNDS: usize = 8;
const IV0: u64 = 0x01470194fc6528a6;
const IV1: u64 = 0x738ec38ac0adffa7;
const IV2: u64 = 0x2ec8e3296c76384c;
const IV3: u64 = 0xd6f6a54d7f52377d;
const IV4: u64 = 0xa13c42a223be8d87;
}
#[derive(Clone, Debug)]
struct ParametersXOF;
impl HashParameters for ParametersXOF {
const ROUNDS: usize = 12;
const IV0: u64 = 0xb57e273b814cd416;
const IV1: u64 = 0x2b51042562ae2420;
const IV2: u64 = 0x66a3a7768ddf2218;
const IV3: u64 = 0x5aad0a7a8153650c;
const IV4: u64 = 0x4f3e0e32539493b6;
}
#[derive(Clone, Debug)]
struct ParametersAXOF;
impl HashParameters for ParametersAXOF {
const ROUNDS: usize = 8;
const IV0: u64 = 0x44906568b77b9832;
const IV1: u64 = 0xcd8d6cae53455532;
const IV2: u64 = 0xf7b5212756422129;
const IV3: u64 = 0x246885e1de0d225b;
const IV4: u64 = 0xa8cb5ce33449973f;
}
#[derive(Clone, Debug)]
struct HashCore<P: HashParameters> {
state: State,
phantom: PhantomData<P>,
}
impl<P: HashParameters> HashCore<P> {
fn absorb_block(&mut self, block: &[u8; 8]) {
self.state[0] ^= u64::from_be_bytes(*block);
self.permute_state();
}
fn absorb_last_block(&mut self, block: &[u8]) {
debug_assert!(block.len() < 8);
let len = block.len();
if len > 0 {
let mut tmp = [0u8; 8];
tmp[0..len].copy_from_slice(block);
self.state[0] ^= u64::from_be_bytes(tmp);
}
self.state[0] ^= pad(len);
self.state.permute_12();
}
fn squeeze(&mut self, mut block: &mut [u8]) {
debug_assert_eq!(block.len() % 8, 0);
while block.len() > 8 {
block[..8].copy_from_slice(&u64::to_be_bytes(self.state[0]));
self.permute_state();
block = &mut block[8..];
}
block[..8].copy_from_slice(&u64::to_be_bytes(self.state[0]));
}
fn squeeze_block(&mut self) -> [u8; 8] {
let ret = u64::to_be_bytes(self.state[0]);
self.permute_state();
ret
}
#[inline(always)]
fn permute_state(&mut self) {
if P::ROUNDS == 12 {
self.state.permute_12();
} else if P::ROUNDS == 8 {
self.state.permute_8();
} else if P::ROUNDS == 6 {
self.state.permute_6();
}
}
}
impl<P: HashParameters> Default for HashCore<P> {
fn default() -> Self {
Self {
state: State::new(P::IV0, P::IV1, P::IV2, P::IV3, P::IV4),
phantom: PhantomData,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct AsconCore {
state: HashCore<Parameters>,
}
impl HashMarker for AsconCore {}
impl BlockSizeUser for AsconCore {
type BlockSize = U8;
}
impl BufferKindUser for AsconCore {
type BufferKind = Eager;
}
impl OutputSizeUser for AsconCore {
type OutputSize = U32;
}
impl UpdateCore for AsconCore {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
for block in blocks {
self.state.absorb_block(block.as_ref());
}
}
}
impl FixedOutputCore for AsconCore {
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
debug_assert!(buffer.get_pos() < 8);
self.state
.absorb_last_block(&buffer.get_data()[..buffer.get_pos()]);
self.state.squeeze(out);
}
}
impl Reset for AsconCore {
fn reset(&mut self) {
*self = Default::default();
}
}
impl AlgorithmName for AsconCore {
fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("AsconHash")
}
}
#[derive(Clone, Debug, Default)]
pub struct AsconACore {
state: HashCore<ParametersA>,
}
impl HashMarker for AsconACore {}
impl BlockSizeUser for AsconACore {
type BlockSize = U8;
}
impl BufferKindUser for AsconACore {
type BufferKind = Eager;
}
impl OutputSizeUser for AsconACore {
type OutputSize = U32;
}
impl UpdateCore for AsconACore {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
for block in blocks {
self.state.absorb_block(block.as_ref());
}
}
}
impl FixedOutputCore for AsconACore {
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
debug_assert!(buffer.get_pos() < 8);
self.state
.absorb_last_block(&buffer.get_data()[..buffer.get_pos()]);
self.state.squeeze(out);
}
}
impl Reset for AsconACore {
fn reset(&mut self) {
*self = Default::default();
}
}
impl AlgorithmName for AsconACore {
fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("AsconAHash")
}
}
#[derive(Clone, Debug, Default)]
pub struct AsconXOFCore {
state: HashCore<ParametersXOF>,
}
impl HashMarker for AsconXOFCore {}
impl BlockSizeUser for AsconXOFCore {
type BlockSize = U8;
}
impl BufferKindUser for AsconXOFCore {
type BufferKind = Eager;
}
impl UpdateCore for AsconXOFCore {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
for block in blocks {
self.state.absorb_block(block.as_ref());
}
}
}
#[derive(Clone, Debug)]
pub struct AsconXOFReaderCore {
hasher: HashCore<ParametersXOF>,
}
impl BlockSizeUser for AsconXOFReaderCore {
type BlockSize = U8;
}
impl XofReaderCore for AsconXOFReaderCore {
fn read_block(&mut self) -> Block<Self> {
self.hasher.squeeze_block().into()
}
}
impl ExtendableOutputCore for AsconXOFCore {
type ReaderCore = AsconXOFReaderCore;
fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore {
debug_assert!(buffer.get_pos() < 8);
self.state
.absorb_last_block(&buffer.get_data()[..buffer.get_pos()]);
Self::ReaderCore {
hasher: self.state.clone(),
}
}
}
impl Reset for AsconXOFCore {
fn reset(&mut self) {
*self = Default::default();
}
}
impl AlgorithmName for AsconXOFCore {
fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("AsconXOF")
}
}
#[derive(Clone, Debug, Default)]
pub struct AsconAXOFCore {
state: HashCore<ParametersAXOF>,
}
impl HashMarker for AsconAXOFCore {}
impl BlockSizeUser for AsconAXOFCore {
type BlockSize = U8;
}
impl BufferKindUser for AsconAXOFCore {
type BufferKind = Eager;
}
impl UpdateCore for AsconAXOFCore {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
for block in blocks {
self.state.absorb_block(block.as_ref());
}
}
}
#[derive(Clone, Debug)]
pub struct AsconAXOFReaderCore {
hasher: HashCore<ParametersAXOF>,
}
impl BlockSizeUser for AsconAXOFReaderCore {
type BlockSize = U8;
}
impl XofReaderCore for AsconAXOFReaderCore {
fn read_block(&mut self) -> Block<Self> {
self.hasher.squeeze_block().into()
}
}
impl ExtendableOutputCore for AsconAXOFCore {
type ReaderCore = AsconAXOFReaderCore;
fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore {
debug_assert!(buffer.get_pos() < 8);
self.state
.absorb_last_block(&buffer.get_data()[..buffer.get_pos()]);
Self::ReaderCore {
hasher: self.state.clone(),
}
}
}
impl Reset for AsconAXOFCore {
fn reset(&mut self) {
*self = Default::default();
}
}
impl AlgorithmName for AsconAXOFCore {
fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("AsconAXOF")
}
}
pub type AsconHash = CoreWrapper<AsconCore>;
pub type AsconAHash = CoreWrapper<AsconACore>;
pub type AsconXOF = CoreWrapper<AsconXOFCore>;
pub type AsconAXOFReader = XofReaderCoreWrapper<AsconAXOFReaderCore>;
pub type AsconAXOF = CoreWrapper<AsconAXOFCore>;
pub type AsconXOFReader = XofReaderCoreWrapper<AsconAXOFReaderCore>;