1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tpadding/tpaddingPKCS7.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,352 @@
1.4 +/*
1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <random.h>
1.23 +#include <padding.h>
1.24 +#include <securityerr.h>
1.25 +#include "tpaddingPKCS7.h"
1.26 +#include <cryptopanic.h>
1.27 +
1.28 +static void GenerateInput(TInt aBytes, TPtr8& in)
1.29 + {
1.30 + for (TInt j = 0; j < aBytes; j++)
1.31 + {
1.32 + TInt text('a'+j%25);
1.33 + in.Append(text);
1.34 + }
1.35 + }
1.36 +
1.37 +CTestPadPKCS7::CTestPadPKCS7()
1.38 + {
1.39 + SetTestStepName(KPadPKCS7);
1.40 + }
1.41 +
1.42 +CTestPadPKCS7::~CTestPadPKCS7()
1.43 + {
1.44 + }
1.45 +
1.46 +TVerdict CTestPadPKCS7::doTestStepL()
1.47 + {
1.48 + SetTestStepResult(EPass);
1.49 + __UHEAP_MARK;
1.50 +
1.51 + INFO_PRINTF1(_L("Test of PKCS7 padding"));
1.52 +
1.53 + for (TInt i = 1; i < 255; i++)
1.54 + {
1.55 + TestPKCS7Padding(i);
1.56 + }
1.57 +
1.58 + __UHEAP_MARKEND;
1.59 + return TestStepResult();
1.60 + }
1.61 +
1.62 +
1.63 +void CTestPadPKCS7::TestPKCS7Padding(TInt aBlockSize)
1.64 + {
1.65 + CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
1.66 + // Starts with zero input size(total block data is filled/padded with block size)
1.67 + for (TInt i = 0 ; i <= aBlockSize; i++)
1.68 + {
1.69 + HBufC8 *padInData = HBufC8::NewLC(i);
1.70 + HBufC8 *padOutData = HBufC8::NewLC(i+(aBlockSize-i%aBlockSize));
1.71 + TPtr8 in(padInData->Des());
1.72 + TPtr8 out(padOutData->Des());
1.73 +
1.74 + GenerateInput(i, in);
1.75 + TRAPD(err, padding->PadL(in, out));
1.76 +
1.77 + TEST(err == KErrNone);
1.78 +
1.79 + TInt totalLength = out.Length();
1.80 + TUint paddingLength = aBlockSize - in.Length()%aBlockSize;
1.81 + // Test that the total length is a multiple of blockSize
1.82 + TEST((totalLength % aBlockSize) == 0);
1.83 +
1.84 + // Test that the padding bytes are equal in value to the paddingLength,
1.85 + // ie, if padding length is 5 the 5 last octets in the out array should be 0x05
1.86 + for (TInt j = paddingLength; j > 0 ; j--)
1.87 + {
1.88 + TEST(out[out.Length()-j] == paddingLength);
1.89 + }
1.90 +
1.91 + // Test that the data has not been corrupted
1.92 + TEST(in == out.Left(out.Length() - paddingLength));
1.93 +
1.94 + CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
1.95 + }
1.96 + CleanupStack::PopAndDestroy(padding);
1.97 + }
1.98 +
1.99 +CTestUnpadPKCS7::CTestUnpadPKCS7()
1.100 + {
1.101 + SetTestStepName(KUnpadPKCS7);
1.102 + }
1.103 +
1.104 +CTestUnpadPKCS7::~CTestUnpadPKCS7()
1.105 + {
1.106 + }
1.107 +
1.108 +TVerdict CTestUnpadPKCS7::doTestStepL()
1.109 + {
1.110 + SetTestStepResult(EPass);
1.111 + __UHEAP_MARK;
1.112 +
1.113 + INFO_PRINTF1(_L("Test of PKCS7 unpadding"));
1.114 + for (TInt i = 1; i < 255; i++)
1.115 + {
1.116 + TestPKCS7Unpadding(i);
1.117 + }
1.118 +
1.119 + __UHEAP_MARKEND;
1.120 + return TestStepResult();
1.121 + }
1.122 +
1.123 +
1.124 +void CTestUnpadPKCS7::TestPKCS7Unpadding(TInt aBlockSize)
1.125 + {
1.126 + CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
1.127 +
1.128 + // Input must be < aBlockSize otherwise this wouldn't be
1.129 + // a padded block
1.130 + for (TInt i = 0 ; i < aBlockSize; i++)
1.131 + {
1.132 + // Input to un-padding should always be an entire block
1.133 + // for correctly data.
1.134 + HBufC8 *padInData = HBufC8::NewLC(aBlockSize);
1.135 + HBufC8 *padOutData = HBufC8::NewLC(i);
1.136 + HBufC8 *padCompareData = HBufC8::NewLC(i);
1.137 + TPtr8 in(padInData->Des());
1.138 + TPtr8 out(padOutData->Des());
1.139 + TPtr8 comp(padCompareData->Des());
1.140 +
1.141 + GenerateInput(i, in);
1.142 + comp.Copy(in);
1.143 +
1.144 + in.SetLength(aBlockSize);
1.145 + TInt paddingBytes = aBlockSize - (i % aBlockSize);
1.146 + for (TInt j = 1; j <= paddingBytes; j++)
1.147 + {
1.148 + in[in.Length()-j] = (TUint8)paddingBytes;
1.149 + }
1.150 +
1.151 + TRAPD(err, padding->UnPadL(in, out));
1.152 + if (err != KErrNone)
1.153 + {
1.154 + INFO_PRINTF3(_L("The Error returned for block size %d is %d"), aBlockSize,err);
1.155 + }
1.156 + TEST(err == KErrNone); // Verify UnPadL leave code
1.157 + TEST(out == comp); // Verify UnPadL output data with expected data
1.158 + CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
1.159 + }
1.160 + CleanupStack::PopAndDestroy(padding);
1.161 + }
1.162 +
1.163 +
1.164 +CTestUnpadCorruptPKCS7::CTestUnpadCorruptPKCS7()
1.165 + {
1.166 + SetTestStepName(KUnpadCorruptPKCS7);
1.167 + }
1.168 +
1.169 +CTestUnpadCorruptPKCS7::~CTestUnpadCorruptPKCS7()
1.170 + {
1.171 + }
1.172 +
1.173 +TVerdict CTestUnpadCorruptPKCS7::doTestStepL()
1.174 + {
1.175 + SetTestStepResult(EPass);
1.176 + __UHEAP_MARK;
1.177 +
1.178 + TInt blockSize;
1.179 + TInt textSize;
1.180 + TInt paddingNum = 0;
1.181 +
1.182 + if (GetIntFromConfig(ConfigSection(), _L("blocksize"), blockSize))
1.183 + {
1.184 + if (GetIntFromConfig(ConfigSection(), _L("textsize"), textSize))
1.185 + {
1.186 + if (GetIntFromConfig(ConfigSection(), _L("paddingbyte"), paddingNum))
1.187 + {
1.188 + INFO_PRINTF1(_L("Test of PKCS7 unpadding with corrupt data"));
1.189 + TUint8 paddingByte = Min(paddingNum, 255);
1.190 + TestCorruptPKCS7Unpadding(blockSize, textSize, paddingByte);
1.191 + }
1.192 + else
1.193 + {
1.194 + ERR_PRINTF1(_L("Missing parameter - paddingbyte"));
1.195 + }
1.196 + }
1.197 + else
1.198 + {
1.199 + ERR_PRINTF1(_L("Missing parameter - textsize"));
1.200 + }
1.201 + }
1.202 + else
1.203 + {
1.204 + ERR_PRINTF1(_L("Missing parameter - blocksize"));
1.205 + }
1.206 +
1.207 + __UHEAP_MARKEND;
1.208 + return TestStepResult();
1.209 + }
1.210 +
1.211 +void CTestUnpadCorruptPKCS7::TestCorruptPKCS7Unpadding(TInt aBlockSize, TInt aTextSize, TUint8 aPaddingByte)
1.212 + {
1.213 + CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
1.214 + TInt paddingBytes = (aBlockSize - aTextSize % aBlockSize);
1.215 +
1.216 + HBufC8 *padInData = HBufC8::NewLC(aTextSize + paddingBytes);
1.217 + HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
1.218 + TPtr8 in(padInData->Des());
1.219 + TPtr8 out(padOutData->Des());
1.220 +
1.221 + GenerateInput(aTextSize, in);
1.222 +
1.223 + in.SetLength(in.Length() + paddingBytes);
1.224 + for (TInt j = 1; j <= paddingBytes; j++)
1.225 + {
1.226 + in[in.Length()-j] = (TUint8) aPaddingByte;
1.227 + }
1.228 +
1.229 + TRAPD(err, padding->UnPadL(in, out));
1.230 +
1.231 + if ( err == KErrInvalidPadding )
1.232 + {
1.233 + INFO_PRINTF2(_L("The PKCS7 unpadding UnPadL method returned error is %d"), err);
1.234 + TEST(err == KErrInvalidPadding);
1.235 + SetTestStepResult(EPass);
1.236 + }
1.237 + else if ( err == KErrNone )
1.238 + {
1.239 + TEST(err == KErrNone);
1.240 + }
1.241 + CleanupStack::PopAndDestroy(3, padding); // padding, padInData, padOutData
1.242 + }
1.243 +
1.244 +CTestPaddingCorruptPKCS7::CTestPaddingCorruptPKCS7()
1.245 + {
1.246 + SetTestStepName(KPaddingCorruptPKCS7);
1.247 + }
1.248 +
1.249 +CTestPaddingCorruptPKCS7::~CTestPaddingCorruptPKCS7()
1.250 + {
1.251 + }
1.252 +
1.253 +TVerdict CTestPaddingCorruptPKCS7::doTestStepL()
1.254 + {
1.255 + SetTestStepResult(EPass);
1.256 + __UHEAP_MARK;
1.257 +
1.258 + TInt blockSize;
1.259 + TInt textSize;
1.260 +
1.261 + if (GetIntFromConfig(ConfigSection(), _L("blocksize"), blockSize))
1.262 + {
1.263 + if (GetIntFromConfig(ConfigSection(), _L("textsize"), textSize))
1.264 + {
1.265 + INFO_PRINTF1(_L("Test of PKCS7 unpadding with corrupt data"));
1.266 + TestCorruptPKCS7padding(blockSize, textSize);
1.267 + }
1.268 + else
1.269 + {
1.270 + ERR_PRINTF1(_L("Missing parameter - textsize"));
1.271 + }
1.272 + }
1.273 + else
1.274 + {
1.275 + ERR_PRINTF1(_L("Missing parameter - blocksize"));
1.276 + }
1.277 +
1.278 + __UHEAP_MARKEND;
1.279 + return TestStepResult();
1.280 + }
1.281 +
1.282 +void CTestPaddingCorruptPKCS7::TestCorruptPKCS7padding(TInt aBlockSize, TInt aTextSize)
1.283 + {
1.284 + CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
1.285 +
1.286 + TInt paddingBytes = 0;
1.287 + //Divide by 0 is undefined.
1.288 + if(aBlockSize != 0)
1.289 + {
1.290 + paddingBytes = aBlockSize - (aTextSize % aBlockSize);
1.291 + }
1.292 +
1.293 + HBufC8 *padOutData = HBufC8::NewLC(aTextSize + paddingBytes);
1.294 + HBufC8 *padInData = HBufC8::NewLC(aTextSize);
1.295 + TPtr8 in(padInData->Des());
1.296 + TPtr8 out(padOutData->Des());
1.297 +
1.298 + GenerateInput(aTextSize, in);
1.299 + TRAPD(err, padding->PadL(in, out));
1.300 +
1.301 + INFO_PRINTF2(_L("The PKCS7 padding PadL method returned error is %d"), err);
1.302 +
1.303 + //check expected result
1.304 + TPtrC expectedContent;
1.305 + if (GetStringFromConfig(ConfigSection(), _L("case"), expectedContent))
1.306 + {
1.307 + if(expectedContent.Compare(_L("InvalidPadding")) ==0)
1.308 + {
1.309 + TEST(err == KErrInvalidPadding);
1.310 + }
1.311 + else if(expectedContent.Compare(_L("Valid")) ==0)
1.312 + {
1.313 + TEST(err == KErrNone);
1.314 + }
1.315 + else if(expectedContent.Compare(_L("CorruptBlockSize")) ==0)
1.316 + {
1.317 + TEST(err == KErrArgument);
1.318 + }
1.319 + }
1.320 +
1.321 + //skip the checking on padded data if padding is unsuccessful(no padding is done),
1.322 + //otherwise the erroneous operation on output descriptor will panic.
1.323 + if(err != KErrNone)
1.324 + {
1.325 + CleanupStack::PopAndDestroy(3, padding);
1.326 + return;
1.327 + }
1.328 +
1.329 + TInt totalLength = out.Length();
1.330 + TInt inLength = in.Length();
1.331 +
1.332 + TUint paddingLength = 0;
1.333 + //Divide by 0 is undefined.
1.334 + if(aBlockSize != 0)
1.335 + {
1.336 + paddingLength = aBlockSize - inLength%aBlockSize;
1.337 + // Test that the total length is a multiple of blockSize
1.338 + TEST((totalLength % aBlockSize) == 0);
1.339 + }
1.340 +
1.341 + // Test that the padding bytes are equal in value to the paddingLength,
1.342 + // ie, if padding length is 5 the 5 last octets in the out array should be 0x05
1.343 + for (TInt j = paddingLength; j > 0 ; j--)
1.344 + {
1.345 + TEST(out[out.Length()-j] == paddingLength);
1.346 + }
1.347 +
1.348 +
1.349 +
1.350 + // Test that the data has not been corrupted
1.351 + TEST(in == out.Left(out.Length() - paddingLength));
1.352 +
1.353 + CleanupStack::PopAndDestroy(3, padding); // padInData, padOutData, padCompareData, padding
1.354 + }
1.355 +