2 Application for Block Cipher Primitives Validation.
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "TestBaseCryptLib.h"
12 // TDES test vectors are extracted from OpenSSL 0.9.8l, crypto\des\destest.c
14 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcbData
[] = {
15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcbKey
[] = {
19 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
23 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcbCipher
[] = {
24 0x8C, 0xA6, 0x4D, 0xE9, 0xC1, 0xB1, 0x23, 0xA7,
27 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesEcb2Cipher
[] = {
28 0x92, 0x95, 0xB5, 0x9B, 0xB3, 0x84, 0x73, 0x6E,
31 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesCbcData
[] = {
32 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
33 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
34 0x68, 0x65, 0x20, 0x74, 0x69, 0x6D, 0x65, 0x20
37 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesCbcKey
[] = {
38 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
39 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
40 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
43 GLOBAL_REMOVE_IF_UNREFERENCED UINT8 TdesCbcIvec
[] = {
44 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
47 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 TdesCbc3Cipher
[] = {
48 0x3F, 0xE3, 0x01, 0xC9, 0x62, 0xAC, 0x01, 0xD0,
49 0x22, 0x13, 0x76, 0x3C, 0x1C, 0xBD, 0x4C, 0xDC,
50 0x79, 0x96, 0x57, 0xC0, 0x64, 0xEC, 0xF5, 0xD4
54 // AES test vectors are from NIST KAT of AES
56 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128EcbData
[] = {
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
60 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128EcbKey
[] = {
61 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59
64 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128EcbCipher
[] = {
65 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65
68 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes192EcbData
[] = {
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
72 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes192EcbKey
[] = {
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
77 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes192EcbCipher
[] = {
78 0xdd, 0x8a, 0x49, 0x35, 0x14, 0x23, 0x1c, 0xbf, 0x56, 0xec, 0xce, 0xe4, 0xc4, 0x08, 0x89, 0xfb
81 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes256EcbData
[] = {
82 0x01, 0x47, 0x30, 0xf8, 0x0a, 0xc6, 0x25, 0xfe, 0x84, 0xf0, 0x26, 0xc6, 0x0b, 0xfd, 0x54, 0x7d
85 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes256EcbKey
[] = {
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
90 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes256EcbCipher
[] = {
91 0x5c, 0x9d, 0x84, 0x4e, 0xd4, 0x6f, 0x98, 0x85, 0x08, 0x5e, 0x5d, 0x6a, 0x4f, 0x94, 0xc7, 0xd7
94 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcData
[] = {
95 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
96 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
99 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcKey
[] = {
100 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0, 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a
103 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcIvec
[] = {
104 0x56, 0x2e, 0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58
107 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Aes128CbcCipher
[] = {
108 0xd2, 0x96, 0xcd, 0x94, 0xc2, 0xcc, 0xcf, 0x8a, 0x3a, 0x86, 0x30, 0x28, 0xb5, 0xe1, 0xdc, 0x0a,
109 0x75, 0x86, 0x60, 0x2d, 0x25, 0x3c, 0xff, 0xf9, 0x1b, 0x82, 0x66, 0xbe, 0xa6, 0xd6, 0x1a, 0xb1
113 // ARC4 Test Vector defined in "Appendix A.1 Test Vectors from [CRYPTLIB]" of
114 // IETF Draft draft-kaukonen-cipher-arcfour-03 ("A Stream Cipher Encryption Algorithm 'Arcfour'").
116 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Arc4Data
[] = {
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
120 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Arc4Key
[] = {
121 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
124 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 Arc4Cipher
[] = {
125 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79
130 (EFIAPI
*EFI_BLOCK_CIPHER_GET_CONTEXT_SIZE
)(
136 (EFIAPI
*EFI_BLOCK_CIPHER_INIT
)(
137 OUT VOID
*BlockCipherContext
,
144 (EFIAPI
*EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT
)(
145 IN VOID
*BlockCipherContext
,
146 IN CONST UINT8
*Input
,
153 (EFIAPI
*EFI_BLOCK_CIPHER_CBC_ENCRYPT_DECRYPT
)(
154 IN VOID
*BlockCipherContext
,
155 IN CONST UINT8
*Input
,
157 IN CONST UINT8
*Ivec
,
163 (EFIAPI
*EFI_BLOCK_CIPHER_RESET
)(
164 IN OUT VOID
*BlockCipherContext
168 EFI_BLOCK_CIPHER_GET_CONTEXT_SIZE GetContextSize
;
169 EFI_BLOCK_CIPHER_INIT Init
;
170 EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT EcbEncrypt
;
171 EFI_BLOCK_CIPHER_ECB_ENCRYPT_DECRYPT EcbDecrypt
;
172 EFI_BLOCK_CIPHER_CBC_ENCRYPT_DECRYPT CbcEncrypt
;
173 EFI_BLOCK_CIPHER_CBC_ENCRYPT_DECRYPT CbcDecrypt
;
174 EFI_BLOCK_CIPHER_RESET Reset
;
183 } BLOCK_CIPHER_TEST_CONTEXT
;
185 // These are commented out as they are deprecated, but are left in should they be used again
186 // BLOCK_CIPHER_TEST_CONTEXT mTdesEcbTestCtx = {TdesGetContextSize, TdesInit, TdesEcbEncrypt, TdesEcbDecrypt, NULL, NULL, NULL, TdesEcbKey, 64, NULL, TdesEcbData, 8, TdesEcbCipher, 8};
187 // BLOCK_CIPHER_TEST_CONTEXT mTdesCbcTestCtx = {TdesGetContextSize, TdesInit, NULL, NULL, TdesCbcEncrypt, TdesCbcDecrypt, NULL, TdesCbcKey, 192, TdesCbcIvec, TdesCbcData, sizeof(TdesCbcData), TdesCbc3Cipher, sizeof(TdesCbc3Cipher)};
188 // BLOCK_CIPHER_TEST_CONTEXT mAes128EcbTestCtx = {AesGetContextSize, AesInit, AesEcbEncrypt, AesEcbDecrypt, NULL, NULL, NULL, Aes128EcbKey, 128, NULL, Aes128EcbData, sizeof(Aes128EcbData), Aes128EcbCipher, sizeof(Aes128EcbCipher)};
189 // BLOCK_CIPHER_TEST_CONTEXT mAes192EcbTestCtx = {AesGetContextSize, AesInit, AesEcbEncrypt, AesEcbDecrypt, NULL, NULL, NULL, Aes192EcbKey, 192, NULL, Aes192EcbData, sizeof(Aes192EcbData), Aes192EcbCipher, sizeof(Aes192EcbCipher)};
190 // BLOCK_CIPHER_TEST_CONTEXT mAes256EcbTestCtx = {AesGetContextSize, AesInit, AesEcbEncrypt, AesEcbDecrypt, NULL, NULL, NULL, Aes256EcbKey, 256, NULL, Aes256EcbData, sizeof(Aes256EcbData), Aes256EcbCipher, sizeof(Aes256EcbCipher)};
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)};
192 BLOCK_CIPHER_TEST_CONTEXT mAes128CbcTestCtx
= { AesGetContextSize
, AesInit
, NULL
, NULL
, AesCbcEncrypt
, AesCbcDecrypt
, NULL
, Aes128CbcKey
, 128, Aes128CbcIvec
, Aes128CbcData
, sizeof (Aes128CbcData
), Aes128CbcCipher
, sizeof (Aes128CbcCipher
) };
196 TestVerifyBLockCiperPreReq (
197 UNIT_TEST_CONTEXT Context
200 BLOCK_CIPHER_TEST_CONTEXT
*TestContext
;
203 TestContext
= Context
;
204 CtxSize
= TestContext
->GetContextSize ();
205 TestContext
->Ctx
= AllocatePool (CtxSize
);
206 if (TestContext
->Ctx
== NULL
) {
207 return UNIT_TEST_ERROR_TEST_FAILED
;
210 return UNIT_TEST_PASSED
;
215 TestVerifyBLockCiperCleanUp (
216 UNIT_TEST_CONTEXT Context
219 BLOCK_CIPHER_TEST_CONTEXT
*TestContext
;
221 TestContext
= Context
;
222 if (TestContext
->Ctx
!= NULL
) {
223 FreePool (TestContext
->Ctx
);
229 TestVerifyBLockCiper (
230 UNIT_TEST_CONTEXT Context
236 BLOCK_CIPHER_TEST_CONTEXT
*TestContext
;
238 TestContext
= Context
;
240 ZeroMem (Encrypt
, sizeof (Encrypt
));
241 ZeroMem (Decrypt
, sizeof (Decrypt
));
243 Status
= TestContext
->Init (TestContext
->Ctx
, TestContext
->Key
, TestContext
->KeySize
);
244 UT_ASSERT_TRUE (Status
);
246 if (TestContext
->Ivec
== NULL
) {
247 Status
= TestContext
->EcbEncrypt (TestContext
->Ctx
, TestContext
->Data
, TestContext
->DataSize
, Encrypt
);
248 UT_ASSERT_TRUE (Status
);
250 if (TestContext
->Reset
!= NULL
) {
251 Status
= TestContext
->Reset (TestContext
->Ctx
);
252 UT_ASSERT_TRUE (Status
);
255 Status
= TestContext
->EcbDecrypt (TestContext
->Ctx
, Encrypt
, TestContext
->DataSize
, Decrypt
);
256 UT_ASSERT_TRUE (Status
);
258 Status
= TestContext
->CbcEncrypt (TestContext
->Ctx
, TestContext
->Data
, TestContext
->DataSize
, TestContext
->Ivec
, Encrypt
);
259 UT_ASSERT_TRUE (Status
);
261 if (TestContext
->Reset
!= NULL
) {
262 Status
= TestContext
->Reset (TestContext
->Ctx
);
263 UT_ASSERT_TRUE (Status
);
266 Status
= TestContext
->CbcDecrypt (TestContext
->Ctx
, Encrypt
, TestContext
->DataSize
, TestContext
->Ivec
, Decrypt
);
267 UT_ASSERT_TRUE (Status
);
270 UT_ASSERT_MEM_EQUAL (Encrypt
, TestContext
->Cipher
, TestContext
->CipherSize
);
271 UT_ASSERT_MEM_EQUAL (Decrypt
, TestContext
->Data
, TestContext
->DataSize
);
273 return UNIT_TEST_PASSED
;
276 TEST_DESC mBlockCipherTest
[] = {
278 // -----Description-------------------------Class-------------------------Function---------------Pre---------------------------Post------------------Context
280 { "TestVerifyAes128Cbc()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper
, TestVerifyBLockCiperPreReq
, TestVerifyBLockCiperCleanUp
, &mAes128CbcTestCtx
},
281 // These are commented out as these functions have been deprecated, but they have been left in for future reference
282 // {"TestVerifyTdesEcb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mTdesEcbTestCtx},
283 // {"TestVerifyTdesCbc()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mTdesCbcTestCtx},
284 // {"TestVerifyAes128Ecb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes128EcbTestCtx},
285 // {"TestVerifyAes192Ecb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes192EcbTestCtx},
286 // {"TestVerifyAes256Ecb()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mAes256EcbTestCtx},
287 // {"TestVerifyArc4()", "CryptoPkg.BaseCryptLib.BlockCipher", TestVerifyBLockCiper, TestVerifyBLockCiperPreReq, TestVerifyBLockCiperCleanUp, &mArc4TestCtx},
290 UINTN mBlockCipherTestNum
= ARRAY_SIZE (mBlockCipherTest
);