Skip to content

Commit 9afa3ca

Browse files
msdos03zxlhhyccc
authored andcommitted
Fix in 'mbedtls 3.6.0 ver' compilation failure issue
Fix mbedtls 3.6 compatibility Co-authored-by: Zxl hhyccc <[email protected]> Signed-off-by: Lu jicong <[email protected]>
1 parent d83ace0 commit 9afa3ca

File tree

5 files changed

+38
-59
lines changed

5 files changed

+38
-59
lines changed

m4/mbedtls.m4

+20
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ AC_DEFUN([ss_MBEDTLS],
3131
AC_COMPILE_IFELSE(
3232
[AC_LANG_PROGRAM(
3333
[[
34+
#include <mbedtls/version.h>
35+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
36+
#include <mbedtls/mbedtls_config.h>
37+
#else
3438
#include <mbedtls/config.h>
39+
#endif
3540
]],
3641
[[
3742
#ifndef MBEDTLS_CIPHER_MODE_CFB
@@ -48,7 +53,12 @@ AC_DEFUN([ss_MBEDTLS],
4853
AC_COMPILE_IFELSE(
4954
[AC_LANG_PROGRAM(
5055
[[
56+
#include <mbedtls/version.h>
57+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
58+
#include <mbedtls/mbedtls_config.h>
59+
#else
5160
#include <mbedtls/config.h>
61+
#endif
5262
]],
5363
[[
5464
#ifndef MBEDTLS_ARC4_C
@@ -64,7 +74,12 @@ AC_DEFUN([ss_MBEDTLS],
6474
AC_COMPILE_IFELSE(
6575
[AC_LANG_PROGRAM(
6676
[[
77+
#include <mbedtls/version.h>
78+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
79+
#include <mbedtls/mbedtls_config.h>
80+
#else
6781
#include <mbedtls/config.h>
82+
#endif
6883
]],
6984
[[
7085
#ifndef MBEDTLS_BLOWFISH_C
@@ -80,7 +95,12 @@ AC_DEFUN([ss_MBEDTLS],
8095
AC_COMPILE_IFELSE(
8196
[AC_LANG_PROGRAM(
8297
[[
98+
#include <mbedtls/version.h>
99+
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
100+
#include <mbedtls/mbedtls_config.h>
101+
#else
83102
#include <mbedtls/config.h>
103+
#endif
84104
]],
85105
[[
86106
#ifndef MBEDTLS_CAMELLIA_C

src/aead.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ aead_cipher_encrypt(cipher_ctx_t *cipher_ctx,
177177
// Otherwise, just use the mbedTLS one with crappy AES-NI.
178178
case AES192GCM:
179179
case AES128GCM:
180-
180+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
181181
err = mbedtls_cipher_auth_encrypt(cipher_ctx->evp, n, nlen, ad, adlen,
182182
m, mlen, c, clen, c + mlen, tlen);
183+
#else
184+
err = mbedtls_cipher_auth_encrypt_ext(cipher_ctx->evp, n, nlen, ad, adlen,
185+
m, mlen, c, mlen + tlen, clen, tlen);
186+
#endif
183187
*clen += tlen;
184188
break;
185189
case CHACHA20POLY1305IETF:
@@ -226,8 +230,13 @@ aead_cipher_decrypt(cipher_ctx_t *cipher_ctx,
226230
// Otherwise, just use the mbedTLS one with crappy AES-NI.
227231
case AES192GCM:
228232
case AES128GCM:
233+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
229234
err = mbedtls_cipher_auth_decrypt(cipher_ctx->evp, n, nlen, ad, adlen,
230235
m, mlen - tlen, p, plen, m + mlen - tlen, tlen);
236+
#else
237+
err = mbedtls_cipher_auth_decrypt_ext(cipher_ctx->evp, n, nlen, ad, adlen,
238+
m, mlen, p, mlen - tlen, plen, tlen);
239+
#endif
231240
break;
232241
case CHACHA20POLY1305IETF:
233242
err = crypto_aead_chacha20poly1305_ietf_decrypt(p, &long_plen, NULL, m, mlen,
@@ -721,17 +730,7 @@ aead_key_init(int method, const char *pass, const char *key)
721730
cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t));
722731
memset(cipher, 0, sizeof(cipher_t));
723732

724-
if (method >= CHACHA20POLY1305IETF) {
725-
cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t));
726-
cipher->info = cipher_info;
727-
cipher->info->base = NULL;
728-
cipher->info->key_bitlen = supported_aead_ciphers_key_size[method] * 8;
729-
cipher->info->iv_size = supported_aead_ciphers_nonce_size[method];
730-
} else {
731-
cipher->info = (cipher_kt_t *)aead_get_cipher_type(method);
732-
}
733-
734-
if (cipher->info == NULL && cipher->key_len == 0) {
733+
if (method < CHACHA20POLY1305IETF && aead_get_cipher_type(method) == NULL) {
735734
LOGE("Cipher %s not found in crypto library", supported_aead_ciphers[method]);
736735
FATAL("Cannot initialize cipher");
737736
}

src/crypto.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ crypto_md5(const unsigned char *d, size_t n, unsigned char *md)
103103
if (md == NULL) {
104104
md = m;
105105
}
106-
#if MBEDTLS_VERSION_NUMBER >= 0x02070000
106+
#if MBEDTLS_VERSION_NUMBER < 0x03000000 && MBEDTLS_VERSION_NUMBER >= 0x02070000
107107
if (mbedtls_md5_ret(d, n, md) != 0)
108108
FATAL("Failed to calculate MD5");
109109
#else

src/crypto.h

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ typedef struct buffer {
9797
typedef struct {
9898
int method;
9999
int skey;
100-
cipher_kt_t *info;
101100
size_t nonce_len;
102101
size_t key_len;
103102
size_t tag_len;

src/stream.c

+6-45
Original file line numberDiff line numberDiff line change
@@ -168,33 +168,6 @@ crypto_stream_xor_ic(uint8_t *c, const uint8_t *m, uint64_t mlen,
168168
return 0;
169169
}
170170

171-
int
172-
cipher_nonce_size(const cipher_t *cipher)
173-
{
174-
if (cipher == NULL) {
175-
return 0;
176-
}
177-
return cipher->info->iv_size;
178-
}
179-
180-
int
181-
cipher_key_size(const cipher_t *cipher)
182-
{
183-
/*
184-
* Semi-API changes (technically public, morally prnonceate)
185-
* Renamed a few headers to include _internal in the name. Those headers are
186-
* not supposed to be included by users.
187-
* Changed md_info_t into an opaque structure (use md_get_xxx() accessors).
188-
* Changed pk_info_t into an opaque structure.
189-
* Changed cipher_base_t into an opaque structure.
190-
*/
191-
if (cipher == NULL) {
192-
return 0;
193-
}
194-
/* From Version 1.2.7 released 2013-04-13 Default Blowfish keysize is now 128-bits */
195-
return cipher->info->key_bitlen / 8;
196-
}
197-
198171
const cipher_kt_t *
199172
stream_get_cipher_type(int method)
200173
{
@@ -642,34 +615,22 @@ stream_key_init(int method, const char *pass, const char *key)
642615
cipher_t *cipher = (cipher_t *)ss_malloc(sizeof(cipher_t));
643616
memset(cipher, 0, sizeof(cipher_t));
644617

645-
if (method == SALSA20 || method == CHACHA20 || method == CHACHA20IETF) {
646-
cipher_kt_t *cipher_info = (cipher_kt_t *)ss_malloc(sizeof(cipher_kt_t));
647-
cipher->info = cipher_info;
648-
cipher->info->base = NULL;
649-
cipher->info->key_bitlen = supported_stream_ciphers_key_size[method] * 8;
650-
cipher->info->iv_size = supported_stream_ciphers_nonce_size[method];
651-
} else {
652-
cipher->info = (cipher_kt_t *)stream_get_cipher_type(method);
653-
}
654-
655-
if (cipher->info == NULL && cipher->key_len == 0) {
618+
if (method < SALSA20 && stream_get_cipher_type(method) == NULL) {
656619
LOGE("Cipher %s not found in crypto library", supported_stream_ciphers[method]);
657620
FATAL("Cannot initialize cipher");
658621
}
659622

660623
if (key != NULL)
661-
cipher->key_len = crypto_parse_key(key, cipher->key, cipher_key_size(cipher));
624+
cipher->key_len = crypto_parse_key(key, cipher->key,
625+
supported_stream_ciphers_key_size[method]);
662626
else
663-
cipher->key_len = crypto_derive_key(pass, cipher->key, cipher_key_size(cipher));
627+
cipher->key_len = crypto_derive_key(pass, cipher->key,
628+
supported_stream_ciphers_key_size[method]);
664629

665630
if (cipher->key_len == 0) {
666631
FATAL("Cannot generate key and NONCE");
667632
}
668-
if (method == RC4_MD5) {
669-
cipher->nonce_len = 16;
670-
} else {
671-
cipher->nonce_len = cipher_nonce_size(cipher);
672-
}
633+
cipher->nonce_len = supported_stream_ciphers_nonce_size[method];
673634
cipher->method = method;
674635

675636
return cipher;

0 commit comments

Comments
 (0)