ReactOS 0.4.17-dev-1005-g171e1de
pbkdf2.c
Go to the documentation of this file.
1//
2// pbkdf2.c
3//
4// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
5//
6
7//
8// This module contains the routines to implement the pbkdf2 function
9//
10//
11
12#include "precomp.h"
13
18 _In_reads_opt_(cbSalt) PCBYTE pbSalt,
19 SIZE_T cbSalt,
20 UINT64 iterationCnt,
21 _Out_writes_(cbResult) PBYTE pbResult,
22 SIZE_T cbResult)
23{
24 SYMCRYPT_MAC_STATE macState;
25 UINT32 iBlock;
27 SIZE_T blockSize = pExpandedKey->macAlg->resultSize;
31
33 blockSize <= SYMCRYPT_MAC_MAX_RESULT_SIZE &&
34 cbResult > 0 );
35
36 if (iterationCnt == 0)
37 {
38 return SYMCRYPT_WRONG_ITERATION_COUNT;
39 }
40
41 iBlock = 0;
42 while( cbResult > 0 )
43 {
44 iBlock += 1;
45 SYMCRYPT_STORE_MSBFIRST32( &rbBlockResult[0], iBlock ); // use result buf as temp
46
47 pExpandedKey->macAlg->initFunc ( &macState, &pExpandedKey->macKey);
48 pExpandedKey->macAlg->appendFunc( &macState, pbSalt, cbSalt);
49 pExpandedKey->macAlg->appendFunc( &macState, &rbBlockResult[0], 4 ); // block count encoded in 4 bytes
50 pExpandedKey->macAlg->resultFunc( &macState, rbWorkBuffer);
51
52#pragma warning(suppress: 22105)
53 memcpy( rbBlockResult, rbWorkBuffer, blockSize );
54 for( iterations = 1; iterations < iterationCnt; iterations++ )
55 {
56 pExpandedKey->macAlg->initFunc ( &macState, &pExpandedKey->macKey );
57 pExpandedKey->macAlg->appendFunc( &macState, rbWorkBuffer, blockSize );
58 pExpandedKey->macAlg->resultFunc( &macState, rbWorkBuffer );
59 SymCryptXorBytes( &rbWorkBuffer[0], &rbBlockResult[0], &rbBlockResult[0], blockSize );
60 }
61
62 bytes = SYMCRYPT_MIN( cbResult, blockSize );
63 memcpy( pbResult, rbBlockResult, bytes );
64 pbResult += bytes;
65 cbResult -= bytes;
66 }
67
68 SymCryptWipeKnownSize( &rbWorkBuffer[0], sizeof( rbWorkBuffer ) );
69 SymCryptWipeKnownSize( &rbBlockResult[0], sizeof( rbBlockResult ) );
70 return SYMCRYPT_NO_ERROR;
71}
72
80{
82
83 pExpandedKey->macAlg = macAlgorithm;
85}
86
93 _In_reads_opt_(cbSalt) PCBYTE pbSalt,
94 SIZE_T cbSalt,
95 UINT64 iterationCnt,
96 _Out_writes_(cbResult) PBYTE pbResult,
97 SIZE_T cbResult)
98{
100 SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
101
103 if( scError != SYMCRYPT_NO_ERROR )
104 {
105 goto cleanup;
106 }
107
108 scError = SymCryptPbkdf2Derive( &key, pbSalt, cbSalt, iterationCnt, pbResult, cbResult );
109 if( scError != SYMCRYPT_NO_ERROR )
110 {
111 goto cleanup;
112 }
113
114cleanup:
115
116 SymCryptWipeKnownSize( &key, sizeof( key ) );
117
118 return scError;
119
120}
121
122//
123// Self tests are in pbkdf_*.c files
124// to avoid pulling in SHA-1 when only PBKDF-SHA256 is used and
125// similar scenarios.
126//
COMPILER_DEPENDENT_UINT64 UINT64
Definition: actypes.h:131
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
ULONGLONG iterations
Definition: bcrypt.c:539
static void cleanup(void)
Definition: main.c:1335
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define _In_reads_(s)
Definition: no_sal2.h:168
#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
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptPbkdf2ExpandKey(_Out_ PSYMCRYPT_PBKDF2_EXPANDED_KEY pExpandedKey, _In_ PCSYMCRYPT_MAC macAlgorithm, _In_reads_(cbKey) PCBYTE pbKey, SIZE_T cbKey)
Definition: pbkdf2.c:75
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptPbkdf2(PCSYMCRYPT_MAC macAlgorithm, _In_reads_(cbKey) PCBYTE pbKey, SIZE_T cbKey, _In_reads_opt_(cbSalt) PCBYTE pbSalt, SIZE_T cbSalt, UINT64 iterationCnt, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
Definition: pbkdf2.c:89
SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptPbkdf2Derive(_In_ PCSYMCRYPT_PBKDF2_EXPANDED_KEY pExpandedKey, _In_reads_opt_(cbSalt) PCBYTE pbSalt, SIZE_T cbSalt, UINT64 iterationCnt, _Out_writes_(cbResult) PBYTE pbResult, SIZE_T cbResult)
Definition: pbkdf2.c:16
BYTE * PBYTE
Definition: pedump.c:66
static const BYTE pbResult[]
PSYMCRYPT_MAC_EXPAND_KEY expandKeyFunc
Definition: copy.c:22
#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 SymCryptXorBytes(_In_reads_(cbBytes) PCBYTE pbSrc1, _In_reads_(cbBytes) PCBYTE pbSrc2, _Out_writes_(cbBytes) PBYTE pbResult, SIZE_T cbBytes)
Definition: libmain.c:236
#define SYMCRYPT_STORE_MSBFIRST32(p, v)
Definition: symcrypt.h:311
SYMCRYPT_ERROR
Definition: symcrypt.h:227
#define SYMCRYPT_ALIGN
#define SYMCRYPT_CALL
PCBYTE pbKey
PCBYTE SIZE_T cbKey
#define SYMCRYPT_MIN(_a, _b)
const BYTE * PCBYTE
#define SYMCRYPT_MAC_MAX_RESULT_SIZE
PCSYMCRYPT_MAC macAlgorithm
PCVOID pExpandedKey
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t UINT32
Definition: typedefs.h:59
unsigned char BYTE
Definition: xxhash.c:193