TianoCore EDK2 master
Loading...
Searching...
No Matches
EcTests.c
Go to the documentation of this file.
1
9#include "TestBaseCryptLib.h"
10
11#define EC_CURVE_NUM_SUPPORTED 3
12UINTN EcCurveList[EC_CURVE_NUM_SUPPORTED] = { CRYPTO_NID_SECP256R1, CRYPTO_NID_SECP384R1, CRYPTO_NID_SECP521R1 };
13UINTN EcKeyHalfSize[EC_CURVE_NUM_SUPPORTED] = { 32, 48, 66 };
14
15struct Generator {
16 UINT8 X[66];
17 UINT8 Y[66];
18};
19
20// Generator points of all ec curve
21struct Generator EcCurveGenerator[EC_CURVE_NUM_SUPPORTED] =
22{
23 // CRYPTO_NID_SECP256R1
24 {
25 { 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8, 0xBC, 0xE6, 0xE5,
26 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
27 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96 },
28
29 { 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a,
30 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
31 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5 }
32 },
33 // CRYPTO_NID_SECP384R1
34 {
35 { 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E,
36 0xF3, 0x20, 0xAD, 0x74, 0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B, 0x98,
37 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A, 0x38, 0x55, 0x02, 0xF2, 0x5D,
38 0xBF, 0x55, 0x29, 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A, 0xB7 },
39
40 { 0x36, 0x17, 0xde, 0x4a, 0x96, 0x26, 0x2c, 0x6f, 0x5d, 0x9e, 0x98, 0xbf,
41 0x92, 0x92, 0xdc, 0x29, 0xf8, 0xf4, 0x1d, 0xbd, 0x28, 0x9a, 0x14, 0x7c,
42 0xe9, 0xda, 0x31, 0x13, 0xb5, 0xf0, 0xb8, 0xc0, 0x0a, 0x60, 0xb1, 0xce,
43 0x1d, 0x7e, 0x81, 0x9d, 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f }
44 },
45 // CRYPTO_NID_SECP521R1
46 {
47 { 0x00, 0xC6, 0x85, 0x8E, 0x06, 0xB7, 0x04, 0x04, 0xE9, 0xCD, 0x9E, 0x3E,
48 0xCB, 0x66, 0x23, 0x95, 0xB4, 0x42, 0x9C, 0x64, 0x81, 0x39, 0x05, 0x3F,
49 0xB5, 0x21, 0xF8, 0x28, 0xAF, 0x60, 0x6B, 0x4D, 0x3D, 0xBA, 0xA1, 0x4B,
50 0x5E, 0x77, 0xEF, 0xE7, 0x59, 0x28, 0xFE, 0x1D, 0xC1, 0x27, 0xA2, 0xFF,
51 0xA8, 0xDE, 0x33, 0x48, 0xB3, 0xC1, 0x85, 0x6A, 0x42, 0x9B, 0xF9, 0x7E,
52 0x7E, 0x31, 0xC2, 0xE5, 0xBD, 0x66 },
53
54 { 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a,
55 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b,
56 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee,
57 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad,
58 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe,
59 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50 }
60 }
61};
62
63//
64// Root CA X509 Certificate for X509 Verification Routine (Generated by OpenSSL utility).
65//
66GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 mEccTestRootCer[] = {
67 0x30, 0x82, 0x01, 0xd2, 0x30, 0x82, 0x01, 0x77, 0xa0, 0x03, 0x02, 0x01,
68 0x02, 0x02, 0x09, 0x00, 0xcc, 0x10, 0x45, 0x50, 0xaf, 0x50, 0x1b, 0xe2,
69 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
70 0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
71 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
72 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
73 0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49,
74 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67,
75 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30,
76 0x1e, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x32, 0x32, 0x37, 0x31, 0x32, 0x30,
77 0x32, 0x31, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x30, 0x30, 0x32, 0x32, 0x34,
78 0x31, 0x32, 0x30, 0x32, 0x31, 0x30, 0x5a, 0x30, 0x45, 0x31, 0x0b, 0x30,
79 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13,
80 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
81 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1f, 0x06,
82 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
83 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50,
84 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,
85 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48,
86 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x11, 0xa4, 0x06,
87 0x65, 0xb6, 0x79, 0x6e, 0x72, 0xb6, 0xd8, 0x09, 0x84, 0x92, 0x86, 0x11,
88 0x09, 0xde, 0xea, 0xd0, 0x0c, 0x60, 0xf1, 0x8a, 0xff, 0x7c, 0xde, 0xce,
89 0xec, 0x07, 0xba, 0xa5, 0xb8, 0xd5, 0x17, 0xe5, 0x62, 0x33, 0x2d, 0x88,
90 0xb1, 0x9a, 0xe6, 0xf3, 0x09, 0x43, 0x0e, 0xa9, 0xf7, 0x3c, 0xe9, 0x20,
91 0xba, 0xbd, 0xb1, 0x3c, 0x03, 0x89, 0x1e, 0x2a, 0xff, 0x6e, 0x08, 0xff,
92 0x2e, 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
93 0x04, 0x16, 0x04, 0x14, 0x62, 0xe6, 0xd9, 0xa0, 0xee, 0x38, 0x18, 0x83,
94 0xfa, 0xe3, 0xed, 0x44, 0xa4, 0x37, 0xfd, 0x4a, 0x04, 0xdf, 0xe1, 0xd5,
95 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
96 0x14, 0x62, 0xe6, 0xd9, 0xa0, 0xee, 0x38, 0x18, 0x83, 0xfa, 0xe3, 0xed,
97 0x44, 0xa4, 0x37, 0xfd, 0x4a, 0x04, 0xdf, 0xe1, 0xd5, 0x30, 0x0c, 0x06,
98 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30,
99 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03,
100 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0x99, 0x2f, 0x43, 0xeb, 0xdc,
101 0x4e, 0x53, 0xc7, 0xc1, 0xbd, 0xed, 0x95, 0xdc, 0xae, 0xd3, 0x75, 0xfa,
102 0xc4, 0xf7, 0xa4, 0x61, 0x00, 0x57, 0xce, 0xf3, 0xe0, 0x23, 0xf6, 0xf0,
103 0x41, 0x6f, 0xb5, 0x02, 0x21, 0x00, 0xf3, 0x97, 0x11, 0x06, 0x61, 0x10,
104 0xc7, 0x35, 0xe9, 0xf8, 0x3b, 0x59, 0xec, 0xf5, 0x51, 0xa0, 0xa6, 0x64,
105 0x6e, 0xe1, 0x44, 0xc7, 0xe1, 0xa2, 0xce, 0x90, 0x7f, 0xae, 0xad, 0xf4,
106 0xa9, 0xfa,
107};
108
109//
110// PEM key data for EC Private key Retrieving. (Generated by OpenSSL utility).
111//
112GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 mEccTestPemKey[] = {
113 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45,
114 0x43, 0x20, 0x50, 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53,
115 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71, 0x68, 0x6b,
116 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d, 0x0a, 0x2d,
117 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x45, 0x43, 0x20, 0x50,
118 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
119 0x2d, 0x2d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49,
120 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45,
121 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x48,
122 0x63, 0x43, 0x41, 0x51, 0x45, 0x45, 0x49, 0x4d, 0x4a, 0x54, 0x69, 0x75,
123 0x34, 0x56, 0x54, 0x54, 0x57, 0x68, 0x78, 0x4b, 0x62, 0x51, 0x65, 0x78,
124 0x6e, 0x30, 0x43, 0x58, 0x41, 0x77, 0x33, 0x44, 0x57, 0x6b, 0x6f, 0x78,
125 0x79, 0x77, 0x6b, 0x7a, 0x46, 0x50, 0x62, 0x32, 0x48, 0x68, 0x5a, 0x6e,
126 0x5a, 0x52, 0x6f, 0x41, 0x6f, 0x47, 0x43, 0x43, 0x71, 0x47, 0x53, 0x4d,
127 0x34, 0x39, 0x0a, 0x41, 0x77, 0x45, 0x48, 0x6f, 0x55, 0x51, 0x44, 0x51,
128 0x67, 0x41, 0x45, 0x45, 0x61, 0x51, 0x47, 0x5a, 0x62, 0x5a, 0x35, 0x62,
129 0x6e, 0x4b, 0x32, 0x32, 0x41, 0x6d, 0x45, 0x6b, 0x6f, 0x59, 0x52, 0x43,
130 0x64, 0x37, 0x71, 0x30, 0x41, 0x78, 0x67, 0x38, 0x59, 0x72, 0x2f, 0x66,
131 0x4e, 0x37, 0x4f, 0x37, 0x41, 0x65, 0x36, 0x70, 0x62, 0x6a, 0x56, 0x46,
132 0x2b, 0x56, 0x69, 0x4d, 0x79, 0x32, 0x49, 0x0a, 0x73, 0x5a, 0x72, 0x6d,
133 0x38, 0x77, 0x6c, 0x44, 0x44, 0x71, 0x6e, 0x33, 0x50, 0x4f, 0x6b, 0x67,
134 0x75, 0x72, 0x32, 0x78, 0x50, 0x41, 0x4f, 0x4a, 0x48, 0x69, 0x72, 0x2f,
135 0x62, 0x67, 0x6a, 0x2f, 0x4c, 0x67, 0x3d, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d,
136 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x45, 0x43, 0x20, 0x50, 0x52, 0x49,
137 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d,
138 0x2d, 0x0a,
139};
140
141//
142// Payload for PKCS#7 Signing & Verification Validation.
143//
144CONST CHAR8 *mEcPayload = "payload data for PKCS#7 EC Signing";
145
146VOID *Ec1;
147VOID *Ec2;
148VOID *Group;
149VOID *Point1;
150VOID *Point2;
151VOID *PointRes;
152VOID *BnX;
153VOID *BnY;
154VOID *BnP;
155VOID *BnOrder;
156
158EFIAPI
159TestVerifyEcPreReq (
160 UNIT_TEST_CONTEXT Context
161 )
162{
163 Ec1 = NULL;
164 Ec2 = NULL;
165 Group = NULL;
166 Point1 = NULL;
167 Point2 = NULL;
168 PointRes = NULL;
169 BnX = NULL;
170 BnY = NULL;
171 BnP = BigNumInit ();
172 BnOrder = BigNumInit ();
173 if ((BnP == NULL) || (BnOrder == NULL)) {
174 return UNIT_TEST_ERROR_TEST_FAILED;
175 }
176
177 return UNIT_TEST_PASSED;
178}
179
180VOID
181EFIAPI
182TestVerifyEcCleanUp (
183 UNIT_TEST_CONTEXT Context
184 )
185{
186 BigNumFree (BnX, TRUE);
187 BigNumFree (BnY, TRUE);
188 BigNumFree (BnP, TRUE);
189 BigNumFree (BnOrder, TRUE);
190 EcGroupFree (Group);
191 EcPointDeInit (Point1, TRUE);
192 EcPointDeInit (Point2, TRUE);
193 EcPointDeInit (PointRes, TRUE);
194 EcFree (Ec1);
195 EcFree (Ec2);
196}
197
199EFIAPI
200TestVerifyEcBasic (
201 UNIT_TEST_CONTEXT Context
202 )
203{
204 UINTN CurveCount;
205 BOOLEAN Status;
206
207 //
208 // Initialize BigNumbers
209 //
210 for (CurveCount = 0; CurveCount < EC_CURVE_NUM_SUPPORTED; CurveCount++) {
211 //
212 // Basic EC functions unit test
213 //
214 Group = EcGroupInit (EcCurveList[CurveCount]);
215 if (Group == NULL) {
216 return UNIT_TEST_ERROR_TEST_FAILED;
217 }
218
219 Point1 = EcPointInit (Group);
220 Point2 = EcPointInit (Group);
221 PointRes = EcPointInit (Group);
222 BnX = BigNumFromBin (EcCurveGenerator[CurveCount].X, EcKeyHalfSize[CurveCount]);
223 BnY = BigNumFromBin (EcCurveGenerator[CurveCount].Y, EcKeyHalfSize[CurveCount]);
224 if ((Point1 == NULL) || (Point2 == NULL) || (PointRes == NULL) || (BnX == NULL) || (BnY == NULL)) {
225 return UNIT_TEST_ERROR_TEST_FAILED;
226 }
227
228 Status = EcGroupGetCurve (Group, BnP, NULL, NULL, NULL);
229 UT_ASSERT_TRUE (Status);
230
231 Status = EcGroupGetOrder (Group, BnOrder);
232 UT_ASSERT_TRUE (Status);
233
234 // Point G should on curve
235 Status = EcPointSetAffineCoordinates (Group, Point1, BnX, BnY, NULL);
236 UT_ASSERT_TRUE (Status);
237
238 Status = EcPointSetAffineCoordinates (Group, Point2, BnX, BnY, NULL);
239 UT_ASSERT_TRUE (Status);
240
241 Status = EcPointEqual (Group, Point1, Point2, NULL);
242 UT_ASSERT_TRUE (Status);
243
244 Status = EcPointIsOnCurve (Group, Point1, NULL);
245 UT_ASSERT_TRUE (Status);
246
247 Status = EcPointIsAtInfinity (Group, Point1);
248 UT_ASSERT_FALSE (Status);
249
250 // Point 2G should on curve
251 Status = EcPointAdd (Group, PointRes, Point1, Point1, NULL);
252 UT_ASSERT_TRUE (Status);
253
254 Status = EcPointIsOnCurve (Group, PointRes, NULL);
255 UT_ASSERT_TRUE (Status);
256
257 // Point Order * G should at infinity
258 Status = EcPointMul (Group, PointRes, Point1, BnOrder, NULL);
259 UT_ASSERT_TRUE (Status);
260
261 Status = EcPointIsAtInfinity (Group, PointRes);
262 UT_ASSERT_TRUE (Status);
263
264 // -(-G) == G
265 Status = EcPointInvert (Group, Point2, NULL);
266 UT_ASSERT_TRUE (Status);
267
268 Status = EcPointEqual (Group, Point2, Point1, NULL);
269 UT_ASSERT_FALSE (Status);
270
271 Status = EcPointInvert (Group, Point2, NULL);
272 UT_ASSERT_TRUE (Status);
273
274 Status = EcPointEqual (Group, Point2, Point1, NULL);
275 UT_ASSERT_TRUE (Status);
276
277 // Compress point test
278 Status = EcPointSetCompressedCoordinates (Group, Point1, BnX, 0, NULL);
279 UT_ASSERT_TRUE (Status);
280
281 Status = EcPointSetCompressedCoordinates (Group, Point2, BnX, 1, NULL);
282 UT_ASSERT_TRUE (Status);
283
284 Status = EcPointEqual (Group, Point2, Point1, NULL);
285 UT_ASSERT_FALSE (Status);
286
287 Status = EcPointInvert (Group, Point2, NULL);
288 UT_ASSERT_TRUE (Status);
289
290 Status = EcPointEqual (Group, Point2, Point1, NULL);
291 UT_ASSERT_TRUE (Status);
292 }
293
294 return UNIT_TEST_PASSED;
295}
296
298EFIAPI
299TestVerifyEcDh (
300 UNIT_TEST_CONTEXT Context
301 )
302{
303 UINT8 Public1[66 * 2];
304 UINTN Public1Length;
305 UINT8 Public2[66 * 2];
306 UINTN Public2Length;
307 UINT8 Key1[66];
308 UINTN Key1Length;
309 UINT8 Key2[66];
310 UINTN Key2Length;
311 UINTN CurveCount;
312 BOOLEAN Status;
313
314 for (CurveCount = 0; CurveCount < EC_CURVE_NUM_SUPPORTED; CurveCount++) {
315 //
316 // Initial key length
317 //
318 Public1Length = sizeof (Public1);
319 Public2Length = sizeof (Public2);
320 Key1Length = sizeof (Key1);
321 Key2Length = sizeof (Key2);
322 //
323 // ECDH functions unit test
324 //
325 Ec1 = EcNewByNid (EcCurveList[CurveCount]);
326 if (Ec1 == NULL) {
327 return UNIT_TEST_ERROR_TEST_FAILED;
328 }
329
330 Ec2 = EcNewByNid (EcCurveList[CurveCount]);
331 if (Ec2 == NULL) {
332 return UNIT_TEST_ERROR_TEST_FAILED;
333 }
334
335 Status = EcGenerateKey (Ec1, Public1, &Public1Length);
336 UT_ASSERT_TRUE (Status);
337 UT_ASSERT_EQUAL (Public1Length, EcKeyHalfSize[CurveCount] * 2);
338
339 Status = EcGenerateKey (Ec2, Public2, &Public2Length);
340 UT_ASSERT_TRUE (Status);
341 UT_ASSERT_EQUAL (Public2Length, EcKeyHalfSize[CurveCount] * 2);
342
343 Status = EcDhComputeKey (Ec1, Public2, Public2Length, NULL, Key1, &Key1Length);
344 UT_ASSERT_TRUE (Status);
345 UT_ASSERT_EQUAL (Key1Length, EcKeyHalfSize[CurveCount]);
346
347 Status = EcDhComputeKey (Ec2, Public1, Public1Length, NULL, Key2, &Key2Length);
348 UT_ASSERT_TRUE (Status);
349 UT_ASSERT_EQUAL (Key2Length, EcKeyHalfSize[CurveCount]);
350
351 UT_ASSERT_EQUAL (Key1Length, Key2Length);
352 UT_ASSERT_MEM_EQUAL (Key1, Key2, Key1Length);
353
354 Status = EcGetPubKey (Ec1, Public2, &Public2Length);
355 UT_ASSERT_TRUE (Status);
356 UT_ASSERT_EQUAL (Public2Length, EcKeyHalfSize[CurveCount] * 2);
357
358 UT_ASSERT_EQUAL (Public1Length, Public2Length);
359 UT_ASSERT_MEM_EQUAL (Public1, Public2, Public1Length);
360 }
361
362 return UNIT_TEST_PASSED;
363}
364
366EFIAPI
367TestVerifyEcKey (
368 UNIT_TEST_CONTEXT Context
369 )
370{
371 BOOLEAN Status;
372 VOID *EcPrivKey;
373 VOID *EcPubKey;
374 UINT8 HashValue[SHA256_DIGEST_SIZE];
375 UINTN HashSize;
376 UINT8 Signature[66 * 2];
377 UINTN SigSize;
378
379 //
380 // Retrieve EC private key from PEM data.
381 //
382 Status = EcGetPrivateKeyFromPem (
383 mEccTestPemKey,
384 sizeof (mEccTestPemKey),
385 NULL,
386 &EcPrivKey
387 );
388 UT_ASSERT_TRUE (Status);
389
390 //
391 // Retrieve EC public key from X509 Certificate.
392 //
393 Status = EcGetPublicKeyFromX509 (
394 mEccTestRootCer,
395 sizeof (mEccTestRootCer),
396 &EcPubKey
397 );
398 UT_ASSERT_TRUE (Status);
399
400 //
401 // Verify EC-DSA
402 //
403 HashSize = sizeof (HashValue);
404 SigSize = sizeof (Signature);
405 //
406 // EC-DSA Signing ...
407 //
408 Status = EcDsaSign (
409 EcPrivKey,
410 CRYPTO_NID_SHA256,
411 HashValue,
412 HashSize,
413 Signature,
414 &SigSize
415 );
416 UT_ASSERT_TRUE (Status);
417
418 //
419 // EC-DSA Verification ...
420 //
421 Status = EcDsaVerify (
422 EcPubKey,
423 CRYPTO_NID_SHA256,
424 HashValue,
425 HashSize,
426 Signature,
427 SigSize
428 );
429 UT_ASSERT_TRUE (Status);
430
431 EcFree (EcPrivKey);
432 EcFree (EcPubKey);
433
434 return UNIT_TEST_PASSED;
435}
436
437TEST_DESC mEcTest[] = {
438 //
439 // -----Description-----------------Class------------------Function----Pre----Post----Context
440 //
441 { "TestVerifyEcBasic()", "CryptoPkg.BaseCryptLib.Ec", TestVerifyEcBasic, TestVerifyEcPreReq, TestVerifyEcCleanUp, NULL },
442 { "TestVerifyEcDh()", "CryptoPkg.BaseCryptLib.Ec", TestVerifyEcDh, TestVerifyEcPreReq, TestVerifyEcCleanUp, NULL },
443 { "TestVerifyEcKey()", "CryptoPkg.BaseCryptLib.Ec", TestVerifyEcKey, NULL, NULL, NULL },
444};
445
446UINTN mEcTestNum = ARRAY_SIZE (mEcTest);
UINT64 UINTN
BOOLEAN EFIAPI EcPointInvert(IN CONST VOID *EcGroup, IN OUT VOID *EcPoint, IN VOID *BnCtx)
Definition: CryptEc.c:302
BOOLEAN EFIAPI EcPointSetCompressedCoordinates(IN CONST VOID *EcGroup, IN VOID *EcPoint, IN CONST VOID *BnX, IN UINT8 YBit, IN VOID *BnCtx)
Definition: CryptEc.c:393
BOOLEAN EFIAPI EcPointMul(IN CONST VOID *EcGroup, OUT VOID *EcPointResult, IN CONST VOID *EcPoint, IN CONST VOID *BnPScalar, IN VOID *BnCtx)
Definition: CryptEc.c:279
BOOLEAN EFIAPI EcPointAdd(IN CONST VOID *EcGroup, OUT VOID *EcPointResult, IN CONST VOID *EcPointA, IN CONST VOID *EcPointB, IN VOID *BnCtx)
Definition: CryptEc.c:253
BOOLEAN EFIAPI EcGenerateKey(IN OUT VOID *EcContext, OUT UINT8 *PublicKey, IN OUT UINTN *PublicKeySize)
Definition: CryptEc.c:475
BOOLEAN EFIAPI EcPointIsOnCurve(IN CONST VOID *EcGroup, IN CONST VOID *EcPoint, IN VOID *BnCtx)
Definition: CryptEc.c:323
VOID *EFIAPI BigNumFromBin(IN CONST UINT8 *Buf, IN UINTN Len)
Definition: CryptBn.c:35
BOOLEAN EFIAPI EcPointIsAtInfinity(IN CONST VOID *EcGroup, IN CONST VOID *EcPoint)
Definition: CryptEc.c:343
BOOLEAN EFIAPI EcGetPubKey(IN OUT VOID *EcContext, OUT UINT8 *PublicKey, IN OUT UINTN *PublicKeySize)
Definition: CryptEc.c:567
BOOLEAN EFIAPI EcDsaSign(IN VOID *EcContext, IN UINTN HashNid, IN CONST UINT8 *MessageHash, IN UINTN HashSize, OUT UINT8 *Signature, IN OUT UINTN *SigSize)
Definition: CryptEc.c:801
VOID EFIAPI BigNumFree(IN VOID *Bn, IN BOOLEAN Clear)
Definition: CryptBn.c:70
VOID *EFIAPI EcNewByNid(IN UINTN Nid)
Definition: CryptEc.c:419
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
VOID *EFIAPI EcPointInit(IN CONST VOID *EcGroup)
Definition: CryptEc.c:158
VOID *EFIAPI EcGroupInit(IN UINTN CryptoNid)
Definition: CryptEc.c:68
BOOLEAN EFIAPI EcGetPrivateKeyFromPem(IN CONST UINT8 *PemData, IN UINTN PemSize, IN CONST CHAR8 *Password, OUT VOID **EcContext)
Definition: CryptPem.c:149
BOOLEAN EFIAPI EcPointEqual(IN CONST VOID *EcGroup, IN CONST VOID *EcPointA, IN CONST VOID *EcPointB, IN VOID *BnCtx)
Definition: CryptEc.c:364
BOOLEAN EFIAPI EcGroupGetCurve(IN CONST VOID *EcGroup, OUT VOID *BnPrime, OUT VOID *BnA, OUT VOID *BnB, IN VOID *BnCtx)
Definition: CryptEc.c:100
BOOLEAN EFIAPI EcGetPublicKeyFromX509(IN CONST UINT8 *Cert, IN UINTN CertSize, OUT VOID **EcContext)
Definition: CryptX509.c:878
VOID EFIAPI EcPointDeInit(IN VOID *EcPoint, IN BOOLEAN Clear)
Definition: CryptEc.c:173
BOOLEAN EFIAPI EcDsaVerify(IN VOID *EcContext, IN UINTN HashNid, IN CONST UINT8 *MessageHash, IN UINTN HashSize, IN CONST UINT8 *Signature, IN UINTN SigSize)
Definition: CryptEc.c:933
VOID *EFIAPI BigNumInit(VOID)
Definition: CryptBn.c:18
BOOLEAN EFIAPI EcGroupGetOrder(IN VOID *EcGroup, OUT VOID *BnOrder)
Definition: CryptEc.c:125
BOOLEAN EFIAPI EcDhComputeKey(IN OUT VOID *EcContext, IN CONST UINT8 *PeerPublic, IN UINTN PeerPublicSize, IN CONST INT32 *CompressFlag, OUT UINT8 *Key, IN OUT UINTN *KeySize)
Definition: CryptEc.c:667
VOID EFIAPI EcGroupFree(IN VOID *EcGroup)
Definition: CryptEc.c:140
BOOLEAN EFIAPI EcPointSetAffineCoordinates(IN CONST VOID *EcGroup, IN VOID *EcPoint, IN CONST VOID *BnX, IN CONST VOID *BnY, IN VOID *BnCtx)
Definition: CryptEc.c:227
VOID EFIAPI EcFree(IN VOID *EcContext)
Definition: CryptEc.c:440
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length)
Definition: UnitTestLib.h:389
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
#define UT_ASSERT_FALSE(Expression)
Definition: UnitTestLib.h:362