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