os/security/crypto/weakcrypto/test/tpadding/tpaddingPKCS1.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 "tpaddingPKCS1.h"
    22 
    23 CTestPadPKCS1::CTestPadPKCS1()
    24 {
    25    SetTestStepName(KPadPKCS1);
    26 }
    27 
    28 CTestPadPKCS1::~CTestPadPKCS1()
    29 {
    30 }
    31 
    32 TVerdict CTestPadPKCS1::doTestStepL()
    33 {
    34 
    35    __UHEAP_MARK;
    36    
    37    INFO_PRINTF1(_L("Test of PKCS1 signature padding"));
    38 
    39    SetTestStepResult(EPass);
    40    INFO_PRINTF1(_L("Blocksize 512"));
    41    TestPKCS1SignaturePadding(512);
    42 
    43    SetTestStepResult(EPass);
    44    INFO_PRINTF1(_L("Blocksize 1024"));
    45    TestPKCS1SignaturePadding(1024);
    46 
    47    SetTestStepResult(EPass);
    48    INFO_PRINTF1(_L("Blocksize 2048"));
    49    TestPKCS1SignaturePadding(2048);
    50 
    51    INFO_PRINTF1(_L("Test of PKCS1 encryption padding"));
    52 
    53    SetTestStepResult(EPass);
    54    INFO_PRINTF1(_L("Blocksize 512"));
    55    TestPKCS1EncryptionPadding(512);
    56 
    57    SetTestStepResult(EPass);
    58    INFO_PRINTF1(_L("Blocksize 1024"));
    59    TestPKCS1EncryptionPadding(1024);
    60 
    61    SetTestStepResult(EPass);
    62    INFO_PRINTF1(_L("Blocksize 2048"));
    63    TestPKCS1EncryptionPadding(2048);
    64    
    65 
    66    __UHEAP_MARKEND;
    67    return TestStepResult();
    68 }
    69 
    70 void CTestPadPKCS1::TestPKCS1SignaturePadding(TInt aBlockSize)
    71 {
    72    CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
    73    /*
    74     * The length of the data D shall not be more than k-11 octets,
    75     * which is positive since the length k of the modulus is at
    76     * least 12 octets.
    77     */
    78    TInt noDataBytes = aBlockSize-11;
    79    
    80    for (TInt i = 0; i <= noDataBytes; i++)
    81    {
    82       HBufC8 *inData = HBufC8::NewLC(noDataBytes);
    83       HBufC8 *outData = HBufC8::NewLC(aBlockSize);
    84       TPtr8 in(inData->Des());
    85       TPtr8 out(outData->Des());
    86       TInt j;
    87       
    88       for (j = 0; j < i; j++)
    89       {
    90 	 TInt text('a'+j%25);
    91 	 in.Append(text);
    92       }
    93       
    94       TRAPD(err, padding->DoPadL(in, out));
    95       TEST(err == KErrNone);
    96       
    97       // check the padding.
    98       // should have the following syntax:
    99       // 00 || 01 || PS || 00 || D where PS is the padding octects of value 0xff
   100       
   101       TEST(out[0] == 0 && out[1] == 1);
   102 
   103       TInt endOfPadding = aBlockSize - 1 - in.Length();
   104       for (j = 2; j < endOfPadding; j++)
   105       {
   106 	 TEST(out[j]==0xff);
   107 	 
   108       }
   109       // Check for the delimiter
   110       TEST(out[endOfPadding] == 0);
   111       // Check that the rest of the data is not corrupted
   112       TInt startOfData = aBlockSize - in.Length();
   113       TEST(in == out.Mid(startOfData));
   114       CleanupStack::PopAndDestroy(2, inData); // inData, outData
   115    }
   116    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   117    CleanupStack::PopAndDestroy(); // padding
   118 }
   119 
   120 void CTestPadPKCS1::TestPKCS1EncryptionPadding(TInt aBlockSize)
   121 {
   122    CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
   123    /*
   124     * The length of the data D shall not be more than k-11 octets,
   125     * which is positive since the length k of the modulus is at
   126     * least 12 octets.
   127     */
   128    TInt noDataBytes = aBlockSize-11;
   129    
   130    for (TInt i = 0; i <= noDataBytes; i++)
   131    {
   132       HBufC8 *inData = HBufC8::NewLC(noDataBytes);
   133       HBufC8 *outData = HBufC8::NewLC(aBlockSize);
   134       TPtr8 in(inData->Des());
   135       TPtr8 out(outData->Des());
   136       TInt j;
   137       
   138       for (j = 0; j < i; j++)
   139       {
   140 	 TInt text('a'+j%25);
   141 	 in.Append(text);
   142       }
   143       
   144       TRAPD(err, padding->DoPadL(in, out));
   145       TEST(err == KErrNone);
   146       // check the padding.
   147       // should have the following syntax:
   148       // 00 || 02 || PS || 00 || D where PS is the padding octects containing random data
   149       
   150       TEST(out[0] == 0 && out[1] == 2);
   151       
   152       TInt endOfPadding = aBlockSize - 1 - in.Length();
   153       for (j = 2; j < endOfPadding; j++)
   154       {
   155 	 TEST(out[j]!=0);
   156 	 
   157       }
   158       // Check for the delimiter
   159       TEST(out[endOfPadding] == 0);
   160       
   161       // Check that the rest of the data is not corrupted
   162       TInt startOfData = aBlockSize - in.Length();
   163       TEST(in == out.Mid(startOfData));
   164       CleanupStack::PopAndDestroy(2, inData); // inData, outData
   165    }
   166    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   167    CleanupStack::PopAndDestroy(); // padding
   168 }
   169 
   170    
   171 CTestUnpadPKCS1::CTestUnpadPKCS1()
   172 {
   173    SetTestStepName(KUnpadPKCS1);
   174 }
   175 
   176 CTestUnpadPKCS1::~CTestUnpadPKCS1()
   177 {
   178 }
   179 
   180 TVerdict CTestUnpadPKCS1::doTestStepL()
   181 {
   182    __UHEAP_MARK;
   183 
   184    INFO_PRINTF1(_L("Test of PKCS1 signature unpadding"));
   185    INFO_PRINTF1(_L("Blocksize 512"));
   186    SetTestStepResult(EPass);
   187    TestPKCS1SignatureUnpadding(512);
   188    INFO_PRINTF1(_L("Blocksize 1024"));
   189    SetTestStepResult(EPass);
   190    TestPKCS1SignatureUnpadding(1024);
   191    INFO_PRINTF1(_L("Blocksize 2048"));
   192    SetTestStepResult(EPass);
   193    TestPKCS1SignatureUnpadding(2048);
   194 
   195    INFO_PRINTF1(_L("Test of PKCS1 encryption unpadding"));
   196    INFO_PRINTF1(_L("Blocksize 512"));
   197    SetTestStepResult(EPass);
   198    TestPKCS1EncryptionUnpadding(512);
   199    INFO_PRINTF1(_L("Blocksize 1024"));
   200    SetTestStepResult(EPass);
   201    TestPKCS1EncryptionUnpadding(1024);
   202    INFO_PRINTF1(_L("Blocksize 2048"));
   203    SetTestStepResult(EPass);
   204    TestPKCS1EncryptionUnpadding(2048);
   205 
   206    __UHEAP_MARKEND;
   207    return TestStepResult();
   208 }
   209 
   210 void CTestUnpadPKCS1::TestPKCS1SignatureUnpadding(TInt aBlockSize)
   211 {
   212    CPaddingPKCS1Signature *padding = CPaddingPKCS1Signature::NewLC(aBlockSize);
   213    /*
   214     * The length of the data D shall not be more than k-11 octets,
   215     * which is positive since the length k of the modulus is at
   216     * least 12 octets.
   217     */
   218    TInt noDataBytes = aBlockSize-11;
   219    
   220    for (TInt i = 0; i <= noDataBytes; i++)
   221    {
   222       HBufC8 *inData = HBufC8::NewLC(aBlockSize);
   223       HBufC8 *outData = HBufC8::NewLC(noDataBytes);
   224       HBufC8 *compData = HBufC8::NewLC(noDataBytes);
   225       TPtr8 in(inData->Des());
   226       TPtr8 out(outData->Des());
   227       TPtr8 comp(compData->Des());
   228       
   229       TInt endOfPadding = aBlockSize - 1 - i;
   230       in.SetLength(endOfPadding+1);
   231       in[0] = 0;
   232       in[1] = 1;
   233       TInt j;
   234       for (j = 2; j < endOfPadding; j++)
   235       {
   236 	 in[j] = 0xff;
   237       }
   238       in[endOfPadding] = 0; // delimiter
   239 
   240       for (j = 0; j < i; j++)
   241       {
   242 	 TInt text('a'+j%25);
   243 	 in.Append(text);
   244 	 comp.Append(text);
   245       }
   246 
   247       TRAPD(err, padding->UnPadL(in, out));
   248       TEST(err == KErrNone);
   249 
   250       TEST(out == comp);
   251 
   252       TEST(out.Length() == i);
   253 
   254       
   255       CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
   256    }
   257    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   258    CleanupStack::PopAndDestroy(); // padding
   259 }
   260 
   261 void CTestUnpadPKCS1::TestPKCS1EncryptionUnpadding(TInt aBlockSize)
   262 {
   263    CPaddingPKCS1Encryption *padding = CPaddingPKCS1Encryption::NewLC(aBlockSize);
   264    /*
   265     * "The length of the data D shall not be more than k-11 octets,
   266     * which is positive since the length k of the modulus is at
   267     * least 12 octets."
   268     */
   269    TInt noDataBytes = aBlockSize-11;
   270    for (TInt i = 0; i <= noDataBytes; i++)
   271    {
   272       HBufC8 *inData = HBufC8::NewLC(aBlockSize);
   273       HBufC8 *outData = HBufC8::NewLC(noDataBytes);
   274       HBufC8 *compData = HBufC8::NewLC(noDataBytes);
   275       TPtr8 in(inData->Des());
   276       TPtr8 out(outData->Des());
   277       TPtr8 comp(compData->Des());
   278       
   279       TInt endOfPadding = aBlockSize - 1 - i;
   280       in.SetLength(endOfPadding+1);
   281       in[0] = 0;
   282       in[1] = 2;
   283       TBuf8<256> rnd(256);
   284       GenerateRandomBytesL(rnd);
   285 
   286       TInt k = 2;
   287       TInt j = 0;
   288       while (k < endOfPadding)
   289       {
   290          if (rnd[j%256])
   291          {
   292             in[k++] = rnd[j%256];
   293          }
   294          
   295          if (++j%256 == 0)
   296          {
   297             GenerateRandomBytesL(rnd);
   298          }
   299       }
   300       in[endOfPadding] = 0; // delimiter
   301 
   302       for (j = 0; j < i; j++)
   303       {
   304 	 TInt text('a'+j%25);
   305 	 in.Append(text);
   306      comp.Append(text);
   307       }
   308 
   309       TRAPD(err, padding->UnPadL(in, out));
   310       TEST(err == KErrNone);
   311 
   312 	  TEST(out == comp);		  
   313 
   314       TEST(out.Length() == i);
   315 
   316       
   317       CleanupStack::PopAndDestroy(3, inData); // inData, outData, compData
   318    }
   319    TestStepResult() ? INFO_PRINTF1(_L("Test failed")) : INFO_PRINTF1(_L("Test passed"));
   320    CleanupStack::PopAndDestroy(); // padding
   321 }