5 * Implementation of the SHA-1 hash function
7 * (c) 1999 Straylight/Edgeware
8 * (c) 2000 Mark Wooding
11 /*----- Licensing notice --------------------------------------------------*
13 * Copyright (c) 2000 Mark Wooding
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
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.
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
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.
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.
50 /*----- Header files ------------------------------------------------------*/
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @sha_compress@ --- *
61 * Arguments: @sha_ctx *ctx@ = pointer to context block
62 * @const void *sbuf@ = pointer to buffer of appropriate size
66 * Use: SHA-1 compression function.
69 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
74 /* --- Fetch the chaining variables --- */
82 /* --- Fetch and expand the buffer contents --- */
88 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
90 for (i
= 16; i
< 80; i
++) {
91 uint32 x
= buf
[i
- 3] ^ buf
[i
- 8] ^ buf
[i
- 14] ^ buf
[i
- 16];
96 /* --- Definitions for round functions --- */
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)))
102 #define T(v, w, x, y, z, i, f, k) do { \
104 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
106 _x = v; v = z; z = y; y = x; x = w; w = _x; \
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)
114 /* --- The main compression function --- */
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
);
135 /* --- @sha_init@ --- *
137 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
141 * Use: Initializes a context block ready for hashing.
144 void sha_init(sha_ctx
*ctx
)
152 ctx
->nl
= ctx
->nh
= 0;
155 /* --- @sha_hash@ --- *
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
163 * Use: Hashes a buffer of data. The buffer may be of any size and
167 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
169 sha_ctx
*_bctx
= (ctx
);
171 const octet
*_bbuf
= (octet
*) (buf
);
174 uint32 _l
= ((uint32
) ((_bsz
) & MASK32
));
175 uint32 _h
= ((_bsz
& ~MASK32
) >> 16) >> 16;
178 if (_bctx
->nl
< _l
|| _bctx
->nl
& ~MASK32
)
181 if (_bctx
->off
+ _bsz
< SHA_BUFSZ
) {
182 memcpy(_bctx
->buf
+ _bctx
->off
, _bbuf
, _bsz
);
186 size_t s
= SHA_BUFSZ
- _bctx
->off
;
187 memcpy(_bctx
->buf
+ _bctx
->off
, _bbuf
, s
);
188 sha_compress(_bctx
, _bctx
->buf
);
192 while (_bsz
>= SHA_BUFSZ
) {
193 sha_compress(_bctx
, _bbuf
);
198 memcpy(_bctx
->buf
, _bbuf
, _bsz
);
203 /* --- @sha_done@ --- *
205 * Arguments: @sha_ctx *ctx@ = pointer to context block
206 * @void *hash@ = pointer to output buffer
210 * Use: Returns the hash of the data read so far.
213 void sha_done(sha_ctx
*ctx
, void *hash
)
217 sha_ctx
*_pctx
= (ctx
);
218 _pctx
->buf
[_pctx
->off
] = 0x80;
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);
226 memset(_pctx
->buf
+ _pctx
->off
, 0, SHA_BUFSZ
- _pctx
->off
- 8);
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
);
238 /*----- Testing -----------------------------------------------------------*/
240 /* --- Quick test --- */
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 };
259 for (i
= 0; i
< 23; i
++) {
261 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\n",
266 if (memcmp(buf
, v
, SHA_HASHSZ
) != 0) {
267 fprintf(stderr
, "SHA validation error\n");
268 return (EXIT_FAILURE
);
276 /*----- That's all, folks -------------------------------------------------*/