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
pub use cipher::{BlockCipher, NewBlockCipher};
use core::ops::{BitXor, Shl, Shr};

use byteorder::{BigEndian, ByteOrder};

use cipher::consts::{U16, U4, U8};
use cipher::generic_array::GenericArray;

use crate::simd::{slice_block, unslice_block, WrapArithmetic};

const TEA_DELTA: u32 = 0x9E3779B9;

pub type Block8 = GenericArray<u8, U8>;
pub type Block8x4 = GenericArray<Block8, U4>;

macro_rules! define_tea_impl {
    (
        $name:ident,
        $rounds:expr,
        $shift:expr,
        $doc:expr
    ) => {
        #[doc=$doc]
        #[derive(Clone)]
        pub struct $name {
            key: [u32; 4],
        }

        impl $name {
            #[inline]
            fn encrypt_core<T>(&self, x: T, y: T) -> (T, T)
            where T: Copy +
                     WrapArithmetic<T> + WrapArithmetic<u32> +
                     BitXor<Output = T> +
                     Shl<usize, Output = T> + Shr<usize, Output = T> {
                let mut sum: u32 = 0;
                let (mut x, mut y) = (x, y);
                let k0 = self.key[0];
                let k1 = self.key[1];
                let k2 = self.key[2];
                let k3 = self.key[3];

                for _ in 0..$rounds {
                    sum = sum.wrapping_add(TEA_DELTA);
                    x = WrapArithmetic::wrapping_add(x,
                                                     WrapArithmetic::wrapping_add(y << 4, k0) ^
                                                     WrapArithmetic::wrapping_add(y, sum) ^
                                                     WrapArithmetic::wrapping_add(y >> 5, k1));
                    y = WrapArithmetic::wrapping_add(y,
                                                     WrapArithmetic::wrapping_add(x << 4, k2) ^
                                                     WrapArithmetic::wrapping_add(x, sum) ^
                                                     WrapArithmetic::wrapping_add(x >> 5, k3));
                }

                (x, y)
            }

            #[inline]
            fn decrypt_core<T>(&self, x: T, y: T) -> (T, T)
            where T: Copy +
                     WrapArithmetic<T> + WrapArithmetic<u32> +
                     BitXor<Output = T> +
                     Shl<usize, Output = T> + Shr<usize, Output = T> {
                let mut sum: u32 = TEA_DELTA << $shift;
                let (mut x, mut y) = (x, y);
                let k0 = self.key[0];
                let k1 = self.key[1];
                let k2 = self.key[2];
                let k3 = self.key[3];

                for _ in 0..$rounds {
                    y = WrapArithmetic::wrapping_sub(y,
                                                     WrapArithmetic::wrapping_add(x << 4, k2) ^
                                                     WrapArithmetic::wrapping_add(x, sum) ^
                                                     WrapArithmetic::wrapping_add(x >> 5, k3));
                    x = WrapArithmetic::wrapping_sub(x,
                                                     WrapArithmetic::wrapping_add(y << 4, k0) ^
                                                     WrapArithmetic::wrapping_add(y, sum) ^
                                                     WrapArithmetic::wrapping_add(y >> 5, k1));
                    sum = sum.wrapping_sub(TEA_DELTA);
                }

                (x, y)
            }
        }

        impl NewBlockCipher for $name {
            type KeySize = U16;

            #[inline]
            fn new(key: &GenericArray<u8, U16>) -> Self {
                Self {
                    key: [
                        BigEndian::read_u32(&key[0..4]),
                        BigEndian::read_u32(&key[4..8]),
                        BigEndian::read_u32(&key[8..12]),
                        BigEndian::read_u32(&key[12..16]),
                    ]
                }
            }
        }

        impl BlockCipher for $name {
            type BlockSize = U8;
            type ParBlocks = U4;

            #[inline]
            fn encrypt_block(&self, block: &mut Block8) {
                let x = BigEndian::read_u32(&block[0..4]);
                let y = BigEndian::read_u32(&block[4..8]);

                let (x, y) = self.encrypt_core(x, y);

                BigEndian::write_u32(&mut block[0..4], x);
                BigEndian::write_u32(&mut block[4..8], y);
            }

            #[inline]
            fn decrypt_block(&self, block: &mut Block8) {
                let x = BigEndian::read_u32(&block[0..4]);
                let y = BigEndian::read_u32(&block[4..8]);

                let (x, y) = self.decrypt_core(x, y);

                BigEndian::write_u32(&mut block[0..4], x);
                BigEndian::write_u32(&mut block[4..8], y);
            }

            #[inline]
            fn encrypt_blocks(&self, blocks: &mut Block8x4) {
                let (xs, ys) = slice_block(blocks);
                let (xs, ys) = self.encrypt_core(xs, ys);
                unslice_block(xs, ys, blocks);
            }

            #[inline]
            fn decrypt_blocks(&self, blocks: &mut Block8x4) {
                let (xs, ys) = slice_block(blocks);
                let (xs, ys) = self.decrypt_core(xs, ys);
                unslice_block(xs, ys, blocks);
            }
        }
    }
}

define_tea_impl!(Tea16, 16, 4, "TEA block cipher instance of 16 rounds");
define_tea_impl!(Tea32, 32, 5, "TEA block cipher instance of 32 rounds");
define_tea_impl!(Tea64, 64, 6, "TEA block cipher instance of 64 rounds");