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