ReactOS 0.4.17-dev-1005-g171e1de
hmac_pattern.c
Go to the documentation of this file.
1//
2// hmac_pattern.c
3// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
4//
5
6#if 0
7#pragma makedep header
8#endif
9
10VOID
16{
18
19 SYMCRYPT_XxxStateCopy( &pSrc->hash, &pDst->hash );
20
21 if( pExpandedKey != NULL )
22 {
24 pDst->pKey = pExpandedKey;
25 }
26 else
27 {
28 SYMCRYPT_CHECK_MAGIC( pSrc->pKey );
29 pDst->pKey = pSrc->pKey;
30 }
31 SYMCRYPT_SET_MAGIC( pDst );
32}
33
34VOID
37{
39 *pDst = *pSrc;
40 SYMCRYPT_SET_MAGIC( pDst );
41}
42
49{
50 SYMCRYPT_XXX_STATE hashState;
51 SYMCRYPT_ALIGN BYTE iblock[ SYMCRYPT_XXX_INPUT_BLOCK_SIZE ]; // One input block for the hash function
52 SIZE_T tmp;
53
55
56 //
57 // Initialize our hash state and our input block
58 // We wipe the whole block & then copy the key into it. This is often faster
59 // as the compiler can optimize the wipe because it knows the size at compile time.
60 //
61 SYMCRYPT_XxxInit( &hashState );
62 memset( iblock, 0, sizeof( iblock ) );
63
64 if( cbKey <= sizeof( iblock ) )
65 {
66 if( cbKey > 0 )
67 {
68 memcpy( iblock, pbKey, cbKey );
69 }
70 } else {
71 //
72 // We can use the existing MD5 state to hash the long key.
73 // The state is re-initialized by the SymCryptMd5Result() function.
74 //
75 SYMCRYPT_XxxAppend( &hashState, pbKey, cbKey );
76 SYMCRYPT_XxxResult( &hashState, iblock );
77 }
78
79 XorByteIntoBuffer( iblock, sizeof( iblock )/8, HMAC_IPAD_BYTE );
80
81 //
82 // Copy the initial chaining state to both states in the expanded key
83 //
84 pExpandedKey->innerState = hashState.chain;
85 pExpandedKey->outerState = hashState.chain;
86
87 //
88 // Update the state in the expanded key directly
89 //
90 SYMCRYPT_XxxAppendBlocks( &pExpandedKey->innerState, iblock, sizeof( iblock ), &tmp );
91
92 XorByteIntoBuffer( iblock, sizeof( iblock )/8, HMAC_IPAD_BYTE ^ HMAC_OPAD_BYTE );
93
94 SYMCRYPT_XxxAppendBlocks( &pExpandedKey->outerState, iblock, sizeof( iblock ), &tmp );
95
96 SymCryptWipeKnownSize( iblock, sizeof( iblock ) );
97 SymCryptWipeKnownSize( &hashState, sizeof( hashState ) );
98
99 return SYMCRYPT_NO_ERROR;
100}
101
102
103SYMCRYPT_NOINLINE
104VOID
109{
111
113
114 //
115 // We don't call SymCryptXxxInit on the hash sub-state;
116 // instead we directly initialize its fields.
117 //
118 SYMCRYPT_SET_MAGIC( &pState->hash );
119 pState->hash.chain = pExpandedKey->innerState;
121 pState->hash.bytesInBuffer = 0;
122 pState->pKey = pExpandedKey;
123}
124
125VOID
130 SIZE_T cbData )
131{
133}
134
136
137SYMCRYPT_NOINLINE
138VOID
143{
145
147
148 //
149 // We have to buffer the inner hash result. We can't put it directly in the
150 // hash state data buffer as the Result() function wipes that buffer before returning.
151 //
152
153 SYMCRYPT_XxxResult( &pState->hash, innerRes );
154
156
157 pState->hash.chain = pState->pKey->outerState;
158
159 //
160 // We put the data directly in the buffer, rather than call the Append function.
161 //
162 memcpy( &pState->hash.buffer, innerRes, sizeof( innerRes ) );
164 pState->hash.bytesInBuffer = SYMCRYPT_XXX_RESULT_SIZE;
165
167
168 //
169 // The SymCryptXxxResult already wipes the hash state.
170 // We only need to wipe our own buffer.
171 //
172 // We also set the key pointer to NULL. This is not for security;
173 // it creates a clear error when callers forget to call the Init routine
174 // when re-using a state. Rather than the wrong result, they will get
175 // a NULL pointer exception, and they will fix their code.
176 //
177
178 SymCryptWipe( innerRes, sizeof( innerRes ) );
179 pState->pKey = NULL;
180}
181
182SYMCRYPT_NOINLINE
183VOID
190{
192
196
197}
static int state
Definition: maze.c:121
#define NULL
Definition: types.h:112
#define SET_DATALENGTH(state, len)
Definition: hmacmd5.c:11
#define C_ASSERT(e)
Definition: intsafe.h:73
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define _In_reads_(s)
Definition: no_sal2.h:168
#define _Inout_
Definition: no_sal2.h:162
#define _Out_writes_(s)
Definition: no_sal2.h:176
#define _Out_
Definition: no_sal2.h:160
#define _In_reads_opt_(s)
Definition: no_sal2.h:222
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
BYTE * PBYTE
Definition: pedump.c:66
#define SYMCRYPT_HMAC_XXX_RESULT_SIZE
Definition: sc_lib.h:155
#define SYMCRYPT_HmacXxxKeyCopy
Definition: sc_lib.h:144
#define SYMCRYPT_HmacXxxResult
Definition: sc_lib.h:148
#define SYMCRYPT_XxxInit
Definition: sc_lib.h:123
#define SYMCRYPT_XXX_RESULT_SIZE
Definition: sc_lib.h:152
#define SYMCRYPT_HmacXxxExpandKey
Definition: sc_lib.h:145
#define PSYMCRYPT_HMAC_XXX_EXPANDED_KEY
Definition: sc_lib.h:157
#define SYMCRYPT_XxxAppend
Definition: sc_lib.h:124
#define SYMCRYPT_HmacXxxAppend
Definition: sc_lib.h:147
#define PCSYMCRYPT_HMAC_XXX_EXPANDED_KEY
Definition: sc_lib.h:158
#define SYMCRYPT_HmacXxx
Definition: sc_lib.h:142
#define SYMCRYPT_HmacXxxInit
Definition: sc_lib.h:146
#define SYMCRYPT_XXX_STATE
Definition: sc_lib.h:116
#define SYMCRYPT_XXX_INPUT_BLOCK_SIZE
Definition: sc_lib.h:151
#define SYMCRYPT_HMAC_XXX_STATE
Definition: sc_lib.h:159
#define HMAC_IPAD_BYTE
Definition: sc_lib.h:301
#define SYMCRYPT_XxxAppendBlocks
Definition: sc_lib.h:126
#define HMAC_OPAD_BYTE
Definition: sc_lib.h:302
#define PCSYMCRYPT_HMAC_XXX_STATE
Definition: sc_lib.h:161
#define PSYMCRYPT_HMAC_XXX_STATE
Definition: sc_lib.h:160
#define SYMCRYPT_XxxResult
Definition: sc_lib.h:125
FORCEINLINE VOID SYMCRYPT_CALL XorByteIntoBuffer(_Inout_updates_(8 *cqBuf) PBYTE pbBuf, SIZE_T cqBuf, BYTE v)
Definition: sc_lib.h:915
#define SYMCRYPT_XxxStateCopy
Definition: sc_lib.h:122
#define SYMCRYPT_HmacXxxStateCopy
Definition: sc_lib.h:143
#define memset(x, y, z)
Definition: compat.h:39
static const BYTE pbResult[]
FORCEINLINE VOID SYMCRYPT_CALL SymCryptWipeKnownSize(_Out_writes_bytes_(cbData) PVOID pbData, SIZE_T cbData)
VOID SYMCRYPT_CALL SymCryptWipe(_Out_writes_bytes_(cbData) PVOID pbData, SIZE_T cbData)
Definition: libmain.c:137
SYMCRYPT_ERROR
Definition: symcrypt.h:227
#define SYMCRYPT_ALIGN
#define SYMCRYPT_CALL
PCBYTE pbKey
PCBYTE SIZE_T cbKey
PCBYTE PBYTE SIZE_T cbData
#define SYMCRYPT_SET_MAGIC(p)
PSYMCRYPT_COMMON_HASH_STATE pState
const BYTE * PCBYTE
PCBYTE pbData
#define SYMCRYPT_CHECK_MAGIC(p)
PCVOID pExpandedKey
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned char BYTE
Definition: xxhash.c:193