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