dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / wsutil / xtea.c
blobb98de5f5be55e42572a692ececa904d550ba97db
1 /* xtea.c
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
13 #include "xtea.h"
15 #include <string.h>
17 #include "pint.h"
19 void decrypt_xtea_ecb(uint8_t plaintext[8], const uint8_t ciphertext[8], const uint32_t key[4], unsigned num_rounds)
21 unsigned i;
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]);
29 sum -= delta;
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)
41 unsigned i;
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]);
49 sum -= delta;
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
62 * Local variables:
63 * c-basic-offset: 4
64 * tab-width: 8
65 * indent-tabs-mode: nil
66 * End:
68 * vi: set shiftwidth=4 tabstop=8 expandtab:
69 * :indentSize=4:tabSize=8:noTabs=true: