crypto/weakcrypto/test/tpadding/tpaddingPKCS7.cpp
changeset 72 de46a57f75fb
equal deleted inserted replaced
65:970c0057d9bc 72:de46a57f75fb
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <random.h>
       
    20 #include <padding.h>
       
    21 #include <securityerr.h>
       
    22 #include "tpaddingPKCS7.h"
       
    23 #include <cryptopanic.h>
       
    24 
       
    25 static void GenerateInput(TInt aBytes, TPtr8& in)
       
    26  	{
       
    27  	for (TInt j = 0; j < aBytes; j++)
       
    28  		{
       
    29  		TInt text('a'+j%25);
       
    30  		in.Append(text);
       
    31  		}
       
    32  	}
       
    33 
       
    34 CTestPadPKCS7::CTestPadPKCS7()
       
    35 	{
       
    36 	SetTestStepName(KPadPKCS7);
       
    37 	}
       
    38 
       
    39 CTestPadPKCS7::~CTestPadPKCS7()
       
    40 	{
       
    41 	}
       
    42 
       
    43 TVerdict CTestPadPKCS7::doTestStepL()
       
    44 	{
       
    45 	SetTestStepResult(EPass);
       
    46 	__UHEAP_MARK;
       
    47    
       
    48 	INFO_PRINTF1(_L("Test of PKCS7  padding"));
       
    49 
       
    50 	for (TInt i = 1; i < 255; i++)
       
    51 		{
       
    52 		TestPKCS7Padding(i);
       
    53 		}
       
    54   
       
    55 	__UHEAP_MARKEND;
       
    56 	return TestStepResult();
       
    57 	}
       
    58 
       
    59 
       
    60 void CTestPadPKCS7::TestPKCS7Padding(TInt aBlockSize)
       
    61 	{
       
    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++)
       
    65 		{
       
    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());
       
    70 
       
    71 		GenerateInput(i, in);
       
    72 		TRAPD(err, padding->DoPadL(in, out));
       
    73 		
       
    74 		TEST(err == KErrNone);
       
    75 		
       
    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);
       
    80       
       
    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--)
       
    84 			{
       
    85 			TEST(out[out.Length()-j] == paddingLength);
       
    86 			}
       
    87       
       
    88 		// Test that the data has not been corrupted
       
    89 		TEST(in == out.Left(out.Length() - paddingLength));
       
    90 		
       
    91 		CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
       
    92 		}
       
    93 	CleanupStack::PopAndDestroy(padding);
       
    94 	}
       
    95    
       
    96 CTestUnpadPKCS7::CTestUnpadPKCS7()
       
    97 	{
       
    98    	SetTestStepName(KUnpadPKCS7);
       
    99 	}
       
   100 
       
   101 CTestUnpadPKCS7::~CTestUnpadPKCS7()
       
   102 	{
       
   103 	}
       
   104 
       
   105 TVerdict CTestUnpadPKCS7::doTestStepL()
       
   106 	{
       
   107 	SetTestStepResult(EPass);
       
   108 	__UHEAP_MARK;
       
   109 
       
   110 	INFO_PRINTF1(_L("Test of PKCS7 unpadding"));
       
   111 	for (TInt i = 1; i < 255; i++)
       
   112 		{
       
   113 		TestPKCS7Unpadding(i);
       
   114 		}
       
   115 
       
   116 	__UHEAP_MARKEND;
       
   117 	return TestStepResult();
       
   118 	}
       
   119 
       
   120 
       
   121 void CTestUnpadPKCS7::TestPKCS7Unpadding(TInt aBlockSize)
       
   122 	{
       
   123 	CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
       
   124 
       
   125 	// Input must be < aBlockSize otherwise this wouldn't be 
       
   126 	// a padded block
       
   127 	for (TInt i = 0 ; i < aBlockSize; i++)
       
   128 		{
       
   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());
       
   137 
       
   138 		GenerateInput(i, in);
       
   139 		comp.Copy(in);		
       
   140 
       
   141 		in.SetLength(aBlockSize);
       
   142 		TInt paddingBytes = aBlockSize - (i % aBlockSize);
       
   143 		for (TInt j = 1; j <= paddingBytes; j++)
       
   144 			{
       
   145 			in[in.Length()-j] = (TUint8)paddingBytes;
       
   146 			}
       
   147 
       
   148 		TRAPD(err, padding->UnPadL(in, out));
       
   149 		if (err != KErrNone)
       
   150 			{
       
   151 			INFO_PRINTF3(_L("The Error returned for block size %d is %d"), aBlockSize,err);
       
   152 			}
       
   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
       
   156 		}
       
   157 	CleanupStack::PopAndDestroy(padding);
       
   158 	}
       
   159 
       
   160  
       
   161 CTestUnpadCorruptPKCS7::CTestUnpadCorruptPKCS7()
       
   162 	{
       
   163 	SetTestStepName(KUnpadCorruptPKCS7);
       
   164 	}
       
   165  
       
   166 CTestUnpadCorruptPKCS7::~CTestUnpadCorruptPKCS7()
       
   167 	{
       
   168 	}
       
   169  
       
   170 TVerdict CTestUnpadCorruptPKCS7::doTestStepL()
       
   171  	{
       
   172  	SetTestStepResult(EPass);
       
   173  	__UHEAP_MARK;
       
   174  
       
   175  	TInt blockSize;
       
   176  	TInt textSize;
       
   177  	TInt paddingNum = 0;
       
   178  	
       
   179  	if (GetIntFromConfig(ConfigSection(), _L("blocksize"), blockSize))
       
   180  		{
       
   181  		if (GetIntFromConfig(ConfigSection(), _L("textsize"), textSize))
       
   182  			{
       
   183  			if (GetIntFromConfig(ConfigSection(), _L("paddingbyte"), paddingNum))
       
   184  				{
       
   185  				INFO_PRINTF1(_L("Test of PKCS7 unpadding with corrupt data"));
       
   186  				TUint8 paddingByte = Min(paddingNum, 255);
       
   187    				TestCorruptPKCS7Unpadding(blockSize, textSize, paddingByte);	  					
       
   188  				}				
       
   189  			else
       
   190  				{
       
   191  				ERR_PRINTF1(_L("Missing parameter - paddingbyte"));
       
   192  				}
       
   193  			}
       
   194  		else
       
   195  			{
       
   196  			ERR_PRINTF1(_L("Missing parameter - textsize"));
       
   197  			}
       
   198  		}
       
   199  	else
       
   200 		{
       
   201  		ERR_PRINTF1(_L("Missing parameter - blocksize"));
       
   202  		}
       
   203  
       
   204  	__UHEAP_MARKEND;
       
   205  	return TestStepResult();
       
   206  	}
       
   207  
       
   208 void CTestUnpadCorruptPKCS7::TestCorruptPKCS7Unpadding(TInt aBlockSize, TInt aTextSize, TUint8 aPaddingByte)
       
   209 	{
       
   210  	CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
       
   211  	TInt paddingBytes = (aBlockSize - aTextSize % aBlockSize);
       
   212  	
       
   213 	HBufC8 *padInData = HBufC8::NewLC(aTextSize + paddingBytes);
       
   214  	HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
       
   215  	TPtr8 in(padInData->Des());
       
   216  	TPtr8 out(padOutData->Des());
       
   217  
       
   218  	GenerateInput(aTextSize, in);
       
   219  
       
   220  	in.SetLength(in.Length() + paddingBytes);
       
   221  	for (TInt j = 1; j <= paddingBytes; j++)
       
   222  		{
       
   223  		in[in.Length()-j] = (TUint8) aPaddingByte;
       
   224  		}
       
   225  
       
   226  	TRAPD(err, padding->UnPadL(in, out));
       
   227  	
       
   228  	if ( err == KErrInvalidPadding )
       
   229 		{
       
   230   		INFO_PRINTF2(_L("The PKCS7 unpadding UnPadL method returned error is %d"), err);
       
   231   		TEST(err == KErrInvalidPadding);
       
   232   		SetTestStepResult(EPass);
       
   233   		}
       
   234 	else if ( err == KErrNone )
       
   235 		{
       
   236 		TEST(err == KErrNone);
       
   237 		}
       
   238 	CleanupStack::PopAndDestroy(3, padding); // padding, padInData, padOutData
       
   239 	}
       
   240 
       
   241 CTestPaddingCorruptPKCS7::CTestPaddingCorruptPKCS7()
       
   242 	{
       
   243 	SetTestStepName(KPaddingCorruptPKCS7);
       
   244 	}
       
   245  
       
   246 CTestPaddingCorruptPKCS7::~CTestPaddingCorruptPKCS7()
       
   247 	{
       
   248 	}
       
   249  
       
   250 TVerdict CTestPaddingCorruptPKCS7::doTestStepL()
       
   251  	{
       
   252  	SetTestStepResult(EPass);
       
   253  	__UHEAP_MARK;
       
   254  
       
   255  	TInt blockSize;
       
   256  	TInt textSize;
       
   257  	 	
       
   258  	if (GetIntFromConfig(ConfigSection(), _L("blocksize"), blockSize))
       
   259  		{
       
   260  		if (GetIntFromConfig(ConfigSection(), _L("textsize"), textSize))
       
   261  			{
       
   262  			INFO_PRINTF1(_L("Test of PKCS7 unpadding with corrupt data"));
       
   263    			TestCorruptPKCS7padding(blockSize, textSize);	  					
       
   264  			}
       
   265  		else
       
   266  			{
       
   267  			ERR_PRINTF1(_L("Missing parameter - textsize"));
       
   268  			}
       
   269  		}
       
   270  	else
       
   271 		{
       
   272  		ERR_PRINTF1(_L("Missing parameter - blocksize"));
       
   273  		}
       
   274  
       
   275  	__UHEAP_MARKEND;
       
   276  	return TestStepResult();
       
   277  	}
       
   278  
       
   279 void CTestPaddingCorruptPKCS7::TestCorruptPKCS7padding(TInt aBlockSize, TInt aTextSize)
       
   280 	{
       
   281 	CPaddingPKCS7 *padding = CPaddingPKCS7::NewLC(aBlockSize);
       
   282 
       
   283 	TInt paddingBytes = 0;
       
   284 	//Divide by 0 is undefined.
       
   285 	if(aBlockSize != 0)
       
   286 		{
       
   287 		paddingBytes = aBlockSize - (aTextSize % aBlockSize);
       
   288 		}
       
   289 		
       
   290  	HBufC8 *padOutData = HBufC8::NewLC(aTextSize + paddingBytes);
       
   291  	HBufC8 *padInData = HBufC8::NewLC(aTextSize);
       
   292  	TPtr8 in(padInData->Des());
       
   293  	TPtr8 out(padOutData->Des());
       
   294  
       
   295  	GenerateInput(aTextSize, in);
       
   296  	TRAPD(err, padding->DoPadL(in, out));
       
   297 
       
   298 	INFO_PRINTF2(_L("The PKCS7 padding DoPadL method returned error is %d"), err);
       
   299 	TInt totalLength = out.Length();
       
   300 	TInt inLength = in.Length();
       
   301 	
       
   302 	TUint paddingLength = 0;
       
   303 	//Divide by 0 is undefined.
       
   304 	if(aBlockSize != 0)
       
   305 		{
       
   306 		paddingLength = aBlockSize - inLength%aBlockSize;
       
   307 		// Test that the total length is a multiple of blockSize
       
   308 		TEST((totalLength % aBlockSize) == 0);
       
   309 		}			
       
   310 	
       
   311  	// Test that the padding bytes are equal in value to the paddingLength,
       
   312 	// ie, if padding length is 5 the 5 last octets in the out array should be 0x05
       
   313 	for (TInt j = paddingLength; j > 0 ; j--)
       
   314 		{
       
   315 		TEST(out[out.Length()-j] == paddingLength);
       
   316 		}
       
   317 
       
   318 	TPtrC expectedContent;
       
   319  	if (GetStringFromConfig(ConfigSection(), _L("case"), expectedContent))
       
   320  		{
       
   321  		if(expectedContent.Compare(_L("InvalidPadding")) ==0)
       
   322  			{
       
   323  			TEST(err == KErrInvalidPadding);
       
   324  			}
       
   325  		else if(expectedContent.Compare(_L("Valid")) ==0)
       
   326  			{
       
   327  			TEST(err == KErrNone);
       
   328  			}
       
   329  		else if(expectedContent.Compare(_L("CorruptBlockSize")) ==0)
       
   330  			{
       
   331  			TEST(err == KErrArgument);
       
   332  			}
       
   333  		}
       
   334  	
       
   335  	// Test that the data has not been corrupted
       
   336 	TEST(in == out.Left(out.Length() - paddingLength));
       
   337  	
       
   338  	CleanupStack::PopAndDestroy(3, padding); // padInData, padOutData, padCompareData, padding
       
   339 	}
       
   340