annotate libpurple/ciphers/des.c @ 31667:076d62344ede

broke des and des3 out to ciphers/des.c
author Gary Kramlich <grim@reaperworld.com>
date Mon, 14 Feb 2011 06:51:10 +0000
parents
children 2d3c1197f930
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31667
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2 * purple
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 * Purple is the legal property of its developers, whose names are too numerous
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * source distribution.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 * Original des taken from gpg
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * des.c - DES and Triple-DES encryption/decryption Algorithm
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * Copyright (C) 1998 Free Software Foundation, Inc.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 * Please see below for more legal information!
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * According to the definition of DES in FIPS PUB 46-2 from December 1993.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * For a description of triple encryption, see:
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 * Bruce Schneier: Applied Cryptography. Second Edition.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20 * This file is part of GnuPG.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
22 * This program is free software; you can redistribute it and/or modify
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
23 * it under the terms of the GNU General Public License as published by
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24 * the Free Software Foundation; either version 2 of the License, or
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25 * (at your option) any later version.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
26 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27 * This program is distributed in the hope that it will be useful,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30 * GNU General Public License for more details.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32 * You should have received a copy of the GNU General Public License
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33 * along with this program; if not, write to the Free Software
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
35 */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 #include <cipher.h>
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
37 #include <string.h>
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
38
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
39 /******************************************************************************
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
40 * DES
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
41 *****************************************************************************/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
42 typedef struct _des_ctx
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
43 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
44 guint32 encrypt_subkeys[32];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
45 guint32 decrypt_subkeys[32];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
46 } des_ctx[1];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
47
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
48 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
49 * The s-box values are permuted according to the 'primitive function P'
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
50 */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
51 static const guint32 sbox1[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
52 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
53 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
54 0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
55 0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
56 0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
57 0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58 0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60 0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 static const guint32 sbox2[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68 0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69 0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70 0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71 0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72 0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 static const guint32 sbox3[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79 0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81 0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84 0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 static const guint32 sbox4[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90 0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91 0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94 0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
95 0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
96 0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
97 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
98
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
99 static const guint32 sbox5[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
100 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
102 0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
104 0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105 0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
106 0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
107 0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
110
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
111 static const guint32 sbox6[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
113 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114 0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116 0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
118 0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
119 0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
120 0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
121 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
122
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
123 static const guint32 sbox7[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
124 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
125 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
126 0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
127 0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
128 0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
129 0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
130 0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
131 0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
132 0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
133 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
134
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
135 static const guint32 sbox8[64] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
136 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
137 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
138 0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
139 0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
140 0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
141 0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
142 0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
143 0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
144 0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
145 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
147
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
149 * * These two tables are part of the 'permuted choice 1' function.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150 * * In this implementation several speed improvements are done.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
151 * */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
152 static const guint32 leftkey_swap[16] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
153 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
154 0x00000000, 0x00000001, 0x00000100, 0x00000101,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
155 0x00010000, 0x00010001, 0x00010100, 0x00010101,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156 0x01000000, 0x01000001, 0x01000100, 0x01000101,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
157 0x01010000, 0x01010001, 0x01010100, 0x01010101
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
158 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
159
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 static const guint32 rightkey_swap[16] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162 0x00000000, 0x01000000, 0x00010000, 0x01010000,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163 0x00000100, 0x01000100, 0x00010100, 0x01010100,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
164 0x00000001, 0x01000001, 0x00010001, 0x01010001,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165 0x00000101, 0x01000101, 0x00010101, 0x01010101,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
166 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
167
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
170 * Numbers of left shifts per round for encryption subkey schedule
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
171 * To calculate the decryption key scheduling we just reverse the
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
172 * ordering of the subkeys so we can omit the table for decryption
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
173 * subkey schedule.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
174 */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
175 static const guint8 encrypt_rotate_tab[16] =
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
176 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
177 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
178 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
179
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
180 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
181 * Macro to swap bits across two words
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
182 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
183 #define DO_PERMUTATION(a, temp, b, offset, mask) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
184 temp = ((a>>offset) ^ b) & mask; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
185 b ^= temp; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
186 a ^= temp<<offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
187
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
188
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
189 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
190 * This performs the 'initial permutation' for the data to be encrypted or decrypted
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
191 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
192 #define INITIAL_PERMUTATION(left, temp, right) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
193 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
194 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
195 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
196 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
197 DO_PERMUTATION(left, temp, right, 1, 0x55555555)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
198
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
199
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
200 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
201 * The 'inverse initial permutation'
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
202 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
203 #define FINAL_PERMUTATION(left, temp, right) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
204 DO_PERMUTATION(left, temp, right, 1, 0x55555555) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
205 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
206 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
207 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
208 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
209
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
210
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
211 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
212 * A full DES round including 'expansion function', 'sbox substitution'
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
213 * and 'primitive function P' but without swapping the left and right word.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
214 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
215 #define DES_ROUND(from, to, work, subkey) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
216 work = ((from<<1) | (from>>31)) ^ *subkey++; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
217 to ^= sbox8[ work & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
218 to ^= sbox6[ (work>>8) & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
219 to ^= sbox4[ (work>>16) & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
220 to ^= sbox2[ (work>>24) & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
221 work = ((from>>3) | (from<<29)) ^ *subkey++; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
222 to ^= sbox7[ work & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
223 to ^= sbox5[ (work>>8) & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
224 to ^= sbox3[ (work>>16) & 0x3f ]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
225 to ^= sbox1[ (work>>24) & 0x3f ];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
226
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
227
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
228 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
229 * Macros to convert 8 bytes from/to 32bit words
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
230 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
231 #define READ_64BIT_DATA(data, left, right) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
232 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
233 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
234
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
235 #define WRITE_64BIT_DATA(data, left, right) \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
236 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
237 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
238 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
239 data[6] = (right >> 8) &0xff; data[7] = right &0xff;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
240
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
241
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
242 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
243 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
244 * 16 encryption rounds.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
245 * To calculate subkeys for decryption the caller
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
246 * have to reorder the generated subkeys.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
247 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
248 * rawkey: 8 Bytes of key data
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
249 * subkey: Array of at least 32 guint32s. Will be filled
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
250 * with calculated subkeys.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
251 *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
252 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
253 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
254 des_key_schedule (const guint8 * rawkey, guint32 * subkey)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
255 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
256 guint32 left, right, work;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
257 int round;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
258
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
259 READ_64BIT_DATA (rawkey, left, right)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
260
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
261 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
262 DO_PERMUTATION (right, work, left, 0, 0x10101010)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
263
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
264 left = (leftkey_swap[(left >> 0) & 0xf] << 3) | (leftkey_swap[(left >> 8) & 0xf] << 2)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
265 | (leftkey_swap[(left >> 16) & 0xf] << 1) | (leftkey_swap[(left >> 24) & 0xf])
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
266 | (leftkey_swap[(left >> 5) & 0xf] << 7) | (leftkey_swap[(left >> 13) & 0xf] << 6)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
267 | (leftkey_swap[(left >> 21) & 0xf] << 5) | (leftkey_swap[(left >> 29) & 0xf] << 4);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
268
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
269 left &= 0x0fffffff;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
270
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
271 right = (rightkey_swap[(right >> 1) & 0xf] << 3) | (rightkey_swap[(right >> 9) & 0xf] << 2)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
272 | (rightkey_swap[(right >> 17) & 0xf] << 1) | (rightkey_swap[(right >> 25) & 0xf])
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
273 | (rightkey_swap[(right >> 4) & 0xf] << 7) | (rightkey_swap[(right >> 12) & 0xf] << 6)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
274 | (rightkey_swap[(right >> 20) & 0xf] << 5) | (rightkey_swap[(right >> 28) & 0xf] << 4);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
275
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
276 right &= 0x0fffffff;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
277
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
278 for (round = 0; round < 16; ++round)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
279 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
280 left = ((left << encrypt_rotate_tab[round]) | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
281 right = ((right << encrypt_rotate_tab[round]) | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
282
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
283 *subkey++ = ((left << 4) & 0x24000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
284 | ((left << 28) & 0x10000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
285 | ((left << 14) & 0x08000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
286 | ((left << 18) & 0x02080000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
287 | ((left << 6) & 0x01000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
288 | ((left << 9) & 0x00200000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
289 | ((left >> 1) & 0x00100000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
290 | ((left << 10) & 0x00040000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
291 | ((left << 2) & 0x00020000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
292 | ((left >> 10) & 0x00010000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
293 | ((right >> 13) & 0x00002000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
294 | ((right >> 4) & 0x00001000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
295 | ((right << 6) & 0x00000800)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
296 | ((right >> 1) & 0x00000400)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
297 | ((right >> 14) & 0x00000200)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
298 | (right & 0x00000100)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
299 | ((right >> 5) & 0x00000020)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
300 | ((right >> 10) & 0x00000010)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
301 | ((right >> 3) & 0x00000008)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
302 | ((right >> 18) & 0x00000004)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
303 | ((right >> 26) & 0x00000002)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
304 | ((right >> 24) & 0x00000001);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
305
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
306 *subkey++ = ((left << 15) & 0x20000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
307 | ((left << 17) & 0x10000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
308 | ((left << 10) & 0x08000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
309 | ((left << 22) & 0x04000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
310 | ((left >> 2) & 0x02000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
311 | ((left << 1) & 0x01000000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
312 | ((left << 16) & 0x00200000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
313 | ((left << 11) & 0x00100000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
314 | ((left << 3) & 0x00080000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
315 | ((left >> 6) & 0x00040000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
316 | ((left << 15) & 0x00020000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
317 | ((left >> 4) & 0x00010000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
318 | ((right >> 2) & 0x00002000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
319 | ((right << 8) & 0x00001000)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
320 | ((right >> 14) & 0x00000808)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
321 | ((right >> 9) & 0x00000400)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
322 | ((right) & 0x00000200)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
323 | ((right << 7) & 0x00000100)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
324 | ((right >> 7) & 0x00000020)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
325 | ((right >> 3) & 0x00000011)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
326 | ((right << 2) & 0x00000004)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
327 | ((right >> 21) & 0x00000002);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
328 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
329 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
330
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
331
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
332 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
333 * Fill a DES context with subkeys calculated from a 64bit key.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
334 * Does not check parity bits, but simply ignore them.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
335 * Does not check for weak keys.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
336 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
337 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
338 des_set_key (PurpleCipherContext *context, const guchar * key)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
339 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
340 struct _des_ctx *ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
341 int i;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
342
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
343 des_key_schedule (key, ctx->encrypt_subkeys);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
344
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
345 for(i=0; i<32; i+=2)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
346 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
347 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
348 ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
349 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
350 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
351
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
352
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
353 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
354 * Electronic Codebook Mode DES encryption/decryption of data according
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
355 * to 'mode'.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
356 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
357 static int
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
358 des_ecb_crypt (struct _des_ctx *ctx, const guint8 * from, guint8 * to, int mode)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
359 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
360 guint32 left, right, work;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
361 guint32 *keys;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
362
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
363 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
364
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
365 READ_64BIT_DATA (from, left, right)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
366 INITIAL_PERMUTATION (left, work, right)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
367
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
368 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
369 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
370 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
371 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
372 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
373 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
374 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
375 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
376
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
377 FINAL_PERMUTATION (right, work, left)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
378 WRITE_64BIT_DATA (to, right, left)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
379
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
380 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
381 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
382
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
383 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
384 des_encrypt(PurpleCipherContext *context, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
385 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
386 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
387 int offset = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
388 int i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
389 int tmp;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
390 guint8 buf[8] = {0,0,0,0,0,0,0,0};
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
391 while(offset+8<=len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
392 des_ecb_crypt(purple_cipher_context_get_data(context),
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
393 data+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
394 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
395 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
396 offset+=8;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
397 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
398 *outlen = len;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
399 if(offset<len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
400 *outlen += len - offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
401 tmp = offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
402 while(tmp<len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
403 buf[i++] = data[tmp];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
404 tmp++;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
405 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
406 des_ecb_crypt(purple_cipher_context_get_data(context),
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
407 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
408 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
409 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
410 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
411 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
412 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
413
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
414 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
415 des_decrypt(PurpleCipherContext *context, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
416 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
417 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
418 int offset = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
419 int i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
420 int tmp;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
421 guint8 buf[8] = {0,0,0,0,0,0,0,0};
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
422 while(offset+8<=len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
423 des_ecb_crypt(purple_cipher_context_get_data(context),
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
424 data+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
425 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
426 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
427 offset+=8;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
428 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
429 *outlen = len;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
430 if(offset<len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
431 *outlen += len - offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
432 tmp = offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
433 while(tmp<len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
434 buf[i++] = data[tmp];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
435 tmp++;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
436 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
437 des_ecb_crypt(purple_cipher_context_get_data(context),
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
438 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
439 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
440 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
441 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
442 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
443 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
444
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
445 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
446 des_init(PurpleCipherContext *context, gpointer extra) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
447 struct _des_ctx *mctx;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
448 mctx = g_new0(struct _des_ctx, 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
449 purple_cipher_context_set_data(context, mctx);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
450 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
451
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
452 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
453 des_uninit(PurpleCipherContext *context) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
454 struct _des_ctx *des_context;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
455
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
456 des_context = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
457 memset(des_context, 0, sizeof(*des_context));
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
458
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
459 g_free(des_context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
460 des_context = NULL;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
461 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
462
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
463 static PurpleCipherOps DESOps = {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
464 .init = des_init,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
465 .uninit = des_uninit,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
466 .encrypt = des_encrypt,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
467 .decrypt = des_decrypt,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
468 .set_key = des_set_key,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
469 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
470
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
471 /******************************************************************************
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
472 * Triple-DES
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
473 *****************************************************************************/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
474
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
475 typedef struct _des3_ctx
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
476 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
477 PurpleCipherBatchMode mode;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
478 guchar iv[8];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
479 /* First key for encryption */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
480 struct _des_ctx key1;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
481 /* Second key for decryption */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
482 struct _des_ctx key2;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
483 /* Third key for encryption */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
484 struct _des_ctx key3;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
485 } des3_ctx[1];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
486
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
487 /*
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
488 * Fill a DES3 context with subkeys calculated from 3 64bit key.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
489 * Does not check parity bits, but simply ignore them.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
490 * Does not check for weak keys.
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
491 **/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
492 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
493 des3_set_key(PurpleCipherContext *context, const guchar * key)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
494 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
495 struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
496 int i;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
497
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
498 des_key_schedule (key + 0, ctx->key1.encrypt_subkeys);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
499 des_key_schedule (key + 8, ctx->key2.encrypt_subkeys);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
500 des_key_schedule (key + 16, ctx->key3.encrypt_subkeys);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
501
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
502 for (i = 0; i < 32; i += 2)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
503 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
504 ctx->key1.decrypt_subkeys[i] = ctx->key1.encrypt_subkeys[30-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
505 ctx->key1.decrypt_subkeys[i+1] = ctx->key1.encrypt_subkeys[31-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
506 ctx->key2.decrypt_subkeys[i] = ctx->key2.encrypt_subkeys[30-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
507 ctx->key2.decrypt_subkeys[i+1] = ctx->key2.encrypt_subkeys[31-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
508 ctx->key3.decrypt_subkeys[i] = ctx->key3.encrypt_subkeys[30-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
509 ctx->key3.decrypt_subkeys[i+1] = ctx->key3.encrypt_subkeys[31-i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
510 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
511 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
512
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
513 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
514 des3_ecb_encrypt(struct _des3_ctx *ctx, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
515 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
516 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
517 int offset = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
518 int i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
519 int tmp;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
520 guint8 buf[8] = {0,0,0,0,0,0,0,0};
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
521 while (offset + 8 <= len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
522 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
523 data+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
524 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
525 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
526 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
527 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
528 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
529 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
530 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
531 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
532 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
533 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
534 offset += 8;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
535 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
536 *outlen = len;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
537 if (offset < len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
538 *outlen += len - offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
539 tmp = offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
540 memset(buf, 0, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
541 while (tmp < len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
542 buf[i++] = data[tmp];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
543 tmp++;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
544 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
545 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
546 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
547 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
548 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
549 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
550 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
551 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
552 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
553 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
554 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
555 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
556 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
557 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
558 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
559 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
560
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
561 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
562 des3_cbc_encrypt(struct _des3_ctx *ctx, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
563 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
564 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
565 int offset = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
566 int i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
567 int tmp;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
568 guint8 buf[8];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
569 memcpy(buf, ctx->iv, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
570 while (offset + 8 <= len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
571 for (i = 0; i < 8; i++)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
572 buf[i] ^= data[offset + i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
573
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
574 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
575 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
576 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
577 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
578 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
579 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
580 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
581 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
582 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
583 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
584 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
585 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
586 memcpy(buf, output+offset, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
587 offset += 8;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
588 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
589 *outlen = len;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
590 if (offset < len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
591 *outlen += len - offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
592 tmp = offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
593 i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
594 while (tmp < len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
595 buf[i++] ^= data[tmp];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
596 tmp++;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
597 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
598 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
599 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
600 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
601 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
602 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
603 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
604 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
605 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
606 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
607 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
608 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
609 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
610 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
611 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
612 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
613
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
614 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
615 des3_encrypt(PurpleCipherContext *context, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
616 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
617 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
618 struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
619
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
620 if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_ECB) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
621 return des3_ecb_encrypt(ctx, data, len, output, outlen);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
622 } else if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_CBC) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
623 return des3_cbc_encrypt(ctx, data, len, output, outlen);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
624 } else {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
625 g_return_val_if_reached(0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
626 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
627
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
628 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
629 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
630
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
631 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
632 des3_ecb_decrypt(struct _des3_ctx *ctx, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
633 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
634 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
635 int offset = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
636 int i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
637 int tmp;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
638 guint8 buf[8] = {0,0,0,0,0,0,0,0};
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
639 while (offset + 8 <= len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
640 /* NOTE: Apply key in reverse */
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
641 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
642 data+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
643 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
644 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
645 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
646 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
647 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
648 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
649 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
650 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
651 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
652 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
653 offset+=8;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
654 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
655 *outlen = len;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
656 if (offset < len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
657 *outlen += len - offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
658 tmp = offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
659 memset(buf, 0, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
660 while (tmp < len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
661 buf[i++] = data[tmp];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
662 tmp++;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
663 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
664 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
665 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
666 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
667 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
668 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
669 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
670 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
671 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
672 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
673 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
674 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
675 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
676 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
677 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
678 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
679
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
680 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
681 des3_cbc_decrypt(struct _des3_ctx *ctx, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
682 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
683 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
684 int offset = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
685 int i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
686 int tmp;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
687 guint8 buf[8] = {0,0,0,0,0,0,0,0};
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
688 guint8 link[8];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
689 memcpy(link, ctx->iv, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
690 while (offset + 8 <= len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
691 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
692 data+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
693 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
694 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
695 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
696 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
697 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
698 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
699 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
700 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
701 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
702 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
703 for (i = 0; i < 8; i++)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
704 output[offset + i] ^= link[i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
705 memcpy(link, data + offset, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
706 offset+=8;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
707 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
708 *outlen = len;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
709 if(offset<len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
710 *outlen += len - offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
711 tmp = offset;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
712 memset(buf, 0, 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
713 i = 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
714 while(tmp<len) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
715 buf[i++] = data[tmp];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
716 tmp++;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
717 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
718 des_ecb_crypt(&ctx->key3,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
719 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
720 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
721 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
722 des_ecb_crypt(&ctx->key2,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
723 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
724 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
725 0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
726 des_ecb_crypt(&ctx->key1,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
727 buf,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
728 output+offset,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
729 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
730 for (i = 0; i < 8; i++)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
731 output[offset + i] ^= link[i];
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
732 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
733 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
734 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
735
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
736 static gint
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
737 des3_decrypt(PurpleCipherContext *context, const guchar data[],
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
738 size_t len, guchar output[], size_t *outlen)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
739 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
740 struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
741
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
742 if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_ECB) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
743 return des3_ecb_decrypt(ctx, data, len, output, outlen);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
744 } else if (ctx->mode == PURPLE_CIPHER_BATCH_MODE_CBC) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
745 return des3_cbc_decrypt(ctx, data, len, output, outlen);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
746 } else {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
747 g_return_val_if_reached(0);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
748 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
749
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
750 return 0;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
751 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
752
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
753 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
754 des3_set_batch(PurpleCipherContext *context, PurpleCipherBatchMode mode)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
755 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
756 struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
757
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
758 ctx->mode = mode;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
759 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
760
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
761 static PurpleCipherBatchMode
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
762 des3_get_batch(PurpleCipherContext *context)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
763 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
764 struct _des3_ctx *ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
765
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
766 return ctx->mode;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
767 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
768
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
769 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
770 des3_set_iv(PurpleCipherContext *context, guchar *iv, size_t len)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
771 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
772 struct _des3_ctx *ctx;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
773
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
774 g_return_if_fail(len == 8);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
775
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
776 ctx = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
777
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
778 memcpy(ctx->iv, iv, len);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
779 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
780
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
781 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
782 des3_init(PurpleCipherContext *context, gpointer extra)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
783 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
784 struct _des3_ctx *mctx;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
785 mctx = g_new0(struct _des3_ctx, 1);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
786 purple_cipher_context_set_data(context, mctx);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
787 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
788
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
789 static void
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
790 des3_uninit(PurpleCipherContext *context)
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
791 {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
792 struct _des3_ctx *des3_context;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
793
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
794 des3_context = purple_cipher_context_get_data(context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
795 memset(des3_context, 0, sizeof(*des3_context));
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
796
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
797 g_free(des3_context);
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
798 des3_context = NULL;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
799 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
800
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
801 static PurpleCipherOps DES3Ops = {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
802 .init = des3_init,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
803 .uninit = des3_uninit,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
804 .set_iv = des3_set_iv,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
805 .encrypt = des3_encrypt,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
806 .decrypt = des3_decrypt,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
807 .set_key = des3_set_key,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
808 .set_batch_mode = des3_set_batch,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
809 .get_batch_mode = des3_get_batch,
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
810 };
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
811
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
812 /******************************************************************************
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
813 * Registration
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
814 *****************************************************************************/
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
815 PurpleCipherOps *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
816 purple_des_cipher_get_ops(void) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
817 return &DESOps;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
818 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
819
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
820 PurpleCipherOps *
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
821 purple_des3_cipher_get_ops(void) {
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
822 return &DES3Ops;
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
823 }
076d62344ede broke des and des3 out to ciphers/des.c
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
824