sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
/**
|
sl@0
|
20 |
@file
|
sl@0
|
21 |
@internalComponent
|
sl@0
|
22 |
@released
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
#include "cryptodriver.h"
|
sl@0
|
25 |
#include <e32base.h>
|
sl@0
|
26 |
#include <e32cons.h>
|
sl@0
|
27 |
#include <e32test.h>
|
sl@0
|
28 |
#include <e32debug.h>
|
sl@0
|
29 |
#include <rijndael.h>
|
sl@0
|
30 |
#include <cbcmode.h>
|
sl@0
|
31 |
#include <padding.h>
|
sl@0
|
32 |
#include <bufferedtransformation.h>
|
sl@0
|
33 |
#include <random.h>
|
sl@0
|
34 |
#include "keys.h"
|
sl@0
|
35 |
#include <cryptospi/cryptospidef.h>
|
sl@0
|
36 |
#include "cryptosymmetriccipherapi.h"
|
sl@0
|
37 |
#include "kmsclient.h"
|
sl@0
|
38 |
#include <f32file.h>
|
sl@0
|
39 |
//#include "kmsservercommon.h"
|
sl@0
|
40 |
using namespace CryptoSpi;
|
sl@0
|
41 |
|
sl@0
|
42 |
_LIT(KTxtEPOC32EX,"temb: mainL failed");
|
sl@0
|
43 |
//_LIT(KTxtPressAnyKey," [press any key]");
|
sl@0
|
44 |
|
sl@0
|
45 |
//#define KEYLEN 16
|
sl@0
|
46 |
#define KEYLEN 24
|
sl@0
|
47 |
//#define KEYLEN 32
|
sl@0
|
48 |
|
sl@0
|
49 |
#define PKCS7PAD
|
sl@0
|
50 |
|
sl@0
|
51 |
|
sl@0
|
52 |
//#define BUFLEN 256
|
sl@0
|
53 |
#define BUFLEN (256*16)
|
sl@0
|
54 |
#define LOOPCOUNT 10000
|
sl@0
|
55 |
|
sl@0
|
56 |
LOCAL_D RTest test(_L("Embedded Key Tests"));
|
sl@0
|
57 |
|
sl@0
|
58 |
|
sl@0
|
59 |
class CTestConsole:public CConsoleBase
|
sl@0
|
60 |
|
sl@0
|
61 |
{
|
sl@0
|
62 |
public:
|
sl@0
|
63 |
static CTestConsole* NewL(CConsoleBase* aCon);
|
sl@0
|
64 |
TInt Create(const TDesC16& aTitle,TSize aSize) {return iCon->Create(aTitle,aSize);};
|
sl@0
|
65 |
void Read(TRequestStatus& aStatus) {iCon->Read(aStatus);};
|
sl@0
|
66 |
void ReadCancel(void) {iCon->ReadCancel();};
|
sl@0
|
67 |
void Write(const TDesC16& aString);
|
sl@0
|
68 |
TPoint CursorPos(void) const {return iCon->CursorPos();};
|
sl@0
|
69 |
void SetCursorPosAbs(const TPoint& aPos) {iCon->SetCursorPosAbs(aPos);};
|
sl@0
|
70 |
void SetCursorPosRel(const TPoint& aPos) {iCon->SetCursorPosRel(aPos);};
|
sl@0
|
71 |
void SetCursorHeight(TInt aHeight) {iCon->SetCursorHeight(aHeight);};
|
sl@0
|
72 |
void SetTitle(const TDesC16& aTitle) {iCon->SetTitle(aTitle);};
|
sl@0
|
73 |
void ClearScreen(void) {iCon->ClearScreen();};
|
sl@0
|
74 |
void ClearToEndOfLine(void) {iCon->ClearToEndOfLine();};
|
sl@0
|
75 |
TSize ScreenSize(void) const {return iCon->ScreenSize();};
|
sl@0
|
76 |
TKeyCode KeyCode(void) const {return iCon->KeyCode();};
|
sl@0
|
77 |
TUint KeyModifiers(void) const {return iCon->KeyModifiers();};
|
sl@0
|
78 |
~CTestConsole(void);
|
sl@0
|
79 |
void SetLogFile(RFile &aFile);
|
sl@0
|
80 |
private:
|
sl@0
|
81 |
CTestConsole(void);
|
sl@0
|
82 |
CConsoleBase* iCon;
|
sl@0
|
83 |
RFile* iFile;
|
sl@0
|
84 |
};
|
sl@0
|
85 |
|
sl@0
|
86 |
CTestConsole* CTestConsole::NewL(CConsoleBase* aCon)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
CTestConsole* self;
|
sl@0
|
89 |
self=new (ELeave) CTestConsole;
|
sl@0
|
90 |
self->iCon=aCon;
|
sl@0
|
91 |
self->iFile=NULL;
|
sl@0
|
92 |
return self;
|
sl@0
|
93 |
}
|
sl@0
|
94 |
|
sl@0
|
95 |
CTestConsole::CTestConsole(void):CConsoleBase()
|
sl@0
|
96 |
|
sl@0
|
97 |
{
|
sl@0
|
98 |
}
|
sl@0
|
99 |
|
sl@0
|
100 |
CTestConsole::~CTestConsole(void)
|
sl@0
|
101 |
|
sl@0
|
102 |
{
|
sl@0
|
103 |
delete iCon;
|
sl@0
|
104 |
if (iFile)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
iFile->Close();
|
sl@0
|
107 |
}
|
sl@0
|
108 |
}
|
sl@0
|
109 |
|
sl@0
|
110 |
void CTestConsole::Write(const TDesC16& aString)
|
sl@0
|
111 |
|
sl@0
|
112 |
{
|
sl@0
|
113 |
iCon->Write(aString);
|
sl@0
|
114 |
if (iFile)
|
sl@0
|
115 |
{
|
sl@0
|
116 |
TUint8 space[200];
|
sl@0
|
117 |
TPtr8 ptr(space,200);
|
sl@0
|
118 |
ptr.Copy(aString);
|
sl@0
|
119 |
iFile->Write(ptr);
|
sl@0
|
120 |
}
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
void CTestConsole::SetLogFile(RFile &aFile)
|
sl@0
|
124 |
|
sl@0
|
125 |
{
|
sl@0
|
126 |
iFile = &aFile;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
|
sl@0
|
129 |
// private
|
sl@0
|
130 |
LOCAL_C void mainL();
|
sl@0
|
131 |
|
sl@0
|
132 |
GLDEF_C TInt E32Main() // main function called by E32
|
sl@0
|
133 |
{
|
sl@0
|
134 |
CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
|
sl@0
|
135 |
|
sl@0
|
136 |
//
|
sl@0
|
137 |
// Run the tests
|
sl@0
|
138 |
//
|
sl@0
|
139 |
__UHEAP_MARK;
|
sl@0
|
140 |
TRAPD(error,mainL()); // more initialization, then do example
|
sl@0
|
141 |
__ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
|
sl@0
|
142 |
__UHEAP_MARKEND;
|
sl@0
|
143 |
|
sl@0
|
144 |
delete cleanup; // destroy clean-up stack
|
sl@0
|
145 |
return 0; // and return
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
|
sl@0
|
149 |
_LIT(KLddFileName,"cryptoldd.ldd");
|
sl@0
|
150 |
_LIT(KPddFileName,"crypto.h4.pdd");
|
sl@0
|
151 |
|
sl@0
|
152 |
LOCAL_C void mainL() // initialize and call example code under cleanup stack
|
sl@0
|
153 |
{
|
sl@0
|
154 |
test.Title();
|
sl@0
|
155 |
CTestConsole *con = CTestConsole::NewL(test.Console());
|
sl@0
|
156 |
|
sl@0
|
157 |
RFs fs;
|
sl@0
|
158 |
User::LeaveIfError(fs.Connect());
|
sl@0
|
159 |
CleanupClosePushL(fs);
|
sl@0
|
160 |
|
sl@0
|
161 |
TDriveUnit sysDrive (fs.GetSystemDrive());
|
sl@0
|
162 |
TBuf<24> logFile (sysDrive.Name());
|
sl@0
|
163 |
logFile.Append(_L("\\temblog.txt"));
|
sl@0
|
164 |
|
sl@0
|
165 |
RFile file;
|
sl@0
|
166 |
User::LeaveIfError(file.Replace(fs, logFile, EFileShareAny|EFileWrite));
|
sl@0
|
167 |
CleanupClosePushL(file);
|
sl@0
|
168 |
|
sl@0
|
169 |
con->SetLogFile(file);
|
sl@0
|
170 |
test.SetConsole(con);
|
sl@0
|
171 |
|
sl@0
|
172 |
TInt r;
|
sl@0
|
173 |
RDebug::Printf("Hello from user side\n");
|
sl@0
|
174 |
|
sl@0
|
175 |
test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-TEMB-0001 Load driver "));
|
sl@0
|
176 |
|
sl@0
|
177 |
test.Next(_L("Loading Physical Device"));
|
sl@0
|
178 |
r=User::LoadPhysicalDevice(KPddFileName);
|
sl@0
|
179 |
test(r==KErrNone || r==KErrAlreadyExists);
|
sl@0
|
180 |
|
sl@0
|
181 |
test.Next(_L("Loading Logical Device"));
|
sl@0
|
182 |
r=User::LoadLogicalDevice(KLddFileName);
|
sl@0
|
183 |
test(r==KErrNone || r==KErrAlreadyExists);
|
sl@0
|
184 |
|
sl@0
|
185 |
//
|
sl@0
|
186 |
// Generate key and IV
|
sl@0
|
187 |
//
|
sl@0
|
188 |
test.Start(_L("Random - Generating key & IV for AES tests"));
|
sl@0
|
189 |
test.Printf(_L("\tGenerating random key\n"));
|
sl@0
|
190 |
// Generate random 16 byte key
|
sl@0
|
191 |
TBuf8<KEYLEN> key;
|
sl@0
|
192 |
key.SetLength(key.MaxLength());
|
sl@0
|
193 |
TRandom::RandomL(key);
|
sl@0
|
194 |
key[0] = 'K';
|
sl@0
|
195 |
key[1] = 'E';
|
sl@0
|
196 |
key[2] = 'Y';
|
sl@0
|
197 |
key[3] = '*';
|
sl@0
|
198 |
for(int z=4; z<KEYLEN; ++z) key[z] = z;
|
sl@0
|
199 |
|
sl@0
|
200 |
test.Printf(_L("\tGenerating random IV\n"));
|
sl@0
|
201 |
// Generate random 16 byte IV
|
sl@0
|
202 |
TBuf8<16> iv;
|
sl@0
|
203 |
iv.SetLength(iv.MaxLength());
|
sl@0
|
204 |
TRandom::RandomL(iv);
|
sl@0
|
205 |
iv[0] = 'I';
|
sl@0
|
206 |
iv[1] = 'V';
|
sl@0
|
207 |
iv[2] = '*';
|
sl@0
|
208 |
iv[3] = '*';
|
sl@0
|
209 |
|
sl@0
|
210 |
TBuf8<BUFLEN> plaintext;
|
sl@0
|
211 |
plaintext.FillZ();
|
sl@0
|
212 |
plaintext.SetLength(BUFLEN);
|
sl@0
|
213 |
plaintext[0] = 'P';
|
sl@0
|
214 |
plaintext[1] = 'L';
|
sl@0
|
215 |
plaintext[2] = 'A';
|
sl@0
|
216 |
plaintext[3] = 'I';
|
sl@0
|
217 |
plaintext[4] = 'N';
|
sl@0
|
218 |
for(int i=0; i<BUFLEN; ++i)
|
sl@0
|
219 |
{
|
sl@0
|
220 |
plaintext[i] = i;
|
sl@0
|
221 |
}
|
sl@0
|
222 |
|
sl@0
|
223 |
|
sl@0
|
224 |
//
|
sl@0
|
225 |
// KMS tests
|
sl@0
|
226 |
//
|
sl@0
|
227 |
test.Next(_L("KMS - Store key"));
|
sl@0
|
228 |
TBuf8<BUFLEN+16> kmsData;
|
sl@0
|
229 |
kmsData.FillZ();
|
sl@0
|
230 |
kmsData.SetLength(0);
|
sl@0
|
231 |
do
|
sl@0
|
232 |
{
|
sl@0
|
233 |
RKeyMgmtSession kms;
|
sl@0
|
234 |
User::LeaveIfError(kms.Connect());
|
sl@0
|
235 |
CleanupClosePushL(kms);
|
sl@0
|
236 |
|
sl@0
|
237 |
TKeyHandle keyHandle;
|
sl@0
|
238 |
User::LeaveIfError(kms.StoreKey(key, keyHandle));
|
sl@0
|
239 |
_LIT_SECURITY_POLICY_PASS(KAlwaysPass);
|
sl@0
|
240 |
User::LeaveIfError(kms.AddUsage(keyHandle, 0 /* operation */, KAlwaysPass));
|
sl@0
|
241 |
|
sl@0
|
242 |
test.Next(_L("KMS - Attempt to use key via embedded key handle"));
|
sl@0
|
243 |
TPckgBuf<TKeyHandle> keyHandlePkg;
|
sl@0
|
244 |
keyHandlePkg() = keyHandle;
|
sl@0
|
245 |
|
sl@0
|
246 |
TKeyProperty keyProperty = {KAesUid, KNullUid, KSymmetricKeyUid, KNonExtractableKey};
|
sl@0
|
247 |
CCryptoParams* keyParam =CCryptoParams::NewLC();
|
sl@0
|
248 |
keyParam->AddL(keyHandlePkg, KSymmetricKeyParameterUid);
|
sl@0
|
249 |
CKey *ckey=CKey::NewL(keyProperty, *keyParam);
|
sl@0
|
250 |
CleanupStack::PopAndDestroy(keyParam);
|
sl@0
|
251 |
CleanupStack::PushL(ckey);
|
sl@0
|
252 |
CryptoSpi::CSymmetricCipher *aes = 0;
|
sl@0
|
253 |
CSymmetricCipherFactory::CreateSymmetricCipherL(aes,
|
sl@0
|
254 |
KAesUid,
|
sl@0
|
255 |
*ckey,
|
sl@0
|
256 |
KCryptoModeEncryptUid,
|
sl@0
|
257 |
KOperationModeCBCUid,
|
sl@0
|
258 |
KPaddingModePKCS7Uid,
|
sl@0
|
259 |
NULL);
|
sl@0
|
260 |
CleanupStack::PopAndDestroy(ckey);
|
sl@0
|
261 |
CleanupStack::PushL(aes);
|
sl@0
|
262 |
|
sl@0
|
263 |
aes->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
|
sl@0
|
264 |
aes->SetIvL(iv);
|
sl@0
|
265 |
|
sl@0
|
266 |
aes->ProcessFinalL(plaintext, kmsData);
|
sl@0
|
267 |
|
sl@0
|
268 |
CleanupStack::PopAndDestroy(aes);
|
sl@0
|
269 |
CleanupStack::PopAndDestroy(&kms);
|
sl@0
|
270 |
} while(false);
|
sl@0
|
271 |
|
sl@0
|
272 |
|
sl@0
|
273 |
//
|
sl@0
|
274 |
// Encrypt using legacy API
|
sl@0
|
275 |
//
|
sl@0
|
276 |
TBuf8<BUFLEN+16> sw;
|
sl@0
|
277 |
sw.FillZ();
|
sl@0
|
278 |
sw.SetLength(0);
|
sl@0
|
279 |
do
|
sl@0
|
280 |
{
|
sl@0
|
281 |
test.Next(_L("Encrypt using key directly (non-KMS)"));
|
sl@0
|
282 |
|
sl@0
|
283 |
// ECB
|
sl@0
|
284 |
test.Printf(_L(" CBC\n"));
|
sl@0
|
285 |
CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // rawaes
|
sl@0
|
286 |
CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, iv);
|
sl@0
|
287 |
CleanupStack::Pop(rawaes); //
|
sl@0
|
288 |
CleanupStack::PushL(cbc); // cbc
|
sl@0
|
289 |
|
sl@0
|
290 |
#ifdef PKCS7PAD
|
sl@0
|
291 |
CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad
|
sl@0
|
292 |
#else
|
sl@0
|
293 |
CPadding *pad = CPaddingNone::NewLC(16); // cbc, pad
|
sl@0
|
294 |
#endif
|
sl@0
|
295 |
CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad);
|
sl@0
|
296 |
CleanupStack::Pop(pad); // cbc
|
sl@0
|
297 |
CleanupStack::Pop(cbc);
|
sl@0
|
298 |
CleanupStack::PushL(aes); // aes
|
sl@0
|
299 |
|
sl@0
|
300 |
test.Printf(_L("About to s/w encrypt (old api)\n"));
|
sl@0
|
301 |
aes->ProcessFinalL(plaintext, sw);
|
sl@0
|
302 |
|
sl@0
|
303 |
CleanupStack::PopAndDestroy(aes);
|
sl@0
|
304 |
} while(false);
|
sl@0
|
305 |
|
sl@0
|
306 |
test.Printf(_L("Checking KMS encrypt and direct encrypt had the same result\n"));
|
sl@0
|
307 |
test(kmsData == sw);
|
sl@0
|
308 |
test.End();
|
sl@0
|
309 |
|
sl@0
|
310 |
test.Printf(_L("\r\n0 tests failed out of 1\r\n"));
|
sl@0
|
311 |
|
sl@0
|
312 |
// test.Printf(KTxtPressAnyKey);
|
sl@0
|
313 |
// test.Getch(); // get and ignore character
|
sl@0
|
314 |
test.Close();
|
sl@0
|
315 |
|
sl@0
|
316 |
CleanupStack::PopAndDestroy(&file);
|
sl@0
|
317 |
CleanupStack::PopAndDestroy(&fs);
|
sl@0
|
318 |
}
|
sl@0
|
319 |
|
sl@0
|
320 |
|
sl@0
|
321 |
// End of file
|