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
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, rust_2018_idioms)]
#![forbid(unsafe_code)]
#[cfg(feature = "cipher")]
pub use cipher;
use crate::consts::{H13, H21, H29, H5};
use core::{mem::swap, num::Wrapping};
#[cfg(feature = "cipher")]
mod cipher_impl;
mod consts;
#[cfg(feature = "cipher")]
pub use cipher_impl::BeltBlock;
macro_rules! g {
($($name:ident: ($a:expr, $b:expr, $c:expr, $d:expr)),+) => {
$(
#[inline]
fn $name(Wrapping(u): Wrapping<u32>) -> Wrapping<u32> {
Wrapping($a[((u >> 24) & 0xFF) as usize]
^ $b[((u >> 16) & 0xFF) as usize]
^ $c[((u >> 8) & 0xFF) as usize]
^ $d[(u & 0xFF) as usize])
}
)+
}
}
g!(
g5: (H29, H21, H13, H5),
g13: (H5, H29, H21, H13),
g21: (H13, H5, H29, H21)
);
#[inline(always)]
fn key_idx(key: &[u32; 8], i: usize, delta: usize) -> Wrapping<u32> {
Wrapping(key[(7 * i - delta - 1) % 8])
}
#[inline(always)]
pub fn belt_block_raw(x: [u32; 4], key: &[u32; 8]) -> [u32; 4] {
let mut a = Wrapping(x[0]);
let mut b = Wrapping(x[1]);
let mut c = Wrapping(x[2]);
let mut d = Wrapping(x[3]);
for i in 1..9 {
b ^= g5(a + key_idx(key, i, 6));
c ^= g21(d + key_idx(key, i, 5));
a -= g13(b + key_idx(key, i, 4));
let e = g21(b + c + key_idx(key, i, 3)) ^ Wrapping(i as u32);
b += e;
c -= e;
d += g13(c + key_idx(key, i, 2));
b ^= g21(a + key_idx(key, i, 1));
c ^= g5(d + key_idx(key, i, 0));
swap(&mut a, &mut b);
swap(&mut c, &mut d);
swap(&mut b, &mut c);
}
[b.0, d.0, a.0, c.0]
}