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