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: * RC4 implementation
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 __ARC4_H__
sl@0: #define __ARC4_H__
sl@0:
sl@0: #include "streamcipher.h"
sl@0:
sl@0: /** The size of the substitution box (i.e. lookup table) in bytes. */
sl@0: const TInt KSBoxSize = 256;
sl@0:
sl@0: /** Maximum ARC4 key size in bytes. */
sl@0: const TInt KMaxARC4KeyBytes = 256; //2048 bits
sl@0:
sl@0: /** Number of bytes to discard by default from an ARC4 key stream. */
sl@0: const TUint KDefaultDiscardBytes = 768;
sl@0:
sl@0: /**
sl@0: * Implements an RC4-compatible stream cipher that outputs a pseudorandom stream
sl@0: * of bits, having been initialised with a key.
sl@0: *
sl@0: */
sl@0: class CARC4 : public CStreamCipher
sl@0: {
sl@0: public:
sl@0: /**
sl@0: * Constructs an instance of a CARC4 object, and initialises it with a key and
sl@0: * (optionally) the number of initial bytes to discard. Defaults to 256.
sl@0: *
sl@0: * The number of dropped bytes must be agreed with the other
sl@0: * party, with which information is to be exchanged, prior to encipherment.
sl@0: *
sl@0: * @note Several papers have been published indicating that there are weaknesses
sl@0: * in the first bytes of an ARC4 byte stream. A search for "ARC4
sl@0: * discard" should find these papers. Recommended practice is to drop the first
sl@0: * KDefaultDiscardBytes bytes of the key stream.
sl@0: *
sl@0: * @param aKey The key to use. aKey must be less than or equal to
sl@0: * KRC4MaxKeySizeBytes.
sl@0: * @param aDiscardBytes The number of bytes to drop from the beginning of the key
sl@0: * stream.
sl@0: * @return A pointer to the new CARC4 object.
sl@0: *
sl@0: * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
sl@0: * cipher strength restrictions of the crypto library.
sl@0: * See TCrypto::IsSymmetricWeakEnoughL()
sl@0: */
sl@0: IMPORT_C static CARC4* NewL(const TDesC8& aKey,
sl@0: TUint aDiscardBytes = KDefaultDiscardBytes);
sl@0:
sl@0: /**
sl@0: * Constructs an instance of a CARC4 object, and initialises it with a key and
sl@0: * (optionally) the number of initial bytes to discard. Defaults to 256.
sl@0: *
sl@0: * The number of dropped bytes must be agreed with the other
sl@0: * party, with which information is to be exchanged, prior to encipherment.
sl@0: *
sl@0: * @see CARC4::NewL()
sl@0: *
sl@0: * @param aKey The key to use. aKey must be less than or equal to
sl@0: * KRC4MaxKeySizeBytes.
sl@0: * @param aDiscardBytes The number of bytes to drop from the beginning of the key
sl@0: * stream.
sl@0: * @return A pointer to the new CARC4 object.
sl@0: *
sl@0: * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
sl@0: * cipher strength restrictions of the crypto library.
sl@0: * See TCrypto::IsSymmetricWeakEnoughL()
sl@0: */
sl@0: IMPORT_C static CARC4* NewLC(const TDesC8& aKey,
sl@0: TUint aDiscardBytes = KDefaultDiscardBytes);
sl@0: public:
sl@0: virtual void Reset(void);
sl@0: virtual TInt KeySize(void) const;
sl@0: protected:
sl@0: /**
sl@0: * Performs an ARC4 encryption or decryption on supplied data.
sl@0: *
sl@0: * @note ARC4 encryption and decryption are symmetrical.
sl@0: *
sl@0: * @param aData On input, data to be transformed;
sl@0: * on return, transformed data.
sl@0: */
sl@0: virtual void DoProcess(TDes8& aData);
sl@0: private:
sl@0: CARC4(const TDesC8& aKey, TUint aDiscardBytes);
sl@0: void GenerateSBox();
sl@0: inline TUint8 GenerateByte();
sl@0: void DiscardBytes(TInt aDiscardBytes);
sl@0: private:
sl@0: TUint8 ix;
sl@0: TUint8 iy;
sl@0: TInt iDiscardBytes;
sl@0: TUint8 iState[KSBoxSize];
sl@0: TBuf8 iKey;
sl@0: };
sl@0:
sl@0: #endif // __ARC4_H__