1 | /* $Id: sha512-alt.cpp 51841 2014-07-03 11:58:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - SHA-512 and SHA-384 hash functions, Alternative Implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2014 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Defined Constants And Macros *
|
---|
30 | *******************************************************************************/
|
---|
31 | /** The SHA-512 block size (in bytes). */
|
---|
32 | #define RTSHA512_BLOCK_SIZE 128U
|
---|
33 |
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Header Files *
|
---|
37 | *******************************************************************************/
|
---|
38 | #include "internal/iprt.h"
|
---|
39 | #include <iprt/types.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /** Our private context structure. */
|
---|
46 | typedef struct RTSHA512ALTPRIVATECTX
|
---|
47 | {
|
---|
48 | /** The W array.
|
---|
49 | * Buffering happens in the first 16 words, converted from big endian to host
|
---|
50 | * endian immediately before processing. The amount of buffered data is kept
|
---|
51 | * in the 6 least significant bits of cbMessage. */
|
---|
52 | uint64_t auW[80];
|
---|
53 | /** The message length (in bytes). */
|
---|
54 | RTUINT128U cbMessage;
|
---|
55 | /** The 8 hash values. */
|
---|
56 | uint64_t auH[8];
|
---|
57 | } RTSHA512ALTPRIVATECTX;
|
---|
58 |
|
---|
59 | #define RT_SHA512_PRIVATE_ALT_CONTEXT
|
---|
60 | #include <iprt/sha.h>
|
---|
61 |
|
---|
62 |
|
---|
63 | AssertCompile(RT_SIZEOFMEMB(RTSHA512CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA512CONTEXT, AltPrivate));
|
---|
64 | AssertCompileMemberSize(RTSHA512ALTPRIVATECTX, auH, RTSHA512_HASH_SIZE);
|
---|
65 |
|
---|
66 |
|
---|
67 | /*******************************************************************************
|
---|
68 | * Global Variables *
|
---|
69 | *******************************************************************************/
|
---|
70 | /** The K constants. */
|
---|
71 | static uint64_t const g_auKs[] =
|
---|
72 | {
|
---|
73 | UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
|
---|
74 | UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118),
|
---|
75 | UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2),
|
---|
76 | UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694),
|
---|
77 | UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
|
---|
78 | UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5),
|
---|
79 | UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4),
|
---|
80 | UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70),
|
---|
81 | UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df),
|
---|
82 | UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
|
---|
83 | UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30),
|
---|
84 | UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8),
|
---|
85 | UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8),
|
---|
86 | UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3),
|
---|
87 | UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
|
---|
88 | UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b),
|
---|
89 | UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178),
|
---|
90 | UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b),
|
---|
91 | UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c),
|
---|
92 | UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817),
|
---|
93 | };
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 | RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx)
|
---|
98 | {
|
---|
99 | pCtx->AltPrivate.cbMessage.s.Lo = 0;
|
---|
100 | pCtx->AltPrivate.cbMessage.s.Hi = 0;
|
---|
101 | pCtx->AltPrivate.auH[0] = UINT64_C(0x6a09e667f3bcc908);
|
---|
102 | pCtx->AltPrivate.auH[1] = UINT64_C(0xbb67ae8584caa73b);
|
---|
103 | pCtx->AltPrivate.auH[2] = UINT64_C(0x3c6ef372fe94f82b);
|
---|
104 | pCtx->AltPrivate.auH[3] = UINT64_C(0xa54ff53a5f1d36f1);
|
---|
105 | pCtx->AltPrivate.auH[4] = UINT64_C(0x510e527fade682d1);
|
---|
106 | pCtx->AltPrivate.auH[5] = UINT64_C(0x9b05688c2b3e6c1f);
|
---|
107 | pCtx->AltPrivate.auH[6] = UINT64_C(0x1f83d9abfb41bd6b);
|
---|
108 | pCtx->AltPrivate.auH[7] = UINT64_C(0x5be0cd19137e2179);
|
---|
109 | }
|
---|
110 | RT_EXPORT_SYMBOL(RTSha512Init);
|
---|
111 |
|
---|
112 |
|
---|
113 | /** Function 4.8. */
|
---|
114 | DECL_FORCE_INLINE(uint64_t) rtSha512Ch(uint64_t uX, uint64_t uY, uint64_t uZ)
|
---|
115 | {
|
---|
116 | uint64_t uResult = uX & uY;
|
---|
117 | uResult ^= ~uX & uZ;
|
---|
118 | return uResult;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /** Function 4.9. */
|
---|
123 | DECL_FORCE_INLINE(uint64_t) rtSha512Maj(uint64_t uX, uint64_t uY, uint64_t uZ)
|
---|
124 | {
|
---|
125 | uint64_t uResult = uX & uY;
|
---|
126 | uResult ^= uX & uZ;
|
---|
127 | uResult ^= uY & uZ;
|
---|
128 | return uResult;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** Function 4.10. */
|
---|
133 | DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma0(uint64_t uX)
|
---|
134 | {
|
---|
135 | uint64_t uResult = uX = ASMRotateRightU64(uX, 28);
|
---|
136 | uX = ASMRotateRightU64(uX, 34 - 28);
|
---|
137 | uResult ^= uX;
|
---|
138 | uX = ASMRotateRightU64(uX, 39 - 34);
|
---|
139 | uResult ^= uX;
|
---|
140 | return uResult;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /** Function 4.11. */
|
---|
145 | DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma1(uint64_t uX)
|
---|
146 | {
|
---|
147 | uint64_t uResult = uX = ASMRotateRightU64(uX, 14);
|
---|
148 | uX = ASMRotateRightU64(uX, 18 - 14);
|
---|
149 | uResult ^= uX;
|
---|
150 | uX = ASMRotateRightU64(uX, 41 - 18);
|
---|
151 | uResult ^= uX;
|
---|
152 | return uResult;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /** Function 4.12. */
|
---|
157 | DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma0(uint64_t uX)
|
---|
158 | {
|
---|
159 | uint64_t uResult = uX >> 7;
|
---|
160 | uX = ASMRotateRightU64(uX, 1);
|
---|
161 | uResult ^= uX;
|
---|
162 | uX = ASMRotateRightU64(uX, 8 - 1);
|
---|
163 | uResult ^= uX;
|
---|
164 | return uResult;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /** Function 4.13. */
|
---|
169 | DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma1(uint64_t uX)
|
---|
170 | {
|
---|
171 | uint64_t uResult = uX >> 6;
|
---|
172 | uX = ASMRotateRightU64(uX, 19);
|
---|
173 | uResult ^= uX;
|
---|
174 | uX = ASMRotateRightU64(uX, 61 - 19);
|
---|
175 | uResult ^= uX;
|
---|
176 | return uResult;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Initializes the auW array from the specfied input block.
|
---|
182 | *
|
---|
183 | * @param pCtx The SHA-512 context.
|
---|
184 | * @param pbBlock The block. Must be 32-bit aligned.
|
---|
185 | */
|
---|
186 | DECLINLINE(void) rtSha512BlockInit(PRTSHA512CONTEXT pCtx, uint8_t const *pbBlock)
|
---|
187 | {
|
---|
188 | uint64_t const *pu32Block = (uint64_t const *)pbBlock;
|
---|
189 | Assert(!((uintptr_t)pu32Block & 3));
|
---|
190 |
|
---|
191 | unsigned iWord;
|
---|
192 | for (iWord = 0; iWord < 16; iWord++)
|
---|
193 | pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pu32Block[iWord]);
|
---|
194 |
|
---|
195 | for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
|
---|
196 | {
|
---|
197 | uint64_t u64 = rtSha512SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
|
---|
198 | u64 += rtSha512SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
|
---|
199 | u64 += pCtx->AltPrivate.auW[iWord - 7];
|
---|
200 | u64 += pCtx->AltPrivate.auW[iWord - 16];
|
---|
201 | pCtx->AltPrivate.auW[iWord] = u64;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Initializes the auW array from data buffered in the first part of the array.
|
---|
208 | *
|
---|
209 | * @param pCtx The SHA-512 context.
|
---|
210 | */
|
---|
211 | DECLINLINE(void) rtSha512BlockInitBuffered(PRTSHA512CONTEXT pCtx)
|
---|
212 | {
|
---|
213 | unsigned iWord;
|
---|
214 | for (iWord = 0; iWord < 16; iWord++)
|
---|
215 | pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pCtx->AltPrivate.auW[iWord]);
|
---|
216 |
|
---|
217 | for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
|
---|
218 | {
|
---|
219 | uint64_t u64 = rtSha512SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
|
---|
220 | u64 += rtSha512SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
|
---|
221 | u64 += pCtx->AltPrivate.auW[iWord - 7];
|
---|
222 | u64 += pCtx->AltPrivate.auW[iWord - 16];
|
---|
223 | pCtx->AltPrivate.auW[iWord] = u64;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Process the current block.
|
---|
230 | *
|
---|
231 | * Requires one of the rtSha512BlockInit functions to be called first.
|
---|
232 | *
|
---|
233 | * @param pCtx The SHA-512 context.
|
---|
234 | */
|
---|
235 | static void rtSha512BlockProcess(PRTSHA512CONTEXT pCtx)
|
---|
236 | {
|
---|
237 | uint64_t uA = pCtx->AltPrivate.auH[0];
|
---|
238 | uint64_t uB = pCtx->AltPrivate.auH[1];
|
---|
239 | uint64_t uC = pCtx->AltPrivate.auH[2];
|
---|
240 | uint64_t uD = pCtx->AltPrivate.auH[3];
|
---|
241 | uint64_t uE = pCtx->AltPrivate.auH[4];
|
---|
242 | uint64_t uF = pCtx->AltPrivate.auH[5];
|
---|
243 | uint64_t uG = pCtx->AltPrivate.auH[6];
|
---|
244 | uint64_t uH = pCtx->AltPrivate.auH[7];
|
---|
245 |
|
---|
246 | for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
|
---|
247 | {
|
---|
248 | uint64_t uT1 = uH;
|
---|
249 | uT1 += rtSha512CapitalSigma1(uE);
|
---|
250 | uT1 += rtSha512Ch(uE, uF, uG);
|
---|
251 | uT1 += g_auKs[iWord];
|
---|
252 | uT1 += pCtx->AltPrivate.auW[iWord];
|
---|
253 |
|
---|
254 | uint64_t uT2 = rtSha512CapitalSigma0(uA);
|
---|
255 | uT2 += rtSha512Maj(uA, uB, uC);
|
---|
256 |
|
---|
257 | uH = uG;
|
---|
258 | uG = uF;
|
---|
259 | uF = uE;
|
---|
260 | uE = uD + uT1;
|
---|
261 | uD = uC;
|
---|
262 | uC = uB;
|
---|
263 | uB = uA;
|
---|
264 | uA = uT1 + uT2;
|
---|
265 | }
|
---|
266 |
|
---|
267 | pCtx->AltPrivate.auH[0] += uA;
|
---|
268 | pCtx->AltPrivate.auH[1] += uB;
|
---|
269 | pCtx->AltPrivate.auH[2] += uC;
|
---|
270 | pCtx->AltPrivate.auH[3] += uD;
|
---|
271 | pCtx->AltPrivate.auH[4] += uE;
|
---|
272 | pCtx->AltPrivate.auH[5] += uF;
|
---|
273 | pCtx->AltPrivate.auH[6] += uG;
|
---|
274 | pCtx->AltPrivate.auH[7] += uH;
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
|
---|
279 | {
|
---|
280 | Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
|
---|
281 | uint8_t const *pbBuf = (uint8_t const *)pvBuf;
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Deal with buffered bytes first.
|
---|
285 | */
|
---|
286 | size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
|
---|
287 | if (cbBuffered)
|
---|
288 | {
|
---|
289 | size_t cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
|
---|
290 | if (cbBuf >= cbMissing)
|
---|
291 | {
|
---|
292 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
|
---|
293 | pCtx->AltPrivate.cbMessage.s.Lo += cbMissing;
|
---|
294 | if (!pCtx->AltPrivate.cbMessage.s.Lo)
|
---|
295 | pCtx->AltPrivate.cbMessage.s.Hi++;
|
---|
296 | pbBuf += cbMissing;
|
---|
297 | cbBuf -= cbMissing;
|
---|
298 |
|
---|
299 | rtSha512BlockInitBuffered(pCtx);
|
---|
300 | rtSha512BlockProcess(pCtx);
|
---|
301 | }
|
---|
302 | else
|
---|
303 | {
|
---|
304 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
|
---|
305 | pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
|
---|
306 | return;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (!((uintptr_t)pbBuf & 3))
|
---|
311 | {
|
---|
312 | /*
|
---|
313 | * Process full blocks directly from the input buffer.
|
---|
314 | */
|
---|
315 | while (cbBuf >= RTSHA512_BLOCK_SIZE)
|
---|
316 | {
|
---|
317 | rtSha512BlockInit(pCtx, pbBuf);
|
---|
318 | rtSha512BlockProcess(pCtx);
|
---|
319 |
|
---|
320 | pCtx->AltPrivate.cbMessage.s.Lo += RTSHA512_BLOCK_SIZE;
|
---|
321 | if (!pCtx->AltPrivate.cbMessage.s.Lo)
|
---|
322 | pCtx->AltPrivate.cbMessage.s.Hi++;
|
---|
323 | pbBuf += RTSHA512_BLOCK_SIZE;
|
---|
324 | cbBuf -= RTSHA512_BLOCK_SIZE;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | else
|
---|
328 | {
|
---|
329 | /*
|
---|
330 | * Unaligned input, so buffer it.
|
---|
331 | */
|
---|
332 | while (cbBuf >= RTSHA512_BLOCK_SIZE)
|
---|
333 | {
|
---|
334 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA512_BLOCK_SIZE);
|
---|
335 | rtSha512BlockInitBuffered(pCtx);
|
---|
336 | rtSha512BlockProcess(pCtx);
|
---|
337 |
|
---|
338 | pCtx->AltPrivate.cbMessage.s.Lo += RTSHA512_BLOCK_SIZE;
|
---|
339 | if (!pCtx->AltPrivate.cbMessage.s.Lo)
|
---|
340 | pCtx->AltPrivate.cbMessage.s.Hi++;
|
---|
341 | pbBuf += RTSHA512_BLOCK_SIZE;
|
---|
342 | cbBuf -= RTSHA512_BLOCK_SIZE;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Stash any remaining bytes into the context buffer.
|
---|
348 | */
|
---|
349 | if (cbBuf > 0)
|
---|
350 | {
|
---|
351 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
|
---|
352 | pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
|
---|
353 | if (!pCtx->AltPrivate.cbMessage.s.Lo)
|
---|
354 | pCtx->AltPrivate.cbMessage.s.Hi++;
|
---|
355 | }
|
---|
356 | }
|
---|
357 | RT_EXPORT_SYMBOL(RTSha512Update);
|
---|
358 |
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Internal worker for RTSha512Final and RTSha384Final that finalizes the
|
---|
362 | * computation but does not copy out the hash value.
|
---|
363 | *
|
---|
364 | * @param pCtx The SHA-512 context.
|
---|
365 | */
|
---|
366 | static void rtSha512FinalInternal(PRTSHA512CONTEXT pCtx)
|
---|
367 | {
|
---|
368 | Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
|
---|
369 |
|
---|
370 | /*
|
---|
371 | * Complete the message by adding a single bit (0x80), padding till
|
---|
372 | * the next 448-bit boundrary, the add the message length.
|
---|
373 | */
|
---|
374 | RTUINT128U cMessageBits = pCtx->AltPrivate.cbMessage;
|
---|
375 | cMessageBits.s.Hi <<= 3;
|
---|
376 | cMessageBits.s.Hi |= cMessageBits.s.Lo >> 61;
|
---|
377 | cMessageBits.s.Lo <<= 3;
|
---|
378 |
|
---|
379 | unsigned cbMissing = RTSHA512_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U));
|
---|
380 | static uint8_t const s_abSingleBitAndSomePadding[20] =
|
---|
381 | { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
|
---|
382 | if (cbMissing < 1U + 16U)
|
---|
383 | /* Less than 64+16 bits left in the current block, force a new block. */
|
---|
384 | RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
|
---|
385 | else
|
---|
386 | RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, 1);
|
---|
387 |
|
---|
388 | unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
|
---|
389 | cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
|
---|
390 | Assert(cbMissing >= 16);
|
---|
391 | memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 16);
|
---|
392 |
|
---|
393 | pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits.s.Hi);
|
---|
394 | pCtx->AltPrivate.auW[15] = RT_H2BE_U64(cMessageBits.s.Lo);
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * Process the last buffered block constructed/completed above.
|
---|
398 | */
|
---|
399 | rtSha512BlockInitBuffered(pCtx);
|
---|
400 | rtSha512BlockProcess(pCtx);
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * Convert the byte order of the hash words and we're done.
|
---|
404 | */
|
---|
405 | pCtx->AltPrivate.auH[0] = RT_H2BE_U64(pCtx->AltPrivate.auH[0]);
|
---|
406 | pCtx->AltPrivate.auH[1] = RT_H2BE_U64(pCtx->AltPrivate.auH[1]);
|
---|
407 | pCtx->AltPrivate.auH[2] = RT_H2BE_U64(pCtx->AltPrivate.auH[2]);
|
---|
408 | pCtx->AltPrivate.auH[3] = RT_H2BE_U64(pCtx->AltPrivate.auH[3]);
|
---|
409 | pCtx->AltPrivate.auH[4] = RT_H2BE_U64(pCtx->AltPrivate.auH[4]);
|
---|
410 | pCtx->AltPrivate.auH[5] = RT_H2BE_U64(pCtx->AltPrivate.auH[5]);
|
---|
411 | pCtx->AltPrivate.auH[6] = RT_H2BE_U64(pCtx->AltPrivate.auH[6]);
|
---|
412 | pCtx->AltPrivate.auH[7] = RT_H2BE_U64(pCtx->AltPrivate.auH[7]);
|
---|
413 |
|
---|
414 | pCtx->AltPrivate.cbMessage.s.Lo = UINT64_MAX;
|
---|
415 | pCtx->AltPrivate.cbMessage.s.Hi = UINT64_MAX;
|
---|
416 | }
|
---|
417 | RT_EXPORT_SYMBOL(RTSha512Final);
|
---|
418 |
|
---|
419 |
|
---|
420 | RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE])
|
---|
421 | {
|
---|
422 | rtSha512FinalInternal(pCtx);
|
---|
423 | memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512_HASH_SIZE);
|
---|
424 | }
|
---|
425 | RT_EXPORT_SYMBOL(RTSha512Final);
|
---|
426 |
|
---|
427 |
|
---|
428 | RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE])
|
---|
429 | {
|
---|
430 | RTSHA512CONTEXT Ctx;
|
---|
431 | RTSha512Init(&Ctx);
|
---|
432 | RTSha512Update(&Ctx, pvBuf, cbBuf);
|
---|
433 | RTSha512Final(&Ctx, pabDigest);
|
---|
434 | }
|
---|
435 | RT_EXPORT_SYMBOL(RTSha512);
|
---|
436 |
|
---|
437 |
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * SHA-384 is just SHA-512 with different initial values an a truncated result.
|
---|
441 | */
|
---|
442 |
|
---|
443 | RTDECL(void) RTSha384Init(PRTSHA384CONTEXT pCtx)
|
---|
444 | {
|
---|
445 | pCtx->AltPrivate.cbMessage.s.Lo = 0;
|
---|
446 | pCtx->AltPrivate.cbMessage.s.Hi = 0;
|
---|
447 | pCtx->AltPrivate.auH[0] = UINT64_C(0xcbbb9d5dc1059ed8);
|
---|
448 | pCtx->AltPrivate.auH[1] = UINT64_C(0x629a292a367cd507);
|
---|
449 | pCtx->AltPrivate.auH[2] = UINT64_C(0x9159015a3070dd17);
|
---|
450 | pCtx->AltPrivate.auH[3] = UINT64_C(0x152fecd8f70e5939);
|
---|
451 | pCtx->AltPrivate.auH[4] = UINT64_C(0x67332667ffc00b31);
|
---|
452 | pCtx->AltPrivate.auH[5] = UINT64_C(0x8eb44a8768581511);
|
---|
453 | pCtx->AltPrivate.auH[6] = UINT64_C(0xdb0c2e0d64f98fa7);
|
---|
454 | pCtx->AltPrivate.auH[7] = UINT64_C(0x47b5481dbefa4fa4);
|
---|
455 | }
|
---|
456 | RT_EXPORT_SYMBOL(RTSha384Init);
|
---|
457 |
|
---|
458 |
|
---|
459 | RTDECL(void) RTSha384Update(PRTSHA384CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
|
---|
460 | {
|
---|
461 | RTSha512Update(pCtx, pvBuf, cbBuf);
|
---|
462 | }
|
---|
463 | RT_EXPORT_SYMBOL(RTSha384Update);
|
---|
464 |
|
---|
465 |
|
---|
466 | RTDECL(void) RTSha384Final(PRTSHA384CONTEXT pCtx, uint8_t pabDigest[RTSHA384_HASH_SIZE])
|
---|
467 | {
|
---|
468 | rtSha512FinalInternal(pCtx);
|
---|
469 | memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA384_HASH_SIZE);
|
---|
470 | }
|
---|
471 | RT_EXPORT_SYMBOL(RTSha384Final);
|
---|
472 |
|
---|
473 |
|
---|
474 | RTDECL(void) RTSha384(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA384_HASH_SIZE])
|
---|
475 | {
|
---|
476 | RTSHA384CONTEXT Ctx;
|
---|
477 | RTSha384Init(&Ctx);
|
---|
478 | RTSha384Update(&Ctx, pvBuf, cbBuf);
|
---|
479 | RTSha384Final(&Ctx, pabDigest);
|
---|
480 | }
|
---|
481 | RT_EXPORT_SYMBOL(RTSha384);
|
---|
482 |
|
---|