First public contribution.
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.
15 * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the
16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
34 * Contains the Password Based Encryption parameters.
35 * An object of this class can be input for CPBEncryptElement or CPBEncryptSet objects.
39 class CPBEncryptParms : public CBase
44 Key derivation function.
45 This does not affect the authentication key,
46 which always uses PKCS#5.
54 * Creates a new CPBEncryptParms object.
56 * @param aCipher The cipher to use
57 * @param aSalt The salt
58 * @param aIV The Initialization Vector
59 * @param aIterations The number of iterations of the PBE algorithm
60 * @return A pointer to the new CPBEncryptParms object
62 IMPORT_C static CPBEncryptParms* NewL(TPBECipher aCipher,
63 const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations);
66 * Creates a new CPBEncryptParms object and puts a pointer to it onto the cleanup stack.
68 * @param aCipher The cipher to use
69 * @param aSalt The salt
70 * @param aIV The Initialization Vector
71 * @param aIterations The number of iterations of the PBE algorithm
72 * @return A pointer to the new CPBEncryptParms object
74 IMPORT_C static CPBEncryptParms* NewLC(TPBECipher aCipher,
75 const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations);
78 * Creates a new CPBEncryptParms object from an existing object.
80 * @param aParms The existing encryption parameters object
81 * @return A pointer to the new CPBEncryptParms object
83 IMPORT_C static CPBEncryptParms* NewL(const CPBEncryptParms& aParms);
86 * Creates a new CPBEncryptParms object from an existing object
87 * and puts a pointer to it onto the cleanup stack.
89 * @param aParms The existing encryption parameters object
90 * @return A pointer to the new CPBEncryptParms object
92 IMPORT_C static CPBEncryptParms* NewLC(const CPBEncryptParms& aParms);
95 * Internalizes encryption parameter data from a read stream.
97 * @param aStream The read stream to be internalized
98 * @return A pointer to the new CPBEncryptParms object
101 IMPORT_C static CPBEncryptParms* NewL(RReadStream& aStream);
104 * Internalizes encryption parameter data from a read stream, and
105 * puts a pointer to the new object onto the cleanup stack.
107 * @param aStream The read stream to be internalized
108 * @return A pointer to the new CPBEncryptParms object
110 IMPORT_C static CPBEncryptParms* NewLC(RReadStream& aStream);
113 * Gets the PBE cipher
115 * @return The cipher to use
117 IMPORT_C TPBECipher Cipher() const;
119 Gets the key derivation function (KDF.)
121 @return The key derivation function (KDF) which
122 transforms the password into an encryption key.
124 IMPORT_C TKdf Kdf() const;
131 IMPORT_C TPtrC8 Salt() const;
134 * Gets the number of iterations for the PKCS#5 algorithm.
136 * @return The number of iterations
138 IMPORT_C TInt Iterations() const;
141 * Gets the PBE Initialization Vector
145 IMPORT_C TPtrC8 IV() const;
147 IMPORT_C void SetIV(const TDesC8& aNewIv);
150 * Externalizes the encryption parameters into a write stream.
152 * @param aStream The stream to write to
154 IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
155 IMPORT_C static CPBEncryptParms* NewL();
156 IMPORT_C static CPBEncryptParms* NewLC();
158 IMPORT_C void SetCipher(TPBECipher aCipher);
159 IMPORT_C void SetKdf(TKdf aKdf);
160 IMPORT_C void ResizeSaltL(TInt aNewLen);
161 IMPORT_C void SetIterations(TInt aIterCount);
164 virtual ~CPBEncryptParms(void);
166 void DeriveKeyL(const TDesC8& aPassword, TDes8& aKeyBuf) const;
170 #ifdef PBEDATA_NO_EXPORTED_CONSTRUCTORS
172 CPBEncryptParms(void);
175 IMPORT_C CPBEncryptParms(void);
182 void ConstructL(TPBECipher aCipher, const TDesC8& aSalt,
183 const TDesC8& aIV, TUint aIterations);
185 void ConstructL(const CPBEncryptParms& aParms);
187 void ConstructL(RReadStream& aStream);
189 CPBEncryptParms(const CPBEncryptParms&);
190 CPBEncryptParms& operator= (const CPBEncryptParms&);
194 This class contains multiple objects. Its
195 pointer is stored in CPBEncryptParms to preserve
196 BC, because CPBEncryptParms has an exported,
197 protected constructor.
203 The password is processed with this function
204 to generate the encryption key.
217 * Contains the password based authentication data.
218 * Used to check the passphrase when decrypting.
222 class CPBAuthData : public CBase
226 * Derives an authentication key.
228 * @param aPassword The user's initial password
229 * @param aSalt The salt
230 * @param aKeySize The key size
231 * @param aIterations The number of iterations of the PBE algorithm
232 * @return A pointer to the new CPBAuthData object
234 IMPORT_C static CPBAuthData* NewL(const TDesC8& aPassword,
235 const TDesC8& aSalt, TUint aKeySize, TUint aIterations);
238 * Derives an authentication key.
240 * The returned pointer is put onto the cleanup stack.
242 * @param aPassword The user's initial password
243 * @param aSalt The salt
244 * @param aKeySize The key size
245 * @param aIterations The number of iterations of the PBE algorithm
246 * @return A pointer to the new CPBAuthData object
248 IMPORT_C static CPBAuthData* NewLC(const TDesC8& aPassword,
249 const TDesC8& aSalt, TUint aKeySize, TUint aIterations);
252 * Creates a new CPBAuthData object from an existing authentication key.
254 * @param aData The existing CPBAuthData object
255 * @return A pointer to the new CPBAuthData object
257 IMPORT_C static CPBAuthData* NewL(const CPBAuthData& aData);
260 * Creates a new CPBAuthData object from an existing authentication key.
262 * The returned pointer is put onto the cleanup stack.
264 * @param aData The existing CPBAuthData object
265 * @return A pointer to the new CPBAuthData object
267 IMPORT_C static CPBAuthData* NewLC(const CPBAuthData& aData);
270 * Creates a new CPBAuthData object from an existing authentication key
271 * by internalizing the authentication data from a read stream.
273 * @param aStream The stream to read from
274 * @return A pointer to the new CPBAuthData object
276 IMPORT_C static CPBAuthData* NewL(RReadStream& aStream);
279 * Creates a new CPBAuthData object from an existing authentication key
280 * by internalizing the authentication data from a read stream.
282 * The returned pointer is put onto the cleanup stack.
284 * @param aStream The stream to read from
285 * @return A pointer to the new CPBAuthData object
287 IMPORT_C static CPBAuthData* NewLC(RReadStream& aStream);
290 * Gets the authentication key
294 IMPORT_C TPtrC8 Key() const;
297 * Gets the salt used for the authentication
301 IMPORT_C TPtrC8 Salt() const;
304 * Gets the number of iterations of the hashing algorithm.
306 * @return The number of iterations
308 IMPORT_C TInt Iterations() const;
311 * Tests whether two authentication keys are identical
313 * @param aAuth The authentication data object which holds the key to be tested
314 * @return ETrue, if they are identical; EFalse, otherwise
316 IMPORT_C TBool operator==(const CPBAuthData& aAuth) const;
319 * Externalizes the encryption parameters into a write stream.
321 * @param aStream The stream to write to
323 IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
326 virtual ~CPBAuthData(void);
328 #ifdef PBEDATA_NO_EXPORTED_CONSTRUCTORS
333 IMPORT_C CPBAuthData(void);
336 void ConstructL(const TDesC8& aPassword, const TDesC8& aSalt,
337 TUint aKeySize, TUint aIterations);
339 void ConstructL(const CPBAuthData& aData);
341 void ConstructL(RReadStream& aStream);
343 CPBAuthData(const CPBAuthData&);
344 CPBAuthData& operator= (const CPBAuthData&);
352 * Represents the information needed to decrypt encrypted data given the correct password.
353 * Contains the authentication key, and the parameters used to derive the encryption key.
354 * A CPBEncryptionData object needs to be stored to recover any data for later use.
356 * @see CPBEncryptParms
361 class CPBEncryptionData : public CBase
365 * Creates a new CPBEncryptionData object
367 * @param aPassword The user's initial password
368 * @param aCipher The cipher to use
369 * @param aAuthSalt The salt used for the authentication
370 * @param aEncryptSalt The salt used for the encryption
371 * @param aIV The Initialization Vector
372 * @param aIterations The number of iterations of the PBE algorithm
373 * @return A pointer to the new CPBEncryptionData object
375 IMPORT_C static CPBEncryptionData* NewL(const TDesC8& aPassword,
376 TPBECipher aCipher, const TDesC8& aAuthSalt,
377 const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations);
380 * Creates a new CPBEncryptionData object
381 * and puts a pointer to it onto the cleanup stack.
383 * @param aPassword The user's initial password
384 * @param aCipher The cipher to use
385 * @param aAuthSalt The salt used for the authentication
386 * @param aEncryptSalt The salt used for the encryption
387 * @param aIV The Initialization Vector
388 * @param aIterations The number of iterations of the PBE algorithm
389 * @return A pointer to the new CPBEncryptionData object
391 IMPORT_C static CPBEncryptionData* NewLC(const TDesC8& aPassword,
392 TPBECipher aCipher, const TDesC8& aAuthSalt,
393 const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations);
396 * Creates a new CPBEncryptionData from an existing one.
398 * @param aData The existing CPBEncryptionData object
399 * @return A pointer to the new CPBEncryptionData object
401 IMPORT_C static CPBEncryptionData* NewL(const CPBEncryptionData& aData);
404 * Creates a new CPBEncryptionData from an existing one,
405 * and puts a pointer to it onto the cleanup stack.
407 * @param aData The existing CPBEncryptionData object
408 * @return A pointer to the new CPBEncryptionData object
410 IMPORT_C static CPBEncryptionData* NewLC(const CPBEncryptionData& aData);
413 * Internalizes the encryption data from a read stream.
415 * @param aStream The stream to read from
416 * @return A pointer to the new CPBEncryptionData object
418 IMPORT_C static CPBEncryptionData* NewL(RReadStream& aStream);
421 * Internalizes the encryption data from a read stream,
422 * and puts a pointer to it onto the cleanup stack.
424 * @param aStream The stream to read from
425 * @return A pointer to the new CPBEncryptionData object
427 IMPORT_C static CPBEncryptionData* NewLC(RReadStream& aStream);
428 IMPORT_C static CPBEncryptionData* NewL(
429 const TDesC8& aPassword, const TDesC8& aAuthSalt,
430 const CPBEncryptParms& aParms);
434 * Returns the encryption parameter object.
436 * @return The CPBEncryptParms object
438 IMPORT_C const CPBEncryptParms& EncryptParms() const;
441 * Returns the authentication data object.
443 * @return The CPBAuthData object
445 IMPORT_C const CPBAuthData& AuthData() const;
448 * Externalizes the encryption data into a write stream.
450 * @param aStream The stream to write to
452 IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
455 virtual ~CPBEncryptionData(void);
457 #ifdef PBEDATA_NO_EXPORTED_CONSTRUCTORS
459 CPBEncryptionData(void);
462 IMPORT_C CPBEncryptionData(void);
465 void ConstructL(const TDesC8& aPassword, TPBECipher aCipher,
466 const TDesC8& aAuthSalt, const TDesC8& aEncryptSalt, const TDesC8& aIV,
469 void ConstructL(const CPBEncryptionData& aData);
472 const TDesC8& aPassword, const TDesC8& aAuthSalt,
473 const CPBEncryptParms& aParms);
475 void ConstructL(RReadStream& aStream);
477 CPBEncryptionData(const CPBEncryptionData&);
478 CPBEncryptionData& operator= (const CPBEncryptionData&);
480 CPBEncryptParms* iParms;