os/security/crypto/weakcryptospi/source/asymmetric/dhkeys.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 "dhkeypairshim.h"
sl@0
    21
sl@0
    22
sl@0
    23
/* CDHParameters */
sl@0
    24
EXPORT_C const TInteger& CDHParameters::N(void) const
sl@0
    25
	{
sl@0
    26
	return iN;
sl@0
    27
	}
sl@0
    28
sl@0
    29
EXPORT_C const TInteger& CDHParameters::G(void) const
sl@0
    30
	{
sl@0
    31
	return iG;
sl@0
    32
	}
sl@0
    33
sl@0
    34
EXPORT_C CDHParameters::~CDHParameters(void)
sl@0
    35
	{
sl@0
    36
	iN.Close();
sl@0
    37
	iG.Close();
sl@0
    38
	}
sl@0
    39
sl@0
    40
EXPORT_C CDHParameters::CDHParameters(RInteger& aN, RInteger& aG) : iN(aN), iG(aG)
sl@0
    41
	{
sl@0
    42
	}
sl@0
    43
sl@0
    44
/* CDHPublicKey */
sl@0
    45
EXPORT_C CDHPublicKey* CDHPublicKey::NewL(RInteger& aN, RInteger& aG, 
sl@0
    46
	RInteger& aX)
sl@0
    47
	{
sl@0
    48
	CDHPublicKey* self = new(ELeave) CDHPublicKey(aN, aG, aX);
sl@0
    49
	return self;
sl@0
    50
	}
sl@0
    51
sl@0
    52
EXPORT_C CDHPublicKey* CDHPublicKey::NewLC(RInteger& aN, RInteger& aG, 
sl@0
    53
	RInteger& aX)
sl@0
    54
	{
sl@0
    55
	CDHPublicKey* self = NewL(aN, aG, aX);
sl@0
    56
	CleanupStack::PushL(self);
sl@0
    57
	return self;
sl@0
    58
	}
sl@0
    59
sl@0
    60
EXPORT_C const TInteger& CDHPublicKey::X(void) const
sl@0
    61
	{
sl@0
    62
	return iX;
sl@0
    63
	}
sl@0
    64
sl@0
    65
EXPORT_C CDHPublicKey::CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX)
sl@0
    66
	: CDHParameters(aN, aG), iX(aX)
sl@0
    67
	{
sl@0
    68
	}
sl@0
    69
sl@0
    70
EXPORT_C CDHPublicKey::~CDHPublicKey(void)
sl@0
    71
	{
sl@0
    72
	iX.Close();
sl@0
    73
	}
sl@0
    74
sl@0
    75
/* CDHPrivateKey */
sl@0
    76
EXPORT_C CDHPrivateKey* CDHPrivateKey::NewL(RInteger& aN, RInteger& aG, 
sl@0
    77
	RInteger& ax)
sl@0
    78
	{
sl@0
    79
	CDHPrivateKey* self = new(ELeave) CDHPrivateKey(aN, aG, ax);
sl@0
    80
	return self;
sl@0
    81
	}
sl@0
    82
sl@0
    83
EXPORT_C CDHPrivateKey* CDHPrivateKey::NewLC(RInteger& aN, RInteger& aG, 
sl@0
    84
	RInteger& ax)
sl@0
    85
	{
sl@0
    86
	CDHPrivateKey* self = NewL(aN, aG, ax);
sl@0
    87
	CleanupStack::PushL(self);
sl@0
    88
	return self;
sl@0
    89
	}
sl@0
    90
sl@0
    91
EXPORT_C const TInteger& CDHPrivateKey::x(void) const
sl@0
    92
	{
sl@0
    93
	return ix;
sl@0
    94
	}
sl@0
    95
sl@0
    96
EXPORT_C CDHPrivateKey::CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax)
sl@0
    97
	: CDHParameters(aN, aG), ix(ax)
sl@0
    98
	{
sl@0
    99
	}
sl@0
   100
sl@0
   101
EXPORT_C CDHPrivateKey::~CDHPrivateKey(void)
sl@0
   102
	{
sl@0
   103
	ix.Close();
sl@0
   104
	}
sl@0
   105
sl@0
   106
/* CDHKeyPair */
sl@0
   107
sl@0
   108
EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG)
sl@0
   109
	{
sl@0
   110
	CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG);
sl@0
   111
	CleanupStack::Pop(self);
sl@0
   112
	return self;
sl@0
   113
	}
sl@0
   114
sl@0
   115
EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG)
sl@0
   116
	{
sl@0
   117
	CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG);
sl@0
   118
	return self;
sl@0
   119
	}
sl@0
   120
sl@0
   121
EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG, RInteger& ax)
sl@0
   122
	{
sl@0
   123
	CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax);
sl@0
   124
	CleanupStack::Pop(self);
sl@0
   125
	return self;
sl@0
   126
	}
sl@0
   127
sl@0
   128
EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
sl@0
   129
	{
sl@0
   130
	CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax);
sl@0
   131
	return self;
sl@0
   132
	}
sl@0
   133
sl@0
   134
EXPORT_C const CDHPublicKey& CDHKeyPair::PublicKey(void) const
sl@0
   135
	{
sl@0
   136
	return *iPublic;
sl@0
   137
	}
sl@0
   138
sl@0
   139
EXPORT_C const CDHPrivateKey& CDHKeyPair::PrivateKey(void) const
sl@0
   140
	{
sl@0
   141
	return *iPrivate;
sl@0
   142
	}
sl@0
   143
sl@0
   144
EXPORT_C CDHKeyPair::~CDHKeyPair(void)
sl@0
   145
	{
sl@0
   146
	delete iPublic;
sl@0
   147
	delete iPrivate;
sl@0
   148
	}
sl@0
   149
sl@0
   150
EXPORT_C CDHKeyPair::CDHKeyPair(void)
sl@0
   151
	{
sl@0
   152
	}
sl@0
   153
sl@0
   154
// Excluded from coverage due to shim replacements.
sl@0
   155
#ifdef _BullseyeCoverage
sl@0
   156
#pragma suppress_warnings on
sl@0
   157
#pragma BullseyeCoverage off
sl@0
   158
#pragma suppress_warnings off
sl@0
   159
#endif
sl@0
   160
EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG)
sl@0
   161
	{
sl@0
   162
	//declaring a reference just for clarity in NewRandomL statement
sl@0
   163
	RInteger& nminus2 = aN;
sl@0
   164
	--nminus2;
sl@0
   165
	--nminus2;
sl@0
   166
sl@0
   167
	//find a random x | 1 <= x <= n-2
sl@0
   168
	RInteger x = RInteger::NewRandomL(TInteger::One(), nminus2);
sl@0
   169
	CleanupStack::PushL(x);
sl@0
   170
	++nminus2;
sl@0
   171
	++nminus2; // reincrement aN
sl@0
   172
sl@0
   173
	ConstructL(aN, aG, x);
sl@0
   174
sl@0
   175
	CleanupStack::Pop(&x);
sl@0
   176
	}
sl@0
   177
sl@0
   178
EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
sl@0
   179
	{
sl@0
   180
	//declaring a reference just for clarity in if statements
sl@0
   181
	RInteger& nminus2 = aN;
sl@0
   182
	--nminus2;
sl@0
   183
	--nminus2;
sl@0
   184
	
sl@0
   185
	if( aG < TInteger::Two() || aG > nminus2 )
sl@0
   186
		{
sl@0
   187
		User::Leave(KErrArgument);
sl@0
   188
		}
sl@0
   189
	//In the case of the other ConstructL calling this function this if
sl@0
   190
	//statement is redundant.  However, we need to check as this is can be
sl@0
   191
	//called without going through the other api.
sl@0
   192
	if( ax < TInteger::One() || ax > nminus2 )
sl@0
   193
		{
sl@0
   194
		User::Leave(KErrArgument);
sl@0
   195
		}
sl@0
   196
sl@0
   197
	++nminus2;
sl@0
   198
	++nminus2; // reincrement aN
sl@0
   199
sl@0
   200
	// Calculate X = g^(x) mod n; (note the case sensitivity)
sl@0
   201
	RInteger X = TInteger::ModularExponentiateL(aG, ax, aN);
sl@0
   202
	CleanupStack::PushL(X);
sl@0
   203
sl@0
   204
	RInteger n1 = RInteger::NewL(aN);
sl@0
   205
	CleanupStack::PushL(n1);
sl@0
   206
	RInteger g1 = RInteger::NewL(aG);
sl@0
   207
	CleanupStack::PushL(g1);
sl@0
   208
	iPublic = CDHPublicKey::NewL(n1, g1, X);
sl@0
   209
	CleanupStack::Pop(3, &X); // g1, n1, X all owned by iPublic
sl@0
   210
	
sl@0
   211
	iPrivate = CDHPrivateKey::NewL(aN, aG, ax);
sl@0
   212
	}
sl@0
   213
sl@0
   214
// Unused exported and protected members. So, exclude them from coverage.
sl@0
   215
EXPORT_C CDHParameters::CDHParameters(void)
sl@0
   216
	{
sl@0
   217
	}
sl@0
   218
sl@0
   219
EXPORT_C CDHPrivateKey::CDHPrivateKey(void)
sl@0
   220
	{
sl@0
   221
	}
sl@0
   222
sl@0
   223
EXPORT_C CDHPublicKey::CDHPublicKey(void)
sl@0
   224
	{
sl@0
   225
	}