libpurple/protocols/jabber/auth_scram.c

Sun, 08 Nov 2009 18:39:30 +0000

author
Paul Aurich <darkrain42@pidgin.im>
date
Sun, 08 Nov 2009 18:39:30 +0000
branch
cpw.darkrain42.xmpp.scram
changeset 28865
554be021cd4c
parent 28864
5b3810bb7f1a
child 28866
e3d867ce000b
permissions
-rw-r--r--

Clean up the two temporary buffers.

28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
1 /*
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
2 * purple - Jabber Protocol Plugin
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
3 *
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
4 * Purple is the legal property of its developers, whose names are too numerous
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
6 * source distribution.
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
7 *
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
11 * (at your option) any later version.
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
12 *
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
16 * GNU General Public License for more details.
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
17 *
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
21 *
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
22 */
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
23 #include "internal.h"
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
24
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
25 #include "auth.h"
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
26 #include "auth_scram.h"
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
27
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
28 #include "cipher.h"
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
29 #include "debug.h"
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
30
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
31 static const struct {
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
32 const char *hash;
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
33 guint size;
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
34 } hash_sizes[] = {
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
35 { "sha1", 20 },
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
36 { "sha224", 28 },
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
37 { "sha256", 32 },
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
38 { "sha384", 48 },
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
39 { "sha512", 64 }
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
40 };
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
41
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
42 static guint hash_to_output_len(const gchar *hash)
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
43 {
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
44 int i;
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
45
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
46 g_return_val_if_fail(hash != NULL && *hash != '\0', 0);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
47
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
48 for (i = 0; i < G_N_ELEMENTS(hash_sizes); ++i) {
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
49 if (g_str_equal(hash, hash_sizes[i].hash))
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
50 return hash_sizes[i].size;
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
51 }
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
52
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
53 purple_debug_error("jabber", "Unknown SCRAM hash function %s\n", hash);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
54
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
55 return 0;
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
56 }
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
57
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
58 GString *jabber_auth_scram_hi(const gchar *hash, const GString *str,
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
59 GString *salt, guint iterations)
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
60 {
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
61 PurpleCipherContext *context;
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
62 GString *result;
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
63 guint i;
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
64 guint hash_len;
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
65 guchar *prev, *tmp;
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
66
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
67 g_return_val_if_fail(hash != NULL, NULL);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
68 g_return_val_if_fail(str != NULL && str->len > 0, NULL);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
69 g_return_val_if_fail(salt != NULL && salt->len > 0, NULL);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
70 g_return_val_if_fail(iterations > 0, NULL);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
71
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
72 hash_len = hash_to_output_len(hash);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
73 g_return_val_if_fail(hash_len > 0, NULL);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
74
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
75 prev = g_new0(guint8, hash_len);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
76 tmp = g_new0(guint8, hash_len);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
77
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
78 context = purple_cipher_context_new_by_name("hmac", NULL);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
79
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
80 /* Append INT(1), a four-octet encoding of the integer 1, most significant
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
81 * octet first. */
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
82 g_string_append_len(salt, "\0\0\0\1", 4);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
83
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
84 result = g_string_sized_new(hash_len);
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
85
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
86 /* Compute U0 */
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
87 purple_cipher_context_set_option(context, "hash", (gpointer)hash);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
88 purple_cipher_context_set_key_with_len(context, (guchar *)str->str, str->len);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
89 purple_cipher_context_append(context, (guchar *)salt->str, salt->len);
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
90 purple_cipher_context_digest(context, hash_len, (guchar *)result->str, &(result->len));
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
91
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
92 memcpy(prev, result->str, hash_len);
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents: 28862
diff changeset
93
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
94 /* Compute U1...Ui */
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
95 for (i = 1; i < iterations; ++i) {
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
96 guint j;
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
97 purple_cipher_context_set_option(context, "hash", (gpointer)hash);
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
98 purple_cipher_context_set_key_with_len(context, (guchar *)str->str, str->len);
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
99 purple_cipher_context_append(context, prev, hash_len);
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
100 purple_cipher_context_digest(context, hash_len, tmp, NULL);
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
101
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
102 for (j = 0; j < hash_len; ++j)
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
103 result->str[j] ^= tmp[j];
28863
b4e8c372e06b Fix the Hi() function and actually 'mtn add' the test file.
Paul Aurich <darkrain42@pidgin.im>
parents: 28862
diff changeset
104
28864
5b3810bb7f1a How is it that there's no programmatic way to get the output size of the hash functions without resorting to a hardcoded table? Or did I miss something?
Paul Aurich <darkrain42@pidgin.im>
parents: 28863
diff changeset
105 memcpy(prev, tmp, hash_len);
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
106 }
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
107
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
108 purple_cipher_context_destroy(context);
28865
554be021cd4c Clean up the two temporary buffers.
Paul Aurich <darkrain42@pidgin.im>
parents: 28864
diff changeset
109 g_free(tmp);
554be021cd4c Clean up the two temporary buffers.
Paul Aurich <darkrain42@pidgin.im>
parents: 28864
diff changeset
110 g_free(prev);
28862
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
111 return result;
8a37b7df0850 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <darkrain42@pidgin.im>
parents:
diff changeset
112 }

mercurial