-
Notifications
You must be signed in to change notification settings - Fork 263
Modify MCUboot to use PSA key_id_t for key type in AES encryption context #565
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
Open
de-nordic
wants to merge
2
commits into
nrfconnect:main
Choose a base branch
from
de-nordic:psa_use_key_id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * This module provides a thin abstraction over some of the crypto | ||
| * primitives to make it easier to swap out the used crypto library. | ||
| * | ||
| * At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or | ||
| * MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly | ||
| * one of these defined. | ||
| */ | ||
|
|
||
| #ifndef __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_ | ||
| #define __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_ | ||
|
|
||
| #include <string.h> | ||
| #include <stdint.h> | ||
| #include "mcuboot_config/mcuboot_config.h" | ||
| #include "bootutil/enc_key_public.h" | ||
| #include <mbedtls/aes.h> | ||
|
|
||
| #define BOOT_ENC_BLOCK_SIZE (16) | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef mbedtls_aes_context bootutil_aes_ctr_context; | ||
|
|
||
| static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx) | ||
| { | ||
| (void)mbedtls_aes_init(ctx); | ||
| } | ||
|
|
||
| static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx) | ||
| { | ||
| mbedtls_aes_free(ctx); | ||
| } | ||
|
|
||
| static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k) | ||
| { | ||
| return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8); | ||
| } | ||
|
|
||
| static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c) | ||
| { | ||
| uint8_t stream_block[BOOT_ENC_BLOCK_SIZE]; | ||
| return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c); | ||
| } | ||
|
|
||
| static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m) | ||
| { | ||
| uint8_t stream_block[BOOT_ENC_BLOCK_SIZE]; | ||
| return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m); | ||
| } | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_ */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * This module provides a thin abstraction over some of the crypto | ||
| * primitives to make it easier to swap out the used crypto library. | ||
| * | ||
| * At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or | ||
| * MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly | ||
| * one of these defined. | ||
| */ | ||
|
|
||
| #ifndef __BOOTUTIL_CRYPTO_AES_CTR_PSA_H_ | ||
| #define __BOOTUTIL_CRYPTO_AES_CTR_PSA_H_ | ||
|
|
||
| #include <string.h> | ||
| #include <stdint.h> | ||
| #include "mcuboot_config/mcuboot_config.h" | ||
| #include "bootutil/enc_key_public.h" | ||
| #include <psa/crypto.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef struct { | ||
| psa_key_id_t key; | ||
| } bootutil_aes_ctr_context; | ||
|
|
||
| void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx); | ||
|
|
||
| void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx); | ||
| int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k); | ||
|
|
||
| int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, | ||
|
Check warning on line 32 in boot/bootutil/include/bootutil/crypto/aes_ctr_psa.h
|
||
| const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c); | ||
| int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, | ||
|
Check warning on line 34 in boot/bootutil/include/bootutil/crypto/aes_ctr_psa.h
|
||
| const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * This module provides a thin abstraction over some of the crypto | ||
| * primitives to make it easier to swap out the used crypto library. | ||
| * | ||
| * At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or | ||
| * MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly | ||
| * one of these defined. | ||
| */ | ||
|
|
||
| #ifndef __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_ | ||
| #define __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_ | ||
|
|
||
| #include <string.h> | ||
| #include <stdint.h> | ||
| #include "mcuboot_config/mcuboot_config.h" | ||
| #include "bootutil/enc_key_public.h" | ||
|
|
||
| #include <tinycrypt/aes.h> | ||
| #include <tinycrypt/ctr_mode.h> | ||
| #include <tinycrypt/constants.h> | ||
|
|
||
| #if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE) | ||
| #error "Cannot use AES-256 for encryption with Tinycrypt library." | ||
| #endif | ||
|
|
||
| #define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef struct tc_aes_key_sched_struct bootutil_aes_ctr_context; | ||
| static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx) | ||
| { | ||
| (void)ctx; | ||
| } | ||
|
|
||
| static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx) | ||
| { | ||
| (void)ctx; | ||
| } | ||
|
|
||
| static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k) | ||
| { | ||
| int rc; | ||
| rc = tc_aes128_set_encrypt_key(ctx, k); | ||
| if (rc != TC_CRYPTO_SUCCESS) { | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static int common_bootutil_aes_ctr_crypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *in, uint32_t inlen, uint32_t blk_off, uint8_t *out) | ||
| { | ||
| int rc; | ||
| rc = tc_ctr_mode(out, inlen, in, inlen, counter, &blk_off, ctx); | ||
| if (rc != TC_CRYPTO_SUCCESS) { | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, uint32_t blk_off, uint8_t *c) | ||
| { | ||
| return common_bootutil_aes_ctr_crypt(ctx, counter, m, mlen, blk_off, c); | ||
| } | ||
|
|
||
| static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, uint32_t blk_off, uint8_t *m) | ||
| { | ||
| return common_bootutil_aes_ctr_crypt(ctx, counter, c, clen, blk_off, m); | ||
| } | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_ */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ideally, I'd prefer to have docstrings inside the interface header, but since this is a [fromtree] commit, I'm not going to require them at this stage.