]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs5Pbkdf2.c
CryptoPkg/BaseCryptLib: Add NULL pointer checks in DH and P7Verify
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs5Pbkdf2.c
1 /** @file
2 PBKDF2 Key Derivation Function Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalCryptLib.h"
16 #include <openssl/evp.h>
17 #include <openssl/hmac.h>
18
19 /**
20 Derives a key from a password using a salt and iteration count, based on PKCS#5 v2.0
21 password based encryption key derivation function PBKDF2, as specified in RFC 2898.
22
23 If Password or Salt or OutKey is NULL, then return FALSE.
24 If the hash algorithm could not be determined, then return FALSE.
25
26 @param[in] PasswordLength Length of input password in bytes.
27 @param[in] Password Pointer to the array for the password.
28 @param[in] SaltLength Size of the Salt in bytes.
29 @param[in] Salt Pointer to the Salt.
30 @param[in] IterationCount Number of iterations to perform. Its value should be
31 greater than or equal to 1.
32 @param[in] DigestSize Size of the message digest to be used (eg. SHA256_DIGEST_SIZE).
33 NOTE: DigestSize will be used to determine the hash algorithm.
34 Only SHA1_DIGEST_SIZE or SHA256_DIGEST_SIZE is supported.
35 @param[in] KeyLength Size of the derived key buffer in bytes.
36 @param[out] OutKey Pointer to the output derived key buffer.
37
38 @retval TRUE A key was derived successfully.
39 @retval FALSE One of the pointers was NULL or one of the sizes was too large.
40 @retval FALSE The hash algorithm could not be determined from the digest size.
41 @retval FALSE The key derivation operation failed.
42
43 **/
44 BOOLEAN
45 EFIAPI
46 Pkcs5HashPassword (
47 IN UINTN PasswordLength,
48 IN CONST CHAR8 *Password,
49 IN UINTN SaltLength,
50 IN CONST UINT8 *Salt,
51 IN UINTN IterationCount,
52 IN UINTN DigestSize,
53 IN UINTN KeyLength,
54 OUT UINT8 *OutKey
55 )
56 {
57 CONST EVP_MD *HashAlg;
58
59 HashAlg = NULL;
60
61 //
62 // Parameter Checking.
63 //
64 if ((Password == NULL) || (Salt == NULL) || (OutKey == NULL)) {
65 return FALSE;
66 }
67 if ((PasswordLength == 0) || (PasswordLength > INT_MAX) ||
68 (SaltLength == 0) || (SaltLength > INT_MAX) ||
69 (KeyLength == 0) || (KeyLength > INT_MAX) ||
70 (IterationCount < 1) || (IterationCount > INT_MAX)) {
71 return FALSE;
72 }
73 //
74 // Make sure the digest algorithm is supported.
75 //
76 switch (DigestSize) {
77 case SHA1_DIGEST_SIZE:
78 HashAlg = EVP_sha1();
79 break;
80 case SHA256_DIGEST_SIZE:
81 HashAlg = EVP_sha256();
82 break;
83 default:
84 return FALSE;
85 break;
86 }
87
88 //
89 // Perform password-based key derivation routines.
90 //
91 return (BOOLEAN)PKCS5_PBKDF2_HMAC (
92 (const char *)Password,
93 (int)PasswordLength,
94 (const unsigned char *)Salt,
95 (int)SaltLength,
96 (int)IterationCount,
97 HashAlg,
98 (int)KeyLength,
99 (unsigned char *)OutKey
100 );
101 }