os/security/crypto/weakcrypto/inc/pbencryptor.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 __PBENCRYPTOR_H__
    28 #define __PBENCRYPTOR_H__
    29 
    30 #include "pbe.h"
    31 #include "padding.h"
    32 #include "msymmetriccipher.h"
    33 
    34 /**
    35  * Implements the password based encryption of elements.
    36  *
    37  * @see CPBEncryptElement
    38  * @since v7.0s
    39  */
    40 class CPBEncryptorElement : public CPBEncryptor
    41 	{
    42 public:
    43 	/**
    44 	 * Creates a new CPBEncryptorElement object from the specified cipher, 
    45 	 * key, and Initialization Vector (IV).
    46 	 *
    47 	 * @param aCipher	The encryption cipher
    48 	 * @param aKey		The encryption key
    49 	 * @param aIV		The Initialization Vector
    50 	 * @return			A pointer to the new CPBEncryptorElement object
    51 	 */
    52 	IMPORT_C static CPBEncryptorElement* NewL(TPBECipher aCipher, 
    53 		const TDesC8& aKey, const TDesC8& aIV);
    54 
    55 	/**
    56 	 * Creates a new CPBEncryptorElement object from the specified cipher, 
    57 	 * key, and IV.
    58 	 * 
    59 	 * Puts a pointer to the returned object onto the cleanup stack.
    60 	 *
    61 	 * @param aCipher	The encryption cipher
    62 	 * @param aKey		The encryption key
    63 	 * @param aIV		The Initialization Vector
    64 	 * @return			A pointer to the new CPBEncryptorElement object
    65 	 */
    66 	IMPORT_C static CPBEncryptorElement* NewLC(TPBECipher aCipher, 
    67 		const TDesC8& aKey, const TDesC8& aIV);
    68 
    69 	/** 
    70 	 * Transforms aInput into its encrypted form, aOutput.
    71 	 *
    72 	 * aOutput must have CPBEncryptorElement::MaxOutputLength() empty bytes remaining in its length. 
    73 	 *
    74 	 *	See the Cryptography api-guide documentation for an explanation of
    75 	 *	how buffering of data supplied to this function is handled.
    76 	 *
    77 	 * @param aInput	The plaintext.
    78 	 * @param aOutput	The ciphertext.
    79 	 */
    80 	void Process(const TDesC8& aInput, TDes8& aOutput);
    81 
    82 	/** 
    83 	 * Transforms aInput into its encrypted form, aOutput, and applies a
    84 	 * padding scheme to ensure a block aligned result.
    85 	 *
    86 	 * aOutput must have CPBEncryptorElement::MaxFinalOutputLength() 
    87 	 * empty bytes remaining in its length. 
    88 	 *
    89 	 *	See the Cryptography api-guide documentation for an explanation of
    90 	 *	how buffering of data supplied to this function is handled.
    91 	 * 
    92 	 * @param aInput	The plaintext.
    93 	 * @param aOutput	The ciphertext.
    94 	 */
    95 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
    96 
    97 	/** 
    98 	 * Gets the maximum size of the output resulting from calling Process() with a
    99 	 * given input length.
   100 	 *
   101 	 * @param aMaxInputLength	The maximum input length in bytes.
   102 	 * @return					The maximum output length in bytes.
   103 	 */
   104 	TInt MaxOutputLength(TUint aMaxInputLength) const;
   105 
   106 	/** 
   107 	 * Gets the maximum size of the output resulting from calling ProcessFinalL()
   108 	 * with a given input length.
   109 	 *
   110 	 * @param aMaxInputLength	The maximum input length in bytes.
   111 	 * @return					TInt The maximum output length in bytes.
   112 	 */
   113 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
   114 
   115 	/** Destructor */
   116 	virtual ~CPBEncryptorElement();
   117 protected:
   118 	/* @internalComponent */
   119 	CPBEncryptorElement();
   120 	/* @internalComponent */
   121 	void ConstructL(TPBECipher aCipher, const TDesC8& aKey, const TDesC8& aIV);
   122 private:
   123 	CSymmetricCipher* iCipher;
   124 	};
   125 
   126 /**
   127  * Implements the password based decryption of elements.
   128  *
   129  * @since v7.0s
   130  */
   131 class CPBDecryptorElement : public CPBDecryptor
   132 	{
   133 public:
   134 	/**
   135 	 * Creates a new CPBDecryptorElement object from the specified cipher, 
   136 	 * key, and IV.
   137 	 *
   138 	 * @param aCipher	The decryption cipher
   139 	 * @param aKey		The decryption key
   140 	 * @param aIV		The Initialization Vector
   141 	 * @return			A pointer to the new CPBDecryptorElement object
   142 	 */
   143 	IMPORT_C static CPBDecryptorElement* NewL(const TPBECipher aCipher, 
   144 		const TDesC8& aKey, const TDesC8& aIV);
   145 	
   146 	/**
   147 	 * Creates a new CPBDecryptorElement object from the specified cipher, 
   148 	 * key, and IV.
   149 	 * 
   150 	 * Puts a pointer to the returned object onto the cleanup stack.
   151 	 *
   152 	 * @param aCipher	The decryption cipher
   153 	 * @param aKey		The decryption key
   154 	 * @param aIV		The Initialization Vector
   155 	 * @return			A pointer to the new CPBDecryptorElement object
   156 	 */
   157 	IMPORT_C static CPBDecryptorElement* NewLC(const TPBECipher aCipher, 
   158 		const TDesC8& aKey, const TDesC8& aIV);
   159 
   160 	/** 
   161 	 * Transforms aInput into its decrypted form, aOutput.
   162 	 *
   163 	 * aOutput must have CPBDecryptorElement::MaxOutputLength() empty bytes
   164 	 * remaining in its length. 
   165 	 *
   166 	 *	See the Cryptography api-guide documentation for an explanation of
   167 	 *	how buffering of data supplied to this function is handled.
   168 	 * 
   169 	 * @param aInput	The ciphertext.
   170 	 * @param aOutput	The plaintext.
   171 	 */
   172 	void Process(const TDesC8& aInput, TDes8& aOutput);
   173 
   174 	/** 
   175 	 * Transforms aInput into its decrypted form, aOutput.
   176 	 *
   177 	 * aOutput must have CPBDecryptorElement::MaxFinalOutputLength() 
   178 	 * empty bytes remaining in its length. 
   179 	 * 
   180 	 * @param aInput	The ciphertext.
   181 	 * @param aOutput	The plaintext.
   182 	 */
   183 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
   184 
   185 	/** 
   186 	 * Gets the maximum size of the output given a certain input length.
   187 	 * 
   188 	 * @param aMaxInputLength	The maximum input length in bytes.
   189 	 * @return					The maximum output length in bytes.
   190 	 */
   191 	TInt MaxOutputLength(TUint aMaxInputLength) const;
   192 
   193 	/** 
   194 	 * Gets the maximum size of the output given a certain input length.
   195 	 * 
   196 	 * @param aMaxInputLength	The maximum input length in bytes.
   197 	 * @return					The maximum output length in bytes.
   198 	 */
   199 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
   200 
   201 	/** Destructor */
   202 	virtual ~CPBDecryptorElement();
   203 protected:
   204 	/* @internalComponent */
   205 	CPBDecryptorElement();
   206 	/* @internalComponent */
   207 	void ConstructL(const TPBECipher aCipher, const TDesC8& aKey, const TDesC8& aIV);
   208 private:
   209 	CSymmetricCipher* iCipher;
   210 	};
   211 
   212 /**
   213  * Implements the password based encryption of multiple elements.
   214  *
   215  * @see CPBEncryptSet
   216  * @since v7.0s
   217  */
   218 class CPBEncryptorSet : public CPBEncryptor
   219 	{
   220 public:
   221 	/**
   222 	 * Creates a new CPBEncryptorSet object from the specified cipher and key,
   223 	 * and a random Initialization Vector (IV).
   224 	 *
   225 	 * @param aCipher	The encryption cipher
   226 	 * @param aKey		The encryption key
   227 	 * @return			A pointer to the new CPBEncryptorSet object
   228 	 */
   229 	IMPORT_C static CPBEncryptorSet* NewL(const TPBECipher aCipher, 
   230 		const TDesC8& aKey);
   231 
   232 	/**
   233 	 * Creates a new CPBEncryptorSet object from the specified cipher and key,
   234 	 * and a random IV.
   235 	 * 
   236 	 * Puts a pointer to the returned object onto the cleanup stack.
   237 	 *
   238 	 * @param aCipher	The encryption cipher
   239 	 * @param aKey		The encryption key
   240 	 * @return			A pointer to the new CPBEncryptorSet object
   241 	 */
   242 	IMPORT_C static CPBEncryptorSet* NewLC(const TPBECipher aCipher, 
   243 		const TDesC8& aKey);
   244 
   245 	/**
   246 	 * Resets the CPBEncryptorSet object back to its original state
   247 	 * and clears all its buffers.
   248 	 */
   249 	IMPORT_C void Reset(void);
   250 
   251 	/** 
   252 	 * Transforms aInput into its encrypted form, aOutput.
   253 	 *
   254 	 * aOutput must have CPBEncryptorSet::MaxOutputLength() empty bytes
   255 	 * remaining in its length. 
   256 	 *
   257 	 * @param aInput	The plaintext.
   258 	 * @param aOutput	The ciphertext.
   259 	 */
   260 	void Process(const TDesC8& aInput, TDes8& aOutput);
   261 
   262 	/** 
   263 	 * Transforms aInput into its encrypted form, aOutput, and applies a
   264 	 * padding scheme to ensure a block aligned result.
   265 	 *
   266 	 * aOutput must have CPBEncryptorSet::MaxFinalOutputLength() 
   267 	 * empty bytes remaining in its length. 
   268 	 * 
   269 	 * @param aInput	The plaintext.
   270 	 * @param aOutput	The ciphertext.
   271 	 */
   272 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
   273 
   274 	/** 
   275 	 * Gets the maximum size of the output given a certain input length.
   276 	 * 
   277 	 * @param aMaxInputLength	The maximum input length in bytes.
   278 	 * @return					The maximum output length in bytes.
   279 	 */
   280 	TInt MaxOutputLength(TUint aMaxInputLength) const;
   281 
   282 	/** 
   283 	 * Gets the maximum size of the output given a certain input length.
   284 	 * 
   285 	 * @param aMaxInputLength	The maximum input length in bytes.
   286 	 * @return					The maximum output length in bytes.
   287 	 */
   288 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
   289 
   290 	/** Destructor */
   291 	virtual ~CPBEncryptorSet();
   292 protected:
   293 	/* @internalComponent */
   294 	CPBEncryptorSet();
   295 	/* @internalComponent */
   296 	void ConstructL(TPBECipher aCipher, const TDesC8& aKey);
   297 private:
   298 	CSymmetricCipher* iCipher;
   299 	HBufC8* iIV;
   300 	TBool iIVSent;
   301 	};
   302 
   303 
   304 /**
   305  * Implements the password based decryption of multiple elements.
   306  *
   307  * @since v7.0s
   308  */
   309 class CPBDecryptorSet : public CPBDecryptor
   310 	{
   311 public:
   312 	/**
   313 	 * Creates a new CPBDecryptorSet object from the specified cipher and key,
   314 	 * and a random IV.
   315 	 *
   316 	 * @param aCipher	The decryption cipher
   317 	 * @param aKey		The decryption key
   318 	 * @return			A pointer to the new CPBDecryptorSet object
   319 	 */
   320 	IMPORT_C static CPBDecryptorSet* NewL(const TPBECipher aCipher, 
   321 		const TDesC8& aKey);
   322 
   323 	/**
   324 	 * Creates a new CPBDecryptorSet object from the specified cipher and key,
   325 	 * and a random IV.
   326 	 * 
   327 	 * Puts a pointer to the returned object onto the cleanup stack.
   328 	 *
   329 	 * @param aCipher	The decryption cipher
   330 	 * @param aKey		The decryption key
   331 	 * @return			A pointer to the new CPBDecryptorSet object
   332 	 */
   333 	IMPORT_C static CPBDecryptorSet* NewLC(const TPBECipher aCipher, 
   334 		const TDesC8& aKey);
   335 
   336 	/**
   337 	 * Resets the CPBDecryptorSet object back to its original state
   338 	 * and clears all its buffers.
   339 	 */
   340 	IMPORT_C void Reset(void);
   341 
   342 	/** 
   343 	 * Transforms aInput into its decrypted form, aOutput.
   344 	 *
   345 	 * aOutput must have CPBDecryptorSet::MaxOutputLength() empty bytes 
   346 	 * remaining in its length. 
   347 	 *
   348 	 * @param aInput	The ciphertext.
   349 	 * @param aOutput	The plaintext.
   350 	 */
   351 	void Process(const TDesC8& aInput, TDes8& aOutput);
   352 
   353 	/** 
   354 	 * Transforms aInput into its decrypted form, aOutput, and applies a
   355 	 * padding scheme to ensure a block aligned result.
   356 	 *
   357 	 * aOutput must have CPBDecryptorSet::MaxFinalOutputLength() 
   358 	 * empty bytes remaining in its length. 
   359 	 * 
   360 	 * @param aInput	The ciphertext.
   361 	 * @param aOutput	The plaintext.
   362 	 */
   363 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
   364 
   365 	/** 
   366 	 * Gets the maximum size of the output given a certain input length.
   367 	 * 
   368 	 * @param aMaxInputLength	The maximum input length in bytes.
   369 	 * @return					The maximum output length in bytes.
   370 	 */
   371 	TInt MaxOutputLength(TUint aMaxInputLength) const;
   372 
   373 	/** 
   374 	 * Gets the maximum size of the output given a certain input length.
   375 	 * 
   376 	 * @param aMaxInputLength	The maximum input length in bytes.
   377 	 * @return					The maximum output length in bytes.
   378 	 */
   379 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
   380 
   381 	/** Destructor */
   382 	virtual ~CPBDecryptorSet();
   383 protected:
   384 	/* @internalComponent */
   385 	CPBDecryptorSet();
   386 	/* @internalComponent */
   387 	void ConstructL(TPBECipher aCipher, const TDesC8& aKey, const TDesC8& aIV);
   388 private:
   389 	TPtrC8 ProcessIV(const TDesC8& aInput);
   390 private:
   391 	CSymmetricCipher* iCipher;
   392 	HBufC8* iIVBuf;
   393 	TBool iIVSent;
   394 	};
   395 
   396 #endif