cipher_suites.go 26 KB

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