/soc/2013/ankitkv/gobjectification: fb40b0460a36: GObjectify the...

Ankit Vani a at nevitus.org
Thu Jun 13 13:30:23 EDT 2013


Changeset: fb40b0460a36d90887295dcf599599e2ca6fb5b6
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-06-13 22:59 +0530
Branch:	 soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/fb40b0460a36

Description:

GObjectify the DES and Triple-DES ciphers

diffstat:

 libpurple/ciphers/Makefile.am |     4 +-
 libpurple/ciphers/des.c       |  1109 ++++++++++++++--------------------------
 libpurple/ciphers/des.h       |    71 ++
 libpurple/ciphers/des3.c      |   518 +++++++++++++++++++
 libpurple/ciphers/des3.h      |    68 ++
 5 files changed, 1053 insertions(+), 717 deletions(-)

diffs (truncated from 1964 to 300 lines):

diff --git a/libpurple/ciphers/Makefile.am b/libpurple/ciphers/Makefile.am
--- a/libpurple/ciphers/Makefile.am
+++ b/libpurple/ciphers/Makefile.am
@@ -11,15 +11,13 @@ endif
 libpurple_ciphers_la_SOURCES=\
 	$(AES_SOURCE) \
 	des.c \
+	des3.c \
 	gchecksum.c \
 	hmac.c \
 	md4.c \
 	pbkdf2.c \
 	rc4.c
 
-noinst_HEADERS =\
-	ciphers.h
-
 INCLUDES = -I$(top_srcdir)/libpurple
 
 AM_CPPFLAGS = \
diff --git a/libpurple/ciphers/des.c b/libpurple/ciphers/des.c
--- a/libpurple/ciphers/des.c
+++ b/libpurple/ciphers/des.c
@@ -1,23 +1,19 @@
 /*
- * purple
- *
- * Purple is the legal property of its developers, whose names are too numerous
- * to list here.  Please refer to the COPYRIGHT file distributed with this
- * source distribution.
- *
  * Original des taken from gpg
  *
- * des.c - DES and Triple-DES encryption/decryption Algorithm
+ * des.c - DES encryption/decryption Algorithm
  *  Copyright (C) 1998 Free Software Foundation, Inc.
  *
  *  Please see below for more legal information!
- *
+ *  
  *   According to the definition of DES in FIPS PUB 46-2 from December 1993.
  *   For a description of triple encryption, see:
  *     Bruce Schneier: Applied Cryptography. Second Edition.
  *     John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
- *
- *   This file is part of GnuPG.
+ *  
+ * Purple is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
  *
  * 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
@@ -33,228 +29,255 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
+#include "des.h"
 
-#include "internal.h"
-#include <cipher.h>
-#include "ciphers.h"
+#include <string.h>
 
 /******************************************************************************
- * DES
+ * Structs
  *****************************************************************************/
-typedef struct _des_ctx
-{
+#define PURPLE_DES_CIPHER_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_DES_CIPHER, PurpleDESCipherPrivate))
+
+typedef struct {
 	guint32 encrypt_subkeys[32];
 	guint32 decrypt_subkeys[32];
-} des_ctx[1];
+} PurpleDESCipherPrivate;
+
+/******************************************************************************
+ * Enums
+ *****************************************************************************/
+enum {
+	PROP_NONE,
+	PROP_KEY,
+	PROP_LAST,
+};
+
+/******************************************************************************
+ * Globals
+ *****************************************************************************/
+static GObjectClass *parent_class = NULL;
+
+/*  
+ *  The s-box values are permuted according to the 'primitive function P'
+ */
+static guint32 sbox1[64] = {
+	0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202,
+	0x00000002, 0x00008000, 0x00000200, 0x00808200, 0x00808202, 0x00000200,
+	0x00800202, 0x00808002, 0x00800000, 0x00000002, 0x00000202, 0x00800200,
+	0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
+	0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202,
+	0x00008202, 0x00800000, 0x00008000, 0x00808202, 0x00000002, 0x00808000,
+	0x00808200, 0x00800000, 0x00800000, 0x00000200, 0x00808002, 0x00008000,
+	0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
+	0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202,
+	0x00008202, 0x00808200, 0x00000202, 0x00800200, 0x00800200, 0x00000000,
+	0x00008002, 0x00008200, 0x00000000, 0x00808002
+};
+
+static guint32 sbox2[64] = {
+	0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010,
+	0x40080010, 0x40004010, 0x40000010, 0x40084010, 0x40084000, 0x40000000,
+	0x40004000, 0x00080000, 0x00000010, 0x40080010, 0x00084000, 0x00080010,
+	0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
+	0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000,
+	0x40080000, 0x00004010, 0x00000000, 0x00084010, 0x40080010, 0x00080000,
+	0x40004010, 0x40080000, 0x40084000, 0x00004000, 0x40080000, 0x40004000,
+	0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
+	0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010,
+	0x40000010, 0x00080010, 0x00084000, 0x00000000, 0x40004000, 0x00004010,
+	0x40000000, 0x40080010, 0x40084010, 0x00084000
+};
+
+static guint32 sbox3[64] = {
+	0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000,
+	0x00010104, 0x04000100, 0x00010004, 0x04000004, 0x04000004, 0x00010000,
+	0x04010104, 0x00010004, 0x04010000, 0x00000104, 0x04000000, 0x00000004,
+	0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
+	0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104,
+	0x00000100, 0x04000000, 0x04010100, 0x04000000, 0x00010004, 0x00000104,
+	0x00010000, 0x04010100, 0x04000100, 0x00000000, 0x00000100, 0x00010004,
+	0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
+	0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104,
+	0x00010100, 0x04000004, 0x04010000, 0x04000104, 0x00000104, 0x04010000,
+	0x00010104, 0x00000004, 0x04010004, 0x00010100
+};
+
+static guint32 sbox4[64] = {
+	0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040,
+	0x80400000, 0x80001000, 0x00000000, 0x00401000, 0x00401000, 0x80401040,
+	0x80000040, 0x00000000, 0x00400040, 0x80400000, 0x80000000, 0x00001000,
+	0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
+	0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040,
+	0x80401040, 0x80000040, 0x00400040, 0x80400000, 0x00401000, 0x80401040,
+	0x80000040, 0x00000000, 0x00000000, 0x00401000, 0x00001040, 0x00400040,
+	0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
+	0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000,
+	0x00401040, 0x80400040, 0x80001000, 0x00001040, 0x00400000, 0x80401000,
+	0x00000040, 0x00400000, 0x00001000, 0x00401040
+};
+
+static guint32 sbox5[64] = {
+	0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080,
+	0x20000000, 0x01040000, 0x20040080, 0x00040000, 0x01000080, 0x20040080,
+	0x21000080, 0x21040000, 0x00040080, 0x20000000, 0x01000000, 0x20040000,
+	0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
+	0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000,
+	0x21000000, 0x00040080, 0x00040000, 0x21000080, 0x00000080, 0x01000000,
+	0x20000000, 0x01040000, 0x21000080, 0x20040080, 0x01000080, 0x20000000,
+	0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
+	0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000,
+	0x20040000, 0x21000000, 0x00040080, 0x01000080, 0x20000080, 0x00040000,
+	0x00000000, 0x20040000, 0x01040080, 0x20000080
+};
+
+static guint32 sbox6[64] = {
+	0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008,
+	0x10202008, 0x00200000, 0x10002000, 0x00202008, 0x00200000, 0x10000008,
+	0x00200008, 0x10002000, 0x10000000, 0x00002008, 0x00000000, 0x00200008,
+	0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
+	0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000,
+	0x10202000, 0x10000000, 0x10002000, 0x00000008, 0x10200008, 0x00202000,
+	0x10202008, 0x00200000, 0x00002008, 0x10000008, 0x00200000, 0x10002000,
+	0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
+	0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000,
+	0x10200000, 0x00202008, 0x00002000, 0x00200008, 0x10002008, 0x00000000,
+	0x10202000, 0x10000000, 0x00200008, 0x10002008
+};
+
+static guint32 sbox7[64] = {
+	0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401,
+	0x00100401, 0x02100400, 0x02100401, 0x00100000, 0x00000000, 0x02000001,
+	0x00000001, 0x02000000, 0x02100001, 0x00000401, 0x02000400, 0x00100401,
+	0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
+	0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001,
+	0x02000000, 0x00100400, 0x02000000, 0x00100400, 0x00100000, 0x02000401,
+	0x02000401, 0x02100001, 0x02100001, 0x00000001, 0x00100001, 0x02000000,
+	0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
+	0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000,
+	0x00000001, 0x02100401, 0x00000000, 0x00100401, 0x02100000, 0x00000400,
+	0x02000001, 0x02000400, 0x00000400, 0x00100001
+};
+
+static guint32 sbox8[64] = {
+	0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820,
+	0x00000020, 0x08000000, 0x00020020, 0x08020000, 0x08020820, 0x00020800,
+	0x08020800, 0x00020820, 0x00000800, 0x00000020, 0x08020000, 0x08000020,
+	0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800,
+	0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800,
+	0x00020820, 0x00020000, 0x00020820, 0x00020000, 0x08020800, 0x00000800,
+	0x00000020, 0x08020020, 0x00000800, 0x00020820, 0x08000800, 0x00000020,
+	0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820,
+	0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800,
+	0x08000820, 0x00000000, 0x08020820, 0x00020800, 0x00020800, 0x00000820,
+	0x00000820, 0x00020020, 0x08000000, 0x08020800
+};
 
 /*
- *  The s-box values are permuted according to the 'primitive function P'
+ * These two tables are part of the 'permuted choice 1' function.
+ * In this implementation several speed improvements are done.
  */
-static const guint32 sbox1[64] =
-{
-	0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
-	0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002,
-	0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
-	0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000,
-	0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200,
-	0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
-	0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200,
-	0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002
-};
-
-static const guint32 sbox2[64] =
-{
-	0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
-	0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010,
-	0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
-	0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010,
-	0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000,
-	0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
-	0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010,
-	0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000
-};
-
-static const guint32 sbox3[64] =
-{
-	0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
-	0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104,
-	0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
-	0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000,
-	0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000,
-	0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
-	0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004,
-	0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100
-};
-
-static const guint32 sbox4[64] =
-{
-	0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
-	0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000,
-	0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
-	0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040,
-	0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000,
-	0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
-	0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040,
-	0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040
-};
-
-static const guint32 sbox5[64] =
-{
-	0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
-	0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000,
-	0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
-	0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080,
-	0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080,
-	0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
-	0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000,
-	0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080
-};
-
-static const guint32 sbox6[64] =
-{
-	0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
-	0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008,
-	0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
-	0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000,
-	0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008,
-	0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
-	0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008,
-	0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008
-};
-
-static const guint32 sbox7[64] =
-{
-	0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
-	0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401,
-	0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
-	0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400,
-	0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001,
-	0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
-	0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401,
-	0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001
-};
-
-static const guint32 sbox8[64] =
-{
-	0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,



More information about the Commits mailing list