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