Skip to content

Support mbedtls crypto library #2705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

edison-ai
Copy link

Move #2345 here.

Edison Ai and others added 2 commits December 13, 2018 16:47
Initial step of mbedtls cryptos integration.
Directory created and interface file is drafted.
All function interfaces are set to "not supported".
The mbedtls can be selected by specifying build flags
"CFG_CRYPTOLIB_NAME=mbedtls" and "CFG_CRYPTOLIB_DIR=lib/libmbedtls"

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
1. Support mbedtls hash algorithm.
2. Add mbedtls source configure

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
@jenswi-linaro
Copy link
Contributor

There seems to be some confusion here. Only the commits:
"libmbedtls: GCM and CCM enhance for integrate", "libmbedtls: fix no CRT issue" and "libmbedtls: add interfaces in mbedtls for context memory operation" should first be merged into import/mbedtls-2.6.1.

The purpose of the import/mbedtls-2.6.1 is to record all changes to the imports source code of mbedtls-2.6.1. This will then be used when we import a newer version of mbedtls.

Since import/mbedtls-2.6.1 happens to be quite close to master for the moment it's still meaningful to review the changes here. It's just that before anything can be merged we'll need a new PR against the master branch. I'll continue the review now.

Copy link
Contributor

@jenswi-linaro jenswi-linaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment for "libmbedtls: Support DSA algorithm"

Copy link
Contributor

@jenswi-linaro jenswi-linaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments for the commit "libmbedtls: Support cipher CTS and XTS algorithm"

@@ -1931,11 +1990,13 @@ static TEE_Result cipher_get_ctx_size(uint32_t algo, size_t *size)
#endif
#if defined(CFG_CRYPTO_XTS)
case TEE_ALG_AES_XTS:
return TEE_ERROR_NOT_SUPPORTED;
*size = sizeof(struct tee_symmetric_cts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tee_symmetric_xts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry for mistake.

#endif
#if defined(CFG_CRYPTO_CTS)
case TEE_ALG_AES_CTS:
return TEE_ERROR_NOT_SUPPORTED;
*size = sizeof(struct tee_symmetric_xts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tee_symmetric_cts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will fix it.

@@ -2016,8 +2077,10 @@ TEE_Result crypto_cipher_alloc_ctx(void **ctx_ret, uint32_t algo)
return TEE_ERROR_NOT_SUPPORTED;
}

if (!cipher_info)
if (algo != TEE_ALG_AES_XTS && algo != TEE_ALG_AES_CTS &&
!cipher_info) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: the braces aren't needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will fix it.

@@ -2049,13 +2116,26 @@ void crypto_cipher_free_ctx(void *ctx, uint32_t algo __maybe_unused)
* could never have succeded above.
*/
assert(!cipher_get_ctx_size(algo, &ctx_size));
mbedtls_cipher_free(ctx);
#if defined(CFG_CRYPTO_XTS) || defined(CFG_CRYPTO_CTS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless CFG_CRYPTO_XTS or CFG_CRYPTO_CTS is defined we'll potentially leak memory.
I see no harm in always having the if (...) below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a mistake. Should not add them here.

if (algo == TEE_ALG_AES_XTS || algo == TEE_ALG_AES_CTS) {
res = cipher_get_ctx_size(algo, &ctx_size);
assert(!res);
memcpy(dst_ctx, src_ctx, ctx_size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a return; is needed after this line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will add it.

@@ -2071,6 +2151,58 @@ TEE_Result crypto_cipher_init(void *ctx, uint32_t algo,
const mbedtls_cipher_info_t *cipher_info = NULL;
int lmd_res;
int lmd_mode;
#if defined(CFG_CRYPTO_CTS)
struct tee_symmetric_cts *cts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These helper variables are only used in their if blocks below so please move the declarations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.


#if defined(CFG_CRYPTO_XTS)
if (algo == TEE_ALG_AES_XTS) {
res = tee_algo_to_ltc_cipherindex(algo, &ltc_cipherindex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could move these three lines into the other if (algo == TEE_ALG_AES_XTS) block below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@@ -2127,6 +2259,13 @@ TEE_Result crypto_cipher_update(void *ctx, uint32_t algo,
int lmd_res;
size_t olen;
size_t finish_olen;
#if defined(CFG_CRYPTO_CTS)
struct tee_symmetric_cts *cts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if the #if was skipped and the __maybe_unused tag was used instead, here and the two other locals below.

Copy link
Author

@edison-ai edison-ai Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for just copy code to here directly. I will fix it.

Copy link
Contributor

@jenswi-linaro jenswi-linaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments for commit "libmbedtls: Support CBC MAC algorithm"


#if defined(CFG_CRYPTO_CBC_MAC)
/*
* CBC-MAC is not implemented in Libtomcrypt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Libtomcrypt/mbedtls/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will fix it.

return TEE_ERROR_NOT_SUPPORTED;
cbc = (struct cbc_state *)ctx;

res = tee_algo_to_ltc_cipherindex(algo, &ltc_cipherindex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come the mbedtls implementation of AES/DES/DES3 isn't used instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry not notice it before. I will refine it by using mbedtls.

@jenswi-linaro
Copy link
Contributor

For "libmbedtls: Support DSA algorithm" please apply:
Reviewed-by: Jens Wiklander <[email protected]>

@jenswi-linaro
Copy link
Contributor

I'm done with the review for the moment.

@edison-ai
Copy link
Author

For "libmbedtls: GCM and CCM enhance for integrate", "libmbedtls: fix no CRT issue" and "libmbedtls: add interfaces in mbedtls for context memory operation", I will drop them in this PR. Because they had been in #2439.

@jenswi-linaro
Copy link
Contributor

We've advanced far enough in this PR that it would make sense to start merging that #2439.

@edison-ai edison-ai force-pushed the merge_to_mbedtls-2.6.1 branch from 1ae0da7 to fcc6a08 Compare December 19, 2018 07:03
Edison Ai and others added 13 commits December 19, 2018 15:50
Adds support for symmetrical ciphers. The CTS and XTS modes
are not supported in mbedTLS and will be dealt with later.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Adds size of expanded AES encryption key to crypto_aes_expand_enc_key()
and crypto_aes_enc_block() to make the functions more safe to call.

Signed-off-by: Summer Qin <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
These two implement interfaces will be used by AES-GCM algo.

Signed-off-by: Summer Qin <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Implement HMAC function which based on mbedtls.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Implement CMAC function which based on mbedtls.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Implement bignum function which based on mbedtls.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Support RSA:
	RSASSA_PKCS1_V1_5
	RSASSA_PKCS1_PSS_MGF1
	RSAES_PKCS1_V1_5
	RSAES_PKCS1_OAEP_MGF1

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Implement DH function which based on mbedtls.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Support mbedtls ECC: ecdh and ecdsa.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Implement CCM function which based on mbedtls.

Signed-off-by: Shawn Shan <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
DSA is not supported in MbedTLS, call libtomcrypt directly.

Signed-off-by: Edison Ai <[email protected]>
Reviewed-by: Jens Wiklander <[email protected]>
Cipher CTS and XTS are not supported in MbedTLS, call libtomcrypt directly.

Signed-off-by: Edison Ai <[email protected]>
CBC MAC supported in MbedTLS, implement is by using cipher.

Signed-off-by: Edison Ai <[email protected]>
@edison-ai edison-ai force-pushed the merge_to_mbedtls-2.6.1 branch from fcc6a08 to a1a3d47 Compare December 19, 2018 08:01
@jbech-linaro
Copy link
Contributor

There are a couple of compiler errors in lib/libmbedtls/core/tee_lmd_provider.c (see IBART).

@edison-ai
Copy link
Author

@jbech-linaro Yes. It is because I remove 3 patches form this PR for them are in another PR #2439. As discuss, I remove them here.
Is it possible to set the PR dependence, and include the #2439 when build this PR?

@jbech-linaro
Copy link
Contributor

Is it possible to set the PR dependence, and include the #2439 when build this PR?

Not at this point, that's a feature on the IBART to-do list and was actually one of the reasons I started writing IBART. So if it's a cross dependency, then we have to live with that (the error) for now.

@jenswi-linaro
Copy link
Contributor

Replaced by the now merged #2904

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants