xref: /lighttpd1.4/src/algo_sha1.c (revision 1fc8a3e1)
1 #include "first.h"
2 typedef int innocuous_typedef_to_quiet_empty_translation_unit_compiler_warning;
3 
4 #include "sys-crypto-md.h"
5 #ifndef USE_LIB_CRYPTO_SHA1
6 
7 #include "sys-endian.h"
8 #include "algo_sha1.h"
9 
10 /*
11  * obtained from https://github.com/nori0428/mod_websocket
12  */
13 
14 /*
15  * sha1.c
16  *
17  * Originally written by Steve Reid <[email protected]>
18  *
19  * Modified by Aaron D. Gifford <[email protected]>
20  *
21  * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN
22  *
23  * The original unmodified version is available at:
24  *    ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <string.h>
40 
41 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
42 
43 /* blk0() and blk() perform the initial expand. */
44 /* I got the idea of expanding during the round function from SSLeay */
45 
46 #ifdef __LITTLE_ENDIAN__
47 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&(sha1_quadbyte)0xFF00FF00) \
48     |(rol(block->l[i],8)&(sha1_quadbyte)0x00FF00FF))
49 #else
50 #define blk0(i) block->l[i]
51 #endif
52 
53 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
54     ^block->l[(i+2)&15]^block->l[i&15],1))
55 
56 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
57 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
58 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
59 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
60 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
61 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
62 
63 typedef union _BYTE64QUAD16 {
64     sha1_byte c[64];
65     sha1_quadbyte l[16];
66 } BYTE64QUAD16;
67 
68 /* Hash a single 512-bit block. This is the core of the algorithm. */
69 static
SHA1_Transform(sha1_quadbyte state[5],const sha1_byte buffer[64])70 void SHA1_Transform(sha1_quadbyte state[5], const sha1_byte buffer[64]) {
71     sha1_quadbyte a, b, c, d, e;
72     BYTE64QUAD16 src;
73     BYTE64QUAD16 *block;
74 
75     /* slow but cast-align */
76     memcpy(src.c, buffer, sizeof(sha1_byte) * 64);
77     block = &src;
78     /* Copy context->state[] to working vars */
79     a = state[0];
80     b = state[1];
81     c = state[2];
82     d = state[3];
83     e = state[4];
84     /* 4 rounds of 20 operations each. Loop unrolled. */
85     R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
86     R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
87     R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
88     R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
89     R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
90     R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
91     R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
92     R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
93     R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
94     R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
95     R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
96     R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
97     R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
98     R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
99     R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
100     R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
101     R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
102     R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
103     R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
104     R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
105     /* Add the working vars back into context.state[] */
106     state[0] += a;
107     state[1] += b;
108     state[2] += c;
109     state[3] += d;
110     state[4] += e;
111     /* Wipe variables */
112     a = b = c = d = e = 0;
113 }
114 
115 
116 /* SHA1_Init - Initialize new context */
SHA1_Init(SHA_CTX * context)117 void SHA1_Init(SHA_CTX* context) {
118     /* SHA1 initialization constants */
119     context->state[0] = 0x67452301;
120     context->state[1] = 0xEFCDAB89;
121     context->state[2] = 0x98BADCFE;
122     context->state[3] = 0x10325476;
123     context->state[4] = 0xC3D2E1F0;
124     context->count[0] = context->count[1] = 0;
125 }
126 
127 /* Run your data through this. */
SHA1_Update(SHA_CTX * context,const sha1_byte * data,unsigned int len)128 void SHA1_Update(SHA_CTX *context, const sha1_byte *data, unsigned int len) {
129     unsigned int i, j;
130 
131     j = (context->count[0] >> 3) & 63;
132     if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
133     context->count[1] += (len >> 29);
134     if ((j + len) > 63) {
135         memcpy(&context->buffer[j], data, (i = 64-j));
136         SHA1_Transform(context->state, context->buffer);
137         for ( ; i + 63 < len; i += 64) {
138             SHA1_Transform(context->state, &data[i]);
139         }
140         j = 0;
141     }
142     else i = 0;
143     memcpy(&context->buffer[j], &data[i], len - i);
144 }
145 
146 
147 /* Add padding and return the message digest. */
SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH],SHA_CTX * context)148 void SHA1_Final(sha1_byte digest[SHA1_DIGEST_LENGTH], SHA_CTX *context) {
149     sha1_quadbyte i, j;
150     sha1_byte finalcount[8];
151 
152     for (i = 0; i < 8; i++) {
153         finalcount[i] = (sha1_byte)((context->count[(i >= 4 ? 0 : 1)]
154          >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
155     }
156     SHA1_Update(context, (sha1_byte *)"\200", 1);
157     while ((context->count[0] & 504) != 448) {
158         SHA1_Update(context, (sha1_byte *)"\0", 1);
159     }
160     /* Should cause a SHA1_Transform() */
161     SHA1_Update(context, finalcount, 8);
162     for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
163         digest[i] = (sha1_byte)
164          ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
165     }
166     /* Wipe variables */
167     i = j = 0;
168     memset(context->buffer, 0, SHA1_BLOCK_LENGTH);
169     memset(context->state, 0, SHA1_DIGEST_LENGTH);
170     memset(context->count, 0, 8);
171     memset(&finalcount, 0, 8);
172 }
173 
174 #endif
175