1 #include "rar.hpp"
2 
3 /*
4 SHA-1 in C
5 By Steve Reid <steve@edmweb.com>
6 100% Public Domain
7 
8 Test Vectors (from FIPS PUB 180-1)
9 "abc"
10   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
11 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
12   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
13 A million repetitions of "a"
14   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
15 */
16 
17 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
18   #if defined(_M_IX86) || defined(_M_I86) || defined(__alpha)
19     #define LITTLE_ENDIAN
20   #else
21     #error "LITTLE_ENDIAN or BIG_ENDIAN must be defined"
22 	#endif
23 #endif
24 
25 /* #define SHA1HANDSOFF * Copies data before messing with it. */
26 
27 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
28 
29 /* blk0() and blk() perform the initial expand. */
30 /* I got the idea of expanding during the round function from SSLeay */
31 #ifdef LITTLE_ENDIAN
32 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
33     |(rol(block->l[i],8)&0x00FF00FF))
34 #else
35 #define blk0(i) block->l[i]
36 #endif
37 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
38     ^block->l[(i+2)&15]^block->l[i&15],1))
39 
40 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
41 #define R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);}
42 #define R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);}
43 #define R2(v,w,x,y,z,i) {z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);}
44 #define R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);}
45 #define R4(v,w,x,y,z,i) {z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);}
46 
47 #ifdef _MSC_VER
48 #pragma optimize( "", off )
49 // We need to disable the optimization to really wipe these variables.
50 #endif
wipevars(uint32 & a,uint32 & b,uint32 & c,uint32 & d,uint32 & e)51 static void wipevars(uint32 &a,uint32 &b,uint32 &c,uint32 &d,uint32 &e)
52 {
53   // Wipe used variables for safety reason.
54   a=b=c=d=e=0;
55 }
56 #ifdef _MSC_VER
57 #pragma optimize( "", on )
58 #endif
59 
60 /* Hash a single 512-bit block. This is the core of the algorithm. */
61 
SHA1Transform(uint32 state[5],unsigned char workspace[64],unsigned char buffer[64],bool handsoff)62 void SHA1Transform(uint32 state[5], unsigned char workspace[64], unsigned char buffer[64], bool handsoff)
63 {
64 #ifndef SFX_MODULE
65   uint32 a, b, c, d, e;
66 #endif
67   typedef union {
68     unsigned char c[64];
69     uint32 l[16];
70 } CHAR64LONG16;
71 CHAR64LONG16* block;
72     if (handsoff)
73     {
74       block = (CHAR64LONG16*)workspace;
75       memcpy(block, buffer, 64);
76     }
77     else
78       block = (CHAR64LONG16*)buffer;
79 #ifdef SFX_MODULE
80     static int pos[80][5];
81     static bool pinit=false;
82     if (!pinit)
83     {
84       for (int I=0,P=0;I<80;I++,P=(P ? P-1:4))
85       {
86         pos[I][0]=P;
87         pos[I][1]=(P+1)%5;
88         pos[I][2]=(P+2)%5;
89         pos[I][3]=(P+3)%5;
90         pos[I][4]=(P+4)%5;
91       }
92       pinit=true;
93     }
94     uint32 s[5];
95     for (int I=0;I<sizeof(s)/sizeof(s[0]);I++)
96       s[I]=state[I];
97 
98     for (int I=0;I<16;I++)
99       R0(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
100     for (int I=16;I<20;I++)
101       R1(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
102     for (int I=20;I<40;I++)
103       R2(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
104     for (int I=40;I<60;I++)
105       R3(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
106     for (int I=60;I<80;I++)
107       R4(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
108 
109     for (int I=0;I<sizeof(s)/sizeof(s[0]);I++)
110       state[I]+=s[I];
111 #else
112     /* Copy context->state[] to working vars */
113     a = state[0];
114     b = state[1];
115     c = state[2];
116     d = state[3];
117     e = state[4];
118     /* 4 rounds of 20 operations each. Loop unrolled. */
119     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);
120     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);
121     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);
122     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);
123     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);
124     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);
125     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);
126     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);
127     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);
128     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);
129     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);
130     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);
131     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);
132     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);
133     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);
134     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);
135     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);
136     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);
137     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);
138     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);
139     /* Add the working vars back into context.state[] */
140     state[0] += a;
141     state[1] += b;
142     state[2] += c;
143     state[3] += d;
144     state[4] += e;
145 
146     /* Wipe variables */
147 // Such wipe method does not work in optimizing compilers.
148 //    a = b = c = d = e = 0;
149 //    memset(&a,0,sizeof(a));
150 
151     wipevars(a,b,c,d,e);
152 #endif
153 }
154 
155 
156 /* Initialize new context */
157 
hash_initial(hash_context * context)158 void hash_initial(hash_context* context)
159 {
160     /* SHA1 initialization constants */
161     context->state[0] = 0x67452301;
162     context->state[1] = 0xEFCDAB89;
163     context->state[2] = 0x98BADCFE;
164     context->state[3] = 0x10325476;
165     context->state[4] = 0xC3D2E1F0;
166     context->count[0] = context->count[1] = 0;
167 }
168 
169 
170 /* Run your data through this. */
hash_process(hash_context * context,unsigned char * data,size_t len,bool handsoff)171 void hash_process( hash_context * context, unsigned char * data, size_t len,
172                    bool handsoff )
173 {
174 unsigned int i, j;
175 uint blen = ((uint)len)<<3;
176 
177     j = (context->count[0] >> 3) & 63;
178     if ((context->count[0] += blen) < blen ) context->count[1]++;
179     context->count[1] += (uint32)(len >> 29);
180     if ((j + len) > 63) {
181         memcpy(&context->buffer[j], data, (i = 64-j));
182         SHA1Transform(context->state, context->workspace, context->buffer, handsoff);
183         for ( ; i + 63 < len; i += 64) {
184 #ifdef ALLOW_NOT_ALIGNED_INT
185             SHA1Transform(context->state, context->workspace, &data[i], handsoff);
186 #else
187             unsigned char buffer[64];
188             memcpy(buffer,data+i,sizeof(buffer));
189             SHA1Transform(context->state, context->workspace, buffer, handsoff);
190             memcpy(data+i,buffer,sizeof(buffer));
191 #endif
192 #ifdef BIG_ENDIAN
193             if (!handsoff)
194             {
195               unsigned char *d=data+i;
196               for (int k=0;k<64;k+=4)
197               {
198                 byte b0=d[k],b1=d[k+1];
199                 d[k]=d[k+3];
200                 d[k+1]=d[k+2];
201                 d[k+2]=b1;
202                 d[k+3]=b0;
203               }
204             }
205 #endif
206         }
207         j = 0;
208     }
209     else i = 0;
210     if (len > i)
211       memcpy(&context->buffer[j], &data[i], len - i);
212 }
213 
214 
215 /* Add padding and return the message digest. */
216 
hash_final(hash_context * context,uint32 digest[5],bool handsoff)217 void hash_final( hash_context* context, uint32 digest[5], bool handsoff)
218 {
219 uint i, j;
220 unsigned char finalcount[8];
221 
222     for (i = 0; i < 8; i++) {
223         finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
224          >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
225     }
226     unsigned char ch=(unsigned char)'\200';
227     hash_process(context, &ch, 1, handsoff);
228     while ((context->count[0] & 504) != 448) {
229         ch=0;
230         hash_process(context, &ch, 1, handsoff);
231     }
232     hash_process(context, finalcount, 8, handsoff);  /* Should cause a SHA1Transform() */
233     for (i = 0; i < 5; i++) {
234         digest[i] = context->state[i] & 0xffffffff;
235     }
236     /* Wipe variables */
237     memset(&i,0,sizeof(i));
238     memset(&j,0,sizeof(j));
239     memset(context->buffer, 0, 64);
240     memset(context->state, 0, 20);
241     memset(context->count, 0, 8);
242     memset(&finalcount, 0, 8);
243     if (handsoff)
244       memset(context->workspace,0,sizeof(context->workspace)); // Wipe the temporary buffer.
245 //      SHA1Transform(context->state, context->workspace, context->buffer, true); /* make SHA1Transform overwrite it's own static vars */
246 }
247 
248 
249