TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptHmac.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <mbedtls/md.h>
11
20VOID *
22 VOID
23 )
24{
25 VOID *HmacMdCtx;
26
27 HmacMdCtx = AllocateZeroPool (sizeof (mbedtls_md_context_t));
28 if (HmacMdCtx == NULL) {
29 return NULL;
30 }
31
32 return HmacMdCtx;
33}
34
41VOID
43 IN VOID *HmacMdCtx
44 )
45{
46 mbedtls_md_free (HmacMdCtx);
47 if (HmacMdCtx != NULL) {
48 FreePool (HmacMdCtx);
49 }
50}
51
68BOOLEAN
70 IN mbedtls_md_type_t MdType,
71 OUT VOID *HmacMdContext,
72 IN CONST UINT8 *Key,
73 IN UINTN KeySize
74 )
75{
76 const mbedtls_md_info_t *md_info;
77 INT32 Ret;
78
79 if ((HmacMdContext == NULL) || (KeySize > INT_MAX)) {
80 return FALSE;
81 }
82
83 ZeroMem (HmacMdContext, sizeof (mbedtls_md_context_t));
84 mbedtls_md_init (HmacMdContext);
85
86 md_info = mbedtls_md_info_from_type (MdType);
87 ASSERT (md_info != NULL);
88
89 Ret = mbedtls_md_setup (HmacMdContext, md_info, 1);
90 if (Ret != 0) {
91 return FALSE;
92 }
93
94 Ret = mbedtls_md_hmac_starts (HmacMdContext, Key, KeySize);
95 if (Ret != 0) {
96 return FALSE;
97 }
98
99 return TRUE;
100}
101
110int
112 mbedtls_md_type_t MdType
113 )
114{
115 switch (MdType) {
116 case MBEDTLS_MD_SHA256:
117 return 64;
118 case MBEDTLS_MD_SHA384:
119 return 128;
120 default:
121 ASSERT (FALSE);
122 return 0;
123 }
124}
125
140STATIC
141BOOLEAN
143 IN CONST mbedtls_md_type_t MdType,
144 IN CONST VOID *HmacMdContext,
145 OUT VOID *NewHmacMdContext
146 )
147{
148 INT32 Ret;
149 CONST mbedtls_md_info_t *md_info;
150 mbedtls_md_context_t *MdContext;
151
152 if ((HmacMdContext == NULL) || (NewHmacMdContext == NULL)) {
153 return FALSE;
154 }
155
156 ZeroMem (NewHmacMdContext, sizeof (mbedtls_md_context_t));
157 mbedtls_md_init (NewHmacMdContext);
158 md_info = mbedtls_md_info_from_type (MdType);
159 ASSERT (md_info != NULL);
160
161 Ret = mbedtls_md_setup (NewHmacMdContext, md_info, 1);
162 if (Ret != 0) {
163 return FALSE;
164 }
165
166 MdContext = (mbedtls_md_context_t *)NewHmacMdContext;
167
168 Ret = mbedtls_md_clone (NewHmacMdContext, HmacMdContext);
169 if (Ret != 0) {
170 if (MdContext->md_ctx != NULL) {
171 mbedtls_free (MdContext->md_ctx);
172 }
173
174 if (MdContext->hmac_ctx != NULL) {
175 mbedtls_free (MdContext->hmac_ctx);
176 }
177
178 return FALSE;
179 }
180
181 CopyMem (
182 ((mbedtls_md_context_t *)NewHmacMdContext)->hmac_ctx,
183 ((CONST mbedtls_md_context_t *)HmacMdContext)->hmac_ctx,
184 HmacMdGetBlockSize (MdType) * 2
185 );
186
187 return TRUE;
188}
189
208STATIC
209BOOLEAN
211 IN OUT VOID *HmacMdContext,
212 IN CONST VOID *Data,
213 IN UINTN DataSize
214 )
215{
216 INT32 Ret;
217
218 if (HmacMdContext == NULL) {
219 return FALSE;
220 }
221
222 if ((Data == NULL) && (DataSize != 0)) {
223 return FALSE;
224 }
225
226 if (DataSize > INT_MAX) {
227 return FALSE;
228 }
229
230 Ret = mbedtls_md_hmac_update (HmacMdContext, Data, DataSize);
231 if (Ret != 0) {
232 return FALSE;
233 }
234
235 return TRUE;
236}
237
258STATIC
259BOOLEAN
261 IN OUT VOID *HmacMdContext,
262 OUT UINT8 *HmacValue
263 )
264{
265 INT32 Ret;
266
267 if ((HmacMdContext == NULL) || (HmacValue == NULL)) {
268 return FALSE;
269 }
270
271 Ret = mbedtls_md_hmac_finish (HmacMdContext, HmacValue);
272 if (Ret != 0) {
273 return FALSE;
274 }
275
276 Ret = mbedtls_md_hmac_reset (HmacMdContext);
277 if (Ret != 0) {
278 return FALSE;
279 }
280
281 return TRUE;
282}
283
305STATIC
306BOOLEAN
308 IN mbedtls_md_type_t MdType,
309 IN CONST VOID *Data,
310 IN UINTN DataSize,
311 IN CONST UINT8 *Key,
312 IN UINTN KeySize,
313 OUT UINT8 *HmacValue
314 )
315{
316 const mbedtls_md_info_t *md_info;
317 INT32 Ret;
318
319 md_info = mbedtls_md_info_from_type (MdType);
320 ASSERT (md_info != NULL);
321
322 Ret = mbedtls_md_hmac (md_info, Key, KeySize, Data, DataSize, HmacValue);
323 if (Ret != 0) {
324 return FALSE;
325 }
326
327 return TRUE;
328}
329
337VOID *
338EFIAPI
340 VOID
341 )
342{
343 return HmacMdNew ();
344}
345
352VOID
353EFIAPI
355 IN VOID *HmacSha256Ctx
356 )
357{
358 HmacMdFree (HmacSha256Ctx);
359}
360
375BOOLEAN
376EFIAPI
378 OUT VOID *HmacSha256Context,
379 IN CONST UINT8 *Key,
380 IN UINTN KeySize
381 )
382{
383 return HmacMdSetKey (MBEDTLS_MD_SHA256, HmacSha256Context, Key, KeySize);
384}
385
399BOOLEAN
400EFIAPI
402 IN CONST VOID *HmacSha256Context,
403 OUT VOID *NewHmacSha256Context
404 )
405{
406 return HmacMdDuplicate (MBEDTLS_MD_SHA256, HmacSha256Context, NewHmacSha256Context);
407}
408
427BOOLEAN
428EFIAPI
430 IN OUT VOID *HmacSha256Context,
431 IN CONST VOID *Data,
432 IN UINTN DataSize
433 )
434{
435 return HmacMdUpdate (HmacSha256Context, Data, DataSize);
436}
437
458BOOLEAN
459EFIAPI
461 IN OUT VOID *HmacSha256Context,
462 OUT UINT8 *HmacValue
463 )
464{
465 return HmacMdFinal (HmacSha256Context, HmacValue);
466}
467
488BOOLEAN
489EFIAPI
491 IN CONST VOID *Data,
492 IN UINTN DataSize,
493 IN CONST UINT8 *Key,
494 IN UINTN KeySize,
495 OUT UINT8 *HmacValue
496 )
497{
498 return HmacMdAll (MBEDTLS_MD_SHA256, Data, DataSize, Key, KeySize, HmacValue);
499}
500
508VOID *
509EFIAPI
511 VOID
512 )
513{
514 return HmacMdNew ();
515}
516
523VOID
524EFIAPI
526 IN VOID *HmacSha384Ctx
527 )
528{
529 HmacMdFree (HmacSha384Ctx);
530}
531
548BOOLEAN
549EFIAPI
551 OUT VOID *HmacSha384Context,
552 IN CONST UINT8 *Key,
553 IN UINTN KeySize
554 )
555{
556 return HmacMdSetKey (MBEDTLS_MD_SHA384, HmacSha384Context, Key, KeySize);
557}
558
574BOOLEAN
575EFIAPI
577 IN CONST VOID *HmacSha384Context,
578 OUT VOID *NewHmacSha384Context
579 )
580{
581 return HmacMdDuplicate (MBEDTLS_MD_SHA384, HmacSha384Context, NewHmacSha384Context);
582}
583
604BOOLEAN
605EFIAPI
607 IN OUT VOID *HmacSha384Context,
608 IN CONST VOID *Data,
609 IN UINTN DataSize
610 )
611{
612 return HmacMdUpdate (HmacSha384Context, Data, DataSize);
613}
614
637BOOLEAN
638EFIAPI
640 IN OUT VOID *HmacSha384Context,
641 OUT UINT8 *HmacValue
642 )
643{
644 return HmacMdFinal (HmacSha384Context, HmacValue);
645}
646
667BOOLEAN
668EFIAPI
670 IN CONST VOID *Data,
671 IN UINTN DataSize,
672 IN CONST UINT8 *Key,
673 IN UINTN KeySize,
674 OUT UINT8 *HmacValue
675 )
676{
677 return HmacMdAll (MBEDTLS_MD_SHA384, Data, DataSize, Key, KeySize, HmacValue);
678}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
BOOLEAN EFIAPI HmacSha384Update(IN OUT VOID *HmacSha384Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptHmac.c:564
STATIC VOID * HmacMdNew(VOID)
Definition: CryptHmac.c:21
BOOLEAN EFIAPI HmacSha384SetKey(OUT VOID *HmacSha384Context, IN CONST UINT8 *Key, IN UINTN KeySize)
Definition: CryptHmac.c:508
STATIC VOID HmacMdFree(IN VOID *HmacMdCtx)
Definition: CryptHmac.c:39
VOID *EFIAPI HmacSha256New(VOID)
Definition: CryptHmac.c:297
BOOLEAN EFIAPI HmacSha256Duplicate(IN CONST VOID *HmacSha256Context, OUT VOID *NewHmacSha256Context)
Definition: CryptHmac.c:359
VOID *EFIAPI HmacSha384New(VOID)
Definition: CryptHmac.c:468
BOOLEAN EFIAPI HmacSha384All(IN CONST VOID *Data, IN UINTN DataSize, IN CONST UINT8 *Key, IN UINTN KeySize, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:627
VOID EFIAPI HmacSha384Free(IN VOID *HmacSha384Ctx)
Definition: CryptHmac.c:483
VOID EFIAPI HmacSha256Free(IN VOID *HmacSha256Ctx)
Definition: CryptHmac.c:312
BOOLEAN EFIAPI HmacSha384Duplicate(IN CONST VOID *HmacSha384Context, OUT VOID *NewHmacSha384Context)
Definition: CryptHmac.c:534
BOOLEAN EFIAPI HmacSha384Final(IN OUT VOID *HmacSha384Context, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:597
BOOLEAN EFIAPI HmacSha256SetKey(OUT VOID *HmacSha256Context, IN CONST UINT8 *Key, IN UINTN KeySize)
Definition: CryptHmac.c:335
BOOLEAN EFIAPI HmacSha256Update(IN OUT VOID *HmacSha256Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptHmac.c:387
STATIC BOOLEAN HmacMdFinal(IN OUT VOID *HmacMdContext, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:193
BOOLEAN EFIAPI HmacSha256All(IN CONST VOID *Data, IN UINTN DataSize, IN CONST UINT8 *Key, IN UINTN KeySize, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:448
STATIC BOOLEAN HmacMdAll(IN CONST EVP_MD *Md, IN CONST VOID *Data, IN UINTN DataSize, IN CONST UINT8 *Key, IN UINTN KeySize, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:244
STATIC BOOLEAN HmacMdDuplicate(IN CONST VOID *HmacMdContext, OUT VOID *NewHmacMdContext)
Definition: CryptHmac.c:102
BOOLEAN EFIAPI HmacSha256Final(IN OUT VOID *HmacSha256Context, OUT UINT8 *HmacValue)
Definition: CryptHmac.c:418
STATIC BOOLEAN HmacMdUpdate(IN OUT VOID *HmacMdContext, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptHmac.c:141
STATIC BOOLEAN HmacMdSetKey(IN CONST EVP_MD *Md, OUT VOID *HmacMdContext, IN CONST UINT8 *Key, IN UINTN KeySize)
Definition: CryptHmac.c:66
#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
int HmacMdGetBlockSize(mbedtls_md_type_t MdType)
Definition: CryptHmac.c:111