/src/dovecot/src/lib/sha2.c
Line | Count | Source |
1 | | /* |
2 | | * FIPS 180-2 SHA-224/256/384/512 implementation |
3 | | * Last update: 02/02/2007 |
4 | | * Issue date: 04/30/2005 |
5 | | * |
6 | | * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch> |
7 | | * All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * 1. Redistributions of source code must retain the above copyright |
13 | | * notice, this list of conditions and the following disclaimer. |
14 | | * 2. Redistributions in binary form must reproduce the above copyright |
15 | | * notice, this list of conditions and the following disclaimer in the |
16 | | * documentation and/or other materials provided with the distribution. |
17 | | * 3. Neither the name of the project nor the names of its contributors |
18 | | * may be used to endorse or promote products derived from this software |
19 | | * without specific prior written permission. |
20 | | * |
21 | | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
22 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
25 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | | * SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include "lib.h" |
35 | | #include "hash-method.h" |
36 | | #include "sha2.h" |
37 | | |
38 | 0 | #define SHFR(x, n) (x >> n) |
39 | 0 | #define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) |
40 | | #define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) |
41 | 0 | #define CH(x, y, z) ((x & y) ^ (~x & z)) |
42 | 0 | #define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) |
43 | | |
44 | 0 | #define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) |
45 | 0 | #define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) |
46 | 0 | #define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3)) |
47 | 0 | #define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10)) |
48 | | |
49 | 0 | #define SHA384_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39)) |
50 | 0 | #define SHA384_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41)) |
51 | | #define SHA384_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7)) |
52 | | #define SHA384_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6)) |
53 | | |
54 | 0 | #define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39)) |
55 | 0 | #define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41)) |
56 | 0 | #define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7)) |
57 | 0 | #define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6)) |
58 | | |
59 | 0 | #define UNPACK32(x, str) \ |
60 | 0 | { \ |
61 | 0 | *((str) + 3) = (uint8_t) ((x) ); \ |
62 | 0 | *((str) + 2) = (uint8_t) ((x) >> 8); \ |
63 | 0 | *((str) + 1) = (uint8_t) ((x) >> 16); \ |
64 | 0 | *((str) + 0) = (uint8_t) ((x) >> 24); \ |
65 | 0 | } |
66 | | |
67 | 0 | #define PACK32(str, x) \ |
68 | 0 | { \ |
69 | 0 | *(x) = ((uint32_t) *((str) + 3) ) \ |
70 | 0 | | ((uint32_t) *((str) + 2) << 8) \ |
71 | 0 | | ((uint32_t) *((str) + 1) << 16) \ |
72 | 0 | | ((uint32_t) *((str) + 0) << 24); \ |
73 | 0 | } |
74 | | |
75 | 0 | #define UNPACK64(x, str) \ |
76 | 0 | { \ |
77 | 0 | *((str) + 7) = (uint8_t) ((x) ); \ |
78 | 0 | *((str) + 6) = (uint8_t) ((x) >> 8); \ |
79 | 0 | *((str) + 5) = (uint8_t) ((x) >> 16); \ |
80 | 0 | *((str) + 4) = (uint8_t) ((x) >> 24); \ |
81 | 0 | *((str) + 3) = (uint8_t) ((x) >> 32); \ |
82 | 0 | *((str) + 2) = (uint8_t) ((x) >> 40); \ |
83 | 0 | *((str) + 1) = (uint8_t) ((x) >> 48); \ |
84 | 0 | *((str) + 0) = (uint8_t) ((x) >> 56); \ |
85 | 0 | } |
86 | | |
87 | 0 | #define PACK64(str, x) \ |
88 | 0 | { \ |
89 | 0 | *(x) = ((uint64_t) *((str) + 7) ) \ |
90 | 0 | | ((uint64_t) *((str) + 6) << 8) \ |
91 | 0 | | ((uint64_t) *((str) + 5) << 16) \ |
92 | 0 | | ((uint64_t) *((str) + 4) << 24) \ |
93 | 0 | | ((uint64_t) *((str) + 3) << 32) \ |
94 | 0 | | ((uint64_t) *((str) + 2) << 40) \ |
95 | 0 | | ((uint64_t) *((str) + 1) << 48) \ |
96 | 0 | | ((uint64_t) *((str) + 0) << 56); \ |
97 | 0 | } |
98 | | |
99 | 0 | #define SHA256_SCR(i) \ |
100 | 0 | { \ |
101 | 0 | w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \ |
102 | 0 | + SHA256_F3(w[i - 15]) + w[i - 16]; \ |
103 | 0 | } |
104 | | |
105 | 0 | #define SHA384_SCR(i) \ |
106 | 0 | { \ |
107 | 0 | w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \ |
108 | 0 | + SHA512_F3(w[i - 15]) + w[i - 16]; \ |
109 | 0 | } |
110 | | |
111 | 0 | #define SHA512_SCR(i) \ |
112 | 0 | { \ |
113 | 0 | w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \ |
114 | 0 | + SHA512_F3(w[i - 15]) + w[i - 16]; \ |
115 | 0 | } |
116 | | |
117 | | static const uint32_t sha256_h0[8] = |
118 | | {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
119 | | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; |
120 | | |
121 | | static const uint64_t sha384_h0[8] = |
122 | | {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, |
123 | | 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, |
124 | | 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, |
125 | | 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL}; |
126 | | |
127 | | static const uint64_t sha512_h0[8] = |
128 | | {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, |
129 | | 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, |
130 | | 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, |
131 | | 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; |
132 | | |
133 | | static const uint32_t sha256_k[64] = |
134 | | {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
135 | | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
136 | | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
137 | | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
138 | | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
139 | | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
140 | | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
141 | | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
142 | | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
143 | | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
144 | | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
145 | | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
146 | | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
147 | | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
148 | | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
149 | | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; |
150 | | |
151 | | static const uint64_t sha512_k[80] = |
152 | | {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, |
153 | | 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, |
154 | | 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, |
155 | | 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, |
156 | | 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, |
157 | | 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, |
158 | | 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, |
159 | | 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, |
160 | | 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, |
161 | | 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, |
162 | | 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, |
163 | | 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, |
164 | | 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, |
165 | | 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, |
166 | | 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, |
167 | | 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, |
168 | | 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, |
169 | | 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, |
170 | | 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, |
171 | | 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, |
172 | | 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, |
173 | | 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, |
174 | | 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, |
175 | | 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, |
176 | | 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, |
177 | | 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, |
178 | | 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, |
179 | | 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, |
180 | | 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, |
181 | | 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, |
182 | | 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, |
183 | | 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, |
184 | | 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, |
185 | | 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, |
186 | | 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, |
187 | | 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, |
188 | | 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, |
189 | | 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, |
190 | | 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, |
191 | | 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; |
192 | | |
193 | | |
194 | | /* SHA-256 functions */ |
195 | | |
196 | | static void ATTR_UNSIGNED_WRAPS |
197 | | sha256_transf(struct sha256_ctx *ctx, const unsigned char *data, |
198 | | size_t block_nb) |
199 | 0 | { |
200 | 0 | uint32_t w[64]; |
201 | 0 | uint32_t wv[8]; |
202 | 0 | uint32_t t1, t2; |
203 | 0 | const unsigned char *sub_block; |
204 | 0 | int i,j; |
205 | | |
206 | |
|
207 | 0 | for (i = 0; i < (int) block_nb; i++) { |
208 | 0 | sub_block = data + (i << 6); |
209 | |
|
210 | 0 | for (j = 0; j < 16; j++) { |
211 | 0 | PACK32(&sub_block[j << 2], &w[j]); |
212 | 0 | } |
213 | |
|
214 | 0 | for (j = 16; j < 64; j++) { |
215 | 0 | SHA256_SCR(j); |
216 | 0 | } |
217 | |
|
218 | 0 | for (j = 0; j < 8; j++) { |
219 | 0 | wv[j] = ctx->h[j]; |
220 | 0 | } |
221 | |
|
222 | 0 | for (j = 0; j < 64; j++) { |
223 | 0 | t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) |
224 | 0 | + sha256_k[j] + w[j]; |
225 | 0 | t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); |
226 | 0 | wv[7] = wv[6]; |
227 | 0 | wv[6] = wv[5]; |
228 | 0 | wv[5] = wv[4]; |
229 | 0 | wv[4] = wv[3] + t1; |
230 | 0 | wv[3] = wv[2]; |
231 | 0 | wv[2] = wv[1]; |
232 | 0 | wv[1] = wv[0]; |
233 | 0 | wv[0] = t1 + t2; |
234 | 0 | } |
235 | |
|
236 | 0 | for (j = 0; j < 8; j++) { |
237 | 0 | ctx->h[j] += wv[j]; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | void sha256_init(struct sha256_ctx *ctx) |
243 | 0 | { |
244 | 0 | int i; |
245 | |
|
246 | 0 | for (i = 0; i < 8; i++) { |
247 | 0 | ctx->h[i] = sha256_h0[i]; |
248 | 0 | } |
249 | |
|
250 | 0 | ctx->len = 0; |
251 | 0 | ctx->tot_len = 0; |
252 | 0 | } |
253 | | |
254 | | void sha256_loop(struct sha256_ctx *ctx, const void *data, |
255 | | size_t len) |
256 | 0 | { |
257 | 0 | const unsigned char *shifted_message; |
258 | 0 | size_t block_nb; |
259 | 0 | size_t new_len, rem_len, tmp_len; |
260 | |
|
261 | 0 | if (len == 0) |
262 | 0 | return; |
263 | | |
264 | 0 | tmp_len = SHA256_BLOCK_SIZE - ctx->len; |
265 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
266 | |
|
267 | 0 | memcpy(&ctx->block[ctx->len], data, rem_len); |
268 | |
|
269 | 0 | if (ctx->len + len < SHA256_BLOCK_SIZE) { |
270 | 0 | ctx->len += len; |
271 | 0 | return; |
272 | 0 | } |
273 | | |
274 | 0 | new_len = len - rem_len; |
275 | 0 | block_nb = new_len / SHA256_BLOCK_SIZE; |
276 | |
|
277 | 0 | shifted_message = CONST_PTR_OFFSET(data, rem_len); |
278 | |
|
279 | 0 | sha256_transf(ctx, ctx->block, 1); |
280 | 0 | sha256_transf(ctx, shifted_message, block_nb); |
281 | |
|
282 | 0 | rem_len = new_len % SHA256_BLOCK_SIZE; |
283 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len); |
284 | |
|
285 | 0 | ctx->len = rem_len; |
286 | 0 | ctx->tot_len += (block_nb + 1) << 6; |
287 | 0 | } |
288 | | |
289 | | void sha256_result(struct sha256_ctx *ctx, |
290 | | unsigned char digest[STATIC_ARRAY SHA256_RESULTLEN]) |
291 | 0 | { |
292 | 0 | size_t block_nb; |
293 | 0 | size_t pm_len; |
294 | 0 | uint64_t len_b; |
295 | 0 | int i; |
296 | |
|
297 | 0 | block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) |
298 | 0 | < (ctx->len % SHA256_BLOCK_SIZE))); |
299 | |
|
300 | 0 | len_b = (ctx->tot_len + ctx->len) << 3; |
301 | 0 | pm_len = block_nb << 6; |
302 | |
|
303 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
304 | 0 | ctx->block[ctx->len] = 0x80; |
305 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
306 | |
|
307 | 0 | sha256_transf(ctx, ctx->block, block_nb); |
308 | |
|
309 | 0 | for (i = 0 ; i < 8; i++) { |
310 | 0 | UNPACK32(ctx->h[i], &digest[i << 2]); |
311 | 0 | } |
312 | 0 | } |
313 | | |
314 | | void sha256_get_digest(const void *data, size_t size, |
315 | | unsigned char digest[STATIC_ARRAY SHA256_RESULTLEN]) |
316 | 0 | { |
317 | 0 | struct sha256_ctx ctx; |
318 | |
|
319 | 0 | sha256_init(&ctx); |
320 | 0 | sha256_loop(&ctx, data, size); |
321 | 0 | sha256_result(&ctx, digest); |
322 | 0 | } |
323 | | |
324 | | /* SHA-384 functions */ |
325 | | |
326 | | static void ATTR_UNSIGNED_WRAPS |
327 | | sha384_transf(struct sha384_ctx *ctx, const unsigned char *data, |
328 | | size_t block_nb) |
329 | 0 | { |
330 | 0 | uint64_t w[80]; |
331 | 0 | uint64_t wv[8]; |
332 | 0 | uint64_t t1, t2; |
333 | 0 | const unsigned char *sub_block; |
334 | 0 | int i, j; |
335 | |
|
336 | 0 | for (i = 0; i < (int) block_nb; i++) { |
337 | 0 | sub_block = data + (i << 7); |
338 | |
|
339 | 0 | for (j = 0; j < 16; j++) { |
340 | 0 | PACK64(&sub_block[j << 3], &w[j]); |
341 | 0 | } |
342 | |
|
343 | 0 | for (j = 16; j < 80; j++) { |
344 | 0 | SHA384_SCR(j); |
345 | 0 | } |
346 | |
|
347 | 0 | for (j = 0; j < 8; j++) { |
348 | 0 | wv[j] = ctx->h[j]; |
349 | 0 | } |
350 | |
|
351 | 0 | for (j = 0; j < 80; j++) { |
352 | | /* sha384_k is same as sha512_k */ |
353 | 0 | t1 = wv[7] + SHA384_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) |
354 | 0 | + sha512_k[j] + w[j]; |
355 | 0 | t2 = SHA384_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); |
356 | 0 | wv[7] = wv[6]; |
357 | 0 | wv[6] = wv[5]; |
358 | 0 | wv[5] = wv[4]; |
359 | 0 | wv[4] = wv[3] + t1; |
360 | 0 | wv[3] = wv[2]; |
361 | 0 | wv[2] = wv[1]; |
362 | 0 | wv[1] = wv[0]; |
363 | 0 | wv[0] = t1 + t2; |
364 | 0 | } |
365 | |
|
366 | 0 | for (j = 0; j < 8; j++) { |
367 | 0 | ctx->h[j] += wv[j]; |
368 | 0 | } |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | | void sha384_init(struct sha384_ctx *ctx) |
373 | 0 | { |
374 | 0 | int i; |
375 | |
|
376 | 0 | for (i = 0; i < 8; i++) { |
377 | 0 | ctx->h[i] = sha384_h0[i]; |
378 | 0 | } |
379 | |
|
380 | 0 | ctx->len = 0; |
381 | 0 | ctx->tot_len = 0; |
382 | 0 | } |
383 | | |
384 | | void sha384_loop(struct sha384_ctx *ctx, const void *data, |
385 | | size_t len) |
386 | 0 | { |
387 | 0 | const unsigned char *shifted_message; |
388 | 0 | size_t block_nb; |
389 | 0 | size_t new_len, rem_len, tmp_len; |
390 | |
|
391 | 0 | if (len == 0) |
392 | 0 | return; |
393 | | |
394 | 0 | tmp_len = SHA384_BLOCK_SIZE - ctx->len; |
395 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
396 | |
|
397 | 0 | memcpy(&ctx->block[ctx->len], data, rem_len); |
398 | |
|
399 | 0 | if (ctx->len + len < SHA384_BLOCK_SIZE) { |
400 | 0 | ctx->len += len; |
401 | 0 | return; |
402 | 0 | } |
403 | | |
404 | 0 | new_len = len - rem_len; |
405 | 0 | block_nb = new_len / SHA384_BLOCK_SIZE; |
406 | |
|
407 | 0 | shifted_message = CONST_PTR_OFFSET(data, rem_len); |
408 | |
|
409 | 0 | sha384_transf(ctx, ctx->block, 1); |
410 | 0 | sha384_transf(ctx, shifted_message, block_nb); |
411 | |
|
412 | 0 | rem_len = new_len % SHA384_BLOCK_SIZE; |
413 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); |
414 | |
|
415 | 0 | ctx->len = rem_len; |
416 | 0 | ctx->tot_len += (block_nb + 1) << 7; |
417 | 0 | } |
418 | | |
419 | | void sha384_result(struct sha384_ctx *ctx, |
420 | | unsigned char digest[STATIC_ARRAY SHA384_RESULTLEN]) |
421 | 0 | { |
422 | 0 | unsigned int block_nb; |
423 | 0 | unsigned int pm_len; |
424 | 0 | uint64_t len_b; |
425 | 0 | int i; |
426 | |
|
427 | 0 | block_nb = 1 + ((SHA384_BLOCK_SIZE - 17) |
428 | 0 | < (ctx->len % SHA384_BLOCK_SIZE)); |
429 | |
|
430 | 0 | len_b = (ctx->tot_len + ctx->len) << 3; |
431 | 0 | pm_len = block_nb << 7; |
432 | |
|
433 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
434 | 0 | ctx->block[ctx->len] = 0x80; |
435 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
436 | |
|
437 | 0 | sha384_transf(ctx, ctx->block, block_nb); |
438 | |
|
439 | 0 | for (i = 0 ; i < 6; i++) { |
440 | 0 | UNPACK64(ctx->h[i], &digest[i << 3]); |
441 | 0 | } |
442 | 0 | } |
443 | | |
444 | | void sha384_get_digest(const void *data, size_t size, |
445 | | unsigned char digest[STATIC_ARRAY SHA384_RESULTLEN]) |
446 | 0 | { |
447 | 0 | struct sha384_ctx ctx; |
448 | |
|
449 | 0 | sha384_init(&ctx); |
450 | 0 | sha384_loop(&ctx, data, size); |
451 | 0 | sha384_result(&ctx, digest); |
452 | 0 | } |
453 | | |
454 | | |
455 | | /* SHA-512 functions */ |
456 | | |
457 | | static void ATTR_UNSIGNED_WRAPS |
458 | | sha512_transf(struct sha512_ctx *ctx, const unsigned char *data, |
459 | | size_t block_nb) |
460 | 0 | { |
461 | 0 | uint64_t w[80]; |
462 | 0 | uint64_t wv[8]; |
463 | 0 | uint64_t t1, t2; |
464 | 0 | const unsigned char *sub_block; |
465 | 0 | int i, j; |
466 | |
|
467 | 0 | for (i = 0; i < (int) block_nb; i++) { |
468 | 0 | sub_block = data + (i << 7); |
469 | |
|
470 | 0 | for (j = 0; j < 16; j++) { |
471 | 0 | PACK64(&sub_block[j << 3], &w[j]); |
472 | 0 | } |
473 | |
|
474 | 0 | for (j = 16; j < 80; j++) { |
475 | 0 | SHA512_SCR(j); |
476 | 0 | } |
477 | |
|
478 | 0 | for (j = 0; j < 8; j++) { |
479 | 0 | wv[j] = ctx->h[j]; |
480 | 0 | } |
481 | |
|
482 | 0 | for (j = 0; j < 80; j++) { |
483 | 0 | t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) |
484 | 0 | + sha512_k[j] + w[j]; |
485 | 0 | t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); |
486 | 0 | wv[7] = wv[6]; |
487 | 0 | wv[6] = wv[5]; |
488 | 0 | wv[5] = wv[4]; |
489 | 0 | wv[4] = wv[3] + t1; |
490 | 0 | wv[3] = wv[2]; |
491 | 0 | wv[2] = wv[1]; |
492 | 0 | wv[1] = wv[0]; |
493 | 0 | wv[0] = t1 + t2; |
494 | 0 | } |
495 | |
|
496 | 0 | for (j = 0; j < 8; j++) { |
497 | 0 | ctx->h[j] += wv[j]; |
498 | 0 | } |
499 | 0 | } |
500 | 0 | } |
501 | | |
502 | | void sha512_init(struct sha512_ctx *ctx) |
503 | 0 | { |
504 | 0 | int i; |
505 | |
|
506 | 0 | for (i = 0; i < 8; i++) { |
507 | 0 | ctx->h[i] = sha512_h0[i]; |
508 | 0 | } |
509 | |
|
510 | 0 | ctx->len = 0; |
511 | 0 | ctx->tot_len = 0; |
512 | 0 | } |
513 | | |
514 | | void sha512_loop(struct sha512_ctx *ctx, const void *data, |
515 | | size_t len) |
516 | 0 | { |
517 | 0 | const unsigned char *shifted_message; |
518 | 0 | size_t block_nb; |
519 | 0 | size_t new_len, rem_len, tmp_len; |
520 | |
|
521 | 0 | if (len == 0) |
522 | 0 | return; |
523 | | |
524 | 0 | tmp_len = SHA512_BLOCK_SIZE - ctx->len; |
525 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
526 | |
|
527 | 0 | memcpy(&ctx->block[ctx->len], data, rem_len); |
528 | |
|
529 | 0 | if (ctx->len + len < SHA512_BLOCK_SIZE) { |
530 | 0 | ctx->len += len; |
531 | 0 | return; |
532 | 0 | } |
533 | | |
534 | 0 | new_len = len - rem_len; |
535 | 0 | block_nb = new_len / SHA512_BLOCK_SIZE; |
536 | |
|
537 | 0 | shifted_message = CONST_PTR_OFFSET(data, rem_len); |
538 | |
|
539 | 0 | sha512_transf(ctx, ctx->block, 1); |
540 | 0 | sha512_transf(ctx, shifted_message, block_nb); |
541 | |
|
542 | 0 | rem_len = new_len % SHA512_BLOCK_SIZE; |
543 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); |
544 | |
|
545 | 0 | ctx->len = rem_len; |
546 | 0 | ctx->tot_len += (block_nb + 1) << 7; |
547 | 0 | } |
548 | | |
549 | | void sha512_result(struct sha512_ctx *ctx, |
550 | | unsigned char digest[STATIC_ARRAY SHA512_RESULTLEN]) |
551 | 0 | { |
552 | 0 | unsigned int block_nb; |
553 | 0 | unsigned int pm_len; |
554 | 0 | uint64_t len_b; |
555 | 0 | int i; |
556 | |
|
557 | 0 | block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) |
558 | 0 | < (ctx->len % SHA512_BLOCK_SIZE)); |
559 | |
|
560 | 0 | len_b = (ctx->tot_len + ctx->len) << 3; |
561 | 0 | pm_len = block_nb << 7; |
562 | |
|
563 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
564 | 0 | ctx->block[ctx->len] = 0x80; |
565 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
566 | |
|
567 | 0 | sha512_transf(ctx, ctx->block, block_nb); |
568 | |
|
569 | 0 | for (i = 0 ; i < 8; i++) { |
570 | 0 | UNPACK64(ctx->h[i], &digest[i << 3]); |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | void sha512_get_digest(const void *data, size_t size, |
575 | | unsigned char digest[STATIC_ARRAY SHA512_RESULTLEN]) |
576 | 0 | { |
577 | 0 | struct sha512_ctx ctx; |
578 | |
|
579 | 0 | sha512_init(&ctx); |
580 | 0 | sha512_loop(&ctx, data, size); |
581 | 0 | sha512_result(&ctx, digest); |
582 | 0 | } |
583 | | |
584 | | static void hash_method_init_sha256(void *context) |
585 | 0 | { |
586 | 0 | sha256_init(context); |
587 | 0 | } |
588 | | static void hash_method_loop_sha256(void *context, const void *data, size_t size) |
589 | 0 | { |
590 | 0 | sha256_loop(context, data, size); |
591 | 0 | } |
592 | | |
593 | | static void hash_method_result_sha256(void *context, unsigned char *result_r) |
594 | 0 | { |
595 | 0 | sha256_result(context, result_r); |
596 | 0 | } |
597 | | |
598 | | const struct hash_method hash_method_sha256 = { |
599 | | .name = "sha256", |
600 | | .block_size = SHA256_BLOCK_SIZE, |
601 | | .context_size = sizeof(struct sha256_ctx), |
602 | | .digest_size = SHA256_RESULTLEN, |
603 | | |
604 | | .init = hash_method_init_sha256, |
605 | | .loop = hash_method_loop_sha256, |
606 | | .result = hash_method_result_sha256, |
607 | | }; |
608 | | |
609 | | static void hash_method_init_sha384(void *context) |
610 | 0 | { |
611 | 0 | sha384_init(context); |
612 | 0 | } |
613 | | static void hash_method_loop_sha384(void *context, const void *data, size_t size) |
614 | 0 | { |
615 | 0 | sha384_loop(context, data, size); |
616 | 0 | } |
617 | | |
618 | | static void hash_method_result_sha384(void *context, unsigned char *result_r) |
619 | 0 | { |
620 | 0 | sha384_result(context, result_r); |
621 | 0 | } |
622 | | |
623 | | const struct hash_method hash_method_sha384 = { |
624 | | .name = "sha384", |
625 | | .block_size = SHA384_BLOCK_SIZE, |
626 | | .context_size = sizeof(struct sha384_ctx), |
627 | | .digest_size = SHA384_RESULTLEN, |
628 | | |
629 | | .init = hash_method_init_sha384, |
630 | | .loop = hash_method_loop_sha384, |
631 | | .result = hash_method_result_sha384, |
632 | | }; |
633 | | |
634 | | static void hash_method_init_sha512(void *context) |
635 | 0 | { |
636 | 0 | sha512_init(context); |
637 | 0 | } |
638 | | static void hash_method_loop_sha512(void *context, const void *data, size_t size) |
639 | 0 | { |
640 | 0 | sha512_loop(context, data, size); |
641 | 0 | } |
642 | | |
643 | | static void hash_method_result_sha512(void *context, unsigned char *result_r) |
644 | 0 | { |
645 | 0 | sha512_result(context, result_r); |
646 | 0 | } |
647 | | |
648 | | const struct hash_method hash_method_sha512 = { |
649 | | .name = "sha512", |
650 | | .block_size = SHA512_BLOCK_SIZE, |
651 | | .context_size = sizeof(struct sha512_ctx), |
652 | | .digest_size = SHA512_RESULTLEN, |
653 | | |
654 | | .init = hash_method_init_sha512, |
655 | | .loop = hash_method_loop_sha512, |
656 | | .result = hash_method_result_sha512, |
657 | | }; |