libpurple/ciphers/des3cipher.c

changeset 38413
e35c6b6ca094
parent 38412
e25c73450414
child 38414
3892b306732f
equal deleted inserted replaced
38412:e25c73450414 38413:e35c6b6ca094
1 /*
2 * Original des taken from gpg
3 *
4 * des.c - Triple-DES encryption/decryption Algorithm
5 * Copyright (C) 1998 Free Software Foundation, Inc.
6 *
7 * Please see below for more legal information!
8 *
9 * According to the definition of DES in FIPS PUB 46-2 from December 1993.
10 * For a description of triple encryption, see:
11 * Bruce Schneier: Applied Cryptography. Second Edition.
12 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
13 *
14 * Purple is the legal property of its developers, whose names are too numerous
15 * to list here. Please refer to the COPYRIGHT file distributed with this
16 * source distribution.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
31 */
32 #include "internal.h"
33 #include "glibcompat.h"
34
35 #include "des3cipher.h"
36 #include "descipher.h"
37 #include "enums.h"
38
39 #include <string.h>
40
41 /******************************************************************************
42 * Structs
43 *****************************************************************************/
44
45 #define PURPLE_DES3_CIPHER_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_DES3_CIPHER, PurpleDES3CipherPrivate))
47
48 typedef struct _PurpleDES3CipherPrivate PurpleDES3CipherPrivate;
49 struct _PurpleDES3CipherPrivate
50 {
51 PurpleCipherBatchMode mode;
52 guchar iv[8];
53 /* First key for encryption */
54 PurpleCipher *key1;
55 /* Second key for decryption */
56 PurpleCipher *key2;
57 /* Third key for encryption */
58 PurpleCipher *key3;
59 };
60
61 /******************************************************************************
62 * Enums
63 *****************************************************************************/
64 enum {
65 PROP_NONE,
66 PROP_BATCH_MODE,
67 PROP_IV,
68 PROP_KEY,
69 PROP_LAST,
70 };
71
72 /******************************************************************************
73 * Globals
74 *****************************************************************************/
75 static GObjectClass *parent_class = NULL;
76 static GParamSpec *properties[PROP_LAST];
77
78 /******************************************************************************
79 * Cipher Stuff
80 *****************************************************************************/
81
82 static size_t
83 purple_des3_cipher_get_key_size(PurpleCipher *cipher)
84 {
85 return 24;
86 }
87
88 /*
89 * Fill a DES3 context with subkeys calculated from 3 64bit key.
90 * Does not check parity bits, but simply ignore them.
91 * Does not check for weak keys.
92 **/
93 static void
94 purple_des3_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len)
95 {
96 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
97 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
98
99 g_return_if_fail(len == 24);
100
101 purple_cipher_set_key(PURPLE_CIPHER(priv->key1), key + 0,
102 purple_cipher_get_key_size(PURPLE_CIPHER(priv->key1)));
103 purple_cipher_set_key(PURPLE_CIPHER(priv->key2), key + 8,
104 purple_cipher_get_key_size(PURPLE_CIPHER(priv->key2)));
105 purple_cipher_set_key(PURPLE_CIPHER(priv->key3), key + 16,
106 purple_cipher_get_key_size(PURPLE_CIPHER(priv->key3)));
107
108 g_object_notify_by_pspec(G_OBJECT(cipher), properties[PROP_KEY]);
109 }
110
111 static ssize_t
112 purple_des3_cipher_ecb_encrypt(PurpleDES3Cipher *des3_cipher, const guchar input[],
113 size_t in_len, guchar output[], size_t out_size)
114 {
115 gsize offset = 0;
116 int i = 0;
117 gsize tmp;
118 guint8 buf[8] = {0,0,0,0,0,0,0,0};
119 gsize out_len;
120 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
121
122 g_return_val_if_fail(out_size >= in_len, -1);
123
124 while (offset + 8 <= in_len) {
125 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
126 input + offset, output + offset, 0);
127 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
128 output + offset, buf, 1);
129 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
130 buf, output + offset, 0);
131
132 offset += 8;
133 }
134
135 out_len = in_len;
136 if (offset < in_len) {
137 g_return_val_if_fail(in_len >= offset, -1);
138 out_len += in_len - offset;
139 g_return_val_if_fail(out_size >= out_len, -1);
140 tmp = offset;
141 memset(buf, 0, 8);
142 while (tmp < in_len) {
143 buf[i++] = input[tmp];
144 tmp++;
145 }
146
147 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
148 buf, output + offset, 0);
149 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
150 output + offset, buf, 1);
151 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
152 buf, output + offset, 0);
153 }
154
155 return out_len;
156 }
157
158 static ssize_t
159 purple_des3_cipher_cbc_encrypt(PurpleDES3Cipher *des3_cipher, const guchar input[],
160 size_t in_len, guchar output[], size_t out_size)
161 {
162 gsize offset = 0;
163 int i = 0;
164 gsize tmp;
165 guint8 buf[8];
166 gsize out_len;
167 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
168
169 memcpy(buf, priv->iv, 8);
170
171 g_return_val_if_fail(out_size >= in_len, -1);
172
173 while (offset + 8 <= in_len) {
174 for (i = 0; i < 8; i++)
175 buf[i] ^= input[offset + i];
176
177 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
178 buf, output + offset, 0);
179 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
180 output + offset, buf, 1);
181 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
182 buf, output + offset, 0);
183
184 memcpy(buf, output+offset, 8);
185 offset += 8;
186 }
187
188 out_len = in_len;
189 if (offset < in_len) {
190 g_return_val_if_fail(in_len >= offset, -1);
191 out_len += in_len - offset;
192 g_return_val_if_fail(out_size >= out_len, -1);
193 tmp = offset;
194 i = 0;
195 while (tmp < in_len) {
196 buf[i++] ^= input[tmp];
197 tmp++;
198 }
199
200 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
201 buf, output + offset, 0);
202 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
203 output + offset, buf, 1);
204 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
205 buf, output + offset, 0);
206 }
207
208 return out_len;
209 }
210
211 static ssize_t
212 purple_des3_cipher_encrypt(PurpleCipher *cipher, const guchar input[],
213 size_t in_len, guchar output[], size_t out_size)
214 {
215 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
216 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
217
218 if (priv->mode == PURPLE_CIPHER_BATCH_MODE_ECB) {
219 return purple_des3_cipher_ecb_encrypt(des3_cipher, input, in_len, output, out_size);
220 } else if (priv->mode == PURPLE_CIPHER_BATCH_MODE_CBC) {
221 return purple_des3_cipher_cbc_encrypt(des3_cipher, input, in_len, output, out_size);
222 } else {
223 g_return_val_if_reached(0);
224 }
225
226 return 0;
227 }
228
229 static ssize_t
230 purple_des3_cipher_ecb_decrypt(PurpleDES3Cipher *des3_cipher, const guchar input[],
231 size_t in_len, guchar output[], size_t out_size)
232 {
233 gsize offset = 0;
234 int i = 0;
235 gsize tmp;
236 guint8 buf[8] = {0,0,0,0,0,0,0,0};
237 gsize out_len;
238 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
239
240 g_return_val_if_fail(out_size >= in_len, -1);
241
242 while (offset + 8 <= in_len) {
243 /* NOTE: Apply key in reverse */
244 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
245 input + offset, output + offset, 1);
246 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
247 output + offset, buf, 0);
248 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
249 buf, output + offset, 1);
250
251 offset += 8;
252 }
253
254 out_len = in_len;
255 if (offset < in_len) {
256 g_return_val_if_fail(in_len >= offset, -1);
257 out_len += in_len - offset;
258 g_return_val_if_fail(out_size >= out_len, -1);
259 tmp = offset;
260 memset(buf, 0, 8);
261 while (tmp < in_len) {
262 buf[i++] = input[tmp];
263 tmp++;
264 }
265
266 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
267 buf, output + offset, 1);
268 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
269 output + offset, buf, 0);
270 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
271 buf, output + offset, 1);
272 }
273
274 return out_len;
275 }
276
277 static ssize_t
278 purple_des3_cipher_cbc_decrypt(PurpleDES3Cipher *des3_cipher, const guchar input[],
279 size_t in_len, guchar output[], size_t out_size)
280 {
281 gsize offset = 0;
282 int i = 0;
283 gsize tmp;
284 guint8 buf[8] = {0,0,0,0,0,0,0,0};
285 guint8 link[8];
286 gsize out_len;
287 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
288
289 g_return_val_if_fail(out_size >= in_len, -1);
290
291 memcpy(link, priv->iv, 8);
292 while (offset + 8 <= in_len) {
293 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
294 input + offset, output + offset, 1);
295 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
296 output + offset, buf, 0);
297 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
298 buf, output + offset, 1);
299
300 for (i = 0; i < 8; i++)
301 output[offset + i] ^= link[i];
302
303 memcpy(link, input + offset, 8);
304
305 offset+=8;
306 }
307
308 out_len = in_len;
309 if(offset<in_len) {
310 g_return_val_if_fail(in_len >= offset, -1);
311 out_len += in_len - offset;
312 g_return_val_if_fail(out_size >= out_len, -1);
313 tmp = offset;
314 memset(buf, 0, 8);
315 i = 0;
316 while(tmp<in_len) {
317 buf[i++] = input[tmp];
318 tmp++;
319 }
320
321 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key3),
322 buf, output + offset, 1);
323 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key2),
324 output + offset, buf, 0);
325 purple_des_cipher_ecb_crypt(PURPLE_DES_CIPHER(priv->key1),
326 buf, output + offset, 1);
327
328 for (i = 0; i < 8; i++)
329 output[offset + i] ^= link[i];
330 }
331
332 return out_len;
333 }
334
335 static ssize_t
336 purple_des3_cipher_decrypt(PurpleCipher *cipher, const guchar input[],
337 size_t in_len, guchar output[], size_t out_size)
338 {
339 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
340 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(cipher);
341
342 if (priv->mode == PURPLE_CIPHER_BATCH_MODE_ECB) {
343 return purple_des3_cipher_ecb_decrypt(des3_cipher, input, in_len, output, out_size);
344 } else if (priv->mode == PURPLE_CIPHER_BATCH_MODE_CBC) {
345 return purple_des3_cipher_cbc_decrypt(des3_cipher, input, in_len, output, out_size);
346 } else {
347 g_return_val_if_reached(0);
348 }
349
350 return 0;
351 }
352
353 static void
354 purple_des3_cipher_set_batch_mode(PurpleCipher *cipher, PurpleCipherBatchMode mode)
355 {
356 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
357 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
358
359 priv->mode = mode;
360
361 g_object_notify_by_pspec(G_OBJECT(cipher), properties[PROP_BATCH_MODE]);
362 }
363
364 static PurpleCipherBatchMode
365 purple_des3_cipher_get_batch_mode(PurpleCipher *cipher)
366 {
367 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
368 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
369
370 return priv->mode;
371 }
372
373 static void
374 purple_des3_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len)
375 {
376 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
377 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
378
379 g_return_if_fail(len == 8);
380
381 memcpy(priv->iv, iv, len);
382
383 g_object_notify_by_pspec(G_OBJECT(cipher), properties[PROP_IV]);
384 }
385
386 /******************************************************************************
387 * Object Stuff
388 *****************************************************************************/
389 static void
390 purple_des3_cipher_get_property(GObject *obj, guint param_id, GValue *value,
391 GParamSpec *pspec)
392 {
393 PurpleCipher *cipher = PURPLE_CIPHER(obj);
394
395 switch(param_id) {
396 case PROP_BATCH_MODE:
397 g_value_set_enum(value,
398 purple_cipher_get_batch_mode(cipher));
399 break;
400 default:
401 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
402 break;
403 }
404 }
405
406 static void
407 purple_des3_cipher_set_property(GObject *obj, guint param_id,
408 const GValue *value, GParamSpec *pspec)
409 {
410 PurpleCipher *cipher = PURPLE_CIPHER(obj);
411
412 switch(param_id) {
413 case PROP_BATCH_MODE:
414 purple_cipher_set_batch_mode(cipher,
415 g_value_get_enum(value));
416 break;
417 case PROP_IV:
418 {
419 guchar *iv = (guchar *)g_value_get_string(value);
420 purple_cipher_set_iv(cipher, iv, strlen((gchar*)iv));
421 }
422 break;
423 case PROP_KEY:
424 purple_cipher_set_key(cipher, (guchar *)g_value_get_string(value),
425 purple_des3_cipher_get_key_size(cipher));
426 break;
427 default:
428 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
429 break;
430 }
431 }
432
433 static void
434 purple_des3_cipher_finalize(GObject *obj)
435 {
436 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(obj);
437 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
438
439 g_object_unref(G_OBJECT(priv->key1));
440 g_object_unref(G_OBJECT(priv->key2));
441 g_object_unref(G_OBJECT(priv->key3));
442
443 memset(priv->iv, 0, sizeof(priv->iv));
444
445 parent_class->finalize(obj);
446 }
447
448 static void
449 purple_des3_cipher_class_init(PurpleDES3CipherClass *klass) {
450 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
451 PurpleCipherClass *cipher_class = PURPLE_CIPHER_CLASS(klass);
452
453 parent_class = g_type_class_peek_parent(klass);
454
455 obj_class->finalize = purple_des3_cipher_finalize;
456 obj_class->get_property = purple_des3_cipher_get_property;
457 obj_class->set_property = purple_des3_cipher_set_property;
458
459 cipher_class->set_iv = purple_des3_cipher_set_iv;
460 cipher_class->encrypt = purple_des3_cipher_encrypt;
461 cipher_class->decrypt = purple_des3_cipher_decrypt;
462 cipher_class->set_key = purple_des3_cipher_set_key;
463 cipher_class->set_batch_mode = purple_des3_cipher_set_batch_mode;
464 cipher_class->get_batch_mode = purple_des3_cipher_get_batch_mode;
465 cipher_class->get_key_size = purple_des3_cipher_get_key_size;
466
467 g_type_class_add_private(klass, sizeof(PurpleDES3CipherPrivate));
468
469 properties[PROP_BATCH_MODE] = g_param_spec_enum("batch-mode", "batch-mode",
470 "batch-mode", PURPLE_TYPE_CIPHER_BATCH_MODE, 0,
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
472
473 properties[PROP_IV] = g_param_spec_string("iv", "iv", "iv", NULL,
474 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
475
476 properties[PROP_KEY] = g_param_spec_string("key", "key", "key", NULL,
477 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
478
479 g_object_class_install_properties(obj_class, PROP_LAST, properties);
480 }
481
482 static void
483 purple_des3_cipher_init(PurpleCipher *cipher) {
484 PurpleDES3Cipher *des3_cipher = PURPLE_DES3_CIPHER(cipher);
485 PurpleDES3CipherPrivate *priv = PURPLE_DES3_CIPHER_GET_PRIVATE(des3_cipher);
486
487 priv->key1 = purple_des_cipher_new();
488 priv->key2 = purple_des_cipher_new();
489 priv->key3 = purple_des_cipher_new();
490 }
491
492 /******************************************************************************
493 * API
494 *****************************************************************************/
495 GType
496 purple_des3_cipher_get_type(void) {
497 static GType type = 0;
498
499 if(type == 0) {
500 static const GTypeInfo info = {
501 sizeof(PurpleDES3CipherClass),
502 NULL,
503 NULL,
504 (GClassInitFunc)purple_des3_cipher_class_init,
505 NULL,
506 NULL,
507 sizeof(PurpleDES3Cipher),
508 0,
509 (GInstanceInitFunc)purple_des3_cipher_init,
510 NULL
511 };
512
513 type = g_type_register_static(PURPLE_TYPE_CIPHER,
514 "PurpleDES3Cipher",
515 &info, 0);
516 }
517
518 return type;
519 }
520
521 PurpleCipher *
522 purple_des3_cipher_new(void) {
523 return g_object_new(PURPLE_TYPE_DES3_CIPHER, NULL);
524 }

mercurial