-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
crypto: Add Galois/Counter Mode (GCM) support #22407
Conversation
All checks passed. Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages. |
15b54e8
to
7e8b60c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks ok for me, just a minor comment
@@ -21,6 +21,7 @@ | |||
#endif /* CONFIG_MBEDTLS_CFG_FILE */ | |||
|
|||
#include <mbedtls/ccm.h> | |||
#include <mbedtls/gcm.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this header available when mbedTLS is built without gcm support ? Shouldn't you put this include under ifdef ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's available - but you are right, it's probably better to include gcm.h
conditionally. That would mean that the mbedtls_gcm_context
in the session would also have to be surrounded by an ifdef
, though. Will change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of small comments, but generally looking good.
drivers/crypto/crypto_mtls_shim.c
Outdated
LOG_ERR("Message authentication failed"); | ||
return -EFAULT; | ||
} | ||
|
||
LOG_ERR("Could non decrypt/auth (%d)", ret); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you didn't touch this line of code, but I couldn't help to spot this spelling mistake, could you include in your fix ?
LOG_ERR("Could non decrypt/auth (%d)", ret); | |
LOG_ERR("Could not decrypt/auth (%d)", ret); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been fixed.
@@ -325,6 +398,25 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx, | |||
ctx->ops.ccm_crypt_hndlr = mtls_ccm_decrypt_auth; | |||
} | |||
break; | |||
case CRYPTO_CIPHER_MODE_GCM: | |||
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a test for CRYPTO_CIPHER_MODE_GCM
is already done i L423, which means if CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED=n
then code never reaches here when mode is CRYPTO_CIPHER_MODE_GCM
, so any reason not to include the case CRYPTO_CIPHER_MODE_GCM:
inside the #ifdef
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Should we add a default case to the switch statement then? I don't know, if all compilers and static code analysis tools will accept the previous mode check without complaining about unhandled case values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been changed. As GCC issued a
warning: enumeration value 'CRYPTO_CIPHER_MODE_GCM' not handled in switch
I decided to add a default case. It also seems to be more future-proof this way when new modes are added to the Crypto API.
drivers/crypto/crypto_mtls_shim.c
Outdated
ret = mbedtls_gcm_setkey(gcm_ctx, MBEDTLS_CIPHER_ID_AES, | ||
ctx->key.bit_stream, ctx->keylen * 8U); | ||
if (ret) { | ||
LOG_ERR("Could not setup the key (%d)", ret); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you follow same style as in:
https://github.com/zephyrproject-rtos/zephyr/blob/master/drivers/crypto/crypto_mtls_shim.c#L284
https://github.com/zephyrproject-rtos/zephyr/blob/master/drivers/crypto/crypto_mtls_shim.c#L305
LOG_ERR("Could not setup the key (%d)", ret); | |
LOG_ERR("AES_GCM: failed at setkey (%d)", ret); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been changed. Thanks for spotting and the suggestion!
Add support for AES Galois/Counter Mode of Operation to the Crypto API. Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
Add support for AES Galois/Counter Mode (GCM) of operation to the mbed TLS shim driver. Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
Add sample for AES Galois/Counter Mode (GCM) of operation with a MACsec GCM-AES test vector. Also improve existing code by declaring expected ciphertext arrays as constant. Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
Add support for AES Galois/Counter Mode of Operation to the Crypto API,
implement GCM in the mbed TLS shim driver and show its usage in the
crypto sample project.