Signing and verification

The following example demonstrates signing and verifying with DSA keys. It generates a DSA key pair from the factory, signs some randomly generated data using the private key and then verifies the signature using the public key.

#include <legacyselector.h>
#include <cryptosignatureapi.h>
#include <cryptokeypairgeneratorapi.h>
#include <random.h>
#include <keypair.h>

using namespace CryptoSpi;

//Get a DSA key pair generator
CKeyPairGenerator* keyPairGeneratorImpl = NULL;

TRAPD(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL
        (keyPairGeneratorImpl,
        KDSAKeyPairGeneratorAlgorithmUid,
        NULL));

if(keyPairGeneratorImpl && (err == KErrNone))
    {
    CleanupStack::PushL(keyPairGeneratorImpl);
    CCryptoParams* keyParameters = CCryptoParams::NewLC();

    //Create a DSA key pair
    CKeyPair* keyPair = NULL;
                
    //Hardcode the keybit to 512
    TRAP(err,keyPairGeneratorImpl->GenerateKeyPairL
        (512,
        *keyParameters,
        keyPair));
            
    if(keyPair && (err == KErrNone))
        {
        CleanupStack::PushL(keyPair);
                
        //Create a pointer for the signing implementation object
        CSigner* signerImpl = NULL;
                
        //Create a signer implementation object using the private key
        const CKey& privateKey = keyPair->PrivateKey();
        TRAP(err,CSignatureFactory::CreateSignerL(
             signerImpl,
             KDsaSignerUid,
             privateKey,
             KPaddingModeNoneUid,
             NULL));
                
        if(!signerImpl || (err != KErrNone))
            {
            User::Panic(_L("Signer/Verifier"),err);
            }
        else
            {
            CleanupStack::PushL(signerImpl);
                
            //Create the object to hold the signature
            CCryptoParams* signature = CCryptoParams::NewLC();

            //Generate random data; 128 bytes max length and 128 bytes long
            TBuf8<128> message(128); 
            TRandom::RandomL(message);

            //Sign the random data
            //On return, second parameter contains the signature
            TRAP(err,signerImpl->SignL(message, *signature));
            
            if(err != KErrNone)
                {
                User::Panic(_L("Signature using private key"),err);
                }

            //Now verify the signature using the public key
            CVerifier* verifierImpl = NULL;
                    
            const CKey& publicKey = keyPair->PublicKey();

            // Create a verification object from the factory
            TRAP(err,CSignatureFactory::CreateVerifierL(verifierImpl,
                 KDsaVerifierUid,
                 publicKey,
                 KPaddingModeNoneUid,
                 NULL));
                        
            if (!verifierImpl || (err != KErrNone))
                {
                User::Panic(_L("Signer/Verifier"),err);
                }
            else
                {
                CleanupStack::PushL(verifierImpl);

                TBool verifyResult = EFalse;    
                        
                //Verify the signature using the public key
                verifierImpl->VerifyL(message, *signature, verifyResult);

                if (verifyResult == EFalse)
                    {
                    User::Panic(_L("Signer/Verifier"),err);
                    }
                            
                CleanupStack::PopAndDestroy(verifierImpl);
                }
                        
            CleanupStack::PopAndDestroy(signature);
            CleanupStack::PopAndDestroy(signerImpl);
            }
                    
        CleanupStack::PopAndDestroy(keyPair);                    
        }
            
    CleanupStack::PopAndDestroy(keyParameters);    
    CleanupStack::PopAndDestroy(keyPairGeneratorImpl);    
    }