Update contrib.
2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include "pbencryptor.h"
23 #include "pbesymmetricfactory.h"
25 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
26 /** The maximum block size supported (in bytes) */
27 const TUint KMaxBlockSizeSupported = 32;
30 EXPORT_C CPBEncryptorSet* CPBEncryptorSet::NewL(
31 const TPBECipher aCipher, const TDesC8& aKey)
33 CPBEncryptorSet* self = NewLC(aCipher, aKey);
38 EXPORT_C CPBEncryptorSet* CPBEncryptorSet::NewLC(
39 const TPBECipher aCipher, const TDesC8& aKey)
41 CPBEncryptorSet* self = new(ELeave) CPBEncryptorSet;
42 CleanupStack::PushL(self);
44 self->ConstructL(aCipher, aKey);
49 void CPBEncryptorSet::ConstructL(const TPBECipher aCipher,const TDesC8& aKey)
51 TInt blocksize = PBE::GetBlockBytes(aCipher);
52 iIV = HBufC8::NewMaxL(blocksize);
53 TPtr8 iv = iIV->Des();
56 iCipher = PBE::MakeEncryptorL(aCipher, aKey, iv);
59 void CPBEncryptorSet::Process(const TDesC8& aInput, TDes8& aOutput)
66 iCipher->Process(aInput, aOutput);
69 void CPBEncryptorSet::ProcessFinalL(const TDesC8& aInput,
78 iCipher->ProcessFinalL(aInput, aOutput);
82 CPBEncryptorSet::CPBEncryptorSet() : iIVSent(EFalse)
86 CPBEncryptorSet::~CPBEncryptorSet()
89 delete iCipher; // Don't delete iCBCDecryptor, this belongs to iCipher
92 TInt CPBEncryptorSet::MaxOutputLength(TUint aMaxInputLength) const
96 return (iCipher->MaxOutputLength(aMaxInputLength));
99 {// If we've not sent the iv yet then its the length
100 // from the cipher plus a blocksize for the iv
101 return (iCipher->MaxOutputLength(aMaxInputLength + iCipher->BlockSize()));
105 TInt CPBEncryptorSet::MaxFinalOutputLength(TUint aMaxInputLength) const
109 return (iCipher->MaxFinalOutputLength(aMaxInputLength));
112 {// If we've not sent the iv yet then its the length
113 // from the cipher plus a blocksize for the iv
114 return (iCipher->MaxFinalOutputLength(aMaxInputLength + iCipher->BlockSize()));
118 EXPORT_C CPBDecryptorSet* CPBDecryptorSet::NewL(const TPBECipher aCipher,
121 CPBDecryptorSet* self = NewLC(aCipher, aKey);
126 EXPORT_C CPBDecryptorSet* CPBDecryptorSet::NewLC(const TPBECipher aCipher,
129 CPBDecryptorSet* self = new(ELeave) CPBDecryptorSet;
130 CleanupStack::PushL(self);
131 TBuf8<KMaxBlockSizeSupported> fakeIV;
132 fakeIV.SetLength(PBE::GetBlockBytes(aCipher));
133 self->ConstructL(aCipher, aKey, fakeIV);
137 void CPBDecryptorSet::ConstructL(const TPBECipher aCipher,
138 const TDesC8& aKey, const TDesC8& aIV)
140 iCipher = PBE::MakeDecryptorL(aCipher, aKey, aIV);
141 iIVBuf = HBufC8::NewL(PBE::GetBlockBytes(aCipher));
144 void CPBDecryptorSet::Process(const TDesC8& aInput, TDes8& aOutput)
146 TPtrC8 input = ProcessIV(aInput);
147 iCipher->Process(input, aOutput);
150 void CPBDecryptorSet::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
152 TPtrC8 input = ProcessIV(aInput);
153 iCipher->ProcessFinalL(input, aOutput);
156 TPtrC8 CPBDecryptorSet::ProcessIV(const TDesC8& aInput)
160 TPtr8 iIVBufPtr(iIVBuf->Des());
161 if(aInput.Length() + iIVBufPtr.Length() < iCipher->BlockSize())
163 iIVBufPtr.Append(aInput);
168 TInt rem = iCipher->BlockSize() - iIVBufPtr.Length();
169 iIVBufPtr.Append(aInput.Mid(0, rem));
170 CBufferedDecryptor* blockDecryptor = static_cast<CBufferedDecryptor*>(iCipher);
171 static_cast<CModeCBCDecryptor*>(blockDecryptor->BlockTransformer())->SetIV(iIVBufPtr);
173 return aInput.Mid(rem);
182 CPBDecryptorSet::CPBDecryptorSet() : iIVSent(EFalse)
186 CPBDecryptorSet::~CPBDecryptorSet()
189 //iCipher owns iCBCDecryptor we _don't_ need to delete it
193 TInt CPBDecryptorSet::MaxOutputLength(TUint aMaxInputLength) const
197 return (iCipher->MaxOutputLength(aMaxInputLength));
199 else // if we haven't sent the iv yet, then the length the max length of the
200 { // cipher minus a BlockSize where the IV was sitting (as the iv would be part
201 // of the length they've sent us)
202 return (iCipher->MaxOutputLength(aMaxInputLength - iCipher->BlockSize()));
206 TInt CPBDecryptorSet::MaxFinalOutputLength(TUint aMaxInputLength) const
210 return (iCipher->MaxFinalOutputLength(aMaxInputLength));
212 else // if we haven't sent the iv yet, then the length the max length of the
213 { // cipher minus a BlockSize where the IV was sitting (as the iv would be part
214 // of the length they've sent us)
215 return (iCipher->MaxFinalOutputLength(aMaxInputLength - iCipher->BlockSize()));