2 typedef int innocuous_typedef_to_quiet_empty_translation_unit_compiler_warning
;
4 #if defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H
5 #define USE_OPENSSL_CRYPTO
8 #ifndef USE_OPENSSL_CRYPTO
10 #include "sys-endian.h"
11 #include "algo_sha1.h"
14 * obtained from https://github.com/nori0428/mod_websocket
20 * Originally written by Steve Reid <steve@edmweb.com>
22 * Modified by Aaron D. Gifford <agifford@infowest.com>
24 * NO COPYRIGHT - THIS IS 100% IN THE PUBLIC DOMAIN
26 * The original unmodified version is available at:
27 * ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
46 /* blk0() and blk() perform the initial expand. */
47 /* I got the idea of expanding during the round function from SSLeay */
49 #ifdef __LITTLE_ENDIAN__
50 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&(sha1_quadbyte)0xFF00FF00) \
51 |(rol(block->l[i],8)&(sha1_quadbyte)0x00FF00FF))
53 #define blk0(i) block->l[i]
56 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
57 ^block->l[(i+2)&15]^block->l[i&15],1))
59 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
60 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
61 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
62 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
63 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
64 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
66 typedef union _BYTE64QUAD16
{
71 /* Hash a single 512-bit block. This is the core of the algorithm. */
72 void SHA1_Transform(sha1_quadbyte state
[5], const sha1_byte buffer
[64]) {
73 sha1_quadbyte a
, b
, c
, d
, e
;
77 /* slow but cast-align */
78 memcpy(src
.c
, buffer
, sizeof(sha1_byte
) * 64);
80 /* Copy context->state[] to working vars */
86 /* 4 rounds of 20 operations each. Loop unrolled. */
87 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);
88 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);
89 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);
90 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);
91 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);
92 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);
93 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);
94 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);
95 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);
96 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);
97 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);
98 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);
99 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);
100 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);
101 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);
102 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);
103 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);
104 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);
105 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);
106 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);
107 /* Add the working vars back into context.state[] */
114 a
= b
= c
= d
= e
= 0;
118 /* SHA1_Init - Initialize new context */
119 void SHA1_Init(SHA_CTX
* context
) {
120 /* SHA1 initialization constants */
121 context
->state
[0] = 0x67452301;
122 context
->state
[1] = 0xEFCDAB89;
123 context
->state
[2] = 0x98BADCFE;
124 context
->state
[3] = 0x10325476;
125 context
->state
[4] = 0xC3D2E1F0;
126 context
->count
[0] = context
->count
[1] = 0;
129 /* Run your data through this. */
130 void SHA1_Update(SHA_CTX
*context
, const sha1_byte
*data
, unsigned int len
) {
133 j
= (context
->count
[0] >> 3) & 63;
134 if ((context
->count
[0] += len
<< 3) < (len
<< 3)) context
->count
[1]++;
135 context
->count
[1] += (len
>> 29);
136 if ((j
+ len
) > 63) {
137 memcpy(&context
->buffer
[j
], data
, (i
= 64-j
));
138 SHA1_Transform(context
->state
, context
->buffer
);
139 for ( ; i
+ 63 < len
; i
+= 64) {
140 SHA1_Transform(context
->state
, &data
[i
]);
145 memcpy(&context
->buffer
[j
], &data
[i
], len
- i
);
149 /* Add padding and return the message digest. */
150 void SHA1_Final(sha1_byte digest
[SHA1_DIGEST_LENGTH
], SHA_CTX
*context
) {
152 sha1_byte finalcount
[8];
154 for (i
= 0; i
< 8; i
++) {
155 finalcount
[i
] = (sha1_byte
)((context
->count
[(i
>= 4 ? 0 : 1)]
156 >> ((3-(i
& 3)) * 8) ) & 255); /* Endian independent */
158 SHA1_Update(context
, (sha1_byte
*)"\200", 1);
159 while ((context
->count
[0] & 504) != 448) {
160 SHA1_Update(context
, (sha1_byte
*)"\0", 1);
162 /* Should cause a SHA1_Transform() */
163 SHA1_Update(context
, finalcount
, 8);
164 for (i
= 0; i
< SHA1_DIGEST_LENGTH
; i
++) {
165 digest
[i
] = (sha1_byte
)
166 ((context
->state
[i
>>2] >> ((3-(i
& 3)) * 8) ) & 255);
170 memset(context
->buffer
, 0, SHA1_BLOCK_LENGTH
);
171 memset(context
->state
, 0, SHA1_DIGEST_LENGTH
);
172 memset(context
->count
, 0, 8);
173 memset(&finalcount
, 0, 8);
176 unsigned char *SHA1(const unsigned char *d
, size_t n
, unsigned char *md
) {
179 SHA1_Update(&ctx
, d
, (unsigned int)n
);
180 SHA1_Final(md
, &ctx
);