os/security/crypto/weakcrypto/test/tasymmetric/tdsavector.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) 1998-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 "tdsavector.h"
    20 #include "tvectorutils.h"
    21 #include "t_input.h"
    22 #include <bigint.h>
    23 #include "tbrokenrandom.h"
    24 
    25 ////////////////////////////////////////////////////////////////////////////////
    26 // CDSASignVector
    27 ////////////////////////////////////////////////////////////////////////////////
    28 
    29 CTestAction* CDSASignVector::NewL(RFs& aFs,
    30                                   CConsoleBase& aConsole,
    31                                   Output& aOut, 
    32                                   const TTestActionSpec& aTestActionSpec)
    33 	{
    34 	CTestAction* self = CDSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
    35 	CleanupStack::Pop();
    36 	return self;
    37 	}
    38 
    39 CTestAction* CDSASignVector::NewLC(RFs& aFs,
    40                                    CConsoleBase& aConsole,
    41                                    Output& aOut, 
    42                                    const TTestActionSpec& aTestActionSpec)
    43 	{
    44 	CDSASignVector* self = new(ELeave) CDSASignVector(aFs, aConsole, aOut);
    45 	CleanupStack::PushL(self);
    46 	self->ConstructL(aTestActionSpec);
    47 	return self;
    48 	}
    49 
    50 CDSASignVector::~CDSASignVector()
    51 	{
    52     delete iPrivKey;
    53 	delete iSignature;
    54 	delete iK;
    55 	delete iSigInput;
    56 	}
    57 
    58 CDSASignVector::CDSASignVector(RFs& /*aFs*/, 
    59                                CConsoleBase& aConsole,
    60                                Output& aOut)					 
    61     : CVectorTest(aConsole, aOut)
    62 	{
    63 	}
    64 
    65 void CDSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
    66 	{
    67 	CVectorTest::ConstructL(aTestActionSpec);
    68 
    69     iPrivKey = VectorUtils::ReadDSAPrivateKeyL(aTestActionSpec.iActionBody);
    70 
    71 	iMessage.Set(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
    72 	iK = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<k>"));
    73 
    74 	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
    75 	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
    76 	iSigInput->Update(iMessage);
    77 	}
    78 
    79 void CDSASignVector::DoPerformActionL()
    80 	{
    81 	__UHEAP_MARK;
    82 
    83 	CRandomSetSource* random = new(ELeave)CRandomSetSource(*iK);
    84 	SetThreadRandomLC(random);
    85 
    86     CDSASigner* signer = CDSASigner::NewLC(*iPrivKey);
    87 	const CDSASignature* testSig = signer->SignL(iSigInput->Final());
    88     iResult = (*testSig == *iSignature);
    89 
    90 	delete testSig;
    91     CleanupStack::PopAndDestroy(signer);
    92 	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
    93 
    94 	__UHEAP_MARKEND;
    95 	}
    96 
    97 void CDSASignVector::DoPerformanceTestActionL()
    98 	{
    99 	iResult = ETrue;
   100 	}
   101 
   102 void CDSASignVector::DoCheckResult(TInt /*aError*/)
   103 	{
   104 //	If using Keith's fixed up testframework for testing failures, iResult will 
   105 //	have already been corrected for a deliberate fail result.
   106 //	If not using Keith's testframework iResult is still a fail result at this
   107 //	point, so now's the time to adjust for deliberate failure.
   108 
   109 	if (!iResult)
   110 		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
   111 	
   112 	if( iResult == EFalse )
   113 		{
   114 		iConsole.Printf(_L("X"));
   115 		}
   116 	else 
   117 		{
   118 		iConsole.Printf(_L("."));
   119 		}
   120 	}
   121 
   122 ////////////////////////////////////////////////////////////////////////////////
   123 // CDSAVerifyVector
   124 ////////////////////////////////////////////////////////////////////////////////
   125 
   126 CTestAction* CDSAVerifyVector::NewL(RFs& aFs,
   127                                   CConsoleBase& aConsole,
   128                                   Output& aOut, 
   129                                   const TTestActionSpec& aTestActionSpec)
   130 	{
   131 	CTestAction* self = CDSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   132 	CleanupStack::Pop();
   133 	return self;
   134 	}
   135 
   136 CTestAction* CDSAVerifyVector::NewLC(RFs& aFs,
   137                                    CConsoleBase& aConsole,
   138                                    Output& aOut, 
   139                                    const TTestActionSpec& aTestActionSpec)
   140 	{
   141 	CDSAVerifyVector* self = new(ELeave) CDSAVerifyVector(aFs, aConsole, aOut);
   142 	CleanupStack::PushL(self);
   143 	self->ConstructL(aTestActionSpec);
   144 	return self;
   145 	}
   146 
   147 CDSAVerifyVector::~CDSAVerifyVector()
   148 	{
   149     delete iPubKey;
   150 	delete iSignature;
   151 	delete iMessage;
   152 	delete iSigInput;
   153 	}
   154 
   155 CDSAVerifyVector::CDSAVerifyVector(RFs& /*aFs*/, 
   156                                    CConsoleBase& aConsole,
   157                                    Output& aOut)					 
   158     : CVectorTest(aConsole, aOut)
   159 	{
   160 	}
   161 
   162 void CDSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
   163 	{
   164 	CVectorTest::ConstructL(aTestActionSpec);
   165 
   166     iPubKey = VectorUtils::ReadDSAPublicKeyL(aTestActionSpec.iActionBody);
   167 
   168 	TPtrC8 message(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
   169 	if (message.Length()==0)	
   170 		iMessage = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<hexm>"));
   171 	else
   172 		iMessage = message.AllocL();
   173 		
   174 	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
   175 	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
   176 	iSigInput->Update(*iMessage);
   177 	}	
   178 
   179 void CDSAVerifyVector::DoPerformActionL()
   180 	{
   181 	__UHEAP_MARK;
   182 
   183     CDSAVerifier* verifier = CDSAVerifier::NewLC(*iPubKey);
   184     iResult = verifier->VerifyL(iSigInput->Final(), *iSignature);
   185 
   186     CleanupStack::PopAndDestroy(verifier);
   187 	__UHEAP_MARKEND;
   188 	}
   189 
   190 void CDSAVerifyVector::DoPerformanceTestActionL()
   191 	{
   192 	iResult = ETrue;
   193 	}
   194 
   195 void CDSAVerifyVector::DoCheckResult(TInt /*aError*/)
   196 	{
   197 //	If using Keith's fixed up testframework for testing failures, iResult will 
   198 //	have already been corrected for a deliberate fail result.
   199 //	If not using Keith's testframework iResult is still a fail result at this
   200 //	point, so now's the time to adjust for deliberate failure.
   201 
   202 	if (!iResult)
   203 		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
   204 	
   205 	if( iResult == EFalse )
   206 		{
   207 		iConsole.Printf(_L("X"));
   208 		}
   209 	else 
   210 		{
   211 		iConsole.Printf(_L("."));
   212 		}
   213 	}