wsutil: Free files in reset_default_profile
[metze/wireshark/wip.git] / wsutil / xtea.c
1 /* xtea.c
2  * Implementation of XTEA cipher
3  * By Ahmad Fatoum <ahmad[AT]a3f.at>
4  * Copyright 2017 Ahmad Fatoum
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  */
25
26 #include <glib.h>
27 #include <string.h>
28
29 #include "pint.h"
30 #include "xtea.h"
31
32 void decrypt_xtea_ecb(guint8 output[8], const guint8 v_in[8], const guint32 key[4], guint num_rounds)
33 {
34     guint i;
35     guint32 v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
36
37     v[0] = pntoh32(&v_in[0]);
38     v[1] = pntoh32(&v_in[4]);
39
40     for (i = 0; i < num_rounds; i++) {
41         v[1] -= (((v[0] << 4) ^ (v[0] >> 5)) + v[0]) ^ (sum + key[(sum >> 11) & 3]);
42         sum -= delta;
43         v[0] -= (((v[1] << 4) ^ (v[1] >> 5)) + v[1]) ^ (sum + key[sum & 3]);
44     }
45
46     v[0] = GUINT32_TO_BE(v[0]);
47     v[1] = GUINT32_TO_BE(v[1]);
48
49     memcpy(output, v, sizeof v);
50 }
51
52 void decrypt_xtea_le_ecb(guint8 output[8], const guint8 v_in[8], const guint32 key[4], guint num_rounds)
53 {
54     guint i;
55     guint32 v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
56
57     v[0] = pletoh32(&v_in[0]);
58     v[1] = pletoh32(&v_in[4]);
59
60     for (i = 0; i < num_rounds; i++) {
61         v[1] -= (((v[0] << 4) ^ (v[0] >> 5)) + v[0]) ^ (sum + key[(sum >> 11) & 3]);
62         sum -= delta;
63         v[0] -= (((v[1] << 4) ^ (v[1] >> 5)) + v[1]) ^ (sum + key[sum & 3]);
64     }
65
66     v[0] = GUINT32_TO_LE(v[0]);
67     v[1] = GUINT32_TO_LE(v[1]);
68
69     memcpy(output, v, sizeof v);
70 }