os/security/crypto/weakcryptospi/inc/asymmetrickeys.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003-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 ** 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 * Asymmetric keys implementation
    18 *
    19 */
    20 
    21 
    22 /**
    23  @file 
    24  @publishedAll
    25  @released 
    26 */
    27  
    28 #ifndef __ASYMMETRICKEYS_H__
    29 #define __ASYMMETRICKEYS_H__
    30 
    31 #include <e32base.h>
    32 #include <random.h>
    33 #include <bigint.h>
    34 
    35 /** 
    36 * Defines the various ways of representing supported RSA private keys.
    37 * 
    38 */
    39 enum TRSAPrivateKeyType 
    40 	{
    41 	/** 
    42 	 * Standard type of RSA private key
    43 	 * 
    44 	 * This consists of the modulus (n) and decryption exponent (d).
    45 	 */
    46 	EStandard,
    47 	/** 
    48 	 * CRT (Chinese Remainder Theorem) type of RSA private key
    49 	 *
    50 	 * This consists of the the first factor (p), the second factor (q), 
    51 	 * the first factor's CRT exponent (dP), the second factor's CRT exponent (dQ),
    52 	 * and the (first) CRT coefficient (qInv). The two factors, p and q, are the
    53 	 * first two prime factors of the RSA modulus, n.
    54 	 */
    55 	EStandardCRT
    56 	//We may support types like this in the future (currently these are a patent
    57 	//minefield):
    58 	//EMulti, //multi prime version of EStandard
    59 	//EMultiCRT //multi prime version of EStandardCRT
    60 	};
    61 
    62 /** 
    63 * Concrete class representing the parameters common to both an RSA public and
    64 * private key.
    65 * 
    66 * See ANSI X9.31 and RSA PKCS#1
    67 *
    68 */
    69 class CRSAParameters : public CBase
    70 	{
    71 public:
    72 	/** 
    73 	 * Gets the RSA parameter, n (the modulus)
    74 	 *
    75 	 * @return	The RSA parameter, n
    76 	 */
    77 	IMPORT_C const TInteger& N(void) const;
    78 	
    79 	/** Destructor */
    80 	IMPORT_C virtual ~CRSAParameters(void);
    81 protected:
    82 	/** 
    83 	 * Constructor 
    84 	 *
    85 	 * @param aN	The RSA parameter, n (the modulus)
    86 	 */
    87 	IMPORT_C CRSAParameters(RInteger& aN);
    88 	
    89 	/** Default constructor */
    90 	IMPORT_C CRSAParameters(void);
    91 protected:
    92 	/** The RSA modulus, n, a positive integer */
    93 	RInteger iN;
    94 private:
    95 	CRSAParameters(const CRSAParameters&);
    96 	CRSAParameters& operator=(const CRSAParameters&);
    97 	};
    98 
    99 /** 
   100 * Representation of an RSA public key.  
   101 * 
   102 * An RSA public key is identified by its modulus (n) and its encryption exponent
   103 * (e).
   104 * 
   105 */
   106 class CRSAPublicKey : public CRSAParameters
   107 	{
   108 public:
   109 	/**
   110 	 * Creates a new CRSAPublicKey object from a specified 
   111 	 * modulus and encryption exponent.
   112 	 * 
   113 	 * @param aN	The RSA parameter, n (the modulus)
   114 	 * @param aE	The RSA parameter, e (the encryption exponent)
   115 	 * @return		A pointer to a new CRSAPublicKey object
   116 	 *
   117 	 * @leave KErrArgument	If either aN or aE are not positive integers,
   118 	 *						and releases ownership. 
   119 	 */
   120 	IMPORT_C static CRSAPublicKey* NewL(RInteger& aN, RInteger& aE);
   121 
   122 	/**
   123 	 * Creates a new CRSAPublicKey object from a specified 
   124 	 * modulus and encryption exponent.
   125 	 * 
   126 	 * The returned pointer is put onto the cleanup stack.
   127 	 * 
   128 	 * @param aN	The RSA parameter, n (the modulus)
   129 	 * @param aE	The RSA parameter, e (the encryption exponent)
   130 	 * @return		A pointer to a new CRSAPublicKey object
   131 	 * 
   132 	 * @leave KErrArgument	If either aN or aE are not positive integers,
   133 	 *	 					and releases ownership. 
   134 	 */
   135 	IMPORT_C static CRSAPublicKey* NewLC(RInteger& aN, RInteger& aE);
   136 
   137 	/** 
   138 	 * Gets the RSA parameter, e (the encryption exponent)
   139 	 *
   140 	 * @return	The RSA parameter, e
   141 	 */
   142 	IMPORT_C const TInteger& E(void) const;
   143 	
   144 	/** Destructor */
   145 	IMPORT_C virtual ~CRSAPublicKey(void);
   146 protected:
   147 	/**
   148 	 * Constructor 
   149 	 *
   150 	 * @param aN	The RSA parameter, n (the modulus)
   151 	 * @param aE	The RSA parameter, e (the encryption exponent)
   152 	 */	
   153 	IMPORT_C CRSAPublicKey(RInteger& aN, RInteger& aE);
   154 	
   155 	/** Default constructor */
   156 	IMPORT_C CRSAPublicKey(void);
   157 protected:
   158 	/** The RSA encryption exponent, e */
   159 	RInteger iE;
   160 private:
   161 	CRSAPublicKey(const CRSAPublicKey&);
   162 	CRSAPublicKey& operator=(const CRSAPublicKey&);
   163 	void ConstructL();
   164 	};
   165 
   166 /** 
   167 * Non-exported container class for the various ways of representing an RSA
   168 * private key.
   169 *
   170 * To instantiate a representation of an RSA private key, find a
   171 * subclass of this appropriate to your key type.  
   172 *
   173 */
   174 class CRSAPrivateKey : public CRSAParameters
   175 	{
   176 public:
   177 	/**
   178 	 * Constructor
   179 	 * 
   180 	 * @param aKeyType	The type of the RSA private key
   181 	 * @param aN		The RSA parameter, n (the modulus)
   182 	 * @internalAll 
   183 	 */
   184 	CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN);
   185 public:
   186 	/**
   187 	 * Gets the type of RSA private key
   188 	 *
   189 	 * @return	The RSA private key type
   190 	 */
   191 	inline const TRSAPrivateKeyType PrivateKeyType() const {return (iKeyType);};
   192 protected:
   193 	/** The type of the RSA private key */
   194 	const TRSAPrivateKeyType iKeyType;
   195 private:
   196 	CRSAPrivateKey(const CRSAPrivateKey&);
   197 	CRSAPrivateKey& operator=(const CRSAPrivateKey&);
   198 	};
   199 
   200 /** 
   201 * The 'classical' representation of a RSA private key.
   202 * 
   203 * Such a private key is composed of a modulus (n) and a decryption exponent (d).
   204 *   
   205 */
   206 class CRSAPrivateKeyStandard : public CRSAPrivateKey
   207 	{
   208 public:
   209 	/**
   210 	 * Creates a new CRSAPrivateKeyStandard object from a specified 
   211 	 * modulus and decryption exponent.
   212 	 * 
   213 	 * @param aN	The RSA parameter, n (the modulus)
   214 	 * @param aD	The RSA parameter, d (the decryption exponent)
   215 	 * @return		A pointer to a new CRSAPrivateKeyStandard object
   216 	 * 
   217 	 * @leave KErrArgument	If either aN or aD are not positive integers,
   218 	 *	 					and releases ownership. 
   219 	 */
   220 	IMPORT_C static CRSAPrivateKeyStandard* NewL(RInteger& aN, RInteger& aD);
   221 
   222 	/**
   223 	 * Creates a new CRSAPrivateKeyStandard object from a specified 
   224 	 * modulus and decryption exponent.
   225 	 * 
   226 	 * The returned pointer is put onto the cleanup stack.
   227 	 * 
   228 	 * @param aN	The RSA parameter, n (the modulus)
   229 	 * @param aD	The RSA parameter, d (the decryption exponent)
   230 	 * @return		A pointer to a new CRSAPrivateKeyStandard object
   231 	 * 
   232 	 * @leave KErrArgument	If either aN or aD are not positive integers,
   233 	 *	 					and releases ownership. 
   234 	 */
   235 	IMPORT_C static CRSAPrivateKeyStandard* NewLC(RInteger& aN, RInteger& aD);
   236 
   237 	/** 
   238 	 * Gets the RSA parameter, d (the decryption exponent)
   239 	 *
   240 	 * @return	The RSA parameter, d
   241 	 */
   242 	IMPORT_C const TInteger& D(void) const;
   243 
   244 	/** Destructor */
   245 	IMPORT_C virtual ~CRSAPrivateKeyStandard(void);
   246 protected:
   247 	/** 
   248 	 * Constructor
   249 	 * 
   250 	 * @param aN	The RSA parameter, n (the modulus)
   251 	 * @param aD	The RSA parameter, d (the decryption exponent)
   252 	 */	 
   253 	IMPORT_C CRSAPrivateKeyStandard(RInteger& aN, RInteger& aD);
   254 protected:
   255 	/** The RSA decryption exponent, d */
   256 	RInteger iD;
   257 private:
   258 	CRSAPrivateKeyStandard(const CRSAPrivateKeyStandard&);
   259 	CRSAPrivateKeyStandard& operator=(const CRSAPrivateKeyStandard&);
   260 	void ConstructL();
   261 	};
   262 
   263 /** 
   264 * An alternate representation of an RSA private key providing significant
   265 * speed enhancements through its use of the Chinese Remainder Theorem (CRT).
   266 *
   267 * Here, a private key is represented by a modulus (n), the two prime factors of
   268 * the modulus (p, q), p's CRT exponent (dP), q's CRT exponent (dQ), and the CRT
   269 * coefficient (qInv).  See PKCS#1 at http://www.rsasecurity.com/rsalabs/pkcs/
   270 * for more information.
   271 *
   272 */
   273 class CRSAPrivateKeyCRT : public CRSAPrivateKey
   274 	{
   275 public:
   276 	/**
   277 	 * Creates a new CRSAPrivateKeyCRT object from a specified 
   278 	 * modulus and decryption exponent.
   279 	 * 
   280 	 * @param iN	The RSA parameter, n (the modulus)
   281 	 * @param aP	The RSA parameter, p (the first factor)
   282 	 * @param aQ	The RSA parameter, q (the second factor)
   283 	 * @param aDP	The RSA parameter, dP (the first factor's CRT exponent)
   284 	 * @param aDQ	The RSA parameter, dQ (the second factor's CRT exponent)
   285 	 * @param aQInv	The RSA parameter, qInv (the CRT coefficient)
   286 	 * @return		A pointer to a new CRSAPrivateKeyCRT object
   287 	 * 
   288 	 * @leave KErrArgument	If any of the parameters are not positive integers,
   289 	 *	 					and releases ownership. 
   290 	 */
   291 	IMPORT_C static CRSAPrivateKeyCRT* NewL(RInteger& iN, RInteger& aP, 
   292 		RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv);
   293 
   294 	/**
   295 	 * Creates a new CRSAPrivateKeyCRT object from a specified 
   296 	 * modulus and decryption exponent.
   297 	 * 
   298 	 * The returned pointer is put onto the cleanup stack.
   299 	 * 
   300 	 * @param iN	The RSA parameter, n (the modulus)
   301 	 * @param aP	The RSA parameter, p (the first factor)
   302 	 * @param aQ	The RSA parameter, q (the second factor)
   303 	 * @param aDP	The RSA parameter, dP (the first factor's CRT exponent)
   304 	 * @param aDQ	The RSA parameter, dQ (the second factor's CRT exponent)
   305 	 * @param aQInv	The RSA parameter, qInv (the CRT coefficient)
   306 	 * @return		A pointer to a new CRSAPrivateKeyCRT object
   307 	 * 
   308 	 * @leave KErrArgument	If any of the parameters are not positive integers,
   309 	 *	 					and releases ownership. 
   310 	 */
   311 	IMPORT_C static CRSAPrivateKeyCRT* NewLC(RInteger& iN, RInteger& aP, 
   312 		RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv);
   313 
   314 	/** Destructor */
   315 	IMPORT_C virtual ~CRSAPrivateKeyCRT(void);
   316 	
   317 	/**
   318 	 * Gets the RSA parameter, p (the first factor) 
   319 	 *
   320 	 * @return	The first factor
   321 	 */
   322 	IMPORT_C const TInteger& P(void) const;
   323 	
   324 	/**
   325 	 * Gets the RSA parameter, q (the second factor) 
   326 	 *
   327 	 * @return	The second factor
   328 	 */
   329 	IMPORT_C const TInteger& Q(void) const;
   330 	
   331 	/**
   332 	 * Gets the RSA parameter, dP (the first factor's CRT exponent) 
   333 	 *
   334 	 * @return	The first factor's CRT exponent
   335 	 */
   336 	IMPORT_C const TInteger& DP(void) const;
   337 	
   338 	/**
   339 	 * Gets the RSA parameter, dQ (the second factor's CRT exponent) 
   340 	 *
   341 	 * @return	The second factor's CRT exponent
   342 	 */
   343 	IMPORT_C const TInteger& DQ(void) const;
   344 	
   345 	/**
   346 	 * Gets the RSA parameter, qInv (the CRT coefficient) 
   347 	 *
   348 	 * @return	The CRT coefficient
   349 	 */
   350 	IMPORT_C const TInteger& QInv(void) const;
   351 protected:
   352 	/**
   353 	 * Constructor
   354 	 * 
   355 	 * @param aN	The RSA parameter, n (the modulus)
   356 	 * @param aP	The RSA parameter, p (the first factor)
   357 	 * @param aQ	The RSA parameter, q (the second factor)
   358 	 * @param aDP	The RSA parameter, dP (the first factor's CRT exponent)
   359 	 * @param aDQ	The RSA parameter, dQ (the second factor's CRT exponent)
   360 	 * @param aQInv	The RSA parameter, qInv (the CRT coefficient)
   361 	 */
   362 	IMPORT_C CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP, RInteger& aQ, 
   363 		RInteger& aDP, RInteger& aDQ, RInteger& aQInv);
   364 protected:
   365 	/** The RSA parameter, p, which is the first factor */
   366 	RInteger iP;
   367 	/** The RSA parameter, q, which is the second factor */
   368 	RInteger iQ;
   369 	/** The RSA parameter, dP, which is the first factor's CRT exponent */
   370 	RInteger iDP;
   371 	/** The RSA parameter, dQ, which is the second factor's CRT exponent */
   372 	RInteger iDQ;
   373 	/** The RSA parameter, qInv, which is the CRT coefficient */
   374 	RInteger iQInv;
   375 private:
   376 	CRSAPrivateKeyCRT(const CRSAPrivateKeyCRT&);
   377 	CRSAPrivateKeyCRT& operator=(const CRSAPrivateKeyCRT&);
   378 	void ConstructL();
   379 	};
   380 
   381 /** 
   382 * This class is capable of generating an RSA public/private key pair.
   383 *
   384 * By default, it generates 2 prime (standard) CRT private keys.
   385 *
   386 */
   387 class CRSAKeyPair : public CBase
   388 	{
   389 public:
   390 	/**
   391 	 * Creates a new RSA key pair
   392 	 * 
   393 	 * @param aModulusBits	The length of the modulus, n (in bits)
   394 	 * @param aKeyType		The type of the RSA key
   395 	 * @return				A pointer to a new CRSAKeyPair object
   396 	 * 
   397 	 * @leave KErrNotSupported	If the type of RSA key is not supported
   398 	 */
   399 	IMPORT_C static CRSAKeyPair* NewL(TUint aModulusBits, 
   400 		TRSAPrivateKeyType aKeyType = EStandardCRT);
   401 
   402 	/**
   403 	 * Creates a new RSA key pair
   404 	 * 
   405 	 * The returned pointer is put onto the cleanup stack.
   406 	 * 
   407 	 * @param aModulusBits	The length of the modulus, n (in bits)
   408 	 * @param aKeyType		The type of the RSA key
   409 	 * @return				A pointer to a new CRSAKeyPair object
   410 	 * 
   411 	 * @leave KErrNotSupported	If the type of RSA key is not supported
   412 	 */
   413 	IMPORT_C static CRSAKeyPair* NewLC(TUint aModulusBits, 
   414 		TRSAPrivateKeyType aKeyType = EStandardCRT);
   415 	
   416 	/** 
   417 	 * Gets the RSA public key
   418 	 *
   419 	 * @return	A CRSAPublicKey object
   420 	 */
   421 	IMPORT_C const CRSAPublicKey& PublicKey(void) const;
   422 	
   423 	/** 
   424 	 * Gets the RSA private key
   425 	 *
   426 	 * @return	A CRSAPrivateKey object
   427 	 */
   428 	IMPORT_C const CRSAPrivateKey& PrivateKey(void) const;
   429 	
   430 	/** The destructor frees all resources owned by the object, prior to its destruction. */
   431 	IMPORT_C virtual ~CRSAKeyPair(void);
   432 protected:
   433 	/** Default destructor */
   434 	IMPORT_C CRSAKeyPair(void);
   435 protected:
   436 	/** The RSA public key */
   437 	CRSAPublicKey* iPublic;
   438 	/** The RSA private key */
   439 	CRSAPrivateKey* iPrivate;
   440 private:
   441 	void ConstructL(TUint aModulusBits, TRSAPrivateKeyType aKeyType, 
   442 		TUint aPublicExponent);
   443 	CRSAKeyPair(const CRSAKeyPair&);
   444 	CRSAKeyPair& operator=(const CRSAKeyPair&);
   445 	};
   446 
   447 /** 
   448 * Representation of the parameters used to generate the primes in a
   449 * CDSAParameters object.
   450 * 
   451 * Given such a certificate, one can ensure that the DSA
   452 * primes contained in CDSAParameters were generated correctly.
   453 * 
   454 * @see CDSAParameters::ValidatePrimesL() 
   455 * 
   456 */
   457 class CDSAPrimeCertificate : public CBase
   458 	{
   459 public:
   460 	/** 
   461 	 * Creates a new DSA prime certificate from a specified 
   462 	 * seed and counter value.
   463 	 * 
   464 	 * @param aSeed		The seed from a DSA key generation process
   465 	 * @param aCounter	The counter value from a DSA key generation process
   466 	 * @return			A pointer to a new CDSAPrimeCertificate object
   467 	 */
   468 	IMPORT_C static CDSAPrimeCertificate* NewL(const TDesC8& aSeed, 
   469 		TUint aCounter);
   470 
   471 	/** 
   472 	 * Creates a new DSA prime certificate from a specified 
   473 	 * seed and counter value.
   474 	 *
   475 	 * The returned pointer is put onto the cleanup stack.
   476 	 * 
   477 	 * @param aSeed		The seed from a DSA key generation process
   478 	 * @param aCounter	The counter value from a DSA key generation process
   479 	 * @return			A pointer to a new CDSAPrimeCertificate object
   480 	 */
   481 	IMPORT_C static CDSAPrimeCertificate* NewLC(const TDesC8& aSeed,
   482 		TUint aCounter);
   483 
   484 	/**
   485 	 * Gets the seed of the DSA prime certificate
   486 	 *
   487 	 * @return	The seed
   488 	 */ 
   489 	IMPORT_C const TDesC8& Seed(void) const;
   490 	
   491 	/**
   492 	 * Gets the counter value of the DSA prime certificate
   493 	 *
   494 	 * @return	The counter's value
   495 	 */
   496 	IMPORT_C TUint Counter(void) const;
   497 	
   498 	/** Destructor */
   499 	IMPORT_C virtual ~CDSAPrimeCertificate(void);
   500 protected:
   501 	/** 
   502 	 * Constructor 
   503 	 *
   504 	 * @param aCounter	The DSA key generation counter
   505 	 */
   506 	IMPORT_C CDSAPrimeCertificate(TUint aCounter);
   507 
   508 	/** Default constructor */
   509 	IMPORT_C CDSAPrimeCertificate(void);
   510 	/** @internalAll */
   511 	void ConstructL(const TDesC8& aSeed);
   512 protected:
   513 	/** The DSA key generation seed */
   514 	const HBufC8* iSeed;
   515 	/** The DSA key generation counter */
   516 	TUint iCounter;
   517 private:
   518 	CDSAPrimeCertificate(const CDSAPrimeCertificate&);
   519 	CDSAPrimeCertificate& operator=(const CDSAPrimeCertificate&);
   520 	};
   521 
   522 /** 
   523 * Concrete class representing the parameters common to both a DSA public and
   524 * private key. 
   525 *
   526 * See FIPS 186-2, Digital Signature Standard
   527 * 
   528 */
   529 class CDSAParameters : public CBase
   530 	{
   531 public:
   532 	/**
   533 	 * Gets the DSA parameter, p (the prime)
   534 	 * 
   535 	 * @return	The DSA parameter, p
   536 	 */
   537 	IMPORT_C const TInteger& P(void) const;
   538 
   539 	/**
   540 	 * Gets the DSA parameter, q (the subprime)
   541 	 * 
   542 	 * @return	The DSA parameter, q
   543 	 */
   544 	IMPORT_C const TInteger& Q(void) const;
   545 
   546 	/**
   547 	 * Gets the DSA parameter, g (the base)
   548 	 * 
   549 	 * @return	The DSA parameter, g
   550 	 */
   551 	IMPORT_C const TInteger& G(void) const;
   552 
   553 	/**
   554 	 * Validates the primes regenerated from a DSA prime certificate 
   555 	 *
   556 	 * @param aCert	The DSA prime certificate that contains the seed and 
   557 	 *				counter value from a DSA key generation process
   558 	 * @return		Whether or not the primes are valid	
   559 	 */
   560 	IMPORT_C TBool ValidatePrimesL(const CDSAPrimeCertificate& aCert) const;
   561 
   562 	/** 
   563 	 * Whether or not the prime is of a valid length 
   564 	 * 
   565 	 * It is valid if the length of the prime modulus is between KMinPrimeLength
   566 	 * and KMaxPrimeLength bits, and the prime is a multiple of KPrimeLengthMultiple. 
   567 	 *
   568 	 * @param aPrimeBits	The prime modulus
   569 	 * @return				ETrue, if within the constraints; EFalse, otherwise.
   570 	 */
   571 	IMPORT_C static TBool ValidPrimeLength(TUint aPrimeBits);
   572 	
   573 	/** Destructor */
   574 	IMPORT_C virtual ~CDSAParameters(void);
   575 
   576 	/** 
   577 	 * Creates a new DSA parameters object from a specified 
   578 	 * prime, subprime, and base.
   579 	 * 
   580 	 * @param aP	The DSA parameter, p (the prime)
   581 	 * @param aQ	The DSA parameter, g (the subprime)
   582 	 * @param aG	The DSA parameter, g (the base)
   583 	 * @return		A pointer to a new CDSAParameters object
   584 	 */
   585 	IMPORT_C static CDSAParameters* NewL(RInteger& aP, RInteger& aQ, 
   586 		RInteger& aG);
   587 public:
   588 	/** @internalAll */
   589 	static TBool GeneratePrimesL(const TDesC8& aSeed, TUint& aCounter, 
   590 		RInteger& aP, TUint aL, RInteger& aQ, TBool aUseInputCounter=EFalse);
   591 protected:
   592 	/** 
   593 	 * Constructor
   594 	 * 
   595 	 * @param aP	The DSA parameter, p (the prime)
   596 	 * @param aQ	The DSA parameter, g (the subprime)
   597 	 * @param aG	The DSA parameter, g (the base)
   598 	 */
   599 	IMPORT_C CDSAParameters(RInteger& aP, RInteger& aQ, RInteger& aG);
   600 	
   601 	/** Default constructor */
   602 	IMPORT_C CDSAParameters(void);
   603 protected:
   604 	/** 
   605 	 * The DSA parameter, p (the prime).
   606 	 * 
   607 	 * A prime modulus whose length is between KMinPrimeLength and KMaxPrimeLength bits,
   608 	 * and is a multiple of KPrimeLengthMultiple. 
   609 	 */
   610 	RInteger iP;
   611 	
   612 	/** 
   613 	 * The DSA parameter, q (the subprime)
   614 	 * 
   615 	 * This is a 160-bit prime divisor of <code>p-1</code>. 
   616 	 */
   617 	RInteger iQ;
   618 	
   619 	/** 
   620 	 * The DSA parameter, g (the base)
   621 	 * 
   622 	 * <code>g = h^((p-1)/q) mod p</code>,
   623 	 * 
   624 	 * where h is any integer less than <code>p-1</code> such that <code>g &gt; 1</code> 
   625 	 */
   626 	RInteger iG;
   627 private:
   628 	CDSAParameters(const CDSAParameters&);
   629 	CDSAParameters& operator=(const CDSAParameters&);
   630 	};
   631 
   632 /**
   633 * Representation of a DSA public key.  
   634 *
   635 */
   636 class CDSAPublicKey : public CDSAParameters
   637 	{
   638 public:
   639 	/** 
   640 	 * Creates a new DSA public key object from a specified
   641 	 * primes, base, and public key. 
   642 	 * 
   643 	 * @param aP	The DSA parameter, p (the prime)
   644 	 * @param aQ	The DSA parameter, q (the subprime)
   645 	 * @param aG	The DSA parameter, g (the base)
   646 	 * @param aY	The DSA parameter, y (the public key)
   647 	 * @return		A pointer to a new CDSAPublicKey object
   648 	 */
   649 	IMPORT_C static CDSAPublicKey* NewL(RInteger& aP, RInteger& aQ, 
   650 		RInteger& aG, RInteger& aY);
   651 
   652 	/** 
   653 	 * Creates a new DSA public key object from a specified
   654 	 * primes, base, and public key. 
   655 	 * 
   656 	 * The returned pointer is put onto the cleanup stack.
   657 	 * 
   658 	 * @param aP	The DSA parameter, p (the prime)
   659 	 * @param aQ	The DSA parameter, q (the subprime)
   660 	 * @param aG	The DSA parameter, g (the base)
   661 	 * @param aY	The DSA parameter, y (the public key)
   662 	 * @return		A pointer to a new CDSAPublicKey object
   663 	 */
   664 	IMPORT_C static CDSAPublicKey* NewLC(RInteger& aP, RInteger& aQ, 
   665 		RInteger& aG, RInteger& aY);
   666 
   667 	/**
   668 	 * Gets the DSA parameter, y (the public key)
   669 	 *
   670 	 * @return	The DSA parameter, y
   671 	 */
   672 	IMPORT_C const TInteger& Y(void) const;
   673 
   674 	/** Destructor */
   675 	IMPORT_C virtual ~CDSAPublicKey(void);
   676 protected:
   677 	/** 
   678 	 * Constructor
   679 	 * 
   680 	 * @param aP	The DSA parameter, p (the prime)
   681 	 * @param aQ	The DSA parameter, q (the subprime)
   682 	 * @param aG	The DSA parameter, g (the base)
   683 	 * @param aY	The DSA parameter, y (the public key)
   684 	 */
   685 	IMPORT_C CDSAPublicKey(RInteger& aP, RInteger& aQ, RInteger& aG, 
   686 		RInteger& aY);
   687 	
   688 	/** Default constructor */
   689 	IMPORT_C CDSAPublicKey(void);
   690 protected:
   691 	/** 
   692 	 * The DSA parameter, y, which is the public key 
   693 	 *
   694 	 * <code>y = g^x mod p</code>
   695 	 */
   696 	RInteger iY;
   697 private:
   698 	CDSAPublicKey(const CDSAPublicKey&);
   699 	CDSAPublicKey& operator=(const CDSAPublicKey&);
   700 	};
   701 
   702 /** 
   703 * Representation of a DSA private key.  
   704 * 
   705 */
   706 class CDSAPrivateKey : public CDSAParameters
   707 	{
   708 public:
   709 	/** 
   710 	 * Creates a new DSA private key object from a specified
   711 	 * primes, base, and private key. 
   712 	 * 
   713 	 * @param aP	The DSA parameter, p (the prime)
   714 	 * @param aQ	The DSA parameter, q (the subprime)
   715 	 * @param aG	The DSA parameter, g (the base)
   716 	 * @param aX	The DSA parameter, x (the private key)
   717 	 * @return		A pointer to a new CDSAPrivateKey object
   718 	 */
   719 	IMPORT_C static CDSAPrivateKey* NewL(RInteger& aP, RInteger& aQ, 
   720 		RInteger& aG, RInteger& aX);
   721 
   722 	/** 
   723 	 * Creates a new DSA private key object from a specified
   724 	 * primes, base, and private key. 
   725 	 * 
   726 	 * The returned pointer is put onto the cleanup stack.
   727 	 * 
   728 	 * @param aP	The DSA parameter, p (the prime)
   729 	 * @param aQ	The DSA parameter, q (the subprime)
   730 	 * @param aG	The DSA parameter, g (the base)
   731 	 * @param aX	The DSA parameter, x (the private key)
   732 	 * @return		A pointer to a new CDSAPrivateKey object
   733 	 */
   734 	IMPORT_C static CDSAPrivateKey* NewLC(RInteger& aP, RInteger& aQ, 
   735 		RInteger& aG, RInteger& aX);
   736 
   737 	/**
   738 	 * Gets the DSA parameter, x (the private key)
   739 	 *
   740 	 * @return	The DSA parameter, x
   741 	 */
   742 	IMPORT_C const TInteger& X(void) const;
   743 
   744 	/** Destructor */
   745 	IMPORT_C virtual ~CDSAPrivateKey(void);
   746 protected:
   747 	/** 
   748 	 * Constructor
   749 	 * 
   750 	 * @param aP	The DSA parameter, p (the prime)
   751 	 * @param aQ	The DSA parameter, q (the subprime)
   752 	 * @param aG	The DSA parameter, g (the base)
   753 	 * @param aX	The DSA parameter, x (the private key)
   754 	 */
   755 	IMPORT_C CDSAPrivateKey(RInteger& aP, RInteger& aQ, RInteger& aG, 
   756 		RInteger& aX);
   757 		
   758 	/** Default constructor */
   759 	IMPORT_C CDSAPrivateKey(void);
   760 protected:
   761 	/** 
   762 	 * The DSA parameter, x, which is the private key 
   763 	 *
   764 	 * A pseudorandomly generated integer whose value is between 0 and q.
   765 	*/
   766 	RInteger iX;
   767 private:
   768 	CDSAPrivateKey(const CDSAPrivateKey&);
   769 	CDSAPrivateKey& operator=(const CDSAPrivateKey&);
   770 	};
   771 
   772 /** 
   773 * This class is capable of generating a DSA public/private key pair.
   774 * 
   775 */
   776 class CDSAKeyPair : public CBase
   777 	{
   778 public:
   779 	/** 
   780 	 * Creates a new DSA key pair and also a DSA prime certificate
   781 	 * 
   782 	 * @param aSize	The length (in bits) of the DSA parameter, p (the prime)
   783 	 * @return		A pointer to a new CDSAKeyPair object
   784 	 */
   785 	IMPORT_C static CDSAKeyPair* NewL(TUint aSize);
   786 
   787 	/** 
   788 	 * Creates a new DSA key pair and also a DSA prime certificate
   789 	 * 
   790 	 * The returned pointer is put onto the cleanup stack.
   791 	 * 
   792 	 * @param aSize	The length (in bits) of the DSA parameter, p (the prime)
   793 	 * @return		A pointer to a new CDSAKeyPair object
   794 	 */
   795 	IMPORT_C static CDSAKeyPair* NewLC(TUint aSize);
   796 	
   797 	/** 
   798 	 * Gets the DSA public key
   799 	 *
   800 	 * @return	The DSA public key object
   801 	 */
   802 	IMPORT_C const CDSAPublicKey& PublicKey(void) const;
   803 	
   804 	/** 
   805 	 * Gets the DSA private key
   806 	 *
   807 	 * @return	The DSA private key object
   808 	 */
   809 	IMPORT_C const CDSAPrivateKey& PrivateKey(void) const;
   810 	
   811 	/** 
   812 	 * Gets the DSA prime certificate (i.e., the seed and counter)
   813 	 *
   814 	 * @return	The DSA prime certificate object
   815 	 */
   816 	IMPORT_C const CDSAPrimeCertificate& PrimeCertificate(void) const;
   817 	
   818 	/** The destructor frees all resources owned by the object, prior to its destruction. */
   819 	IMPORT_C virtual ~CDSAKeyPair(void);
   820 protected:
   821 	/** Default constructor */
   822 	IMPORT_C CDSAKeyPair(void);
   823 protected:
   824 	/** The DSA public key */
   825 	CDSAPublicKey* iPublic;
   826 	/** The DSA private key */
   827 	CDSAPrivateKey* iPrivate;
   828 	/** The DSA prime certificate */
   829 	CDSAPrimeCertificate* iPrimeCertificate;
   830 private:
   831 	void ConstructL(TUint aSize);
   832 	CDSAKeyPair(const CDSAKeyPair&);
   833 	CDSAKeyPair& operator=(const CDSAKeyPair&);
   834 	};
   835 
   836 /** 
   837 * Concrete class representing the parameters common to both 
   838 * a Diffie-Hellman (DH) public and private key.  
   839 * 
   840 */
   841 class CDHParameters : public CBase
   842 	{
   843 public:
   844 	/**
   845 	 * Gets the DH parameter, n
   846 	 *
   847 	 * @return	An integer representing the DH parameter, n
   848 	 */
   849 	IMPORT_C const TInteger& N(void) const;
   850 
   851 	/**
   852 	 * Gets the DH parameter, g
   853 	 *
   854 	 * @return	An integer representing the DH parameter, g
   855 	 */
   856 	IMPORT_C const TInteger& G(void) const;
   857 	
   858 	/** Destructor */
   859 	IMPORT_C virtual ~CDHParameters(void);
   860 protected:
   861 	/** 
   862 	 * Constructor
   863 	 * 
   864 	 * @param aN	The DH parameter, n
   865 	 * @param aG	The DH parameter, g
   866 	 */
   867 	IMPORT_C CDHParameters(RInteger& aN, RInteger& aG);
   868 	
   869 	/** Default constructor */
   870 	IMPORT_C CDHParameters(void);
   871 protected:
   872 	/**
   873 	 * The DH parameter, n (a prime number)
   874 	 * 
   875 	 * <code>X = g^x mod n</code> (note the case sensitivity)
   876 	 */
   877 	RInteger iN;
   878 	/** 
   879 	 * The DH parameter, g (the generator) 
   880 	 *
   881 	 * <code>X = g^x mod n</code> (note the case sensitivity)
   882 	 */
   883 	RInteger iG;
   884 private:
   885 	CDHParameters(const CDHParameters&);
   886 	CDHParameters& operator=(const CDHParameters&);
   887 	};
   888 
   889 /** 
   890 * Representation of a Diffie-Hellman (DH) public key.  
   891 * 
   892 */
   893 class CDHPublicKey : public CDHParameters
   894 	{
   895 public:
   896 	/** 
   897 	 * Creates a new DH public key from a specified 
   898 	 * large prime, generator, and random large integer.
   899 	 * 
   900 	 * @param aN	The DH parameter, n (a large prime)
   901 	 * @param aG	The DH parameter, g (the generator)
   902 	 * @param aX	The DH value, X
   903 	 * @return		A pointer to a new CDHPublicKey object
   904 	 */
   905 	IMPORT_C static CDHPublicKey* NewL(RInteger& aN, RInteger& aG, 
   906 		RInteger& aX);
   907 
   908 	/** 
   909 	 * Creates a new DH public key from a specified 
   910 	 * large prime, generator, and random large integer.
   911 	 *
   912 	 * The returned pointer is put onto the cleanup stack.
   913 	 * 
   914 	 * @param aN	The DH parameter, n (a large prime)
   915 	 * @param aG	The DH parameter, g (the generator)
   916 	 * @param aX	The DH value, X
   917 	 * @return		A pointer to a new CDHPublicKey object
   918 	 */
   919 	IMPORT_C static CDHPublicKey* NewLC(RInteger& aN, RInteger& aG, 
   920 		RInteger& aX);
   921 	
   922 	/** 
   923 	 * Gets the DH value, X
   924 	 * 
   925 	 * @return	The DH value, X
   926 	 */	
   927 	IMPORT_C const TInteger& X(void) const;
   928 
   929 	/** Destructor */
   930 	IMPORT_C virtual ~CDHPublicKey(void);
   931 protected:
   932 	/** 
   933 	 * Constructor
   934 	 * 
   935 	 * @param aN	The DH parameter, n (a large prime)
   936 	 * @param aG	The DH parameter, g (the generator)
   937 	 * @param aX	The DH value, X
   938 	 */
   939 	IMPORT_C CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX);
   940 
   941 	/** Constructor */
   942 	IMPORT_C CDHPublicKey(void);
   943 protected:
   944 	/** 
   945 	 * The DH value, X
   946 	 *
   947 	 * <code>X = g^x mod n</code> (note the case sensitivity)
   948 	 */
   949 	RInteger iX;
   950 private:
   951 	CDHPublicKey(const CDHPublicKey&);
   952 	CDHPublicKey& operator=(const CDHPublicKey&);
   953 	};
   954 
   955 /** 
   956 * Representation of a Diffie-Hellman (DH) private key.  
   957 * 
   958 */
   959 class CDHPrivateKey : public CDHParameters
   960 	{
   961 public:
   962 	/** 
   963 	 * Creates a new DH private key from a specified 
   964 	 * large prime, generator, and random large integer.
   965 	 * 
   966 	 * @param aN	The DH parameter, n (a large prime)
   967 	 * @param aG	The DH parameter, g (the generator)
   968 	 * @param ax	The DH value, x (a random large integer)
   969 	 * @return		A pointer to a new CDHPrivateKey object
   970 	 */
   971 	IMPORT_C static CDHPrivateKey* NewL(RInteger& aN, RInteger& aG, 
   972 		RInteger& ax);
   973 
   974 	/** 
   975 	 * Creates a new DH private key from a specified 
   976 	 * large prime, generator, and random large integer.
   977 	 *
   978 	 * The returned pointer is put onto the cleanup stack.
   979 	 * 
   980 	 * @param aN	The DH parameter, n (a large prime)
   981 	 * @param aG	The DH parameter, g (the generator)
   982 	 * @param ax	The DH value, x (a random large integer)
   983 	 * @return		A pointer to a new CDHPrivateKey object
   984 	 */
   985 	IMPORT_C static CDHPrivateKey* NewLC(RInteger& aN, RInteger& aG, 
   986 		RInteger& ax);
   987 	
   988 	/** 
   989 	 * Gets the DH value, x, which is a random large integer.
   990 	 * 
   991 	 * @return	The DH value, x
   992 	 */	
   993 	IMPORT_C const TInteger& x(void) const;
   994 	
   995 	/** Destructor */
   996 	IMPORT_C virtual ~CDHPrivateKey(void);
   997 protected:
   998 	/** 
   999 	 * Constructor
  1000 	 * 
  1001 	 * @param aN	The DH parameter, n (a large prime)
  1002 	 * @param aG	The DH parameter, g (the generator)
  1003 	 * @param ax	The DH value, x (a random large integer)
  1004 	 */
  1005 	IMPORT_C CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax);
  1006 	
  1007 	/** Constructor */
  1008 	IMPORT_C CDHPrivateKey(void);
  1009 protected:
  1010 	/** 
  1011 	 * The DH value, x, which is a random large integer.
  1012 	 *
  1013 	 * <code>X = g^x mod n</code> (note the case sensitivity)
  1014 	 */
  1015 	RInteger ix;
  1016 private:
  1017 	CDHPrivateKey(const CDHPrivateKey&);
  1018 	CDHPrivateKey& operator=(const CDHPrivateKey&);
  1019 	};
  1020 
  1021 /** 
  1022 * This class is capable of generating a Diffie-Hellman (DH) public/private key pair.
  1023 * 
  1024 */
  1025 class CDHKeyPair : public CBase
  1026 	{
  1027 public:
  1028 	/**
  1029 	 * Creates a new DH key pair from a random large integer,
  1030 	 * and a specified large prime and generator.
  1031 	 *
  1032 	 * @param aN	The DH parameter, n (a large prime)
  1033 	 * @param aG	The DH parameter, g (the generator)
  1034 	 * @return		A pointer to a new CDHKeyPair object
  1035 	 * 
  1036 	 * @leave KErrArgument	If aG is out of bounds 
  1037 	 */
  1038 	IMPORT_C static CDHKeyPair* NewL(RInteger& aN, RInteger& aG);
  1039 
  1040 	/**
  1041 	 * Creates a new DH key pair from a random large integer,
  1042 	 * and a specified large prime and generator.
  1043 	 *
  1044 	 * The returned pointer is put onto the cleanup stack.
  1045 	 *
  1046 	 * @param aN	The DH parameter, n (a large prime)
  1047 	 * @param aG	The DH parameter, g (the generator)
  1048 	 * @return		A pointer to a new CDHKeyPair object
  1049 	 * 
  1050 	 * @leave KErrArgument	If aG is out of bounds 
  1051 	 */
  1052 	IMPORT_C static CDHKeyPair* NewLC(RInteger& aN, RInteger& aG);
  1053 
  1054 	/**
  1055 	 * Creates a new DH key pair from a specified 
  1056 	 * large prime, generator, and random large integer.
  1057 	 *
  1058 	 * @param aN	The DH parameter, n (a large prime)
  1059 	 * @param aG	The DH parameter, g (the generator)
  1060 	 * @param ax	The DH value, x (a random large integer)
  1061 	 * @return		A pointer to a new CDHKeyPair object
  1062 	 * 
  1063 	 * @leave KErrArgument	If either aG or ax are out of bounds 
  1064 	 */
  1065 	IMPORT_C static CDHKeyPair* NewL(RInteger& aN, RInteger& aG, RInteger& ax);
  1066 
  1067 	/**
  1068 	 * Creates a new DH key pair from a specified 
  1069 	 * large prime, generator, and random large integer.
  1070 	 *
  1071 	 * The returned pointer is put onto the cleanup stack.
  1072 	 *
  1073 	 * @param aN	The DH parameter, n (a large prime)
  1074 	 * @param aG	The DH parameter, g (the generator)
  1075 	 * @param ax	The DH value, x (a random large integer)
  1076 	 * @return		A pointer to a new CDHKeyPair object
  1077 	 * 
  1078 	 * @leave KErrArgument	If either aG or ax are out of bounds 
  1079 	 */
  1080 	IMPORT_C static CDHKeyPair* NewLC(RInteger& aN, RInteger& aG, RInteger& ax);
  1081 
  1082 	/**
  1083 	 * Gets the DH public key
  1084 	 *
  1085 	 * @return	The DH public key
  1086 	 */
  1087 	IMPORT_C const CDHPublicKey& PublicKey(void) const;
  1088 
  1089 	/**
  1090 	 * Gets the DH private key
  1091 	 *
  1092 	 * @return	The DH private key
  1093 	 */
  1094 	IMPORT_C const CDHPrivateKey& PrivateKey(void) const;
  1095 	
  1096 	/** The destructor frees all resources owned by the object, prior to its destruction. */
  1097 	IMPORT_C virtual ~CDHKeyPair(void);
  1098 protected:
  1099 	/** Default constructor */
  1100 	IMPORT_C CDHKeyPair(void);
  1101 	
  1102 	/** 
  1103 	 * Constructor
  1104 	 *
  1105 	 * @param aN	The DH parameter, n (a large prime)
  1106 	 * @param aG	The DH parameter, g (the generator)
  1107 	 */
  1108 	IMPORT_C void ConstructL(RInteger& aN, RInteger& aG);
  1109 
  1110 	/** 
  1111 	 * Constructor
  1112 	 *
  1113 	 * @param aN	The DH parameter, n (a large prime)
  1114 	 * @param aG	The DH parameter, g (the generator)
  1115 	 * @param ax	The DH value, x (a random large integer)
  1116 	 */
  1117 	IMPORT_C void ConstructL(RInteger& aN, RInteger& aG, RInteger& ax);
  1118 
  1119 protected:	
  1120 	/** The DH public key */
  1121 	CDHPublicKey* iPublic;
  1122 	/** The DH private key */
  1123 	CDHPrivateKey* iPrivate;
  1124 private:
  1125 	CDHKeyPair(const CDHKeyPair&);
  1126 	CDHKeyPair& operator=(const CDHKeyPair&);
  1127 	};
  1128 #endif	//	__ASYMMETRICKEYS_H__