os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/asymmetriccipherimpl.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 <e32def.h>
    20 #include "keys.h"
    21 #include "asymmetriccipherimpl.h"
    22 #include <cryptostrength.h>
    23 #include <cryptospi/cryptospidef.h>
    24 #include <cryptospi/plugincharacteristics.h>
    25 #include "pluginconfig.h"
    26 #include "common/inlines.h"
    27 
    28 using namespace SoftwareCrypto;
    29 
    30 CAsymmetricCipherImpl::CAsymmetricCipherImpl(
    31 	TUid aCryptoMode,
    32 	TUid aPaddingMode) :
    33 	iCryptoMode(aCryptoMode),
    34 	iPaddingMode(aPaddingMode)
    35 	{
    36 	}
    37 
    38 void CAsymmetricCipherImpl::ConstructL(const CKey& aKey)
    39 	{
    40 	DoSetCryptoModeL(iCryptoMode);
    41 	DoSetKeyL(aKey);
    42 	DoSetPaddingModeL(iPaddingMode);
    43 	}
    44 	
    45 CAsymmetricCipherImpl::~CAsymmetricCipherImpl()
    46 	{
    47 	delete iKey;
    48 	delete iPadding;
    49 	}
    50 		
    51 void CAsymmetricCipherImpl::Close()
    52 	{
    53 	delete this;
    54 	}
    55 	
    56 void CAsymmetricCipherImpl::Reset()
    57 	{
    58 	}
    59 	
    60 TAny* CAsymmetricCipherImpl::GetExtension(TUid /*aExtensionId*/)
    61 	{
    62 	return 0;
    63 	}
    64 	
    65 void CAsymmetricCipherImpl::GetCharacteristicsL(const TAny*& aPluginCharacteristics)
    66 	{
    67 	TInt numCiphers = sizeof(KAsymmetricCipherCharacteristics)/sizeof(TAsymmetricCipherCharacteristics*);
    68 	TInt32 implUid = ImplementationUid().iUid;
    69 	for (TInt i = 0; i < numCiphers; ++i)
    70 		{
    71 		if (KAsymmetricCipherCharacteristics[i]->cmn.iImplementationUID == implUid)
    72 			{
    73 			aPluginCharacteristics = KAsymmetricCipherCharacteristics[i];
    74 			break;
    75 			}
    76 		}
    77 	}
    78 
    79 void CAsymmetricCipherImpl::SetKeyL(const CKey& aKey)
    80 	{
    81 	DoSetKeyL(aKey);
    82 	}
    83 
    84 void CAsymmetricCipherImpl::DoSetKeyL(const CKey& aKey)
    85 	{
    86 	delete iKey;								// delete any previous key
    87 	iKey = CKey::NewL(aKey);
    88 	}
    89 
    90 void CAsymmetricCipherImpl::SetCryptoModeL(TUid aCryptoMode)
    91 	{
    92 	DoSetCryptoModeL(aCryptoMode);
    93 	Reset();
    94 	}
    95 	
    96 void CAsymmetricCipherImpl::DoSetCryptoModeL(TUid aCryptoMode)
    97 	{
    98 	switch (aCryptoMode.iUid)
    99 		{
   100 		case KCryptoModeEncrypt:
   101 		case KCryptoModeDecrypt:
   102 			break;
   103 		default:
   104 			User::Leave(KErrNotSupported);
   105 		}
   106 	iCryptoMode = aCryptoMode;
   107 	}
   108 
   109 void CAsymmetricCipherImpl::SetPaddingModeL(TUid aPaddingMode)
   110 	{
   111 	DoSetPaddingModeL(aPaddingMode);
   112 	Reset();
   113 	}
   114 	
   115 void CAsymmetricCipherImpl::DoSetPaddingModeL(TUid aPaddingMode)
   116 	{
   117 	CPadding* padding(0);
   118 	TInt padlength = 0;
   119 	
   120 	switch (iCryptoMode.iUid)
   121 		{
   122 		case KCryptoModeEncrypt:
   123 			padlength = GetMaximumOutputLengthL();
   124 			break;
   125 		case KCryptoModeDecrypt:
   126 			padlength = GetMaximumInputLengthL();
   127 			break;
   128 		}
   129 		
   130 	switch (aPaddingMode.iUid)
   131 		{
   132 		case KPaddingModeNone:
   133 			padding = CPaddingNone::NewL(padlength);
   134 			break;
   135 		case KPaddingModePkcs1_v1_5_Encryption:
   136 			padding = CPaddingPKCS1Encryption::NewL(padlength);
   137 			break;
   138 		default:
   139 			User::Leave(KErrNotSupported);
   140 		}
   141 		
   142 	delete iPadding;
   143 	iPadding = padding;
   144 	iPaddingMode = aPaddingMode;
   145 	}
   146 
   147 // Methods implemented in subclass. No coverage here.
   148 #ifdef _BullseyeCoverage
   149 #pragma suppress_warnings on
   150 #pragma BullseyeCoverage off
   151 #pragma suppress_warnings off
   152 #endif
   153 TInt CAsymmetricCipherImpl::GetMaximumOutputLengthL() const
   154 	{
   155 	// Override in subclass
   156 	User::Leave(KErrNotSupported);
   157 	return 0;
   158 	}
   159 
   160 TInt CAsymmetricCipherImpl::GetMaximumInputLengthL() const
   161 	{
   162 	// Override in subclass
   163 	User::Leave(KErrNotSupported);
   164 	return 0;
   165 	}
   166