rtp/srtpstack/src/srtpcryptohandlersrtcp.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2004 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 "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 
       
    21 // INCLUDES
       
    22 #include <e32std.h>
       
    23 #include <e32math.h>
       
    24 #include "srtpcryptohandler.h"
       
    25 #include "srtpcryptohandlersrtcp.h"
       
    26 #include "srtppacketrtcp.h"
       
    27 #include "srtppacketsrtcp.h"
       
    28 #include "msrtpcryptohandlercontext.h"
       
    29 
       
    30 #include "srtputils.h"
       
    31 #include "srtpmasterkey.h"
       
    32 #include "srtpmastersalt.h"
       
    33 #include "msrtpkeyderivation.h"
       
    34 #include "msrtpcipher.h"
       
    35 #include "srtpstream.h"
       
    36 #include "srtpcryptoparams.h"
       
    37 #include "srtpcipher_aescm128.h"
       
    38 #include "srtpcipher_null.h"
       
    39 #include "srtpauthentication_hmac_sha1.h"
       
    40 #include "srtpauthentication_null.h"
       
    41 #include "srtpauthentication_rcc.h"
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CSRTPCryptoHandlerSRTCP::CSRTPCryptoHandlerSRTCP
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CSRTPCryptoHandlerSRTCP::CSRTPCryptoHandlerSRTCP( CSRTPStream& aStream)
       
    48     : CSRTPCryptoHandler(aStream)
       
    49             {}
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // Two-phased constructor. 
       
    53 // 
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CSRTPCryptoHandlerSRTCP* CSRTPCryptoHandlerSRTCP::NewL(CSRTPStream& aStream)
       
    57     {
       
    58     CSRTPCryptoHandlerSRTCP* self = CSRTPCryptoHandlerSRTCP::NewLC( aStream);
       
    59     CleanupStack::Pop( self );
       
    60     return self;    
       
    61     }
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CSRTPCryptoHandlerSRTCP::NewLC
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 EXPORT_C CSRTPCryptoHandlerSRTCP* CSRTPCryptoHandlerSRTCP::NewLC( CSRTPStream& aStream)
       
    69     {
       
    70     CSRTPCryptoHandlerSRTCP* self = new( ELeave )CSRTPCryptoHandlerSRTCP( aStream);
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CSRTPCryptoHandlerSRTCP::~CSRTPCryptoHandlerSRTCP
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CSRTPCryptoHandlerSRTCP::~CSRTPCryptoHandlerSRTCP( )
       
    81     {
       
    82 
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CSRTPCryptoHandlerSRTCP::ConstructL()
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CSRTPCryptoHandlerSRTCP::ConstructL() 
       
    91     {
       
    92     //construct the upper class
       
    93     CSRTPCryptoHandler::ConstructL();   
       
    94     SetEncAndAuthL(CryptoParams().iSrtcpEncAlg,
       
    95     				CryptoParams().iSrtcpAuthAlg);
       
    96     iReplayPktIndex=0;
       
    97     iReplayIndexDelta=0; 
       
    98     iSRTCPMasterKeyLifeTime = CryptoParams().iMasterKeysLifeTime;
       
    99     
       
   100     if ( !iSRTCPMasterKeyLifeTime || 
       
   101     	iSRTCPMasterKeyLifeTime > KSRTCPMaterKeysLifeTime )
       
   102     	{
       
   103     	iSRTCPMasterKeyLifeTime = KSRTCPMaterKeysLifeTime;								
       
   104     	}    
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // void CSRTPCryptoHandlerSRTCP::MasterKeysUpdated()
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 TBool CSRTPCryptoHandlerSRTCP::MasterKeysUpdated()
       
   112     {
       
   113     return iMasterDataUpdated;
       
   114     
       
   115     }  
       
   116 
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // void CSRTPCryptoHandlerSRTCP::CheckMasterKeyIdentifierL()
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CSRTPCryptoHandlerSRTCP::CheckMasterKeyIdentifierL()
       
   123     {
       
   124     // RFC 3711, section 3.4):
       
   125     // verify that this packet uses the same MKI
       
   126     // than we have in CryptoContext    
       
   127     if(Context().MasterKey().MKI()!= KNullDesC8)
       
   128     	{
       
   129 	    if (Context().MasterKey().MKI().Length() > 0)
       
   130 	        {
       
   131 	        TPtrC8 mki = iCurrentPacket->MasterKeyIdentifier();
       
   132 	        if (Context().MasterKey().MKI().Compare(mki) != 0)
       
   133 	            {
       
   134 	            // we can not process this packet, since MKI does not match
       
   135 	             User::Leave(KErrNotFound);
       
   136 	            }
       
   137 	        }       
       
   138     	}
       
   139     }  
       
   140 // ---------------------------------------------------------------------------
       
   141 // void CSRTPCryptoHandlerSRTCP::AuthenticateL()
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CSRTPCryptoHandlerSRTCP::AuthenticateL()
       
   145     {
       
   146     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::AuthenticateL() entry" );
       
   147     if ( CryptoParams().iSrtcpAuthAlg != EAuthNull &&
       
   148         iCurrentPacket->AuthenticationTag().Length()==0)
       
   149         {
       
   150         User::Leave(KErrNotFound);
       
   151         }
       
   152     
       
   153     if (iCurrentPacket->AuthenticationTag().Length()>0)
       
   154         {        
       
   155         // calculate auth tag from the authenticated area..
       
   156         HBufC8* calculatedAuthTag = CalculateAuthTagL();                
       
   157         CleanupStack::PushL(calculatedAuthTag);
       
   158        	SRTP_DEBUG_DETAIL( "SRTCP HMAC caculated Authentication tag" );
       
   159         SRTP_DEBUG_PACKET( *calculatedAuthTag );    
       
   160     	                                                 
       
   161         // finally, compare the two auth tags..
       
   162         if (iCurrentPacket->AuthenticationTag().Compare(
       
   163                                     *calculatedAuthTag) != 0)
       
   164             {
       
   165             // authentication failed, since tags dont match
       
   166             SRTP_DEBUG_DETAIL( "authenticate tag is not match to caculated one" );
       
   167             User::Leave(KErrNotFound);
       
   168             }
       
   169         
       
   170         // then, lose the auth tag we just calculated 
       
   171         CleanupStack::PopAndDestroy(calculatedAuthTag);       
       
   172                        
       
   173         }
       
   174     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::AuthenticateL() exit" );    
       
   175     }  
       
   176         
       
   177 // ---------------------------------------------------------------------------
       
   178 // CSRTPCryptoHandlerSRTCP::CountIV_And_TransformL()
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 HBufC8* CSRTPCryptoHandlerSRTCP::CountIV_And_TransformL()
       
   182     {        
       
   183     /*    
       
   184     RTCP case is different. 
       
   185     */ 
       
   186     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::CountIV_And_TransformL entry" );       
       
   187     //IV is 128 bit value
       
   188     TBuf8<16> iv;           
       
   189     // set IV to be 128 bit, 16 octets
       
   190     iv.SetLength(16);       
       
   191     
       
   192     // count the IV for decryption   
       
   193     //RFC 3711 4.1.1 
       
   194     TInt ret = TSRTPUtils::CountIV(iv, *iSessionSaltKey, SSRC(), iSRTCPIndexEStripped);
       
   195     
       
   196     SRTP_DEBUG_TUINT_VALUE( "SRTCP in ssrc" , SSRC());
       
   197                 
       
   198     if (ret!=KErrNone)
       
   199         {
       
   200         User::Leave(ret);
       
   201         }
       
   202    	SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::CountIV_And_TransformL exit" );       
       
   203 
       
   204     // Step 5, encryption (in RFC 3711, section 3.3):    
       
   205     return iCipher->TransformL( *iSessionEncrKey, 
       
   206                                 iv, iCurrentPacket->Payload() );  
       
   207     }
       
   208     
       
   209 // ---------------------------------------------------------------------------
       
   210 // CSRTPCryptoHandlerSRTCP::EncryptL()
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 HBufC8* CSRTPCryptoHandlerSRTCP::EncryptL()
       
   214     {
       
   215    	SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::EncryptL() entry" );       
       
   216 
       
   217    	HBufC8* encryptedPayload = CountIV_And_TransformL();      
       
   218     CleanupStack::PushL(encryptedPayload);         
       
   219     
       
   220     TUint8* encryptedPayloadPtr = const_cast<TUint8*>(encryptedPayload->Des().Ptr());        
       
   221 
       
   222     HBufC8* srtpPacket = iCurrentPacket->CreateEncryptedPacketL(encryptedPayloadPtr);
       
   223     CleanupStack::PushL(srtpPacket);     
       
   224     
       
   225     TUint8* srtpPacketPtr = const_cast<TUint8*>(srtpPacket->Des().Ptr());                
       
   226   	  
       
   227     //Step 6, Add E flag and SRTCP Index (RFC 3711 section 3.4)
       
   228     AddEflagAndSrtcpIndex(srtpPacketPtr);
       
   229     //Step 7, Add MKI (in RFC 3711, section 3.3):    
       
   230     AddMKIToPacket(srtpPacketPtr);
       
   231 
       
   232     //Step 8, Add authentication tag (in RFC 3711, section 3.3):
       
   233     AddAuthTagToPacketL(srtpPacketPtr);     
       
   234     
       
   235 
       
   236     CleanupStack::Pop(srtpPacket); 
       
   237     SRTP_DEBUG_DETAIL( "Encrypted Payload" );
       
   238     SRTP_DEBUG_PACKET( *encryptedPayload );    
       
   239                                                 
       
   240     CleanupStack::PopAndDestroy(encryptedPayload);   
       
   241     iNumPackets++;     
       
   242     
       
   243     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::EncryptL() exit" );         
       
   244     
       
   245     return srtpPacket;        
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // CSRTPCryptoHandlerSRTCP::DecryptL()
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 HBufC8* CSRTPCryptoHandlerSRTCP::DecryptL()
       
   253     {
       
   254     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::DecryptL() entry" ); 
       
   255     // Step 6, decryption (in RFC 3711, section 3.3):    
       
   256     HBufC8* decryptedPayload = CountIV_And_TransformL();      
       
   257     CleanupStack::PushL(decryptedPayload);                                               
       
   258                                                                  
       
   259     TUint8* decryptedPayloadPtr = const_cast<TUint8*>(decryptedPayload->Des().Ptr());        
       
   260     HBufC8* rtpPacket = iCurrentPacket->CreateDecryptedPacketL(decryptedPayloadPtr);
       
   261     TUint8* rtpPacketPtr = const_cast<TUint8*>(rtpPacket->Des().Ptr());        
       
   262     SRTP_DEBUG_DETAIL( "SRTCP Decrypt Payload" );
       
   263     SRTP_DEBUG_PACKET( *decryptedPayload );    
       
   264                                              
       
   265     CleanupStack::PopAndDestroy(decryptedPayload);    
       
   266     
       
   267     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::DecryptL() exit" ); 
       
   268                 
       
   269     return rtpPacket;                              
       
   270     }
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // CSRTPCryptoHandlerSRTCP::AddMKIToPacket
       
   274 // 
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 void CSRTPCryptoHandlerSRTCP::AddMKIToPacket(TUint8* aSrtpPacketPtr)
       
   278     {
       
   279     TUint8* ptr = aSrtpPacketPtr;        
       
   280     if( Context().MasterKey().MKI()!= KNullDesC8)
       
   281     	{
       
   282 	    //Step 6, Add MKI (in RFC 3711, section 3.3):    
       
   283 	    if (Context().MasterKey().MKI().Length()>0  )
       
   284 	        {
       
   285 	        ptr += iCurrentPacket->HeaderLength();
       
   286 	        ptr += iCurrentPacket->PayloadLength();
       
   287 	        ptr += KSRTCPPacketIndexLength4;
       
   288 	        TUint8* mkiPtr = const_cast<TUint8*>(Context().MasterKey().MKI().Ptr());        
       
   289 	        Mem::Copy( ptr, mkiPtr, Context().MasterKey().MKI().Length());                
       
   290 	        }
       
   291     	}
       
   292     }
       
   293 // ---------------------------------------------------------------------------
       
   294 // CSRTPCryptoHandlerSRTCP::AddAuthTagToPacketL
       
   295 // 
       
   296 // ---------------------------------------------------------------------------
       
   297 //
       
   298 void CSRTPCryptoHandlerSRTCP::AddAuthTagToPacketL(TUint8* aSrtpPacketPtr)
       
   299     {
       
   300     TUint8* ptr = aSrtpPacketPtr;        
       
   301 
       
   302     //Step 7, Add authentication tag (in RFC 3711, section 3.3):
       
   303     if ( CryptoParams().iSrtcpAuthTagLen>0)
       
   304         {
       
   305         ptr += iCurrentPacket->HeaderLength();
       
   306         ptr += iCurrentPacket->PayloadLength();
       
   307         ptr += KSRTCPPacketIndexLength4 ; /*srtcp index length*/
       
   308         if(Context().MasterKey().MKI()!= KNullDesC8)
       
   309     		{
       
   310         	ptr += Context().MasterKey().MKI().Length(); 
       
   311     		}
       
   312         //Copy the authenticatin portion only not with MKI key part
       
   313 		//RFC 3711 3.4 SRCP authentication portion including index
       
   314   		TInt len= iCurrentPacket->HeaderLength() + 	iCurrentPacket->PayloadLength()
       
   315   				+ KSRTCPPacketIndexLength4 /*SRTCP index length*/;
       
   316    		HBufC8* authPortion = HBufC8::NewMaxLC(len);
       
   317         TUint8* authptr = const_cast<TUint8*>(authPortion->Des().Ptr());
       
   318         Mem::Copy( authptr, aSrtpPacketPtr, len);
       
   319     	authPortion->Des().SetLength(len);    
       
   320                  
       
   321     	HBufC8* authTag = DoAuthenticationL(*authPortion, KNullDesC8);
       
   322                   
       
   323        
       
   324     	CleanupStack::PopAndDestroy( authPortion );        
       
   325     	authPortion=NULL;       
       
   326         
       
   327         
       
   328         Mem::Copy( ptr, authTag->Ptr(), authTag->Length());
       
   329         delete authTag;
       
   330         authTag=NULL;
       
   331         }    
       
   332     }
       
   333 // ---------------------------------------------------------------------------
       
   334 // CSRTPCryptoHandlerSRTCP::CalculateAuthTagL()
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 HBufC8* CSRTPCryptoHandlerSRTCP::CalculateAuthTagL()
       
   338     {  
       
   339     //RFC 3711 3.4 SRCP authentication portion including index                           
       
   340     // that is, M = header + payload (Report block)  + SRTCP indexp     
       
   341     
       
   342     // calculate auth tag from the authenticated area..
       
   343    
       
   344  	TInt len= iCurrentPacket->HeaderLength() + 	
       
   345  		iCurrentPacket->PayloadLength()	+4; /*SRTCP index length*/
       
   346    	HBufC8* authPortion = iCurrentPacket->Get_Concatenated_MDataL(ETrue);
       
   347    	CleanupStack::PushL( authPortion );
       
   348     authPortion->Des().SetLength(len);    
       
   349                  
       
   350     HBufC8* calculatedAuthTag = DoAuthenticationL(*authPortion, KNullDesC8);
       
   351                   
       
   352     // then, lose authPortion buffer
       
   353          
       
   354     CleanupStack::PopAndDestroy( authPortion );  
       
   355     authPortion= NULL; 
       
   356     
       
   357     return calculatedAuthTag;
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // CSRTPCryptoHandlerSRTCP::DoAuthenticationL
       
   362 // 
       
   363 // ---------------------------------------------------------------------------
       
   364 //
       
   365 HBufC8* CSRTPCryptoHandlerSRTCP::DoAuthenticationL(const TDesC8& aAuthenticatedArea,
       
   366 											const TDesC8& aRoc)
       
   367     {                                     
       
   368     // calculate auth tag from the authenticated area..
       
   369     TUint authtaglen= CryptoParams().iSrtcpAuthTagLen;
       
   370     // Note that RCCm3 work as Auth Null so taglen does not matter to RTP/RTCP
       
   371     if (aRoc.Length() && 
       
   372     	(CryptoParams().iSrtcpAuthAlg == EAuthRCCm1 ||
       
   373 		CryptoParams().iSrtcpAuthAlg == EAuthRCCm2 ))
       
   374     	{
       
   375     	authtaglen-= KSRTPAuthTagLength32 ;
       
   376     	}
       
   377     if 	((!aRoc.Length()) && 
       
   378     		( CryptoParams().iSrtcpAuthAlg != EAuthNull &&
       
   379     		CryptoParams().iSrtcpAuthAlg != EAuthHMAC_SHA1))
       
   380     	{
       
   381     	//This is for RTCP if using RCC mode because currently handler sharing 
       
   382     	//the same context
       
   383 		authtaglen=KSRTPAuthTagDefaultLength;
       
   384     	}
       
   385     return iAuthenticator->AuthenticateL(authtaglen,
       
   386                                     *iSessionAuthKey, 
       
   387                                     aAuthenticatedArea,
       
   388                                     aRoc);                  
       
   389     }
       
   390     
       
   391 // ---------------------------------------------------------------------------
       
   392 // void CSRTPCryptoHandlerSRTCP::InitializePlainPacketL()
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 void CSRTPCryptoHandlerSRTCP::InitializePlainPacketL(const TDesC8& aPacket)
       
   396     {
       
   397     if(iCurrentPacket)
       
   398     	delete iCurrentPacket;iCurrentPacket = NULL;   
       
   399     iCurrentPacket = CSRTPPacketRTCP::NewL(aPacket, *this);  
       
   400     }  
       
   401 
       
   402 // ---------------------------------------------------------------------------
       
   403 // void CSRTPCryptoHandlerSRTCP::InitializeEncryptedPacketL()
       
   404 // ---------------------------------------------------------------------------
       
   405 //
       
   406 void CSRTPCryptoHandlerSRTCP::InitializeEncryptedPacketL(const TDesC8& aPacket)
       
   407     {
       
   408     if(iCurrentPacket)
       
   409     	delete iCurrentPacket;iCurrentPacket = NULL;       
       
   410     iCurrentPacket = CSRTPPacketSRTCP::NewL(aPacket, *this);        
       
   411     }  
       
   412 
       
   413 // ---------------------------------------------------------------------------
       
   414 // CSRTPCryptoHandlerSRTCP::DeriveSessionKeysL()
       
   415 // ---------------------------------------------------------------------------
       
   416 //
       
   417 void CSRTPCryptoHandlerSRTCP::DeriveSessionKeysL()
       
   418     {
       
   419     SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::DeriveSessionKeysL() Entry" );
       
   420 
       
   421     TBuf8<100> sessionKey_X;
       
   422     DeleteSessionKeys();        
       
   423 
       
   424     TUint64 r = TSRTPUtils::Cnt_r(iSRTCPIndexEStripped, 
       
   425     				CryptoParams().iSrtcpKeyDervRate);
       
   426     
       
   427     // session encryption key
       
   428     //sessionKey_X.SetLength(Context()->MasterKey().EncrKeyLength());
       
   429     sessionKey_X.SetLength(100);
       
   430     // session encryption key derivation :
       
   431     sessionKey_X.FillZ();    
       
   432     Count_X(r, KSRTCPEncryptionLabel, KSRTPIndexLength, sessionKey_X);
       
   433     iSessionEncrKey = iKeyDeriver->PRF_128BitL(Context().MasterKey().EncrKeyLength(),
       
   434                                             Context().MasterKey().MasterKey(), 
       
   435                                             sessionKey_X);
       
   436     SRTP_DEBUG_DETAIL( "SRTCP iSessionEncrKey as CipherKey " );
       
   437     SRTP_DEBUG_PACKET( *iSessionEncrKey );     
       
   438                                     
       
   439     // session authentication key
       
   440     sessionKey_X.FillZ();
       
   441     //sessionKey_X.SetLength(Context()->MasterKey().AuthKeyLength());
       
   442     Count_X(r, KSRTCPAuthenticationLabel, KSRTPIndexLength, sessionKey_X);        
       
   443     iSessionAuthKey = iKeyDeriver->PRF_128BitL(Context().MasterKey().AuthKeyLength(),
       
   444                                             Context().MasterKey().MasterKey(), 
       
   445                                             sessionKey_X);
       
   446 
       
   447 	SRTP_DEBUG_DETAIL( "SRTCP iSessionAuthKey" );
       
   448     SRTP_DEBUG_PACKET( *iSessionAuthKey);    
       
   449        
       
   450     // session salt key
       
   451     sessionKey_X.FillZ();
       
   452     //sessionKey_X.SetLength(Context()->MasterSalt().SaltLength());
       
   453     Count_X(r, KSRTCPSaltingLabel, KSRTPIndexLength, sessionKey_X);        
       
   454     iSessionSaltKey = iKeyDeriver->PRF_128BitL(Context().MasterSalt().SaltLength(),
       
   455                                             Context().MasterKey().MasterKey(), 
       
   456                                             sessionKey_X);
       
   457     SRTP_DEBUG_DETAIL( "SRTCP   iSessionSaltKey" );
       
   458     SRTP_DEBUG_PACKET( *iSessionSaltKey ); 
       
   459                
       
   460     
       
   461    	iMasterDataUpdated = EFalse;  
       
   462    	SRTP_DEBUG_DETAIL( "CSRTPCryptoHandlerSRTCP::DeriveSessionKeysL() Exit" );
       
   463                                                                              
       
   464  	}
       
   465     
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // CSRTPCryptoHandlerSRTCP::StripEBit()
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CSRTPCryptoHandlerSRTCP::StripEBit()
       
   472     {    
       
   473     // strip the Ebit away
       
   474     iSRTCPIndexEStripped = iSRTCPIndex ^ 0x80000000;
       
   475     } 
       
   476     
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 // void CSRTPCryptoHandlerSRTCP::ReplayProtectionL()
       
   480 // ---------------------------------------------------------------------------
       
   481 //
       
   482 void CSRTPCryptoHandlerSRTCP::ReplayProtectionL()
       
   483     {
       
   484     if ( CryptoParams().iSrtcpReplayProtection)
       
   485     	{
       
   486 	    TInt delta =0;
       
   487 	  	delta= iSRTCPIndexEStripped/*updated one*/ - iReplayPktIndex; //set as 0
       
   488 		      
       
   489 		//Index check
       
   490 		if ( CryptoParams().iSrtcpReplayProtection)
       
   491 			{
       
   492 			User::LeaveIfError (ReplayCheck(delta));
       
   493 			}
       
   494 		//if the index is inside the replay window and index does not appear in rdb
       
   495 		//then Index add after authentication passed  
       
   496 	    iReplayIndexDelta= delta;
       
   497     	}
       
   498     }
       
   499 
       
   500 // ---------------------------------------------------------------------------
       
   501 // CSRTPCryptoHandlerSRTCP::CountSenderPacketIndex()
       
   502 // ---------------------------------------------------------------------------
       
   503 //
       
   504 void CSRTPCryptoHandlerSRTCP::CountSenderPacketIndexL()
       
   505     {  
       
   506    	//Re-Key was needed earlier but key has not yet been updated
       
   507     //iReKey is false
       
   508     //Call it after each packet has been sent
       
   509     //TReal modulus(0x10000000);
       
   510     TReal twoexp31=  0x80000000;
       
   511     
       
   512     TReal srtcpindexincremented= iSRTCPIndexEStripped+1;
       
   513     TReal result;    
       
   514     TInt32 intResult;       
       
   515     // count (ROC - 1) mod 2^31   
       
   516        
       
   517     TInt res = Math::Mod(result, srtcpindexincremented, twoexp31); 
       
   518     if (res!=KErrNone)
       
   519         {
       
   520         User::Leave(res);
       
   521         }
       
   522     
       
   523     intResult = (TUint)result;
       
   524     
       
   525     iSRTCPIndexEStripped = intResult;
       
   526     // E bit set to 1 indicates encrpted pacet 0 otherwise
       
   527     iSRTCPIndex =0x00000000;
       
   528     if (CryptoParams().iSrtcpEncAlg!=ENullAlg)
       
   529     	{
       
   530     	iSRTCPIndex =0x80000000;	
       
   531     	}
       
   532     	
       
   533     iSRTCPIndex = iSRTCPIndex + iSRTCPIndexEStripped;
       
   534     iCurrentPacket->SetPacketIndex(iSRTCPIndex);
       
   535     //Checking next Index if Keys needed to be renew
       
   536     IndexReKeyCheckL();
       
   537     }
       
   538     
       
   539 // ---------------------------------------------------------------------------
       
   540 // CSRTPCryptoHandlerSRTCP::ReceiverPacketIndexL()
       
   541 // ---------------------------------------------------------------------------
       
   542 //    
       
   543 void CSRTPCryptoHandlerSRTCP::ReceiverPacketIndexL()
       
   544     { 
       
   545     if (iReKey)
       
   546     	{
       
   547     	//Re-Key was needed earlier but key has not yet been updated
       
   548     	User::Leave(KErrTooBig);
       
   549     	}      
       
   550     /* Can be done in the Replay protection
       
   551     if (iCurrentPacket->PacketIndex()<iSRTCPIndex)
       
   552     	{
       
   553     	//the receiver packet index is samller than the earlier packet index
       
   554     	//ignore this packet by leave
       
   555     	User::Leave(KErrCorrupt);
       
   556     	}*/
       
   557     //Updated the packet index during the construction of Packet	
       
   558     iSRTCPIndex = iCurrentPacket->PacketIndex();
       
   559     //Strip EBit only when the encAlg is not NULL
       
   560     if (CryptoParams().iSrtcpEncAlg!=ENullAlg)
       
   561     	StripEBit();
       
   562     IndexReKeyCheckL();
       
   563     }
       
   564 // ---------------------------------------------------------------------------
       
   565 // CSRTPCryptoHandlerSRTCP::InitialPacketIndex()
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void CSRTPCryptoHandlerSRTCP::InitialPacketIndex()
       
   569 	{
       
   570 	//iSRTCPIndexEStripped=0x00;
       
   571 	iSRTCPIndex =0x80000000;
       
   572 	StripEBit();
       
   573 	iSRTCPIndex = iSRTCPIndex + iSRTCPIndexEStripped;
       
   574 	iCurrentPacket->SetPacketIndex(iSRTCPIndex);
       
   575 	}
       
   576 
       
   577 // ---------------------------------------------------------------------------
       
   578 // CSRTPCryptoHandlerSRTCP::AddEflagAndSrtcpIndex()
       
   579 // ---------------------------------------------------------------------------
       
   580 //	
       
   581 void CSRTPCryptoHandlerSRTCP::AddEflagAndSrtcpIndex(TUint8* aSrtcpPacketPtr)
       
   582 	{
       
   583 	//EBit set to 1 as hardcoded
       
   584 	
       
   585 	TUint8* ptr = aSrtcpPacketPtr;        
       
   586 
       
   587     //Step 6, Add E flag and SRTCP Index (RFC 3711 section 3.4)
       
   588     
       
   589     ptr += iCurrentPacket->HeaderLength();
       
   590     ptr += iCurrentPacket->PayloadLength();
       
   591     //EBit set to 1 indicates encryted packet, otherwise set to 0
       
   592     if (CryptoParams().iSrtcpEncAlg!=ENullAlg )
       
   593     	{
       
   594     	TSRTPUtils::Write32( ptr, iSRTCPIndex );
       
   595     	}
       
   596 	else
       
   597     	{
       
   598     	TSRTPUtils::Write32( ptr, iSRTCPIndexEStripped);
       
   599     	}
       
   600 	
       
   601 	}
       
   602 // ---------------------------------------------------------------------------
       
   603 // CSRTPCryptoHandlerSRTCP::IndexReKeyCheck()
       
   604 // ---------------------------------------------------------------------------
       
   605 //
       
   606 void CSRTPCryptoHandlerSRTCP::IndexReKeyCheckL()
       
   607 	{
       
   608 	TReal twoexp31=  0x80000000;
       
   609     
       
   610     TReal srtcpIndexIncremented= iSRTCPIndexEStripped+1;
       
   611     TReal remainder;    
       
   612        
       
   613     
       
   614        
       
   615     TInt res = Math::Mod(remainder, srtcpIndexIncremented, twoexp31); 
       
   616     if (res!=KErrNone)
       
   617         {
       
   618         User::Leave(res);
       
   619         }
       
   620     if (remainder == 0.0 && iSRTCPIndexEStripped != 0)
       
   621     	{
       
   622     	//RFC 3711 8.1 & 9.2
       
   623     	iReKey = ETrue;
       
   624     	iStream.KeyExpired();
       
   625     	//called incase the packets index is big in the begining
       
   626     	iStream.ReKeyNeeded();
       
   627     	}
       
   628     //should create function for checking.when value is bigger
       
   629     TUint64 reKeyNumPacket = iNumPackets + KLeftNumOfPacketBeforeReKey; 
       
   630     // Re-key only call one time when there are 100 number packets left to send/recv
       
   631     if ( ( reKeyNumPacket ) == iSRTCPMasterKeyLifeTime )
       
   632     	{
       
   633     	iStream.ReKeyNeeded();
       
   634     	}
       
   635     //Key is really expired	
       
   636     if ( iNumPackets >=  iSRTCPMasterKeyLifeTime )
       
   637     	{
       
   638 		iReKey = ETrue;
       
   639     	iStream.KeyExpired();        
       
   640     	}
       
   641     }
       
   642 
       
   643  // ---------------------------------------------------------------------------
       
   644 // CSRTPCryptoHandlerSRTCP::AddReplayIndex()
       
   645 // ---------------------------------------------------------------------------
       
   646 //   
       
   647 void CSRTPCryptoHandlerSRTCP::AddReplayIndex()
       
   648 	{
       
   649 	if (CryptoParams().iSrtcpReplayProtection)
       
   650 		{
       
   651 		if (iReplayIndexDelta > 0) 
       
   652 			{
       
   653 		    /* shift forward by delta */
       
   654 		    iReplayPktIndex = iSRTCPIndexEStripped;
       
   655 		  	} 
       
   656 		 else 
       
   657 		  	{
       
   658 		    /* delta is in window, so flip bit in bitmask */
       
   659 		    /* mark as seen */
       
   660 		    TInt diff= -(iReplayIndexDelta);
       
   661 		    iBitmap |= ((TUint64)1 << diff); 
       
   662 		  	}
       
   663 		  /* note that we need not consider the case that delta == 0 */
       
   664 		}
       
   665 	//Add Number of packets received
       
   666 	iNumPackets++;  
       
   667 	}