Update contrib.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
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 crypto implementation
28 #ifndef __ASYMMETRIC_H__
29 #define __ASYMMETRIC_H__
32 #include <asymmetrickeys.h>
36 // All the classes in this file have their default constructors and
37 // assignment operators defined private, but not implemented, in order to
41 * Mixin class defining common operations for public key encryption and
49 * Gets the maximum size of input accepted by this object.
51 * @return The maximum input length allowed in bytes.
53 virtual TInt MaxInputLength(void) const = 0;
56 * Gets the maximum size of output that can be generated by this object.
58 * @return The maximum output length in bytes.
60 virtual TInt MaxOutputLength(void) const = 0;
65 IMPORT_C MCryptoSystem(void);
67 MCryptoSystem(const MCryptoSystem&);
68 MCryptoSystem& operator=(const MCryptoSystem&);
72 * Abstract base class for all public key encryptors.
75 class CEncryptor : public CBase, public MCryptoSystem
79 * Encrypts the specified plaintext into ciphertext.
81 * @param aInput The plaintext
82 * @param aOutput On return, the ciphertext
84 * @panic KCryptoPanic If the input data is too long.
85 * See ECryptoPanicInputTooLarge
86 * @panic KCryptoPanic If the supplied output descriptor is not large enough to store the result.
87 * See ECryptoPanicOutputDescriptorOverflow
89 virtual void EncryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
91 /** Default constructor */
92 IMPORT_C CEncryptor(void);
94 CEncryptor(const CEncryptor&);
95 CEncryptor& operator=(const CEncryptor&);
99 * Abstract base class for all public key decryptors.
102 class CDecryptor : public CBase, public MCryptoSystem
106 * Decrypts the specified ciphertext into plaintext
108 * @param aInput The ciphertext to be decrypted
109 * @param aOutput On return, the plaintext
111 * @panic KCryptoPanic If the input data is too long.
112 * See ECryptoPanicInputTooLarge
113 * @panic KCryptoPanic If the supplied output descriptor is not large enough to store the result.
114 * See ECryptoPanicOutputDescriptorOverflow
116 virtual void DecryptL(const TDesC8& aInput, TDes8& aOutput) const = 0;
118 /** Default constructor */
119 IMPORT_C CDecryptor(void);
121 CDecryptor(const CDecryptor&);
122 CDecryptor& operator=(const CDecryptor&);
126 * Implementation of RSA encryption as described in PKCS#1 v1.5.
129 class CRSAPKCS1v15Encryptor : public CEncryptor
133 * Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
135 * @param aKey The RSA encryption key
136 * @return A pointer to a new CRSAPKCS1v15Encryptor object
138 * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
139 * cipher strength restrictions of the crypto library.
140 * See TCrypto::IsAsymmetricWeakEnoughL()
141 * @leave KErrKeySize If the key length is too small
143 IMPORT_C static CRSAPKCS1v15Encryptor* NewL(const CRSAPublicKey& aKey);
146 * Creates a new RSA encryptor object using PKCS#1 v1.5 padding.
148 * The returned pointer is put onto the cleanup stack.
150 * @param aKey The RSA encryption key
151 * @return A pointer to a new CRSAPKCS1v15Encryptor object
153 * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
154 * cipher strength restrictions of the crypto library.
155 * See TCrypto::IsAsymmetricWeakEnoughL()
156 * @leave KErrKeySize If the key length is too small
158 IMPORT_C static CRSAPKCS1v15Encryptor* NewLC(const CRSAPublicKey& aKey);
159 void EncryptL(const TDesC8& aInput, TDes8& aOutput) const;
160 TInt MaxInputLength(void) const;
161 TInt MaxOutputLength(void) const;
162 /** The destructor frees all resources owned by the object, prior to its destruction. */
163 virtual ~CRSAPKCS1v15Encryptor(void);
166 CRSAPKCS1v15Encryptor(const CRSAPublicKey& aKey);
168 void ConstructL(void);
170 /** The RSA public key */
171 const CRSAPublicKey& iPublicKey;
172 /** The PKCS#1 v1.5 encryption padding */
173 CPaddingPKCS1Encryption* iPadding;
175 CRSAPKCS1v15Encryptor(const CRSAPKCS1v15Encryptor&);
176 CRSAPKCS1v15Encryptor& operator=(const CRSAPKCS1v15Encryptor&);
180 * Implementation of RSA decryption as described in PKCS#1 v1.5.
183 class CRSAPKCS1v15Decryptor : public CDecryptor
187 * Creates a new RSA decryptor object using PKCS#1 v1.5 padding.
189 * @param aKey The RSA private key for decryption
191 * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
192 * cipher strength restrictions of the crypto library.
193 * See TCrypto::IsAsymmetricWeakEnoughL()
194 * @leave KErrKeySize If the key length is too small
196 IMPORT_C static CRSAPKCS1v15Decryptor* NewL(const CRSAPrivateKey& aKey);
199 * Creates a new RSA decryptor object using PKCS#1 v1.5 padding
201 * The returned pointer is put onto the cleanup stack.
203 * @param aKey The RSA private key for decryption
205 * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
206 * cipher strength restrictions of the crypto library.
207 * See TCrypto::IsAsymmetricWeakEnoughL()
208 * @leave KErrKeySize If the key length is too small
209 * @leave KErrNotSupported If the RSA private key is not a supported TRSAPrivateKeyType
211 IMPORT_C static CRSAPKCS1v15Decryptor* NewLC(const CRSAPrivateKey& aKey);
212 void DecryptL(const TDesC8& aInput, TDes8& aOutput) const;
213 TInt MaxInputLength(void) const;
214 TInt MaxOutputLength(void) const;
215 /** The destructor frees all resources owned by the object, prior to its destruction. */
216 virtual ~CRSAPKCS1v15Decryptor(void);
219 CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey);
221 void ConstructL(void);
223 /** The RSA private key */
224 const CRSAPrivateKey& iPrivateKey;
225 /** The PKCS#1 v1.5 encryption padding */
226 CPaddingPKCS1Encryption* iPadding;
228 CRSAPKCS1v15Decryptor(const CRSAPKCS1v15Decryptor&);
229 CRSAPKCS1v15Decryptor& operator=(const CRSAPKCS1v15Decryptor&);
233 * Mixin class defining operations common to all public key signature systems.
236 class MSignatureSystem
240 * Gets the maximum size of input accepted by this object.
242 * @return The maximum length allowed in bytes
244 virtual TInt MaxInputLength(void) const = 0;
247 IMPORT_C MSignatureSystem(void);
249 MSignatureSystem(const MSignatureSystem&);
250 MSignatureSystem& operator=(const MSignatureSystem&);
254 * Abstract base class for all public key signers.
256 * The template parameter, CSignature, should be a class that encapsulates the
257 * concept of a digital signature. Derived signature classes must own their
258 * respective signatures (and hence be CBase derived). There are no other
259 * restrictions on the formation of the signature classes.
262 template <class CSignature> class CSigner : public CBase, public MSignatureSystem
266 * Digitally signs the specified input message
268 * @param aInput The raw data to sign, typically a hash of the actual message
269 * @return A pointer to a new CSignature object
271 * @panic ECryptoPanicInputTooLarge If aInput is larger than MaxInputLength(),
272 * which is likely to happen if the caller
273 * has passed in something that has not been
276 virtual CSignature* SignL(const TDesC8& aInput) const = 0;
281 CSigner(const CSigner&);
282 CSigner& operator=(const CSigner&);
286 * Abstract class for all public key verifiers.
288 * The template parameter, CSignature, should be a class that encapsulates the
289 * concept of a digital signature. Derived signature classes must own their
290 * respective signatures (and hence be CBase derived). There are no other
291 * restrictions on the formation of the signature classes.
294 template <class CSignature> class CVerifier : public CBase, public MSignatureSystem
298 * Verifies the specified digital signature
300 * @param aInput The message digest that was originally signed
301 * @param aSignature The signature to be verified
303 * @return Whether the signature is the result of signing
304 * aInput with the supplied key
306 virtual TBool VerifyL(const TDesC8& aInput,
307 const CSignature& aSignature) const = 0;
312 CVerifier(const CVerifier&);
313 CVerifier& operator=(const CVerifier&);
316 /* Template nastiness for CVerifier and CSigner in asymmetric.inl */
318 #include <asymmetric.inl>
321 * An encapsulation of a RSA signature.
324 class CRSASignature : public CBase
328 * Creates a new CRSASignature object from the integer value
329 * output of a previous RSA signing operation.
331 * @param aS The integer value output from a previous RSA signing operation
332 * @return A pointer to the new CRSASignature object.
334 IMPORT_C static CRSASignature* NewL(RInteger& aS);
337 * Creates a new CRSASignature object from the integer value
338 * output of a previous RSA signing operation.
340 * The returned pointer is put onto the cleanup stack.
342 * @param aS The integer value output from a previous RSA signing operation
343 * @return A pointer to the new CRSASignature object.
345 IMPORT_C static CRSASignature* NewLC(RInteger& aS);
348 * Gets the integer value of the RSA signature
350 * @return The integer value of the RSA signature
352 IMPORT_C const TInteger& S(void) const;
355 * Whether this RSASignature is identical to a specified RSASignature
357 * @param aSig The RSASignature for comparison
358 * @return ETrue, if the two signatures are identical; EFalse, otherwise.
360 IMPORT_C TBool operator== (const CRSASignature& aSig) const;
363 /** The destructor frees all resources owned by the object, prior to its destruction. */
364 IMPORT_C virtual ~CRSASignature(void);
367 * Second phase constructor
369 * @see CRSASignature::NewL()
371 * @param aS The integer value output from a previous RSA signing operation
373 IMPORT_C CRSASignature(RInteger& aS);
375 /** Default constructor */
376 IMPORT_C CRSASignature(void);
378 /** An integer value; the output from a previous RSA signing operation. */
381 CRSASignature(const CRSASignature&);
382 CRSASignature& operator=(const CRSASignature);
386 * Abstract base class for all RSA Signers.
389 class CRSASigner : public CSigner<CRSASignature>
393 * Gets the maximum size of output that can be generated by this object.
395 * @return The maximum output length in bytes
397 virtual TInt MaxOutputLength(void) const = 0;
399 /** Default constructor */
400 IMPORT_C CRSASigner(void);
402 CRSASigner(const CRSASigner&);
403 CRSASigner& operator=(const CRSASigner&);
407 * Implementation of RSA signing as described in PKCS#1 v1.5.
409 * This class creates RSA signatures following the RSA PKCS#1 v1.5 standard (with
410 * the one caveat noted below) and using PKCS#1 v1.5 signature padding. The only
411 * exception is that the SignL() function simply performs a 'raw' PKCS#1 v1.5 sign
412 * operation on whatever it is given. It does <b>not</b> hash or in any way
413 * manipulate the input data before signing.
416 class CRSAPKCS1v15Signer : public CRSASigner
420 * Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
422 * @param aKey The RSA private key to be used for signing
423 * @return A pointer to the new CRSAPKCS1v15Signer object
425 * @leave KErrKeySize If the key length is too small
427 IMPORT_C static CRSAPKCS1v15Signer* NewL(const CRSAPrivateKey& aKey);
430 * Creates a new CRSAPKCS1v15Signer object from a specified RSA private key.
432 * The returned pointer is put onto the cleanup stack.
434 * @param aKey The RSA private key to be used for signing
435 * @return A pointer to the new CRSAPKCS1v15Signer object
437 * @leave KErrKeySize If the key length is too small
439 IMPORT_C static CRSAPKCS1v15Signer* NewLC(const CRSAPrivateKey& aKey);
441 * Digitally signs the specified input message
443 * @param aInput The raw data to sign, typically a hash of the actual message
444 * @return A pointer to a new CSignature object
446 * @leave KErrNotSupported If the private key is not a supported TRSAPrivateKeyType
447 * @panic ECryptoPanicInputTooLarge If aInput is larger than MaxInputLength(),
448 * which is likely to happen if the caller
449 * has passed in something that has not been hashed.
451 virtual CRSASignature* SignL(const TDesC8& aInput) const;
452 virtual TInt MaxInputLength(void) const;
453 virtual TInt MaxOutputLength(void) const;
454 /** The destructor frees all resources owned by the object, prior to its destruction.
456 ~CRSAPKCS1v15Signer(void);
459 CRSAPKCS1v15Signer(const CRSAPrivateKey& aKey);
461 void ConstructL(void);
463 /** The RSA private key to be used for signing */
464 const CRSAPrivateKey& iPrivateKey;
465 /** The PKCS#1 v1.5 signature padding */
466 CPaddingPKCS1Signature* iPadding;
468 CRSAPKCS1v15Signer(const CRSAPKCS1v15Signer&);
469 CRSAPKCS1v15Signer& operator=(const CRSAPKCS1v15Signer&);
473 * Abstract base class for all RSA Verifiers.
476 class CRSAVerifier : public CVerifier<CRSASignature>
480 * Gets the maximum size of output that can be generated by this object.
482 * @return The maximum output length in bytes
484 virtual TInt MaxOutputLength(void) const = 0;
487 * Performs a decryption operation on a signature using the public key.
489 * This is the inverse of the sign operation, which performs a encryption
490 * operation on its input data using the private key. Although this can be
491 * used to verify signatures, CRSAVerifier::VerifyL should be used in
492 * preference. This method is however required by some security protocols.
494 * @param aSignature The signature to be verified
495 * @return A pointer to a new buffer containing the result of the
496 * operation. The pointer is left on the cleanup stack.
498 virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const = 0;
500 IMPORT_C virtual TBool VerifyL(const TDesC8& aInput,
501 const CRSASignature& aSignature) const;
503 /** Default constructor */
504 IMPORT_C CRSAVerifier(void);
506 CRSAVerifier(const CRSAVerifier&);
507 CRSAVerifier& operator=(const CRSAVerifier&);
511 * This class verifies RSA signatures given a message and its supposed
512 * signature. It follows the RSA PKCS#1 v1.5 with PKCS#1 v1.5 padding specification
513 * with the following exception: the VerifyL() function does <b>not</b> hash or
514 * in any way manipulate the input data before checking. Thus in order to verify
515 * RSA signatures in PKCS#1 v1.5 format, the input data needs to follow PKCS#1 v1.5
516 * specification, i.e. be ASN.1 encoded and prefixed by ASN.1 encoded digestId.
519 class CRSAPKCS1v15Verifier : public CRSAVerifier
523 * Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
525 * @param aKey The RSA public key to be used for verifying
526 * @return A pointer to the new CRSAPKCS1v15Verifier object
528 * @leave KErrKeySize If the key length is too small
530 IMPORT_C static CRSAPKCS1v15Verifier* NewL(const CRSAPublicKey& aKey);
533 * Creates a new CRSAPKCS1v15Verifier object from a specified RSA public key.
535 * The returned pointer is put onto the cleanup stack.
537 * @param aKey The RSA public key to be used for verifying
538 * @return A pointer to the new CRSAPKCS1v15Verifier object
540 * @leave KErrKeySize If the key length is too small
542 IMPORT_C static CRSAPKCS1v15Verifier* NewLC(const CRSAPublicKey& aKey);
543 virtual HBufC8* InverseSignLC(const CRSASignature& aSignature) const;
544 virtual TInt MaxInputLength(void) const;
545 virtual TInt MaxOutputLength(void) const;
546 /** The destructor frees all resources owned by the object, prior to its destruction. */
547 virtual ~CRSAPKCS1v15Verifier(void);
550 CRSAPKCS1v15Verifier(const CRSAPublicKey& aKey);
552 void ConstructL(void);
554 /** The RSA public key to be used for verification */
555 const CRSAPublicKey& iPublicKey;
556 /** The PKCS#1 v1.5 signature padding */
557 CPaddingPKCS1Signature* iPadding;
559 CRSAPKCS1v15Verifier(const CRSAPKCS1v15Verifier&);
560 CRSAPKCS1v15Verifier& operator=(const CRSAPKCS1v15Verifier&);
564 * An encapsulation of a DSA signature.
567 class CDSASignature : public CBase
571 * Creates a new CDSASignature object from the specified R and S values.
573 * @param aR The DSA signature's R value
574 * @param aS The DSA signature's S value
575 * @return A pointer to the new CDSASignature object
577 IMPORT_C static CDSASignature* NewL(RInteger& aR, RInteger& aS);
580 * Creates a new CDSASignature object from the specified R and S values.
582 * The returned pointer is put onto the cleanup stack.
584 * @param aR The DSA signature's R value
585 * @param aS The DSA signature's S value
586 * @return A pointer to the new CDSASignature object
588 IMPORT_C static CDSASignature* NewLC(RInteger& aR, RInteger& aS);
591 * Gets the DSA signature's R value
593 * @return The R value
595 IMPORT_C const TInteger& R(void) const;
598 * Gets the DSA signature's S value
600 * @return The S value
602 IMPORT_C const TInteger& S(void) const;
605 * Whether this DSASignature is identical to a specified DSASignature
607 * @param aSig The DSASignature for comparison
608 * @return ETrue, if the two signatures are identical; EFalse, otherwise.
610 IMPORT_C TBool operator== (const CDSASignature& aSig) const;
612 /** The destructor frees all resources owned by the object, prior to its destruction. */
613 IMPORT_C virtual ~CDSASignature(void);
616 * Protected constructor
618 * @param aR The DSA signature's R value
619 * @param aS The DSA signature's S value
621 IMPORT_C CDSASignature(RInteger& aR, RInteger& aS);
623 /** Default constructor */
624 IMPORT_C CDSASignature(void);
626 /** The DSA signature's R value */
628 /** The DSA signature's S value */
631 CDSASignature(const CDSASignature&);
632 CDSASignature& operator=(const CDSASignature&);
636 * Implementation of DSA signing as specified in FIPS 186-2 change request 1.
639 class CDSASigner : public CSigner<CDSASignature>
643 * Creates a new CDSASigner object from a specified DSA private key.
645 * @param aKey The DSA private key to be used for signing
646 * @return A pointer to the new CDSASigner object
648 IMPORT_C static CDSASigner* NewL(const CDSAPrivateKey& aKey);
651 * Creates a new CDSASigner object from a specified DSA private key.
653 * The returned pointer is put onto the cleanup stack.
655 * @param aKey The DSA private key to be used for signing
656 * @return A pointer to the new CDSASigner object
658 IMPORT_C static CDSASigner* NewLC(const CDSAPrivateKey& aKey);
660 * Digitally signs the specified input message
662 * Note that in order to be interoperable and compliant with the DSS, aInput
663 * must be the result of a SHA-1 hash.
665 * @param aInput A SHA-1 hash of the message to sign
666 * @return A pointer to a new CSignature object
668 * @panic ECryptoPanicInputTooLarge If aInput is larger than MaxInputLength(),
669 * which is likely to happen if the caller
670 * has passed in something that has not been hashed.
672 virtual CDSASignature* SignL(const TDesC8& aInput) const;
673 virtual TInt MaxInputLength(void) const;
676 CDSASigner(const CDSAPrivateKey& aKey);
678 /** The DSA private key to be used for signing */
679 const CDSAPrivateKey& iPrivateKey;
681 CDSASigner(const CDSASigner&);
682 CDSASigner& operator=(const CDSASigner&);
686 * Implementation of DSA signature verification as specified in FIPS 186-2 change
690 class CDSAVerifier : public CVerifier<CDSASignature>
694 * Creates a new CDSAVerifier object from a specified DSA public key.
696 * @param aKey The DSA public key to be used for verifying
697 * @return A pointer to the new CDSAVerifier object
699 IMPORT_C static CDSAVerifier* NewL(const CDSAPublicKey& aKey);
702 * Creates a new CDSAVerifier object from a specified DSA public key.
704 * The returned pointer is put onto the cleanup stack.
706 * @param aKey The DSA public key to be used for verifying
707 * @return A pointer to the new CDSAVerifier object
709 IMPORT_C static CDSAVerifier* NewLC(const CDSAPublicKey& aKey);
711 * Verifies the specified digital signature
713 * Note that in order to be interoperable and compliant with the DSS, aInput
714 * must be the result of a SHA-1 hash.
716 * @param aInput A SHA-1 hash of the received message
717 * @param aSignature The signature to be verified
719 * @return Whether the signature is the result of signing
720 * aInput with the supplied key
722 virtual TBool VerifyL(const TDesC8& aInput, const CDSASignature& aSignature) const;
723 virtual TInt MaxInputLength(void) const;
726 CDSAVerifier(const CDSAPublicKey& aKey);
728 /** The DSA public key to be used for verification */
729 const CDSAPublicKey& iPublicKey;
731 CDSAVerifier(const CDSAVerifier&);
732 CDSAVerifier& operator=(const CDSAVerifier&);
736 * Implementation of Diffie-Hellman key agreement as specified in PKCS#3.
739 class CDH : public CBase
743 * Creates a new CDH object from a specified DH private key.
745 * @param aPrivateKey The private key of this party
746 * @return A pointer to the new CDH object
748 IMPORT_C static CDH* NewL(const CDHPrivateKey& aPrivateKey);
751 * Creates a new CDH object from a specified DH private key.
753 * The returned pointer is put onto the cleanup stack.
755 * @param aPrivateKey The private key of this party
756 * @return A pointer to the new CDH object
758 IMPORT_C static CDH* NewLC(const CDHPrivateKey& aPrivateKey);
761 * Performs the key agreement operation.
763 * @param aPublicKey The public key of the other party
764 * @return The agreed key
766 IMPORT_C HBufC8* AgreeL(const CDHPublicKey& aPublicKey) const;
771 * @param aPrivateKey The DH private key
773 IMPORT_C CDH(const CDHPrivateKey& aPrivateKey);
775 /** The DH private key */
776 const CDHPrivateKey& iPrivateKey;
779 CDH& operator=(const CDH&);
782 #endif // __ASYMMETRIC_H__