crypto/weakcrypto/source/pbe/pbedata.cpp
changeset 72 de46a57f75fb
equal deleted inserted replaced
65:970c0057d9bc 72:de46a57f75fb
       
     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 
       
    20 #include "pkcs5kdf.h"
       
    21 #include "pkcs12kdf.h"
       
    22 #include "pbedata.h"
       
    23 #include "pbesymmetricfactory.h"
       
    24 #include "cryptostrength.h"
       
    25 
       
    26 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(const TDesC8& aPassword,
       
    27 	TPBECipher aCipher, const TDesC8& aAuthSalt, 
       
    28 	const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations)
       
    29 	{
       
    30 	CPBEncryptionData* self = NewLC(aPassword, aCipher, aAuthSalt, aEncryptSalt,
       
    31 		aIV, aIterations);
       
    32 	CleanupStack::Pop(self);
       
    33 	return self;
       
    34 	}
       
    35 
       
    36 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewLC(const TDesC8& aPassword,
       
    37 	TPBECipher aCipher, const TDesC8& aAuthSalt, 
       
    38 	const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations)
       
    39 	{
       
    40 	CPBEncryptionData* self = new(ELeave)CPBEncryptionData();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL(aPassword, aCipher, aAuthSalt, aEncryptSalt, aIV,
       
    43 		aIterations);
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(
       
    48 	const CPBEncryptionData& aData)
       
    49 	{
       
    50 	CPBEncryptionData* self = NewLC(aData);
       
    51 	CleanupStack::Pop(self);
       
    52 	return self;
       
    53 	}
       
    54 
       
    55 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewLC(
       
    56 	const CPBEncryptionData& aData)
       
    57 	{
       
    58 	CPBEncryptionData* self = new(ELeave)CPBEncryptionData(); 
       
    59 	CleanupStack::PushL(self);
       
    60 	self->ConstructL(aData);
       
    61 	return self;
       
    62 	}
       
    63 
       
    64 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(
       
    65 	const TDesC8& aPassword, const TDesC8& aAuthSalt,
       
    66 	const CPBEncryptParms& aParms)
       
    67 /**
       
    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.
       
    73 
       
    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
       
    82 							key.
       
    83 	@param	aParms			Encryption parameters describe how the
       
    84 							data is encrypted.
       
    85 	@return					New instance of CPBEncryptionData.
       
    86  */
       
    87 	{
       
    88 	CPBEncryptionData* self = new(ELeave) CPBEncryptionData;
       
    89 	CleanupStack::PushL(self);
       
    90 	self->ConstructL(aPassword, aAuthSalt, aParms);
       
    91 	CleanupStack::Pop(self);
       
    92 	return self;
       
    93 	}
       
    94 
       
    95 void CPBEncryptionData::ConstructL(
       
    96 	const TDesC8& aPassword, const TDesC8& aAuthSalt,
       
    97 	const CPBEncryptParms& aParms)
       
    98 /**
       
    99 	Second-phase constructor for factory function with
       
   100 	same signature.
       
   101  */
       
   102 	{
       
   103 	iParms = CPBEncryptParms::NewL(aParms);
       
   104 	iAuth = CPBAuthData::NewL(
       
   105 		aPassword,
       
   106 		aAuthSalt,
       
   107 		PBE::GetKeyBytes(aParms.Cipher()),
       
   108 		aParms.Iterations());
       
   109 	}
       
   110 
       
   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)
       
   114 	{
       
   115 	}
       
   116 
       
   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)
       
   120 	{
       
   121 	delete iParms;
       
   122 	delete iAuth;
       
   123 	}
       
   124 
       
   125 void CPBEncryptionData::ConstructL(const TDesC8& aPassword,
       
   126 	TPBECipher aCipher, const TDesC8& aAuthSalt, 
       
   127 	const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations)
       
   128 	{
       
   129 	iParms = CPBEncryptParms::NewL(aCipher, aEncryptSalt, aIV, aIterations);
       
   130 	iAuth = CPBAuthData::NewL(aPassword, aAuthSalt,
       
   131 		PBE::GetKeyBytes(aCipher), aIterations);
       
   132 	}
       
   133 
       
   134 void CPBEncryptionData::ConstructL(const CPBEncryptionData& aData)
       
   135 	{
       
   136 	iParms = CPBEncryptParms::NewL(aData.EncryptParms());
       
   137 	iAuth = CPBAuthData::NewL(aData.AuthData());
       
   138 	}
       
   139 
       
   140 EXPORT_C const CPBEncryptParms& CPBEncryptionData::EncryptParms(void) const
       
   141 	{
       
   142 	return *iParms;
       
   143 	} 
       
   144 EXPORT_C const CPBAuthData& CPBEncryptionData::AuthData(void) const
       
   145 	{
       
   146 	return *iAuth;
       
   147 	}
       
   148 
       
   149 /* CPBEncryptParms */
       
   150 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL()
       
   151 /**
       
   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.
       
   157 
       
   158 	This factory function is provided so that individual
       
   159 	parameters can be modified without providing many
       
   160 	factory functions.
       
   161 
       
   162 	@return					New instance of CPBEncryptParms.
       
   163  */
       
   164 	{
       
   165 	CPBEncryptParms* self = NewLC();
       
   166 	CleanupStack::Pop(self);
       
   167 	return self;
       
   168 	}
       
   169 
       
   170 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC()
       
   171 /**
       
   172 	Similar to the NewL overload which takes no
       
   173 	arguments, this function additionally puts the
       
   174 	allocated instance of CPBEncryptParms on the
       
   175 	cleanup stack.
       
   176 
       
   177 	@return					New instance of CPBEncryptParms.
       
   178  */
       
   179 	{
       
   180 	CPBEncryptParms* self = new(ELeave) CPBEncryptParms;
       
   181 	CleanupStack::PushL(self);
       
   182 	self->ConstructL();
       
   183 	return self;
       
   184 	}
       
   185 
       
   186 void CPBEncryptParms::ConstructL()
       
   187 /**
       
   188 	Initialize this object with default cipher, kdf (PKCS#5,)
       
   189 	salt length, iteration count, and IV.
       
   190  */
       
   191 	{
       
   192 	iData = new(ELeave) TParamsData;
       
   193 	iData->iKdf = EKdfPkcs5;
       
   194 	
       
   195 	iSalt = HBufC8::NewMaxL(KPBEDefaultSaltBytes);
       
   196 	TPtr8 saltDes = iSalt->Des();
       
   197 	TRandom::RandomL(saltDes);
       
   198 	
       
   199 	iIterations = KDefaultIterations;
       
   200 	
       
   201 	iIV = HBufC8::NewMaxL(KPBEMaxCipherIVBytes);
       
   202 	
       
   203 	SetCipher(
       
   204 			(TCrypto::Strength() == TCrypto::EStrong)
       
   205 		?	KPBEDefaultStrongCipher : KPBEDefaultWeakCipher );
       
   206 	}
       
   207 
       
   208 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL(TPBECipher aCipher,
       
   209 	const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations)
       
   210 	{
       
   211 	CPBEncryptParms* self = NewLC(aCipher, aSalt, aIV, aIterations);
       
   212 	CleanupStack::Pop(self);
       
   213 	return self;
       
   214 	}
       
   215 
       
   216 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC(TPBECipher aCipher,
       
   217 	const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations) 
       
   218 	{
       
   219 	CPBEncryptParms* self = new(ELeave)CPBEncryptParms();
       
   220 	CleanupStack::PushL(self);
       
   221 	self->ConstructL(aCipher, aSalt, aIV, aIterations);
       
   222 	return self;
       
   223 	}
       
   224 
       
   225 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL(const CPBEncryptParms& aParms)
       
   226 	{
       
   227 	CPBEncryptParms* self = NewLC(aParms);
       
   228 	CleanupStack::Pop(self);
       
   229 	return self;
       
   230 	}
       
   231 
       
   232 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC(const CPBEncryptParms& aParms)
       
   233 	{
       
   234 	CPBEncryptParms* self = new(ELeave)CPBEncryptParms();
       
   235 	CleanupStack::PushL(self);
       
   236 	self->ConstructL(aParms);
       
   237 	return self;
       
   238 	}
       
   239 
       
   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()
       
   243 	{
       
   244 	}
       
   245 
       
   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()
       
   249 	{
       
   250 	delete iData;
       
   251 	delete iSalt;
       
   252 	delete iIV;	
       
   253 	}
       
   254 
       
   255 void CPBEncryptParms::ConstructL(TPBECipher aCipher, const TDesC8& aSalt,
       
   256 	const TDesC8& aIV, TUint aIterations)
       
   257 	{
       
   258 	iData = new(ELeave) TParamsData;
       
   259 	iData->iCipher = aCipher;
       
   260 	iData->iKdf = EKdfPkcs5;
       
   261 	iSalt = aSalt.AllocL();
       
   262 	iIV = aIV.AllocL();
       
   263 	iIterations = aIterations;
       
   264 	}
       
   265 
       
   266 void CPBEncryptParms::ConstructL(const CPBEncryptParms& aParms)
       
   267 	{
       
   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();
       
   274 	}
       
   275 
       
   276 EXPORT_C TPBECipher CPBEncryptParms::Cipher() const
       
   277 	{
       
   278 	return iData->iCipher;
       
   279 	}
       
   280 
       
   281 EXPORT_C void CPBEncryptParms::SetCipher(TPBECipher aCipher)
       
   282 /**
       
   283 	Replace the current cipher.  This function resizes the
       
   284 	IV and replaces its existing contents.
       
   285 	
       
   286 	@param	aCipher			New cipher.
       
   287  */
       
   288 	{
       
   289 	TPtr8 ivDes = iIV->Des();
       
   290 	ivDes.SetLength(PBE::GetBlockBytes(aCipher));
       
   291 	TRandom::RandomL(ivDes);
       
   292 	
       
   293 	iData->iCipher = aCipher;
       
   294 	}
       
   295 
       
   296 EXPORT_C CPBEncryptParms::TKdf CPBEncryptParms::Kdf() const
       
   297 /**
       
   298 	Accessor function returns the key derivation function
       
   299 	(KDF) specified by this object.
       
   300 
       
   301 	@return					KDF specified by this object.
       
   302  */
       
   303 	{
       
   304 	return iData->iKdf;
       
   305 	}
       
   306 
       
   307 EXPORT_C void CPBEncryptParms::SetKdf(CPBEncryptParms::TKdf aKdf)
       
   308 /**
       
   309 	Replace the current key derivation function.
       
   310 
       
   311 	@param	aKdf			Key derivation function.
       
   312  */
       
   313 	{
       
   314 	iData->iKdf = aKdf;
       
   315 	}
       
   316 
       
   317 EXPORT_C TPtrC8 CPBEncryptParms::Salt() const
       
   318 	{
       
   319 	return TPtrC8(*iSalt);
       
   320 	}
       
   321 
       
   322 EXPORT_C void CPBEncryptParms::ResizeSaltL(TInt aNewLen)
       
   323 /**
       
   324 	Resize the current salt and replace its contents.
       
   325 
       
   326 	@param	aNewLen			New salt length.
       
   327  */
       
   328 	{
       
   329 	iSalt = iSalt->ReAllocL(aNewLen);
       
   330 	TPtr8 saltDes = iSalt->Des();
       
   331 	TRandom::RandomL(saltDes);
       
   332 	}
       
   333 
       
   334 EXPORT_C TInt CPBEncryptParms::Iterations() const
       
   335 	{
       
   336 	return iIterations;
       
   337 	}
       
   338 
       
   339 EXPORT_C void CPBEncryptParms::SetIterations(TInt aIterCount)
       
   340 /**
       
   341 	Replace the current iteration count with the supplied value.
       
   342 	
       
   343 	@param	aIterCount		Number of iterations to apply in
       
   344 							the KDF.
       
   345  */
       
   346 	{
       
   347 	ASSERT(aIterCount >= 0);
       
   348 	iIterations = aIterCount;
       
   349 	}
       
   350 
       
   351 EXPORT_C TPtrC8 CPBEncryptParms::IV() const
       
   352 	{
       
   353 	return TPtrC8(*iIV);
       
   354 	}
       
   355 
       
   356 EXPORT_C void CPBEncryptParms::SetIV(const TDesC8& aNewIv)
       
   357 /**
       
   358 	Replace the initialization vector.
       
   359 	
       
   360 	@param	aNewIv			New initialization vector length.
       
   361 							This must have no more than
       
   362 							KPBEMaxCipherIVBytes bytes.
       
   363  */
       
   364 	{
       
   365 	iIV->Des().Copy(aNewIv);
       
   366 	}
       
   367 
       
   368 void CPBEncryptParms::DeriveKeyL(const TDesC8& aPassword, TDes8& aKeyBuf) const
       
   369 /**
       
   370 	Derive a key from this object's kdf, salt, amd iteration count.
       
   371 	
       
   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
       
   375 							key length.
       
   376  */
       
   377 	{
       
   378 	switch (iData->iKdf)
       
   379 		{
       
   380 	case CPBEncryptParms::EKdfPkcs5:
       
   381 		TPKCS5KDF::DeriveKeyL(aKeyBuf, aPassword, *iSalt, iIterations);
       
   382 		break;
       
   383 	
       
   384 	case CPBEncryptParms::EKdfPkcs12:
       
   385 		PKCS12KDF::DeriveKeyL(aKeyBuf, PKCS12KDF::EIDByteEncryptKey, aPassword, *iSalt, iIterations);
       
   386 		break;
       
   387 	
       
   388 	default:
       
   389 		ASSERT(EFalse);
       
   390 		break;
       
   391 		}
       
   392 	}
       
   393 
       
   394 /* CPBAuthData */
       
   395 
       
   396 EXPORT_C CPBAuthData* CPBAuthData::NewL(const TDesC8& aPassword,
       
   397 	const TDesC8& aSalt, TUint aKeySize, TUint aIterations)
       
   398 	{
       
   399 	CPBAuthData* self = NewLC(aPassword, aSalt, aKeySize, aIterations);
       
   400 	CleanupStack::Pop(self);
       
   401 	return self;
       
   402 	}
       
   403 
       
   404 EXPORT_C CPBAuthData* CPBAuthData::NewLC(const TDesC8& aPassword,
       
   405 	const TDesC8& aSalt, TUint aKeySize, TUint aIterations) 
       
   406 	{
       
   407 	CPBAuthData* self = new(ELeave)CPBAuthData();
       
   408 	CleanupStack::PushL(self);
       
   409 	self->ConstructL(aPassword, aSalt, aKeySize, aIterations);
       
   410 	return self;
       
   411 	}
       
   412 
       
   413 EXPORT_C CPBAuthData* CPBAuthData::NewL(const CPBAuthData& aData)
       
   414 	{
       
   415 	CPBAuthData* self = NewLC(aData);
       
   416 	CleanupStack::Pop(self);
       
   417 	return self;
       
   418 	}
       
   419 
       
   420 EXPORT_C CPBAuthData* CPBAuthData::NewLC(const CPBAuthData& aData)
       
   421 	{
       
   422 	CPBAuthData* self = new(ELeave)CPBAuthData();
       
   423 	CleanupStack::PushL(self);
       
   424 	self->ConstructL(aData);
       
   425 	return self;
       
   426 	}
       
   427 
       
   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()
       
   431 	{
       
   432 	}
       
   433 
       
   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()
       
   437 	{
       
   438 	delete iAuthKey;
       
   439 	delete iSalt;
       
   440 	}
       
   441 
       
   442 void CPBAuthData::ConstructL(const TDesC8& aPassword, const TDesC8& aSalt, 
       
   443 	TUint aKeySize, TUint aIterations)
       
   444 	{
       
   445 	iSalt = aSalt.AllocL();
       
   446 	iIterations = aIterations;
       
   447 	iAuthKey = HBufC8::NewMaxL(aKeySize);
       
   448 	TPtr8 authKeyPtr = iAuthKey->Des();
       
   449 	TPKCS5KDF::DeriveKeyL(authKeyPtr, aPassword, *iSalt, iIterations);
       
   450 	}
       
   451 
       
   452 void CPBAuthData::ConstructL(const CPBAuthData& aData)
       
   453 	{
       
   454 	iAuthKey = aData.Key().AllocL();
       
   455 	iSalt = aData.Salt().AllocL();
       
   456 	iIterations = aData.Iterations();
       
   457 	}
       
   458 
       
   459 EXPORT_C TPtrC8 CPBAuthData::Key() const
       
   460 	{
       
   461 	return TPtrC8(*iAuthKey);
       
   462 	}
       
   463 
       
   464 EXPORT_C TPtrC8 CPBAuthData::Salt() const
       
   465 	{
       
   466 	return TPtrC8(*iSalt);
       
   467 	}
       
   468 
       
   469 EXPORT_C TInt CPBAuthData::Iterations() const
       
   470 	{
       
   471 	return iIterations;
       
   472 	}
       
   473 
       
   474 EXPORT_C TBool CPBAuthData::operator==(const CPBAuthData& aAuth) const
       
   475 	{
       
   476 	//if the key's are equal, the its true, as the other members are used in key derivation
       
   477 	return (*iAuthKey == aAuth.Key());
       
   478 	}
       
   479