os/security/crypto/weakcryptospi/source/asymmetric/rsakeys.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-2009 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 <asymmetrickeys.h>
sl@0
    20
#include "rsakeypairshim.h"
sl@0
    21
#include "../common/inlines.h"
sl@0
    22
sl@0
    23
/* CRSAParameters */
sl@0
    24
sl@0
    25
EXPORT_C const TInteger& CRSAParameters::N(void) const
sl@0
    26
	{
sl@0
    27
	return iN;
sl@0
    28
	}
sl@0
    29
sl@0
    30
EXPORT_C CRSAParameters::~CRSAParameters(void)
sl@0
    31
	{
sl@0
    32
	iN.Close();
sl@0
    33
	}
sl@0
    34
sl@0
    35
EXPORT_C CRSAParameters::CRSAParameters(RInteger& aN) : iN(aN)
sl@0
    36
	{
sl@0
    37
	}
sl@0
    38
sl@0
    39
EXPORT_C CRSAParameters::CRSAParameters(void)
sl@0
    40
	{
sl@0
    41
	}
sl@0
    42
sl@0
    43
/* CRSAPublicKey */
sl@0
    44
sl@0
    45
EXPORT_C CRSAPublicKey* CRSAPublicKey::NewL(RInteger& aN, RInteger& aE)
sl@0
    46
	{
sl@0
    47
	CRSAPublicKey* self = NewLC(aN, aE);
sl@0
    48
	CleanupStack::Pop();
sl@0
    49
	return self;
sl@0
    50
	}
sl@0
    51
sl@0
    52
EXPORT_C CRSAPublicKey* CRSAPublicKey::NewLC(RInteger& aN, RInteger& aE)
sl@0
    53
	{
sl@0
    54
	CRSAPublicKey* self = new(ELeave) CRSAPublicKey(aN, aE);
sl@0
    55
	CleanupStack::PushL(self);
sl@0
    56
	self->ConstructL();
sl@0
    57
	return self;
sl@0
    58
	}
sl@0
    59
sl@0
    60
sl@0
    61
void CRSAPublicKey::ConstructL()
sl@0
    62
	{
sl@0
    63
	// Check that the modulus and exponent are positive integers 
sl@0
    64
	// as specified by RSA
sl@0
    65
	if(!N().IsPositive() || !E().IsPositive() || (E() <= 1))
sl@0
    66
		{
sl@0
    67
		// If we need to leave during construction we must release ownership
sl@0
    68
		// of the RInteger parameters that were passed in.
sl@0
    69
		// These parameters should be on the cleanup stack so if we don't 
sl@0
    70
		// release ownership they will be deleted twice, causing a panic
sl@0
    71
		iN = RInteger();
sl@0
    72
		iE = RInteger();
sl@0
    73
		User::Leave(KErrArgument);
sl@0
    74
		}
sl@0
    75
	}
sl@0
    76
sl@0
    77
sl@0
    78
EXPORT_C const TInteger& CRSAPublicKey::E(void) const
sl@0
    79
	{
sl@0
    80
	return iE;
sl@0
    81
	}
sl@0
    82
sl@0
    83
EXPORT_C CRSAPublicKey::CRSAPublicKey()
sl@0
    84
	{
sl@0
    85
	}
sl@0
    86
sl@0
    87
EXPORT_C CRSAPublicKey::CRSAPublicKey(RInteger& aN, RInteger& aE)
sl@0
    88
	: CRSAParameters(aN), iE(aE)
sl@0
    89
	{
sl@0
    90
	}
sl@0
    91
sl@0
    92
EXPORT_C CRSAPublicKey::~CRSAPublicKey(void)
sl@0
    93
	{
sl@0
    94
	iE.Close();
sl@0
    95
	}
sl@0
    96
sl@0
    97
/* CRSAPrivateKeyType */
sl@0
    98
sl@0
    99
CRSAPrivateKey::CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN)
sl@0
   100
:	CRSAParameters(aN), iKeyType(aKeyType)
sl@0
   101
{}
sl@0
   102
sl@0
   103
sl@0
   104
/* CRSAPrivateKeyStandard */
sl@0
   105
sl@0
   106
EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewL(RInteger& aN, 
sl@0
   107
	RInteger& aD)
sl@0
   108
	{
sl@0
   109
	CRSAPrivateKeyStandard* self = NewLC(aN, aD);
sl@0
   110
	CleanupStack::Pop();
sl@0
   111
	return self;
sl@0
   112
	}
sl@0
   113
sl@0
   114
EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewLC(RInteger& aN,
sl@0
   115
	RInteger& aD)
sl@0
   116
	{
sl@0
   117
	CRSAPrivateKeyStandard* self = new(ELeave) CRSAPrivateKeyStandard(aN, aD);
sl@0
   118
	CleanupStack::PushL(self);
sl@0
   119
	self->ConstructL();
sl@0
   120
	return self;
sl@0
   121
	}
sl@0
   122
sl@0
   123
void CRSAPrivateKeyStandard::ConstructL()
sl@0
   124
	{
sl@0
   125
	// Check that the modulus and exponent are positive integers
sl@0
   126
	if(!N().IsPositive() || !D().IsPositive() || (D() <= 1))
sl@0
   127
		{
sl@0
   128
		// If we need to leave during construction we must release ownership
sl@0
   129
		// of the RInteger parameters that were passed in.
sl@0
   130
		// These parameters should be on the cleanup stack so if we don't 
sl@0
   131
		// release ownership they will be deleted twice, causing a panic
sl@0
   132
		iN = RInteger();
sl@0
   133
		iD = RInteger();
sl@0
   134
		User::Leave(KErrArgument);
sl@0
   135
		}
sl@0
   136
	}
sl@0
   137
sl@0
   138
EXPORT_C const TInteger& CRSAPrivateKeyStandard::D(void) const
sl@0
   139
	{
sl@0
   140
	return iD;
sl@0
   141
	}
sl@0
   142
sl@0
   143
EXPORT_C CRSAPrivateKeyStandard::CRSAPrivateKeyStandard(RInteger& aN, 
sl@0
   144
	RInteger& aD) : CRSAPrivateKey(EStandard, aN), iD(aD)
sl@0
   145
	{
sl@0
   146
	}
sl@0
   147
sl@0
   148
EXPORT_C CRSAPrivateKeyStandard::~CRSAPrivateKeyStandard()
sl@0
   149
	{	
sl@0
   150
	iD.Close();
sl@0
   151
	}
sl@0
   152
sl@0
   153
/* CRSAPrivateKeyCRT */
sl@0
   154
sl@0
   155
EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewL(RInteger& aN, RInteger& aP,
sl@0
   156
	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
sl@0
   157
	{
sl@0
   158
	CRSAPrivateKeyCRT* self = NewLC(aN, aP, aQ, aDP, aDQ, aQInv);
sl@0
   159
	CleanupStack::Pop();
sl@0
   160
	return self;
sl@0
   161
	}
sl@0
   162
sl@0
   163
EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewLC(RInteger& aN, RInteger& aP, 
sl@0
   164
	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
sl@0
   165
	{
sl@0
   166
	CRSAPrivateKeyCRT* self = new(ELeave) CRSAPrivateKeyCRT(aN, aP, aQ, 
sl@0
   167
		aDP, aDQ, aQInv);
sl@0
   168
	CleanupStack::PushL(self);
sl@0
   169
	self->ConstructL();
sl@0
   170
	return self;
sl@0
   171
	}
sl@0
   172
sl@0
   173
EXPORT_C CRSAPrivateKeyCRT::CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP, 
sl@0
   174
	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv) 
sl@0
   175
	: CRSAPrivateKey(EStandardCRT, aN), iP(aP), iQ(aQ), iDP(aDP), iDQ(aDQ), 
sl@0
   176
		iQInv(aQInv)
sl@0
   177
	{
sl@0
   178
	}
sl@0
   179
sl@0
   180
void CRSAPrivateKeyCRT::ConstructL()
sl@0
   181
	{
sl@0
   182
	// Check that all parameters are positive integers
sl@0
   183
	if(!P().IsPositive() || !Q().IsPositive() || !DP().IsPositive() 
sl@0
   184
		|| !DQ().IsPositive() || !QInv().IsPositive())
sl@0
   185
		{
sl@0
   186
		// If we need to leave during construction we must release ownership
sl@0
   187
		// of the RInteger parameters that were passed in.
sl@0
   188
		// These parameters should be on the cleanup stack so if we don't 
sl@0
   189
		// release ownership they will be deleted twice, causing a panic
sl@0
   190
		iN = RInteger();
sl@0
   191
		iP = RInteger();
sl@0
   192
		iQ = RInteger();
sl@0
   193
		iDP = RInteger();
sl@0
   194
		iDQ = RInteger();
sl@0
   195
		iQInv = RInteger();
sl@0
   196
		User::Leave(KErrArgument);
sl@0
   197
		}
sl@0
   198
	}
sl@0
   199
sl@0
   200
sl@0
   201
EXPORT_C CRSAPrivateKeyCRT::~CRSAPrivateKeyCRT()
sl@0
   202
	{	
sl@0
   203
	iP.Close();
sl@0
   204
	iQ.Close();
sl@0
   205
	iDP.Close();
sl@0
   206
	iDQ.Close();
sl@0
   207
	iQInv.Close();
sl@0
   208
	}
sl@0
   209
sl@0
   210
EXPORT_C const TInteger& CRSAPrivateKeyCRT::P(void) const
sl@0
   211
	{
sl@0
   212
	return iP;
sl@0
   213
	}
sl@0
   214
sl@0
   215
EXPORT_C const TInteger& CRSAPrivateKeyCRT::Q(void) const
sl@0
   216
	{
sl@0
   217
	return iQ;
sl@0
   218
	}
sl@0
   219
sl@0
   220
EXPORT_C const TInteger& CRSAPrivateKeyCRT::DP(void) const
sl@0
   221
	{
sl@0
   222
	return iDP;
sl@0
   223
	}
sl@0
   224
sl@0
   225
EXPORT_C const TInteger& CRSAPrivateKeyCRT::DQ(void) const
sl@0
   226
	{
sl@0
   227
	return iDQ;
sl@0
   228
	}
sl@0
   229
sl@0
   230
EXPORT_C const TInteger& CRSAPrivateKeyCRT::QInv(void) const
sl@0
   231
	{
sl@0
   232
	return iQInv;
sl@0
   233
	}
sl@0
   234
sl@0
   235
/* CRSAKeyPair */
sl@0
   236
sl@0
   237
EXPORT_C CRSAKeyPair* CRSAKeyPair::NewL(TUint aModulusBits, 
sl@0
   238
	TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
sl@0
   239
	{
sl@0
   240
	CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
sl@0
   241
	CleanupStack::Pop();
sl@0
   242
	return self;
sl@0
   243
	}
sl@0
   244
sl@0
   245
EXPORT_C CRSAKeyPair* CRSAKeyPair::NewLC(TUint aModulusBits, 
sl@0
   246
	TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
sl@0
   247
	{
sl@0
   248
	CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
sl@0
   249
	return self;
sl@0
   250
	}
sl@0
   251
sl@0
   252
EXPORT_C const CRSAPublicKey& CRSAKeyPair::PublicKey(void) const
sl@0
   253
	{
sl@0
   254
	return *iPublic;
sl@0
   255
	}
sl@0
   256
	
sl@0
   257
EXPORT_C const CRSAPrivateKey& CRSAKeyPair::PrivateKey(void) const
sl@0
   258
	{
sl@0
   259
	return *iPrivate;
sl@0
   260
	}
sl@0
   261
sl@0
   262
EXPORT_C CRSAKeyPair::~CRSAKeyPair(void)
sl@0
   263
	{
sl@0
   264
	delete iPublic;
sl@0
   265
	delete iPrivate;
sl@0
   266
	}
sl@0
   267
sl@0
   268
EXPORT_C CRSAKeyPair::CRSAKeyPair(void)
sl@0
   269
	{
sl@0
   270
	}
sl@0
   271