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