chiark / gitweb /
Fix licence text.
[storin] / sha.c
1 /* -*-c-*-
2  *
3  * $Id: sha.c,v 1.2 2000/07/02 15:21:20 mdw Exp $
4  *
5  * Implementation of the SHA-1 hash function
6  *
7  * (c) 1999 Straylight/Edgeware
8  * (c) 2000 Mark Wooding
9  */
10
11 /*----- Licensing notice --------------------------------------------------* 
12  *
13  * Copyright (c) 2000 Mark Wooding
14  * All rights reserved.
15  * 
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are
18  * met:
19  * 
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 
23  * 2, Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 
27  * 3. The name of the authors may not be used to endorse or promote
28  *    products derived from this software without specific prior written
29  *    permission.
30  * 
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
34  * NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  * 
43  * Instead of accepting the above terms, you may redistribute and/or modify
44  * this software under the terms of either the GNU General Public License,
45  * or the GNU Library General Public License, published by the Free
46  * Software Foundation; either version 2 of the License, or (at your
47  * option) any later version.
48  */
49
50 /*----- Header files ------------------------------------------------------*/
51
52 #include <string.h>
53
54 #include "bits.h"
55 #include "sha.h"
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 /* --- @sha_compress@ --- *
60  *
61  * Arguments:   @sha_ctx *ctx@ = pointer to context block
62  *              @const void *sbuf@ = pointer to buffer of appropriate size
63  *
64  * Returns:     ---
65  *
66  * Use:         SHA-1 compression function.
67  */
68
69 void sha_compress(sha_ctx *ctx, const void *sbuf)
70 {
71   uint32 a, b, c, d, e;
72   uint32 buf[80];
73
74   /* --- Fetch the chaining variables --- */
75
76   a = ctx->a;
77   b = ctx->b;
78   c = ctx->c;
79   d = ctx->d;
80   e = ctx->e;
81
82   /* --- Fetch and expand the buffer contents --- */
83
84   {
85     int i;
86     const octet *p;
87
88     for (i = 0, p = sbuf; i < 16; i++, p += 4)
89       buf[i] = LOAD32(p);
90     for (i = 16; i < 80; i++) {
91       uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16];
92       buf[i] = ROL32(x, 1);
93     }
94   }
95
96   /* --- Definitions for round functions --- */
97
98 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
99 #define G(x, y, z) ((x) ^ (y) ^ (z))
100 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
101
102 #define T(v, w, x, y, z, i, f, k) do {                                  \
103   uint32 _x;                                                            \
104   z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k;                        \
105   w = ROR32(w, 2);                                                      \
106   _x = v; v = z; z = y; y = x; x = w; w = _x;                           \
107 } while (0)
108
109 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
110 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
111 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
112 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
113
114   /* --- The main compression function --- */
115
116   {
117     unsigned i;
118     for (i = 0; i < 20; i++)
119       FF(a, b, c, d, e, i);
120     for (i = 20; i < 40; i++)
121       GG(a, b, c, d, e, i);
122     for (i = 40; i < 60; i++)
123       HH(a, b, c, d, e, i);
124     for (i = 60; i < 80; i++)
125       II(a, b, c, d, e, i);
126   }
127
128   ctx->a += a;
129   ctx->b += b;
130   ctx->c += c;
131   ctx->d += d;
132   ctx->e += e;
133 }
134
135 /* --- @sha_init@ --- *
136  *
137  * Arguments:   @sha_ctx *ctx@ = pointer to context block to initialize
138  *
139  * Returns:     ---
140  *
141  * Use:         Initializes a context block ready for hashing.
142  */
143
144 void sha_init(sha_ctx *ctx)
145 {
146   ctx->a = 0x67452301;
147   ctx->b = 0xefcdab89;
148   ctx->c = 0x98badcfe;
149   ctx->d = 0x10325476;
150   ctx->e = 0xc3d2e1f0;
151   ctx->off = 0;
152   ctx->nl = ctx->nh = 0;
153 }
154
155 /* --- @sha_hash@ --- *
156  *
157  * Arguments:   @sha_ctx *ctx@ = pointer to context block
158  *              @const void *buf@ = buffer of data to hash
159  *              @size_t sz@ = size of buffer to hash
160  *
161  * Returns:     ---
162  *
163  * Use:         Hashes a buffer of data.  The buffer may be of any size and
164  *              alignment.
165  */
166
167 void sha_hash(sha_ctx *ctx, const void *buf, size_t sz)
168 {
169   sha_ctx *_bctx = (ctx);
170   size_t _bsz = (sz);
171   const octet *_bbuf = (octet *) (buf);
172
173   {
174     uint32 _l = ((uint32) ((_bsz) & MASK32));
175     uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16;
176     _bctx->nh += _h;
177     _bctx->nl += _l;
178     if (_bctx->nl < _l || _bctx->nl & ~MASK32)
179       _bctx->nh++;
180   }
181   if (_bctx->off + _bsz < SHA_BUFSZ) {
182     memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz);
183     _bctx->off += _bsz;
184   } else {
185     if (_bctx->off) {
186       size_t s = SHA_BUFSZ - _bctx->off;
187       memcpy(_bctx->buf + _bctx->off, _bbuf, s);
188       sha_compress(_bctx, _bctx->buf);
189       _bsz -= s;
190       _bbuf += s;
191     }
192     while (_bsz >= SHA_BUFSZ) {
193       sha_compress(_bctx, _bbuf);
194       _bsz -= SHA_BUFSZ;
195       _bbuf += SHA_BUFSZ;
196     }
197     if (_bsz)
198       memcpy(_bctx->buf, _bbuf, _bsz);
199     _bctx->off = _bsz;
200   }
201 }
202
203 /* --- @sha_done@ --- *
204  *
205  * Arguments:   @sha_ctx *ctx@ = pointer to context block
206  *              @void *hash@ = pointer to output buffer
207  *
208  * Returns:     ---
209  *
210  * Use:         Returns the hash of the data read so far.
211  */
212
213 void sha_done(sha_ctx *ctx, void *hash)
214 {
215   octet *p = hash;
216   {
217     sha_ctx *_pctx = (ctx);
218     _pctx->buf[_pctx->off] = 0x80;
219     _pctx->off++;
220     if (_pctx->off > SHA_BUFSZ - 8) {
221       if (_pctx->off < SHA_BUFSZ)
222         memset(_pctx->buf + _pctx->off, 0, SHA_BUFSZ - _pctx->off);
223       sha_compress(_pctx, _pctx->buf);
224       memset(_pctx->buf, 0, SHA_BUFSZ - 8);
225     } else
226       memset(_pctx->buf + _pctx->off, 0, SHA_BUFSZ - _pctx->off - 8);
227   }
228   STORE32(ctx->buf + SHA_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3));
229   STORE32(ctx->buf + SHA_BUFSZ - 4, ctx->nl << 3);
230   sha_compress(ctx, ctx->buf);
231   STORE32(p +  0, ctx->a);
232   STORE32(p +  4, ctx->b);
233   STORE32(p +  8, ctx->c);
234   STORE32(p + 12, ctx->d);
235   STORE32(p + 16, ctx->e);
236 }
237
238 /*----- Testing -----------------------------------------------------------*/
239
240 /* --- Quick test --- */
241   
242 #ifdef QUICK_TEST
243
244 #include <stdio.h>
245 #include <stdlib.h>
246
247 int main(void)
248 {
249   octet buf[SHA_HASHSZ] = { 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89,
250                             0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76,
251                             0xc3, 0xd2, 0xe1, 0xf0 };
252   octet v[SHA_HASHSZ] = { 0xf7, 0x4d, 0x36, 0xbf, 0x17, 0xee, 0x23, 0xc4,
253                           0x6e, 0xc1, 0x66, 0xa4, 0x8a, 0x24, 0xda, 0x6a,
254                           0xb9, 0x99, 0xea, 0xea };
255   sha_ctx s;
256   int i;
257
258   sha_set(&s, buf, 0);
259   for (i = 0; i < 23; i++) {
260     sha_hash(&s,
261              "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\n",
262              63);
263   }
264   sha_done(&s, buf);
265
266   if (memcmp(buf, v, SHA_HASHSZ) != 0) {
267     fprintf(stderr, "SHA validation error\n");
268     return (EXIT_FAILURE);
269   }
270
271   return (0);
272 }
273
274 #endif    
275
276 /*----- That's all, folks -------------------------------------------------*/