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.
13 "crypto/internal/boring"
18 "golang_org/x/crypto/chacha20poly1305"
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.
27 // In the case that the key agreement protocol doesn't use a
28 // ServerKeyExchange message, generateServerKeyExchange can return nil,
30 generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
31 processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
33 // On the client side, the next two methods are called in order.
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)
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
52 // suiteTLS12 indicates that the cipher suite should only be advertised
53 // and accepted when using TLS 1.2.
55 // suiteSHA384 indicates that the cipher suite uses SHA384 as the
58 // suiteDefaultOff indicates that this cipher suite is not included by
63 // A cipherSuite is a specific combination of key agreement, cipher and MAC function.
64 type cipherSuite struct {
66 // the lengths, in bytes, of the key material needed for each component.
70 ka func(version uint16) keyAgreement
71 // flags is a bitmask of the suite* values, above.
73 cipher func(key, iv []byte, isRead bool) interface{}
74 mac func(version uint16, macKey []byte) macFunction
75 aead func(key, fixedNonce []byte) aead
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},
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},
107 // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
108 // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
109 type cipherSuiteTLS13 struct {
112 aead func(key, fixedNonce []byte) aead
116 var cipherSuitesTLS13 = []*cipherSuiteTLS13{
117 {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
118 {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
119 {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
122 func cipherRC4(key, iv []byte, isRead bool) interface{} {
123 cipher, _ := rc4.NewCipher(key)
127 func cipher3DES(key, iv []byte, isRead bool) interface{} {
128 block, _ := des.NewTripleDESCipher(key)
130 return cipher.NewCBCDecrypter(block, iv)
132 return cipher.NewCBCEncrypter(block, iv)
135 func cipherAES(key, iv []byte, isRead bool) interface{} {
136 block, _ := aes.NewCipher(key)
138 return cipher.NewCBCDecrypter(block, iv)
140 return cipher.NewCBCEncrypter(block, iv)
143 // macSHA1 returns a macFunction for the given protocol version.
144 func macSHA1(version uint16, key []byte) macFunction {
145 if version == VersionSSL30 {
148 key: make([]byte, len(key)),
155 h = newConstantTimeHash(h)
157 return tls10MAC{h: hmac.New(h, key)}
160 // macSHA256 returns a SHA-256 based MAC. These are only supported in TLS 1.2
161 // so the given version is ignored.
162 func macSHA256(version uint16, key []byte) macFunction {
163 return tls10MAC{h: hmac.New(sha256.New, key)}
166 type macFunction interface {
167 // Size returns the length of the MAC.
169 // MAC appends the MAC of (seq, header, data) to out. The extra data is fed
170 // into the MAC after obtaining the result to normalize timing. The result
171 // is only valid until the next invocation of MAC as the buffer is reused.
172 MAC(seq, header, data, extra []byte) []byte
175 type aead interface {
178 // explicitNonceLen returns the number of bytes of explicit nonce
179 // included in each record. This is eight for older AEADs and
180 // zero for modern ones.
181 explicitNonceLen() int
186 noncePrefixLength = 4
189 // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
191 type prefixNonceAEAD struct {
192 // nonce contains the fixed part of the nonce in the first four bytes.
193 nonce [aeadNonceLength]byte
197 func (f *prefixNonceAEAD) NonceSize() int { return aeadNonceLength - noncePrefixLength }
198 func (f *prefixNonceAEAD) Overhead() int { return f.aead.Overhead() }
199 func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
201 func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
202 copy(f.nonce[4:], nonce)
203 return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
206 func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
207 copy(f.nonce[4:], nonce)
208 return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
211 // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
213 type xorNonceAEAD struct {
214 nonceMask [aeadNonceLength]byte
218 func (f *xorNonceAEAD) NonceSize() int { return 8 } // 64-bit sequence number
219 func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
220 func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
222 func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
223 for i, b := range nonce {
224 f.nonceMask[4+i] ^= b
226 result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
227 for i, b := range nonce {
228 f.nonceMask[4+i] ^= b
234 func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
235 for i, b := range nonce {
236 f.nonceMask[4+i] ^= b
238 result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
239 for i, b := range nonce {
240 f.nonceMask[4+i] ^= b
246 type gcmtls interface {
247 NewGCMTLS() (cipher.AEAD, error)
250 func aeadAESGCM(key, noncePrefix []byte) aead {
251 if len(noncePrefix) != noncePrefixLength {
252 panic("tls: internal error: wrong nonce length")
254 aes, err := aes.NewCipher(key)
259 if aesTLS, ok := aes.(gcmtls); ok {
260 aead, err = aesTLS.NewGCMTLS()
263 aead, err = cipher.NewGCM(aes)
269 ret := &prefixNonceAEAD{aead: aead}
270 copy(ret.nonce[:], noncePrefix)
274 func aeadAESGCMTLS13(key, nonceMask []byte) aead {
275 if len(nonceMask) != aeadNonceLength {
276 panic("tls: internal error: wrong nonce length")
278 aes, err := aes.NewCipher(key)
282 aead, err := cipher.NewGCM(aes)
287 ret := &xorNonceAEAD{aead: aead}
288 copy(ret.nonceMask[:], nonceMask)
292 func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
293 if len(nonceMask) != aeadNonceLength {
294 panic("tls: internal error: wrong nonce length")
296 aead, err := chacha20poly1305.New(key)
301 ret := &xorNonceAEAD{aead: aead}
302 copy(ret.nonceMask[:], nonceMask)
306 // ssl30MAC implements the SSLv3 MAC function, as defined in
307 // www.mozilla.org/projects/security/pki/nss/ssl/draft302.txt section 5.2.3.1
308 type ssl30MAC struct {
314 func (s ssl30MAC) Size() int {
318 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}
320 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}
322 // MAC does not offer constant timing guarantees for SSL v3.0, since it's deemed
323 // useless considering the similar, protocol-level POODLE vulnerability.
324 func (s ssl30MAC) MAC(seq, header, data, extra []byte) []byte {
326 if s.h.Size() == 20 {
332 s.h.Write(ssl30Pad1[:padLength])
334 s.h.Write(header[:1])
335 s.h.Write(header[3:5])
337 s.buf = s.h.Sum(s.buf[:0])
341 s.h.Write(ssl30Pad2[:padLength])
343 return s.h.Sum(s.buf[:0])
346 type constantTimeHash interface {
348 ConstantTimeSum(b []byte) []byte
351 // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
352 // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
353 type cthWrapper struct {
357 func (c *cthWrapper) Size() int { return c.h.Size() }
358 func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
359 func (c *cthWrapper) Reset() { c.h.Reset() }
360 func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
361 func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
363 func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
365 // The BoringCrypto SHA1 does not have a constant-time
366 // checksum function, so don't try to use it.
369 return func() hash.Hash {
370 return &cthWrapper{h().(constantTimeHash)}
374 // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
375 type tls10MAC struct {
380 func (s tls10MAC) Size() int {
384 // MAC is guaranteed to take constant time, as long as
385 // len(seq)+len(header)+len(data)+len(extra) is constant. extra is not fed into
386 // the MAC, but is only provided to make the timing profile constant.
387 func (s tls10MAC) MAC(seq, header, data, extra []byte) []byte {
392 res := s.h.Sum(s.buf[:0])
399 func rsaKA(version uint16) keyAgreement {
400 return rsaKeyAgreement{}
403 func ecdheECDSAKA(version uint16) keyAgreement {
404 return &ecdheKeyAgreement{
410 func ecdheRSAKA(version uint16) keyAgreement {
411 return &ecdheKeyAgreement{
417 // mutualCipherSuite returns a cipherSuite given a list of supported
418 // ciphersuites and the id requested by the peer.
419 func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
420 for _, id := range have {
422 for _, suite := range cipherSuites {
423 if suite.id == want {
433 func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
434 for _, id := range have {
436 for _, suite := range cipherSuitesTLS13 {
437 if suite.id == want {
447 // A list of cipher suite IDs that are, or have been, implemented by this
450 // Taken from https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
452 // TLS 1.0 - 1.2 cipher suites.
453 TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
454 TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
455 TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
456 TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
457 TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
458 TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
459 TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
460 TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
461 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
462 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
463 TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
464 TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
465 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
466 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
467 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
468 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
469 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
470 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
471 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
472 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
473 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca8
474 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 uint16 = 0xcca9
476 // TLS 1.3 cipher suites.
477 TLS_AES_128_GCM_SHA256 uint16 = 0x1301
478 TLS_AES_256_GCM_SHA384 uint16 = 0x1302
479 TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
481 // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
482 // that the client is doing version fallback. See RFC 7507.
483 TLS_FALLBACK_SCSV uint16 = 0x5600