crypto/weakcrypto/inc/arc4.h
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     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 * RC4 implementation
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 /**
       
    25  @file 
       
    26  @internalAll
       
    27 */
       
    28  
       
    29 #ifndef __ARC4_H__
       
    30 #define __ARC4_H__
       
    31 
       
    32 #include "streamcipher.h"
       
    33 
       
    34 /** The size of the substitution box (i.e. lookup table) in bytes. */
       
    35 const TInt KSBoxSize = 256;
       
    36 
       
    37 /** Maximum ARC4 key size in bytes. */
       
    38 const TInt KMaxARC4KeyBytes = 256; //2048 bits
       
    39 
       
    40 /** Number of bytes to discard by default from an ARC4 key stream. */
       
    41 const TUint KDefaultDiscardBytes = 768;
       
    42 
       
    43 /**
       
    44 * Implements an RC4-compatible stream cipher that outputs a pseudorandom stream
       
    45 * of bits, having been initialised with a key. 
       
    46 *
       
    47 * @publishedPartner
       
    48 * @released 
       
    49 */
       
    50 class CARC4 : public CStreamCipher
       
    51 {
       
    52 public:
       
    53 	/**
       
    54 	* Constructs an instance of a CARC4 object, and initialises it with a key and
       
    55 	* (optionally) the number of initial bytes to discard. Defaults to 256. 
       
    56 	*
       
    57 	* The number of dropped bytes <b>must</b> be agreed with the other
       
    58 	* party, with which information is to be exchanged, prior to encipherment.
       
    59 	*
       
    60 	* @note	Several papers have been published indicating that there are weaknesses 
       
    61 	*		in the first bytes of an ARC4 byte stream.  A search for "ARC4
       
    62 	*		discard" should find these papers.  Recommended practice is to drop the first
       
    63 	*		KDefaultDiscardBytes bytes of the key stream.  
       
    64 	*
       
    65 	* @param aKey			The key to use.  aKey must be less than or equal to
       
    66 	*						KRC4MaxKeySizeBytes.  
       
    67 	* @param aDiscardBytes	The number of bytes to drop from the beginning of the key
       
    68 	*						stream.
       
    69 	* @return				A pointer to the new CARC4 object.
       
    70 	*  
       
    71 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
       
    72 	*								cipher strength restrictions of the crypto library.
       
    73 	*								See TCrypto::IsSymmetricWeakEnoughL()
       
    74 	*/
       
    75 	IMPORT_C static CARC4* NewL(const TDesC8& aKey, 
       
    76 		TUint aDiscardBytes = KDefaultDiscardBytes);
       
    77 
       
    78 	/**
       
    79 	* Constructs an instance of a CARC4 object, and initialises it with a key and
       
    80 	* (optionally) the number of initial bytes to discard. Defaults to 256. 
       
    81 	*
       
    82 	* The number of dropped bytes <b>must</b> be agreed with the other
       
    83 	* party, with which information is to be exchanged, prior to encipherment.
       
    84 	*
       
    85 	* @see CARC4::NewL()
       
    86 	*
       
    87 	* @param aKey			The key to use.  aKey must be less than or equal to
       
    88 	*						KRC4MaxKeySizeBytes.  
       
    89 	* @param aDiscardBytes	The number of bytes to drop from the beginning of the key
       
    90 	*						stream.
       
    91 	* @return				A pointer to the new CARC4 object.
       
    92 	*  
       
    93 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
       
    94 	*								cipher strength restrictions of the crypto library.
       
    95 	*								See TCrypto::IsSymmetricWeakEnoughL()
       
    96 	*/
       
    97 	IMPORT_C static CARC4* NewLC(const TDesC8& aKey, 
       
    98 		TUint aDiscardBytes = KDefaultDiscardBytes);
       
    99 public:	
       
   100 	virtual void Reset(void);
       
   101 	virtual TInt KeySize(void) const;
       
   102 protected:
       
   103 	/**	
       
   104 	 * Performs an ARC4 encryption or decryption on supplied data.
       
   105 	 * 
       
   106 	 * @note ARC4 encryption and decryption are symmetrical.
       
   107 	 *
       
   108 	 * @param aData	On input, data to be transformed; 
       
   109 	 *				on return, transformed data.
       
   110 	 */
       
   111 	virtual void DoProcess(TDes8& aData);
       
   112 private:
       
   113 	CARC4(const TDesC8& aKey, TUint aDiscardBytes);
       
   114 	void GenerateSBox();
       
   115 	inline TUint8 GenerateByte();
       
   116 	void DiscardBytes(TInt aDiscardBytes);
       
   117 private:
       
   118 	TUint8 ix;
       
   119 	TUint8 iy;
       
   120 	TInt iDiscardBytes;
       
   121 	TUint8 iState[KSBoxSize];
       
   122 	TBuf8<KMaxARC4KeyBytes> iKey;
       
   123 };
       
   124 
       
   125 #endif	//	__ARC4_H__