os/security/crypto/weakcryptospi/source/symmetric/rc2shim.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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "rc2shim.h"
sl@0
    20
sl@0
    21
#include <cryptospi/cryptoparams.h>
sl@0
    22
#include <cryptospi/cryptosymmetriccipherapi.h>
sl@0
    23
#include <cryptospi/cryptospidef.h>
sl@0
    24
#include <cryptospi/plugincharacteristics.h>
sl@0
    25
#include <cryptospi/keys.h>
sl@0
    26
#include <cryptostrength.h>
sl@0
    27
sl@0
    28
#include "../common/inlines.h"
sl@0
    29
sl@0
    30
using namespace CryptoSpi;
sl@0
    31
sl@0
    32
// CRC2EncryptorShim ////////////////////////////////////////////////////////
sl@0
    33
CRC2EncryptorShim* CRC2EncryptorShim::NewL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
sl@0
    34
	{
sl@0
    35
	CRC2EncryptorShim* self = CRC2EncryptorShim::NewLC(aKey, aEffectiveKeyLenBits);
sl@0
    36
	CleanupStack::Pop(self);
sl@0
    37
	return self;
sl@0
    38
	}
sl@0
    39
sl@0
    40
CRC2EncryptorShim* CRC2EncryptorShim::NewLC(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
sl@0
    41
	{
sl@0
    42
	CRC2EncryptorShim* self = new (ELeave) CRC2EncryptorShim();
sl@0
    43
	CleanupStack::PushL(self);
sl@0
    44
	self->ConstructL(aKey, aEffectiveKeyLenBits);
sl@0
    45
	// weak enough if either aKey or aEffectiveKeyLenBits is weak
sl@0
    46
	TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
sl@0
    47
	TCrypto::IsSymmetricWeakEnoughL(minKeySize);
sl@0
    48
	return self;
sl@0
    49
	}
sl@0
    50
sl@0
    51
CRC2EncryptorShim::CRC2EncryptorShim()
sl@0
    52
	{
sl@0
    53
	}
sl@0
    54
sl@0
    55
CRC2EncryptorShim::~CRC2EncryptorShim()
sl@0
    56
	{
sl@0
    57
	delete iSymmetricCipherImpl;
sl@0
    58
	delete iKey;	
sl@0
    59
	delete iAlgorithmParams;
sl@0
    60
	}	
sl@0
    61
sl@0
    62
void CRC2EncryptorShim::ConstructL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
sl@0
    63
	{
sl@0
    64
	TKeyProperty keyProperty = {KRc2Uid, KNullUid, KSymmetricKeyUid, KNonEmbeddedKeyUid};
sl@0
    65
	CCryptoParams* keyParam =CCryptoParams::NewLC();
sl@0
    66
	keyParam->AddL(aKey, KSymmetricKeyParameterUid);
sl@0
    67
	iKey=CKey::NewL(keyProperty, *keyParam);
sl@0
    68
	CleanupStack::PopAndDestroy(keyParam);
sl@0
    69
	
sl@0
    70
	iAlgorithmParams = CCryptoParams::NewL();
sl@0
    71
	iAlgorithmParams->AddL(aEffectiveKeyLenBits, KRC2EffectiveKeyLenBits);
sl@0
    72
	
sl@0
    73
	CSymmetricCipherFactory::CreateSymmetricCipherL(
sl@0
    74
											iSymmetricCipherImpl,
sl@0
    75
											KRc2Uid,
sl@0
    76
											*iKey,
sl@0
    77
											KCryptoModeEncryptUid,
sl@0
    78
											KOperationModeECBUid,
sl@0
    79
											KPaddingModeNoneUid,
sl@0
    80
											iAlgorithmParams);
sl@0
    81
	}		
sl@0
    82
sl@0
    83
TInt CRC2EncryptorShim::BlockSize() const
sl@0
    84
	{
sl@0
    85
	// SPI returns block size in BITS
sl@0
    86
	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
sl@0
    87
	}	
sl@0
    88
sl@0
    89
TInt CRC2EncryptorShim::KeySize() const
sl@0
    90
	{
sl@0
    91
	return iSymmetricCipherImpl->KeySize();
sl@0
    92
	}	
sl@0
    93
sl@0
    94
void CRC2EncryptorShim::Transform(TDes8& aBlock)
sl@0
    95
	{
sl@0
    96
	iOutputBlock.Zero();
sl@0
    97
	TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);)
sl@0
    98
	aBlock = iOutputBlock;
sl@0
    99
	}
sl@0
   100
sl@0
   101
void CRC2EncryptorShim::Reset()
sl@0
   102
	{
sl@0
   103
	iSymmetricCipherImpl->Reset();
sl@0
   104
	}	
sl@0
   105
sl@0
   106
TInt CRC2EncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
sl@0
   107
	{
sl@0
   108
	TInt ret(KErrExtensionNotSupported);
sl@0
   109
	
sl@0
   110
	if (KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl)
sl@0
   111
		{
sl@0
   112
		a0=iSymmetricCipherImpl;
sl@0
   113
		ret=KErrNone;	
sl@0
   114
		}		
sl@0
   115
	return ret;
sl@0
   116
	}	
sl@0
   117
sl@0
   118
// CRC2DecryptorShim ////////////////////////////////////////////////////////
sl@0
   119
CRC2DecryptorShim* CRC2DecryptorShim::NewL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
sl@0
   120
	{
sl@0
   121
	CRC2DecryptorShim* self = CRC2DecryptorShim::NewLC(aKey, aEffectiveKeyLenBits);
sl@0
   122
	CleanupStack::Pop(self);
sl@0
   123
	return self;
sl@0
   124
	}
sl@0
   125
sl@0
   126
sl@0
   127
CRC2DecryptorShim* CRC2DecryptorShim::NewLC(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
sl@0
   128
	{
sl@0
   129
	CRC2DecryptorShim* self = new (ELeave) CRC2DecryptorShim();
sl@0
   130
	CleanupStack::PushL(self);
sl@0
   131
	self->ConstructL(aKey, aEffectiveKeyLenBits);
sl@0
   132
	// weak enough if either aKey or aEffectiveKeyLenBits is weak
sl@0
   133
	TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size()));
sl@0
   134
	TCrypto::IsSymmetricWeakEnoughL(minKeySize);
sl@0
   135
	return self;
sl@0
   136
	}
sl@0
   137
sl@0
   138
CRC2DecryptorShim::CRC2DecryptorShim()
sl@0
   139
	{	
sl@0
   140
	}
sl@0
   141
sl@0
   142
CRC2DecryptorShim::~CRC2DecryptorShim()
sl@0
   143
	{
sl@0
   144
	delete iSymmetricCipherImpl;
sl@0
   145
	delete iKey;
sl@0
   146
	delete iAlgorithmParams;			
sl@0
   147
	}
sl@0
   148
sl@0
   149
sl@0
   150
void CRC2DecryptorShim::ConstructL(const TDesC8& aKey, TInt aEffectiveKeyLenBits)
sl@0
   151
	{
sl@0
   152
	TKeyProperty keyProperty = {KRc2Uid, KNullUid, KSymmetricKeyUid, KNonEmbeddedKeyUid};
sl@0
   153
	CCryptoParams* keyParam =CCryptoParams::NewLC();
sl@0
   154
	keyParam->AddL(aKey, KSymmetricKeyParameterUid);
sl@0
   155
	iKey=CKey::NewL(keyProperty, *keyParam);
sl@0
   156
	CleanupStack::PopAndDestroy(keyParam);
sl@0
   157
sl@0
   158
	iAlgorithmParams = CCryptoParams::NewL();
sl@0
   159
	iAlgorithmParams->AddL(aEffectiveKeyLenBits, KRC2EffectiveKeyLenBits);
sl@0
   160
	
sl@0
   161
	CSymmetricCipherFactory::CreateSymmetricCipherL(
sl@0
   162
												iSymmetricCipherImpl,
sl@0
   163
												KRc2Uid,
sl@0
   164
												*iKey,
sl@0
   165
												KCryptoModeDecryptUid,
sl@0
   166
												KOperationModeECBUid,
sl@0
   167
												KPaddingModeNoneUid,
sl@0
   168
												iAlgorithmParams);	
sl@0
   169
	}	
sl@0
   170
sl@0
   171
TInt CRC2DecryptorShim::BlockSize() const
sl@0
   172
	{
sl@0
   173
	// SPI returns block size in BITS
sl@0
   174
	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
sl@0
   175
	}
sl@0
   176
	
sl@0
   177
TInt CRC2DecryptorShim::KeySize() const
sl@0
   178
	{
sl@0
   179
	return iSymmetricCipherImpl->KeySize();
sl@0
   180
	}	
sl@0
   181
sl@0
   182
void CRC2DecryptorShim::Transform(TDes8& aBlock)
sl@0
   183
	{
sl@0
   184
	iOutputBlock.Zero();	
sl@0
   185
	TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);)
sl@0
   186
	aBlock = iOutputBlock;	
sl@0
   187
	}
sl@0
   188
sl@0
   189
void CRC2DecryptorShim::Reset()
sl@0
   190
	{
sl@0
   191
	iSymmetricCipherImpl->Reset();
sl@0
   192
	}
sl@0
   193
sl@0
   194
TInt CRC2DecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
sl@0
   195
	{
sl@0
   196
	TInt ret(KErrExtensionNotSupported);
sl@0
   197
	
sl@0
   198
	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl)
sl@0
   199
		{
sl@0
   200
		a0=iSymmetricCipherImpl;
sl@0
   201
		ret=KErrNone;	
sl@0
   202
		}		
sl@0
   203
	return ret;
sl@0
   204
	}