1 package org.bouncycastle.jcajce.provider.symmetric; 2 3 // BEGIN android-removed 4 // import java.security.AlgorithmParameters; 5 // import java.security.InvalidAlgorithmParameterException; 6 // END android-removed 7 import java.security.SecureRandom; 8 // BEGIN android-removed 9 // import java.security.spec.AlgorithmParameterSpec; 10 // END android-removed 11 import java.security.spec.InvalidKeySpecException; 12 import java.security.spec.KeySpec; 13 14 import javax.crypto.SecretKey; 15 import javax.crypto.spec.DESedeKeySpec; 16 // BEGIN android-removed 17 // import javax.crypto.spec.IvParameterSpec; 18 // END android-removed 19 import javax.crypto.spec.SecretKeySpec; 20 21 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 22 import org.bouncycastle.crypto.KeyGenerationParameters; 23 import org.bouncycastle.crypto.engines.DESedeEngine; 24 import org.bouncycastle.crypto.engines.DESedeWrapEngine; 25 // BEGIN android-removed 26 // import org.bouncycastle.crypto.engines.RFC3211WrapEngine; 27 // END android-removed 28 import org.bouncycastle.crypto.generators.DESedeKeyGenerator; 29 import org.bouncycastle.crypto.macs.CBCBlockCipherMac; 30 // BEGIN android-removed 31 // import org.bouncycastle.crypto.macs.CFBBlockCipherMac; 32 // import org.bouncycastle.crypto.macs.CMac; 33 // END android-removed 34 import org.bouncycastle.crypto.modes.CBCBlockCipher; 35 import org.bouncycastle.crypto.paddings.ISO7816d4Padding; 36 import org.bouncycastle.jcajce.provider.config.ConfigurableProvider; 37 // BEGIN android-removed 38 // import org.bouncycastle.jcajce.provider.symmetric.util.BaseAlgorithmParameterGenerator; 39 // END android-removed 40 import org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher; 41 import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator; 42 import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac; 43 import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory; 44 import org.bouncycastle.jcajce.provider.symmetric.util.BaseWrapCipher; 45 import org.bouncycastle.jcajce.provider.util.AlgorithmProvider; 46 47 public final class DESede 48 { 49 private DESede() 50 { 51 } 52 53 static public class ECB 54 extends BaseBlockCipher 55 { 56 public ECB() 57 { 58 super(new DESedeEngine()); 59 } 60 } 61 62 static public class CBC 63 extends BaseBlockCipher 64 { 65 public CBC() 66 { 67 super(new CBCBlockCipher(new DESedeEngine()), 64); 68 } 69 } 70 71 // BEGIN android-removed 72 // /** 73 // * DESede CFB8 74 // */ 75 // public static class DESedeCFB8 76 // extends BaseMac 77 // { 78 // public DESedeCFB8() 79 // { 80 // super(new CFBBlockCipherMac(new DESedeEngine())); 81 // } 82 // } 83 // END android-removed 84 85 /** 86 * DESede64 87 */ 88 public static class DESede64 89 extends BaseMac 90 { 91 public DESede64() 92 { 93 super(new CBCBlockCipherMac(new DESedeEngine(), 64)); 94 } 95 } 96 97 /** 98 * DESede64with7816-4Padding 99 */ 100 public static class DESede64with7816d4 101 extends BaseMac 102 { 103 public DESede64with7816d4() 104 { 105 super(new CBCBlockCipherMac(new DESedeEngine(), 64, new ISO7816d4Padding())); 106 } 107 } 108 109 public static class CBCMAC 110 extends BaseMac 111 { 112 public CBCMAC() 113 { 114 super(new CBCBlockCipherMac(new DESedeEngine())); 115 } 116 } 117 118 // BEGIN android-removed 119 // static public class CMAC 120 // extends BaseMac 121 // { 122 // public CMAC() 123 // { 124 // super(new CMac(new DESedeEngine())); 125 // } 126 // } 127 // END android-removed 128 129 public static class Wrap 130 extends BaseWrapCipher 131 { 132 public Wrap() 133 { 134 super(new DESedeWrapEngine()); 135 } 136 } 137 138 // BEGIN android-removed 139 // public static class RFC3211 140 // extends BaseWrapCipher 141 // { 142 // public RFC3211() 143 // { 144 // super(new RFC3211WrapEngine(new DESedeEngine()), 8); 145 // } 146 // } 147 // END android-removed 148 149 /** 150 * DESede - the default for this is to generate a key in 151 * a-b-a format that's 24 bytes long but has 16 bytes of 152 * key material (the first 8 bytes is repeated as the last 153 * 8 bytes). If you give it a size, you'll get just what you 154 * asked for. 155 */ 156 public static class KeyGenerator 157 extends BaseKeyGenerator 158 { 159 private boolean keySizeSet = false; 160 161 public KeyGenerator() 162 { 163 super("DESede", 192, new DESedeKeyGenerator()); 164 } 165 166 protected void engineInit( 167 int keySize, 168 SecureRandom random) 169 { 170 super.engineInit(keySize, random); 171 keySizeSet = true; 172 } 173 174 protected SecretKey engineGenerateKey() 175 { 176 if (uninitialised) 177 { 178 engine.init(new KeyGenerationParameters(new SecureRandom(), defaultKeySize)); 179 uninitialised = false; 180 } 181 182 // 183 // if no key size has been defined generate a 24 byte key in 184 // the a-b-a format 185 // 186 if (!keySizeSet) 187 { 188 byte[] k = engine.generateKey(); 189 190 System.arraycopy(k, 0, k, 16, 8); 191 192 return new SecretKeySpec(k, algName); 193 } 194 else 195 { 196 return new SecretKeySpec(engine.generateKey(), algName); 197 } 198 } 199 } 200 201 /** 202 * generate a desEDE key in the a-b-c format. 203 */ 204 public static class KeyGenerator3 205 extends BaseKeyGenerator 206 { 207 public KeyGenerator3() 208 { 209 super("DESede3", 192, new DESedeKeyGenerator()); 210 } 211 } 212 213 /** 214 * PBEWithSHAAnd3-KeyTripleDES-CBC 215 */ 216 static public class PBEWithSHAAndDES3Key 217 extends BaseBlockCipher 218 { 219 public PBEWithSHAAndDES3Key() 220 { 221 super(new CBCBlockCipher(new DESedeEngine()), PKCS12, SHA1, 192, 8); 222 } 223 } 224 225 /** 226 * PBEWithSHAAnd2-KeyTripleDES-CBC 227 */ 228 static public class PBEWithSHAAndDES2Key 229 extends BaseBlockCipher 230 { 231 public PBEWithSHAAndDES2Key() 232 { 233 super(new CBCBlockCipher(new DESedeEngine()), PKCS12, SHA1, 128, 8); 234 } 235 } 236 237 /** 238 * PBEWithSHAAnd3-KeyTripleDES-CBC 239 */ 240 static public class PBEWithSHAAndDES3KeyFactory 241 extends DES.DESPBEKeyFactory 242 { 243 public PBEWithSHAAndDES3KeyFactory() 244 { 245 super("PBEwithSHAandDES3Key-CBC", PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, true, PKCS12, SHA1, 192, 64); 246 } 247 } 248 249 /** 250 * PBEWithSHAAnd2-KeyTripleDES-CBC 251 */ 252 static public class PBEWithSHAAndDES2KeyFactory 253 extends DES.DESPBEKeyFactory 254 { 255 public PBEWithSHAAndDES2KeyFactory() 256 { 257 super("PBEwithSHAandDES2Key-CBC", PKCSObjectIdentifiers.pbeWithSHAAnd2_KeyTripleDES_CBC, true, PKCS12, SHA1, 128, 64); 258 } 259 } 260 261 // BEGIN android-removed 262 // public static class AlgParamGen 263 // extends BaseAlgorithmParameterGenerator 264 // { 265 // protected void engineInit( 266 // AlgorithmParameterSpec genParamSpec, 267 // SecureRandom random) 268 // throws InvalidAlgorithmParameterException 269 // { 270 // throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for DES parameter generation."); 271 // } 272 273 // protected AlgorithmParameters engineGenerateParameters() 274 // { 275 // byte[] iv = new byte[8]; 276 277 // if (random == null) 278 // { 279 // random = new SecureRandom(); 280 // } 281 282 // random.nextBytes(iv); 283 284 // AlgorithmParameters params; 285 286 // try 287 // { 288 // params = createParametersInstance("DES"); 289 // params.init(new IvParameterSpec(iv)); 290 // } 291 // catch (Exception e) 292 // { 293 // throw new RuntimeException(e.getMessage()); 294 // } 295 296 // return params; 297 // } 298 // } 299 // END android-removed 300 301 static public class KeyFactory 302 extends BaseSecretKeyFactory 303 { 304 public KeyFactory() 305 { 306 super("DESede", null); 307 } 308 309 protected KeySpec engineGetKeySpec( 310 SecretKey key, 311 Class keySpec) 312 throws InvalidKeySpecException 313 { 314 if (keySpec == null) 315 { 316 throw new InvalidKeySpecException("keySpec parameter is null"); 317 } 318 if (key == null) 319 { 320 throw new InvalidKeySpecException("key parameter is null"); 321 } 322 323 if (SecretKeySpec.class.isAssignableFrom(keySpec)) 324 { 325 return new SecretKeySpec(key.getEncoded(), algName); 326 } 327 else if (DESedeKeySpec.class.isAssignableFrom(keySpec)) 328 { 329 byte[] bytes = key.getEncoded(); 330 331 try 332 { 333 if (bytes.length == 16) 334 { 335 byte[] longKey = new byte[24]; 336 337 System.arraycopy(bytes, 0, longKey, 0, 16); 338 System.arraycopy(bytes, 0, longKey, 16, 8); 339 340 return new DESedeKeySpec(longKey); 341 } 342 else 343 { 344 return new DESedeKeySpec(bytes); 345 } 346 } 347 catch (Exception e) 348 { 349 throw new InvalidKeySpecException(e.toString()); 350 } 351 } 352 353 throw new InvalidKeySpecException("Invalid KeySpec"); 354 } 355 356 protected SecretKey engineGenerateSecret( 357 KeySpec keySpec) 358 throws InvalidKeySpecException 359 { 360 if (keySpec instanceof DESedeKeySpec) 361 { 362 DESedeKeySpec desKeySpec = (DESedeKeySpec)keySpec; 363 return new SecretKeySpec(desKeySpec.getKey(), "DESede"); 364 } 365 366 return super.engineGenerateSecret(keySpec); 367 } 368 } 369 370 public static class Mappings 371 extends AlgorithmProvider 372 { 373 private static final String PREFIX = DESede.class.getName(); 374 private static final String PACKAGE = "org.bouncycastle.jcajce.provider.symmetric"; // JDK 1.2 375 376 public Mappings() 377 { 378 } 379 380 public void configure(ConfigurableProvider provider) 381 { 382 provider.addAlgorithm("Cipher.DESEDE", PREFIX + "$ECB"); 383 // BEGIN android-removed 384 // provider.addAlgorithm("Cipher", PKCSObjectIdentifiers.des_EDE3_CBC, PREFIX + "$CBC"); 385 // END android-removed 386 provider.addAlgorithm("Cipher.DESEDEWRAP", PREFIX + "$Wrap"); 387 // BEGIN android-changed 388 provider.addAlgorithm("Alg.Alias.Cipher." + PKCSObjectIdentifiers.id_alg_CMS3DESwrap, "DESEDEWRAP"); 389 // END android-changed 390 // BEGIN android-removed 391 // provider.addAlgorithm("Cipher.DESEDERFC3211WRAP", PREFIX + "$RFC3211"); 392 // provider.addAlgorithm("Alg.Alias.Cipher.DESEDERFC3217WRAP", "DESEDEWRAP"); 393 // END android-removed 394 395 provider.addAlgorithm("Alg.Alias.Cipher.TDEA", "DESEDE"); 396 provider.addAlgorithm("Alg.Alias.Cipher.TDEAWRAP", "DESEDEWRAP"); 397 provider.addAlgorithm("Alg.Alias.KeyGenerator.TDEA", "DESEDE"); 398 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.TDEA", "DESEDE"); 399 // BEGIN android-removed 400 // provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator.TDEA", "DESEDE"); 401 // END android-removed 402 provider.addAlgorithm("Alg.Alias.SecretKeyFactory.TDEA", "DESEDE"); 403 404 if (provider.hasAlgorithm("MessageDigest", "SHA-1")) 405 { 406 provider.addAlgorithm("Cipher.PBEWITHSHAAND3-KEYTRIPLEDES-CBC", PREFIX + "$PBEWithSHAAndDES3Key"); 407 // BEGIN android-removed 408 // provider.addAlgorithm("Cipher.BROKENPBEWITHSHAAND3-KEYTRIPLEDES-CBC", PREFIX + "$BrokePBEWithSHAAndDES3Key"); 409 // provider.addAlgorithm("Cipher.OLDPBEWITHSHAAND3-KEYTRIPLEDES-CBC", PREFIX + "$OldPBEWithSHAAndDES3Key"); 410 // END android-removed 411 provider.addAlgorithm("Cipher.PBEWITHSHAAND2-KEYTRIPLEDES-CBC", PREFIX + "$PBEWithSHAAndDES2Key"); 412 // BEGIN android-removed 413 // provider.addAlgorithm("Cipher.BROKENPBEWITHSHAAND2-KEYTRIPLEDES-CBC", PREFIX + "$BrokePBEWithSHAAndDES2Key"); 414 // END android-removed 415 provider.addAlgorithm("Alg.Alias.Cipher", PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 416 provider.addAlgorithm("Alg.Alias.Cipher", PKCSObjectIdentifiers.pbeWithSHAAnd2_KeyTripleDES_CBC, "PBEWITHSHAAND2-KEYTRIPLEDES-CBC"); 417 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1ANDDESEDE", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 418 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1AND3-KEYTRIPLEDES-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 419 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1AND2-KEYTRIPLEDES-CBC", "PBEWITHSHAAND2-KEYTRIPLEDES-CBC"); 420 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHAAND3-KEYDESEDE-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 421 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHAAND2-KEYDESEDE-CBC", "PBEWITHSHAAND2-KEYTRIPLEDES-CBC"); 422 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1AND3-KEYDESEDE-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 423 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1AND2-KEYDESEDE-CBC", "PBEWITHSHAAND2-KEYTRIPLEDES-CBC"); 424 provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1ANDDESEDE-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 425 } 426 427 provider.addAlgorithm("KeyGenerator.DESEDE", PREFIX + "$KeyGenerator"); 428 // BEGIN android-removed 429 // provider.addAlgorithm("KeyGenerator." + PKCSObjectIdentifiers.des_EDE3_CBC, PREFIX + "$KeyGenerator3"); 430 // provider.addAlgorithm("KeyGenerator.DESEDEWRAP", PREFIX + "$KeyGenerator"); 431 // END android-removed 432 433 provider.addAlgorithm("SecretKeyFactory.DESEDE", PREFIX + "$KeyFactory"); 434 435 // BEGIN android-removed 436 // provider.addAlgorithm("Mac.DESEDECMAC", PREFIX + "$CMAC"); 437 // provider.addAlgorithm("Mac.DESEDEMAC", PREFIX + "$CBCMAC"); 438 // provider.addAlgorithm("Alg.Alias.Mac.DESEDE", "DESEDEMAC"); 439 // 440 // provider.addAlgorithm("Mac.DESEDEMAC/CFB8", PREFIX + "$DESedeCFB8"); 441 // provider.addAlgorithm("Alg.Alias.Mac.DESEDE/CFB8", "DESEDEMAC/CFB8"); 442 // 443 // provider.addAlgorithm("Mac.DESEDEMAC64", PREFIX + "$DESede64"); 444 // provider.addAlgorithm("Alg.Alias.Mac.DESEDE64", "DESEDEMAC64"); 445 // 446 // provider.addAlgorithm("Mac.DESEDEMAC64WITHISO7816-4PADDING", PREFIX + "$DESede64with7816d4"); 447 // provider.addAlgorithm("Alg.Alias.Mac.DESEDE64WITHISO7816-4PADDING", "DESEDEMAC64WITHISO7816-4PADDING"); 448 // provider.addAlgorithm("Alg.Alias.Mac.DESEDEISO9797ALG1MACWITHISO7816-4PADDING", "DESEDEMAC64WITHISO7816-4PADDING"); 449 // provider.addAlgorithm("Alg.Alias.Mac.DESEDEISO9797ALG1WITHISO7816-4PADDING", "DESEDEMAC64WITHISO7816-4PADDING"); 450 // END android-removed 451 452 provider.addAlgorithm("AlgorithmParameters.DESEDE", PACKAGE + ".util.IvAlgorithmParameters"); 453 provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + PKCSObjectIdentifiers.des_EDE3_CBC, "DESEDE"); 454 455 // BEGIN android-removed 456 // provider.addAlgorithm("AlgorithmParameterGenerator.DESEDE", PREFIX + "$AlgParamGen"); 457 // provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + PKCSObjectIdentifiers.des_EDE3_CBC, "DESEDE"); 458 // END android-removed 459 460 provider.addAlgorithm("SecretKeyFactory.PBEWITHSHAAND3-KEYTRIPLEDES-CBC", PREFIX + "$PBEWithSHAAndDES3KeyFactory"); 461 provider.addAlgorithm("SecretKeyFactory.PBEWITHSHAAND2-KEYTRIPLEDES-CBC", PREFIX + "$PBEWithSHAAndDES2KeyFactory"); 462 463 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND3-KEYTRIPLEDES", "PKCS12PBE"); 464 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND2-KEYTRIPLEDES", "PKCS12PBE"); 465 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND3-KEYTRIPLEDES-CBC", "PKCS12PBE"); 466 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND2-KEYTRIPLEDES-CBC", "PKCS12PBE"); 467 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAANDDES3KEY-CBC", "PKCS12PBE"); 468 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAANDDES2KEY-CBC", "PKCS12PBE"); 469 470 provider.addAlgorithm("Alg.Alias.SecretKeyFactory.1.2.840.113549.1.12.1.3", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 471 provider.addAlgorithm("Alg.Alias.SecretKeyFactory.1.2.840.113549.1.12.1.4", "PBEWITHSHAAND2-KEYTRIPLEDES-CBC"); 472 provider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBEWithSHAAnd3KeyTripleDES", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 473 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.1.2.840.113549.1.12.1.3", "PKCS12PBE"); 474 provider.addAlgorithm("Alg.Alias.AlgorithmParameters.1.2.840.113549.1.12.1.4", "PKCS12PBE"); 475 provider.addAlgorithm("Alg.Alias.Cipher.PBEWithSHAAnd3KeyTripleDES", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"); 476 } 477 } 478 } 479