1
package org.bouncycastle.crypto.tls;
3
import java.io.IOException;
5
import org.bouncycastle.crypto.BlockCipher;
6
import org.bouncycastle.crypto.Digest;
7
import org.bouncycastle.crypto.digests.MD5Digest;
8
import org.bouncycastle.crypto.digests.SHA1Digest;
9
import org.bouncycastle.crypto.digests.SHA256Digest;
10
import org.bouncycastle.crypto.digests.SHA384Digest;
11
import org.bouncycastle.crypto.engines.AESFastEngine;
12
import org.bouncycastle.crypto.engines.DESedeEngine;
13
import org.bouncycastle.crypto.modes.CBCBlockCipher;
15
public class DefaultTlsCipherFactory implements TlsCipherFactory
17
public TlsCipher createCipher(TlsClientContext context, int encryptionAlgorithm, int digestAlgorithm) throws IOException
19
switch (encryptionAlgorithm)
21
case EncryptionAlgorithm._3DES_EDE_CBC:
22
return createDESedeCipher(context, 24, digestAlgorithm);
23
case EncryptionAlgorithm.AES_128_CBC:
24
return createAESCipher(context, 16, digestAlgorithm);
25
case EncryptionAlgorithm.AES_256_CBC:
26
return createAESCipher(context, 32, digestAlgorithm);
28
throw new TlsFatalAlert(AlertDescription.internal_error);
32
protected TlsCipher createAESCipher(TlsClientContext context, int cipherKeySize, int digestAlgorithm) throws IOException
34
return new TlsBlockCipher(context, createAESBlockCipher(),
35
createAESBlockCipher(), createDigest(digestAlgorithm), createDigest(digestAlgorithm), cipherKeySize);
38
protected TlsCipher createDESedeCipher(TlsClientContext context, int cipherKeySize, int digestAlgorithm) throws IOException
40
return new TlsBlockCipher(context, createDESedeBlockCipher(),
41
createDESedeBlockCipher(), createDigest(digestAlgorithm), createDigest(digestAlgorithm), cipherKeySize);
44
protected BlockCipher createAESBlockCipher()
46
return new CBCBlockCipher(new AESFastEngine());
49
protected BlockCipher createDESedeBlockCipher()
51
return new CBCBlockCipher(new DESedeEngine());
54
protected Digest createDigest(int digestAlgorithm) throws IOException
56
switch (digestAlgorithm)
58
case DigestAlgorithm.MD5:
59
return new MD5Digest();
60
case DigestAlgorithm.SHA:
61
return new SHA1Digest();
62
case DigestAlgorithm.SHA256:
63
return new SHA256Digest();
64
case DigestAlgorithm.SHA384:
65
return new SHA384Digest();
67
throw new TlsFatalAlert(AlertDescription.internal_error);