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