Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include <securityerr.h>
22 #include "tpaddingPKCS7.h"
23 #include <cryptopanic.h>
25 static void GenerateInput(TInt aBytes, TPtr8& in)
27 for (TInt j = 0; j < aBytes; j++)
34 CTestPadPKCS7::CTestPadPKCS7()
36 SetTestStepName(KPadPKCS7);
39 CTestPadPKCS7::~CTestPadPKCS7()
43 TVerdict CTestPadPKCS7::doTestStepL()
45 SetTestStepResult(EPass);
48 INFO_PRINTF1(_L("Test of PKCS7 padding"));
50 for (TInt i = 1; i < 255; i++)
56 return TestStepResult();
60 void CTestPadPKCS7::TestPKCS7Padding(TInt aBlockSize)
62 CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
63 // Starts with zero input size(total block data is filled/padded with block size)
64 for (TInt i = 0 ; i <= aBlockSize; i++)
66 HBufC8 *padInData = HBufC8::NewLC(i);
67 HBufC8 *padOutData = HBufC8::NewLC(i+(aBlockSize-i%aBlockSize));
68 TPtr8 in(padInData->Des());
69 TPtr8 out(padOutData->Des());
72 TRAPD(err, padding->PadL(in, out));
74 TEST(err == KErrNone);
76 TInt totalLength = out.Length();
77 TUint paddingLength = aBlockSize - in.Length()%aBlockSize;
78 // Test that the total length is a multiple of blockSize
79 TEST((totalLength % aBlockSize) == 0);
81 // Test that the padding bytes are equal in value to the paddingLength,
82 // ie, if padding length is 5 the 5 last octets in the out array should be 0x05
83 for (TInt j = paddingLength; j > 0 ; j--)
85 TEST(out[out.Length()-j] == paddingLength);
88 // Test that the data has not been corrupted
89 TEST(in == out.Left(out.Length() - paddingLength));
91 CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
93 CleanupStack::PopAndDestroy(padding);
96 CTestUnpadPKCS7::CTestUnpadPKCS7()
98 SetTestStepName(KUnpadPKCS7);
101 CTestUnpadPKCS7::~CTestUnpadPKCS7()
105 TVerdict CTestUnpadPKCS7::doTestStepL()
107 SetTestStepResult(EPass);
110 INFO_PRINTF1(_L("Test of PKCS7 unpadding"));
111 for (TInt i = 1; i < 255; i++)
113 TestPKCS7Unpadding(i);
117 return TestStepResult();
121 void CTestUnpadPKCS7::TestPKCS7Unpadding(TInt aBlockSize)
123 CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
125 // Input must be < aBlockSize otherwise this wouldn't be
127 for (TInt i = 0 ; i < aBlockSize; i++)
129 // Input to un-padding should always be an entire block
130 // for correctly data.
131 HBufC8 *padInData = HBufC8::NewLC(aBlockSize);
132 HBufC8 *padOutData = HBufC8::NewLC(i);
133 HBufC8 *padCompareData = HBufC8::NewLC(i);
134 TPtr8 in(padInData->Des());
135 TPtr8 out(padOutData->Des());
136 TPtr8 comp(padCompareData->Des());
138 GenerateInput(i, in);
141 in.SetLength(aBlockSize);
142 TInt paddingBytes = aBlockSize - (i % aBlockSize);
143 for (TInt j = 1; j <= paddingBytes; j++)
145 in[in.Length()-j] = (TUint8)paddingBytes;
148 TRAPD(err, padding->UnPadL(in, out));
151 INFO_PRINTF3(_L("The Error returned for block size %d is %d"), aBlockSize,err);
153 TEST(err == KErrNone); // Verify UnPadL leave code
154 TEST(out == comp); // Verify UnPadL output data with expected data
155 CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
157 CleanupStack::PopAndDestroy(padding);
161 CTestUnpadCorruptPKCS7::CTestUnpadCorruptPKCS7()
163 SetTestStepName(KUnpadCorruptPKCS7);
166 CTestUnpadCorruptPKCS7::~CTestUnpadCorruptPKCS7()
170 TVerdict CTestUnpadCorruptPKCS7::doTestStepL()
172 SetTestStepResult(EPass);
179 if (GetIntFromConfig(ConfigSection(), _L("blocksize"), blockSize))
181 if (GetIntFromConfig(ConfigSection(), _L("textsize"), textSize))
183 if (GetIntFromConfig(ConfigSection(), _L("paddingbyte"), paddingNum))
185 INFO_PRINTF1(_L("Test of PKCS7 unpadding with corrupt data"));
186 TUint8 paddingByte = Min(paddingNum, 255);
187 TestCorruptPKCS7Unpadding(blockSize, textSize, paddingByte);
191 ERR_PRINTF1(_L("Missing parameter - paddingbyte"));
196 ERR_PRINTF1(_L("Missing parameter - textsize"));
201 ERR_PRINTF1(_L("Missing parameter - blocksize"));
205 return TestStepResult();
208 void CTestUnpadCorruptPKCS7::TestCorruptPKCS7Unpadding(TInt aBlockSize, TInt aTextSize, TUint8 aPaddingByte)
210 CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
211 TInt paddingBytes = (aBlockSize - aTextSize % aBlockSize);
213 HBufC8 *padInData = HBufC8::NewLC(aTextSize + paddingBytes);
214 HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
215 TPtr8 in(padInData->Des());
216 TPtr8 out(padOutData->Des());
218 GenerateInput(aTextSize, in);
220 in.SetLength(in.Length() + paddingBytes);
221 for (TInt j = 1; j <= paddingBytes; j++)
223 in[in.Length()-j] = (TUint8) aPaddingByte;
226 TRAPD(err, padding->UnPadL(in, out));
228 if ( err == KErrInvalidPadding )
230 INFO_PRINTF2(_L("The PKCS7 unpadding UnPadL method returned error is %d"), err);
231 TEST(err == KErrInvalidPadding);
232 SetTestStepResult(EPass);
234 else if ( err == KErrNone )
236 TEST(err == KErrNone);
238 CleanupStack::PopAndDestroy(3, padding); // padding, padInData, padOutData
241 CTestPaddingCorruptPKCS7::CTestPaddingCorruptPKCS7()
243 SetTestStepName(KPaddingCorruptPKCS7);
246 CTestPaddingCorruptPKCS7::~CTestPaddingCorruptPKCS7()
250 TVerdict CTestPaddingCorruptPKCS7::doTestStepL()
252 SetTestStepResult(EPass);
258 if (GetIntFromConfig(ConfigSection(), _L("blocksize"), blockSize))
260 if (GetIntFromConfig(ConfigSection(), _L("textsize"), textSize))
262 INFO_PRINTF1(_L("Test of PKCS7 unpadding with corrupt data"));
263 TestCorruptPKCS7padding(blockSize, textSize);
267 ERR_PRINTF1(_L("Missing parameter - textsize"));
272 ERR_PRINTF1(_L("Missing parameter - blocksize"));
276 return TestStepResult();
279 void CTestPaddingCorruptPKCS7::TestCorruptPKCS7padding(TInt aBlockSize, TInt aTextSize)
281 CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
283 TInt paddingBytes = 0;
284 //Divide by 0 is undefined.
287 paddingBytes = aBlockSize - (aTextSize % aBlockSize);
290 HBufC8 *padOutData = HBufC8::NewLC(aTextSize + paddingBytes);
291 HBufC8 *padInData = HBufC8::NewLC(aTextSize);
292 TPtr8 in(padInData->Des());
293 TPtr8 out(padOutData->Des());
295 GenerateInput(aTextSize, in);
296 TRAPD(err, padding->PadL(in, out));
298 INFO_PRINTF2(_L("The PKCS7 padding PadL method returned error is %d"), err);
300 //check expected result
301 TPtrC expectedContent;
302 if (GetStringFromConfig(ConfigSection(), _L("case"), expectedContent))
304 if(expectedContent.Compare(_L("InvalidPadding")) ==0)
306 TEST(err == KErrInvalidPadding);
308 else if(expectedContent.Compare(_L("Valid")) ==0)
310 TEST(err == KErrNone);
312 else if(expectedContent.Compare(_L("CorruptBlockSize")) ==0)
314 TEST(err == KErrArgument);
318 //skip the checking on padded data if padding is unsuccessful(no padding is done),
319 //otherwise the erroneous operation on output descriptor will panic.
322 CleanupStack::PopAndDestroy(3, padding);
326 TInt totalLength = out.Length();
327 TInt inLength = in.Length();
329 TUint paddingLength = 0;
330 //Divide by 0 is undefined.
333 paddingLength = aBlockSize - inLength%aBlockSize;
334 // Test that the total length is a multiple of blockSize
335 TEST((totalLength % aBlockSize) == 0);
338 // Test that the padding bytes are equal in value to the paddingLength,
339 // ie, if padding length is 5 the 5 last octets in the out array should be 0x05
340 for (TInt j = paddingLength; j > 0 ; j--)
342 TEST(out[out.Length()-j] == paddingLength);
347 // Test that the data has not been corrupted
348 TEST(in == out.Left(out.Length() - paddingLength));
350 CleanupStack::PopAndDestroy(3, padding); // padInData, padOutData, padCompareData, padding