|
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 "trsasignfb.h" |
|
20 #include "t_input.h" |
|
21 #include <asymmetric.h> |
|
22 #include <padding.h> |
|
23 #include <bigint.h> |
|
24 #include "performancetest.h" |
|
25 |
|
26 _LIT8(KInputStart, "<input>"); |
|
27 _LIT8(KKeyBitsStart, "<keybits>"); |
|
28 _LIT8(KKeyBitsEnd, "</keybits>"); |
|
29 |
|
30 CRSASignFB::~CRSASignFB() |
|
31 { |
|
32 delete iBody; |
|
33 } |
|
34 |
|
35 CRSASignFB::CRSASignFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut) |
|
36 : CTestAction(aConsole, aOut), iFs(aFs), iPerfTestIterations(100) |
|
37 { |
|
38 } |
|
39 |
|
40 CTestAction* CRSASignFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, |
|
41 const TTestActionSpec& aTestActionSpec) |
|
42 { |
|
43 CTestAction* self = CRSASignFB::NewLC(aFs, aConsole, |
|
44 aOut, aTestActionSpec); |
|
45 CleanupStack::Pop(); |
|
46 return self; |
|
47 } |
|
48 |
|
49 CTestAction* CRSASignFB::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, |
|
50 const TTestActionSpec& aTestActionSpec) |
|
51 { |
|
52 CRSASignFB* self = new(ELeave) CRSASignFB(aFs, aConsole, aOut); |
|
53 CleanupStack::PushL(self); |
|
54 self->ConstructL(aTestActionSpec); |
|
55 return self; |
|
56 } |
|
57 |
|
58 |
|
59 TRSAPrivateKeyType CRSASignFB::TypeOfCrypto() |
|
60 { |
|
61 return iCryptoType; |
|
62 } |
|
63 |
|
64 void CRSASignFB::ConstructL(const TTestActionSpec& aTestActionSpec) |
|
65 { |
|
66 CTestAction::ConstructL(aTestActionSpec); |
|
67 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); |
|
68 iBody->Des().Copy(aTestActionSpec.iActionBody); |
|
69 |
|
70 if (CPerformance::PerformanceTester()->IsTestingPerformance()) |
|
71 {// Number of test iterations |
|
72 TPtrC8 itsPtr = Input::ParseElement(aTestActionSpec.iActionBody, KIterationsStart, KIterationsEnd); |
|
73 if (itsPtr!=KNullDesC8) |
|
74 { |
|
75 TLex8 lex; |
|
76 lex.Assign(itsPtr); |
|
77 User::LeaveIfError(lex.Val(iPerfTestIterations)); |
|
78 if (iPerfTestIterations > KMaxIterations) |
|
79 User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument); |
|
80 |
|
81 } |
|
82 TPtrC8 cryptoPtr = Input::ParseElement(aTestActionSpec.iActionBody, KTypeOfCryptoStart, KTypeOfCryptoEnd); |
|
83 if (cryptoPtr.CompareF(KRSAStandard) == 0) |
|
84 { |
|
85 iCryptoType = EStandard; |
|
86 } |
|
87 else if (cryptoPtr.CompareF(KRSAStandardCRT) == 0) |
|
88 { |
|
89 iCryptoType = EStandardCRT; |
|
90 } |
|
91 else |
|
92 { |
|
93 User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument); |
|
94 } |
|
95 } |
|
96 |
|
97 TPtrC8 keyBitsPtr = Input::ParseElement(*iBody, KKeyBitsStart, KKeyBitsEnd); |
|
98 if (keyBitsPtr!=KNullDesC8) |
|
99 { |
|
100 TLex8 lex; |
|
101 lex.Assign(keyBitsPtr); |
|
102 User::LeaveIfError(lex.Val(iKeyBits)); |
|
103 } |
|
104 } |
|
105 |
|
106 void CRSASignFB::DoPerformPrerequisite(TRequestStatus& aStatus) |
|
107 { |
|
108 TRequestStatus* status = &aStatus; |
|
109 |
|
110 HBufC8* input = Input::ParseElementHexL(*iBody, KInputStart); |
|
111 CleanupStack::PushL(input); |
|
112 iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1); |
|
113 iSigInput->Update(*input); |
|
114 iInput = iSigInput->Final().AllocL(); |
|
115 CleanupStack::PopAndDestroy(input); |
|
116 |
|
117 User::RequestComplete(status, KErrNone); |
|
118 iActionState = CTestAction::EAction; |
|
119 } |
|
120 |
|
121 void CRSASignFB::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
122 { |
|
123 TRequestStatus* status = &aStatus; |
|
124 delete iInput; |
|
125 delete iSigInput; |
|
126 |
|
127 iFinished = ETrue; |
|
128 User::RequestComplete(status, iActionResult); |
|
129 } |
|
130 |
|
131 void CRSASignFB::DoReportAction(void) |
|
132 { |
|
133 } |
|
134 |
|
135 void CRSASignFB::DoCheckResult(TInt) |
|
136 { |
|
137 if (iResult) |
|
138 iConsole.Printf(_L(".")); |
|
139 else |
|
140 iConsole.Printf(_L("X")); |
|
141 } |
|
142 |
|
143 void CRSASignFB::Hex(HBufC8& aString) |
|
144 { |
|
145 TPtr8 ptr=aString.Des(); |
|
146 if (aString.Length()%2) |
|
147 { |
|
148 ptr.SetLength(0); |
|
149 return; |
|
150 } |
|
151 TInt i; |
|
152 for (i=0;i<aString.Length();i+=2) |
|
153 { |
|
154 TUint8 tmp; |
|
155 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0')); |
|
156 tmp*=16; |
|
157 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); |
|
158 ptr[i/2]=tmp; |
|
159 } |
|
160 ptr.SetLength(aString.Length()/2); |
|
161 } |
|
162 |
|
163 void CRSASignFB::PerformAction(TRequestStatus& aStatus) |
|
164 { |
|
165 TRequestStatus* status = &aStatus; |
|
166 |
|
167 if (CPerformance::PerformanceTester()->IsTestingPerformance()) |
|
168 { |
|
169 iConsole.Printf(_L(">")); // Indicates start of test |
|
170 TRAP(iActionResult, DoPerformanceTestActionL()); |
|
171 } |
|
172 else |
|
173 { |
|
174 TRAP(iActionResult, DoPerformActionL()); |
|
175 } |
|
176 |
|
177 if (iActionResult==KErrNoMemory) |
|
178 User::Leave(iActionResult); // For OOM testing |
|
179 |
|
180 User::RequestComplete(status, iActionResult); |
|
181 iActionState = CTestAction::EPostrequisite; |
|
182 } |
|
183 |
|
184 |
|
185 void CRSASignFB::DoPerformanceTestActionL() |
|
186 { |
|
187 __UHEAP_MARK; |
|
188 iResult = EFalse; |
|
189 |
|
190 TTimeIntervalMicroSeconds keyCreateTime(0); |
|
191 TTimeIntervalMicroSeconds signTime(0); |
|
192 TTimeIntervalMicroSeconds verifyTime(0); |
|
193 TTime start, end; |
|
194 TTimeIntervalSeconds diff(0); |
|
195 const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); |
|
196 |
|
197 // Time key pair creation |
|
198 CRSAKeyPair *rsaPair = NULL; |
|
199 TUint noRSAPairs = 0; |
|
200 start.UniversalTime(); |
|
201 while (diff < KIterationTime) |
|
202 { |
|
203 rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); |
|
204 CleanupStack::PopAndDestroy(); |
|
205 noRSAPairs++; |
|
206 end.UniversalTime(); |
|
207 end.SecondsFrom(start, diff); |
|
208 } |
|
209 end.UniversalTime(); |
|
210 keyCreateTime = end.MicroSecondsFrom(start); |
|
211 TReal keycreatetime = I64REAL(keyCreateTime.Int64()); |
|
212 |
|
213 rsaPair=CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); // Create one keypair for operations |
|
214 |
|
215 CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(rsaPair->PrivateKey()); |
|
216 CleanupStack::PushL(signer); |
|
217 CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(rsaPair->PublicKey()); |
|
218 CleanupStack::PushL(verifier); |
|
219 |
|
220 const CRSASignature *testSig; |
|
221 |
|
222 // Time signing |
|
223 diff = 0; |
|
224 TInt noSignings = 0; |
|
225 start.UniversalTime(); |
|
226 while (diff < KIterationTime) |
|
227 { |
|
228 testSig = signer->SignL(*iInput); |
|
229 delete testSig; |
|
230 noSignings++; |
|
231 end.UniversalTime(); |
|
232 end.SecondsFrom(start, diff); |
|
233 } |
|
234 end.UniversalTime(); |
|
235 signTime = end.MicroSecondsFrom(start); |
|
236 TReal signtime = I64REAL(signTime.Int64()); |
|
237 // Time verification |
|
238 TInt noVerifies = 0; |
|
239 diff = 0; |
|
240 testSig = signer->SignL(*iInput); |
|
241 CleanupStack::PushL(const_cast<CRSASignature*>(testSig)); |
|
242 start.UniversalTime(); |
|
243 while (diff < KIterationTime) |
|
244 { |
|
245 iResult = verifier->VerifyL(*iInput, *testSig); |
|
246 |
|
247 // do as many verfies as possible |
|
248 if (!iResult) |
|
249 { |
|
250 break; |
|
251 } |
|
252 noVerifies++; |
|
253 end.UniversalTime(); |
|
254 end.SecondsFrom(start, diff); |
|
255 } |
|
256 end.UniversalTime(); |
|
257 verifyTime = end.MicroSecondsFrom(start); |
|
258 TReal verifytime = I64REAL(verifyTime.Int64()); |
|
259 CleanupStack::PopAndDestroy(4); // verifier,signer,testSig,rsaPairs |
|
260 __UHEAP_MARKEND; |
|
261 |
|
262 if (iResult) |
|
263 { |
|
264 TBuf<256> buf; |
|
265 TReal rate = I64REAL(keyCreateTime.Int64()) / noRSAPairs; |
|
266 |
|
267 _LIT(KKeyCreateTime, "\n\tKeyCreate: %f us/key creation (no creations: %i in %f us)\r\n"); |
|
268 buf.Format(KKeyCreateTime, rate, noRSAPairs, keycreatetime); |
|
269 iOut.writeString(buf); |
|
270 |
|
271 rate = I64REAL(signTime.Int64()) / noSignings; |
|
272 _LIT(KSignTime, "\tSigning: %f us/signing (no signings: %i in %f us)\r\n"); |
|
273 buf.Format(KSignTime, rate, noSignings, signtime); |
|
274 iOut.writeString(buf); |
|
275 |
|
276 rate = I64REAL(verifyTime.Int64()) / noVerifies; |
|
277 _LIT(KVerifyTime, "\tVerifying: %f us/verify (no verfies: %i in %f us)\r\n"); |
|
278 buf.Format(KVerifyTime, rate, noVerifies, verifytime); |
|
279 iOut.writeString(buf); |
|
280 } |
|
281 else |
|
282 { |
|
283 _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); |
|
284 iOut.writeString(KNoTimingInfo); |
|
285 } |
|
286 } |
|
287 |
|
288 void CRSASignFB::DoPerformActionL() |
|
289 { |
|
290 __UHEAP_MARK; |
|
291 |
|
292 iResult = EFalse; |
|
293 |
|
294 CRSAKeyPair* rsaPair = CRSAKeyPair::NewLC(512, TypeOfCrypto()); |
|
295 |
|
296 CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(rsaPair->PrivateKey()); |
|
297 CleanupStack::PushL(signer); |
|
298 |
|
299 CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(rsaPair->PublicKey()); |
|
300 CleanupStack::PushL(verifier); |
|
301 |
|
302 const CRSASignature* signature = signer->SignL(*iInput); |
|
303 CleanupStack::PushL(const_cast<CRSASignature*>(signature)); |
|
304 |
|
305 if(verifier->VerifyL(*iInput, *signature)) |
|
306 { |
|
307 iResult = ETrue; |
|
308 } |
|
309 |
|
310 CleanupStack::PopAndDestroy(4, rsaPair); //signature, verifier, signer, rsaPair |
|
311 |
|
312 __UHEAP_MARKEND; |
|
313 } |