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