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.
21 #include "pkcs12kdf.h"
23 #include "pbesymmetricfactory.h"
24 #include "cryptostrength.h"
26 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(const TDesC8& aPassword,
27 TPBECipher aCipher, const TDesC8& aAuthSalt,
28 const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations)
30 CPBEncryptionData* self = NewLC(aPassword, aCipher, aAuthSalt, aEncryptSalt,
32 CleanupStack::Pop(self);
36 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewLC(const TDesC8& aPassword,
37 TPBECipher aCipher, const TDesC8& aAuthSalt,
38 const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations)
40 CPBEncryptionData* self = new(ELeave)CPBEncryptionData();
41 CleanupStack::PushL(self);
42 self->ConstructL(aPassword, aCipher, aAuthSalt, aEncryptSalt, aIV,
47 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(
48 const CPBEncryptionData& aData)
50 CPBEncryptionData* self = NewLC(aData);
51 CleanupStack::Pop(self);
55 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewLC(
56 const CPBEncryptionData& aData)
58 CPBEncryptionData* self = new(ELeave)CPBEncryptionData();
59 CleanupStack::PushL(self);
60 self->ConstructL(aData);
64 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(
65 const TDesC8& aPassword, const TDesC8& aAuthSalt,
66 const CPBEncryptParms& aParms)
68 This factory function takes the user-supplied password
69 and the randomly-generated authentication salt, along
70 with the encryption paramaters. It is provided so the
71 encryption parameters can be extended without having to
72 provide multiple factory functions.
74 @param aPassword User-supplied password. This
75 password is not transformed so
76 if it needs to be in a particular
77 format, e.g. for PKCS#12, the
78 transformation must be applied before
79 this function is called.
80 @param aAuthSalt The salt is used to derive the
81 authentication key; not the encryption
83 @param aParms Encryption parameters describe how the
85 @return New instance of CPBEncryptionData.
88 CPBEncryptionData* self = new(ELeave) CPBEncryptionData;
89 CleanupStack::PushL(self);
90 self->ConstructL(aPassword, aAuthSalt, aParms);
91 CleanupStack::Pop(self);
95 void CPBEncryptionData::ConstructL(
96 const TDesC8& aPassword, const TDesC8& aAuthSalt,
97 const CPBEncryptParms& aParms)
99 Second-phase constructor for factory function with
103 iParms = CPBEncryptParms::NewL(aParms);
104 iAuth = CPBAuthData::NewL(
107 PBE::GetKeyBytes(aParms.Cipher()),
108 aParms.Iterations());
111 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
112 // This method is DUPLICATED in common/generic/syslibs/store/ucrypt/ue_strm.cpp
113 EXPORT_C CPBEncryptionData::CPBEncryptionData(void)
117 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
118 // This method is DUPLICATED in common/generic/syslibs/store/ucrypt/ue_strm.cpp
119 CPBEncryptionData::~CPBEncryptionData(void)
125 void CPBEncryptionData::ConstructL(const TDesC8& aPassword,
126 TPBECipher aCipher, const TDesC8& aAuthSalt,
127 const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations)
129 iParms = CPBEncryptParms::NewL(aCipher, aEncryptSalt, aIV, aIterations);
130 iAuth = CPBAuthData::NewL(aPassword, aAuthSalt,
131 PBE::GetKeyBytes(aCipher), aIterations);
134 void CPBEncryptionData::ConstructL(const CPBEncryptionData& aData)
136 iParms = CPBEncryptParms::NewL(aData.EncryptParms());
137 iAuth = CPBAuthData::NewL(aData.AuthData());
140 EXPORT_C const CPBEncryptParms& CPBEncryptionData::EncryptParms(void) const
144 EXPORT_C const CPBAuthData& CPBEncryptionData::AuthData(void) const
149 /* CPBEncryptParms */
150 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL()
152 This factory function allocates an encryption
153 parameters object with default settings. The
154 individual settings can be retrieved and modified
155 with the accessor and mutator functions after
156 this object has been created.
158 This factory function is provided so that individual
159 parameters can be modified without providing many
162 @return New instance of CPBEncryptParms.
165 CPBEncryptParms* self = NewLC();
166 CleanupStack::Pop(self);
170 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC()
172 Similar to the NewL overload which takes no
173 arguments, this function additionally puts the
174 allocated instance of CPBEncryptParms on the
177 @return New instance of CPBEncryptParms.
180 CPBEncryptParms* self = new(ELeave) CPBEncryptParms;
181 CleanupStack::PushL(self);
186 void CPBEncryptParms::ConstructL()
188 Initialize this object with default cipher, kdf (PKCS#5,)
189 salt length, iteration count, and IV.
192 iData = new(ELeave) TParamsData;
193 iData->iKdf = EKdfPkcs5;
195 iSalt = HBufC8::NewMaxL(KPBEDefaultSaltBytes);
196 TPtr8 saltDes = iSalt->Des();
197 TRandom::RandomL(saltDes);
199 iIterations = KDefaultIterations;
201 iIV = HBufC8::NewMaxL(KPBEMaxCipherIVBytes);
204 (TCrypto::Strength() == TCrypto::EStrong)
205 ? KPBEDefaultStrongCipher : KPBEDefaultWeakCipher );
208 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL(TPBECipher aCipher,
209 const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations)
211 CPBEncryptParms* self = NewLC(aCipher, aSalt, aIV, aIterations);
212 CleanupStack::Pop(self);
216 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC(TPBECipher aCipher,
217 const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations)
219 CPBEncryptParms* self = new(ELeave)CPBEncryptParms();
220 CleanupStack::PushL(self);
221 self->ConstructL(aCipher, aSalt, aIV, aIterations);
225 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL(const CPBEncryptParms& aParms)
227 CPBEncryptParms* self = NewLC(aParms);
228 CleanupStack::Pop(self);
232 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC(const CPBEncryptParms& aParms)
234 CPBEncryptParms* self = new(ELeave)CPBEncryptParms();
235 CleanupStack::PushL(self);
236 self->ConstructL(aParms);
240 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
241 // This method is DUPLICATED in common/generic/syslibs/store/ucrypt/ue_strm.cpp
242 EXPORT_C CPBEncryptParms::CPBEncryptParms()
246 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
247 // This method is DUPLICATED in common/generic/syslibs/store/ucrypt/ue_strm.cpp
248 CPBEncryptParms::~CPBEncryptParms()
255 void CPBEncryptParms::ConstructL(TPBECipher aCipher, const TDesC8& aSalt,
256 const TDesC8& aIV, TUint aIterations)
258 iData = new(ELeave) TParamsData;
259 iData->iCipher = aCipher;
260 iData->iKdf = EKdfPkcs5;
261 iSalt = aSalt.AllocL();
263 iIterations = aIterations;
266 void CPBEncryptParms::ConstructL(const CPBEncryptParms& aParms)
268 iData = new(ELeave) TParamsData;
269 iData->iCipher = aParms.Cipher();
270 iData->iKdf = aParms.iData->iKdf;
271 iSalt = aParms.Salt().AllocL();
272 iIterations = aParms.Iterations();
273 iIV = aParms.IV().AllocL();
276 EXPORT_C TPBECipher CPBEncryptParms::Cipher() const
278 return iData->iCipher;
281 EXPORT_C void CPBEncryptParms::SetCipher(TPBECipher aCipher)
283 Replace the current cipher. This function resizes the
284 IV and replaces its existing contents.
286 @param aCipher New cipher.
289 TPtr8 ivDes = iIV->Des();
290 ivDes.SetLength(PBE::GetBlockBytes(aCipher));
291 TRandom::RandomL(ivDes);
293 iData->iCipher = aCipher;
296 EXPORT_C CPBEncryptParms::TKdf CPBEncryptParms::Kdf() const
298 Accessor function returns the key derivation function
299 (KDF) specified by this object.
301 @return KDF specified by this object.
307 EXPORT_C void CPBEncryptParms::SetKdf(CPBEncryptParms::TKdf aKdf)
309 Replace the current key derivation function.
311 @param aKdf Key derivation function.
317 EXPORT_C TPtrC8 CPBEncryptParms::Salt() const
319 return TPtrC8(*iSalt);
322 EXPORT_C void CPBEncryptParms::ResizeSaltL(TInt aNewLen)
324 Resize the current salt and replace its contents.
326 @param aNewLen New salt length.
329 iSalt = iSalt->ReAllocL(aNewLen);
330 TPtr8 saltDes = iSalt->Des();
331 TRandom::RandomL(saltDes);
334 EXPORT_C TInt CPBEncryptParms::Iterations() const
339 EXPORT_C void CPBEncryptParms::SetIterations(TInt aIterCount)
341 Replace the current iteration count with the supplied value.
343 @param aIterCount Number of iterations to apply in
347 ASSERT(aIterCount >= 0);
348 iIterations = aIterCount;
351 EXPORT_C TPtrC8 CPBEncryptParms::IV() const
356 EXPORT_C void CPBEncryptParms::SetIV(const TDesC8& aNewIv)
358 Replace the initialization vector.
360 @param aNewIv New initialization vector length.
361 This must have no more than
362 KPBEMaxCipherIVBytes bytes.
365 iIV->Des().Copy(aNewIv);
368 void CPBEncryptParms::DeriveKeyL(const TDesC8& aPassword, TDes8& aKeyBuf) const
370 Derive a key from this object's kdf, salt, amd iteration count.
372 @param aPassword User-supplied password used to generate key.
373 @param aKeyBuf Buffer to populate with new key.
374 On entry it must be set to the required
380 case CPBEncryptParms::EKdfPkcs5:
381 TPKCS5KDF::DeriveKeyL(aKeyBuf, aPassword, *iSalt, iIterations);
384 case CPBEncryptParms::EKdfPkcs12:
385 PKCS12KDF::DeriveKeyL(aKeyBuf, PKCS12KDF::EIDByteEncryptKey, aPassword, *iSalt, iIterations);
396 EXPORT_C CPBAuthData* CPBAuthData::NewL(const TDesC8& aPassword,
397 const TDesC8& aSalt, TUint aKeySize, TUint aIterations)
399 CPBAuthData* self = NewLC(aPassword, aSalt, aKeySize, aIterations);
400 CleanupStack::Pop(self);
404 EXPORT_C CPBAuthData* CPBAuthData::NewLC(const TDesC8& aPassword,
405 const TDesC8& aSalt, TUint aKeySize, TUint aIterations)
407 CPBAuthData* self = new(ELeave)CPBAuthData();
408 CleanupStack::PushL(self);
409 self->ConstructL(aPassword, aSalt, aKeySize, aIterations);
413 EXPORT_C CPBAuthData* CPBAuthData::NewL(const CPBAuthData& aData)
415 CPBAuthData* self = NewLC(aData);
416 CleanupStack::Pop(self);
420 EXPORT_C CPBAuthData* CPBAuthData::NewLC(const CPBAuthData& aData)
422 CPBAuthData* self = new(ELeave)CPBAuthData();
423 CleanupStack::PushL(self);
424 self->ConstructL(aData);
428 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
429 // This method is DUPLICATED in common/generic/syslibs/store/ucrypt/ue_strm.cpp
430 EXPORT_C CPBAuthData::CPBAuthData()
434 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
435 // This method is DUPLICATED in common/generic/syslibs/store/ucrypt/ue_strm.cpp
436 CPBAuthData::~CPBAuthData()
442 void CPBAuthData::ConstructL(const TDesC8& aPassword, const TDesC8& aSalt,
443 TUint aKeySize, TUint aIterations)
445 iSalt = aSalt.AllocL();
446 iIterations = aIterations;
447 iAuthKey = HBufC8::NewMaxL(aKeySize);
448 TPtr8 authKeyPtr = iAuthKey->Des();
449 TPKCS5KDF::DeriveKeyL(authKeyPtr, aPassword, *iSalt, iIterations);
452 void CPBAuthData::ConstructL(const CPBAuthData& aData)
454 iAuthKey = aData.Key().AllocL();
455 iSalt = aData.Salt().AllocL();
456 iIterations = aData.Iterations();
459 EXPORT_C TPtrC8 CPBAuthData::Key() const
461 return TPtrC8(*iAuthKey);
464 EXPORT_C TPtrC8 CPBAuthData::Salt() const
466 return TPtrC8(*iSalt);
469 EXPORT_C TInt CPBAuthData::Iterations() const
474 EXPORT_C TBool CPBAuthData::operator==(const CPBAuthData& aAuth) const
476 //if the key's are equal, the its true, as the other members are used in key derivation
477 return (*iAuthKey == aAuth.Key());