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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the 
sl@0
    16
* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
sl@0
    17
* DES encryptor and decryptor implementation
sl@0
    18
*
sl@0
    19
*/
sl@0
    20
sl@0
    21
sl@0
    22
/**
sl@0
    23
 @file 
sl@0
    24
 @publishedPartner
sl@0
    25
 @released 
sl@0
    26
*/
sl@0
    27
 
sl@0
    28
#ifndef __DATAENCRYPTIONSTANDARD_H__
sl@0
    29
#define __DATAENCRYPTIONSTANDARD_H__
sl@0
    30
sl@0
    31
#include "blocktransformation.h"
sl@0
    32
#ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
sl@0
    33
#include <securityerr.h>
sl@0
    34
#endif
sl@0
    35
sl@0
    36
/** The size of the key schedule array (in 32-bit words).
sl@0
    37
* 
sl@0
    38
*/
sl@0
    39
const TUint KDESScheduleSizeInWords = 32;
sl@0
    40
sl@0
    41
/**
sl@0
    42
* Abstract base class for DES, implementing features common between DES encryption and
sl@0
    43
* decryption. From CBlockTransformation
sl@0
    44
* 
sl@0
    45
*/
sl@0
    46
class CDES : public CBlockTransformation
sl@0
    47
{
sl@0
    48
public:	
sl@0
    49
	virtual void Transform(TDes8& aBlock);
sl@0
    50
	virtual TInt BlockSize() const;
sl@0
    51
	virtual TInt KeySize() const;
sl@0
    52
	virtual void Reset();
sl@0
    53
	/**
sl@0
    54
	 * Indicates whether a supplied key is weak. If the key is one of the weak keys
sl@0
    55
	 * defined by the crypto library (e.g. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01})
sl@0
    56
	 * ETrue is returned.
sl@0
    57
	 * 
sl@0
    58
	 * @param aKey	    		The Key to be checked. The key length must be
sl@0
    59
	 							KDESKeySize = 8 bytes.
sl@0
    60
	 * @return					Whether the key is weak (ETrue or EFalse)
sl@0
    61
	 *						
sl@0
    62
	 */
sl@0
    63
	IMPORT_C static TBool IsWeakKey(const TDesC8& aKey);
sl@0
    64
	virtual ~CDES();
sl@0
    65
protected:
sl@0
    66
	/** @internalAll */
sl@0
    67
	CDES();
sl@0
    68
	/** @internalAll */
sl@0
    69
	void DoTransform(TUint32& l, TUint32& r, const TUint32* aKey);
sl@0
    70
	virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer);
sl@0
    71
	virtual void ConstructL(const TDesC8& aKey, TBool aCheckWeakKey);
sl@0
    72
protected:
sl@0
    73
	/** 
sl@0
    74
	 * Key schedule array 
sl@0
    75
	 *
sl@0
    76
	 * Also used as the first key in triple-DES
sl@0
    77
	 */
sl@0
    78
	TUint32 iK1[KDESScheduleSizeInWords];	//	= 32
sl@0
    79
	/** 
sl@0
    80
	 * The initial key. 
sl@0
    81
	 *
sl@0
    82
	 * The key length must be KDESKeySize = 8 bytes.
sl@0
    83
	 */
sl@0
    84
	HBufC8* iKey;
sl@0
    85
};
sl@0
    86
sl@0
    87
/**
sl@0
    88
* Concrete class for DES encryption.
sl@0
    89
* 
sl@0
    90
*/
sl@0
    91
class CDESEncryptor : public CDES
sl@0
    92
{
sl@0
    93
public:
sl@0
    94
	/**
sl@0
    95
	* Creates an instance of this class.
sl@0
    96
	* 
sl@0
    97
	* @param aKey			The key to be used for encryption. The key length must be
sl@0
    98
	*						KDESKeySize = 8 bytes.
sl@0
    99
	* @param aCheckWeakKey	Boolean determining whether to check the key against
sl@0
   100
	*						a set of known weak key values. Defaults to ETrue. 
sl@0
   101
	* @return				A pointer to the new CDESEncryptor object.
sl@0
   102
	*
sl@0
   103
	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
sl@0
   104
	*								previously cleaned up any previously allocated memory.
sl@0
   105
	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
sl@0
   106
	*								cipher strength restrictions of the crypto library.
sl@0
   107
	* 								See TCrypto::IsSymmetricWeakEnoughL()
sl@0
   108
	*/
sl@0
   109
	IMPORT_C static CDESEncryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
sl@0
   110
sl@0
   111
	/**
sl@0
   112
	* Creates an instance of this class and leaves it on the cleanup stack.
sl@0
   113
	*
sl@0
   114
	* @param aKey			The key to be used for encryption. The key length must be
sl@0
   115
	*						KDESKeySize = 8 bytes.
sl@0
   116
	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
sl@0
   117
	*						a set of known weak key values. Defaults to ETrue. 
sl@0
   118
	* @return				A pointer to the new CDESEncryptor object.
sl@0
   119
	*
sl@0
   120
	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
sl@0
   121
	*								previously cleaned up any previously allocated memory.
sl@0
   122
	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
sl@0
   123
	*								cipher strength restrictions of the crypto library.
sl@0
   124
	*								See TCrypto::IsSymmetricWeakEnoughL()
sl@0
   125
	*/
sl@0
   126
	IMPORT_C static CDESEncryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
sl@0
   127
private:
sl@0
   128
	CDESEncryptor(void);
sl@0
   129
};
sl@0
   130
sl@0
   131
/**
sl@0
   132
* Concrete class for DES decryption.
sl@0
   133
*
sl@0
   134
*/
sl@0
   135
class CDESDecryptor : public CDES
sl@0
   136
{
sl@0
   137
public:
sl@0
   138
	/**
sl@0
   139
	* Creates an instance of this class.
sl@0
   140
	*
sl@0
   141
	* @param aKey			The key to be used for decryption. The key length must be
sl@0
   142
	*						KDESKeySize = 8 bytes.
sl@0
   143
	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
sl@0
   144
	*						a set of known weak key values. Defaults to ETrue.
sl@0
   145
	* @return				A pointer to the new CDESDecryptor object.
sl@0
   146
	*
sl@0
   147
	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
sl@0
   148
	*								previously cleaned up any previously allocated memory.
sl@0
   149
	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
sl@0
   150
	*								cipher strength restrictions of the crypto library.
sl@0
   151
	*								See TCrypto::IsSymmetricWeakEnoughL()
sl@0
   152
	*/
sl@0
   153
	IMPORT_C static CDESDecryptor* NewL(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
sl@0
   154
sl@0
   155
	/**
sl@0
   156
	* Creates an instance of this class and leaves it on the cleanup stack.
sl@0
   157
	* 
sl@0
   158
	* @param aKey			The key to be used for decryption. The key length must be
sl@0
   159
	*						KDESKeySize = 8 bytes.
sl@0
   160
	* @param aCheckWeakKey	Boolean determining whether to check the resultant key against
sl@0
   161
	*						a set of known weak key values. Defaults to ETrue.
sl@0
   162
	* @return				A pointer to the new CDESDecryptor object.
sl@0
   163
	*
sl@0
   164
	* @leave KErrWeakKey			If the key is a weak one, the function leaves having
sl@0
   165
	*								previously cleaned up any previously allocated memory.
sl@0
   166
	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
sl@0
   167
	*								cipher strength restrictions of the crypto library.
sl@0
   168
	*								See TCrypto::IsSymmetricWeakEnoughL()
sl@0
   169
	*/
sl@0
   170
	IMPORT_C static CDESDecryptor* NewLC(const TDesC8& aKey, TBool aCheckWeakKey = ETrue);
sl@0
   171
protected:	//	From CDES
sl@0
   172
	virtual void SetKey(const TDesC8& aKey, TUint32* aKeyBuffer);
sl@0
   173
private:
sl@0
   174
	CDESDecryptor(void);
sl@0
   175
};
sl@0
   176
sl@0
   177
#endif	//	__DATAENCRYPTIONSTANDARD_H__