os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/h4cipherimpl.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/h4cipherimpl.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,219 @@
     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 +#ifndef	__SYMMETRICCIPHERIMPL_H__
    1.28 +#define	__SYMMETRICCIPHERIMPL_H__
    1.29 +
    1.30 +#include <e32base.h>
    1.31 +#include <e32cmn.h>
    1.32 +#include <cryptospi/cryptospidef.h>
    1.33 +#include <padding.h>
    1.34 +#include "symmetriccipherplugin.h"
    1.35 +
    1.36 +
    1.37 +#ifdef __MARM__
    1.38 +#define __NOTSHARED __declspec(notshared)
    1.39 +#else
    1.40 +#define __NOTSHARED
    1.41 +#endif
    1.42 +
    1.43 +/** The maximum block size supported (in bytes) */
    1.44 +const TUint KMaxBlockSizeSupported = 32;
    1.45 +
    1.46 +/**
    1.47 +Abstract base class for symmetric cipher plug-ins.
    1.48 +*/
    1.49 +namespace HwCrypto
    1.50 +	{
    1.51 +	using namespace CryptoSpi;
    1.52 +		
    1.53 +	class __NOTSHARED CH4CipherImpl : public CBase, public MSymmetricCipher
    1.54 +		{
    1.55 +	public:
    1.56 +		/**
    1.57 +		Implemented by each cipher subclass to determine whether the
    1.58 +		specified key length is valid for that cipher.
    1.59 +		This is called by ConstructL and SetKeyL
    1.60 +		@param aKeyLength The key length in bytes to verify.
    1.61 +		*/
    1.62 +		virtual TBool IsValidKeyLength(TInt aKeyBytes) const = 0;
    1.63 +		
    1.64 +		/**
    1.65 +		Helper function implemented by concrete cipher sub-class that 
    1.66 +		allows GetCharacteristicsL to return the correct characteristics object.
    1.67 +		@return The implemention uid
    1.68 +		*/
    1.69 +		virtual TUid ImplementationUid() const = 0;
    1.70 +		
    1.71 +		/**
    1.72 +		Gets the strength of the current key, needed to check whether the cipher
    1.73 +		may operate if strong cryptography is not enabled.
    1.74 +		@return The strength of the current key
    1.75 +		*/
    1.76 +		virtual TInt GetKeyStrength() const;
    1.77 +		
    1.78 +				
    1.79 +		// Override MPlugin virtual functions
    1.80 +		void Close();
    1.81 +		TAny* GetExtension(TUid aExtensionId);
    1.82 +
    1.83 +		void GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics);	
    1.84 +
    1.85 +
    1.86 +		void Reset(); // Always call reset in super-class if you override this
    1.87 +		// End of MPlugin virtual functions
    1.88 +		
    1.89 +		// Override MSymmetricCipherBase virtual functions 
    1.90 +		TInt KeySize() const;
    1.91 +		virtual void SetKeyL(const CKey& aKey);
    1.92 +
    1.93 +		TInt BlockSize() const;
    1.94 +		void SetCryptoModeL(TUid aCryptoMode);
    1.95 +		void SetOperationModeL(TUid aOperationMode);
    1.96 +		void SetPaddingModeL(TUid aPaddingMode);
    1.97 +		void SetIvL(const TDesC8& aIv);
    1.98 +		
    1.99 +		TInt MaxOutputLength(TInt aInputLength) const;
   1.100 +		TInt MaxFinalOutputLength(TInt aInputLength) const;
   1.101 +
   1.102 +		void ProcessL(const TDesC8& aInput, TDes8& aOutput);
   1.103 +		void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);						
   1.104 +		// End of MSymmetricCipherBase
   1.105 +						
   1.106 +		/// Destructor
   1.107 +		~CH4CipherImpl();
   1.108 +
   1.109 +	protected:
   1.110 +		
   1.111 +		/**
   1.112 +		Constructor
   1.113 +		@param aBlockBytes The block size in bytes
   1.114 +		@param aOperationMode The mode of operation e.g. CBC
   1.115 +		@param aCryptoMode Whether to encrypt or decrypt
   1.116 +		*/
   1.117 +		CH4CipherImpl(TUint8 aBlockBytes,
   1.118 +					  TUid aCryptoMode,
   1.119 +					  TUid aOperationMode,
   1.120 +					  TUid aPaddingMode);
   1.121 +		
   1.122 +		/**
   1.123 +		Second phase of construction. Always call ConstructL in the super-class
   1.124 +		if your override this method.
   1.125 +		
   1.126 +		@param aKey The key to initialise the cipher with.
   1.127 +		*/
   1.128 +		virtual void ConstructL(const CKey& aKey);		
   1.129 +		
   1.130 +		/**
   1.131 +		Creates the extended characteristics object. Concrete plug-in classes
   1.132 +		may override this to customise the object returned by GetCharacteristics();
   1.133 +		
   1.134 +		The default configuration is that  plug-ins have unlimited concurrency, 
   1.135 +		cannot be reserved for exclusive use and are not CERTIFIED to be standards compliant.		
   1.136 +		@return The extended characteristics object
   1.137 +		*/
   1.138 +		virtual CExtendedCharacteristics* CreateExtendedCharacteristicsL();		
   1.139 +		
   1.140 +		/**
   1.141 +		Extracts the raw symmetric key from a generic key object. The buffer
   1.142 +		is placed on the cleanup stack.
   1.143 +		
   1.144 +		@param aKey The key object
   1.145 +		@return A buffer containing the raw key value
   1.146 +		*/
   1.147 +		HBufC8* ExtractKeyDataLC(const CKey& aKey) const;	
   1.148 +	
   1.149 +
   1.150 +		/**
   1.151 +		   DoSetDetails
   1.152 +
   1.153 +		   Reconfigure h/w based on iCryptoMode/iOperationMode/iKey/iIv
   1.154 +
   1.155 +		   Implemented by each cipher subclass.
   1.156 +		*/
   1.157 +		virtual void DoSetupL() = 0;
   1.158 +
   1.159 +		/**
   1.160 +		   DoWrite
   1.161 +
   1.162 +		   Blocking write of data to be transformed. Need not be a
   1.163 +		   multiple of the block size.
   1.164 +
   1.165 +		   Implemented by each cipher subclass.
   1.166 +		*/
   1.167 +		virtual void DoWriteL(const TUint8* aBuffer, TUint aNumBytes) = 0;
   1.168 +		
   1.169 +		/**
   1.170 +		   DoRead		   
   1.171 +
   1.172 +		   Blocking read of transformed data.
   1.173 +
   1.174 +		   Need not be a multiple of the block size, but caller should
   1.175 +		   ensure enough blocks of data have been written to satisfy
   1.176 +		   the request.
   1.177 +
   1.178 +		   The data is appended to the supplied descriptor.
   1.179 +
   1.180 +		   Implemented by each cipher subclass.
   1.181 +		*/
   1.182 +		virtual void DoReadL(TDes8 &aBuffer, TUint32 aLength) = 0;
   1.183 +			
   1.184 +			
   1.185 +	protected:
   1.186 +
   1.187 +		/// the key, extracted from a CKey object
   1.188 +		HBufC8* iKey;
   1.189 +		
   1.190 +		/// key size in bytes
   1.191 +		TUint iKeyBytes;
   1.192 +		
   1.193 +		/// Standards conformance information.
   1.194 +		RArray<TUid> iStandardsConformance;
   1.195 +
   1.196 +		/// block size in bytes, current largest block size is 16 bytes (AES)
   1.197 +		TUint8 iBlockBytes;	
   1.198 +		/// encryption or decryption
   1.199 +		TUid iCryptoMode;		
   1.200 +		/// The block cipher mode e.g. ECB, CBC
   1.201 +		TUid iOperationMode;
   1.202 +		/// the current padding scheme
   1.203 +		TUid iPaddingMode;
   1.204 +		
   1.205 +		/// the initialisation vector
   1.206 +		RBuf8 iIv;
   1.207 +		
   1.208 +		/// current padding scheme implementation
   1.209 +		CPadding* iPadding;
   1.210 +		/// buffer to store blocks
   1.211 +		RBuf8 iPartialBlock;
   1.212 +		/// buffer to store input / output of padding
   1.213 +		RBuf8 iPaddingBlock;
   1.214 +		
   1.215 +	private:
   1.216 +		TBool iNeedToSetupHw;
   1.217 +		};
   1.218 +
   1.219 +			
   1.220 +	}
   1.221 +
   1.222 +#endif	//	__SYMMETRICCIPHERIMPL_H__