]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Test/UnitTest/Library/BaseCryptLib/BlockCipherTests.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Test / UnitTest / Library / BaseCryptLib / BlockCipherTests.c
CommitLineData
694bfd6f
MC
1/** @file\r
2 Application for Block Cipher Primitives Validation.\r
3\r
4Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "TestBaseCryptLib.h"\r
10\r
11//\r
12// TDES test vectors are extracted from OpenSSL 0.9.8l, crypto\des\destest.c\r
13//\r
7c342378 14GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcbData[] = {\r
694bfd6f 15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
7c342378 16};\r
694bfd6f 17\r
7c342378 18GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcbKey[] = {\r
694bfd6f
MC
19 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
20 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF\r
7c342378 21};\r
694bfd6f 22\r
7c342378 23GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcbCipher[] = {\r
694bfd6f 24 0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7,\r
7c342378 25};\r
694bfd6f 26\r
7c342378 27GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcb2Cipher[] = {\r
694bfd6f 28 0x92, 0x95, 0xB5, 0x9B, 0xB3, 0x84, 0x73, 0x6E,\r
7c342378 29};\r
694bfd6f 30\r
7c342378 31GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesCbcData[] = {\r
694bfd6f
MC
32 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,\r
33 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,\r
34 0x68, 0x65, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x20\r
7c342378 35};\r
694bfd6f 36\r
7c342378 37GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesCbcKey[] = {\r
694bfd6f
MC
38 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,\r
39 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,\r
40 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10\r
7c342378 41};\r
694bfd6f 42\r
7c342378 43GLOBAL_REMOVE_IF_UNREFERENCED UINT8 TdesCbcIvec[] = {\r
694bfd6f 44 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10\r
7c342378 45};\r
694bfd6f 46\r
7c342378 47GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesCbc3Cipher[] = {\r
694bfd6f
MC
48 0x3F, 0xE3, 0x01, 0xC9, 0x62, 0xAC, 0x01, 0xD0,\r
49 0x22, 0x13, 0x76, 0x3C, 0x1C, 0xBD, 0x4C, 0xDC,\r
50 0x79, 0x96, 0x57, 0xC0, 0x64, 0xEC, 0xF5, 0xD4\r
7c342378 51};\r
694bfd6f
MC
52\r
53//\r
54// AES test vectors are from NIST KAT of AES\r
55//\r
7c342378 56GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128EcbData[] = {\r
694bfd6f 57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\r
7c342378 58};\r
694bfd6f 59\r
7c342378 60GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128EcbKey[] = {\r
694bfd6f 61 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59\r
7c342378 62};\r
694bfd6f 63\r
7c342378 64GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128EcbCipher[] = {\r
694bfd6f 65 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65\r
7c342378 66};\r
694bfd6f 67\r
7c342378 68GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes192EcbData[] = {\r
694bfd6f 69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\r
7c342378 70};\r
694bfd6f 71\r
7c342378 72GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes192EcbKey[] = {\r
694bfd6f
MC
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\r
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff\r
7c342378 75};\r
694bfd6f 76\r
7c342378 77GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes192EcbCipher[] = {\r
694bfd6f 78 0xdd, 0x8a, 0x49, 0x35, 0x14, 0x23, 0x1c, 0xbf, 0x56, 0xec, 0xce, 0xe4, 0xc4, 0x08, 0x89, 0xfb\r
7c342378 79};\r
694bfd6f 80\r
7c342378 81GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes256EcbData[] = {\r
694bfd6f 82 0x01, 0x47, 0x30, 0xf8, 0x0a, 0xc6, 0x25, 0xfe, 0x84, 0xf0, 0x26, 0xc6, 0x0b, 0xfd, 0x54, 0x7d\r
7c342378 83};\r
694bfd6f 84\r
7c342378 85GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes256EcbKey[] = {\r
694bfd6f
MC
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\r
7c342378 88};\r
694bfd6f 89\r
7c342378 90GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes256EcbCipher[] = {\r
694bfd6f 91 0x5c, 0x9d, 0x84, 0x4e, 0xd4, 0x6f, 0x98, 0x85, 0x08, 0x5e, 0x5d, 0x6a, 0x4f, 0x94, 0xc7, 0xd7\r
7c342378 92};\r
694bfd6f 93\r
7c342378 94GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcData[] = {\r
694bfd6f
MC
95 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,\r
96 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f\r
7c342378 97};\r
694bfd6f 98\r
7c342378 99GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcKey[] = {\r
694bfd6f 100 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0, 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a\r
7c342378 101};\r
694bfd6f 102\r
7c342378 103GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcIvec[] = {\r
694bfd6f 104 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58\r
7c342378 105};\r
694bfd6f 106\r
7c342378 107GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcCipher[] = {\r
694bfd6f
MC
108 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a, 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,\r
109 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9, 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1\r
7c342378 110};\r
694bfd6f
MC
111\r
112//\r
113// ARC4 Test Vector defined in "Appendix A.1 Test Vectors from [CRYPTLIB]" of\r
114// IETF Draft draft-kaukonen-cipher-arcfour-03 ("A Stream Cipher Encryption Algorithm 'Arcfour'").\r
115//\r
7c342378 116GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Arc4Data[] = {\r
694bfd6f 117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\r
7c342378 118};\r
694bfd6f 119\r
7c342378 120GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Arc4Key[] = {\r
694bfd6f 121 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF\r
7c342378 122};\r
694bfd6f 123\r
7c342378 124GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Arc4Cipher[] = {\r
694bfd6f 125 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79\r
7c342378 126};\r
694bfd6f
MC
127\r
128typedef\r
129UINTN\r
7c342378 130(EFIAPI *EFI_BLOCK_CIPHER_GET_CONTEXT_SIZE)(\r
694bfd6f
MC
131 VOID\r
132 );\r
133\r
134typedef\r
135BOOLEAN\r
7c342378 136(EFIAPI *EFI_BLOCK_CIPHER_INIT)(\r
694bfd6f
MC
137 OUT VOID *BlockCipherContext,\r
138 IN CONST UINT8 *Key,\r
139 IN UINTN KeyLength\r
140 );\r
141\r
142typedef\r
143BOOLEAN\r
7c342378 144(EFIAPI *EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT)(\r
694bfd6f
MC
145 IN VOID *BlockCipherContext,\r
146 IN CONST UINT8 *Input,\r
147 IN UINTN InputSize,\r
148 OUT UINT8 *Output\r
149 );\r
150\r
151typedef\r
152BOOLEAN\r
7c342378 153(EFIAPI *EFI_BLOCK_CIPHER_CBC_ENCRYPT_DECRYPT)(\r
694bfd6f
MC
154 IN VOID *BlockCipherContext,\r
155 IN CONST UINT8 *Input,\r
156 IN UINTN InputSize,\r
157 IN CONST UINT8 *Ivec,\r
158 OUT UINT8 *Output\r
159 );\r
160\r
161typedef\r
162BOOLEAN\r
7c342378 163(EFIAPI *EFI_BLOCK_CIPHER_RESET)(\r
694bfd6f
MC
164 IN OUT VOID *BlockCipherContext\r
165 );\r
166\r
167typedef struct {\r
7c342378
MK
168 EFI_BLOCK_CIPHER_GET_CONTEXT_SIZE GetContextSize;\r
169 EFI_BLOCK_CIPHER_INIT Init;\r
170 EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT EcbEncrypt;\r
171 EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT EcbDecrypt;\r
172 EFI_BLOCK_CIPHER_CBC_ENCRYPT_DECRYPT CbcEncrypt;\r
173 EFI_BLOCK_CIPHER_CBC_ENCRYPT_DECRYPT CbcDecrypt;\r
174 EFI_BLOCK_CIPHER_RESET Reset;\r
175 CONST UINT8 *Key;\r
176 UINTN KeySize;\r
177 CONST UINT8 *Ivec;\r
178 CONST UINT8 *Data;\r
179 UINTN DataSize;\r
180 CONST UINT8 *Cipher;\r
181 UINTN CipherSize;\r
182 VOID *Ctx;\r
694bfd6f
MC
183} BLOCK_CIPHER_TEST_CONTEXT;\r
184\r
185// These are commented out as they are deprecated, but are left in should they be used again\r
7c342378
MK
186// BLOCK_CIPHER_TEST_CONTEXT mTdesEcbTestCtx = {TdesGetContextSize, TdesInit, TdesEcbEncrypt, TdesEcbDecrypt, NULL, NULL, NULL, TdesEcbKey, 64, NULL, TdesEcbData, 8, TdesEcbCipher, 8};\r
187// BLOCK_CIPHER_TEST_CONTEXT mTdesCbcTestCtx = {TdesGetContextSize, TdesInit, NULL, NULL, TdesCbcEncrypt, TdesCbcDecrypt, NULL, TdesCbcKey, 192, TdesCbcIvec, TdesCbcData, sizeof(TdesCbcData), TdesCbc3Cipher, sizeof(TdesCbc3Cipher)};\r
188// BLOCK_CIPHER_TEST_CONTEXT mAes128EcbTestCtx = {AesGetContextSize, AesInit, AesEcbEncrypt, AesEcbDecrypt, NULL, NULL, NULL, Aes128EcbKey, 128, NULL, Aes128EcbData, sizeof(Aes128EcbData), Aes128EcbCipher, sizeof(Aes128EcbCipher)};\r
189// BLOCK_CIPHER_TEST_CONTEXT mAes192EcbTestCtx = {AesGetContextSize, AesInit, AesEcbEncrypt, AesEcbDecrypt, NULL, NULL, NULL, Aes192EcbKey, 192, NULL, Aes192EcbData, sizeof(Aes192EcbData), Aes192EcbCipher, sizeof(Aes192EcbCipher)};\r
190// BLOCK_CIPHER_TEST_CONTEXT mAes256EcbTestCtx = {AesGetContextSize, AesInit, AesEcbEncrypt, AesEcbDecrypt, NULL, NULL, NULL, Aes256EcbKey, 256, NULL, Aes256EcbData, sizeof(Aes256EcbData), Aes256EcbCipher, sizeof(Aes256EcbCipher)};\r
191// BLOCK_CIPHER_TEST_CONTEXT mArc4TestCtx = {Arc4GetContextSize, Arc4Init, Arc4Encrypt, (EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT), Arc4Decrypt, NULL, NULL, Arc4Reset, Arc4Key, sizeof(Arc4Key), NULL, Arc4Data, sizeof(Arc4Data), Arc4Cipher, sizeof(Arc4Cipher)};\r
192BLOCK_CIPHER_TEST_CONTEXT mAes128CbcTestCtx = { AesGetContextSize, AesInit, NULL, NULL, AesCbcEncrypt, AesCbcDecrypt, NULL, Aes128CbcKey, 128, Aes128CbcIvec, Aes128CbcData, sizeof (Aes128CbcData), Aes128CbcCipher, sizeof (Aes128CbcCipher) };\r
694bfd6f
MC
193\r
194UNIT_TEST_STATUS\r
195EFIAPI\r
196TestVerifyBLockCiperPreReq (\r
7c342378 197 UNIT_TEST_CONTEXT Context\r
694bfd6f
MC
198 )\r
199{\r
7c342378
MK
200 BLOCK_CIPHER_TEST_CONTEXT *TestContext;\r
201 UINTN CtxSize;\r
694bfd6f 202\r
7c342378
MK
203 TestContext = Context;\r
204 CtxSize = TestContext->GetContextSize ();\r
694bfd6f
MC
205 TestContext->Ctx = AllocatePool (CtxSize);\r
206 if (TestContext->Ctx == NULL) {\r
207 return UNIT_TEST_ERROR_TEST_FAILED;\r
208 }\r
209\r
210 return UNIT_TEST_PASSED;\r
211}\r
212\r
213VOID\r
214EFIAPI\r
215TestVerifyBLockCiperCleanUp (\r
7c342378 216 UNIT_TEST_CONTEXT Context\r
694bfd6f
MC
217 )\r
218{\r
7c342378 219 BLOCK_CIPHER_TEST_CONTEXT *TestContext;\r
694bfd6f
MC
220\r
221 TestContext = Context;\r
222 if (TestContext->Ctx != NULL) {\r
223 FreePool (TestContext->Ctx);\r
224 }\r
225}\r
226\r
227UNIT_TEST_STATUS\r
228EFIAPI\r
229TestVerifyBLockCiper (\r
7c342378 230 UNIT_TEST_CONTEXT Context\r
694bfd6f
MC
231 )\r
232{\r
7c342378
MK
233 UINT8 Encrypt[256];\r
234 UINT8 Decrypt[256];\r
235 BOOLEAN Status;\r
236 BLOCK_CIPHER_TEST_CONTEXT *TestContext;\r
694bfd6f
MC
237\r
238 TestContext = Context;\r
239\r
240 ZeroMem (Encrypt, sizeof (Encrypt));\r
241 ZeroMem (Decrypt, sizeof (Decrypt));\r
242\r
243 Status = TestContext->Init (TestContext->Ctx, TestContext->Key, TestContext->KeySize);\r
244 UT_ASSERT_TRUE (Status);\r
245\r
246 if (TestContext->Ivec == NULL) {\r
247 Status = TestContext->EcbEncrypt (TestContext->Ctx, TestContext->Data, TestContext->DataSize, Encrypt);\r
248 UT_ASSERT_TRUE (Status);\r
249\r
250 if (TestContext->Reset != NULL) {\r
251 Status = TestContext->Reset (TestContext->Ctx);\r
252 UT_ASSERT_TRUE (Status);\r
253 }\r
254\r
255 Status = TestContext->EcbDecrypt (TestContext->Ctx, Encrypt, TestContext->DataSize, Decrypt);\r
256 UT_ASSERT_TRUE (Status);\r
694bfd6f
MC
257 } else {\r
258 Status = TestContext->CbcEncrypt (TestContext->Ctx, TestContext->Data, TestContext->DataSize, TestContext->Ivec, Encrypt);\r
259 UT_ASSERT_TRUE (Status);\r
260\r
261 if (TestContext->Reset != NULL) {\r
262 Status = TestContext->Reset (TestContext->Ctx);\r
263 UT_ASSERT_TRUE (Status);\r
264 }\r
265\r
266 Status = TestContext->CbcDecrypt (TestContext->Ctx, Encrypt, TestContext->DataSize, TestContext->Ivec, Decrypt);\r
267 UT_ASSERT_TRUE (Status);\r
268 }\r
269\r
270 UT_ASSERT_MEM_EQUAL (Encrypt, TestContext->Cipher, TestContext->CipherSize);\r
7c342378 271 UT_ASSERT_MEM_EQUAL (Decrypt, TestContext->Data, TestContext->DataSize);\r
694bfd6f
MC
272\r
273 return UNIT_TEST_PASSED;\r
274}\r
275\r
7c342378
MK
276TEST_DESC mBlockCipherTest[] = {\r
277 //\r
278 // -----Description-------------------------Class-------------------------Function---------------Pre---------------------------Post------------------Context\r
279 //\r
280 { "TestVerifyAes128Cbc()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes128CbcTestCtx },\r
281 // These are commented out as these functions have been deprecated, but they have been left in for future reference\r
282 // {"TestVerifyTdesEcb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mTdesEcbTestCtx},\r
283 // {"TestVerifyTdesCbc()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mTdesCbcTestCtx},\r
284 // {"TestVerifyAes128Ecb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes128EcbTestCtx},\r
285 // {"TestVerifyAes192Ecb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes192EcbTestCtx},\r
286 // {"TestVerifyAes256Ecb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes256EcbTestCtx},\r
287 // {"TestVerifyArc4()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mArc4TestCtx},\r
694bfd6f
MC
288};\r
289\r
7c342378 290UINTN mBlockCipherTestNum = ARRAY_SIZE (mBlockCipherTest);\r