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