os/security/crypto/weakcrypto/inc/pbedata.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 * ** 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.
    17 *
    18 */
    19 
    20 
    21 /**
    22  @file
    23  @publishedPartner
    24  @released
    25 */
    26 
    27 #ifndef __PBEDATA_H__
    28 #define __PBEDATA_H__
    29 
    30 #include <s32strm.h>
    31 #include <pbe.h>
    32 
    33 /**
    34  * Contains the Password Based Encryption parameters.
    35  * An object of this class can be input for CPBEncryptElement or CPBEncryptSet objects.
    36  *
    37  * @since v7.0s
    38  */
    39 class CPBEncryptParms : public CBase
    40 	{
    41 public:
    42 	enum TKdf
    43 	/**
    44 		Key derivation function.
    45 		This does not affect the authentication key,
    46 		which always uses PKCS#5.
    47 	 */
    48 		{
    49 		EKdfPkcs5,
    50 		EKdfPkcs12
    51 		};
    52 	
    53 	/**
    54 	 * Creates a new CPBEncryptParms object.
    55 	 *
    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
    61 	 */
    62 	IMPORT_C static CPBEncryptParms* NewL(TPBECipher aCipher,
    63 		const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations);
    64 
    65 	/**
    66 	 * Creates a new CPBEncryptParms object and puts a pointer to it onto the cleanup stack.
    67 	 *
    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
    73 	 */
    74 	IMPORT_C static CPBEncryptParms* NewLC(TPBECipher aCipher,
    75 		const TDesC8& aSalt, const TDesC8& aIV, TUint aIterations);
    76 
    77 	/**
    78 	 * Creates a new CPBEncryptParms object from an existing object.
    79 	 *
    80 	 * @param aParms		The existing encryption parameters object
    81 	 * @return				A pointer to the new CPBEncryptParms object
    82 	 */
    83 	IMPORT_C static CPBEncryptParms* NewL(const CPBEncryptParms& aParms);
    84 
    85 	/**
    86 	 * Creates a new CPBEncryptParms object from an existing object
    87 	 * and puts a pointer to it onto the cleanup stack.
    88 	 *
    89 	 * @param aParms		The existing encryption parameters object
    90 	 * @return				A pointer to the new CPBEncryptParms object
    91 	 */
    92 	IMPORT_C static CPBEncryptParms* NewLC(const CPBEncryptParms& aParms);
    93 
    94 	/**
    95 	 * Internalizes encryption parameter data from a read stream.
    96 	 *
    97 	 * @param aStream	The read stream to be internalized
    98 	 * @return			A pointer to the new CPBEncryptParms object
    99 	 * 
   100 	 */
   101 	IMPORT_C static CPBEncryptParms* NewL(RReadStream& aStream);
   102 
   103 	/**
   104 	 * Internalizes encryption parameter data from a read stream, and 
   105 	 * puts a pointer to the new object onto the cleanup stack.
   106 	 *
   107 	 * @param aStream	The read stream to be internalized
   108 	 * @return			A pointer to the new CPBEncryptParms object
   109 	 */
   110 	IMPORT_C static CPBEncryptParms* NewLC(RReadStream& aStream);
   111 
   112 	/**
   113 	 * Gets the PBE cipher
   114 	 *
   115 	 * @return	The cipher to use
   116 	 */
   117 	IMPORT_C TPBECipher Cipher() const;
   118 	/**
   119 		Gets the key derivation function (KDF.)
   120 		
   121 		@return The key derivation function (KDF) which
   122 			transforms the password into an encryption key.
   123 	 */
   124 	IMPORT_C TKdf Kdf() const;
   125 
   126 	/**
   127 	 * Gets the PBE salt
   128 	 *
   129 	 * @return	The salt
   130 	 */
   131 	IMPORT_C TPtrC8 Salt() const;
   132 
   133 	/**
   134 	 * Gets the number of iterations for the PKCS#5 algorithm.
   135 	 *
   136 	 * @return	The number of iterations
   137 	 */
   138 	IMPORT_C TInt Iterations() const;
   139 
   140 	/**
   141 	 * Gets the PBE Initialization Vector
   142 	 *
   143 	 * @return	The IV
   144 	 */
   145 	IMPORT_C TPtrC8 IV() const;
   146 
   147 	IMPORT_C void SetIV(const TDesC8& aNewIv);
   148 
   149 	/**
   150 	 * Externalizes the encryption parameters into a write stream.
   151 	 *
   152 	 * @param aStream	The stream to write to
   153 	 */
   154 	IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
   155 	IMPORT_C static CPBEncryptParms* NewL();
   156 	IMPORT_C static CPBEncryptParms* NewLC();
   157 	
   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);
   162 
   163 	/** Destructor */
   164 	virtual ~CPBEncryptParms(void);
   165 	/** @internalAll */
   166 	void DeriveKeyL(const TDesC8& aPassword, TDes8& aKeyBuf) const;
   167 
   168 	
   169 protected:	
   170 #ifdef PBEDATA_NO_EXPORTED_CONSTRUCTORS
   171 	/** @internalAll */
   172 	CPBEncryptParms(void);
   173 #else
   174 	/** Constructor */
   175 	IMPORT_C CPBEncryptParms(void);
   176 #endif
   177 
   178 	/** @internalAll */
   179 	void ConstructL();
   180 
   181 	/** @internalAll*/
   182 	void ConstructL(TPBECipher aCipher, const TDesC8& aSalt,
   183 		const TDesC8& aIV, TUint aIterations);
   184 	/** @internalAll*/
   185 	void ConstructL(const CPBEncryptParms& aParms);
   186 	/** @internalAll*/
   187 	void ConstructL(RReadStream& aStream);
   188 private:
   189 	CPBEncryptParms(const CPBEncryptParms&);
   190 	CPBEncryptParms& operator= (const CPBEncryptParms&);
   191 private:
   192 	class TParamsData
   193 	/**
   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.
   198 	 */
   199 		{
   200 	public:
   201 		TPBECipher iCipher;
   202 		/**
   203 			The password is processed with this function
   204 			to generate the encryption key.
   205 		 */
   206 		TKdf iKdf;
   207 		};
   208 	TParamsData* iData;
   209 
   210 	HBufC8* iSalt;
   211 	HBufC8* iIV;
   212 	TUint iIterations;
   213 	};
   214 
   215 
   216 /**
   217  * Contains the password based authentication data.
   218  * Used to check the passphrase when decrypting.
   219  *
   220  * @since v7.0s
   221  */
   222 class CPBAuthData : public CBase
   223 	{
   224 public:
   225 	/**
   226 	 * Derives an authentication key.
   227 	 *
   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
   233 	 */
   234 	IMPORT_C static CPBAuthData* NewL(const TDesC8& aPassword, 
   235 		const TDesC8& aSalt, TUint aKeySize, TUint aIterations);
   236 
   237 	/**
   238 	 * Derives an authentication key.
   239 	 *
   240 	 * The returned pointer is put onto the cleanup stack.
   241 	 *
   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
   247 	 */
   248 	IMPORT_C static CPBAuthData* NewLC(const TDesC8& aPassword, 
   249 		const TDesC8& aSalt, TUint aKeySize, TUint aIterations);
   250 
   251 	/**
   252 	 * Creates a new CPBAuthData object from an existing authentication key.
   253 	 *
   254 	 * @param aData	The existing CPBAuthData object
   255 	 * @return		A pointer to the new CPBAuthData object
   256 	 */
   257 	IMPORT_C static CPBAuthData* NewL(const CPBAuthData& aData);
   258 
   259 	/**
   260 	 * Creates a new CPBAuthData object from an existing authentication key.
   261 	 *
   262 	 * The returned pointer is put onto the cleanup stack.
   263 	 *
   264 	 * @param aData	The existing CPBAuthData object
   265 	 * @return		A pointer to the new CPBAuthData object
   266 	 */
   267 	IMPORT_C static CPBAuthData* NewLC(const CPBAuthData& aData);
   268 
   269 	/**
   270 	 * Creates a new CPBAuthData object from an existing authentication key
   271 	 * by internalizing the authentication data from a read stream.
   272 	 *
   273 	 * @param aStream	The stream to read from
   274 	 * @return			A pointer to the new CPBAuthData object
   275 	 */
   276 	IMPORT_C static CPBAuthData* NewL(RReadStream& aStream);
   277 
   278 	/**
   279 	 * Creates a new CPBAuthData object from an existing authentication key
   280 	 * by internalizing the authentication data from a read stream.
   281 	 *
   282 	 * The returned pointer is put onto the cleanup stack.
   283 	 *
   284 	 * @param aStream	The stream to read from
   285 	 * @return			A pointer to the new CPBAuthData object
   286 	 */
   287 	IMPORT_C static CPBAuthData* NewLC(RReadStream& aStream);
   288 
   289 	/**
   290 	 * Gets the authentication key
   291 	 *
   292 	 * @return	The key
   293 	 */
   294 	IMPORT_C TPtrC8 Key() const;
   295 
   296 	/**
   297 	 * Gets the salt used for the authentication 
   298 	 *
   299 	 * @return	The salt
   300 	 */
   301 	IMPORT_C TPtrC8 Salt() const;
   302 
   303 	/**
   304 	 * Gets the number of iterations of the hashing algorithm.
   305 	 *
   306 	 * @return	The number of iterations
   307 	 */
   308 	IMPORT_C TInt Iterations() const;
   309 
   310 	/**
   311 	 * Tests whether two authentication keys are identical 
   312 	 *
   313 	 * @param aAuth	The authentication data object which holds the key to be tested
   314 	 * @return		ETrue, if they are identical; EFalse, otherwise
   315 	 */
   316 	IMPORT_C TBool operator==(const CPBAuthData& aAuth) const;
   317 
   318 	/**
   319 	 * Externalizes the encryption parameters into a write stream.
   320 	 *
   321 	 * @param aStream	The stream to write to
   322 	 */
   323 	IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
   324 
   325 	/** Destructor */
   326 	virtual ~CPBAuthData(void);
   327 protected:
   328 #ifdef PBEDATA_NO_EXPORTED_CONSTRUCTORS
   329 	/** @internalAll */
   330 	CPBAuthData(void);
   331 #else
   332 	/** Constructor */
   333 	IMPORT_C CPBAuthData(void);
   334 #endif
   335 	/** @internalAll */
   336 	void ConstructL(const TDesC8& aPassword, const TDesC8& aSalt,
   337 		TUint aKeySize, TUint aIterations);
   338 	/** @internalAll */
   339 	void ConstructL(const CPBAuthData& aData);
   340 	/** @internalAll */
   341 	void ConstructL(RReadStream& aStream);
   342 private:
   343 	CPBAuthData(const CPBAuthData&);
   344 	CPBAuthData& operator= (const CPBAuthData&);
   345 private:
   346 	HBufC8* iAuthKey;
   347 	HBufC8* iSalt;
   348 	TUint iIterations;
   349 	};
   350 
   351 /** 
   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.
   355  *
   356  * @see CPBEncryptParms
   357  * @see CPBAuthData
   358  *
   359  * @since v7.0s
   360  */
   361 class CPBEncryptionData : public CBase
   362 	{
   363 public:
   364 	/**
   365 	 * Creates a new CPBEncryptionData object 
   366 	 *
   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
   374 	 */
   375 	IMPORT_C static CPBEncryptionData* NewL(const TDesC8& aPassword,
   376 		TPBECipher aCipher, const TDesC8& aAuthSalt, 
   377 		const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations);
   378 
   379 	/**
   380 	 * Creates a new CPBEncryptionData object 
   381 	 * and puts a pointer to it onto the cleanup stack.
   382 	 *
   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
   390 	 */
   391 	IMPORT_C static CPBEncryptionData* NewLC(const TDesC8& aPassword,
   392 		TPBECipher aCipher, const TDesC8& aAuthSalt, 
   393 		const TDesC8& aEncryptSalt, const TDesC8& aIV, TUint aIterations);
   394 
   395 	/**
   396 	 * Creates a new CPBEncryptionData from an existing one.
   397 	 *
   398 	 * @param aData	The existing CPBEncryptionData object
   399 	 * @return		A pointer to the new CPBEncryptionData object
   400 	 */
   401 	IMPORT_C static CPBEncryptionData* NewL(const CPBEncryptionData& aData);
   402 
   403 	/**
   404 	 * Creates a new CPBEncryptionData from an existing one,
   405 	 * and puts a pointer to it onto the cleanup stack.
   406 	 *
   407 	 * @param aData	The existing CPBEncryptionData object
   408 	 * @return		A pointer to the new CPBEncryptionData object
   409 	 */
   410 	IMPORT_C static CPBEncryptionData* NewLC(const CPBEncryptionData& aData);
   411 
   412 	/**
   413 	 * Internalizes the encryption data from a read stream.
   414 	 *
   415 	 * @param aStream	The stream to read from
   416 	 * @return			A pointer to the new CPBEncryptionData object
   417 	 */
   418 	IMPORT_C static CPBEncryptionData* NewL(RReadStream& aStream);
   419 
   420 	/**
   421 	 * Internalizes the encryption data from a read stream,
   422 	 * and puts a pointer to it onto the cleanup stack.
   423 	 *
   424 	 * @param aStream	The stream to read from
   425 	 * @return			A pointer to the new CPBEncryptionData object
   426 	 */
   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);
   431 
   432 
   433 	/**
   434 	 * Returns the encryption parameter object.
   435 	 *
   436 	 * @return	The CPBEncryptParms object
   437 	 */
   438 	IMPORT_C const CPBEncryptParms& EncryptParms() const;
   439 
   440 	/**
   441 	 * Returns the authentication data object.
   442 	 *
   443 	 * @return	The CPBAuthData object
   444 	 */
   445 	IMPORT_C const CPBAuthData& AuthData() const;
   446 
   447 	/**
   448 	 * Externalizes the encryption data into a write stream.
   449 	 *
   450 	 * @param aStream	The stream to write to
   451 	 */
   452 	IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
   453 
   454 	/** Destructor */
   455 	virtual ~CPBEncryptionData(void);
   456 protected:
   457 #ifdef PBEDATA_NO_EXPORTED_CONSTRUCTORS
   458 	/** @internalAll */
   459 	CPBEncryptionData(void);
   460 #else
   461 	/** Constructor */
   462 	IMPORT_C CPBEncryptionData(void);
   463 #endif
   464 	/** @internalAll */
   465 	void ConstructL(const TDesC8& aPassword, TPBECipher aCipher,
   466 		const TDesC8& aAuthSalt, const TDesC8& aEncryptSalt, const TDesC8& aIV,
   467 		TUint aIterations);
   468 	/** @internalAll */
   469 	void ConstructL(const CPBEncryptionData& aData);
   470 	/** @internalAll */
   471 	void ConstructL(
   472 		const TDesC8& aPassword, const TDesC8& aAuthSalt,
   473 		const CPBEncryptParms& aParms);
   474 	/** @internalAll */
   475 	void ConstructL(RReadStream& aStream);
   476 private:
   477 	CPBEncryptionData(const CPBEncryptionData&);
   478 	CPBEncryptionData& operator= (const CPBEncryptionData&);
   479 private:
   480 	CPBEncryptParms* iParms;
   481 	CPBAuthData* iAuth;
   482 	};
   483 
   484 #endif