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 "tpaddingPKCS1.h"
23 CTestPadPKCS1::CTestPadPKCS1()
25 SetTestStepName(KPadPKCS1);
28 CTestPadPKCS1::~CTestPadPKCS1()
32 TVerdict CTestPadPKCS1::doTestStepL()
37 INFO_PRINTF1(_L("Test of PKCS1 signature padding"));
39 SetTestStepResult(EPass);
40 INFO_PRINTF1(_L("Blocksize 512"));
41 TestPKCS1SignaturePadding(512);
43 SetTestStepResult(EPass);
44 INFO_PRINTF1(_L("Blocksize 1024"));
45 TestPKCS1SignaturePadding(1024);
47 SetTestStepResult(EPass);
48 INFO_PRINTF1(_L("Blocksize 2048"));
49 TestPKCS1SignaturePadding(2048);
51 INFO_PRINTF1(_L("Test of PKCS1 encryption padding"));
53 SetTestStepResult(EPass);
54 INFO_PRINTF1(_L("Blocksize 512"));
55 TestPKCS1EncryptionPadding(512);
57 SetTestStepResult(EPass);
58 INFO_PRINTF1(_L("Blocksize 1024"));
59 TestPKCS1EncryptionPadding(1024);
61 SetTestStepResult(EPass);
62 INFO_PRINTF1(_L("Blocksize 2048"));
63 TestPKCS1EncryptionPadding(2048);
67 return TestStepResult();
70 void CTestPadPKCS1::TestPKCS1SignaturePadding(TInt aBlockSize)
72 CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
74 * The length of the data D shall not be more than k-11 octets,
75 * which is positive since the length k of the modulus is at
78 TInt noDataBytes = aBlockSize-11;
80 for (TInt i = 0; i <= noDataBytes; i++)
82 HBufC8 *inData = HBufC8::NewLC(noDataBytes);
83 HBufC8 *outData = HBufC8::NewLC(aBlockSize);
84 TPtr8 in(inData->Des());
85 TPtr8 out(outData->Des());
88 for (j = 0; j < i; j++)
94 TRAPD(err, padding->DoPadL(in, out));
95 TEST(err == KErrNone);
98 // should have the following syntax:
99 // 00 || 01 || PS || 00 || D where PS is the padding octects of value 0xff
101 TEST(out[0] == 0 && out[1] == 1);
103 TInt endOfPadding = aBlockSize - 1 - in.Length();
104 for (j = 2; j < endOfPadding; j++)
109 // Check for the delimiter
110 TEST(out[endOfPadding] == 0);
111 // Check that the rest of the data is not corrupted
112 TInt startOfData = aBlockSize - in.Length();
113 TEST(in == out.Mid(startOfData));
114 CleanupStack::PopAndDestroy(2, inData); // inData, outData
116 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
117 CleanupStack::PopAndDestroy(); // padding
120 void CTestPadPKCS1::TestPKCS1EncryptionPadding(TInt aBlockSize)
122 CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
124 * The length of the data D shall not be more than k-11 octets,
125 * which is positive since the length k of the modulus is at
128 TInt noDataBytes = aBlockSize-11;
130 for (TInt i = 0; i <= noDataBytes; i++)
132 HBufC8 *inData = HBufC8::NewLC(noDataBytes);
133 HBufC8 *outData = HBufC8::NewLC(aBlockSize);
134 TPtr8 in(inData->Des());
135 TPtr8 out(outData->Des());
138 for (j = 0; j < i; j++)
144 TRAPD(err, padding->DoPadL(in, out));
145 TEST(err == KErrNone);
146 // check the padding.
147 // should have the following syntax:
148 // 00 || 02 || PS || 00 || D where PS is the padding octects containing random data
150 TEST(out[0] == 0 && out[1] == 2);
152 TInt endOfPadding = aBlockSize - 1 - in.Length();
153 for (j = 2; j < endOfPadding; j++)
158 // Check for the delimiter
159 TEST(out[endOfPadding] == 0);
161 // Check that the rest of the data is not corrupted
162 TInt startOfData = aBlockSize - in.Length();
163 TEST(in == out.Mid(startOfData));
164 CleanupStack::PopAndDestroy(2, inData); // inData, outData
166 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
167 CleanupStack::PopAndDestroy(); // padding
171 CTestUnpadPKCS1::CTestUnpadPKCS1()
173 SetTestStepName(KUnpadPKCS1);
176 CTestUnpadPKCS1::~CTestUnpadPKCS1()
180 TVerdict CTestUnpadPKCS1::doTestStepL()
184 INFO_PRINTF1(_L("Test of PKCS1 signature unpadding"));
185 INFO_PRINTF1(_L("Blocksize 512"));
186 SetTestStepResult(EPass);
187 TestPKCS1SignatureUnpadding(512);
188 INFO_PRINTF1(_L("Blocksize 1024"));
189 SetTestStepResult(EPass);
190 TestPKCS1SignatureUnpadding(1024);
191 INFO_PRINTF1(_L("Blocksize 2048"));
192 SetTestStepResult(EPass);
193 TestPKCS1SignatureUnpadding(2048);
195 INFO_PRINTF1(_L("Test of PKCS1 encryption unpadding"));
196 INFO_PRINTF1(_L("Blocksize 512"));
197 SetTestStepResult(EPass);
198 TestPKCS1EncryptionUnpadding(512);
199 INFO_PRINTF1(_L("Blocksize 1024"));
200 SetTestStepResult(EPass);
201 TestPKCS1EncryptionUnpadding(1024);
202 INFO_PRINTF1(_L("Blocksize 2048"));
203 SetTestStepResult(EPass);
204 TestPKCS1EncryptionUnpadding(2048);
207 return TestStepResult();
210 void CTestUnpadPKCS1::TestPKCS1SignatureUnpadding(TInt aBlockSize)
212 CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
214 * The length of the data D shall not be more than k-11 octets,
215 * which is positive since the length k of the modulus is at
218 TInt noDataBytes = aBlockSize-11;
220 for (TInt i = 0; i <= noDataBytes; i++)
222 HBufC8 *inData = HBufC8::NewLC(aBlockSize);
223 HBufC8 *outData = HBufC8::NewLC(noDataBytes);
224 HBufC8 *compData = HBufC8::NewLC(noDataBytes);
225 TPtr8 in(inData->Des());
226 TPtr8 out(outData->Des());
227 TPtr8 comp(compData->Des());
229 TInt endOfPadding = aBlockSize - 1 - i;
230 in.SetLength(endOfPadding+1);
234 for (j = 2; j < endOfPadding; j++)
238 in[endOfPadding] = 0; // delimiter
240 for (j = 0; j < i; j++)
247 TRAPD(err, padding->UnPadL(in, out));
248 TEST(err == KErrNone);
252 TEST(out.Length() == i);
255 CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
257 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
258 CleanupStack::PopAndDestroy(); // padding
261 void CTestUnpadPKCS1::TestPKCS1EncryptionUnpadding(TInt aBlockSize)
263 CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
265 * "The length of the data D shall not be more than k-11 octets,
266 * which is positive since the length k of the modulus is at
269 TInt noDataBytes = aBlockSize-11;
270 for (TInt i = 0; i <= noDataBytes; i++)
272 HBufC8 *inData = HBufC8::NewLC(aBlockSize);
273 HBufC8 *outData = HBufC8::NewLC(noDataBytes);
274 HBufC8 *compData = HBufC8::NewLC(noDataBytes);
275 TPtr8 in(inData->Des());
276 TPtr8 out(outData->Des());
277 TPtr8 comp(compData->Des());
279 TInt endOfPadding = aBlockSize - 1 - i;
280 in.SetLength(endOfPadding+1);
284 GenerateRandomBytesL(rnd);
288 while (k < endOfPadding)
292 in[k++] = rnd[j%256];
297 GenerateRandomBytesL(rnd);
300 in[endOfPadding] = 0; // delimiter
302 for (j = 0; j < i; j++)
309 TRAPD(err, padding->UnPadL(in, out));
310 TEST(err == KErrNone);
314 TEST(out.Length() == i);
317 CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
319 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
320 CleanupStack::PopAndDestroy(); // padding