Update contrib.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "tdhvector.h"
21 #include "performancetest.h"
23 enum TActionMode {EMode1 = 1, EMode2 = 2, EMode3 = 3, EMode4 = 4};
25 CTestAction* CDHVector::NewL(RFs& aFs,
26 CConsoleBase& aConsole,
28 const TTestActionSpec& aTestActionSpec)
30 CTestAction* self = CDHVector::NewLC(aFs, aConsole,
31 aOut, aTestActionSpec);
36 CTestAction* CDHVector::NewLC(RFs& aFs,
37 CConsoleBase& aConsole,
39 const TTestActionSpec& aTestActionSpec)
41 CDHVector* self = new(ELeave) CDHVector(aFs, aConsole, aOut);
42 CleanupStack::PushL(self);
43 self->ConstructL(aTestActionSpec);
47 CDHVector::~CDHVector()
53 if (iActionMode != EMode2)
59 if (iActionMode == EMode3)
67 if (iActionMode == EMode4)
76 CDHVector::CDHVector(RFs& /*aFs*/,
77 CConsoleBase& aConsole,
79 : CVectorTest(aConsole, aOut)
83 void CDHVector::ConstructL(const TTestActionSpec& aTestActionSpec)
85 CVectorTest::ConstructL(aTestActionSpec);
87 iActionMode = Input::ParseIntElement(aTestActionSpec.iActionBody, _L8("<actionmode>"), _L8("</actionmode>"));
89 iN = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<n>"));
90 iG = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<g>"));
92 if(iActionMode == EMode2 || iActionMode == EMode4)
94 iX = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<x>"));
96 else if(iActionMode == EMode3)
98 iNd = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<nd>"));
99 iGd = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<gd>"));
103 void CDHVector::DoPerformPrerequisite(TRequestStatus& aStatus)
105 iN1 = RInteger::NewL(*iN);
106 iN2 = RInteger::NewL(*iN);
107 iG1 = RInteger::NewL(*iG);
108 iG2 = RInteger::NewL(*iG);
110 if(iActionMode == EMode2 || iActionMode == EMode4)
112 iX1 = RInteger::NewL(*iX);
113 iX2 = RInteger::NewL(*iX);
115 else if(iActionMode == EMode3)
117 iN3 = RInteger::NewL(*iNd);
118 iG3 = RInteger::NewL(*iGd);
121 TRequestStatus* status = &aStatus;
122 User::RequestComplete(status, KErrNone);
123 iActionState = CTestAction::EAction;
126 void CDHVector::DoPerformanceTestActionL()
131 CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);
132 CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
133 CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
135 TTimeIntervalMicroSeconds agreeTime(0);
137 TTimeIntervalSeconds diff(0);
138 const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
142 start.UniversalTime();
144 while (diff < KIterationTime)
146 delete const_cast<HBufC8*>(dh1->AgreeL(keyPair2->PublicKey()));
148 end.SecondsFrom(start, diff);
153 agreeTime = end.MicroSecondsFrom(start);
155 CleanupStack::PopAndDestroy(dh1);
156 CleanupStack::PopAndDestroy(keyPair2);
157 CleanupStack::PopAndDestroy(keyPair1);
163 TReal rate = I64REAL(agreeTime.Int64()) / index;
164 TReal agreetime = I64REAL(agreeTime.Int64());
166 _LIT(KEncryptTime, "\tKey Agreements\t%f");
167 buf.Format(KEncryptTime, rate);
168 iOut.writeString(buf);
172 _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
173 iOut.writeString(KNoTimingInfo);
177 void CDHVector::DoPerformActionL()
184 // Test for successful keypair generation with N and G
187 CDHKeyPair* keyPair1 = CDHKeyPair::NewL(iN1, iG1);
188 CleanupStack::PushL(keyPair1);
189 CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
190 CDH* dh1 = CDH::NewL(keyPair1->PrivateKey());
191 CleanupStack::PushL(dh1);
192 CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
193 const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
194 CleanupStack::PushL(const_cast<HBufC8*>(key1));
195 const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
196 CleanupStack::PushL(const_cast<HBufC8*>(key2));
201 CleanupStack::PopAndDestroy(2, const_cast<HBufC8*>(key1));
202 CleanupStack::PopAndDestroy(4, keyPair1);
206 // Test for successful keypair generation with N , G and X
209 CDHKeyPair* keyPair1 = CDHKeyPair::NewL(iN1, iG1, iX1);
210 CleanupStack::PushL(keyPair1);
211 CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2, iX2);
212 CDH* dh1 = CDH::NewL(keyPair1->PrivateKey());
213 CleanupStack::PushL(dh1);
214 CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
215 const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
216 CleanupStack::PushL(const_cast<HBufC8*>(key1));
217 const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
218 CleanupStack::PushL(const_cast<HBufC8*>(key2));
223 CleanupStack::PopAndDestroy(2, const_cast<HBufC8*>(key1));
224 CleanupStack::PopAndDestroy(4, keyPair1);
228 // Test for mis matching N & G key pair.
231 CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);
232 CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN3, iG3);
233 CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
235 TRAPD(err, key1 = dh1->AgreeL(keyPair2->PublicKey()));
236 if (err != KErrArgument)
241 CleanupStack::PopAndDestroy(3, keyPair1);
245 // Test for wromg values of G and X
248 CDHKeyPair* keyPair1 = NULL;
249 TRAPD(err, keyPair1 = CDHKeyPair::NewLC(iN1, iG1, iX1));
250 if (err != KErrArgument)