sl@0: /* sl@0: * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include "keys.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include "rsafunction.h" sl@0: #include "mont.h" sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: // Public Encrypt sl@0: void RSAFunction::EncryptL(const CKey& aPublicKey, sl@0: const TInteger& aInput, RInteger& aOutput) sl@0: { sl@0: const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid); sl@0: const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid); sl@0: FunctionL(N, E, aInput, aOutput); sl@0: } sl@0: sl@0: // Private Decrypt sl@0: void RSAFunction::DecryptL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput) sl@0: { sl@0: if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid) sl@0: { sl@0: const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid); sl@0: const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid); sl@0: FunctionL(N, D, aInput, aOutput); sl@0: } sl@0: else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid) sl@0: { sl@0: FunctionCRTL(aPrivateKey, aInput, aOutput); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: sl@0: // Private Encrypt sl@0: void RSAFunction::SignL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput) sl@0: { sl@0: if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid) sl@0: { sl@0: const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid); sl@0: const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid); sl@0: FunctionL(N, D, aInput, aOutput); sl@0: } sl@0: else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid) sl@0: { sl@0: FunctionCRTL(aPrivateKey, aInput, aOutput); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: sl@0: // Public Decrypt sl@0: void RSAFunction::VerifyL(const CKey& aPublicKey, sl@0: const TInteger& aInput, RInteger& aOutput) sl@0: { sl@0: const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid); sl@0: const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid); sl@0: FunctionL(N, E, aInput, aOutput); sl@0: } sl@0: sl@0: // The RSA Trapdoor Function sl@0: void RSAFunction::FunctionL(const TInteger& aModulus, const TInteger& aExponent, sl@0: const TInteger& aBase, RInteger& aOutput) sl@0: { sl@0: IsInputValidL(aBase, aModulus); sl@0: sl@0: aOutput = TInteger::ModularExponentiateL(aBase, aExponent, aModulus); sl@0: } sl@0: sl@0: // The CRT version of the RSA Trapdoor Function sl@0: void RSAFunction::FunctionCRTL(const CKey& aPrivateKey, sl@0: const TInteger& aInput, RInteger& aOutput) sl@0: { sl@0: const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid); sl@0: IsInputValidL(aInput, N); sl@0: sl@0: const TInteger& P = aPrivateKey.GetBigIntL(KRsaKeyParameterPUid); sl@0: const TInteger& Q = aPrivateKey.GetBigIntL(KRsaKeyParameterQUid); sl@0: const TInteger& DP = aPrivateKey.GetBigIntL(KRsaKeyParameterDPUid); sl@0: const TInteger& DQ = aPrivateKey.GetBigIntL(KRsaKeyParameterDQUid); sl@0: const TInteger& QInv = aPrivateKey.GetBigIntL(KRsaKeyParameterQInvUid); sl@0: sl@0: CMontgomeryStructure* montP = CMontgomeryStructure::NewLC(P); sl@0: CMontgomeryStructure* montQ = CMontgomeryStructure::NewLC(Q); sl@0: sl@0: // m1 = c^(dP) mod(p) sl@0: RInteger inputReduced = aInput.ModuloL(P); sl@0: CleanupStack::PushL(inputReduced); sl@0: const TInteger& m1 = montP->ExponentiateL(inputReduced, DP); sl@0: CleanupStack::PopAndDestroy(&inputReduced); sl@0: sl@0: // m2 = c^(dQ) mod(Q) sl@0: inputReduced = aInput.ModuloL(Q); sl@0: CleanupStack::PushL(inputReduced); sl@0: const TInteger& m2 = montQ->ExponentiateL(inputReduced, DQ); sl@0: CleanupStack::PopAndDestroy(&inputReduced); sl@0: sl@0: // Calculate CRT sl@0: // h = (m1-m2) qInv mod(p) sl@0: RInteger h = m1.MinusL(m2); sl@0: CleanupStack::PushL(h); sl@0: h *= QInv; sl@0: h %= P; sl@0: sl@0: // m = m2 + q * h sl@0: h *= Q; sl@0: h += m2; sl@0: sl@0: aOutput = h; sl@0: CleanupStack::Pop(&h); sl@0: sl@0: CleanupStack::PopAndDestroy(montQ); sl@0: CleanupStack::PopAndDestroy(montP); sl@0: }