annotate libpurple/protocols/jabber/auth_scram.c @ 29085:338eeaf371e2

jabber: Add the Hi() function (PBKDF2). The single test is taken from draft-josefsson-pbkdf2-test-vectors.
author Paul Aurich <paul@darkrain42.org>
date Sun, 08 Nov 2009 03:36:14 +0000
parents
children 398ff52e7d62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29085
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
1 /*
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
2 * purple - Jabber Protocol Plugin
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
3 *
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
4 * Purple is the legal property of its developers, whose names are too numerous
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
6 * source distribution.
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
7 *
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
11 * (at your option) any later version.
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
12 *
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
16 * GNU General Public License for more details.
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
17 *
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
21 *
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
22 */
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
23 #include "internal.h"
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
24
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
25 #include "auth.h"
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
26 #include "auth_scram.h"
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
27
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
28 #include "cipher.h"
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
29 #include "debug.h"
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
30
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
31
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
32 GString *jabber_auth_scram_hi(const gchar *hash, const GString *str,
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
33 GString *salt, guint iterations)
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
34 {
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
35 PurpleCipherContext *context;
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
36 GString *result;
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
37 guint i;
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
38
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
39 g_return_val_if_fail(hash != NULL, NULL);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
40 g_return_val_if_fail(str != NULL && str->len > 0, NULL);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
41 g_return_val_if_fail(salt != NULL && salt->len > 0, NULL);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
42 g_return_val_if_fail(iterations > 0, NULL);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
43
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
44 context = purple_cipher_context_new_by_name("hmac", NULL);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
45
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
46 /* Append INT(1), a four-octet encoding of the integer 1, most significant
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
47 * octet first. */
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
48 g_string_append_len(salt, "\0\0\0\1", 4);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
49
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
50 result = g_string_sized_new(20); /* FIXME: Hardcoded 20 */
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
51
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
52 /* Compute U0 */
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
53 purple_cipher_context_set_option(context, "hash", (gpointer)hash);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
54 purple_cipher_context_set_key_with_len(context, (guchar *)str->str, str->len);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
55 purple_cipher_context_append(context, (guchar *)salt->str, salt->len);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
56 purple_cipher_context_digest(context, result->allocated_len, (guchar *)result->str, &(result->len));
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
57
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
58 /* Compute U1...Ui */
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
59 for (i = 1; i < iterations; ++i) {
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
60 guchar tmp[20]; /* FIXME: hardcoded 20 */
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
61 guint j;
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
62 purple_cipher_context_set_option(context, "hash", (gpointer)hash);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
63 purple_cipher_context_set_key_with_len(context, (guchar *)str->str, str->len);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
64 purple_cipher_context_append(context, (guchar *)result->str, result->len);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
65 purple_cipher_context_digest(context, sizeof(tmp), tmp, NULL);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
66
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
67 for (j = 0; j < 20; ++j)
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
68 result->str[j] ^= tmp[j];
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
69 }
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
70
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
71 purple_cipher_context_destroy(context);
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
72 return result;
338eeaf371e2 jabber: Add the Hi() function (PBKDF2).
Paul Aurich <paul@darkrain42.org>
parents:
diff changeset
73 }