os/security/crypto/weakcryptospi/source/asymmetric/dsashim.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2010 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 *
    16 */
    17 
    18 
    19 #include "dsashim.h"
    20 #include <cryptospi/cryptosignatureapi.h>
    21 #include <cryptospi/cryptospidef.h>
    22 #include <cryptospi/keys.h>
    23 #include "keyconverter.h"
    24 
    25 #include "../common/inlines.h"
    26 
    27 
    28 using namespace CryptoSpi;
    29 
    30 // CDsaSignerShim ////////////////////////////////////////////////////////
    31 
    32 CDsaSignerShim* CDsaSignerShim::NewL(const CDSAPrivateKey& aKey)
    33 	{
    34 	CDsaSignerShim* self = CDsaSignerShim::NewLC(aKey);
    35 	CleanupStack::Pop(self);
    36 	return self;
    37 	}
    38 
    39 CDsaSignerShim* CDsaSignerShim::NewLC(const CDSAPrivateKey& aKey)
    40 	{
    41 	CDsaSignerShim* self = new (ELeave) CDsaSignerShim(aKey);
    42 	CleanupStack::PushL(self);
    43 	self->ConstructL(aKey);
    44 	return self;
    45 	}
    46 
    47 CDsaSignerShim::CDsaSignerShim(const CDSAPrivateKey& aKey)
    48 	: CDSASigner(aKey)
    49 	{
    50 	}
    51 
    52 CDsaSignerShim::~CDsaSignerShim()
    53 	{
    54 	delete iSignerImpl;
    55 	delete iKey;
    56 	}
    57 
    58 void CDsaSignerShim::ConstructL(const CDSAPrivateKey& aKey)
    59 	{
    60 	iKey = KeyConverter::CreateKeyL(aKey);
    61 	CSignatureFactory::CreateSignerL(
    62 									iSignerImpl,
    63 									KDsaSignerUid,
    64 									*iKey,
    65 									KPaddingModeNoneUid,
    66 									NULL);
    67 	}
    68 
    69 
    70 CDSASignature* CDsaSignerShim::SignL(const TDesC8& aInput) const
    71 	{
    72 	//Sign the input data
    73 	CCryptoParams* signature = CCryptoParams::NewLC();
    74 	iSignerImpl->SignL(aInput, *signature);
    75 	
    76 	//Retrieve the R&S in DSA signature from the array 
    77 	const TInteger& cR=signature->GetBigIntL(KDsaSignatureParameterRUid);
    78 	const TInteger& cS=signature->GetBigIntL(KDsaSignatureParameterSUid);
    79 	
    80 	
    81 	//Make copies of the DSA signature
    82 	RInteger r=RInteger::NewL(cR);
    83 	CleanupClosePushL(r);
    84 	RInteger s=RInteger::NewL(cS);
    85 	CleanupClosePushL(s);	
    86 	
    87 	//Create the DSA signature object, the ownership of r&s is transfered to dsaSig
    88 	CDSASignature* dsaSig=CDSASignature::NewL(r, s);
    89 	
    90 	//Cleanup
    91 	CleanupStack::Pop(2, &r); 	
    92 	CleanupStack::PopAndDestroy(signature);
    93 	
    94 	return dsaSig;
    95 	}
    96 
    97 TInt CDsaSignerShim::MaxInputLength() const
    98 	{
    99 	TInt maxInputLength=0; 
   100 	TRAPD(err, maxInputLength=iSignerImpl->GetMaximumInputLengthL())
   101 	if (err==KErrNone)
   102 		{
   103 		return maxInputLength;
   104 		}
   105 	else
   106 		{
   107 		return err;
   108 		}
   109 	}
   110 
   111 
   112 // CDsaVerifierShim ////////////////////////////////////////////////////////
   113 CDsaVerifierShim* CDsaVerifierShim::NewL(const CDSAPublicKey& aKey)
   114 	{
   115 	CDsaVerifierShim* self = CDsaVerifierShim::NewLC(aKey);
   116 	CleanupStack::Pop(self);
   117 	return self;
   118 	}
   119 
   120 CDsaVerifierShim* CDsaVerifierShim::NewLC(const CDSAPublicKey& aKey)
   121 	{
   122 	CDsaVerifierShim* self = new (ELeave) CDsaVerifierShim(aKey);
   123 	CleanupStack::PushL(self);
   124 	self->ConstructL(aKey);
   125 	return self;
   126 	}
   127 
   128 CDsaVerifierShim::CDsaVerifierShim(const CDSAPublicKey& aKey)
   129 	: CDSAVerifier(aKey)
   130 	{
   131 	}
   132 
   133 CDsaVerifierShim::~CDsaVerifierShim()
   134 	{
   135 	delete iVerifierImpl;
   136 	delete iKey;
   137 	}
   138 
   139 void CDsaVerifierShim::ConstructL(const CDSAPublicKey& aKey)
   140 	{
   141 	iKey = KeyConverter::CreateKeyL(aKey);
   142 	CSignatureFactory::CreateVerifierL(
   143 									iVerifierImpl,
   144 									KDsaVerifierUid,
   145 									*iKey,
   146 									KPaddingModeNoneUid,
   147 									NULL);
   148 											
   149 	}
   150 
   151 TBool CDsaVerifierShim::VerifyL(const TDesC8& aInput, const CDSASignature& aSignature) const
   152 	{
   153 	//create the array format dsa signature for the new crypto spi
   154 	CCryptoParams* dsaSig = CCryptoParams::NewLC();
   155 
   156 	dsaSig->AddL(aSignature.R(), KDsaSignatureParameterRUid);
   157 	dsaSig->AddL(aSignature.S(), KDsaSignatureParameterSUid);
   158 
   159 	//pass the signature and input to the new crypto spi to be verified
   160 	TBool verificationResult=EFalse;
   161 	iVerifierImpl->VerifyL(aInput, *dsaSig, verificationResult);
   162 
   163 	//Cleanup the array
   164 	CleanupStack::PopAndDestroy(dsaSig);
   165 
   166 	return verificationResult;
   167 	}
   168 
   169 TInt CDsaVerifierShim::MaxInputLength() const
   170 	{
   171 	TInt maxInputLength=0; 
   172 	TRAPD(err, maxInputLength=iVerifierImpl->GetMaximumInputLengthL())
   173 	if (err==KErrNone)
   174 		{
   175 		return maxInputLength;
   176 		}
   177 	else
   178 		{
   179 		return err;
   180 		}
   181 	}
   182