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