]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Application/Cryptest/HmacVerify.c
CryptoPkg: Add new API to retrieve commonName of X.509 certificate
[mirror_edk2.git] / CryptoPkg / Application / Cryptest / HmacVerify.c
1 /** @file
2 Application for HMAC Primitives Validation.
3
4 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Cryptest.h"
16
17 //
18 // Max Known Digest Size is SHA512 Output (64 bytes) by far
19 //
20 #define MAX_DIGEST_SIZE 64
21
22 //
23 // Data string for HMAC validation
24 //
25 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *HmacData = "Hi There";
26
27 //
28 // Key value for HMAC-MD5 validation. (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
29 //
30 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Key[16] = {
31 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
32 };
33
34 //
35 // Result for HMAC-MD5("Hi There"). (From "2. Test Cases for HMAC-MD5" of IETF RFC2202)
36 //
37 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacMd5Digest[] = {
38 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d
39 };
40
41 //
42 // Key value for HMAC-SHA-1 validation. (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
43 //
44 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Key[20] = {
45 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
46 0x0b, 0x0b, 0x0b, 0x0b
47 };
48
49 //
50 // Result for HMAC-SHA-1 ("Hi There"). (From "3. Test Cases for HMAC-SHA-1" of IETF RFC2202)
51 //
52 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha1Digest[] = {
53 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
54 0xf1, 0x46, 0xbe, 0x00
55 };
56
57 //
58 // Key value for HMAC-SHA-256 validation. (From "4. Test Vectors" of IETF RFC4231)
59 //
60 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Key[20] = {
61 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
62 0x0b, 0x0b, 0x0b, 0x0b
63 };
64
65 //
66 // Result for HMAC-SHA-256 ("Hi There"). (From "4. Test Vectors" of IETF RFC4231)
67 //
68 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 HmacSha256Digest[] = {
69 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
70 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
71 };
72
73 /**
74 Validate UEFI-OpenSSL Message Authentication Codes Interfaces.
75
76 @retval EFI_SUCCESS Validation succeeded.
77 @retval EFI_ABORTED Validation failed.
78
79 **/
80 EFI_STATUS
81 ValidateCryptHmac (
82 VOID
83 )
84 {
85 UINTN CtxSize;
86 VOID *HmacCtx;
87 UINT8 Digest[MAX_DIGEST_SIZE];
88 BOOLEAN Status;
89
90 Print (L" \nUEFI-OpenSSL HMAC Engine Testing:\n");
91
92 Print (L"- HMAC-MD5: ");
93
94 //
95 // HMAC-MD5 Digest Validation
96 //
97 ZeroMem (Digest, MAX_DIGEST_SIZE);
98 CtxSize = HmacMd5GetContextSize ();
99 HmacCtx = AllocatePool (CtxSize);
100
101 Print (L"Init... ");
102 Status = HmacMd5Init (HmacCtx, HmacMd5Key, sizeof (HmacMd5Key));
103 if (!Status) {
104 Print (L"[Fail]");
105 return EFI_ABORTED;
106 }
107
108 Print (L"Update... ");
109 Status = HmacMd5Update (HmacCtx, HmacData, 8);
110 if (!Status) {
111 Print (L"[Fail]");
112 return EFI_ABORTED;
113 }
114
115 Print (L"Finalize... ");
116 Status = HmacMd5Final (HmacCtx, Digest);
117 if (!Status) {
118 Print (L"[Fail]");
119 return EFI_ABORTED;
120 }
121
122 FreePool (HmacCtx);
123
124 Print (L"Check Value... ");
125 if (CompareMem (Digest, HmacMd5Digest, MD5_DIGEST_SIZE) != 0) {
126 Print (L"[Fail]");
127 return EFI_ABORTED;
128 }
129
130 Print (L"[Pass]\n");
131
132 Print (L"- HMAC-SHA1: ");
133
134 //
135 // HMAC-SHA1 Digest Validation
136 //
137 ZeroMem (Digest, MAX_DIGEST_SIZE);
138 CtxSize = HmacSha1GetContextSize ();
139 HmacCtx = AllocatePool (CtxSize);
140
141 Print (L"Init... ");
142 Status = HmacSha1Init (HmacCtx, HmacSha1Key, sizeof (HmacSha1Key));
143 if (!Status) {
144 Print (L"[Fail]");
145 return EFI_ABORTED;
146 }
147
148 Print (L"Update... ");
149 Status = HmacSha1Update (HmacCtx, HmacData, 8);
150 if (!Status) {
151 Print (L"[Fail]");
152 return EFI_ABORTED;
153 }
154
155 Print (L"Finalize... ");
156 Status = HmacSha1Final (HmacCtx, Digest);
157 if (!Status) {
158 Print (L"[Fail]");
159 return EFI_ABORTED;
160 }
161
162 FreePool (HmacCtx);
163
164 Print (L"Check Value... ");
165 if (CompareMem (Digest, HmacSha1Digest, SHA1_DIGEST_SIZE) != 0) {
166 Print (L"[Fail]");
167 return EFI_ABORTED;
168 }
169
170 Print (L"[Pass]\n");
171
172 Print (L"- HMAC-SHA256: ");
173 //
174 // HMAC-SHA-256 Digest Validation
175 //
176 ZeroMem (Digest, MAX_DIGEST_SIZE);
177 CtxSize = HmacSha256GetContextSize ();
178 HmacCtx = AllocatePool (CtxSize);
179
180 Print (L"Init... ");
181 Status = HmacSha256Init (HmacCtx, HmacSha256Key, sizeof (HmacSha256Key));
182 if (!Status) {
183 Print (L"[Fail]");
184 return EFI_ABORTED;
185 }
186
187 Print (L"Update... ");
188 Status = HmacSha256Update (HmacCtx, HmacData, 8);
189 if (!Status) {
190 Print (L"[Fail]");
191 return EFI_ABORTED;
192 }
193
194 Print (L"Finalize... ");
195 Status = HmacSha256Final (HmacCtx, Digest);
196 if (!Status) {
197 Print (L"[Fail]");
198 return EFI_ABORTED;
199 }
200
201 FreePool (HmacCtx);
202
203 Print (L"Check Value... ");
204 if (CompareMem (Digest, HmacSha256Digest, SHA256_DIGEST_SIZE) != 0) {
205 Print (L"[Fail]");
206 return EFI_ABORTED;
207 }
208
209 Print (L"[Pass]\n");
210
211 return EFI_SUCCESS;
212 }