epoc32/include/asymmetric.h
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/asymmetric.h	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,782 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* ** IMPORTANT **  API's in this file are published to 3rd party developers via the 
    1.19 +* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
    1.20 +* Asymmetric crypto implementation
    1.21 +*
    1.22 +*/
    1.23 +
    1.24 +
    1.25 +/**
    1.26 + @file 
    1.27 + @publishedAll
    1.28 + @released 
    1.29 +*/
    1.30 + 
    1.31 +#ifndef __ASYMMETRIC_H__
    1.32 +#define __ASYMMETRIC_H__
    1.33 +
    1.34 +#include <padding.h>
    1.35 +#include <asymmetrickeys.h>
    1.36 +#include <random.h>
    1.37 +#include <hash.h>
    1.38 +
    1.39 +// All the classes in this file have their default constructors and
    1.40 +// assignment operators defined private, but not implemented, in order to
    1.41 +// prevent their use.
    1.42 +
    1.43 +/** 
    1.44 +* Mixin class defining common operations for public key encryption and
    1.45 +* decryption classes.
    1.46 +* 
    1.47 +*/
    1.48 +class MCryptoSystem 
    1.49 +	{
    1.50 +public:
    1.51 +	/**
    1.52 +	 * Gets the maximum size of input accepted by this object.
    1.53 +	 *	
    1.54 +	 * @return	The maximum input length allowed in bytes.
    1.55 +	 */	 
    1.56 +	virtual TInt MaxInputLength(void) const = 0;
    1.57 +	
    1.58 +	/**
    1.59 +	 * Gets the maximum size of output that can be generated by this object.
    1.60 +	 *
    1.61 +	 * @return	The maximum output length in bytes.
    1.62 +	 */	 
    1.63 +	virtual TInt MaxOutputLength(void) const = 0;
    1.64 +protected:
    1.65 +	/**
    1.66 +	 * Constructor
    1.67 + 	 */	 
    1.68 +	IMPORT_C MCryptoSystem(void);
    1.69 +private:
    1.70 +	MCryptoSystem(const MCryptoSystem&);
    1.71 +	MCryptoSystem& operator=(const MCryptoSystem&);
    1.72 +	};
    1.73 +
    1.74 +/** 
    1.75 +* Abstract base class for all public key encryptors.
    1.76 +* 
    1.77 +*/
    1.78 +class CEncryptor : public CBase, public MCryptoSystem
    1.79 +	{
    1.80 +public:
    1.81 +	/**
    1.82 +	 * Encrypts the specified plaintext into ciphertext.
    1.83 +	 * 
    1.84 +	 * @param aInput	The plaintext
    1.85 +	 * @param aOutput	On return, the ciphertext
    1.86 +	 *
    1.87 +	 * @panic KCryptoPanic	If the input data is too long.
    1.88 +	 *						See ECryptoPanicInputTooLarge
    1.89 +	 * @panic KCryptoPanic	If the supplied output descriptor is not large enough to store the result.
    1.90 +	 *						See ECryptoPanicOutputDescriptorOverflow
    1.91 +	 */	 
    1.92 +	virtual void EncryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
    1.93 +protected:
    1.94 +	/** Default constructor */	 
    1.95 +	IMPORT_C CEncryptor(void);
    1.96 +private:
    1.97 +	CEncryptor(const CEncryptor&);
    1.98 +	CEncryptor& operator=(const CEncryptor&);
    1.99 +	};
   1.100 +
   1.101 +/** 
   1.102 +* Abstract base class for all public key decryptors.
   1.103 +* 
   1.104 +*/
   1.105 +class CDecryptor : public CBase, public MCryptoSystem
   1.106 +	{
   1.107 +public:
   1.108 +	/**
   1.109 +	 * Decrypts the specified ciphertext into plaintext
   1.110 +	 *
   1.111 +	 * @param aInput	The ciphertext to be decrypted
   1.112 +	 * @param aOutput	On return, the plaintext
   1.113 +	 *
   1.114 +	 * @panic KCryptoPanic		If the input data is too long.
   1.115 +	 *							See ECryptoPanicInputTooLarge
   1.116 +	 * @panic KCryptoPanic		If the supplied output descriptor is not large enough to store the result.
   1.117 +	 *							See ECryptoPanicOutputDescriptorOverflow
   1.118 +	 */	 
   1.119 +	virtual void DecryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
   1.120 +protected:
   1.121 +	/** Default constructor */	 
   1.122 +	IMPORT_C CDecryptor(void);
   1.123 +private:
   1.124 +	CDecryptor(const CDecryptor&);
   1.125 +	CDecryptor& operator=(const CDecryptor&);
   1.126 +	};
   1.127 +
   1.128 +/**
   1.129 +* Implementation of RSA encryption as described in PKCS#1 v1.5.
   1.130 +* 
   1.131 +*/
   1.132 +class CRSAPKCS1v15Encryptor : public CEncryptor
   1.133 +	{
   1.134 +public:
   1.135 +	/**
   1.136 +	 * Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
   1.137 +	 * 
   1.138 +	 * @param aKey	The RSA encryption key
   1.139 +	 * @return		A pointer to a new CRSAPKCS1v15Encryptor object
   1.140 +	 *
   1.141 +	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   1.142 +	 *								cipher strength restrictions of the crypto library.
   1.143 +	 *								See TCrypto::IsAsymmetricWeakEnoughL()
   1.144 +	 * @leave KErrKeySize			If the key length is too small
   1.145 +	 */
   1.146 +	IMPORT_C static CRSAPKCS1v15Encryptor* NewL(const CRSAPublicKey& aKey);
   1.147 +
   1.148 +	/**
   1.149 +	 * Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
   1.150 +	 * 
   1.151 +	 * The returned pointer is put onto the cleanup stack.
   1.152 +	 *
   1.153 +	 * @param aKey	The RSA encryption key
   1.154 +	 * @return		A pointer to a new CRSAPKCS1v15Encryptor object
   1.155 +	 *
   1.156 +	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   1.157 +	 *								cipher strength restrictions of the crypto library.
   1.158 +	 *								See TCrypto::IsAsymmetricWeakEnoughL()
   1.159 +	 * @leave KErrKeySize			If the key length is too small
   1.160 +	 */
   1.161 +	IMPORT_C static CRSAPKCS1v15Encryptor* NewLC(const CRSAPublicKey& aKey);
   1.162 +	void EncryptL(const TDesC8& aInput, TDes8& aOutput) const;
   1.163 +	TInt MaxInputLength(void) const;
   1.164 +	TInt MaxOutputLength(void) const;
   1.165 +	/** The destructor frees all resources owned by the object, prior to its destruction. */
   1.166 +	virtual ~CRSAPKCS1v15Encryptor(void);
   1.167 +protected:
   1.168 +	/** @internalAll */
   1.169 +	CRSAPKCS1v15Encryptor(const CRSAPublicKey& aKey);
   1.170 +	/** @internalAll */
   1.171 +	void ConstructL(void);
   1.172 +protected:
   1.173 +	/** The RSA public key */	 
   1.174 +	const CRSAPublicKey& iPublicKey;
   1.175 +	/** The PKCS#1 v1.5 encryption padding */	 
   1.176 +	CPaddingPKCS1Encryption* iPadding;
   1.177 +private:
   1.178 +	CRSAPKCS1v15Encryptor(const CRSAPKCS1v15Encryptor&);
   1.179 +	CRSAPKCS1v15Encryptor& operator=(const CRSAPKCS1v15Encryptor&);
   1.180 +	};
   1.181 +
   1.182 +/** 
   1.183 +* Implementation of RSA decryption as described in PKCS#1 v1.5.
   1.184 +*
   1.185 +*/
   1.186 +class CRSAPKCS1v15Decryptor : public CDecryptor
   1.187 +	{
   1.188 +public:
   1.189 +	/**
   1.190 +	 * Creates a new RSA decryptor object using PKCS#1 v1.5 padding.
   1.191 +	 *
   1.192 +	 * @param aKey	The RSA private key for decryption
   1.193 +	 *
   1.194 +	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   1.195 +	 *								cipher strength restrictions of the crypto library.
   1.196 +	 * 								See TCrypto::IsAsymmetricWeakEnoughL()
   1.197 +	 * @leave KErrKeySize			If the key length is too small
   1.198 +	 */
   1.199 +	IMPORT_C static CRSAPKCS1v15Decryptor* NewL(const CRSAPrivateKey& aKey);
   1.200 +	
   1.201 +	/**
   1.202 +	 * Creates a new RSA decryptor object using PKCS#1 v1.5 padding
   1.203 +	 *
   1.204 +	 * The returned pointer is put onto the cleanup stack.
   1.205 +	 *
   1.206 +	 * @param aKey	The RSA private key for decryption
   1.207 +	 *
   1.208 +	 * @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
   1.209 +	 *								cipher strength restrictions of the crypto library.
   1.210 +	 * 								See TCrypto::IsAsymmetricWeakEnoughL()
   1.211 +	 * @leave KErrKeySize			If the key length is too small
   1.212 +	 * @leave KErrNotSupported	    If the RSA private key is not a supported TRSAPrivateKeyType
   1.213 +	 */
   1.214 +	IMPORT_C static CRSAPKCS1v15Decryptor* NewLC(const CRSAPrivateKey& aKey);
   1.215 +	void DecryptL(const TDesC8& aInput, TDes8& aOutput) const;
   1.216 +	TInt MaxInputLength(void) const;
   1.217 +	TInt MaxOutputLength(void) const;
   1.218 +	/** The destructor frees all resources owned by the object, prior to its destruction. */
   1.219 +	virtual ~CRSAPKCS1v15Decryptor(void);
   1.220 +protected:
   1.221 +	/** @internalAll */
   1.222 +	CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey);
   1.223 +	/** @internalAll */
   1.224 +	void ConstructL(void);
   1.225 +protected:
   1.226 +	/** The RSA private key */	 
   1.227 +	const CRSAPrivateKey& iPrivateKey;
   1.228 +	/** The PKCS#1 v1.5 encryption padding */	 
   1.229 +	CPaddingPKCS1Encryption* iPadding;
   1.230 +private:
   1.231 +	CRSAPKCS1v15Decryptor(const CRSAPKCS1v15Decryptor&);
   1.232 +	CRSAPKCS1v15Decryptor& operator=(const CRSAPKCS1v15Decryptor&);
   1.233 +	};
   1.234 +
   1.235 +/** 
   1.236 +* Mixin class defining operations common to all public key signature systems.
   1.237 +*
   1.238 +*/
   1.239 +class MSignatureSystem 
   1.240 +	{
   1.241 +public:
   1.242 +	/**
   1.243 +	 * Gets the maximum size of input accepted by this object.
   1.244 +	 *	
   1.245 +	 * @return	The maximum length allowed in bytes
   1.246 +	 */	 
   1.247 +	virtual TInt MaxInputLength(void) const = 0;
   1.248 +protected:
   1.249 +	/** Constructor */
   1.250 +	IMPORT_C MSignatureSystem(void);
   1.251 +private:
   1.252 +	MSignatureSystem(const MSignatureSystem&);
   1.253 +	MSignatureSystem& operator=(const MSignatureSystem&);
   1.254 +	};
   1.255 +
   1.256 +/** 
   1.257 +* Abstract base class for all public key signers.
   1.258 +*
   1.259 +* The template parameter, CSignature, should be a class that encapsulates the
   1.260 +* concept of a digital signature.  Derived signature classes must own their
   1.261 +* respective signatures (and hence be CBase derived).  There are no other
   1.262 +* restrictions on the formation of the signature classes.
   1.263 +* 
   1.264 +*/
   1.265 +template <class CSignature> class CSigner : public CBase, public MSignatureSystem
   1.266 +	{
   1.267 +public:
   1.268 +	/**
   1.269 +	 * Digitally signs the specified input message
   1.270 +	 *
   1.271 +	 * @param aInput	The raw data to sign, typically a hash of the actual message
   1.272 +	 * @return			A pointer to a new CSignature object
   1.273 +	 *
   1.274 +	 * @panic ECryptoPanicInputTooLarge	If aInput is larger than MaxInputLength(),
   1.275 +	 *									which is likely to happen if the caller
   1.276 +	 *									has passed in something that has not been
   1.277 +	 *									hashed.
   1.278 +	 */
   1.279 +	virtual CSignature* SignL(const TDesC8& aInput) const = 0;
   1.280 +protected:
   1.281 +	/** @internalAll */
   1.282 +	CSigner(void);
   1.283 +private:
   1.284 +	CSigner(const CSigner&);
   1.285 +	CSigner& operator=(const CSigner&);
   1.286 +	};
   1.287 +
   1.288 +/** 
   1.289 +* Abstract class for all public key verifiers.
   1.290 +*
   1.291 +* The template parameter, CSignature, should be a class that encapsulates the
   1.292 +* concept of a digital signature.  Derived signature classes must own their
   1.293 +* respective signatures (and hence be CBase derived).  There are no other
   1.294 +* restrictions on the formation of the signature classes.
   1.295 +* 
   1.296 +*/
   1.297 +template <class CSignature> class CVerifier : public CBase, public MSignatureSystem
   1.298 +	{
   1.299 +public:
   1.300 +	/**
   1.301 +	 * Verifies the specified digital signature
   1.302 +	 *
   1.303 +	 * @param aInput		The message digest that was originally signed
   1.304 +	 * @param aSignature	The signature to be verified
   1.305 +	 * 
   1.306 +	 * @return				Whether the signature is the result of signing
   1.307 +	 *						aInput with the supplied key
   1.308 +	 */
   1.309 +	virtual TBool VerifyL(const TDesC8& aInput, 
   1.310 +		const CSignature& aSignature) const = 0;
   1.311 +protected:
   1.312 +	/** @internalAll */
   1.313 +	CVerifier(void);
   1.314 +private:
   1.315 +	CVerifier(const CVerifier&);
   1.316 +	CVerifier& operator=(const CVerifier&);
   1.317 +	};
   1.318 +
   1.319 +/* Template nastiness for CVerifier and CSigner in asymmetric.inl */
   1.320 +
   1.321 +#include <asymmetric.inl>
   1.322 +
   1.323 +/** 
   1.324 +* An encapsulation of a RSA signature.
   1.325 +* 
   1.326 +*/
   1.327 +class CRSASignature : public CBase
   1.328 +	{
   1.329 +public:
   1.330 +	/**
   1.331 +	 * Creates a new CRSASignature object from the integer value 
   1.332 +	 * output of a previous RSA signing operation.
   1.333 +	 * 
   1.334 +	 * @param aS	The integer value output from a previous RSA signing operation
   1.335 +	 * @return		A pointer to the new CRSASignature object.
   1.336 +	 */
   1.337 +	IMPORT_C static CRSASignature* NewL(RInteger& aS);
   1.338 +	
   1.339 +	/**
   1.340 +	 * Creates a new CRSASignature object from the integer value 
   1.341 +	 * output of a previous RSA signing operation.
   1.342 +	 * 
   1.343 +	 * The returned pointer is put onto the cleanup stack.
   1.344 +	 *
   1.345 +	 * @param aS	The integer value output from a previous RSA signing operation
   1.346 +	 * @return		A pointer to the new CRSASignature object.
   1.347 +	 */
   1.348 +	IMPORT_C static CRSASignature* NewLC(RInteger& aS);
   1.349 +	
   1.350 +	/**
   1.351 +	 * Gets the integer value of the RSA signature
   1.352 +	 * 
   1.353 +	 * @return	The integer value of the RSA signature
   1.354 +	 */
   1.355 +	IMPORT_C const TInteger& S(void) const;
   1.356 +	
   1.357 +	/**
   1.358 +	 * Whether this RSASignature is identical to a specified RSASignature
   1.359 +	 *
   1.360 +	 * @param aSig	The RSASignature for comparison
   1.361 +	 * @return		ETrue, if the two signatures are identical; EFalse, otherwise.
   1.362 +	 */
   1.363 +	IMPORT_C TBool operator== (const CRSASignature& aSig) const;
   1.364 +	
   1.365 +	/** Destructor */
   1.366 +	/** The destructor frees all resources owned by the object, prior to its destruction. */
   1.367 +	IMPORT_C virtual ~CRSASignature(void);
   1.368 +protected:
   1.369 +	/** 
   1.370 +	 * Second phase constructor
   1.371 +	 *
   1.372 +	 * @see CRSASignature::NewL()
   1.373 +	 *
   1.374 +	 * @param aS	The integer value output from a previous RSA signing operation	
   1.375 +	 */
   1.376 +	IMPORT_C CRSASignature(RInteger& aS);
   1.377 +
   1.378 +	/** Default constructor */
   1.379 +	IMPORT_C CRSASignature(void);
   1.380 +protected:
   1.381 +	/** An integer value; the output from a previous RSA signing operation. */
   1.382 +	RInteger iS;
   1.383 +private:
   1.384 +	CRSASignature(const CRSASignature&);
   1.385 +	CRSASignature& operator=(const CRSASignature);
   1.386 +	};
   1.387 +
   1.388 +/** 
   1.389 +* Abstract base class for all RSA Signers.
   1.390 +* 
   1.391 +*/
   1.392 +class CRSASigner : public CSigner<CRSASignature>
   1.393 +	{
   1.394 +public:
   1.395 +	/**
   1.396 +	 * Gets the maximum size of output that can be generated by this object.
   1.397 +	 *
   1.398 +	 * @return	The maximum output length in bytes
   1.399 +	 */	 
   1.400 +	virtual TInt MaxOutputLength(void) const = 0;
   1.401 +protected:
   1.402 +	/** Default constructor */
   1.403 +	IMPORT_C CRSASigner(void);
   1.404 +private:
   1.405 +	CRSASigner(const CRSASigner&);
   1.406 +	CRSASigner& operator=(const CRSASigner&);
   1.407 +	};
   1.408 +
   1.409 +/**
   1.410 +* Implementation of RSA signing as described in PKCS#1 v1.5.
   1.411 +* 
   1.412 +* This class creates RSA signatures following the RSA PKCS#1 v1.5 standard (with
   1.413 +* the one caveat noted below) and using PKCS#1 v1.5 signature padding.  The only
   1.414 +* exception is that the SignL() function simply performs a 'raw' PKCS#1 v1.5 sign
   1.415 +* operation on whatever it is given.  It does <b>not</b> hash or in any way
   1.416 +* manipulate the input data before signing.  
   1.417 +* 
   1.418 +*/
   1.419 +class CRSAPKCS1v15Signer : public CRSASigner
   1.420 +	{
   1.421 +public:
   1.422 +	/**
   1.423 +	 * Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
   1.424 +	 *  
   1.425 +	 * @param aKey	The RSA private key to be used for signing
   1.426 +	 * @return		A pointer to the new CRSAPKCS1v15Signer object
   1.427 +	 *
   1.428 +	 * @leave KErrKeySize	If the key length is too small
   1.429 +	 */
   1.430 +	IMPORT_C static CRSAPKCS1v15Signer* NewL(const CRSAPrivateKey& aKey);
   1.431 +
   1.432 +	/**
   1.433 +	 * Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
   1.434 +	 *  
   1.435 +	 * The returned pointer is put onto the cleanup stack.
   1.436 +	 *
   1.437 +	 * @param aKey	The RSA private key to be used for signing
   1.438 +	 * @return		A pointer to the new CRSAPKCS1v15Signer object
   1.439 +	 *
   1.440 +	 * @leave KErrKeySize	If the key length is too small
   1.441 +	 */
   1.442 +	IMPORT_C static CRSAPKCS1v15Signer* NewLC(const CRSAPrivateKey& aKey);
   1.443 +	/**
   1.444 +	 * Digitally signs the specified input message
   1.445 +	 *
   1.446 +	 * @param aInput	The raw data to sign, typically a hash of the actual message
   1.447 +	 * @return			A pointer to a new CSignature object
   1.448 +	 *
   1.449 +	 * @leave KErrNotSupported			If the private key is not a supported TRSAPrivateKeyType
   1.450 +	 * @panic ECryptoPanicInputTooLarge	If aInput is larger than MaxInputLength(),
   1.451 +	 *									which is likely to happen if the caller
   1.452 +	 *									has passed in something that has not been hashed.
   1.453 +	 */
   1.454 +	virtual CRSASignature* SignL(const TDesC8& aInput) const;
   1.455 +	virtual TInt MaxInputLength(void) const;
   1.456 +	virtual TInt MaxOutputLength(void) const;
   1.457 +	/** The destructor frees all resources owned by the object, prior to its destruction. 
   1.458 +	 * @internalAll */
   1.459 +	~CRSAPKCS1v15Signer(void);
   1.460 +protected:
   1.461 +	/** @internalAll */
   1.462 +	CRSAPKCS1v15Signer(const CRSAPrivateKey& aKey);
   1.463 +	/** @internalAll */
   1.464 +	void ConstructL(void);
   1.465 +protected:
   1.466 +	/** The RSA private key to be used for signing */
   1.467 +	const CRSAPrivateKey& iPrivateKey;
   1.468 +	/** The PKCS#1 v1.5 signature padding */
   1.469 +	CPaddingPKCS1Signature* iPadding;
   1.470 +private:
   1.471 +	CRSAPKCS1v15Signer(const CRSAPKCS1v15Signer&);
   1.472 +	CRSAPKCS1v15Signer& operator=(const CRSAPKCS1v15Signer&);
   1.473 +	};
   1.474 +
   1.475 +/** 
   1.476 +* Abstract base class for all RSA Verifiers.
   1.477 +*
   1.478 +*/
   1.479 +class CRSAVerifier : public CVerifier<CRSASignature>
   1.480 +	{
   1.481 +public:
   1.482 +	/**
   1.483 +	 * Gets the maximum size of output that can be generated by this object.
   1.484 +	 *
   1.485 +	 * @return	The maximum output length in bytes
   1.486 +	 */	 
   1.487 +	virtual TInt MaxOutputLength(void) const = 0;
   1.488 +
   1.489 +	/**
   1.490 +	 * Performs a decryption operation on a signature using the public key.
   1.491 +	 *
   1.492 +	 * This is the inverse of the sign operation, which performs a encryption
   1.493 +	 * operation on its input data using the private key.  Although this can be
   1.494 +	 * used to verify signatures, CRSAVerifier::VerifyL should be used in
   1.495 +	 * preference.  This method is however required by some security protocols.
   1.496 +	 * 
   1.497 +	 * @param aSignature	The signature to be verified
   1.498 +	 * @return				A pointer to a new buffer containing the result of the
   1.499 +	 *						operation. The pointer is left on the cleanup stack.
   1.500 +	 */
   1.501 +	virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const = 0;
   1.502 +
   1.503 +	IMPORT_C virtual TBool VerifyL(const TDesC8& aInput, 
   1.504 +		const CRSASignature& aSignature) const;
   1.505 +protected:
   1.506 +	/** Default constructor */
   1.507 +	IMPORT_C CRSAVerifier(void);
   1.508 +private:
   1.509 +	CRSAVerifier(const CRSAVerifier&);
   1.510 +	CRSAVerifier& operator=(const CRSAVerifier&);
   1.511 +	};
   1.512 +
   1.513 +/**
   1.514 +* This class verifies RSA signatures given a message and its supposed
   1.515 +* signature.  It follows the RSA PKCS#1 v1.5 with PKCS#1 v1.5 padding specification
   1.516 +* with the following exception: the VerifyL() function does <b>not</b> hash or
   1.517 +* in any way manipulate the input data before checking.  Thus in order to verify
   1.518 +* RSA signatures in PKCS#1 v1.5 format, the input data needs to follow PKCS#1 v1.5 
   1.519 +* specification, i.e. be ASN.1 encoded and prefixed  by ASN.1 encoded digestId.
   1.520 +* 
   1.521 +*/
   1.522 +class CRSAPKCS1v15Verifier : public CRSAVerifier
   1.523 +	{
   1.524 +public:
   1.525 +	/**
   1.526 +	 * Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
   1.527 +	 *
   1.528 +	 * @param aKey	The RSA public key to be used for verifying
   1.529 +	 * @return		A pointer to the new CRSAPKCS1v15Verifier object
   1.530 +	 *
   1.531 +	 * @leave KErrKeySize	If the key length is too small
   1.532 +	 */
   1.533 +	IMPORT_C static CRSAPKCS1v15Verifier* NewL(const CRSAPublicKey& aKey);
   1.534 +
   1.535 +	/**
   1.536 +	 * Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
   1.537 +	 *  
   1.538 +	 * The returned pointer is put onto the cleanup stack.
   1.539 +	 *
   1.540 +	 * @param aKey	The RSA public key to be used for verifying
   1.541 +	 * @return		A pointer to the new CRSAPKCS1v15Verifier object
   1.542 +	 *
   1.543 +	 * @leave KErrKeySize	If the key length is too small
   1.544 +	 */
   1.545 +	IMPORT_C static CRSAPKCS1v15Verifier* NewLC(const CRSAPublicKey& aKey);
   1.546 +	virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const;
   1.547 +	virtual TInt MaxInputLength(void) const;
   1.548 +	virtual TInt MaxOutputLength(void) const;
   1.549 +	/** The destructor frees all resources owned by the object, prior to its destruction. */
   1.550 +	virtual ~CRSAPKCS1v15Verifier(void);
   1.551 +protected:
   1.552 +	/** @internalAll */
   1.553 +	CRSAPKCS1v15Verifier(const CRSAPublicKey& aKey);
   1.554 +	/** @internalAll */
   1.555 +	void ConstructL(void);
   1.556 +protected:
   1.557 +	/** The RSA public key to be used for verification */
   1.558 +	const CRSAPublicKey& iPublicKey;
   1.559 +	/** The PKCS#1 v1.5 signature padding */
   1.560 +	CPaddingPKCS1Signature* iPadding;
   1.561 +private:
   1.562 +	CRSAPKCS1v15Verifier(const CRSAPKCS1v15Verifier&);
   1.563 +	CRSAPKCS1v15Verifier& operator=(const CRSAPKCS1v15Verifier&);
   1.564 +	};
   1.565 +	
   1.566 +/** 
   1.567 +* An encapsulation of a DSA signature.
   1.568 +* 
   1.569 +*/
   1.570 +class CDSASignature : public CBase
   1.571 +	{
   1.572 +public:
   1.573 +	/**
   1.574 +	 * Creates a new CDSASignature object from the specified R and S values.
   1.575 +	 *
   1.576 +	 * @param aR 	The DSA signature's R value
   1.577 +	 * @param aS	The DSA signature's S value
   1.578 +	 * @return		A pointer to the new CDSASignature object
   1.579 +	 */
   1.580 +	IMPORT_C static CDSASignature* NewL(RInteger& aR, RInteger& aS);
   1.581 +
   1.582 +	/**
   1.583 +	 * Creates a new CDSASignature object from the specified R and S values.
   1.584 +	 *  
   1.585 +	 * The returned pointer is put onto the cleanup stack.
   1.586 +	 *
   1.587 +	 * @param aR 	The DSA signature's R value
   1.588 +	 * @param aS	The DSA signature's S value
   1.589 +	 * @return		A pointer to the new CDSASignature object
   1.590 +	 */
   1.591 +	IMPORT_C static CDSASignature* NewLC(RInteger& aR, RInteger& aS);
   1.592 +	
   1.593 +	/**
   1.594 +	 * Gets the DSA signature's R value
   1.595 +	 * 
   1.596 +	 * @return	The R value
   1.597 +	 */
   1.598 +	IMPORT_C const TInteger& R(void) const;
   1.599 +	
   1.600 +	/**
   1.601 +	 * Gets the DSA signature's S value
   1.602 +	 * 
   1.603 +	 * @return	The S value
   1.604 +	 */
   1.605 +	IMPORT_C const TInteger& S(void) const;
   1.606 +	
   1.607 +	/**
   1.608 +	 * Whether this DSASignature is identical to a specified DSASignature
   1.609 +	 *
   1.610 +	 * @param aSig	The DSASignature for comparison
   1.611 +	 * @return		ETrue, if the two signatures are identical; EFalse, otherwise.
   1.612 +	 */
   1.613 +	IMPORT_C TBool operator== (const CDSASignature& aSig) const;
   1.614 +	
   1.615 +	/** The destructor frees all resources owned by the object, prior to its destruction. */
   1.616 +	IMPORT_C virtual ~CDSASignature(void);
   1.617 +protected:
   1.618 +	/**
   1.619 +	 * Protected constructor
   1.620 +	 *
   1.621 +	 * @param aR 	The DSA signature's R value
   1.622 +	 * @param aS	The DSA signature's S value
   1.623 +	 */
   1.624 +	IMPORT_C CDSASignature(RInteger& aR, RInteger& aS);
   1.625 +	
   1.626 +	/** Default constructor */
   1.627 +	IMPORT_C CDSASignature(void);
   1.628 +protected:
   1.629 +	/** The DSA signature's R value */
   1.630 +	RInteger iR;
   1.631 +	/** The DSA signature's S value */
   1.632 +	RInteger iS;
   1.633 +private:
   1.634 +	CDSASignature(const CDSASignature&);
   1.635 +	CDSASignature& operator=(const CDSASignature&);
   1.636 +	};
   1.637 +
   1.638 +/**
   1.639 +* Implementation of DSA signing as specified in FIPS 186-2 change request 1.
   1.640 +* 
   1.641 +*/
   1.642 +class CDSASigner : public CSigner<CDSASignature>
   1.643 +	{
   1.644 +public:
   1.645 +	/**
   1.646 +	 * Creates a new CDSASigner object from a specified DSA private key.
   1.647 +	 *
   1.648 +	 * @param aKey	The DSA private key to be used for signing
   1.649 +	 * @return		A pointer to the new CDSASigner object
   1.650 +	 */
   1.651 +	IMPORT_C static CDSASigner* NewL(const CDSAPrivateKey& aKey);
   1.652 +
   1.653 +	/**
   1.654 +	 * Creates a new CDSASigner object from a specified DSA private key.
   1.655 +	 *  
   1.656 +	 * The returned pointer is put onto the cleanup stack.
   1.657 +	 *
   1.658 +	 * @param aKey	The DSA private key to be used for signing
   1.659 +	 * @return		A pointer to the new CDSASigner object
   1.660 +	 */
   1.661 +	IMPORT_C static CDSASigner* NewLC(const CDSAPrivateKey& aKey);
   1.662 +	/**
   1.663 +	 * Digitally signs the specified input message
   1.664 +	 *
   1.665 +	 * Note that in order to be interoperable and compliant with the DSS, aInput
   1.666 +	 * must be the result of a SHA-1 hash.
   1.667 +	 *
   1.668 +	 * @param aInput	A SHA-1 hash of the message to sign
   1.669 +	 * @return			A pointer to a new CSignature object
   1.670 +	 *
   1.671 +	 * @panic ECryptoPanicInputTooLarge	If aInput is larger than MaxInputLength(),
   1.672 +	 *									which is likely to happen if the caller
   1.673 +	 *									has passed in something that has not been hashed.
   1.674 +	 */
   1.675 +	virtual CDSASignature* SignL(const TDesC8& aInput) const;
   1.676 +	virtual TInt MaxInputLength(void) const;
   1.677 +protected:
   1.678 +	/** @internalAll */
   1.679 +	CDSASigner(const CDSAPrivateKey& aKey);
   1.680 +protected:
   1.681 +	/** The DSA private key to be used for signing */
   1.682 +	const CDSAPrivateKey& iPrivateKey;
   1.683 +private:
   1.684 +	CDSASigner(const CDSASigner&);
   1.685 +	CDSASigner& operator=(const CDSASigner&);
   1.686 +	};
   1.687 +
   1.688 +/**
   1.689 +* Implementation of DSA signature verification as specified in FIPS 186-2 change
   1.690 +* request 1.
   1.691 +* 
   1.692 +*/
   1.693 +class CDSAVerifier : public CVerifier<CDSASignature>
   1.694 +	{
   1.695 +public:
   1.696 +	/**
   1.697 +	 * Creates a new CDSAVerifier object from a specified DSA public key.
   1.698 +	 *
   1.699 +	 * @param aKey	The DSA public key to be used for verifying
   1.700 +	 * @return		A pointer to the new CDSAVerifier object
   1.701 +	 */
   1.702 +	IMPORT_C static CDSAVerifier* NewL(const CDSAPublicKey& aKey);
   1.703 +
   1.704 +	/**
   1.705 +	 * Creates a new CDSAVerifier object from a specified DSA public key.
   1.706 +	 *  
   1.707 +	 * The returned pointer is put onto the cleanup stack.
   1.708 +	 *
   1.709 +	 * @param aKey	The DSA public key to be used for verifying
   1.710 +	 * @return		A pointer to the new CDSAVerifier object
   1.711 +	 */
   1.712 +	IMPORT_C static CDSAVerifier* NewLC(const CDSAPublicKey& aKey);
   1.713 +	/**
   1.714 +	 * Verifies the specified digital signature
   1.715 +	 *
   1.716 +	 * Note that in order to be interoperable and compliant with the DSS, aInput
   1.717 +	 * must be the result of a SHA-1 hash.
   1.718 +	 *
   1.719 +	 * @param aInput		A SHA-1 hash of the received message
   1.720 +	 * @param aSignature	The signature to be verified
   1.721 +	 * 
   1.722 +	 * @return				Whether the signature is the result of signing
   1.723 +	 *						aInput with the supplied key
   1.724 +	 */
   1.725 +	virtual TBool VerifyL(const TDesC8& aInput, const CDSASignature& aSignature) const;
   1.726 +	virtual TInt MaxInputLength(void) const;
   1.727 +protected:
   1.728 +	/** @internalAll */
   1.729 +	CDSAVerifier(const CDSAPublicKey& aKey);
   1.730 +protected:
   1.731 +	/** The DSA public key to be used for verification */
   1.732 +	const CDSAPublicKey& iPublicKey;
   1.733 +private:
   1.734 +	CDSAVerifier(const CDSAVerifier&);
   1.735 +	CDSAVerifier& operator=(const CDSAVerifier&);
   1.736 +	};
   1.737 +
   1.738 +/**
   1.739 +* Implementation of Diffie-Hellman key agreement as specified in PKCS#3.
   1.740 +* 
   1.741 +*/
   1.742 +class CDH : public CBase
   1.743 +	{
   1.744 +public:
   1.745 +	/**
   1.746 +	 * Creates a new CDH object from a specified DH private key.
   1.747 +	 *
   1.748 +	 * @param aPrivateKey	The private key of this party
   1.749 +	 * @return				A pointer to the new CDH object
   1.750 +	 */
   1.751 +	IMPORT_C static CDH* NewL(const CDHPrivateKey& aPrivateKey);
   1.752 +
   1.753 +	/**
   1.754 +	 * Creates a new CDH object from a specified DH private key.
   1.755 +	 *  
   1.756 +	 * The returned pointer is put onto the cleanup stack.
   1.757 +	 *
   1.758 +	 * @param aPrivateKey	The private key of this party
   1.759 +	 * @return				A pointer to the new CDH object
   1.760 +	 */
   1.761 +	IMPORT_C static CDH* NewLC(const CDHPrivateKey& aPrivateKey);
   1.762 +	
   1.763 +	/**
   1.764 +	 * Performs the key agreement operation.
   1.765 +	 *
   1.766 +	 * @param aPublicKey	The public key of the other party
   1.767 +	 * @return				The agreed key
   1.768 +	 */
   1.769 +	IMPORT_C HBufC8* AgreeL(const CDHPublicKey& aPublicKey) const;
   1.770 +protected:
   1.771 +	/**
   1.772 +	 * Constructor
   1.773 +	 *
   1.774 +	 * @param aPrivateKey	The DH private key
   1.775 +	 */
   1.776 +	IMPORT_C CDH(const CDHPrivateKey& aPrivateKey);
   1.777 +protected:
   1.778 +	/** The DH private key */
   1.779 +	const CDHPrivateKey& iPrivateKey;
   1.780 +private:
   1.781 +	CDH(const CDH&);
   1.782 +	CDH& operator=(const CDH&);
   1.783 +	};
   1.784 +
   1.785 +#endif	//	__ASYMMETRIC_H__