sl@0: /* sl@0: * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * ** IMPORTANT ** API's in this file are published to 3rd party developers via the sl@0: * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted. sl@0: * Asymmetric keys implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: sl@0: #ifndef __ASYMMETRICKEYS_H__ sl@0: #define __ASYMMETRICKEYS_H__ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /** sl@0: * Defines the various ways of representing supported RSA private keys. sl@0: * sl@0: */ sl@0: enum TRSAPrivateKeyType sl@0: { sl@0: /** sl@0: * Standard type of RSA private key sl@0: * sl@0: * This consists of the modulus (n) and decryption exponent (d). sl@0: */ sl@0: EStandard, sl@0: /** sl@0: * CRT (Chinese Remainder Theorem) type of RSA private key sl@0: * sl@0: * This consists of the the first factor (p), the second factor (q), sl@0: * the first factor's CRT exponent (dP), the second factor's CRT exponent (dQ), sl@0: * and the (first) CRT coefficient (qInv). The two factors, p and q, are the sl@0: * first two prime factors of the RSA modulus, n. sl@0: */ sl@0: EStandardCRT sl@0: //We may support types like this in the future (currently these are a patent sl@0: //minefield): sl@0: //EMulti, //multi prime version of EStandard sl@0: //EMultiCRT //multi prime version of EStandardCRT sl@0: }; sl@0: sl@0: /** sl@0: * Concrete class representing the parameters common to both an RSA public and sl@0: * private key. sl@0: * sl@0: * See ANSI X9.31 and RSA PKCS#1 sl@0: * sl@0: */ sl@0: class CRSAParameters : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Gets the RSA parameter, n (the modulus) sl@0: * sl@0: * @return The RSA parameter, n sl@0: */ sl@0: IMPORT_C const TInteger& N(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CRSAParameters(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: */ sl@0: IMPORT_C CRSAParameters(RInteger& aN); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CRSAParameters(void); sl@0: protected: sl@0: /** The RSA modulus, n, a positive integer */ sl@0: RInteger iN; sl@0: private: sl@0: CRSAParameters(const CRSAParameters&); sl@0: CRSAParameters& operator=(const CRSAParameters&); sl@0: }; sl@0: sl@0: /** sl@0: * Representation of an RSA public key. sl@0: * sl@0: * An RSA public key is identified by its modulus (n) and its encryption exponent sl@0: * (e). sl@0: * sl@0: */ sl@0: class CRSAPublicKey : public CRSAParameters sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new CRSAPublicKey object from a specified sl@0: * modulus and encryption exponent. sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aE The RSA parameter, e (the encryption exponent) sl@0: * @return A pointer to a new CRSAPublicKey object sl@0: * sl@0: * @leave KErrArgument If either aN or aE are not positive integers, sl@0: * and releases ownership. sl@0: */ sl@0: IMPORT_C static CRSAPublicKey* NewL(RInteger& aN, RInteger& aE); sl@0: sl@0: /** sl@0: * Creates a new CRSAPublicKey object from a specified sl@0: * modulus and encryption exponent. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aE The RSA parameter, e (the encryption exponent) sl@0: * @return A pointer to a new CRSAPublicKey object sl@0: * sl@0: * @leave KErrArgument If either aN or aE are not positive integers, sl@0: * and releases ownership. sl@0: */ sl@0: IMPORT_C static CRSAPublicKey* NewLC(RInteger& aN, RInteger& aE); sl@0: sl@0: /** sl@0: * Gets the RSA parameter, e (the encryption exponent) sl@0: * sl@0: * @return The RSA parameter, e sl@0: */ sl@0: IMPORT_C const TInteger& E(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CRSAPublicKey(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aE The RSA parameter, e (the encryption exponent) sl@0: */ sl@0: IMPORT_C CRSAPublicKey(RInteger& aN, RInteger& aE); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CRSAPublicKey(void); sl@0: protected: sl@0: /** The RSA encryption exponent, e */ sl@0: RInteger iE; sl@0: private: sl@0: CRSAPublicKey(const CRSAPublicKey&); sl@0: CRSAPublicKey& operator=(const CRSAPublicKey&); sl@0: void ConstructL(); sl@0: }; sl@0: sl@0: /** sl@0: * Non-exported container class for the various ways of representing an RSA sl@0: * private key. sl@0: * sl@0: * To instantiate a representation of an RSA private key, find a sl@0: * subclass of this appropriate to your key type. sl@0: * sl@0: */ sl@0: class CRSAPrivateKey : public CRSAParameters sl@0: { sl@0: public: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aKeyType The type of the RSA private key sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @internalAll sl@0: */ sl@0: CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN); sl@0: public: sl@0: /** sl@0: * Gets the type of RSA private key sl@0: * sl@0: * @return The RSA private key type sl@0: */ sl@0: inline const TRSAPrivateKeyType PrivateKeyType() const {return (iKeyType);}; sl@0: protected: sl@0: /** The type of the RSA private key */ sl@0: const TRSAPrivateKeyType iKeyType; sl@0: private: sl@0: CRSAPrivateKey(const CRSAPrivateKey&); sl@0: CRSAPrivateKey& operator=(const CRSAPrivateKey&); sl@0: }; sl@0: sl@0: /** sl@0: * The 'classical' representation of a RSA private key. sl@0: * sl@0: * Such a private key is composed of a modulus (n) and a decryption exponent (d). sl@0: * sl@0: */ sl@0: class CRSAPrivateKeyStandard : public CRSAPrivateKey sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new CRSAPrivateKeyStandard object from a specified sl@0: * modulus and decryption exponent. sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aD The RSA parameter, d (the decryption exponent) sl@0: * @return A pointer to a new CRSAPrivateKeyStandard object sl@0: * sl@0: * @leave KErrArgument If either aN or aD are not positive integers, sl@0: * and releases ownership. sl@0: */ sl@0: IMPORT_C static CRSAPrivateKeyStandard* NewL(RInteger& aN, RInteger& aD); sl@0: sl@0: /** sl@0: * Creates a new CRSAPrivateKeyStandard object from a specified sl@0: * modulus and decryption exponent. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aD The RSA parameter, d (the decryption exponent) sl@0: * @return A pointer to a new CRSAPrivateKeyStandard object sl@0: * sl@0: * @leave KErrArgument If either aN or aD are not positive integers, sl@0: * and releases ownership. sl@0: */ sl@0: IMPORT_C static CRSAPrivateKeyStandard* NewLC(RInteger& aN, RInteger& aD); sl@0: sl@0: /** sl@0: * Gets the RSA parameter, d (the decryption exponent) sl@0: * sl@0: * @return The RSA parameter, d sl@0: */ sl@0: IMPORT_C const TInteger& D(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CRSAPrivateKeyStandard(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aD The RSA parameter, d (the decryption exponent) sl@0: */ sl@0: IMPORT_C CRSAPrivateKeyStandard(RInteger& aN, RInteger& aD); sl@0: protected: sl@0: /** The RSA decryption exponent, d */ sl@0: RInteger iD; sl@0: private: sl@0: CRSAPrivateKeyStandard(const CRSAPrivateKeyStandard&); sl@0: CRSAPrivateKeyStandard& operator=(const CRSAPrivateKeyStandard&); sl@0: void ConstructL(); sl@0: }; sl@0: sl@0: /** sl@0: * An alternate representation of an RSA private key providing significant sl@0: * speed enhancements through its use of the Chinese Remainder Theorem (CRT). sl@0: * sl@0: * Here, a private key is represented by a modulus (n), the two prime factors of sl@0: * the modulus (p, q), p's CRT exponent (dP), q's CRT exponent (dQ), and the CRT sl@0: * coefficient (qInv). See PKCS#1 at http://www.rsasecurity.com/rsalabs/pkcs/ sl@0: * for more information. sl@0: * sl@0: */ sl@0: class CRSAPrivateKeyCRT : public CRSAPrivateKey sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new CRSAPrivateKeyCRT object from a specified sl@0: * modulus and decryption exponent. sl@0: * sl@0: * @param iN The RSA parameter, n (the modulus) sl@0: * @param aP The RSA parameter, p (the first factor) sl@0: * @param aQ The RSA parameter, q (the second factor) sl@0: * @param aDP The RSA parameter, dP (the first factor's CRT exponent) sl@0: * @param aDQ The RSA parameter, dQ (the second factor's CRT exponent) sl@0: * @param aQInv The RSA parameter, qInv (the CRT coefficient) sl@0: * @return A pointer to a new CRSAPrivateKeyCRT object sl@0: * sl@0: * @leave KErrArgument If any of the parameters are not positive integers, sl@0: * and releases ownership. sl@0: */ sl@0: IMPORT_C static CRSAPrivateKeyCRT* NewL(RInteger& iN, RInteger& aP, sl@0: RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv); sl@0: sl@0: /** sl@0: * Creates a new CRSAPrivateKeyCRT object from a specified sl@0: * modulus and decryption exponent. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param iN The RSA parameter, n (the modulus) sl@0: * @param aP The RSA parameter, p (the first factor) sl@0: * @param aQ The RSA parameter, q (the second factor) sl@0: * @param aDP The RSA parameter, dP (the first factor's CRT exponent) sl@0: * @param aDQ The RSA parameter, dQ (the second factor's CRT exponent) sl@0: * @param aQInv The RSA parameter, qInv (the CRT coefficient) sl@0: * @return A pointer to a new CRSAPrivateKeyCRT object sl@0: * sl@0: * @leave KErrArgument If any of the parameters are not positive integers, sl@0: * and releases ownership. sl@0: */ sl@0: IMPORT_C static CRSAPrivateKeyCRT* NewLC(RInteger& iN, RInteger& aP, sl@0: RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv); sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CRSAPrivateKeyCRT(void); sl@0: sl@0: /** sl@0: * Gets the RSA parameter, p (the first factor) sl@0: * sl@0: * @return The first factor sl@0: */ sl@0: IMPORT_C const TInteger& P(void) const; sl@0: sl@0: /** sl@0: * Gets the RSA parameter, q (the second factor) sl@0: * sl@0: * @return The second factor sl@0: */ sl@0: IMPORT_C const TInteger& Q(void) const; sl@0: sl@0: /** sl@0: * Gets the RSA parameter, dP (the first factor's CRT exponent) sl@0: * sl@0: * @return The first factor's CRT exponent sl@0: */ sl@0: IMPORT_C const TInteger& DP(void) const; sl@0: sl@0: /** sl@0: * Gets the RSA parameter, dQ (the second factor's CRT exponent) sl@0: * sl@0: * @return The second factor's CRT exponent sl@0: */ sl@0: IMPORT_C const TInteger& DQ(void) const; sl@0: sl@0: /** sl@0: * Gets the RSA parameter, qInv (the CRT coefficient) sl@0: * sl@0: * @return The CRT coefficient sl@0: */ sl@0: IMPORT_C const TInteger& QInv(void) const; sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The RSA parameter, n (the modulus) sl@0: * @param aP The RSA parameter, p (the first factor) sl@0: * @param aQ The RSA parameter, q (the second factor) sl@0: * @param aDP The RSA parameter, dP (the first factor's CRT exponent) sl@0: * @param aDQ The RSA parameter, dQ (the second factor's CRT exponent) sl@0: * @param aQInv The RSA parameter, qInv (the CRT coefficient) sl@0: */ sl@0: IMPORT_C CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP, RInteger& aQ, sl@0: RInteger& aDP, RInteger& aDQ, RInteger& aQInv); sl@0: protected: sl@0: /** The RSA parameter, p, which is the first factor */ sl@0: RInteger iP; sl@0: /** The RSA parameter, q, which is the second factor */ sl@0: RInteger iQ; sl@0: /** The RSA parameter, dP, which is the first factor's CRT exponent */ sl@0: RInteger iDP; sl@0: /** The RSA parameter, dQ, which is the second factor's CRT exponent */ sl@0: RInteger iDQ; sl@0: /** The RSA parameter, qInv, which is the CRT coefficient */ sl@0: RInteger iQInv; sl@0: private: sl@0: CRSAPrivateKeyCRT(const CRSAPrivateKeyCRT&); sl@0: CRSAPrivateKeyCRT& operator=(const CRSAPrivateKeyCRT&); sl@0: void ConstructL(); sl@0: }; sl@0: sl@0: /** sl@0: * This class is capable of generating an RSA public/private key pair. sl@0: * sl@0: * By default, it generates 2 prime (standard) CRT private keys. sl@0: * sl@0: */ sl@0: class CRSAKeyPair : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new RSA key pair sl@0: * sl@0: * @param aModulusBits The length of the modulus, n (in bits) sl@0: * @param aKeyType The type of the RSA key sl@0: * @return A pointer to a new CRSAKeyPair object sl@0: * sl@0: * @leave KErrNotSupported If the type of RSA key is not supported sl@0: */ sl@0: IMPORT_C static CRSAKeyPair* NewL(TUint aModulusBits, sl@0: TRSAPrivateKeyType aKeyType = EStandardCRT); sl@0: sl@0: /** sl@0: * Creates a new RSA key pair sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aModulusBits The length of the modulus, n (in bits) sl@0: * @param aKeyType The type of the RSA key sl@0: * @return A pointer to a new CRSAKeyPair object sl@0: * sl@0: * @leave KErrNotSupported If the type of RSA key is not supported sl@0: */ sl@0: IMPORT_C static CRSAKeyPair* NewLC(TUint aModulusBits, sl@0: TRSAPrivateKeyType aKeyType = EStandardCRT); sl@0: sl@0: /** sl@0: * Gets the RSA public key sl@0: * sl@0: * @return A CRSAPublicKey object sl@0: */ sl@0: IMPORT_C const CRSAPublicKey& PublicKey(void) const; sl@0: sl@0: /** sl@0: * Gets the RSA private key sl@0: * sl@0: * @return A CRSAPrivateKey object sl@0: */ sl@0: IMPORT_C const CRSAPrivateKey& PrivateKey(void) const; sl@0: sl@0: /** The destructor frees all resources owned by the object, prior to its destruction. */ sl@0: IMPORT_C virtual ~CRSAKeyPair(void); sl@0: protected: sl@0: /** Default destructor */ sl@0: IMPORT_C CRSAKeyPair(void); sl@0: protected: sl@0: /** The RSA public key */ sl@0: CRSAPublicKey* iPublic; sl@0: /** The RSA private key */ sl@0: CRSAPrivateKey* iPrivate; sl@0: private: sl@0: void ConstructL(TUint aModulusBits, TRSAPrivateKeyType aKeyType, sl@0: TUint aPublicExponent); sl@0: CRSAKeyPair(const CRSAKeyPair&); sl@0: CRSAKeyPair& operator=(const CRSAKeyPair&); sl@0: }; sl@0: sl@0: /** sl@0: * Representation of the parameters used to generate the primes in a sl@0: * CDSAParameters object. sl@0: * sl@0: * Given such a certificate, one can ensure that the DSA sl@0: * primes contained in CDSAParameters were generated correctly. sl@0: * sl@0: * @see CDSAParameters::ValidatePrimesL() sl@0: * sl@0: */ sl@0: class CDSAPrimeCertificate : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DSA prime certificate from a specified sl@0: * seed and counter value. sl@0: * sl@0: * @param aSeed The seed from a DSA key generation process sl@0: * @param aCounter The counter value from a DSA key generation process sl@0: * @return A pointer to a new CDSAPrimeCertificate object sl@0: */ sl@0: IMPORT_C static CDSAPrimeCertificate* NewL(const TDesC8& aSeed, sl@0: TUint aCounter); sl@0: sl@0: /** sl@0: * Creates a new DSA prime certificate from a specified sl@0: * seed and counter value. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aSeed The seed from a DSA key generation process sl@0: * @param aCounter The counter value from a DSA key generation process sl@0: * @return A pointer to a new CDSAPrimeCertificate object sl@0: */ sl@0: IMPORT_C static CDSAPrimeCertificate* NewLC(const TDesC8& aSeed, sl@0: TUint aCounter); sl@0: sl@0: /** sl@0: * Gets the seed of the DSA prime certificate sl@0: * sl@0: * @return The seed sl@0: */ sl@0: IMPORT_C const TDesC8& Seed(void) const; sl@0: sl@0: /** sl@0: * Gets the counter value of the DSA prime certificate sl@0: * sl@0: * @return The counter's value sl@0: */ sl@0: IMPORT_C TUint Counter(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDSAPrimeCertificate(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aCounter The DSA key generation counter sl@0: */ sl@0: IMPORT_C CDSAPrimeCertificate(TUint aCounter); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CDSAPrimeCertificate(void); sl@0: /** @internalAll */ sl@0: void ConstructL(const TDesC8& aSeed); sl@0: protected: sl@0: /** The DSA key generation seed */ sl@0: const HBufC8* iSeed; sl@0: /** The DSA key generation counter */ sl@0: TUint iCounter; sl@0: private: sl@0: CDSAPrimeCertificate(const CDSAPrimeCertificate&); sl@0: CDSAPrimeCertificate& operator=(const CDSAPrimeCertificate&); sl@0: }; sl@0: sl@0: /** sl@0: * Concrete class representing the parameters common to both a DSA public and sl@0: * private key. sl@0: * sl@0: * See FIPS 186-2, Digital Signature Standard sl@0: * sl@0: */ sl@0: class CDSAParameters : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Gets the DSA parameter, p (the prime) sl@0: * sl@0: * @return The DSA parameter, p sl@0: */ sl@0: IMPORT_C const TInteger& P(void) const; sl@0: sl@0: /** sl@0: * Gets the DSA parameter, q (the subprime) sl@0: * sl@0: * @return The DSA parameter, q sl@0: */ sl@0: IMPORT_C const TInteger& Q(void) const; sl@0: sl@0: /** sl@0: * Gets the DSA parameter, g (the base) sl@0: * sl@0: * @return The DSA parameter, g sl@0: */ sl@0: IMPORT_C const TInteger& G(void) const; sl@0: sl@0: /** sl@0: * Validates the primes regenerated from a DSA prime certificate sl@0: * sl@0: * @param aCert The DSA prime certificate that contains the seed and sl@0: * counter value from a DSA key generation process sl@0: * @return Whether or not the primes are valid sl@0: */ sl@0: IMPORT_C TBool ValidatePrimesL(const CDSAPrimeCertificate& aCert) const; sl@0: sl@0: /** sl@0: * Whether or not the prime is of a valid length sl@0: * sl@0: * It is valid if the length of the prime modulus is between KMinPrimeLength sl@0: * and KMaxPrimeLength bits, and the prime is a multiple of KPrimeLengthMultiple. sl@0: * sl@0: * @param aPrimeBits The prime modulus sl@0: * @return ETrue, if within the constraints; EFalse, otherwise. sl@0: */ sl@0: IMPORT_C static TBool ValidPrimeLength(TUint aPrimeBits); sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDSAParameters(void); sl@0: sl@0: /** sl@0: * Creates a new DSA parameters object from a specified sl@0: * prime, subprime, and base. sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, g (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @return A pointer to a new CDSAParameters object sl@0: */ sl@0: IMPORT_C static CDSAParameters* NewL(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG); sl@0: public: sl@0: /** @internalAll */ sl@0: static TBool GeneratePrimesL(const TDesC8& aSeed, TUint& aCounter, sl@0: RInteger& aP, TUint aL, RInteger& aQ, TBool aUseInputCounter=EFalse); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, g (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: */ sl@0: IMPORT_C CDSAParameters(RInteger& aP, RInteger& aQ, RInteger& aG); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CDSAParameters(void); sl@0: protected: sl@0: /** sl@0: * The DSA parameter, p (the prime). sl@0: * sl@0: * A prime modulus whose length is between KMinPrimeLength and KMaxPrimeLength bits, sl@0: * and is a multiple of KPrimeLengthMultiple. sl@0: */ sl@0: RInteger iP; sl@0: sl@0: /** sl@0: * The DSA parameter, q (the subprime) sl@0: * sl@0: * This is a 160-bit prime divisor of p-1. sl@0: */ sl@0: RInteger iQ; sl@0: sl@0: /** sl@0: * The DSA parameter, g (the base) sl@0: * sl@0: * g = h^((p-1)/q) mod p, sl@0: * sl@0: * where h is any integer less than p-1 such that g > 1 sl@0: */ sl@0: RInteger iG; sl@0: private: sl@0: CDSAParameters(const CDSAParameters&); sl@0: CDSAParameters& operator=(const CDSAParameters&); sl@0: }; sl@0: sl@0: /** sl@0: * Representation of a DSA public key. sl@0: * sl@0: */ sl@0: class CDSAPublicKey : public CDSAParameters sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DSA public key object from a specified sl@0: * primes, base, and public key. sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, q (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @param aY The DSA parameter, y (the public key) sl@0: * @return A pointer to a new CDSAPublicKey object sl@0: */ sl@0: IMPORT_C static CDSAPublicKey* NewL(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aY); sl@0: sl@0: /** sl@0: * Creates a new DSA public key object from a specified sl@0: * primes, base, and public key. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, q (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @param aY The DSA parameter, y (the public key) sl@0: * @return A pointer to a new CDSAPublicKey object sl@0: */ sl@0: IMPORT_C static CDSAPublicKey* NewLC(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aY); sl@0: sl@0: /** sl@0: * Gets the DSA parameter, y (the public key) sl@0: * sl@0: * @return The DSA parameter, y sl@0: */ sl@0: IMPORT_C const TInteger& Y(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDSAPublicKey(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, q (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @param aY The DSA parameter, y (the public key) sl@0: */ sl@0: IMPORT_C CDSAPublicKey(RInteger& aP, RInteger& aQ, RInteger& aG, sl@0: RInteger& aY); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CDSAPublicKey(void); sl@0: protected: sl@0: /** sl@0: * The DSA parameter, y, which is the public key sl@0: * sl@0: * y = g^x mod p sl@0: */ sl@0: RInteger iY; sl@0: private: sl@0: CDSAPublicKey(const CDSAPublicKey&); sl@0: CDSAPublicKey& operator=(const CDSAPublicKey&); sl@0: }; sl@0: sl@0: /** sl@0: * Representation of a DSA private key. sl@0: * sl@0: */ sl@0: class CDSAPrivateKey : public CDSAParameters sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DSA private key object from a specified sl@0: * primes, base, and private key. sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, q (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @param aX The DSA parameter, x (the private key) sl@0: * @return A pointer to a new CDSAPrivateKey object sl@0: */ sl@0: IMPORT_C static CDSAPrivateKey* NewL(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aX); sl@0: sl@0: /** sl@0: * Creates a new DSA private key object from a specified sl@0: * primes, base, and private key. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, q (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @param aX The DSA parameter, x (the private key) sl@0: * @return A pointer to a new CDSAPrivateKey object sl@0: */ sl@0: IMPORT_C static CDSAPrivateKey* NewLC(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aX); sl@0: sl@0: /** sl@0: * Gets the DSA parameter, x (the private key) sl@0: * sl@0: * @return The DSA parameter, x sl@0: */ sl@0: IMPORT_C const TInteger& X(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDSAPrivateKey(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aP The DSA parameter, p (the prime) sl@0: * @param aQ The DSA parameter, q (the subprime) sl@0: * @param aG The DSA parameter, g (the base) sl@0: * @param aX The DSA parameter, x (the private key) sl@0: */ sl@0: IMPORT_C CDSAPrivateKey(RInteger& aP, RInteger& aQ, RInteger& aG, sl@0: RInteger& aX); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CDSAPrivateKey(void); sl@0: protected: sl@0: /** sl@0: * The DSA parameter, x, which is the private key sl@0: * sl@0: * A pseudorandomly generated integer whose value is between 0 and q. sl@0: */ sl@0: RInteger iX; sl@0: private: sl@0: CDSAPrivateKey(const CDSAPrivateKey&); sl@0: CDSAPrivateKey& operator=(const CDSAPrivateKey&); sl@0: }; sl@0: sl@0: /** sl@0: * This class is capable of generating a DSA public/private key pair. sl@0: * sl@0: */ sl@0: class CDSAKeyPair : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DSA key pair and also a DSA prime certificate sl@0: * sl@0: * @param aSize The length (in bits) of the DSA parameter, p (the prime) sl@0: * @return A pointer to a new CDSAKeyPair object sl@0: */ sl@0: IMPORT_C static CDSAKeyPair* NewL(TUint aSize); sl@0: sl@0: /** sl@0: * Creates a new DSA key pair and also a DSA prime certificate sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aSize The length (in bits) of the DSA parameter, p (the prime) sl@0: * @return A pointer to a new CDSAKeyPair object sl@0: */ sl@0: IMPORT_C static CDSAKeyPair* NewLC(TUint aSize); sl@0: sl@0: /** sl@0: * Gets the DSA public key sl@0: * sl@0: * @return The DSA public key object sl@0: */ sl@0: IMPORT_C const CDSAPublicKey& PublicKey(void) const; sl@0: sl@0: /** sl@0: * Gets the DSA private key sl@0: * sl@0: * @return The DSA private key object sl@0: */ sl@0: IMPORT_C const CDSAPrivateKey& PrivateKey(void) const; sl@0: sl@0: /** sl@0: * Gets the DSA prime certificate (i.e., the seed and counter) sl@0: * sl@0: * @return The DSA prime certificate object sl@0: */ sl@0: IMPORT_C const CDSAPrimeCertificate& PrimeCertificate(void) const; sl@0: sl@0: /** The destructor frees all resources owned by the object, prior to its destruction. */ sl@0: IMPORT_C virtual ~CDSAKeyPair(void); sl@0: protected: sl@0: /** Default constructor */ sl@0: IMPORT_C CDSAKeyPair(void); sl@0: protected: sl@0: /** The DSA public key */ sl@0: CDSAPublicKey* iPublic; sl@0: /** The DSA private key */ sl@0: CDSAPrivateKey* iPrivate; sl@0: /** The DSA prime certificate */ sl@0: CDSAPrimeCertificate* iPrimeCertificate; sl@0: private: sl@0: void ConstructL(TUint aSize); sl@0: CDSAKeyPair(const CDSAKeyPair&); sl@0: CDSAKeyPair& operator=(const CDSAKeyPair&); sl@0: }; sl@0: sl@0: /** sl@0: * Concrete class representing the parameters common to both sl@0: * a Diffie-Hellman (DH) public and private key. sl@0: * sl@0: */ sl@0: class CDHParameters : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Gets the DH parameter, n sl@0: * sl@0: * @return An integer representing the DH parameter, n sl@0: */ sl@0: IMPORT_C const TInteger& N(void) const; sl@0: sl@0: /** sl@0: * Gets the DH parameter, g sl@0: * sl@0: * @return An integer representing the DH parameter, g sl@0: */ sl@0: IMPORT_C const TInteger& G(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDHParameters(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The DH parameter, n sl@0: * @param aG The DH parameter, g sl@0: */ sl@0: IMPORT_C CDHParameters(RInteger& aN, RInteger& aG); sl@0: sl@0: /** Default constructor */ sl@0: IMPORT_C CDHParameters(void); sl@0: protected: sl@0: /** sl@0: * The DH parameter, n (a prime number) sl@0: * sl@0: * X = g^x mod n (note the case sensitivity) sl@0: */ sl@0: RInteger iN; sl@0: /** sl@0: * The DH parameter, g (the generator) sl@0: * sl@0: * X = g^x mod n (note the case sensitivity) sl@0: */ sl@0: RInteger iG; sl@0: private: sl@0: CDHParameters(const CDHParameters&); sl@0: CDHParameters& operator=(const CDHParameters&); sl@0: }; sl@0: sl@0: /** sl@0: * Representation of a Diffie-Hellman (DH) public key. sl@0: * sl@0: */ sl@0: class CDHPublicKey : public CDHParameters sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DH public key from a specified sl@0: * large prime, generator, and random large integer. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param aX The DH value, X sl@0: * @return A pointer to a new CDHPublicKey object sl@0: */ sl@0: IMPORT_C static CDHPublicKey* NewL(RInteger& aN, RInteger& aG, sl@0: RInteger& aX); sl@0: sl@0: /** sl@0: * Creates a new DH public key from a specified sl@0: * large prime, generator, and random large integer. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param aX The DH value, X sl@0: * @return A pointer to a new CDHPublicKey object sl@0: */ sl@0: IMPORT_C static CDHPublicKey* NewLC(RInteger& aN, RInteger& aG, sl@0: RInteger& aX); sl@0: sl@0: /** sl@0: * Gets the DH value, X sl@0: * sl@0: * @return The DH value, X sl@0: */ sl@0: IMPORT_C const TInteger& X(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDHPublicKey(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param aX The DH value, X sl@0: */ sl@0: IMPORT_C CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX); sl@0: sl@0: /** Constructor */ sl@0: IMPORT_C CDHPublicKey(void); sl@0: protected: sl@0: /** sl@0: * The DH value, X sl@0: * sl@0: * X = g^x mod n (note the case sensitivity) sl@0: */ sl@0: RInteger iX; sl@0: private: sl@0: CDHPublicKey(const CDHPublicKey&); sl@0: CDHPublicKey& operator=(const CDHPublicKey&); sl@0: }; sl@0: sl@0: /** sl@0: * Representation of a Diffie-Hellman (DH) private key. sl@0: * sl@0: */ sl@0: class CDHPrivateKey : public CDHParameters sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DH private key from a specified sl@0: * large prime, generator, and random large integer. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param ax The DH value, x (a random large integer) sl@0: * @return A pointer to a new CDHPrivateKey object sl@0: */ sl@0: IMPORT_C static CDHPrivateKey* NewL(RInteger& aN, RInteger& aG, sl@0: RInteger& ax); sl@0: sl@0: /** sl@0: * Creates a new DH private key from a specified sl@0: * large prime, generator, and random large integer. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param ax The DH value, x (a random large integer) sl@0: * @return A pointer to a new CDHPrivateKey object sl@0: */ sl@0: IMPORT_C static CDHPrivateKey* NewLC(RInteger& aN, RInteger& aG, sl@0: RInteger& ax); sl@0: sl@0: /** sl@0: * Gets the DH value, x, which is a random large integer. sl@0: * sl@0: * @return The DH value, x sl@0: */ sl@0: IMPORT_C const TInteger& x(void) const; sl@0: sl@0: /** Destructor */ sl@0: IMPORT_C virtual ~CDHPrivateKey(void); sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param ax The DH value, x (a random large integer) sl@0: */ sl@0: IMPORT_C CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax); sl@0: sl@0: /** Constructor */ sl@0: IMPORT_C CDHPrivateKey(void); sl@0: protected: sl@0: /** sl@0: * The DH value, x, which is a random large integer. sl@0: * sl@0: * X = g^x mod n (note the case sensitivity) sl@0: */ sl@0: RInteger ix; sl@0: private: sl@0: CDHPrivateKey(const CDHPrivateKey&); sl@0: CDHPrivateKey& operator=(const CDHPrivateKey&); sl@0: }; sl@0: sl@0: /** sl@0: * This class is capable of generating a Diffie-Hellman (DH) public/private key pair. sl@0: * sl@0: */ sl@0: class CDHKeyPair : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new DH key pair from a random large integer, sl@0: * and a specified large prime and generator. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @return A pointer to a new CDHKeyPair object sl@0: * sl@0: * @leave KErrArgument If aG is out of bounds sl@0: */ sl@0: IMPORT_C static CDHKeyPair* NewL(RInteger& aN, RInteger& aG); sl@0: sl@0: /** sl@0: * Creates a new DH key pair from a random large integer, sl@0: * and a specified large prime and generator. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @return A pointer to a new CDHKeyPair object sl@0: * sl@0: * @leave KErrArgument If aG is out of bounds sl@0: */ sl@0: IMPORT_C static CDHKeyPair* NewLC(RInteger& aN, RInteger& aG); sl@0: sl@0: /** sl@0: * Creates a new DH key pair from a specified sl@0: * large prime, generator, and random large integer. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param ax The DH value, x (a random large integer) sl@0: * @return A pointer to a new CDHKeyPair object sl@0: * sl@0: * @leave KErrArgument If either aG or ax are out of bounds sl@0: */ sl@0: IMPORT_C static CDHKeyPair* NewL(RInteger& aN, RInteger& aG, RInteger& ax); sl@0: sl@0: /** sl@0: * Creates a new DH key pair from a specified sl@0: * large prime, generator, and random large integer. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param ax The DH value, x (a random large integer) sl@0: * @return A pointer to a new CDHKeyPair object sl@0: * sl@0: * @leave KErrArgument If either aG or ax are out of bounds sl@0: */ sl@0: IMPORT_C static CDHKeyPair* NewLC(RInteger& aN, RInteger& aG, RInteger& ax); sl@0: sl@0: /** sl@0: * Gets the DH public key sl@0: * sl@0: * @return The DH public key sl@0: */ sl@0: IMPORT_C const CDHPublicKey& PublicKey(void) const; sl@0: sl@0: /** sl@0: * Gets the DH private key sl@0: * sl@0: * @return The DH private key sl@0: */ sl@0: IMPORT_C const CDHPrivateKey& PrivateKey(void) const; sl@0: sl@0: /** The destructor frees all resources owned by the object, prior to its destruction. */ sl@0: IMPORT_C virtual ~CDHKeyPair(void); sl@0: protected: sl@0: /** Default constructor */ sl@0: IMPORT_C CDHKeyPair(void); sl@0: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: */ sl@0: IMPORT_C void ConstructL(RInteger& aN, RInteger& aG); sl@0: sl@0: /** sl@0: * Constructor sl@0: * sl@0: * @param aN The DH parameter, n (a large prime) sl@0: * @param aG The DH parameter, g (the generator) sl@0: * @param ax The DH value, x (a random large integer) sl@0: */ sl@0: IMPORT_C void ConstructL(RInteger& aN, RInteger& aG, RInteger& ax); sl@0: sl@0: protected: sl@0: /** The DH public key */ sl@0: CDHPublicKey* iPublic; sl@0: /** The DH private key */ sl@0: CDHPrivateKey* iPrivate; sl@0: private: sl@0: CDHKeyPair(const CDHKeyPair&); sl@0: CDHKeyPair& operator=(const CDHKeyPair&); sl@0: }; sl@0: #endif // __ASYMMETRICKEYS_H__