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