sl@0: /*
sl@0: * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the 
sl@0: * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: /**
sl@0:  @file
sl@0:  @publishedPartner
sl@0:  @released
sl@0: */
sl@0: 
sl@0: #ifndef __PBENCRYPTOR_H__
sl@0: #define __PBENCRYPTOR_H__
sl@0: 
sl@0: #include "pbe.h"
sl@0: #include "padding.h"
sl@0: #include "msymmetriccipher.h"
sl@0: 
sl@0: /**
sl@0:  * Implements the password based encryption of elements.
sl@0:  *
sl@0:  * @see CPBEncryptElement
sl@0:  * @since v7.0s
sl@0:  */
sl@0: class CPBEncryptorElement : public CPBEncryptor
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Creates a new CPBEncryptorElement object from the specified cipher, 
sl@0: 	 * key, and Initialization Vector (IV).
sl@0: 	 *
sl@0: 	 * @param aCipher	The encryption cipher
sl@0: 	 * @param aKey		The encryption key
sl@0: 	 * @param aIV		The Initialization Vector
sl@0: 	 * @return			A pointer to the new CPBEncryptorElement object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBEncryptorElement* NewL(TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey, const TDesC8& aIV);
sl@0: 
sl@0: 	/**
sl@0: 	 * Creates a new CPBEncryptorElement object from the specified cipher, 
sl@0: 	 * key, and IV.
sl@0: 	 * 
sl@0: 	 * Puts a pointer to the returned object onto the cleanup stack.
sl@0: 	 *
sl@0: 	 * @param aCipher	The encryption cipher
sl@0: 	 * @param aKey		The encryption key
sl@0: 	 * @param aIV		The Initialization Vector
sl@0: 	 * @return			A pointer to the new CPBEncryptorElement object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBEncryptorElement* NewLC(TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey, const TDesC8& aIV);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its encrypted form, aOutput.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBEncryptorElement::MaxOutputLength() empty bytes remaining in its length. 
sl@0: 	 *
sl@0: 	 *	See the Cryptography api-guide documentation for an explanation of
sl@0: 	 *	how buffering of data supplied to this function is handled.
sl@0: 	 *
sl@0: 	 * @param aInput	The plaintext.
sl@0: 	 * @param aOutput	The ciphertext.
sl@0: 	 */
sl@0: 	void Process(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its encrypted form, aOutput, and applies a
sl@0: 	 * padding scheme to ensure a block aligned result.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBEncryptorElement::MaxFinalOutputLength() 
sl@0: 	 * empty bytes remaining in its length. 
sl@0: 	 *
sl@0: 	 *	See the Cryptography api-guide documentation for an explanation of
sl@0: 	 *	how buffering of data supplied to this function is handled.
sl@0: 	 * 
sl@0: 	 * @param aInput	The plaintext.
sl@0: 	 * @param aOutput	The ciphertext.
sl@0: 	 */
sl@0: 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output resulting from calling Process() with a
sl@0: 	 * given input length.
sl@0: 	 *
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output resulting from calling ProcessFinalL()
sl@0: 	 * with a given input length.
sl@0: 	 *
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					TInt The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** Destructor */
sl@0: 	virtual ~CPBEncryptorElement();
sl@0: protected:
sl@0: 	/* @internalComponent */
sl@0: 	CPBEncryptorElement();
sl@0: 	/* @internalComponent */
sl@0: 	void ConstructL(TPBECipher aCipher, const TDesC8& aKey, const TDesC8& aIV);
sl@0: private:
sl@0: 	CSymmetricCipher* iCipher;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0:  * Implements the password based decryption of elements.
sl@0:  *
sl@0:  * @since v7.0s
sl@0:  */
sl@0: class CPBDecryptorElement : public CPBDecryptor
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Creates a new CPBDecryptorElement object from the specified cipher, 
sl@0: 	 * key, and IV.
sl@0: 	 *
sl@0: 	 * @param aCipher	The decryption cipher
sl@0: 	 * @param aKey		The decryption key
sl@0: 	 * @param aIV		The Initialization Vector
sl@0: 	 * @return			A pointer to the new CPBDecryptorElement object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBDecryptorElement* NewL(const TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey, const TDesC8& aIV);
sl@0: 	
sl@0: 	/**
sl@0: 	 * Creates a new CPBDecryptorElement object from the specified cipher, 
sl@0: 	 * key, and IV.
sl@0: 	 * 
sl@0: 	 * Puts a pointer to the returned object onto the cleanup stack.
sl@0: 	 *
sl@0: 	 * @param aCipher	The decryption cipher
sl@0: 	 * @param aKey		The decryption key
sl@0: 	 * @param aIV		The Initialization Vector
sl@0: 	 * @return			A pointer to the new CPBDecryptorElement object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBDecryptorElement* NewLC(const TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey, const TDesC8& aIV);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its decrypted form, aOutput.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBDecryptorElement::MaxOutputLength() empty bytes
sl@0: 	 * remaining in its length. 
sl@0: 	 *
sl@0: 	 *	See the Cryptography api-guide documentation for an explanation of
sl@0: 	 *	how buffering of data supplied to this function is handled.
sl@0: 	 * 
sl@0: 	 * @param aInput	The ciphertext.
sl@0: 	 * @param aOutput	The plaintext.
sl@0: 	 */
sl@0: 	void Process(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its decrypted form, aOutput.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBDecryptorElement::MaxFinalOutputLength() 
sl@0: 	 * empty bytes remaining in its length. 
sl@0: 	 * 
sl@0: 	 * @param aInput	The ciphertext.
sl@0: 	 * @param aOutput	The plaintext.
sl@0: 	 */
sl@0: 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output given a certain input length.
sl@0: 	 * 
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output given a certain input length.
sl@0: 	 * 
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** Destructor */
sl@0: 	virtual ~CPBDecryptorElement();
sl@0: protected:
sl@0: 	/* @internalComponent */
sl@0: 	CPBDecryptorElement();
sl@0: 	/* @internalComponent */
sl@0: 	void ConstructL(const TPBECipher aCipher, const TDesC8& aKey, const TDesC8& aIV);
sl@0: private:
sl@0: 	CSymmetricCipher* iCipher;
sl@0: 	};
sl@0: 
sl@0: /**
sl@0:  * Implements the password based encryption of multiple elements.
sl@0:  *
sl@0:  * @see CPBEncryptSet
sl@0:  * @since v7.0s
sl@0:  */
sl@0: class CPBEncryptorSet : public CPBEncryptor
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Creates a new CPBEncryptorSet object from the specified cipher and key,
sl@0: 	 * and a random Initialization Vector (IV).
sl@0: 	 *
sl@0: 	 * @param aCipher	The encryption cipher
sl@0: 	 * @param aKey		The encryption key
sl@0: 	 * @return			A pointer to the new CPBEncryptorSet object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBEncryptorSet* NewL(const TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey);
sl@0: 
sl@0: 	/**
sl@0: 	 * Creates a new CPBEncryptorSet object from the specified cipher and key,
sl@0: 	 * and a random IV.
sl@0: 	 * 
sl@0: 	 * Puts a pointer to the returned object onto the cleanup stack.
sl@0: 	 *
sl@0: 	 * @param aCipher	The encryption cipher
sl@0: 	 * @param aKey		The encryption key
sl@0: 	 * @return			A pointer to the new CPBEncryptorSet object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBEncryptorSet* NewLC(const TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey);
sl@0: 
sl@0: 	/**
sl@0: 	 * Resets the CPBEncryptorSet object back to its original state
sl@0: 	 * and clears all its buffers.
sl@0: 	 */
sl@0: 	IMPORT_C void Reset(void);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its encrypted form, aOutput.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBEncryptorSet::MaxOutputLength() empty bytes
sl@0: 	 * remaining in its length. 
sl@0: 	 *
sl@0: 	 * @param aInput	The plaintext.
sl@0: 	 * @param aOutput	The ciphertext.
sl@0: 	 */
sl@0: 	void Process(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its encrypted form, aOutput, and applies a
sl@0: 	 * padding scheme to ensure a block aligned result.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBEncryptorSet::MaxFinalOutputLength() 
sl@0: 	 * empty bytes remaining in its length. 
sl@0: 	 * 
sl@0: 	 * @param aInput	The plaintext.
sl@0: 	 * @param aOutput	The ciphertext.
sl@0: 	 */
sl@0: 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output given a certain input length.
sl@0: 	 * 
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output given a certain input length.
sl@0: 	 * 
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** Destructor */
sl@0: 	virtual ~CPBEncryptorSet();
sl@0: protected:
sl@0: 	/* @internalComponent */
sl@0: 	CPBEncryptorSet();
sl@0: 	/* @internalComponent */
sl@0: 	void ConstructL(TPBECipher aCipher, const TDesC8& aKey);
sl@0: private:
sl@0: 	CSymmetricCipher* iCipher;
sl@0: 	HBufC8* iIV;
sl@0: 	TBool iIVSent;
sl@0: 	};
sl@0: 
sl@0: 
sl@0: /**
sl@0:  * Implements the password based decryption of multiple elements.
sl@0:  *
sl@0:  * @since v7.0s
sl@0:  */
sl@0: class CPBDecryptorSet : public CPBDecryptor
sl@0: 	{
sl@0: public:
sl@0: 	/**
sl@0: 	 * Creates a new CPBDecryptorSet object from the specified cipher and key,
sl@0: 	 * and a random IV.
sl@0: 	 *
sl@0: 	 * @param aCipher	The decryption cipher
sl@0: 	 * @param aKey		The decryption key
sl@0: 	 * @return			A pointer to the new CPBDecryptorSet object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBDecryptorSet* NewL(const TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey);
sl@0: 
sl@0: 	/**
sl@0: 	 * Creates a new CPBDecryptorSet object from the specified cipher and key,
sl@0: 	 * and a random IV.
sl@0: 	 * 
sl@0: 	 * Puts a pointer to the returned object onto the cleanup stack.
sl@0: 	 *
sl@0: 	 * @param aCipher	The decryption cipher
sl@0: 	 * @param aKey		The decryption key
sl@0: 	 * @return			A pointer to the new CPBDecryptorSet object
sl@0: 	 */
sl@0: 	IMPORT_C static CPBDecryptorSet* NewLC(const TPBECipher aCipher, 
sl@0: 		const TDesC8& aKey);
sl@0: 
sl@0: 	/**
sl@0: 	 * Resets the CPBDecryptorSet object back to its original state
sl@0: 	 * and clears all its buffers.
sl@0: 	 */
sl@0: 	IMPORT_C void Reset(void);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its decrypted form, aOutput.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBDecryptorSet::MaxOutputLength() empty bytes 
sl@0: 	 * remaining in its length. 
sl@0: 	 *
sl@0: 	 * @param aInput	The ciphertext.
sl@0: 	 * @param aOutput	The plaintext.
sl@0: 	 */
sl@0: 	void Process(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Transforms aInput into its decrypted form, aOutput, and applies a
sl@0: 	 * padding scheme to ensure a block aligned result.
sl@0: 	 *
sl@0: 	 * aOutput must have CPBDecryptorSet::MaxFinalOutputLength() 
sl@0: 	 * empty bytes remaining in its length. 
sl@0: 	 * 
sl@0: 	 * @param aInput	The ciphertext.
sl@0: 	 * @param aOutput	The plaintext.
sl@0: 	 */
sl@0: 	void ProcessFinalL(const TDesC8& aInput, TDes8& aOutput);
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output given a certain input length.
sl@0: 	 * 
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** 
sl@0: 	 * Gets the maximum size of the output given a certain input length.
sl@0: 	 * 
sl@0: 	 * @param aMaxInputLength	The maximum input length in bytes.
sl@0: 	 * @return					The maximum output length in bytes.
sl@0: 	 */
sl@0: 	TInt MaxFinalOutputLength(TUint aMaxInputLength) const;
sl@0: 
sl@0: 	/** Destructor */
sl@0: 	virtual ~CPBDecryptorSet();
sl@0: protected:
sl@0: 	/* @internalComponent */
sl@0: 	CPBDecryptorSet();
sl@0: 	/* @internalComponent */
sl@0: 	void ConstructL(TPBECipher aCipher, const TDesC8& aKey, const TDesC8& aIV);
sl@0: private:
sl@0: 	TPtrC8 ProcessIV(const TDesC8& aInput);
sl@0: private:
sl@0: 	CSymmetricCipher* iCipher;
sl@0: 	HBufC8* iIVBuf;
sl@0: 	TBool iIVSent;
sl@0: 	};
sl@0: 
sl@0: #endif