140-allow-prefer-chacha20.patch 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. From 0000000000000000000000000000000000000000 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/ssl/ssl_ciph.c
  14. +++ b/ssl/ssl_ciph.c
  15. @@ -1488,11 +1488,29 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
  16. ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head,
  17. &tail);
  18. + /*
  19. + * If OPENSSL_PREFER_CHACHA_OVER_GCM is defined, ChaCha20_Poly1305
  20. + * will be placed before AES-256. Otherwise, the default behavior of
  21. + * preferring GCM over CHACHA is used.
  22. + * This is useful for systems that do not have AES-specific CPU
  23. + * instructions, where ChaCha20-Poly1305 is 3 times faster than AES.
  24. + * Note that this does not have the same effect as the SSL_OP_PRIORITIZE_CHACHA
  25. + * option, which prioritizes ChaCha20-Poly1305 only when the client has it on top
  26. + * of its ciphersuite preference.
  27. + */
  28. +
  29. +#ifdef OPENSSL_PREFER_CHACHA_OVER_GCM
  30. + ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
  31. + &head, &tail);
  32. + ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
  33. + &head, &tail);
  34. +#else
  35. /* Within each strength group, we prefer GCM over CHACHA... */
  36. ssl_cipher_apply_rule(0, 0, 0, SSL_AESGCM, 0, 0, 0, CIPHER_ADD, -1,
  37. &head, &tail);
  38. ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20, 0, 0, 0, CIPHER_ADD, -1,
  39. &head, &tail);
  40. +#endif
  41. /*
  42. * ...and generally, our preferred cipher is AES.
  43. @@ -1547,7 +1565,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
  44. * Within each group, ciphers remain sorted by strength and previous
  45. * preference, i.e.,
  46. * 1) ECDHE > DHE
  47. - * 2) GCM > CHACHA
  48. + * 2) GCM > CHACHA, reversed if OPENSSL_PREFER_CHACHA_OVER_GCM is defined
  49. * 3) AES > rest
  50. * 4) TLS 1.2 > legacy
  51. *
  52. @@ -2246,7 +2264,13 @@ const char *OSSL_default_cipher_list(voi
  53. */
  54. const char *OSSL_default_ciphersuites(void)
  55. {
  56. +#ifdef OPENSSL_PREFER_CHACHA_OVER_GCM
  57. + return "TLS_CHACHA20_POLY1305_SHA256:"
  58. + "TLS_AES_256_GCM_SHA384:"
  59. + "TLS_AES_128_GCM_SHA256";
  60. +#else
  61. return "TLS_AES_256_GCM_SHA384:"
  62. "TLS_CHACHA20_POLY1305_SHA256:"
  63. "TLS_AES_128_GCM_SHA256";
  64. +#endif
  65. }
  66. --- a/include/openssl/ssl.h.in
  67. +++ b/include/openssl/ssl.h.in
  68. @@ -199,9 +199,15 @@ extern "C" {
  69. * DEPRECATED IN 3.0.0, in favor of OSSL_default_ciphersuites()
  70. * Update both macro and function simultaneously
  71. */
  72. -# define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \
  73. - "TLS_CHACHA20_POLY1305_SHA256:" \
  74. - "TLS_AES_128_GCM_SHA256"
  75. +# ifdef OPENSSL_PREFER_CHACHA_OVER_GCM
  76. +# define TLS_DEFAULT_CIPHERSUITES "TLS_CHACHA20_POLY1305_SHA256:" \
  77. + "TLS_AES_256_GCM_SHA384:" \
  78. + "TLS_AES_128_GCM_SHA256"
  79. +# else
  80. +# define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \
  81. + "TLS_CHACHA20_POLY1305_SHA256:" \
  82. + "TLS_AES_128_GCM_SHA256"
  83. +# endif
  84. # endif
  85. /*
  86. * As of OpenSSL 1.0.0, ssl_create_cipher_list() in ssl/ssl_ciph.c always