Add an internal-representation no-op function.
[u/mdw/catacomb] / sha256.c
CommitLineData
eee16120 1/* -*-c-*-
2 *
3 * $Id: sha256.c,v 1.1 2000/10/15 17:48:14 mdw Exp $
4 *
5 * Implementation of the SHA-256 hash function
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: sha256.c,v $
33 * Revision 1.1 2000/10/15 17:48:14 mdw
34 * New SHA variants with longer outputs.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <mLib/bits.h>
41
42#include "ghash.h"
43#include "ghash-def.h"
44#include "hash.h"
45#include "sha256.h"
46
47/*----- Main code ---------------------------------------------------------*/
48
49/* --- @sha256_compress@ --- *
50 *
51 * Arguments: @sha256_ctx *ctx@ = pointer to context block
52 * @const void *sbuf@ = pointer to buffer of appropriate size
53 *
54 * Returns: ---
55 *
56 * Use: SHA-256 compression function.
57 */
58
59void sha256_compress(sha256_ctx *ctx, const void *sbuf)
60{
61 uint32 a, b, c, d, e, f, g, h;
62 uint32 buf[64];
63
64 /* --- Fetch the chaining variables --- */
65
66 a = ctx->a;
67 b = ctx->b;
68 c = ctx->c;
69 d = ctx->d;
70 e = ctx->e;
71 f = ctx->f;
72 g = ctx->g;
73 h = ctx->h;
74
75 /* --- Definitions for round functions --- */
76
77#define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
78#define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
79
80#define S0(x) (ROR32((x), 2) ^ ROR32((x), 13) ^ ROR32((x), 22))
81#define S1(x) (ROR32((x), 6) ^ ROR32((x), 11) ^ ROR32((x), 25))
82#define s0(x) (ROR32((x), 7) ^ ROR32((x), 18) ^ LSR32((x), 3))
83#define s1(x) (ROR32((x), 17) ^ ROR32((x), 19) ^ LSR32((x), 10))
84
85#define T(a, b, c, d, e, f, g, h, i, k) do { \
86 uint32 t1 = h + S1(e) + CH(e, f, g) + k + buf[i]; \
87 uint32 t2 = S0(a) + MAJ(a, b, c); \
88 d += t1; h = t1 + t2; \
89} while (0)
90
91 /* --- Fetch and expand the buffer contents --- */
92
93 {
94 int i;
95 const octet *p;
96
97 for (i = 0, p = sbuf; i < 16; i++, p += 4)
98 buf[i] = LOAD32(p);
99 for (i = 16; i < 64; i++)
100 buf[i] = s1(buf[i - 2]) + buf[i - 7] + s0(buf[i - 15]) + buf[i - 16];
101 }
102
103 /* --- The main compression function --- */
104
105 T(a, b, c, d, e, f, g, h, 0, 0x428a2f98);
106 T(h, a, b, c, d, e, f, g, 1, 0x71374491);
107 T(g, h, a, b, c, d, e, f, 2, 0xb5c0fbcf);
108 T(f, g, h, a, b, c, d, e, 3, 0xe9b5dba5);
109 T(e, f, g, h, a, b, c, d, 4, 0x3956c25b);
110 T(d, e, f, g, h, a, b, c, 5, 0x59f111f1);
111 T(c, d, e, f, g, h, a, b, 6, 0x923f82a4);
112 T(b, c, d, e, f, g, h, a, 7, 0xab1c5ed5);
113 T(a, b, c, d, e, f, g, h, 8, 0xd807aa98);
114 T(h, a, b, c, d, e, f, g, 9, 0x12835b01);
115 T(g, h, a, b, c, d, e, f, 10, 0x243185be);
116 T(f, g, h, a, b, c, d, e, 11, 0x550c7dc3);
117 T(e, f, g, h, a, b, c, d, 12, 0x72be5d74);
118 T(d, e, f, g, h, a, b, c, 13, 0x80deb1fe);
119 T(c, d, e, f, g, h, a, b, 14, 0x9bdc06a7);
120 T(b, c, d, e, f, g, h, a, 15, 0xc19bf174);
121 T(a, b, c, d, e, f, g, h, 16, 0xe49b69c1);
122 T(h, a, b, c, d, e, f, g, 17, 0xefbe4786);
123 T(g, h, a, b, c, d, e, f, 18, 0x0fc19dc6);
124 T(f, g, h, a, b, c, d, e, 19, 0x240ca1cc);
125 T(e, f, g, h, a, b, c, d, 20, 0x2de92c6f);
126 T(d, e, f, g, h, a, b, c, 21, 0x4a7484aa);
127 T(c, d, e, f, g, h, a, b, 22, 0x5cb0a9dc);
128 T(b, c, d, e, f, g, h, a, 23, 0x76f988da);
129 T(a, b, c, d, e, f, g, h, 24, 0x983e5152);
130 T(h, a, b, c, d, e, f, g, 25, 0xa831c66d);
131 T(g, h, a, b, c, d, e, f, 26, 0xb00327c8);
132 T(f, g, h, a, b, c, d, e, 27, 0xbf597fc7);
133 T(e, f, g, h, a, b, c, d, 28, 0xc6e00bf3);
134 T(d, e, f, g, h, a, b, c, 29, 0xd5a79147);
135 T(c, d, e, f, g, h, a, b, 30, 0x06ca6351);
136 T(b, c, d, e, f, g, h, a, 31, 0x14292967);
137 T(a, b, c, d, e, f, g, h, 32, 0x27b70a85);
138 T(h, a, b, c, d, e, f, g, 33, 0x2e1b2138);
139 T(g, h, a, b, c, d, e, f, 34, 0x4d2c6dfc);
140 T(f, g, h, a, b, c, d, e, 35, 0x53380d13);
141 T(e, f, g, h, a, b, c, d, 36, 0x650a7354);
142 T(d, e, f, g, h, a, b, c, 37, 0x766a0abb);
143 T(c, d, e, f, g, h, a, b, 38, 0x81c2c92e);
144 T(b, c, d, e, f, g, h, a, 39, 0x92722c85);
145 T(a, b, c, d, e, f, g, h, 40, 0xa2bfe8a1);
146 T(h, a, b, c, d, e, f, g, 41, 0xa81a664b);
147 T(g, h, a, b, c, d, e, f, 42, 0xc24b8b70);
148 T(f, g, h, a, b, c, d, e, 43, 0xc76c51a3);
149 T(e, f, g, h, a, b, c, d, 44, 0xd192e819);
150 T(d, e, f, g, h, a, b, c, 45, 0xd6990624);
151 T(c, d, e, f, g, h, a, b, 46, 0xf40e3585);
152 T(b, c, d, e, f, g, h, a, 47, 0x106aa070);
153 T(a, b, c, d, e, f, g, h, 48, 0x19a4c116);
154 T(h, a, b, c, d, e, f, g, 49, 0x1e376c08);
155 T(g, h, a, b, c, d, e, f, 50, 0x2748774c);
156 T(f, g, h, a, b, c, d, e, 51, 0x34b0bcb5);
157 T(e, f, g, h, a, b, c, d, 52, 0x391c0cb3);
158 T(d, e, f, g, h, a, b, c, 53, 0x4ed8aa4a);
159 T(c, d, e, f, g, h, a, b, 54, 0x5b9cca4f);
160 T(b, c, d, e, f, g, h, a, 55, 0x682e6ff3);
161 T(a, b, c, d, e, f, g, h, 56, 0x748f82ee);
162 T(h, a, b, c, d, e, f, g, 57, 0x78a5636f);
163 T(g, h, a, b, c, d, e, f, 58, 0x84c87814);
164 T(f, g, h, a, b, c, d, e, 59, 0x8cc70208);
165 T(e, f, g, h, a, b, c, d, 60, 0x90befffa);
166 T(d, e, f, g, h, a, b, c, 61, 0xa4506ceb);
167 T(c, d, e, f, g, h, a, b, 62, 0xbef9a3f7);
168 T(b, c, d, e, f, g, h, a, 63, 0xc67178f2);
169
170 /* --- Update the chaining variables --- */
171
172 ctx->a += a;
173 ctx->b += b;
174 ctx->c += c;
175 ctx->d += d;
176 ctx->e += e;
177 ctx->f += f;
178 ctx->g += g;
179 ctx->h += h;
180}
181
182/* --- @sha256_init@ --- *
183 *
184 * Arguments: @sha256_ctx *ctx@ = pointer to context block to initialize
185 *
186 * Returns: ---
187 *
188 * Use: Initializes a context block ready for hashing.
189 */
190
191void sha256_init(sha256_ctx *ctx)
192{
193 ctx->a = 0x6a09e667;
194 ctx->b = 0xbb67ae85;
195 ctx->c = 0x3c6ef372;
196 ctx->d = 0xa54ff53a;
197 ctx->e = 0x510e527f;
198 ctx->f = 0x9b05688c;
199 ctx->g = 0x1f83d9ab;
200 ctx->h = 0x5be0cd19;
201 ctx->off = 0;
202 ctx->nl = ctx->nh = 0;
203}
204
205/* --- @sha256_set@ --- *
206 *
207 * Arguments: @sha256_ctx *ctx@ = pointer to context block
208 * @const void *buf@ = pointer to state buffer
209 * @unsigned long count@ = current count of bytes processed
210 *
211 * Returns: ---
212 *
213 * Use: Initializes a context block from a given state. This is
214 * useful in cases where the initial hash state is meant to be
215 * secret, e.g., for NMAC and HMAC support.
216 */
217
218void sha256_set(sha256_ctx *ctx, const void *buf, unsigned long count)
219{
220 const octet *p = buf;
221 ctx->a = LOAD32(p + 0);
222 ctx->b = LOAD32(p + 4);
223 ctx->c = LOAD32(p + 8);
224 ctx->d = LOAD32(p + 12);
225 ctx->e = LOAD32(p + 16);
226 ctx->f = LOAD32(p + 20);
227 ctx->g = LOAD32(p + 24);
228 ctx->h = LOAD32(p + 28);
229 ctx->off = 0;
230 ctx->nl = U32(count);
231 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
232}
233
234/* --- @sha256_hash@ --- *
235 *
236 * Arguments: @sha256_ctx *ctx@ = pointer to context block
237 * @const void *buf@ = buffer of data to hash
238 * @size_t sz@ = size of buffer to hash
239 *
240 * Returns: ---
241 *
242 * Use: Hashes a buffer of data. The buffer may be of any size and
243 * alignment.
244 */
245
246void sha256_hash(sha256_ctx *ctx, const void *buf, size_t sz)
247{
248 HASH_BUFFER(SHA256, sha256, ctx, buf, sz);
249}
250
251/* --- @sha256_done@ --- *
252 *
253 * Arguments: @sha256_ctx *ctx@ = pointer to context block
254 * @void *hash@ = pointer to output buffer
255 *
256 * Returns: ---
257 *
258 * Use: Returns the hash of the data read so far.
259 */
260
261void sha256_done(sha256_ctx *ctx, void *hash)
262{
263 octet *p = hash;
264 HASH_PAD(SHA256, sha256, ctx, 0x80, 0, 8);
265 STORE32(ctx->buf + SHA256_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3));
266 STORE32(ctx->buf + SHA256_BUFSZ - 4, ctx->nl << 3);
267 sha256_compress(ctx, ctx->buf);
268 STORE32(p + 0, ctx->a);
269 STORE32(p + 4, ctx->b);
270 STORE32(p + 8, ctx->c);
271 STORE32(p + 12, ctx->d);
272 STORE32(p + 16, ctx->e);
273 STORE32(p + 20, ctx->f);
274 STORE32(p + 24, ctx->g);
275 STORE32(p + 28, ctx->h);
276}
277
278/* --- @sha256_state@ --- *
279 *
280 * Arguments: @sha256_ctx *ctx@ = pointer to context
281 * @void *state@ = pointer to buffer for current state
282 *
283 * Returns: Number of bytes written to the hash function so far.
284 *
285 * Use: Returns the current state of the hash function such that
286 * it can be passed to @sha256_set@.
287 */
288
289unsigned long sha256_state(sha256_ctx *ctx, void *state)
290{
291 octet *p = state;
292 STORE32(p + 0, ctx->a);
293 STORE32(p + 4, ctx->b);
294 STORE32(p + 8, ctx->c);
295 STORE32(p + 12, ctx->d);
296 STORE32(p + 16, ctx->e);
297 STORE32(p + 20, ctx->f);
298 STORE32(p + 24, ctx->g);
299 STORE32(p + 28, ctx->h);
300 return (ctx->nl | ((ctx->nh << 16) << 16));
301}
302
303/* --- Generic interface --- */
304
305GHASH_DEF(SHA256, sha256)
306
307/* --- Test code --- */
308
309HASH_TEST(SHA256, sha256)
310
311/*----- That's all, folks -------------------------------------------------*/