|
10 | 10 | #ifndef __BOOTUTIL_CRYPTO_AES_CTR_H_ |
11 | 11 | #define __BOOTUTIL_CRYPTO_AES_CTR_H_ |
12 | 12 |
|
13 | | -#include <string.h> |
14 | | - |
15 | 13 | #include "mcuboot_config/mcuboot_config.h" |
16 | 14 |
|
17 | 15 | #if (defined(MCUBOOT_USE_MBED_TLS) + \ |
18 | 16 | defined(MCUBOOT_USE_TINYCRYPT) + defined(MCUBOOT_USE_PSA_CRYPTO)) != 1 |
19 | 17 | #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA" |
20 | 18 | #endif |
21 | 19 |
|
22 | | -#include "bootutil/enc_key_public.h" |
23 | | - |
24 | 20 | #if defined(MCUBOOT_USE_MBED_TLS) |
25 | | - #include <mbedtls/aes.h> |
26 | | - #define BOOT_ENC_BLOCK_SIZE (16) |
| 21 | + #include "bootutil/crypto/aes_ctr_mbedtls.h" |
27 | 22 | #endif /* MCUBOOT_USE_MBED_TLS */ |
28 | 23 |
|
29 | 24 | #if defined(MCUBOOT_USE_TINYCRYPT) |
30 | | - #include <string.h> |
31 | | - #include <tinycrypt/aes.h> |
32 | | - #include <tinycrypt/ctr_mode.h> |
33 | | - #include <tinycrypt/constants.h> |
34 | | - #if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE) |
35 | | - #error "Cannot use AES-256 for encryption with Tinycrypt library." |
36 | | - #endif |
37 | | - #define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE |
| 25 | + #include "bootutil/crypto/aes_ctr_tinycrypt.h" |
38 | 26 | #endif /* MCUBOOT_USE_TINYCRYPT */ |
39 | 27 |
|
40 | 28 | #if defined(MCUBOOT_USE_PSA_CRYPTO) |
41 | | - #include <psa/crypto.h> |
42 | | - #define BOOT_ENC_BLOCK_SIZE (16) |
43 | | -#endif |
44 | | - |
45 | | -#include <stdint.h> |
46 | | - |
47 | | -#ifdef __cplusplus |
48 | | -extern "C" { |
49 | | -#endif |
50 | | - |
51 | | -#if defined(MCUBOOT_USE_PSA_CRYPTO) |
52 | | -typedef struct { |
53 | | - /* Fixme: This should not be, here, psa_key_id should be passed */ |
54 | | - uint8_t key[BOOT_ENC_KEY_SIZE]; |
55 | | -} bootutil_aes_ctr_context; |
56 | | - |
57 | | -void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx); |
58 | | - |
59 | | -static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx) |
60 | | -{ |
61 | | - memset(ctx, 0, sizeof(*ctx)); |
62 | | -} |
63 | | - |
64 | | -static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k) |
65 | | -{ |
66 | | - memcpy(ctx->key, k, sizeof(ctx->key)); |
67 | | - |
68 | | - return 0; |
69 | | -} |
70 | | - |
71 | | -int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, |
72 | | - const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c); |
73 | | -int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, |
74 | | - const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m); |
75 | | -#endif |
76 | | - |
77 | | -#if defined(MCUBOOT_USE_MBED_TLS) |
78 | | -typedef mbedtls_aes_context bootutil_aes_ctr_context; |
79 | | -static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx) |
80 | | -{ |
81 | | - (void)mbedtls_aes_init(ctx); |
82 | | -} |
83 | | - |
84 | | -static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx) |
85 | | -{ |
86 | | - mbedtls_aes_free(ctx); |
87 | | -} |
88 | | - |
89 | | -static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k) |
90 | | -{ |
91 | | - return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8); |
92 | | -} |
93 | | - |
94 | | -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) |
95 | | -{ |
96 | | - uint8_t stream_block[BOOT_ENC_BLOCK_SIZE]; |
97 | | - return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c); |
98 | | -} |
99 | | - |
100 | | -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) |
101 | | -{ |
102 | | - uint8_t stream_block[BOOT_ENC_BLOCK_SIZE]; |
103 | | - return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m); |
104 | | -} |
105 | | -#endif /* MCUBOOT_USE_MBED_TLS */ |
106 | | - |
107 | | -#if defined(MCUBOOT_USE_TINYCRYPT) |
108 | | -typedef struct tc_aes_key_sched_struct bootutil_aes_ctr_context; |
109 | | -static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx) |
110 | | -{ |
111 | | - (void)ctx; |
112 | | -} |
113 | | - |
114 | | -static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx) |
115 | | -{ |
116 | | - (void)ctx; |
117 | | -} |
118 | | - |
119 | | -static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k) |
120 | | -{ |
121 | | - int rc; |
122 | | - rc = tc_aes128_set_encrypt_key(ctx, k); |
123 | | - if (rc != TC_CRYPTO_SUCCESS) { |
124 | | - return -1; |
125 | | - } |
126 | | - return 0; |
127 | | -} |
128 | | - |
129 | | -static int _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) |
130 | | -{ |
131 | | - int rc; |
132 | | - rc = tc_ctr_mode(out, inlen, in, inlen, counter, &blk_off, ctx); |
133 | | - if (rc != TC_CRYPTO_SUCCESS) { |
134 | | - return -1; |
135 | | - } |
136 | | - return 0; |
137 | | -} |
138 | | - |
139 | | -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) |
140 | | -{ |
141 | | - return _bootutil_aes_ctr_crypt(ctx, counter, m, mlen, blk_off, c); |
142 | | -} |
143 | | - |
144 | | -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) |
145 | | -{ |
146 | | - return _bootutil_aes_ctr_crypt(ctx, counter, c, clen, blk_off, m); |
147 | | -} |
148 | | -#endif /* MCUBOOT_USE_TINYCRYPT */ |
149 | | - |
150 | | -#ifdef __cplusplus |
151 | | -} |
| 29 | + #include "bootutil/crypto/aes_ctr_psa.h" |
152 | 30 | #endif |
153 | 31 |
|
154 | 32 | #endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */ |
0 commit comments