Move NULL check inside accessor function
This achieves two related goals:
1. Those members are now only accessed via the accessor function (except in
code paths that we don't care about: those guarded by
MBEDTLS_PK_RSA_ALT_SUPPORT or MBEDTLS_ECP_RESTARTABLE)
2. When we turn on compile-time dispatch, we don't obviously don't want to
keep a runtime NULL check.
For debug this requires changing the signature or the accessor function to
return int; this is done without changing the signature of the accessed
function.
diff --git a/library/pk.c b/library/pk.c
index 2519d87..9ded7e0 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -92,6 +92,9 @@
const unsigned char *hash, size_t hash_len,
const unsigned char *sig, size_t sig_len )
{
+ if( info->verify_func == NULL )
+ return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
+
return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
}
@@ -102,6 +105,9 @@
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
+ if( info->sign_func == NULL )
+ return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
+
return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
f_rng, p_rng ) );
}
@@ -113,6 +119,9 @@
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
+ if( info->decrypt_func == NULL )
+ return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
+
return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
f_rng, p_rng ) );
}
@@ -124,6 +133,9 @@
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
+ if( info->encrypt_func == NULL )
+ return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
+
return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
f_rng, p_rng ) );
}
@@ -131,6 +143,9 @@
MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
const mbedtls_pk_info_t *info, const void *pub, const void *prv )
{
+ if( info->check_pair_func == NULL )
+ return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
+
return( info->check_pair_func( pub, prv ) );
}
@@ -146,11 +161,15 @@
info->ctx_free_func( ctx );
}
-MBEDTLS_ALWAYS_INLINE static inline void pk_info_debug_func(
+MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
const mbedtls_pk_info_t *info,
const void *ctx, mbedtls_pk_debug_item *items )
{
+ if( info->debug_func == NULL )
+ return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
+
info->debug_func( ctx, items );
+ return( 0 );
}
/*
@@ -388,9 +407,6 @@
(void) rs_ctx;
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
- if( ctx->pk_info->verify_func == NULL )
- return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
-
return( pk_info_verify_func( ctx->pk_info, ctx->pk_ctx, md_alg, hash, hash_len,
sig, sig_len ) );
}
@@ -511,9 +527,6 @@
(void) rs_ctx;
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
- if( ctx->pk_info->sign_func == NULL )
- return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
-
return( pk_info_sign_func( ctx->pk_info, ctx->pk_ctx, md_alg, hash, hash_len,
sig, sig_len, f_rng, p_rng ) );
}
@@ -546,9 +559,6 @@
if( ctx->pk_info == NULL )
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
- if( ctx->pk_info->decrypt_func == NULL )
- return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
-
return( pk_info_decrypt_func( ctx->pk_info, ctx->pk_ctx, input, ilen,
output, olen, osize, f_rng, p_rng ) );
}
@@ -569,9 +579,6 @@
if( ctx->pk_info == NULL )
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
- if( ctx->pk_info->encrypt_func == NULL )
- return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
-
return( pk_info_encrypt_func( ctx->pk_info, ctx->pk_ctx, input, ilen,
output, olen, osize, f_rng, p_rng ) );
}
@@ -600,9 +607,6 @@
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
}
- if( prv->pk_info->check_pair_func == NULL )
- return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
-
return( pk_info_check_pair_func( prv->pk_info, pub->pk_ctx, prv->pk_ctx ) );
}
@@ -628,11 +632,7 @@
if( ctx->pk_info == NULL )
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
- if( ctx->pk_info->debug_func == NULL )
- return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
-
- pk_info_debug_func( ctx->pk_info, ctx->pk_ctx, items );
- return( 0 );
+ return( pk_info_debug_func( ctx->pk_info, ctx->pk_ctx, items ) );
}
/*