libmbedtls: add interfaces in mbedtls for context memory operation
For integrating into OPTEE_OS, it needs add some interfaces:
1. add mbedtls_cipher_clone() for cipher to copy context between two
operations.
2. add mbedtls_cipher_setup_info() for cipher. cipher need to get its
"cipher_info" according the key length, while the key length is not an
input in allocate function. So, use a default key len in the beginning.
It need to reset the cipher info again in init function.
3. add mbedtls_cipher_cmac_setup() for cmac. This function is separate
from mbedtls_cipher_cmac_starts().
4. copy hmac context in md.
Acked-by: Etienne Carriere <etienne.carriere@linaro.org>
Signed-off-by: Edison Ai <edison.ai@arm.com>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/lib/libmbedtls/mbedtls/library/cipher.c b/lib/libmbedtls/mbedtls/library/cipher.c
index 599cd36..e722313 100644
--- a/lib/libmbedtls/mbedtls/library/cipher.c
+++ b/lib/libmbedtls/mbedtls/library/cipher.c
@@ -179,6 +179,36 @@
mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
}
+int mbedtls_cipher_clone( mbedtls_cipher_context_t *dst,
+ const mbedtls_cipher_context_t *src )
+{
+ if( dst == NULL || dst->cipher_info == NULL ||
+ src == NULL || src->cipher_info == NULL)
+ {
+ return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
+ }
+
+ dst->cipher_info = src->cipher_info;
+ dst->key_bitlen = src->key_bitlen;
+ dst->operation = src->operation;
+#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
+ dst->add_padding = src->add_padding;
+ dst->get_padding = src->get_padding;
+#endif
+ memcpy( dst->unprocessed_data, src->unprocessed_data, MBEDTLS_MAX_BLOCK_LENGTH );
+ dst->unprocessed_len = src->unprocessed_len;
+ memcpy( dst->iv, src->iv, MBEDTLS_MAX_IV_LENGTH );
+ dst->iv_size = src->iv_size;
+ if( dst->cipher_info->base->ctx_clone_func )
+ dst->cipher_info->base->ctx_clone_func( dst->cipher_ctx, src->cipher_ctx );
+
+#if defined(MBEDTLS_CMAC_C)
+ if( dst->cmac_ctx != NULL && src->cmac_ctx != NULL )
+ memcpy( dst->cmac_ctx, src->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
+#endif
+ return( 0 );
+}
+
int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
{
CIPHER_VALIDATE_RET( ctx != NULL );
@@ -206,6 +236,15 @@
return( 0 );
}
+int mbedtls_cipher_setup_info( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
+{
+ if( NULL == cipher_info || NULL == ctx )
+ return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
+
+ ctx->cipher_info = cipher_info;
+ return( 0 );
+}
+
int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
const unsigned char *key,
int key_bitlen,