crypto/weakcryptospi/examples/mac_api_usage/t_mac_example.cpp
changeset 8 35751d3474b7
child 43 2f10d260163b
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     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 * Example Code for showing usage of MAC APIs. 
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalTechnology
       
    23  @released
       
    24 */
       
    25 
       
    26 #include <cryptospi/cryptospidef.h>
       
    27 #include <cryptospi/cryptomacapi.h>
       
    28 #include <cryptospi/cryptoparams.h>
       
    29 #include "keys.h"
       
    30 #include <f32file.h>
       
    31 #include "utf.h" 
       
    32 
       
    33 using namespace CryptoSpi;
       
    34 
       
    35 
       
    36 const TUint8 KMacBlockSize = 16;                                                               
       
    37 
       
    38 const TUint8 key[]  = 	{
       
    39 						 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       
    40 						 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
       
    41 						};
       
    42 
       
    43 const TUint8 data[] = 	{
       
    44 						 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
       
    45 						 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
       
    46 						};
       
    47 
       
    48 
       
    49 #define __CLEAN_UP 	logBuffer = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*macResult); \
       
    50 					CleanupStack::PopAndDestroy(macResult);  \
       
    51 					CleanupStack::PushL(logBuffer); \
       
    52 					User::LeaveIfError(log.Write(*logBuffer)); \
       
    53 					CleanupStack::PopAndDestroy(logBuffer);  
       
    54 
       
    55 
       
    56 class TVariantPtrC : public TPtrC
       
    57 	{
       
    58 	public:
       
    59 	HBufC* HexStringLC();
       
    60 	};                                  
       
    61 
       
    62 HBufC* TVariantPtrC::HexStringLC()
       
    63 	{
       
    64 	HBufC* parsedString = HBufC::NewLC(this->Length()*2);
       
    65 	TBuf<2> pair;
       
    66 	_LIT(KFormat2,"%02x");                                    
       
    67 						    
       
    68    	for(TInt i = 0; i<this->Length() ; ++i)
       
    69     	{
       
    70 		TInt num  = (*this)[i];
       
    71     	pair.Format(KFormat2,num);
       
    72     	parsedString->Des().Append(pair);
       
    73     	}
       
    74 	    
       
    75 	return parsedString;
       
    76 	}
       
    77 
       
    78 void EvaluateMacL(const TDesC8& aKey, const TDesC8& aSourceData)
       
    79 	{
       
    80 	//-----------------------------CREATION-OF-KEY-AND-MAC-INSTANCE--------------------------
       
    81 
       
    82 	// Set the key
       
    83 	CryptoSpi::TKeyProperty keyProperty = {CryptoSpi::KAesUid, KNullUid, CryptoSpi::KSymmetricKey, CryptoSpi::KNonEmbeddedKeyUid};
       
    84 	CCryptoParams* keyParam =CCryptoParams::NewLC();
       
    85 	keyParam->AddL(aKey, CryptoSpi::KSymmetricKeyParameterUid);
       
    86 	CKey* uniKey=CKey::NewLC(keyProperty, *keyParam);
       
    87 	
       
    88 	//Retrieve a Synchronous MAC Factory Object and use AES-XCBC-MAC-96 or any other MAC algorithm.
       
    89 	CMac* macImpl = NULL;
       
    90 	CMacFactory::CreateMacL(macImpl, CryptoSpi::KAesXcbcMac96Uid,*uniKey, NULL);
       
    91 
       
    92 	//cleanup uniKey and keyParam.
       
    93 	CleanupStack::PopAndDestroy(2,keyParam);
       
    94 	CleanupStack::PushL(macImpl);
       
    95 	
       
    96 	//---------------------------------EVALUATION-OF-MAC-VALUE---------------------------------
       
    97 	
       
    98 	// Usage of Mac APIs: MacL, UpdateL, and FinalL 
       
    99 	
       
   100 	//Retrieve the 8bit mac value
       
   101 	HBufC* macValueIntermediate = HBufC::NewLC(KMacBlockSize);
       
   102 	HBufC* macValueFinal        = HBufC::NewLC(KMacBlockSize);
       
   103 
       
   104 	TPtr macPtrIntermediate = macValueIntermediate->Des();
       
   105 	TPtr macPtrFinal 		= macValueFinal->Des();
       
   106 	
       
   107 	//Retrieve the final 8bit mac value and convert to 16bit
       
   108 	macPtrIntermediate.Copy(macImpl->MacL(aSourceData.Mid(0,3)));
       
   109 	macImpl->UpdateL(aSourceData.Mid(3,5));
       
   110 	macPtrFinal.Copy(macImpl->FinalL(aSourceData.Mid(8,8)));
       
   111 
       
   112 	//---------------------------------WRITE-RESULTS-TO-LOG--------------------------
       
   113 
       
   114 	// Add log file for automated test environment
       
   115 	RFs fs;
       
   116 	User::LeaveIfError(fs.Connect());
       
   117 	CleanupClosePushL(fs);
       
   118 	RFile log;
       
   119 	CleanupClosePushL(log);
       
   120 	User::LeaveIfError(log.Replace(fs, _L("c:\\t_mac_apis.log"), EFileShareAny|EFileWrite));
       
   121 	User::LeaveIfError(log.Write(_L8("\n*** Key Used for Mac Evaluation (in hex) is = 0x000102030405060708090a0b0c0d0e0f ")));
       
   122 
       
   123 	
       
   124 	//Take the 16bit descriptor and convert the string to hexadecimal
       
   125 	TVariantPtrC convertMac;
       
   126 	convertMac.Set(macPtrIntermediate);
       
   127 	HBufC* macResult = convertMac.HexStringLC();	
       
   128 	HBufC8* logBuffer = NULL;
       
   129 	
       
   130 	User::LeaveIfError(log.Write(_L8("\n*** Mac value for hex data 0x000102 is = ")));
       
   131 	
       
   132 	// cleanup macResult and logBuffer
       
   133 	__CLEAN_UP;
       
   134 	
       
   135 	convertMac.Set(macPtrFinal);
       
   136 	macResult = convertMac.HexStringLC();
       
   137 	
       
   138 	User::LeaveIfError(log.Write(_L8("\n*** Mac value for hex data 0x000102030405060708090a0b0c0d0e0f is = ")));
       
   139 	
       
   140 	// cleanup macResult and logBuffer
       
   141 	__CLEAN_UP
       
   142 	
       
   143 	User::LeaveIfError(log.Write(_L8("\n\n 0 tests failed out of 1 \t\n")));
       
   144 	//------------------------------------------CLEANUP------------------------------------------
       
   145 
       
   146 	//cleanup log, fs, macValueFinal, macValueIntermediate and macImpl
       
   147 	CleanupStack::PopAndDestroy(5, macImpl);
       
   148 	}
       
   149 
       
   150 void MainL()
       
   151 	{
       
   152 	const TPtrC8 Key(key, KMacBlockSize);
       
   153 	const TPtrC8 Data(data, KMacBlockSize);
       
   154 
       
   155 	// pass the key and the source data for MAC evaluation
       
   156 	__UHEAP_MARK;
       
   157 		EvaluateMacL(Key,Data);
       
   158 	__UHEAP_MARKEND;
       
   159 	}
       
   160 
       
   161 TInt E32Main()
       
   162 	{
       
   163  	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   164  	if (cleanupStack == NULL)
       
   165  		{
       
   166  		return KErrNoMemory;
       
   167  		}
       
   168  	TRAP_IGNORE(MainL());
       
   169  	delete cleanupStack;
       
   170  	return KErrNone;
       
   171 	}