epoc32/include/cryptospi/cryptomacapi.h
branchSymbian3
changeset 4 837f303aceeb
equal deleted inserted replaced
3:e1b950c65cb4 4:837f303aceeb
       
     1 /*
       
     2 * Copyright (c) 2008-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 * crypto MAC application interface 
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @publishedAll
       
    23  @released
       
    24 */
       
    25 
       
    26 #ifndef __CRYPTOAPI_MACAPI_H__
       
    27 #define __CRYPTOAPI_MACAPI_H__
       
    28 
       
    29 #include <e32base.h>
       
    30 #include <cryptospi/cryptobaseapi.h>
       
    31 
       
    32 
       
    33 namespace CryptoSpi
       
    34 	{
       
    35 	class MPlugin;
       
    36 	class CCryptoParams;
       
    37 	class CKey;
       
    38 	class MMac;
       
    39 	class MAsyncMac;
       
    40 	
       
    41 	
       
    42 	/**
       
    43 	 * Mac API, which wraps a synchronous Mac plugin implementation
       
    44 	 * This Mac interface helps the client application to get the message 
       
    45 	 * authentication code value of a given message which provides
       
    46 	 * data integrity and data origin authentication. These two goals are 
       
    47 	 * dependent upon the scope of the distribution of the secret key.
       
    48 	 */
       
    49 	
       
    50 	NONSHARABLE_CLASS(CMac) : public CCryptoBase
       
    51 		{
       
    52 	public:
       
    53 		/**
       
    54 		 * @internalComponent
       
    55 		 * Create a CMac instance from the given MMac instance
       
    56 		 *
       
    57 		 * @param aMac  	The mac plugin instance
       
    58 		 * @param aHandle	The current plugin DLL loaded.
       
    59 		 * @return      	pointer to a CMac instance
       
    60 		 */
       
    61 		static CMac* NewL(MMac* aMac, TInt aHandle);
       
    62 		
       
    63 		/**
       
    64 		 * Adds message to the internal representation of data for which the MAC value
       
    65 		 * needs to be evaluated and then returns a TPtrC8 of the finalised MAC value 
       
    66 		 * of all the previously appended messages. 
       
    67 		 * 
       
    68 		 * @param aMessage  The data for which MAC value is to be evaluated.
       
    69 		 * @return          A descriptor pointer to the buffer containing the
       
    70 		 *                  resulting MAC value.
       
    71 		 * @leave ...		Any of the crypto error codes defined in 
       
    72   							cryptospi_errs.h or any of the system-wide error codes.
       
    73 		 */
       
    74 		IMPORT_C TPtrC8 MacL(const TDesC8& aMessage);    
       
    75 		
       
    76         /**
       
    77          * Adds data to the internal representation of messages for which the MAC value
       
    78 		 * needs to be evaluated.
       
    79          * 
       
    80          * @param aMessage	The data to be included in the MAC evaluation.
       
    81 		 * @leave ...		Any of the crypto error codes defined in 
       
    82   							cryptospi_errs.h or any of the system-wide error codes.
       
    83          */
       
    84 		IMPORT_C void UpdateL(const TDesC8& aMessage);
       
    85 
       
    86         /**
       
    87          * Produces a final MAC value from all the previous updates of data to be MACed. 
       
    88          * It resets the MAC algorithm in a state similar to creating a new MAC instance
       
    89          * with the same underlying algorithm and supplied symmetric key.
       
    90          *   
       
    91          * @param aMessage	The data to be included in the MAC evaluation.
       
    92 		 * @return          A descriptor pointer to the buffer containing the
       
    93 		 *                  resulting MAC value.
       
    94 		 * @leave ...		Any of the crypto error codes defined in 
       
    95   							cryptospi_errs.h or any of the system-wide error codes.
       
    96 		 */
       
    97 		IMPORT_C TPtrC8 FinalL(const TDesC8& aMessage);
       
    98 
       
    99 		/**
       
   100 		 * This re-initialises the underlying MAC algorithm with a new symmetric key. 
       
   101          * It resets the MAC algorithm in a state similar to creating a new MAC instance
       
   102          * with the same underlying algorithm but a new symmetric key.
       
   103 		 *
       
   104 		 * @param aKey  Symmetric key for calculating message authentication code value. 
       
   105 		 * @leave ...		Any of the crypto error codes defined in 
       
   106   							cryptospi_errs.h or any of the system-wide error codes.
       
   107 		 */
       
   108 		IMPORT_C void ReInitialiseAndSetKeyL(const CKey& aKey);    
       
   109 		
       
   110 		/**
       
   111 		 * Creates a brand new reset CMac object containing no state
       
   112 		 * information from the current object.  
       
   113 		 * 
       
   114 		 * @return 	A pointer to the new reset CMac object
       
   115 		 * @leave ...		Any of the crypto error codes defined in 
       
   116   							cryptospi_errs.h or any of the system-wide error codes.
       
   117 		 */
       
   118 		IMPORT_C CMac* ReplicateL();		
       
   119 
       
   120 		/** 
       
   121 		 * Creates a new CMac object with the exact same state as
       
   122 		 * the current object.  
       
   123  		 * This function copies all internal state of the message digest.
       
   124 		 * 
       
   125 		 * @return 	A pointer to the new CMac object
       
   126 		 * @leave ...		Any of the crypto error codes defined in 
       
   127   							cryptospi_errs.h or any of the system-wide error codes.
       
   128 		 */
       
   129 		IMPORT_C CMac* CopyL();
       
   130 
       
   131 		/**
       
   132 		 * This destructor is exported so that the client application after using
       
   133 		 * a specific plug-in implementation can destroy its instance. Both the framework 
       
   134 		 * and the plug-in use/derived from the same interface 'MPlugin. 
       
   135 		 * By exporting the destructor we are destroying the plug-in instance at
       
   136 		 * client side via the CryptoSPI framework.
       
   137 		 */
       
   138 		IMPORT_C ~CMac();
       
   139 		
       
   140 	private:
       
   141 		/**
       
   142 		 * The constructor of this class is private. An user application will use
       
   143 		 * CMacFactory::CreateMacL for the object's initialisation.
       
   144 		 * 
       
   145 		 * @param aMac 		The mac plug-in instance
       
   146 		 * @param aHandle   The current plug-in DLL loaded
       
   147 		 */
       
   148 		CMac(MMac* aMac, TInt aHandle);
       
   149 		};
       
   150 
       
   151 	
       
   152 	/**
       
   153 	 * This is the asynchronous version of CMac class typically used by the 
       
   154 	 * client applications if hardware plug-in implementation of 
       
   155 	 * the MAC interface is present. 	
       
   156 	 */
       
   157 		
       
   158 	NONSHARABLE_CLASS(CAsyncMac) : public CCryptoBase
       
   159 		{
       
   160 	public:
       
   161 		/**
       
   162 		 * @internalComponent
       
   163 		 * Create a CAsyncMac instance from the given MAsyncMac instance
       
   164 		 *
       
   165 		 * @param aMac  The mac plugin instance
       
   166 		 * @param aHandle	The current plugin DLL loaded.
       
   167 		 * @return      pointer to a CMac instance
       
   168 		 */
       
   169 		static CAsyncMac* NewL(MAsyncMac* aMac, TInt aHandle);
       
   170 			
       
   171 		/**
       
   172 		 * Adds message to the internal representation of data for which the MAC value
       
   173 		 * needs to be evaluated and then returns a TPtrC8 of the finalised MAC value 
       
   174 		 * of all the previously appended messages. 
       
   175 		 * 
       
   176 		 * @param aMessage  The data for which MAC value is to be evaluated.
       
   177 		 * @param aStatus   Holds the completion status of an asynchronous
       
   178 		 * 					request for MAC evaluation.
       
   179 		 * @return          A descriptor pointer to the buffer containing the
       
   180 		 *                  resulting MAC value.
       
   181 		 * @leave ...		Any of the crypto error codes defined in 
       
   182   							cryptospi_errs.h or any of the system-wide error codes.
       
   183 		 */
       
   184 		IMPORT_C TPtrC8 MacL(const TDesC8& aMessage, TRequestStatus& aStatus);    
       
   185 			
       
   186         /**
       
   187          * Adds data to the internal representation of messages for which the MAC value
       
   188 		 * needs to be evaluated.
       
   189          * 
       
   190          * @param aMessage	The data to be included in the MAC evaluation.
       
   191 		 * @param aStatus   Holds the completion status of an asynchronous
       
   192 		 * 					request for MAC evaluation.
       
   193 		 * @leave ...		Any of the crypto error codes defined in 
       
   194   							cryptospi_errs.h or any of the system-wide error codes.
       
   195          */
       
   196 		IMPORT_C void UpdateL(const TDesC8& aMessage, TRequestStatus& aStatus);
       
   197 
       
   198 		/**
       
   199          * Produces a final MAC value from all the previous updates of data to be MACed. 
       
   200          * It resets the MAC algorithm in a state similar to creating a new MAC instance
       
   201          * with the same underlying algorithm and supplied symmetric key.
       
   202          *  
       
   203          * @param aMessage	The data to be included in the MAC evaluation.
       
   204 		 * @param aStatus   Holds the completion status of an asynchronous
       
   205 		 * 					request for MAC evaluation.
       
   206 		 * @return          A descriptor pointer to the buffer containing the
       
   207 		 *                  resulting MAC value.
       
   208 		 * @leave ...		Any of the crypto error codes defined in 
       
   209   							cryptospi_errs.h or any of the system-wide error codes.
       
   210 		 */
       
   211 		IMPORT_C TPtrC8 FinalL(const TDesC8& aMessage, TRequestStatus& aStatus);
       
   212 
       
   213 		/**
       
   214 		 * This re-initialises the underlying MAC algorithm with a new symmetric key. 
       
   215          * It resets the MAC algorithm in a state similar to creating a new MAC instance
       
   216          * with the same underlying algorithm but a new symmetric key.
       
   217 		 *
       
   218 		 * @param aKey  Symmetric key for calculating message authentication code value. 
       
   219 		 * @leave ...		Any of the crypto error codes defined in 
       
   220   							cryptospi_errs.h or any of the system-wide error codes.
       
   221 		 */
       
   222 		IMPORT_C void ReInitialiseAndSetKeyL(const CKey& aKey);    
       
   223 		
       
   224 		/**
       
   225 		 * Creates a brand new reset CAsyncMac object containing no state
       
   226 		 * information from the current object.  
       
   227 		 * 
       
   228 		 * @return	A pointer to the new reset CAsyncMac object
       
   229 		 * @leave ...		Any of the crypto error codes defined in 
       
   230   							cryptospi_errs.h or any of the system-wide error codes.
       
   231 		 */
       
   232 		IMPORT_C CAsyncMac* ReplicateL();		
       
   233 
       
   234 		/** 
       
   235 		 * Creates a new CAsyncMac object with the exact same state as
       
   236 		 * the current object.  
       
   237 		 * This function copies all internal state of the message digest.
       
   238 		 * 
       
   239 		 * @return	A pointer to the new CAsyncHash object
       
   240 		 * @leave ...		Any of the crypto error codes defined in 
       
   241   							cryptospi_errs.h or any of the system-wide error codes.
       
   242 		 */
       
   243 		IMPORT_C CAsyncMac* CopyL();
       
   244 		
       
   245 		/**
       
   246 		 * Cancels an outstanding request from the client.
       
   247 		 */
       
   248 		IMPORT_C void Cancel();
       
   249 
       
   250 		/**
       
   251 		 * This destructor is exported so that the client application after using
       
   252 		 * a specific plug-in implementation can destroy its instance. Both the framework 
       
   253 		 * and the plug-in use/derived from the same interface 'MPlugin. 
       
   254 		 * By exporting the destructor we are destroying the plug-in instance at
       
   255 		 * client side via the CryptoSPI framework.
       
   256 		 */
       
   257 		IMPORT_C ~CAsyncMac();
       
   258 			
       
   259 	private:
       
   260 		/**
       
   261 		 * The constructor of this class is private. An user application will use
       
   262 		 * CMacFactory::CreateAsyncMacL for the object's initialisation.
       
   263 		 * 
       
   264 		 * @param aMac 		The mac plug-in instance
       
   265 		 * @param aHandle   The current plug-in DLL loaded
       
   266 		 */
       
   267 		CAsyncMac(MAsyncMac* aMac, TInt aHandle);
       
   268 		};
       
   269 
       
   270 		
       
   271 	/**
       
   272 	 * The Factory to create synchronous and asynchronous Mac instances
       
   273 	 */
       
   274 		
       
   275 	class CMacFactory
       
   276 		{
       
   277 	public:
       
   278 
       
   279 		/**
       
   280 		 * Create a CMac instance (for software based MAC plug-in dll implementation)
       
   281 		 *
       
   282 		 * @param aMac           	The pointer to CMac. This will be initialised with 
       
   283 		 * 						  	the plug-in implementation of the desired MAC algorithm.
       
   284 		 * @param aAlgorithmUid  	The specific MAC algorithm desired for evaluation of MAC value.
       
   285 		 *                       	e.g. MD2, SHA1 or AES-XCBC-MAC-96, AES-XCBC-PRF-128
       
   286 		 * @param aKey           	Symmetric key for calculating message authentication code value. 
       
   287 		 * @param aAlgorithmParams  The parameters those are specific to a particular MAC algorithm.
       
   288 		 * 							This is for extendibility and will normally be null.                     	
       
   289 		 * @leave               	KErrNone if successful; otherwise, leaves with a system wide error code.
       
   290 		 */
       
   291 		IMPORT_C static void CreateMacL(CMac*& aMac,
       
   292 										const TUid aAlgorithmUid,
       
   293 										const CKey& aKey,
       
   294 										const CCryptoParams* aAlgorithmParams);
       
   295 		
       
   296 		/**
       
   297 		 * Create a CAsyncMac instance (for hardware based MAC plug-in dll implementation)
       
   298 		 *
       
   299 		 * @param aMac           	The pointer to CMac. This will be initialised with 
       
   300 		 * 						  	the plug-in implementation of the desired MAC algorithm.
       
   301 		 * @param aAlgorithmUid  	The specific MAC algorithm desired for evaluation of MAC value.
       
   302 		 *                       	e.g. MD2, SHA1 or AES-XCBC-MAC-96, AES-XCBC-PRF-128
       
   303 		 * @param aKey           	Symmetric key for calculating message authentication code value. 
       
   304 		 * @param aAlgorithmParams  The parameters those are specific to a particular MAC algorithm.
       
   305 		 * 							This is for extendibility and will normally be null.                     	
       
   306 		 * @leave               	KErrNone if successful; otherwise, leaves with a system wide error code.
       
   307 		 */
       
   308 		IMPORT_C static void CreateAsyncMacL(CAsyncMac*& aMac,
       
   309 										const TUid aAlgorithmUid,
       
   310 										const CKey& aKey,
       
   311 										const CCryptoParams* aAlgorithmParams);
       
   312 	
       
   313 	private:
       
   314 		/**
       
   315 		 * The class is used as a static class since there is no data 
       
   316 		 * or behaviour in the class that depends on object identity.
       
   317 		 * Therefore the default constructor of this class is made private. 
       
   318 		 */
       
   319 		CMacFactory();	
       
   320 		};
       
   321 		
       
   322 	}
       
   323 
       
   324 #endif //__CRYPTOAPI_MACAPI_H__