1#![no_std]
16#![doc(
17 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
18 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
19)]
20#![cfg_attr(docsrs, feature(doc_cfg))]
21#![warn(missing_docs, rust_2018_idioms)]
22#![forbid(unsafe_code)]
23
24#[cfg(feature = "cipher")]
25pub use cipher;
26
27use crate::consts::{H5, H13, H21, H29};
28use core::{mem::swap, num::Wrapping};
29
30#[cfg(feature = "cipher")]
31mod cipher_impl;
32mod consts;
33
34#[cfg(feature = "cipher")]
35pub use cipher_impl::BeltBlock;
36
37macro_rules! g {
38 ($($name:ident: ($a:expr, $b:expr, $c:expr, $d:expr)),+) => {
39 $(
40 #[inline]
41 fn $name(Wrapping(u): Wrapping<u32>) -> Wrapping<u32> {
42 Wrapping($a[((u >> 24) & 0xFF) as usize]
43 ^ $b[((u >> 16) & 0xFF) as usize]
44 ^ $c[((u >> 8) & 0xFF) as usize]
45 ^ $d[(u & 0xFF) as usize])
46 }
47 )+
48 }
49}
50
51g!(
52 g5: (H29, H21, H13, H5),
53 g13: (H5, H29, H21, H13),
54 g21: (H13, H5, H29, H21)
55);
56
57#[inline(always)]
58fn key_idx(key: &[u32; 8], i: usize, delta: usize) -> Wrapping<u32> {
59 Wrapping(key[(7 * i - delta - 1) % 8])
60}
61
62#[inline(always)]
65pub fn belt_block_raw(x: [u32; 4], key: &[u32; 8]) -> [u32; 4] {
66 let mut a = Wrapping(x[0]);
67 let mut b = Wrapping(x[1]);
68 let mut c = Wrapping(x[2]);
69 let mut d = Wrapping(x[3]);
70
71 for i in 1..9 {
73 b ^= g5(a + key_idx(key, i, 6));
75 c ^= g21(d + key_idx(key, i, 5));
77 a -= g13(b + key_idx(key, i, 4));
79 let e = g21(b + c + key_idx(key, i, 3)) ^ Wrapping(i as u32);
81 b += e;
83 c -= e;
85 d += g13(c + key_idx(key, i, 2));
87 b ^= g21(a + key_idx(key, i, 1));
89 c ^= g5(d + key_idx(key, i, 0));
91 swap(&mut a, &mut b);
93 swap(&mut c, &mut d);
95 swap(&mut b, &mut c);
97 }
98
99 [b.0, d.0, a.0, c.0]
101}
102
103const BLOCK_SIZE: usize = 16;
104type Block = [u8; BLOCK_SIZE];
105
106#[inline]
110pub fn belt_wblock_enc(data: &mut [u8], key: &[u32; 8]) -> Result<(), InvalidLengthError> {
111 if data.len() < 2 * BLOCK_SIZE {
112 return Err(InvalidLengthError);
113 }
114
115 let len = data.len();
116 let n = len.div_ceil(BLOCK_SIZE);
117 for i in 1..(2 * n + 1) {
118 let s = data[..len - 1]
119 .chunks_exact(BLOCK_SIZE)
120 .fold(Block::default(), xor);
121
122 data.copy_within(BLOCK_SIZE.., 0);
123 let (tail1, tail2) = data[len - 2 * BLOCK_SIZE..].split_at_mut(BLOCK_SIZE);
124 tail2.copy_from_slice(&s);
125
126 let s = belt_block_raw(to_u32(&s), key);
127 xor_set(tail1, &from_u32::<16>(&s));
128 xor_set(tail1, &i.to_le_bytes());
129 }
130
131 Ok(())
132}
133
134#[inline]
138pub fn belt_wblock_dec(data: &mut [u8], key: &[u32; 8]) -> Result<(), InvalidLengthError> {
139 if data.len() < 2 * BLOCK_SIZE {
140 return Err(InvalidLengthError);
141 }
142
143 let len = data.len();
144 let n = len.div_ceil(BLOCK_SIZE);
145 for i in (1..(2 * n + 1)).rev() {
146 let tail_pos = len - BLOCK_SIZE;
147 let s = Block::try_from(&data[tail_pos..]).unwrap();
148 data.copy_within(..tail_pos, BLOCK_SIZE);
149
150 let s_enc = belt_block_raw(to_u32(&s), key);
151 xor_set(&mut data[tail_pos..], &from_u32::<16>(&s_enc));
152 xor_set(&mut data[tail_pos..], &i.to_le_bytes());
153
154 let r1 = data[..len - 1]
155 .chunks_exact(BLOCK_SIZE)
156 .skip(1)
157 .fold(s, xor);
158 data[..BLOCK_SIZE].copy_from_slice(&r1);
159 }
160 Ok(())
161}
162
163#[derive(Debug, Copy, Clone)]
165pub struct InvalidLengthError;
166
167#[inline(always)]
173fn to_u32<const N: usize>(src: &[u8]) -> [u32; N] {
174 assert_eq!(src.len(), 4 * N);
175 let mut res = [0u32; N];
176 res.iter_mut()
177 .zip(src.chunks_exact(4))
178 .for_each(|(dst, src)| *dst = u32::from_le_bytes(src.try_into().unwrap()));
179 res
180}
181
182#[inline(always)]
183fn from_u32<const N: usize>(src: &[u32]) -> [u8; N] {
184 assert_eq!(N, 4 * src.len());
185 let mut res = [0u8; N];
186 res.chunks_exact_mut(4)
187 .zip(src.iter())
188 .for_each(|(dst, src)| dst.copy_from_slice(&src.to_le_bytes()));
189 res
190}
191
192#[inline(always)]
193fn xor_set(block: &mut [u8], val: &[u8]) {
194 block.iter_mut().zip(val.iter()).for_each(|(a, b)| *a ^= b);
195}
196
197#[inline(always)]
198fn xor(mut block: Block, val: &[u8]) -> Block {
199 block.iter_mut().zip(val.iter()).for_each(|(a, b)| *a ^= b);
200 block
201}