VirtualBox

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

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

clear the context after use, converted SHA-1 to use an array of H values.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
Line 
1/* $Id: alt-sha256.cpp 51861 2014-07-03 22:56:18Z 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
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. */
46typedef struct RTSHA256ALTPRIVATECTX
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 uint32_t auW[64];
53 /** The message length (in bytes). */
54 uint64_t cbMessage;
55 /** The 8 hash values. */
56 uint32_t auH[8];
57} RTSHA256ALTPRIVATECTX;
58
59#define RT_SHA256_PRIVATE_ALT_CONTEXT
60#include <iprt/sha.h>
61
62
63AssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, AltPrivate));
64AssertCompileMemberSize(RTSHA256ALTPRIVATECTX, auH, RTSHA256_HASH_SIZE);
65
66
67/*******************************************************************************
68* Global Variables *
69*******************************************************************************/
70/** The K constants */
71static uint32_t const g_auKs[] =
72{
73 UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
74 UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5),
75 UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
76 UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174),
77 UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
78 UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da),
79 UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
80 UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967),
81 UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
82 UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85),
83 UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
84 UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070),
85 UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
86 UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3),
87 UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
88 UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2),
89};
90
91
92
93RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx)
94{
95 pCtx->AltPrivate.cbMessage = 0;
96 pCtx->AltPrivate.auH[0] = UINT32_C(0x6a09e667);
97 pCtx->AltPrivate.auH[1] = UINT32_C(0xbb67ae85);
98 pCtx->AltPrivate.auH[2] = UINT32_C(0x3c6ef372);
99 pCtx->AltPrivate.auH[3] = UINT32_C(0xa54ff53a);
100 pCtx->AltPrivate.auH[4] = UINT32_C(0x510e527f);
101 pCtx->AltPrivate.auH[5] = UINT32_C(0x9b05688c);
102 pCtx->AltPrivate.auH[6] = UINT32_C(0x1f83d9ab);
103 pCtx->AltPrivate.auH[7] = UINT32_C(0x5be0cd19);
104}
105RT_EXPORT_SYMBOL(RTSha256Init);
106
107
108/** Function 4.2. */
109DECL_FORCE_INLINE(uint32_t) rtSha256Ch(uint32_t uX, uint32_t uY, uint32_t uZ)
110{
111 uint32_t uResult = uX & uY;
112 uResult ^= ~uX & uZ;
113 return uResult;
114}
115
116
117/** Function 4.3. */
118DECL_FORCE_INLINE(uint32_t) rtSha256Maj(uint32_t uX, uint32_t uY, uint32_t uZ)
119{
120 uint32_t uResult = uX & uY;
121 uResult ^= uX & uZ;
122 uResult ^= uY & uZ;
123 return uResult;
124}
125
126
127/** Function 4.4. */
128DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma0(uint32_t uX)
129{
130 uint32_t uResult = uX = ASMRotateRightU32(uX, 2);
131 uX = ASMRotateRightU32(uX, 13 - 2);
132 uResult ^= uX;
133 uX = ASMRotateRightU32(uX, 22 - 13);
134 uResult ^= uX;
135 return uResult;
136}
137
138
139/** Function 4.5. */
140DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma1(uint32_t uX)
141{
142 uint32_t uResult = uX = ASMRotateRightU32(uX, 6);
143 uX = ASMRotateRightU32(uX, 11 - 6);
144 uResult ^= uX;
145 uX = ASMRotateRightU32(uX, 25 - 11);
146 uResult ^= uX;
147 return uResult;
148}
149
150
151/** Function 4.6. */
152DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma0(uint32_t uX)
153{
154 uint32_t uResult = uX >> 3;
155 uX = ASMRotateRightU32(uX, 7);
156 uResult ^= uX;
157 uX = ASMRotateRightU32(uX, 18 - 7);
158 uResult ^= uX;
159 return uResult;
160}
161
162
163/** Function 4.7. */
164DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma1(uint32_t uX)
165{
166 uint32_t uResult = uX >> 10;
167 uX = ASMRotateRightU32(uX, 17);
168 uResult ^= uX;
169 uX = ASMRotateRightU32(uX, 19 - 17);
170 uResult ^= uX;
171 return uResult;
172}
173
174
175/**
176 * Initializes the auW array from the specfied input block.
177 *
178 * @param pCtx The SHA-256 context.
179 * @param pbBlock The block. Must be 32-bit aligned.
180 */
181DECLINLINE(void) rtSha256BlockInit(PRTSHA256CONTEXT pCtx, uint8_t const *pbBlock)
182{
183 uint32_t const *pu32Block = (uint32_t const *)pbBlock;
184 Assert(!((uintptr_t)pu32Block & 3));
185
186 unsigned iWord;
187 for (iWord = 0; iWord < 16; iWord++)
188 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]);
189
190 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
191 {
192 uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
193 u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
194 u32 += pCtx->AltPrivate.auW[iWord - 7];
195 u32 += pCtx->AltPrivate.auW[iWord - 16];
196 pCtx->AltPrivate.auW[iWord] = u32;
197 }
198}
199
200
201/**
202 * Initializes the auW array from data buffered in the first part of the array.
203 *
204 * @param pCtx The SHA-256 context.
205 */
206DECLINLINE(void) rtSha256BlockInitBuffered(PRTSHA256CONTEXT pCtx)
207{
208 unsigned iWord;
209 for (iWord = 0; iWord < 16; iWord++)
210 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]);
211
212 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
213 {
214 uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
215 u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
216 u32 += pCtx->AltPrivate.auW[iWord - 7];
217 u32 += pCtx->AltPrivate.auW[iWord - 16];
218 pCtx->AltPrivate.auW[iWord] = u32;
219 }
220}
221
222
223/**
224 * Process the current block.
225 *
226 * Requires one of the rtSha256BlockInit functions to be called first.
227 *
228 * @param pCtx The SHA-256 context.
229 */
230static void rtSha256BlockProcess(PRTSHA256CONTEXT pCtx)
231{
232 uint32_t uA = pCtx->AltPrivate.auH[0];
233 uint32_t uB = pCtx->AltPrivate.auH[1];
234 uint32_t uC = pCtx->AltPrivate.auH[2];
235 uint32_t uD = pCtx->AltPrivate.auH[3];
236 uint32_t uE = pCtx->AltPrivate.auH[4];
237 uint32_t uF = pCtx->AltPrivate.auH[5];
238 uint32_t uG = pCtx->AltPrivate.auH[6];
239 uint32_t uH = pCtx->AltPrivate.auH[7];
240
241 for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
242 {
243 uint32_t uT1 = uH;
244 uT1 += rtSha256CapitalSigma1(uE);
245 uT1 += rtSha256Ch(uE, uF, uG);
246 uT1 += g_auKs[iWord];
247 uT1 += pCtx->AltPrivate.auW[iWord];
248
249 uint32_t uT2 = rtSha256CapitalSigma0(uA);
250 uT2 += rtSha256Maj(uA, uB, uC);
251
252 uH = uG;
253 uG = uF;
254 uF = uE;
255 uE = uD + uT1;
256 uD = uC;
257 uC = uB;
258 uB = uA;
259 uA = uT1 + uT2;
260 }
261
262 pCtx->AltPrivate.auH[0] += uA;
263 pCtx->AltPrivate.auH[1] += uB;
264 pCtx->AltPrivate.auH[2] += uC;
265 pCtx->AltPrivate.auH[3] += uD;
266 pCtx->AltPrivate.auH[4] += uE;
267 pCtx->AltPrivate.auH[5] += uF;
268 pCtx->AltPrivate.auH[6] += uG;
269 pCtx->AltPrivate.auH[7] += uH;
270}
271
272
273RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
274{
275 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
276 uint8_t const *pbBuf = (uint8_t const *)pvBuf;
277
278 /*
279 * Deal with buffered bytes first.
280 */
281 size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
282 if (cbBuffered)
283 {
284 size_t cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
285 if (cbBuf >= cbMissing)
286 {
287 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
288 pCtx->AltPrivate.cbMessage += cbMissing;
289 pbBuf += cbMissing;
290 cbBuf -= cbMissing;
291
292 rtSha256BlockInitBuffered(pCtx);
293 rtSha256BlockProcess(pCtx);
294 }
295 else
296 {
297 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
298 pCtx->AltPrivate.cbMessage += cbBuf;
299 return;
300 }
301 }
302
303 if (!((uintptr_t)pbBuf & 3))
304 {
305 /*
306 * Process full blocks directly from the input buffer.
307 */
308 while (cbBuf >= RTSHA256_BLOCK_SIZE)
309 {
310 rtSha256BlockInit(pCtx, pbBuf);
311 rtSha256BlockProcess(pCtx);
312
313 pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE;
314 pbBuf += RTSHA256_BLOCK_SIZE;
315 cbBuf -= RTSHA256_BLOCK_SIZE;
316 }
317 }
318 else
319 {
320 /*
321 * Unaligned input, so buffer it.
322 */
323 while (cbBuf >= RTSHA256_BLOCK_SIZE)
324 {
325 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA256_BLOCK_SIZE);
326 rtSha256BlockInitBuffered(pCtx);
327 rtSha256BlockProcess(pCtx);
328
329 pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE;
330 pbBuf += RTSHA256_BLOCK_SIZE;
331 cbBuf -= RTSHA256_BLOCK_SIZE;
332 }
333 }
334
335 /*
336 * Stash any remaining bytes into the context buffer.
337 */
338 if (cbBuf > 0)
339 {
340 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
341 pCtx->AltPrivate.cbMessage += cbBuf;
342 }
343}
344RT_EXPORT_SYMBOL(RTSha256Update);
345
346
347/**
348 * Internal worker for RTSha256Final and RTSha224Final that finalizes the
349 * computation but does not copy out the hash value.
350 *
351 * @param pCtx The SHA-256 context.
352 */
353static void rtSha256FinalInternal(PRTSHA256CONTEXT pCtx)
354{
355 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
356
357 /*
358 * Complete the message by adding a single bit (0x80), padding till
359 * the next 448-bit boundrary, the add the message length.
360 */
361 uint64_t const cMessageBits = pCtx->AltPrivate.cbMessage * 8;
362
363 unsigned cbMissing = RTSHA256_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U));
364 static uint8_t const s_abSingleBitAndSomePadding[12] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
365 if (cbMissing < 1U + 8U)
366 /* Less than 64+8 bits left in the current block, force a new block. */
367 RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
368 else
369 RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, 1);
370
371 unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
372 cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
373 Assert(cbMissing >= 8);
374 memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 8);
375
376 *(uint64_t *)&pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits);
377
378 /*
379 * Process the last buffered block constructed/completed above.
380 */
381 rtSha256BlockInitBuffered(pCtx);
382 rtSha256BlockProcess(pCtx);
383
384 /*
385 * Convert the byte order of the hash words and we're done.
386 */
387 pCtx->AltPrivate.auH[0] = RT_H2BE_U32(pCtx->AltPrivate.auH[0]);
388 pCtx->AltPrivate.auH[1] = RT_H2BE_U32(pCtx->AltPrivate.auH[1]);
389 pCtx->AltPrivate.auH[2] = RT_H2BE_U32(pCtx->AltPrivate.auH[2]);
390 pCtx->AltPrivate.auH[3] = RT_H2BE_U32(pCtx->AltPrivate.auH[3]);
391 pCtx->AltPrivate.auH[4] = RT_H2BE_U32(pCtx->AltPrivate.auH[4]);
392 pCtx->AltPrivate.auH[5] = RT_H2BE_U32(pCtx->AltPrivate.auH[5]);
393 pCtx->AltPrivate.auH[6] = RT_H2BE_U32(pCtx->AltPrivate.auH[6]);
394 pCtx->AltPrivate.auH[7] = RT_H2BE_U32(pCtx->AltPrivate.auH[7]);
395
396 RT_ZERO(pCtx->AltPrivate.auW);
397 pCtx->AltPrivate.cbMessage = UINT64_MAX;
398}
399RT_EXPORT_SYMBOL(RTSha256Final);
400
401
402RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE])
403{
404 rtSha256FinalInternal(pCtx);
405 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA256_HASH_SIZE);
406 RT_ZERO(pCtx->AltPrivate.auH);
407}
408RT_EXPORT_SYMBOL(RTSha256Final);
409
410
411RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE])
412{
413 RTSHA256CONTEXT Ctx;
414 RTSha256Init(&Ctx);
415 RTSha256Update(&Ctx, pvBuf, cbBuf);
416 RTSha256Final(&Ctx, pabDigest);
417}
418RT_EXPORT_SYMBOL(RTSha256);
419
420
421
422/*
423 * SHA-224 is just SHA-256 with different initial values an a truncated result.
424 */
425
426RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx)
427{
428 pCtx->AltPrivate.cbMessage = 0;
429 pCtx->AltPrivate.auH[0] = UINT32_C(0xc1059ed8);
430 pCtx->AltPrivate.auH[1] = UINT32_C(0x367cd507);
431 pCtx->AltPrivate.auH[2] = UINT32_C(0x3070dd17);
432 pCtx->AltPrivate.auH[3] = UINT32_C(0xf70e5939);
433 pCtx->AltPrivate.auH[4] = UINT32_C(0xffc00b31);
434 pCtx->AltPrivate.auH[5] = UINT32_C(0x68581511);
435 pCtx->AltPrivate.auH[6] = UINT32_C(0x64f98fa7);
436 pCtx->AltPrivate.auH[7] = UINT32_C(0xbefa4fa4);
437}
438RT_EXPORT_SYMBOL(RTSha224Init);
439
440
441RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
442{
443 RTSha256Update(pCtx, pvBuf, cbBuf);
444}
445RT_EXPORT_SYMBOL(RTSha224Update);
446
447
448RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE])
449{
450 rtSha256FinalInternal(pCtx);
451 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA224_HASH_SIZE);
452 RT_ZERO(pCtx->AltPrivate.auH);
453}
454RT_EXPORT_SYMBOL(RTSha224Final);
455
456
457RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE])
458{
459 RTSHA224CONTEXT Ctx;
460 RTSha224Init(&Ctx);
461 RTSha224Update(&Ctx, pvBuf, cbBuf);
462 RTSha224Final(&Ctx, pabDigest);
463}
464RT_EXPORT_SYMBOL(RTSha224);
465
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