aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2017-06-04 12:32:13 +0200
committerPeter Wu <peter@lekensteyn.nl>2017-06-05 16:10:58 +0000
commit345046c83d780c6c32a729ce5945b869f27c37f5 (patch)
treeb69902b32bedae2e110d58ed28c3c7495dd4a10d /wsutil
parent027aae1cd39f8cf9ce220fd20b94a9780d3b167e (diff)
wsutil: Add XTEA block cipher
XTEA is a 64-bit block Feistel cipher with a 128-bit key and a suggested 64 rounds. It's used by the MMORPG Tibia for encrypting game server traffic. Usual XTEA treats the blocks as big-endian. Tibia treats them as little endian, therefore both versions are provided. Change-Id: I9ad0c8e066f848b20772ce4e1d3df19deff307b8 Reviewed-on: https://code.wireshark.org/review/21942 Petri-Dish: Michael Mann <mmann78@netscape.net> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/Makefile.am6
-rw-r--r--wsutil/xtea.c70
-rw-r--r--wsutil/xtea.h39
3 files changed, 113 insertions, 2 deletions
diff --git a/wsutil/Makefile.am b/wsutil/Makefile.am
index a92982d61a..28f04c5be0 100644
--- a/wsutil/Makefile.am
+++ b/wsutil/Makefile.am
@@ -154,7 +154,8 @@ libwsutil_la_SOURCES = \
unicode-utils.c \
ws_mempbrk.c \
wsgcrypt.c \
- wsjsmn.c
+ wsjsmn.c \
+ xtea.c
if HAVE_OS_X_FRAMEWORKS
libwsutil_la_SOURCES += cfutils.c cfutils.h
@@ -201,7 +202,8 @@ EXTRA_DIST = \
ws_mempbrk_sse42.c \
wsgcrypt.h \
wsgetopt.h \
- wspcap.h
+ wspcap.h \
+ xtea.h
CLEANFILES = \
libwsutil.a \
diff --git a/wsutil/xtea.c b/wsutil/xtea.c
new file mode 100644
index 0000000000..253042f1b1
--- /dev/null
+++ b/wsutil/xtea.c
@@ -0,0 +1,70 @@
+/* xtea.c
+ * Implementation of XTEA cipher
+ * By Ahmad Fatoum <ahmad[AT]a3f.at>
+ * Copyright 2017 Ahmad Fatoum
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <glib.h>
+#include <string.h>
+
+#include "pint.h"
+#include "xtea.h"
+
+void decrypt_xtea_ecb(guint8 output[8], const guint8 v_in[8], const guint32 key[4], guint num_rounds)
+{
+ guint i;
+ guint32 v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
+
+ v[0] = pntoh32(&v_in[0]);
+ v[1] = pntoh32(&v_in[4]);
+
+ for (i = 0; i < num_rounds; i++) {
+ v[1] -= (((v[0] << 4) ^ (v[0] >> 5)) + v[0]) ^ (sum + key[(sum >> 11) & 3]);
+ sum -= delta;
+ v[0] -= (((v[1] << 4) ^ (v[1] >> 5)) + v[1]) ^ (sum + key[sum & 3]);
+ }
+
+ v[0] = GUINT32_TO_BE(v[0]);
+ v[1] = GUINT32_TO_BE(v[1]);
+
+ memcpy(output, v, sizeof v);
+}
+
+void decrypt_xtea_le_ecb(guint8 output[8], const guint8 v_in[8], const guint32 key[4], guint num_rounds)
+{
+ guint i;
+ guint32 v[2], delta = 0x9E3779B9, sum = delta * num_rounds;
+
+ v[0] = pletoh32(&v_in[0]);
+ v[1] = pletoh32(&v_in[4]);
+
+ for (i = 0; i < num_rounds; i++) {
+ v[1] -= (((v[0] << 4) ^ (v[0] >> 5)) + v[0]) ^ (sum + key[(sum >> 11) & 3]);
+ sum -= delta;
+ v[0] -= (((v[1] << 4) ^ (v[1] >> 5)) + v[1]) ^ (sum + key[sum & 3]);
+ }
+
+ v[0] = GUINT32_TO_LE(v[0]);
+ v[1] = GUINT32_TO_LE(v[1]);
+
+ memcpy(output, v, sizeof v);
+}
diff --git a/wsutil/xtea.h b/wsutil/xtea.h
new file mode 100644
index 0000000000..6a9160222d
--- /dev/null
+++ b/wsutil/xtea.h
@@ -0,0 +1,39 @@
+/* xtea.h
+ * Implementation of XTEA cipher
+ * By Ahmad Fatoum <ahmad[AT]a3f.at>
+ * Copyright 2017 Ahmad Fatoum
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef __XTEA_H__
+#define __XTEA_H__
+
+/* Actual XTEA is big-endian, nevertheless there exist protocols that treat every block
+ * as little endian, so we provide both
+ */
+#include "ws_symbol_export.h"
+#include <glib.h>
+
+WS_DLL_PUBLIC void decrypt_xtea_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const guint32 key[4], guint num_rounds);
+
+WS_DLL_PUBLIC void decrypt_xtea_le_ecb(guint8 plaintext[8], const guint8 ciphertext[8], const guint32 key[4], guint num_rounds);
+
+#endif /* __XTEA_H__ */