TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptHkdf.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <openssl/evp.h>
11#include <openssl/kdf.h>
12
31BOOLEAN
33 IN CONST EVP_MD *Md,
34 IN CONST UINT8 *Key,
35 IN UINTN KeySize,
36 IN CONST UINT8 *Salt,
37 IN UINTN SaltSize,
38 IN CONST UINT8 *Info,
39 IN UINTN InfoSize,
40 OUT UINT8 *Out,
41 IN UINTN OutSize
42 )
43{
44 EVP_PKEY_CTX *pHkdfCtx;
45 BOOLEAN Result;
46
47 if ((Key == NULL) || (Salt == NULL) || (Info == NULL) || (Out == NULL) ||
48 (KeySize > INT_MAX) || (SaltSize > INT_MAX) || (InfoSize > INT_MAX) || (OutSize > INT_MAX))
49 {
50 return FALSE;
51 }
52
53 pHkdfCtx = EVP_PKEY_CTX_new_id (EVP_PKEY_HKDF, NULL);
54 if (pHkdfCtx == NULL) {
55 return FALSE;
56 }
57
58 Result = EVP_PKEY_derive_init (pHkdfCtx) > 0;
59 if (Result) {
60 Result = EVP_PKEY_CTX_set_hkdf_md (pHkdfCtx, Md) > 0;
61 }
62
63 if (Result) {
64 Result = EVP_PKEY_CTX_set1_hkdf_salt (pHkdfCtx, Salt, (UINT32)SaltSize) > 0;
65 }
66
67 if (Result) {
68 Result = EVP_PKEY_CTX_set1_hkdf_key (pHkdfCtx, Key, (UINT32)KeySize) > 0;
69 }
70
71 if (Result) {
72 Result = EVP_PKEY_CTX_add1_hkdf_info (pHkdfCtx, Info, (UINT32)InfoSize) > 0;
73 }
74
75 if (Result) {
76 Result = EVP_PKEY_derive (pHkdfCtx, Out, &OutSize) > 0;
77 }
78
79 EVP_PKEY_CTX_free (pHkdfCtx);
80 pHkdfCtx = NULL;
81 return Result;
82}
83
100BOOLEAN
102 IN CONST EVP_MD *Md,
103 IN CONST UINT8 *Key,
104 IN UINTN KeySize,
105 IN CONST UINT8 *Salt,
106 IN UINTN SaltSize,
107 OUT UINT8 *PrkOut,
108 UINTN PrkOutSize
109 )
110{
111 EVP_PKEY_CTX *pHkdfCtx;
112 BOOLEAN Result;
113
114 if ((Key == NULL) || (Salt == NULL) || (PrkOut == NULL) ||
115 (KeySize > INT_MAX) || (SaltSize > INT_MAX) ||
116 (PrkOutSize > INT_MAX))
117 {
118 return FALSE;
119 }
120
121 pHkdfCtx = EVP_PKEY_CTX_new_id (EVP_PKEY_HKDF, NULL);
122 if (pHkdfCtx == NULL) {
123 return FALSE;
124 }
125
126 Result = EVP_PKEY_derive_init (pHkdfCtx) > 0;
127 if (Result) {
128 Result = EVP_PKEY_CTX_set_hkdf_md (pHkdfCtx, Md) > 0;
129 }
130
131 if (Result) {
132 Result =
133 EVP_PKEY_CTX_hkdf_mode (
134 pHkdfCtx,
135 EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
136 ) > 0;
137 }
138
139 if (Result) {
140 Result = EVP_PKEY_CTX_set1_hkdf_salt (
141 pHkdfCtx,
142 Salt,
143 (uint32_t)SaltSize
144 ) > 0;
145 }
146
147 if (Result) {
148 Result = EVP_PKEY_CTX_set1_hkdf_key (
149 pHkdfCtx,
150 Key,
151 (uint32_t)KeySize
152 ) > 0;
153 }
154
155 if (Result) {
156 Result = EVP_PKEY_derive (pHkdfCtx, PrkOut, &PrkOutSize) > 0;
157 }
158
159 EVP_PKEY_CTX_free (pHkdfCtx);
160 pHkdfCtx = NULL;
161 return Result;
162}
163
179STATIC
180BOOLEAN
182 IN CONST EVP_MD *Md,
183 IN CONST UINT8 *Prk,
184 IN UINTN PrkSize,
185 IN CONST UINT8 *Info,
186 IN UINTN InfoSize,
187 OUT UINT8 *Out,
188 IN UINTN OutSize
189 )
190{
191 EVP_PKEY_CTX *pHkdfCtx;
192 BOOLEAN Result;
193
194 if ((Prk == NULL) || (Info == NULL) || (Out == NULL) ||
195 (PrkSize > INT_MAX) || (InfoSize > INT_MAX) || (OutSize > INT_MAX))
196 {
197 return FALSE;
198 }
199
200 pHkdfCtx = EVP_PKEY_CTX_new_id (EVP_PKEY_HKDF, NULL);
201 if (pHkdfCtx == NULL) {
202 return FALSE;
203 }
204
205 Result = EVP_PKEY_derive_init (pHkdfCtx) > 0;
206 if (Result) {
207 Result = EVP_PKEY_CTX_set_hkdf_md (pHkdfCtx, Md) > 0;
208 }
209
210 if (Result) {
211 Result = EVP_PKEY_CTX_hkdf_mode (pHkdfCtx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) > 0;
212 }
213
214 if (Result) {
215 Result = EVP_PKEY_CTX_set1_hkdf_key (pHkdfCtx, Prk, (UINT32)PrkSize) > 0;
216 }
217
218 if (Result) {
219 Result = EVP_PKEY_CTX_add1_hkdf_info (pHkdfCtx, Info, (UINT32)InfoSize) > 0;
220 }
221
222 if (Result) {
223 Result = EVP_PKEY_derive (pHkdfCtx, Out, &OutSize) > 0;
224 }
225
226 EVP_PKEY_CTX_free (pHkdfCtx);
227 pHkdfCtx = NULL;
228 return Result;
229}
230
247BOOLEAN
248EFIAPI
250 IN CONST UINT8 *Key,
251 IN UINTN KeySize,
252 IN CONST UINT8 *Salt,
253 IN UINTN SaltSize,
254 IN CONST UINT8 *Info,
255 IN UINTN InfoSize,
256 OUT UINT8 *Out,
257 IN UINTN OutSize
258 )
259{
260 return HkdfMdExtractAndExpand (EVP_sha256 (), Key, KeySize, Salt, SaltSize, Info, InfoSize, Out, OutSize);
261}
262
277BOOLEAN
278EFIAPI
280 IN CONST UINT8 *Key,
281 IN UINTN KeySize,
282 IN CONST UINT8 *Salt,
283 IN UINTN SaltSize,
284 OUT UINT8 *PrkOut,
285 UINTN PrkOutSize
286 )
287{
288 return HkdfMdExtract (
289 EVP_sha256 (),
290 Key,
291 KeySize,
292 Salt,
293 SaltSize,
294 PrkOut,
295 PrkOutSize
296 );
297}
298
313BOOLEAN
314EFIAPI
316 IN CONST UINT8 *Prk,
317 IN UINTN PrkSize,
318 IN CONST UINT8 *Info,
319 IN UINTN InfoSize,
320 OUT UINT8 *Out,
321 IN UINTN OutSize
322 )
323{
324 return HkdfMdExpand (EVP_sha256 (), Prk, PrkSize, Info, InfoSize, Out, OutSize);
325}
326
343BOOLEAN
344EFIAPI
346 IN CONST UINT8 *Key,
347 IN UINTN KeySize,
348 IN CONST UINT8 *Salt,
349 IN UINTN SaltSize,
350 IN CONST UINT8 *Info,
351 IN UINTN InfoSize,
352 OUT UINT8 *Out,
353 IN UINTN OutSize
354 )
355{
356 return HkdfMdExtractAndExpand (EVP_sha384 (), Key, KeySize, Salt, SaltSize, Info, InfoSize, Out, OutSize);
357}
358
373BOOLEAN
374EFIAPI
376 IN CONST UINT8 *Key,
377 IN UINTN KeySize,
378 IN CONST UINT8 *Salt,
379 IN UINTN SaltSize,
380 OUT UINT8 *PrkOut,
381 UINTN PrkOutSize
382 )
383{
384 return HkdfMdExtract (
385 EVP_sha384 (),
386 Key,
387 KeySize,
388 Salt,
389 SaltSize,
390 PrkOut,
391 PrkOutSize
392 );
393}
394
409BOOLEAN
410EFIAPI
412 IN CONST UINT8 *Prk,
413 IN UINTN PrkSize,
414 IN CONST UINT8 *Info,
415 IN UINTN InfoSize,
416 OUT UINT8 *Out,
417 IN UINTN OutSize
418 )
419{
420 return HkdfMdExpand (EVP_sha384 (), Prk, PrkSize, Info, InfoSize, Out, OutSize);
421}
UINT64 UINTN
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 FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284