1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/test/tasymmetric/tdsasignfb.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,158 @@
1.4 +/*
1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "tdsasignfb.h"
1.23 +#include "t_input.h"
1.24 +#include "t_output.h"
1.25 +#include <asymmetric.h>
1.26 +#include <random.h>
1.27 +#include <padding.h>
1.28 +#include <bigint.h>
1.29 +#include "tbrokenrandom.h"
1.30 +
1.31 +CTestAction* CDSASignFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.32 + const TTestActionSpec& aTestActionSpec)
1.33 + {
1.34 + CTestAction* self = CDSASignFB::NewLC(aFs, aConsole,
1.35 + aOut, aTestActionSpec);
1.36 + CleanupStack::Pop();
1.37 + return self;
1.38 + }
1.39 +
1.40 +CTestAction* CDSASignFB::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
1.41 + const TTestActionSpec& aTestActionSpec)
1.42 + {
1.43 + CDSASignFB* self = new(ELeave) CDSASignFB(aFs, aConsole, aOut);
1.44 + CleanupStack::PushL(self);
1.45 + self->ConstructL(aTestActionSpec);
1.46 + return self;
1.47 + }
1.48 +
1.49 +CDSASignFB::~CDSASignFB()
1.50 + {
1.51 + delete iBody;
1.52 + delete iSeed;
1.53 + }
1.54 +
1.55 +CDSASignFB::CDSASignFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.56 + : CTestAction(aConsole, aOut), iFs(aFs)
1.57 + {
1.58 + }
1.59 +
1.60 +void CDSASignFB::ConstructL(const TTestActionSpec& aTestActionSpec)
1.61 + {
1.62 + CTestAction::ConstructL(aTestActionSpec);
1.63 + iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
1.64 + iBody->Des().Copy(aTestActionSpec.iActionBody);
1.65 +
1.66 +
1.67 + iSeed = Input::ParseElementHexL(*iBody, _L8("<seed>"));
1.68 + iKeyBits = Input::ParseIntElement(*iBody, _L8("<keybits>"), _L8("</keybits>"));
1.69 + }
1.70 +
1.71 +void CDSASignFB::DoPerformPrerequisite(TRequestStatus& aStatus)
1.72 + {
1.73 + TRequestStatus* status = &aStatus;
1.74 + User::RequestComplete(status, KErrNone);
1.75 + iActionState = CTestAction::EAction;
1.76 + }
1.77 +
1.78 +void CDSASignFB::DoPerformPostrequisite(TRequestStatus& aStatus)
1.79 + {
1.80 + TRequestStatus* status = &aStatus;
1.81 +
1.82 + iFinished = ETrue;
1.83 + User::RequestComplete(status, KErrNone);
1.84 + }
1.85 +
1.86 +void CDSASignFB::DoReportAction(void)
1.87 + {
1.88 + }
1.89 +
1.90 +void CDSASignFB::DoCheckResult(TInt)
1.91 + {
1.92 + if (iResult)
1.93 + iConsole.Printf(_L("."));
1.94 + else
1.95 + iConsole.Printf(_L("X"));
1.96 + }
1.97 +
1.98 +void CDSASignFB::PerformAction(TRequestStatus& aStatus)
1.99 + {
1.100 + __UHEAP_MARK;
1.101 + TRequestStatus* status = &aStatus;
1.102 + iResult = EFalse;
1.103 +
1.104 + CRandomSetSource* rng = new(ELeave)CRandomSetSource(*iSeed);
1.105 + SetThreadRandomLC(rng);
1.106 + CDSAKeyPair* dsaPair = CDSAKeyPair::NewLC(iKeyBits);
1.107 + CDSASigner* signer = CDSASigner::NewLC(dsaPair->PrivateKey());
1.108 + CDSAVerifier* verifier = CDSAVerifier::NewLC(dsaPair->PublicKey());
1.109 +
1.110 + TBuf8<128> message(128);
1.111 + TRandom::RandomL(message);
1.112 +
1.113 + const CDSASignature* signature = signer->SignL(message);
1.114 + CleanupStack::PushL(const_cast<CDSASignature*>(signature));
1.115 +
1.116 + if(verifier->VerifyL(message, *signature))
1.117 + {
1.118 + iResult = ETrue;
1.119 + }
1.120 + else
1.121 + {
1.122 + iOut.writeString(_L("Failure in dsasignfb with following data:\n"));
1.123 + iOut.writeString(_L("input data: "));
1.124 + iOut.writeOctetStringL(message);
1.125 + iOut.writeString(_L("\n"));
1.126 + iOut.writeString(_L("seed: "));
1.127 + iOut.writeOctetStringL(*iSeed);
1.128 + iOut.writeString(_L("\n"));
1.129 + }
1.130 +
1.131 + CleanupStack::PopAndDestroy(const_cast<CDSASignature*>(signature));
1.132 + CleanupStack::PopAndDestroy(verifier);
1.133 + CleanupStack::PopAndDestroy(signer);
1.134 + CleanupStack::PopAndDestroy(dsaPair);
1.135 + CleanupStack::PopAndDestroy(); //SetThreadRandomLC
1.136 +
1.137 + User::RequestComplete(status, KErrNone);
1.138 + iActionState = CTestAction::EPostrequisite;
1.139 + __UHEAP_MARKEND;
1.140 + }
1.141 +
1.142 +void CDSASignFB::Hex(HBufC8& aString)
1.143 + {
1.144 + TPtr8 ptr=aString.Des();
1.145 + if (aString.Length()%2)
1.146 + {
1.147 + ptr.SetLength(0);
1.148 + return;
1.149 + }
1.150 + TInt i;
1.151 + for (i=0;i<aString.Length();i+=2)
1.152 + {
1.153 + TUint8 tmp;
1.154 + tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
1.155 + tmp*=16;
1.156 + tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
1.157 + ptr[i/2]=tmp;
1.158 + }
1.159 + ptr.SetLength(aString.Length()/2);
1.160 + }
1.161 +