Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / ssl / test / runner / cipher_suites.go
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8         "crypto/aes"
9         "crypto/cipher"
10         "crypto/des"
11         "crypto/hmac"
12         "crypto/md5"
13         "crypto/rc4"
14         "crypto/sha1"
15         "crypto/x509"
16         "hash"
17 )
18
19 // a keyAgreement implements the client and server side of a TLS key agreement
20 // protocol by generating and processing key exchange messages.
21 type keyAgreement interface {
22         // On the server side, the first two methods are called in order.
23
24         // In the case that the key agreement protocol doesn't use a
25         // ServerKeyExchange message, generateServerKeyExchange can return nil,
26         // nil.
27         generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
28         processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
29
30         // On the client side, the next two methods are called in order.
31
32         // This method may not be called if the server doesn't send a
33         // ServerKeyExchange message.
34         processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
35         generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
36 }
37
38 const (
39         // suiteECDH indicates that the cipher suite involves elliptic curve
40         // Diffie-Hellman. This means that it should only be selected when the
41         // client indicates that it supports ECC with a curve and point format
42         // that we're happy with.
43         suiteECDHE = 1 << iota
44         // suiteECDSA indicates that the cipher suite involves an ECDSA
45         // signature and therefore may only be selected when the server's
46         // certificate is ECDSA. If this is not set then the cipher suite is
47         // RSA based.
48         suiteECDSA
49         // suiteTLS12 indicates that the cipher suite should only be advertised
50         // and accepted when using TLS 1.2.
51         suiteTLS12
52         // suiteSHA384 indicates that the cipher suite uses SHA384 as the
53         // handshake hash.
54         suiteSHA384
55 )
56
57 // A cipherSuite is a specific combination of key agreement, cipher and MAC
58 // function. All cipher suites currently assume RSA key agreement.
59 type cipherSuite struct {
60         id uint16
61         // the lengths, in bytes, of the key material needed for each component.
62         keyLen int
63         macLen int
64         ivLen  int
65         ka     func(version uint16) keyAgreement
66         // flags is a bitmask of the suite* values, above.
67         flags  int
68         cipher func(key, iv []byte, isRead bool) interface{}
69         mac    func(version uint16, macKey []byte) macFunction
70         aead   func(key, fixedNonce []byte) cipher.AEAD
71 }
72
73 var cipherSuites = []*cipherSuite{
74         // Ciphersuite order is chosen so that ECDHE comes before plain RSA
75         // and RC4 comes before AES (because of the Lucky13 attack).
76         {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
77         {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
78         {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
79         {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
80         {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherRC4, macSHA1, nil},
81         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
82         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
83         {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
84         {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
85         {TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, dheRSAKA, suiteTLS12, nil, nil, aeadAESGCM},
86         {TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, dheRSAKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
87         {TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
88         {TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, dheRSAKA, 0, cipherAES, macSHA1, nil},
89         {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
90         {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
91         {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
92         {TLS_RSA_WITH_RC4_128_MD5, 16, 16, 0, rsaKA, 0, cipherRC4, macMD5, nil},
93         {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
94         {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
95         {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
96         {TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, dheRSAKA, 0, cipher3DES, macSHA1, nil},
97         {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
98 }
99
100 func cipherRC4(key, iv []byte, isRead bool) interface{} {
101         cipher, _ := rc4.NewCipher(key)
102         return cipher
103 }
104
105 func cipher3DES(key, iv []byte, isRead bool) interface{} {
106         block, _ := des.NewTripleDESCipher(key)
107         if isRead {
108                 return cipher.NewCBCDecrypter(block, iv)
109         }
110         return cipher.NewCBCEncrypter(block, iv)
111 }
112
113 func cipherAES(key, iv []byte, isRead bool) interface{} {
114         block, _ := aes.NewCipher(key)
115         if isRead {
116                 return cipher.NewCBCDecrypter(block, iv)
117         }
118         return cipher.NewCBCEncrypter(block, iv)
119 }
120
121 // macSHA1 returns a macFunction for the given protocol version.
122 func macSHA1(version uint16, key []byte) macFunction {
123         if version == VersionSSL30 {
124                 mac := ssl30MAC{
125                         h:   sha1.New(),
126                         key: make([]byte, len(key)),
127                 }
128                 copy(mac.key, key)
129                 return mac
130         }
131         return tls10MAC{hmac.New(sha1.New, key)}
132 }
133
134 func macMD5(version uint16, key []byte) macFunction {
135         if version == VersionSSL30 {
136                 mac := ssl30MAC{
137                         h:   md5.New(),
138                         key: make([]byte, len(key)),
139                 }
140                 copy(mac.key, key)
141                 return mac
142         }
143         return tls10MAC{hmac.New(md5.New, key)}
144 }
145
146 type macFunction interface {
147         Size() int
148         MAC(digestBuf, seq, header, data []byte) []byte
149 }
150
151 // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
152 // each call.
153 type fixedNonceAEAD struct {
154         // sealNonce and openNonce are buffers where the larger nonce will be
155         // constructed. Since a seal and open operation may be running
156         // concurrently, there is a separate buffer for each.
157         sealNonce, openNonce []byte
158         aead                 cipher.AEAD
159 }
160
161 func (f *fixedNonceAEAD) NonceSize() int { return 8 }
162 func (f *fixedNonceAEAD) Overhead() int  { return f.aead.Overhead() }
163
164 func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
165         copy(f.sealNonce[len(f.sealNonce)-8:], nonce)
166         return f.aead.Seal(out, f.sealNonce, plaintext, additionalData)
167 }
168
169 func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
170         copy(f.openNonce[len(f.openNonce)-8:], nonce)
171         return f.aead.Open(out, f.openNonce, plaintext, additionalData)
172 }
173
174 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
175         aes, err := aes.NewCipher(key)
176         if err != nil {
177                 panic(err)
178         }
179         aead, err := cipher.NewGCM(aes)
180         if err != nil {
181                 panic(err)
182         }
183
184         nonce1, nonce2 := make([]byte, 12), make([]byte, 12)
185         copy(nonce1, fixedNonce)
186         copy(nonce2, fixedNonce)
187
188         return &fixedNonceAEAD{nonce1, nonce2, aead}
189 }
190
191 // ssl30MAC implements the SSLv3 MAC function, as defined in
192 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
193 type ssl30MAC struct {
194         h   hash.Hash
195         key []byte
196 }
197
198 func (s ssl30MAC) Size() int {
199         return s.h.Size()
200 }
201
202 var ssl30Pad1 = [48]byte{0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36}
203
204 var ssl30Pad2 = [48]byte{0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c}
205
206 func (s ssl30MAC) MAC(digestBuf, seq, header, data []byte) []byte {
207         padLength := 48
208         if s.h.Size() == 20 {
209                 padLength = 40
210         }
211
212         s.h.Reset()
213         s.h.Write(s.key)
214         s.h.Write(ssl30Pad1[:padLength])
215         s.h.Write(seq)
216         s.h.Write(header[:1])
217         s.h.Write(header[3:5])
218         s.h.Write(data)
219         digestBuf = s.h.Sum(digestBuf[:0])
220
221         s.h.Reset()
222         s.h.Write(s.key)
223         s.h.Write(ssl30Pad2[:padLength])
224         s.h.Write(digestBuf)
225         return s.h.Sum(digestBuf[:0])
226 }
227
228 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, section 6.2.3.
229 type tls10MAC struct {
230         h hash.Hash
231 }
232
233 func (s tls10MAC) Size() int {
234         return s.h.Size()
235 }
236
237 func (s tls10MAC) MAC(digestBuf, seq, header, data []byte) []byte {
238         s.h.Reset()
239         s.h.Write(seq)
240         s.h.Write(header)
241         s.h.Write(data)
242         return s.h.Sum(digestBuf[:0])
243 }
244
245 func rsaKA(version uint16) keyAgreement {
246         return rsaKeyAgreement{}
247 }
248
249 func ecdheECDSAKA(version uint16) keyAgreement {
250         return &ecdheKeyAgreement{
251                 signedKeyAgreement: signedKeyAgreement{
252                         sigType: signatureECDSA,
253                         version: version,
254                 },
255         }
256 }
257
258 func ecdheRSAKA(version uint16) keyAgreement {
259         return &ecdheKeyAgreement{
260                 signedKeyAgreement: signedKeyAgreement{
261                         sigType: signatureRSA,
262                         version: version,
263                 },
264         }
265 }
266
267 func dheRSAKA(version uint16) keyAgreement {
268         return &dheKeyAgreement{
269                 signedKeyAgreement: signedKeyAgreement{
270                         sigType: signatureRSA,
271                         version: version,
272                 },
273         }
274 }
275
276 // mutualCipherSuite returns a cipherSuite given a list of supported
277 // ciphersuites and the id requested by the peer.
278 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
279         for _, id := range have {
280                 if id == want {
281                         for _, suite := range cipherSuites {
282                                 if suite.id == want {
283                                         return suite
284                                 }
285                         }
286                         return nil
287                 }
288         }
289         return nil
290 }
291
292 // A list of the possible cipher suite ids. Taken from
293 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml
294 const (
295         TLS_RSA_WITH_RC4_128_MD5                uint16 = 0x0004
296         TLS_RSA_WITH_RC4_128_SHA                uint16 = 0x0005
297         TLS_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0x000a
298         TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA       uint16 = 0x0016
299         TLS_RSA_WITH_AES_128_CBC_SHA            uint16 = 0x002f
300         TLS_DHE_RSA_WITH_AES_128_CBC_SHA        uint16 = 0x0033
301         TLS_RSA_WITH_AES_256_CBC_SHA            uint16 = 0x0035
302         TLS_DHE_RSA_WITH_AES_256_CBC_SHA        uint16 = 0x0039
303         TLS_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0x009c
304         TLS_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0x009d
305         TLS_DHE_RSA_WITH_AES_128_GCM_SHA256     uint16 = 0x009e
306         TLS_DHE_RSA_WITH_AES_256_GCM_SHA384     uint16 = 0x009f
307         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA        uint16 = 0xc007
308         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA    uint16 = 0xc009
309         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA    uint16 = 0xc00a
310         TLS_ECDHE_RSA_WITH_RC4_128_SHA          uint16 = 0xc011
311         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA     uint16 = 0xc012
312         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA      uint16 = 0xc013
313         TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA      uint16 = 0xc014
314         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256   uint16 = 0xc02f
315         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
316         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384   uint16 = 0xc030
317         fallbackSCSV                            uint16 = 0x5600
318 )