os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/asymmetriccipherimpl.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/asymmetriccipherimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,166 @@
1.4 +/*
1.5 +* Copyright (c) 2006-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 <e32def.h>
1.23 +#include "keys.h"
1.24 +#include "asymmetriccipherimpl.h"
1.25 +#include <cryptostrength.h>
1.26 +#include <cryptospi/cryptospidef.h>
1.27 +#include <cryptospi/plugincharacteristics.h>
1.28 +#include "pluginconfig.h"
1.29 +#include "common/inlines.h"
1.30 +
1.31 +using namespace SoftwareCrypto;
1.32 +
1.33 +CAsymmetricCipherImpl::CAsymmetricCipherImpl(
1.34 + TUid aCryptoMode,
1.35 + TUid aPaddingMode) :
1.36 + iCryptoMode(aCryptoMode),
1.37 + iPaddingMode(aPaddingMode)
1.38 + {
1.39 + }
1.40 +
1.41 +void CAsymmetricCipherImpl::ConstructL(const CKey& aKey)
1.42 + {
1.43 + DoSetCryptoModeL(iCryptoMode);
1.44 + DoSetKeyL(aKey);
1.45 + DoSetPaddingModeL(iPaddingMode);
1.46 + }
1.47 +
1.48 +CAsymmetricCipherImpl::~CAsymmetricCipherImpl()
1.49 + {
1.50 + delete iKey;
1.51 + delete iPadding;
1.52 + }
1.53 +
1.54 +void CAsymmetricCipherImpl::Close()
1.55 + {
1.56 + delete this;
1.57 + }
1.58 +
1.59 +void CAsymmetricCipherImpl::Reset()
1.60 + {
1.61 + }
1.62 +
1.63 +TAny* CAsymmetricCipherImpl::GetExtension(TUid /*aExtensionId*/)
1.64 + {
1.65 + return 0;
1.66 + }
1.67 +
1.68 +void CAsymmetricCipherImpl::GetCharacteristicsL(const TAny*& aPluginCharacteristics)
1.69 + {
1.70 + TInt numCiphers = sizeof(KAsymmetricCipherCharacteristics)/sizeof(TAsymmetricCipherCharacteristics*);
1.71 + TInt32 implUid = ImplementationUid().iUid;
1.72 + for (TInt i = 0; i < numCiphers; ++i)
1.73 + {
1.74 + if (KAsymmetricCipherCharacteristics[i]->cmn.iImplementationUID == implUid)
1.75 + {
1.76 + aPluginCharacteristics = KAsymmetricCipherCharacteristics[i];
1.77 + break;
1.78 + }
1.79 + }
1.80 + }
1.81 +
1.82 +void CAsymmetricCipherImpl::SetKeyL(const CKey& aKey)
1.83 + {
1.84 + DoSetKeyL(aKey);
1.85 + }
1.86 +
1.87 +void CAsymmetricCipherImpl::DoSetKeyL(const CKey& aKey)
1.88 + {
1.89 + delete iKey; // delete any previous key
1.90 + iKey = CKey::NewL(aKey);
1.91 + }
1.92 +
1.93 +void CAsymmetricCipherImpl::SetCryptoModeL(TUid aCryptoMode)
1.94 + {
1.95 + DoSetCryptoModeL(aCryptoMode);
1.96 + Reset();
1.97 + }
1.98 +
1.99 +void CAsymmetricCipherImpl::DoSetCryptoModeL(TUid aCryptoMode)
1.100 + {
1.101 + switch (aCryptoMode.iUid)
1.102 + {
1.103 + case KCryptoModeEncrypt:
1.104 + case KCryptoModeDecrypt:
1.105 + break;
1.106 + default:
1.107 + User::Leave(KErrNotSupported);
1.108 + }
1.109 + iCryptoMode = aCryptoMode;
1.110 + }
1.111 +
1.112 +void CAsymmetricCipherImpl::SetPaddingModeL(TUid aPaddingMode)
1.113 + {
1.114 + DoSetPaddingModeL(aPaddingMode);
1.115 + Reset();
1.116 + }
1.117 +
1.118 +void CAsymmetricCipherImpl::DoSetPaddingModeL(TUid aPaddingMode)
1.119 + {
1.120 + CPadding* padding(0);
1.121 + TInt padlength = 0;
1.122 +
1.123 + switch (iCryptoMode.iUid)
1.124 + {
1.125 + case KCryptoModeEncrypt:
1.126 + padlength = GetMaximumOutputLengthL();
1.127 + break;
1.128 + case KCryptoModeDecrypt:
1.129 + padlength = GetMaximumInputLengthL();
1.130 + break;
1.131 + }
1.132 +
1.133 + switch (aPaddingMode.iUid)
1.134 + {
1.135 + case KPaddingModeNone:
1.136 + padding = CPaddingNone::NewL(padlength);
1.137 + break;
1.138 + case KPaddingModePkcs1_v1_5_Encryption:
1.139 + padding = CPaddingPKCS1Encryption::NewL(padlength);
1.140 + break;
1.141 + default:
1.142 + User::Leave(KErrNotSupported);
1.143 + }
1.144 +
1.145 + delete iPadding;
1.146 + iPadding = padding;
1.147 + iPaddingMode = aPaddingMode;
1.148 + }
1.149 +
1.150 +// Methods implemented in subclass. No coverage here.
1.151 +#ifdef _BullseyeCoverage
1.152 +#pragma suppress_warnings on
1.153 +#pragma BullseyeCoverage off
1.154 +#pragma suppress_warnings off
1.155 +#endif
1.156 +TInt CAsymmetricCipherImpl::GetMaximumOutputLengthL() const
1.157 + {
1.158 + // Override in subclass
1.159 + User::Leave(KErrNotSupported);
1.160 + return 0;
1.161 + }
1.162 +
1.163 +TInt CAsymmetricCipherImpl::GetMaximumInputLengthL() const
1.164 + {
1.165 + // Override in subclass
1.166 + User::Leave(KErrNotSupported);
1.167 + return 0;
1.168 + }
1.169 +