Replace current verbose GPL stuff in libbb/*.c with one-line GPL boilerplate.
[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  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11  *
12  *  ---------------------------------------------------------------------------
13  *  Issue Date: 10/11/2002
14  *
15  *  This is a byte oriented version of SHA1 that operates on arrays of bytes
16  *  stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
17  */
18
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "libbb.h"
28
29 # define SHA1_BLOCK_SIZE  64
30 # define SHA1_DIGEST_SIZE 20
31 # define SHA1_HASH_SIZE   SHA1_DIGEST_SIZE
32 # define SHA2_GOOD        0
33 # define SHA2_BAD         1
34
35 # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
36
37 # define SHA1_MASK   (SHA1_BLOCK_SIZE - 1)
38
39 /* reverse byte order in 32-bit words   */
40 #define ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))
41 #define parity(x,y,z)   ((x) ^ (y) ^ (z))
42 #define maj(x,y,z)      (((x) & (y)) | ((z) & ((x) | (y))))
43
44 /* A normal version as set out in the FIPS. This version uses   */
45 /* partial loop unrolling and is optimised for the Pentium 4    */
46 # define rnd(f,k)    \
47     t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
48     e = d; d = c; c = rotl32(b, 30); b = t
49
50
51 static void sha1_compile(sha1_ctx_t *ctx)
52 {
53         uint32_t w[80], i, a, b, c, d, e, t;
54
55         /* note that words are compiled from the buffer into 32-bit */
56         /* words in big-endian order so an order reversal is needed */
57         /* here on little endian machines                           */
58         for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
59                 w[i] = htonl(ctx->wbuf[i]);
60
61         for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
62                 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
63
64         a = ctx->hash[0];
65         b = ctx->hash[1];
66         c = ctx->hash[2];
67         d = ctx->hash[3];
68         e = ctx->hash[4];
69
70         for (i = 0; i < 20; ++i) {
71                 rnd(ch, 0x5a827999);
72         }
73
74         for (i = 20; i < 40; ++i) {
75                 rnd(parity, 0x6ed9eba1);
76         }
77
78         for (i = 40; i < 60; ++i) {
79                 rnd(maj, 0x8f1bbcdc);
80         }
81
82         for (i = 60; i < 80; ++i) {
83                 rnd(parity, 0xca62c1d6);
84         }
85
86         ctx->hash[0] += a;
87         ctx->hash[1] += b;
88         ctx->hash[2] += c;
89         ctx->hash[3] += d;
90         ctx->hash[4] += e;
91 }
92
93 void sha1_begin(sha1_ctx_t *ctx)
94 {
95         ctx->count[0] = ctx->count[1] = 0;
96         ctx->hash[0] = 0x67452301;
97         ctx->hash[1] = 0xefcdab89;
98         ctx->hash[2] = 0x98badcfe;
99         ctx->hash[3] = 0x10325476;
100         ctx->hash[4] = 0xc3d2e1f0;
101 }
102
103 /* SHA1 hash data in an array of bytes into hash buffer and call the        */
104 /* hash_compile function as required.                                       */
105 void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx)
106 {
107         uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
108         uint32_t freeb = SHA1_BLOCK_SIZE - pos;
109         const unsigned char *sp = data;
110
111         if ((ctx->count[0] += length) < length)
112                 ++(ctx->count[1]);
113
114         while (length >= freeb) {       /* tranfer whole blocks while possible  */
115                 memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
116                 sp += freeb;
117                 length -= freeb;
118                 freeb = SHA1_BLOCK_SIZE;
119                 pos = 0;
120                 sha1_compile(ctx);
121         }
122
123         memcpy(((unsigned char *) ctx->wbuf) + pos, sp, length);
124 }
125
126 void *sha1_end(void *resbuf, sha1_ctx_t *ctx)
127 {
128         /* SHA1 Final padding and digest calculation  */
129 #if BB_BIG_ENDIAN
130         static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
131         static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
132 #else
133         static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
134         static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
135 #endif
136
137         uint8_t *hval = resbuf;
138         uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
139
140         /* mask out the rest of any partial 32-bit word and then set    */
141         /* the next byte to 0x80. On big-endian machines any bytes in   */
142         /* the buffer will be at the top end of 32 bit words, on little */
143         /* endian machines they will be at the bottom. Hence the AND    */
144         /* and OR masks above are reversed for little endian systems    */
145         ctx->wbuf[cnt >> 2] =
146                 (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
147
148         /* we need 9 or more empty positions, one for the padding byte  */
149         /* (above) and eight for the length count.  If there is not     */
150         /* enough space pad and empty the buffer                        */
151         if (cnt > SHA1_BLOCK_SIZE - 9) {
152                 if (cnt < 60)
153                         ctx->wbuf[15] = 0;
154                 sha1_compile(ctx);
155                 cnt = 0;
156         } else                          /* compute a word index for the empty buffer positions  */
157                 cnt = (cnt >> 2) + 1;
158
159         while (cnt < 14)        /* and zero pad all but last two positions      */
160                 ctx->wbuf[cnt++] = 0;
161
162         /* assemble the eight byte counter in the buffer in big-endian  */
163         /* format                                                      */
164
165         ctx->wbuf[14] = htonl((ctx->count[1] << 3) | (ctx->count[0] >> 29));
166         ctx->wbuf[15] = htonl(ctx->count[0] << 3);
167
168         sha1_compile(ctx);
169
170         /* extract the hash value as bytes in case the hash buffer is   */
171         /* misaligned for 32-bit words                                  */
172
173         for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
174                 hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));
175         
176         return resbuf;
177 }
178
179