crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherctrmodeoutoforderstep.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-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 /**
       
    20  @file
       
    21  @internalTechnology
       
    22 */
       
    23  
       
    24 #include "symmetriccipherctrmodeoutoforderstep.h"
       
    25 
       
    26 using namespace CryptoSpi;
       
    27 
       
    28 CSymmetricCipherCtrModeOutOfOrderStep::CSymmetricCipherCtrModeOutOfOrderStep()
       
    29 	{
       
    30 	SetTestStepName(KSymmetricCipherCtrModeOutOfOrderStep);
       
    31 	}
       
    32 
       
    33 
       
    34 TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepPreambleL()
       
    35 	{
       
    36 	SetTestStepResult(EPass);
       
    37 	return TestStepResult();
       
    38 	}
       
    39 
       
    40 
       
    41 TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepL()
       
    42 	{
       
    43 	INFO_PRINTF1(_L("*** Symmetric Cipher - Counter mode out of order operation ***"));
       
    44 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    45   	
       
    46   	if (TestStepResult() != EPass)
       
    47   		{
       
    48   		return TestStepResult();
       
    49   		}
       
    50   	
       
    51   	//Assume failure, unless all is successful
       
    52 	SetTestStepResult(EFail);
       
    53 		
       
    54 	TVariantPtrC operationMode;
       
    55 		
       
    56 	CSymmetricCipher* impl = NULL;
       
    57 	CKey* key = NULL;
       
    58 	SetupCipherL(EFalse, EFalse, operationMode, impl, key);
       
    59 	
       
    60 	INFO_PRINTF1(_L("Plugin loaded."));
       
    61 	
       
    62 	CleanupStack::PushL(key);
       
    63 	CleanupStack::PushL(impl);
       
    64 
       
    65   	
       
    66   	if (TUid(operationMode) != KOperationModeCTRUid)
       
    67   		{
       
    68   		ERR_PRINTF2(_L("*** FAIL: This test supports CTR operation mode only and not mode id: %d ***"), (TUid(operationMode)).iUid);
       
    69   		CleanupStack::PopAndDestroy(2, key);
       
    70   		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    71   		return TestStepResult();
       
    72   		}
       
    73 	
       
    74 	
       
    75 	HBufC8* plaintext = ReadInPlaintextL();
       
    76 	CleanupStack::PushL(plaintext);
       
    77 
       
    78 		
       
    79 	TInt blockSize = CtrModeCalcBlockSizeL(*impl)/8;
       
    80 
       
    81 	if (plaintext->Length() < ((blockSize * 2) + 1))
       
    82 		{
       
    83 		ERR_PRINTF2(_L("*** FAIL: Plaintext argument is not long enough for this test, length(B) = %d ***"), plaintext->Length());
       
    84 		CleanupStack::PopAndDestroy(3, key);
       
    85 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    86 		return TestStepResult();
       
    87 		}
       
    88 				
       
    89   	// This will store our calculated version of the ciphertext to compare with that in the .ini file
       
    90   	TUint8* calculatedCiphertext = new (ELeave) TUint8[plaintext->Length()];
       
    91   	CleanupStack::PushL(calculatedCiphertext);
       
    92   	// Ptr to the first block of the calculated ciphertext
       
    93   	TPtr8 calcCipherPtr1(calculatedCiphertext, blockSize);
       
    94   	// Ptr to the remaining blocks of the calculated ciphertext
       
    95   	TPtr8 calcCipherPtr2((calculatedCiphertext + blockSize), (plaintext->Length() - blockSize));
       
    96   	
       
    97   	// Ptr to the first block of the .ini file's plaintext
       
    98   	TPtrC8 knownPlainPtr1(plaintext->Ptr(), blockSize);
       
    99   	// Ptr to the remaining blocks of the .ini file's plaintext
       
   100   	TPtrC8 knownPlainPtr2((plaintext->Ptr() + blockSize), (plaintext->Length() - blockSize));
       
   101   	
       
   102 	
       
   103 	HBufC8* iv = ReadInIvL();
       
   104 	CleanupStack::PushL(iv);
       
   105 		
       
   106 	// Increment IV to the value for the second block so we can encrypt blocks 2+ first
       
   107 	HBufC8* incrementedIv1 = CtrModeIncrementCounterL((*iv));
       
   108 	CleanupStack::PushL(incrementedIv1);
       
   109 	
       
   110 	impl->SetIvL(*incrementedIv1);
       
   111   	
       
   112   	
       
   113 	INFO_PRINTF1(_L("Setup complete.  Encrypting blocks 2+."));
       
   114 	impl->ProcessL(knownPlainPtr2, calcCipherPtr2);
       
   115 	
       
   116 	INFO_PRINTF1(_L("Blocks 2+ encrypted.  Reseting and encrypting block 1."));
       
   117 	impl->SetIvL((*iv));
       
   118 	impl->ProcessL(knownPlainPtr1, calcCipherPtr1);
       
   119 	
       
   120 	
       
   121 	HBufC8* knownCiphertext = ReadInCiphertextL();
       
   122 	CleanupStack::PushL(knownCiphertext);
       
   123 	
       
   124 	// Check that calculated ciphertext matches the expected value
       
   125 	TPtrC8 wholeCalcCiphertext(calculatedCiphertext, (plaintext->Length()));
       
   126 	if (wholeCalcCiphertext.Compare((*knownCiphertext)) != 0)
       
   127 		{
       
   128 		ERR_PRINTF1(_L("*** FAIL: Calculated ciphertext does not match expected value ***"));
       
   129   		CleanupStack::PopAndDestroy(7, key);
       
   130   		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   131   		return TestStepResult();
       
   132 		}
       
   133 	else
       
   134 		{
       
   135 		INFO_PRINTF1(_L("Calculated ciphertext matches the expected value."));
       
   136 		}
       
   137 	
       
   138 	
       
   139 	// ****  SWITCH TO DECRYPTION NOW  ****
       
   140 	
       
   141 	// This will store our calculated version of the plaintext to compare with that in the .ini file
       
   142   	TUint8* calculatedPlaintext = new (ELeave) TUint8[plaintext->Length()];
       
   143   	CleanupStack::PushL(calculatedPlaintext);
       
   144   	// Ptr to the first block of the calculated plaintext
       
   145   	TPtr8 calcPlainPtr1(calculatedPlaintext, (blockSize * 2));
       
   146   	// Ptr to the remaining blocks of the calculated plaintext
       
   147   	TPtr8 calcPlainPtr2((calculatedPlaintext + (blockSize * 2)), (plaintext->Length() - (blockSize * 2)));
       
   148   	
       
   149   	// Ptr to the first 2 blocks of the ciphertext
       
   150   	TPtrC8 knownCipherPtr1(wholeCalcCiphertext.Ptr(), (blockSize * 2));
       
   151   	// Ptr to the remaining blocks of the ciphertext
       
   152   	TPtrC8 knownCipherPtr2((wholeCalcCiphertext.Ptr() + (blockSize * 2)), (wholeCalcCiphertext.Length() - (blockSize * 2)));
       
   153 	
       
   154 	
       
   155 	//	Increment IV to the value for the third block so we can decrypt blocks 3+ first
       
   156 	HBufC8* incrementedIv2 = CtrModeIncrementCounterL(*incrementedIv1);
       
   157 	CleanupStack::PushL(incrementedIv2);
       
   158 	impl->SetIvL(*incrementedIv2);
       
   159 	
       
   160 	INFO_PRINTF1(_L("Setup complete.  Decrypting blocks 3+."));
       
   161 	impl->ProcessL(knownCipherPtr2, calcPlainPtr2);
       
   162 	
       
   163 	INFO_PRINTF1(_L("Blocks 3+ encrypted.  Reseting and encrypting blocks 1 and 2."));
       
   164 	impl->SetIvL((*iv));
       
   165 	impl->ProcessL(knownCipherPtr1, calcPlainPtr1);	
       
   166 	
       
   167 	TPtrC8 wholeCalcPlaintext(calculatedPlaintext, (plaintext->Length()));
       
   168 	
       
   169 	if (wholeCalcPlaintext.Compare((*plaintext)) != 0)
       
   170 		{
       
   171 		ERR_PRINTF1(_L("*** FAIL: Calculated plaintext does not match expected value ***"));
       
   172 		}
       
   173 	else
       
   174 		{
       
   175 		INFO_PRINTF1(_L("*** PASS: Calculated plaintext matches the original one ***"));
       
   176 		SetTestStepResult(EPass);			
       
   177 		}
       
   178   	
       
   179   	CleanupStack::PopAndDestroy(9, key);
       
   180   	
       
   181 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   182 
       
   183 	return TestStepResult();
       
   184 	}
       
   185 
       
   186 
       
   187 TVerdict CSymmetricCipherCtrModeOutOfOrderStep::doTestStepPostambleL()
       
   188 	{
       
   189 	return TestStepResult();
       
   190 	}