Mercurial > pidgin
annotate src/cipher.c @ 11329:f93d434ee222
[gaim-migrate @ 13541]
added MD4 cipher needed for NTLM
committer: Tailor Script <tailor@pidgin.im>
author | Thomas Butter <tbutter> |
---|---|
date | Tue, 23 Aug 2005 08:08:43 +0000 |
parents | 8dca96cbcd64 |
children | b0185f9214d3 |
rev | line source |
---|---|
10684 | 1 /* |
2 * gaim | |
3 * | |
4 * Gaim is the legal property of its developers, whose names are too numerous | |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
7 * | |
8 * Original md5 | |
9 * Copyright (C) 2001-2003 Christophe Devine <c.devine@cr0.net> | |
10 * | |
11329 | 11 * Original md4 taken from linux kernel |
12 * MD4 Message Digest Algorithm (RFC1320). | |
13 * | |
14 * Implementation derived from Andrew Tridgell and Steve French's | |
15 * CIFS MD4 implementation, and the cryptoapi implementation | |
16 * originally based on the public domain implementation written | |
17 * by Colin Plumb in 1993. | |
18 * | |
19 * Copyright (c) Andrew Tridgell 1997-1998. | |
20 * Modified by Steve French (sfrench@us.ibm.com) 2002 | |
21 * Copyright (c) Cryptoapi developers. | |
22 * Copyright (c) 2002 David S. Miller (davem@redhat.com) | |
23 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> | |
24 * | |
10684 | 25 * This program is free software; you can redistribute it and/or modify |
26 * it under the terms of the GNU General Public License as published by | |
27 * the Free Software Foundation; either version 2 of the License, or | |
28 * (at your option) any later version. | |
29 * | |
30 * This program is distributed in the hope that it will be useful, | |
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
33 * GNU General Public License for more details. | |
34 * | |
35 * You should have received a copy of the GNU General Public License | |
36 * along with this program; if not, write to the Free Software | |
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
38 */ | |
39 #include <glib.h> | |
40 #include <string.h> | |
41 #include <stdio.h> | |
42 | |
43 #include "internal.h" | |
44 #include "cipher.h" | |
45 #include "debug.h" | |
46 #include "signals.h" | |
47 #include "value.h" | |
48 | |
49 /******************************************************************************* | |
50 * MD5 | |
51 ******************************************************************************/ | |
52 struct MD5Context { | |
53 guint32 total[2]; | |
54 guint32 state[4]; | |
11183 | 55 guchar buffer[64]; |
10684 | 56 }; |
57 | |
58 #define MD5_GET_GUINT32(n,b,i) { \ | |
59 (n) = ((guint32)(b) [(i) ] ) \ | |
60 | ((guint32)(b) [(i) + 1] << 8) \ | |
61 | ((guint32)(b) [(i) + 2] << 16) \ | |
62 | ((guint32)(b) [(i) + 3] << 24); \ | |
63 } | |
11183 | 64 #define MD5_PUT_GUINT32(n,b,i) { \ |
65 (b)[(i) ] = (guchar)((n) ); \ | |
66 (b)[(i) + 1] = (guchar)((n) >> 8); \ | |
67 (b)[(i) + 2] = (guchar)((n) >> 16); \ | |
68 (b)[(i) + 3] = (guchar)((n) >> 24); \ | |
10684 | 69 } |
70 | |
71 static void | |
72 md5_init(GaimCipherContext *context, gpointer extra) { | |
73 struct MD5Context *md5_context; | |
74 | |
75 md5_context = g_new0(struct MD5Context, 1); | |
76 | |
77 gaim_cipher_context_set_data(context, md5_context); | |
78 | |
79 gaim_cipher_context_reset(context, extra); | |
80 } | |
81 | |
82 static void | |
83 md5_reset(GaimCipherContext *context, gpointer extra) { | |
84 struct MD5Context *md5_context; | |
85 | |
86 md5_context = gaim_cipher_context_get_data(context); | |
87 | |
88 md5_context->total[0] = 0; | |
89 md5_context->total[1] = 0; | |
90 | |
91 md5_context->state[0] = 0x67452301; | |
92 md5_context->state[1] = 0xEFCDAB89; | |
93 md5_context->state[2] = 0x98BADCFE; | |
94 md5_context->state[3] = 0x10325476; | |
95 | |
96 memset(md5_context->buffer, 0, sizeof(md5_context->buffer)); | |
97 } | |
98 | |
99 static void | |
100 md5_uninit(GaimCipherContext *context) { | |
101 struct MD5Context *md5_context; | |
102 | |
103 gaim_cipher_context_reset(context, NULL); | |
104 | |
105 md5_context = gaim_cipher_context_get_data(context); | |
106 memset(md5_context, 0, sizeof(md5_context)); | |
107 | |
108 g_free(md5_context); | |
109 md5_context = NULL; | |
110 } | |
111 | |
112 static void | |
11183 | 113 md5_process(struct MD5Context *md5_context, const guchar data[64]) { |
10684 | 114 guint32 X[16], A, B, C, D; |
115 | |
116 A = md5_context->state[0]; | |
117 B = md5_context->state[1]; | |
118 C = md5_context->state[2]; | |
119 D = md5_context->state[3]; | |
120 | |
121 MD5_GET_GUINT32(X[ 0], data, 0); | |
122 MD5_GET_GUINT32(X[ 1], data, 4); | |
123 MD5_GET_GUINT32(X[ 2], data, 8); | |
124 MD5_GET_GUINT32(X[ 3], data, 12); | |
125 MD5_GET_GUINT32(X[ 4], data, 16); | |
126 MD5_GET_GUINT32(X[ 5], data, 20); | |
127 MD5_GET_GUINT32(X[ 6], data, 24); | |
128 MD5_GET_GUINT32(X[ 7], data, 28); | |
129 MD5_GET_GUINT32(X[ 8], data, 32); | |
130 MD5_GET_GUINT32(X[ 9], data, 36); | |
131 MD5_GET_GUINT32(X[10], data, 40); | |
132 MD5_GET_GUINT32(X[11], data, 44); | |
133 MD5_GET_GUINT32(X[12], data, 48); | |
134 MD5_GET_GUINT32(X[13], data, 52); | |
135 MD5_GET_GUINT32(X[14], data, 56); | |
136 MD5_GET_GUINT32(X[15], data, 60); | |
137 | |
138 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) | |
139 #define P(a,b,c,d,k,s,t) { \ | |
140 a += F(b,c,d) + X[k] + t; \ | |
141 a = S(a,s) + b; \ | |
142 } | |
143 | |
144 /* first pass */ | |
145 #define F(x,y,z) (z ^ (x & (y ^ z))) | |
146 P(A, B, C, D, 0, 7, 0xD76AA478); | |
147 P(D, A, B, C, 1, 12, 0xE8C7B756); | |
148 P(C, D, A, B, 2, 17, 0x242070DB); | |
149 P(B, C, D, A, 3, 22, 0xC1BDCEEE); | |
150 P(A, B, C, D, 4, 7, 0xF57C0FAF); | |
151 P(D, A, B, C, 5, 12, 0x4787C62A); | |
152 P(C, D, A, B, 6, 17, 0xA8304613); | |
153 P(B, C, D, A, 7, 22, 0xFD469501); | |
154 P(A, B, C, D, 8, 7, 0x698098D8); | |
155 P(D, A, B, C, 9, 12, 0x8B44F7AF); | |
156 P(C, D, A, B, 10, 17, 0xFFFF5BB1); | |
157 P(B, C, D, A, 11, 22, 0x895CD7BE); | |
158 P(A, B, C, D, 12, 7, 0x6B901122); | |
159 P(D, A, B, C, 13, 12, 0xFD987193); | |
160 P(C, D, A, B, 14, 17, 0xA679438E); | |
161 P(B, C, D, A, 15, 22, 0x49B40821); | |
162 #undef F | |
163 | |
164 /* second pass */ | |
165 #define F(x,y,z) (y ^ (z & (x ^ y))) | |
166 P(A, B, C, D, 1, 5, 0xF61E2562); | |
167 P(D, A, B, C, 6, 9, 0xC040B340); | |
168 P(C, D, A, B, 11, 14, 0x265E5A51); | |
169 P(B, C, D, A, 0, 20, 0xE9B6C7AA); | |
170 P(A, B, C, D, 5, 5, 0xD62F105D); | |
171 P(D, A, B, C, 10, 9, 0x02441453); | |
172 P(C, D, A, B, 15, 14, 0xD8A1E681); | |
173 P(B, C, D, A, 4, 20, 0xE7D3FBC8); | |
174 P(A, B, C, D, 9, 5, 0x21E1CDE6); | |
175 P(D, A, B, C, 14, 9, 0xC33707D6); | |
176 P(C, D, A, B, 3, 14, 0xF4D50D87); | |
177 P(B, C, D, A, 8, 20, 0x455A14ED); | |
178 P(A, B, C, D, 13, 5, 0xA9E3E905); | |
179 P(D, A, B, C, 2, 9, 0xFCEFA3F8); | |
180 P(C, D, A, B, 7, 14, 0x676F02D9); | |
181 P(B, C, D, A, 12, 20, 0x8D2A4C8A); | |
182 #undef F | |
11183 | 183 |
10684 | 184 /* third pass */ |
185 #define F(x,y,z) (x ^ y ^ z) | |
186 P(A, B, C, D, 5, 4, 0xFFFA3942); | |
187 P(D, A, B, C, 8, 11, 0x8771F681); | |
188 P(C, D, A, B, 11, 16, 0x6D9D6122); | |
189 P(B, C, D, A, 14, 23, 0xFDE5380C); | |
190 P(A, B, C, D, 1, 4, 0xA4BEEA44); | |
191 P(D, A, B, C, 4, 11, 0x4BDECFA9); | |
192 P(C, D, A, B, 7, 16, 0xF6BB4B60); | |
193 P(B, C, D, A, 10, 23, 0xBEBFBC70); | |
194 P(A, B, C, D, 13, 4, 0x289B7EC6); | |
195 P(D, A, B, C, 0, 11, 0xEAA127FA); | |
196 P(C, D, A, B, 3, 16, 0xD4EF3085); | |
197 P(B, C, D, A, 6, 23, 0x04881D05); | |
198 P(A, B, C, D, 9, 4, 0xD9D4D039); | |
199 P(D, A, B, C, 12, 11, 0xE6DB99E5); | |
200 P(C, D, A, B, 15, 16, 0x1FA27CF8); | |
201 P(B, C, D, A, 2, 23, 0xC4AC5665); | |
202 #undef F | |
203 | |
204 /* forth pass */ | |
205 #define F(x,y,z) (y ^ (x | ~z)) | |
206 P(A, B, C, D, 0, 6, 0xF4292244); | |
207 P(D, A, B, C, 7, 10, 0x432AFF97); | |
208 P(C, D, A, B, 14, 15, 0xAB9423A7); | |
209 P(B, C, D, A, 5, 21, 0xFC93A039); | |
210 P(A, B, C, D, 12, 6, 0x655B59C3); | |
211 P(D, A, B, C, 3, 10, 0x8F0CCC92); | |
212 P(C, D, A, B, 10, 15, 0xFFEFF47D); | |
213 P(B, C, D, A, 1, 21, 0x85845DD1); | |
214 P(A, B, C, D, 8, 6, 0x6FA87E4F); | |
215 P(D, A, B, C, 15, 10, 0xFE2CE6E0); | |
216 P(C, D, A, B, 6, 15, 0xA3014314); | |
217 P(B, C, D, A, 13, 21, 0x4E0811A1); | |
218 P(A, B, C, D, 4, 6, 0xF7537E82); | |
219 P(D, A, B, C, 11, 10, 0xBD3AF235); | |
220 P(C, D, A, B, 2, 15, 0x2AD7D2BB); | |
221 P(B, C, D, A, 9, 21, 0xEB86D391); | |
222 #undef F | |
223 #undef P | |
224 #undef S | |
225 | |
226 md5_context->state[0] += A; | |
227 md5_context->state[1] += B; | |
228 md5_context->state[2] += C; | |
229 md5_context->state[3] += D; | |
230 } | |
231 | |
232 static void | |
11183 | 233 md5_append(GaimCipherContext *context, const guchar *data, size_t len) { |
10684 | 234 struct MD5Context *md5_context = NULL; |
235 guint32 left = 0, fill = 0; | |
236 | |
237 g_return_if_fail(context != NULL); | |
238 | |
239 md5_context = gaim_cipher_context_get_data(context); | |
240 g_return_if_fail(md5_context != NULL); | |
241 | |
242 left = md5_context->total[0] & 0x3F; | |
243 fill = 64 - left; | |
244 | |
245 md5_context->total[0] += len; | |
246 md5_context->total[0] &= 0xFFFFFFFF; | |
247 | |
248 if(md5_context->total[0] < len) | |
249 md5_context->total[1]++; | |
250 | |
251 if(left && len >= fill) { | |
252 memcpy((md5_context->buffer + left), data, fill); | |
253 md5_process(md5_context, md5_context->buffer); | |
254 len -= fill; | |
255 data += fill; | |
256 left = 0; | |
257 } | |
258 | |
259 while(len >= 64) { | |
260 md5_process(md5_context, data); | |
261 len -= 64; | |
262 data += 64; | |
263 } | |
264 | |
265 if(len) { | |
266 memcpy((md5_context->buffer + left), data, len); | |
267 } | |
268 } | |
269 | |
270 static gboolean | |
11183 | 271 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
|
272 size_t *out_len) |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
273 { |
10684 | 274 struct MD5Context *md5_context = NULL; |
275 guint32 last, pad; | |
276 guint32 high, low; | |
11183 | 277 guchar message[8]; |
278 guchar padding[64] = { | |
10684 | 279 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
283 }; | |
284 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
285 g_return_val_if_fail(in_len >= 16, FALSE); |
10684 | 286 |
287 md5_context = gaim_cipher_context_get_data(context); | |
288 | |
289 high = (md5_context->total[0] >> 29) | |
290 | (md5_context->total[1] << 3); | |
291 low = (md5_context->total[0] << 3); | |
292 | |
293 MD5_PUT_GUINT32(low, message, 0); | |
294 MD5_PUT_GUINT32(high, message, 4); | |
295 | |
296 last = md5_context->total[0] & 0x3F; | |
297 pad = (last < 56) ? (56 - last) : (120 - last); | |
298 | |
299 md5_append(context, padding, pad); | |
300 md5_append(context, message, 8); | |
301 | |
302 MD5_PUT_GUINT32(md5_context->state[0], digest, 0); | |
303 MD5_PUT_GUINT32(md5_context->state[1], digest, 4); | |
304 MD5_PUT_GUINT32(md5_context->state[2], digest, 8); | |
305 MD5_PUT_GUINT32(md5_context->state[3], digest, 12); | |
306 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
307 if(out_len) |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
308 *out_len = 16; |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
309 |
10684 | 310 return TRUE; |
311 } | |
312 | |
313 static GaimCipherOps MD5Ops = { | |
314 NULL, /* Set option */ | |
315 NULL, /* Get option */ | |
316 md5_init, /* init */ | |
317 md5_reset, /* reset */ | |
318 md5_uninit, /* uninit */ | |
319 NULL, /* set iv */ | |
320 md5_append, /* append */ | |
321 md5_digest, /* digest */ | |
322 NULL, /* encrypt */ | |
323 NULL, /* decrypt */ | |
324 NULL, /* set salt */ | |
325 NULL, /* get salt size */ | |
326 NULL, /* set key */ | |
327 NULL /* get key size */ | |
328 }; | |
329 | |
330 /******************************************************************************* | |
11329 | 331 * MD4 |
332 ******************************************************************************/ | |
333 #define MD4_DIGEST_SIZE 16 | |
334 #define MD4_HMAC_BLOCK_SIZE 64 | |
335 #define MD4_BLOCK_WORDS 16 | |
336 #define MD4_HASH_WORDS 4 | |
337 | |
338 | |
339 | |
340 struct MD4_Context { | |
341 guint32 hash[MD4_HASH_WORDS]; | |
342 guint32 block[MD4_BLOCK_WORDS]; | |
343 guint64 byte_count; | |
344 }; | |
345 | |
346 static inline guint32 lshift(guint32 x, unsigned int s) | |
347 { | |
348 x &= 0xFFFFFFFF; | |
349 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s)); | |
350 } | |
351 | |
352 static inline guint32 F(guint32 x, guint32 y, guint32 z) | |
353 { | |
354 return (x & y) | ((~x) & z); | |
355 } | |
356 | |
357 static inline guint32 G(guint32 x, guint32 y, guint32 z) | |
358 { | |
359 return (x & y) | (x & z) | (y & z); | |
360 } | |
361 | |
362 static inline guint32 H(guint32 x, guint32 y, guint32 z) | |
363 { | |
364 return x ^ y ^ z; | |
365 } | |
366 | |
367 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s)) | |
368 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (guint32)0x5A827999,s)) | |
369 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (guint32)0x6ED9EBA1,s)) | |
370 | |
371 static inline void le32_to_cpu_array(guint32 *buf, unsigned int words) | |
372 { | |
373 while (words--) { | |
374 *buf=GUINT_FROM_LE(*buf); | |
375 // __le32_to_cpus(buf); | |
376 buf++; | |
377 } | |
378 } | |
379 | |
380 static inline void cpu_to_le32_array(guint32 *buf, unsigned int words) | |
381 { | |
382 while (words--) { | |
383 // __cpu_to_le32s(buf); | |
384 *buf=GUINT_TO_LE(*buf); | |
385 buf++; | |
386 } | |
387 } | |
388 | |
389 static void md4_transform(guint32 *hash, guint32 const *in) | |
390 { | |
391 guint32 a, b, c, d; | |
392 | |
393 a = hash[0]; | |
394 b = hash[1]; | |
395 c = hash[2]; | |
396 d = hash[3]; | |
397 | |
398 ROUND1(a, b, c, d, in[0], 3); | |
399 ROUND1(d, a, b, c, in[1], 7); | |
400 ROUND1(c, d, a, b, in[2], 11); | |
401 ROUND1(b, c, d, a, in[3], 19); | |
402 ROUND1(a, b, c, d, in[4], 3); | |
403 ROUND1(d, a, b, c, in[5], 7); | |
404 ROUND1(c, d, a, b, in[6], 11); | |
405 ROUND1(b, c, d, a, in[7], 19); | |
406 ROUND1(a, b, c, d, in[8], 3); | |
407 ROUND1(d, a, b, c, in[9], 7); | |
408 ROUND1(c, d, a, b, in[10], 11); | |
409 ROUND1(b, c, d, a, in[11], 19); | |
410 ROUND1(a, b, c, d, in[12], 3); | |
411 ROUND1(d, a, b, c, in[13], 7); | |
412 ROUND1(c, d, a, b, in[14], 11); | |
413 ROUND1(b, c, d, a, in[15], 19); | |
414 | |
415 ROUND2(a, b, c, d,in[ 0], 3); | |
416 ROUND2(d, a, b, c, in[4], 5); | |
417 ROUND2(c, d, a, b, in[8], 9); | |
418 ROUND2(b, c, d, a, in[12], 13); | |
419 ROUND2(a, b, c, d, in[1], 3); | |
420 ROUND2(d, a, b, c, in[5], 5); | |
421 ROUND2(c, d, a, b, in[9], 9); | |
422 ROUND2(b, c, d, a, in[13], 13); | |
423 ROUND2(a, b, c, d, in[2], 3); | |
424 ROUND2(d, a, b, c, in[6], 5); | |
425 ROUND2(c, d, a, b, in[10], 9); | |
426 ROUND2(b, c, d, a, in[14], 13); | |
427 ROUND2(a, b, c, d, in[3], 3); | |
428 ROUND2(d, a, b, c, in[7], 5); | |
429 ROUND2(c, d, a, b, in[11], 9); | |
430 ROUND2(b, c, d, a, in[15], 13); | |
431 | |
432 ROUND3(a, b, c, d,in[ 0], 3); | |
433 ROUND3(d, a, b, c, in[8], 9); | |
434 ROUND3(c, d, a, b, in[4], 11); | |
435 ROUND3(b, c, d, a, in[12], 15); | |
436 ROUND3(a, b, c, d, in[2], 3); | |
437 ROUND3(d, a, b, c, in[10], 9); | |
438 ROUND3(c, d, a, b, in[6], 11); | |
439 ROUND3(b, c, d, a, in[14], 15); | |
440 ROUND3(a, b, c, d, in[1], 3); | |
441 ROUND3(d, a, b, c, in[9], 9); | |
442 ROUND3(c, d, a, b, in[5], 11); | |
443 ROUND3(b, c, d, a, in[13], 15); | |
444 ROUND3(a, b, c, d, in[3], 3); | |
445 ROUND3(d, a, b, c, in[11], 9); | |
446 ROUND3(c, d, a, b, in[7], 11); | |
447 ROUND3(b, c, d, a, in[15], 15); | |
448 | |
449 hash[0] += a; | |
450 hash[1] += b; | |
451 hash[2] += c; | |
452 hash[3] += d; | |
453 } | |
454 | |
455 static inline void md4_transform_helper(struct MD4_Context *ctx) | |
456 { | |
457 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(guint32)); | |
458 md4_transform(ctx->hash, ctx->block); | |
459 } | |
460 | |
461 static void | |
462 md4_init(GaimCipherContext *context, gpointer extra) { | |
463 struct MD4_Context *mctx; | |
464 mctx = g_new0(struct MD4_Context, 1); | |
465 gaim_cipher_context_set_data(context, mctx); | |
466 gaim_cipher_context_reset(context, extra); | |
467 | |
468 mctx->hash[0] = 0x67452301; | |
469 mctx->hash[1] = 0xefcdab89; | |
470 mctx->hash[2] = 0x98badcfe; | |
471 mctx->hash[3] = 0x10325476; | |
472 mctx->byte_count = 0; | |
473 } | |
474 | |
475 static void | |
476 md4_reset(GaimCipherContext *context, gpointer extra) { | |
477 struct MD4_Context *mctx; | |
478 | |
479 mctx = gaim_cipher_context_get_data(context); | |
480 | |
481 mctx->hash[0] = 0x67452301; | |
482 mctx->hash[1] = 0xefcdab89; | |
483 mctx->hash[2] = 0x98badcfe; | |
484 mctx->hash[3] = 0x10325476; | |
485 mctx->byte_count = 0; | |
486 } | |
487 | |
488 static void | |
489 md4_append(GaimCipherContext *context, const guchar *data, size_t len) | |
490 { | |
491 struct MD4_Context *mctx = gaim_cipher_context_get_data(context); | |
492 const guint32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); | |
493 | |
494 mctx->byte_count += len; | |
495 | |
496 if (avail > len) { | |
497 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), | |
498 data, len); | |
499 return; | |
500 } | |
501 | |
502 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), | |
503 data, avail); | |
504 | |
505 md4_transform_helper(mctx); | |
506 data += avail; | |
507 len -= avail; | |
508 | |
509 while (len >= sizeof(mctx->block)) { | |
510 memcpy(mctx->block, data, sizeof(mctx->block)); | |
511 md4_transform_helper(mctx); | |
512 data += sizeof(mctx->block); | |
513 len -= sizeof(mctx->block); | |
514 } | |
515 | |
516 memcpy(mctx->block, data, len); | |
517 } | |
518 | |
519 static gboolean | |
520 md4_digest(GaimCipherContext *context, size_t in_len, guchar *out, | |
521 size_t *out_len) | |
522 { | |
523 struct MD4_Context *mctx = gaim_cipher_context_get_data(context); | |
524 const unsigned int offset = mctx->byte_count & 0x3f; | |
525 char *p = (char *)mctx->block + offset; | |
526 int padding = 56 - (offset + 1); | |
527 | |
528 | |
529 if(in_len<16) return FALSE; | |
530 if(out_len) *out_len = 16; | |
531 *p++ = 0x80; | |
532 if (padding < 0) { | |
533 memset(p, 0x00, padding + sizeof (guint64)); | |
534 md4_transform_helper(mctx); | |
535 p = (char *)mctx->block; | |
536 padding = 56; | |
537 } | |
538 | |
539 memset(p, 0, padding); | |
540 mctx->block[14] = mctx->byte_count << 3; | |
541 mctx->block[15] = mctx->byte_count >> 29; | |
542 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - | |
543 sizeof(guint64)) / sizeof(guint32)); | |
544 md4_transform(mctx->hash, mctx->block); | |
545 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(guint32)); | |
546 memcpy(out, mctx->hash, sizeof(mctx->hash)); | |
547 memset(mctx, 0, sizeof(*mctx)); | |
548 return TRUE; | |
549 } | |
550 | |
551 static void | |
552 md4_uninit(GaimCipherContext *context) { | |
553 struct MD4_Context *md4_context; | |
554 | |
555 gaim_cipher_context_reset(context, NULL); | |
556 | |
557 md4_context = gaim_cipher_context_get_data(context); | |
558 memset(md4_context, 0, sizeof(md4_context)); | |
559 | |
560 g_free(md4_context); | |
561 md4_context = NULL; | |
562 } | |
563 | |
564 static GaimCipherOps MD4Ops = { | |
565 NULL, /* Set option */ | |
566 NULL, /* Get option */ | |
567 md4_init, /* init */ | |
568 md4_reset, /* reset */ | |
569 md4_uninit, /* uninit */ | |
570 NULL, /* set iv */ | |
571 md4_append, /* append */ | |
572 md4_digest, /* digest */ | |
573 NULL, /* encrypt */ | |
574 NULL, /* decrypt */ | |
575 NULL, /* set salt */ | |
576 NULL, /* get salt size */ | |
577 NULL, /* set key */ | |
578 NULL /* get key size */ | |
579 }; | |
580 | |
581 /******************************************************************************* | |
10684 | 582 * SHA-1 |
583 ******************************************************************************/ | |
584 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF) | |
585 | |
586 struct SHA1Context { | |
587 guint32 H[5]; | |
588 guint32 W[80]; | |
589 | |
590 gint lenW; | |
591 | |
592 guint32 sizeHi; | |
593 guint32 sizeLo; | |
594 }; | |
595 | |
596 static void | |
597 sha1_hash_block(struct SHA1Context *sha1_ctx) { | |
598 gint i; | |
599 guint32 A, B, C, D, E, T; | |
600 | |
601 for(i = 16; i < 80; i++) { | |
602 sha1_ctx->W[i] = SHA1_ROTL(sha1_ctx->W[i - 3] ^ | |
603 sha1_ctx->W[i - 8] ^ | |
604 sha1_ctx->W[i - 14] ^ | |
605 sha1_ctx->W[i - 16], 1); | |
606 } | |
607 | |
608 A = sha1_ctx->H[0]; | |
609 B = sha1_ctx->H[1]; | |
610 C = sha1_ctx->H[2]; | |
611 D = sha1_ctx->H[3]; | |
612 E = sha1_ctx->H[4]; | |
613 | |
614 for(i = 0; i < 20; i++) { | |
615 T = (SHA1_ROTL(A, 5) + (((C ^ D) & B) ^ D) + E + sha1_ctx->W[i] + 0x5A827999) & 0xFFFFFFFF; | |
616 E = D; | |
617 D = C; | |
618 C = SHA1_ROTL(B, 30); | |
619 B = A; | |
620 A = T; | |
621 } | |
622 | |
623 for(i = 20; i < 40; i++) { | |
624 T = (SHA1_ROTL(A, 5) + (B ^ C ^ D) + E + sha1_ctx->W[i] + 0x6ED9EBA1) & 0xFFFFFFFF; | |
625 E = D; | |
626 D = C; | |
627 C = SHA1_ROTL(B, 30); | |
628 B = A; | |
629 A = T; | |
630 } | |
631 | |
632 for(i = 40; i < 60; i++) { | |
633 T = (SHA1_ROTL(A, 5) + ((B & C) | (D & (B | C))) + E + sha1_ctx->W[i] + 0x8F1BBCDC) & 0xFFFFFFFF; | |
634 E = D; | |
635 D = C; | |
636 C = SHA1_ROTL(B, 30); | |
637 B = A; | |
638 A = T; | |
639 } | |
640 | |
641 for(i = 60; i < 80; i++) { | |
642 T = (SHA1_ROTL(A, 5) + (B ^ C ^ D) + E + sha1_ctx->W[i] + 0xCA62C1D6) & 0xFFFFFFFF; | |
643 E = D; | |
644 D = C; | |
645 C = SHA1_ROTL(B, 30); | |
646 B = A; | |
647 A = T; | |
648 } | |
649 | |
650 sha1_ctx->H[0] += A; | |
651 sha1_ctx->H[1] += B; | |
652 sha1_ctx->H[2] += C; | |
653 sha1_ctx->H[3] += D; | |
11183 | 654 sha1_ctx->H[4] += E; |
10684 | 655 } |
656 | |
657 static void | |
658 sha1_set_opt(GaimCipherContext *context, const gchar *name, void *value) { | |
659 struct SHA1Context *ctx; | |
660 | |
661 ctx = gaim_cipher_context_get_data(context); | |
662 | |
663 if(!strcmp(name, "sizeHi")) { | |
664 ctx->sizeHi = GPOINTER_TO_INT(value); | |
665 } else if(!strcmp(name, "sizeLo")) { | |
666 ctx->sizeLo = GPOINTER_TO_INT(value); | |
667 } else if(!strcmp(name, "lenW")) { | |
668 ctx->lenW = GPOINTER_TO_INT(value); | |
669 } | |
670 } | |
671 | |
672 static void * | |
673 sha1_get_opt(GaimCipherContext *context, const gchar *name) { | |
674 struct SHA1Context *ctx; | |
675 | |
676 ctx = gaim_cipher_context_get_data(context); | |
677 | |
678 if(!strcmp(name, "sizeHi")) { | |
679 return GINT_TO_POINTER(ctx->sizeHi); | |
680 } else if(!strcmp(name, "sizeLo")) { | |
681 return GINT_TO_POINTER(ctx->sizeLo); | |
682 } else if(!strcmp(name, "lenW")) { | |
683 return GINT_TO_POINTER(ctx->lenW); | |
684 } | |
685 | |
686 return NULL; | |
687 } | |
688 | |
689 static void | |
690 sha1_init(GaimCipherContext *context, void *extra) { | |
691 struct SHA1Context *sha1_ctx; | |
692 | |
693 sha1_ctx = g_new0(struct SHA1Context, 1); | |
694 | |
695 gaim_cipher_context_set_data(context, sha1_ctx); | |
696 | |
697 gaim_cipher_context_reset(context, extra); | |
698 } | |
699 | |
700 static void | |
701 sha1_reset(GaimCipherContext *context, void *extra) { | |
702 struct SHA1Context *sha1_ctx; | |
703 gint i; | |
704 | |
705 sha1_ctx = gaim_cipher_context_get_data(context); | |
706 | |
707 g_return_if_fail(sha1_ctx); | |
708 | |
709 sha1_ctx->lenW = 0; | |
710 sha1_ctx->sizeHi = 0; | |
711 sha1_ctx->sizeLo = 0; | |
712 | |
713 sha1_ctx->H[0] = 0x67452301; | |
714 sha1_ctx->H[1] = 0xEFCDAB89; | |
715 sha1_ctx->H[2] = 0x98BADCFE; | |
716 sha1_ctx->H[3] = 0x10325476; | |
717 sha1_ctx->H[4] = 0xC3D2E1F0; | |
718 | |
719 for(i = 0; i < 80; i++) | |
720 sha1_ctx->W[i] = 0; | |
721 } | |
722 | |
723 static void | |
724 sha1_uninit(GaimCipherContext *context) { | |
725 struct SHA1Context *sha1_ctx; | |
726 | |
727 gaim_cipher_context_reset(context, NULL); | |
728 | |
729 sha1_ctx = gaim_cipher_context_get_data(context); | |
730 | |
731 memset(sha1_ctx, 0, sizeof(struct SHA1Context)); | |
732 | |
733 g_free(sha1_ctx); | |
734 sha1_ctx = NULL; | |
735 } | |
736 | |
737 | |
738 static void | |
11183 | 739 sha1_append(GaimCipherContext *context, const guchar *data, size_t len) { |
10684 | 740 struct SHA1Context *sha1_ctx; |
741 gint i; | |
742 | |
743 sha1_ctx = gaim_cipher_context_get_data(context); | |
744 | |
745 g_return_if_fail(sha1_ctx); | |
746 | |
747 for(i = 0; i < len; i++) { | |
748 sha1_ctx->W[sha1_ctx->lenW / 4] <<= 8; | |
749 sha1_ctx->W[sha1_ctx->lenW / 4] |= data[i]; | |
750 | |
751 if((++sha1_ctx->lenW) % 64 == 0) { | |
752 sha1_hash_block(sha1_ctx); | |
753 sha1_ctx->lenW = 0; | |
754 } | |
755 | |
756 sha1_ctx->sizeLo += 8; | |
757 sha1_ctx->sizeHi += (sha1_ctx->sizeLo < 8); | |
758 } | |
759 } | |
760 | |
761 static gboolean | |
11183 | 762 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
|
763 size_t *out_len) |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
764 { |
10684 | 765 struct SHA1Context *sha1_ctx; |
11183 | 766 guchar pad0x80 = 0x80, pad0x00 = 0x00; |
767 guchar padlen[8]; | |
10684 | 768 gint i; |
769 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
770 g_return_val_if_fail(in_len >= 20, FALSE); |
10684 | 771 |
772 sha1_ctx = gaim_cipher_context_get_data(context); | |
773 | |
774 g_return_val_if_fail(sha1_ctx, FALSE); | |
775 | |
11183 | 776 padlen[0] = (guchar)((sha1_ctx->sizeHi >> 24) & 255); |
777 padlen[1] = (guchar)((sha1_ctx->sizeHi >> 16) & 255); | |
778 padlen[2] = (guchar)((sha1_ctx->sizeHi >> 8) & 255); | |
779 padlen[3] = (guchar)((sha1_ctx->sizeHi >> 0) & 255); | |
780 padlen[4] = (guchar)((sha1_ctx->sizeLo >> 24) & 255); | |
781 padlen[5] = (guchar)((sha1_ctx->sizeLo >> 16) & 255); | |
782 padlen[6] = (guchar)((sha1_ctx->sizeLo >> 8) & 255); | |
783 padlen[7] = (guchar)((sha1_ctx->sizeLo >> 0) & 255); | |
10684 | 784 |
785 /* pad with a 1, then zeroes, then length */ | |
786 gaim_cipher_context_append(context, &pad0x80, 1); | |
787 while(sha1_ctx->lenW != 56) | |
788 gaim_cipher_context_append(context, &pad0x00, 1); | |
789 gaim_cipher_context_append(context, padlen, 8); | |
790 | |
791 for(i = 0; i < 20; i++) { | |
11183 | 792 digest[i] = (guchar)(sha1_ctx->H[i / 4] >> 24); |
10684 | 793 sha1_ctx->H[i / 4] <<= 8; |
794 } | |
795 | |
796 gaim_cipher_context_reset(context, NULL); | |
797 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
798 if(out_len) |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
799 *out_len = 20; |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
800 |
10684 | 801 return TRUE; |
802 } | |
803 | |
804 static GaimCipherOps SHA1Ops = { | |
805 sha1_set_opt, /* Set Option */ | |
806 sha1_get_opt, /* Get Option */ | |
807 sha1_init, /* init */ | |
808 sha1_reset, /* reset */ | |
809 sha1_uninit, /* uninit */ | |
810 NULL, /* set iv */ | |
811 sha1_append, /* append */ | |
812 sha1_digest, /* digest */ | |
813 NULL, /* encrypt */ | |
814 NULL, /* decrypt */ | |
815 NULL, /* set salt */ | |
816 NULL, /* get salt size */ | |
817 NULL, /* set key */ | |
818 NULL /* get key size */ | |
819 }; | |
820 | |
821 /******************************************************************************* | |
822 * Structs | |
823 ******************************************************************************/ | |
824 struct _GaimCipher { | |
825 gchar *name; | |
826 GaimCipherOps *ops; | |
827 guint ref; | |
828 }; | |
829 | |
830 struct _GaimCipherContext { | |
831 GaimCipher *cipher; | |
832 gpointer data; | |
833 }; | |
834 | |
835 /****************************************************************************** | |
836 * Globals | |
837 *****************************************************************************/ | |
838 static GList *ciphers = NULL; | |
839 | |
840 /****************************************************************************** | |
841 * GaimCipher API | |
842 *****************************************************************************/ | |
843 const gchar * | |
844 gaim_cipher_get_name(GaimCipher *cipher) { | |
845 g_return_val_if_fail(cipher, NULL); | |
846 | |
847 return cipher->name; | |
848 } | |
849 | |
850 guint | |
851 gaim_cipher_get_capabilities(GaimCipher *cipher) { | |
852 GaimCipherOps *ops = NULL; | |
853 guint caps = 0; | |
854 | |
855 g_return_val_if_fail(cipher, 0); | |
856 | |
857 ops = cipher->ops; | |
858 g_return_val_if_fail(ops, 0); | |
859 | |
860 if(ops->set_option) | |
861 caps |= GAIM_CIPHER_CAPS_SET_OPT; | |
862 if(ops->get_option) | |
863 caps |= GAIM_CIPHER_CAPS_GET_OPT; | |
864 if(ops->init) | |
865 caps |= GAIM_CIPHER_CAPS_INIT; | |
866 if(ops->reset) | |
867 caps |= GAIM_CIPHER_CAPS_RESET; | |
868 if(ops->uninit) | |
869 caps |= GAIM_CIPHER_CAPS_UNINIT; | |
870 if(ops->set_iv) | |
871 caps |= GAIM_CIPHER_CAPS_SET_IV; | |
872 if(ops->append) | |
873 caps |= GAIM_CIPHER_CAPS_APPEND; | |
874 if(ops->digest) | |
875 caps |= GAIM_CIPHER_CAPS_DIGEST; | |
876 if(ops->encrypt) | |
877 caps |= GAIM_CIPHER_CAPS_ENCRYPT; | |
878 if(ops->decrypt) | |
879 caps |= GAIM_CIPHER_CAPS_DECRYPT; | |
880 if(ops->set_salt) | |
881 caps |= GAIM_CIPHER_CAPS_SET_SALT; | |
882 if(ops->get_salt_size) | |
883 caps |= GAIM_CIPHER_CAPS_GET_SALT_SIZE; | |
884 if(ops->set_key) | |
885 caps |= GAIM_CIPHER_CAPS_SET_KEY; | |
886 if(ops->get_key_size) | |
887 caps |= GAIM_CIPHER_CAPS_GET_KEY_SIZE; | |
888 | |
889 return caps; | |
890 } | |
891 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
892 gboolean |
11183 | 893 gaim_cipher_digest_region(const gchar *name, const guchar *data, |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
894 size_t data_len, size_t in_len, |
11183 | 895 guchar digest[], size_t *out_len) |
10684 | 896 { |
897 GaimCipher *cipher; | |
898 GaimCipherContext *context; | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
899 gboolean ret = FALSE; |
10684 | 900 |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
901 g_return_val_if_fail(name, FALSE); |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
902 g_return_val_if_fail(data, FALSE); |
10684 | 903 |
904 cipher = gaim_ciphers_find_cipher(name); | |
905 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
906 g_return_val_if_fail(cipher, FALSE); |
10684 | 907 |
908 if(!cipher->ops->append || !cipher->ops->digest) { | |
909 gaim_debug_info("cipher", "gaim_cipher_region failed: " | |
910 "the %s cipher does not support appending and or " | |
911 "digesting.", cipher->name); | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
912 return FALSE; |
10684 | 913 } |
914 | |
915 context = gaim_cipher_context_new(cipher, NULL); | |
916 gaim_cipher_context_append(context, data, data_len); | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
917 ret = gaim_cipher_context_digest(context, in_len, digest, out_len); |
11143 | 918 gaim_cipher_context_destroy(context); |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
919 |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
920 return ret; |
10684 | 921 } |
922 | |
923 /****************************************************************************** | |
924 * GaimCiphers API | |
925 *****************************************************************************/ | |
926 GaimCipher * | |
927 gaim_ciphers_find_cipher(const gchar *name) { | |
928 GaimCipher *cipher; | |
929 GList *l; | |
930 | |
931 g_return_val_if_fail(name, NULL); | |
932 | |
933 for(l = ciphers; l; l = l->next) { | |
934 cipher = GAIM_CIPHER(l->data); | |
935 | |
936 if(!g_ascii_strcasecmp(cipher->name, name)) | |
937 return cipher; | |
938 } | |
939 | |
940 return NULL; | |
941 } | |
942 | |
943 GaimCipher * | |
944 gaim_ciphers_register_cipher(const gchar *name, GaimCipherOps *ops) { | |
945 GaimCipher *cipher = NULL; | |
946 | |
947 g_return_val_if_fail(name, NULL); | |
948 g_return_val_if_fail(ops, NULL); | |
949 g_return_val_if_fail(!gaim_ciphers_find_cipher(name), NULL); | |
950 | |
951 cipher = g_new0(GaimCipher, 1); | |
952 | |
953 cipher->name = g_strdup(name); | |
954 cipher->ops = ops; | |
955 | |
956 ciphers = g_list_append(ciphers, cipher); | |
957 | |
958 gaim_signal_emit(gaim_ciphers_get_handle(), "cipher-added", cipher); | |
959 | |
960 return cipher; | |
961 } | |
962 | |
963 gboolean | |
964 gaim_ciphers_unregister_cipher(GaimCipher *cipher) { | |
965 g_return_val_if_fail(cipher, FALSE); | |
966 g_return_val_if_fail(cipher->ref == 0, FALSE); | |
967 | |
968 gaim_signal_emit(gaim_ciphers_get_handle(), "cipher-removed", cipher); | |
969 | |
970 ciphers = g_list_remove(ciphers, cipher); | |
971 | |
972 g_free(cipher->name); | |
973 g_free(cipher); | |
974 | |
975 return TRUE; | |
976 } | |
977 | |
978 GList * | |
979 gaim_ciphers_get_ciphers() { | |
980 return ciphers; | |
981 } | |
982 | |
983 /****************************************************************************** | |
984 * GaimCipher Subsystem API | |
985 *****************************************************************************/ | |
986 gpointer | |
987 gaim_ciphers_get_handle() { | |
988 static gint handle; | |
989 | |
990 return &handle; | |
991 } | |
992 | |
993 void | |
994 gaim_ciphers_init() { | |
995 gpointer handle; | |
996 | |
997 handle = gaim_ciphers_get_handle(); | |
998 | |
999 gaim_signal_register(handle, "cipher-added", | |
1000 gaim_marshal_VOID__POINTER, NULL, 1, | |
1001 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
1002 GAIM_SUBTYPE_CIPHER)); | |
1003 gaim_signal_register(handle, "cipher-removed", | |
1004 gaim_marshal_VOID__POINTER, NULL, 1, | |
1005 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
1006 GAIM_SUBTYPE_CIPHER)); | |
1007 | |
1008 gaim_ciphers_register_cipher("md5", &MD5Ops); | |
1009 gaim_ciphers_register_cipher("sha1", &SHA1Ops); | |
11329 | 1010 gaim_ciphers_register_cipher("md4", &MD4Ops); |
10684 | 1011 } |
1012 | |
1013 void | |
1014 gaim_ciphers_uninit() { | |
1015 GaimCipher *cipher; | |
1016 GList *l, *ll; | |
1017 | |
1018 for(l = ciphers; l; l = ll) { | |
1019 ll = l->next; | |
1020 | |
1021 cipher = GAIM_CIPHER(l->data); | |
1022 gaim_ciphers_unregister_cipher(cipher); | |
1023 | |
1024 ciphers = g_list_remove(ciphers, cipher); | |
1025 } | |
1026 | |
1027 g_list_free(ciphers); | |
1028 | |
1029 gaim_signals_unregister_by_instance(gaim_ciphers_get_handle()); | |
1030 } | |
1031 /****************************************************************************** | |
1032 * GaimCipherContext API | |
1033 *****************************************************************************/ | |
1034 void | |
1035 gaim_cipher_context_set_option(GaimCipherContext *context, const gchar *name, | |
1036 gpointer value) | |
1037 { | |
1038 GaimCipher *cipher = NULL; | |
1039 | |
1040 g_return_if_fail(context); | |
1041 g_return_if_fail(name); | |
1042 | |
1043 cipher = context->cipher; | |
1044 g_return_if_fail(cipher); | |
1045 | |
1046 if(cipher->ops && cipher->ops->set_option) | |
1047 cipher->ops->set_option(context, name, value); | |
1048 else | |
1049 gaim_debug_info("cipher", "the %s cipher does not support the " | |
1050 "set_option operation\n", cipher->name); | |
1051 } | |
1052 | |
1053 gpointer | |
1054 gaim_cipher_context_get_option(GaimCipherContext *context, const gchar *name) { | |
1055 GaimCipher *cipher = NULL; | |
1056 | |
1057 g_return_val_if_fail(context, NULL); | |
1058 g_return_val_if_fail(name, NULL); | |
1059 | |
1060 cipher = context->cipher; | |
1061 g_return_val_if_fail(cipher, NULL); | |
1062 | |
1063 if(cipher->ops && cipher->ops->get_option) | |
1064 return cipher->ops->get_option(context, name); | |
1065 else { | |
1066 gaim_debug_info("cipher", "the %s cipher does not support the " | |
1067 "get_option operation\n", cipher->name); | |
1068 | |
1069 return NULL; | |
1070 } | |
1071 } | |
1072 | |
1073 GaimCipherContext * | |
1074 gaim_cipher_context_new(GaimCipher *cipher, void *extra) { | |
1075 GaimCipherContext *context = NULL; | |
1076 | |
1077 g_return_val_if_fail(cipher, NULL); | |
1078 | |
1079 cipher->ref++; | |
1080 | |
1081 context = g_new0(GaimCipherContext, 1); | |
1082 context->cipher = cipher; | |
1083 | |
1084 if(cipher->ops->init) | |
1085 cipher->ops->init(context, extra); | |
1086 | |
1087 return context; | |
1088 } | |
1089 | |
1090 GaimCipherContext * | |
1091 gaim_cipher_context_new_by_name(const gchar *name, void *extra) { | |
1092 GaimCipher *cipher; | |
1093 | |
1094 g_return_val_if_fail(name, NULL); | |
1095 | |
1096 cipher = gaim_ciphers_find_cipher(name); | |
1097 | |
1098 g_return_val_if_fail(cipher, NULL); | |
1099 | |
1100 return gaim_cipher_context_new(cipher, extra); | |
1101 } | |
1102 | |
1103 void | |
1104 gaim_cipher_context_reset(GaimCipherContext *context, void *extra) { | |
1105 GaimCipher *cipher = NULL; | |
1106 | |
1107 g_return_if_fail(context); | |
1108 | |
1109 cipher = context->cipher; | |
1110 g_return_if_fail(cipher); | |
1111 | |
1112 if(cipher->ops && cipher->ops->reset) | |
1113 context->cipher->ops->reset(context, extra); | |
1114 } | |
1115 | |
1116 void | |
1117 gaim_cipher_context_destroy(GaimCipherContext *context) { | |
1118 GaimCipher *cipher = NULL; | |
1119 | |
1120 g_return_if_fail(context); | |
1121 | |
1122 cipher = context->cipher; | |
1123 g_return_if_fail(cipher); | |
1124 | |
1125 cipher->ref--; | |
1126 | |
1127 if(cipher->ops && cipher->ops->uninit) | |
1128 cipher->ops->uninit(context); | |
1129 | |
1130 memset(context, 0, sizeof(context)); | |
1131 g_free(context); | |
1132 context = NULL; | |
1133 } | |
1134 | |
1135 void | |
11183 | 1136 gaim_cipher_context_set_iv(GaimCipherContext *context, guchar *iv, size_t len) |
10684 | 1137 { |
1138 GaimCipher *cipher = NULL; | |
1139 | |
1140 g_return_if_fail(context); | |
1141 g_return_if_fail(iv); | |
1142 | |
1143 cipher = context->cipher; | |
1144 g_return_if_fail(cipher); | |
1145 | |
1146 if(cipher->ops && cipher->ops->set_iv) | |
1147 cipher->ops->set_iv(context, iv, len); | |
1148 else | |
1149 gaim_debug_info("cipher", "the %s cipher does not support the set" | |
1150 "initialization vector operation\n", cipher->name); | |
1151 } | |
1152 | |
1153 void | |
11183 | 1154 gaim_cipher_context_append(GaimCipherContext *context, const guchar *data, |
10684 | 1155 size_t len) |
1156 { | |
1157 GaimCipher *cipher = NULL; | |
1158 | |
1159 g_return_if_fail(context); | |
1160 | |
1161 cipher = context->cipher; | |
1162 g_return_if_fail(cipher); | |
1163 | |
1164 if(cipher->ops && cipher->ops->append) | |
1165 cipher->ops->append(context, data, len); | |
1166 else | |
1167 gaim_debug_info("cipher", "the %s cipher does not support the append " | |
1168 "operation\n", cipher->name); | |
1169 } | |
1170 | |
1171 gboolean | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1172 gaim_cipher_context_digest(GaimCipherContext *context, size_t in_len, |
11183 | 1173 guchar digest[], size_t *out_len) |
10684 | 1174 { |
1175 GaimCipher *cipher = NULL; | |
1176 | |
1177 g_return_val_if_fail(context, FALSE); | |
1178 | |
1179 cipher = context->cipher; | |
1180 g_return_val_if_fail(context, FALSE); | |
1181 | |
1182 if(cipher->ops && cipher->ops->digest) | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1183 return cipher->ops->digest(context, in_len, digest, out_len); |
10684 | 1184 else { |
1185 gaim_debug_info("cipher", "the %s cipher does not support the digest " | |
1186 "operation\n", cipher->name); | |
1187 return FALSE; | |
1188 } | |
1189 } | |
1190 | |
1191 gboolean | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1192 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
|
1193 gchar digest_s[], size_t *out_len) |
10684 | 1194 { |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1195 /* 8k is a bit excessive, will tweak later. */ |
11183 | 1196 guchar digest[BUF_LEN * 4]; |
10684 | 1197 gint n = 0; |
1198 size_t dlen = 0; | |
1199 | |
1200 g_return_val_if_fail(context, FALSE); | |
1201 g_return_val_if_fail(digest_s, FALSE); | |
1202 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1203 if(!gaim_cipher_context_digest(context, sizeof(digest), digest, &dlen)) |
10684 | 1204 return FALSE; |
1205 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1206 if(in_len < dlen * 2) |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1207 return FALSE; |
10684 | 1208 |
1209 for(n = 0; n < dlen; n++) | |
1210 sprintf(digest_s + (n * 2), "%02x", digest[n]); | |
1211 | |
1212 digest_s[n * 2] = '\0'; | |
1213 | |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1214 if(out_len) |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1215 *out_len = dlen * 2; |
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
1216 |
10684 | 1217 return TRUE; |
1218 } | |
1219 | |
1220 gint | |
11183 | 1221 gaim_cipher_context_encrypt(GaimCipherContext *context, const guchar data[], |
1222 size_t len, guchar output[], size_t *outlen) | |
10684 | 1223 { |
1224 GaimCipher *cipher = NULL; | |
1225 | |
1226 g_return_val_if_fail(context, -1); | |
1227 | |
1228 cipher = context->cipher; | |
1229 g_return_val_if_fail(cipher, -1); | |
1230 | |
1231 if(cipher->ops && cipher->ops->encrypt) | |
1232 return cipher->ops->encrypt(context, data, len, output, outlen); | |
1233 else { | |
1234 gaim_debug_info("cipher", "the %s cipher does not support the encrypt" | |
1235 "operation\n", cipher->name); | |
1236 | |
1237 if(outlen) | |
1238 *outlen = -1; | |
1239 | |
1240 return -1; | |
1241 } | |
1242 } | |
1243 | |
1244 gint | |
11183 | 1245 gaim_cipher_context_decrypt(GaimCipherContext *context, const guchar data[], |
1246 size_t len, guchar output[], size_t *outlen) | |
10684 | 1247 { |
1248 GaimCipher *cipher = NULL; | |
1249 | |
1250 g_return_val_if_fail(context, -1); | |
1251 | |
1252 cipher = context->cipher; | |
1253 g_return_val_if_fail(cipher, -1); | |
1254 | |
1255 if(cipher->ops && cipher->ops->decrypt) | |
1256 return cipher->ops->decrypt(context, data, len, output, outlen); | |
1257 else { | |
1258 gaim_debug_info("cipher", "the %s cipher does not support the decrypt" | |
1259 "operation\n", cipher->name); | |
1260 | |
1261 if(outlen) | |
1262 *outlen = -1; | |
1263 | |
1264 return -1; | |
1265 } | |
1266 } | |
1267 | |
1268 void | |
11183 | 1269 gaim_cipher_context_set_salt(GaimCipherContext *context, guchar *salt) { |
10684 | 1270 GaimCipher *cipher = NULL; |
1271 | |
1272 g_return_if_fail(context); | |
1273 | |
1274 cipher = context->cipher; | |
1275 g_return_if_fail(cipher); | |
1276 | |
1277 if(cipher->ops && cipher->ops->set_salt) | |
1278 cipher->ops->set_salt(context, salt); | |
1279 else | |
1280 gaim_debug_info("cipher", "the %s cipher does not support the " | |
1281 "set_salt operation\n", cipher->name); | |
1282 } | |
1283 | |
1284 size_t | |
1285 gaim_cipher_context_get_salt_size(GaimCipherContext *context) { | |
1286 GaimCipher *cipher = NULL; | |
1287 | |
1288 g_return_val_if_fail(context, -1); | |
1289 | |
1290 cipher = context->cipher; | |
1291 g_return_val_if_fail(cipher, -1); | |
1292 | |
1293 if(cipher->ops && cipher->ops->get_salt_size) | |
1294 return cipher->ops->get_salt_size(context); | |
1295 else { | |
1296 gaim_debug_info("cipher", "the %s cipher does not support the " | |
1297 "get_salt_size operation\n", cipher->name); | |
1298 | |
1299 return -1; | |
1300 } | |
1301 } | |
1302 | |
1303 void | |
11183 | 1304 gaim_cipher_context_set_key(GaimCipherContext *context, guchar *key) { |
10684 | 1305 GaimCipher *cipher = NULL; |
1306 | |
1307 g_return_if_fail(context); | |
1308 | |
1309 cipher = context->cipher; | |
1310 g_return_if_fail(cipher); | |
1311 | |
1312 if(cipher->ops && cipher->ops->set_key) | |
1313 cipher->ops->set_key(context, key); | |
1314 else | |
1315 gaim_debug_info("cipher", "the %s cipher does not support the " | |
1316 "set_key operation\n", cipher->name); | |
1317 } | |
1318 | |
1319 size_t | |
1320 gaim_cipher_context_get_key_size(GaimCipherContext *context) { | |
1321 GaimCipher *cipher = NULL; | |
1322 | |
1323 g_return_val_if_fail(context, -1); | |
1324 | |
1325 cipher = context->cipher; | |
1326 g_return_val_if_fail(cipher, -1); | |
1327 | |
1328 if(cipher->ops && cipher->ops->get_key_size) | |
1329 return cipher->ops->get_key_size(context); | |
1330 else { | |
1331 gaim_debug_info("cipher", "the %s cipher does not support the " | |
1332 "get_key_size operation\n", cipher->name); | |
1333 | |
1334 return -1; | |
1335 } | |
1336 } | |
1337 | |
1338 void | |
1339 gaim_cipher_context_set_data(GaimCipherContext *context, gpointer data) { | |
1340 g_return_if_fail(context); | |
1341 | |
1342 context->data = data; | |
1343 } | |
1344 | |
1345 gpointer | |
1346 gaim_cipher_context_get_data(GaimCipherContext *context) { | |
1347 g_return_val_if_fail(context, NULL); | |
1348 | |
1349 return context->data; | |
1350 } |