Remove useless comparison
[pidgin-git.git] / libpurple / cipher.c
blobd0a7bb7937acae30ecaba65563f71b1a0aa9ecf7
1 /*
2 * purple
4 * Purple 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.
8 * Original des taken from gpg
10 * des.c - DES and Triple-DES encryption/decryption Algorithm
11 * Copyright (C) 1998 Free Software Foundation, Inc.
13 * Please see below for more legal information!
15 * According to the definition of DES in FIPS PUB 46-2 from December 1993.
16 * For a description of triple encryption, see:
17 * Bruce Schneier: Applied Cryptography. Second Edition.
18 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
20 * This file is part of GnuPG.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
36 #include "internal.h"
37 #include "cipher.h"
38 #include "dbus-maybe.h"
39 #include "debug.h"
40 #include "signals.h"
41 #include "value.h"
43 /*******************************************************************************
44 * Structs
45 ******************************************************************************/
46 struct _PurpleCipher {
47 gchar *name; /**< Internal name - used for searching */
48 PurpleCipherOps *ops; /**< Operations supported by this cipher */
49 guint ref; /**< Reference count */
52 struct _PurpleCipherContext {
53 PurpleCipher *cipher; /**< Cipher this context is under */
54 gpointer data; /**< Internal cipher state data */
57 /******************************************************************************
58 * Globals
59 *****************************************************************************/
60 static GList *ciphers = NULL;
62 /******************************************************************************
63 * PurpleCipher API
64 *****************************************************************************/
65 const gchar *
66 purple_cipher_get_name(PurpleCipher *cipher) {
67 g_return_val_if_fail(cipher, NULL);
69 return cipher->name;
72 guint
73 purple_cipher_get_capabilities(PurpleCipher *cipher) {
74 PurpleCipherOps *ops = NULL;
75 guint caps = 0;
77 g_return_val_if_fail(cipher, 0);
79 ops = cipher->ops;
80 g_return_val_if_fail(ops, 0);
82 if(ops->set_option)
83 caps |= PURPLE_CIPHER_CAPS_SET_OPT;
84 if(ops->get_option)
85 caps |= PURPLE_CIPHER_CAPS_GET_OPT;
86 if(ops->init)
87 caps |= PURPLE_CIPHER_CAPS_INIT;
88 if(ops->reset)
89 caps |= PURPLE_CIPHER_CAPS_RESET;
90 if(ops->uninit)
91 caps |= PURPLE_CIPHER_CAPS_UNINIT;
92 if(ops->set_iv)
93 caps |= PURPLE_CIPHER_CAPS_SET_IV;
94 if(ops->append)
95 caps |= PURPLE_CIPHER_CAPS_APPEND;
96 if(ops->digest)
97 caps |= PURPLE_CIPHER_CAPS_DIGEST;
98 if(ops->encrypt)
99 caps |= PURPLE_CIPHER_CAPS_ENCRYPT;
100 if(ops->decrypt)
101 caps |= PURPLE_CIPHER_CAPS_DECRYPT;
102 if(ops->set_salt)
103 caps |= PURPLE_CIPHER_CAPS_SET_SALT;
104 if(ops->get_salt_size)
105 caps |= PURPLE_CIPHER_CAPS_GET_SALT_SIZE;
106 if(ops->set_key)
107 caps |= PURPLE_CIPHER_CAPS_SET_KEY;
108 if(ops->get_key_size)
109 caps |= PURPLE_CIPHER_CAPS_GET_KEY_SIZE;
110 if(ops->set_batch_mode)
111 caps |= PURPLE_CIPHER_CAPS_SET_BATCH_MODE;
112 if(ops->get_batch_mode)
113 caps |= PURPLE_CIPHER_CAPS_GET_BATCH_MODE;
114 if(ops->get_block_size)
115 caps |= PURPLE_CIPHER_CAPS_GET_BLOCK_SIZE;
116 if(ops->set_key_with_len)
117 caps |= PURPLE_CIPHER_CAPS_SET_KEY_WITH_LEN;
119 return caps;
122 gboolean
123 purple_cipher_digest_region(const gchar *name, const guchar *data,
124 size_t data_len, size_t in_len,
125 guchar digest[], size_t *out_len)
127 PurpleCipher *cipher;
128 PurpleCipherContext *context;
129 gboolean ret = FALSE;
131 g_return_val_if_fail(name, FALSE);
132 g_return_val_if_fail(data, FALSE);
134 cipher = purple_ciphers_find_cipher(name);
136 g_return_val_if_fail(cipher, FALSE);
138 if(!cipher->ops->append || !cipher->ops->digest) {
139 purple_debug_warning("cipher", "purple_cipher_region failed: "
140 "the %s cipher does not support appending and or "
141 "digesting.", cipher->name);
142 return FALSE;
145 context = purple_cipher_context_new(cipher, NULL);
146 purple_cipher_context_append(context, data, data_len);
147 ret = purple_cipher_context_digest(context, in_len, digest, out_len);
148 purple_cipher_context_destroy(context);
150 return ret;
153 /******************************************************************************
154 * PurpleCiphers API
155 *****************************************************************************/
156 PurpleCipher *
157 purple_ciphers_find_cipher(const gchar *name) {
158 PurpleCipher *cipher;
159 GList *l;
161 g_return_val_if_fail(name, NULL);
163 for(l = ciphers; l; l = l->next) {
164 cipher = PURPLE_CIPHER(l->data);
166 if(!g_ascii_strcasecmp(cipher->name, name))
167 return cipher;
170 return NULL;
173 PurpleCipher *
174 purple_ciphers_register_cipher(const gchar *name, PurpleCipherOps *ops) {
175 PurpleCipher *cipher = NULL;
177 g_return_val_if_fail(name, NULL);
178 g_return_val_if_fail(ops, NULL);
179 g_return_val_if_fail(!purple_ciphers_find_cipher(name), NULL);
181 cipher = g_new0(PurpleCipher, 1);
182 PURPLE_DBUS_REGISTER_POINTER(cipher, PurpleCipher);
184 cipher->name = g_strdup(name);
185 cipher->ops = ops;
187 ciphers = g_list_append(ciphers, cipher);
189 purple_signal_emit(purple_ciphers_get_handle(), "cipher-added", cipher);
191 return cipher;
194 gboolean
195 purple_ciphers_unregister_cipher(PurpleCipher *cipher) {
196 g_return_val_if_fail(cipher, FALSE);
197 g_return_val_if_fail(cipher->ref == 0, FALSE);
199 purple_signal_emit(purple_ciphers_get_handle(), "cipher-removed", cipher);
201 ciphers = g_list_remove(ciphers, cipher);
203 g_free(cipher->name);
205 PURPLE_DBUS_UNREGISTER_POINTER(cipher);
206 g_free(cipher);
208 return TRUE;
211 GList *
212 purple_ciphers_get_ciphers() {
213 return ciphers;
216 /******************************************************************************
217 * PurpleCipher Subsystem API
218 *****************************************************************************/
219 gpointer
220 purple_ciphers_get_handle() {
221 static gint handle;
223 return &handle;
226 /* These are implemented in the purple-ciphers sublibrary built in the ciphers
227 * directory. We could put a header file in there, but it's less hassle for
228 * the developer to just add it here since they have to register it here as
229 * well.
231 PurpleCipherOps *purple_des_cipher_get_ops();
232 PurpleCipherOps *purple_des3_cipher_get_ops();
233 PurpleCipherOps *purple_hmac_cipher_get_ops();
234 PurpleCipherOps *purple_md4_cipher_get_ops();
235 PurpleCipherOps *purple_md5_cipher_get_ops();
236 PurpleCipherOps *purple_rc4_cipher_get_ops();
237 PurpleCipherOps *purple_sha1_cipher_get_ops();
238 PurpleCipherOps *purple_sha256_cipher_get_ops();
240 void
241 purple_ciphers_init() {
242 gpointer handle;
244 handle = purple_ciphers_get_handle();
246 purple_signal_register(handle, "cipher-added",
247 purple_marshal_VOID__POINTER, NULL, 1,
248 purple_value_new(PURPLE_TYPE_SUBTYPE,
249 PURPLE_SUBTYPE_CIPHER));
250 purple_signal_register(handle, "cipher-removed",
251 purple_marshal_VOID__POINTER, NULL, 1,
252 purple_value_new(PURPLE_TYPE_SUBTYPE,
253 PURPLE_SUBTYPE_CIPHER));
255 purple_ciphers_register_cipher("md5", purple_md5_cipher_get_ops());
256 purple_ciphers_register_cipher("sha1", purple_sha1_cipher_get_ops());
257 purple_ciphers_register_cipher("sha256", purple_sha256_cipher_get_ops());
258 purple_ciphers_register_cipher("md4", purple_md4_cipher_get_ops());
259 purple_ciphers_register_cipher("hmac", purple_hmac_cipher_get_ops());
260 purple_ciphers_register_cipher("des", purple_des_cipher_get_ops());
261 purple_ciphers_register_cipher("des3", purple_des3_cipher_get_ops());
262 purple_ciphers_register_cipher("rc4", purple_rc4_cipher_get_ops());
265 void
266 purple_ciphers_uninit() {
267 PurpleCipher *cipher;
268 GList *l, *ll;
270 for(l = ciphers; l; l = ll) {
271 ll = l->next;
273 cipher = PURPLE_CIPHER(l->data);
274 purple_ciphers_unregister_cipher(cipher);
277 g_list_free(ciphers);
279 purple_signals_unregister_by_instance(purple_ciphers_get_handle());
282 /******************************************************************************
283 * PurpleCipherContext API
284 *****************************************************************************/
285 void
286 purple_cipher_context_set_option(PurpleCipherContext *context, const gchar *name,
287 gpointer value)
289 PurpleCipher *cipher = NULL;
291 g_return_if_fail(context);
292 g_return_if_fail(name);
294 cipher = context->cipher;
295 g_return_if_fail(cipher);
297 if(cipher->ops && cipher->ops->set_option)
298 cipher->ops->set_option(context, name, value);
299 else
300 purple_debug_warning("cipher", "the %s cipher does not support the "
301 "set_option operation\n", cipher->name);
304 gpointer
305 purple_cipher_context_get_option(PurpleCipherContext *context, const gchar *name) {
306 PurpleCipher *cipher = NULL;
308 g_return_val_if_fail(context, NULL);
309 g_return_val_if_fail(name, NULL);
311 cipher = context->cipher;
312 g_return_val_if_fail(cipher, NULL);
314 if(cipher->ops && cipher->ops->get_option)
315 return cipher->ops->get_option(context, name);
316 else {
317 purple_debug_warning("cipher", "the %s cipher does not support the "
318 "get_option operation\n", cipher->name);
320 return NULL;
324 PurpleCipherContext *
325 purple_cipher_context_new(PurpleCipher *cipher, void *extra) {
326 PurpleCipherContext *context = NULL;
328 g_return_val_if_fail(cipher, NULL);
330 cipher->ref++;
332 context = g_new0(PurpleCipherContext, 1);
333 context->cipher = cipher;
335 if(cipher->ops->init)
336 cipher->ops->init(context, extra);
338 return context;
341 PurpleCipherContext *
342 purple_cipher_context_new_by_name(const gchar *name, void *extra) {
343 PurpleCipher *cipher;
345 g_return_val_if_fail(name, NULL);
347 cipher = purple_ciphers_find_cipher(name);
349 g_return_val_if_fail(cipher, NULL);
351 return purple_cipher_context_new(cipher, extra);
354 void
355 purple_cipher_context_reset(PurpleCipherContext *context, void *extra) {
356 PurpleCipher *cipher = NULL;
358 g_return_if_fail(context);
360 cipher = context->cipher;
361 g_return_if_fail(cipher);
363 if(cipher->ops && cipher->ops->reset)
364 context->cipher->ops->reset(context, extra);
367 void
368 purple_cipher_context_destroy(PurpleCipherContext *context) {
369 PurpleCipher *cipher = NULL;
371 g_return_if_fail(context);
373 cipher = context->cipher;
374 g_return_if_fail(cipher);
376 cipher->ref--;
378 if(cipher->ops && cipher->ops->uninit)
379 cipher->ops->uninit(context);
381 memset(context, 0, sizeof(*context));
382 g_free(context);
383 context = NULL;
386 void
387 purple_cipher_context_set_iv(PurpleCipherContext *context, guchar *iv, size_t len)
389 PurpleCipher *cipher = NULL;
391 g_return_if_fail(context);
392 g_return_if_fail(iv);
394 cipher = context->cipher;
395 g_return_if_fail(cipher);
397 if(cipher->ops && cipher->ops->set_iv)
398 cipher->ops->set_iv(context, iv, len);
399 else
400 purple_debug_warning("cipher", "the %s cipher does not support the set"
401 "initialization vector operation\n", cipher->name);
404 void
405 purple_cipher_context_append(PurpleCipherContext *context, const guchar *data,
406 size_t len)
408 PurpleCipher *cipher = NULL;
410 g_return_if_fail(context);
412 cipher = context->cipher;
413 g_return_if_fail(cipher);
415 if(cipher->ops && cipher->ops->append)
416 cipher->ops->append(context, data, len);
417 else
418 purple_debug_warning("cipher", "the %s cipher does not support the append "
419 "operation\n", cipher->name);
422 gboolean
423 purple_cipher_context_digest(PurpleCipherContext *context, size_t in_len,
424 guchar digest[], size_t *out_len)
426 PurpleCipher *cipher = NULL;
428 g_return_val_if_fail(context, FALSE);
430 cipher = context->cipher;
432 if(cipher->ops && cipher->ops->digest)
433 return cipher->ops->digest(context, in_len, digest, out_len);
434 else {
435 purple_debug_warning("cipher", "the %s cipher does not support the digest "
436 "operation\n", cipher->name);
437 return FALSE;
441 gboolean
442 purple_cipher_context_digest_to_str(PurpleCipherContext *context, size_t in_len,
443 gchar digest_s[], size_t *out_len)
445 /* 8k is a bit excessive, will tweak later. */
446 guchar digest[BUF_LEN * 4];
447 size_t n, dlen = 0;
449 g_return_val_if_fail(context, FALSE);
450 g_return_val_if_fail(digest_s, FALSE);
452 if(!purple_cipher_context_digest(context, sizeof(digest), digest, &dlen))
453 return FALSE;
455 /* in_len must be greater than dlen * 2 so we have room for the NUL. */
456 if(in_len <= dlen * 2)
457 return FALSE;
459 for(n = 0; n < dlen; n++)
460 sprintf(digest_s + (n * 2), "%02x", digest[n]);
462 digest_s[n * 2] = '\0';
464 if(out_len)
465 *out_len = dlen * 2;
467 return TRUE;
470 gint
471 purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar data[],
472 size_t len, guchar output[], size_t *outlen)
474 PurpleCipher *cipher = NULL;
476 g_return_val_if_fail(context, -1);
478 cipher = context->cipher;
479 g_return_val_if_fail(cipher, -1);
481 if(cipher->ops && cipher->ops->encrypt)
482 return cipher->ops->encrypt(context, data, len, output, outlen);
483 else {
484 purple_debug_warning("cipher", "the %s cipher does not support the encrypt"
485 "operation\n", cipher->name);
487 if(outlen)
488 *outlen = -1;
490 return -1;
494 gint
495 purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar data[],
496 size_t len, guchar output[], size_t *outlen)
498 PurpleCipher *cipher = NULL;
500 g_return_val_if_fail(context, -1);
502 cipher = context->cipher;
503 g_return_val_if_fail(cipher, -1);
505 if(cipher->ops && cipher->ops->decrypt)
506 return cipher->ops->decrypt(context, data, len, output, outlen);
507 else {
508 purple_debug_warning("cipher", "the %s cipher does not support the decrypt"
509 "operation\n", cipher->name);
511 if(outlen)
512 *outlen = -1;
514 return -1;
518 void
519 purple_cipher_context_set_salt(PurpleCipherContext *context, guchar *salt) {
520 PurpleCipher *cipher = NULL;
522 g_return_if_fail(context);
524 cipher = context->cipher;
525 g_return_if_fail(cipher);
527 if(cipher->ops && cipher->ops->set_salt)
528 cipher->ops->set_salt(context, salt);
529 else
530 purple_debug_warning("cipher", "the %s cipher does not support the "
531 "set_salt operation\n", cipher->name);
534 size_t
535 purple_cipher_context_get_salt_size(PurpleCipherContext *context) {
536 PurpleCipher *cipher = NULL;
538 g_return_val_if_fail(context, -1);
540 cipher = context->cipher;
541 g_return_val_if_fail(cipher, -1);
543 if(cipher->ops && cipher->ops->get_salt_size)
544 return cipher->ops->get_salt_size(context);
545 else {
546 purple_debug_warning("cipher", "the %s cipher does not support the "
547 "get_salt_size operation\n", cipher->name);
549 return -1;
553 void
554 purple_cipher_context_set_key(PurpleCipherContext *context, const guchar *key) {
555 PurpleCipher *cipher = NULL;
557 g_return_if_fail(context);
559 cipher = context->cipher;
560 g_return_if_fail(cipher);
562 if(cipher->ops && cipher->ops->set_key)
563 cipher->ops->set_key(context, key);
564 else
565 purple_debug_warning("cipher", "the %s cipher does not support the "
566 "set_key operation\n", cipher->name);
569 size_t
570 purple_cipher_context_get_key_size(PurpleCipherContext *context) {
571 PurpleCipher *cipher = NULL;
573 g_return_val_if_fail(context, -1);
575 cipher = context->cipher;
576 g_return_val_if_fail(cipher, -1);
578 if(cipher->ops && cipher->ops->get_key_size)
579 return cipher->ops->get_key_size(context);
580 else {
581 purple_debug_warning("cipher", "the %s cipher does not support the "
582 "get_key_size operation\n", cipher->name);
584 return -1;
588 void
589 purple_cipher_context_set_batch_mode(PurpleCipherContext *context,
590 PurpleCipherBatchMode mode)
592 PurpleCipher *cipher = NULL;
594 g_return_if_fail(context);
596 cipher = context->cipher;
597 g_return_if_fail(cipher);
599 if(cipher->ops && cipher->ops->set_batch_mode)
600 cipher->ops->set_batch_mode(context, mode);
601 else
602 purple_debug_warning("cipher", "The %s cipher does not support the "
603 "set_batch_mode operation\n", cipher->name);
606 PurpleCipherBatchMode
607 purple_cipher_context_get_batch_mode(PurpleCipherContext *context)
609 PurpleCipher *cipher = NULL;
611 g_return_val_if_fail(context, -1);
613 cipher = context->cipher;
614 g_return_val_if_fail(cipher, -1);
616 if(cipher->ops && cipher->ops->get_batch_mode)
617 return cipher->ops->get_batch_mode(context);
618 else {
619 purple_debug_warning("cipher", "The %s cipher does not support the "
620 "get_batch_mode operation\n", cipher->name);
621 return -1;
625 size_t
626 purple_cipher_context_get_block_size(PurpleCipherContext *context)
628 PurpleCipher *cipher = NULL;
630 g_return_val_if_fail(context, -1);
632 cipher = context->cipher;
633 g_return_val_if_fail(cipher, -1);
635 if(cipher->ops && cipher->ops->get_block_size)
636 return cipher->ops->get_block_size(context);
637 else {
638 purple_debug_warning("cipher", "The %s cipher does not support the "
639 "get_block_size operation\n", cipher->name);
640 return -1;
644 void
645 purple_cipher_context_set_key_with_len(PurpleCipherContext *context,
646 const guchar *key, size_t len)
648 PurpleCipher *cipher = NULL;
650 g_return_if_fail(context);
652 cipher = context->cipher;
653 g_return_if_fail(cipher);
655 if(cipher->ops && cipher->ops->set_key_with_len)
656 cipher->ops->set_key_with_len(context, key, len);
657 else
658 purple_debug_warning("cipher", "The %s cipher does not support the "
659 "set_key_with_len operation\n", cipher->name);
662 void
663 purple_cipher_context_set_data(PurpleCipherContext *context, gpointer data) {
664 g_return_if_fail(context);
666 context->data = data;
669 gpointer
670 purple_cipher_context_get_data(PurpleCipherContext *context) {
671 g_return_val_if_fail(context, NULL);
673 return context->data;
676 gchar *purple_cipher_http_digest_calculate_session_key(
677 const gchar *algorithm,
678 const gchar *username,
679 const gchar *realm,
680 const gchar *password,
681 const gchar *nonce,
682 const gchar *client_nonce)
684 PurpleCipher *cipher;
685 PurpleCipherContext *context;
686 gchar hash[33]; /* We only support MD5. */
688 g_return_val_if_fail(username != NULL, NULL);
689 g_return_val_if_fail(realm != NULL, NULL);
690 g_return_val_if_fail(password != NULL, NULL);
691 g_return_val_if_fail(nonce != NULL, NULL);
693 /* Check for a supported algorithm. */
694 g_return_val_if_fail(algorithm == NULL ||
695 *algorithm == '\0' ||
696 g_ascii_strcasecmp(algorithm, "MD5") ||
697 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
699 cipher = purple_ciphers_find_cipher("md5");
700 g_return_val_if_fail(cipher != NULL, NULL);
702 context = purple_cipher_context_new(cipher, NULL);
704 purple_cipher_context_append(context, (guchar *)username, strlen(username));
705 purple_cipher_context_append(context, (guchar *)":", 1);
706 purple_cipher_context_append(context, (guchar *)realm, strlen(realm));
707 purple_cipher_context_append(context, (guchar *)":", 1);
708 purple_cipher_context_append(context, (guchar *)password, strlen(password));
710 if (algorithm != NULL && !g_ascii_strcasecmp(algorithm, "MD5-sess"))
712 guchar digest[16];
714 if (client_nonce == NULL)
716 purple_cipher_context_destroy(context);
717 purple_debug_error("cipher", "Required client_nonce missing for MD5-sess digest calculation.\n");
718 return NULL;
721 purple_cipher_context_digest(context, sizeof(digest), digest, NULL);
722 purple_cipher_context_destroy(context);
724 context = purple_cipher_context_new(cipher, NULL);
725 purple_cipher_context_append(context, digest, sizeof(digest));
726 purple_cipher_context_append(context, (guchar *)":", 1);
727 purple_cipher_context_append(context, (guchar *)nonce, strlen(nonce));
728 purple_cipher_context_append(context, (guchar *)":", 1);
729 purple_cipher_context_append(context, (guchar *)client_nonce, strlen(client_nonce));
732 purple_cipher_context_digest_to_str(context, sizeof(hash), hash, NULL);
733 purple_cipher_context_destroy(context);
735 return g_strdup(hash);
738 gchar *purple_cipher_http_digest_calculate_response(
739 const gchar *algorithm,
740 const gchar *method,
741 const gchar *digest_uri,
742 const gchar *qop,
743 const gchar *entity,
744 const gchar *nonce,
745 const gchar *nonce_count,
746 const gchar *client_nonce,
747 const gchar *session_key)
749 PurpleCipher *cipher;
750 PurpleCipherContext *context;
751 static gchar hash2[33]; /* We only support MD5. */
753 g_return_val_if_fail(method != NULL, NULL);
754 g_return_val_if_fail(digest_uri != NULL, NULL);
755 g_return_val_if_fail(nonce != NULL, NULL);
756 g_return_val_if_fail(session_key != NULL, NULL);
758 /* Check for a supported algorithm. */
759 g_return_val_if_fail(algorithm == NULL ||
760 *algorithm == '\0' ||
761 g_ascii_strcasecmp(algorithm, "MD5") ||
762 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
764 /* Check for a supported "quality of protection". */
765 g_return_val_if_fail(qop == NULL ||
766 *qop == '\0' ||
767 g_ascii_strcasecmp(qop, "auth") ||
768 g_ascii_strcasecmp(qop, "auth-int"), NULL);
770 cipher = purple_ciphers_find_cipher("md5");
771 g_return_val_if_fail(cipher != NULL, NULL);
773 context = purple_cipher_context_new(cipher, NULL);
775 purple_cipher_context_append(context, (guchar *)method, strlen(method));
776 purple_cipher_context_append(context, (guchar *)":", 1);
777 purple_cipher_context_append(context, (guchar *)digest_uri, strlen(digest_uri));
779 if (qop != NULL && !g_ascii_strcasecmp(qop, "auth-int"))
781 PurpleCipherContext *context2;
782 gchar entity_hash[33];
784 if (entity == NULL)
786 purple_cipher_context_destroy(context);
787 purple_debug_error("cipher", "Required entity missing for auth-int digest calculation.\n");
788 return NULL;
791 context2 = purple_cipher_context_new(cipher, NULL);
792 purple_cipher_context_append(context2, (guchar *)entity, strlen(entity));
793 purple_cipher_context_digest_to_str(context2, sizeof(entity_hash), entity_hash, NULL);
794 purple_cipher_context_destroy(context2);
796 purple_cipher_context_append(context, (guchar *)":", 1);
797 purple_cipher_context_append(context, (guchar *)entity_hash, strlen(entity_hash));
800 purple_cipher_context_digest_to_str(context, sizeof(hash2), hash2, NULL);
801 purple_cipher_context_destroy(context);
803 context = purple_cipher_context_new(cipher, NULL);
804 purple_cipher_context_append(context, (guchar *)session_key, strlen(session_key));
805 purple_cipher_context_append(context, (guchar *)":", 1);
806 purple_cipher_context_append(context, (guchar *)nonce, strlen(nonce));
807 purple_cipher_context_append(context, (guchar *)":", 1);
809 if (qop != NULL && *qop != '\0')
811 if (nonce_count == NULL)
813 purple_cipher_context_destroy(context);
814 purple_debug_error("cipher", "Required nonce_count missing for digest calculation.\n");
815 return NULL;
818 if (client_nonce == NULL)
820 purple_cipher_context_destroy(context);
821 purple_debug_error("cipher", "Required client_nonce missing for digest calculation.\n");
822 return NULL;
825 purple_cipher_context_append(context, (guchar *)nonce_count, strlen(nonce_count));
826 purple_cipher_context_append(context, (guchar *)":", 1);
827 purple_cipher_context_append(context, (guchar *)client_nonce, strlen(client_nonce));
828 purple_cipher_context_append(context, (guchar *)":", 1);
830 purple_cipher_context_append(context, (guchar *)qop, strlen(qop));
832 purple_cipher_context_append(context, (guchar *)":", 1);
835 purple_cipher_context_append(context, (guchar *)hash2, strlen(hash2));
836 purple_cipher_context_digest_to_str(context, sizeof(hash2), hash2, NULL);
837 purple_cipher_context_destroy(context);
839 return g_strdup(hash2);