os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/rijndaelimpl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2007-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 /**
    20  @file
    21  @internalComponent
    22  @released
    23 */
    24 #include "rijndaelimpl.h"
    25 #include "keys.h"
    26 #include "pluginconfig.h"
    27 
    28 using namespace HwCrypto;
    29 
    30 const TUint KAESKeyBytes128 = 16;
    31 const TUint KAESKeyBytes192 = 24;
    32 const TUint KAESKeyBytes256 = 32;
    33 const TUint KAESBlockBytes = 16;
    34 
    35 _LIT(KLddFileName,"cryptoldd.ldd");
    36 _LIT(KPddFileName,"crypto.h4.pdd");
    37 
    38 
    39 /* CRijndaelmpl*/
    40 CH4RijndaelImpl::CH4RijndaelImpl(
    41 	TUid aCryptoMode,
    42 	TUid aOperationMode,
    43 	TUid aPadding) :
    44 	CH4CipherImpl(KAESBlockBytes, aCryptoMode, aOperationMode, aPadding)
    45 	{
    46 	}
    47 
    48 CH4RijndaelImpl* CH4RijndaelImpl::NewL(const CKey& aKey, TUid aCryptoMode,	TUid aOperationMode, TUid aPadding)
    49 	{
    50 	CH4RijndaelImpl* self = CH4RijndaelImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding);
    51 	CleanupStack::Pop(self);
    52 	return self;
    53 	}
    54 	
    55 CH4RijndaelImpl* CH4RijndaelImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding)
    56 	{
    57 	CH4RijndaelImpl* self = new(ELeave) CH4RijndaelImpl(aCryptoMode, aOperationMode, aPadding);
    58 	CleanupStack::PushL(self);
    59 	self->ConstructL(aKey);
    60 	return self;
    61 	}
    62 		
    63 CH4RijndaelImpl::~CH4RijndaelImpl()
    64 	{
    65 	iDriver.Close();
    66 	}
    67 	
    68 void CH4RijndaelImpl::ConstructL(const CKey& aKey)
    69 	{
    70 	// Load PDD
    71     TInt r = User::LoadPhysicalDevice(KPddFileName);
    72 	if((r != KErrNone) && (r != KErrAlreadyExists)) User::Leave(r);
    73 
    74 	// Load LDD
    75     r = User::LoadLogicalDevice(KLddFileName);
    76 	if((r != KErrNone) && (r != KErrAlreadyExists)) User::Leave(r);
    77 
    78 
    79 	CH4CipherImpl::ConstructL(aKey);			
    80 	}
    81 
    82 TUid CH4RijndaelImpl::ImplementationUid() const
    83 	{
    84 	return KCryptoPluginAesUid;
    85 	}
    86 
    87 const CExtendedCharacteristics* CH4RijndaelImpl::GetExtendedCharacteristicsL()
    88 	{
    89 	return StaticGetExtendedCharacteristicsL();
    90 	}
    91 
    92 CExtendedCharacteristics* CH4RijndaelImpl::StaticGetExtendedCharacteristicsL()
    93 	{
    94 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
    95 	// for exclusive use and are not CERTIFIED to be standards compliant.
    96 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
    97 	}
    98 
    99 	
   100 TBool CH4RijndaelImpl::IsValidKeyLength(TInt aKeyBytes) const
   101 	{
   102 	switch(aKeyBytes)
   103 		{
   104 		case KAESKeyBytes128:
   105 		case KAESKeyBytes192:
   106 		case KAESKeyBytes256:
   107 			return ETrue;
   108 		default:
   109 			return EFalse;
   110 		}			
   111 	}
   112 
   113 void CH4RijndaelImpl::DoSetupL()
   114 	{
   115 	iDriver.Close();
   116 	User::LeaveIfError(iDriver.Open());
   117 
   118 	TBool encrypt = (iCryptoMode.iUid == KCryptoModeEncrypt);
   119 	RCryptoDriver::TChainingMode chainingMode = (iOperationMode.iUid == KOperationModeCBC) ? (RCryptoDriver::ECbcMode) : (RCryptoDriver::EEcbMode);
   120 	User::LeaveIfError(iDriver.SetAesConfig(encrypt, chainingMode, iKey->Des(), iIv));
   121 	}
   122 
   123 void CH4RijndaelImpl::DoWriteL(const TUint8* aBuffer, TUint aNumBytes)
   124 	{
   125 	TPtrC8 ptr(aBuffer, aNumBytes);
   126 
   127     TRequestStatus status;
   128 	iDriver.AesWrite(status, ptr);
   129 	User::WaitForRequest(status);
   130 	User::LeaveIfError(status.Int());
   131 	}
   132 
   133 void CH4RijndaelImpl::DoReadL(TDes8 &aBuffer, TUint32 aLength)
   134 	{
   135     TRequestStatus status;
   136 	iDriver.AesRead(status, aBuffer, aLength);
   137 	User::WaitForRequest(status);
   138 	User::LeaveIfError(status.Int());
   139 	}
   140 
   141 
   142 	
   143 // End of file