rtp/srtpstack/src/srtpauthentication_hmac_sha1.cpp
changeset 0 307788aac0a8
child 9 1e1cc61f56c3
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:    Provides class for authentication with HMAC-SHA1 algorithm.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "srtpauthentication_hmac_sha1.h" 
       
    22 #include "srtpaesctrcrypto.h"
       
    23 #include "hash.h"
       
    24 #include "srtpdef.h"
       
    25 #include "srtputils.h"
       
    26 
       
    27 #define DES_AS_8_BIT(str) (TPtrC8((TText8*)((str).Ptr()), (str).Size()))
       
    28     
       
    29 // ---------------------------------------------------------------------------
       
    30 // CSRTPAuthentication_HMAC_SHA1::NewL
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CSRTPAuthentication_HMAC_SHA1* CSRTPAuthentication_HMAC_SHA1::NewL()
       
    34     {
       
    35     CSRTPAuthentication_HMAC_SHA1* self = CSRTPAuthentication_HMAC_SHA1::NewLC();
       
    36     CleanupStack::Pop(self);
       
    37     return self;    
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CSRTPAuthentication_HMAC_SHA1::NewLC
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CSRTPAuthentication_HMAC_SHA1* CSRTPAuthentication_HMAC_SHA1::NewLC()
       
    45     {
       
    46     CSRTPAuthentication_HMAC_SHA1* self = new( ELeave ) CSRTPAuthentication_HMAC_SHA1();
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     return self;
       
    50     }
       
    51 
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // SRTPAuthentication_HMAC_SHA1::AuthenticateL
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 HBufC8* CSRTPAuthentication_HMAC_SHA1::AuthenticateL(TUint aBitLength,
       
    58                                                     const TDesC8& aKey, 
       
    59                                                     const TDesC8& aAuthPortion,
       
    60                                                     const TDesC8& aRoc)
       
    61     {        
       
    62     SRTP_DEBUG_DETAIL( "CSRTPAuthentication_HMAC_SHA1::AuthenticateL Entry" );
       
    63     
       
    64     TBuf8<20> buf;    
       
    65 	
       
    66 
       
    67     if (aBitLength != KSRTPAuthTagLength80 && aBitLength != KSRTPAuthTagLength32)
       
    68         {
       
    69         SRTP_DEBUG_TUINT_VALUE( "aBitLength is not valid", aBitLength );
       
    70 	    
       
    71         User::Leave(KErrArgument);
       
    72         }
       
    73 	
       
    74     // reserve size for 160 bit tag which seems to be the default        
       
    75 	HBufC8* result = HBufC8::NewL(20);  
       
    76 	TPtr8 ptrOutputBuff = result->Des();	
       
    77 	
       
    78     CleanupStack::PushL(result);
       
    79 
       
    80     CSHA1* sha1 = CSHA1::NewL(); 
       
    81     CleanupStack::PushL(sha1);
       
    82     
       
    83     CHMAC *hmac = CHMAC::NewL(aKey, sha1);        
       
    84     CleanupStack::Pop(sha1);
       
    85     CleanupStack::Pop(result);
       
    86     
       
    87     SRTP_DEBUG_TINT_VALUE( "HMAC INPUT and INPUT Length is ", aAuthPortion.Length() );
       
    88     SRTP_DEBUG_TINT_VALUE( "HMAC INPUT and INPUT Size is ", aAuthPortion.Size());     
       
    89     SRTP_DEBUG_PACKET ( DES_AS_8_BIT(aAuthPortion) );    
       
    90     
       
    91     if(aRoc.Length())
       
    92     	{
       
    93   		hmac->Update(DES_AS_8_BIT(aAuthPortion));
       
    94     	buf.Copy(hmac->Final(DES_AS_8_BIT(aRoc))); 	
       
    95     	}
       
    96     else
       
    97     	{
       
    98     	buf.Copy(hmac->Final(DES_AS_8_BIT(aAuthPortion)));
       
    99     	}
       
   100     *result = buf;    
       
   101     ptrOutputBuff.SetLength(aBitLength/8);
       
   102     
       
   103     delete hmac;
       
   104     
       
   105    	SRTP_DEBUG_DETAIL( "HMAC caculated Authentication tag");
       
   106     SRTP_DEBUG_PACKET( *result );    
       
   107     SRTP_DEBUG_DETAIL( "CSRTPAuthentication_HMAC_SHA1::AuthenticateL Exit" );
       
   108                             
       
   109     return result;            
       
   110     }
       
   111     
       
   112     
       
   113 // ---------------------------------------------------------------------------
       
   114 // CSRTPAuthentication_HMAC_SHA1::ConstructL
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 void CSRTPAuthentication_HMAC_SHA1::ConstructL()
       
   118     {    
       
   119     
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // CSRTPAuthentication_HMAC_SHA1::~CSRTPAuthentication_HMAC_SHA1
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 CSRTPAuthentication_HMAC_SHA1::~CSRTPAuthentication_HMAC_SHA1()
       
   127     {    
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // CSRTPAuthentication_HMAC_SHA1::CSRTPAuthentication_HMAC_SHA1
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 CSRTPAuthentication_HMAC_SHA1::CSRTPAuthentication_HMAC_SHA1()
       
   135     {    
       
   136         
       
   137     }
       
   138