/src/selinux/libselinux/src/sha1.c
Line | Count | Source |
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 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 <william.c.roberts@intel.com> |
19 | | // - Silence clang's -Wextra-semi-stmt warning - July 2021, Nicolas Iooss <nicolas.iooss@m4x.org> |
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 | 0 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
46 | | |
47 | | // blk0() and blk() perform the initial expand. |
48 | 0 | #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |
49 | 0 | |(rol(block->l[i],8)&0x00FF00FF)) |
50 | | |
51 | 0 | #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ |
52 | 0 | ^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 | 0 | #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 | 0 | #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 | 0 | #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 | 0 | #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 | 0 | #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 |
70 | | TransformFunction |
71 | | ( |
72 | | uint32_t state[5], |
73 | | const uint8_t buffer[64] |
74 | | ) |
75 | 0 | { |
76 | 0 | uint32_t a; |
77 | 0 | uint32_t b; |
78 | 0 | uint32_t c; |
79 | 0 | uint32_t d; |
80 | 0 | uint32_t e; |
81 | 0 | CHAR64LONG16 workspace; |
82 | 0 | CHAR64LONG16* block = &workspace; |
83 | |
|
84 | 0 | memcpy(block, buffer, 64); |
85 | | |
86 | | // Copy context->state[] to working vars |
87 | 0 | a = state[0]; |
88 | 0 | b = state[1]; |
89 | 0 | c = state[2]; |
90 | 0 | d = state[3]; |
91 | 0 | e = state[4]; |
92 | | |
93 | | // 4 rounds of 20 operations each. Loop unrolled. |
94 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | state[0] += a; |
117 | 0 | state[1] += b; |
118 | 0 | state[2] += c; |
119 | 0 | state[3] += d; |
120 | 0 | state[4] += e; |
121 | 0 | } |
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 |
133 | | Sha1Initialise |
134 | | ( |
135 | | Sha1Context* Context |
136 | | ) |
137 | 0 | { |
138 | | // SHA1 initialization constants |
139 | 0 | Context->State[0] = 0x67452301; |
140 | 0 | Context->State[1] = 0xEFCDAB89; |
141 | 0 | Context->State[2] = 0x98BADCFE; |
142 | 0 | Context->State[3] = 0x10325476; |
143 | 0 | Context->State[4] = 0xC3D2E1F0; |
144 | 0 | Context->Count[0] = 0; |
145 | 0 | Context->Count[1] = 0; |
146 | 0 | } |
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 |
155 | | Sha1Update |
156 | | ( |
157 | | Sha1Context* Context, |
158 | | const void* Buffer, |
159 | | uint32_t BufferSize |
160 | | ) |
161 | 0 | { |
162 | 0 | uint32_t i; |
163 | 0 | uint32_t j; |
164 | |
|
165 | 0 | j = (Context->Count[0] >> 3) & 63; |
166 | 0 | if ((Context->Count[0] += BufferSize << 3) < (BufferSize << 3)) |
167 | 0 | { |
168 | 0 | Context->Count[1]++; |
169 | 0 | } |
170 | |
|
171 | 0 | Context->Count[1] += (BufferSize >> 29); |
172 | 0 | if ((j + BufferSize) > 63) |
173 | 0 | { |
174 | 0 | i = 64 - j; |
175 | 0 | memcpy(&Context->Buffer[j], Buffer, i); |
176 | 0 | TransformFunction(Context->State, Context->Buffer); |
177 | 0 | for (; i + 63 < BufferSize; i += 64) |
178 | 0 | { |
179 | 0 | TransformFunction(Context->State, (const uint8_t*)Buffer + i); |
180 | 0 | } |
181 | 0 | j = 0; |
182 | 0 | } |
183 | 0 | else |
184 | 0 | { |
185 | 0 | i = 0; |
186 | 0 | } |
187 | |
|
188 | 0 | memcpy(&Context->Buffer[j], &((const uint8_t*)Buffer)[i], BufferSize - i); |
189 | 0 | } |
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 |
198 | | Sha1Finalise |
199 | | ( |
200 | | Sha1Context* Context, |
201 | | SHA1_HASH* Digest |
202 | | ) |
203 | 0 | { |
204 | 0 | uint32_t i; |
205 | 0 | uint8_t finalcount[8]; |
206 | |
|
207 | 0 | for (i = 0; i < 8; i++) |
208 | 0 | { |
209 | 0 | finalcount[i] = (unsigned char)((Context->Count[(i >= 4 ? 0 : 1)] |
210 | 0 | >> ((3-(i & 3)) * 8) ) & 255); // Endian independent |
211 | 0 | } |
212 | 0 | Sha1Update(Context, (const uint8_t*)"\x80", 1); |
213 | 0 | while ((Context->Count[0] & 504) != 448) |
214 | 0 | { |
215 | 0 | Sha1Update(Context, (const uint8_t*)"\0", 1); |
216 | 0 | } |
217 | |
|
218 | 0 | Sha1Update(Context, finalcount, 8); // Should cause a Sha1TransformFunction() |
219 | 0 | for (i = 0; i < SHA1_HASH_SIZE; i++) |
220 | 0 | { |
221 | 0 | Digest->bytes[i] = (uint8_t)((Context->State[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
222 | 0 | } |
223 | 0 | } |