sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalComponent sl@0: @released sl@0: */ sl@0: #ifndef __SYMMETRICCIPHERIMPL_H__ sl@0: #define __SYMMETRICCIPHERIMPL_H__ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "symmetriccipherplugin.h" sl@0: sl@0: sl@0: #ifdef __MARM__ sl@0: #define __NOTSHARED __declspec(notshared) sl@0: #else sl@0: #define __NOTSHARED sl@0: #endif sl@0: sl@0: /** The maximum block size supported (in bytes) */ sl@0: const TUint KMaxBlockSizeSupported = 32; sl@0: sl@0: /** sl@0: Abstract base class for symmetric cipher plug-ins. sl@0: */ sl@0: namespace HwCrypto sl@0: { sl@0: using namespace CryptoSpi; sl@0: sl@0: class __NOTSHARED CH4CipherImpl : public CBase, public MSymmetricCipher sl@0: { sl@0: public: sl@0: /** sl@0: Implemented by each cipher subclass to determine whether the sl@0: specified key length is valid for that cipher. sl@0: This is called by ConstructL and SetKeyL sl@0: @param aKeyLength The key length in bytes to verify. sl@0: */ sl@0: virtual TBool IsValidKeyLength(TInt aKeyBytes) const = 0; sl@0: sl@0: /** sl@0: Helper function implemented by concrete cipher sub-class that sl@0: allows GetCharacteristicsL to return the correct characteristics object. sl@0: @return The implemention uid sl@0: */ sl@0: virtual TUid ImplementationUid() const = 0; sl@0: sl@0: /** sl@0: Gets the strength of the current key, needed to check whether the cipher sl@0: may operate if strong cryptography is not enabled. sl@0: @return The strength of the current key sl@0: */ sl@0: virtual TInt GetKeyStrength() const; sl@0: sl@0: sl@0: // Override MPlugin virtual functions sl@0: void Close(); sl@0: TAny* GetExtension(TUid aExtensionId); sl@0: sl@0: void GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics); sl@0: sl@0: sl@0: void Reset(); // Always call reset in super-class if you override this sl@0: // End of MPlugin virtual functions sl@0: sl@0: // Override MSymmetricCipherBase virtual functions sl@0: TInt KeySize() const; sl@0: virtual void SetKeyL(const CKey& aKey); sl@0: sl@0: TInt BlockSize() const; sl@0: void SetCryptoModeL(TUid aCryptoMode); sl@0: void SetOperationModeL(TUid aOperationMode); sl@0: void SetPaddingModeL(TUid aPaddingMode); sl@0: void SetIvL(const TDesC8& aIv); sl@0: sl@0: TInt MaxOutputLength(TInt aInputLength) const; sl@0: TInt MaxFinalOutputLength(TInt aInputLength) const; sl@0: sl@0: void ProcessL(const TDesC8& aInput, TDes8& aOutput); sl@0: void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput); sl@0: // End of MSymmetricCipherBase sl@0: sl@0: /// Destructor sl@0: ~CH4CipherImpl(); sl@0: sl@0: protected: sl@0: sl@0: /** sl@0: Constructor sl@0: @param aBlockBytes The block size in bytes sl@0: @param aOperationMode The mode of operation e.g. CBC sl@0: @param aCryptoMode Whether to encrypt or decrypt sl@0: */ sl@0: CH4CipherImpl(TUint8 aBlockBytes, sl@0: TUid aCryptoMode, sl@0: TUid aOperationMode, sl@0: TUid aPaddingMode); sl@0: sl@0: /** sl@0: Second phase of construction. Always call ConstructL in the super-class sl@0: if your override this method. sl@0: sl@0: @param aKey The key to initialise the cipher with. sl@0: */ sl@0: virtual void ConstructL(const CKey& aKey); sl@0: sl@0: /** sl@0: Creates the extended characteristics object. Concrete plug-in classes sl@0: may override this to customise the object returned by GetCharacteristics(); sl@0: sl@0: The default configuration is that plug-ins have unlimited concurrency, sl@0: cannot be reserved for exclusive use and are not CERTIFIED to be standards compliant. sl@0: @return The extended characteristics object sl@0: */ sl@0: virtual CExtendedCharacteristics* CreateExtendedCharacteristicsL(); sl@0: sl@0: /** sl@0: Extracts the raw symmetric key from a generic key object. The buffer sl@0: is placed on the cleanup stack. sl@0: sl@0: @param aKey The key object sl@0: @return A buffer containing the raw key value sl@0: */ sl@0: HBufC8* ExtractKeyDataLC(const CKey& aKey) const; sl@0: sl@0: sl@0: /** sl@0: DoSetDetails sl@0: sl@0: Reconfigure h/w based on iCryptoMode/iOperationMode/iKey/iIv sl@0: sl@0: Implemented by each cipher subclass. sl@0: */ sl@0: virtual void DoSetupL() = 0; sl@0: sl@0: /** sl@0: DoWrite sl@0: sl@0: Blocking write of data to be transformed. Need not be a sl@0: multiple of the block size. sl@0: sl@0: Implemented by each cipher subclass. sl@0: */ sl@0: virtual void DoWriteL(const TUint8* aBuffer, TUint aNumBytes) = 0; sl@0: sl@0: /** sl@0: DoRead sl@0: sl@0: Blocking read of transformed data. sl@0: sl@0: Need not be a multiple of the block size, but caller should sl@0: ensure enough blocks of data have been written to satisfy sl@0: the request. sl@0: sl@0: The data is appended to the supplied descriptor. sl@0: sl@0: Implemented by each cipher subclass. sl@0: */ sl@0: virtual void DoReadL(TDes8 &aBuffer, TUint32 aLength) = 0; sl@0: sl@0: sl@0: protected: sl@0: sl@0: /// the key, extracted from a CKey object sl@0: HBufC8* iKey; sl@0: sl@0: /// key size in bytes sl@0: TUint iKeyBytes; sl@0: sl@0: /// Standards conformance information. sl@0: RArray iStandardsConformance; sl@0: sl@0: /// block size in bytes, current largest block size is 16 bytes (AES) sl@0: TUint8 iBlockBytes; sl@0: /// encryption or decryption sl@0: TUid iCryptoMode; sl@0: /// The block cipher mode e.g. ECB, CBC sl@0: TUid iOperationMode; sl@0: /// the current padding scheme sl@0: TUid iPaddingMode; sl@0: sl@0: /// the initialisation vector sl@0: RBuf8 iIv; sl@0: sl@0: /// current padding scheme implementation sl@0: CPadding* iPadding; sl@0: /// buffer to store blocks sl@0: RBuf8 iPartialBlock; sl@0: /// buffer to store input / output of padding sl@0: RBuf8 iPaddingBlock; sl@0: sl@0: private: sl@0: TBool iNeedToSetupHw; sl@0: }; sl@0: sl@0: sl@0: } sl@0: sl@0: #endif // __SYMMETRICCIPHERIMPL_H__