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