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
//! Simple [`XTEA`] implementation in Rust.
//!
//! Minimal protection is afforded by the use of `wrapping_add`
//! and `wrapping_sub` functions of Rust's core.  Generally
//! speaking the code should not have any troubles (and this
//! code has been moderately used in production).
//!
//! [`XTEA`]: https://en.wikipedia.org/wiki/XTEA

#![no_std]

/// Enciphers the blocks of data using the given key.
/// 
/// Blocks must be greater than two or no action is performed.
/// Blocks should be divisible by two, where this is not the case
/// any remaining blocks will not be processed (i.e. the last block
/// will be ignored.)
/// 
/// The key data should be a set of four values (128 bits total).
pub fn encipher(blocks: &mut [u32], key: &[u32], delta: u32, rounds: usize) {

    // Can't process less than two blocks because
    // that's not how block ciphers work.
    let n = blocks.len();
    if n < 2 {
        return;
    }

    // Value of 'a' block
    let mut a: u32;

    // Value of 'b' block
    let mut b: u32;

    // Running/internal sum value
    let mut s: u32;

    // Step the blocks in pairs.
    for i in 0..(n / 2) {
        // Get 'a'
        a = blocks[i * 2];

        // Get 'b'
        b = blocks[(i * 2) + 1];

        // Reset running sum.
        s = 0;

        // Step the number of rounds;
        // the iterator index isn't relevant to us.
        for _ in 0..rounds {

            // Modify 'a'
            a = a.wrapping_add((((b << 4) ^ (b >> 5)).wrapping_add(b)) ^
                               s.wrapping_add(key[(s & 3) as usize]));

            // Update running sum using delta
            s = s.wrapping_add(delta);

            // modify 'b'
            b = b.wrapping_add((((a << 4) ^ (a >> 5)).wrapping_add(a)) ^
                               s.wrapping_add(key[((s >> 11) & 3) as usize]));
        }

        // Store a
        blocks[i * 2] = a;

        // Store b
        blocks[(i * 2) + 1] = b;
    }
}

/// Deciphers the blocks of data using the given key.
/// 
/// Blocks must be greater than two or no action is performed.
/// Blocks should be divisible by two, where this is not the case
/// any remaining blocks will not be processed (i.e. the last block
/// will be ignored.)
/// 
/// The key data should be a set of four values (128 bits total).
pub fn decipher(blocks: &mut [u32], key: &[u32], delta: u32, rounds: usize) {

    // Can't process less than two blocks because
    // that's not how block ciphers work.
    let n = blocks.len();
    if n < 2 {
        return;
    }

    // Value of 'a' block
    let mut a: u32;

    // Value of 'b' block
    let mut b: u32;

    // Running/internal sum value
    let mut s: u32;

    // Pre-calculate the initial sum
    // so it can be re-used.
    let sum: u32 = delta.wrapping_mul(rounds as u32);

    // Step the blocks in pairs.
    for i in 0..(n / 2) {

        // Get 'a'
        a = blocks[i * 2];

        // Get 'b'
        b = blocks[(i * 2) + 1];

        // Reset running sum.
        s = sum;

        // Step the number of rounds;
        // the iterator index isn't relevant to us.
        for _ in 0..rounds {
            // modify b
            b = b.wrapping_sub((((a << 4) ^ (a >> 5)).wrapping_add(a)) ^
                               s.wrapping_add(key[((s >> 11) & 3) as usize]));

            // Update running sum using delta
            s = s.wrapping_sub(delta);

            // modify a
            a = a.wrapping_sub((((b << 4) ^ (b >> 5)).wrapping_add(b)) ^
                               s.wrapping_add(key[(s & 3) as usize]));
        }

        // Store a
        blocks[i * 2] = a;

        // Store b
        blocks[(i * 2) + 1] = b;
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn simple_test() {
        use super::{encipher, decipher};

        const DELTA: u32 = 0x9E3779B9;
        const ROUNDS: usize = 32;
        const KEY: [u32; 4] = [0xDEADBEEF, 0xAAAAAAAA, 0x0CD0CDAA, 0x12345678];

        // Decoded
        let data_d: [u32; 32] =
            [0x90b61054, 0x4f117340, 0x2a192f72, 0xc6d20912, 0xd4a00486, 0x343b1fa9, 0xe7806b43,
             0xd80e41e0, 0x81462e8a, 0x5e59805e, 0x7310266c, 0xb5c3f09d, 0xc830c818, 0xff5425ad,
             0x5a4f3477, 0xb63eb737, 0x3df4acab, 0x679b76e1, 0x52befb4a, 0x54a6d777, 0xcfb5eb73,
             0x83e661e2, 0xcddc1290, 0xe1d3972b, 0x9e9fe877, 0xe021d4c5, 0x69ac7f72, 0x3c3476cd,
             0x7bf3ba34, 0x4183fa09, 0xbd82c98a, 0xdc181a43];

        // Encoded
        let data_e: [u32; 32] =
            [0xecaf9488, 0xd768bb69, 0xa6b2a074, 0x6b43c81c, 0x2fefb898, 0xa385f8cc, 0x3a9d7e7a,
             0x2461333d, 0x5faffa3e, 0x043fa28b, 0x22e91e81, 0x11b5e618, 0x10d741fe, 0x661f1de6,
             0x19b91f26, 0xa3f94e5d, 0xe822918e, 0x62afd15b, 0x14aa9fb4, 0x23237adb, 0xdc64bf3a,
             0x6e93102d, 0x2b1b7c24, 0x246e6058, 0x590e37a8, 0x60b51b69, 0x35970d7c, 0x2f247f1d,
             0x361bded3, 0x40c6ff4b, 0xb7d1370d, 0x471ebe43];

        // Start state (pulled from decoded);
        // Do an explicit clone here so if the data changes above it doesn't break.
        let mut data: [u32; 32] = data_d.clone();

        encipher(&mut data, &KEY, DELTA, ROUNDS);

        for i in 0..32 {
            assert_eq!(data_e[i], data[i]);
        }

        decipher(&mut data, &KEY, DELTA, ROUNDS);

        for i in 0..32 {
            assert_eq!(data_d[i], data[i]);
        }
    }
}