os/security/crypto/weakcryptospi/test/tpadding/tpaddingPKCS1.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 "tpaddingPKCS1.h"
    23 
    24 CTestPadPKCS1::CTestPadPKCS1()
    25 {
    26    SetTestStepName(KPadPKCS1);
    27 }
    28 
    29 CTestPadPKCS1::~CTestPadPKCS1()
    30 {
    31 }
    32 
    33 TVerdict CTestPadPKCS1::doTestStepL()
    34 {
    35 
    36    __UHEAP_MARK;
    37    
    38    INFO_PRINTF1(_L("Test of PKCS1 signature padding"));
    39 
    40    SetTestStepResult(EPass);
    41    INFO_PRINTF1(_L("Blocksize 512"));
    42    TestPKCS1SignaturePadding(512);
    43 
    44    SetTestStepResult(EPass);
    45    INFO_PRINTF1(_L("Blocksize 1024"));
    46    TestPKCS1SignaturePadding(1024);
    47 
    48    SetTestStepResult(EPass);
    49    INFO_PRINTF1(_L("Blocksize 2048"));
    50    TestPKCS1SignaturePadding(2048);
    51 
    52    INFO_PRINTF1(_L("Test of PKCS1 encryption padding"));
    53 
    54    SetTestStepResult(EPass);
    55    INFO_PRINTF1(_L("Blocksize 512"));
    56    TestPKCS1EncryptionPadding(512);
    57 
    58    SetTestStepResult(EPass);
    59    INFO_PRINTF1(_L("Blocksize 1024"));
    60    TestPKCS1EncryptionPadding(1024);
    61 
    62    SetTestStepResult(EPass);
    63    INFO_PRINTF1(_L("Blocksize 2048"));
    64    TestPKCS1EncryptionPadding(2048);
    65    
    66 
    67    __UHEAP_MARKEND;
    68    return TestStepResult();
    69 }
    70 
    71 void CTestPadPKCS1::TestPKCS1SignaturePadding(TInt aBlockSize)
    72 {
    73    CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
    74    /*
    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
    77     * least 12 octets.
    78     */
    79    TInt noDataBytes = aBlockSize-11;
    80    
    81    for (TInt i = 0; i <= noDataBytes; i++)
    82    {
    83       HBufC8 *inData = HBufC8::NewLC(noDataBytes);
    84       HBufC8 *outData = HBufC8::NewLC(aBlockSize);
    85       TPtr8 in(inData->Des());
    86       TPtr8 out(outData->Des());
    87       TInt j;
    88       
    89       for (j = 0; j < i; j++)
    90       {
    91 	 TInt text('a'+j%25);
    92 	 in.Append(text);
    93       }
    94       
    95       TRAPD(err, padding->PadL(in, out));
    96       TEST(err == KErrNone);
    97       
    98       // check the padding.
    99       // should have the following syntax:
   100       // 00 || 01 || PS || 00 || D where PS is the padding octects of value 0xff
   101       
   102       TEST(out[0] == 0 && out[1] == 1);
   103 
   104       TInt endOfPadding = aBlockSize - 1 - in.Length();
   105       for (j = 2; j < endOfPadding; j++)
   106       {
   107 	 TEST(out[j]==0xff);
   108 	 
   109       }
   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
   116    }
   117    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   118    CleanupStack::PopAndDestroy(); // padding
   119 }
   120 
   121 void CTestPadPKCS1::TestPKCS1EncryptionPadding(TInt aBlockSize)
   122 {
   123    CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
   124    /*
   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
   127     * least 12 octets.
   128     */
   129    TInt noDataBytes = aBlockSize-11;
   130    
   131    for (TInt i = 0; i <= noDataBytes; i++)
   132    {
   133       HBufC8 *inData = HBufC8::NewLC(noDataBytes);
   134       HBufC8 *outData = HBufC8::NewLC(aBlockSize);
   135       TPtr8 in(inData->Des());
   136       TPtr8 out(outData->Des());
   137       TInt j;
   138       
   139       for (j = 0; j < i; j++)
   140       {
   141 	 TInt text('a'+j%25);
   142 	 in.Append(text);
   143       }
   144       
   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
   150       
   151       TEST(out[0] == 0 && out[1] == 2);
   152       
   153       TInt endOfPadding = aBlockSize - 1 - in.Length();
   154       for (j = 2; j < endOfPadding; j++)
   155       {
   156 	 TEST(out[j]!=0);
   157 	 
   158       }
   159       // Check for the delimiter
   160       TEST(out[endOfPadding] == 0);
   161       
   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
   166    }
   167    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   168    CleanupStack::PopAndDestroy(); // padding
   169 }
   170 
   171    
   172 CTestUnpadPKCS1::CTestUnpadPKCS1()
   173 {
   174    SetTestStepName(KUnpadPKCS1);
   175 }
   176 
   177 CTestUnpadPKCS1::~CTestUnpadPKCS1()
   178 {
   179 }
   180 
   181 TVerdict CTestUnpadPKCS1::doTestStepL()
   182 {
   183    __UHEAP_MARK;
   184 
   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);
   195 
   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);
   206 
   207    __UHEAP_MARKEND;
   208    return TestStepResult();
   209 }
   210 
   211 void CTestUnpadPKCS1::TestPKCS1SignatureUnpadding(TInt aBlockSize)
   212 {
   213    CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
   214    /*
   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
   217     * least 12 octets.
   218     */
   219    TInt noDataBytes = aBlockSize-11;
   220    
   221    for (TInt i = 0; i <= noDataBytes; i++)
   222    {
   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());
   229       
   230       TInt endOfPadding = aBlockSize - 1 - i;
   231       in.SetLength(endOfPadding+1);
   232       in[0] = 0;
   233       in[1] = 1;
   234       TInt j;
   235       for (j = 2; j < endOfPadding; j++)
   236       {
   237 	 in[j] = 0xff;
   238       }
   239       in[endOfPadding] = 0; // delimiter
   240 
   241       for (j = 0; j < i; j++)
   242       {
   243 	 TInt text('a'+j%25);
   244 	 in.Append(text);
   245 	 comp.Append(text);
   246       }
   247 
   248       TRAPD(err, padding->UnPadL(in, out));
   249       TEST(err == KErrNone);
   250 
   251       TEST(out == comp);
   252 
   253       TEST(out.Length() == i);
   254 
   255       
   256       CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
   257    }
   258    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   259    CleanupStack::PopAndDestroy(); // padding
   260 }
   261 
   262 void CTestUnpadPKCS1::TestPKCS1EncryptionUnpadding(TInt aBlockSize)
   263 {
   264    CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
   265    /*
   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
   268     * least 12 octets."
   269     */
   270    TInt noDataBytes = aBlockSize-11;
   271    for (TInt i = 0; i <= noDataBytes; i++)
   272    {
   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());
   279       
   280       TInt endOfPadding = aBlockSize - 1 - i;
   281       in.SetLength(endOfPadding+1);
   282       in[0] = 0;
   283       in[1] = 2;
   284       TBuf8<256> rnd(256);
   285       TRAPD(err, GenerateRandomBytesL(rnd));
   286       if((err != KErrNone) && (err != KErrNotSecure))
   287           User::Leave(err);
   288 
   289 
   290       TInt k = 2;
   291       TInt j = 0;
   292       while (k < endOfPadding)
   293       {
   294          if (rnd[j%256])
   295          {
   296             in[k++] = rnd[j%256];
   297          }
   298          
   299          if (++j%256 == 0)
   300          {
   301          TRAP(err, GenerateRandomBytesL(rnd));
   302          if((err != KErrNone) && (err != KErrNotSecure))
   303              User::Leave(err);
   304          }
   305       }
   306       in[endOfPadding] = 0; // delimiter
   307 
   308       for (j = 0; j < i; j++)
   309       {
   310 	 TInt text('a'+j%25);
   311 	 in.Append(text);
   312      comp.Append(text);
   313       }
   314 
   315       TRAP(err, padding->UnPadL(in, out));
   316       TEST(err == KErrNone);
   317 
   318 	  TEST(out == comp);		  
   319 
   320       TEST(out.Length() == i);
   321 
   322       
   323       CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
   324    }
   325    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   326    CleanupStack::PopAndDestroy(); // padding
   327 }