cipher_suites.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. package tls
  5. import (
  6. "crypto"
  7. "crypto/aes"
  8. "crypto/cipher"
  9. "crypto/des"
  10. "crypto/hmac"
  11. "crypto/rc4"
  12. "crypto/sha1"
  13. "crypto/sha256"
  14. "fmt"
  15. "hash"
  16. "runtime"
  17. "golang.org/x/crypto/chacha20poly1305"
  18. "golang.org/x/sys/cpu"
  19. )
  20. // CipherSuite is a TLS cipher suite. Note that most functions in this package
  21. // accept and expose cipher suite IDs instead of this type.
  22. type CipherSuite struct {
  23. ID uint16
  24. Name string
  25. // Supported versions is the list of TLS protocol versions that can
  26. // negotiate this cipher suite.
  27. SupportedVersions []uint16
  28. // Insecure is true if the cipher suite has known security issues
  29. // due to its primitives, design, or implementation.
  30. Insecure bool
  31. }
  32. var (
  33. supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
  34. supportedOnlyTLS12 = []uint16{VersionTLS12}
  35. supportedOnlyTLS13 = []uint16{VersionTLS13}
  36. )
  37. // CipherSuites returns a list of cipher suites currently implemented by this
  38. // package, excluding those with security issues, which are returned by
  39. // InsecureCipherSuites.
  40. //
  41. // The list is sorted by ID. Note that the default cipher suites selected by
  42. // this package might depend on logic that can't be captured by a static list,
  43. // and might not match those returned by this function.
  44. func CipherSuites() []*CipherSuite {
  45. return []*CipherSuite{
  46. {TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  47. {TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  48. {TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  49. {TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  50. {TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
  51. {TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
  52. {TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
  53. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  54. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  55. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
  56. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
  57. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  58. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  59. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
  60. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
  61. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
  62. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
  63. }
  64. }
  65. // InsecureCipherSuites returns a list of cipher suites currently implemented by
  66. // this package and which have security issues.
  67. //
  68. // Most applications should not use the cipher suites in this list, and should
  69. // only use those returned by CipherSuites.
  70. func InsecureCipherSuites() []*CipherSuite {
  71. // This list includes RC4, CBC_SHA256, and 3DES cipher suites. See
  72. // cipherSuitesPreferenceOrder for details.
  73. return []*CipherSuite{
  74. {TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  75. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
  76. {TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  77. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  78. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
  79. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
  80. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  81. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
  82. }
  83. }
  84. // CipherSuiteName returns the standard name for the passed cipher suite ID
  85. // (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
  86. // of the ID value if the cipher suite is not implemented by this package.
  87. func CipherSuiteName(id uint16) string {
  88. for _, c := range CipherSuites() {
  89. if c.ID == id {
  90. return c.Name
  91. }
  92. }
  93. for _, c := range InsecureCipherSuites() {
  94. if c.ID == id {
  95. return c.Name
  96. }
  97. }
  98. return fmt.Sprintf("0x%04X", id)
  99. }
  100. const (
  101. // suiteECDHE indicates that the cipher suite involves elliptic curve
  102. // Diffie-Hellman. This means that it should only be selected when the
  103. // client indicates that it supports ECC with a curve and point format
  104. // that we're happy with.
  105. suiteECDHE = 1 << iota
  106. // suiteECSign indicates that the cipher suite involves an ECDSA or
  107. // EdDSA signature and therefore may only be selected when the server's
  108. // certificate is ECDSA or EdDSA. If this is not set then the cipher suite
  109. // is RSA based.
  110. suiteECSign
  111. // suiteTLS12 indicates that the cipher suite should only be advertised
  112. // and accepted when using TLS 1.2.
  113. suiteTLS12
  114. // suiteSHA384 indicates that the cipher suite uses SHA384 as the
  115. // handshake hash.
  116. suiteSHA384
  117. )
  118. // A cipherSuite is a TLS 1.0–1.2 cipher suite, and defines the key exchange
  119. // mechanism, as well as the cipher+MAC pair or the AEAD.
  120. type cipherSuite struct {
  121. id uint16
  122. // the lengths, in bytes, of the key material needed for each component.
  123. keyLen int
  124. macLen int
  125. ivLen int
  126. ka func(version uint16) keyAgreement
  127. // flags is a bitmask of the suite* values, above.
  128. flags int
  129. cipher func(key, iv []byte, isRead bool) any
  130. mac func(key []byte) hash.Hash
  131. aead func(key, fixedNonce []byte) aead
  132. }
  133. var cipherSuites = []*cipherSuite{ // TODO: replace with a map, since the order doesn't matter.
  134. {TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  135. {TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
  136. {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
  137. {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
  138. {TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  139. {TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  140. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
  141. {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  142. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, cipherAES, macSHA256, nil},
  143. {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
  144. {TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
  145. {TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
  146. {TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
  147. {TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
  148. {TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
  149. {TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  150. {TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
  151. {TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
  152. {TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
  153. {TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
  154. {TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
  155. {TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherRC4, macSHA1, nil},
  156. }
  157. // selectCipherSuite returns the first TLS 1.0–1.2 cipher suite from ids which
  158. // is also in supportedIDs and passes the ok filter.
  159. func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
  160. for _, id := range ids {
  161. candidate := cipherSuiteByID(id)
  162. if candidate == nil || !ok(candidate) {
  163. continue
  164. }
  165. for _, suppID := range supportedIDs {
  166. if id == suppID {
  167. return candidate
  168. }
  169. }
  170. }
  171. return nil
  172. }
  173. // A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
  174. // algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
  175. type cipherSuiteTLS13 struct {
  176. id uint16
  177. keyLen int
  178. aead func(key, fixedNonce []byte) aead
  179. hash crypto.Hash
  180. }
  181. var cipherSuitesTLS13 = []*cipherSuiteTLS13{ // TODO: replace with a map.
  182. {TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
  183. {TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
  184. {TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
  185. }
  186. // cipherSuitesPreferenceOrder is the order in which we'll select (on the
  187. // server) or advertise (on the client) TLS 1.0–1.2 cipher suites.
  188. //
  189. // Cipher suites are filtered but not reordered based on the application and
  190. // peer's preferences, meaning we'll never select a suite lower in this list if
  191. // any higher one is available. This makes it more defensible to keep weaker
  192. // cipher suites enabled, especially on the server side where we get the last
  193. // word, since there are no known downgrade attacks on cipher suites selection.
  194. //
  195. // The list is sorted by applying the following priority rules, stopping at the
  196. // first (most important) applicable one:
  197. //
  198. // - Anything else comes before RC4
  199. //
  200. // RC4 has practically exploitable biases. See https://www.rc4nomore.com.
  201. //
  202. // - Anything else comes before CBC_SHA256
  203. //
  204. // SHA-256 variants of the CBC ciphersuites don't implement any Lucky13
  205. // countermeasures. See http://www.isg.rhul.ac.uk/tls/Lucky13.html and
  206. // https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
  207. //
  208. // - Anything else comes before 3DES
  209. //
  210. // 3DES has 64-bit blocks, which makes it fundamentally susceptible to
  211. // birthday attacks. See https://sweet32.info.
  212. //
  213. // - ECDHE comes before anything else
  214. //
  215. // Once we got the broken stuff out of the way, the most important
  216. // property a cipher suite can have is forward secrecy. We don't
  217. // implement FFDHE, so that means ECDHE.
  218. //
  219. // - AEADs come before CBC ciphers
  220. //
  221. // Even with Lucky13 countermeasures, MAC-then-Encrypt CBC cipher suites
  222. // are fundamentally fragile, and suffered from an endless sequence of
  223. // padding oracle attacks. See https://eprint.iacr.org/2015/1129,
  224. // https://www.imperialviolet.org/2014/12/08/poodleagain.html, and
  225. // https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/.
  226. //
  227. // - AES comes before ChaCha20
  228. //
  229. // When AES hardware is available, AES-128-GCM and AES-256-GCM are faster
  230. // than ChaCha20Poly1305.
  231. //
  232. // When AES hardware is not available, AES-128-GCM is one or more of: much
  233. // slower, way more complex, and less safe (because not constant time)
  234. // than ChaCha20Poly1305.
  235. //
  236. // We use this list if we think both peers have AES hardware, and
  237. // cipherSuitesPreferenceOrderNoAES otherwise.
  238. //
  239. // - AES-128 comes before AES-256
  240. //
  241. // The only potential advantages of AES-256 are better multi-target
  242. // margins, and hypothetical post-quantum properties. Neither apply to
  243. // TLS, and AES-256 is slower due to its four extra rounds (which don't
  244. // contribute to the advantages above).
  245. //
  246. // - ECDSA comes before RSA
  247. //
  248. // The relative order of ECDSA and RSA cipher suites doesn't matter,
  249. // as they depend on the certificate. Pick one to get a stable order.
  250. var cipherSuitesPreferenceOrder = []uint16{
  251. // AEADs w/ ECDHE
  252. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  253. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  254. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  255. // CBC w/ ECDHE
  256. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  257. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  258. // AEADs w/o ECDHE
  259. TLS_RSA_WITH_AES_128_GCM_SHA256,
  260. TLS_RSA_WITH_AES_256_GCM_SHA384,
  261. // CBC w/o ECDHE
  262. TLS_RSA_WITH_AES_128_CBC_SHA,
  263. TLS_RSA_WITH_AES_256_CBC_SHA,
  264. // 3DES
  265. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  266. TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  267. // CBC_SHA256
  268. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  269. TLS_RSA_WITH_AES_128_CBC_SHA256,
  270. // RC4
  271. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  272. TLS_RSA_WITH_RC4_128_SHA,
  273. }
  274. var cipherSuitesPreferenceOrderNoAES = []uint16{
  275. // ChaCha20Poly1305
  276. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  277. // AES-GCM w/ ECDHE
  278. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  279. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  280. // The rest of cipherSuitesPreferenceOrder.
  281. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  282. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  283. TLS_RSA_WITH_AES_128_GCM_SHA256,
  284. TLS_RSA_WITH_AES_256_GCM_SHA384,
  285. TLS_RSA_WITH_AES_128_CBC_SHA,
  286. TLS_RSA_WITH_AES_256_CBC_SHA,
  287. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  288. TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  289. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  290. TLS_RSA_WITH_AES_128_CBC_SHA256,
  291. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  292. TLS_RSA_WITH_RC4_128_SHA,
  293. }
  294. // disabledCipherSuites are not used unless explicitly listed in
  295. // Config.CipherSuites. They MUST be at the end of cipherSuitesPreferenceOrder.
  296. var disabledCipherSuites = []uint16{
  297. // CBC_SHA256
  298. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
  299. TLS_RSA_WITH_AES_128_CBC_SHA256,
  300. // RC4
  301. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
  302. TLS_RSA_WITH_RC4_128_SHA,
  303. }
  304. var (
  305. defaultCipherSuitesLen = len(cipherSuitesPreferenceOrder) - len(disabledCipherSuites)
  306. defaultCipherSuites = cipherSuitesPreferenceOrder[:defaultCipherSuitesLen]
  307. )
  308. // defaultCipherSuitesTLS13 is also the preference order, since there are no
  309. // disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
  310. // cipherSuitesPreferenceOrder applies.
  311. var defaultCipherSuitesTLS13 = []uint16{
  312. TLS_AES_128_GCM_SHA256,
  313. TLS_AES_256_GCM_SHA384,
  314. TLS_CHACHA20_POLY1305_SHA256,
  315. }
  316. var defaultCipherSuitesTLS13NoAES = []uint16{
  317. TLS_CHACHA20_POLY1305_SHA256,
  318. TLS_AES_128_GCM_SHA256,
  319. TLS_AES_256_GCM_SHA384,
  320. }
  321. var (
  322. hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
  323. hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
  324. // Keep in sync with crypto/aes/cipher_s390x.go.
  325. hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
  326. (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
  327. hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
  328. runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
  329. runtime.GOARCH == "s390x" && hasGCMAsmS390X
  330. )
  331. var aesgcmCiphers = map[uint16]bool{
  332. // TLS 1.2
  333. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: true,
  334. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: true,
  335. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
  336. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
  337. // TLS 1.3
  338. TLS_AES_128_GCM_SHA256: true,
  339. TLS_AES_256_GCM_SHA384: true,
  340. }
  341. var nonAESGCMAEADCiphers = map[uint16]bool{
  342. // TLS 1.2
  343. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305: true,
  344. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: true,
  345. // TLS 1.3
  346. TLS_CHACHA20_POLY1305_SHA256: true,
  347. }
  348. // aesgcmPreferred returns whether the first known cipher in the preference list
  349. // is an AES-GCM cipher, implying the peer has hardware support for it.
  350. func aesgcmPreferred(ciphers []uint16) bool {
  351. for _, cID := range ciphers {
  352. if c := cipherSuiteByID(cID); c != nil {
  353. return aesgcmCiphers[cID]
  354. }
  355. if c := cipherSuiteTLS13ByID(cID); c != nil {
  356. return aesgcmCiphers[cID]
  357. }
  358. }
  359. return false
  360. }
  361. func cipherRC4(key, iv []byte, isRead bool) any {
  362. cipher, _ := rc4.NewCipher(key)
  363. return cipher
  364. }
  365. func cipher3DES(key, iv []byte, isRead bool) any {
  366. block, _ := des.NewTripleDESCipher(key)
  367. if isRead {
  368. return cipher.NewCBCDecrypter(block, iv)
  369. }
  370. return cipher.NewCBCEncrypter(block, iv)
  371. }
  372. func cipherAES(key, iv []byte, isRead bool) any {
  373. block, _ := aes.NewCipher(key)
  374. if isRead {
  375. return cipher.NewCBCDecrypter(block, iv)
  376. }
  377. return cipher.NewCBCEncrypter(block, iv)
  378. }
  379. // macSHA1 returns a SHA-1 based constant time MAC.
  380. func macSHA1(key []byte) hash.Hash {
  381. return hmac.New(newConstantTimeHash(sha1.New), key)
  382. }
  383. // macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
  384. // is currently only used in disabled-by-default cipher suites.
  385. func macSHA256(key []byte) hash.Hash {
  386. return hmac.New(sha256.New, key)
  387. }
  388. type aead interface {
  389. cipher.AEAD
  390. // explicitNonceLen returns the number of bytes of explicit nonce
  391. // included in each record. This is eight for older AEADs and
  392. // zero for modern ones.
  393. explicitNonceLen() int
  394. }
  395. const (
  396. aeadNonceLength = 12
  397. noncePrefixLength = 4
  398. )
  399. // prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
  400. // each call.
  401. type prefixNonceAEAD struct {
  402. // nonce contains the fixed part of the nonce in the first four bytes.
  403. nonce [aeadNonceLength]byte
  404. aead cipher.AEAD
  405. }
  406. func (f *prefixNonceAEAD) NonceSize() int { return aeadNonceLength - noncePrefixLength }
  407. func (f *prefixNonceAEAD) Overhead() int { return f.aead.Overhead() }
  408. func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
  409. func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  410. copy(f.nonce[4:], nonce)
  411. return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
  412. }
  413. func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  414. copy(f.nonce[4:], nonce)
  415. return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
  416. }
  417. // xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
  418. // before each call.
  419. type xorNonceAEAD struct {
  420. nonceMask [aeadNonceLength]byte
  421. aead cipher.AEAD
  422. }
  423. func (f *xorNonceAEAD) NonceSize() int { return 8 } // 64-bit sequence number
  424. func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
  425. func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
  426. func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
  427. for i, b := range nonce {
  428. f.nonceMask[4+i] ^= b
  429. }
  430. result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
  431. for i, b := range nonce {
  432. f.nonceMask[4+i] ^= b
  433. }
  434. return result
  435. }
  436. func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
  437. for i, b := range nonce {
  438. f.nonceMask[4+i] ^= b
  439. }
  440. result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
  441. for i, b := range nonce {
  442. f.nonceMask[4+i] ^= b
  443. }
  444. return result, err
  445. }
  446. func aeadAESGCM(key, noncePrefix []byte) aead {
  447. if len(noncePrefix) != noncePrefixLength {
  448. panic("tls: internal error: wrong nonce length")
  449. }
  450. aes, err := aes.NewCipher(key)
  451. if err != nil {
  452. panic(err)
  453. }
  454. aead, err := cipher.NewGCM(aes)
  455. if err != nil {
  456. panic(err)
  457. }
  458. ret := &prefixNonceAEAD{aead: aead}
  459. copy(ret.nonce[:], noncePrefix)
  460. return ret
  461. }
  462. func aeadAESGCMTLS13(key, nonceMask []byte) aead {
  463. if len(nonceMask) != aeadNonceLength {
  464. panic("tls: internal error: wrong nonce length")
  465. }
  466. aes, err := aes.NewCipher(key)
  467. if err != nil {
  468. panic(err)
  469. }
  470. aead, err := cipher.NewGCM(aes)
  471. if err != nil {
  472. panic(err)
  473. }
  474. ret := &xorNonceAEAD{aead: aead}
  475. copy(ret.nonceMask[:], nonceMask)
  476. return ret
  477. }
  478. func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
  479. if len(nonceMask) != aeadNonceLength {
  480. panic("tls: internal error: wrong nonce length")
  481. }
  482. aead, err := chacha20poly1305.New(key)
  483. if err != nil {
  484. panic(err)
  485. }
  486. ret := &xorNonceAEAD{aead: aead}
  487. copy(ret.nonceMask[:], nonceMask)
  488. return ret
  489. }
  490. type constantTimeHash interface {
  491. hash.Hash
  492. ConstantTimeSum(b []byte) []byte
  493. }
  494. // cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
  495. // with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
  496. type cthWrapper struct {
  497. h constantTimeHash
  498. }
  499. func (c *cthWrapper) Size() int { return c.h.Size() }
  500. func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
  501. func (c *cthWrapper) Reset() { c.h.Reset() }
  502. func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
  503. func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
  504. func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
  505. return func() hash.Hash {
  506. return &cthWrapper{h().(constantTimeHash)}
  507. }
  508. }
  509. // tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
  510. func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
  511. h.Reset()
  512. h.Write(seq)
  513. h.Write(header)
  514. h.Write(data)
  515. res := h.Sum(out)
  516. if extra != nil {
  517. h.Write(extra)
  518. }
  519. return res
  520. }
  521. func rsaKA(version uint16) keyAgreement {
  522. return rsaKeyAgreement{}
  523. }
  524. func ecdheECDSAKA(version uint16) keyAgreement {
  525. return &ecdheKeyAgreement{
  526. isRSA: false,
  527. version: version,
  528. }
  529. }
  530. func ecdheRSAKA(version uint16) keyAgreement {
  531. return &ecdheKeyAgreement{
  532. isRSA: true,
  533. version: version,
  534. }
  535. }
  536. // mutualCipherSuite returns a cipherSuite given a list of supported
  537. // ciphersuites and the id requested by the peer.
  538. func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
  539. for _, id := range have {
  540. if id == want {
  541. return cipherSuiteByID(id)
  542. }
  543. }
  544. return nil
  545. }
  546. func cipherSuiteByID(id uint16) *cipherSuite {
  547. for _, cipherSuite := range cipherSuites {
  548. if cipherSuite.id == id {
  549. return cipherSuite
  550. }
  551. }
  552. return nil
  553. }
  554. func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
  555. for _, id := range have {
  556. if id == want {
  557. return cipherSuiteTLS13ByID(id)
  558. }
  559. }
  560. return nil
  561. }
  562. func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
  563. for _, cipherSuite := range cipherSuitesTLS13 {
  564. if cipherSuite.id == id {
  565. return cipherSuite
  566. }
  567. }
  568. return nil
  569. }
  570. // A list of cipher suite IDs that are, or have been, implemented by this
  571. // package.
  572. //
  573. // See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
  574. const (
  575. // TLS 1.0 - 1.2 cipher suites.
  576. TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
  577. TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
  578. TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
  579. TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
  580. TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
  581. TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
  582. TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
  583. TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
  584. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
  585. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
  586. TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
  587. TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
  588. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
  589. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
  590. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
  591. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
  592. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
  593. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
  594. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
  595. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
  596. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8
  597. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
  598. // TLS 1.3 cipher suites.
  599. TLS_AES_128_GCM_SHA256 uint16 = 0x1301
  600. TLS_AES_256_GCM_SHA384 uint16 = 0x1302
  601. TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
  602. // TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
  603. // that the client is doing version fallback. See RFC 7507.
  604. TLS_FALLBACK_SCSV uint16 = 0x5600
  605. // Legacy names for the corresponding cipher suites with the correct _SHA256
  606. // suffix, retained for backward compatibility.
  607. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  608. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  609. )