First public contribution.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include <asymmetrickeys.h>
22 #include <cryptospi/cryptospidef.h>
23 #include <cryptospi/cryptoparams.h>
24 #include "rsafunction.h"
27 using namespace CryptoSpi;
30 void RSAFunction::EncryptL(const CKey& aPublicKey,
31 const TInteger& aInput, RInteger& aOutput)
33 const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid);
34 const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid);
35 FunctionL(N, E, aInput, aOutput);
39 void RSAFunction::DecryptL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput)
41 if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid)
43 const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
44 const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid);
45 FunctionL(N, D, aInput, aOutput);
47 else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid)
49 FunctionCRTL(aPrivateKey, aInput, aOutput);
53 User::Leave(KErrNotSupported);
58 void RSAFunction::SignL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput)
60 if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid)
62 const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
63 const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid);
64 FunctionL(N, D, aInput, aOutput);
66 else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid)
68 FunctionCRTL(aPrivateKey, aInput, aOutput);
72 User::Leave(KErrNotSupported);
77 void RSAFunction::VerifyL(const CKey& aPublicKey,
78 const TInteger& aInput, RInteger& aOutput)
80 const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid);
81 const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid);
82 FunctionL(N, E, aInput, aOutput);
85 // The RSA Trapdoor Function
86 void RSAFunction::FunctionL(const TInteger& aModulus, const TInteger& aExponent,
87 const TInteger& aBase, RInteger& aOutput)
89 IsInputValidL(aBase, aModulus);
91 aOutput = TInteger::ModularExponentiateL(aBase, aExponent, aModulus);
94 // The CRT version of the RSA Trapdoor Function
95 void RSAFunction::FunctionCRTL(const CKey& aPrivateKey,
96 const TInteger& aInput, RInteger& aOutput)
98 const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
99 IsInputValidL(aInput, N);
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);
107 CMontgomeryStructure* montP = CMontgomeryStructure::NewLC(P);
108 CMontgomeryStructure* montQ = CMontgomeryStructure::NewLC(Q);
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);
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);
123 // h = (m1-m2) qInv mod(p)
124 RInteger h = m1.MinusL(m2);
125 CleanupStack::PushL(h);
134 CleanupStack::Pop(&h);
136 CleanupStack::PopAndDestroy(montQ);
137 CleanupStack::PopAndDestroy(montP);