os/security/crypto/weakcryptospi/source/pbe/pbesymmetricfactory.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2002-2009 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 <symmetric.h>
sl@0
    20
#include "pbesymmetricfactory.h"
sl@0
    21
sl@0
    22
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
sl@0
    23
sl@0
    24
/** OpenSSL PKCS8 Effective Key Length Compatibility.*/
sl@0
    25
const TUint KPkcs8CompatibilityBits = 128;
sl@0
    26
sl@0
    27
/** PKCS12 PBE Effective Key Length Compatibility.*/
sl@0
    28
const TUint KPkcs12CompatibilityBits = 40;
sl@0
    29
sl@0
    30
#endif
sl@0
    31
sl@0
    32
TUint PBE::GetBlockBytes(TPBECipher aCipher)
sl@0
    33
	{
sl@0
    34
	switch(aCipher)
sl@0
    35
		{
sl@0
    36
		case ECipherAES_CBC_128:
sl@0
    37
		case ECipherAES_CBC_192:
sl@0
    38
		case ECipherAES_CBC_256:
sl@0
    39
			return KAESBlockBytes;
sl@0
    40
		case ECipherDES_CBC:
sl@0
    41
		case ECipher3DES_CBC:
sl@0
    42
sl@0
    43
		case ECipher2Key3DES_CBC: 
sl@0
    44
sl@0
    45
			return KDESBlockBytes;
sl@0
    46
		case ECipherRC2_CBC_128_16:
sl@0
    47
		case ECipherRC2_CBC_40_16:
sl@0
    48
		case ECipherRC2_CBC_128:
sl@0
    49
		case ECipherRC2_CBC_40:
sl@0
    50
sl@0
    51
		case ECipherRC2_CBC_40_5:
sl@0
    52
sl@0
    53
			return KRC2BlockBytes;
sl@0
    54
sl@0
    55
		case ECipherARC4_128:
sl@0
    56
		case ECipherARC4_40:
sl@0
    57
			return 1; // 1 byte block for stream cipher
sl@0
    58
sl@0
    59
		default:
sl@0
    60
			User::Panic(_L("Invalid PBE cipher"), 1);
sl@0
    61
		}
sl@0
    62
	return (KErrNone); //	For the compiler
sl@0
    63
	}
sl@0
    64
sl@0
    65
TUint PBE::GetKeyBytes(TPBECipher aCipher)
sl@0
    66
	{
sl@0
    67
	switch(aCipher)
sl@0
    68
		{
sl@0
    69
		case ECipherAES_CBC_128:
sl@0
    70
			return KAESKeyBytes128;
sl@0
    71
		case ECipherAES_CBC_192:
sl@0
    72
			return KAESKeyBytes192;
sl@0
    73
		case ECipherAES_CBC_256:
sl@0
    74
			return KAESKeyBytes256;
sl@0
    75
		case ECipherDES_CBC:
sl@0
    76
			return KDESKeyBytes;
sl@0
    77
		case ECipher3DES_CBC:
sl@0
    78
			return K3DESKeyBytes;
sl@0
    79
sl@0
    80
		case ECipher2Key3DES_CBC:
sl@0
    81
			return K2Key3DESKeyBytes;
sl@0
    82
sl@0
    83
 		case ECipherRC2_CBC_128:
sl@0
    84
 		case ECipherRC2_CBC_128_16:
sl@0
    85
			return KRC2KeyBytes128;
sl@0
    86
		case ECipherRC2_CBC_40:
sl@0
    87
		case ECipherRC2_CBC_40_16:
sl@0
    88
sl@0
    89
		case ECipherRC2_CBC_40_5:
sl@0
    90
sl@0
    91
			return KRC2KeyBytes40;
sl@0
    92
sl@0
    93
		case ECipherARC4_128:
sl@0
    94
			return KRC4KeyBytes128;
sl@0
    95
		case ECipherARC4_40:	
sl@0
    96
			return KRC4KeyBytes40;
sl@0
    97
sl@0
    98
		default:
sl@0
    99
			User::Panic(_L("Invalid PBE cipher"), 1);
sl@0
   100
		}
sl@0
   101
	return (KErrNone);	//	For the compiler
sl@0
   102
	}
sl@0
   103
sl@0
   104
CSymmetricCipher* PBE::MakeEncryptorL(TPBECipher aCipher, const TDesC8& aKey, 
sl@0
   105
	const TDesC8& aIV)
sl@0
   106
	{
sl@0
   107
	CSymmetricCipher* cipher = 0;	
sl@0
   108
	CBlockTransformation* block = 0;
sl@0
   109
	switch(aCipher)
sl@0
   110
		{
sl@0
   111
sl@0
   112
		// stream cipher
sl@0
   113
		case ECipherARC4_40:
sl@0
   114
		case ECipherARC4_128:
sl@0
   115
			cipher = CARC4::NewL(aKey, 0);
sl@0
   116
			break;	
sl@0
   117
sl@0
   118
		// block cipher	
sl@0
   119
		case ECipherAES_CBC_128:
sl@0
   120
		case ECipherAES_CBC_192:
sl@0
   121
		case ECipherAES_CBC_256:
sl@0
   122
			block = CAESEncryptor::NewLC(aKey);
sl@0
   123
			break;
sl@0
   124
sl@0
   125
		case ECipherDES_CBC:
sl@0
   126
			block = CDESEncryptor::NewLC(aKey);
sl@0
   127
			break;
sl@0
   128
			
sl@0
   129
		case ECipher3DES_CBC:		
sl@0
   130
			block = C3DESEncryptor::NewLC(aKey);
sl@0
   131
			break;
sl@0
   132
	
sl@0
   133
		case ECipher2Key3DES_CBC:
sl@0
   134
			{
sl@0
   135
			// Construct 3key from 2 key ( copy first key to 3rd key ) each key 8 bytes
sl@0
   136
			TBuf8<K3DESKeyBytes>  encryptKey(aKey);			
sl@0
   137
			encryptKey.Append(aKey.Ptr(),KDESKeyBytes);
sl@0
   138
			block = C3DESEncryptor::NewLC(encryptKey);			
sl@0
   139
			break;
sl@0
   140
			}
sl@0
   141
sl@0
   142
		case ECipherRC2_CBC_40:
sl@0
   143
		case ECipherRC2_CBC_128:		
sl@0
   144
			block = CRC2Encryptor::NewLC(aKey);
sl@0
   145
			break;
sl@0
   146
sl@0
   147
		case ECipherRC2_CBC_40_16:
sl@0
   148
		case ECipherRC2_CBC_128_16:
sl@0
   149
			block = CRC2Encryptor::NewLC(aKey, KPkcs8CompatibilityBits);
sl@0
   150
			break;
sl@0
   151
sl@0
   152
		case ECipherRC2_CBC_40_5:
sl@0
   153
			block = CRC2Encryptor::NewLC(aKey, KPkcs12CompatibilityBits);
sl@0
   154
			break;	
sl@0
   155
sl@0
   156
		default:
sl@0
   157
			User::Panic(_L("Invalid PBE encryptor"), 1);
sl@0
   158
		}
sl@0
   159
sl@0
   160
	// if aCipher is not stream cipher, create block cipher object
sl@0
   161
	if(aCipher != ECipherARC4_40 && aCipher != ECipherARC4_128)
sl@0
   162
		{			
sl@0
   163
		block = CModeCBCEncryptor::NewL(block, aIV);
sl@0
   164
		CleanupStack::Pop(); //1st block owned by 2nd
sl@0
   165
		CleanupStack::PushL(block);//2nd block
sl@0
   166
		CPadding* padding = CPaddingSSLv3::NewLC(GetBlockBytes(aCipher));
sl@0
   167
		cipher = CBufferedEncryptor::NewL(block, padding);
sl@0
   168
		CleanupStack::Pop(padding); //owned by cipher
sl@0
   169
		CleanupStack::Pop(block); //owned by cipher
sl@0
   170
		}
sl@0
   171
sl@0
   172
	return cipher;
sl@0
   173
	}
sl@0
   174
sl@0
   175
sl@0
   176
CSymmetricCipher* PBE::MakeDecryptorL(TPBECipher aCipher, const TDesC8& aKey, 
sl@0
   177
	const TDesC8& aIV)
sl@0
   178
	{
sl@0
   179
	CSymmetricCipher* cipher = 0;
sl@0
   180
	CBlockTransformation* block = 0;
sl@0
   181
	switch(aCipher)
sl@0
   182
		{
sl@0
   183
		// stream cipher
sl@0
   184
		case ECipherARC4_40:
sl@0
   185
		case ECipherARC4_128:
sl@0
   186
			cipher = CARC4::NewL(aKey, 0);
sl@0
   187
			break;	
sl@0
   188
sl@0
   189
		// block cipher	
sl@0
   190
		case ECipherAES_CBC_128:
sl@0
   191
		case ECipherAES_CBC_192:
sl@0
   192
		case ECipherAES_CBC_256:
sl@0
   193
			block = CAESDecryptor::NewLC(aKey);
sl@0
   194
			break;
sl@0
   195
sl@0
   196
		case ECipherDES_CBC:
sl@0
   197
			block = CDESDecryptor::NewLC(aKey);
sl@0
   198
			break;
sl@0
   199
			
sl@0
   200
		case ECipher3DES_CBC:		
sl@0
   201
			block = C3DESDecryptor::NewLC(aKey);
sl@0
   202
			break;
sl@0
   203
sl@0
   204
		case ECipher2Key3DES_CBC:
sl@0
   205
			{
sl@0
   206
			// Construct 3key from 2 key ( copy first key to 3rd key ) each key 8 bytes
sl@0
   207
			TBuf8<K3DESKeyBytes>  encryptKey(aKey);			
sl@0
   208
			encryptKey.Append(aKey.Ptr(),KDESKeyBytes);
sl@0
   209
			block = C3DESDecryptor::NewLC(encryptKey);			
sl@0
   210
			break;
sl@0
   211
			}
sl@0
   212
sl@0
   213
		case ECipherRC2_CBC_40:
sl@0
   214
		case ECipherRC2_CBC_128:
sl@0
   215
		  	block = CRC2Decryptor::NewLC(aKey);
sl@0
   216
			break;
sl@0
   217
			
sl@0
   218
		case ECipherRC2_CBC_40_16:
sl@0
   219
		case ECipherRC2_CBC_128_16:
sl@0
   220
		  	block = CRC2Decryptor::NewLC(aKey, KPkcs8CompatibilityBits);
sl@0
   221
			break;
sl@0
   222
sl@0
   223
		case ECipherRC2_CBC_40_5:
sl@0
   224
			block = CRC2Decryptor::NewLC(aKey, KPkcs12CompatibilityBits);
sl@0
   225
			break;	
sl@0
   226
sl@0
   227
		default:
sl@0
   228
			User::Panic(_L("Invalid PBE decryptor"), 1);
sl@0
   229
		}
sl@0
   230
sl@0
   231
	// if aCipher is not stream cipher, create block cipher object
sl@0
   232
	if(aCipher != ECipherARC4_40 && aCipher != ECipherARC4_128)
sl@0
   233
		{	
sl@0
   234
		block = CModeCBCDecryptor::NewL(block, aIV);
sl@0
   235
		CleanupStack::Pop(); //1st block owned by 2nd
sl@0
   236
		CleanupStack::PushL(block);//2nd block
sl@0
   237
sl@0
   238
		CPadding* padding = CPaddingSSLv3::NewLC(GetBlockBytes(aCipher));
sl@0
   239
		cipher = CBufferedDecryptor::NewL(block, padding);
sl@0
   240
		CleanupStack::Pop(padding); //owned by cipher
sl@0
   241
		CleanupStack::Pop(block); //owned by cipher
sl@0
   242
		}
sl@0
   243
sl@0
   244
	return cipher;
sl@0
   245
	}