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>
[jf: rebase onto mbedtls-2.22.0]
[jf: rebase onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <jerome@forissier.org>
[jf: rebase onto mbedtls-2.28.1, fix typo in comment]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
[jw: rebase onto mbedtls-3.4.0, adjust new coding style]
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
[tve: rebase onto mbedtls-3.6.0, adjust for changes between 3.4 and 3.6]
Signed-off-by: Tom Van Eyck <tom.vaneyck@kuleuven.be>
diff --git a/lib/libmbedtls/mbedtls/library/cipher_wrap.c b/lib/libmbedtls/mbedtls/library/cipher_wrap.c
index d2fee22..05f4aa8 100644
--- a/lib/libmbedtls/mbedtls/library/cipher_wrap.c
+++ b/lib/libmbedtls/mbedtls/library/cipher_wrap.c
@@ -11,6 +11,8 @@
#include "common.h"
+#include <string.h>
+
#if defined(MBEDTLS_CIPHER_C)
#include "cipher_wrap.h"
@@ -129,6 +131,11 @@
return ctx;
}
+static void gcm_ctx_clone(void *dst, const void *src)
+{
+ memcpy(dst, src, sizeof(mbedtls_gcm_context));
+}
+
static void gcm_ctx_free(void *ctx)
{
mbedtls_gcm_free(ctx);
@@ -151,6 +158,11 @@
return ctx;
}
+static void ccm_ctx_clone(void *dst, const void *src)
+{
+ memcpy(dst, src, sizeof(mbedtls_ccm_context));
+}
+
static void ccm_ctx_free(void *ctx)
{
mbedtls_ccm_free(ctx);
@@ -257,6 +269,11 @@
return aes;
}
+static void aes_ctx_clone(void *dst, const void *src)
+{
+ memcpy(dst, src, sizeof(mbedtls_aes_context));
+}
+
static void aes_ctx_free(void *ctx)
{
mbedtls_aes_free((mbedtls_aes_context *) ctx);
@@ -289,6 +306,7 @@
aes_setkey_dec_wrap,
#endif
aes_ctx_alloc,
+ aes_ctx_clone,
aes_ctx_free
};
@@ -603,6 +621,7 @@
gcm_aes_setkey_wrap,
#endif
gcm_ctx_alloc,
+ gcm_ctx_clone,
gcm_ctx_free,
#else
NULL,
@@ -687,6 +706,7 @@
ccm_aes_setkey_wrap,
#endif
ccm_ctx_alloc,
+ ccm_ctx_clone,
ccm_ctx_free,
#else
NULL,
@@ -839,6 +859,11 @@
return ctx;
}
+static void camellia_ctx_clone(void *dst, const void *src)
+{
+ memcpy(dst, src, sizeof(mbedtls_camellia_context));
+}
+
static void camellia_ctx_free(void *ctx)
{
mbedtls_camellia_free((mbedtls_camellia_context *) ctx);
@@ -871,6 +896,7 @@
camellia_setkey_dec_wrap,
#endif
camellia_ctx_alloc,
+ camellia_ctx_clone,
camellia_ctx_free
};
@@ -1046,6 +1072,7 @@
gcm_camellia_setkey_wrap,
#endif
gcm_ctx_alloc,
+ gcm_ctx_clone,
gcm_ctx_free,
};
@@ -1117,6 +1144,7 @@
ccm_camellia_setkey_wrap,
#endif
ccm_ctx_alloc,
+ ccm_ctx_clone,
ccm_ctx_free,
};
@@ -1702,6 +1730,11 @@
return des;
}
+static void des_ctx_clone(void *dst, const void *src)
+{
+ memcpy(dst, src, sizeof(mbedtls_des_context));
+}
+
static void des_ctx_free(void *ctx)
{
mbedtls_des_free((mbedtls_des_context *) ctx);
@@ -1722,6 +1755,11 @@
return des3;
}
+static void des3_ctx_clone(void *dst, const void *src)
+{
+ memcpy(dst, src, sizeof(mbedtls_des3_context));
+}
+
static void des3_ctx_free(void *ctx)
{
mbedtls_des3_free((mbedtls_des3_context *) ctx);
@@ -1752,6 +1790,7 @@
des_setkey_enc_wrap,
des_setkey_dec_wrap,
des_ctx_alloc,
+ des_ctx_clone,
des_ctx_free
};
@@ -1803,6 +1842,7 @@
des3_set2key_enc_wrap,
des3_set2key_dec_wrap,
des3_ctx_alloc,
+ des3_ctx_clone,
des3_ctx_free
};
@@ -1854,6 +1894,7 @@
des3_set3key_enc_wrap,
des3_set3key_dec_wrap,
des3_ctx_alloc,
+ des3_ctx_clone,
des3_ctx_free
};
@@ -2073,6 +2114,12 @@
return (void *) 1;
}
+static void null_ctx_clone(void *dst, const void *src)
+{
+ ((void) dst);
+ ((void) src);
+}
+
static void null_ctx_free(void *ctx)
{
((void) ctx);
@@ -2104,6 +2151,7 @@
null_setkey,
#endif
null_ctx_alloc,
+ null_ctx_clone,
null_ctx_free
};