os/security/crypto/weakcryptospi/inc/des.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 * DES encryptor and decryptor implementation
    18 *
    19 */
    20 
    21 
    22 /**
    23  @file 
    24  @publishedPartner
    25  @released 
    26 */
    27  
    28 #ifndef __DATAENCRYPTIONSTANDARD_H__
    29 #define __DATAENCRYPTIONSTANDARD_H__
    30 
    31 #include <blocktransformation.h>
    32 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
    33 #include <securityerr.h>
    34 #endif
    35 
    36 /** The size of the key schedule array (in 32-bit words).
    37 * 
    38 */
    39 const TUint KDESScheduleSizeInWords = 32;
    40 
    41 /**
    42 * Abstract base class for DES, implementing features common between DES encryption and
    43 * decryption. From CBlockTransformation
    44 * 
    45 */
    46 class CDES : public CBlockTransformation
    47 {
    48 public:	
    49 	virtual void Transform(TDes8& aBlock);
    50 	virtual TInt BlockSize() const;
    51 	virtual TInt KeySize() const;
    52 	virtual void Reset();
    53 	virtual ~CDES();
    54 	
    55  	/**
    56 	Indicates whether a supplied key is weak. If the key is one of the weak keys
    57  	defined by the crypto library (e.g. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01})
    58  	ETrue is returned.
    59  	@param aKey	    		The Key to be checked. The key length must be
    60  	 							KDESKeySize = 8 bytes.
    61  	@return					Whether the key is weak (ETrue or EFalse)					
    62  	*/
    63  	IMPORT_C static TBool IsWeakKey(const TDesC8& aKey);
    64 	
    65 protected:
    66 	/** @internalAll */
    67 	CDES();
    68 	/** @internalAll */
    69 	void DoTransform(TUint32& l, TUint32& r, const TUint32* aKey);
    70 	virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer);
    71 	virtual void ConstructL(const TDesC8& aKey, TBool aCheckWeakKey);
    72 protected:
    73 	/** 
    74 	 * Key schedule array 
    75 	 *
    76 	 * Also used as the first key in triple-DES
    77 	 */
    78 	TUint32 iK1[KDESScheduleSizeInWords];	//	= 32
    79 	/** 
    80 	 * The initial key. 
    81 	 *
    82 	 * The key length must be KDESKeySize = 8 bytes.
    83 	 */
    84 	HBufC8* iKey;
    85 };
    86 
    87 /**
    88 * Concrete class for DES encryption.
    89 * 
    90 */
    91 class CDESEncryptor : public CDES
    92 {
    93 public:
    94 	/**
    95 	* Creates an instance of this class.
    96 	* 
    97 	* @param aKey			The key to be used for encryption. The key length must be
    98 	*						KDESKeySize = 8 bytes.
    99 	* @param aCheckWeakKey	Boolean determining whether to check the key against
   100 	*						a set of known weak key values. Defaults to ETrue. 
   101 	* @return				A pointer to the new CDESEncryptor object.
   102 	*
   103 	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
   104 	*								previously cleaned up any previously allocated memory.
   105 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   106 	*								cipher strength restrictions of the crypto library.
   107 	* 								See TCrypto::IsSymmetricWeakEnoughL()
   108 	*/
   109 	IMPORT_C static CDESEncryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
   110 
   111 	/**
   112 	* Creates an instance of this class and leaves it on the cleanup stack.
   113 	*
   114 	* @param aKey			The key to be used for encryption. The key length must be
   115 	*						KDESKeySize = 8 bytes.
   116 	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
   117 	*						a set of known weak key values. Defaults to ETrue. 
   118 	* @return				A pointer to the new CDESEncryptor object.
   119 	*
   120 	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
   121 	*								previously cleaned up any previously allocated memory.
   122 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   123 	*								cipher strength restrictions of the crypto library.
   124 	*								See TCrypto::IsSymmetricWeakEnoughL()
   125 	*/
   126 	IMPORT_C static CDESEncryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
   127 protected:
   128 	/** @internalComponent */
   129 	CDESEncryptor(void);
   130 };
   131 
   132 /**
   133 * Concrete class for DES decryption.
   134 *
   135 */
   136 class CDESDecryptor : public CDES
   137 {
   138 public:
   139 	/**
   140 	* Creates an instance of this class.
   141 	*
   142 	* @param aKey			The key to be used for decryption. The key length must be
   143 	*						KDESKeySize = 8 bytes.
   144 	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
   145 	*						a set of known weak key values. Defaults to ETrue.
   146 	* @return				A pointer to the new CDESDecryptor object.
   147 	*
   148 	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
   149 	*								previously cleaned up any previously allocated memory.
   150 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   151 	*								cipher strength restrictions of the crypto library.
   152 	*								See TCrypto::IsSymmetricWeakEnoughL()
   153 	*/
   154 	IMPORT_C static CDESDecryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
   155 
   156 	/**
   157 	* Creates an instance of this class and leaves it on the cleanup stack.
   158 	* 
   159 	* @param aKey			The key to be used for decryption. The key length must be
   160 	*						KDESKeySize = 8 bytes.
   161 	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
   162 	*						a set of known weak key values. Defaults to ETrue.
   163 	* @return				A pointer to the new CDESDecryptor object.
   164 	*
   165 	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
   166 	*								previously cleaned up any previously allocated memory.
   167 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   168 	*								cipher strength restrictions of the crypto library.
   169 	*								See TCrypto::IsSymmetricWeakEnoughL()
   170 	*/
   171 	IMPORT_C static CDESDecryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
   172 protected:	//	From CDES
   173 	virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer);
   174 	/** @internalComponent */
   175 	CDESDecryptor(void);
   176 };
   177 
   178 #endif	//	__DATAENCRYPTIONSTANDARD_H__