os/security/crypto/weakcrypto/test/tpadding/tpaddingPKCS1.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/test/tpadding/tpaddingPKCS1.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,321 @@
     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 "tpaddingPKCS1.h"
    1.25 +
    1.26 +CTestPadPKCS1::CTestPadPKCS1()
    1.27 +{
    1.28 +   SetTestStepName(KPadPKCS1);
    1.29 +}
    1.30 +
    1.31 +CTestPadPKCS1::~CTestPadPKCS1()
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +TVerdict CTestPadPKCS1::doTestStepL()
    1.36 +{
    1.37 +
    1.38 +   __UHEAP_MARK;
    1.39 +   
    1.40 +   INFO_PRINTF1(_L("Test of PKCS1 signature padding"));
    1.41 +
    1.42 +   SetTestStepResult(EPass);
    1.43 +   INFO_PRINTF1(_L("Blocksize 512"));
    1.44 +   TestPKCS1SignaturePadding(512);
    1.45 +
    1.46 +   SetTestStepResult(EPass);
    1.47 +   INFO_PRINTF1(_L("Blocksize 1024"));
    1.48 +   TestPKCS1SignaturePadding(1024);
    1.49 +
    1.50 +   SetTestStepResult(EPass);
    1.51 +   INFO_PRINTF1(_L("Blocksize 2048"));
    1.52 +   TestPKCS1SignaturePadding(2048);
    1.53 +
    1.54 +   INFO_PRINTF1(_L("Test of PKCS1 encryption padding"));
    1.55 +
    1.56 +   SetTestStepResult(EPass);
    1.57 +   INFO_PRINTF1(_L("Blocksize 512"));
    1.58 +   TestPKCS1EncryptionPadding(512);
    1.59 +
    1.60 +   SetTestStepResult(EPass);
    1.61 +   INFO_PRINTF1(_L("Blocksize 1024"));
    1.62 +   TestPKCS1EncryptionPadding(1024);
    1.63 +
    1.64 +   SetTestStepResult(EPass);
    1.65 +   INFO_PRINTF1(_L("Blocksize 2048"));
    1.66 +   TestPKCS1EncryptionPadding(2048);
    1.67 +   
    1.68 +
    1.69 +   __UHEAP_MARKEND;
    1.70 +   return TestStepResult();
    1.71 +}
    1.72 +
    1.73 +void CTestPadPKCS1::TestPKCS1SignaturePadding(TInt aBlockSize)
    1.74 +{
    1.75 +   CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
    1.76 +   /*
    1.77 +    * The length of the data D shall not be more than k-11 octets,
    1.78 +    * which is positive since the length k of the modulus is at
    1.79 +    * least 12 octets.
    1.80 +    */
    1.81 +   TInt noDataBytes = aBlockSize-11;
    1.82 +   
    1.83 +   for (TInt i = 0; i <= noDataBytes; i++)
    1.84 +   {
    1.85 +      HBufC8 *inData = HBufC8::NewLC(noDataBytes);
    1.86 +      HBufC8 *outData = HBufC8::NewLC(aBlockSize);
    1.87 +      TPtr8 in(inData->Des());
    1.88 +      TPtr8 out(outData->Des());
    1.89 +      TInt j;
    1.90 +      
    1.91 +      for (j = 0; j < i; j++)
    1.92 +      {
    1.93 +	 TInt text('a'+j%25);
    1.94 +	 in.Append(text);
    1.95 +      }
    1.96 +      
    1.97 +      TRAPD(err, padding->DoPadL(in, out));
    1.98 +      TEST(err == KErrNone);
    1.99 +      
   1.100 +      // check the padding.
   1.101 +      // should have the following syntax:
   1.102 +      // 00 || 01 || PS || 00 || D where PS is the padding octects of value 0xff
   1.103 +      
   1.104 +      TEST(out[0] == 0 && out[1] == 1);
   1.105 +
   1.106 +      TInt endOfPadding = aBlockSize - 1 - in.Length();
   1.107 +      for (j = 2; j < endOfPadding; j++)
   1.108 +      {
   1.109 +	 TEST(out[j]==0xff);
   1.110 +	 
   1.111 +      }
   1.112 +      // Check for the delimiter
   1.113 +      TEST(out[endOfPadding] == 0);
   1.114 +      // Check that the rest of the data is not corrupted
   1.115 +      TInt startOfData = aBlockSize - in.Length();
   1.116 +      TEST(in == out.Mid(startOfData));
   1.117 +      CleanupStack::PopAndDestroy(2, inData); // inData, outData
   1.118 +   }
   1.119 +   TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   1.120 +   CleanupStack::PopAndDestroy(); // padding
   1.121 +}
   1.122 +
   1.123 +void CTestPadPKCS1::TestPKCS1EncryptionPadding(TInt aBlockSize)
   1.124 +{
   1.125 +   CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
   1.126 +   /*
   1.127 +    * The length of the data D shall not be more than k-11 octets,
   1.128 +    * which is positive since the length k of the modulus is at
   1.129 +    * least 12 octets.
   1.130 +    */
   1.131 +   TInt noDataBytes = aBlockSize-11;
   1.132 +   
   1.133 +   for (TInt i = 0; i <= noDataBytes; i++)
   1.134 +   {
   1.135 +      HBufC8 *inData = HBufC8::NewLC(noDataBytes);
   1.136 +      HBufC8 *outData = HBufC8::NewLC(aBlockSize);
   1.137 +      TPtr8 in(inData->Des());
   1.138 +      TPtr8 out(outData->Des());
   1.139 +      TInt j;
   1.140 +      
   1.141 +      for (j = 0; j < i; j++)
   1.142 +      {
   1.143 +	 TInt text('a'+j%25);
   1.144 +	 in.Append(text);
   1.145 +      }
   1.146 +      
   1.147 +      TRAPD(err, padding->DoPadL(in, out));
   1.148 +      TEST(err == KErrNone);
   1.149 +      // check the padding.
   1.150 +      // should have the following syntax:
   1.151 +      // 00 || 02 || PS || 00 || D where PS is the padding octects containing random data
   1.152 +      
   1.153 +      TEST(out[0] == 0 && out[1] == 2);
   1.154 +      
   1.155 +      TInt endOfPadding = aBlockSize - 1 - in.Length();
   1.156 +      for (j = 2; j < endOfPadding; j++)
   1.157 +      {
   1.158 +	 TEST(out[j]!=0);
   1.159 +	 
   1.160 +      }
   1.161 +      // Check for the delimiter
   1.162 +      TEST(out[endOfPadding] == 0);
   1.163 +      
   1.164 +      // Check that the rest of the data is not corrupted
   1.165 +      TInt startOfData = aBlockSize - in.Length();
   1.166 +      TEST(in == out.Mid(startOfData));
   1.167 +      CleanupStack::PopAndDestroy(2, inData); // inData, outData
   1.168 +   }
   1.169 +   TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   1.170 +   CleanupStack::PopAndDestroy(); // padding
   1.171 +}
   1.172 +
   1.173 +   
   1.174 +CTestUnpadPKCS1::CTestUnpadPKCS1()
   1.175 +{
   1.176 +   SetTestStepName(KUnpadPKCS1);
   1.177 +}
   1.178 +
   1.179 +CTestUnpadPKCS1::~CTestUnpadPKCS1()
   1.180 +{
   1.181 +}
   1.182 +
   1.183 +TVerdict CTestUnpadPKCS1::doTestStepL()
   1.184 +{
   1.185 +   __UHEAP_MARK;
   1.186 +
   1.187 +   INFO_PRINTF1(_L("Test of PKCS1 signature unpadding"));
   1.188 +   INFO_PRINTF1(_L("Blocksize 512"));
   1.189 +   SetTestStepResult(EPass);
   1.190 +   TestPKCS1SignatureUnpadding(512);
   1.191 +   INFO_PRINTF1(_L("Blocksize 1024"));
   1.192 +   SetTestStepResult(EPass);
   1.193 +   TestPKCS1SignatureUnpadding(1024);
   1.194 +   INFO_PRINTF1(_L("Blocksize 2048"));
   1.195 +   SetTestStepResult(EPass);
   1.196 +   TestPKCS1SignatureUnpadding(2048);
   1.197 +
   1.198 +   INFO_PRINTF1(_L("Test of PKCS1 encryption unpadding"));
   1.199 +   INFO_PRINTF1(_L("Blocksize 512"));
   1.200 +   SetTestStepResult(EPass);
   1.201 +   TestPKCS1EncryptionUnpadding(512);
   1.202 +   INFO_PRINTF1(_L("Blocksize 1024"));
   1.203 +   SetTestStepResult(EPass);
   1.204 +   TestPKCS1EncryptionUnpadding(1024);
   1.205 +   INFO_PRINTF1(_L("Blocksize 2048"));
   1.206 +   SetTestStepResult(EPass);
   1.207 +   TestPKCS1EncryptionUnpadding(2048);
   1.208 +
   1.209 +   __UHEAP_MARKEND;
   1.210 +   return TestStepResult();
   1.211 +}
   1.212 +
   1.213 +void CTestUnpadPKCS1::TestPKCS1SignatureUnpadding(TInt aBlockSize)
   1.214 +{
   1.215 +   CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
   1.216 +   /*
   1.217 +    * The length of the data D shall not be more than k-11 octets,
   1.218 +    * which is positive since the length k of the modulus is at
   1.219 +    * least 12 octets.
   1.220 +    */
   1.221 +   TInt noDataBytes = aBlockSize-11;
   1.222 +   
   1.223 +   for (TInt i = 0; i <= noDataBytes; i++)
   1.224 +   {
   1.225 +      HBufC8 *inData = HBufC8::NewLC(aBlockSize);
   1.226 +      HBufC8 *outData = HBufC8::NewLC(noDataBytes);
   1.227 +      HBufC8 *compData = HBufC8::NewLC(noDataBytes);
   1.228 +      TPtr8 in(inData->Des());
   1.229 +      TPtr8 out(outData->Des());
   1.230 +      TPtr8 comp(compData->Des());
   1.231 +      
   1.232 +      TInt endOfPadding = aBlockSize - 1 - i;
   1.233 +      in.SetLength(endOfPadding+1);
   1.234 +      in[0] = 0;
   1.235 +      in[1] = 1;
   1.236 +      TInt j;
   1.237 +      for (j = 2; j < endOfPadding; j++)
   1.238 +      {
   1.239 +	 in[j] = 0xff;
   1.240 +      }
   1.241 +      in[endOfPadding] = 0; // delimiter
   1.242 +
   1.243 +      for (j = 0; j < i; j++)
   1.244 +      {
   1.245 +	 TInt text('a'+j%25);
   1.246 +	 in.Append(text);
   1.247 +	 comp.Append(text);
   1.248 +      }
   1.249 +
   1.250 +      TRAPD(err, padding->UnPadL(in, out));
   1.251 +      TEST(err == KErrNone);
   1.252 +
   1.253 +      TEST(out == comp);
   1.254 +
   1.255 +      TEST(out.Length() == i);
   1.256 +
   1.257 +      
   1.258 +      CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
   1.259 +   }
   1.260 +   TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   1.261 +   CleanupStack::PopAndDestroy(); // padding
   1.262 +}
   1.263 +
   1.264 +void CTestUnpadPKCS1::TestPKCS1EncryptionUnpadding(TInt aBlockSize)
   1.265 +{
   1.266 +   CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
   1.267 +   /*
   1.268 +    * "The length of the data D shall not be more than k-11 octets,
   1.269 +    * which is positive since the length k of the modulus is at
   1.270 +    * least 12 octets."
   1.271 +    */
   1.272 +   TInt noDataBytes = aBlockSize-11;
   1.273 +   for (TInt i = 0; i <= noDataBytes; i++)
   1.274 +   {
   1.275 +      HBufC8 *inData = HBufC8::NewLC(aBlockSize);
   1.276 +      HBufC8 *outData = HBufC8::NewLC(noDataBytes);
   1.277 +      HBufC8 *compData = HBufC8::NewLC(noDataBytes);
   1.278 +      TPtr8 in(inData->Des());
   1.279 +      TPtr8 out(outData->Des());
   1.280 +      TPtr8 comp(compData->Des());
   1.281 +      
   1.282 +      TInt endOfPadding = aBlockSize - 1 - i;
   1.283 +      in.SetLength(endOfPadding+1);
   1.284 +      in[0] = 0;
   1.285 +      in[1] = 2;
   1.286 +      TBuf8<256> rnd(256);
   1.287 +      GenerateRandomBytesL(rnd);
   1.288 +
   1.289 +      TInt k = 2;
   1.290 +      TInt j = 0;
   1.291 +      while (k < endOfPadding)
   1.292 +      {
   1.293 +         if (rnd[j%256])
   1.294 +         {
   1.295 +            in[k++] = rnd[j%256];
   1.296 +         }
   1.297 +         
   1.298 +         if (++j%256 == 0)
   1.299 +         {
   1.300 +            GenerateRandomBytesL(rnd);
   1.301 +         }
   1.302 +      }
   1.303 +      in[endOfPadding] = 0; // delimiter
   1.304 +
   1.305 +      for (j = 0; j < i; j++)
   1.306 +      {
   1.307 +	 TInt text('a'+j%25);
   1.308 +	 in.Append(text);
   1.309 +     comp.Append(text);
   1.310 +      }
   1.311 +
   1.312 +      TRAPD(err, padding->UnPadL(in, out));
   1.313 +      TEST(err == KErrNone);
   1.314 +
   1.315 +	  TEST(out == comp);		  
   1.316 +
   1.317 +      TEST(out.Length() == i);
   1.318 +
   1.319 +      
   1.320 +      CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
   1.321 +   }
   1.322 +   TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   1.323 +   CleanupStack::PopAndDestroy(); // padding
   1.324 +}