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