Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / libbb / sha1.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  Based on shasum from http://www.netsw.org/crypto/hash/
4  *  Majorly hacked up to use Dr Brian Gladman's sha1 code
5  *
6  *  Copyright (C) 2002 Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
7  *  Copyright (C) 2003 Glenn L. McGrath
8  *  Copyright (C) 2003 Erik Andersen
9  *  
10  *  LICENSE TERMS
11  *
12  *  The free distribution and use of this software in both source and binary
13  *  form is allowed (with or without changes) provided that:
14  *
15  *    1. distributions of this source code include the above copyright
16  *       notice, this list of conditions and the following disclaimer;
17  *
18  *    2. distributions in binary form include the above copyright
19  *       notice, this list of conditions and the following disclaimer
20  *       in the documentation and/or other associated materials;
21  *
22  *    3. the copyright holder's name is not used to endorse products
23  *       built using this software without specific written permission.
24  *
25  *  ALTERNATIVELY, provided that this notice is retained in full, this product
26  *  may be distributed under the terms of the GNU General Public License (GPL),
27  *  in which case the provisions of the GPL apply INSTEAD OF those given above.
28  *
29  *  DISCLAIMER
30  *
31  *  This software is provided 'as is' with no explicit or implied warranties
32  *  in respect of its properties, including, but not limited to, correctness
33  *  and/or fitness for purpose.
34  *  ---------------------------------------------------------------------------
35  *  Issue Date: 10/11/2002
36  *
37  *  This is a byte oriented version of SHA1 that operates on arrays of bytes
38  *  stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
39  */
40
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdint.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "libbb.h"
50
51 # define SHA1_BLOCK_SIZE  64
52 # define SHA1_DIGEST_SIZE 20
53 # define SHA1_HASH_SIZE   SHA1_DIGEST_SIZE
54 # define SHA2_GOOD        0
55 # define SHA2_BAD         1
56
57 # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
58
59 # define SHA1_MASK   (SHA1_BLOCK_SIZE - 1)
60
61 /* reverse byte order in 32-bit words   */
62 #define ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))
63 #define parity(x,y,z)   ((x) ^ (y) ^ (z))
64 #define maj(x,y,z)      (((x) & (y)) | ((z) & ((x) | (y))))
65
66 /* A normal version as set out in the FIPS. This version uses   */
67 /* partial loop unrolling and is optimised for the Pentium 4    */
68 # define rnd(f,k)    \
69     t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
70     e = d; d = c; c = rotl32(b, 30); b = t
71
72
73 static void sha1_compile(sha1_ctx_t *ctx)
74 {
75         uint32_t w[80], i, a, b, c, d, e, t;
76
77         /* note that words are compiled from the buffer into 32-bit */
78         /* words in big-endian order so an order reversal is needed */
79         /* here on little endian machines                           */
80         for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
81                 w[i] = htonl(ctx->wbuf[i]);
82
83         for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
84                 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
85
86         a = ctx->hash[0];
87         b = ctx->hash[1];
88         c = ctx->hash[2];
89         d = ctx->hash[3];
90         e = ctx->hash[4];
91
92         for (i = 0; i < 20; ++i) {
93                 rnd(ch, 0x5a827999);
94         }
95
96         for (i = 20; i < 40; ++i) {
97                 rnd(parity, 0x6ed9eba1);
98         }
99
100         for (i = 40; i < 60; ++i) {
101                 rnd(maj, 0x8f1bbcdc);
102         }
103
104         for (i = 60; i < 80; ++i) {
105                 rnd(parity, 0xca62c1d6);
106         }
107
108         ctx->hash[0] += a;
109         ctx->hash[1] += b;
110         ctx->hash[2] += c;
111         ctx->hash[3] += d;
112         ctx->hash[4] += e;
113 }
114
115 void sha1_begin(sha1_ctx_t *ctx)
116 {
117         ctx->count[0] = ctx->count[1] = 0;
118         ctx->hash[0] = 0x67452301;
119         ctx->hash[1] = 0xefcdab89;
120         ctx->hash[2] = 0x98badcfe;
121         ctx->hash[3] = 0x10325476;
122         ctx->hash[4] = 0xc3d2e1f0;
123 }
124
125 /* SHA1 hash data in an array of bytes into hash buffer and call the        */
126 /* hash_compile function as required.                                       */
127 void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx)
128 {
129         uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
130         uint32_t freeb = SHA1_BLOCK_SIZE - pos;
131         const unsigned char *sp = data;
132
133         if ((ctx->count[0] += length) < length)
134                 ++(ctx->count[1]);
135
136         while (length >= freeb) {       /* tranfer whole blocks while possible  */
137                 memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
138                 sp += freeb;
139                 length -= freeb;
140                 freeb = SHA1_BLOCK_SIZE;
141                 pos = 0;
142                 sha1_compile(ctx);
143         }
144
145         memcpy(((unsigned char *) ctx->wbuf) + pos, sp, length);
146 }
147
148 void *sha1_end(void *resbuf, sha1_ctx_t *ctx)
149 {
150         /* SHA1 Final padding and digest calculation  */
151 #if BB_BIG_ENDIAN
152         static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
153         static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
154 #else
155         static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
156         static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
157 #endif
158
159         uint8_t *hval = resbuf;
160         uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
161
162         /* mask out the rest of any partial 32-bit word and then set    */
163         /* the next byte to 0x80. On big-endian machines any bytes in   */
164         /* the buffer will be at the top end of 32 bit words, on little */
165         /* endian machines they will be at the bottom. Hence the AND    */
166         /* and OR masks above are reversed for little endian systems    */
167         ctx->wbuf[cnt >> 2] =
168                 (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
169
170         /* we need 9 or more empty positions, one for the padding byte  */
171         /* (above) and eight for the length count.  If there is not     */
172         /* enough space pad and empty the buffer                        */
173         if (cnt > SHA1_BLOCK_SIZE - 9) {
174                 if (cnt < 60)
175                         ctx->wbuf[15] = 0;
176                 sha1_compile(ctx);
177                 cnt = 0;
178         } else                          /* compute a word index for the empty buffer positions  */
179                 cnt = (cnt >> 2) + 1;
180
181         while (cnt < 14)        /* and zero pad all but last two positions      */
182                 ctx->wbuf[cnt++] = 0;
183
184         /* assemble the eight byte counter in the buffer in big-endian  */
185         /* format                                                      */
186
187         ctx->wbuf[14] = htonl((ctx->count[1] << 3) | (ctx->count[0] >> 29));
188         ctx->wbuf[15] = htonl(ctx->count[0] << 3);
189
190         sha1_compile(ctx);
191
192         /* extract the hash value as bytes in case the hash buffer is   */
193         /* misaligned for 32-bit words                                  */
194
195         for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
196                 hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));
197         
198         return resbuf;
199 }
200
201