1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/temb.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,321 @@
1.4 +/*
1.5 +* Copyright (c) 2007-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 +/**
1.23 + @file
1.24 + @internalComponent
1.25 + @released
1.26 +*/
1.27 +#include "cryptodriver.h"
1.28 +#include <e32base.h>
1.29 +#include <e32cons.h>
1.30 +#include <e32test.h>
1.31 +#include <e32debug.h>
1.32 +#include <rijndael.h>
1.33 +#include <cbcmode.h>
1.34 +#include <padding.h>
1.35 +#include <bufferedtransformation.h>
1.36 +#include <random.h>
1.37 +#include "keys.h"
1.38 +#include <cryptospi/cryptospidef.h>
1.39 +#include "cryptosymmetriccipherapi.h"
1.40 +#include "kmsclient.h"
1.41 +#include <f32file.h>
1.42 +//#include "kmsservercommon.h"
1.43 +using namespace CryptoSpi;
1.44 +
1.45 +_LIT(KTxtEPOC32EX,"temb: mainL failed");
1.46 +//_LIT(KTxtPressAnyKey," [press any key]");
1.47 +
1.48 +//#define KEYLEN 16
1.49 +#define KEYLEN 24
1.50 +//#define KEYLEN 32
1.51 +
1.52 +#define PKCS7PAD
1.53 +
1.54 +
1.55 +//#define BUFLEN 256
1.56 +#define BUFLEN (256*16)
1.57 +#define LOOPCOUNT 10000
1.58 +
1.59 +LOCAL_D RTest test(_L("Embedded Key Tests"));
1.60 +
1.61 +
1.62 +class CTestConsole:public CConsoleBase
1.63 +
1.64 + {
1.65 + public:
1.66 + static CTestConsole* NewL(CConsoleBase* aCon);
1.67 + TInt Create(const TDesC16& aTitle,TSize aSize) {return iCon->Create(aTitle,aSize);};
1.68 + void Read(TRequestStatus& aStatus) {iCon->Read(aStatus);};
1.69 + void ReadCancel(void) {iCon->ReadCancel();};
1.70 + void Write(const TDesC16& aString);
1.71 + TPoint CursorPos(void) const {return iCon->CursorPos();};
1.72 + void SetCursorPosAbs(const TPoint& aPos) {iCon->SetCursorPosAbs(aPos);};
1.73 + void SetCursorPosRel(const TPoint& aPos) {iCon->SetCursorPosRel(aPos);};
1.74 + void SetCursorHeight(TInt aHeight) {iCon->SetCursorHeight(aHeight);};
1.75 + void SetTitle(const TDesC16& aTitle) {iCon->SetTitle(aTitle);};
1.76 + void ClearScreen(void) {iCon->ClearScreen();};
1.77 + void ClearToEndOfLine(void) {iCon->ClearToEndOfLine();};
1.78 + TSize ScreenSize(void) const {return iCon->ScreenSize();};
1.79 + TKeyCode KeyCode(void) const {return iCon->KeyCode();};
1.80 + TUint KeyModifiers(void) const {return iCon->KeyModifiers();};
1.81 + ~CTestConsole(void);
1.82 + void SetLogFile(RFile &aFile);
1.83 + private:
1.84 + CTestConsole(void);
1.85 + CConsoleBase* iCon;
1.86 + RFile* iFile;
1.87 + };
1.88 +
1.89 +CTestConsole* CTestConsole::NewL(CConsoleBase* aCon)
1.90 + {
1.91 + CTestConsole* self;
1.92 + self=new (ELeave) CTestConsole;
1.93 + self->iCon=aCon;
1.94 + self->iFile=NULL;
1.95 + return self;
1.96 + }
1.97 +
1.98 +CTestConsole::CTestConsole(void):CConsoleBase()
1.99 +
1.100 + {
1.101 + }
1.102 +
1.103 +CTestConsole::~CTestConsole(void)
1.104 +
1.105 + {
1.106 + delete iCon;
1.107 + if (iFile)
1.108 + {
1.109 + iFile->Close();
1.110 + }
1.111 + }
1.112 +
1.113 +void CTestConsole::Write(const TDesC16& aString)
1.114 +
1.115 + {
1.116 + iCon->Write(aString);
1.117 + if (iFile)
1.118 + {
1.119 + TUint8 space[200];
1.120 + TPtr8 ptr(space,200);
1.121 + ptr.Copy(aString);
1.122 + iFile->Write(ptr);
1.123 + }
1.124 + }
1.125 +
1.126 +void CTestConsole::SetLogFile(RFile &aFile)
1.127 +
1.128 + {
1.129 + iFile = &aFile;
1.130 + }
1.131 +
1.132 +// private
1.133 +LOCAL_C void mainL();
1.134 +
1.135 +GLDEF_C TInt E32Main() // main function called by E32
1.136 + {
1.137 + CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
1.138 +
1.139 + //
1.140 + // Run the tests
1.141 + //
1.142 + __UHEAP_MARK;
1.143 + TRAPD(error,mainL()); // more initialization, then do example
1.144 + __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
1.145 + __UHEAP_MARKEND;
1.146 +
1.147 + delete cleanup; // destroy clean-up stack
1.148 + return 0; // and return
1.149 + }
1.150 +
1.151 +
1.152 +_LIT(KLddFileName,"cryptoldd.ldd");
1.153 +_LIT(KPddFileName,"crypto.h4.pdd");
1.154 +
1.155 +LOCAL_C void mainL() // initialize and call example code under cleanup stack
1.156 + {
1.157 + test.Title();
1.158 + CTestConsole *con = CTestConsole::NewL(test.Console());
1.159 +
1.160 + RFs fs;
1.161 + User::LeaveIfError(fs.Connect());
1.162 + CleanupClosePushL(fs);
1.163 +
1.164 + TDriveUnit sysDrive (fs.GetSystemDrive());
1.165 + TBuf<24> logFile (sysDrive.Name());
1.166 + logFile.Append(_L("\\temblog.txt"));
1.167 +
1.168 + RFile file;
1.169 + User::LeaveIfError(file.Replace(fs, logFile, EFileShareAny|EFileWrite));
1.170 + CleanupClosePushL(file);
1.171 +
1.172 + con->SetLogFile(file);
1.173 + test.SetConsole(con);
1.174 +
1.175 + TInt r;
1.176 + RDebug::Printf("Hello from user side\n");
1.177 +
1.178 + test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-TEMB-0001 Load driver "));
1.179 +
1.180 + test.Next(_L("Loading Physical Device"));
1.181 + r=User::LoadPhysicalDevice(KPddFileName);
1.182 + test(r==KErrNone || r==KErrAlreadyExists);
1.183 +
1.184 + test.Next(_L("Loading Logical Device"));
1.185 + r=User::LoadLogicalDevice(KLddFileName);
1.186 + test(r==KErrNone || r==KErrAlreadyExists);
1.187 +
1.188 + //
1.189 + // Generate key and IV
1.190 + //
1.191 + test.Start(_L("Random - Generating key & IV for AES tests"));
1.192 + test.Printf(_L("\tGenerating random key\n"));
1.193 + // Generate random 16 byte key
1.194 + TBuf8<KEYLEN> key;
1.195 + key.SetLength(key.MaxLength());
1.196 + TRandom::RandomL(key);
1.197 + key[0] = 'K';
1.198 + key[1] = 'E';
1.199 + key[2] = 'Y';
1.200 + key[3] = '*';
1.201 + for(int z=4; z<KEYLEN; ++z) key[z] = z;
1.202 +
1.203 + test.Printf(_L("\tGenerating random IV\n"));
1.204 + // Generate random 16 byte IV
1.205 + TBuf8<16> iv;
1.206 + iv.SetLength(iv.MaxLength());
1.207 + TRandom::RandomL(iv);
1.208 + iv[0] = 'I';
1.209 + iv[1] = 'V';
1.210 + iv[2] = '*';
1.211 + iv[3] = '*';
1.212 +
1.213 + TBuf8<BUFLEN> plaintext;
1.214 + plaintext.FillZ();
1.215 + plaintext.SetLength(BUFLEN);
1.216 + plaintext[0] = 'P';
1.217 + plaintext[1] = 'L';
1.218 + plaintext[2] = 'A';
1.219 + plaintext[3] = 'I';
1.220 + plaintext[4] = 'N';
1.221 + for(int i=0; i<BUFLEN; ++i)
1.222 + {
1.223 + plaintext[i] = i;
1.224 + }
1.225 +
1.226 +
1.227 + //
1.228 + // KMS tests
1.229 + //
1.230 + test.Next(_L("KMS - Store key"));
1.231 + TBuf8<BUFLEN+16> kmsData;
1.232 + kmsData.FillZ();
1.233 + kmsData.SetLength(0);
1.234 + do
1.235 + {
1.236 + RKeyMgmtSession kms;
1.237 + User::LeaveIfError(kms.Connect());
1.238 + CleanupClosePushL(kms);
1.239 +
1.240 + TKeyHandle keyHandle;
1.241 + User::LeaveIfError(kms.StoreKey(key, keyHandle));
1.242 + _LIT_SECURITY_POLICY_PASS(KAlwaysPass);
1.243 + User::LeaveIfError(kms.AddUsage(keyHandle, 0 /* operation */, KAlwaysPass));
1.244 +
1.245 + test.Next(_L("KMS - Attempt to use key via embedded key handle"));
1.246 + TPckgBuf<TKeyHandle> keyHandlePkg;
1.247 + keyHandlePkg() = keyHandle;
1.248 +
1.249 + TKeyProperty keyProperty = {KAesUid, KNullUid, KSymmetricKeyUid, KNonExtractableKey};
1.250 + CCryptoParams* keyParam =CCryptoParams::NewLC();
1.251 + keyParam->AddL(keyHandlePkg, KSymmetricKeyParameterUid);
1.252 + CKey *ckey=CKey::NewL(keyProperty, *keyParam);
1.253 + CleanupStack::PopAndDestroy(keyParam);
1.254 + CleanupStack::PushL(ckey);
1.255 + CryptoSpi::CSymmetricCipher *aes = 0;
1.256 + CSymmetricCipherFactory::CreateSymmetricCipherL(aes,
1.257 + KAesUid,
1.258 + *ckey,
1.259 + KCryptoModeEncryptUid,
1.260 + KOperationModeCBCUid,
1.261 + KPaddingModePKCS7Uid,
1.262 + NULL);
1.263 + CleanupStack::PopAndDestroy(ckey);
1.264 + CleanupStack::PushL(aes);
1.265 +
1.266 + aes->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
1.267 + aes->SetIvL(iv);
1.268 +
1.269 + aes->ProcessFinalL(plaintext, kmsData);
1.270 +
1.271 + CleanupStack::PopAndDestroy(aes);
1.272 + CleanupStack::PopAndDestroy(&kms);
1.273 + } while(false);
1.274 +
1.275 +
1.276 + //
1.277 + // Encrypt using legacy API
1.278 + //
1.279 + TBuf8<BUFLEN+16> sw;
1.280 + sw.FillZ();
1.281 + sw.SetLength(0);
1.282 + do
1.283 + {
1.284 + test.Next(_L("Encrypt using key directly (non-KMS)"));
1.285 +
1.286 + // ECB
1.287 + test.Printf(_L(" CBC\n"));
1.288 + CAESEncryptor *rawaes = CAESEncryptor::NewLC(key); // rawaes
1.289 + CModeCBCEncryptor *cbc = CModeCBCEncryptor::NewL(rawaes, iv);
1.290 + CleanupStack::Pop(rawaes); //
1.291 + CleanupStack::PushL(cbc); // cbc
1.292 +
1.293 +#ifdef PKCS7PAD
1.294 + CPadding *pad = CPaddingPKCS7::NewLC(16); // cbc, pad
1.295 +#else
1.296 + CPadding *pad = CPaddingNone::NewLC(16); // cbc, pad
1.297 +#endif
1.298 + CBufferedEncryptor *aes = CBufferedEncryptor::NewL(cbc, pad);
1.299 + CleanupStack::Pop(pad); // cbc
1.300 + CleanupStack::Pop(cbc);
1.301 + CleanupStack::PushL(aes); // aes
1.302 +
1.303 + test.Printf(_L("About to s/w encrypt (old api)\n"));
1.304 + aes->ProcessFinalL(plaintext, sw);
1.305 +
1.306 + CleanupStack::PopAndDestroy(aes);
1.307 + } while(false);
1.308 +
1.309 + test.Printf(_L("Checking KMS encrypt and direct encrypt had the same result\n"));
1.310 + test(kmsData == sw);
1.311 + test.End();
1.312 +
1.313 + test.Printf(_L("\r\n0 tests failed out of 1\r\n"));
1.314 +
1.315 + // test.Printf(KTxtPressAnyKey);
1.316 + // test.Getch(); // get and ignore character
1.317 + test.Close();
1.318 +
1.319 + CleanupStack::PopAndDestroy(&file);
1.320 + CleanupStack::PopAndDestroy(&fs);
1.321 + }
1.322 +
1.323 +
1.324 +// End of file