annotate libpurple/ciphers/sha1.c @ 32205:958621e23f5d

Fix the handling of IRC formatting following mIRC ^O. Fixes #14436
author Ethan Blanton <elb@pidgin.im>
date Tue, 19 Jul 2011 16:56:23 +0000
parents ca94413ccd0e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31664
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2 * purple
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 *
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 * Purple is the legal property of its developers, whose names are too numerous
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * source distribution.
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 *
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * (at your option) any later version.
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 *
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * GNU General Public License for more details.
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 *
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 */
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
22 #include <cipher.h>
31674
4418c97490ed rc4.c and sha1.c needs to include libpurple/util.h as well
Gary Kramlich <grim@reaperworld.com>
parents: 31670
diff changeset
23 #include <util.h>
31664
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24
31670
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
25 #if !GLIB_CHECK_VERSION(2,16,0)
31664
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
26
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27 #define SHA1_HMAC_BLOCK_SIZE 64
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28 #define SHA1_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xFFFFFFFF)
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30 struct SHA1Context {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31 guint32 H[5];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32 guint32 W[80];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34 gint lenW;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
35
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 guint32 sizeHi;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
37 guint32 sizeLo;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
38 };
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
39
31670
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
40 static size_t
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
41 sha1_get_block_size(PurpleCipherContext *context)
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
42 {
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
43 /* This does not change (in this case) */
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
44 return SHA1_HMAC_BLOCK_SIZE;
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
45 }
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
46
31664
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
47 static void
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
48 sha1_hash_block(struct SHA1Context *sha1_ctx) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
49 gint i;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
50 guint32 A, B, C, D, E, T;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
51
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
52 for(i = 16; i < 80; i++) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
53 sha1_ctx->W[i] = SHA1_ROTL(sha1_ctx->W[i - 3] ^
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
54 sha1_ctx->W[i - 8] ^
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
55 sha1_ctx->W[i - 14] ^
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
56 sha1_ctx->W[i - 16], 1);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
57 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 A = sha1_ctx->H[0];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60 B = sha1_ctx->H[1];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 C = sha1_ctx->H[2];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62 D = sha1_ctx->H[3];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 E = sha1_ctx->H[4];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65 for(i = 0; i < 20; i++) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66 T = (SHA1_ROTL(A, 5) + (((C ^ D) & B) ^ D) + E + sha1_ctx->W[i] + 0x5A827999) & 0xFFFFFFFF;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 E = D;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68 D = C;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69 C = SHA1_ROTL(B, 30);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70 B = A;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71 A = T;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74 for(i = 20; i < 40; i++) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 T = (SHA1_ROTL(A, 5) + (B ^ C ^ D) + E + sha1_ctx->W[i] + 0x6ED9EBA1) & 0xFFFFFFFF;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 E = D;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77 D = C;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 C = SHA1_ROTL(B, 30);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79 B = A;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 A = T;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 for(i = 40; i < 60; i++) {
31706
4ef9165ebe45 Fix compilation on glib < 2.16. Somehow a trailing semicolon got lost.
Mark Doliner <mark@kingant.net>
parents: 31674
diff changeset
84 T = (SHA1_ROTL(A, 5) + ((B & C) | (D & (B | C))) + E + sha1_ctx->W[i] + 0x8F1BBCDC) & 0xFFFFFFFF;
4ef9165ebe45 Fix compilation on glib < 2.16. Somehow a trailing semicolon got lost.
Mark Doliner <mark@kingant.net>
parents: 31674
diff changeset
85 E = D;
31664
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86 D = C;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 C = SHA1_ROTL(B, 30);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88 B = A;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 A = T;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 for(i = 60; i < 80; i++) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 T = (SHA1_ROTL(A, 5) + (B ^ C ^ D) + E + sha1_ctx->W[i] + 0xCA62C1D6) & 0xFFFFFFFF;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94 E = D;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
95 D = C;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
96 C = SHA1_ROTL(B, 30);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
97 B = A;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
98 A = T;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
99 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
100
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 sha1_ctx->H[0] += A;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
102 sha1_ctx->H[1] += B;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 sha1_ctx->H[2] += C;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
104 sha1_ctx->H[3] += D;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105 sha1_ctx->H[4] += E;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
106 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
107
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 static void
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109 sha1_set_opt(PurpleCipherContext *context, const gchar *name, void *value) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
110 struct SHA1Context *ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
111
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112 ctx = purple_cipher_context_get_data(context);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
113
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114 if(purple_strequal(name, "sizeHi")) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 ctx->sizeHi = GPOINTER_TO_INT(value);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116 } else if(purple_strequal(name, "sizeLo")) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 ctx->sizeLo = GPOINTER_TO_INT(value);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
118 } else if(purple_strequal(name, "lenW")) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
119 ctx->lenW = GPOINTER_TO_INT(value);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
120 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
121 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
122
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
123 static void *
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
124 sha1_get_opt(PurpleCipherContext *context, const gchar *name) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
125 struct SHA1Context *ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
126
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
127 ctx = purple_cipher_context_get_data(context);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
128
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
129 if(purple_strequal(name, "sizeHi")) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
130 return GINT_TO_POINTER(ctx->sizeHi);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
131 } else if(purple_strequal(name, "sizeLo")) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
132 return GINT_TO_POINTER(ctx->sizeLo);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
133 } else if(purple_strequal(name, "lenW")) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
134 return GINT_TO_POINTER(ctx->lenW);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
135 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
136
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
137 return NULL;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
138 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
139
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
140 static void
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
141 sha1_init(PurpleCipherContext *context, void *extra) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
142 struct SHA1Context *sha1_ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
143
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
144 sha1_ctx = g_new0(struct SHA1Context, 1);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
145
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146 purple_cipher_context_set_data(context, sha1_ctx);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
147
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 purple_cipher_context_reset(context, extra);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
149 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
151 static void
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
152 sha1_reset(PurpleCipherContext *context, void *extra) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
153 struct SHA1Context *sha1_ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
154 gint i;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
155
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156 sha1_ctx = purple_cipher_context_get_data(context);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
157
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
158 g_return_if_fail(sha1_ctx);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
159
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 sha1_ctx->lenW = 0;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161 sha1_ctx->sizeHi = 0;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162 sha1_ctx->sizeLo = 0;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
164 sha1_ctx->H[0] = 0x67452301;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165 sha1_ctx->H[1] = 0xEFCDAB89;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
166 sha1_ctx->H[2] = 0x98BADCFE;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
167 sha1_ctx->H[3] = 0x10325476;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168 sha1_ctx->H[4] = 0xC3D2E1F0;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
170 for(i = 0; i < 80; i++)
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
171 sha1_ctx->W[i] = 0;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
172 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
173
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
174 static void
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
175 sha1_uninit(PurpleCipherContext *context) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
176 struct SHA1Context *sha1_ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
177
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
178 purple_cipher_context_reset(context, NULL);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
179
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
180 sha1_ctx = purple_cipher_context_get_data(context);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
181
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
182 memset(sha1_ctx, 0, sizeof(struct SHA1Context));
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
183
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
184 g_free(sha1_ctx);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
185 sha1_ctx = NULL;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
186 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
187
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
188 static void
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
189 sha1_append(PurpleCipherContext *context, const guchar *data, size_t len) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
190 struct SHA1Context *sha1_ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
191 gint i;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
192
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
193 sha1_ctx = purple_cipher_context_get_data(context);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
194
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
195 g_return_if_fail(sha1_ctx);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
196
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
197 for(i = 0; i < len; i++) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
198 sha1_ctx->W[sha1_ctx->lenW / 4] <<= 8;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
199 sha1_ctx->W[sha1_ctx->lenW / 4] |= data[i];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
200
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
201 if((++sha1_ctx->lenW) % 64 == 0) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
202 sha1_hash_block(sha1_ctx);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
203 sha1_ctx->lenW = 0;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
204 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
205
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
206 sha1_ctx->sizeLo += 8;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
207 sha1_ctx->sizeHi += (sha1_ctx->sizeLo < 8);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
208 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
209 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
210
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
211 static gboolean
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
212 sha1_digest(PurpleCipherContext *context, size_t in_len, guchar digest[20],
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
213 size_t *out_len)
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
214 {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
215 struct SHA1Context *sha1_ctx;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
216 guchar pad0x80 = 0x80, pad0x00 = 0x00;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
217 guchar padlen[8];
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
218 gint i;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
219
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
220 g_return_val_if_fail(in_len >= 20, FALSE);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
221
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
222 sha1_ctx = purple_cipher_context_get_data(context);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
223
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
224 g_return_val_if_fail(sha1_ctx, FALSE);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
225
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
226 padlen[0] = (guchar)((sha1_ctx->sizeHi >> 24) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
227 padlen[1] = (guchar)((sha1_ctx->sizeHi >> 16) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
228 padlen[2] = (guchar)((sha1_ctx->sizeHi >> 8) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
229 padlen[3] = (guchar)((sha1_ctx->sizeHi >> 0) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
230 padlen[4] = (guchar)((sha1_ctx->sizeLo >> 24) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
231 padlen[5] = (guchar)((sha1_ctx->sizeLo >> 16) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
232 padlen[6] = (guchar)((sha1_ctx->sizeLo >> 8) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
233 padlen[7] = (guchar)((sha1_ctx->sizeLo >> 0) & 255);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
234
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
235 /* pad with a 1, then zeroes, then length */
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
236 purple_cipher_context_append(context, &pad0x80, 1);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
237 while(sha1_ctx->lenW != 56)
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
238 purple_cipher_context_append(context, &pad0x00, 1);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
239 purple_cipher_context_append(context, padlen, 8);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
240
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
241 for(i = 0; i < 20; i++) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
242 digest[i] = (guchar)(sha1_ctx->H[i / 4] >> 24);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
243 sha1_ctx->H[i / 4] <<= 8;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
244 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
245
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
246 purple_cipher_context_reset(context, NULL);
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
247
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
248 if(out_len)
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
249 *out_len = 20;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
250
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
251 return TRUE;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
252 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
253
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
254 static PurpleCipherOps SHA1Ops = {
31802
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
255 sha1_set_opt, /* Set Option */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
256 sha1_get_opt, /* Get Option */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
257 sha1_init, /* init */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
258 sha1_reset, /* reset */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
259 sha1_uninit, /* uninit */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
260 NULL, /* set iv */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
261 sha1_append, /* append */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
262 sha1_digest, /* digest */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
263 NULL, /* encrypt */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
264 NULL, /* decrypt */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
265 NULL, /* set salt */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
266 NULL, /* get salt size */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
267 NULL, /* set key */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
268 NULL, /* get key size */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
269 NULL, /* set batch mode */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
270 NULL, /* get batch mode */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
271 sha1_get_block_size, /* get block size */
ca94413ccd0e Named initializers and most other C99isms don't work in Visual C++ .NET 2005 in
Florian Quèze <florian@instantbird.org>
parents: 31706
diff changeset
272 NULL /* set key with len */
31664
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
273 };
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
274
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
275 PurpleCipherOps *
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
276 purple_sha1_cipher_get_ops(void) {
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
277 return &SHA1Ops;
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
278 }
521febcb717a Broke sha1 out
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
279
31670
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
280 #endif /* !GLIB_CHECK_VERSION(2,16,0) */
2b041e31b825 Removed the "new" api I added by moving it to ciphers/gchecksum.c. Moved the gchecksum implements into gchecksum.c as a preproc macro, removed them from their individual files
Gary Kramlich <grim@reaperworld.com>
parents: 31668
diff changeset
281