annotate libpurple/ciphers/sha256.c @ 31221: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
author Gary Kramlich <grim@reaperworld.com>
date Thu, 17 Feb 2011 00:42:53 +0000
parents 2d3c1197f930
children ca94413ccd0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31217
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2 * purple
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 *
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 * Purple is the legal property of its developers, whose names are too numerous
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * source distribution.
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 *
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * (at your option) any later version.
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 *
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * GNU General Public License for more details.
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 *
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 */
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
22 #include <cipher.h>
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
23
31221
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: 31219
diff changeset
24 #if !GLIB_CHECK_VERSION(2,16,0)
31217
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25
31221
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: 31219
diff changeset
26 #define SHA256_HMAC_BLOCK_SIZE 64
31217
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27 #define SHA256_ROTR(X,n) ((((X) >> (n)) | ((X) << (32-(n)))) & 0xFFFFFFFF)
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29 static const guint32 sha256_K[64] =
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30 {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
35 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
37 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
38 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
39 };
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
40
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
41 struct SHA256Context {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
42 guint32 H[8];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
43 guint32 W[64];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
44
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
45 gint lenW;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
46
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
47 guint32 sizeHi;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
48 guint32 sizeLo;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
49 };
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
50
31221
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: 31219
diff changeset
51 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: 31219
diff changeset
52 sha256_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: 31219
diff changeset
53 {
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: 31219
diff changeset
54 /* 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: 31219
diff changeset
55 return SHA256_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: 31219
diff changeset
56 }
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: 31219
diff changeset
57
31217
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58 static void
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 sha256_hash_block(struct SHA256Context *sha256_ctx) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60 gint i;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 guint32 A, B, C, D, E, F, G, H, T1, T2;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 for(i = 16; i < 64; i++) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64 sha256_ctx->W[i] =
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65 (SHA256_ROTR(sha256_ctx->W[i-2], 17) ^ SHA256_ROTR(sha256_ctx->W[i-2], 19) ^ (sha256_ctx->W[i-2] >> 10))
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66 + sha256_ctx->W[i-7]
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 + (SHA256_ROTR(sha256_ctx->W[i-15], 7) ^ SHA256_ROTR(sha256_ctx->W[i-15], 18) ^ (sha256_ctx->W[i-15] >> 3))
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68 + sha256_ctx->W[i-16];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71 A = sha256_ctx->H[0];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72 B = sha256_ctx->H[1];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73 C = sha256_ctx->H[2];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74 D = sha256_ctx->H[3];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 E = sha256_ctx->H[4];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 F = sha256_ctx->H[5];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77 G = sha256_ctx->H[6];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 H = sha256_ctx->H[7];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 for(i = 0; i < 64; i++) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81 T1 = H
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 + (SHA256_ROTR(E, 6) ^ SHA256_ROTR(E, 11) ^ SHA256_ROTR(E, 25))
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 + ((E & F) ^ ((~E) & G))
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84 + sha256_K[i] + sha256_ctx->W[i];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 T2 = (SHA256_ROTR(A, 2) ^ SHA256_ROTR(A, 13) ^ SHA256_ROTR(A, 22))
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86 + ((A & B) ^ (A & C) ^ (B & C));
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 H = G;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88 G = F;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 F = E;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90 E = D + T1;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91 D = C;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 C = B;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 B = A;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94 A = T1 + T2;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
95 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
96
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
97 sha256_ctx->H[0] += A;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
98 sha256_ctx->H[1] += B;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
99 sha256_ctx->H[2] += C;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
100 sha256_ctx->H[3] += D;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 sha256_ctx->H[4] += E;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
102 sha256_ctx->H[5] += F;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 sha256_ctx->H[6] += G;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
104 sha256_ctx->H[7] += H;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
106
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
107 static void
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 sha256_set_opt(PurpleCipherContext *context, const gchar *name, void *value) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109 struct SHA256Context *ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
110
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
111 ctx = purple_cipher_context_get_data(context);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
113 if(!strcmp(name, "sizeHi")) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114 ctx->sizeHi = GPOINTER_TO_INT(value);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 } else if(!strcmp(name, "sizeLo")) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116 ctx->sizeLo = GPOINTER_TO_INT(value);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 } else if(!strcmp(name, "lenW")) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
118 ctx->lenW = GPOINTER_TO_INT(value);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
119 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
120 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
121
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
122 static void *
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
123 sha256_get_opt(PurpleCipherContext *context, const gchar *name) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
124 struct SHA256Context *ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
125
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
126 ctx = purple_cipher_context_get_data(context);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
127
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
128 if(!strcmp(name, "sizeHi")) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
129 return GINT_TO_POINTER(ctx->sizeHi);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
130 } else if(!strcmp(name, "sizeLo")) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
131 return GINT_TO_POINTER(ctx->sizeLo);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
132 } else if(!strcmp(name, "lenW")) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
133 return GINT_TO_POINTER(ctx->lenW);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
134 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
135
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
136 return NULL;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
137 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
138
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
139 static void
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
140 sha256_init(PurpleCipherContext *context, void *extra) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
141 struct SHA256Context *sha256_ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
142
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
143 sha256_ctx = g_new0(struct SHA256Context, 1);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
144
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
145 purple_cipher_context_set_data(context, sha256_ctx);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
147 purple_cipher_context_reset(context, extra);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
149
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150 static void
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
151 sha256_reset(PurpleCipherContext *context, void *extra) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
152 struct SHA256Context *sha256_ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
153 gint i;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
154
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
155 sha256_ctx = purple_cipher_context_get_data(context);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
157 g_return_if_fail(sha256_ctx);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
158
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
159 sha256_ctx->lenW = 0;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 sha256_ctx->sizeHi = 0;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161 sha256_ctx->sizeLo = 0;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163 sha256_ctx->H[0] = 0x6a09e667;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
164 sha256_ctx->H[1] = 0xbb67ae85;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165 sha256_ctx->H[2] = 0x3c6ef372;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
166 sha256_ctx->H[3] = 0xa54ff53a;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
167 sha256_ctx->H[4] = 0x510e527f;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168 sha256_ctx->H[5] = 0x9b05688c;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169 sha256_ctx->H[6] = 0x1f83d9ab;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
170 sha256_ctx->H[7] = 0x5be0cd19;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
171
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
172 for(i = 0; i < 64; i++)
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
173 sha256_ctx->W[i] = 0;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
174 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
175
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
176 static void
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
177 sha256_uninit(PurpleCipherContext *context) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
178 struct SHA256Context *sha256_ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
179
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
180 purple_cipher_context_reset(context, NULL);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
181
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
182 sha256_ctx = purple_cipher_context_get_data(context);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
183
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
184 memset(sha256_ctx, 0, sizeof(struct SHA256Context));
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
185
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
186 g_free(sha256_ctx);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
187 sha256_ctx = NULL;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
188 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
189
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
190 static void
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
191 sha256_append(PurpleCipherContext *context, const guchar *data, size_t len) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
192 struct SHA256Context *sha256_ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
193 gint i;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
194
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
195 sha256_ctx = purple_cipher_context_get_data(context);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
196
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
197 g_return_if_fail(sha256_ctx);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
198
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
199 for(i = 0; i < len; i++) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
200 sha256_ctx->W[sha256_ctx->lenW / 4] <<= 8;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
201 sha256_ctx->W[sha256_ctx->lenW / 4] |= data[i];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
202
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
203 if((++sha256_ctx->lenW) % 64 == 0) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
204 sha256_hash_block(sha256_ctx);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
205 sha256_ctx->lenW = 0;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
206 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
207
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
208 sha256_ctx->sizeLo += 8;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
209 sha256_ctx->sizeHi += (sha256_ctx->sizeLo < 8);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
210 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
211 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
212
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
213 static gboolean
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
214 sha256_digest(PurpleCipherContext *context, size_t in_len, guchar digest[32],
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
215 size_t *out_len)
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
216 {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
217 struct SHA256Context *sha256_ctx;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
218 guchar pad0x80 = 0x80, pad0x00 = 0x00;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
219 guchar padlen[8];
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
220 gint i;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
221
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
222 g_return_val_if_fail(in_len >= 32, FALSE);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
223
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
224 sha256_ctx = purple_cipher_context_get_data(context);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
225
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
226 g_return_val_if_fail(sha256_ctx, FALSE);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
227
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
228 padlen[0] = (guchar)((sha256_ctx->sizeHi >> 24) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
229 padlen[1] = (guchar)((sha256_ctx->sizeHi >> 16) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
230 padlen[2] = (guchar)((sha256_ctx->sizeHi >> 8) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
231 padlen[3] = (guchar)((sha256_ctx->sizeHi >> 0) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
232 padlen[4] = (guchar)((sha256_ctx->sizeLo >> 24) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
233 padlen[5] = (guchar)((sha256_ctx->sizeLo >> 16) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
234 padlen[6] = (guchar)((sha256_ctx->sizeLo >> 8) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
235 padlen[7] = (guchar)((sha256_ctx->sizeLo >> 0) & 255);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
236
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
237 /* pad with a 1, then zeroes, then length */
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
238 purple_cipher_context_append(context, &pad0x80, 1);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
239 while(sha256_ctx->lenW != 56)
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
240 purple_cipher_context_append(context, &pad0x00, 1);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
241 purple_cipher_context_append(context, padlen, 8);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
242
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
243 for(i = 0; i < 32; i++) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
244 digest[i] = (guchar)(sha256_ctx->H[i / 4] >> 24);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
245 sha256_ctx->H[i / 4] <<= 8;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
246 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
247
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
248 purple_cipher_context_reset(context, NULL);
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
249
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
250 if(out_len)
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
251 *out_len = 32;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
252
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
253 return TRUE;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
254 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
255
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
256 static PurpleCipherOps SHA256Ops = {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
257 .set_option = sha256_set_opt,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
258 .get_option = sha256_get_opt,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
259 .init = sha256_init,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
260 .reset = sha256_reset,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
261 .uninit = sha256_uninit,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
262 .append = sha256_append,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
263 .digest = sha256_digest,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
264 .get_block_size = sha256_get_block_size,
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
265 };
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
266
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
267 PurpleCipherOps *
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
268 purple_sha256_cipher_get_ops(void) {
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
269 return &SHA256Ops;
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
270 }
3af51303d45c Moved sha256 to the sub library
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
271
31221
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: 31219
diff changeset
272 #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: 31219
diff changeset
273