72
|
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::RandomL(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 |
CleanupStack::PopAndDestroy(const_cast<CDSASignature*>(signature));
|
|
129 |
CleanupStack::PopAndDestroy(verifier);
|
|
130 |
CleanupStack::PopAndDestroy(signer);
|
|
131 |
CleanupStack::PopAndDestroy(dsaPair);
|
|
132 |
CleanupStack::PopAndDestroy(); //SetThreadRandomLC
|
|
133 |
|
|
134 |
User::RequestComplete(status, KErrNone);
|
|
135 |
iActionState = CTestAction::EPostrequisite;
|
|
136 |
__UHEAP_MARKEND;
|
|
137 |
}
|
|
138 |
|
|
139 |
void CDSASignFB::Hex(HBufC8& aString)
|
|
140 |
{
|
|
141 |
TPtr8 ptr=aString.Des();
|
|
142 |
if (aString.Length()%2)
|
|
143 |
{
|
|
144 |
ptr.SetLength(0);
|
|
145 |
return;
|
|
146 |
}
|
|
147 |
TInt i;
|
|
148 |
for (i=0;i<aString.Length();i+=2)
|
|
149 |
{
|
|
150 |
TUint8 tmp;
|
|
151 |
tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
|
|
152 |
tmp*=16;
|
|
153 |
tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
|
|
154 |
ptr[i/2]=tmp;
|
|
155 |
}
|
|
156 |
ptr.SetLength(aString.Length()/2);
|
|
157 |
}
|
|
158 |
|