~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/crypto/tls/DefaultTlsCipherFactory.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.crypto.tls;
 
2
 
 
3
import java.io.IOException;
 
4
 
 
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;
 
14
 
 
15
public class DefaultTlsCipherFactory implements TlsCipherFactory
 
16
{
 
17
    public TlsCipher createCipher(TlsClientContext context, int encryptionAlgorithm, int digestAlgorithm) throws IOException
 
18
    {
 
19
        switch (encryptionAlgorithm)
 
20
        {
 
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);
 
27
            default:
 
28
                throw new TlsFatalAlert(AlertDescription.internal_error);
 
29
        }
 
30
    }
 
31
 
 
32
    protected TlsCipher createAESCipher(TlsClientContext context, int cipherKeySize, int digestAlgorithm) throws IOException
 
33
    {
 
34
        return new TlsBlockCipher(context, createAESBlockCipher(),
 
35
            createAESBlockCipher(), createDigest(digestAlgorithm), createDigest(digestAlgorithm), cipherKeySize);
 
36
    }
 
37
 
 
38
    protected TlsCipher createDESedeCipher(TlsClientContext context, int cipherKeySize, int digestAlgorithm) throws IOException
 
39
    {
 
40
        return new TlsBlockCipher(context, createDESedeBlockCipher(),
 
41
            createDESedeBlockCipher(), createDigest(digestAlgorithm), createDigest(digestAlgorithm), cipherKeySize);
 
42
    }
 
43
 
 
44
    protected BlockCipher createAESBlockCipher()
 
45
    {
 
46
        return new CBCBlockCipher(new AESFastEngine());
 
47
    }
 
48
 
 
49
    protected BlockCipher createDESedeBlockCipher()
 
50
    {
 
51
        return new CBCBlockCipher(new DESedeEngine());
 
52
    }
 
53
 
 
54
    protected Digest createDigest(int digestAlgorithm) throws IOException
 
55
    {
 
56
        switch (digestAlgorithm)
 
57
        {
 
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();
 
66
            default:
 
67
                throw new TlsFatalAlert(AlertDescription.internal_error);
 
68
        }
 
69
    }
 
70
}