os/security/crypto/weakcrypto/source/pbe/pbeset.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <e32std.h>
sl@0
    20
#include <random.h>
sl@0
    21
#include <pbedata.h>
sl@0
    22
#include "pkcs5kdf.h"
sl@0
    23
#include "pbencryptor.h"
sl@0
    24
#include "pbe.h"
sl@0
    25
#include <securityerr.h>
sl@0
    26
#include "pbesymmetricfactory.h"
sl@0
    27
sl@0
    28
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewL(const TPBPassword& aPassword)
sl@0
    29
	{
sl@0
    30
	CPBEncryptSet* self = NewLC(aPassword);
sl@0
    31
	CleanupStack::Pop();
sl@0
    32
	return self;
sl@0
    33
	}
sl@0
    34
sl@0
    35
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewLC(const TPBPassword& aPassword)
sl@0
    36
	{
sl@0
    37
	CPBEncryptSet* self = new(ELeave) CPBEncryptSet;
sl@0
    38
	CleanupStack::PushL(self);
sl@0
    39
	self->ConstructL(aPassword.Password());
sl@0
    40
	return self;
sl@0
    41
	}
sl@0
    42
sl@0
    43
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewL(const TPBPassword& aPassword, 
sl@0
    44
	const TPBECipher aCipher)
sl@0
    45
	{
sl@0
    46
	CPBEncryptSet* self = NewLC(aPassword, aCipher);
sl@0
    47
	CleanupStack::Pop();
sl@0
    48
	return self;
sl@0
    49
	}
sl@0
    50
sl@0
    51
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewLC(const TPBPassword& aPassword, 
sl@0
    52
	const TPBECipher aCipher)
sl@0
    53
	{
sl@0
    54
	CPBEncryptSet* self = new(ELeave) CPBEncryptSet;
sl@0
    55
	CleanupStack::PushL(self);
sl@0
    56
	self->ConstructL(aPassword.Password(), aCipher);
sl@0
    57
	return self;
sl@0
    58
	}
sl@0
    59
sl@0
    60
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewL(const TPBPassword& aPassword,
sl@0
    61
	const CPBEncryptParms& aParms)
sl@0
    62
	{
sl@0
    63
	CPBEncryptSet* self = NewLC(aPassword, aParms);
sl@0
    64
	CleanupStack::Pop();
sl@0
    65
	return self;
sl@0
    66
	}
sl@0
    67
sl@0
    68
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewLC(const TPBPassword& aPassword, 
sl@0
    69
	const CPBEncryptParms& aParms)
sl@0
    70
	{
sl@0
    71
	CPBEncryptSet* self = new(ELeave) CPBEncryptSet;
sl@0
    72
	CleanupStack::PushL(self);
sl@0
    73
	self->ConstructL(aPassword.Password(), aParms);
sl@0
    74
	return self;
sl@0
    75
	}
sl@0
    76
sl@0
    77
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewL(const CPBEncryptionData& aData, 
sl@0
    78
	const TDesC8& aEncryptedKey, const TPBPassword& aPassword)
sl@0
    79
	{
sl@0
    80
	CPBEncryptSet* self = NewLC(aData, aEncryptedKey, aPassword);
sl@0
    81
	CleanupStack::Pop();
sl@0
    82
	return self;
sl@0
    83
	}
sl@0
    84
sl@0
    85
EXPORT_C CPBEncryptSet* CPBEncryptSet::NewLC(const CPBEncryptionData& aData,
sl@0
    86
	const TDesC8& aEncryptedKey, const TPBPassword& aPassword)
sl@0
    87
	{
sl@0
    88
	CPBEncryptSet* self = new(ELeave) CPBEncryptSet;
sl@0
    89
	CleanupStack::PushL(self);
sl@0
    90
	self->ConstructL(aData, aEncryptedKey, aPassword);
sl@0
    91
	return self;
sl@0
    92
	}
sl@0
    93
sl@0
    94
void CPBEncryptSet::ConstructL(const TDesC8& aPassword)
sl@0
    95
	{
sl@0
    96
	CPBEncryptElement::ConstructL(aPassword);
sl@0
    97
	ConstructMasterKeyL();
sl@0
    98
	}
sl@0
    99
sl@0
   100
void CPBEncryptSet::ConstructL(const TDesC8& aPassword,
sl@0
   101
	const TPBECipher aCipher)
sl@0
   102
	{
sl@0
   103
	CPBEncryptElement::ConstructL(aPassword, aCipher);
sl@0
   104
	ConstructMasterKeyL();
sl@0
   105
	}
sl@0
   106
sl@0
   107
void CPBEncryptSet::ConstructL(const TDesC8& aPassword, 
sl@0
   108
	const CPBEncryptParms& aParms)
sl@0
   109
	{
sl@0
   110
	CPBEncryptElement::ConstructL(aPassword, aParms);
sl@0
   111
	ConstructMasterKeyL();
sl@0
   112
	}
sl@0
   113
sl@0
   114
void CPBEncryptSet::ConstructMasterKeyL(void)
sl@0
   115
	{
sl@0
   116
	TBuf8<KAESKeyBytes256> masterKey(KAESKeyBytes256);
sl@0
   117
	TRandom::RandomL(masterKey);
sl@0
   118
	iEncryptedMasterKey = HBufC8::NewL(KAESKeyBytes256);
sl@0
   119
	EncryptMasterKeyL(masterKey);
sl@0
   120
	}
sl@0
   121
sl@0
   122
void CPBEncryptSet::ConstructL(const CPBEncryptionData& aData,
sl@0
   123
	const TDesC8& aEncryptedMasterKey, const TPBPassword& aPassword)
sl@0
   124
	{
sl@0
   125
	CPBEncryptElement::ConstructL(aData, aPassword);
sl@0
   126
	iEncryptedMasterKey = aEncryptedMasterKey.AllocL();
sl@0
   127
	}
sl@0
   128
sl@0
   129
EXPORT_C void CPBEncryptSet::ChangePasswordL(const TPBPassword& aNewPassword)
sl@0
   130
	{
sl@0
   131
	//1) Decrypt master key with old encrypt key 
sl@0
   132
	TBuf8<KPBEMaxCipherKeyBytes> masterKey;
sl@0
   133
	DecryptMasterKeyL(masterKey);
sl@0
   134
sl@0
   135
	//2) create new encrypt parms
sl@0
   136
sl@0
   137
	TBuf8<KPBEMaxSaltBytes> authSalt(KPBEMaxSaltBytes);
sl@0
   138
	TRandom::RandomL(authSalt);
sl@0
   139
sl@0
   140
	//3) create a totally new CPBEncryptionData representing the new password
sl@0
   141
	CPBEncryptionData* newData = CPBEncryptionData::NewL(
sl@0
   142
		aNewPassword.Password(), authSalt, iData->EncryptParms());
sl@0
   143
sl@0
   144
	delete iData;
sl@0
   145
	iData = newData;
sl@0
   146
sl@0
   147
	// regenerate the password using a maximum length salt.
sl@0
   148
	CPBEncryptParms& epNonConst =
sl@0
   149
		const_cast<CPBEncryptParms&>(iData->EncryptParms());
sl@0
   150
	epNonConst.ResizeSaltL(KPBEMaxSaltBytes);
sl@0
   151
sl@0
   152
	TPtr8 iEncryptKeyBuf(iEncryptKey->Des());
sl@0
   153
	iEncryptKeyBuf.SetLength(PBE::GetKeyBytes(iData->EncryptParms().Cipher()));
sl@0
   154
	
sl@0
   155
	iData->EncryptParms().DeriveKeyL(aNewPassword.Password(), iEncryptKeyBuf);
sl@0
   156
sl@0
   157
	//4) Encrypt master key with new encrypt key
sl@0
   158
	EncryptMasterKeyL(masterKey);
sl@0
   159
	}
sl@0
   160
sl@0
   161
EXPORT_C const TDesC8& CPBEncryptSet::EncryptedMasterKey(void) const
sl@0
   162
	{
sl@0
   163
	return *iEncryptedMasterKey;
sl@0
   164
	}
sl@0
   165
sl@0
   166
CPBEncryptor* CPBEncryptSet::NewEncryptLC(void) const
sl@0
   167
	{
sl@0
   168
	CPBEncryptor* encryptor = NewEncryptL();
sl@0
   169
	CleanupStack::PushL(encryptor);
sl@0
   170
	return encryptor;
sl@0
   171
	}
sl@0
   172
sl@0
   173
CPBEncryptor* CPBEncryptSet::NewEncryptL(void) const
sl@0
   174
	{
sl@0
   175
	TBuf8<KPBEMaxCipherKeyBytes> masterKey;
sl@0
   176
	DecryptMasterKeyL(masterKey);
sl@0
   177
sl@0
   178
	CPBEncryptor* encryptor = 0;
sl@0
   179
	//make sure the masterkey we pass is exactly the right length for the cipher
sl@0
   180
	encryptor = CPBEncryptorSet::NewL(iData->EncryptParms().Cipher(),
sl@0
   181
		masterKey.Left(PBE::GetKeyBytes(iData->EncryptParms().Cipher())));		
sl@0
   182
	return encryptor;
sl@0
   183
	}
sl@0
   184
sl@0
   185
CPBDecryptor* CPBEncryptSet::NewDecryptLC(void) const
sl@0
   186
	{
sl@0
   187
	CPBDecryptor* decryptor = NewDecryptL();
sl@0
   188
	CleanupStack::PushL(decryptor);
sl@0
   189
	return decryptor;
sl@0
   190
	}
sl@0
   191
sl@0
   192
CPBDecryptor* CPBEncryptSet::NewDecryptL(void) const
sl@0
   193
	{
sl@0
   194
	TBuf8<KPBEMaxCipherKeyBytes> masterKey;
sl@0
   195
	DecryptMasterKeyL(masterKey);
sl@0
   196
sl@0
   197
	CPBDecryptor* decryptor = 0;
sl@0
   198
	//make sure the masterkey we pass is exactly the right length for the cipher
sl@0
   199
	decryptor = CPBDecryptorSet::NewL(iData->EncryptParms().Cipher(),
sl@0
   200
		masterKey.Left(PBE::GetKeyBytes(iData->EncryptParms().Cipher())));		
sl@0
   201
	return decryptor;
sl@0
   202
	}
sl@0
   203
sl@0
   204
void CPBEncryptSet::DecryptMasterKeyL(TDes8& aMasterKey) const
sl@0
   205
	{
sl@0
   206
	CPBDecryptorElement* decryptor = CPBDecryptorElement::NewLC(
sl@0
   207
		iData->EncryptParms().Cipher(), *iEncryptKey, iData->EncryptParms().IV());
sl@0
   208
	aMasterKey.SetLength(0);
sl@0
   209
	decryptor->Process(*iEncryptedMasterKey, aMasterKey);
sl@0
   210
	CleanupStack::PopAndDestroy(decryptor);
sl@0
   211
	}
sl@0
   212
sl@0
   213
void CPBEncryptSet::EncryptMasterKeyL(const TDesC8& aMasterKey)
sl@0
   214
	{
sl@0
   215
	CPBEncryptorElement* encryptor = CPBEncryptorElement::NewLC(
sl@0
   216
		iData->EncryptParms().Cipher(), *iEncryptKey, iData->EncryptParms().IV());
sl@0
   217
	TPtr8 encryptedMasterKeyBuf(iEncryptedMasterKey->Des());
sl@0
   218
	encryptedMasterKeyBuf.SetLength(0);
sl@0
   219
	encryptor->Process(aMasterKey, encryptedMasterKeyBuf);
sl@0
   220
	CleanupStack::PopAndDestroy(encryptor);
sl@0
   221
	}
sl@0
   222
sl@0
   223
CPBEncryptSet::CPBEncryptSet()
sl@0
   224
	{
sl@0
   225
	}
sl@0
   226
sl@0
   227
CPBEncryptSet::~CPBEncryptSet()
sl@0
   228
	{
sl@0
   229
	delete iEncryptedMasterKey;
sl@0
   230
	}
sl@0
   231
sl@0
   232
// Warning: This function is only valid BEFORE you call NewEncryptL
sl@0
   233
// After creating the cipher, ask it about itself, not me!
sl@0
   234
// This is _very_ dodgy as I assume all sorts of things about the encryptor.
sl@0
   235
// 1) That it uses SSLv3 or similar style padding
sl@0
   236
// 2) That it stores the IV for that stream at the front.
sl@0
   237
// This is here for specific application that requires this and aren't able to
sl@0
   238
// actually construct the cipher and ask it.  In almost all other cases you
sl@0
   239
// should construct the cipher and ask it.
sl@0
   240
TInt CPBEncryptSet::MaxCiphertextLength(TInt aPlaintextLength) const
sl@0
   241
    {
sl@0
   242
	TUint blocksize = PBE::GetBlockBytes(iData->EncryptParms().Cipher());
sl@0
   243
	TUint padding = blocksize - aPlaintextLength % blocksize;
sl@0
   244
	//totallength = blocksize of iv hidden at beginning + inputLength + padding
sl@0
   245
	return blocksize + aPlaintextLength + padding;
sl@0
   246
    }
sl@0
   247
sl@0
   248
// Warning: This function is only valid BEFORE you call NewDecryptL
sl@0
   249
// After creating the cipher, ask it about itself, not me!
sl@0
   250
TInt CPBEncryptSet::MaxPlaintextLength(TInt aCiphertextLength) const
sl@0
   251
    {
sl@0
   252
	/*It's impossible to determine anything about how much padding will be
sl@0
   253
	 * removed.  So we'll return a max length that is longer than will ever
sl@0
   254
	 * happen by at most a blocksize - 1.
sl@0
   255
	 */
sl@0
   256
	//In all cases SSLv3 padding has at least one byte of padding.
sl@0
   257
	TUint blocksize = PBE::GetBlockBytes(iData->EncryptParms().Cipher());
sl@0
   258
	//totallength = inputlength - iv hidden at beginning - 1 byte of padding
sl@0
   259
    return aCiphertextLength - blocksize - 1;
sl@0
   260
    }
sl@0
   261