First public contribution.
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 "tpaddingPKCS1.h"
24 CTestPadPKCS1::CTestPadPKCS1()
26 SetTestStepName(KPadPKCS1);
29 CTestPadPKCS1::~CTestPadPKCS1()
33 TVerdict CTestPadPKCS1::doTestStepL()
38 INFO_PRINTF1(_L("Test of PKCS1 signature padding"));
40 SetTestStepResult(EPass);
41 INFO_PRINTF1(_L("Blocksize 512"));
42 TestPKCS1SignaturePadding(512);
44 SetTestStepResult(EPass);
45 INFO_PRINTF1(_L("Blocksize 1024"));
46 TestPKCS1SignaturePadding(1024);
48 SetTestStepResult(EPass);
49 INFO_PRINTF1(_L("Blocksize 2048"));
50 TestPKCS1SignaturePadding(2048);
52 INFO_PRINTF1(_L("Test of PKCS1 encryption padding"));
54 SetTestStepResult(EPass);
55 INFO_PRINTF1(_L("Blocksize 512"));
56 TestPKCS1EncryptionPadding(512);
58 SetTestStepResult(EPass);
59 INFO_PRINTF1(_L("Blocksize 1024"));
60 TestPKCS1EncryptionPadding(1024);
62 SetTestStepResult(EPass);
63 INFO_PRINTF1(_L("Blocksize 2048"));
64 TestPKCS1EncryptionPadding(2048);
68 return TestStepResult();
71 void CTestPadPKCS1::TestPKCS1SignaturePadding(TInt aBlockSize)
73 CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
75 * The length of the data D shall not be more than k-11 octets,
76 * which is positive since the length k of the modulus is at
79 TInt noDataBytes = aBlockSize-11;
81 for (TInt i = 0; i <= noDataBytes; i++)
83 HBufC8 *inData = HBufC8::NewLC(noDataBytes);
84 HBufC8 *outData = HBufC8::NewLC(aBlockSize);
85 TPtr8 in(inData->Des());
86 TPtr8 out(outData->Des());
89 for (j = 0; j < i; j++)
95 TRAPD(err, padding->PadL(in, out));
96 TEST(err == KErrNone);
99 // should have the following syntax:
100 // 00 || 01 || PS || 00 || D where PS is the padding octects of value 0xff
102 TEST(out[0] == 0 && out[1] == 1);
104 TInt endOfPadding = aBlockSize - 1 - in.Length();
105 for (j = 2; j < endOfPadding; j++)
110 // Check for the delimiter
111 TEST(out[endOfPadding] == 0);
112 // Check that the rest of the data is not corrupted
113 TInt startOfData = aBlockSize - in.Length();
114 TEST(in == out.Mid(startOfData));
115 CleanupStack::PopAndDestroy(2, inData); // inData, outData
117 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
118 CleanupStack::PopAndDestroy(); // padding
121 void CTestPadPKCS1::TestPKCS1EncryptionPadding(TInt aBlockSize)
123 CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
125 * The length of the data D shall not be more than k-11 octets,
126 * which is positive since the length k of the modulus is at
129 TInt noDataBytes = aBlockSize-11;
131 for (TInt i = 0; i <= noDataBytes; i++)
133 HBufC8 *inData = HBufC8::NewLC(noDataBytes);
134 HBufC8 *outData = HBufC8::NewLC(aBlockSize);
135 TPtr8 in(inData->Des());
136 TPtr8 out(outData->Des());
139 for (j = 0; j < i; j++)
145 TRAPD(err, padding->PadL(in, out));
146 TEST(err == KErrNone);
147 // check the padding.
148 // should have the following syntax:
149 // 00 || 02 || PS || 00 || D where PS is the padding octects containing random data
151 TEST(out[0] == 0 && out[1] == 2);
153 TInt endOfPadding = aBlockSize - 1 - in.Length();
154 for (j = 2; j < endOfPadding; j++)
159 // Check for the delimiter
160 TEST(out[endOfPadding] == 0);
162 // Check that the rest of the data is not corrupted
163 TInt startOfData = aBlockSize - in.Length();
164 TEST(in == out.Mid(startOfData));
165 CleanupStack::PopAndDestroy(2, inData); // inData, outData
167 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
168 CleanupStack::PopAndDestroy(); // padding
172 CTestUnpadPKCS1::CTestUnpadPKCS1()
174 SetTestStepName(KUnpadPKCS1);
177 CTestUnpadPKCS1::~CTestUnpadPKCS1()
181 TVerdict CTestUnpadPKCS1::doTestStepL()
185 INFO_PRINTF1(_L("Test of PKCS1 signature unpadding"));
186 INFO_PRINTF1(_L("Blocksize 512"));
187 SetTestStepResult(EPass);
188 TestPKCS1SignatureUnpadding(512);
189 INFO_PRINTF1(_L("Blocksize 1024"));
190 SetTestStepResult(EPass);
191 TestPKCS1SignatureUnpadding(1024);
192 INFO_PRINTF1(_L("Blocksize 2048"));
193 SetTestStepResult(EPass);
194 TestPKCS1SignatureUnpadding(2048);
196 INFO_PRINTF1(_L("Test of PKCS1 encryption unpadding"));
197 INFO_PRINTF1(_L("Blocksize 512"));
198 SetTestStepResult(EPass);
199 TestPKCS1EncryptionUnpadding(512);
200 INFO_PRINTF1(_L("Blocksize 1024"));
201 SetTestStepResult(EPass);
202 TestPKCS1EncryptionUnpadding(1024);
203 INFO_PRINTF1(_L("Blocksize 2048"));
204 SetTestStepResult(EPass);
205 TestPKCS1EncryptionUnpadding(2048);
208 return TestStepResult();
211 void CTestUnpadPKCS1::TestPKCS1SignatureUnpadding(TInt aBlockSize)
213 CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
215 * The length of the data D shall not be more than k-11 octets,
216 * which is positive since the length k of the modulus is at
219 TInt noDataBytes = aBlockSize-11;
221 for (TInt i = 0; i <= noDataBytes; i++)
223 HBufC8 *inData = HBufC8::NewLC(aBlockSize);
224 HBufC8 *outData = HBufC8::NewLC(noDataBytes);
225 HBufC8 *compData = HBufC8::NewLC(noDataBytes);
226 TPtr8 in(inData->Des());
227 TPtr8 out(outData->Des());
228 TPtr8 comp(compData->Des());
230 TInt endOfPadding = aBlockSize - 1 - i;
231 in.SetLength(endOfPadding+1);
235 for (j = 2; j < endOfPadding; j++)
239 in[endOfPadding] = 0; // delimiter
241 for (j = 0; j < i; j++)
248 TRAPD(err, padding->UnPadL(in, out));
249 TEST(err == KErrNone);
253 TEST(out.Length() == i);
256 CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
258 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
259 CleanupStack::PopAndDestroy(); // padding
262 void CTestUnpadPKCS1::TestPKCS1EncryptionUnpadding(TInt aBlockSize)
264 CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
266 * "The length of the data D shall not be more than k-11 octets,
267 * which is positive since the length k of the modulus is at
270 TInt noDataBytes = aBlockSize-11;
271 for (TInt i = 0; i <= noDataBytes; i++)
273 HBufC8 *inData = HBufC8::NewLC(aBlockSize);
274 HBufC8 *outData = HBufC8::NewLC(noDataBytes);
275 HBufC8 *compData = HBufC8::NewLC(noDataBytes);
276 TPtr8 in(inData->Des());
277 TPtr8 out(outData->Des());
278 TPtr8 comp(compData->Des());
280 TInt endOfPadding = aBlockSize - 1 - i;
281 in.SetLength(endOfPadding+1);
285 TRAPD(err, GenerateRandomBytesL(rnd));
286 if((err != KErrNone) && (err != KErrNotSecure))
292 while (k < endOfPadding)
296 in[k++] = rnd[j%256];
301 TRAP(err, GenerateRandomBytesL(rnd));
302 if((err != KErrNone) && (err != KErrNotSecure))
306 in[endOfPadding] = 0; // delimiter
308 for (j = 0; j < i; j++)
315 TRAP(err, padding->UnPadL(in, out));
316 TEST(err == KErrNone);
320 TEST(out.Length() == i);
323 CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
325 TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
326 CleanupStack::PopAndDestroy(); // padding