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:
12//    - stop symbols being exported for libselinux shared library - October 2015
13//								       Richard Haines <richard_c_haines@btinternet.com>
14//    - Not cast the workspace from a byte array to a CHAR64LONG16 due to allignment isses.
15//      Fixes:
16//        sha1.c:73:33: error: cast from 'uint8_t *' (aka 'unsigned char *') to 'CHAR64LONG16 *' increases required alignment from 1 to 4 [-Werror,-Wcast-align]
17//             CHAR64LONG16*       block = (CHAR64LONG16*) workspace;
18//                                                                     William Roberts <william.c.roberts@intel.com>
19///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
21///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
22//  IMPORTS
23///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24
25#include "sha1.h"
26#include "dso.h"
27#include <memory.h>
28
29///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30//  TYPES
31///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
32
33typedef union
34{
35    uint8_t     c [64];
36    uint32_t    l [16];
37} CHAR64LONG16;
38
39///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40//  INTERNAL FUNCTIONS
41///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
43#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
44
45// blk0() and blk() perform the initial expand.
46#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
47    |(rol(block->l[i],8)&0x00FF00FF))
48
49#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
50    ^block->l[(i+2)&15]^block->l[i&15],1))
51
52// (R0+R1), R2, R3, R4 are the different operations used in SHA1
53#define R0(v,w,x,y,z,i)  z += ((w&(x^y))^y)     + blk0(i)+ 0x5A827999 + rol(v,5); w=rol(w,30);
54#define R1(v,w,x,y,z,i)  z += ((w&(x^y))^y)     + blk(i) + 0x5A827999 + rol(v,5); w=rol(w,30);
55#define R2(v,w,x,y,z,i)  z += (w^x^y)           + blk(i) + 0x6ED9EBA1 + rol(v,5); w=rol(w,30);
56#define R3(v,w,x,y,z,i)  z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v,5); w=rol(w,30);
57#define R4(v,w,x,y,z,i)  z += (w^x^y)           + blk(i) + 0xCA62C1D6 + rol(v,5); w=rol(w,30);
58
59
60///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
61//  TransformFunction
62//
63//  Hash a single 512-bit block. This is the core of the algorithm
64///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65static
66void
67    TransformFunction
68    (
69        uint32_t            state[5],
70        const uint8_t       buffer[64]
71    )
72{
73    uint32_t            a;
74    uint32_t            b;
75    uint32_t            c;
76    uint32_t            d;
77    uint32_t            e;
78    CHAR64LONG16        workspace;
79    CHAR64LONG16*       block = &workspace;
80
81    memcpy( block, buffer, 64 );
82
83    // Copy context->state[] to working vars
84    a = state[0];
85    b = state[1];
86    c = state[2];
87    d = state[3];
88    e = state[4];
89
90    // 4 rounds of 20 operations each. Loop unrolled.
91    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);
92    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);
93    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);
94    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);
95    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);
96    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);
97    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);
98    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);
99    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);
100    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);
101    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);
102    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);
103    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);
104    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);
105    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);
106    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);
107    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);
108    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);
109    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);
110    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);
111
112    // Add the working vars back into context.state[]
113    state[0] += a;
114    state[1] += b;
115    state[2] += c;
116    state[3] += d;
117    state[4] += e;
118}
119
120///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121//  PUBLIC FUNCTIONS
122///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123
124///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125//  Sha1Initialise
126//
127//  Initialises an SHA1 Context. Use this to initialise/reset a context.
128///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129void hidden
130    Sha1Initialise
131    (
132        Sha1Context*                Context
133    )
134{
135    // SHA1 initialization constants
136    Context->State[0] = 0x67452301;
137    Context->State[1] = 0xEFCDAB89;
138    Context->State[2] = 0x98BADCFE;
139    Context->State[3] = 0x10325476;
140    Context->State[4] = 0xC3D2E1F0;
141    Context->Count[0] = 0;
142    Context->Count[1] = 0;
143}
144
145///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146//  Sha1Update
147//
148//  Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
149//  calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
150///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151void hidden
152    Sha1Update
153    (
154        Sha1Context*        Context,
155        void*               Buffer,
156        uint32_t            BufferSize
157    )
158{
159    uint32_t    i;
160    uint32_t    j;
161
162    j = (Context->Count[0] >> 3) & 63;
163    if( (Context->Count[0] += BufferSize << 3) < (BufferSize << 3) )
164    {
165        Context->Count[1]++;
166    }
167
168    Context->Count[1] += (BufferSize >> 29);
169    if( (j + BufferSize) > 63 )
170    {
171        i = 64 - j;
172        memcpy( &Context->Buffer[j], Buffer, i );
173        TransformFunction(Context->State, Context->Buffer);
174        for( ; i + 63 < BufferSize; i += 64 )
175        {
176            TransformFunction(Context->State, (uint8_t*)Buffer + i);
177        }
178        j = 0;
179    }
180    else
181    {
182        i = 0;
183    }
184
185    memcpy( &Context->Buffer[j], &((uint8_t*)Buffer)[i], BufferSize - i );
186}
187
188///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
189//  Sha1Finalise
190//
191//  Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
192//  calling this, Sha1Initialised must be used to reuse the context.
193///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194void hidden
195    Sha1Finalise
196    (
197        Sha1Context*                Context,
198        SHA1_HASH*                  Digest
199    )
200{
201    uint32_t    i;
202    uint8_t     finalcount[8];
203
204    for( i=0; i<8; i++ )
205    {
206        finalcount[i] = (unsigned char)((Context->Count[(i >= 4 ? 0 : 1)]
207         >> ((3-(i & 3)) * 8) ) & 255);  // Endian independent
208    }
209    Sha1Update( Context, (uint8_t*)"\x80", 1 );
210    while( (Context->Count[0] & 504) != 448 )
211    {
212        Sha1Update( Context, (uint8_t*)"\0", 1 );
213    }
214
215    Sha1Update( Context, finalcount, 8 );  // Should cause a Sha1TransformFunction()
216    for( i=0; i<SHA1_HASH_SIZE; i++ )
217    {
218        Digest->bytes[i] = (uint8_t)((Context->State[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
219    }
220}
221