os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsafunction.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.
     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 <bigint.h>
    20 #include "keys.h"
    21 #include <asymmetrickeys.h>
    22 #include <cryptospi/cryptospidef.h>
    23 #include <cryptospi/cryptoparams.h>
    24 #include "rsafunction.h"
    25 #include "mont.h"
    26 
    27 using namespace CryptoSpi;
    28 
    29 // Public Encrypt
    30 void RSAFunction::EncryptL(const CKey& aPublicKey,
    31 	const TInteger& aInput, RInteger& aOutput)
    32 	{
    33 	const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid);
    34 	const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid);
    35 	FunctionL(N, E, aInput, aOutput);
    36 	}
    37 
    38 // Private Decrypt
    39 void RSAFunction::DecryptL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput)
    40 	{
    41 	if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid)
    42 		{
    43 		const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
    44 		const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid);
    45 		FunctionL(N, D, aInput, aOutput);
    46 		}
    47 	else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid)
    48 		{
    49 		FunctionCRTL(aPrivateKey, aInput, aOutput);
    50 		}
    51 	else
    52 		{
    53 		User::Leave(KErrNotSupported);
    54 		}
    55 	}
    56 
    57 // Private Encrypt
    58 void RSAFunction::SignL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput)
    59 	{
    60 	if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid)
    61 		{
    62 		const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
    63 		const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid);
    64 		FunctionL(N, D, aInput, aOutput);
    65 		}
    66 	else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid)
    67 		{
    68 		FunctionCRTL(aPrivateKey, aInput, aOutput);
    69 		}
    70 	else
    71 	{
    72 		User::Leave(KErrNotSupported);
    73 	}
    74 }
    75 
    76 // Public Decrypt
    77 void RSAFunction::VerifyL(const CKey& aPublicKey,
    78 	const TInteger& aInput, RInteger& aOutput)
    79 	{
    80 	const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid);
    81 	const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid);
    82 	FunctionL(N, E, aInput, aOutput);
    83 	}
    84 	
    85 // The RSA Trapdoor Function
    86 void RSAFunction::FunctionL(const TInteger& aModulus, const TInteger& aExponent, 
    87 							 const TInteger& aBase, RInteger& aOutput)
    88 	{
    89 	IsInputValidL(aBase, aModulus);
    90 
    91 	aOutput = TInteger::ModularExponentiateL(aBase, aExponent, aModulus);
    92 	}
    93 
    94 // The CRT version of the RSA Trapdoor Function
    95 void RSAFunction::FunctionCRTL(const CKey& aPrivateKey,
    96 								const TInteger& aInput, RInteger& aOutput)
    97 	{
    98 	const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
    99 	IsInputValidL(aInput, N);
   100 
   101 	const TInteger& P = aPrivateKey.GetBigIntL(KRsaKeyParameterPUid);
   102 	const TInteger& Q = aPrivateKey.GetBigIntL(KRsaKeyParameterQUid);
   103 	const TInteger& DP = aPrivateKey.GetBigIntL(KRsaKeyParameterDPUid);
   104 	const TInteger& DQ = aPrivateKey.GetBigIntL(KRsaKeyParameterDQUid);
   105 	const TInteger& QInv = aPrivateKey.GetBigIntL(KRsaKeyParameterQInvUid);
   106 
   107 	CMontgomeryStructure* montP = CMontgomeryStructure::NewLC(P);
   108 	CMontgomeryStructure* montQ = CMontgomeryStructure::NewLC(Q);
   109 	
   110 	// m1 = c^(dP) mod(p)
   111 	RInteger inputReduced = aInput.ModuloL(P);
   112 	CleanupStack::PushL(inputReduced);
   113 	const TInteger& m1 = montP->ExponentiateL(inputReduced, DP);
   114 	CleanupStack::PopAndDestroy(&inputReduced);
   115 
   116 	// m2 = c^(dQ) mod(Q)
   117 	inputReduced = aInput.ModuloL(Q);
   118 	CleanupStack::PushL(inputReduced);
   119 	const TInteger& m2 = montQ->ExponentiateL(inputReduced, DQ);
   120 	CleanupStack::PopAndDestroy(&inputReduced);
   121 	
   122 	// Calculate CRT
   123 	// h = (m1-m2) qInv mod(p)
   124 	RInteger h = m1.MinusL(m2);
   125 	CleanupStack::PushL(h);
   126 	h *= QInv;
   127 	h %= P;
   128 
   129 	// m = m2 + q * h
   130 	h *= Q;
   131 	h += m2;
   132 
   133 	aOutput = h;
   134 	CleanupStack::Pop(&h);
   135 
   136 	CleanupStack::PopAndDestroy(montQ);
   137 	CleanupStack::PopAndDestroy(montP);
   138 	}