]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Test/UnitTest/Library/BaseCryptLib/HmacTests.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Test / UnitTest / Library / BaseCryptLib / HmacTests.c
1 /** @file
2 Application for HMAC Primitives Validation.
3
4 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "TestBaseCryptLib.h"
10
11 //
12 // Max Known Digest Size is SHA512 Output (64 bytes) by far
13 //
14 #define MAX_DIGEST_SIZE 64
15
16 //
17 // Data string for HMAC validation
18 //
19 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HmacData = "Hi There";
20
21 //
22 // Key value for HMAC-MD5 validation. (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
23 //
24 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Key[16] = {
25 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
26 };
27
28 //
29 // Result for HMAC-MD5("Hi There"). (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
30 //
31 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Digest[] = {
32 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d
33 };
34
35 //
36 // Key value for HMAC-SHA-1 validation. (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
37 //
38 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Key[20] = {
39 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
40 0x0b, 0x0b, 0x0b, 0x0b
41 };
42
43 //
44 // Result for HMAC-SHA-1 ("Hi There"). (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
45 //
46 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Digest[] = {
47 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
48 0xf1, 0x46, 0xbe, 0x00
49 };
50
51 //
52 // Key value for HMAC-SHA-256 validation. (From "4. Test Vectors" of IETF RFC4231)
53 //
54 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Key[20] = {
55 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
56 0x0b, 0x0b, 0x0b, 0x0b
57 };
58
59 //
60 // Result for HMAC-SHA-256 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
61 //
62 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Digest[] = {
63 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
64 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
65 };
66
67 typedef
68 VOID *
69 (EFIAPI *EFI_HMAC_NEW)(
70 VOID
71 );
72
73 typedef
74 BOOLEAN
75 (EFIAPI *EFI_HMAC_INIT)(
76 IN OUT VOID *HashContext,
77 IN CONST UINT8 *Key,
78 IN UINTN KeySize
79 );
80
81 typedef
82 BOOLEAN
83 (EFIAPI *EFI_HMAC_UPDATE)(
84 IN OUT VOID *HashContext,
85 IN CONST VOID *Data,
86 IN UINTN DataSize
87 );
88
89 typedef
90 BOOLEAN
91 (EFIAPI *EFI_HMAC_FINAL)(
92 IN OUT VOID *HashContext,
93 OUT UINT8 *HashValue
94 );
95
96 typedef struct {
97 UINT32 DigestSize;
98 EFI_HMAC_NEW HmacNew;
99 EFI_HMAC_INIT HmacInit;
100 EFI_HMAC_UPDATE HmacUpdate;
101 EFI_HMAC_FINAL HmacFinal;
102 CONST UINT8 *Key;
103 UINTN KeySize;
104 CONST UINT8 *Digest;
105 VOID *HmacCtx;
106 } HMAC_TEST_CONTEXT;
107
108 // These functions have been deprecated but they've been left commented out for future reference
109 // HMAC_TEST_CONTEXT mHmacMd5TestCtx = {MD5_DIGEST_SIZE, HmacMd5New, HmacMd5SetKey, HmacMd5Update, HmacMd5Final, HmacMd5Key, sizeof(HmacMd5Key), HmacMd5Digest};
110 // HMAC_TEST_CONTEXT mHmacSha1TestCtx = {SHA1_DIGEST_SIZE, HmacSha1New, HmacSha1SetKey, HmacSha1Update, HmacSha1Final, HmacSha1Key, sizeof(HmacSha1Key), HmacSha1Digest};
111 HMAC_TEST_CONTEXT mHmacSha256TestCtx = { SHA256_DIGEST_SIZE, HmacSha256New, HmacSha256SetKey, HmacSha256Update, HmacSha256Final, HmacSha256Key, sizeof (HmacSha256Key), HmacSha256Digest };
112
113 UNIT_TEST_STATUS
114 EFIAPI
115 TestVerifyHmacPreReq (
116 UNIT_TEST_CONTEXT Context
117 )
118 {
119 HMAC_TEST_CONTEXT *HmacTestContext;
120
121 HmacTestContext = Context;
122 HmacTestContext->HmacCtx = HmacTestContext->HmacNew ();
123 if (HmacTestContext->HmacCtx == NULL) {
124 return UNIT_TEST_ERROR_TEST_FAILED;
125 }
126
127 return UNIT_TEST_PASSED;
128 }
129
130 VOID
131 EFIAPI
132 TestVerifyHmacCleanUp (
133 UNIT_TEST_CONTEXT Context
134 )
135 {
136 HMAC_TEST_CONTEXT *HmacTestContext;
137
138 HmacTestContext = Context;
139 if (HmacTestContext->HmacCtx != NULL) {
140 FreePool (HmacTestContext->HmacCtx);
141 }
142 }
143
144 UNIT_TEST_STATUS
145 EFIAPI
146 TestVerifyHmac (
147 IN UNIT_TEST_CONTEXT Context
148 )
149 {
150 UINT8 Digest[MAX_DIGEST_SIZE];
151 BOOLEAN Status;
152 HMAC_TEST_CONTEXT *HmacTestContext;
153
154 HmacTestContext = Context;
155
156 ZeroMem (Digest, MAX_DIGEST_SIZE);
157
158 Status = HmacTestContext->HmacInit (HmacTestContext->HmacCtx, HmacTestContext->Key, HmacTestContext->KeySize);
159 UT_ASSERT_TRUE (Status);
160
161 Status = HmacTestContext->HmacUpdate (HmacTestContext->HmacCtx, HmacData, 8);
162 UT_ASSERT_TRUE (Status);
163
164 Status = HmacTestContext->HmacFinal (HmacTestContext->HmacCtx, Digest);
165 UT_ASSERT_TRUE (Status);
166
167 UT_ASSERT_MEM_EQUAL (Digest, HmacTestContext->Digest, HmacTestContext->DigestSize);
168
169 return UNIT_TEST_PASSED;
170 }
171
172 TEST_DESC mHmacTest[] = {
173 //
174 // -----Description---------------------Class---------------------Function---------------Pre------------------Post------------Context
175 //
176 { "TestVerifyHmacSha256()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha256TestCtx },
177 // These functions have been deprecated but they've been left commented out for future reference
178 // {"TestVerifyHmacMd5()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacMd5TestCtx},
179 // {"TestVerifyHmacSha1()", "CryptoPkg.BaseCryptLib.Hmac", TestVerifyHmac, TestVerifyHmacPreReq, TestVerifyHmacCleanUp, &mHmacSha1TestCtx},
180 };
181
182 UINTN mHmacTestNum = ARRAY_SIZE (mHmacTest);