VirtualBox

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

Last change on this file since 51861 was 51861, checked in by vboxsync, 11 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: 10.7 KB
Line 
1/* $Id: alt-sha1.cpp 51861 2014-07-03 22:56:18Z vboxsync $ */
2/** @file
3 * IPRT - SHA-1 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-1 block size (in bytes). */
32#define RTSHA1_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 RTSHA1ALTPRIVATECTX
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[80];
53 /** The message length (in bytes). */
54 uint64_t cbMessage;
55
56 /** The 5 hash values. */
57 uint32_t auH[5];
58} RTSHA1ALTPRIVATECTX;
59
60#define RT_SHA1_PRIVATE_ALT_CONTEXT
61#include <iprt/sha.h>
62
63
64AssertCompile(RT_SIZEOFMEMB(RTSHA1CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA1CONTEXT, AltPrivate));
65AssertCompileMemberSize(RTSHA1ALTPRIVATECTX, auH, RTSHA1_HASH_SIZE);
66
67
68
69
70RTDECL(void) RTSha1Init(PRTSHA1CONTEXT pCtx)
71{
72 pCtx->AltPrivate.cbMessage = 0;
73 pCtx->AltPrivate.auH[0] = UINT32_C(0x67452301);
74 pCtx->AltPrivate.auH[1] = UINT32_C(0xefcdab89);
75 pCtx->AltPrivate.auH[2] = UINT32_C(0x98badcfe);
76 pCtx->AltPrivate.auH[3] = UINT32_C(0x10325476);
77 pCtx->AltPrivate.auH[4] = UINT32_C(0xc3d2e1f0);
78}
79RT_EXPORT_SYMBOL(RTSha1Init);
80
81
82/**
83 * Initializes the auW array from the specfied input block.
84 *
85 * @param pCtx The SHA1 context.
86 * @param pbBlock The block. Must be 32-bit aligned.
87 */
88DECLINLINE(void) rtSha1BlockInit(PRTSHA1CONTEXT pCtx, uint8_t const *pbBlock)
89{
90 uint32_t const *pu32Block = (uint32_t const *)pbBlock;
91 Assert(!((uintptr_t)pu32Block & 3));
92
93 unsigned iWord;
94 for (iWord = 0; iWord < 16; iWord++)
95 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]);
96
97 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
98 {
99 uint32_t u32 = pCtx->AltPrivate.auW[iWord - 16];
100 u32 ^= pCtx->AltPrivate.auW[iWord - 14];
101 u32 ^= pCtx->AltPrivate.auW[iWord - 8];
102 u32 ^= pCtx->AltPrivate.auW[iWord - 3];
103 pCtx->AltPrivate.auW[iWord] = ASMRotateLeftU32(u32, 1);
104 }
105}
106
107
108/**
109 * Initializes the auW array from data buffered in the first part of the array.
110 *
111 * @param pCtx The SHA1 context.
112 */
113DECLINLINE(void) rtSha1BlockInitBuffered(PRTSHA1CONTEXT pCtx)
114{
115 unsigned iWord;
116 for (iWord = 0; iWord < 16; iWord++)
117 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]);
118
119 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
120 {
121 uint32_t u32 = pCtx->AltPrivate.auW[iWord - 16];
122 u32 ^= pCtx->AltPrivate.auW[iWord - 14];
123 u32 ^= pCtx->AltPrivate.auW[iWord - 8];
124 u32 ^= pCtx->AltPrivate.auW[iWord - 3];
125 pCtx->AltPrivate.auW[iWord] = ASMRotateLeftU32(u32, 1);
126 }
127}
128
129
130/**
131 * Process the current block.
132 *
133 * Requires one of the rtSha1BlockInit functions to be called first.
134 *
135 * @param pCtx The SHA1 context.
136 */
137static void rtSha1BlockProcess(PRTSHA1CONTEXT pCtx)
138{
139 uint32_t uA = pCtx->AltPrivate.auH[0];
140 uint32_t uB = pCtx->AltPrivate.auH[1];
141 uint32_t uC = pCtx->AltPrivate.auH[2];
142 uint32_t uD = pCtx->AltPrivate.auH[3];
143 uint32_t uE = pCtx->AltPrivate.auH[4];
144
145#if 1
146 unsigned iWord = 0;
147# define TWENTY_ITERATIONS(a_iWordStop, a_uK, a_uExprBCD) \
148 for (; iWord < a_iWordStop; iWord++) \
149 { \
150 uint32_t uTemp = ASMRotateLeftU32(uA, 5); \
151 uTemp += (a_uExprBCD); \
152 uTemp += uE; \
153 uTemp += pCtx->AltPrivate.auW[iWord]; \
154 uTemp += (a_uK); \
155 \
156 uE = uD; \
157 uD = uC; \
158 uC = ASMRotateLeftU32(uB, 30); \
159 uB = uA; \
160 uA = uTemp; \
161 } do { } while (0)
162 TWENTY_ITERATIONS(20, UINT32_C(0x5a827999), (uB & uC) | (~uB & uD));
163 TWENTY_ITERATIONS(40, UINT32_C(0x6ed9eba1), uB ^ uC ^ uD);
164 TWENTY_ITERATIONS(60, UINT32_C(0x8f1bbcdc), (uB & uC) | (uB & uD) | (uC & uD));
165 TWENTY_ITERATIONS(80, UINT32_C(0xca62c1d6), uB ^ uC ^ uD);
166#else
167 for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
168 {
169 uint32_t uTemp = ASMRotateLeftU32(uA, 5);
170 uTemp += uE;
171 uTemp += pCtx->AltPrivate.auW[iWord];
172 if (iWord <= 19)
173 {
174 uTemp += (uB & uC) | (~uB & uD);
175 uTemp += UINT32_C(0x5a827999);
176 }
177 else if (iWord <= 39)
178 {
179 uTemp += uB ^ uC ^ uD;
180 uTemp += UINT32_C(0x6ed9eba1);
181 }
182 else if (iWord <= 59)
183 {
184 uTemp += (uB & uC) | (uB & uD) | (uC & uD);
185 uTemp += UINT32_C(0x8f1bbcdc);
186 }
187 else
188 {
189 uTemp += uB ^ uC ^ uD;
190 uTemp += UINT32_C(0xca62c1d6);
191 }
192
193 uE = uD;
194 uD = uC;
195 uC = ASMRotateLeftU32(uB, 30);
196 uB = uA;
197 uA = uTemp;
198 }
199#endif
200
201 pCtx->AltPrivate.auH[0] += uA;
202 pCtx->AltPrivate.auH[1] += uB;
203 pCtx->AltPrivate.auH[2] += uC;
204 pCtx->AltPrivate.auH[3] += uD;
205 pCtx->AltPrivate.auH[4] += uE;
206}
207
208
209RTDECL(void) RTSha1Update(PRTSHA1CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
210{
211 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 2);
212 uint8_t const *pbBuf = (uint8_t const *)pvBuf;
213
214 /*
215 * Deal with buffered bytes first.
216 */
217 size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA1_BLOCK_SIZE - 1U);
218 if (cbBuffered)
219 {
220 size_t cbMissing = RTSHA1_BLOCK_SIZE - cbBuffered;
221 if (cbBuf >= cbMissing)
222 {
223 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
224 pCtx->AltPrivate.cbMessage += cbMissing;
225 pbBuf += cbMissing;
226 cbBuf -= cbMissing;
227
228 rtSha1BlockInitBuffered(pCtx);
229 rtSha1BlockProcess(pCtx);
230 }
231 else
232 {
233 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
234 pCtx->AltPrivate.cbMessage += cbBuf;
235 return;
236 }
237 }
238
239 if (!((uintptr_t)pbBuf & 3))
240 {
241 /*
242 * Process full blocks directly from the input buffer.
243 */
244 while (cbBuf >= RTSHA1_BLOCK_SIZE)
245 {
246 rtSha1BlockInit(pCtx, pbBuf);
247 rtSha1BlockProcess(pCtx);
248
249 pCtx->AltPrivate.cbMessage += RTSHA1_BLOCK_SIZE;
250 pbBuf += RTSHA1_BLOCK_SIZE;
251 cbBuf -= RTSHA1_BLOCK_SIZE;
252 }
253 }
254 else
255 {
256 /*
257 * Unaligned input, so buffer it.
258 */
259 while (cbBuf >= RTSHA1_BLOCK_SIZE)
260 {
261 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA1_BLOCK_SIZE);
262 rtSha1BlockInitBuffered(pCtx);
263 rtSha1BlockProcess(pCtx);
264
265 pCtx->AltPrivate.cbMessage += RTSHA1_BLOCK_SIZE;
266 pbBuf += RTSHA1_BLOCK_SIZE;
267 cbBuf -= RTSHA1_BLOCK_SIZE;
268 }
269 }
270
271 /*
272 * Stash any remaining bytes into the context buffer.
273 */
274 if (cbBuf > 0)
275 {
276 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
277 pCtx->AltPrivate.cbMessage += cbBuf;
278 }
279}
280RT_EXPORT_SYMBOL(RTSha1Update);
281
282
283RTDECL(void) RTSha1Final(PRTSHA1CONTEXT pCtx, uint8_t pabDigest[RTSHA1_HASH_SIZE])
284{
285 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 2);
286
287 /*
288 * Complete the message by adding a single bit (0x80), padding till
289 * the next 448-bit boundrary, the add the message length.
290 */
291 uint64_t const cMessageBits = pCtx->AltPrivate.cbMessage * 8;
292
293 unsigned cbMissing = RTSHA1_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA1_BLOCK_SIZE - 1U));
294 static uint8_t const s_abSingleBitAndSomePadding[12] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
295 if (cbMissing < 1U + 8U)
296 /* Less than 64+8 bits left in the current block, force a new block. */
297 RTSha1Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
298 else
299 RTSha1Update(pCtx, &s_abSingleBitAndSomePadding, 1);
300
301 unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage & (RTSHA1_BLOCK_SIZE - 1U);
302 cbMissing = RTSHA1_BLOCK_SIZE - cbBuffered;
303 Assert(cbMissing >= 8);
304 memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 8);
305
306 *(uint64_t *)&pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits);
307
308 /*
309 * Process the last buffered block constructed/completed above.
310 */
311 rtSha1BlockInitBuffered(pCtx);
312 rtSha1BlockProcess(pCtx);
313
314 /*
315 * Convert the byte order of the hash words and we're done.
316 */
317 pCtx->AltPrivate.auH[0] = RT_H2BE_U32(pCtx->AltPrivate.auH[0]);
318 pCtx->AltPrivate.auH[1] = RT_H2BE_U32(pCtx->AltPrivate.auH[1]);
319 pCtx->AltPrivate.auH[2] = RT_H2BE_U32(pCtx->AltPrivate.auH[2]);
320 pCtx->AltPrivate.auH[3] = RT_H2BE_U32(pCtx->AltPrivate.auH[3]);
321 pCtx->AltPrivate.auH[4] = RT_H2BE_U32(pCtx->AltPrivate.auH[4]);
322
323 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA1_HASH_SIZE);
324
325 RT_ZERO(pCtx->AltPrivate);
326 pCtx->AltPrivate.cbMessage = UINT64_MAX;
327}
328RT_EXPORT_SYMBOL(RTSha1Final);
329
330
331RTDECL(void) RTSha1(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA1_HASH_SIZE])
332{
333 RTSHA1CONTEXT Ctx;
334 RTSha1Init(&Ctx);
335 RTSha1Update(&Ctx, pvBuf, cbBuf);
336 RTSha1Final(&Ctx, pabDigest);
337}
338RT_EXPORT_SYMBOL(RTSha1);
339
340
Note: See TracBrowser for help on using the repository browser.

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