forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
217 lines (183 loc) · 6.83 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// hash.c generic code used in many hash implementations.
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
#include "precomp.h"
VOID
SYMCRYPT_CALL
SymCryptHashAppendInternal(
_In_ PCSYMCRYPT_HASH pHash,
_Inout_ PSYMCRYPT_COMMON_HASH_STATE pState,
_In_reads_bytes_( cbData ) PCBYTE pbData,
SIZE_T cbData )
{
UINT32 bytesInBuffer;
UINT32 freeInBuffer;
SIZE_T tmp;
SYMCRYPT_CHECK_MAGIC( pState );
pState->dataLengthL += cbData;
if( pState->dataLengthL < cbData ) {
pState->dataLengthH ++; // This is almost-unreachable code as it requires 2^64 bytes to be hashed.
}
bytesInBuffer = pState->bytesInBuffer;
//
// If previous data in buffer, buffer new input and transform if possible.
//
if( bytesInBuffer > 0 )
{
SYMCRYPT_ASSERT( pHash->inputBlockSize > bytesInBuffer );
freeInBuffer = pHash->inputBlockSize - bytesInBuffer;
if( cbData < freeInBuffer )
{
//
// All the data will fit in the buffer.
// We don't do anything here.
// As cbData < inputBlockSize the bulk data processing is skipped,
// and the data will be copied to the buffer at the end
// of this code.
} else {
//
// Enough data to fill the whole buffer & process it
//
memcpy(&pState->buffer[bytesInBuffer], pbData, freeInBuffer);
pbData += freeInBuffer;
cbData -= freeInBuffer;
(*pHash->appendBlockFunc)( (PBYTE)pState + pHash->chainOffset, &pState->buffer[0], pHash->inputBlockSize, &tmp );
bytesInBuffer = 0;
}
}
//
// Internal buffer is empty; process all remaining whole blocks in the input
//
if( cbData >= pHash->inputBlockSize )
{
(*pHash->appendBlockFunc)( (PBYTE)pState + pHash->chainOffset, pbData, cbData, &tmp );
SYMCRYPT_ASSERT( tmp < pHash->inputBlockSize );
pbData += cbData - tmp;
cbData = tmp;
}
SYMCRYPT_ASSERT( cbData < pHash->inputBlockSize );
//
// buffer remaining input if necessary.
//
if( cbData > 0 )
{
memcpy( &pState->buffer[bytesInBuffer], pbData, cbData );
bytesInBuffer += (UINT32) cbData;
}
pState->bytesInBuffer = bytesInBuffer;
}
VOID
SYMCRYPT_CALL
SymCryptHashCommonPaddingMd4Style(
_In_ PCSYMCRYPT_HASH pHash,
_Inout_ PSYMCRYPT_COMMON_HASH_STATE pState )
{
SIZE_T tmp;
SIZE_T bytesInBuffer = pState->bytesInBuffer;
SYMCRYPT_CHECK_MAGIC( pState );
SYMCRYPT_ASSERT( pHash->inputBlockSize == 64 );
SYMCRYPT_ASSERT( bytesInBuffer == (pState->dataLengthL & 0x3f) );
//
// The buffer is never completely full, so we can always put the first
// padding byte in.
//
pState->buffer[bytesInBuffer++] = 0x80;
if( bytesInBuffer > 64-8 ) {
//
// No room for the rest of the padding. Pad with zeroes & process block
// bytesInBuffer is at most 64, so we do not have an integer underflow
//
SymCryptWipe( &pState->buffer[bytesInBuffer], 64-bytesInBuffer );
(*pHash->appendBlockFunc)( (PBYTE)pState + pHash->chainOffset, pState->buffer, 64, &tmp );
SYMCRYPT_ASSERT( tmp == 0 );
bytesInBuffer = 0;
}
//
// Set rest of padding
// At this point bytesInBuffer <= 64-8, so we don't have an underflow
// We wipe to the end of the buffer as it is 16-aligned,
// and it is faster to wipe to an aligned point
//
SymCryptWipe( &pState->buffer[bytesInBuffer], 64-bytesInBuffer );
SYMCRYPT_STORE_LSBFIRST64( &pState->buffer[64-8], pState->dataLengthL * 8 );
//
// Process the final block
//
(*pHash->appendBlockFunc)( (PBYTE)pState + pHash->chainOffset, pState->buffer, 64, &tmp );
}
SIZE_T
SYMCRYPT_CALL
SymCryptHashResultSize( _In_ PCSYMCRYPT_HASH pHash )
{
return pHash->resultSize;
}
SIZE_T
SYMCRYPT_CALL
SymCryptHashInputBlockSize( _In_ PCSYMCRYPT_HASH pHash )
{
return pHash->inputBlockSize;
}
SIZE_T
SYMCRYPT_CALL
SymCryptHashStateSize( _In_ PCSYMCRYPT_HASH pHash )
{
return pHash->stateSize;
}
VOID
SYMCRYPT_CALL
SymCryptHash(
_In_ PCSYMCRYPT_HASH pHash,
_In_reads_( cbData ) PCBYTE pbData,
SIZE_T cbData,
_Out_writes_( SYMCRYPT_MIN( cbResult, pHash->resultSize ) ) PBYTE pbResult,
SIZE_T cbResult )
{
SYMCRYPT_HASH_STATE hash;
_Analysis_assume_( pHash->stateSize <= sizeof( hash ) );
SymCryptHashInit( pHash, &hash );
SymCryptHashAppend( pHash, &hash, pbData, cbData );
SymCryptHashResult( pHash, &hash, pbResult, cbResult );
SymCryptWipe( &hash, pHash->stateSize );
}
VOID
SYMCRYPT_CALL
SymCryptHashInit(
_In_ PCSYMCRYPT_HASH pHash,
_Out_writes_bytes_( pHash->stateSize ) PVOID pState )
{
(*pHash->initFunc)( pState );
}
VOID
SYMCRYPT_CALL
SymCryptHashAppend(
_In_ PCSYMCRYPT_HASH pHash,
_Inout_updates_bytes_( pHash->stateSize ) PVOID pState,
_In_reads_( cbData ) PCBYTE pbData,
SIZE_T cbData )
{
(*pHash->appendFunc)( pState, pbData, cbData );
}
VOID
SYMCRYPT_CALL
SymCryptHashResult(
_In_ PCSYMCRYPT_HASH pHash,
_Inout_updates_bytes_( pHash->stateSize ) PVOID pState,
_Out_writes_( SYMCRYPT_MIN( cbResult, pHash->resultSize ) ) PBYTE pbResult,
SIZE_T cbResult )
{
BYTE buf[SYMCRYPT_HASH_MAX_RESULT_SIZE];
_Analysis_assume_( pHash->resultSize <= SYMCRYPT_HASH_MAX_RESULT_SIZE );
(*pHash->resultFunc)( pState, buf );
memcpy( pbResult, buf, SYMCRYPT_MIN( cbResult, pHash->resultSize ));
SymCryptWipe( buf, pHash->resultSize );
}
VOID
SYMCRYPT_CALL
SymCryptHashStateCopy(
_In_ PCSYMCRYPT_HASH pHash,
_In_reads_( pHash->stateSize ) PCVOID pSrc,
_Out_writes_( pHash->stateSize ) PVOID pDst)
{
(*pHash->stateCopyFunc)( pSrc, pDst );
}