|
1 /* |
|
2 * Copyright (c) 2006-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 "dhimpl.h" |
|
20 #include "pluginconfig.h" |
|
21 |
|
22 using namespace SoftwareCrypto; |
|
23 using namespace CryptoSpi; |
|
24 |
|
25 /* CDHImpl */ |
|
26 CDHImpl::CDHImpl() |
|
27 { |
|
28 } |
|
29 |
|
30 CDHImpl::~CDHImpl() |
|
31 { |
|
32 } |
|
33 |
|
34 CDHImpl* CDHImpl::NewL(const CKey& aPrivateKey, const CCryptoParams* aParams) |
|
35 { |
|
36 CDHImpl* self = CDHImpl::NewLC(aPrivateKey, aParams); |
|
37 CleanupStack::Pop(self); |
|
38 return self; |
|
39 } |
|
40 |
|
41 CDHImpl* CDHImpl::NewLC(const CKey& aPrivateKey, const CCryptoParams* aParams) |
|
42 { |
|
43 CDHImpl* self = new(ELeave) CDHImpl(); |
|
44 CleanupStack::PushL(self); |
|
45 self->ConstructL(aPrivateKey, aParams); |
|
46 return self; |
|
47 } |
|
48 |
|
49 CKey* CDHImpl::AgreeL(const CKey& aOtherPublicKey, const CCryptoParams* aParams) |
|
50 { |
|
51 /* |
|
52 * unpack the parameters, we're expecting the N and G parameters |
|
53 */ |
|
54 const TInteger& N = aParams->GetBigIntL(KDhKeyParameterNUid); |
|
55 const TInteger& G = aParams->GetBigIntL(KDhKeyParameterGUid); |
|
56 const TInteger& privateN = iSharedParams->GetBigIntL(KDhKeyParameterNUid); |
|
57 const TInteger& privateG = iSharedParams->GetBigIntL(KDhKeyParameterGUid); |
|
58 const TInteger& X = aOtherPublicKey.GetBigIntL(KDhKeyParameterXUid); |
|
59 const TInteger& x = iPrivateKey->GetBigIntL(KDhKeyParameterxUid); |
|
60 |
|
61 /* |
|
62 * both DH keys (ie our private and their public keys) must use the same N and G parameters |
|
63 */ |
|
64 if ((N != privateN) || (G != privateG)) |
|
65 { |
|
66 User::Leave(KErrArgument); |
|
67 } |
|
68 |
|
69 /* |
|
70 * do the key agreement algo X ^ x mod N |
|
71 */ |
|
72 RInteger result = TInteger::ModularExponentiateL(X, x, N); |
|
73 CleanupClosePushL(result); |
|
74 |
|
75 /* |
|
76 * create the agreed key |
|
77 */ |
|
78 CCryptoParams* agreedKeyParameters = CCryptoParams::NewLC(); |
|
79 agreedKeyParameters->AddL(result, KSymmetricKeyParameterUid); |
|
80 TKeyProperty agreedKeyProperties = {KDHAgreementUid, KCryptoPluginDhKeyAgreementUid, |
|
81 KDHAgreedKeyUid, KNonEmbeddedKeyUid }; |
|
82 CKey* agreedKey = CKey::NewL(agreedKeyProperties, *agreedKeyParameters); |
|
83 |
|
84 // cleanup result, agreedKeyParameters |
|
85 CleanupStack::PopAndDestroy(2, &result); |
|
86 return agreedKey; |
|
87 } |
|
88 |
|
89 TUid CDHImpl::ImplementationUid() const |
|
90 { |
|
91 return KCryptoPluginDhKeyAgreementUid; |
|
92 } |
|
93 |
|
94 CExtendedCharacteristics* CDHImpl::CreateExtendedCharacteristicsL() |
|
95 { |
|
96 // All Symbian software plug-ins have unlimited concurrency, cannot be |
|
97 // reserved for exclusive use and are not CERTIFIED to be standards compliant. |
|
98 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse); |
|
99 } |
|
100 |
|
101 const CExtendedCharacteristics* CDHImpl::GetExtendedCharacteristicsL() |
|
102 { |
|
103 return CDHImpl::CreateExtendedCharacteristicsL(); |
|
104 } |
|
105 |