crypto/weakcryptospi/test/tasymmetric/tdsasignfb.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 "tdsasignfb.h"
       
    20 #include "t_input.h"
       
    21 #include "t_output.h"
       
    22 #include <asymmetric.h>
       
    23 #include <random.h>
       
    24 #include <padding.h>
       
    25 #include <bigint.h>
       
    26 #include "tbrokenrandom.h"
       
    27 
       
    28 CTestAction* CDSASignFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
       
    29 	const TTestActionSpec& aTestActionSpec)
       
    30 	{
       
    31 	CTestAction* self = CDSASignFB::NewLC(aFs, aConsole,
       
    32 		aOut, aTestActionSpec);
       
    33 	CleanupStack::Pop();
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 CTestAction* CDSASignFB::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
       
    38 	const TTestActionSpec& aTestActionSpec)
       
    39 	{
       
    40 	CDSASignFB* self = new(ELeave) CDSASignFB(aFs, aConsole, aOut);
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL(aTestActionSpec);
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 CDSASignFB::~CDSASignFB()
       
    47 	{
       
    48 	delete iBody;
       
    49 	delete iSeed;
       
    50 	}
       
    51 
       
    52 CDSASignFB::CDSASignFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
       
    53 	: CTestAction(aConsole, aOut), iFs(aFs)
       
    54 	{
       
    55 	}
       
    56 
       
    57 void CDSASignFB::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    58 	{
       
    59 	CTestAction::ConstructL(aTestActionSpec);
       
    60 	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
       
    61 	iBody->Des().Copy(aTestActionSpec.iActionBody);
       
    62 	
       
    63 
       
    64 	iSeed = Input::ParseElementHexL(*iBody, _L8("<seed>"));
       
    65 	iKeyBits = Input::ParseIntElement(*iBody, _L8("<keybits>"), _L8("</keybits>"));
       
    66 	}
       
    67 
       
    68 void CDSASignFB::DoPerformPrerequisite(TRequestStatus& aStatus)
       
    69 	{
       
    70 	TRequestStatus* status = &aStatus;
       
    71 	User::RequestComplete(status, KErrNone);
       
    72 	iActionState = CTestAction::EAction;
       
    73 	}
       
    74 
       
    75 void CDSASignFB::DoPerformPostrequisite(TRequestStatus& aStatus)
       
    76 	{
       
    77 	TRequestStatus* status = &aStatus;
       
    78 
       
    79 	iFinished = ETrue;
       
    80 	User::RequestComplete(status, KErrNone);
       
    81 	}
       
    82 
       
    83 void CDSASignFB::DoReportAction(void)
       
    84 	{
       
    85 	}
       
    86 
       
    87 void CDSASignFB::DoCheckResult(TInt)
       
    88 	{
       
    89 	if (iResult)
       
    90 		iConsole.Printf(_L("."));
       
    91 	else
       
    92 		iConsole.Printf(_L("X"));
       
    93 	}
       
    94 
       
    95 void CDSASignFB::PerformAction(TRequestStatus& aStatus)
       
    96 	{
       
    97 	__UHEAP_MARK;
       
    98 	TRequestStatus* status = &aStatus;
       
    99 	iResult = EFalse;
       
   100 
       
   101 	CRandomSetSource* rng = new(ELeave)CRandomSetSource(*iSeed);
       
   102 	SetThreadRandomLC(rng);
       
   103 	CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
       
   104 	CDSASigner* signer = CDSASigner::NewLC(dsaPair->PrivateKey());
       
   105 	CDSAVerifier* verifier = CDSAVerifier::NewLC(dsaPair->PublicKey());
       
   106 
       
   107 	TBuf8<128> message(128);
       
   108 	TRandom::Random(message);
       
   109 
       
   110 	const CDSASignature* signature = signer->SignL(message);
       
   111 	CleanupStack::PushL(const_cast<CDSASignature*>(signature));
       
   112 
       
   113 	if(verifier->VerifyL(message, *signature))
       
   114 		{
       
   115 		iResult = ETrue;
       
   116 		}
       
   117 	else
       
   118 		{
       
   119 		iOut.writeString(_L("Failure in dsasignfb with following data:\n"));
       
   120 		iOut.writeString(_L("input data: "));
       
   121 		iOut.writeOctetStringL(message);
       
   122 		iOut.writeString(_L("\n"));
       
   123 		iOut.writeString(_L("seed: "));
       
   124 		iOut.writeOctetStringL(*iSeed);
       
   125 		iOut.writeString(_L("\n"));
       
   126 		}
       
   127 
       
   128 	// Check the maximum input lengths.	
       
   129 	if (signer->MaxInputLength() == verifier->MaxInputLength())
       
   130 		{
       
   131 		iResult = ETrue;
       
   132 		}
       
   133 	else
       
   134 		{
       
   135 		iResult = EFalse;
       
   136 		iOut.writeString(_L("\n\t DSA Signer-Verifier Max Input length mismatch. \n\r"));
       
   137 		}
       
   138 	
       
   139 	CleanupStack::PopAndDestroy(const_cast<CDSASignature*>(signature));
       
   140 	CleanupStack::PopAndDestroy(verifier);
       
   141 	CleanupStack::PopAndDestroy(signer);
       
   142 	CleanupStack::PopAndDestroy(dsaPair);
       
   143 	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
       
   144 	
       
   145 	User::RequestComplete(status, KErrNone);
       
   146 	iActionState = CTestAction::EPostrequisite;
       
   147 	__UHEAP_MARKEND;
       
   148 	}
       
   149 
       
   150 void CDSASignFB::Hex(HBufC8& aString)
       
   151     {
       
   152     TPtr8 ptr=aString.Des();
       
   153     if (aString.Length()%2)
       
   154         {
       
   155         ptr.SetLength(0);
       
   156         return;
       
   157         }
       
   158     TInt i;
       
   159     for (i=0;i<aString.Length();i+=2)
       
   160         {
       
   161         TUint8 tmp;
       
   162         tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
       
   163         tmp*=16;
       
   164         tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
       
   165         ptr[i/2]=tmp;
       
   166         }
       
   167     ptr.SetLength(aString.Length()/2);
       
   168     }
       
   169