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