os/security/cryptoplugins/cryptospiplugins/test/h4drv/crypto_h4_plugin/h4cipherimpl.h
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 #ifndef	__SYMMETRICCIPHERIMPL_H__
    25 #define	__SYMMETRICCIPHERIMPL_H__
    26 
    27 #include <e32base.h>
    28 #include <e32cmn.h>
    29 #include <cryptospi/cryptospidef.h>
    30 #include <padding.h>
    31 #include "symmetriccipherplugin.h"
    32 
    33 
    34 #ifdef __MARM__
    35 #define __NOTSHARED __declspec(notshared)
    36 #else
    37 #define __NOTSHARED
    38 #endif
    39 
    40 /** The maximum block size supported (in bytes) */
    41 const TUint KMaxBlockSizeSupported = 32;
    42 
    43 /**
    44 Abstract base class for symmetric cipher plug-ins.
    45 */
    46 namespace HwCrypto
    47 	{
    48 	using namespace CryptoSpi;
    49 		
    50 	class __NOTSHARED CH4CipherImpl : public CBase, public MSymmetricCipher
    51 		{
    52 	public:
    53 		/**
    54 		Implemented by each cipher subclass to determine whether the
    55 		specified key length is valid for that cipher.
    56 		This is called by ConstructL and SetKeyL
    57 		@param aKeyLength The key length in bytes to verify.
    58 		*/
    59 		virtual TBool IsValidKeyLength(TInt aKeyBytes) const = 0;
    60 		
    61 		/**
    62 		Helper function implemented by concrete cipher sub-class that 
    63 		allows GetCharacteristicsL to return the correct characteristics object.
    64 		@return The implemention uid
    65 		*/
    66 		virtual TUid ImplementationUid() const = 0;
    67 		
    68 		/**
    69 		Gets the strength of the current key, needed to check whether the cipher
    70 		may operate if strong cryptography is not enabled.
    71 		@return The strength of the current key
    72 		*/
    73 		virtual TInt GetKeyStrength() const;
    74 		
    75 				
    76 		// Override MPlugin virtual functions
    77 		void Close();
    78 		TAny* GetExtension(TUid aExtensionId);
    79 
    80 		void GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics);	
    81 
    82 
    83 		void Reset(); // Always call reset in super-class if you override this
    84 		// End of MPlugin virtual functions
    85 		
    86 		// Override MSymmetricCipherBase virtual functions 
    87 		TInt KeySize() const;
    88 		virtual void SetKeyL(const CKey& aKey);
    89 
    90 		TInt BlockSize() const;
    91 		void SetCryptoModeL(TUid aCryptoMode);
    92 		void SetOperationModeL(TUid aOperationMode);
    93 		void SetPaddingModeL(TUid aPaddingMode);
    94 		void SetIvL(const TDesC8& aIv);
    95 		
    96 		TInt MaxOutputLength(TInt aInputLength) const;
    97 		TInt MaxFinalOutputLength(TInt aInputLength) const;
    98 
    99 		void ProcessL(const TDesC8& aInput, TDes8& aOutput);
   100 		void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);						
   101 		// End of MSymmetricCipherBase
   102 						
   103 		/// Destructor
   104 		~CH4CipherImpl();
   105 
   106 	protected:
   107 		
   108 		/**
   109 		Constructor
   110 		@param aBlockBytes The block size in bytes
   111 		@param aOperationMode The mode of operation e.g. CBC
   112 		@param aCryptoMode Whether to encrypt or decrypt
   113 		*/
   114 		CH4CipherImpl(TUint8 aBlockBytes,
   115 					  TUid aCryptoMode,
   116 					  TUid aOperationMode,
   117 					  TUid aPaddingMode);
   118 		
   119 		/**
   120 		Second phase of construction. Always call ConstructL in the super-class
   121 		if your override this method.
   122 		
   123 		@param aKey The key to initialise the cipher with.
   124 		*/
   125 		virtual void ConstructL(const CKey& aKey);		
   126 		
   127 		/**
   128 		Creates the extended characteristics object. Concrete plug-in classes
   129 		may override this to customise the object returned by GetCharacteristics();
   130 		
   131 		The default configuration is that  plug-ins have unlimited concurrency, 
   132 		cannot be reserved for exclusive use and are not CERTIFIED to be standards compliant.		
   133 		@return The extended characteristics object
   134 		*/
   135 		virtual CExtendedCharacteristics* CreateExtendedCharacteristicsL();		
   136 		
   137 		/**
   138 		Extracts the raw symmetric key from a generic key object. The buffer
   139 		is placed on the cleanup stack.
   140 		
   141 		@param aKey The key object
   142 		@return A buffer containing the raw key value
   143 		*/
   144 		HBufC8* ExtractKeyDataLC(const CKey& aKey) const;	
   145 	
   146 
   147 		/**
   148 		   DoSetDetails
   149 
   150 		   Reconfigure h/w based on iCryptoMode/iOperationMode/iKey/iIv
   151 
   152 		   Implemented by each cipher subclass.
   153 		*/
   154 		virtual void DoSetupL() = 0;
   155 
   156 		/**
   157 		   DoWrite
   158 
   159 		   Blocking write of data to be transformed. Need not be a
   160 		   multiple of the block size.
   161 
   162 		   Implemented by each cipher subclass.
   163 		*/
   164 		virtual void DoWriteL(const TUint8* aBuffer, TUint aNumBytes) = 0;
   165 		
   166 		/**
   167 		   DoRead		   
   168 
   169 		   Blocking read of transformed data.
   170 
   171 		   Need not be a multiple of the block size, but caller should
   172 		   ensure enough blocks of data have been written to satisfy
   173 		   the request.
   174 
   175 		   The data is appended to the supplied descriptor.
   176 
   177 		   Implemented by each cipher subclass.
   178 		*/
   179 		virtual void DoReadL(TDes8 &aBuffer, TUint32 aLength) = 0;
   180 			
   181 			
   182 	protected:
   183 
   184 		/// the key, extracted from a CKey object
   185 		HBufC8* iKey;
   186 		
   187 		/// key size in bytes
   188 		TUint iKeyBytes;
   189 		
   190 		/// Standards conformance information.
   191 		RArray<TUid> iStandardsConformance;
   192 
   193 		/// block size in bytes, current largest block size is 16 bytes (AES)
   194 		TUint8 iBlockBytes;	
   195 		/// encryption or decryption
   196 		TUid iCryptoMode;		
   197 		/// The block cipher mode e.g. ECB, CBC
   198 		TUid iOperationMode;
   199 		/// the current padding scheme
   200 		TUid iPaddingMode;
   201 		
   202 		/// the initialisation vector
   203 		RBuf8 iIv;
   204 		
   205 		/// current padding scheme implementation
   206 		CPadding* iPadding;
   207 		/// buffer to store blocks
   208 		RBuf8 iPartialBlock;
   209 		/// buffer to store input / output of padding
   210 		RBuf8 iPaddingBlock;
   211 		
   212 	private:
   213 		TBool iNeedToSetupHw;
   214 		};
   215 
   216 			
   217 	}
   218 
   219 #endif	//	__SYMMETRICCIPHERIMPL_H__