sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-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 |
/**
|
sl@0
|
20 |
@file
|
sl@0
|
21 |
@internalTechnology
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
#include "symmetriccipherstepbase.h"
|
sl@0
|
25 |
#include <bigint.h>
|
sl@0
|
26 |
|
sl@0
|
27 |
|
sl@0
|
28 |
/**
|
sl@0
|
29 |
Common setup of a crypto SPI symmetric cipher.
|
sl@0
|
30 |
@param aArc4Check Determines whether the check is run on the cipher type for ARC4 and if it matches, sets up the CCryptoParams accordingly.
|
sl@0
|
31 |
@param aRc2Check Determines whether the check is run on the cipher type for RC2 and if it matches, sets up the CCryptoParams accordingly.
|
sl@0
|
32 |
@param aOperationMode On return is the operation mode read in from the config file and used in the initial setup of the cipher.
|
sl@0
|
33 |
@param aCipher On return the symmetric cipher implementation.
|
sl@0
|
34 |
@param aKey On return aCipher's key object. This is should not be deleted until after aCipher is deleted.
|
sl@0
|
35 |
@return The error value from the call to the symmetric cipher creation function.
|
sl@0
|
36 |
*/
|
sl@0
|
37 |
void CSymmetricCipherStepBase::SetupCipherL(TBool aArc4Check, TBool aRc2Check, TVariantPtrC& aOperationMode, CSymmetricCipher*& aCipher, CKey*& aKey)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
TPtrC keyPath;
|
sl@0
|
40 |
TVariantPtrC algorithm;
|
sl@0
|
41 |
TVariantPtrC paddingMode;
|
sl@0
|
42 |
|
sl@0
|
43 |
if( !GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
|
sl@0
|
44 |
!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
|
sl@0
|
45 |
!GetStringFromConfig(ConfigSection(),KConfigOperationMode, aOperationMode) ||
|
sl@0
|
46 |
!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
|
sl@0
|
47 |
{
|
sl@0
|
48 |
User::Leave(KErrNotFound);
|
sl@0
|
49 |
}
|
sl@0
|
50 |
|
sl@0
|
51 |
// Create key
|
sl@0
|
52 |
TKeyProperty keyProperty;
|
sl@0
|
53 |
CCryptoParams* params = CCryptoParams::NewLC();
|
sl@0
|
54 |
|
sl@0
|
55 |
CFileReader* keyData = CFileReader::NewLC(keyPath);
|
sl@0
|
56 |
params->AddL( *keyData, KSymmetricKeyParameterUid);
|
sl@0
|
57 |
|
sl@0
|
58 |
aKey=CKey::NewL(keyProperty, *params);
|
sl@0
|
59 |
CleanupStack::PushL(aKey);
|
sl@0
|
60 |
|
sl@0
|
61 |
CCryptoParams* xparams = NULL;
|
sl@0
|
62 |
if ((aArc4Check) && (TUid(algorithm) == KArc4Uid))
|
sl@0
|
63 |
{
|
sl@0
|
64 |
// Set the RC4 DiscardBytes to 0
|
sl@0
|
65 |
xparams = CCryptoParams::NewLC();
|
sl@0
|
66 |
xparams->AddL(NULL, KARC4DiscardBytes);
|
sl@0
|
67 |
|
sl@0
|
68 |
// Create a Symmetric Cipher with the values from the ini file
|
sl@0
|
69 |
CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
|
sl@0
|
70 |
CleanupStack::PopAndDestroy(xparams);
|
sl@0
|
71 |
CleanupStack::Pop(aKey);
|
sl@0
|
72 |
CleanupStack::PopAndDestroy(2, params);
|
sl@0
|
73 |
}
|
sl@0
|
74 |
else if ((aRc2Check) && (TUid(algorithm) == KRc2Uid))
|
sl@0
|
75 |
{
|
sl@0
|
76 |
TInt keylen = TPtrC8(*keyData).Length() * 8;
|
sl@0
|
77 |
xparams = CCryptoParams::NewLC();
|
sl@0
|
78 |
|
sl@0
|
79 |
// Set the RC2 EffectiveKeyLen according to the input key size
|
sl@0
|
80 |
xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
|
sl@0
|
81 |
|
sl@0
|
82 |
// Create a Symmetric Cipher with the values from the ini file
|
sl@0
|
83 |
CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
|
sl@0
|
84 |
CleanupStack::PopAndDestroy(xparams);
|
sl@0
|
85 |
CleanupStack::Pop(aKey);
|
sl@0
|
86 |
CleanupStack::PopAndDestroy(2, params);
|
sl@0
|
87 |
}
|
sl@0
|
88 |
else
|
sl@0
|
89 |
{
|
sl@0
|
90 |
// Create a Symmetric Cipher with the values from the ini file
|
sl@0
|
91 |
CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
|
sl@0
|
92 |
CleanupStack::Pop(aKey);
|
sl@0
|
93 |
CleanupStack::PopAndDestroy(2, params);
|
sl@0
|
94 |
}
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
|
sl@0
|
98 |
/**
|
sl@0
|
99 |
When running in CTR mode call this function to calculate the block size of a cipher.
|
sl@0
|
100 |
@param aCipher The cipher whose block size is returned.
|
sl@0
|
101 |
@return The block size in bits.
|
sl@0
|
102 |
*/
|
sl@0
|
103 |
TInt CSymmetricCipherStepBase::CtrModeCalcBlockSizeL(CSymmetricCipher& aCipher)
|
sl@0
|
104 |
{
|
sl@0
|
105 |
// aCipher MUST be running in CTR mode
|
sl@0
|
106 |
aCipher.SetOperationModeL(KOperationModeCBCUid);
|
sl@0
|
107 |
TInt blockSize = aCipher.BlockSize();
|
sl@0
|
108 |
aCipher.SetOperationModeL(KOperationModeCTRUid);
|
sl@0
|
109 |
return blockSize;
|
sl@0
|
110 |
}
|
sl@0
|
111 |
|
sl@0
|
112 |
|
sl@0
|
113 |
/**
|
sl@0
|
114 |
Read in the plaintext from the course file listed in the configuration file.
|
sl@0
|
115 |
@param aPlaintext Descriptor pointing to the plaintext.
|
sl@0
|
116 |
@param aReader This CFileReader pointer must be NULL when passed in. It must not be deleted until after the client has finished with the plaintext.
|
sl@0
|
117 |
*/
|
sl@0
|
118 |
HBufC8* CSymmetricCipherStepBase::ReadInPlaintextL()
|
sl@0
|
119 |
{
|
sl@0
|
120 |
TPtrC plaintextPath;
|
sl@0
|
121 |
if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, plaintextPath))
|
sl@0
|
122 |
{
|
sl@0
|
123 |
User::Leave(KErrNotFound);
|
sl@0
|
124 |
}
|
sl@0
|
125 |
|
sl@0
|
126 |
return ReadFileL(plaintextPath);
|
sl@0
|
127 |
}
|
sl@0
|
128 |
|
sl@0
|
129 |
|
sl@0
|
130 |
/**
|
sl@0
|
131 |
Read in the ciphertext from the course file listed in the configuration file.
|
sl@0
|
132 |
@param aCiphertext Descriptor pointing to the ciphertext.
|
sl@0
|
133 |
@param aReader This CFileReader pointer must be NULL when passed in. It must not be deleted until after the client has finished with the ciphertext.
|
sl@0
|
134 |
|
sl@0
|
135 |
*/
|
sl@0
|
136 |
HBufC8* CSymmetricCipherStepBase::ReadInCiphertextL()
|
sl@0
|
137 |
{
|
sl@0
|
138 |
TPtrC ciphertextPath;
|
sl@0
|
139 |
if(!GetStringFromConfig(ConfigSection(),KConfigEncryptedPath, ciphertextPath))
|
sl@0
|
140 |
{
|
sl@0
|
141 |
User::Leave(KErrNotFound);
|
sl@0
|
142 |
}
|
sl@0
|
143 |
|
sl@0
|
144 |
return ReadFileL(ciphertextPath);
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
|
sl@0
|
148 |
/**
|
sl@0
|
149 |
Read in the IV from the course file listed in the configuration file.
|
sl@0
|
150 |
@param aIV Descriptor pointing to the IV.
|
sl@0
|
151 |
@param aReader This CFileReader pointer must be NULL when passed in. It must not be deleted until after the client has finished with the IV.
|
sl@0
|
152 |
|
sl@0
|
153 |
*/
|
sl@0
|
154 |
HBufC8* CSymmetricCipherStepBase::ReadInIvL()
|
sl@0
|
155 |
{
|
sl@0
|
156 |
TPtrC ivPath;
|
sl@0
|
157 |
if(!GetStringFromConfig(ConfigSection(),KConfigIVPath, ivPath))
|
sl@0
|
158 |
{
|
sl@0
|
159 |
User::Leave(KErrNotFound);
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
return ReadFileL(ivPath);
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
|
sl@0
|
166 |
/**
|
sl@0
|
167 |
@param aCounter
|
sl@0
|
168 |
*/
|
sl@0
|
169 |
HBufC8* CSymmetricCipherStepBase::CtrModeIncrementCounterL(TDesC8& aCounter)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
RInteger bigInt = RInteger::NewL(aCounter);
|
sl@0
|
172 |
CleanupClosePushL(bigInt);
|
sl@0
|
173 |
++bigInt;
|
sl@0
|
174 |
HBufC8* result = bigInt.BufferLC();
|
sl@0
|
175 |
CleanupStack::Pop(result);
|
sl@0
|
176 |
CleanupStack::PopAndDestroy();
|
sl@0
|
177 |
return result;
|
sl@0
|
178 |
}
|
sl@0
|
179 |
|
sl@0
|
180 |
HBufC8* CSymmetricCipherStepBase::ReadInHexCiphertextL()
|
sl@0
|
181 |
{
|
sl@0
|
182 |
HBufC8* hex = ReadInCiphertextL();
|
sl@0
|
183 |
CleanupStack::PushL(hex);
|
sl@0
|
184 |
HBufC8* result = ConvertFromHexFormatToRawL((*hex));
|
sl@0
|
185 |
CleanupStack::PopAndDestroy(hex);
|
sl@0
|
186 |
return result;
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
HBufC8* CSymmetricCipherStepBase::ReadInHexPlaintextL()
|
sl@0
|
190 |
{
|
sl@0
|
191 |
HBufC8* hex = ReadInPlaintextL();
|
sl@0
|
192 |
CleanupStack::PushL(hex);
|
sl@0
|
193 |
HBufC8* result = ConvertFromHexFormatToRawL((*hex));
|
sl@0
|
194 |
CleanupStack::PopAndDestroy(hex);
|
sl@0
|
195 |
return result;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
|
sl@0
|
198 |
HBufC8* CSymmetricCipherStepBase::ReadInHexPlainTextL(TPtrC aFile)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
HBufC8* data = ReadFileL(aFile);
|
sl@0
|
201 |
CleanupStack::PushL(data);
|
sl@0
|
202 |
HBufC8* result = ConvertFromHexFormatToRawL(*data);
|
sl@0
|
203 |
CleanupStack::PopAndDestroy(data);
|
sl@0
|
204 |
return result;
|
sl@0
|
205 |
}
|
sl@0
|
206 |
|
sl@0
|
207 |
HBufC8* CSymmetricCipherStepBase::ReadFileL(TPtrC aFile)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
CFileReader* reader = CFileReader::NewL(aFile);
|
sl@0
|
210 |
CleanupStack::PushL(reader);
|
sl@0
|
211 |
TPtrC8 ptr(*reader);
|
sl@0
|
212 |
HBufC8* fileData = ptr.AllocL();
|
sl@0
|
213 |
CleanupStack::PopAndDestroy(reader);
|
sl@0
|
214 |
return fileData;
|
sl@0
|
215 |
}
|
sl@0
|
216 |
|
sl@0
|
217 |
HBufC8* CSymmetricCipherStepBase::ReadInHexIvL()
|
sl@0
|
218 |
{
|
sl@0
|
219 |
HBufC8* hex = ReadInIvL();
|
sl@0
|
220 |
CleanupStack::PushL(hex);
|
sl@0
|
221 |
HBufC8* result = ConvertFromHexFormatToRawL((*hex));
|
sl@0
|
222 |
CleanupStack::PopAndDestroy(hex);
|
sl@0
|
223 |
return result;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
|
sl@0
|
226 |
HBufC8* CSymmetricCipherStepBase::ConvertFromHexFormatToRawL(TDesC8& aInput)
|
sl@0
|
227 |
{
|
sl@0
|
228 |
TBuf8<2> hexPair;
|
sl@0
|
229 |
HBufC8* result = HBufC8::NewLC(aInput.Length()/2);
|
sl@0
|
230 |
TUint8 val;
|
sl@0
|
231 |
|
sl@0
|
232 |
for(TInt i = 0 ; i < aInput.Length()-1 ; i+=2)
|
sl@0
|
233 |
{
|
sl@0
|
234 |
hexPair = aInput.Mid(i,2);
|
sl@0
|
235 |
TLex8 lex(hexPair);
|
sl@0
|
236 |
User::LeaveIfError(lex.Val(val, EHex));
|
sl@0
|
237 |
result->Des().Append(val);
|
sl@0
|
238 |
}
|
sl@0
|
239 |
CleanupStack::Pop(result);
|
sl@0
|
240 |
return result;
|
sl@0
|
241 |
}
|