1///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//  LibSha1
3//
4//  Implementation of SHA1 hash function.
5//  Original author:  Steve Reid <sreid@sea-to-sky.net>
6//  Contributions by: James H. Brown <jbrown@burgoyne.com>, Saul Kravitz <Saul.Kravitz@celera.com>,
7//  and Ralph Giles <giles@ghostscript.com>
8//  Modified by WaterJuice retaining Public Domain license.
9//
10//  This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
11//  Modified to stop symbols being exported for libselinux shared library - October 2015
12//								       Richard Haines <richard_c_haines@btinternet.com>
13///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
14
15///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16//  IMPORTS
17///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18
19#include "sha1.h"
20#include "dso.h"
21#include <memory.h>
22
23///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24//  TYPES
25///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27typedef union
28{
29    uint8_t     c [64];
30    uint32_t    l [16];
31} CHAR64LONG16;
32
33///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
34//  INTERNAL FUNCTIONS
35///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
36
37#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
38
39// blk0() and blk() perform the initial expand.
40#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
41    |(rol(block->l[i],8)&0x00FF00FF))
42
43#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
44    ^block->l[(i+2)&15]^block->l[i&15],1))
45
46// (R0+R1), R2, R3, R4 are the different operations used in SHA1
47#define R0(v,w,x,y,z,i)  z += ((w&(x^y))^y)     + blk0(i)+ 0x5A827999 + rol(v,5); w=rol(w,30);
48#define R1(v,w,x,y,z,i)  z += ((w&(x^y))^y)     + blk(i) + 0x5A827999 + rol(v,5); w=rol(w,30);
49#define R2(v,w,x,y,z,i)  z += (w^x^y)           + blk(i) + 0x6ED9EBA1 + rol(v,5); w=rol(w,30);
50#define R3(v,w,x,y,z,i)  z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v,5); w=rol(w,30);
51#define R4(v,w,x,y,z,i)  z += (w^x^y)           + blk(i) + 0xCA62C1D6 + rol(v,5); w=rol(w,30);
52
53
54///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55//  TransformFunction
56//
57//  Hash a single 512-bit block. This is the core of the algorithm
58///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59static
60void
61    TransformFunction
62    (
63        uint32_t            state[5],
64        const uint8_t       buffer[64]
65    )
66{
67    uint32_t            a;
68    uint32_t            b;
69    uint32_t            c;
70    uint32_t            d;
71    uint32_t            e;
72    uint8_t             workspace[64];
73    CHAR64LONG16*       block = (CHAR64LONG16*) workspace;
74
75    memcpy( block, buffer, 64 );
76
77    // Copy context->state[] to working vars
78    a = state[0];
79    b = state[1];
80    c = state[2];
81    d = state[3];
82    e = state[4];
83
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
106    // Add the working vars back into context.state[]
107    state[0] += a;
108    state[1] += b;
109    state[2] += c;
110    state[3] += d;
111    state[4] += e;
112}
113
114///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
115//  PUBLIC FUNCTIONS
116///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
117
118///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
119//  Sha1Initialise
120//
121//  Initialises an SHA1 Context. Use this to initialise/reset a context.
122///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123void hidden
124    Sha1Initialise
125    (
126        Sha1Context*                Context
127    )
128{
129    // SHA1 initialization constants
130    Context->State[0] = 0x67452301;
131    Context->State[1] = 0xEFCDAB89;
132    Context->State[2] = 0x98BADCFE;
133    Context->State[3] = 0x10325476;
134    Context->State[4] = 0xC3D2E1F0;
135    Context->Count[0] = 0;
136    Context->Count[1] = 0;
137}
138
139///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140//  Sha1Update
141//
142//  Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
143//  calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
144///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
145void hidden
146    Sha1Update
147    (
148        Sha1Context*        Context,
149        void*               Buffer,
150        uint32_t            BufferSize
151    )
152{
153    uint32_t    i;
154    uint32_t    j;
155
156    j = (Context->Count[0] >> 3) & 63;
157    if( (Context->Count[0] += BufferSize << 3) < (BufferSize << 3) )
158    {
159        Context->Count[1]++;
160    }
161
162    Context->Count[1] += (BufferSize >> 29);
163    if( (j + BufferSize) > 63 )
164    {
165        i = 64 - j;
166        memcpy( &Context->Buffer[j], Buffer, i );
167        TransformFunction(Context->State, Context->Buffer);
168        for( ; i + 63 < BufferSize; i += 64 )
169        {
170            TransformFunction(Context->State, (uint8_t*)Buffer + i);
171        }
172        j = 0;
173    }
174    else
175    {
176        i = 0;
177    }
178
179    memcpy( &Context->Buffer[j], &((uint8_t*)Buffer)[i], BufferSize - i );
180}
181
182///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
183//  Sha1Finalise
184//
185//  Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
186//  calling this, Sha1Initialised must be used to reuse the context.
187///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188void hidden
189    Sha1Finalise
190    (
191        Sha1Context*                Context,
192        SHA1_HASH*                  Digest
193    )
194{
195    uint32_t    i;
196    uint8_t     finalcount[8];
197
198    for( i=0; i<8; i++ )
199    {
200        finalcount[i] = (unsigned char)((Context->Count[(i >= 4 ? 0 : 1)]
201         >> ((3-(i & 3)) * 8) ) & 255);  // Endian independent
202    }
203    Sha1Update( Context, (uint8_t*)"\x80", 1 );
204    while( (Context->Count[0] & 504) != 448 )
205    {
206        Sha1Update( Context, (uint8_t*)"\0", 1 );
207    }
208
209    Sha1Update( Context, finalcount, 8 );  // Should cause a Sha1TransformFunction()
210    for( i=0; i<SHA1_HASH_SIZE; i++ )
211    {
212        Digest->bytes[i] = (uint8_t)((Context->State[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
213    }
214}
215