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: * DES encryptor and decryptor 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 __DATAENCRYPTIONSTANDARD_H__ sl@0: #define __DATAENCRYPTIONSTANDARD_H__ sl@0: sl@0: #include "blocktransformation.h" sl@0: #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS sl@0: #include sl@0: #endif sl@0: sl@0: /** The size of the key schedule array (in 32-bit words). sl@0: * sl@0: */ sl@0: const TUint KDESScheduleSizeInWords = 32; sl@0: sl@0: /** sl@0: * Abstract base class for DES, implementing features common between DES encryption and sl@0: * decryption. From CBlockTransformation sl@0: * sl@0: */ sl@0: class CDES : public CBlockTransformation sl@0: { sl@0: public: sl@0: virtual void Transform(TDes8& aBlock); sl@0: virtual TInt BlockSize() const; sl@0: virtual TInt KeySize() const; sl@0: virtual void Reset(); sl@0: /** sl@0: * Indicates whether a supplied key is weak. If the key is one of the weak keys sl@0: * defined by the crypto library (e.g. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}) sl@0: * ETrue is returned. sl@0: * sl@0: * @param aKey The Key to be checked. The key length must be sl@0: KDESKeySize = 8 bytes. sl@0: * @return Whether the key is weak (ETrue or EFalse) sl@0: * sl@0: */ sl@0: IMPORT_C static TBool IsWeakKey(const TDesC8& aKey); sl@0: virtual ~CDES(); sl@0: protected: sl@0: /** @internalAll */ sl@0: CDES(); sl@0: /** @internalAll */ sl@0: void DoTransform(TUint32& l, TUint32& r, const TUint32* aKey); sl@0: virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer); sl@0: virtual void ConstructL(const TDesC8& aKey, TBool aCheckWeakKey); sl@0: protected: sl@0: /** sl@0: * Key schedule array sl@0: * sl@0: * Also used as the first key in triple-DES sl@0: */ sl@0: TUint32 iK1[KDESScheduleSizeInWords]; // = 32 sl@0: /** sl@0: * The initial key. sl@0: * sl@0: * The key length must be KDESKeySize = 8 bytes. sl@0: */ sl@0: HBufC8* iKey; sl@0: }; sl@0: sl@0: /** sl@0: * Concrete class for DES encryption. sl@0: * sl@0: */ sl@0: class CDESEncryptor : public CDES sl@0: { sl@0: public: sl@0: /** sl@0: * Creates an instance of this class. sl@0: * sl@0: * @param aKey The key to be used for encryption. The key length must be sl@0: * KDESKeySize = 8 bytes. sl@0: * @param aCheckWeakKey Boolean determining whether to check the key against sl@0: * a set of known weak key values. Defaults to ETrue. sl@0: * @return A pointer to the new CDESEncryptor object. sl@0: * sl@0: * @leave KErrWeakKey If the key is a weak one, the function leaves having sl@0: * previously cleaned up any previously allocated memory. 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 CDESEncryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue); sl@0: sl@0: /** sl@0: * Creates an instance of this class and leaves it on the cleanup stack. sl@0: * sl@0: * @param aKey The key to be used for encryption. The key length must be sl@0: * KDESKeySize = 8 bytes. sl@0: * @param aCheckWeakKey Boolean determining whether to check the resultant key against sl@0: * a set of known weak key values. Defaults to ETrue. sl@0: * @return A pointer to the new CDESEncryptor object. sl@0: * sl@0: * @leave KErrWeakKey If the key is a weak one, the function leaves having sl@0: * previously cleaned up any previously allocated memory. 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 CDESEncryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue); sl@0: private: sl@0: CDESEncryptor(void); sl@0: }; sl@0: sl@0: /** sl@0: * Concrete class for DES decryption. sl@0: * sl@0: */ sl@0: class CDESDecryptor : public CDES sl@0: { sl@0: public: sl@0: /** sl@0: * Creates an instance of this class. sl@0: * sl@0: * @param aKey The key to be used for decryption. The key length must be sl@0: * KDESKeySize = 8 bytes. sl@0: * @param aCheckWeakKey Boolean determining whether to check the resultant key against sl@0: * a set of known weak key values. Defaults to ETrue. sl@0: * @return A pointer to the new CDESDecryptor object. sl@0: * sl@0: * @leave KErrWeakKey If the key is a weak one, the function leaves having sl@0: * previously cleaned up any previously allocated memory. 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 CDESDecryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue); sl@0: sl@0: /** sl@0: * Creates an instance of this class and leaves it on the cleanup stack. sl@0: * sl@0: * @param aKey The key to be used for decryption. The key length must be sl@0: * KDESKeySize = 8 bytes. sl@0: * @param aCheckWeakKey Boolean determining whether to check the resultant key against sl@0: * a set of known weak key values. Defaults to ETrue. sl@0: * @return A pointer to the new CDESDecryptor object. sl@0: * sl@0: * @leave KErrWeakKey If the key is a weak one, the function leaves having sl@0: * previously cleaned up any previously allocated memory. 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 CDESDecryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue); sl@0: protected: // From CDES sl@0: virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer); sl@0: private: sl@0: CDESDecryptor(void); sl@0: }; sl@0: sl@0: #endif // __DATAENCRYPTIONSTANDARD_H__