cryptoxide 0.5.1

pure implementation of various common modern cryptographic algorithms, WASM compatible
Documentation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! An implementation of the SHA-2 cryptographic hash algorithms.
//!
//! There are 6 standard algorithms specified in the SHA-2 standard:
//!
//!  * `Sha224`, which is the 32-bit `Sha256` algorithm with the result truncated to 224 bits.
//!  * `Sha256`, which is the 32-bit `Sha256` algorithm.
//!  * `Sha384`, which is the 64-bit `Sha512` algorithm with the result truncated to 384 bits.
//!  * `Sha512`, which is the 64-bit `Sha512` algorithm.
//!  * `Sha512Trunc224`, which is the 64-bit `Sha512` algorithm with the result truncated to 224 bits.
//!  * `Sha512Trunc256`, which is the 64-bit `Sha512` algorithm with the result truncated to 256 bits.
//!
//! Algorithmically, there are only 2 core algorithms: `Sha256` and `Sha512`.
//! All other algorithms are just applications of these with different initial hash
//! values, and truncated to different digest bit lengths.
//!
//! # Usage
//!
//! An example of using `Sha256` is:
//!
//! ```rust
//! use cryptoxide::hashing::sha2::Sha256;
//!
//! // create a Sha256 object
//! let mut context = Sha256::new();
//!
//! // write input message
//! context.update_mut(b"hello world");
//!
//! // read hash digest
//! let output = context.finalize();
//! ```
//!
//! An example of using `Sha512` is:
//!
//! ```rust
//! use cryptoxide::hashing::sha2::Sha512;
//!
//! // create a Sha512 object
//! let mut context = Sha512::new();
//!
//! // write input message
//! context.update_mut(b"hello world");
//!
//! // read hash digest
//! let output = context.finalize();
//! ```

mod eng256;
mod eng512;
mod impl256;
mod impl512;
mod initials;

use crate::cryptoutil::FixedBuffer;
use initials::*;

macro_rules! digest {
    (256 $name:ident, $ctxname:ident, $output_fn: ident, $output_bits:expr, $state:ident) => {
        digest!(
            @internal
            $name,
            $ctxname,
            Engine256,
            $output_fn,
            $output_bits,
            64,
            $state
        );
    };
    (512 $name:ident, $ctxname:ident, $output_fn:ident, $output_bits:expr, $state:ident) => {
        digest!(
            @internal
            $name,
            $ctxname,
            Engine512,
            $output_fn,
            $output_bits,
            128,
            $state
        );
    };
    (@internal $name:ident, $ctxname:ident, $init:ident, $output_fn:ident, $output_bits:expr, $block_size:literal, $state: ident) => {
        /// Hash Algorithm
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub struct $name;

        impl $name {
            /// Output of the hashing algorithm in bits
            pub const OUTPUT_BITS: usize = $output_bits;

            /// The block size in bytes of the algorithm, which is the number of bytes the algorithm typically buffer
            /// before calling its compression function
            pub const BLOCK_BYTES: usize = $block_size;

            /// Create a new context for this algorithm
            pub fn new() -> $ctxname {
                $ctxname::new()
            }
        }

        /// The hash algorithm context
        #[derive(Clone)]
        pub struct $ctxname {
            engine: $init,
        }

        impl $ctxname {
            /// Create a new hashing algorithm context
            pub const fn new() -> Self {
                Self {
                    engine: $init::new(&$state),
                }
            }

            /// Update in-place the hashing state by adding the input bytes slice into the state
            ///
            /// For the immutable version see [`update`]
            pub fn update_mut(&mut self, input: &[u8]) {
                self.engine.input(input)
            }

            /// Update the hashing state by adding the input bytes slice into the state
            pub fn update(mut self, input: &[u8]) -> Self {
                self.engine.input(input);
                self
            }

            /// Finalize the context and return an array of bytes
            ///
            /// The context is consumed by this function, to prevent buggy reuse.
            ///
            /// If the context need to be kept before finalizing, the user can clone the Context
            pub fn finalize(mut self) -> [u8; $output_bits / 8] {
                let mut out = [0; $output_bits / 8];
                self.engine.finish();
                self.engine.state.$output_fn(&mut out);
                out
            }

            /// Same as `finalize` but do not consume the context, but instead
            /// reset it in a ready to use state.
            pub fn finalize_reset(&mut self) -> [u8; $output_bits / 8] {
                let mut out = [0; $output_bits / 8];
                self.engine.finish();
                self.engine.state.$output_fn(&mut out);
                self.reset();
                out
            }

            /// Reset the context state, as if a new context had been created
            pub fn reset(&mut self) {
                self.engine.reset(&$state);
            }
        }
    };
}

// A structure that keeps track of the state of the Sha-512 operation and contains the logic
// necessary to perform the final calculations.
#[derive(Clone)]
struct Engine512 {
    processed_bytes: u128,
    buffer: FixedBuffer<128>,
    state: eng512::Engine,
}

impl Engine512 {
    const fn new(h: &[u64; eng512::STATE_LEN]) -> Engine512 {
        Engine512 {
            processed_bytes: 0,
            buffer: FixedBuffer::new(),
            state: eng512::Engine::new(h),
        }
    }

    fn reset(&mut self, h: &[u64; eng512::STATE_LEN]) {
        self.processed_bytes = 0;
        self.buffer.reset();
        self.state.reset(h);
    }

    fn input(&mut self, input: &[u8]) {
        self.processed_bytes += input.len() as u128;
        let self_state = &mut self.state;
        self.buffer.input(input, |input| self_state.blocks(input));
    }

    fn finish(&mut self) {
        let self_state = &mut self.state;
        self.buffer
            .standard_padding(16, |input| self_state.blocks(input));
        *self.buffer.next::<16>() = (self.processed_bytes << 3).to_be_bytes();
        self.state.blocks(self.buffer.full_buffer());
    }
}

// A structure that keeps track of the state of the Sha-256 operation and contains the logic
// necessary to perform the final calculations.
#[derive(Clone)]
struct Engine256 {
    processed_bytes: u64,
    buffer: FixedBuffer<64>,
    state: eng256::Engine,
    finished: bool,
}

impl Engine256 {
    const fn new(h: &[u32; eng256::STATE_LEN]) -> Engine256 {
        Engine256 {
            processed_bytes: 0,
            buffer: FixedBuffer::new(),
            state: eng256::Engine::new(h),
            finished: false,
        }
    }

    fn reset(&mut self, h: &[u32; eng256::STATE_LEN]) {
        self.processed_bytes = 0;
        self.buffer.reset();
        self.state.reset(h);
        self.finished = false;
    }

    fn input(&mut self, input: &[u8]) {
        assert!(!self.finished);
        self.processed_bytes += input.len() as u64;
        let self_state = &mut self.state;
        self.buffer.input(input, |input| self_state.blocks(input));
    }

    fn finish(&mut self) {
        if self.finished {
            return;
        }

        let self_state = &mut self.state;
        self.buffer
            .standard_padding(8, |input| self_state.blocks(input));
        *self.buffer.next::<8>() = (self.processed_bytes << 3).to_be_bytes();
        self.state.blocks(self.buffer.full_buffer());

        self.finished = true;
    }
}

digest!(512 Sha512, Context512, output_512bits_at, 512, H512);
digest!(512 Sha384, Context384, output_384bits_at, 384, H384);
digest!(
    512
    Sha512Trunc256,
    Context512_256,
    output_256bits_at,
    256,
    H512_TRUNC_256
);
digest!(
    512
    Sha512Trunc224,
    Context512_224,
    output_224bits_at,
    224,
    H512_TRUNC_224
);
digest!(256 Sha256, Context256, output_256bits_at, 256, H256);
digest!(256 Sha224, Context224, output_224bits_at, 224, H224);

#[cfg(test)]
mod tests {
    use super::super::tests::{test_hashing, Test};
    use super::*;

    #[test]
    fn test_sha512() {
        // Examples from wikipedia
        let tests = [
            Test {
                input: b"",
                output: [
                    0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6,
                    0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4,
                    0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2,
                    0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd,
                    0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog",
                output: [
                    0x07, 0xe5, 0x47, 0xd9, 0x58, 0x6f, 0x6a, 0x73, 0xf7, 0x3f, 0xba, 0xc0, 0x43,
                    0x5e, 0xd7, 0x69, 0x51, 0x21, 0x8f, 0xb7, 0xd0, 0xc8, 0xd7, 0x88, 0xa3, 0x09,
                    0xd7, 0x85, 0x43, 0x6b, 0xbb, 0x64, 0x2e, 0x93, 0xa2, 0x52, 0xa9, 0x54, 0xf2,
                    0x39, 0x12, 0x54, 0x7d, 0x1e, 0x8a, 0x3b, 0x5e, 0xd6, 0xe1, 0xbf, 0xd7, 0x09,
                    0x78, 0x21, 0x23, 0x3f, 0xa0, 0x53, 0x8f, 0x3d, 0xb8, 0x54, 0xfe, 0xe6,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog.",
                output: [
                    0x91, 0xea, 0x12, 0x45, 0xf2, 0x0d, 0x46, 0xae, 0x9a, 0x03, 0x7a, 0x98, 0x9f,
                    0x54, 0xf1, 0xf7, 0x90, 0xf0, 0xa4, 0x76, 0x07, 0xee, 0xb8, 0xa1, 0x4d, 0x12,
                    0x89, 0x0c, 0xea, 0x77, 0xa1, 0xbb, 0xc6, 0xc7, 0xed, 0x9c, 0xf2, 0x05, 0xe6,
                    0x7b, 0x7f, 0x2b, 0x8f, 0xd4, 0xc7, 0xdf, 0xd3, 0xa7, 0xa8, 0x61, 0x7e, 0x45,
                    0xf3, 0xc4, 0x63, 0xd4, 0x81, 0xc7, 0xe5, 0x86, 0xc3, 0x9a, 0xc1, 0xed,
                ],
            },
        ];
        test_hashing(
            &tests,
            Sha512,
            |_| Context512::new(),
            |ctx, input| ctx.update(input),
            |ctx, input| ctx.update_mut(input),
            |ctx| ctx.finalize(),
            |ctx| ctx.finalize_reset(),
            |ctx| ctx.reset(),
        )
    }

    #[test]
    fn test_sha384() {
        // Examples from wikipedia
        let tests = [
            Test {
                input: b"",
                output: [
                    0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1,
                    0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c,
                    0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65,
                    0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog",
                output: [
                    0xca, 0x73, 0x7f, 0x10, 0x14, 0xa4, 0x8f, 0x4c, 0x0b, 0x6d, 0xd4, 0x3c, 0xb1,
                    0x77, 0xb0, 0xaf, 0xd9, 0xe5, 0x16, 0x93, 0x67, 0x54, 0x4c, 0x49, 0x40, 0x11,
                    0xe3, 0x31, 0x7d, 0xbf, 0x9a, 0x50, 0x9c, 0xb1, 0xe5, 0xdc, 0x1e, 0x85, 0xa9,
                    0x41, 0xbb, 0xee, 0x3d, 0x7f, 0x2a, 0xfb, 0xc9, 0xb1,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog.",
                output: [
                    0xed, 0x89, 0x24, 0x81, 0xd8, 0x27, 0x2c, 0xa6, 0xdf, 0x37, 0x0b, 0xf7, 0x06,
                    0xe4, 0xd7, 0xbc, 0x1b, 0x57, 0x39, 0xfa, 0x21, 0x77, 0xaa, 0xe6, 0xc5, 0x0e,
                    0x94, 0x66, 0x78, 0x71, 0x8f, 0xc6, 0x7a, 0x7a, 0xf2, 0x81, 0x9a, 0x02, 0x1c,
                    0x2f, 0xc3, 0x4e, 0x91, 0xbd, 0xb6, 0x34, 0x09, 0xd7,
                ],
            },
        ];
        test_hashing(
            &tests,
            Sha384,
            |_| Context384::new(),
            |ctx, input| ctx.update(input),
            |ctx, input| ctx.update_mut(input),
            |ctx| ctx.finalize(),
            |ctx| ctx.finalize_reset(),
            |ctx| ctx.reset(),
        )
    }

    #[test]
    fn test_sha512_256() {
        // Examples from wikipedia
        let tests = [
            Test {
                input: b"",
                output: [
                    0xc6, 0x72, 0xb8, 0xd1, 0xef, 0x56, 0xed, 0x28, 0xab, 0x87, 0xc3, 0x62, 0x2c,
                    0x51, 0x14, 0x06, 0x9b, 0xdd, 0x3a, 0xd7, 0xb8, 0xf9, 0x73, 0x74, 0x98, 0xd0,
                    0xc0, 0x1e, 0xce, 0xf0, 0x96, 0x7a,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog",
                output: [
                    0xdd, 0x9d, 0x67, 0xb3, 0x71, 0x51, 0x9c, 0x33, 0x9e, 0xd8, 0xdb, 0xd2, 0x5a,
                    0xf9, 0x0e, 0x97, 0x6a, 0x1e, 0xee, 0xfd, 0x4a, 0xd3, 0xd8, 0x89, 0x00, 0x5e,
                    0x53, 0x2f, 0xc5, 0xbe, 0xf0, 0x4d,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog.",
                output: [
                    0x15, 0x46, 0x74, 0x18, 0x40, 0xf8, 0xa4, 0x92, 0xb9, 0x59, 0xd9, 0xb8, 0xb2,
                    0x34, 0x4b, 0x9b, 0x0e, 0xb5, 0x1b, 0x00, 0x4b, 0xba, 0x35, 0xc0, 0xae, 0xba,
                    0xac, 0x86, 0xd4, 0x52, 0x64, 0xc3,
                ],
            },
        ];
        test_hashing(
            &tests,
            Sha512Trunc256,
            |_| Context512_256::new(),
            |ctx, input| ctx.update(input),
            |ctx, input| ctx.update_mut(input),
            |ctx| ctx.finalize(),
            |ctx| ctx.finalize_reset(),
            |ctx| ctx.reset(),
        )
    }

    #[test]
    fn test_sha512_224() {
        // Examples from wikipedia
        let tests = [
            Test {
                input: b"",
                output: [
                    0x6e, 0xd0, 0xdd, 0x02, 0x80, 0x6f, 0xa8, 0x9e, 0x25, 0xde, 0x06, 0x0c, 0x19,
                    0xd3, 0xac, 0x86, 0xca, 0xbb, 0x87, 0xd6, 0xa0, 0xdd, 0xd0, 0x5c, 0x33, 0x3b,
                    0x84, 0xf4,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog",
                output: [
                    0x94, 0x4c, 0xd2, 0x84, 0x7f, 0xb5, 0x45, 0x58, 0xd4, 0x77, 0x5d, 0xb0, 0x48,
                    0x5a, 0x50, 0x00, 0x31, 0x11, 0xc8, 0xe5, 0xda, 0xa6, 0x3f, 0xe7, 0x22, 0xc6,
                    0xaa, 0x37,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog.",
                output: [
                    0x6d, 0x6a, 0x92, 0x79, 0x49, 0x5e, 0xc4, 0x06, 0x17, 0x69, 0x75, 0x2e, 0x7f,
                    0xf9, 0xc6, 0x8b, 0x6b, 0x0b, 0x3c, 0x5a, 0x28, 0x1b, 0x79, 0x17, 0xce, 0x05,
                    0x72, 0xde,
                ],
            },
        ];
        test_hashing(
            &tests,
            Sha512Trunc224,
            |_| Context512_224::new(),
            |ctx, input| ctx.update(input),
            |ctx, input| ctx.update_mut(input),
            |ctx| ctx.finalize(),
            |ctx| ctx.finalize_reset(),
            |ctx| ctx.reset(),
        )
    }

    #[test]
    fn test_sha256() {
        // Examples from wikipedia
        let tests = [
            Test {
                input: b"",
                output: [
                    0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99,
                    0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95,
                    0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog",
                output: [
                    0xd7, 0xa8, 0xfb, 0xb3, 0x07, 0xd7, 0x80, 0x94, 0x69, 0xca, 0x9a, 0xbc, 0xb0,
                    0x08, 0x2e, 0x4f, 0x8d, 0x56, 0x51, 0xe4, 0x6d, 0x3c, 0xdb, 0x76, 0x2d, 0x02,
                    0xd0, 0xbf, 0x37, 0xc9, 0xe5, 0x92,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog.",
                output: [
                    0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9,
                    0xb6, 0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65,
                    0x44, 0x8c, 0x86, 0x35, 0xfb, 0x6c,
                ],
            },
        ];
        test_hashing(
            &tests,
            Sha256,
            |_| Context256::new(),
            |ctx, input| ctx.update(input),
            |ctx, input| ctx.update_mut(input),
            |ctx| ctx.finalize(),
            |ctx| ctx.finalize_reset(),
            |ctx| ctx.reset(),
        )
    }

    #[test]
    fn test_sha224() {
        // Examples from wikipedia
        let tests = [
            Test {
                input: b"",
                output: [
                    0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 0x61, 0x02, 0xbb, 0x28,
                    0x82, 0x34, 0xc4, 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3,
                    0xe4, 0x2f,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog",
                output: [
                    0x73, 0x0e, 0x10, 0x9b, 0xd7, 0xa8, 0xa3, 0x2b, 0x1c, 0xb9, 0xd9, 0xa0, 0x9a,
                    0xa2, 0x32, 0x5d, 0x24, 0x30, 0x58, 0x7d, 0xdb, 0xc0, 0xc3, 0x8b, 0xad, 0x91,
                    0x15, 0x25,
                ],
            },
            Test {
                input: b"The quick brown fox jumps over the lazy dog.",
                output: [
                    0x61, 0x9c, 0xba, 0x8e, 0x8e, 0x05, 0x82, 0x6e, 0x9b, 0x8c, 0x51, 0x9c, 0x0a,
                    0x5c, 0x68, 0xf4, 0xfb, 0x65, 0x3e, 0x8a, 0x3d, 0x8a, 0xa0, 0x4b, 0xb2, 0xc8,
                    0xcd, 0x4c,
                ],
            },
        ];
        test_hashing(
            &tests,
            Sha224,
            |_| Context224::new(),
            |ctx, input| ctx.update(input),
            |ctx, input| ctx.update_mut(input),
            |ctx| ctx.finalize(),
            |ctx| ctx.finalize_reset(),
            |ctx| ctx.reset(),
        )
    }
}

#[cfg(all(test, feature = "with-bench"))]
mod bench {
    use super::eng256;
    use super::eng512;
    use super::{Sha256, Sha512};
    use test::Bencher;

    #[bench]
    pub fn sha256_block(bh: &mut Bencher) {
        let mut state = eng256::Engine::new(&[0u32; eng256::STATE_LEN]);
        let block = [1u8; 64];
        bh.iter(|| {
            state.blocks(&block);
        });
        bh.bytes = 64u64;
    }

    #[bench]
    pub fn sha512_block(bh: &mut Bencher) {
        let mut state = eng512::Engine::new(&[0u64; eng512::STATE_LEN]);
        let block = [1u8; 128];
        bh.iter(|| {
            state.blocks(&block);
        });
        bh.bytes = 128u64;
    }

    #[bench]
    pub fn sha256_10(bh: &mut Bencher) {
        let mut sh = Sha256::new();
        let bytes = [1u8; 10];
        bh.iter(|| {
            sh.update_mut(&bytes);
        });
        bh.bytes = bytes.len() as u64;
    }

    #[bench]
    pub fn sha256_1k(bh: &mut Bencher) {
        let mut sh = Sha256::new();
        let bytes = [1u8; 1000];
        bh.iter(|| {
            sh.update_mut(&bytes);
        });
        bh.bytes = bytes.len() as u64;
    }

    #[bench]
    pub fn sha256_64k(bh: &mut Bencher) {
        let mut sh = Sha256::new();
        let bytes = [1u8; 65536];
        bh.iter(|| {
            sh.update_mut(&bytes);
        });
        bh.bytes = bytes.len() as u64;
    }

    #[bench]
    pub fn sha512_10(bh: &mut Bencher) {
        let mut sh = Sha512::new();
        let bytes = [1u8; 10];
        bh.iter(|| {
            sh.update_mut(&bytes);
        });
        bh.bytes = bytes.len() as u64;
    }

    #[bench]
    pub fn sha512_1k(bh: &mut Bencher) {
        let mut sh = Sha512::new();
        let bytes = [1u8; 1024];
        bh.iter(|| {
            sh.update_mut(&bytes);
        });
        bh.bytes = bytes.len() as u64;
    }

    #[bench]
    pub fn sha512_64k(bh: &mut Bencher) {
        let mut sh = Sha512::new();
        let bytes = [1u8; 65536];
        bh.iter(|| {
            sh.update_mut(&bytes);
        });
        bh.bytes = bytes.len() as u64;
    }
}