TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptHmac.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <openssl/hmac.h>
11
20VOID *
22 VOID
23 )
24{
25 //
26 // Allocates & Initializes HMAC_CTX Context by OpenSSL HMAC_CTX_new()
27 //
28 return (VOID *)HMAC_CTX_new ();
29}
30
38VOID
40 IN VOID *HmacMdCtx
41 )
42{
43 //
44 // Free OpenSSL HMAC_CTX Context
45 //
46 HMAC_CTX_free ((HMAC_CTX *)HmacMdCtx);
47}
48
65BOOLEAN
67 IN CONST EVP_MD *Md,
68 OUT VOID *HmacMdContext,
69 IN CONST UINT8 *Key,
70 IN UINTN KeySize
71 )
72{
73 //
74 // Check input parameters.
75 //
76 if ((HmacMdContext == NULL) || (KeySize > INT_MAX)) {
77 return FALSE;
78 }
79
80 if (HMAC_Init_ex ((HMAC_CTX *)HmacMdContext, Key, (UINT32)KeySize, Md, NULL) != 1) {
81 return FALSE;
82 }
83
84 return TRUE;
85}
86
100STATIC
101BOOLEAN
103 IN CONST VOID *HmacMdContext,
104 OUT VOID *NewHmacMdContext
105 )
106{
107 //
108 // Check input parameters.
109 //
110 if ((HmacMdContext == NULL) || (NewHmacMdContext == NULL)) {
111 return FALSE;
112 }
113
114 if (HMAC_CTX_copy ((HMAC_CTX *)NewHmacMdContext, (HMAC_CTX *)HmacMdContext) != 1) {
115 return FALSE;
116 }
117
118 return TRUE;
119}
120
139STATIC
140BOOLEAN
142 IN OUT VOID *HmacMdContext,
143 IN CONST VOID *Data,
144 IN UINTN DataSize
145 )
146{
147 //
148 // Check input parameters.
149 //
150 if (HmacMdContext == NULL) {
151 return FALSE;
152 }
153
154 //
155 // Check invalid parameters, in case that only DataLength was checked in OpenSSL
156 //
157 if ((Data == NULL) && (DataSize != 0)) {
158 return FALSE;
159 }
160
161 //
162 // OpenSSL HMAC-MD digest update
163 //
164 if (HMAC_Update ((HMAC_CTX *)HmacMdContext, Data, DataSize) != 1) {
165 return FALSE;
166 }
167
168 return TRUE;
169}
170
191STATIC
192BOOLEAN
194 IN OUT VOID *HmacMdContext,
195 OUT UINT8 *HmacValue
196 )
197{
198 UINT32 Length;
199
200 //
201 // Check input parameters.
202 //
203 if ((HmacMdContext == NULL) || (HmacValue == NULL)) {
204 return FALSE;
205 }
206
207 //
208 // OpenSSL HMAC-MD digest finalization
209 //
210 if (HMAC_Final ((HMAC_CTX *)HmacMdContext, HmacValue, &Length) != 1) {
211 return FALSE;
212 }
213
214 if (HMAC_CTX_reset ((HMAC_CTX *)HmacMdContext) != 1) {
215 return FALSE;
216 }
217
218 return TRUE;
219}
220
242STATIC
243BOOLEAN
245 IN CONST EVP_MD *Md,
246 IN CONST VOID *Data,
247 IN UINTN DataSize,
248 IN CONST UINT8 *Key,
249 IN UINTN KeySize,
250 OUT UINT8 *HmacValue
251 )
252{
253 UINT32 Length;
254 HMAC_CTX *Ctx;
255 BOOLEAN RetVal;
256
257 Ctx = HMAC_CTX_new ();
258 if (Ctx == NULL) {
259 return FALSE;
260 }
261
262 RetVal = (BOOLEAN)HMAC_CTX_reset (Ctx);
263 if (!RetVal) {
264 goto Done;
265 }
266
267 RetVal = (BOOLEAN)HMAC_Init_ex (Ctx, Key, (UINT32)KeySize, Md, NULL);
268 if (!RetVal) {
269 goto Done;
270 }
271
272 RetVal = (BOOLEAN)HMAC_Update (Ctx, Data, DataSize);
273 if (!RetVal) {
274 goto Done;
275 }
276
277 RetVal = (BOOLEAN)HMAC_Final (Ctx, HmacValue, &Length);
278 if (!RetVal) {
279 goto Done;
280 }
281
282Done:
283 HMAC_CTX_free (Ctx);
284
285 return RetVal;
286}
287
295VOID *
296EFIAPI
298 VOID
299 )
300{
301 return HmacMdNew ();
302}
303
310VOID
311EFIAPI
313 IN VOID *HmacSha256Ctx
314 )
315{
316 HmacMdFree (HmacSha256Ctx);
317}
318
333BOOLEAN
334EFIAPI
336 OUT VOID *HmacSha256Context,
337 IN CONST UINT8 *Key,
338 IN UINTN KeySize
339 )
340{
341 return HmacMdSetKey (EVP_sha256 (), HmacSha256Context, Key, KeySize);
342}
343
357BOOLEAN
358EFIAPI
360 IN CONST VOID *HmacSha256Context,
361 OUT VOID *NewHmacSha256Context
362 )
363{
364 return HmacMdDuplicate (HmacSha256Context, NewHmacSha256Context);
365}
366
385BOOLEAN
386EFIAPI
388 IN OUT VOID *HmacSha256Context,
389 IN CONST VOID *Data,
390 IN UINTN DataSize
391 )
392{
393 return HmacMdUpdate (HmacSha256Context, Data, DataSize);
394}
395
416BOOLEAN
417EFIAPI
419 IN OUT VOID *HmacSha256Context,
420 OUT UINT8 *HmacValue
421 )
422{
423 return HmacMdFinal (HmacSha256Context, HmacValue);
424}
425
446BOOLEAN
447EFIAPI
449 IN CONST VOID *Data,
450 IN UINTN DataSize,
451 IN CONST UINT8 *Key,
452 IN UINTN KeySize,
453 OUT UINT8 *HmacValue
454 )
455{
456 return HmacMdAll (EVP_sha256 (), Data, DataSize, Key, KeySize, HmacValue);
457}
458
466VOID *
467EFIAPI
469 VOID
470 )
471{
472 return HmacMdNew ();
473}
474
481VOID
482EFIAPI
484 IN VOID *HmacSha384Ctx
485 )
486{
487 HmacMdFree (HmacSha384Ctx);
488}
489
506BOOLEAN
507EFIAPI
509 OUT VOID *HmacSha384Context,
510 IN CONST UINT8 *Key,
511 IN UINTN KeySize
512 )
513{
514 return HmacMdSetKey (EVP_sha384 (), HmacSha384Context, Key, KeySize);
515}
516
532BOOLEAN
533EFIAPI
535 IN CONST VOID *HmacSha384Context,
536 OUT VOID *NewHmacSha384Context
537 )
538{
539 return HmacMdDuplicate (HmacSha384Context, NewHmacSha384Context);
540}
541
562BOOLEAN
563EFIAPI
565 IN OUT VOID *HmacSha384Context,
566 IN CONST VOID *Data,
567 IN UINTN DataSize
568 )
569{
570 return HmacMdUpdate (HmacSha384Context, Data, DataSize);
571}
572
595BOOLEAN
596EFIAPI
598 IN OUT VOID *HmacSha384Context,
599 OUT UINT8 *HmacValue
600 )
601{
602 return HmacMdFinal (HmacSha384Context, HmacValue);
603}
604
625BOOLEAN
626EFIAPI
628 IN CONST VOID *Data,
629 IN UINTN DataSize,
630 IN CONST UINT8 *Key,
631 IN UINTN KeySize,
632 OUT UINT8 *HmacValue
633 )
634{
635 return HmacMdAll (EVP_sha384 (), Data, DataSize, Key, KeySize, HmacValue);
636}
UINT64 UINTN
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