crypto/weakcryptospi/source/symmetric/3desshim.cpp
changeset 19 cd501b96611d
child 43 2f10d260163b
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     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 "3desshim.h"
       
    20 
       
    21 #include "cryptosymmetriccipherapi.h"
       
    22 #include <cryptospi/cryptospidef.h>
       
    23 #include <cryptospi/plugincharacteristics.h>
       
    24 #include "keys.h"
       
    25 #include <cryptostrength.h>
       
    26 #include "../common/inlines.h"
       
    27 
       
    28 using namespace CryptoSpi;
       
    29 
       
    30 // C3DESEncryptorShim ////////////////////////////////////////////////////////
       
    31 C3DESEncryptorShim* C3DESEncryptorShim::NewL(const TDesC8& aKey)
       
    32 	{
       
    33 	C3DESEncryptorShim* self = C3DESEncryptorShim::NewLC(aKey);
       
    34 	CleanupStack::Pop(self);
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 C3DESEncryptorShim* C3DESEncryptorShim::NewLC(const TDesC8& aKey)
       
    39 	{
       
    40 	C3DESEncryptorShim* self = new (ELeave) C3DESEncryptorShim();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL(aKey);
       
    43 	// DES only used 7 bits out of every key byte
       
    44 	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 C3DESEncryptorShim::C3DESEncryptorShim()
       
    49 	{
       
    50 	}
       
    51 
       
    52 C3DESEncryptorShim::~C3DESEncryptorShim()
       
    53 	{
       
    54 	delete iSymmetricCipherImpl;
       
    55 	delete iKey;
       
    56 	}	
       
    57 
       
    58 void C3DESEncryptorShim::ConstructL(const TDesC8& aKey)
       
    59 	{
       
    60 	TKeyProperty keyProperty = {K3DesUid, KNullUid, KSymmetricKey, KNonEmbeddedKeyUid};
       
    61 	CCryptoParams* keyParam =CCryptoParams::NewLC();
       
    62 	keyParam->AddL(aKey, KSymmetricKeyParameterUid);
       
    63 	iKey=CKey::NewL(keyProperty, *keyParam);
       
    64 	CleanupStack::PopAndDestroy(keyParam);
       
    65 	
       
    66 	CSymmetricCipherFactory::CreateSymmetricCipherL(
       
    67 											iSymmetricCipherImpl,
       
    68 											K3DesUid,
       
    69 											*iKey,
       
    70 											KCryptoModeEncryptUid,
       
    71 											KOperationModeECBUid,
       
    72 											KPaddingModeNoneUid,
       
    73 											NULL);
       
    74 	}		
       
    75 
       
    76 TInt C3DESEncryptorShim::BlockSize() const
       
    77 	{
       
    78 	// SPI returns block size in BITS
       
    79 	return iSymmetricCipherImpl->BlockSize() >> 3;
       
    80 	}	
       
    81 
       
    82 TInt C3DESEncryptorShim::KeySize() const
       
    83 	{
       
    84 	return iSymmetricCipherImpl->KeySize();
       
    85 	}	
       
    86 
       
    87 void C3DESEncryptorShim::Transform(TDes8& aBlock)
       
    88 	{
       
    89 	iOutputBlock.Zero();
       
    90 	TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);)
       
    91 	aBlock = iOutputBlock;
       
    92 	}
       
    93 
       
    94 void C3DESEncryptorShim::Reset()
       
    95 	{
       
    96 	iSymmetricCipherImpl->Reset();
       
    97 	}	
       
    98 
       
    99 TInt C3DESEncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
       
   100 	{
       
   101 	TInt ret(KErrExtensionNotSupported);
       
   102 	
       
   103 	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl)
       
   104 		{
       
   105 		a0=iSymmetricCipherImpl;
       
   106 		ret=KErrNone;	
       
   107 		}		
       
   108 	return ret;
       
   109 	}	
       
   110 
       
   111 // C3DESDecryptorShim ////////////////////////////////////////////////////////
       
   112 C3DESDecryptorShim* C3DESDecryptorShim::NewL(const TDesC8& aKey)
       
   113 	{
       
   114 	C3DESDecryptorShim* self = C3DESDecryptorShim::NewLC(aKey);
       
   115 	CleanupStack::Pop(self);
       
   116 	return self;
       
   117 	}
       
   118 
       
   119 
       
   120 C3DESDecryptorShim* C3DESDecryptorShim::NewLC(const TDesC8& aKey)
       
   121 	{
       
   122 	C3DESDecryptorShim* self = new (ELeave) C3DESDecryptorShim();
       
   123 	CleanupStack::PushL(self);
       
   124 	self->ConstructL(aKey);
       
   125 	// DES only used 7 bits out of every key byte
       
   126 	TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size()) - aKey.Size());
       
   127 	return self;
       
   128 	}
       
   129 
       
   130 C3DESDecryptorShim::C3DESDecryptorShim()
       
   131 	{	
       
   132 	}
       
   133 
       
   134 C3DESDecryptorShim::~C3DESDecryptorShim()
       
   135 	{
       
   136 	delete iSymmetricCipherImpl;
       
   137 	delete iKey;
       
   138 	}
       
   139 
       
   140 
       
   141 void C3DESDecryptorShim::ConstructL(const TDesC8& aKey)
       
   142 	{
       
   143 	TKeyProperty keyProperty = {K3DesUid, KNullUid, KSymmetricKey, KNonEmbeddedKeyUid};
       
   144 	CCryptoParams* keyParam =CCryptoParams::NewLC();
       
   145 	keyParam->AddL(aKey, KSymmetricKeyParameterUid);
       
   146 	iKey=CKey::NewL(keyProperty, *keyParam);
       
   147 	CleanupStack::PopAndDestroy(keyParam);
       
   148 	CSymmetricCipherFactory::CreateSymmetricCipherL(
       
   149 											iSymmetricCipherImpl,
       
   150 											K3DesUid,
       
   151 											*iKey,
       
   152 											KCryptoModeDecryptUid,
       
   153 											KOperationModeECBUid,
       
   154 											KPaddingModeNoneUid,
       
   155 											NULL);	
       
   156 	}	
       
   157 
       
   158 TInt C3DESDecryptorShim::BlockSize() const
       
   159 	{
       
   160 	// SPI returns block size in BITS
       
   161 	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
       
   162 	}
       
   163 	
       
   164 TInt C3DESDecryptorShim::KeySize() const
       
   165 	{
       
   166 	return iSymmetricCipherImpl->KeySize();
       
   167 	}	
       
   168 
       
   169 void C3DESDecryptorShim::Transform(TDes8& aBlock)
       
   170 	{
       
   171 	iOutputBlock.Zero();
       
   172 	TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);)
       
   173 	aBlock = iOutputBlock;	
       
   174 	}
       
   175 
       
   176 void C3DESDecryptorShim::Reset()
       
   177 	{
       
   178 	iSymmetricCipherImpl->Reset();
       
   179 	}
       
   180 
       
   181 TInt C3DESDecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
       
   182 	{
       
   183 	TInt ret(KErrExtensionNotSupported);
       
   184 	
       
   185 	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl)
       
   186 		{
       
   187 		a0=iSymmetricCipherImpl;
       
   188 		ret=KErrNone;	
       
   189 		}		
       
   190 	return ret;
       
   191 	}