102-Fix-in-mbedtls-3.6.0-ver-compilation-failure-issue.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. From 2b33e8e6778db08624dbf8ec6fe1e8f7b1a4bee8 Mon Sep 17 00:00:00 2001
  2. From: Lu jicong <[email protected]>
  3. Date: Fri, 10 Jan 2025 22:05:31 +0800
  4. Subject: [PATCH] Fix in 'mbedtls 3.6.0 ver' compilation failure issue
  5. Fix mbedtls 3.6 compatibility
  6. Co-authored-by: Zxl hhyccc <[email protected]>
  7. Signed-off-by: Lu jicong <[email protected]>
  8. ---
  9. m4/mbedtls.m4 | 20 ++++++++++++++++++++
  10. src/aead.c | 23 +++++++++++------------
  11. src/crypto.c | 2 +-
  12. src/crypto.h | 1 -
  13. src/stream.c | 51 ++++++---------------------------------------------
  14. 5 files changed, 38 insertions(+), 59 deletions(-)
  15. diff --git a/m4/mbedtls.m4 b/m4/mbedtls.m4
  16. index 2c478b9..a795790 100644
  17. --- a/m4/mbedtls.m4
  18. +++ b/m4/mbedtls.m4
  19. @@ -31,7 +31,12 @@ AC_DEFUN([ss_MBEDTLS],
  20. AC_COMPILE_IFELSE(
  21. [AC_LANG_PROGRAM(
  22. [[
  23. +#include <mbedtls/version.h>
  24. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  25. +#include <mbedtls/mbedtls_config.h>
  26. +#else
  27. #include <mbedtls/config.h>
  28. +#endif
  29. ]],
  30. [[
  31. #ifndef MBEDTLS_CIPHER_MODE_CFB
  32. @@ -48,7 +53,12 @@ AC_DEFUN([ss_MBEDTLS],
  33. AC_COMPILE_IFELSE(
  34. [AC_LANG_PROGRAM(
  35. [[
  36. +#include <mbedtls/version.h>
  37. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  38. +#include <mbedtls/mbedtls_config.h>
  39. +#else
  40. #include <mbedtls/config.h>
  41. +#endif
  42. ]],
  43. [[
  44. #ifndef MBEDTLS_ARC4_C
  45. @@ -64,7 +74,12 @@ AC_DEFUN([ss_MBEDTLS],
  46. AC_COMPILE_IFELSE(
  47. [AC_LANG_PROGRAM(
  48. [[
  49. +#include <mbedtls/version.h>
  50. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  51. +#include <mbedtls/mbedtls_config.h>
  52. +#else
  53. #include <mbedtls/config.h>
  54. +#endif
  55. ]],
  56. [[
  57. #ifndef MBEDTLS_BLOWFISH_C
  58. @@ -80,7 +95,12 @@ AC_DEFUN([ss_MBEDTLS],
  59. AC_COMPILE_IFELSE(
  60. [AC_LANG_PROGRAM(
  61. [[
  62. +#include <mbedtls/version.h>
  63. +#if MBEDTLS_VERSION_NUMBER >= 0x03000000
  64. +#include <mbedtls/mbedtls_config.h>
  65. +#else
  66. #include <mbedtls/config.h>
  67. +#endif
  68. ]],
  69. [[
  70. #ifndef MBEDTLS_CAMELLIA_C
  71. diff --git a/src/aead.c b/src/aead.c
  72. index 358ec93..73349da 100644
  73. --- a/src/aead.c
  74. +++ b/src/aead.c
  75. @@ -177,9 +177,13 @@ aead_cipher_encrypt(cipher_ctx_t *cipher_ctx,
  76. // Otherwise, just use the mbedTLS one with crappy AES-NI.
  77. case AES192GCM:
  78. case AES128GCM:
  79. -
  80. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  81. err = mbedtls_cipher_auth_encrypt(cipher_ctx->evp, n, nlen, ad, adlen,
  82. m, mlen, c, clen, c + mlen, tlen);
  83. +#else
  84. + err = mbedtls_cipher_auth_encrypt_ext(cipher_ctx->evp, n, nlen, ad, adlen,
  85. + m, mlen, c, mlen + tlen, clen, tlen);
  86. +#endif
  87. *clen += tlen;
  88. break;
  89. case CHACHA20POLY1305IETF:
  90. @@ -226,8 +230,13 @@ aead_cipher_decrypt(cipher_ctx_t *cipher_ctx,
  91. // Otherwise, just use the mbedTLS one with crappy AES-NI.
  92. case AES192GCM:
  93. case AES128GCM:
  94. +#if MBEDTLS_VERSION_NUMBER < 0x03000000
  95. err = mbedtls_cipher_auth_decrypt(cipher_ctx->evp, n, nlen, ad, adlen,
  96. m, mlen - tlen, p, plen, m + mlen - tlen, tlen);
  97. +#else
  98. + err = mbedtls_cipher_auth_decrypt_ext(cipher_ctx->evp, n, nlen, ad, adlen,
  99. + m, mlen, p, mlen - tlen, plen, tlen);
  100. +#endif
  101. break;
  102. case CHACHA20POLY1305IETF:
  103. err = crypto_aead_chacha20poly1305_ietf_decrypt(p, &long_plen, NULL, m, mlen,
  104. @@ -721,17 +730,7 @@ aead_key_init(int method, const char *pass, const char *key)
  105. cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t));
  106. memset(cipher, 0, sizeof(cipher_t));
  107. - if (method >= CHACHA20POLY1305IETF) {
  108. - cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t));
  109. - cipher->info = cipher_info;
  110. - cipher->info->base = NULL;
  111. - cipher->info->key_bitlen = supported_aead_ciphers_key_size[method] * 8;
  112. - cipher->info->iv_size = supported_aead_ciphers_nonce_size[method];
  113. - } else {
  114. - cipher->info = (cipher_kt_t *)aead_get_cipher_type(method);
  115. - }
  116. -
  117. - if (cipher->info == NULL && cipher->key_len == 0) {
  118. + if (method < CHACHA20POLY1305IETF && aead_get_cipher_type(method) == NULL) {
  119. LOGE("Cipher %s not found in crypto library", supported_aead_ciphers[method]);
  120. FATAL("Cannot initialize cipher");
  121. }
  122. diff --git a/src/crypto.c b/src/crypto.c
  123. index b44d867..76c426b 100644
  124. --- a/src/crypto.c
  125. +++ b/src/crypto.c
  126. @@ -103,7 +103,7 @@ crypto_md5(const unsigned char *d, size_t n, unsigned char *md)
  127. if (md == NULL) {
  128. md = m;
  129. }
  130. -#if MBEDTLS_VERSION_NUMBER >= 0x02070000
  131. +#if MBEDTLS_VERSION_NUMBER < 0x03000000 && MBEDTLS_VERSION_NUMBER >= 0x02070000
  132. if (mbedtls_md5_ret(d, n, md) != 0)
  133. FATAL("Failed to calculate MD5");
  134. #else
  135. diff --git a/src/crypto.h b/src/crypto.h
  136. index 1791551..7070793 100644
  137. --- a/src/crypto.h
  138. +++ b/src/crypto.h
  139. @@ -97,7 +97,6 @@ typedef struct buffer {
  140. typedef struct {
  141. int method;
  142. int skey;
  143. - cipher_kt_t *info;
  144. size_t nonce_len;
  145. size_t key_len;
  146. size_t tag_len;
  147. diff --git a/src/stream.c b/src/stream.c
  148. index 35d9050..b2e2cea 100644
  149. --- a/src/stream.c
  150. +++ b/src/stream.c
  151. @@ -168,33 +168,6 @@ crypto_stream_xor_ic(uint8_t *c, const uint8_t *m, uint64_t mlen,
  152. return 0;
  153. }
  154. -int
  155. -cipher_nonce_size(const cipher_t *cipher)
  156. -{
  157. - if (cipher == NULL) {
  158. - return 0;
  159. - }
  160. - return cipher->info->iv_size;
  161. -}
  162. -
  163. -int
  164. -cipher_key_size(const cipher_t *cipher)
  165. -{
  166. - /*
  167. - * Semi-API changes (technically public, morally prnonceate)
  168. - * Renamed a few headers to include _internal in the name. Those headers are
  169. - * not supposed to be included by users.
  170. - * Changed md_info_t into an opaque structure (use md_get_xxx() accessors).
  171. - * Changed pk_info_t into an opaque structure.
  172. - * Changed cipher_base_t into an opaque structure.
  173. - */
  174. - if (cipher == NULL) {
  175. - return 0;
  176. - }
  177. - /* From Version 1.2.7 released 2013-04-13 Default Blowfish keysize is now 128-bits */
  178. - return cipher->info->key_bitlen / 8;
  179. -}
  180. -
  181. const cipher_kt_t *
  182. stream_get_cipher_type(int method)
  183. {
  184. @@ -642,34 +615,22 @@ stream_key_init(int method, const char *pass, const char *key)
  185. cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t));
  186. memset(cipher, 0, sizeof(cipher_t));
  187. - if (method == SALSA20 || method == CHACHA20 || method == CHACHA20IETF) {
  188. - cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t));
  189. - cipher->info = cipher_info;
  190. - cipher->info->base = NULL;
  191. - cipher->info->key_bitlen = supported_stream_ciphers_key_size[method] * 8;
  192. - cipher->info->iv_size = supported_stream_ciphers_nonce_size[method];
  193. - } else {
  194. - cipher->info = (cipher_kt_t *)stream_get_cipher_type(method);
  195. - }
  196. -
  197. - if (cipher->info == NULL && cipher->key_len == 0) {
  198. + if (method < SALSA20 && stream_get_cipher_type(method) == NULL) {
  199. LOGE("Cipher %s not found in crypto library", supported_stream_ciphers[method]);
  200. FATAL("Cannot initialize cipher");
  201. }
  202. if (key != NULL)
  203. - cipher->key_len = crypto_parse_key(key, cipher->key, cipher_key_size(cipher));
  204. + cipher->key_len = crypto_parse_key(key, cipher->key,
  205. + supported_stream_ciphers_key_size[method]);
  206. else
  207. - cipher->key_len = crypto_derive_key(pass, cipher->key, cipher_key_size(cipher));
  208. + cipher->key_len = crypto_derive_key(pass, cipher->key,
  209. + supported_stream_ciphers_key_size[method]);
  210. if (cipher->key_len == 0) {
  211. FATAL("Cannot generate key and NONCE");
  212. }
  213. - if (method == RC4_MD5) {
  214. - cipher->nonce_len = 16;
  215. - } else {
  216. - cipher->nonce_len = cipher_nonce_size(cipher);
  217. - }
  218. + cipher->nonce_len = supported_stream_ciphers_nonce_size[method];
  219. cipher->method = method;
  220. return cipher;
  221. --
  222. 2.39.5