os/security/crypto/weakcryptospi/test/tplugins/src/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-2010 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 <cryptospi/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 "../../../source/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 TInt CAsymmetricCipherImpl::GetMaximumOutputLengthL() const
    80 	{
    81 	// Override in subclass
    82 	User::Leave(KErrNotSupported);
    83 	return 0;
    84 	}
    85 
    86 TInt CAsymmetricCipherImpl::GetMaximumInputLengthL() const
    87 	{
    88 	// Override in subclass
    89 	User::Leave(KErrNotSupported);
    90 	return 0;
    91 	}
    92 
    93 void CAsymmetricCipherImpl::SetKeyL(const CKey& aKey)
    94 	{
    95 	DoSetKeyL(aKey);
    96 	}
    97 
    98 void CAsymmetricCipherImpl::DoSetKeyL(const CKey& aKey)
    99 	{
   100 	delete iKey;								// delete any previous key
   101 	iKey = CKey::NewL(aKey);
   102 	}
   103 
   104 void CAsymmetricCipherImpl::SetCryptoModeL(TUid aCryptoMode)
   105 	{
   106 	DoSetCryptoModeL(aCryptoMode);
   107 	Reset();
   108 	}
   109 	
   110 void CAsymmetricCipherImpl::DoSetCryptoModeL(TUid aCryptoMode)
   111 	{
   112 	switch (aCryptoMode.iUid)
   113 		{
   114 		case KCryptoModeEncrypt:
   115 		case KCryptoModeDecrypt:
   116 			break;
   117 		default:
   118 			User::Leave(KErrNotSupported);
   119 		}
   120 	iCryptoMode = aCryptoMode;
   121 	}
   122 
   123 void CAsymmetricCipherImpl::SetPaddingModeL(TUid aPaddingMode)
   124 	{
   125 	DoSetPaddingModeL(aPaddingMode);
   126 	Reset();
   127 	}
   128 	
   129 void CAsymmetricCipherImpl::DoSetPaddingModeL(TUid aPaddingMode)
   130 	{
   131 	CPadding* padding(0);
   132 	TInt padlength = 0;
   133 	
   134 	switch (iCryptoMode.iUid)
   135 		{
   136 		case KCryptoModeEncrypt:
   137 			padlength = GetMaximumOutputLengthL();
   138 			break;
   139 		case KCryptoModeDecrypt:
   140 			padlength = GetMaximumInputLengthL();
   141 			break;
   142 		}
   143 		
   144 	switch (aPaddingMode.iUid)
   145 		{
   146 		case KPaddingModeNone:
   147 			padding = CPaddingNone::NewL(padlength);
   148 			break;
   149 		case KPaddingModePkcs1_v1_5_Encryption:
   150 			padding = CPaddingPKCS1Encryption::NewL(padlength);
   151 			break;
   152 		default:
   153 			User::Leave(KErrNotSupported);
   154 		}
   155 		
   156 	delete iPadding;
   157 	iPadding = padding;
   158 	iPaddingMode = aPaddingMode;
   159 	}