]> Cypherpunks.ru repositories - gostls13.git/blob - src/crypto/tls/cipher_suites.go
[dev.boringcrypto] all: merge master into dev.boringcrypto
[gostls13.git] / src / crypto / tls / 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 tls
6
7 import (
8         "crypto/aes"
9         "crypto/cipher"
10         "crypto/des"
11         "crypto/hmac"
12         "crypto/internal/boring"
13         "crypto/rc4"
14         "crypto/sha1"
15         "crypto/sha256"
16         "crypto/x509"
17         "golang_org/x/crypto/chacha20poly1305"
18         "hash"
19 )
20
21 // a keyAgreement implements the client and server side of a TLS key agreement
22 // protocol by generating and processing key exchange messages.
23 type keyAgreement interface {
24         // On the server side, the first two methods are called in order.
25
26         // In the case that the key agreement protocol doesn't use a
27         // ServerKeyExchange message, generateServerKeyExchange can return nil,
28         // nil.
29         generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
30         processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
31
32         // On the client side, the next two methods are called in order.
33
34         // This method may not be called if the server doesn't send a
35         // ServerKeyExchange message.
36         processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
37         generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
38 }
39
40 const (
41         // suiteECDH indicates that the cipher suite involves elliptic curve
42         // Diffie-Hellman. This means that it should only be selected when the
43         // client indicates that it supports ECC with a curve and point format
44         // that we're happy with.
45         suiteECDHE = 1 << iota
46         // suiteECDSA indicates that the cipher suite involves an ECDSA
47         // signature and therefore may only be selected when the server's
48         // certificate is ECDSA. If this is not set then the cipher suite is
49         // RSA based.
50         suiteECDSA
51         // suiteTLS12 indicates that the cipher suite should only be advertised
52         // and accepted when using TLS 1.2.
53         suiteTLS12
54         // suiteSHA384 indicates that the cipher suite uses SHA384 as the
55         // handshake hash.
56         suiteSHA384
57         // suiteDefaultOff indicates that this cipher suite is not included by
58         // default.
59         suiteDefaultOff
60 )
61
62 // A cipherSuite is a specific combination of key agreement, cipher and MAC
63 // function. All cipher suites currently assume RSA key agreement.
64 type cipherSuite struct {
65         id uint16
66         // the lengths, in bytes, of the key material needed for each component.
67         keyLen int
68         macLen int
69         ivLen  int
70         ka     func(version uint16) keyAgreement
71         // flags is a bitmask of the suite* values, above.
72         flags  int
73         cipher func(key, iv []byte, isRead bool) interface{}
74         mac    func(version uint16, macKey []byte) macFunction
75         aead   func(key, fixedNonce []byte) cipher.AEAD
76 }
77
78 var cipherSuites = []*cipherSuite{
79         // Ciphersuite order is chosen so that ECDHE comes before plain RSA and
80         // AEADs are the top preference.
81         {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
82         {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
83         {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
84         {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12, nil, nil, aeadAESGCM},
85         {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
86         {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
87         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
88         {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
89         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
90         {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
91         {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
92         {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECDSA, cipherAES, macSHA1, nil},
93         {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
94         {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
95         {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12 | suiteDefaultOff, cipherAES, macSHA256, nil},
96         {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
97         {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
98         {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
99         {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
100
101         // RC4-based cipher suites are disabled by default.
102         {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, suiteDefaultOff, cipherRC4, macSHA1, nil},
103         {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE | suiteDefaultOff, cipherRC4, macSHA1, nil},
104         {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECDSA | suiteDefaultOff, cipherRC4, macSHA1, nil},
105 }
106
107 func cipherRC4(key, iv []byte, isRead bool) interface{} {
108         cipher, _ := rc4.NewCipher(key)
109         return cipher
110 }
111
112 func cipher3DES(key, iv []byte, isRead bool) interface{} {
113         block, _ := des.NewTripleDESCipher(key)
114         if isRead {
115                 return cipher.NewCBCDecrypter(block, iv)
116         }
117         return cipher.NewCBCEncrypter(block, iv)
118 }
119
120 func cipherAES(key, iv []byte, isRead bool) interface{} {
121         block, _ := aes.NewCipher(key)
122         if isRead {
123                 return cipher.NewCBCDecrypter(block, iv)
124         }
125         return cipher.NewCBCEncrypter(block, iv)
126 }
127
128 // macSHA1 returns a macFunction for the given protocol version.
129 func macSHA1(version uint16, key []byte) macFunction {
130         if version == VersionSSL30 {
131                 mac := ssl30MAC{
132                         h:   sha1.New(),
133                         key: make([]byte, len(key)),
134                 }
135                 copy(mac.key, key)
136                 return mac
137         }
138         h := sha1.New
139         if !boring.Enabled {
140                 h = newConstantTimeHash(h)
141         }
142         return tls10MAC{h: hmac.New(h, key)}
143 }
144
145 // macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
146 // so the given version is ignored.
147 func macSHA256(version uint16, key []byte) macFunction {
148         return tls10MAC{h: hmac.New(sha256.New, key)}
149 }
150
151 type macFunction interface {
152         // Size returns the length of the MAC.
153         Size() int
154         // MAC appends the MAC of (seq, header, data) to out. The extra data is fed
155         // into the MAC after obtaining the result to normalize timing. The result
156         // is only valid until the next invocation of MAC as the buffer is reused.
157         MAC(seq, header, data, extra []byte) []byte
158 }
159
160 type aead interface {
161         cipher.AEAD
162
163         // explicitNonceLen returns the number of bytes of explicit nonce
164         // included in each record. This is eight for older AEADs and
165         // zero for modern ones.
166         explicitNonceLen() int
167 }
168
169 // fixedNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
170 // each call.
171 type fixedNonceAEAD struct {
172         // nonce contains the fixed part of the nonce in the first four bytes.
173         nonce [12]byte
174         aead  cipher.AEAD
175 }
176
177 func (f *fixedNonceAEAD) NonceSize() int        { return 8 }
178 func (f *fixedNonceAEAD) Overhead() int         { return f.aead.Overhead() }
179 func (f *fixedNonceAEAD) explicitNonceLen() int { return 8 }
180
181 func (f *fixedNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
182         copy(f.nonce[4:], nonce)
183         return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
184 }
185
186 func (f *fixedNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
187         copy(f.nonce[4:], nonce)
188         return f.aead.Open(out, f.nonce[:], plaintext, additionalData)
189 }
190
191 // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
192 // before each call.
193 type xorNonceAEAD struct {
194         nonceMask [12]byte
195         aead      cipher.AEAD
196 }
197
198 func (f *xorNonceAEAD) NonceSize() int        { return 8 }
199 func (f *xorNonceAEAD) Overhead() int         { return f.aead.Overhead() }
200 func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
201
202 func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
203         for i, b := range nonce {
204                 f.nonceMask[4+i] ^= b
205         }
206         result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
207         for i, b := range nonce {
208                 f.nonceMask[4+i] ^= b
209         }
210
211         return result
212 }
213
214 func (f *xorNonceAEAD) Open(out, nonce, plaintext, additionalData []byte) ([]byte, error) {
215         for i, b := range nonce {
216                 f.nonceMask[4+i] ^= b
217         }
218         result, err := f.aead.Open(out, f.nonceMask[:], plaintext, additionalData)
219         for i, b := range nonce {
220                 f.nonceMask[4+i] ^= b
221         }
222
223         return result, err
224 }
225
226 type gcmtls interface {
227         NewGCMTLS() (cipher.AEAD, error)
228 }
229
230 func aeadAESGCM(key, fixedNonce []byte) cipher.AEAD {
231         aes, err := aes.NewCipher(key)
232         if err != nil {
233                 panic(err)
234         }
235         var aead cipher.AEAD
236         if aesTLS, ok := aes.(gcmtls); ok {
237                 aead, err = aesTLS.NewGCMTLS()
238         } else {
239                 boring.Unreachable()
240                 aead, err = cipher.NewGCM(aes)
241         }
242         if err != nil {
243                 panic(err)
244         }
245
246         ret := &fixedNonceAEAD{aead: aead}
247         copy(ret.nonce[:], fixedNonce)
248         return ret
249 }
250
251 func aeadChaCha20Poly1305(key, fixedNonce []byte) cipher.AEAD {
252         aead, err := chacha20poly1305.New(key)
253         if err != nil {
254                 panic(err)
255         }
256
257         ret := &xorNonceAEAD{aead: aead}
258         copy(ret.nonceMask[:], fixedNonce)
259         return ret
260 }
261
262 // ssl30MAC implements the SSLv3 MAC function, as defined in
263 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
264 type ssl30MAC struct {
265         h   hash.Hash
266         key []byte
267         buf []byte
268 }
269
270 func (s ssl30MAC) Size() int {
271         return s.h.Size()
272 }
273
274 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}
275
276 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}
277
278 // MAC does not offer constant timing guarantees for SSL v3.0, since it's deemed
279 // useless considering the similar, protocol-level POODLE vulnerability.
280 func (s ssl30MAC) MAC(seq, header, data, extra []byte) []byte {
281         padLength := 48
282         if s.h.Size() == 20 {
283                 padLength = 40
284         }
285
286         s.h.Reset()
287         s.h.Write(s.key)
288         s.h.Write(ssl30Pad1[:padLength])
289         s.h.Write(seq)
290         s.h.Write(header[:1])
291         s.h.Write(header[3:5])
292         s.h.Write(data)
293         s.buf = s.h.Sum(s.buf[:0])
294
295         s.h.Reset()
296         s.h.Write(s.key)
297         s.h.Write(ssl30Pad2[:padLength])
298         s.h.Write(s.buf)
299         return s.h.Sum(s.buf[:0])
300 }
301
302 type constantTimeHash interface {
303         hash.Hash
304         ConstantTimeSum(b []byte) []byte
305 }
306
307 // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
308 // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
309 type cthWrapper struct {
310         h constantTimeHash
311 }
312
313 func (c *cthWrapper) Size() int                   { return c.h.Size() }
314 func (c *cthWrapper) BlockSize() int              { return c.h.BlockSize() }
315 func (c *cthWrapper) Reset()                      { c.h.Reset() }
316 func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
317 func (c *cthWrapper) Sum(b []byte) []byte         { return c.h.ConstantTimeSum(b) }
318
319 func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
320         if boring.Enabled {
321                 // The BoringCrypto SHA1 does not have a constant-time
322                 // checksum function, so don't try to use it.
323                 return h
324         }
325         return func() hash.Hash {
326                 return &cthWrapper{h().(constantTimeHash)}
327         }
328 }
329
330 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
331 type tls10MAC struct {
332         h   hash.Hash
333         buf []byte
334 }
335
336 func (s tls10MAC) Size() int {
337         return s.h.Size()
338 }
339
340 // MAC is guaranteed to take constant time, as long as
341 // len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
342 // the MAC, but is only provided to make the timing profile constant.
343 func (s tls10MAC) MAC(seq, header, data, extra []byte) []byte {
344         s.h.Reset()
345         s.h.Write(seq)
346         s.h.Write(header)
347         s.h.Write(data)
348         res := s.h.Sum(s.buf[:0])
349         if extra != nil {
350                 s.h.Write(extra)
351         }
352         return res
353 }
354
355 func rsaKA(version uint16) keyAgreement {
356         return rsaKeyAgreement{}
357 }
358
359 func ecdheECDSAKA(version uint16) keyAgreement {
360         return &ecdheKeyAgreement{
361                 isRSA:   false,
362                 version: version,
363         }
364 }
365
366 func ecdheRSAKA(version uint16) keyAgreement {
367         return &ecdheKeyAgreement{
368                 isRSA:   true,
369                 version: version,
370         }
371 }
372
373 // mutualCipherSuite returns a cipherSuite given a list of supported
374 // ciphersuites and the id requested by the peer.
375 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
376         for _, id := range have {
377                 if id == want {
378                         for _, suite := range cipherSuites {
379                                 if suite.id == want {
380                                         return suite
381                                 }
382                         }
383                         return nil
384                 }
385         }
386         return nil
387 }
388
389 // A list of cipher suite IDs that are, or have been, implemented by this
390 // package.
391 //
392 // Taken from https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
393 const (
394         TLS_RSA_WITH_RC4_128_SHA                uint16 = 0x0005
395         TLS_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0x000a
396         TLS_RSA_WITH_AES_128_CBC_SHA            uint16 = 0x002f
397         TLS_RSA_WITH_AES_256_CBC_SHA            uint16 = 0x0035
398         TLS_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0x003c
399         TLS_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0x009c
400         TLS_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0x009d
401         TLS_ECDHE_ECDSA_WITH_RC4_128_SHA        uint16 = 0xc007
402         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA    uint16 = 0xc009
403         TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA    uint16 = 0xc00a
404         TLS_ECDHE_RSA_WITH_RC4_128_SHA          uint16 = 0xc011
405         TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA     uint16 = 0xc012
406         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA      uint16 = 0xc013
407         TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA      uint16 = 0xc014
408         TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
409         TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256   uint16 = 0xc027
410         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256   uint16 = 0xc02f
411         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
412         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384   uint16 = 0xc030
413         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
414         TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305    uint16 = 0xcca8
415         TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305  uint16 = 0xcca9
416
417         // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
418         // that the client is doing version fallback. See RFC 7507.
419         TLS_FALLBACK_SCSV uint16 = 0x5600
420 )