1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsafunction.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,138 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <bigint.h>
1.23 +#include "keys.h"
1.24 +#include <asymmetrickeys.h>
1.25 +#include <cryptospi/cryptospidef.h>
1.26 +#include <cryptospi/cryptoparams.h>
1.27 +#include "rsafunction.h"
1.28 +#include "mont.h"
1.29 +
1.30 +using namespace CryptoSpi;
1.31 +
1.32 +// Public Encrypt
1.33 +void RSAFunction::EncryptL(const CKey& aPublicKey,
1.34 + const TInteger& aInput, RInteger& aOutput)
1.35 + {
1.36 + const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid);
1.37 + const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid);
1.38 + FunctionL(N, E, aInput, aOutput);
1.39 + }
1.40 +
1.41 +// Private Decrypt
1.42 +void RSAFunction::DecryptL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput)
1.43 + {
1.44 + if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid)
1.45 + {
1.46 + const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
1.47 + const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid);
1.48 + FunctionL(N, D, aInput, aOutput);
1.49 + }
1.50 + else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid)
1.51 + {
1.52 + FunctionCRTL(aPrivateKey, aInput, aOutput);
1.53 + }
1.54 + else
1.55 + {
1.56 + User::Leave(KErrNotSupported);
1.57 + }
1.58 + }
1.59 +
1.60 +// Private Encrypt
1.61 +void RSAFunction::SignL(const CKey& aPrivateKey, const TInteger& aInput, RInteger& aOutput)
1.62 + {
1.63 + if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyStandardUid)
1.64 + {
1.65 + const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
1.66 + const TInteger& D = aPrivateKey.GetBigIntL(KRsaKeyParameterDUid);
1.67 + FunctionL(N, D, aInput, aOutput);
1.68 + }
1.69 + else if (aPrivateKey.KeyProperty().iKeyType == KRsaPrivateKeyCRTUid)
1.70 + {
1.71 + FunctionCRTL(aPrivateKey, aInput, aOutput);
1.72 + }
1.73 + else
1.74 + {
1.75 + User::Leave(KErrNotSupported);
1.76 + }
1.77 +}
1.78 +
1.79 +// Public Decrypt
1.80 +void RSAFunction::VerifyL(const CKey& aPublicKey,
1.81 + const TInteger& aInput, RInteger& aOutput)
1.82 + {
1.83 + const TInteger& N = aPublicKey.GetBigIntL(KRsaKeyParameterNUid);
1.84 + const TInteger& E = aPublicKey.GetBigIntL(KRsaKeyParameterEUid);
1.85 + FunctionL(N, E, aInput, aOutput);
1.86 + }
1.87 +
1.88 +// The RSA Trapdoor Function
1.89 +void RSAFunction::FunctionL(const TInteger& aModulus, const TInteger& aExponent,
1.90 + const TInteger& aBase, RInteger& aOutput)
1.91 + {
1.92 + IsInputValidL(aBase, aModulus);
1.93 +
1.94 + aOutput = TInteger::ModularExponentiateL(aBase, aExponent, aModulus);
1.95 + }
1.96 +
1.97 +// The CRT version of the RSA Trapdoor Function
1.98 +void RSAFunction::FunctionCRTL(const CKey& aPrivateKey,
1.99 + const TInteger& aInput, RInteger& aOutput)
1.100 + {
1.101 + const TInteger& N = aPrivateKey.GetBigIntL(KRsaKeyParameterNUid);
1.102 + IsInputValidL(aInput, N);
1.103 +
1.104 + const TInteger& P = aPrivateKey.GetBigIntL(KRsaKeyParameterPUid);
1.105 + const TInteger& Q = aPrivateKey.GetBigIntL(KRsaKeyParameterQUid);
1.106 + const TInteger& DP = aPrivateKey.GetBigIntL(KRsaKeyParameterDPUid);
1.107 + const TInteger& DQ = aPrivateKey.GetBigIntL(KRsaKeyParameterDQUid);
1.108 + const TInteger& QInv = aPrivateKey.GetBigIntL(KRsaKeyParameterQInvUid);
1.109 +
1.110 + CMontgomeryStructure* montP = CMontgomeryStructure::NewLC(P);
1.111 + CMontgomeryStructure* montQ = CMontgomeryStructure::NewLC(Q);
1.112 +
1.113 + // m1 = c^(dP) mod(p)
1.114 + RInteger inputReduced = aInput.ModuloL(P);
1.115 + CleanupStack::PushL(inputReduced);
1.116 + const TInteger& m1 = montP->ExponentiateL(inputReduced, DP);
1.117 + CleanupStack::PopAndDestroy(&inputReduced);
1.118 +
1.119 + // m2 = c^(dQ) mod(Q)
1.120 + inputReduced = aInput.ModuloL(Q);
1.121 + CleanupStack::PushL(inputReduced);
1.122 + const TInteger& m2 = montQ->ExponentiateL(inputReduced, DQ);
1.123 + CleanupStack::PopAndDestroy(&inputReduced);
1.124 +
1.125 + // Calculate CRT
1.126 + // h = (m1-m2) qInv mod(p)
1.127 + RInteger h = m1.MinusL(m2);
1.128 + CleanupStack::PushL(h);
1.129 + h *= QInv;
1.130 + h %= P;
1.131 +
1.132 + // m = m2 + q * h
1.133 + h *= Q;
1.134 + h += m2;
1.135 +
1.136 + aOutput = h;
1.137 + CleanupStack::Pop(&h);
1.138 +
1.139 + CleanupStack::PopAndDestroy(montQ);
1.140 + CleanupStack::PopAndDestroy(montP);
1.141 + }