rtp/srtpstack/src/srtpaesctrcrypto.cpp
branchRCL_3
changeset 9 1e1cc61f56c3
parent 0 307788aac0a8
equal deleted inserted replaced
4:dd3853b8dc3f 9:1e1cc61f56c3
    39 // 
    39 // 
    40 // ---------------------------------------------------------------------------
    40 // ---------------------------------------------------------------------------
    41 //
    41 //
    42 CSrtpAESCTRCrypto::~CSrtpAESCTRCrypto()
    42 CSrtpAESCTRCrypto::~CSrtpAESCTRCrypto()
    43 	{
    43 	{
       
    44 	delete iEncryptor;
       
    45 	delete iKey;
    44 	}
    46 	}
    45 
    47 
    46 
    48 
    47 // ---------------------------------------------------------------------------
    49 // ---------------------------------------------------------------------------
    48 // CSrtpAESCTRCrypto::CSrtpAESCTRCrypto
    50 // CSrtpAESCTRCrypto::CSrtpAESCTRCrypto
   160 	    TBuf8<16> iv; //temp for IV
   162 	    TBuf8<16> iv; //temp for IV
   161 	    TBuf8<16> data; //for a 128-bit piece of a keystream
   163 	    TBuf8<16> data; //for a 128-bit piece of a keystream
   162 	    TBuf8<16> msg; //for a 128-bit piece of a message
   164 	    TBuf8<16> msg; //for a 128-bit piece of a message
   163 	    TInt count=0; // how many full 128-bit pieces can be made of a source
   165 	    TInt count=0; // how many full 128-bit pieces can be made of a source
   164 	    
   166 	    
   165 	    CAESEncryptor* encryptor = NULL;
       
   166 	    SRTP_DEBUG_TINT_VALUE( "EncryptL, Check aBitLengh ==16 and the length is", 
   167 	    SRTP_DEBUG_TINT_VALUE( "EncryptL, Check aBitLengh ==16 and the length is", 
   167 	                aKey.Length()  );
   168 	                aKey.Length()  );
   168 	      
   169 	      
   169 	    //if key's length is not valid
   170 	    //if key's length is not valid
   170 	    if(aKey.Length() != 16)
   171 	    if(aKey.Length() != 16)
   212 	        delete outputBuff;    
   213 	        delete outputBuff;    
   213 	        outputBuff= NULL;  
   214 	        outputBuff= NULL;  
   214 	    	User::Leave(KErrArgument);
   215 	    	User::Leave(KErrArgument);
   215 	    	}
   216 	    	}
   216 
   217 
   217 	    encryptor = CAESEncryptor::NewLC(aKey);
   218 	    if ( !iEncryptor || !iKey || (*iKey != aKey) )
       
   219 	        {
       
   220 	        CreateEncryptorL(aKey);
       
   221             }
   218 	                   
   222 	                   
   219 	    for(int x = 0; x < count; x++)
   223 	    for(int x = 0; x < count; x++)
   220 	    	{
   224 	    	{
   221 	    	msg.Copy(aSrc.Mid(x*16, 16));
   225 	    	msg.Copy(aSrc.Mid(x*16, 16));
   222 	    	
   226 	    	
   223 	    	data.Copy(iv);
   227 	    	data.Copy(iv);
   224 	    	   	
   228 	    	   	
   225 	    	encryptor->Transform(data);
   229 	    	iEncryptor->Transform(data);
   226 
   230 
   227 	    	IncreaseIV(iv);
   231 	    	IncreaseIV(iv);
   228 	    	    	
   232 	    	    	
   229 		    // XOR 128-bits of message with encrypted IV
   233 		    // XOR 128-bits of message with encrypted IV
   230 		    for(int i = 0; i < 16; i++)
   234 		    for(int i = 0; i < 16; i++)
   242 	    	TInt bytesleft;
   246 	    	TInt bytesleft;
   243 	    	bytesleft = aSrc.Length() - count*16;
   247 	    	bytesleft = aSrc.Length() - count*16;
   244 	    	
   248 	    	
   245 	    	msg.Copy(aSrc.Mid(count*16, bytesleft));
   249 	    	msg.Copy(aSrc.Mid(count*16, bytesleft));
   246 	    	data.Copy(iv);
   250 	    	data.Copy(iv);
   247 	    	encryptor->Transform(data);
   251 	    	iEncryptor->Transform(data);
   248 	    		
   252 	    		
   249 		    // XOR last piece of message with encrypted IV
   253 		    // XOR last piece of message with encrypted IV
   250 		    for(int i = 0; i < bytesleft; i++)
   254 		    for(int i = 0; i < bytesleft; i++)
   251 			{
   255 			{
   252 					msg[i] ^= data[i];
   256 					msg[i] ^= data[i];
   254 			
   258 			
   255 			ptrOutputBuff.Append(msg);
   259 			ptrOutputBuff.Append(msg);
   256 			    		
   260 			    		
   257 	    	}
   261 	    	}
   258 		
   262 		
   259 		CleanupStack::PopAndDestroy(encryptor);
       
   260 	    CleanupStack::Pop(outputBuff);    	    
   263 	    CleanupStack::Pop(outputBuff);    	    
   261 	    
   264 	    
   262 	    return outputBuff;
   265 	    return outputBuff;
   263 	}
   266 	}
   264 
   267 
   300 		iv[12] = 0;
   303 		iv[12] = 0;
   301 		}
   304 		}
   302 
   305 
   303 	} 
   306 	} 
   304 
   307 
   305 
   308 // ---------------------------------------------------------------------------
       
   309 // CSrtpAESCTRCrypto::CreateEncryptorL
       
   310 // ---------------------------------------------------------------------------
       
   311 //
       
   312 void CSrtpAESCTRCrypto::CreateEncryptorL(const TDesC8& aKey)
       
   313     {
       
   314     delete iEncryptor;
       
   315     iEncryptor = 0;
       
   316     delete iKey;
       
   317     iKey = 0;
       
   318     iKey = aKey.AllocL();
       
   319     iEncryptor = CAESEncryptor::NewL(*iKey);
       
   320     }