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
//! 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;
    }
}

/// Enciphers a single pair pair (a single 64-bit block) in place.
///
/// The key data should be a set of four values (128 bits total).
pub fn encipher_pair(aa: &mut u32, bb: &mut u32, key: &[u32], delta: u32, rounds: usize) {
    // Set the value of a
    let mut a: u32 = *aa;

    // Set the value of b
    let mut b: u32 = *bb;

    // Reset running sum.
    let mut s: u32 = 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]));
    }

    *aa = a;
    *bb = 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;
    }
}

/// Deciphers a single pair pair (a single 64-bit block) in place.
///
/// The key data should be a set of four values (128 bits total).
pub fn decipher_pair(aa: &mut u32, bb: &mut u32, key: &[u32], delta: u32, rounds: usize) {
    // Set the value of a
    let mut a: u32 = *aa;

    // Set the value of b
    let mut b: u32 = *bb;

    // Reset running sum.
    let mut s: u32 = delta.wrapping_mul(rounds as u32);

    // 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]));
    }

    *aa = a;
    *bb = 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]);
        }
    }

    #[test]
    fn simple_test_pairs() {
        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];

        for j in 0..16 {
            let a = j * 2;
            let b = a + 1;

            let mut data: [u32; 2] = [data_d[a], data_d[b]];

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

            assert_eq!(data_e[a], data[0]);
            assert_eq!(data_e[b], data[1]);

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

            assert_eq!(data_d[a], data[0]);
            assert_eq!(data_d[b], data[1]);
        }
    }
}