101-fix-mbedtls3.6-build.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. From c2bdb9847e374331a4f1c8fcd3d93e0b57d4c6fc Mon Sep 17 00:00:00 2001
  2. From: Zxl hhyccc <[email protected]>
  3. Date: Sun, 7 Jul 2024 17:08:27 +0800
  4. Subject: [PATCH] Fix in 'mbedtls 3.6.0 ver' compilation failure issue
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. *** The added patch is available in 'mbedtls 3.6 version'.
  9. *** fix *clen += tlen; may cause potential bounds error.
  10. Co-authored-by: Lu jicong <[email protected]>
  11. Signed-off-by: Zxl hhyccc <[email protected]>
  12. ---
  13. m4/mbedtls.m4 | 20 +++++++++++++++++++
  14. src/aead.c | 17 ++++++++++++++++
  15. src/crypto.c | 2 +-
  16. src/stream.c | 17 ++++++++++++++++
  17. 4 files changed, 55 insertions(+), 1 deletion(-)
  18. --- a/m4/mbedtls.m4
  19. +++ b/m4/mbedtls.m4
  20. @@ -31,7 +31,12 @@ AC_DEFUN([ss_MBEDTLS],
  21. AC_COMPILE_IFELSE(
  22. [AC_LANG_PROGRAM(
  23. [[
  24. +#include <mbedtls/version.h>
  25. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  26. +#include <mbedtls/mbedtls_config.h>
  27. +#else
  28. #include <mbedtls/config.h>
  29. +#endif
  30. ]],
  31. [[
  32. #ifndef MBEDTLS_CIPHER_MODE_CFB
  33. @@ -48,7 +53,12 @@ AC_DEFUN([ss_MBEDTLS],
  34. AC_COMPILE_IFELSE(
  35. [AC_LANG_PROGRAM(
  36. [[
  37. +#include <mbedtls/version.h>
  38. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  39. +#include <mbedtls/mbedtls_config.h>
  40. +#else
  41. #include <mbedtls/config.h>
  42. +#endif
  43. ]],
  44. [[
  45. #ifndef MBEDTLS_ARC4_C
  46. @@ -64,7 +74,12 @@ AC_DEFUN([ss_MBEDTLS],
  47. AC_COMPILE_IFELSE(
  48. [AC_LANG_PROGRAM(
  49. [[
  50. +#include <mbedtls/version.h>
  51. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  52. +#include <mbedtls/mbedtls_config.h>
  53. +#else
  54. #include <mbedtls/config.h>
  55. +#endif
  56. ]],
  57. [[
  58. #ifndef MBEDTLS_BLOWFISH_C
  59. @@ -80,7 +95,12 @@ AC_DEFUN([ss_MBEDTLS],
  60. AC_COMPILE_IFELSE(
  61. [AC_LANG_PROGRAM(
  62. [[
  63. +#include <mbedtls/version.h>
  64. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  65. +#include <mbedtls/mbedtls_config.h>
  66. +#else
  67. #include <mbedtls/config.h>
  68. +#endif
  69. ]],
  70. [[
  71. #ifndef MBEDTLS_CAMELLIA_C
  72. --- a/src/aead.c
  73. +++ b/src/aead.c
  74. @@ -178,9 +178,14 @@ aead_cipher_encrypt(cipher_ctx_t *cipher
  75. case AES192GCM:
  76. case AES128GCM:
  77. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  78. err = mbedtls_cipher_auth_encrypt(cipher_ctx->evp, n, nlen, ad, adlen,
  79. m, mlen, c, clen, c + mlen, tlen);
  80. *clen += tlen;
  81. +#else
  82. + err = mbedtls_cipher_auth_encrypt_ext(cipher_ctx->evp, n, nlen, ad, adlen,
  83. + m, mlen, c, mlen + tlen, clen, tlen);
  84. +#endif
  85. break;
  86. case CHACHA20POLY1305IETF:
  87. err = crypto_aead_chacha20poly1305_ietf_encrypt(c, &long_clen, m, mlen,
  88. @@ -226,8 +231,13 @@ aead_cipher_decrypt(cipher_ctx_t *cipher
  89. // Otherwise, just use the mbedTLS one with crappy AES-NI.
  90. case AES192GCM:
  91. case AES128GCM:
  92. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  93. err = mbedtls_cipher_auth_decrypt(cipher_ctx->evp, n, nlen, ad, adlen,
  94. m, mlen - tlen, p, plen, m + mlen - tlen, tlen);
  95. +#else
  96. + err = mbedtls_cipher_auth_decrypt_ext(cipher_ctx->evp, n, nlen, ad, adlen,
  97. + m, mlen, p, mlen - tlen, plen, tlen);
  98. +#endif
  99. break;
  100. case CHACHA20POLY1305IETF:
  101. err = crypto_aead_chacha20poly1305_ietf_decrypt(p, &long_plen, NULL, m, mlen,
  102. @@ -724,9 +734,26 @@ aead_key_init(int method, const char *pa
  103. if (method >= CHACHA20POLY1305IETF) {
  104. cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t));
  105. cipher->info = cipher_info;
  106. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  107. cipher->info->base = NULL;
  108. cipher->info->key_bitlen = supported_aead_ciphers_key_size[method] * 8;
  109. cipher->info->iv_size = supported_aead_ciphers_nonce_size[method];
  110. +#else
  111. + cipher->info->private_base_idx = 0;
  112. +
  113. +#ifdef MBEDTLS_KEY_BITLEN_SHIFT
  114. + cipher->info->private_key_bitlen = supported_aead_ciphers_key_size[method] * 8 >> MBEDTLS_KEY_BITLEN_SHIFT;
  115. +#else
  116. + cipher->info->private_key_bitlen = supported_aead_ciphers_key_size[method] * 8;
  117. +#endif
  118. +
  119. +#ifdef MBEDTLS_IV_SIZE_SHIFT
  120. + cipher->info->private_iv_size = supported_aead_ciphers_nonce_size[method] >> MBEDTLS_IV_SIZE_SHIFT;
  121. +#else
  122. + cipher->info->private_iv_size = supported_aead_ciphers_nonce_size[method];
  123. +#endif
  124. +
  125. +#endif
  126. } else {
  127. cipher->info = (cipher_kt_t *)aead_get_cipher_type(method);
  128. }
  129. --- a/src/crypto.c
  130. +++ b/src/crypto.c
  131. @@ -103,7 +103,7 @@ crypto_md5(const unsigned char *d, size_
  132. if (md == NULL) {
  133. md = m;
  134. }
  135. -#if MBEDTLS_VERSION_NUMBER >= 0x02070000
  136. +#if MBEDTLS_VERSION_NUMBER < 0x03000000 && MBEDTLS_VERSION_NUMBER >= 0x02070000
  137. if (mbedtls_md5_ret(d, n, md) != 0)
  138. FATAL("Failed to calculate MD5");
  139. #else
  140. --- a/src/stream.c
  141. +++ b/src/stream.c
  142. @@ -174,7 +174,11 @@ cipher_nonce_size(const cipher_t *cipher
  143. if (cipher == NULL) {
  144. return 0;
  145. }
  146. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  147. return cipher->info->iv_size;
  148. +#else
  149. + return (int)mbedtls_cipher_info_get_iv_size(cipher->info);
  150. +#endif
  151. }
  152. int
  153. @@ -192,7 +196,11 @@ cipher_key_size(const cipher_t *cipher)
  154. return 0;
  155. }
  156. /* From Version 1.2.7 released 2013-04-13 Default Blowfish keysize is now 128-bits */
  157. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  158. return cipher->info->key_bitlen / 8;
  159. +#else
  160. + return (int)mbedtls_cipher_info_get_key_bitlen(cipher->info) / 8;
  161. +#endif
  162. }
  163. const cipher_kt_t *
  164. @@ -645,9 +653,26 @@ stream_key_init(int method, const char *
  165. if (method == SALSA20 || method == CHACHA20 || method == CHACHA20IETF) {
  166. cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t));
  167. cipher->info = cipher_info;
  168. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  169. cipher->info->base = NULL;
  170. cipher->info->key_bitlen = supported_stream_ciphers_key_size[method] * 8;
  171. cipher->info->iv_size = supported_stream_ciphers_nonce_size[method];
  172. +#else
  173. + cipher->info->private_base_idx = 0;
  174. +
  175. +#ifdef MBEDTLS_KEY_BITLEN_SHIFT
  176. + cipher->info->private_key_bitlen = supported_stream_ciphers_key_size[method] * 8 >> MBEDTLS_KEY_BITLEN_SHIFT;
  177. +#else
  178. + cipher->info->private_key_bitlen = supported_stream_ciphers_key_size[method] * 8;
  179. +#endif
  180. +
  181. +#ifdef MBEDTLS_IV_SIZE_SHIFT
  182. + cipher->info->private_iv_size = supported_stream_ciphers_nonce_size[method] >> MBEDTLS_IV_SIZE_SHIFT;
  183. +#else
  184. + cipher->info->private_iv_size = supported_stream_ciphers_nonce_size[method];
  185. +#endif
  186. +
  187. +#endif
  188. } else {
  189. cipher->info = (cipher_kt_t *)stream_get_cipher_type(method);
  190. }