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

Go to the source code of this file.

Functions

VOID SYMCRYPT_CALL SymCryptHmacStateCopy (_In_ PCSYMCRYPT_HMAC_STATE pSrc, _In_opt_ PCSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey, _Out_ PSYMCRYPT_HMAC_STATE pDst)
 
VOID SYMCRYPT_CALL SymCryptHmacKeyCopy (_In_ PCSYMCRYPT_HMAC_EXPANDED_KEY pSrc, _Out_ PSYMCRYPT_HMAC_EXPANDED_KEY pDst)
 
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptHmacExpandKey (_In_ PCSYMCRYPT_HASH pHash, _Out_ PSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey, _In_reads_opt_(cbKey) PCBYTE pbKey, SIZE_T cbKey)
 
SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacInit (_Out_ PSYMCRYPT_HMAC_STATE pState, _In_ PCSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey)
 
VOID SYMCRYPT_CALL SymCryptHmacAppend (_Inout_ PSYMCRYPT_HMAC_STATE pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
 
SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacResult (_Inout_ PSYMCRYPT_HMAC_STATE pState, _Out_writes_(pState->pKey->pHash->resultSize) PBYTE pbResult)
 
SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmac (_In_ PCSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData, _Out_writes_(pExpandedKey->pHash->resultSize) PBYTE pbResult)
 

Function Documentation

◆ SymCryptHmac()

SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmac ( _In_ PCSYMCRYPT_HMAC_EXPANDED_KEY  pExpandedKey,
_In_reads_(cbData) PCBYTE  pbData,
SIZE_T  cbData,
_Out_writes_(pExpandedKey->pHash->resultSize) PBYTE  pbResult 
)

Definition at line 184 of file hmac.c.

189{
191
195}
static int state
Definition: maze.c:121
SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacResult(_Inout_ PSYMCRYPT_HMAC_STATE pState, _Out_writes_(pState->pKey->pHash->resultSize) PBYTE pbResult)
Definition: hmac.c:141
VOID SYMCRYPT_CALL SymCryptHmacAppend(_Inout_ PSYMCRYPT_HMAC_STATE pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: hmac.c:130
SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacInit(_Out_ PSYMCRYPT_HMAC_STATE pState, _In_ PCSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey)
Definition: hmac.c:115
static const BYTE pbResult[]
SYMCRYPT_MAGIC_FIELD SYMCRYPT_HMAC_STATE
PCBYTE PBYTE SIZE_T cbData
PCBYTE pbData
PCVOID pExpandedKey

Referenced by SymCryptHmacSha3_224(), SymCryptHmacSha3_256(), SymCryptHmacSha3_384(), and SymCryptHmacSha3_512().

◆ SymCryptHmacAppend()

VOID SYMCRYPT_CALL SymCryptHmacAppend ( _Inout_ PSYMCRYPT_HMAC_STATE  pState,
_In_reads_(cbData) PCBYTE  pbData,
SIZE_T  cbData 
)

Definition at line 130 of file hmac.c.

134{
135 SymCryptHashAppend( pState->pKey->pHash, &pState->hash, pbData, cbData );
136}
VOID SYMCRYPT_CALL SymCryptHashAppend(_In_ PCSYMCRYPT_HASH pHash, _Inout_updates_bytes_(pHash->stateSize) PVOID pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: hash.c:182
PSYMCRYPT_COMMON_HASH_STATE pState

Referenced by SymCryptHmac(), SymCryptHmacSha3_224Append(), SymCryptHmacSha3_256Append(), SymCryptHmacSha3_384Append(), and SymCryptHmacSha3_512Append().

◆ SymCryptHmacExpandKey()

SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptHmacExpandKey ( _In_ PCSYMCRYPT_HASH  pHash,
_Out_ PSYMCRYPT_HMAC_EXPANDED_KEY  pExpandedKey,
_In_reads_opt_(cbKey) PCBYTE  pbKey,
SIZE_T  cbKey 
)

Definition at line 53 of file hmac.c.

58{
59 // This buffer has to be large enough to hold one input block
60 // and the result of the hash function.
61 // Using SHA3-224 input block size to satisfy those requirements.
63
64 SYMCRYPT_ASSERT( sizeof(iblock) >= pHash->inputBlockSize );
65 SYMCRYPT_ASSERT( sizeof(iblock) >= pHash->resultSize );
66
67 // XorByteIntoBuffer function updates the buffer in multiples of 8-bytes
68 SYMCRYPT_ASSERT( pHash->inputBlockSize % 8 == 0);
69
70 memset( iblock, 0, sizeof( iblock ) );
71
72 if( cbKey <= pHash->inputBlockSize )
73 {
74 if( cbKey > 0 )
75 {
76 memcpy( iblock, pbKey, cbKey );
77 }
78 }
79 else
80 {
81 SymCryptHash( pHash, pbKey, cbKey, iblock, pHash->resultSize );
82 }
83
84 XorByteIntoBuffer( iblock, pHash->inputBlockSize / 8, HMAC_IPAD_BYTE );
85
86 //
87 // Initialize the inner and outer states in the expanded key
88 //
89 SymCryptHashInit( pHash, &pExpandedKey->innerState );
90 SymCryptHashInit( pHash, &pExpandedKey->outerState );
91
92 // Update the inner state in the expanded key
93 SymCryptHashAppend( pHash, &pExpandedKey->innerState, iblock, pHash->inputBlockSize );
94
95 XorByteIntoBuffer( iblock, pHash->inputBlockSize / 8, HMAC_IPAD_BYTE ^ HMAC_OPAD_BYTE );
96
97 // Update the outer state in the expanded key
98 SymCryptHashAppend( pHash, &pExpandedKey->outerState, iblock, pHash->inputBlockSize );
99
100 SymCryptWipeKnownSize( iblock, sizeof( iblock ) );
101
102 // Save the hash function in the expanded key, it will be used in other
103 // generic HMAC function calls.
104 pExpandedKey->pHash = pHash;
105
107
108 return SYMCRYPT_NO_ERROR;
109}
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define HMAC_IPAD_BYTE
Definition: sc_lib.h:301
#define HMAC_OPAD_BYTE
Definition: sc_lib.h:302
FORCEINLINE VOID SYMCRYPT_CALL XorByteIntoBuffer(_Inout_updates_(8 *cqBuf) PBYTE pbBuf, SIZE_T cqBuf, BYTE v)
Definition: sc_lib.h:915
#define memset(x, y, z)
Definition: compat.h:39
#define SYMCRYPT_ASSERT(_x)
Definition: symcrypt.h:10807
FORCEINLINE VOID SYMCRYPT_CALL SymCryptWipeKnownSize(_Out_writes_bytes_(cbData) PVOID pbData, SIZE_T cbData)
VOID SYMCRYPT_CALL SymCryptHashInit(_In_ PCSYMCRYPT_HASH pHash, _Out_writes_bytes_(pHash->stateSize) PVOID pState)
Definition: hash.c:173
#define SYMCRYPT_SHA3_224_INPUT_BLOCK_SIZE
Definition: symcrypt.h:1561
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)
Definition: hash.c:155
#define SYMCRYPT_ALIGN
PCBYTE pbKey
PCBYTE SIZE_T cbKey
#define SYMCRYPT_SET_MAGIC(p)
UINT32 inputBlockSize
unsigned char BYTE
Definition: xxhash.c:193

Referenced by SymCryptHmacSha3_224ExpandKey(), SymCryptHmacSha3_256ExpandKey(), SymCryptHmacSha3_384ExpandKey(), and SymCryptHmacSha3_512ExpandKey().

◆ SymCryptHmacInit()

SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacInit ( _Out_ PSYMCRYPT_HMAC_STATE  pState,
_In_ PCSYMCRYPT_HMAC_EXPANDED_KEY  pExpandedKey 
)

Definition at line 115 of file hmac.c.

118{
120
121 SymCryptHashStateCopy( pExpandedKey->pHash, &pExpandedKey->innerState, &pState->hash );
122
123 pState->pKey = pExpandedKey;
124
126}
VOID SYMCRYPT_CALL SymCryptHashStateCopy(_In_ PCSYMCRYPT_HASH pHash, _In_reads_(pHash->stateSize) PCVOID pSrc, _Out_writes_(pHash->stateSize) PVOID pDst)
Definition: hash.c:210
#define SYMCRYPT_CHECK_MAGIC(p)

Referenced by SymCryptHmac(), SymCryptHmacSha3_224Init(), SymCryptHmacSha3_256Init(), SymCryptHmacSha3_384Init(), and SymCryptHmacSha3_512Init().

◆ SymCryptHmacKeyCopy()

Definition at line 36 of file hmac.c.

39{
41
42 // Copy innerState and outerState
43 SymCryptHashStateCopy(pSrc->pHash, &pSrc->innerState, &pDst->innerState );
44 SymCryptHashStateCopy(pSrc->pHash, &pSrc->outerState, &pDst->outerState );
45
46 pDst->pHash = pSrc->pHash;
47
48 SYMCRYPT_SET_MAGIC( pDst );
49}

Referenced by SymCryptHmacSha3_224KeyCopy(), SymCryptHmacSha3_256KeyCopy(), SymCryptHmacSha3_384KeyCopy(), and SymCryptHmacSha3_512KeyCopy().

◆ SymCryptHmacResult()

SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacResult ( _Inout_ PSYMCRYPT_HMAC_STATE  pState,
_Out_writes_(pState->pKey->pHash->resultSize) PBYTE  pbResult 
)

Definition at line 141 of file hmac.c.

144{
145 BYTE innerRes[64];
146
147 PCSYMCRYPT_HASH pHash = pState->pKey->pHash;
148
149 SYMCRYPT_ASSERT(sizeof(innerRes) >= pHash->resultSize);
150
152
153 //
154 // We have to buffer the inner hash result. We can't put it directly in the
155 // hash state data buffer as the Result() function wipes that buffer before returning.
156 //
157 SymCryptHashResult( pHash, &pState->hash, innerRes, pHash->resultSize );
158
160
162
163 SymCryptHashAppend( pHash, &pState->hash, innerRes, pHash->resultSize );
164
165 SymCryptHashResult( pHash, &pState->hash, pbResult, pHash->resultSize );
166
167 //
168 // The SymCryptHashResult already wipes the hash state.
169 // We only need to wipe our own buffer.
170 //
171 // We also set the key pointer to NULL. This is not for security;
172 // it creates a clear error when callers forget to call the Init routine
173 // when re-using a state. Rather than the wrong result, they will get
174 // a NULL pointer exception, and they will fix their code.
175 //
176
177 SymCryptWipeKnownSize( innerRes, sizeof( innerRes ) );
178 pState->pKey = NULL;
179}
#define NULL
Definition: types.h:112
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)
Definition: hash.c:193
SYMCRYPT_MD5_CHAINING_STATE outerState
const SYMCRYPT_HASH * PCSYMCRYPT_HASH
UINT32 resultSize
PCSYMCRYPT_HMAC_MD5_EXPANDED_KEY pKey
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList

Referenced by SymCryptHmac(), SymCryptHmacSha3_224Result(), SymCryptHmacSha3_256Result(), SymCryptHmacSha3_384Result(), and SymCryptHmacSha3_512Result().

◆ SymCryptHmacStateCopy()

Definition at line 10 of file hmac.c.

14{
16
17 PCSYMCRYPT_HASH pHash = pSrc->pKey->pHash;
18
19 SymCryptHashStateCopy( pHash, &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}

Referenced by SymCryptHmacSha3_224StateCopy(), SymCryptHmacSha3_256StateCopy(), SymCryptHmacSha3_384StateCopy(), and SymCryptHmacSha3_512StateCopy().