TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptHkdf.c
Go to the documentation of this file.
1
11#include "InternalCryptLib.h"
12#include <mbedtls/hkdf.h>
13
32BOOLEAN
34 IN mbedtls_md_type_t MdType,
35 IN CONST UINT8 *Key,
36 IN UINTN KeySize,
37 IN CONST UINT8 *Salt,
38 IN UINTN SaltSize,
39 IN CONST UINT8 *Info,
40 IN UINTN InfoSize,
41 OUT UINT8 *Out,
42 IN UINTN OutSize
43 )
44{
45 const mbedtls_md_info_t *md;
46 INT32 Ret;
47
48 if ((Key == NULL) || (Salt == NULL) || (Info == NULL) || (Out == NULL) ||
49 (KeySize > INT_MAX) || (SaltSize > INT_MAX) || (InfoSize > INT_MAX) || (OutSize > INT_MAX))
50 {
51 return FALSE;
52 }
53
54 md = mbedtls_md_info_from_type (MdType);
55 ASSERT (md != NULL);
56
57 Ret = mbedtls_hkdf (md, Salt, (UINT32)SaltSize, Key, (UINT32)KeySize, Info, (UINT32)InfoSize, Out, (UINT32)OutSize);
58 if (Ret != 0) {
59 return FALSE;
60 }
61
62 return TRUE;
63}
64
81BOOLEAN
83 IN mbedtls_md_type_t MdType,
84 IN CONST UINT8 *Key,
85 IN UINTN KeySize,
86 IN CONST UINT8 *Salt,
87 IN UINTN SaltSize,
88 OUT UINT8 *PrkOut,
89 IN UINTN PrkOutSize
90 )
91{
92 const mbedtls_md_info_t *md;
93 INT32 Ret;
94 UINTN MdSize;
95
96 if ((Key == NULL) || (Salt == NULL) || (PrkOut == NULL) ||
97 (KeySize > INT_MAX) || (SaltSize > INT_MAX) || (PrkOutSize > INT_MAX))
98 {
99 return FALSE;
100 }
101
102 MdSize = 0;
103 switch (MdType) {
104 case MBEDTLS_MD_SHA256:
105 MdSize = SHA256_DIGEST_SIZE;
106 break;
107 case MBEDTLS_MD_SHA384:
108 MdSize = SHA384_DIGEST_SIZE;
109 break;
110 case MBEDTLS_MD_SHA512:
111 MdSize = SHA512_DIGEST_SIZE;
112 break;
113 default:
114 return FALSE;
115 }
116
117 if (PrkOutSize != MdSize) {
118 return FALSE;
119 }
120
121 md = mbedtls_md_info_from_type (MdType);
122 ASSERT (md != NULL);
123
124 Ret = mbedtls_hkdf_extract (md, Salt, (UINT32)SaltSize, Key, (UINT32)KeySize, PrkOut);
125 if (Ret != 0) {
126 return FALSE;
127 }
128
129 return TRUE;
130}
131
147STATIC
148BOOLEAN
150 IN mbedtls_md_type_t MdType,
151 IN CONST UINT8 *Prk,
152 IN UINTN PrkSize,
153 IN CONST UINT8 *Info,
154 IN UINTN InfoSize,
155 OUT UINT8 *Out,
156 IN UINTN OutSize
157 )
158{
159 const mbedtls_md_info_t *md;
160 INT32 Ret;
161 UINTN MdSize;
162
163 if ((Prk == NULL) || (Info == NULL) || (Out == NULL) ||
164 (PrkSize > INT_MAX) || (InfoSize > INT_MAX) || (OutSize > INT_MAX))
165 {
166 return FALSE;
167 }
168
169 switch (MdType) {
170 case MBEDTLS_MD_SHA256:
171 MdSize = SHA256_DIGEST_SIZE;
172 break;
173 case MBEDTLS_MD_SHA384:
174 MdSize = SHA384_DIGEST_SIZE;
175 break;
176 case MBEDTLS_MD_SHA512:
177 MdSize = SHA512_DIGEST_SIZE;
178 break;
179 default:
180 return FALSE;
181 }
182
183 if (PrkSize != MdSize) {
184 return FALSE;
185 }
186
187 md = mbedtls_md_info_from_type (MdType);
188 ASSERT (md != NULL);
189
190 Ret = mbedtls_hkdf_expand (md, Prk, (UINT32)PrkSize, Info, (UINT32)InfoSize, Out, (UINT32)OutSize);
191 if (Ret != 0) {
192 return FALSE;
193 }
194
195 return TRUE;
196}
197
214BOOLEAN
215EFIAPI
217 IN CONST UINT8 *Key,
218 IN UINTN KeySize,
219 IN CONST UINT8 *Salt,
220 IN UINTN SaltSize,
221 IN CONST UINT8 *Info,
222 IN UINTN InfoSize,
223 OUT UINT8 *Out,
224 IN UINTN OutSize
225 )
226{
227 return HkdfMdExtractAndExpand (MBEDTLS_MD_SHA256, Key, KeySize, Salt, SaltSize, Info, InfoSize, Out, OutSize);
228}
229
244BOOLEAN
245EFIAPI
247 IN CONST UINT8 *Key,
248 IN UINTN KeySize,
249 IN CONST UINT8 *Salt,
250 IN UINTN SaltSize,
251 OUT UINT8 *PrkOut,
252 IN UINTN PrkOutSize
253 )
254{
255 return HkdfMdExtract (MBEDTLS_MD_SHA256, Key, KeySize, Salt, SaltSize, PrkOut, PrkOutSize);
256}
257
272BOOLEAN
273EFIAPI
275 IN CONST UINT8 *Prk,
276 IN UINTN PrkSize,
277 IN CONST UINT8 *Info,
278 IN UINTN InfoSize,
279 OUT UINT8 *Out,
280 IN UINTN OutSize
281 )
282{
283 return HkdfMdExpand (MBEDTLS_MD_SHA256, Prk, PrkSize, Info, InfoSize, Out, OutSize);
284}
285
302BOOLEAN
303EFIAPI
305 IN CONST UINT8 *Key,
306 IN UINTN KeySize,
307 IN CONST UINT8 *Salt,
308 IN UINTN SaltSize,
309 IN CONST UINT8 *Info,
310 IN UINTN InfoSize,
311 OUT UINT8 *Out,
312 IN UINTN OutSize
313 )
314{
315 return HkdfMdExtractAndExpand (MBEDTLS_MD_SHA384, Key, KeySize, Salt, SaltSize, Info, InfoSize, Out, OutSize);
316}
317
332BOOLEAN
333EFIAPI
335 IN CONST UINT8 *Key,
336 IN UINTN KeySize,
337 IN CONST UINT8 *Salt,
338 IN UINTN SaltSize,
339 OUT UINT8 *PrkOut,
340 IN UINTN PrkOutSize
341 )
342{
343 return HkdfMdExtract (MBEDTLS_MD_SHA384, Key, KeySize, Salt, SaltSize, PrkOut, PrkOutSize);
344}
345
360BOOLEAN
361EFIAPI
363 IN CONST UINT8 *Prk,
364 IN UINTN PrkSize,
365 IN CONST UINT8 *Info,
366 IN UINTN InfoSize,
367 OUT UINT8 *Out,
368 IN UINTN OutSize
369 )
370{
371 return HkdfMdExpand (MBEDTLS_MD_SHA384, Prk, PrkSize, Info, InfoSize, Out, OutSize);
372}
UINT64 UINTN
#define SHA512_DIGEST_SIZE
Definition: BaseCryptLib.h:54
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
BOOLEAN EFIAPI HkdfSha256Extract(IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Salt, IN UINTN SaltSize, OUT UINT8 *PrkOut, UINTN PrkOutSize)
Definition: CryptHkdf.c:279
STATIC BOOLEAN HkdfMdExpand(IN CONST EVP_MD *Md, IN CONST UINT8 *Prk, IN UINTN PrkSize, IN CONST UINT8 *Info, IN UINTN InfoSize, OUT UINT8 *Out, IN UINTN OutSize)
Definition: CryptHkdf.c:181
BOOLEAN EFIAPI HkdfSha384ExtractAndExpand(IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Salt, IN UINTN SaltSize, IN CONST UINT8 *Info, IN UINTN InfoSize, OUT UINT8 *Out, IN UINTN OutSize)
Definition: CryptHkdf.c:345
STATIC BOOLEAN HkdfMdExtractAndExpand(IN CONST EVP_MD *Md, IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Salt, IN UINTN SaltSize, IN CONST UINT8 *Info, IN UINTN InfoSize, OUT UINT8 *Out, IN UINTN OutSize)
Definition: CryptHkdf.c:32
BOOLEAN EFIAPI HkdfSha384Extract(IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Salt, IN UINTN SaltSize, OUT UINT8 *PrkOut, UINTN PrkOutSize)
Definition: CryptHkdf.c:375
STATIC BOOLEAN HkdfMdExtract(IN CONST EVP_MD *Md, IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Salt, IN UINTN SaltSize, OUT UINT8 *PrkOut, UINTN PrkOutSize)
Definition: CryptHkdf.c:101
BOOLEAN EFIAPI HkdfSha256Expand(IN CONST UINT8 *Prk, IN UINTN PrkSize, IN CONST UINT8 *Info, IN UINTN InfoSize, OUT UINT8 *Out, IN UINTN OutSize)
Definition: CryptHkdf.c:315
BOOLEAN EFIAPI HkdfSha256ExtractAndExpand(IN CONST UINT8 *Key, IN UINTN KeySize, IN CONST UINT8 *Salt, IN UINTN SaltSize, IN CONST UINT8 *Info, IN UINTN InfoSize, OUT UINT8 *Out, IN UINTN OutSize)
Definition: CryptHkdf.c:249
BOOLEAN EFIAPI HkdfSha384Expand(IN CONST UINT8 *Prk, IN UINTN PrkSize, IN CONST UINT8 *Info, IN UINTN InfoSize, OUT UINT8 *Out, IN UINTN OutSize)
Definition: CryptHkdf.c:411
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284