1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/tdsavector.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,221 @@
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 "tdsavector.h"
1.23 +#include "tvectorutils.h"
1.24 +#include "t_input.h"
1.25 +#include <bigint.h>
1.26 +#include "tbrokenrandom.h"
1.27 +
1.28 +const TInt KSha1HashLength = 20;
1.29 +////////////////////////////////////////////////////////////////////////////////
1.30 +// CDSASignVector
1.31 +////////////////////////////////////////////////////////////////////////////////
1.32 +
1.33 +CTestAction* CDSASignVector::NewL(RFs& aFs,
1.34 + CConsoleBase& aConsole,
1.35 + Output& aOut,
1.36 + const TTestActionSpec& aTestActionSpec)
1.37 + {
1.38 + CTestAction* self = CDSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.39 + CleanupStack::Pop();
1.40 + return self;
1.41 + }
1.42 +
1.43 +CTestAction* CDSASignVector::NewLC(RFs& aFs,
1.44 + CConsoleBase& aConsole,
1.45 + Output& aOut,
1.46 + const TTestActionSpec& aTestActionSpec)
1.47 + {
1.48 + CDSASignVector* self = new(ELeave) CDSASignVector(aFs, aConsole, aOut);
1.49 + CleanupStack::PushL(self);
1.50 + self->ConstructL(aTestActionSpec);
1.51 + return self;
1.52 + }
1.53 +
1.54 +CDSASignVector::~CDSASignVector()
1.55 + {
1.56 + delete iPrivKey;
1.57 + delete iSignature;
1.58 + delete iK;
1.59 + delete iSigInput;
1.60 + }
1.61 +
1.62 +CDSASignVector::CDSASignVector(RFs& /*aFs*/,
1.63 + CConsoleBase& aConsole,
1.64 + Output& aOut)
1.65 + : CVectorTest(aConsole, aOut)
1.66 + {
1.67 + }
1.68 +
1.69 +void CDSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.70 + {
1.71 + CVectorTest::ConstructL(aTestActionSpec);
1.72 +
1.73 + iPrivKey = VectorUtils::ReadDSAPrivateKeyL(aTestActionSpec.iActionBody);
1.74 +
1.75 + iMessage.Set(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
1.76 + iK = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<k>"));
1.77 +
1.78 + iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
1.79 + iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
1.80 + iSigInput->Update(iMessage);
1.81 + }
1.82 +
1.83 +void CDSASignVector::DoPerformActionL()
1.84 + {
1.85 + __UHEAP_MARK;
1.86 +
1.87 + CRandomSetSource* random = new(ELeave)CRandomSetSource(*iK);
1.88 + SetThreadRandomLC(random);
1.89 +
1.90 + CDSASigner* signer = CDSASigner::NewLC(*iPrivKey);
1.91 + const CDSASignature* testSig = signer->SignL(iSigInput->Final());
1.92 + iResult = (*testSig == *iSignature);
1.93 + delete testSig;
1.94 + CleanupStack::PopAndDestroy(signer);
1.95 +
1.96 + CDSASigner* signer1 = CDSASigner::NewL(*iPrivKey);
1.97 + CleanupStack::PushL(signer1);
1.98 + if (signer1->MaxInputLength() != KSha1HashLength)
1.99 + {
1.100 + iResult = EFalse;
1.101 + }
1.102 + CleanupStack::PopAndDestroy(signer1);
1.103 + CleanupStack::PopAndDestroy(); //SetThreadRandomLC
1.104 +
1.105 + __UHEAP_MARKEND;
1.106 + }
1.107 +
1.108 +void CDSASignVector::DoPerformanceTestActionL()
1.109 + {
1.110 + iResult = ETrue;
1.111 + }
1.112 +
1.113 +void CDSASignVector::DoCheckResult(TInt /*aError*/)
1.114 + {
1.115 +// If using Keith's fixed up testframework for testing failures, iResult will
1.116 +// have already been corrected for a deliberate fail result.
1.117 +// If not using Keith's testframework iResult is still a fail result at this
1.118 +// point, so now's the time to adjust for deliberate failure.
1.119 +
1.120 + if (!iResult)
1.121 + iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
1.122 +
1.123 + if( iResult == EFalse )
1.124 + {
1.125 + iConsole.Printf(_L("X"));
1.126 + }
1.127 + else
1.128 + {
1.129 + iConsole.Printf(_L("."));
1.130 + }
1.131 + }
1.132 +
1.133 +////////////////////////////////////////////////////////////////////////////////
1.134 +// CDSAVerifyVector
1.135 +////////////////////////////////////////////////////////////////////////////////
1.136 +
1.137 +CTestAction* CDSAVerifyVector::NewL(RFs& aFs,
1.138 + CConsoleBase& aConsole,
1.139 + Output& aOut,
1.140 + const TTestActionSpec& aTestActionSpec)
1.141 + {
1.142 + CTestAction* self = CDSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.143 + CleanupStack::Pop();
1.144 + return self;
1.145 + }
1.146 +
1.147 +CTestAction* CDSAVerifyVector::NewLC(RFs& aFs,
1.148 + CConsoleBase& aConsole,
1.149 + Output& aOut,
1.150 + const TTestActionSpec& aTestActionSpec)
1.151 + {
1.152 + CDSAVerifyVector* self = new(ELeave) CDSAVerifyVector(aFs, aConsole, aOut);
1.153 + CleanupStack::PushL(self);
1.154 + self->ConstructL(aTestActionSpec);
1.155 + return self;
1.156 + }
1.157 +
1.158 +CDSAVerifyVector::~CDSAVerifyVector()
1.159 + {
1.160 + delete iPubKey;
1.161 + delete iSignature;
1.162 + delete iMessage;
1.163 + delete iSigInput;
1.164 + }
1.165 +
1.166 +CDSAVerifyVector::CDSAVerifyVector(RFs& /*aFs*/,
1.167 + CConsoleBase& aConsole,
1.168 + Output& aOut)
1.169 + : CVectorTest(aConsole, aOut)
1.170 + {
1.171 + }
1.172 +
1.173 +void CDSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
1.174 + {
1.175 + CVectorTest::ConstructL(aTestActionSpec);
1.176 +
1.177 + iPubKey = VectorUtils::ReadDSAPublicKeyL(aTestActionSpec.iActionBody);
1.178 +
1.179 + TPtrC8 message(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
1.180 + if (message.Length()==0)
1.181 + iMessage = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<hexm>"));
1.182 + else
1.183 + iMessage = message.AllocL();
1.184 +
1.185 + iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
1.186 + iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
1.187 + iSigInput->Update(*iMessage);
1.188 + }
1.189 +
1.190 +void CDSAVerifyVector::DoPerformActionL()
1.191 + {
1.192 + __UHEAP_MARK;
1.193 +
1.194 + CDSAVerifier* verifier = CDSAVerifier::NewLC(*iPubKey);
1.195 + iResult = verifier->VerifyL(iSigInput->Final(), *iSignature);
1.196 +
1.197 + CleanupStack::PopAndDestroy(verifier);
1.198 + __UHEAP_MARKEND;
1.199 + }
1.200 +
1.201 +void CDSAVerifyVector::DoPerformanceTestActionL()
1.202 + {
1.203 + iResult = ETrue;
1.204 + }
1.205 +
1.206 +void CDSAVerifyVector::DoCheckResult(TInt /*aError*/)
1.207 + {
1.208 +// If using Keith's fixed up testframework for testing failures, iResult will
1.209 +// have already been corrected for a deliberate fail result.
1.210 +// If not using Keith's testframework iResult is still a fail result at this
1.211 +// point, so now's the time to adjust for deliberate failure.
1.212 +
1.213 + if (!iResult)
1.214 + iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
1.215 +
1.216 + if( iResult == EFalse )
1.217 + {
1.218 + iConsole.Printf(_L("X"));
1.219 + }
1.220 + else
1.221 + {
1.222 + iConsole.Printf(_L("."));
1.223 + }
1.224 + }