140-allow-prefer-chacha20.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. From 286e015bf0d30530707a5e7b3b871509f2ab50d7 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <[email protected]>
  3. Date: Thu, 27 Sep 2018 08:44:39 -0300
  4. Subject: Add OPENSSL_PREFER_CHACHA_OVER_GCM option
  5. This enables a compile-time option to prefer ChaCha20-Poly1305 over
  6. AES-GCM in the openssl default ciphersuite, which is useful in systems
  7. without AES specific CPU instructions.
  8. OPENSSL_PREFER_CHACHA_OVER_GCM must be defined to enable it.
  9. Note that this does not have the same effect as the
  10. SL_OP_PRIORITIZE_CHACHA option, which prioritizes ChaCha20-Poly1305 only
  11. when the client has it on top of its ciphersuite preference.
  12. Signed-off-by: Eneas U de Queiroz <[email protected]>
  13. --- a/include/openssl/ssl.h
  14. +++ b/include/openssl/ssl.h
  15. @@ -173,9 +173,15 @@ extern "C" {
  16. # define SSL_DEFAULT_CIPHER_LIST "ALL:!COMPLEMENTOFDEFAULT:!eNULL"
  17. /* This is the default set of TLSv1.3 ciphersuites */
  18. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  19. -# define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \
  20. - "TLS_CHACHA20_POLY1305_SHA256:" \
  21. - "TLS_AES_128_GCM_SHA256"
  22. +# ifdef OPENSSL_PREFER_CHACHA_OVER_GCM
  23. +# define TLS_DEFAULT_CIPHERSUITES "TLS_CHACHA20_POLY1305_SHA256:" \
  24. + "TLS_AES_256_GCM_SHA384:" \
  25. + "TLS_AES_128_GCM_SHA256"
  26. +# else
  27. +# define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \
  28. + "TLS_CHACHA20_POLY1305_SHA256:" \
  29. + "TLS_AES_128_GCM_SHA256"
  30. +# endif
  31. # else
  32. # define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \
  33. "TLS_AES_128_GCM_SHA256"
  34. --- a/ssl/ssl_ciph.c
  35. +++ b/ssl/ssl_ciph.c
  36. @@ -1464,11 +1464,29 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
  37. ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
  38. &tail);
  39. + /*
  40. + * If OPENSSL_PREFER_CHACHA_OVER_GCM is defined, ChaCha20_Poly1305
  41. + * will be placed before AES-256. Otherwise, the default behavior of
  42. + * preferring GCM over CHACHA is used.
  43. + * This is useful for systems that do not have AES-specific CPU
  44. + * instructions, where ChaCha20-Poly1305 is 3 times faster than AES.
  45. + * Note that this does not have the same effect as the SSL_OP_PRIORITIZE_CHACHA
  46. + * option, which prioritizes ChaCha20-Poly1305 only when the client has it on top
  47. + * of its ciphersuite preference.
  48. + */
  49. +
  50. +#ifdef OPENSSL_PREFER_CHACHA_OVER_GCM
  51. + ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
  52. + &head, &tail);
  53. + ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
  54. + &head, &tail);
  55. +#else
  56. /* Within each strength group, we prefer GCM over CHACHA... */
  57. ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
  58. &head, &tail);
  59. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
  60. &head, &tail);
  61. +#endif
  62. /*
  63. * ...and generally, our preferred cipher is AES.
  64. @@ -1524,7 +1542,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
  65. * Within each group, ciphers remain sorted by strength and previous
  66. * preference, i.e.,
  67. * 1) ECDHE > DHE
  68. - * 2) GCM > CHACHA
  69. + * 2) GCM > CHACHA, reversed if OPENSSL_PREFER_CHACHA_OVER_GCM is defined
  70. * 3) AES > rest
  71. * 4) TLS 1.2 > legacy
  72. *