ReactOS 0.4.17-dev-1005-g171e1de
md5.c File Reference
#include "precomp.h"
#include "hash_pattern.c"
Include dependency graph for md5.c:

Go to the source code of this file.

Macros

#define ALG   MD5
 
#define Alg   Md5
 
#define F(x, y, z)   ((((z) ^ (y)) & (x)) ^ (z))
 
#define G(x, y, z)   F( (z), (x), (y) )
 
#define H(x, y, z)   ((x) ^ (y) ^ (z) )
 
#define I(x, y, z)   ((y) ^ ((x) | ~(z)))
 
#define CROUND(r, Func)
 
#define IROUND(r, Func)
 
#define FROUND(r, Func)
 

Functions

VOID SYMCRYPT_CALL SymCryptMd5Init (_Out_ PSYMCRYPT_MD5_STATE pState)
 
VOID SYMCRYPT_CALL SymCryptMd5Append (_Inout_ PSYMCRYPT_MD5_STATE pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
 
VOID SYMCRYPT_CALL SymCryptMd5Result (_Inout_ PSYMCRYPT_MD5_STATE pState, _Out_writes_(SYMCRYPT_MD5_RESULT_SIZE) PBYTE pbResult)
 
VOID SYMCRYPT_CALL SymCryptMd5AppendBlocks (_Inout_ SYMCRYPT_MD5_CHAINING_STATE *pChain, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData, _Out_ SIZE_T *pcbRemaining)
 
VOID SYMCRYPT_CALL SymCryptMd5StateExport (_In_ PCSYMCRYPT_MD5_STATE pState, _Out_writes_bytes_(SYMCRYPT_MD5_STATE_EXPORT_SIZE) PBYTE pbBlob)
 
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptMd5StateImport (_Out_ PSYMCRYPT_MD5_STATE pState, _In_reads_bytes_(SYMCRYPT_MD5_STATE_EXPORT_SIZE) PCBYTE pbBlob)
 
VOID SYMCRYPT_CALL SymCryptMd5Selftest (void)
 

Variables

const SYMCRYPT_HASH SymCryptMd5Algorithm_default
 
const PCSYMCRYPT_HASH SymCryptMd5Algorithm = &SymCryptMd5Algorithm_default
 
static const UINT32 md5Const [64]
 
static const int md5Rotate [64]
 
static const int md5MsgIndex [64]
 
static const UINT32 md5InitialState [4]
 
static const BYTE md5KATAnswer [16]
 

Macro Definition Documentation

◆ ALG

#define ALG   MD5

Definition at line 168 of file md5.c.

◆ Alg

#define Alg   Md5

Definition at line 169 of file md5.c.

◆ CROUND

#define CROUND (   r,
  Func 
)
Value:
{ \
ad[r%4] = ad[(r+3)%4] + ROL32( ad[r%4] + Func(ad[(r+3)%4], ad[(r+2)%4], ad[(r+1)%4]) + Wt + md5Const[r], md5Rotate[r] ); \
}
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
void(* Func)(int)
static const int md5Rotate[64]
Definition: md5.c:122
static const UINT32 md5Const[64]
Definition: md5.c:51

Definition at line 274 of file md5.c.

◆ F

#define F (   x,
  y,
  z 
)    ((((z) ^ (y)) & (x)) ^ (z))

Definition at line 243 of file md5.c.

◆ FROUND

#define FROUND (   r,
  Func 
)
Value:
{ \
Wt = W[md5MsgIndex[r]];\
CROUND( r, Func ); \
}
static const int md5MsgIndex[64]
Definition: md5.c:148
Definition: polytest.cpp:36

Definition at line 288 of file md5.c.

◆ G

#define G (   x,
  y,
  z 
)    F( (z), (x), (y) )

Definition at line 244 of file md5.c.

◆ H

#define H (   x,
  y,
  z 
)    ((x) ^ (y) ^ (z) )

Definition at line 245 of file md5.c.

◆ I

#define I (   x,
  y,
  z 
)    ((y) ^ ((x) | ~(z)))

Definition at line 246 of file md5.c.

◆ IROUND

#define IROUND (   r,
  Func 
)
Value:
{ \
W[r] = Wt; \
CROUND( r, Func ); \
}
#define SYMCRYPT_LOAD_LSBFIRST32(p)
Definition: symcrypt.h:299
PCBYTE pbData

Definition at line 278 of file md5.c.

Function Documentation

◆ SymCryptMd5Append()

VOID SYMCRYPT_CALL SymCryptMd5Append ( _Inout_ PSYMCRYPT_MD5_STATE  pState,
_In_reads_(cbData) PCBYTE  pbData,
SIZE_T  cbData 
)

Definition at line 204 of file md5.c.

208{
210}
VOID SYMCRYPT_CALL SymCryptHashAppendInternal(_In_ PCSYMCRYPT_HASH pHash, _Inout_ PSYMCRYPT_COMMON_HASH_STATE pState, _In_reads_bytes_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: hash.c:10
const PCSYMCRYPT_HASH SymCryptMd5Algorithm
Definition: md5.c:43
PCBYTE PBYTE SIZE_T cbData
* PSYMCRYPT_COMMON_HASH_STATE
PSYMCRYPT_COMMON_HASH_STATE pState

◆ SymCryptMd5AppendBlocks()

VOID SYMCRYPT_CALL SymCryptMd5AppendBlocks ( _Inout_ SYMCRYPT_MD5_CHAINING_STATE *  pChain,
_In_reads_(cbData) PCBYTE  pbData,
SIZE_T  cbData,
_Out_ SIZE_T *  pcbRemaining 
)

Definition at line 295 of file md5.c.

300{
301
302 UINT32 W[16];
303 UINT32 ad[4];
304 UINT32 Wt;
305
306 ad[0] = pChain->H[0];
307 ad[1] = pChain->H[3];
308 ad[2] = pChain->H[2];
309 ad[3] = pChain->H[1];
310
311 while( cbData >= 64 )
312 {
313 //
314 // initial rounds 1 to 16
315 //
316
317 IROUND( 0, F );
318 IROUND( 1, F );
319 IROUND( 2, F );
320 IROUND( 3, F );
321 IROUND( 4, F );
322 IROUND( 5, F );
323 IROUND( 6, F );
324 IROUND( 7, F );
325 IROUND( 8, F );
326 IROUND( 9, F );
327 IROUND( 10, F );
328 IROUND( 11, F );
329 IROUND( 12, F );
330 IROUND( 13, F );
331 IROUND( 14, F );
332 IROUND( 15, F );
333
334 FROUND( 16, G );
335 FROUND( 17, G );
336 FROUND( 18, G );
337 FROUND( 19, G );
338 FROUND( 20, G );
339 FROUND( 21, G );
340 FROUND( 22, G );
341 FROUND( 23, G );
342 FROUND( 24, G );
343 FROUND( 25, G );
344 FROUND( 26, G );
345 FROUND( 27, G );
346 FROUND( 28, G );
347 FROUND( 29, G );
348 FROUND( 30, G );
349 FROUND( 31, G );
350
351 FROUND( 32, H );
352 FROUND( 33, H );
353 FROUND( 34, H );
354 FROUND( 35, H );
355 FROUND( 36, H );
356 FROUND( 37, H );
357 FROUND( 38, H );
358 FROUND( 39, H );
359 FROUND( 40, H );
360 FROUND( 41, H );
361 FROUND( 42, H );
362 FROUND( 43, H );
363 FROUND( 44, H );
364 FROUND( 45, H );
365 FROUND( 46, H );
366 FROUND( 47, H );
367
368 FROUND( 48, I );
369 FROUND( 49, I );
370 FROUND( 50, I );
371 FROUND( 51, I );
372 FROUND( 52, I );
373 FROUND( 53, I );
374 FROUND( 54, I );
375 FROUND( 55, I );
376 FROUND( 56, I );
377 FROUND( 57, I );
378 FROUND( 58, I );
379 FROUND( 59, I );
380 FROUND( 60, I );
381 FROUND( 61, I );
382 FROUND( 62, I );
383 FROUND( 63, I );
384
385 pChain->H[0] = ad[0] = ad[0] + pChain->H[0];
386 pChain->H[3] = ad[1] = ad[1] + pChain->H[3];
387 pChain->H[2] = ad[2] = ad[2] + pChain->H[2];
388 pChain->H[1] = ad[3] = ad[3] + pChain->H[1];
389
390 pbData += 64;
391 cbData -= 64;
392 }
393
394 *pcbRemaining = cbData;
395
396 //
397 // Wipe the variables;
398 //
399 SymCryptWipeKnownSize( ad, sizeof( ad ) );
400 SymCryptWipeKnownSize( W, sizeof( W ) );
401 SymCryptWipeKnownSize( &Wt, sizeof( Wt ) );
402}
#define F(x, y, z)
Definition: md5.c:51
#define I(x, y, z)
Definition: md5.c:55
#define G(x, y, z)
Definition: md5.c:52
#define H(x, y, z)
Definition: md5.c:53
#define FROUND(r, Func)
Definition: md5.c:288
#define IROUND(r, Func)
Definition: md5.c:278
FORCEINLINE VOID SYMCRYPT_CALL SymCryptWipeKnownSize(_Out_writes_bytes_(cbData) PVOID pbData, SIZE_T cbData)
uint32_t UINT32
Definition: typedefs.h:59

◆ SymCryptMd5Init()

VOID SYMCRYPT_CALL SymCryptMd5Init ( _Out_ PSYMCRYPT_MD5_STATE  pState)

Definition at line 182 of file md5.c.

183{
185
186 pState->dataLengthL = 0;
187 pState->dataLengthH = 0;
188 pState->bytesInBuffer = 0;
189
190 memcpy( &pState->chain.H[0], &md5InitialState[0], sizeof( md5InitialState ) );
191
192 //
193 // There is no need to initialize the buffer part of the state as that will be
194 // filled before it is used.
195 //
196}
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static const UINT32 md5InitialState[4]
Definition: md5.c:158
#define SYMCRYPT_SET_MAGIC(p)

Referenced by SymCryptMd5Result().

◆ SymCryptMd5Result()

Definition at line 217 of file md5.c.

220{
222
223 //
224 // Write the output in the correct byte order
225 //
226 SymCryptUint32ToLsbFirst( &pState->chain.H[0], pbResult, 4 );
227
228 //
229 // Wipe & re-initialize
230 // We have to wipe the whole state because the Init call
231 // might be optimized away by a smart compiler.
232 // And we need to wipe old data.
233 //
234 SymCryptWipeKnownSize( pState, sizeof( *pState ) );
236}
FORCEINLINE VOID SYMCRYPT_CALL SymCryptUint32ToLsbFirst(_In_reads_(cuData) PCUINT32 puData, _Out_writes_(4 *cuData) PBYTE pbResult, SIZE_T cuData)
Definition: sc_lib.h:471
VOID SYMCRYPT_CALL SymCryptHashCommonPaddingMd4Style(_In_ PCSYMCRYPT_HASH pHash, _Inout_ PSYMCRYPT_COMMON_HASH_STATE pState)
Definition: hash.c:85
VOID SYMCRYPT_CALL SymCryptMd5Init(_Out_ PSYMCRYPT_MD5_STATE pState)
Definition: md5.c:182
static const BYTE pbResult[]

◆ SymCryptMd5Selftest()

VOID SYMCRYPT_CALL SymCryptMd5Selftest ( void  )

Definition at line 492 of file md5.c.

493{
495
497
498 SymCryptInjectError( result, sizeof( result ) );
499
500 if( memcmp( result, md5KATAnswer, sizeof( result ) ) != 0 ) {
501 SymCryptFatal( 'MD5t' );
502 }
503}
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2807
GLuint64EXT * result
Definition: glext.h:11304
VOID SYMCRYPT_CALL SymCryptInjectError(PBYTE pbData, SIZE_T cbData)
const BYTE SymCryptTestMsg3[3]
Definition: selftest.c:8
static const BYTE md5KATAnswer[16]
Definition: md5.c:485
_Analysis_noreturn_ VOID SYMCRYPT_CALL SymCryptFatal(UINT32 fatalCode)
#define SYMCRYPT_MD5_RESULT_SIZE
Definition: symcrypt.h:1017
VOID SYMCRYPT_CALL SymCryptMd5(_In_reads_(cbData) PCBYTE pbData, SIZE_T cbData, _Out_writes_(SYMCRYPT_MD5_RESULT_SIZE) PBYTE pbResult)
unsigned char BYTE
Definition: xxhash.c:193

◆ SymCryptMd5StateExport()

Definition at line 406 of file md5.c.

409{
410 SYMCRYPT_MD5_STATE_EXPORT_BLOB blob; // local copy to have proper alignment.
412
414
415 SymCryptWipeKnownSize( &blob, sizeof( blob ) ); // wipe to avoid any data leakage
416
417 blob.header.magic = SYMCRYPT_BLOB_MAGIC;
419 blob.header.type = SymCryptBlobTypeMd5State;
420
421 //
422 // Copy the relevant data. Buffer will be 0-padded.
423 //
424
425 SymCryptUint32ToLsbFirst( &pState->chain.H[0], &blob.chain[0], 4 );
426 blob.dataLength = pState->dataLengthL;
427 memcpy( &blob.buffer[0], &pState->buffer[0], blob.dataLength & 0x3f );
428
429 SYMCRYPT_ASSERT( (PCBYTE) &blob + sizeof( blob ) - sizeof( SYMCRYPT_BLOB_TRAILER ) == (PCBYTE) &blob.trailer );
430 SymCryptMarvin32( SymCryptMarvin32DefaultSeed, (PCBYTE) &blob, sizeof( blob ) - sizeof( SYMCRYPT_BLOB_TRAILER ), &blob.trailer.checksum[0] );
431
432 memcpy( pbBlob, &blob, sizeof( blob ) );
433
434//cleanup:
435 SymCryptWipeKnownSize( &blob, sizeof( blob ) );
436 return;
437}
#define C_ASSERT(e)
Definition: intsafe.h:73
#define SYMCRYPT_BLOB_MAGIC
Definition: sc_lib.h:1077
@ SymCryptBlobTypeMd5State
Definition: sc_lib.h:1063
Definition: image.c:229
#define SYMCRYPT_ASSERT(_x)
Definition: symcrypt.h:10807
VOID SYMCRYPT_CALL SymCryptMarvin32(_In_ PCSYMCRYPT_MARVIN32_EXPANDED_SEED pExpandedSeed, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData, _Out_writes_(SYMCRYPT_MARVIN32_RESULT_SIZE) PBYTE pbResult)
Definition: marvin32.c:239
PCSYMCRYPT_MARVIN32_EXPANDED_SEED const SymCryptMarvin32DefaultSeed
Definition: marvin32.c:29
#define SYMCRYPT_MD5_STATE_EXPORT_SIZE
const BYTE * PCBYTE
#define SYMCRYPT_CHECK_MAGIC(p)

◆ SymCryptMd5StateImport()

Definition at line 441 of file md5.c.

444{
445 SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
446 SYMCRYPT_MD5_STATE_EXPORT_BLOB blob; // local copy to have proper alignment.
447 BYTE checksum[8];
448
450 memcpy( &blob, pbBlob, sizeof( blob ) );
451
452 if( blob.header.magic != SYMCRYPT_BLOB_MAGIC ||
453 blob.header.size != SYMCRYPT_MD5_STATE_EXPORT_SIZE ||
454 blob.header.type != SymCryptBlobTypeMd5State )
455 {
456 scError = SYMCRYPT_INVALID_BLOB;
457 goto cleanup;
458 }
459
461 if( memcmp( checksum, &blob.trailer.checksum[0], 8 ) != 0 )
462 {
463 scError = SYMCRYPT_INVALID_BLOB;
464 goto cleanup;
465 }
466
467 SymCryptLsbFirstToUint32( &blob.chain[0], &pState->chain.H[0], 4 );
468 pState->dataLengthL = blob.dataLength;
469 pState->dataLengthH = 0;
470 pState->bytesInBuffer = blob.dataLength & 0x3f;
471 memcpy( &pState->buffer[0], &blob.buffer[0], pState->bytesInBuffer );
472
474
475cleanup:
476 SymCryptWipeKnownSize( &blob, sizeof(blob) );
477 return scError;
478}
static cab_ULONG checksum(const cab_UBYTE *data, cab_UWORD bytes, cab_ULONG csum)
Definition: fdi.c:353
static void cleanup(void)
Definition: main.c:1335
FORCEINLINE VOID SYMCRYPT_CALL SymCryptLsbFirstToUint32(_In_reads_(4 *cuResult) PCBYTE pbData, _Out_writes_(cuResult) PUINT32 puResult, SIZE_T cuResult)
Definition: sc_lib.h:487
SYMCRYPT_ERROR
Definition: symcrypt.h:227

Variable Documentation

◆ md5Const

const UINT32 md5Const[64]
static

Definition at line 51 of file md5.c.

◆ md5InitialState

const UINT32 md5InitialState[4]
static
Initial value:
= {
0x67452301UL,
0xefcdab89UL,
0x98badcfeUL,
0x10325476UL,
}

Definition at line 158 of file md5.c.

Referenced by SymCryptMd5Init().

◆ md5KATAnswer

const BYTE md5KATAnswer[16]
static
Initial value:
= {
0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72,
}

Definition at line 485 of file md5.c.

Referenced by SymCryptMd5Selftest().

◆ md5MsgIndex

const int md5MsgIndex[64]
static
Initial value:
= {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9,
}

Definition at line 148 of file md5.c.

◆ md5Rotate

const int md5Rotate[64]
static
Initial value:
= {
7, 12, 17, 22,
7, 12, 17, 22,
7, 12, 17, 22,
7, 12, 17, 22,
5, 9, 14, 20,
5, 9, 14, 20,
5, 9, 14, 20,
5, 9, 14, 20,
4, 11, 16, 23,
4, 11, 16, 23,
4, 11, 16, 23,
4, 11, 16, 23,
6, 10, 15, 21,
6, 10, 15, 21,
6, 10, 15, 21,
6, 10, 15, 21,
}

Definition at line 122 of file md5.c.

◆ SymCryptMd5Algorithm

Definition at line 43 of file md5.c.

Referenced by init_hash_impl(), SymCryptMd5Append(), and SymCryptMd5Result().

◆ SymCryptMd5Algorithm_default

const SYMCRYPT_HASH SymCryptMd5Algorithm_default
Initial value:
= {
sizeof( SYMCRYPT_MD5_STATE ),
}
VOID SYMCRYPT_CALL SymCryptMd5AppendBlocks(_Inout_ SYMCRYPT_MD5_CHAINING_STATE *pChain, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData, _Out_ SIZE_T *pcbRemaining)
Definition: md5.c:295
VOID SYMCRYPT_CALL SymCryptMd5Append(_Inout_ PSYMCRYPT_MD5_STATE pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: md5.c:204
VOID SYMCRYPT_CALL SymCryptMd5Result(_Inout_ PSYMCRYPT_MD5_STATE pState, _Out_writes_(SYMCRYPT_MD5_RESULT_SIZE) PBYTE pbResult)
Definition: md5.c:217
#define SYMCRYPT_MD5_INPUT_BLOCK_SIZE
Definition: symcrypt.h:1018
VOID SYMCRYPT_CALL SymCryptMd5StateCopy(_In_ PCSYMCRYPT_MD5_STATE pSrc, _Out_ PSYMCRYPT_MD5_STATE pDst)
#define SYMCRYPT_FIELD_SIZE(type, field)
#define SYMCRYPT_FIELD_OFFSET(type, field)
SYMCRYPT_MD5_STATE
struct sock * chain
Definition: tcpcore.h:1

Definition at line 30 of file md5.c.