pk: function to calculate the signature size
Expose a function mbedtls_pk_signature_size to calculate the maximum
size of a signature made with a given key. Document that this is the
buffer size that mbedtls_pk_sign requires.
Add a corresponding field signature_size_func to the mbedtls_pk_info
structure.
diff --git a/library/pk.c b/library/pk.c
index 9037646..b48f4d9 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -343,6 +343,20 @@
}
/*
+ * Maximum signature size
+ */
+size_t mbedtls_pk_signature_size( const mbedtls_pk_context *ctx )
+{
+ if( ctx == NULL || ctx->pk_info == NULL )
+ return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
+
+ if( ctx->pk_info->signature_size_func == NULL )
+ return( ( ctx->pk_info->get_bitlen( ctx->pk_ctx ) + 7 ) / 8 );
+ else
+ return( ctx->pk_info->signature_size_func( ctx->pk_ctx ) );
+}
+
+/*
* Export debug information
*/
int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
diff --git a/library/pk_wrap.c b/library/pk_wrap.c
index 55be595..0d8aee1 100644
--- a/library/pk_wrap.c
+++ b/library/pk_wrap.c
@@ -195,6 +195,7 @@
rsa_alloc_wrap,
rsa_free_wrap,
rsa_debug,
+ NULL,
};
#endif /* MBEDTLS_RSA_C */
@@ -262,6 +263,12 @@
return( ret );
}
+static size_t ecdsa_signature_size( const void *ctx_arg )
+{
+ const mbedtls_ecp_keypair *ctx = ctx_arg;
+ return( MBEDTLS_ECDSA_MAX_SIG_LEN( ctx->grp.pbits ) );
+}
+
#endif /* MBEDTLS_ECDSA_C */
static int eckey_check_pair( const void *pub, const void *prv )
@@ -311,6 +318,11 @@
eckey_alloc_wrap,
eckey_free_wrap,
eckey_debug,
+#if defined(MBEDTLS_ECDSA_C)
+ ecdsa_signature_size,
+#else
+ NULL,
+#endif
};
/*
@@ -336,6 +348,7 @@
eckey_alloc_wrap, /* Same underlying key structure */
eckey_free_wrap, /* Same underlying key structure */
eckey_debug, /* Same underlying key structure */
+ NULL,
};
#endif /* MBEDTLS_ECP_C */
@@ -400,6 +413,7 @@
ecdsa_alloc_wrap,
ecdsa_free_wrap,
eckey_debug, /* Compatible key structures */
+ ecdsa_signature_size,
};
#endif /* MBEDTLS_ECDSA_C */
@@ -519,6 +533,7 @@
rsa_alt_alloc_wrap,
rsa_alt_free_wrap,
NULL,
+ NULL,
};
#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */