ReactOS 0.4.17-dev-1005-g171e1de
hmac.c
Go to the documentation of this file.
1//
2// hmac.c
3// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
4//
5
6#include "precomp.h"
7
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}
33
34VOID
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}
50
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}
110
111
112SYMCRYPT_NOINLINE
113VOID
118{
120
121 SymCryptHashStateCopy( pExpandedKey->pHash, &pExpandedKey->innerState, &pState->hash );
122
123 pState->pKey = pExpandedKey;
124
126}
127
128VOID
133 SIZE_T cbData )
134{
135 SymCryptHashAppend( pState->pKey->pHash, &pState->hash, pbData, cbData );
136}
137
138SYMCRYPT_NOINLINE
139VOID
143 _Out_writes_( pState->pKey->pHash->resultSize ) PBYTE pbResult )
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
161 SymCryptHashStateCopy( pHash, &pState->pKey->outerState, &pState->hash );
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}
180
181SYMCRYPT_NOINLINE
182VOID
188 _Out_writes_( pExpandedKey->pHash->resultSize ) PBYTE pbResult )
189{
191
195}
static int state
Definition: maze.c:121
#define NULL
Definition: types.h:112
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 SymCryptHmacKeyCopy(_In_ PCSYMCRYPT_HMAC_EXPANDED_KEY pSrc, _Out_ PSYMCRYPT_HMAC_EXPANDED_KEY pDst)
Definition: hmac.c:36
VOID SYMCRYPT_CALL SymCryptHmacAppend(_Inout_ PSYMCRYPT_HMAC_STATE pState, _In_reads_(cbData) PCBYTE pbData, SIZE_T cbData)
Definition: hmac.c:130
VOID SYMCRYPT_CALL SymCryptHmacStateCopy(_In_ PCSYMCRYPT_HMAC_STATE pSrc, _In_opt_ PCSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey, _Out_ PSYMCRYPT_HMAC_STATE pDst)
Definition: hmac.c:10
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: hmac.c:53
SYMCRYPT_NOINLINE VOID SYMCRYPT_CALL SymCryptHmacInit(_Out_ PSYMCRYPT_HMAC_STATE pState, _In_ PCSYMCRYPT_HMAC_EXPANDED_KEY pExpandedKey)
Definition: hmac.c:115
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: hmac.c:184
#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 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
static const BYTE pbResult[]
#define SYMCRYPT_ASSERT(_x)
Definition: symcrypt.h:10807
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
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
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_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
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_ERROR
Definition: symcrypt.h:227
#define SYMCRYPT_ALIGN
SYMCRYPT_MAGIC_FIELD * PSYMCRYPT_HMAC_EXPANDED_KEY
const SYMCRYPT_HMAC_EXPANDED_KEY * PCSYMCRYPT_HMAC_EXPANDED_KEY
#define SYMCRYPT_CALL
PCBYTE pbKey
SYMCRYPT_MAGIC_FIELD SYMCRYPT_HMAC_STATE
PCBYTE SIZE_T cbKey
const SYMCRYPT_HASH * PCSYMCRYPT_HASH
PCBYTE PBYTE SIZE_T cbData
SYMCRYPT_MAGIC_FIELD * PSYMCRYPT_HMAC_STATE
#define SYMCRYPT_SET_MAGIC(p)
UINT32 inputBlockSize
PSYMCRYPT_COMMON_HASH_STATE pState
const BYTE * PCBYTE
const SYMCRYPT_HMAC_STATE * PCSYMCRYPT_HMAC_STATE
PCBYTE pbData
#define SYMCRYPT_CHECK_MAGIC(p)
PCVOID pExpandedKey
ULONG_PTR SIZE_T
Definition: typedefs.h:80
unsigned char BYTE
Definition: xxhash.c:193