epoc32/include/asymmetric.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@4
     1
/*
williamr@4
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@4
     3
* All rights reserved.
williamr@4
     4
* This component and the accompanying materials are made available
williamr@4
     5
* under the terms of the License "Eclipse Public License v1.0"
williamr@4
     6
* which accompanies this distribution, and is available
williamr@4
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@4
     8
*
williamr@4
     9
* Initial Contributors:
williamr@4
    10
* Nokia Corporation - initial contribution.
williamr@4
    11
*
williamr@4
    12
* Contributors:
williamr@4
    13
*
williamr@4
    14
* Description: 
williamr@4
    15
* ** IMPORTANT **  API's in this file are published to 3rd party developers via the 
williamr@4
    16
* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
williamr@4
    17
* Asymmetric crypto implementation
williamr@4
    18
*
williamr@4
    19
*/
williamr@4
    20
williamr@4
    21
williamr@4
    22
/**
williamr@4
    23
 @file 
williamr@4
    24
 @publishedAll
williamr@4
    25
 @released 
williamr@4
    26
*/
williamr@4
    27
 
williamr@4
    28
#ifndef __ASYMMETRIC_H__
williamr@4
    29
#define __ASYMMETRIC_H__
williamr@4
    30
williamr@4
    31
#include <padding.h>
williamr@4
    32
#include <asymmetrickeys.h>
williamr@4
    33
#include <random.h>
williamr@4
    34
#include <hash.h>
williamr@4
    35
williamr@4
    36
// All the classes in this file have their default constructors and
williamr@4
    37
// assignment operators defined private, but not implemented, in order to
williamr@4
    38
// prevent their use.
williamr@4
    39
williamr@4
    40
/** 
williamr@4
    41
* Mixin class defining common operations for public key encryption and
williamr@4
    42
* decryption classes.
williamr@4
    43
* 
williamr@4
    44
*/
williamr@4
    45
class MCryptoSystem 
williamr@4
    46
	{
williamr@4
    47
public:
williamr@4
    48
	/**
williamr@4
    49
	 * Gets the maximum size of input accepted by this object.
williamr@4
    50
	 *	
williamr@4
    51
	 * @return	The maximum input length allowed in bytes.
williamr@4
    52
	 */	 
williamr@4
    53
	virtual TInt MaxInputLength(void) const = 0;
williamr@4
    54
	
williamr@4
    55
	/**
williamr@4
    56
	 * Gets the maximum size of output that can be generated by this object.
williamr@4
    57
	 *
williamr@4
    58
	 * @return	The maximum output length in bytes.
williamr@4
    59
	 */	 
williamr@4
    60
	virtual TInt MaxOutputLength(void) const = 0;
williamr@4
    61
protected:
williamr@4
    62
	/**
williamr@4
    63
	 * Constructor
williamr@4
    64
 	 */	 
williamr@4
    65
	IMPORT_C MCryptoSystem(void);
williamr@4
    66
private:
williamr@4
    67
	MCryptoSystem(const MCryptoSystem&);
williamr@4
    68
	MCryptoSystem& operator=(const MCryptoSystem&);
williamr@4
    69
	};
williamr@4
    70
williamr@4
    71
/** 
williamr@4
    72
* Abstract base class for all public key encryptors.
williamr@4
    73
* 
williamr@4
    74
*/
williamr@4
    75
class CEncryptor : public CBase, public MCryptoSystem
williamr@4
    76
	{
williamr@4
    77
public:
williamr@4
    78
	/**
williamr@4
    79
	 * Encrypts the specified plaintext into ciphertext.
williamr@4
    80
	 * 
williamr@4
    81
	 * @param aInput	The plaintext
williamr@4
    82
	 * @param aOutput	On return, the ciphertext
williamr@4
    83
	 *
williamr@4
    84
	 * @panic KCryptoPanic	If the input data is too long.
williamr@4
    85
	 *						See ECryptoPanicInputTooLarge
williamr@4
    86
	 * @panic KCryptoPanic	If the supplied output descriptor is not large enough to store the result.
williamr@4
    87
	 *						See ECryptoPanicOutputDescriptorOverflow
williamr@4
    88
	 */	 
williamr@4
    89
	virtual void EncryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
williamr@4
    90
protected:
williamr@4
    91
	/** Default constructor */	 
williamr@4
    92
	IMPORT_C CEncryptor(void);
williamr@4
    93
private:
williamr@4
    94
	CEncryptor(const CEncryptor&);
williamr@4
    95
	CEncryptor& operator=(const CEncryptor&);
williamr@4
    96
	};
williamr@4
    97
williamr@4
    98
/** 
williamr@4
    99
* Abstract base class for all public key decryptors.
williamr@4
   100
* 
williamr@4
   101
*/
williamr@4
   102
class CDecryptor : public CBase, public MCryptoSystem
williamr@4
   103
	{
williamr@4
   104
public:
williamr@4
   105
	/**
williamr@4
   106
	 * Decrypts the specified ciphertext into plaintext
williamr@4
   107
	 *
williamr@4
   108
	 * @param aInput	The ciphertext to be decrypted
williamr@4
   109
	 * @param aOutput	On return, the plaintext
williamr@4
   110
	 *
williamr@4
   111
	 * @panic KCryptoPanic		If the input data is too long.
williamr@4
   112
	 *							See ECryptoPanicInputTooLarge
williamr@4
   113
	 * @panic KCryptoPanic		If the supplied output descriptor is not large enough to store the result.
williamr@4
   114
	 *							See ECryptoPanicOutputDescriptorOverflow
williamr@4
   115
	 */	 
williamr@4
   116
	virtual void DecryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
williamr@4
   117
protected:
williamr@4
   118
	/** Default constructor */	 
williamr@4
   119
	IMPORT_C CDecryptor(void);
williamr@4
   120
private:
williamr@4
   121
	CDecryptor(const CDecryptor&);
williamr@4
   122
	CDecryptor& operator=(const CDecryptor&);
williamr@4
   123
	};
williamr@4
   124
williamr@4
   125
/**
williamr@4
   126
* Implementation of RSA encryption as described in PKCS#1 v1.5.
williamr@4
   127
* 
williamr@4
   128
*/
williamr@4
   129
class CRSAPKCS1v15Encryptor : public CEncryptor
williamr@4
   130
	{
williamr@4
   131
public:
williamr@4
   132
	/**
williamr@4
   133
	 * Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
williamr@4
   134
	 * 
williamr@4
   135
	 * @param aKey	The RSA encryption key
williamr@4
   136
	 * @return		A pointer to a new CRSAPKCS1v15Encryptor object
williamr@4
   137
	 *
williamr@4
   138
	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
williamr@4
   139
	 *								cipher strength restrictions of the crypto library.
williamr@4
   140
	 *								See TCrypto::IsAsymmetricWeakEnoughL()
williamr@4
   141
	 * @leave KErrKeySize			If the key length is too small
williamr@4
   142
	 */
williamr@4
   143
	IMPORT_C static CRSAPKCS1v15Encryptor* NewL(const CRSAPublicKey& aKey);
williamr@4
   144
williamr@4
   145
	/**
williamr@4
   146
	 * Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
williamr@4
   147
	 * 
williamr@4
   148
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   149
	 *
williamr@4
   150
	 * @param aKey	The RSA encryption key
williamr@4
   151
	 * @return		A pointer to a new CRSAPKCS1v15Encryptor object
williamr@4
   152
	 *
williamr@4
   153
	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
williamr@4
   154
	 *								cipher strength restrictions of the crypto library.
williamr@4
   155
	 *								See TCrypto::IsAsymmetricWeakEnoughL()
williamr@4
   156
	 * @leave KErrKeySize			If the key length is too small
williamr@4
   157
	 */
williamr@4
   158
	IMPORT_C static CRSAPKCS1v15Encryptor* NewLC(const CRSAPublicKey& aKey);
williamr@4
   159
	void EncryptL(const TDesC8& aInput, TDes8& aOutput) const;
williamr@4
   160
	TInt MaxInputLength(void) const;
williamr@4
   161
	TInt MaxOutputLength(void) const;
williamr@4
   162
	/** The destructor frees all resources owned by the object, prior to its destruction. */
williamr@4
   163
	virtual ~CRSAPKCS1v15Encryptor(void);
williamr@4
   164
protected:
williamr@4
   165
	/** @internalAll */
williamr@4
   166
	CRSAPKCS1v15Encryptor(const CRSAPublicKey& aKey);
williamr@4
   167
	/** @internalAll */
williamr@4
   168
	void ConstructL(void);
williamr@4
   169
protected:
williamr@4
   170
	/** The RSA public key */	 
williamr@4
   171
	const CRSAPublicKey& iPublicKey;
williamr@4
   172
	/** The PKCS#1 v1.5 encryption padding */	 
williamr@4
   173
	CPaddingPKCS1Encryption* iPadding;
williamr@4
   174
private:
williamr@4
   175
	CRSAPKCS1v15Encryptor(const CRSAPKCS1v15Encryptor&);
williamr@4
   176
	CRSAPKCS1v15Encryptor& operator=(const CRSAPKCS1v15Encryptor&);
williamr@4
   177
	};
williamr@4
   178
williamr@4
   179
/** 
williamr@4
   180
* Implementation of RSA decryption as described in PKCS#1 v1.5.
williamr@4
   181
*
williamr@4
   182
*/
williamr@4
   183
class CRSAPKCS1v15Decryptor : public CDecryptor
williamr@4
   184
	{
williamr@4
   185
public:
williamr@4
   186
	/**
williamr@4
   187
	 * Creates a new RSA decryptor object using PKCS#1 v1.5 padding.
williamr@4
   188
	 *
williamr@4
   189
	 * @param aKey	The RSA private key for decryption
williamr@4
   190
	 *
williamr@4
   191
	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
williamr@4
   192
	 *								cipher strength restrictions of the crypto library.
williamr@4
   193
	 * 								See TCrypto::IsAsymmetricWeakEnoughL()
williamr@4
   194
	 * @leave KErrKeySize			If the key length is too small
williamr@4
   195
	 */
williamr@4
   196
	IMPORT_C static CRSAPKCS1v15Decryptor* NewL(const CRSAPrivateKey& aKey);
williamr@4
   197
	
williamr@4
   198
	/**
williamr@4
   199
	 * Creates a new RSA decryptor object using PKCS#1 v1.5 padding
williamr@4
   200
	 *
williamr@4
   201
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   202
	 *
williamr@4
   203
	 * @param aKey	The RSA private key for decryption
williamr@4
   204
	 *
williamr@4
   205
	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
williamr@4
   206
	 *								cipher strength restrictions of the crypto library.
williamr@4
   207
	 * 								See TCrypto::IsAsymmetricWeakEnoughL()
williamr@4
   208
	 * @leave KErrKeySize			If the key length is too small
williamr@4
   209
	 * @leave KErrNotSupported	    If the RSA private key is not a supported TRSAPrivateKeyType
williamr@4
   210
	 */
williamr@4
   211
	IMPORT_C static CRSAPKCS1v15Decryptor* NewLC(const CRSAPrivateKey& aKey);
williamr@4
   212
	void DecryptL(const TDesC8& aInput, TDes8& aOutput) const;
williamr@4
   213
	TInt MaxInputLength(void) const;
williamr@4
   214
	TInt MaxOutputLength(void) const;
williamr@4
   215
	/** The destructor frees all resources owned by the object, prior to its destruction. */
williamr@4
   216
	virtual ~CRSAPKCS1v15Decryptor(void);
williamr@4
   217
protected:
williamr@4
   218
	/** @internalAll */
williamr@4
   219
	CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey);
williamr@4
   220
	/** @internalAll */
williamr@4
   221
	void ConstructL(void);
williamr@4
   222
protected:
williamr@4
   223
	/** The RSA private key */	 
williamr@4
   224
	const CRSAPrivateKey& iPrivateKey;
williamr@4
   225
	/** The PKCS#1 v1.5 encryption padding */	 
williamr@4
   226
	CPaddingPKCS1Encryption* iPadding;
williamr@4
   227
private:
williamr@4
   228
	CRSAPKCS1v15Decryptor(const CRSAPKCS1v15Decryptor&);
williamr@4
   229
	CRSAPKCS1v15Decryptor& operator=(const CRSAPKCS1v15Decryptor&);
williamr@4
   230
	};
williamr@4
   231
williamr@4
   232
/** 
williamr@4
   233
* Mixin class defining operations common to all public key signature systems.
williamr@4
   234
*
williamr@4
   235
*/
williamr@4
   236
class MSignatureSystem 
williamr@4
   237
	{
williamr@4
   238
public:
williamr@4
   239
	/**
williamr@4
   240
	 * Gets the maximum size of input accepted by this object.
williamr@4
   241
	 *	
williamr@4
   242
	 * @return	The maximum length allowed in bytes
williamr@4
   243
	 */	 
williamr@4
   244
	virtual TInt MaxInputLength(void) const = 0;
williamr@4
   245
protected:
williamr@4
   246
	/** Constructor */
williamr@4
   247
	IMPORT_C MSignatureSystem(void);
williamr@4
   248
private:
williamr@4
   249
	MSignatureSystem(const MSignatureSystem&);
williamr@4
   250
	MSignatureSystem& operator=(const MSignatureSystem&);
williamr@4
   251
	};
williamr@4
   252
williamr@4
   253
/** 
williamr@4
   254
* Abstract base class for all public key signers.
williamr@4
   255
*
williamr@4
   256
* The template parameter, CSignature, should be a class that encapsulates the
williamr@4
   257
* concept of a digital signature.  Derived signature classes must own their
williamr@4
   258
* respective signatures (and hence be CBase derived).  There are no other
williamr@4
   259
* restrictions on the formation of the signature classes.
williamr@4
   260
* 
williamr@4
   261
*/
williamr@4
   262
template <class CSignature> class CSigner : public CBase, public MSignatureSystem
williamr@4
   263
	{
williamr@4
   264
public:
williamr@4
   265
	/**
williamr@4
   266
	 * Digitally signs the specified input message
williamr@4
   267
	 *
williamr@4
   268
	 * @param aInput	The raw data to sign, typically a hash of the actual message
williamr@4
   269
	 * @return			A pointer to a new CSignature object
williamr@4
   270
	 *
williamr@4
   271
	 * @panic ECryptoPanicInputTooLarge	If aInput is larger than MaxInputLength(),
williamr@4
   272
	 *									which is likely to happen if the caller
williamr@4
   273
	 *									has passed in something that has not been
williamr@4
   274
	 *									hashed.
williamr@4
   275
	 */
williamr@4
   276
	virtual CSignature* SignL(const TDesC8& aInput) const = 0;
williamr@4
   277
protected:
williamr@4
   278
	/** @internalAll */
williamr@4
   279
	CSigner(void);
williamr@4
   280
private:
williamr@4
   281
	CSigner(const CSigner&);
williamr@4
   282
	CSigner& operator=(const CSigner&);
williamr@4
   283
	};
williamr@4
   284
williamr@4
   285
/** 
williamr@4
   286
* Abstract class for all public key verifiers.
williamr@4
   287
*
williamr@4
   288
* The template parameter, CSignature, should be a class that encapsulates the
williamr@4
   289
* concept of a digital signature.  Derived signature classes must own their
williamr@4
   290
* respective signatures (and hence be CBase derived).  There are no other
williamr@4
   291
* restrictions on the formation of the signature classes.
williamr@4
   292
* 
williamr@4
   293
*/
williamr@4
   294
template <class CSignature> class CVerifier : public CBase, public MSignatureSystem
williamr@4
   295
	{
williamr@4
   296
public:
williamr@4
   297
	/**
williamr@4
   298
	 * Verifies the specified digital signature
williamr@4
   299
	 *
williamr@4
   300
	 * @param aInput		The message digest that was originally signed
williamr@4
   301
	 * @param aSignature	The signature to be verified
williamr@4
   302
	 * 
williamr@4
   303
	 * @return				Whether the signature is the result of signing
williamr@4
   304
	 *						aInput with the supplied key
williamr@4
   305
	 */
williamr@4
   306
	virtual TBool VerifyL(const TDesC8& aInput, 
williamr@4
   307
		const CSignature& aSignature) const = 0;
williamr@4
   308
protected:
williamr@4
   309
	/** @internalAll */
williamr@4
   310
	CVerifier(void);
williamr@4
   311
private:
williamr@4
   312
	CVerifier(const CVerifier&);
williamr@4
   313
	CVerifier& operator=(const CVerifier&);
williamr@4
   314
	};
williamr@4
   315
williamr@4
   316
/* Template nastiness for CVerifier and CSigner in asymmetric.inl */
williamr@4
   317
williamr@4
   318
#include <asymmetric.inl>
williamr@4
   319
williamr@4
   320
/** 
williamr@4
   321
* An encapsulation of a RSA signature.
williamr@4
   322
* 
williamr@4
   323
*/
williamr@4
   324
class CRSASignature : public CBase
williamr@4
   325
	{
williamr@4
   326
public:
williamr@4
   327
	/**
williamr@4
   328
	 * Creates a new CRSASignature object from the integer value 
williamr@4
   329
	 * output of a previous RSA signing operation.
williamr@4
   330
	 * 
williamr@4
   331
	 * @param aS	The integer value output from a previous RSA signing operation
williamr@4
   332
	 * @return		A pointer to the new CRSASignature object.
williamr@4
   333
	 */
williamr@4
   334
	IMPORT_C static CRSASignature* NewL(RInteger& aS);
williamr@4
   335
	
williamr@4
   336
	/**
williamr@4
   337
	 * Creates a new CRSASignature object from the integer value 
williamr@4
   338
	 * output of a previous RSA signing operation.
williamr@4
   339
	 * 
williamr@4
   340
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   341
	 *
williamr@4
   342
	 * @param aS	The integer value output from a previous RSA signing operation
williamr@4
   343
	 * @return		A pointer to the new CRSASignature object.
williamr@4
   344
	 */
williamr@4
   345
	IMPORT_C static CRSASignature* NewLC(RInteger& aS);
williamr@4
   346
	
williamr@4
   347
	/**
williamr@4
   348
	 * Gets the integer value of the RSA signature
williamr@4
   349
	 * 
williamr@4
   350
	 * @return	The integer value of the RSA signature
williamr@4
   351
	 */
williamr@4
   352
	IMPORT_C const TInteger& S(void) const;
williamr@4
   353
	
williamr@4
   354
	/**
williamr@4
   355
	 * Whether this RSASignature is identical to a specified RSASignature
williamr@4
   356
	 *
williamr@4
   357
	 * @param aSig	The RSASignature for comparison
williamr@4
   358
	 * @return		ETrue, if the two signatures are identical; EFalse, otherwise.
williamr@4
   359
	 */
williamr@4
   360
	IMPORT_C TBool operator== (const CRSASignature& aSig) const;
williamr@4
   361
	
williamr@4
   362
	/** Destructor */
williamr@4
   363
	/** The destructor frees all resources owned by the object, prior to its destruction. */
williamr@4
   364
	IMPORT_C virtual ~CRSASignature(void);
williamr@4
   365
protected:
williamr@4
   366
	/** 
williamr@4
   367
	 * Second phase constructor
williamr@4
   368
	 *
williamr@4
   369
	 * @see CRSASignature::NewL()
williamr@4
   370
	 *
williamr@4
   371
	 * @param aS	The integer value output from a previous RSA signing operation	
williamr@4
   372
	 */
williamr@4
   373
	IMPORT_C CRSASignature(RInteger& aS);
williamr@4
   374
williamr@4
   375
	/** Default constructor */
williamr@4
   376
	IMPORT_C CRSASignature(void);
williamr@4
   377
protected:
williamr@4
   378
	/** An integer value; the output from a previous RSA signing operation. */
williamr@4
   379
	RInteger iS;
williamr@4
   380
private:
williamr@4
   381
	CRSASignature(const CRSASignature&);
williamr@4
   382
	CRSASignature& operator=(const CRSASignature);
williamr@4
   383
	};
williamr@4
   384
williamr@4
   385
/** 
williamr@4
   386
* Abstract base class for all RSA Signers.
williamr@4
   387
* 
williamr@4
   388
*/
williamr@4
   389
class CRSASigner : public CSigner<CRSASignature>
williamr@4
   390
	{
williamr@4
   391
public:
williamr@4
   392
	/**
williamr@4
   393
	 * Gets the maximum size of output that can be generated by this object.
williamr@4
   394
	 *
williamr@4
   395
	 * @return	The maximum output length in bytes
williamr@4
   396
	 */	 
williamr@4
   397
	virtual TInt MaxOutputLength(void) const = 0;
williamr@4
   398
protected:
williamr@4
   399
	/** Default constructor */
williamr@4
   400
	IMPORT_C CRSASigner(void);
williamr@4
   401
private:
williamr@4
   402
	CRSASigner(const CRSASigner&);
williamr@4
   403
	CRSASigner& operator=(const CRSASigner&);
williamr@4
   404
	};
williamr@4
   405
williamr@4
   406
/**
williamr@4
   407
* Implementation of RSA signing as described in PKCS#1 v1.5.
williamr@4
   408
* 
williamr@4
   409
* This class creates RSA signatures following the RSA PKCS#1 v1.5 standard (with
williamr@4
   410
* the one caveat noted below) and using PKCS#1 v1.5 signature padding.  The only
williamr@4
   411
* exception is that the SignL() function simply performs a 'raw' PKCS#1 v1.5 sign
williamr@4
   412
* operation on whatever it is given.  It does <b>not</b> hash or in any way
williamr@4
   413
* manipulate the input data before signing.  
williamr@4
   414
* 
williamr@4
   415
*/
williamr@4
   416
class CRSAPKCS1v15Signer : public CRSASigner
williamr@4
   417
	{
williamr@4
   418
public:
williamr@4
   419
	/**
williamr@4
   420
	 * Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
williamr@4
   421
	 *  
williamr@4
   422
	 * @param aKey	The RSA private key to be used for signing
williamr@4
   423
	 * @return		A pointer to the new CRSAPKCS1v15Signer object
williamr@4
   424
	 *
williamr@4
   425
	 * @leave KErrKeySize	If the key length is too small
williamr@4
   426
	 */
williamr@4
   427
	IMPORT_C static CRSAPKCS1v15Signer* NewL(const CRSAPrivateKey& aKey);
williamr@4
   428
williamr@4
   429
	/**
williamr@4
   430
	 * Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
williamr@4
   431
	 *  
williamr@4
   432
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   433
	 *
williamr@4
   434
	 * @param aKey	The RSA private key to be used for signing
williamr@4
   435
	 * @return		A pointer to the new CRSAPKCS1v15Signer object
williamr@4
   436
	 *
williamr@4
   437
	 * @leave KErrKeySize	If the key length is too small
williamr@4
   438
	 */
williamr@4
   439
	IMPORT_C static CRSAPKCS1v15Signer* NewLC(const CRSAPrivateKey& aKey);
williamr@4
   440
	/**
williamr@4
   441
	 * Digitally signs the specified input message
williamr@4
   442
	 *
williamr@4
   443
	 * @param aInput	The raw data to sign, typically a hash of the actual message
williamr@4
   444
	 * @return			A pointer to a new CSignature object
williamr@4
   445
	 *
williamr@4
   446
	 * @leave KErrNotSupported			If the private key is not a supported TRSAPrivateKeyType
williamr@4
   447
	 * @panic ECryptoPanicInputTooLarge	If aInput is larger than MaxInputLength(),
williamr@4
   448
	 *									which is likely to happen if the caller
williamr@4
   449
	 *									has passed in something that has not been hashed.
williamr@4
   450
	 */
williamr@4
   451
	virtual CRSASignature* SignL(const TDesC8& aInput) const;
williamr@4
   452
	virtual TInt MaxInputLength(void) const;
williamr@4
   453
	virtual TInt MaxOutputLength(void) const;
williamr@4
   454
	/** The destructor frees all resources owned by the object, prior to its destruction. 
williamr@4
   455
	 * @internalAll */
williamr@4
   456
	~CRSAPKCS1v15Signer(void);
williamr@4
   457
protected:
williamr@4
   458
	/** @internalAll */
williamr@4
   459
	CRSAPKCS1v15Signer(const CRSAPrivateKey& aKey);
williamr@4
   460
	/** @internalAll */
williamr@4
   461
	void ConstructL(void);
williamr@4
   462
protected:
williamr@4
   463
	/** The RSA private key to be used for signing */
williamr@4
   464
	const CRSAPrivateKey& iPrivateKey;
williamr@4
   465
	/** The PKCS#1 v1.5 signature padding */
williamr@4
   466
	CPaddingPKCS1Signature* iPadding;
williamr@4
   467
private:
williamr@4
   468
	CRSAPKCS1v15Signer(const CRSAPKCS1v15Signer&);
williamr@4
   469
	CRSAPKCS1v15Signer& operator=(const CRSAPKCS1v15Signer&);
williamr@4
   470
	};
williamr@4
   471
williamr@4
   472
/** 
williamr@4
   473
* Abstract base class for all RSA Verifiers.
williamr@4
   474
*
williamr@4
   475
*/
williamr@4
   476
class CRSAVerifier : public CVerifier<CRSASignature>
williamr@4
   477
	{
williamr@4
   478
public:
williamr@4
   479
	/**
williamr@4
   480
	 * Gets the maximum size of output that can be generated by this object.
williamr@4
   481
	 *
williamr@4
   482
	 * @return	The maximum output length in bytes
williamr@4
   483
	 */	 
williamr@4
   484
	virtual TInt MaxOutputLength(void) const = 0;
williamr@4
   485
williamr@4
   486
	/**
williamr@4
   487
	 * Performs a decryption operation on a signature using the public key.
williamr@4
   488
	 *
williamr@4
   489
	 * This is the inverse of the sign operation, which performs a encryption
williamr@4
   490
	 * operation on its input data using the private key.  Although this can be
williamr@4
   491
	 * used to verify signatures, CRSAVerifier::VerifyL should be used in
williamr@4
   492
	 * preference.  This method is however required by some security protocols.
williamr@4
   493
	 * 
williamr@4
   494
	 * @param aSignature	The signature to be verified
williamr@4
   495
	 * @return				A pointer to a new buffer containing the result of the
williamr@4
   496
	 *						operation. The pointer is left on the cleanup stack.
williamr@4
   497
	 */
williamr@4
   498
	virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const = 0;
williamr@4
   499
williamr@4
   500
	IMPORT_C virtual TBool VerifyL(const TDesC8& aInput, 
williamr@4
   501
		const CRSASignature& aSignature) const;
williamr@4
   502
protected:
williamr@4
   503
	/** Default constructor */
williamr@4
   504
	IMPORT_C CRSAVerifier(void);
williamr@4
   505
private:
williamr@4
   506
	CRSAVerifier(const CRSAVerifier&);
williamr@4
   507
	CRSAVerifier& operator=(const CRSAVerifier&);
williamr@4
   508
	};
williamr@4
   509
williamr@4
   510
/**
williamr@4
   511
* This class verifies RSA signatures given a message and its supposed
williamr@4
   512
* signature.  It follows the RSA PKCS#1 v1.5 with PKCS#1 v1.5 padding specification
williamr@4
   513
* with the following exception: the VerifyL() function does <b>not</b> hash or
williamr@4
   514
* in any way manipulate the input data before checking.  Thus in order to verify
williamr@4
   515
* RSA signatures in PKCS#1 v1.5 format, the input data needs to follow PKCS#1 v1.5 
williamr@4
   516
* specification, i.e. be ASN.1 encoded and prefixed  by ASN.1 encoded digestId.
williamr@4
   517
* 
williamr@4
   518
*/
williamr@4
   519
class CRSAPKCS1v15Verifier : public CRSAVerifier
williamr@4
   520
	{
williamr@4
   521
public:
williamr@4
   522
	/**
williamr@4
   523
	 * Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
williamr@4
   524
	 *
williamr@4
   525
	 * @param aKey	The RSA public key to be used for verifying
williamr@4
   526
	 * @return		A pointer to the new CRSAPKCS1v15Verifier object
williamr@4
   527
	 *
williamr@4
   528
	 * @leave KErrKeySize	If the key length is too small
williamr@4
   529
	 */
williamr@4
   530
	IMPORT_C static CRSAPKCS1v15Verifier* NewL(const CRSAPublicKey& aKey);
williamr@4
   531
williamr@4
   532
	/**
williamr@4
   533
	 * Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
williamr@4
   534
	 *  
williamr@4
   535
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   536
	 *
williamr@4
   537
	 * @param aKey	The RSA public key to be used for verifying
williamr@4
   538
	 * @return		A pointer to the new CRSAPKCS1v15Verifier object
williamr@4
   539
	 *
williamr@4
   540
	 * @leave KErrKeySize	If the key length is too small
williamr@4
   541
	 */
williamr@4
   542
	IMPORT_C static CRSAPKCS1v15Verifier* NewLC(const CRSAPublicKey& aKey);
williamr@4
   543
	virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const;
williamr@4
   544
	virtual TInt MaxInputLength(void) const;
williamr@4
   545
	virtual TInt MaxOutputLength(void) const;
williamr@4
   546
	/** The destructor frees all resources owned by the object, prior to its destruction. */
williamr@4
   547
	virtual ~CRSAPKCS1v15Verifier(void);
williamr@4
   548
protected:
williamr@4
   549
	/** @internalAll */
williamr@4
   550
	CRSAPKCS1v15Verifier(const CRSAPublicKey& aKey);
williamr@4
   551
	/** @internalAll */
williamr@4
   552
	void ConstructL(void);
williamr@4
   553
protected:
williamr@4
   554
	/** The RSA public key to be used for verification */
williamr@4
   555
	const CRSAPublicKey& iPublicKey;
williamr@4
   556
	/** The PKCS#1 v1.5 signature padding */
williamr@4
   557
	CPaddingPKCS1Signature* iPadding;
williamr@4
   558
private:
williamr@4
   559
	CRSAPKCS1v15Verifier(const CRSAPKCS1v15Verifier&);
williamr@4
   560
	CRSAPKCS1v15Verifier& operator=(const CRSAPKCS1v15Verifier&);
williamr@4
   561
	};
williamr@4
   562
	
williamr@4
   563
/** 
williamr@4
   564
* An encapsulation of a DSA signature.
williamr@4
   565
* 
williamr@4
   566
*/
williamr@4
   567
class CDSASignature : public CBase
williamr@4
   568
	{
williamr@4
   569
public:
williamr@4
   570
	/**
williamr@4
   571
	 * Creates a new CDSASignature object from the specified R and S values.
williamr@4
   572
	 *
williamr@4
   573
	 * @param aR 	The DSA signature's R value
williamr@4
   574
	 * @param aS	The DSA signature's S value
williamr@4
   575
	 * @return		A pointer to the new CDSASignature object
williamr@4
   576
	 */
williamr@4
   577
	IMPORT_C static CDSASignature* NewL(RInteger& aR, RInteger& aS);
williamr@4
   578
williamr@4
   579
	/**
williamr@4
   580
	 * Creates a new CDSASignature object from the specified R and S values.
williamr@4
   581
	 *  
williamr@4
   582
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   583
	 *
williamr@4
   584
	 * @param aR 	The DSA signature's R value
williamr@4
   585
	 * @param aS	The DSA signature's S value
williamr@4
   586
	 * @return		A pointer to the new CDSASignature object
williamr@4
   587
	 */
williamr@4
   588
	IMPORT_C static CDSASignature* NewLC(RInteger& aR, RInteger& aS);
williamr@4
   589
	
williamr@4
   590
	/**
williamr@4
   591
	 * Gets the DSA signature's R value
williamr@4
   592
	 * 
williamr@4
   593
	 * @return	The R value
williamr@4
   594
	 */
williamr@4
   595
	IMPORT_C const TInteger& R(void) const;
williamr@4
   596
	
williamr@4
   597
	/**
williamr@4
   598
	 * Gets the DSA signature's S value
williamr@4
   599
	 * 
williamr@4
   600
	 * @return	The S value
williamr@4
   601
	 */
williamr@4
   602
	IMPORT_C const TInteger& S(void) const;
williamr@4
   603
	
williamr@4
   604
	/**
williamr@4
   605
	 * Whether this DSASignature is identical to a specified DSASignature
williamr@4
   606
	 *
williamr@4
   607
	 * @param aSig	The DSASignature for comparison
williamr@4
   608
	 * @return		ETrue, if the two signatures are identical; EFalse, otherwise.
williamr@4
   609
	 */
williamr@4
   610
	IMPORT_C TBool operator== (const CDSASignature& aSig) const;
williamr@4
   611
	
williamr@4
   612
	/** The destructor frees all resources owned by the object, prior to its destruction. */
williamr@4
   613
	IMPORT_C virtual ~CDSASignature(void);
williamr@4
   614
protected:
williamr@4
   615
	/**
williamr@4
   616
	 * Protected constructor
williamr@4
   617
	 *
williamr@4
   618
	 * @param aR 	The DSA signature's R value
williamr@4
   619
	 * @param aS	The DSA signature's S value
williamr@4
   620
	 */
williamr@4
   621
	IMPORT_C CDSASignature(RInteger& aR, RInteger& aS);
williamr@4
   622
	
williamr@4
   623
	/** Default constructor */
williamr@4
   624
	IMPORT_C CDSASignature(void);
williamr@4
   625
protected:
williamr@4
   626
	/** The DSA signature's R value */
williamr@4
   627
	RInteger iR;
williamr@4
   628
	/** The DSA signature's S value */
williamr@4
   629
	RInteger iS;
williamr@4
   630
private:
williamr@4
   631
	CDSASignature(const CDSASignature&);
williamr@4
   632
	CDSASignature& operator=(const CDSASignature&);
williamr@4
   633
	};
williamr@4
   634
williamr@4
   635
/**
williamr@4
   636
* Implementation of DSA signing as specified in FIPS 186-2 change request 1.
williamr@4
   637
* 
williamr@4
   638
*/
williamr@4
   639
class CDSASigner : public CSigner<CDSASignature>
williamr@4
   640
	{
williamr@4
   641
public:
williamr@4
   642
	/**
williamr@4
   643
	 * Creates a new CDSASigner object from a specified DSA private key.
williamr@4
   644
	 *
williamr@4
   645
	 * @param aKey	The DSA private key to be used for signing
williamr@4
   646
	 * @return		A pointer to the new CDSASigner object
williamr@4
   647
	 */
williamr@4
   648
	IMPORT_C static CDSASigner* NewL(const CDSAPrivateKey& aKey);
williamr@4
   649
williamr@4
   650
	/**
williamr@4
   651
	 * Creates a new CDSASigner object from a specified DSA private key.
williamr@4
   652
	 *  
williamr@4
   653
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   654
	 *
williamr@4
   655
	 * @param aKey	The DSA private key to be used for signing
williamr@4
   656
	 * @return		A pointer to the new CDSASigner object
williamr@4
   657
	 */
williamr@4
   658
	IMPORT_C static CDSASigner* NewLC(const CDSAPrivateKey& aKey);
williamr@4
   659
	/**
williamr@4
   660
	 * Digitally signs the specified input message
williamr@4
   661
	 *
williamr@4
   662
	 * Note that in order to be interoperable and compliant with the DSS, aInput
williamr@4
   663
	 * must be the result of a SHA-1 hash.
williamr@4
   664
	 *
williamr@4
   665
	 * @param aInput	A SHA-1 hash of the message to sign
williamr@4
   666
	 * @return			A pointer to a new CSignature object
williamr@4
   667
	 *
williamr@4
   668
	 * @panic ECryptoPanicInputTooLarge	If aInput is larger than MaxInputLength(),
williamr@4
   669
	 *									which is likely to happen if the caller
williamr@4
   670
	 *									has passed in something that has not been hashed.
williamr@4
   671
	 */
williamr@4
   672
	virtual CDSASignature* SignL(const TDesC8& aInput) const;
williamr@4
   673
	virtual TInt MaxInputLength(void) const;
williamr@4
   674
protected:
williamr@4
   675
	/** @internalAll */
williamr@4
   676
	CDSASigner(const CDSAPrivateKey& aKey);
williamr@4
   677
protected:
williamr@4
   678
	/** The DSA private key to be used for signing */
williamr@4
   679
	const CDSAPrivateKey& iPrivateKey;
williamr@4
   680
private:
williamr@4
   681
	CDSASigner(const CDSASigner&);
williamr@4
   682
	CDSASigner& operator=(const CDSASigner&);
williamr@4
   683
	};
williamr@4
   684
williamr@4
   685
/**
williamr@4
   686
* Implementation of DSA signature verification as specified in FIPS 186-2 change
williamr@4
   687
* request 1.
williamr@4
   688
* 
williamr@4
   689
*/
williamr@4
   690
class CDSAVerifier : public CVerifier<CDSASignature>
williamr@4
   691
	{
williamr@4
   692
public:
williamr@4
   693
	/**
williamr@4
   694
	 * Creates a new CDSAVerifier object from a specified DSA public key.
williamr@4
   695
	 *
williamr@4
   696
	 * @param aKey	The DSA public key to be used for verifying
williamr@4
   697
	 * @return		A pointer to the new CDSAVerifier object
williamr@4
   698
	 */
williamr@4
   699
	IMPORT_C static CDSAVerifier* NewL(const CDSAPublicKey& aKey);
williamr@4
   700
williamr@4
   701
	/**
williamr@4
   702
	 * Creates a new CDSAVerifier object from a specified DSA public key.
williamr@4
   703
	 *  
williamr@4
   704
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   705
	 *
williamr@4
   706
	 * @param aKey	The DSA public key to be used for verifying
williamr@4
   707
	 * @return		A pointer to the new CDSAVerifier object
williamr@4
   708
	 */
williamr@4
   709
	IMPORT_C static CDSAVerifier* NewLC(const CDSAPublicKey& aKey);
williamr@4
   710
	/**
williamr@4
   711
	 * Verifies the specified digital signature
williamr@4
   712
	 *
williamr@4
   713
	 * Note that in order to be interoperable and compliant with the DSS, aInput
williamr@4
   714
	 * must be the result of a SHA-1 hash.
williamr@4
   715
	 *
williamr@4
   716
	 * @param aInput		A SHA-1 hash of the received message
williamr@4
   717
	 * @param aSignature	The signature to be verified
williamr@4
   718
	 * 
williamr@4
   719
	 * @return				Whether the signature is the result of signing
williamr@4
   720
	 *						aInput with the supplied key
williamr@4
   721
	 */
williamr@4
   722
	virtual TBool VerifyL(const TDesC8& aInput, const CDSASignature& aSignature) const;
williamr@4
   723
	virtual TInt MaxInputLength(void) const;
williamr@4
   724
protected:
williamr@4
   725
	/** @internalAll */
williamr@4
   726
	CDSAVerifier(const CDSAPublicKey& aKey);
williamr@4
   727
protected:
williamr@4
   728
	/** The DSA public key to be used for verification */
williamr@4
   729
	const CDSAPublicKey& iPublicKey;
williamr@4
   730
private:
williamr@4
   731
	CDSAVerifier(const CDSAVerifier&);
williamr@4
   732
	CDSAVerifier& operator=(const CDSAVerifier&);
williamr@4
   733
	};
williamr@4
   734
williamr@4
   735
/**
williamr@4
   736
* Implementation of Diffie-Hellman key agreement as specified in PKCS#3.
williamr@4
   737
* 
williamr@4
   738
*/
williamr@4
   739
class CDH : public CBase
williamr@4
   740
	{
williamr@4
   741
public:
williamr@4
   742
	/**
williamr@4
   743
	 * Creates a new CDH object from a specified DH private key.
williamr@4
   744
	 *
williamr@4
   745
	 * @param aPrivateKey	The private key of this party
williamr@4
   746
	 * @return				A pointer to the new CDH object
williamr@4
   747
	 */
williamr@4
   748
	IMPORT_C static CDH* NewL(const CDHPrivateKey& aPrivateKey);
williamr@4
   749
williamr@4
   750
	/**
williamr@4
   751
	 * Creates a new CDH object from a specified DH private key.
williamr@4
   752
	 *  
williamr@4
   753
	 * The returned pointer is put onto the cleanup stack.
williamr@4
   754
	 *
williamr@4
   755
	 * @param aPrivateKey	The private key of this party
williamr@4
   756
	 * @return				A pointer to the new CDH object
williamr@4
   757
	 */
williamr@4
   758
	IMPORT_C static CDH* NewLC(const CDHPrivateKey& aPrivateKey);
williamr@4
   759
	
williamr@4
   760
	/**
williamr@4
   761
	 * Performs the key agreement operation.
williamr@4
   762
	 *
williamr@4
   763
	 * @param aPublicKey	The public key of the other party
williamr@4
   764
	 * @return				The agreed key
williamr@4
   765
	 */
williamr@4
   766
	IMPORT_C HBufC8* AgreeL(const CDHPublicKey& aPublicKey) const;
williamr@4
   767
protected:
williamr@4
   768
	/**
williamr@4
   769
	 * Constructor
williamr@4
   770
	 *
williamr@4
   771
	 * @param aPrivateKey	The DH private key
williamr@4
   772
	 */
williamr@4
   773
	IMPORT_C CDH(const CDHPrivateKey& aPrivateKey);
williamr@4
   774
protected:
williamr@4
   775
	/** The DH private key */
williamr@4
   776
	const CDHPrivateKey& iPrivateKey;
williamr@4
   777
private:
williamr@4
   778
	CDH(const CDH&);
williamr@4
   779
	CDH& operator=(const CDH&);
williamr@4
   780
	};
williamr@4
   781
williamr@4
   782
#endif	//	__ASYMMETRIC_H__