Make sub-context statically allocated
This makes a mbedtls_pk_context memory-wise equivalent to a
mbedtls_uecc_keypair and removes a dynamic allocation, making the PK layer
zero-cost in terms of memory when PK_SINGLE_TYPE is enabled.
diff --git a/include/mbedtls/pk.h b/include/mbedtls/pk.h
index 09a8513..b6c2998 100644
--- a/include/mbedtls/pk.h
+++ b/include/mbedtls/pk.h
@@ -49,6 +49,10 @@
#include "tinycrypt/ecc.h"
#endif
+#if defined(MBEDTLS_PK_SINGLE_TYPE)
+#include "pk_internal.h"
+#endif
+
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
@@ -137,17 +141,6 @@
#define MBEDTLS_PK_INVALID_HANDLE ( (mbedtls_pk_handle_t) NULL )
#endif /* MBEDTLS_PK_SINGLE_TYPE */
-/**
- * \brief Public key container
- */
-typedef struct mbedtls_pk_context
-{
-#if !defined(MBEDTLS_PK_SINGLE_TYPE)
- mbedtls_pk_handle_t pk_info; /**< Public key information */
-#endif
- void * pk_ctx; /**< Underlying public key context */
-} mbedtls_pk_context;
-
#if defined(MBEDTLS_USE_TINYCRYPT)
typedef struct
{
@@ -156,6 +149,22 @@
} mbedtls_uecc_keypair;
#endif
+/**
+ * \brief Public key container
+ */
+typedef struct mbedtls_pk_context
+{
+#if defined(MBEDTLS_PK_SINGLE_TYPE)
+ /* This is an array to make access to it more uniform with the case where
+ * it's a pointer to void - either way it needs casting before use. */
+ unsigned char pk_ctx[sizeof(
+ MBEDTLS_PK_INFO_CONTEXT( MBEDTLS_PK_SINGLE_TYPE ) )];
+#else
+ mbedtls_pk_handle_t pk_info; /**< Public key information */
+ void * pk_ctx; /**< Underlying public key context */
+#endif
+} mbedtls_pk_context;
+
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
/**
* \brief Context for resuming operations
@@ -184,11 +193,18 @@
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_USE_TINYCRYPT)
+#if !defined(MBEDTLS_PK_SINGLE_TYPE)
static inline mbedtls_uecc_keypair *mbedtls_pk_uecc( const mbedtls_pk_context pk )
{
return( (mbedtls_uecc_keypair *) (pk).pk_ctx );
}
+#else
+/* Go with a macro in order to avoid making a copy of the struct (the argument
+ * is not a pointer so it's passed by value) and then returning an address
+ * inside that copy, which would be undefined behaviour. */
+#define mbedtls_pk_uecc( pk ) ( (mbedtls_uecc_keypair *) (pk).pk_ctx )
#endif
+#endif /* MBEDTLS_USE_TINYCRYPT */
#if defined(MBEDTLS_ECP_C)
/**