Skip to content
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

Merged
merged 3 commits into from
Feb 8, 2020

Conversation

mrfuchs
Copy link
Contributor

@mrfuchs mrfuchs commented Feb 2, 2020

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.

[00:00:05.269,000] <inf> main: Cipher Sample
[00:00:05.270,000] <inf> main: ECB Mode
[00:00:05.270,000] <inf> main: Output length (encryption): 16
[00:00:05.270,000] <inf> main: ECB mode ENCRYPT - Match
[00:00:05.270,000] <inf> main: Output length (decryption): 16
[00:00:05.270,000] <inf> main: ECB mode DECRYPT - Match
[00:00:05.271,000] <inf> main: CBC Mode
[00:00:05.271,000] <inf> main: Output length (encryption): 80
[00:00:05.271,000] <inf> main: CBC mode ENCRYPT - Match
[00:00:05.271,000] <inf> main: Output length (decryption): 64
[00:00:05.271,000] <inf> main: CBC mode DECRYPT - Match
[00:00:05.272,000] <inf> main: CTR Mode
[00:00:05.272,000] <err> mbedtls: Unsupported mode
[00:00:05.272,000] <inf> main: CCM Mode
[00:00:05.272,000] <inf> main: Output length (encryption): 31
[00:00:05.272,000] <inf> main: CCM mode ENCRYPT - Match
[00:00:05.273,000] <inf> main: Output length (decryption): 31
[00:00:05.273,000] <inf> main: CCM mode DECRYPT - Match
[00:00:05.273,000] <inf> main: GCM Mode
[00:00:05.274,000] <inf> main: Output length (encryption): 58
[00:00:05.274,000] <inf> main: GCM mode ENCRYPT - Match
[00:00:05.279,000] <inf> main: Output length (decryption): 58
[00:00:05.279,000] <inf> main: GCM mode DECRYPT - Match

@zephyrbot zephyrbot added area: API Changes to public APIs area: Samples Samples labels Feb 2, 2020
@zephyrbot
Copy link
Collaborator

zephyrbot commented Feb 2, 2020

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.

Copy link
Member

@ceolin ceolin left a 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>
Copy link
Member

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 ?

Copy link
Contributor Author

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.

Copy link
Collaborator

@tejlmand tejlmand left a 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.

LOG_ERR("Message authentication failed");
return -EFAULT;
}

LOG_ERR("Could non decrypt/auth (%d)", ret);
Copy link
Collaborator

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 ?

Suggested change
LOG_ERR("Could non decrypt/auth (%d)", ret);
LOG_ERR("Could not decrypt/auth (%d)", ret);

Copy link
Contributor Author

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
Copy link
Collaborator

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 ?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@mrfuchs mrfuchs Feb 4, 2020

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.

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);
Copy link
Collaborator

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

Suggested change
LOG_ERR("Could not setup the key (%d)", ret);
LOG_ERR("AES_GCM: failed at setkey (%d)", ret);

Copy link
Contributor Author

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>
@jhedberg jhedberg merged commit 29630cc into zephyrproject-rtos:master Feb 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Samples Samples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants