summary refs log tree commit diff
path: root/crypto/src/crypto/tls/DefaultTlsCipherFactory.cs
blob: f8222ea9f753fec8fc204a0b55d857c50478bdda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.IO;

using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;

namespace Org.BouncyCastle.Crypto.Tls
{
    public class DefaultTlsCipherFactory
        :   AbstractTlsCipherFactory
    {
        /// <exception cref="IOException"></exception>
        public override TlsCipher CreateCipher(TlsContext context, int encryptionAlgorithm, int macAlgorithm)
        {
            switch (encryptionAlgorithm)
            {
            case EncryptionAlgorithm.cls_3DES_EDE_CBC:
                return CreateDesEdeCipher(context, macAlgorithm);
            case EncryptionAlgorithm.AEAD_CHACHA20_POLY1305:
                // NOTE: Ignores macAlgorithm
                return CreateChaCha20Poly1305(context);
            case EncryptionAlgorithm.AES_128_CBC:
                return CreateAESCipher(context, 16, macAlgorithm);
            case EncryptionAlgorithm.AES_128_CCM:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Aes_Ccm(context, 16, 16);
            case EncryptionAlgorithm.AES_128_CCM_8:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Aes_Ccm(context, 16, 8);
            case EncryptionAlgorithm.AES_256_CCM:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Aes_Ccm(context, 32, 16);
            case EncryptionAlgorithm.AES_256_CCM_8:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Aes_Ccm(context, 32, 8);
            case EncryptionAlgorithm.AES_128_GCM:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Aes_Gcm(context, 16, 16);
            case EncryptionAlgorithm.AES_256_CBC:
                return CreateAESCipher(context, 32, macAlgorithm);
            case EncryptionAlgorithm.AES_256_GCM:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Aes_Gcm(context, 32, 16);
            case EncryptionAlgorithm.CAMELLIA_128_CBC:
                return CreateCamelliaCipher(context, 16, macAlgorithm);
            case EncryptionAlgorithm.CAMELLIA_128_GCM:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Camellia_Gcm(context, 16, 16);
            case EncryptionAlgorithm.CAMELLIA_256_CBC:
                return CreateCamelliaCipher(context, 32, macAlgorithm);
            case EncryptionAlgorithm.CAMELLIA_256_GCM:
                // NOTE: Ignores macAlgorithm
                return CreateCipher_Camellia_Gcm(context, 32, 16);
            case EncryptionAlgorithm.NULL:
                return CreateNullCipher(context, macAlgorithm);
            case EncryptionAlgorithm.RC4_128:
                return CreateRC4Cipher(context, 16, macAlgorithm);
            case EncryptionAlgorithm.SEED_CBC:
                return CreateSeedCipher(context, macAlgorithm);
            default:
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsBlockCipher CreateAESCipher(TlsContext context, int cipherKeySize, int macAlgorithm)
        {
            return new TlsBlockCipher(context, CreateAesBlockCipher(), CreateAesBlockCipher(),
                CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), cipherKeySize);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsBlockCipher CreateCamelliaCipher(TlsContext context, int cipherKeySize, int macAlgorithm)
        {
            return new TlsBlockCipher(context, CreateCamelliaBlockCipher(),
                CreateCamelliaBlockCipher(), CreateHMacDigest(macAlgorithm),
                CreateHMacDigest(macAlgorithm), cipherKeySize);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsCipher CreateChaCha20Poly1305(TlsContext context)
        {
            return new Chacha20Poly1305(context);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsAeadCipher CreateCipher_Aes_Ccm(TlsContext context, int cipherKeySize, int macSize)
        {
            return new TlsAeadCipher(context, CreateAeadBlockCipher_Aes_Ccm(),
                CreateAeadBlockCipher_Aes_Ccm(), cipherKeySize, macSize);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsAeadCipher CreateCipher_Aes_Gcm(TlsContext context, int cipherKeySize, int macSize)
        {
            return new TlsAeadCipher(context, CreateAeadBlockCipher_Aes_Gcm(),
                CreateAeadBlockCipher_Aes_Gcm(), cipherKeySize, macSize);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsAeadCipher CreateCipher_Camellia_Gcm(TlsContext context, int cipherKeySize, int macSize)
        {
            return new TlsAeadCipher(context, CreateAeadBlockCipher_Camellia_Gcm(),
                CreateAeadBlockCipher_Camellia_Gcm(), cipherKeySize, macSize);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsBlockCipher CreateDesEdeCipher(TlsContext context, int macAlgorithm)
        {
            return new TlsBlockCipher(context, CreateDesEdeBlockCipher(), CreateDesEdeBlockCipher(),
                CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), 24);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsNullCipher CreateNullCipher(TlsContext context, int macAlgorithm)
        {
            return new TlsNullCipher(context, CreateHMacDigest(macAlgorithm),
                CreateHMacDigest(macAlgorithm));
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsStreamCipher CreateRC4Cipher(TlsContext context, int cipherKeySize, int macAlgorithm)
        {
            return new TlsStreamCipher(context, CreateRC4StreamCipher(), CreateRC4StreamCipher(),
                CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), cipherKeySize, false);
        }

        /// <exception cref="IOException"></exception>
        protected virtual TlsBlockCipher CreateSeedCipher(TlsContext context, int macAlgorithm)
        {
            return new TlsBlockCipher(context, CreateSeedBlockCipher(), CreateSeedBlockCipher(),
                CreateHMacDigest(macAlgorithm), CreateHMacDigest(macAlgorithm), 16);
        }

        protected virtual IBlockCipher CreateAesEngine()
        {
            return new AesEngine();
        }

        protected virtual IBlockCipher CreateCamelliaEngine()
        {
            return new CamelliaEngine();
        }

        protected virtual IBlockCipher CreateAesBlockCipher()
        {
            return new CbcBlockCipher(CreateAesEngine());
        }

        protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Ccm()
        {
            return new CcmBlockCipher(CreateAesEngine());
        }

        protected virtual IAeadBlockCipher CreateAeadBlockCipher_Aes_Gcm()
        {
            // TODO Consider allowing custom configuration of multiplier
            return new GcmBlockCipher(CreateAesEngine());
        }

        protected virtual IAeadBlockCipher CreateAeadBlockCipher_Camellia_Gcm()
        {
            // TODO Consider allowing custom configuration of multiplier
            return new GcmBlockCipher(CreateCamelliaEngine());
        }

        protected virtual IBlockCipher CreateCamelliaBlockCipher()
        {
            return new CbcBlockCipher(CreateCamelliaEngine());
        }

        protected virtual IBlockCipher CreateDesEdeBlockCipher()
        {
            return new CbcBlockCipher(new DesEdeEngine());
        }

        protected virtual IStreamCipher CreateRC4StreamCipher()
        {
            return new RC4Engine();
        }

        protected virtual IBlockCipher CreateSeedBlockCipher()
        {
            return new CbcBlockCipher(new SeedEngine());
        }

        /// <exception cref="IOException"></exception>
        protected virtual IDigest CreateHMacDigest(int macAlgorithm)
        {
            switch (macAlgorithm)
            {
            case MacAlgorithm.cls_null:
                return null;
            case MacAlgorithm.hmac_md5:
                return TlsUtilities.CreateHash(HashAlgorithm.md5);
            case MacAlgorithm.hmac_sha1:
                return TlsUtilities.CreateHash(HashAlgorithm.sha1);
            case MacAlgorithm.hmac_sha256:
                return TlsUtilities.CreateHash(HashAlgorithm.sha256);
            case MacAlgorithm.hmac_sha384:
                return TlsUtilities.CreateHash(HashAlgorithm.sha384);
            case MacAlgorithm.hmac_sha512:
                return TlsUtilities.CreateHash(HashAlgorithm.sha512);
            default:
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }
        }
    }
}