libpurple/ciphers/md4hash.c

changeset 38413
e35c6b6ca094
parent 38412
e25c73450414
child 38414
3892b306732f
equal deleted inserted replaced
38412:e25c73450414 38413:e35c6b6ca094
1 /*
2 * Original md4 taken from linux kernel
3 * MD4 Message Digest Algorithm (RFC1320).
4 *
5 * Implementation derived from Andrew Tridgell and Steve French's
6 * CIFS MD4 implementation, and the cryptoapi implementation
7 * originally based on the public domain implementation written
8 * by Colin Plumb in 1993.
9 *
10 * Copyright (c) Andrew Tridgell 1997-1998.
11 * Modified by Steve French (sfrench@us.ibm.com) 2002
12 * Copyright (c) Cryptoapi developers.
13 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
14 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
15 */
16 #include "internal.h"
17 #include "md4hash.h"
18
19 #include <string.h>
20
21 #define MD4_DIGEST_SIZE 16
22 #define MD4_BLOCK_WORDS 16
23 #define MD4_HASH_WORDS 4
24
25 #define PURPLE_MD4_HASH_GET_PRIVATE(obj) \
26 (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MD4_HASH, PurpleMD4HashPrivate))
27
28 /******************************************************************************
29 * Structs
30 *****************************************************************************/
31 typedef struct {
32 guint32 hash[MD4_HASH_WORDS];
33 guint32 block[MD4_BLOCK_WORDS];
34 guint64 byte_count;
35 } PurpleMD4HashPrivate;
36
37 /******************************************************************************
38 * Globals
39 *****************************************************************************/
40 static GObjectClass *parent_class = NULL;
41
42 /******************************************************************************
43 * Helpers
44 *****************************************************************************/
45 #define ROUND1(a,b,c,d,k,s) \
46 (a = lshift(a + F(b,c,d) + k, s))
47
48 #define ROUND2(a,b,c,d,k,s) \
49 (a = lshift(a + G(b,c,d) + k + (guint32)0x5a827999,s))
50
51 #define ROUND3(a,b,c,d,k,s) \
52 (a = lshift(a + H(b,c,d) + k + (guint32)0x6ed9eba1,s))
53
54 static inline guint32
55 lshift(guint32 x, unsigned int s) {
56 x &= 0xffffffff;
57 return (((x << s) & 0xffffffff) | (x >> (32 - s)));
58 }
59
60 static inline guint32
61 F(guint32 x, guint32 y, guint32 z) {
62 return ((x & y) | ((~x) & z));
63 }
64
65 static inline guint32
66 G(guint32 x, guint32 y, guint32 z) {
67 return ((x & y) | (x & z) | (y & z));
68 }
69
70 static inline guint32
71 H(guint32 x, guint32 y, guint32 z) {
72 return (x ^ y ^ z);
73 }
74
75 static inline void
76 le32_to_cpu_array(guint32 *buf, unsigned int words) {
77 while(words--) {
78 *buf = GUINT_FROM_LE(*buf);
79 buf++;
80 }
81 }
82
83 static inline void
84 cpu_to_le32_array(guint32 *buf, unsigned int words) {
85 while(words--) {
86 *buf = GUINT_TO_LE(*buf);
87 buf++;
88 }
89 }
90
91 static void
92 md4_transform(guint32 *hash, guint32 const *in) {
93 guint32 a, b, c, d;
94
95 a = hash[0];
96 b = hash[1];
97 c = hash[2];
98 d = hash[3];
99
100 ROUND1(a, b, c, d, in[0], 3);
101 ROUND1(d, a, b, c, in[1], 7);
102 ROUND1(c, d, a, b, in[2], 11);
103 ROUND1(b, c, d, a, in[3], 19);
104 ROUND1(a, b, c, d, in[4], 3);
105 ROUND1(d, a, b, c, in[5], 7);
106 ROUND1(c, d, a, b, in[6], 11);
107 ROUND1(b, c, d, a, in[7], 19);
108 ROUND1(a, b, c, d, in[8], 3);
109 ROUND1(d, a, b, c, in[9], 7);
110 ROUND1(c, d, a, b, in[10], 11);
111 ROUND1(b, c, d, a, in[11], 19);
112 ROUND1(a, b, c, d, in[12], 3);
113 ROUND1(d, a, b, c, in[13], 7);
114 ROUND1(c, d, a, b, in[14], 11);
115 ROUND1(b, c, d, a, in[15], 19);
116
117 ROUND2(a, b, c, d,in[ 0], 3);
118 ROUND2(d, a, b, c, in[4], 5);
119 ROUND2(c, d, a, b, in[8], 9);
120 ROUND2(b, c, d, a, in[12], 13);
121 ROUND2(a, b, c, d, in[1], 3);
122 ROUND2(d, a, b, c, in[5], 5);
123 ROUND2(c, d, a, b, in[9], 9);
124 ROUND2(b, c, d, a, in[13], 13);
125 ROUND2(a, b, c, d, in[2], 3);
126 ROUND2(d, a, b, c, in[6], 5);
127 ROUND2(c, d, a, b, in[10], 9);
128 ROUND2(b, c, d, a, in[14], 13);
129 ROUND2(a, b, c, d, in[3], 3);
130 ROUND2(d, a, b, c, in[7], 5);
131 ROUND2(c, d, a, b, in[11], 9);
132 ROUND2(b, c, d, a, in[15], 13);
133
134 ROUND3(a, b, c, d,in[ 0], 3);
135 ROUND3(d, a, b, c, in[8], 9);
136 ROUND3(c, d, a, b, in[4], 11);
137 ROUND3(b, c, d, a, in[12], 15);
138 ROUND3(a, b, c, d, in[2], 3);
139 ROUND3(d, a, b, c, in[10], 9);
140 ROUND3(c, d, a, b, in[6], 11);
141 ROUND3(b, c, d, a, in[14], 15);
142 ROUND3(a, b, c, d, in[1], 3);
143 ROUND3(d, a, b, c, in[9], 9);
144 ROUND3(c, d, a, b, in[5], 11);
145 ROUND3(b, c, d, a, in[13], 15);
146 ROUND3(a, b, c, d, in[3], 3);
147 ROUND3(d, a, b, c, in[11], 9);
148 ROUND3(c, d, a, b, in[7], 11);
149 ROUND3(b, c, d, a, in[15], 15);
150
151 hash[0] += a;
152 hash[1] += b;
153 hash[2] += c;
154 hash[3] += d;
155 }
156
157 static inline void
158 md4_transform_helper(PurpleHash *hash) {
159 PurpleMD4HashPrivate *priv = PURPLE_MD4_HASH_GET_PRIVATE(hash);
160
161 le32_to_cpu_array(priv->block, sizeof(priv->block) / sizeof(guint32));
162 md4_transform(priv->hash, priv->block);
163 }
164
165 /******************************************************************************
166 * Hash Stuff
167 *****************************************************************************/
168 static void
169 purple_md4_hash_reset(PurpleHash *hash) {
170 PurpleMD4HashPrivate *priv = PURPLE_MD4_HASH_GET_PRIVATE(hash);
171
172 priv->hash[0] = 0x67452301;
173 priv->hash[1] = 0xefcdab89;
174 priv->hash[2] = 0x98badcfe;
175 priv->hash[3] = 0x10325476;
176
177 priv->byte_count = 0;
178
179 memset(priv->block, 0, sizeof(priv->block));
180 }
181
182 static void
183 purple_md4_hash_append(PurpleHash *hash, const guchar *data, size_t len) {
184 PurpleMD4HashPrivate *priv = PURPLE_MD4_HASH_GET_PRIVATE(hash);
185 const guint32 avail = sizeof(priv->block) - (priv->byte_count & 0x3f);
186
187 priv->byte_count += len;
188
189 if(avail > len) {
190 memcpy((char *)priv->block +
191 (sizeof(priv->block) - avail),
192 data, len);
193 return;
194 }
195
196 memcpy((char *)priv->block +
197 (sizeof(priv->block) - avail),
198 data, avail);
199
200 md4_transform_helper(hash);
201 data += avail;
202 len -= avail;
203
204 while(len >= sizeof(priv->block)) {
205 memcpy(priv->block, data, sizeof(priv->block));
206 md4_transform_helper(hash);
207 data += sizeof(priv->block);
208 len -= sizeof(priv->block);
209 }
210
211 memcpy(priv->block, data, len);
212 }
213
214 static gboolean
215 purple_md4_hash_digest(PurpleHash *hash, guchar *out, size_t len)
216 {
217 PurpleMD4HashPrivate *priv = PURPLE_MD4_HASH_GET_PRIVATE(hash);
218 const unsigned int offset = priv->byte_count & 0x3f;
219 gchar *p = (gchar *)priv->block + offset;
220 gint padding = 56 - (offset + 1);
221
222 if(len < 16)
223 return FALSE;
224
225 *p++ = 0x80;
226
227 if(padding < 0) {
228 memset(p, 0x00, padding + sizeof(guint64));
229 md4_transform_helper(hash);
230 p = (gchar *)priv->block;
231 padding = 56;
232 }
233
234 memset(p, 0, padding);
235 priv->block[14] = priv->byte_count << 3;
236 priv->block[15] = priv->byte_count >> 29;
237 le32_to_cpu_array(priv->block,
238 (sizeof(priv->block) - sizeof(guint64)) /
239 sizeof(guint32));
240 md4_transform(priv->hash, priv->block);
241 cpu_to_le32_array(priv->hash, sizeof(priv->hash) / sizeof(guint32));
242 memcpy(out, priv->hash, sizeof(priv->hash));
243
244 return TRUE;
245 }
246
247 static size_t
248 purple_md4_hash_get_digest_size(PurpleHash *hash)
249 {
250 return 16;
251 }
252
253 static size_t
254 purple_md4_hash_get_block_size(PurpleHash *hash)
255 {
256 /* This does not change (in this case) */
257 return 64;
258 }
259
260 /******************************************************************************
261 * Object Stuff
262 *****************************************************************************/
263 static void
264 purple_md4_hash_class_init(PurpleMD4HashClass *klass) {
265 PurpleHashClass *hash_class = PURPLE_HASH_CLASS(klass);
266
267 parent_class = g_type_class_peek_parent(klass);
268
269 g_type_class_add_private(klass, sizeof(PurpleMD4HashPrivate));
270
271 hash_class->reset = purple_md4_hash_reset;
272 hash_class->reset_state = purple_md4_hash_reset;
273 hash_class->append = purple_md4_hash_append;
274 hash_class->digest = purple_md4_hash_digest;
275 hash_class->get_digest_size = purple_md4_hash_get_digest_size;
276 hash_class->get_block_size = purple_md4_hash_get_block_size;
277 }
278
279 /******************************************************************************
280 * API
281 *****************************************************************************/
282 GType
283 purple_md4_hash_get_type(void) {
284 static GType type = 0;
285
286 if(type == 0) {
287 static const GTypeInfo info = {
288 sizeof(PurpleMD4HashClass),
289 NULL,
290 NULL,
291 (GClassInitFunc)purple_md4_hash_class_init,
292 NULL,
293 NULL,
294 sizeof(PurpleMD4Hash),
295 0,
296 (GInstanceInitFunc)purple_hash_reset,
297 NULL,
298 };
299
300 type = g_type_register_static(PURPLE_TYPE_HASH,
301 "PurpleMD4Hash",
302 &info, 0);
303 }
304
305 return type;
306 }
307
308 PurpleHash *
309 purple_md4_hash_new(void) {
310 return g_object_new(PURPLE_TYPE_MD4_HASH, NULL);
311 }

mercurial