annotate src/cipher.c @ 13227:87d2d1230ba2

[gaim-migrate @ 15592] SF Patch #1429712 from Sadrul Fixes SF Bug #1429077 "The pounce should not be freed." committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Sat, 11 Feb 2006 18:16:04 +0000
parents c01aa6ea4947
children ff4e85e04adf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1 /*
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
2 * gaim
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
3 *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
4 * Gaim is the legal property of its developers, whose names are too numerous
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
6 * source distribution.
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
7 *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
8 * Original md5
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
9 * Copyright (C) 2001-2003 Christophe Devine <c.devine@cr0.net>
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
10 *
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
11 * Original md4 taken from linux kernel
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
12 * MD4 Message Digest Algorithm (RFC1320).
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
13 *
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
14 * Implementation derived from Andrew Tridgell and Steve French's
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
15 * CIFS MD4 implementation, and the cryptoapi implementation
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
16 * originally based on the public domain implementation written
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
17 * by Colin Plumb in 1993.
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
18 *
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
19 * Copyright (c) Andrew Tridgell 1997-1998.
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
20 * Modified by Steve French (sfrench@us.ibm.com) 2002
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
21 * Copyright (c) Cryptoapi developers.
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
22 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
23 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
24 *
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
25 * Original des taken from gpg
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
26 *
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
27 * des.c - DES and Triple-DES encryption/decryption Algorithm
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
28 * Copyright (C) 1998 Free Software Foundation, Inc.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
29 *
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
30 * Please see below for more legal information!
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
31 *
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
32 * According to the definition of DES in FIPS PUB 46-2 from December 1993.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
33 * For a description of triple encryption, see:
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
34 * Bruce Schneier: Applied Cryptography. Second Edition.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
35 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
36 *
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
37 * This file is part of GnuPG.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
38 *
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
39 * This program is free software; you can redistribute it and/or modify
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
40 * it under the terms of the GNU General Public License as published by
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
41 * the Free Software Foundation; either version 2 of the License, or
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
42 * (at your option) any later version.
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
43 *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
44 * This program is distributed in the hope that it will be useful,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
47 * GNU General Public License for more details.
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
48 *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
49 * You should have received a copy of the GNU General Public License
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
50 * along with this program; if not, write to the Free Software
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
51 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
52 */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
53 #include <glib.h>
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
54 #include <string.h>
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
55 #include <stdio.h>
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
56
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
57 #include "internal.h"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
58 #include "cipher.h"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
59 #include "debug.h"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
60 #include "signals.h"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
61 #include "value.h"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
62
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
63 /*******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
64 * MD5
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
65 ******************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
66 struct MD5Context {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
67 guint32 total[2];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
68 guint32 state[4];
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
69 guchar buffer[64];
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
70 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
71
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
72 #define MD5_GET_GUINT32(n,b,i) { \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
73 (n) = ((guint32)(b) [(i) ] ) \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
74 | ((guint32)(b) [(i) + 1] << 8) \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
75 | ((guint32)(b) [(i) + 2] << 16) \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
76 | ((guint32)(b) [(i) + 3] << 24); \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
77 }
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
78 #define MD5_PUT_GUINT32(n,b,i) { \
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
79 (b)[(i) ] = (guchar)((n) ); \
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
80 (b)[(i) + 1] = (guchar)((n) >> 8); \
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
81 (b)[(i) + 2] = (guchar)((n) >> 16); \
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
82 (b)[(i) + 3] = (guchar)((n) >> 24); \
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
83 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
84
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
85 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
86 md5_init(GaimCipherContext *context, gpointer extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
87 struct MD5Context *md5_context;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
88
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
89 md5_context = g_new0(struct MD5Context, 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
90
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
91 gaim_cipher_context_set_data(context, md5_context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
92
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
93 gaim_cipher_context_reset(context, extra);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
94 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
95
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
96 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
97 md5_reset(GaimCipherContext *context, gpointer extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
98 struct MD5Context *md5_context;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
99
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
100 md5_context = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
101
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
102 md5_context->total[0] = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
103 md5_context->total[1] = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
104
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
105 md5_context->state[0] = 0x67452301;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
106 md5_context->state[1] = 0xEFCDAB89;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
107 md5_context->state[2] = 0x98BADCFE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
108 md5_context->state[3] = 0x10325476;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
109
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
110 memset(md5_context->buffer, 0, sizeof(md5_context->buffer));
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
111 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
112
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
113 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
114 md5_uninit(GaimCipherContext *context) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
115 struct MD5Context *md5_context;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
116
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
117 gaim_cipher_context_reset(context, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
118
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
119 md5_context = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
120 memset(md5_context, 0, sizeof(md5_context));
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
121
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
122 g_free(md5_context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
123 md5_context = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
124 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
125
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
126 static void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
127 md5_process(struct MD5Context *md5_context, const guchar data[64]) {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
128 guint32 X[16], A, B, C, D;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
129
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
130 A = md5_context->state[0];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
131 B = md5_context->state[1];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
132 C = md5_context->state[2];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
133 D = md5_context->state[3];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
134
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
135 MD5_GET_GUINT32(X[ 0], data, 0);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
136 MD5_GET_GUINT32(X[ 1], data, 4);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
137 MD5_GET_GUINT32(X[ 2], data, 8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
138 MD5_GET_GUINT32(X[ 3], data, 12);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
139 MD5_GET_GUINT32(X[ 4], data, 16);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
140 MD5_GET_GUINT32(X[ 5], data, 20);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
141 MD5_GET_GUINT32(X[ 6], data, 24);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
142 MD5_GET_GUINT32(X[ 7], data, 28);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
143 MD5_GET_GUINT32(X[ 8], data, 32);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
144 MD5_GET_GUINT32(X[ 9], data, 36);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
145 MD5_GET_GUINT32(X[10], data, 40);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
146 MD5_GET_GUINT32(X[11], data, 44);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
147 MD5_GET_GUINT32(X[12], data, 48);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
148 MD5_GET_GUINT32(X[13], data, 52);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
149 MD5_GET_GUINT32(X[14], data, 56);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
150 MD5_GET_GUINT32(X[15], data, 60);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
151
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
152 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
153 #define P(a,b,c,d,k,s,t) { \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
154 a += F(b,c,d) + X[k] + t; \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
155 a = S(a,s) + b; \
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
156 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
157
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
158 /* first pass */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
159 #define F(x,y,z) (z ^ (x & (y ^ z)))
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
160 P(A, B, C, D, 0, 7, 0xD76AA478);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
161 P(D, A, B, C, 1, 12, 0xE8C7B756);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
162 P(C, D, A, B, 2, 17, 0x242070DB);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
163 P(B, C, D, A, 3, 22, 0xC1BDCEEE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
164 P(A, B, C, D, 4, 7, 0xF57C0FAF);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
165 P(D, A, B, C, 5, 12, 0x4787C62A);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
166 P(C, D, A, B, 6, 17, 0xA8304613);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
167 P(B, C, D, A, 7, 22, 0xFD469501);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
168 P(A, B, C, D, 8, 7, 0x698098D8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
169 P(D, A, B, C, 9, 12, 0x8B44F7AF);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
170 P(C, D, A, B, 10, 17, 0xFFFF5BB1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
171 P(B, C, D, A, 11, 22, 0x895CD7BE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
172 P(A, B, C, D, 12, 7, 0x6B901122);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
173 P(D, A, B, C, 13, 12, 0xFD987193);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
174 P(C, D, A, B, 14, 17, 0xA679438E);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
175 P(B, C, D, A, 15, 22, 0x49B40821);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
176 #undef F
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
177
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
178 /* second pass */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
179 #define F(x,y,z) (y ^ (z & (x ^ y)))
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
180 P(A, B, C, D, 1, 5, 0xF61E2562);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
181 P(D, A, B, C, 6, 9, 0xC040B340);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
182 P(C, D, A, B, 11, 14, 0x265E5A51);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
183 P(B, C, D, A, 0, 20, 0xE9B6C7AA);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
184 P(A, B, C, D, 5, 5, 0xD62F105D);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
185 P(D, A, B, C, 10, 9, 0x02441453);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
186 P(C, D, A, B, 15, 14, 0xD8A1E681);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
187 P(B, C, D, A, 4, 20, 0xE7D3FBC8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
188 P(A, B, C, D, 9, 5, 0x21E1CDE6);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
189 P(D, A, B, C, 14, 9, 0xC33707D6);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
190 P(C, D, A, B, 3, 14, 0xF4D50D87);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
191 P(B, C, D, A, 8, 20, 0x455A14ED);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
192 P(A, B, C, D, 13, 5, 0xA9E3E905);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
193 P(D, A, B, C, 2, 9, 0xFCEFA3F8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
194 P(C, D, A, B, 7, 14, 0x676F02D9);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
195 P(B, C, D, A, 12, 20, 0x8D2A4C8A);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
196 #undef F
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
197
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
198 /* third pass */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
199 #define F(x,y,z) (x ^ y ^ z)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
200 P(A, B, C, D, 5, 4, 0xFFFA3942);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
201 P(D, A, B, C, 8, 11, 0x8771F681);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
202 P(C, D, A, B, 11, 16, 0x6D9D6122);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
203 P(B, C, D, A, 14, 23, 0xFDE5380C);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
204 P(A, B, C, D, 1, 4, 0xA4BEEA44);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
205 P(D, A, B, C, 4, 11, 0x4BDECFA9);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
206 P(C, D, A, B, 7, 16, 0xF6BB4B60);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
207 P(B, C, D, A, 10, 23, 0xBEBFBC70);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
208 P(A, B, C, D, 13, 4, 0x289B7EC6);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
209 P(D, A, B, C, 0, 11, 0xEAA127FA);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
210 P(C, D, A, B, 3, 16, 0xD4EF3085);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
211 P(B, C, D, A, 6, 23, 0x04881D05);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
212 P(A, B, C, D, 9, 4, 0xD9D4D039);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
213 P(D, A, B, C, 12, 11, 0xE6DB99E5);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
214 P(C, D, A, B, 15, 16, 0x1FA27CF8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
215 P(B, C, D, A, 2, 23, 0xC4AC5665);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
216 #undef F
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
217
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
218 /* forth pass */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
219 #define F(x,y,z) (y ^ (x | ~z))
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
220 P(A, B, C, D, 0, 6, 0xF4292244);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
221 P(D, A, B, C, 7, 10, 0x432AFF97);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
222 P(C, D, A, B, 14, 15, 0xAB9423A7);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
223 P(B, C, D, A, 5, 21, 0xFC93A039);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
224 P(A, B, C, D, 12, 6, 0x655B59C3);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
225 P(D, A, B, C, 3, 10, 0x8F0CCC92);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
226 P(C, D, A, B, 10, 15, 0xFFEFF47D);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
227 P(B, C, D, A, 1, 21, 0x85845DD1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
228 P(A, B, C, D, 8, 6, 0x6FA87E4F);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
229 P(D, A, B, C, 15, 10, 0xFE2CE6E0);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
230 P(C, D, A, B, 6, 15, 0xA3014314);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
231 P(B, C, D, A, 13, 21, 0x4E0811A1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
232 P(A, B, C, D, 4, 6, 0xF7537E82);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
233 P(D, A, B, C, 11, 10, 0xBD3AF235);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
234 P(C, D, A, B, 2, 15, 0x2AD7D2BB);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
235 P(B, C, D, A, 9, 21, 0xEB86D391);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
236 #undef F
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
237 #undef P
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
238 #undef S
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
239
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
240 md5_context->state[0] += A;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
241 md5_context->state[1] += B;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
242 md5_context->state[2] += C;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
243 md5_context->state[3] += D;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
244 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
245
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
246 static void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
247 md5_append(GaimCipherContext *context, const guchar *data, size_t len) {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
248 struct MD5Context *md5_context = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
249 guint32 left = 0, fill = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
250
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
251 g_return_if_fail(context != NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
252
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
253 md5_context = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
254 g_return_if_fail(md5_context != NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
255
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
256 left = md5_context->total[0] & 0x3F;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
257 fill = 64 - left;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
258
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
259 md5_context->total[0] += len;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
260 md5_context->total[0] &= 0xFFFFFFFF;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
261
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
262 if(md5_context->total[0] < len)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
263 md5_context->total[1]++;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
264
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
265 if(left && len >= fill) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
266 memcpy((md5_context->buffer + left), data, fill);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
267 md5_process(md5_context, md5_context->buffer);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
268 len -= fill;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
269 data += fill;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
270 left = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
271 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
272
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
273 while(len >= 64) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
274 md5_process(md5_context, data);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
275 len -= 64;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
276 data += 64;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
277 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
278
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
279 if(len) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
280 memcpy((md5_context->buffer + left), data, len);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
281 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
282 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
283
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
284 static gboolean
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
285 md5_digest(GaimCipherContext *context, size_t in_len, guchar digest[16],
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
286 size_t *out_len)
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
287 {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
288 struct MD5Context *md5_context = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
289 guint32 last, pad;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
290 guint32 high, low;
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
291 guchar message[8];
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
292 guchar padding[64] = {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
293 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
297 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
298
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
299 g_return_val_if_fail(in_len >= 16, FALSE);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
300
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
301 md5_context = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
302
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
303 high = (md5_context->total[0] >> 29)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
304 | (md5_context->total[1] << 3);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
305 low = (md5_context->total[0] << 3);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
306
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
307 MD5_PUT_GUINT32(low, message, 0);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
308 MD5_PUT_GUINT32(high, message, 4);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
309
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
310 last = md5_context->total[0] & 0x3F;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
311 pad = (last < 56) ? (56 - last) : (120 - last);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
312
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
313 md5_append(context, padding, pad);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
314 md5_append(context, message, 8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
315
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
316 MD5_PUT_GUINT32(md5_context->state[0], digest, 0);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
317 MD5_PUT_GUINT32(md5_context->state[1], digest, 4);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
318 MD5_PUT_GUINT32(md5_context->state[2], digest, 8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
319 MD5_PUT_GUINT32(md5_context->state[3], digest, 12);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
320
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
321 if(out_len)
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
322 *out_len = 16;
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
323
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
324 return TRUE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
325 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
326
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
327 static GaimCipherOps MD5Ops = {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
328 NULL, /* Set option */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
329 NULL, /* Get option */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
330 md5_init, /* init */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
331 md5_reset, /* reset */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
332 md5_uninit, /* uninit */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
333 NULL, /* set iv */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
334 md5_append, /* append */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
335 md5_digest, /* digest */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
336 NULL, /* encrypt */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
337 NULL, /* decrypt */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
338 NULL, /* set salt */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
339 NULL, /* get salt size */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
340 NULL, /* set key */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
341 NULL /* get key size */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
342 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
343
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
344 /*******************************************************************************
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
345 * MD4
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
346 ******************************************************************************/
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
347 #define MD4_DIGEST_SIZE 16
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
348 #define MD4_HMAC_BLOCK_SIZE 64
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
349 #define MD4_BLOCK_WORDS 16
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
350 #define MD4_HASH_WORDS 4
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
351
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
352
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
353
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
354 struct MD4_Context {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
355 guint32 hash[MD4_HASH_WORDS];
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
356 guint32 block[MD4_BLOCK_WORDS];
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
357 guint64 byte_count;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
358 };
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
359
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
360 static inline guint32 lshift(guint32 x, unsigned int s)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
361 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
362 x &= 0xFFFFFFFF;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
363 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
364 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
365
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
366 static inline guint32 F(guint32 x, guint32 y, guint32 z)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
367 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
368 return (x & y) | ((~x) & z);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
369 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
370
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
371 static inline guint32 G(guint32 x, guint32 y, guint32 z)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
372 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
373 return (x & y) | (x & z) | (y & z);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
374 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
375
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
376 static inline guint32 H(guint32 x, guint32 y, guint32 z)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
377 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
378 return x ^ y ^ z;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
379 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
380
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
381 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
382 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (guint32)0x5A827999,s))
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
383 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (guint32)0x6ED9EBA1,s))
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
384
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
385 static inline void le32_to_cpu_array(guint32 *buf, unsigned int words)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
386 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
387 while (words--) {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
388 *buf=GUINT_FROM_LE(*buf);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
389 buf++;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
390 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
391 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
392
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
393 static inline void cpu_to_le32_array(guint32 *buf, unsigned int words)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
394 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
395 while (words--) {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
396 *buf=GUINT_TO_LE(*buf);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
397 buf++;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
398 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
399 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
400
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
401 static void md4_transform(guint32 *hash, guint32 const *in)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
402 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
403 guint32 a, b, c, d;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
404
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
405 a = hash[0];
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
406 b = hash[1];
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
407 c = hash[2];
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
408 d = hash[3];
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
409
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
410 ROUND1(a, b, c, d, in[0], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
411 ROUND1(d, a, b, c, in[1], 7);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
412 ROUND1(c, d, a, b, in[2], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
413 ROUND1(b, c, d, a, in[3], 19);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
414 ROUND1(a, b, c, d, in[4], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
415 ROUND1(d, a, b, c, in[5], 7);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
416 ROUND1(c, d, a, b, in[6], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
417 ROUND1(b, c, d, a, in[7], 19);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
418 ROUND1(a, b, c, d, in[8], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
419 ROUND1(d, a, b, c, in[9], 7);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
420 ROUND1(c, d, a, b, in[10], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
421 ROUND1(b, c, d, a, in[11], 19);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
422 ROUND1(a, b, c, d, in[12], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
423 ROUND1(d, a, b, c, in[13], 7);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
424 ROUND1(c, d, a, b, in[14], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
425 ROUND1(b, c, d, a, in[15], 19);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
426
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
427 ROUND2(a, b, c, d,in[ 0], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
428 ROUND2(d, a, b, c, in[4], 5);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
429 ROUND2(c, d, a, b, in[8], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
430 ROUND2(b, c, d, a, in[12], 13);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
431 ROUND2(a, b, c, d, in[1], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
432 ROUND2(d, a, b, c, in[5], 5);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
433 ROUND2(c, d, a, b, in[9], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
434 ROUND2(b, c, d, a, in[13], 13);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
435 ROUND2(a, b, c, d, in[2], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
436 ROUND2(d, a, b, c, in[6], 5);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
437 ROUND2(c, d, a, b, in[10], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
438 ROUND2(b, c, d, a, in[14], 13);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
439 ROUND2(a, b, c, d, in[3], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
440 ROUND2(d, a, b, c, in[7], 5);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
441 ROUND2(c, d, a, b, in[11], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
442 ROUND2(b, c, d, a, in[15], 13);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
443
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
444 ROUND3(a, b, c, d,in[ 0], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
445 ROUND3(d, a, b, c, in[8], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
446 ROUND3(c, d, a, b, in[4], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
447 ROUND3(b, c, d, a, in[12], 15);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
448 ROUND3(a, b, c, d, in[2], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
449 ROUND3(d, a, b, c, in[10], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
450 ROUND3(c, d, a, b, in[6], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
451 ROUND3(b, c, d, a, in[14], 15);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
452 ROUND3(a, b, c, d, in[1], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
453 ROUND3(d, a, b, c, in[9], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
454 ROUND3(c, d, a, b, in[5], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
455 ROUND3(b, c, d, a, in[13], 15);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
456 ROUND3(a, b, c, d, in[3], 3);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
457 ROUND3(d, a, b, c, in[11], 9);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
458 ROUND3(c, d, a, b, in[7], 11);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
459 ROUND3(b, c, d, a, in[15], 15);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
460
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
461 hash[0] += a;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
462 hash[1] += b;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
463 hash[2] += c;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
464 hash[3] += d;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
465 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
466
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
467 static inline void md4_transform_helper(struct MD4_Context *ctx)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
468 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
469 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(guint32));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
470 md4_transform(ctx->hash, ctx->block);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
471 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
472
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
473 static void
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
474 md4_init(GaimCipherContext *context, gpointer extra) {
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
475 struct MD4_Context *mctx;
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
476 mctx = g_new0(struct MD4_Context, 1);
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
477 gaim_cipher_context_set_data(context, mctx);
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
478 gaim_cipher_context_reset(context, extra);
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
479
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
480 mctx->hash[0] = 0x67452301;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
481 mctx->hash[1] = 0xefcdab89;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
482 mctx->hash[2] = 0x98badcfe;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
483 mctx->hash[3] = 0x10325476;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
484 mctx->byte_count = 0;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
485 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
486
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
487 static void
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
488 md4_reset(GaimCipherContext *context, gpointer extra) {
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
489 struct MD4_Context *mctx;
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
490
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
491 mctx = gaim_cipher_context_get_data(context);
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
492
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
493 mctx->hash[0] = 0x67452301;
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
494 mctx->hash[1] = 0xefcdab89;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
495 mctx->hash[2] = 0x98badcfe;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
496 mctx->hash[3] = 0x10325476;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
497 mctx->byte_count = 0;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
498 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
499
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
500 static void
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
501 md4_append(GaimCipherContext *context, const guchar *data, size_t len)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
502 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
503 struct MD4_Context *mctx = gaim_cipher_context_get_data(context);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
504 const guint32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
505
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
506 mctx->byte_count += len;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
507
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
508 if (avail > len) {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
509 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
510 data, len);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
511 return;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
512 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
513
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
514 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
515 data, avail);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
516
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
517 md4_transform_helper(mctx);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
518 data += avail;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
519 len -= avail;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
520
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
521 while (len >= sizeof(mctx->block)) {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
522 memcpy(mctx->block, data, sizeof(mctx->block));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
523 md4_transform_helper(mctx);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
524 data += sizeof(mctx->block);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
525 len -= sizeof(mctx->block);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
526 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
527
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
528 memcpy(mctx->block, data, len);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
529 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
530
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
531 static gboolean
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
532 md4_digest(GaimCipherContext *context, size_t in_len, guchar *out,
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
533 size_t *out_len)
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
534 {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
535 struct MD4_Context *mctx = gaim_cipher_context_get_data(context);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
536 const unsigned int offset = mctx->byte_count & 0x3f;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
537 char *p = (char *)mctx->block + offset;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
538 int padding = 56 - (offset + 1);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
539
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
540
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
541 if(in_len<16) return FALSE;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
542 if(out_len) *out_len = 16;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
543 *p++ = 0x80;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
544 if (padding < 0) {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
545 memset(p, 0x00, padding + sizeof (guint64));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
546 md4_transform_helper(mctx);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
547 p = (char *)mctx->block;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
548 padding = 56;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
549 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
550
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
551 memset(p, 0, padding);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
552 mctx->block[14] = mctx->byte_count << 3;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
553 mctx->block[15] = mctx->byte_count >> 29;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
554 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
555 sizeof(guint64)) / sizeof(guint32));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
556 md4_transform(mctx->hash, mctx->block);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
557 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(guint32));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
558 memcpy(out, mctx->hash, sizeof(mctx->hash));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
559 memset(mctx, 0, sizeof(*mctx));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
560 return TRUE;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
561 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
562
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
563 static void
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
564 md4_uninit(GaimCipherContext *context) {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
565 struct MD4_Context *md4_context;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
566
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
567 gaim_cipher_context_reset(context, NULL);
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
568
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
569 md4_context = gaim_cipher_context_get_data(context);
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
570 memset(md4_context, 0, sizeof(md4_context));
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
571
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
572 g_free(md4_context);
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
573 md4_context = NULL;
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
574 }
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
575
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
576 static GaimCipherOps MD4Ops = {
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
577 NULL, /* Set option */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
578 NULL, /* Get option */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
579 md4_init, /* init */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
580 md4_reset, /* reset */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
581 md4_uninit, /* uninit */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
582 NULL, /* set iv */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
583 md4_append, /* append */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
584 md4_digest, /* digest */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
585 NULL, /* encrypt */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
586 NULL, /* decrypt */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
587 NULL, /* set salt */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
588 NULL, /* get salt size */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
589 NULL, /* set key */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
590 NULL /* get key size */
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
591 };
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
592
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
593 /******************************************************************************
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
594 * DES
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
595 *****************************************************************************/
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
596
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
597 typedef struct _des_ctx
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
598 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
599 guint32 encrypt_subkeys[32];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
600 guint32 decrypt_subkeys[32];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
601 } des_ctx[1];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
602
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
603 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
604 * The s-box values are permuted according to the 'primitive function P'
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
605 */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
606 static guint32 sbox1[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
607 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
608 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
609 0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
610 0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
611 0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
612 0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
613 0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
614 0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
615 0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
616 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
617
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
618 static guint32 sbox2[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
619 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
620 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
621 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
622 0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
623 0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
624 0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
625 0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
626 0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
627 0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
628 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
629
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
630 static guint32 sbox3[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
631 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
632 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
633 0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
634 0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
635 0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
636 0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
637 0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
638 0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
639 0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
640 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
641
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
642 static guint32 sbox4[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
643 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
644 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
645 0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
646 0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
647 0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
648 0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
649 0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
650 0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
651 0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
652 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
653
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
654 static guint32 sbox5[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
655 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
656 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
657 0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
658 0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
659 0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
660 0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
661 0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
662 0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
663 0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
664 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
665
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
666 static guint32 sbox6[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
667 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
668 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
669 0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
670 0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
671 0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
672 0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
673 0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
674 0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
675 0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
676 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
677
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
678 static guint32 sbox7[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
679 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
680 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
681 0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
682 0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
683 0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
684 0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
685 0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
686 0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
687 0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
688 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
689
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
690 static guint32 sbox8[64] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
691 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
692 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
693 0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
694 0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
695 0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
696 0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
697 0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
698 0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
699 0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
700 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
701
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
702
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
703
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
704 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
705 * * These two tables are part of the 'permuted choice 1' function.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
706 * * In this implementation several speed improvements are done.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
707 * */
11677
8004885fabbe [gaim-migrate @ 13963]
Mark Doliner <mark@kingant.net>
parents: 11597
diff changeset
708 static guint32 leftkey_swap[16] =
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
709 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
710 0x00000000, 0x00000001, 0x00000100, 0x00000101,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
711 0x00010000, 0x00010001, 0x00010100, 0x00010101,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
712 0x01000000, 0x01000001, 0x01000100, 0x01000101,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
713 0x01010000, 0x01010001, 0x01010100, 0x01010101
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
714 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
715
11677
8004885fabbe [gaim-migrate @ 13963]
Mark Doliner <mark@kingant.net>
parents: 11597
diff changeset
716 static guint32 rightkey_swap[16] =
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
717 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
718 0x00000000, 0x01000000, 0x00010000, 0x01010000,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
719 0x00000100, 0x01000100, 0x00010100, 0x01010100,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
720 0x00000001, 0x01000001, 0x00010001, 0x01010001,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
721 0x00000101, 0x01000101, 0x00010101, 0x01010101,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
722 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
723
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
724
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
725
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
726 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
727 * Numbers of left shifts per round for encryption subkey schedule
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
728 * To calculate the decryption key scheduling we just reverse the
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
729 * ordering of the subkeys so we can omit the table for decryption
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
730 * subkey schedule.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
731 */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
732 static guint8 encrypt_rotate_tab[16] =
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
733 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
734 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
735 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
736
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
737 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
738 * Macro to swap bits across two words
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
739 **/
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
740 #define DO_PERMUTATION(a, temp, b, offset, mask) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
741 temp = ((a>>offset) ^ b) & mask; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
742 b ^= temp; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
743 a ^= temp<<offset;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
744
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
745
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
746 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
747 * This performs the 'initial permutation' for the data to be encrypted or decrypted
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
748 **/
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
749 #define INITIAL_PERMUTATION(left, temp, right) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
750 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
751 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
752 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
753 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
754 DO_PERMUTATION(left, temp, right, 1, 0x55555555)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
755
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
756
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
757 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
758 * The 'inverse initial permutation'
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
759 **/
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
760 #define FINAL_PERMUTATION(left, temp, right) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
761 DO_PERMUTATION(left, temp, right, 1, 0x55555555) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
762 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
763 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
764 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
765 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
766
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
767
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
768 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
769 * A full DES round including 'expansion function', 'sbox substitution'
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
770 * and 'primitive function P' but without swapping the left and right word.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
771 **/
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
772 #define DES_ROUND(from, to, work, subkey) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
773 work = ((from<<1) | (from>>31)) ^ *subkey++; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
774 to ^= sbox8[ work & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
775 to ^= sbox6[ (work>>8) & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
776 to ^= sbox4[ (work>>16) & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
777 to ^= sbox2[ (work>>24) & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
778 work = ((from>>3) | (from<<29)) ^ *subkey++; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
779 to ^= sbox7[ work & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
780 to ^= sbox5[ (work>>8) & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
781 to ^= sbox3[ (work>>16) & 0x3f ]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
782 to ^= sbox1[ (work>>24) & 0x3f ];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
783
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
784
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
785 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
786 * Macros to convert 8 bytes from/to 32bit words
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
787 **/
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
788 #define READ_64BIT_DATA(data, left, right) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
789 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
790 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
791
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
792 #define WRITE_64BIT_DATA(data, left, right) \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
793 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
794 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
795 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
796 data[6] = (right >> 8) &0xff; data[7] = right &0xff;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
797
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
798
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
799
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
800
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
801
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
802
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
803 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
804 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
805 * 16 encryption rounds.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
806 * To calculate subkeys for decryption the caller
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
807 * have to reorder the generated subkeys.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
808 *
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
809 * rawkey: 8 Bytes of key data
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
810 * subkey: Array of at least 32 guint32s. Will be filled
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
811 * with calculated subkeys.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
812 *
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
813 **/
11597
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
814 static void
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
815 des_key_schedule (const guint8 * rawkey, guint32 * subkey)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
816 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
817 guint32 left, right, work;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
818 int round;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
819
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
820 READ_64BIT_DATA (rawkey, left, right)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
821
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
822 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
823 DO_PERMUTATION (right, work, left, 0, 0x10101010)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
824
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
825 left = (leftkey_swap[(left >> 0) & 0xf] << 3) | (leftkey_swap[(left >> 8) & 0xf] << 2)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
826 | (leftkey_swap[(left >> 16) & 0xf] << 1) | (leftkey_swap[(left >> 24) & 0xf])
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
827 | (leftkey_swap[(left >> 5) & 0xf] << 7) | (leftkey_swap[(left >> 13) & 0xf] << 6)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
828 | (leftkey_swap[(left >> 21) & 0xf] << 5) | (leftkey_swap[(left >> 29) & 0xf] << 4);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
829
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
830 left &= 0x0fffffff;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
831
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
832 right = (rightkey_swap[(right >> 1) & 0xf] << 3) | (rightkey_swap[(right >> 9) & 0xf] << 2)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
833 | (rightkey_swap[(right >> 17) & 0xf] << 1) | (rightkey_swap[(right >> 25) & 0xf])
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
834 | (rightkey_swap[(right >> 4) & 0xf] << 7) | (rightkey_swap[(right >> 12) & 0xf] << 6)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
835 | (rightkey_swap[(right >> 20) & 0xf] << 5) | (rightkey_swap[(right >> 28) & 0xf] << 4);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
836
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
837 right &= 0x0fffffff;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
838
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
839 for (round = 0; round < 16; ++round)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
840 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
841 left = ((left << encrypt_rotate_tab[round]) | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
842 right = ((right << encrypt_rotate_tab[round]) | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
843
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
844 *subkey++ = ((left << 4) & 0x24000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
845 | ((left << 28) & 0x10000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
846 | ((left << 14) & 0x08000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
847 | ((left << 18) & 0x02080000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
848 | ((left << 6) & 0x01000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
849 | ((left << 9) & 0x00200000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
850 | ((left >> 1) & 0x00100000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
851 | ((left << 10) & 0x00040000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
852 | ((left << 2) & 0x00020000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
853 | ((left >> 10) & 0x00010000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
854 | ((right >> 13) & 0x00002000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
855 | ((right >> 4) & 0x00001000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
856 | ((right << 6) & 0x00000800)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
857 | ((right >> 1) & 0x00000400)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
858 | ((right >> 14) & 0x00000200)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
859 | (right & 0x00000100)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
860 | ((right >> 5) & 0x00000020)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
861 | ((right >> 10) & 0x00000010)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
862 | ((right >> 3) & 0x00000008)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
863 | ((right >> 18) & 0x00000004)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
864 | ((right >> 26) & 0x00000002)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
865 | ((right >> 24) & 0x00000001);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
866
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
867 *subkey++ = ((left << 15) & 0x20000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
868 | ((left << 17) & 0x10000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
869 | ((left << 10) & 0x08000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
870 | ((left << 22) & 0x04000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
871 | ((left >> 2) & 0x02000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
872 | ((left << 1) & 0x01000000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
873 | ((left << 16) & 0x00200000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
874 | ((left << 11) & 0x00100000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
875 | ((left << 3) & 0x00080000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
876 | ((left >> 6) & 0x00040000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
877 | ((left << 15) & 0x00020000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
878 | ((left >> 4) & 0x00010000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
879 | ((right >> 2) & 0x00002000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
880 | ((right << 8) & 0x00001000)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
881 | ((right >> 14) & 0x00000808)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
882 | ((right >> 9) & 0x00000400)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
883 | ((right) & 0x00000200)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
884 | ((right << 7) & 0x00000100)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
885 | ((right >> 7) & 0x00000020)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
886 | ((right >> 3) & 0x00000011)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
887 | ((right << 2) & 0x00000004)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
888 | ((right >> 21) & 0x00000002);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
889 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
890 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
891
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
892
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
893
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
894 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
895 * Fill a DES context with subkeys calculated from a 64bit key.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
896 * Does not check parity bits, but simply ignore them.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
897 * Does not check for weak keys.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
898 **/
11597
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
899 static void
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
900 des_set_key (GaimCipherContext *context, guchar * key)
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
901 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
902 struct _des_ctx *ctx = gaim_cipher_context_get_data(context);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
903 int i;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
904
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
905 des_key_schedule (key, ctx->encrypt_subkeys);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
906
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
907 for(i=0; i<32; i+=2)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
908 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
909 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30-i];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
910 ctx->decrypt_subkeys[i+1] = ctx->encrypt_subkeys[31-i];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
911 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
912 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
913
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
914
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
915
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
916 /*
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
917 * Electronic Codebook Mode DES encryption/decryption of data according
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
918 * to 'mode'.
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
919 **/
11597
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
920 static int
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
921 des_ecb_crypt (struct _des_ctx *ctx, const guint8 * from, guint8 * to, int mode)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
922 {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
923 guint32 left, right, work;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
924 guint32 *keys;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
925
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
926 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
927
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
928 READ_64BIT_DATA (from, left, right)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
929 INITIAL_PERMUTATION (left, work, right)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
930
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
931 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
932 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
933 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
934 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
935 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
936 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
937 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
938 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
939
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
940 FINAL_PERMUTATION (right, work, left)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
941 WRITE_64BIT_DATA (to, right, left)
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
942
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
943 return 0;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
944 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
945
11597
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
946 static gint
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
947 des_encrypt(GaimCipherContext *context, const guchar data[],
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
948 size_t len, guchar output[], size_t *outlen) {
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
949 int offset = 0;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
950 int i = 0;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
951 int tmp;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
952 guint8 buf[8] = {0,0,0,0,0,0,0,0};
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
953 while(offset+8<=len) {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
954 des_ecb_crypt(gaim_cipher_context_get_data(context),
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
955 data+offset,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
956 output+offset,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
957 0);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
958 offset+=8;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
959 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
960 *outlen = len;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
961 if(offset<len) {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
962 *outlen += len - offset;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
963 tmp = offset;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
964 while(tmp<len) {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
965 buf[i++] = data[tmp];
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
966 tmp++;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
967 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
968 des_ecb_crypt(gaim_cipher_context_get_data(context),
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
969 buf,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
970 output+offset,
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
971 0);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
972 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
973 return 0;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
974 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
975
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
976 static void
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
977 des_init(GaimCipherContext *context, gpointer extra) {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
978 struct _des_ctx *mctx;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
979 mctx = g_new0(struct _des_ctx, 1);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
980 gaim_cipher_context_set_data(context, mctx);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
981 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
982
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
983 static void
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
984 des_uninit(GaimCipherContext *context) {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
985 struct _des_ctx *des_context;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
986
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
987 des_context = gaim_cipher_context_get_data(context);
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
988 memset(des_context, 0, sizeof(des_context));
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
989
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
990 g_free(des_context);
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
991 des_context = NULL;
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
992 }
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
993
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
994 static GaimCipherOps DESOps = {
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
995 NULL, /* Set option */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
996 NULL, /* Get option */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
997 des_init, /* init */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
998 NULL, /* reset */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
999 des_uninit, /* uninit */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1000 NULL, /* set iv */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1001 NULL, /* append */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1002 NULL, /* digest */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1003 des_encrypt, /* encrypt */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1004 NULL, /* decrypt */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1005 NULL, /* set salt */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1006 NULL, /* get salt size */
11597
036bd21c5560 [gaim-migrate @ 13867]
Ethan Blanton <elb@pidgin.im>
parents: 11335
diff changeset
1007 des_set_key, /* set key */
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1008 NULL /* get key size */
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1009 };
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1010
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1011
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
1012 /*******************************************************************************
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1013 * SHA-1
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1014 ******************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1015 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1016
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1017 struct SHA1Context {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1018 guint32 H[5];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1019 guint32 W[80];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1020
13218
c01aa6ea4947 [gaim-migrate @ 15582]
Richard Laager <rlaager@wiktel.com>
parents: 12389
diff changeset
1021 gint lenW;
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1022
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1023 guint32 sizeHi;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1024 guint32 sizeLo;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1025 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1026
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1027 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1028 sha1_hash_block(struct SHA1Context *sha1_ctx) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1029 gint i;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1030 guint32 A, B, C, D, E, T;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1031
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1032 for(i = 16; i < 80; i++) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1033 sha1_ctx->W[i] = SHA1_ROTL(sha1_ctx->W[i - 3] ^
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1034 sha1_ctx->W[i - 8] ^
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1035 sha1_ctx->W[i - 14] ^
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1036 sha1_ctx->W[i - 16], 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1037 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1038
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1039 A = sha1_ctx->H[0];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1040 B = sha1_ctx->H[1];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1041 C = sha1_ctx->H[2];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1042 D = sha1_ctx->H[3];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1043 E = sha1_ctx->H[4];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1044
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1045 for(i = 0; i < 20; i++) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1046 T = (SHA1_ROTL(A, 5) + (((C ^ D) & B) ^ D) + E + sha1_ctx->W[i] + 0x5A827999) & 0xFFFFFFFF;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1047 E = D;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1048 D = C;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1049 C = SHA1_ROTL(B, 30);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1050 B = A;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1051 A = T;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1052 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1053
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1054 for(i = 20; i < 40; i++) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1055 T = (SHA1_ROTL(A, 5) + (B ^ C ^ D) + E + sha1_ctx->W[i] + 0x6ED9EBA1) & 0xFFFFFFFF;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1056 E = D;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1057 D = C;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1058 C = SHA1_ROTL(B, 30);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1059 B = A;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1060 A = T;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1061 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1062
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1063 for(i = 40; i < 60; i++) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1064 T = (SHA1_ROTL(A, 5) + ((B & C) | (D & (B | C))) + E + sha1_ctx->W[i] + 0x8F1BBCDC) & 0xFFFFFFFF;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1065 E = D;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1066 D = C;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1067 C = SHA1_ROTL(B, 30);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1068 B = A;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1069 A = T;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1070 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1071
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1072 for(i = 60; i < 80; i++) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1073 T = (SHA1_ROTL(A, 5) + (B ^ C ^ D) + E + sha1_ctx->W[i] + 0xCA62C1D6) & 0xFFFFFFFF;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1074 E = D;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1075 D = C;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1076 C = SHA1_ROTL(B, 30);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1077 B = A;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1078 A = T;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1079 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1080
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1081 sha1_ctx->H[0] += A;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1082 sha1_ctx->H[1] += B;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1083 sha1_ctx->H[2] += C;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1084 sha1_ctx->H[3] += D;
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1085 sha1_ctx->H[4] += E;
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1086 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1087
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1088 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1089 sha1_set_opt(GaimCipherContext *context, const gchar *name, void *value) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1090 struct SHA1Context *ctx;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1091
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1092 ctx = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1093
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1094 if(!strcmp(name, "sizeHi")) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1095 ctx->sizeHi = GPOINTER_TO_INT(value);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1096 } else if(!strcmp(name, "sizeLo")) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1097 ctx->sizeLo = GPOINTER_TO_INT(value);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1098 } else if(!strcmp(name, "lenW")) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1099 ctx->lenW = GPOINTER_TO_INT(value);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1100 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1101 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1102
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1103 static void *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1104 sha1_get_opt(GaimCipherContext *context, const gchar *name) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1105 struct SHA1Context *ctx;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1106
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1107 ctx = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1108
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1109 if(!strcmp(name, "sizeHi")) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1110 return GINT_TO_POINTER(ctx->sizeHi);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1111 } else if(!strcmp(name, "sizeLo")) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1112 return GINT_TO_POINTER(ctx->sizeLo);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1113 } else if(!strcmp(name, "lenW")) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1114 return GINT_TO_POINTER(ctx->lenW);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1115 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1116
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1117 return NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1118 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1119
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1120 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1121 sha1_init(GaimCipherContext *context, void *extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1122 struct SHA1Context *sha1_ctx;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1123
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1124 sha1_ctx = g_new0(struct SHA1Context, 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1125
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1126 gaim_cipher_context_set_data(context, sha1_ctx);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1127
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1128 gaim_cipher_context_reset(context, extra);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1129 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1130
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1131 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1132 sha1_reset(GaimCipherContext *context, void *extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1133 struct SHA1Context *sha1_ctx;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1134 gint i;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1135
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1136 sha1_ctx = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1137
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1138 g_return_if_fail(sha1_ctx);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1139
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1140 sha1_ctx->lenW = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1141 sha1_ctx->sizeHi = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1142 sha1_ctx->sizeLo = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1143
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1144 sha1_ctx->H[0] = 0x67452301;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1145 sha1_ctx->H[1] = 0xEFCDAB89;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1146 sha1_ctx->H[2] = 0x98BADCFE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1147 sha1_ctx->H[3] = 0x10325476;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1148 sha1_ctx->H[4] = 0xC3D2E1F0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1149
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1150 for(i = 0; i < 80; i++)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1151 sha1_ctx->W[i] = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1152 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1153
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1154 static void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1155 sha1_uninit(GaimCipherContext *context) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1156 struct SHA1Context *sha1_ctx;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1157
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1158 gaim_cipher_context_reset(context, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1159
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1160 sha1_ctx = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1161
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1162 memset(sha1_ctx, 0, sizeof(struct SHA1Context));
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1163
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1164 g_free(sha1_ctx);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1165 sha1_ctx = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1166 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1167
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1168
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1169 static void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1170 sha1_append(GaimCipherContext *context, const guchar *data, size_t len) {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1171 struct SHA1Context *sha1_ctx;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1172 gint i;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1173
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1174 sha1_ctx = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1175
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1176 g_return_if_fail(sha1_ctx);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1177
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1178 for(i = 0; i < len; i++) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1179 sha1_ctx->W[sha1_ctx->lenW / 4] <<= 8;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1180 sha1_ctx->W[sha1_ctx->lenW / 4] |= data[i];
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1181
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1182 if((++sha1_ctx->lenW) % 64 == 0) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1183 sha1_hash_block(sha1_ctx);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1184 sha1_ctx->lenW = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1185 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1186
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1187 sha1_ctx->sizeLo += 8;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1188 sha1_ctx->sizeHi += (sha1_ctx->sizeLo < 8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1189 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1190 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1191
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1192 static gboolean
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1193 sha1_digest(GaimCipherContext *context, size_t in_len, guchar digest[20],
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1194 size_t *out_len)
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1195 {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1196 struct SHA1Context *sha1_ctx;
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1197 guchar pad0x80 = 0x80, pad0x00 = 0x00;
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1198 guchar padlen[8];
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1199 gint i;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1200
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1201 g_return_val_if_fail(in_len >= 20, FALSE);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1202
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1203 sha1_ctx = gaim_cipher_context_get_data(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1204
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1205 g_return_val_if_fail(sha1_ctx, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1206
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1207 padlen[0] = (guchar)((sha1_ctx->sizeHi >> 24) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1208 padlen[1] = (guchar)((sha1_ctx->sizeHi >> 16) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1209 padlen[2] = (guchar)((sha1_ctx->sizeHi >> 8) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1210 padlen[3] = (guchar)((sha1_ctx->sizeHi >> 0) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1211 padlen[4] = (guchar)((sha1_ctx->sizeLo >> 24) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1212 padlen[5] = (guchar)((sha1_ctx->sizeLo >> 16) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1213 padlen[6] = (guchar)((sha1_ctx->sizeLo >> 8) & 255);
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1214 padlen[7] = (guchar)((sha1_ctx->sizeLo >> 0) & 255);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1215
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1216 /* pad with a 1, then zeroes, then length */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1217 gaim_cipher_context_append(context, &pad0x80, 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1218 while(sha1_ctx->lenW != 56)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1219 gaim_cipher_context_append(context, &pad0x00, 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1220 gaim_cipher_context_append(context, padlen, 8);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1221
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1222 for(i = 0; i < 20; i++) {
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1223 digest[i] = (guchar)(sha1_ctx->H[i / 4] >> 24);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1224 sha1_ctx->H[i / 4] <<= 8;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1225 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1226
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1227 gaim_cipher_context_reset(context, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1228
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1229 if(out_len)
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1230 *out_len = 20;
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1231
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1232 return TRUE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1233 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1234
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1235 static GaimCipherOps SHA1Ops = {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1236 sha1_set_opt, /* Set Option */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1237 sha1_get_opt, /* Get Option */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1238 sha1_init, /* init */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1239 sha1_reset, /* reset */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1240 sha1_uninit, /* uninit */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1241 NULL, /* set iv */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1242 sha1_append, /* append */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1243 sha1_digest, /* digest */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1244 NULL, /* encrypt */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1245 NULL, /* decrypt */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1246 NULL, /* set salt */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1247 NULL, /* get salt size */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1248 NULL, /* set key */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1249 NULL /* get key size */
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1250 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1251
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1252 /*******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1253 * Structs
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1254 ******************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1255 struct _GaimCipher {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1256 gchar *name;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1257 GaimCipherOps *ops;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1258 guint ref;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1259 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1260
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1261 struct _GaimCipherContext {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1262 GaimCipher *cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1263 gpointer data;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1264 };
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1265
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1266 /******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1267 * Globals
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1268 *****************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1269 static GList *ciphers = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1270
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1271 /******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1272 * GaimCipher API
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1273 *****************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1274 const gchar *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1275 gaim_cipher_get_name(GaimCipher *cipher) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1276 g_return_val_if_fail(cipher, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1277
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1278 return cipher->name;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1279 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1280
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1281 guint
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1282 gaim_cipher_get_capabilities(GaimCipher *cipher) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1283 GaimCipherOps *ops = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1284 guint caps = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1285
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1286 g_return_val_if_fail(cipher, 0);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1287
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1288 ops = cipher->ops;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1289 g_return_val_if_fail(ops, 0);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1290
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1291 if(ops->set_option)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1292 caps |= GAIM_CIPHER_CAPS_SET_OPT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1293 if(ops->get_option)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1294 caps |= GAIM_CIPHER_CAPS_GET_OPT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1295 if(ops->init)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1296 caps |= GAIM_CIPHER_CAPS_INIT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1297 if(ops->reset)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1298 caps |= GAIM_CIPHER_CAPS_RESET;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1299 if(ops->uninit)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1300 caps |= GAIM_CIPHER_CAPS_UNINIT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1301 if(ops->set_iv)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1302 caps |= GAIM_CIPHER_CAPS_SET_IV;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1303 if(ops->append)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1304 caps |= GAIM_CIPHER_CAPS_APPEND;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1305 if(ops->digest)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1306 caps |= GAIM_CIPHER_CAPS_DIGEST;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1307 if(ops->encrypt)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1308 caps |= GAIM_CIPHER_CAPS_ENCRYPT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1309 if(ops->decrypt)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1310 caps |= GAIM_CIPHER_CAPS_DECRYPT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1311 if(ops->set_salt)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1312 caps |= GAIM_CIPHER_CAPS_SET_SALT;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1313 if(ops->get_salt_size)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1314 caps |= GAIM_CIPHER_CAPS_GET_SALT_SIZE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1315 if(ops->set_key)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1316 caps |= GAIM_CIPHER_CAPS_SET_KEY;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1317 if(ops->get_key_size)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1318 caps |= GAIM_CIPHER_CAPS_GET_KEY_SIZE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1319
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1320 return caps;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1321 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1322
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1323 gboolean
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1324 gaim_cipher_digest_region(const gchar *name, const guchar *data,
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1325 size_t data_len, size_t in_len,
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1326 guchar digest[], size_t *out_len)
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1327 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1328 GaimCipher *cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1329 GaimCipherContext *context;
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1330 gboolean ret = FALSE;
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1331
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1332 g_return_val_if_fail(name, FALSE);
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1333 g_return_val_if_fail(data, FALSE);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1334
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1335 cipher = gaim_ciphers_find_cipher(name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1336
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1337 g_return_val_if_fail(cipher, FALSE);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1338
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1339 if(!cipher->ops->append || !cipher->ops->digest) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1340 gaim_debug_info("cipher", "gaim_cipher_region failed: "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1341 "the %s cipher does not support appending and or "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1342 "digesting.", cipher->name);
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1343 return FALSE;
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1344 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1345
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1346 context = gaim_cipher_context_new(cipher, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1347 gaim_cipher_context_append(context, data, data_len);
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1348 ret = gaim_cipher_context_digest(context, in_len, digest, out_len);
11143
5c56223fa24f [gaim-migrate @ 13207]
Mark Doliner <mark@kingant.net>
parents: 10687
diff changeset
1349 gaim_cipher_context_destroy(context);
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1350
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1351 return ret;
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1352 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1353
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1354 /******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1355 * GaimCiphers API
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1356 *****************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1357 GaimCipher *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1358 gaim_ciphers_find_cipher(const gchar *name) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1359 GaimCipher *cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1360 GList *l;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1361
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1362 g_return_val_if_fail(name, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1363
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1364 for(l = ciphers; l; l = l->next) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1365 cipher = GAIM_CIPHER(l->data);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1366
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1367 if(!g_ascii_strcasecmp(cipher->name, name))
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1368 return cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1369 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1370
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1371 return NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1372 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1373
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1374 GaimCipher *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1375 gaim_ciphers_register_cipher(const gchar *name, GaimCipherOps *ops) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1376 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1377
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1378 g_return_val_if_fail(name, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1379 g_return_val_if_fail(ops, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1380 g_return_val_if_fail(!gaim_ciphers_find_cipher(name), NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1381
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1382 cipher = g_new0(GaimCipher, 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1383
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1384 cipher->name = g_strdup(name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1385 cipher->ops = ops;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1386
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1387 ciphers = g_list_append(ciphers, cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1388
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1389 gaim_signal_emit(gaim_ciphers_get_handle(), "cipher-added", cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1390
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1391 return cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1392 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1393
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1394 gboolean
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1395 gaim_ciphers_unregister_cipher(GaimCipher *cipher) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1396 g_return_val_if_fail(cipher, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1397 g_return_val_if_fail(cipher->ref == 0, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1398
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1399 gaim_signal_emit(gaim_ciphers_get_handle(), "cipher-removed", cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1400
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1401 ciphers = g_list_remove(ciphers, cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1402
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1403 g_free(cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1404 g_free(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1405
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1406 return TRUE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1407 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1408
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1409 GList *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1410 gaim_ciphers_get_ciphers() {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1411 return ciphers;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1412 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1413
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1414 /******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1415 * GaimCipher Subsystem API
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1416 *****************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1417 gpointer
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1418 gaim_ciphers_get_handle() {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1419 static gint handle;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1420
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1421 return &handle;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1422 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1423
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1424 void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1425 gaim_ciphers_init() {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1426 gpointer handle;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1427
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1428 handle = gaim_ciphers_get_handle();
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1429
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1430 gaim_signal_register(handle, "cipher-added",
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1431 gaim_marshal_VOID__POINTER, NULL, 1,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1432 gaim_value_new(GAIM_TYPE_SUBTYPE,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1433 GAIM_SUBTYPE_CIPHER));
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1434 gaim_signal_register(handle, "cipher-removed",
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1435 gaim_marshal_VOID__POINTER, NULL, 1,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1436 gaim_value_new(GAIM_TYPE_SUBTYPE,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1437 GAIM_SUBTYPE_CIPHER));
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1438
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1439 gaim_ciphers_register_cipher("md5", &MD5Ops);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1440 gaim_ciphers_register_cipher("sha1", &SHA1Ops);
11329
f93d434ee222 [gaim-migrate @ 13541]
Thomas Butter <tbutter>
parents: 11183
diff changeset
1441 gaim_ciphers_register_cipher("md4", &MD4Ops);
11335
59aa7080eb2d [gaim-migrate @ 13548]
Thomas Butter <tbutter>
parents: 11333
diff changeset
1442 gaim_ciphers_register_cipher("des", &DESOps);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1443 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1444
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1445 void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1446 gaim_ciphers_uninit() {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1447 GaimCipher *cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1448 GList *l, *ll;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1449
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1450 for(l = ciphers; l; l = ll) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1451 ll = l->next;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1452
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1453 cipher = GAIM_CIPHER(l->data);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1454 gaim_ciphers_unregister_cipher(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1455
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1456 ciphers = g_list_remove(ciphers, cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1457 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1458
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1459 g_list_free(ciphers);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1460
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1461 gaim_signals_unregister_by_instance(gaim_ciphers_get_handle());
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1462 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1463 /******************************************************************************
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1464 * GaimCipherContext API
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1465 *****************************************************************************/
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1466 void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1467 gaim_cipher_context_set_option(GaimCipherContext *context, const gchar *name,
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1468 gpointer value)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1469 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1470 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1471
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1472 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1473 g_return_if_fail(name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1474
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1475 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1476 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1477
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1478 if(cipher->ops && cipher->ops->set_option)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1479 cipher->ops->set_option(context, name, value);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1480 else
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1481 gaim_debug_info("cipher", "the %s cipher does not support the "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1482 "set_option operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1483 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1484
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1485 gpointer
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1486 gaim_cipher_context_get_option(GaimCipherContext *context, const gchar *name) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1487 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1488
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1489 g_return_val_if_fail(context, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1490 g_return_val_if_fail(name, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1491
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1492 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1493 g_return_val_if_fail(cipher, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1494
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1495 if(cipher->ops && cipher->ops->get_option)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1496 return cipher->ops->get_option(context, name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1497 else {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1498 gaim_debug_info("cipher", "the %s cipher does not support the "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1499 "get_option operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1500
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1501 return NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1502 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1503 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1504
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1505 GaimCipherContext *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1506 gaim_cipher_context_new(GaimCipher *cipher, void *extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1507 GaimCipherContext *context = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1508
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1509 g_return_val_if_fail(cipher, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1510
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1511 cipher->ref++;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1512
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1513 context = g_new0(GaimCipherContext, 1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1514 context->cipher = cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1515
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1516 if(cipher->ops->init)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1517 cipher->ops->init(context, extra);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1518
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1519 return context;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1520 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1521
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1522 GaimCipherContext *
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1523 gaim_cipher_context_new_by_name(const gchar *name, void *extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1524 GaimCipher *cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1525
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1526 g_return_val_if_fail(name, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1527
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1528 cipher = gaim_ciphers_find_cipher(name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1529
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1530 g_return_val_if_fail(cipher, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1531
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1532 return gaim_cipher_context_new(cipher, extra);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1533 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1534
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1535 void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1536 gaim_cipher_context_reset(GaimCipherContext *context, void *extra) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1537 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1538
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1539 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1540
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1541 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1542 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1543
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1544 if(cipher->ops && cipher->ops->reset)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1545 context->cipher->ops->reset(context, extra);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1546 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1547
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1548 void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1549 gaim_cipher_context_destroy(GaimCipherContext *context) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1550 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1551
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1552 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1553
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1554 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1555 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1556
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1557 cipher->ref--;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1558
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1559 if(cipher->ops && cipher->ops->uninit)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1560 cipher->ops->uninit(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1561
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1562 memset(context, 0, sizeof(context));
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1563 g_free(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1564 context = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1565 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1566
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1567 void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1568 gaim_cipher_context_set_iv(GaimCipherContext *context, guchar *iv, size_t len)
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1569 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1570 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1571
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1572 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1573 g_return_if_fail(iv);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1574
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1575 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1576 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1577
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1578 if(cipher->ops && cipher->ops->set_iv)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1579 cipher->ops->set_iv(context, iv, len);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1580 else
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1581 gaim_debug_info("cipher", "the %s cipher does not support the set"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1582 "initialization vector operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1583 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1584
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1585 void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1586 gaim_cipher_context_append(GaimCipherContext *context, const guchar *data,
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1587 size_t len)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1588 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1589 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1590
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1591 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1592
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1593 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1594 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1595
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1596 if(cipher->ops && cipher->ops->append)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1597 cipher->ops->append(context, data, len);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1598 else
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1599 gaim_debug_info("cipher", "the %s cipher does not support the append "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1600 "operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1601 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1602
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1603 gboolean
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1604 gaim_cipher_context_digest(GaimCipherContext *context, size_t in_len,
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1605 guchar digest[], size_t *out_len)
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1606 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1607 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1608
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1609 g_return_val_if_fail(context, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1610
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1611 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1612 g_return_val_if_fail(context, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1613
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1614 if(cipher->ops && cipher->ops->digest)
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1615 return cipher->ops->digest(context, in_len, digest, out_len);
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1616 else {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1617 gaim_debug_info("cipher", "the %s cipher does not support the digest "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1618 "operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1619 return FALSE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1620 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1621 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1622
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1623 gboolean
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1624 gaim_cipher_context_digest_to_str(GaimCipherContext *context, size_t in_len,
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1625 gchar digest_s[], size_t *out_len)
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1626 {
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1627 /* 8k is a bit excessive, will tweak later. */
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1628 guchar digest[BUF_LEN * 4];
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1629 gint n = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1630 size_t dlen = 0;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1631
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1632 g_return_val_if_fail(context, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1633 g_return_val_if_fail(digest_s, FALSE);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1634
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1635 if(!gaim_cipher_context_digest(context, sizeof(digest), digest, &dlen))
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1636 return FALSE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1637
12388
4e045668b9d0 [gaim-migrate @ 14694]
Richard Laager <rlaager@wiktel.com>
parents: 12382
diff changeset
1638 /* in_len must be greater than dlen * 2 so we have room for the NUL. */
4e045668b9d0 [gaim-migrate @ 14694]
Richard Laager <rlaager@wiktel.com>
parents: 12382
diff changeset
1639 if(in_len <= dlen * 2)
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1640 return FALSE;
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1641
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1642 for(n = 0; n < dlen; n++)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1643 sprintf(digest_s + (n * 2), "%02x", digest[n]);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1644
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1645 digest_s[n * 2] = '\0';
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1646
10687
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1647 if(out_len)
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1648 *out_len = dlen * 2;
b256ce6b85b8 [gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents: 10684
diff changeset
1649
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1650 return TRUE;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1651 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1652
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1653 gint
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1654 gaim_cipher_context_encrypt(GaimCipherContext *context, const guchar data[],
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1655 size_t len, guchar output[], size_t *outlen)
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1656 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1657 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1658
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1659 g_return_val_if_fail(context, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1660
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1661 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1662 g_return_val_if_fail(cipher, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1663
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1664 if(cipher->ops && cipher->ops->encrypt)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1665 return cipher->ops->encrypt(context, data, len, output, outlen);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1666 else {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1667 gaim_debug_info("cipher", "the %s cipher does not support the encrypt"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1668 "operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1669
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1670 if(outlen)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1671 *outlen = -1;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1672
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1673 return -1;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1674 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1675 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1676
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1677 gint
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1678 gaim_cipher_context_decrypt(GaimCipherContext *context, const guchar data[],
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1679 size_t len, guchar output[], size_t *outlen)
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1680 {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1681 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1682
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1683 g_return_val_if_fail(context, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1685 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1686 g_return_val_if_fail(cipher, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1687
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1688 if(cipher->ops && cipher->ops->decrypt)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1689 return cipher->ops->decrypt(context, data, len, output, outlen);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1690 else {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1691 gaim_debug_info("cipher", "the %s cipher does not support the decrypt"
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1692 "operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1693
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1694 if(outlen)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1695 *outlen = -1;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1696
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1697 return -1;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1698 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1699 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1700
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1701 void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1702 gaim_cipher_context_set_salt(GaimCipherContext *context, guchar *salt) {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1703 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1704
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1705 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1706
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1707 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1708 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1709
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1710 if(cipher->ops && cipher->ops->set_salt)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1711 cipher->ops->set_salt(context, salt);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1712 else
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1713 gaim_debug_info("cipher", "the %s cipher does not support the "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1714 "set_salt operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1715 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1716
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1717 size_t
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1718 gaim_cipher_context_get_salt_size(GaimCipherContext *context) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1719 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1720
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1721 g_return_val_if_fail(context, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1722
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1723 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1724 g_return_val_if_fail(cipher, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1725
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1726 if(cipher->ops && cipher->ops->get_salt_size)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1727 return cipher->ops->get_salt_size(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1728 else {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1729 gaim_debug_info("cipher", "the %s cipher does not support the "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1730 "get_salt_size operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1731
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1732 return -1;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1733 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1734 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1735
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1736 void
11183
8dca96cbcd64 [gaim-migrate @ 13295]
Mark Doliner <mark@kingant.net>
parents: 11143
diff changeset
1737 gaim_cipher_context_set_key(GaimCipherContext *context, guchar *key) {
10684
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1738 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1739
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1740 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1741
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1742 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1743 g_return_if_fail(cipher);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1744
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1745 if(cipher->ops && cipher->ops->set_key)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1746 cipher->ops->set_key(context, key);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1747 else
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1748 gaim_debug_info("cipher", "the %s cipher does not support the "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1749 "set_key operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1750 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1751
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1752 size_t
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1753 gaim_cipher_context_get_key_size(GaimCipherContext *context) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1754 GaimCipher *cipher = NULL;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1755
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1756 g_return_val_if_fail(context, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1757
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1758 cipher = context->cipher;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1759 g_return_val_if_fail(cipher, -1);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1760
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1761 if(cipher->ops && cipher->ops->get_key_size)
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1762 return cipher->ops->get_key_size(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1763 else {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1764 gaim_debug_info("cipher", "the %s cipher does not support the "
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1765 "get_key_size operation\n", cipher->name);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1766
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1767 return -1;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1768 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1769 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1770
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1771 void
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1772 gaim_cipher_context_set_data(GaimCipherContext *context, gpointer data) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1773 g_return_if_fail(context);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1774
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1775 context->data = data;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1776 }
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1777
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1778 gpointer
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1779 gaim_cipher_context_get_data(GaimCipherContext *context) {
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1780 g_return_val_if_fail(context, NULL);
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1781
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1782 return context->data;
72a5babfa8b4 [gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
diff changeset
1783 }
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1784
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1785 gchar *gaim_cipher_http_digest_calculate_session_key(
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1786 const gchar *algorithm,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1787 const gchar *username,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1788 const gchar *realm,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1789 const gchar *password,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1790 const gchar *nonce,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1791 const gchar *client_nonce)
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1792 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1793 GaimCipher *cipher;
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1794 GaimCipherContext *context;
12388
4e045668b9d0 [gaim-migrate @ 14694]
Richard Laager <rlaager@wiktel.com>
parents: 12382
diff changeset
1795 gchar hash[33]; /* We only support MD5. */
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1796
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1797 g_return_val_if_fail(username != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1798 g_return_val_if_fail(realm != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1799 g_return_val_if_fail(password != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1800 g_return_val_if_fail(nonce != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1801
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1802 /* Check for a supported algorithm. */
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1803 g_return_val_if_fail(algorithm == NULL ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1804 *algorithm == '\0' ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1805 strcasecmp(algorithm, "MD5") ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1806 strcasecmp(algorithm, "MD5-sess"), NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1807
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1808 cipher = gaim_ciphers_find_cipher("md5");
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1809 g_return_val_if_fail(cipher != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1810
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1811 context = gaim_cipher_context_new(cipher, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1812
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1813 gaim_cipher_context_append(context, (guchar *)username, strlen(username));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1814 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1815 gaim_cipher_context_append(context, (guchar *)realm, strlen(realm));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1816 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1817 gaim_cipher_context_append(context, (guchar *)password, strlen(password));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1818
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1819 if (algorithm != NULL && !strcasecmp(algorithm, "MD5-sess"))
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1820 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1821 guchar digest[16];
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1822
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1823 if (client_nonce == NULL)
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1824 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1825 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1826 gaim_debug_error("cipher", "Required client_nonce missing for MD5-sess digest calculation.");
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1827 return NULL;
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1828 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1829
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1830 gaim_cipher_context_digest(context, sizeof(digest), digest, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1831 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1832
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1833 context = gaim_cipher_context_new(cipher, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1834 gaim_cipher_context_append(context, digest, sizeof(digest));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1835 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1836 gaim_cipher_context_append(context, (guchar *)nonce, strlen(nonce));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1837 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1838 gaim_cipher_context_append(context, (guchar *)client_nonce, strlen(client_nonce));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1839 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1840
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1841 gaim_cipher_context_digest_to_str(context, sizeof(hash), hash, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1842 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1843
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1844 return g_strdup(hash);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1845 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1846
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1847 gchar *gaim_cipher_http_digest_calculate_response(
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1848 const gchar *algorithm,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1849 const gchar *method,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1850 const gchar *digest_uri,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1851 const gchar *qop,
12389
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1852 const gchar *entity,
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1853 const gchar *nonce,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1854 const gchar *nonce_count,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1855 const gchar *client_nonce,
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1856 const gchar *session_key)
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1857 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1858 GaimCipher *cipher;
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1859 GaimCipherContext *context;
12388
4e045668b9d0 [gaim-migrate @ 14694]
Richard Laager <rlaager@wiktel.com>
parents: 12382
diff changeset
1860 static gchar hash2[33]; /* We only support MD5. */
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1861
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1862 g_return_val_if_fail(method != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1863 g_return_val_if_fail(digest_uri != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1864 g_return_val_if_fail(nonce != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1865 g_return_val_if_fail(session_key != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1866
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1867 /* Check for a supported algorithm. */
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1868 g_return_val_if_fail(algorithm == NULL ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1869 *algorithm == '\0' ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1870 strcasecmp(algorithm, "MD5") ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1871 strcasecmp(algorithm, "MD5-sess"), NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1872
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1873 /* Check for a supported "quality of protection". */
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1874 g_return_val_if_fail(qop == NULL ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1875 *qop == '\0' ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1876 strcasecmp(qop, "auth") ||
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1877 strcasecmp(qop, "auth-int"), NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1878
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1879 cipher = gaim_ciphers_find_cipher("md5");
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1880 g_return_val_if_fail(cipher != NULL, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1881
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1882 context = gaim_cipher_context_new(cipher, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1883
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1884 gaim_cipher_context_append(context, (guchar *)method, strlen(method));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1885 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1886 gaim_cipher_context_append(context, (guchar *)digest_uri, strlen(digest_uri));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1887
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1888 if (qop != NULL && !strcasecmp(qop, "auth-int"))
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1889 {
12389
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1890 GaimCipherContext *context2;
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1891 gchar entity_hash[33];
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1892
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1893 if (entity == NULL)
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1894 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1895 gaim_cipher_context_destroy(context);
12389
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1896 gaim_debug_error("cipher", "Required entity missing for auth-int digest calculation.");
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1897 return NULL;
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1898 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1899
12389
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1900 context2 = gaim_cipher_context_new(cipher, NULL);
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1901 gaim_cipher_context_append(context2, (guchar *)entity, strlen(entity));
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1902 gaim_cipher_context_digest_to_str(context2, sizeof(entity_hash), entity_hash, NULL);
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1903 gaim_cipher_context_destroy(context2);
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1904
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1905 gaim_cipher_context_append(context, (guchar *)":", 1);
12389
e024601d45c7 [gaim-migrate @ 14695]
Richard Laager <rlaager@wiktel.com>
parents: 12388
diff changeset
1906 gaim_cipher_context_append(context, (guchar *)entity_hash, strlen(entity_hash));
12382
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1907 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1908
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1909 gaim_cipher_context_digest_to_str(context, sizeof(hash2), hash2, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1910 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1911
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1912 context = gaim_cipher_context_new(cipher, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1913 gaim_cipher_context_append(context, (guchar *)session_key, strlen(session_key));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1914 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1915 gaim_cipher_context_append(context, (guchar *)nonce, strlen(nonce));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1916 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1917
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1918 if (qop != NULL && *qop != '\0')
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1919 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1920 if (nonce_count == NULL)
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1921 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1922 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1923 gaim_debug_error("cipher", "Required nonce_count missing for digest calculation.");
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1924 return NULL;
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1925 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1926
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1927 if (client_nonce == NULL)
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1928 {
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1929 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1930 gaim_debug_error("cipher", "Required client_nonce missing for digest calculation.");
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1931 return NULL;
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1932 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1933
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1934 gaim_cipher_context_append(context, (guchar *)nonce_count, strlen(nonce_count));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1935 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1936 gaim_cipher_context_append(context, (guchar *)client_nonce, strlen(client_nonce));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1937 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1938
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1939 if (qop != NULL)
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1940 gaim_cipher_context_append(context, (guchar *)qop, strlen(qop));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1941 else
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1942 gaim_cipher_context_append(context, (guchar *)"", 0);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1943
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1944 gaim_cipher_context_append(context, (guchar *)":", 1);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1945 }
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1946
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1947 gaim_cipher_context_append(context, (guchar *)hash2, strlen(hash2));
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1948 gaim_cipher_context_digest_to_str(context, sizeof(hash2), hash2, NULL);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1949 gaim_cipher_context_destroy(context);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1950
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1951 return g_strdup(hash2);
cfc808463763 [gaim-migrate @ 14688]
Richard Laager <rlaager@wiktel.com>
parents: 11677
diff changeset
1952 }