crypto/weakcrypto/source/pbe/pbencryptorset.cpp
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2002-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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <random.h>
       
    21 
       
    22 #include "pbencryptor.h"
       
    23 #include "pbesymmetricfactory.h"
       
    24 
       
    25 EXPORT_C CPBEncryptorSet* CPBEncryptorSet::NewL(
       
    26 	const TPBECipher aCipher, const TDesC8& aKey) 
       
    27 	{
       
    28 	CPBEncryptorSet* self = NewLC(aCipher, aKey);
       
    29 	CleanupStack::Pop();
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 EXPORT_C CPBEncryptorSet* CPBEncryptorSet::NewLC(
       
    34 	const TPBECipher aCipher, const TDesC8& aKey)
       
    35 	{
       
    36 	CPBEncryptorSet* self = new(ELeave) CPBEncryptorSet;
       
    37 	CleanupStack::PushL(self);
       
    38 
       
    39 	self->ConstructL(aCipher, aKey);
       
    40 	
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 void CPBEncryptorSet::ConstructL(const TPBECipher aCipher,const TDesC8& aKey)
       
    45 	{
       
    46 	TInt blocksize = PBE::GetBlockBytes(aCipher);
       
    47 	iIV = HBufC8::NewMaxL(blocksize); 
       
    48 	TPtr8 iv = iIV->Des();
       
    49 	TRandom::RandomL(iv);
       
    50 
       
    51 	iCipher = PBE::MakeEncryptorL(aCipher, aKey, iv);
       
    52 	}
       
    53 
       
    54 void CPBEncryptorSet::Process(const TDesC8& aInput, TDes8& aOutput)
       
    55 	{
       
    56 	if(!iIVSent)
       
    57 		{
       
    58 		aOutput.Append(*iIV);
       
    59 		iIVSent = ETrue;
       
    60 		}
       
    61 	iCipher->Process(aInput, aOutput);
       
    62 	}
       
    63 
       
    64 void CPBEncryptorSet::ProcessFinalL(const TDesC8& aInput, 
       
    65 	TDes8& aOutput)
       
    66 	{
       
    67 	if(!iIVSent)
       
    68 		{
       
    69 		aOutput.Append(*iIV);
       
    70 		iIVSent = ETrue;
       
    71 		}
       
    72 	
       
    73 	iCipher->ProcessFinalL(aInput, aOutput);
       
    74 	}
       
    75 
       
    76 
       
    77 CPBEncryptorSet::CPBEncryptorSet() : iIVSent(EFalse)
       
    78 	{
       
    79 	}
       
    80 
       
    81 CPBEncryptorSet::~CPBEncryptorSet()
       
    82 	{
       
    83 	delete iIV;
       
    84 	delete iCipher;	//	Don't delete iCBCDecryptor, this belongs to iCipher
       
    85 	}
       
    86 
       
    87 TInt CPBEncryptorSet::MaxOutputLength(TUint aMaxInputLength) const
       
    88     {
       
    89 	if(iIVSent)
       
    90 		{
       
    91 		return (iCipher->MaxOutputLength(aMaxInputLength));
       
    92 		}
       
    93 	else 
       
    94 		{//	If we've not sent the iv yet then its the length 
       
    95 		//	from the cipher plus a blocksize for the iv
       
    96 		return (iCipher->MaxOutputLength(aMaxInputLength + iCipher->BlockSize()));
       
    97 		}
       
    98     }
       
    99 
       
   100 TInt CPBEncryptorSet::MaxFinalOutputLength(TUint aMaxInputLength) const
       
   101     {
       
   102 	if(iIVSent)
       
   103 		{
       
   104 		return (iCipher->MaxFinalOutputLength(aMaxInputLength));
       
   105 		}
       
   106 	else 
       
   107 		{//	If we've not sent the iv yet then its the length 
       
   108 		//	from the cipher plus a blocksize for the iv
       
   109 		return (iCipher->MaxFinalOutputLength(aMaxInputLength + iCipher->BlockSize()));
       
   110 		}
       
   111     }
       
   112 
       
   113 EXPORT_C CPBDecryptorSet* CPBDecryptorSet::NewL(const TPBECipher aCipher, 
       
   114 	const TDesC8& aKey)
       
   115 	{
       
   116 	CPBDecryptorSet* self = NewLC(aCipher, aKey);
       
   117 	CleanupStack::Pop();
       
   118 	return self;
       
   119 	}
       
   120 
       
   121 EXPORT_C CPBDecryptorSet* CPBDecryptorSet::NewLC(const TPBECipher aCipher,
       
   122 	const TDesC8& aKey)
       
   123 	{
       
   124 	CPBDecryptorSet* self = new(ELeave) CPBDecryptorSet;
       
   125 	CleanupStack::PushL(self);
       
   126 	TBuf8<KMaxBlockSizeSupported> fakeIV;
       
   127 	fakeIV.SetLength(PBE::GetBlockBytes(aCipher));
       
   128 	self->ConstructL(aCipher, aKey, fakeIV);
       
   129 	return self;
       
   130 	}
       
   131 
       
   132 void CPBDecryptorSet::ConstructL(const TPBECipher aCipher, 
       
   133 	const TDesC8& aKey, const TDesC8& aIV)
       
   134 	{
       
   135 	iCipher = PBE::MakeDecryptorL(aCipher, aKey, aIV);
       
   136 	iIVBuf = HBufC8::NewL(PBE::GetBlockBytes(aCipher));
       
   137 	}
       
   138 
       
   139 void CPBDecryptorSet::Process(const TDesC8& aInput, TDes8& aOutput)
       
   140 	{
       
   141 	TPtrC8 input = ProcessIV(aInput);
       
   142 	iCipher->Process(input, aOutput);
       
   143 	}
       
   144 
       
   145 void CPBDecryptorSet::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
       
   146 	{
       
   147 	TPtrC8 input = ProcessIV(aInput);
       
   148 	iCipher->ProcessFinalL(input, aOutput);
       
   149 	}
       
   150 
       
   151 TPtrC8 CPBDecryptorSet::ProcessIV(const TDesC8& aInput)
       
   152 	{
       
   153 	if(!iIVSent)
       
   154 		{
       
   155 		TPtr8 iIVBufPtr(iIVBuf->Des());
       
   156 		if(aInput.Length() + iIVBufPtr.Length() < iCipher->BlockSize())
       
   157 			{
       
   158 			iIVBufPtr.Append(aInput);
       
   159 			return TPtrC8();
       
   160 			}
       
   161 		else 
       
   162 			{
       
   163 			TInt rem = iCipher->BlockSize() - iIVBufPtr.Length();
       
   164 			iIVBufPtr.Append(aInput.Mid(0, rem));
       
   165 			CBufferedDecryptor* blockDecryptor = static_cast<CBufferedDecryptor*>(iCipher);
       
   166 			static_cast<CModeCBCDecryptor*>(blockDecryptor->BlockTransformer())->SetIV(iIVBufPtr);
       
   167 			iIVSent = ETrue;
       
   168 			return aInput.Mid(rem);
       
   169 			}
       
   170 		}
       
   171 	else 
       
   172 		{
       
   173 		return aInput;
       
   174 		}
       
   175 	}
       
   176 
       
   177 CPBDecryptorSet::CPBDecryptorSet() : iIVSent(EFalse)
       
   178 	{
       
   179 	}
       
   180 
       
   181 CPBDecryptorSet::~CPBDecryptorSet()
       
   182 	{
       
   183 	delete iCipher;
       
   184 	//iCipher owns iCBCDecryptor we _don't_ need to delete it
       
   185 	delete iIVBuf;
       
   186 	}
       
   187 
       
   188 TInt CPBDecryptorSet::MaxOutputLength(TUint aMaxInputLength) const
       
   189     {
       
   190 	if(iIVSent)
       
   191 		{
       
   192 		return (iCipher->MaxOutputLength(aMaxInputLength));
       
   193 		}
       
   194 	else	//	if we haven't sent the iv yet, then the length the max length of the
       
   195 		{	//	cipher minus a BlockSize where the IV was sitting (as the iv would be part
       
   196 			//	of the length they've sent us)
       
   197 		return (iCipher->MaxOutputLength(aMaxInputLength - iCipher->BlockSize()));
       
   198 		}
       
   199     }
       
   200 
       
   201 TInt CPBDecryptorSet::MaxFinalOutputLength(TUint aMaxInputLength) const
       
   202     {
       
   203 	if(iIVSent)
       
   204 		{
       
   205 		return (iCipher->MaxFinalOutputLength(aMaxInputLength));
       
   206 		}
       
   207 	else	//	if we haven't sent the iv yet, then the length the max length of the
       
   208 		{	//	cipher minus a BlockSize where the IV was sitting (as the iv would be part
       
   209 			//	of the length they've sent us)
       
   210 		return (iCipher->MaxFinalOutputLength(aMaxInputLength - iCipher->BlockSize()));
       
   211 		}
       
   212     }