2323/**
2424 * @file
2525 * mbedtls compatibility stub.
26- * This file provide compatibility stubs for the mbedtls libraries
27- * prior to version 3. This version made most fields in structs private
28- * and requires accessor functions to be used. For earlier versions, we
29- * implement the accessor functions here.
26+ * This file provides compatibility stubs to handle API differences between
27+ * different versions of Mbed TLS.
3028 */
3129
3230#ifndef MBEDTLS_COMPAT_H_
3634
3735#include "errlevel.h"
3836
39- #include <mbedtls/cipher.h>
40- #include <mbedtls/ctr_drbg.h>
41- #include <mbedtls/dhm.h>
42- #include <mbedtls/ecp.h>
43- #include <mbedtls/md.h>
44- #include <mbedtls/pem.h>
45- #include <mbedtls/pk.h>
46- #include <mbedtls/ssl.h>
47- #include <mbedtls/version.h>
48- #include <mbedtls/x509_crt.h>
49-
5037#ifdef HAVE_PSA_CRYPTO_H
5138#include <psa/crypto.h>
5239#endif
5340
54- #if MBEDTLS_VERSION_NUMBER >= 0x03000000
55- typedef uint16_t mbedtls_compat_group_id ;
56- #else
57- typedef mbedtls_ecp_group_id mbedtls_compat_group_id ;
58- #endif
59-
6041static inline void
6142mbedtls_compat_psa_crypto_init (void )
6243{
@@ -70,162 +51,4 @@ mbedtls_compat_psa_crypto_init(void)
7051#endif
7152}
7253
73- static inline mbedtls_compat_group_id
74- mbedtls_compat_get_group_id (const mbedtls_ecp_curve_info * curve_info )
75- {
76- #if MBEDTLS_VERSION_NUMBER >= 0x03000000
77- return curve_info -> tls_id ;
78- #else
79- return curve_info -> grp_id ;
80- #endif
81- }
82-
83- /*
84- * In older versions of mbedtls, mbedtls_ctr_drbg_update() did not return an
85- * error code, and it was deprecated in favor of mbedtls_ctr_drbg_update_ret()
86- * which does.
87- *
88- * In mbedtls 3, this function was removed and mbedtls_ctr_drbg_update() returns
89- * an error code.
90- */
91- static inline int
92- mbedtls_compat_ctr_drbg_update (mbedtls_ctr_drbg_context * ctx , const unsigned char * additional ,
93- size_t add_len )
94- {
95- #if MBEDTLS_VERSION_NUMBER > 0x03000000
96- return mbedtls_ctr_drbg_update (ctx , additional , add_len );
97- #elif defined(HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET )
98- return mbedtls_ctr_drbg_update_ret (ctx , additional , add_len );
99- #else
100- mbedtls_ctr_drbg_update (ctx , additional , add_len );
101- return 0 ;
102- #endif /* HAVE_MBEDTLS_CTR_DRBG_UPDATE_RET */
103- }
104-
105- static inline int
106- mbedtls_compat_pk_check_pair (const mbedtls_pk_context * pub , const mbedtls_pk_context * prv ,
107- int (* f_rng )(void * , unsigned char * , size_t ), void * p_rng )
108- {
109- #if MBEDTLS_VERSION_NUMBER < 0x03020100
110- return mbedtls_pk_check_pair (pub , prv );
111- #else
112- return mbedtls_pk_check_pair (pub , prv , f_rng , p_rng );
113- #endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
114- }
115-
116- static inline int
117- mbedtls_compat_pk_parse_key (mbedtls_pk_context * ctx , const unsigned char * key , size_t keylen ,
118- const unsigned char * pwd , size_t pwdlen ,
119- int (* f_rng )(void * , unsigned char * , size_t ), void * p_rng )
120- {
121- #if MBEDTLS_VERSION_NUMBER < 0x03020100
122- return mbedtls_pk_parse_key (ctx , key , keylen , pwd , pwdlen );
123- #else
124- return mbedtls_pk_parse_key (ctx , key , keylen , pwd , pwdlen , f_rng , p_rng );
125- #endif
126- }
127-
128- static inline int
129- mbedtls_compat_pk_parse_keyfile (mbedtls_pk_context * ctx , const char * path , const char * password ,
130- int (* f_rng )(void * , unsigned char * , size_t ), void * p_rng )
131- {
132- #if MBEDTLS_VERSION_NUMBER < 0x03020100
133- return mbedtls_pk_parse_keyfile (ctx , path , password );
134- #else
135- return mbedtls_pk_parse_keyfile (ctx , path , password , f_rng , p_rng );
136- #endif
137- }
138-
139- #if MBEDTLS_VERSION_NUMBER < 0x03020100
140- typedef enum
141- {
142- MBEDTLS_SSL_VERSION_UNKNOWN , /*!< Context not in use or version not yet negotiated. */
143- MBEDTLS_SSL_VERSION_TLS1_2 = 0x0303 , /*!< (D)TLS 1.2 */
144- MBEDTLS_SSL_VERSION_TLS1_3 = 0x0304 , /*!< (D)TLS 1.3 */
145- } mbedtls_ssl_protocol_version ;
146-
147- static inline void
148- mbedtls_ssl_conf_min_tls_version (mbedtls_ssl_config * conf , mbedtls_ssl_protocol_version tls_version )
149- {
150- int major = (tls_version >> 8 ) & 0xff ;
151- int minor = tls_version & 0xff ;
152- mbedtls_ssl_conf_min_version (conf , major , minor );
153- }
154-
155- static inline void
156- mbedtls_ssl_conf_max_tls_version (mbedtls_ssl_config * conf , mbedtls_ssl_protocol_version tls_version )
157- {
158- int major = (tls_version >> 8 ) & 0xff ;
159- int minor = tls_version & 0xff ;
160- mbedtls_ssl_conf_max_version (conf , major , minor );
161- }
162-
163- static inline void
164- mbedtls_ssl_conf_groups (mbedtls_ssl_config * conf , mbedtls_compat_group_id * groups )
165- {
166- mbedtls_ssl_conf_curves (conf , groups );
167- }
168-
169- static inline size_t
170- mbedtls_cipher_info_get_block_size (const mbedtls_cipher_info_t * cipher )
171- {
172- return (size_t )cipher -> block_size ;
173- }
174-
175- static inline size_t
176- mbedtls_cipher_info_get_iv_size (const mbedtls_cipher_info_t * cipher )
177- {
178- return (size_t )cipher -> iv_size ;
179- }
180-
181- static inline size_t
182- mbedtls_cipher_info_get_key_bitlen (const mbedtls_cipher_info_t * cipher )
183- {
184- return (size_t )cipher -> key_bitlen ;
185- }
186-
187- static inline mbedtls_cipher_mode_t
188- mbedtls_cipher_info_get_mode (const mbedtls_cipher_info_t * cipher )
189- {
190- return cipher -> mode ;
191- }
192-
193- static inline const char *
194- mbedtls_cipher_info_get_name (const mbedtls_cipher_info_t * cipher )
195- {
196- return cipher -> name ;
197- }
198-
199- static inline mbedtls_cipher_type_t
200- mbedtls_cipher_info_get_type (const mbedtls_cipher_info_t * cipher )
201- {
202- return cipher -> type ;
203- }
204-
205- static inline size_t
206- mbedtls_dhm_get_bitlen (const mbedtls_dhm_context * ctx )
207- {
208- return 8 * ctx -> len ;
209- }
210-
211- static inline const mbedtls_md_info_t *
212- mbedtls_md_info_from_ctx (const mbedtls_md_context_t * ctx )
213- {
214- return ctx -> md_info ;
215- }
216-
217- static inline const unsigned char *
218- mbedtls_pem_get_buffer (const mbedtls_pem_context * ctx , size_t * buf_size )
219- {
220- * buf_size = ctx -> buflen ;
221- return ctx -> buf ;
222- }
223-
224- static inline int
225- mbedtls_x509_crt_has_ext_type (const mbedtls_x509_crt * ctx , int ext_type )
226- {
227- return ctx -> ext_types & ext_type ;
228- }
229- #endif /* MBEDTLS_VERSION_NUMBER < 0x03020100 */
230-
23154#endif /* MBEDTLS_COMPAT_H_ */
0 commit comments