2 * Implementation of XTEA cipher
3 * By Ahmad Fatoum <ahmad[AT]a3f.at>
4 * Copyright 2017 Ahmad Fatoum
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
19 void decrypt_xtea_ecb(uint8_t plaintext
[8], const uint8_t ciphertext
[8], const uint32_t key
[4], unsigned num_rounds
)
22 uint32_t v
[2], delta
= 0x9E3779B9, sum
= delta
* num_rounds
;
24 v
[0] = pntoh32(&ciphertext
[0]);
25 v
[1] = pntoh32(&ciphertext
[4]);
27 for (i
= 0; i
< num_rounds
; i
++) {
28 v
[1] -= (((v
[0] << 4) ^ (v
[0] >> 5)) + v
[0]) ^ (sum
+ key
[(sum
>> 11) & 3]);
30 v
[0] -= (((v
[1] << 4) ^ (v
[1] >> 5)) + v
[1]) ^ (sum
+ key
[sum
& 3]);
33 v
[0] = GUINT32_TO_BE(v
[0]);
34 v
[1] = GUINT32_TO_BE(v
[1]);
36 memcpy(plaintext
, v
, sizeof v
);
39 void decrypt_xtea_le_ecb(uint8_t plaintext
[8], const uint8_t ciphertext
[8], const uint32_t key
[4], unsigned num_rounds
)
42 uint32_t v
[2], delta
= 0x9E3779B9, sum
= delta
* num_rounds
;
44 v
[0] = pletoh32(&ciphertext
[0]);
45 v
[1] = pletoh32(&ciphertext
[4]);
47 for (i
= 0; i
< num_rounds
; i
++) {
48 v
[1] -= (((v
[0] << 4) ^ (v
[0] >> 5)) + v
[0]) ^ (sum
+ key
[(sum
>> 11) & 3]);
50 v
[0] -= (((v
[1] << 4) ^ (v
[1] >> 5)) + v
[1]) ^ (sum
+ key
[sum
& 3]);
53 v
[0] = GUINT32_TO_LE(v
[0]);
54 v
[1] = GUINT32_TO_LE(v
[1]);
56 memcpy(plaintext
, v
, sizeof v
);
60 * Editor modelines - https://www.wireshark.org/tools/modelines.html
65 * indent-tabs-mode: nil
68 * vi: set shiftwidth=4 tabstop=8 expandtab:
69 * :indentSize=4:tabSize=8:noTabs=true: