os/security/crypto/weakcrypto/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 	/**
    54 	 * Indicates whether a supplied key is weak. If the key is one of the weak keys
    55 	 * defined by the crypto library (e.g. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01})
    56 	 * ETrue is returned.
    57 	 * 
    58 	 * @param aKey	    		The Key to be checked. The key length must be
    59 	 							KDESKeySize = 8 bytes.
    60 	 * @return					Whether the key is weak (ETrue or EFalse)
    61 	 *						
    62 	 */
    63 	IMPORT_C static TBool IsWeakKey(const TDesC8& aKey);
    64 	virtual ~CDES();
    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 private:
   128 	CDESEncryptor(void);
   129 };
   130 
   131 /**
   132 * Concrete class for DES decryption.
   133 *
   134 */
   135 class CDESDecryptor : public CDES
   136 {
   137 public:
   138 	/**
   139 	* Creates an instance of this class.
   140 	*
   141 	* @param aKey			The key to be used for decryption. The key length must be
   142 	*						KDESKeySize = 8 bytes.
   143 	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
   144 	*						a set of known weak key values. Defaults to ETrue.
   145 	* @return				A pointer to the new CDESDecryptor object.
   146 	*
   147 	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
   148 	*								previously cleaned up any previously allocated memory.
   149 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   150 	*								cipher strength restrictions of the crypto library.
   151 	*								See TCrypto::IsSymmetricWeakEnoughL()
   152 	*/
   153 	IMPORT_C static CDESDecryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
   154 
   155 	/**
   156 	* Creates an instance of this class and leaves it on the cleanup stack.
   157 	* 
   158 	* @param aKey			The key to be used for decryption. The key length must be
   159 	*						KDESKeySize = 8 bytes.
   160 	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
   161 	*						a set of known weak key values. Defaults to ETrue.
   162 	* @return				A pointer to the new CDESDecryptor object.
   163 	*
   164 	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
   165 	*								previously cleaned up any previously allocated memory.
   166 	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   167 	*								cipher strength restrictions of the crypto library.
   168 	*								See TCrypto::IsSymmetricWeakEnoughL()
   169 	*/
   170 	IMPORT_C static CDESDecryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
   171 protected:	//	From CDES
   172 	virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer);
   173 private:
   174 	CDESDecryptor(void);
   175 };
   176 
   177 #endif	//	__DATAENCRYPTIONSTANDARD_H__