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