TianoCore EDK2 master
Loading...
Searching...
No Matches
RsaTests.c
Go to the documentation of this file.
1
9#include "TestBaseCryptLib.h"
10
11#define RSA_MODULUS_LENGTH 512
12
13//
14// RSA PKCS#1 Validation Data from OpenSSL "Fips_rsa_selftest.c"
15//
16
17//
18// Public Modulus of RSA Key
19//
21 0xBB, 0xF8, 0x2F, 0x09, 0x06, 0x82, 0xCE, 0x9C, 0x23, 0x38, 0xAC, 0x2B, 0x9D, 0xA8, 0x71, 0xF7,
22 0x36, 0x8D, 0x07, 0xEE, 0xD4, 0x10, 0x43, 0xA4, 0x40, 0xD6, 0xB6, 0xF0, 0x74, 0x54, 0xF5, 0x1F,
23 0xB8, 0xDF, 0xBA, 0xAF, 0x03, 0x5C, 0x02, 0xAB, 0x61, 0xEA, 0x48, 0xCE, 0xEB, 0x6F, 0xCD, 0x48,
24 0x76, 0xED, 0x52, 0x0D, 0x60, 0xE1, 0xEC, 0x46, 0x19, 0x71, 0x9D, 0x8A, 0x5B, 0x8B, 0x80, 0x7F,
25 0xAF, 0xB8, 0xE0, 0xA3, 0xDF, 0xC7, 0x37, 0x72, 0x3E, 0xE6, 0xB4, 0xB7, 0xD9, 0x3A, 0x25, 0x84,
26 0xEE, 0x6A, 0x64, 0x9D, 0x06, 0x09, 0x53, 0x74, 0x88, 0x34, 0xB2, 0x45, 0x45, 0x98, 0x39, 0x4E,
27 0xE0, 0xAA, 0xB1, 0x2D, 0x7B, 0x61, 0xA5, 0x1F, 0x52, 0x7A, 0x9A, 0x41, 0xF6, 0xC1, 0x68, 0x7F,
28 0xE2, 0x53, 0x72, 0x98, 0xCA, 0x2A, 0x8F, 0x59, 0x46, 0xF8, 0xE5, 0xFD, 0x09, 0x1D, 0xBD, 0xCB
29};
30
31//
32// Public Exponent of RSA Key
33//
34GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 RsaE[] = { 0x11 };
35
36//
37// Private Exponent of RSA Key
38//
40 0xA5, 0xDA, 0xFC, 0x53, 0x41, 0xFA, 0xF2, 0x89, 0xC4, 0xB9, 0x88, 0xDB, 0x30, 0xC1, 0xCD, 0xF8,
41 0x3F, 0x31, 0x25, 0x1E, 0x06, 0x68, 0xB4, 0x27, 0x84, 0x81, 0x38, 0x01, 0x57, 0x96, 0x41, 0xB2,
42 0x94, 0x10, 0xB3, 0xC7, 0x99, 0x8D, 0x6B, 0xC4, 0x65, 0x74, 0x5E, 0x5C, 0x39, 0x26, 0x69, 0xD6,
43 0x87, 0x0D, 0xA2, 0xC0, 0x82, 0xA9, 0x39, 0xE3, 0x7F, 0xDC, 0xB8, 0x2E, 0xC9, 0x3E, 0xDA, 0xC9,
44 0x7F, 0xF3, 0xAD, 0x59, 0x50, 0xAC, 0xCF, 0xBC, 0x11, 0x1C, 0x76, 0xF1, 0xA9, 0x52, 0x94, 0x44,
45 0xE5, 0x6A, 0xAF, 0x68, 0xC5, 0x6C, 0x09, 0x2C, 0xD3, 0x8D, 0xC3, 0xBE, 0xF5, 0xD2, 0x0A, 0x93,
46 0x99, 0x26, 0xED, 0x4F, 0x74, 0xA1, 0x3E, 0xDD, 0xFB, 0xE1, 0xA1, 0xCE, 0xCC, 0x48, 0x94, 0xAF,
47 0x94, 0x28, 0xC2, 0xB7, 0xB8, 0x88, 0x3F, 0xE4, 0x46, 0x3A, 0x4B, 0xC8, 0x5B, 0x1C, 0xB3, 0xC1
48};
49
50//
51// Known Answer Test (KAT) Data for RSA PKCS#1 Signing
52//
53GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 RsaSignData[] = "OpenSSL FIPS 140-2 Public Key RSA KAT";
54
55//
56// Known Signature for the above message, under SHA-1 Digest
57//
58GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 RsaPkcs1Signature[] = {
59 0x71, 0xEE, 0x1A, 0xC0, 0xFE, 0x01, 0x93, 0x54, 0x79, 0x5C, 0xF2, 0x4C, 0x4A, 0xFD, 0x1A, 0x05,
60 0x8F, 0x64, 0xB1, 0x6D, 0x61, 0x33, 0x8D, 0x9B, 0xE7, 0xFD, 0x60, 0xA3, 0x83, 0xB5, 0xA3, 0x51,
61 0x55, 0x77, 0x90, 0xCF, 0xDC, 0x22, 0x37, 0x8E, 0xD0, 0xE1, 0xAE, 0x09, 0xE3, 0x3D, 0x1E, 0xF8,
62 0x80, 0xD1, 0x8B, 0xC2, 0xEC, 0x0A, 0xD7, 0x6B, 0x88, 0x8B, 0x8B, 0xA1, 0x20, 0x22, 0xBE, 0x59,
63 0x5B, 0xE0, 0x23, 0x24, 0xA1, 0x49, 0x30, 0xBA, 0xA9, 0x9E, 0xE8, 0xB1, 0x8A, 0x62, 0x16, 0xBF,
64 0x4E, 0xCA, 0x2E, 0x4E, 0xBC, 0x29, 0xA8, 0x67, 0x13, 0xB7, 0x9F, 0x1D, 0x04, 0x44, 0xE5, 0x5F,
65 0x35, 0x07, 0x11, 0xBC, 0xED, 0x19, 0x37, 0x21, 0xCF, 0x23, 0x48, 0x1F, 0x72, 0x05, 0xDE, 0xE6,
66 0xE8, 0x7F, 0x33, 0x8A, 0x76, 0x4B, 0x2F, 0x95, 0xDF, 0xF1, 0x5F, 0x84, 0x80, 0xD9, 0x46, 0xB4
67};
68
69//
70// Default public key 0x10001 = 65537
71//
72GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 DefaultPublicKey[] = {
73 0x01, 0x00, 0x01
74};
75
76VOID *mRsa;
77
79EFIAPI
80TestVerifyRsaPreReq (
81 UNIT_TEST_CONTEXT Context
82 )
83{
84 mRsa = RsaNew ();
85
86 if (mRsa == NULL) {
87 return UNIT_TEST_ERROR_TEST_FAILED;
88 }
89
90 return UNIT_TEST_PASSED;
91}
92
93VOID
94EFIAPI
95TestVerifyRsaCleanUp (
96 UNIT_TEST_CONTEXT Context
97 )
98{
99 if (mRsa != NULL) {
100 RsaFree (mRsa);
101 mRsa = NULL;
102 }
103}
104
106EFIAPI
107TestVerifyRsaSetGetKeyComponents (
108 IN UNIT_TEST_CONTEXT Context
109 )
110{
111 BOOLEAN Status;
112 UINTN KeySize;
113 UINT8 *KeyBuffer;
114
115 //
116 // Set/Get RSA Key Components
117 //
118
119 //
120 // Set/Get RSA Key N
121 //
122 Status = RsaSetKey (mRsa, RsaKeyN, RsaN, sizeof (RsaN));
123 UT_ASSERT_TRUE (Status);
124
125 KeySize = 0;
126 Status = RsaGetKey (mRsa, RsaKeyN, NULL, &KeySize);
127 UT_ASSERT_FALSE (Status);
128 UT_ASSERT_EQUAL (KeySize, sizeof (RsaN));
129
130 KeyBuffer = AllocatePool (KeySize);
131 Status = RsaGetKey (mRsa, RsaKeyN, KeyBuffer, &KeySize);
132 UT_ASSERT_TRUE (Status);
133 UT_ASSERT_EQUAL (KeySize, sizeof (RsaN));
134
135 UT_ASSERT_MEM_EQUAL (KeyBuffer, RsaN, KeySize);
136
137 FreePool (KeyBuffer);
138
139 //
140 // Set/Get RSA Key E
141 //
142 Status = RsaSetKey (mRsa, RsaKeyE, RsaE, sizeof (RsaE));
143 UT_ASSERT_TRUE (Status);
144
145 KeySize = 0;
146 Status = RsaGetKey (mRsa, RsaKeyE, NULL, &KeySize);
147 UT_ASSERT_FALSE (Status);
148 UT_ASSERT_EQUAL (KeySize, sizeof (RsaE));
149
150 KeyBuffer = AllocatePool (KeySize);
151 Status = RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize);
152 UT_ASSERT_TRUE (Status);
153 UT_ASSERT_EQUAL (KeySize, sizeof (RsaE));
154
155 UT_ASSERT_MEM_EQUAL (KeyBuffer, RsaE, KeySize);
156
157 FreePool (KeyBuffer);
158
159 //
160 // Clear/Get RSA Key Components
161 //
162
163 //
164 // Clear/Get RSA Key N
165 //
166 Status = RsaSetKey (mRsa, RsaKeyN, NULL, 0);
167 UT_ASSERT_TRUE (Status);
168
169 KeySize = 1;
170 Status = RsaGetKey (mRsa, RsaKeyN, NULL, &KeySize);
171 UT_ASSERT_TRUE (Status);
172 UT_ASSERT_EQUAL (KeySize, 0);
173
174 //
175 // Clear/Get RSA Key E
176 //
177 Status = RsaSetKey (mRsa, RsaKeyE, NULL, 0);
178 UT_ASSERT_TRUE (Status);
179
180 KeySize = 1;
181 Status = RsaGetKey (mRsa, RsaKeyE, NULL, &KeySize);
182 UT_ASSERT_TRUE (Status);
183 UT_ASSERT_EQUAL (KeySize, 0);
184
185 return UNIT_TEST_PASSED;
186}
187
189EFIAPI
190TestVerifyRsaGenerateKeyComponents (
191 IN UNIT_TEST_CONTEXT Context
192 )
193{
194 BOOLEAN Status;
195 UINTN KeySize;
196 UINT8 *KeyBuffer;
197 UINT8 TestPublicExponent1[] = { 0x03 };
198 UINT8 TestPublicExponent2[] = { 0x01, 0x01 };
199 UINT8 TestPublicExponent3[] = { 0x01, 0x00, 0x01 };
200
201 //
202 // Generate RSA Key Components
203 //
204
205 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, NULL, 0);
206 UT_ASSERT_TRUE (Status);
207
208 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent1, sizeof (TestPublicExponent1));
209 UT_ASSERT_TRUE (Status);
210 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent2, sizeof (TestPublicExponent2));
211 UT_ASSERT_TRUE (Status);
212 Status = RsaGenerateKey (mRsa, RSA_MODULUS_LENGTH, TestPublicExponent3, sizeof (TestPublicExponent3));
213 UT_ASSERT_TRUE (Status);
214
215 KeySize = RSA_MODULUS_LENGTH / 8;
216 KeyBuffer = AllocatePool (KeySize);
217 Status = RsaGetKey (mRsa, RsaKeyE, KeyBuffer, &KeySize);
218 UT_ASSERT_TRUE (Status);
219 UT_ASSERT_EQUAL (KeySize, 3);
220 UT_ASSERT_MEM_EQUAL (KeyBuffer, DefaultPublicKey, 3);
221
222 KeySize = RSA_MODULUS_LENGTH / 8;
223 Status = RsaGetKey (mRsa, RsaKeyN, KeyBuffer, &KeySize);
224 UT_ASSERT_TRUE (Status);
225 UT_ASSERT_EQUAL (KeySize, RSA_MODULUS_LENGTH / 8);
226
227 Status = RsaCheckKey (mRsa);
228 UT_ASSERT_TRUE (Status);
229
230 //
231 // Check invalid RSA key components
232 //
233 Status = RsaSetKey (mRsa, RsaKeyN, RsaN, sizeof (RsaN));
234 UT_ASSERT_TRUE (Status);
235
236 Status = RsaCheckKey (mRsa);
237 UT_ASSERT_FALSE (Status);
238
239 Status = RsaSetKey (mRsa, RsaKeyN, KeyBuffer, KeySize);
240 UT_ASSERT_TRUE (Status);
241
242 Status = RsaCheckKey (mRsa);
243 UT_ASSERT_TRUE (Status);
244
245 Status = RsaSetKey (mRsa, RsaKeyE, RsaE, sizeof (RsaE));
246 UT_ASSERT_TRUE (Status);
247
248 Status = RsaCheckKey (mRsa);
249 UT_ASSERT_FALSE (Status);
250
251 FreePool (KeyBuffer);
252
253 return UNIT_TEST_PASSED;
254}
255
257EFIAPI
258TestVerifyRsaPkcs1SignVerify (
259 IN UNIT_TEST_CONTEXT Context
260 )
261{
262 UINT8 HashValue[SHA1_DIGEST_SIZE];
263 UINTN HashSize;
264 UINT8 *Signature;
265 UINTN SigSize;
266 BOOLEAN Status;
267
268 //
269 // SHA-1 Digest Message for PKCS#1 Signature
270 //
271 HashSize = SHA1_DIGEST_SIZE;
272 ZeroMem (HashValue, HashSize);
273
274 Status = Sha1HashAll (RsaSignData, AsciiStrLen (RsaSignData), HashValue);
275 UT_ASSERT_TRUE (Status);
276
277 //
278 // Sign RSA PKCS#1-encoded Signature
279 //
280
281 Status = RsaSetKey (mRsa, RsaKeyN, RsaN, sizeof (RsaN));
282 UT_ASSERT_TRUE (Status);
283
284 Status = RsaSetKey (mRsa, RsaKeyE, RsaE, sizeof (RsaE));
285 UT_ASSERT_TRUE (Status);
286
287 Status = RsaSetKey (mRsa, RsaKeyD, RsaD, sizeof (RsaD));
288 UT_ASSERT_TRUE (Status);
289
290 SigSize = 0;
291 Status = RsaPkcs1Sign (mRsa, HashValue, HashSize, NULL, &SigSize);
292 UT_ASSERT_FALSE (Status);
293 UT_ASSERT_NOT_EQUAL (SigSize, 0);
294
295 Signature = AllocatePool (SigSize);
296 Status = RsaPkcs1Sign (mRsa, HashValue, HashSize, Signature, &SigSize);
297 UT_ASSERT_TRUE (Status);
298 UT_ASSERT_EQUAL (SigSize, sizeof (RsaPkcs1Signature));
299
300 UT_ASSERT_MEM_EQUAL (Signature, RsaPkcs1Signature, SigSize);
301
302 //
303 // Verify RSA PKCS#1-encoded Signature
304 //
305 Status = RsaPkcs1Verify (mRsa, HashValue, HashSize, Signature, SigSize);
306 UT_ASSERT_TRUE (Status);
307
308 FreePool (Signature);
309
310 return UNIT_TEST_PASSED;
311}
312
313TEST_DESC mRsaTest[] = {
314 //
315 // -----Description--------------------------------------Class----------------------Function---------------------------------Pre---------------------Post---------Context
316 //
317 { "TestVerifyRsaSetGetKeyComponents()", "CryptoPkg.BaseCryptLib.Rsa", TestVerifyRsaSetGetKeyComponents, TestVerifyRsaPreReq, TestVerifyRsaCleanUp, NULL },
318 { "TestVerifyRsaGenerateKeyComponents()", "CryptoPkg.BaseCryptLib.Rsa", TestVerifyRsaGenerateKeyComponents, TestVerifyRsaPreReq, TestVerifyRsaCleanUp, NULL },
319 { "TestVerifyRsaPkcs1SignVerify()", "CryptoPkg.BaseCryptLib.Rsa", TestVerifyRsaPkcs1SignVerify, TestVerifyRsaPreReq, TestVerifyRsaCleanUp, NULL },
320};
321
322UINTN mRsaTestNum = ARRAY_SIZE (mRsaTest);
UINT64 UINTN
BOOLEAN EFIAPI RsaCheckKey(IN VOID *RsaContext)
Definition: CryptRsaExt.c:243
@ RsaKeyD
RSA Private exponent (d)
Definition: BaseCryptLib.h:77
@ RsaKeyN
RSA public Modulus (N)
Definition: BaseCryptLib.h:75
@ RsaKeyE
RSA Public exponent (e)
Definition: BaseCryptLib.h:76
#define SHA1_DIGEST_SIZE
Definition: BaseCryptLib.h:39
VOID *EFIAPI RsaNew(VOID)
Definition: CryptRsaBasic.c:30
BOOLEAN EFIAPI RsaGetKey(IN OUT VOID *RsaContext, IN RSA_KEY_TAG KeyTag, OUT UINT8 *BigNumber, IN OUT UINTN *BnSize)
Definition: CryptRsaExt.c:50
BOOLEAN EFIAPI RsaSetKey(IN OUT VOID *RsaContext, IN RSA_KEY_TAG KeyTag, IN CONST UINT8 *BigNumber, IN UINTN BnSize)
Definition: CryptRsaBasic.c:82
VOID EFIAPI RsaFree(IN VOID *RsaContext)
Definition: CryptRsaBasic.c:48
BOOLEAN EFIAPI Sha1HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
Definition: CryptSha1.c:201
BOOLEAN EFIAPI RsaPkcs1Verify(IN VOID *RsaContext, IN CONST UINT8 *MessageHash, IN UINTN HashSize, IN CONST UINT8 *Signature, IN UINTN SigSize)
BOOLEAN EFIAPI RsaGenerateKey(IN OUT VOID *RsaContext, IN UINTN ModulusLength, IN CONST UINT8 *PublicExponent, IN UINTN PublicExponentSize)
Definition: CryptRsaExt.c:179
BOOLEAN EFIAPI RsaPkcs1Sign(IN VOID *RsaContext, IN CONST UINT8 *MessageHash, IN UINTN HashSize, OUT UINT8 *Signature, IN OUT UINTN *SigSize)
Definition: CryptRsaExt.c:297
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:402
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