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.
33 class CPBEncryptionData;
38 * Password Based Encryption ciphers.
40 * Note that RC2 has an additional key parameter, the "effective key length".
42 * Used in the construction of CPBEncryptElement, CPBEncryptSet, CPBEncryptParms,
43 * and CPBEncryptionData objects and in the CPBEncryptParms::Cipher() function.
47 /** AES cipher in CBC mode with a supplied key size of 128 bits. */
49 /** AES cipher in CBC mode with a supplied key size of 192 bits. */
51 /** AES cipher in CBC mode with a supplied key size of 256 bits. */
53 /** DES cipher in CBC mode (with a supplied key size of 56 bits). */
55 /** Triple-DES cipher in CBC mode. */
58 * RC2 cipher in CBC mode with a supplied key length of 40 bits.
60 * It has an effective key length of 1024 bits (128 bytes), which is compatible
61 * with OpenSSL RC2 encryption.
65 * RC2 cipher in CBC mode with a supplied key length of 128 bits.
67 * It has an effective key length of 1024 bits (128 bytes), which is compatible
68 * with OpenSSL RC2 encryption.
72 * RC2 cipher in CBC mode with a supplied key length of 40 bits.
74 * It has an effective key length of 128 bits (16 bytes), which is compatible
75 * with the RC2 encryption used in PKCS#8 encryption keys generated by OpenSSL
79 * RC2 cipher in CBC mode with a supplied key length of 128 bits.
81 * It has an effective key length of 128 bits (16 bytes), which is compatible
82 * with the RC2 encryption used in PKCS#8 encryption keys generated by OpenSSL
84 ECipherRC2_CBC_128_16,
86 * ARC4 cipher with a supplied key length of 128 bits.
87 * PKCS#12 PBE encryption algorithm
91 * ARC4 cipher with a supplied key length of 40 bits.
92 * PKCS#12 PBE encryption algorithm
96 * 2_KeyTriple-DES cipher in CBC mode.
97 * PKCS#12 PBE encryption algorithm
101 * RC2 Cipher in CBC mode with a supplied & effective key length of 40 bits.
102 * PKCS#12 PBE encryption algorithm
108 * Allows the password based encryption and decryption of elements.
109 * Contains the encryption key and its associated encryption data.
110 * See the Cryptography api-guide documentation for more information
113 class CPBEncryptElement : public CPBEncryptionBase
117 * Creates a new CPBEncryptElement object for encryption of new data.
119 * If strong cryptography is present, a 128 bit AES cipher is used;
120 * otherwise, for weak cryptography, a 56 bit DES cipher is used.
122 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
124 * @param aPassword The user supplied password
125 * @return The new CPBEncryptElement object
127 IMPORT_C static CPBEncryptElement* NewL(const TPBPassword& aPassword);
130 * Creates a new CPBEncryptElement object for encryption of new data.
132 * If strong cryptography is present, a 128 bit AES cipher is used;
133 * otherwise, for weak cryptography, a 56 bit DES cipher is used.
135 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
137 * A pointer to the returned object is put onto the cleanup stack.
139 * @param aPassword The user supplied password
140 * @return The new CPBEncryptElement object
142 IMPORT_C static CPBEncryptElement* NewLC(const TPBPassword& aPassword);
145 * Creates a new CPBEncryptElement object for encryption of new data.
147 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
149 * @param aPassword The user supplied password
150 * @param aCipher The cipher to use
151 * @return The new CPBEncryptElement object
153 IMPORT_C static CPBEncryptElement* NewL(const TPBPassword& aPassword,
157 * Creates a new CPBEncryptElement object for encryption of new data.
159 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
161 * A pointer to the returned object is put onto the cleanup stack.
163 * @param aPassword The user supplied password
164 * @param aCipher The cipher to use
165 * @return The new CPBEncryptElement object
167 IMPORT_C static CPBEncryptElement* NewLC(const TPBPassword& aPassword,
171 * Creates a new CPBEncryptElement object for encryption of new data.
173 * The symmetric key is derived from the password using TPKCS5KDF::DeriveKeyL().
175 * @param aPassword The user supplied password
176 * @param aParms An encryption parameter object comprising the cipher,
177 * salt, IV, and iteration count value.
178 * @return The new CPBEncryptElement object
180 IMPORT_C static CPBEncryptElement* NewL(const TPBPassword& aPassword,
181 const CPBEncryptParms& aParms);
184 * Creates a new CPBEncryptElement object for encryption of new data.
186 * The symmetric key is derived from the password using TPKCS5KDF::DeriveKeyL().
188 * A pointer to the returned object is put onto the cleanup stack.
190 * @param aPassword The user supplied password
191 * @param aParms An encryption parameter object comprising the cipher,
192 * salt, IV, and iteration count value.
193 * @return The new CPBEncryptElement object
195 IMPORT_C static CPBEncryptElement* NewLC(const TPBPassword& aPassword,
196 const CPBEncryptParms& aParms);
199 * Creates a new CPBEncryptElement object for decryption of existing data.
201 * If the specified password is valid, the function regenerates the encryption key;
202 * otherwise, it leaves with KErrBadPassphrase.
204 * @param aData The encryption data object
205 * @param aPassword The user supplied password
206 * @return The new CPBEncryptElement object
207 * @leave KErrBadPassphrase If the specified password is incorrect
209 IMPORT_C static CPBEncryptElement* NewL(const CPBEncryptionData& aData,
210 const TPBPassword& aPassword);
213 * Creates a new CPBEncryptElement object for decryption of existing data.
215 * If the specified password is valid, the function regenerates the encryption key;
216 * otherwise, it leaves with KErrBadPassphrase.
218 * A pointer to the returned object is put onto the cleanup stack.
220 * @param aData The encryption data object
221 * @param aPassword The user supplied password
222 * @return The new CPBEncryptElement object
223 * @leave KErrBadPassphrase If the specified password is incorrect
225 IMPORT_C static CPBEncryptElement* NewLC(const CPBEncryptionData& aData,
226 const TPBPassword& aPassword);
229 * Gets the parameters allowing one to re-create the object with the
230 * same state at another point in the future.
232 * In order to decrypt any information previously encrypted with this object,
233 * you <B><I>must</I></B> store this encryption data along with it. Failure
234 * to do this will result in the permanent loss of the encrypted information.
236 * @return The data allowing one to re-create this object at a later time.
238 const CPBEncryptionData& EncryptionData(void) const;
241 * Constructs a CPBEncryptor object allowing the encryption of data.
243 * @return A pointer to a CPBEncryptor object.
244 * The caller assumes ownership of the returned object.
246 CPBEncryptor* NewEncryptL(void) const;
249 * Constructs a CPBEncryptor object allowing the encryption of data.
251 * @return A pointer to a CPBEncryptor object.
252 * The caller assumes ownership of the returned object.
253 * The returned pointer is left on the cleanup stack.
255 CPBEncryptor* NewEncryptLC(void) const;
258 * Constructs a CPBDecryptor object allowing the decryption of data.
260 * @return A pointer to a CPBDecryptor object.
261 * The caller assumes ownership of the returned object.
263 CPBDecryptor* NewDecryptL(void) const;
266 * Constructs a CPBDecryptor object allowing the decryption of data.
268 * @return A pointer to a CPBDecryptor object.
269 * The caller assumes ownership of the returned object.
270 * The returned pointer is left on the cleanup stack.
272 CPBDecryptor* NewDecryptLC(void) const;
275 * Gets the maximum output ciphertext length given a specified input plaintext length.
277 * @param aPlaintextLength The plaintext length
278 * @return The maximum ciphertext length given a plaintext length.
280 TInt MaxCiphertextLength(TInt aPlaintextLength) const;
283 * Gets the maximum output plaintext length given a specified input ciphertext length.
285 * @param aCiphertextLength The ciphertext length
286 * @return The maximum plaintext length given a ciphertext length.
288 TInt MaxPlaintextLength(TInt aCiphertextLength) const;
291 virtual ~CPBEncryptElement(void);
294 void ConstructL(const TDesC8& aPassword);
296 void ConstructL(const TDesC8& aPassword, const TPBECipher aCipher);
298 void ConstructL(const TDesC8& aPassword, const CPBEncryptParms& aParms);
300 void ConstructL(const CPBEncryptionData& aData, const TPBPassword& aPassword);
302 TBool AuthenticateL(const TPBPassword& aPassword);
304 void MakeEncryptKeyL(TUint aKeySize, const TDesC8& aPassword);
306 CPBEncryptElement(void);
308 /** The encryption data */
309 CPBEncryptionData* iData;
310 /** The derived encryption key */
313 CPBEncryptElement(const CPBEncryptElement&);
314 CPBEncryptElement& operator= (const CPBEncryptElement&);
318 * Derived class to allow the efficient password based encryption and
319 * decryption of multiple elements.
321 * This is useful if one wants random access to an encrypted source consisting
322 * of multiple independent elements, for example, a database or a store.
324 * Since it is unreasonable to force the decryption of an entire set to allow
325 * access to just a tiny portion of it, and since it is too costly to derive separate
326 * keys for each element within the set, a single randomly generated <I>master</I>
327 * key is used. This master key is encrypted with the password provided by the
328 * user of the class. Known plaintext attacks against the ciphertext are prevented
329 * by using a randomly chosen Initialisation Vector (IV) for each element.
331 * Contains the master encryption key.
333 * See the Cryptography api-guide documentation for more information and sample code.
335 * @see CPBEncryptElement
339 class CPBEncryptSet : public CPBEncryptElement
343 * Creates a new CPBEncryptSet object for encryption of new data
344 * (and generates an encrypted master key).
346 * If strong cryptography is present, a 128 bit AES cipher is used;
347 * otherwise, for weak cryptography, a 56 bit DES cipher is used.
349 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
351 * @param aPassword The users password.
352 * @return A new CPBEncryptSet object
354 IMPORT_C static CPBEncryptSet* NewL(const TPBPassword& aPassword);
357 * Creates a new CPBEncryptSet object for encryption of new data
358 * (and generates an encrypted master key).
360 * The returned pointer is put onto the cleanup stack.
362 * If strong cryptography is present, a 128 bit AES cipher is used;
363 * otherwise, for weak cryptography, a 56 bit DES cipher is used.
365 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
367 * @param aPassword The user supplied password
368 * @return The new CPBEncryptSet object
370 IMPORT_C static CPBEncryptSet* NewLC(const TPBPassword& aPassword);
373 * Creates a new CPBEncryptSet object for encryption of new data
374 * (and generates an encrypted master key).
376 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
378 * @param aPassword The user supplied password
379 * @param aCipher The cipher to use
380 * @return The new CPBEncryptSet object
382 IMPORT_C static CPBEncryptSet* NewL(const TPBPassword& aPassword,
386 * Creates a new CPBEncryptSet object for encryption of new data
387 * (and generates an encrypted master key).
389 * The returned pointer is put onto the cleanup stack.
391 * The symmetric key is derived from the password and a random salt using TPKCS5KDF::DeriveKeyL().
393 * @param aPassword The user supplied password
394 * @param aCipher The cipher to use
395 * @return The new CPBEncryptSet object
397 IMPORT_C static CPBEncryptSet* NewLC(const TPBPassword& aPassword,
401 * Creates a new CPBEncryptSet object for encryption of new data
402 * (and generates an encrypted master key).
404 * The symmetric key is derived from the password using TPKCS5KDF::DeriveKeyL().
406 * @param aPassword The user supplied password
407 * @param aParms An encryption parameter object comprising the cipher,
408 * salt, IV, and iteration count value.
409 * @return The new CPBEncryptSet object
411 IMPORT_C static CPBEncryptSet* NewL(const TPBPassword& aPassword,
412 const CPBEncryptParms& aParms);
415 * Creates a new CPBEncryptSet object for encryption of new data
416 * (and generates an encrypted master key).
418 * The returned pointer is put onto the cleanup stack.
420 * The symmetric key is derived from the password using TPKCS5KDF::DeriveKeyL().
422 * @param aPassword The user supplied password
423 * @param aParms An encryption parameter object comprising the cipher,
424 * salt, IV, and iteration count value.
425 * @return The new CPBEncryptSet object
427 IMPORT_C static CPBEncryptSet* NewLC(const TPBPassword& aPassword,
428 const CPBEncryptParms& aParms);
431 * Creates a new CPBEncryptSet object for encryption of new data
432 * (and generates an encrypted master key).
434 * If the specified password is valid, the function regenerates the encryption key;
435 * otherwise, it leaves with KErrBadPassphrase.
437 * @param aData The encryption data object to copy
438 * @param aEncryptedMasterKey On return, the encrypted master key
439 * @param aPassword The user supplied password
440 * @return The new CPBEncryptSet object
441 * @leave KErrBadPassphrase If the specified password is incorrect
443 IMPORT_C static CPBEncryptSet* NewL(const CPBEncryptionData& aData,
444 const TDesC8& aEncryptedMasterKey, const TPBPassword& aPassword);
447 * Creates a new CPBEncryptSet object for encryption of new data
448 * (and generates an encrypted master key).
450 * The returned pointer is put onto the cleanup stack.
452 * If the specified password is valid, the function regenerates the encryption key;
453 * otherwise, it leaves with KErrBadPassphrase.
455 * @param aData The encryption data object to copy
456 * @param aEncryptedMasterKey On return, the encrypted master key
457 * @param aPassword The user supplied password
458 * @return The new CPBEncryptSet object
459 * @leave KErrBadPassphrase If the specified password is incorrect
461 IMPORT_C static CPBEncryptSet* NewLC(const CPBEncryptionData& aData,
462 const TDesC8& aEncryptedMasterKey, const TPBPassword& aPassword);
465 * Gets the encrypted form of the master key.
467 * This must be stored along with the object returned by CPBEncryptElement::EncryptionData()
468 * in order for the object to be reconstructed with the same state at
469 * some time in the future. Failure to do so will result in the permanent
470 * loss of any information encrypted with this object.
472 * @return The encrypted master key.
474 IMPORT_C const TDesC8& EncryptedMasterKey(void) const;
477 * Constructs a CPBEncryptor object based on the state of this object
478 * (i.e., the cipher and master key) allowing the encryption of data.
480 * @return A pointer to a CPBEncryptor object.
481 * The caller assumes ownership of the returned object.
483 CPBEncryptor* NewEncryptL(void) const;
486 * Constructs a CPBEncryptor object based on the state of this object
487 * (i.e., the cipher and master key) allowing the encryption of data.
489 * @return A pointer to a CPBEncryptor object.
490 * The caller assumes ownership of the returned object.
491 * The returned pointer is left on the cleanup stack.
493 CPBEncryptor* NewEncryptLC(void) const;
496 * Constructs a CPBDecryptor object based on the state of this object
497 * (i.e., the cipher and master key) allowing the decryption of data.
499 * @return A pointer to a CPBDecryptor object.
500 * The caller assumes ownership of the returned object.
502 CPBDecryptor* NewDecryptL(void) const;
505 * Constructs a CPBDecryptor object based on the state of this object
506 * (i.e., the cipher and master key) allowing the decryption of data.
508 * @return A pointer to a CPBDecryptor object.
509 * The caller assumes ownership of the returned object.
510 * The returned pointer is left on the cleanup stack.
512 CPBDecryptor* NewDecryptLC(void) const;
515 * Re-encrypts the master key with the specified new password.
517 * @param aNewPassword The new password
519 IMPORT_C void ChangePasswordL(const TPBPassword& aNewPassword);
522 * Gets the maximum output ciphertext length given a specified input plaintext length.
524 * @param aPlaintextLength The plaintext length
525 * @return The maximum ciphertext length given a plaintext length.
527 TInt MaxCiphertextLength(TInt aPlaintextLength) const;
530 * Gets the maximum output plaintext length given a specified input ciphertext length.
532 * @param aCiphertextLength The ciphertext length
533 * @return The maximum plaintext length given a ciphertext length.
535 TInt MaxPlaintextLength(TInt aCiphertextLength) const;
538 virtual ~CPBEncryptSet(void);
541 void ConstructL(const TDesC8& aPassword);
543 void ConstructL(const TDesC8& aPassword, TPBECipher aCipher);
545 void ConstructL(const TDesC8& aPassword, const CPBEncryptParms& aParms);
547 void ConstructMasterKeyL(void);
549 void ConstructL(const CPBEncryptionData& aData,
550 const TDesC8& aEncryptedMasterKey, const TPBPassword& aPassword);
552 void DecryptMasterKeyL(TDes8& aMasterKey) const;
554 void EncryptMasterKeyL(const TDesC8& aMasterKey);
558 /** The derived encrypted master key*/
559 HBufC8* iEncryptedMasterKey;
561 CPBEncryptSet(const CPBEncryptSet&);
562 CPBEncryptSet& operator= (const CPBEncryptSet&);
566 * Class representing both 8 and 16 bit descriptor passwords.
567 * Internally these are stored as 8 bit passwords.
575 * Constructs a TPBPassword object with an 8 bit descriptor.
577 * Internally this is represented as an octet byte sequence
578 * (aka 8 bit TPtrC8 descriptor).
580 * @param aPassword A const reference to an 8 bit descriptor.
581 * representing the users initial password.
583 IMPORT_C TPBPassword(const TDesC8& aPassword);
588 * Constructs a TPBPassword object with a 16 bit descriptor.
590 * Internally this is represented as an octet byte sequence
591 * (aka 8 bit TPtrC8 descriptor).
593 * @param aPassword A const reference to a 16 bit descriptor
594 * representing the users initial password.
596 IMPORT_C TPBPassword(const TDesC16& aPassword);
601 * Gets a const reference to an 8 bit descriptor representing the users
602 * initial password (which could have been either 8 or 16 bit).
604 * @return A const reference to an 8 bit descriptor.
606 IMPORT_C const TDesC8& Password(void) const;