VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/alt-sha256.cpp@ 51883

Last change on this file since 51883 was 51883, checked in by vboxsync, 10 years ago

alt-sha512: Applied the optimizations from alt-sha256 and alt-sha1, gaining 15-20.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.1 KB
Line 
1/* $Id: alt-sha256.cpp 51883 2014-07-06 13:59:04Z vboxsync $ */
2/** @file
3 * IPRT - SHA-256 and SHA-224 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-256 block size (in bytes). */
32#define RTSHA256_BLOCK_SIZE 64U
33
34/** Enables the unrolled code. */
35#define RTSHA256_UNROLLED 1
36
37
38/*******************************************************************************
39* Header Files *
40*******************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/types.h>
43#include <iprt/assert.h>
44#include <iprt/asm.h>
45#include <iprt/string.h>
46
47
48/** Our private context structure. */
49typedef struct RTSHA256ALTPRIVATECTX
50{
51 /** The W array.
52 * Buffering happens in the first 16 words, converted from big endian to host
53 * endian immediately before processing. The amount of buffered data is kept
54 * in the 6 least significant bits of cbMessage. */
55 uint32_t auW[64];
56 /** The message length (in bytes). */
57 uint64_t cbMessage;
58 /** The 8 hash values. */
59 uint32_t auH[8];
60} RTSHA256ALTPRIVATECTX;
61
62#define RT_SHA256_PRIVATE_ALT_CONTEXT
63#include <iprt/sha.h>
64
65
66AssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, AltPrivate));
67AssertCompileMemberSize(RTSHA256ALTPRIVATECTX, auH, RTSHA256_HASH_SIZE);
68
69
70/*******************************************************************************
71* Global Variables *
72*******************************************************************************/
73#ifndef RTSHA256_UNROLLED
74/** The K constants */
75static uint32_t const g_auKs[] =
76{
77 UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
78 UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5),
79 UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
80 UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174),
81 UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
82 UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da),
83 UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
84 UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967),
85 UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
86 UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85),
87 UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
88 UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070),
89 UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
90 UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3),
91 UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
92 UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2),
93};
94#endif /* !RTSHA256_UNROLLED */
95
96
97
98RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx)
99{
100 pCtx->AltPrivate.cbMessage = 0;
101 pCtx->AltPrivate.auH[0] = UINT32_C(0x6a09e667);
102 pCtx->AltPrivate.auH[1] = UINT32_C(0xbb67ae85);
103 pCtx->AltPrivate.auH[2] = UINT32_C(0x3c6ef372);
104 pCtx->AltPrivate.auH[3] = UINT32_C(0xa54ff53a);
105 pCtx->AltPrivate.auH[4] = UINT32_C(0x510e527f);
106 pCtx->AltPrivate.auH[5] = UINT32_C(0x9b05688c);
107 pCtx->AltPrivate.auH[6] = UINT32_C(0x1f83d9ab);
108 pCtx->AltPrivate.auH[7] = UINT32_C(0x5be0cd19);
109}
110RT_EXPORT_SYMBOL(RTSha256Init);
111
112
113/** Function 4.2. */
114DECL_FORCE_INLINE(uint32_t) rtSha256Ch(uint32_t uX, uint32_t uY, uint32_t uZ)
115{
116#if 1
117 /* Optimization that saves one operation and probably a temporary variable. */
118 uint32_t uResult = uY;
119 uResult ^= uZ;
120 uResult &= uX;
121 uResult ^= uZ;
122 return uResult;
123#else
124 /* The original. */
125 uint32_t uResult = uX & uY;
126 uResult ^= ~uX & uZ;
127 return uResult;
128#endif
129}
130
131
132/** Function 4.3. */
133DECL_FORCE_INLINE(uint32_t) rtSha256Maj(uint32_t uX, uint32_t uY, uint32_t uZ)
134{
135#if 1
136 /* Optimization that save one operation and probably a temporary variable. */
137 uint32_t uResult = uY;
138 uResult ^= uZ;
139 uResult &= uX;
140 uResult ^= uY & uZ;
141 return uResult;
142#else
143 /* The original. */
144 uint32_t uResult = uX & uY;
145 uResult ^= uX & uZ;
146 uResult ^= uY & uZ;
147 return uResult;
148#endif
149}
150
151
152/** Function 4.4. */
153DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma0(uint32_t uX)
154{
155 uint32_t uResult = uX = ASMRotateRightU32(uX, 2);
156 uX = ASMRotateRightU32(uX, 13 - 2);
157 uResult ^= uX;
158 uX = ASMRotateRightU32(uX, 22 - 13);
159 uResult ^= uX;
160 return uResult;
161}
162
163
164/** Function 4.5. */
165DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma1(uint32_t uX)
166{
167 uint32_t uResult = uX = ASMRotateRightU32(uX, 6);
168 uX = ASMRotateRightU32(uX, 11 - 6);
169 uResult ^= uX;
170 uX = ASMRotateRightU32(uX, 25 - 11);
171 uResult ^= uX;
172 return uResult;
173}
174
175
176/** Function 4.6. */
177DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma0(uint32_t uX)
178{
179 uint32_t uResult = uX >> 3;
180 uX = ASMRotateRightU32(uX, 7);
181 uResult ^= uX;
182 uX = ASMRotateRightU32(uX, 18 - 7);
183 uResult ^= uX;
184 return uResult;
185}
186
187
188/** Function 4.7. */
189DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma1(uint32_t uX)
190{
191 uint32_t uResult = uX >> 10;
192 uX = ASMRotateRightU32(uX, 17);
193 uResult ^= uX;
194 uX = ASMRotateRightU32(uX, 19 - 17);
195 uResult ^= uX;
196 return uResult;
197}
198
199
200/**
201 * Initializes the auW array from the specfied input block.
202 *
203 * @param pCtx The SHA-256 context.
204 * @param pbBlock The block. Must be 32-bit aligned.
205 */
206DECLINLINE(void) rtSha256BlockInit(PRTSHA256CONTEXT pCtx, uint8_t const *pbBlock)
207{
208#ifdef RTSHA256_UNROLLED
209 uint32_t const *puSrc = (uint32_t const *)pbBlock;
210 uint32_t *puW = &pCtx->AltPrivate.auW[0];
211 Assert(!((uintptr_t)puSrc & 3));
212 Assert(!((uintptr_t)puW & 3));
213
214 /* Copy and byte-swap the block. Initializing the rest of the Ws are done
215 in the processing loop. */
216# ifdef RT_LITTLE_ENDIAN
217 *puW++ = ASMByteSwapU32(*puSrc++);
218 *puW++ = ASMByteSwapU32(*puSrc++);
219 *puW++ = ASMByteSwapU32(*puSrc++);
220 *puW++ = ASMByteSwapU32(*puSrc++);
221
222 *puW++ = ASMByteSwapU32(*puSrc++);
223 *puW++ = ASMByteSwapU32(*puSrc++);
224 *puW++ = ASMByteSwapU32(*puSrc++);
225 *puW++ = ASMByteSwapU32(*puSrc++);
226
227 *puW++ = ASMByteSwapU32(*puSrc++);
228 *puW++ = ASMByteSwapU32(*puSrc++);
229 *puW++ = ASMByteSwapU32(*puSrc++);
230 *puW++ = ASMByteSwapU32(*puSrc++);
231
232 *puW++ = ASMByteSwapU32(*puSrc++);
233 *puW++ = ASMByteSwapU32(*puSrc++);
234 *puW++ = ASMByteSwapU32(*puSrc++);
235 *puW++ = ASMByteSwapU32(*puSrc++);
236# else
237 memcpy(puW, puSrc, RTSHA256_BLOCK_SIZE);
238# endif
239
240#else /* !RTSHA256_UNROLLED */
241 uint32_t const *pu32Block = (uint32_t const *)pbBlock;
242 Assert(!((uintptr_t)pu32Block & 3));
243
244 unsigned iWord;
245 for (iWord = 0; iWord < 16; iWord++)
246 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]);
247
248 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
249 {
250 uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
251 u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
252 u32 += pCtx->AltPrivate.auW[iWord - 7];
253 u32 += pCtx->AltPrivate.auW[iWord - 16];
254 pCtx->AltPrivate.auW[iWord] = u32;
255 }
256#endif /* !RTSHA256_UNROLLED */
257}
258
259
260/**
261 * Initializes the auW array from data buffered in the first part of the array.
262 *
263 * @param pCtx The SHA-256 context.
264 */
265DECLINLINE(void) rtSha256BlockInitBuffered(PRTSHA256CONTEXT pCtx)
266{
267#ifdef RTSHA256_UNROLLED
268 uint32_t *puW = &pCtx->AltPrivate.auW[0];
269 Assert(!((uintptr_t)puW & 3));
270
271 /* Do the byte swap if necessary. Initializing the rest of the Ws are done
272 in the processing loop. */
273# ifdef RT_LITTLE_ENDIAN
274 *puW = ASMByteSwapU32(*puW); puW++;
275 *puW = ASMByteSwapU32(*puW); puW++;
276 *puW = ASMByteSwapU32(*puW); puW++;
277 *puW = ASMByteSwapU32(*puW); puW++;
278
279 *puW = ASMByteSwapU32(*puW); puW++;
280 *puW = ASMByteSwapU32(*puW); puW++;
281 *puW = ASMByteSwapU32(*puW); puW++;
282 *puW = ASMByteSwapU32(*puW); puW++;
283
284 *puW = ASMByteSwapU32(*puW); puW++;
285 *puW = ASMByteSwapU32(*puW); puW++;
286 *puW = ASMByteSwapU32(*puW); puW++;
287 *puW = ASMByteSwapU32(*puW); puW++;
288
289 *puW = ASMByteSwapU32(*puW); puW++;
290 *puW = ASMByteSwapU32(*puW); puW++;
291 *puW = ASMByteSwapU32(*puW); puW++;
292 *puW = ASMByteSwapU32(*puW); puW++;
293# endif
294
295#else /* !RTSHA256_UNROLLED */
296 unsigned iWord;
297 for (iWord = 0; iWord < 16; iWord++)
298 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]);
299
300 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
301 {
302 uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
303 u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
304 u32 += pCtx->AltPrivate.auW[iWord - 7];
305 u32 += pCtx->AltPrivate.auW[iWord - 16];
306 pCtx->AltPrivate.auW[iWord] = u32;
307 }
308#endif /* !RTSHA256_UNROLLED */
309}
310
311
312/**
313 * Process the current block.
314 *
315 * Requires one of the rtSha256BlockInit functions to be called first.
316 *
317 * @param pCtx The SHA-256 context.
318 */
319static void rtSha256BlockProcess(PRTSHA256CONTEXT pCtx)
320{
321 uint32_t uA = pCtx->AltPrivate.auH[0];
322 uint32_t uB = pCtx->AltPrivate.auH[1];
323 uint32_t uC = pCtx->AltPrivate.auH[2];
324 uint32_t uD = pCtx->AltPrivate.auH[3];
325 uint32_t uE = pCtx->AltPrivate.auH[4];
326 uint32_t uF = pCtx->AltPrivate.auH[5];
327 uint32_t uG = pCtx->AltPrivate.auH[6];
328 uint32_t uH = pCtx->AltPrivate.auH[7];
329
330#ifdef RTSHA256_UNROLLED
331 uint32_t *puW = &pCtx->AltPrivate.auW[0];
332# define RTSHA256_BODY(a_iWord, a_uK, a_uA, a_uB, a_uC, a_uD, a_uE, a_uF, a_uG, a_uH) \
333 do { \
334 if ((a_iWord) < 16) \
335 a_uH += *puW++; \
336 else \
337 { \
338 uint32_t u32 = puW[-16]; \
339 u32 += rtSha256SmallSigma0(puW[-15]); \
340 u32 += puW[-7]; \
341 u32 += rtSha256SmallSigma1(puW[-2]); \
342 if (a_iWord < 64-2) *puW++ = u32; else puW++; \
343 a_uH += u32; \
344 } \
345 \
346 a_uH += rtSha256CapitalSigma1(a_uE); \
347 a_uH += a_uK; \
348 a_uH += rtSha256Ch(a_uE, a_uF, a_uG); \
349 a_uD += a_uH; \
350 \
351 a_uH += rtSha256CapitalSigma0(a_uA); \
352 a_uH += rtSha256Maj(a_uA, a_uB, a_uC); \
353 } while (0)
354# define RTSHA256_EIGHT(a_uK0, a_uK1, a_uK2, a_uK3, a_uK4, a_uK5, a_uK6, a_uK7, a_iFirst) \
355 do { \
356 RTSHA256_BODY(a_iFirst + 0, a_uK0, uA, uB, uC, uD, uE, uF, uG, uH); \
357 RTSHA256_BODY(a_iFirst + 1, a_uK1, uH, uA, uB, uC, uD, uE, uF, uG); \
358 RTSHA256_BODY(a_iFirst + 2, a_uK2, uG, uH, uA, uB, uC, uD, uE, uF); \
359 RTSHA256_BODY(a_iFirst + 3, a_uK3, uF, uG, uH, uA, uB, uC, uD, uE); \
360 RTSHA256_BODY(a_iFirst + 4, a_uK4, uE, uF, uG, uH, uA, uB, uC, uD); \
361 RTSHA256_BODY(a_iFirst + 5, a_uK5, uD, uE, uF, uG, uH, uA, uB, uC); \
362 RTSHA256_BODY(a_iFirst + 6, a_uK6, uC, uD, uE, uF, uG, uH, uA, uB); \
363 RTSHA256_BODY(a_iFirst + 7, a_uK7, uB, uC, uD, uE, uF, uG, uH, uA); \
364 } while (0)
365 RTSHA256_EIGHT(UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
366 UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5), 0);
367 RTSHA256_EIGHT(UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
368 UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174), 8);
369 RTSHA256_EIGHT(UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
370 UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da), 16);
371 RTSHA256_EIGHT(UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
372 UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967), 24);
373 RTSHA256_EIGHT(UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
374 UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85), 32);
375 RTSHA256_EIGHT(UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
376 UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070), 40);
377 RTSHA256_EIGHT(UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
378 UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3), 48);
379 RTSHA256_EIGHT(UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
380 UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2), 56);
381
382#else /* !RTSHA256_UNROLLED */
383 for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
384 {
385 uint32_t uT1 = uH;
386 uT1 += rtSha256CapitalSigma1(uE);
387 uT1 += rtSha256Ch(uE, uF, uG);
388 uT1 += g_auKs[iWord];
389 uT1 += pCtx->AltPrivate.auW[iWord];
390
391 uint32_t uT2 = rtSha256CapitalSigma0(uA);
392 uT2 += rtSha256Maj(uA, uB, uC);
393
394 uH = uG;
395 uG = uF;
396 uF = uE;
397 uE = uD + uT1;
398 uD = uC;
399 uC = uB;
400 uB = uA;
401 uA = uT1 + uT2;
402 }
403#endif /* !RTSHA256_UNROLLED */
404
405 pCtx->AltPrivate.auH[0] += uA;
406 pCtx->AltPrivate.auH[1] += uB;
407 pCtx->AltPrivate.auH[2] += uC;
408 pCtx->AltPrivate.auH[3] += uD;
409 pCtx->AltPrivate.auH[4] += uE;
410 pCtx->AltPrivate.auH[5] += uF;
411 pCtx->AltPrivate.auH[6] += uG;
412 pCtx->AltPrivate.auH[7] += uH;
413}
414
415
416RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
417{
418 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
419 uint8_t const *pbBuf = (uint8_t const *)pvBuf;
420
421 /*
422 * Deal with buffered bytes first.
423 */
424 size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
425 if (cbBuffered)
426 {
427 size_t cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
428 if (cbBuf >= cbMissing)
429 {
430 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
431 pCtx->AltPrivate.cbMessage += cbMissing;
432 pbBuf += cbMissing;
433 cbBuf -= cbMissing;
434
435 rtSha256BlockInitBuffered(pCtx);
436 rtSha256BlockProcess(pCtx);
437 }
438 else
439 {
440 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
441 pCtx->AltPrivate.cbMessage += cbBuf;
442 return;
443 }
444 }
445
446 if (!((uintptr_t)pbBuf & 3))
447 {
448 /*
449 * Process full blocks directly from the input buffer.
450 */
451 while (cbBuf >= RTSHA256_BLOCK_SIZE)
452 {
453 rtSha256BlockInit(pCtx, pbBuf);
454 rtSha256BlockProcess(pCtx);
455
456 pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE;
457 pbBuf += RTSHA256_BLOCK_SIZE;
458 cbBuf -= RTSHA256_BLOCK_SIZE;
459 }
460 }
461 else
462 {
463 /*
464 * Unaligned input, so buffer it.
465 */
466 while (cbBuf >= RTSHA256_BLOCK_SIZE)
467 {
468 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA256_BLOCK_SIZE);
469 rtSha256BlockInitBuffered(pCtx);
470 rtSha256BlockProcess(pCtx);
471
472 pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE;
473 pbBuf += RTSHA256_BLOCK_SIZE;
474 cbBuf -= RTSHA256_BLOCK_SIZE;
475 }
476 }
477
478 /*
479 * Stash any remaining bytes into the context buffer.
480 */
481 if (cbBuf > 0)
482 {
483 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
484 pCtx->AltPrivate.cbMessage += cbBuf;
485 }
486}
487RT_EXPORT_SYMBOL(RTSha256Update);
488
489
490/**
491 * Internal worker for RTSha256Final and RTSha224Final that finalizes the
492 * computation but does not copy out the hash value.
493 *
494 * @param pCtx The SHA-256 context.
495 */
496static void rtSha256FinalInternal(PRTSHA256CONTEXT pCtx)
497{
498 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
499
500 /*
501 * Complete the message by adding a single bit (0x80), padding till
502 * the next 448-bit boundrary, the add the message length.
503 */
504 uint64_t const cMessageBits = pCtx->AltPrivate.cbMessage * 8;
505
506 unsigned cbMissing = RTSHA256_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U));
507 static uint8_t const s_abSingleBitAndSomePadding[12] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
508 if (cbMissing < 1U + 8U)
509 /* Less than 64+8 bits left in the current block, force a new block. */
510 RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
511 else
512 RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, 1);
513
514 unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
515 cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
516 Assert(cbMissing >= 8);
517 memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 8);
518
519 *(uint64_t *)&pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits);
520
521 /*
522 * Process the last buffered block constructed/completed above.
523 */
524 rtSha256BlockInitBuffered(pCtx);
525 rtSha256BlockProcess(pCtx);
526
527 /*
528 * Convert the byte order of the hash words and we're done.
529 */
530 pCtx->AltPrivate.auH[0] = RT_H2BE_U32(pCtx->AltPrivate.auH[0]);
531 pCtx->AltPrivate.auH[1] = RT_H2BE_U32(pCtx->AltPrivate.auH[1]);
532 pCtx->AltPrivate.auH[2] = RT_H2BE_U32(pCtx->AltPrivate.auH[2]);
533 pCtx->AltPrivate.auH[3] = RT_H2BE_U32(pCtx->AltPrivate.auH[3]);
534 pCtx->AltPrivate.auH[4] = RT_H2BE_U32(pCtx->AltPrivate.auH[4]);
535 pCtx->AltPrivate.auH[5] = RT_H2BE_U32(pCtx->AltPrivate.auH[5]);
536 pCtx->AltPrivate.auH[6] = RT_H2BE_U32(pCtx->AltPrivate.auH[6]);
537 pCtx->AltPrivate.auH[7] = RT_H2BE_U32(pCtx->AltPrivate.auH[7]);
538
539 RT_ZERO(pCtx->AltPrivate.auW);
540 pCtx->AltPrivate.cbMessage = UINT64_MAX;
541}
542RT_EXPORT_SYMBOL(RTSha256Final);
543
544
545RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE])
546{
547 rtSha256FinalInternal(pCtx);
548 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA256_HASH_SIZE);
549 RT_ZERO(pCtx->AltPrivate.auH);
550}
551RT_EXPORT_SYMBOL(RTSha256Final);
552
553
554RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE])
555{
556 RTSHA256CONTEXT Ctx;
557 RTSha256Init(&Ctx);
558 RTSha256Update(&Ctx, pvBuf, cbBuf);
559 RTSha256Final(&Ctx, pabDigest);
560}
561RT_EXPORT_SYMBOL(RTSha256);
562
563
564
565/*
566 * SHA-224 is just SHA-256 with different initial values an a truncated result.
567 */
568
569RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx)
570{
571 pCtx->AltPrivate.cbMessage = 0;
572 pCtx->AltPrivate.auH[0] = UINT32_C(0xc1059ed8);
573 pCtx->AltPrivate.auH[1] = UINT32_C(0x367cd507);
574 pCtx->AltPrivate.auH[2] = UINT32_C(0x3070dd17);
575 pCtx->AltPrivate.auH[3] = UINT32_C(0xf70e5939);
576 pCtx->AltPrivate.auH[4] = UINT32_C(0xffc00b31);
577 pCtx->AltPrivate.auH[5] = UINT32_C(0x68581511);
578 pCtx->AltPrivate.auH[6] = UINT32_C(0x64f98fa7);
579 pCtx->AltPrivate.auH[7] = UINT32_C(0xbefa4fa4);
580}
581RT_EXPORT_SYMBOL(RTSha224Init);
582
583
584RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
585{
586 RTSha256Update(pCtx, pvBuf, cbBuf);
587}
588RT_EXPORT_SYMBOL(RTSha224Update);
589
590
591RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE])
592{
593 rtSha256FinalInternal(pCtx);
594 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA224_HASH_SIZE);
595 RT_ZERO(pCtx->AltPrivate.auH);
596}
597RT_EXPORT_SYMBOL(RTSha224Final);
598
599
600RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE])
601{
602 RTSHA224CONTEXT Ctx;
603 RTSha224Init(&Ctx);
604 RTSha224Update(&Ctx, pvBuf, cbBuf);
605 RTSha224Final(&Ctx, pabDigest);
606}
607RT_EXPORT_SYMBOL(RTSha224);
608
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette