Skip to content

Commit 5a161e4

Browse files
committed
bootutil: Split header with encryption functions
Split definitions to crypto backend specific headers. Signed-off-by: Dominik Ermel <[email protected]>
1 parent c27bb0f commit 5a161e4

File tree

4 files changed

+191
-125
lines changed

4 files changed

+191
-125
lines changed

boot/bootutil/include/bootutil/crypto/aes_ctr.h

Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -10,145 +10,23 @@
1010
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_H_
1111
#define __BOOTUTIL_CRYPTO_AES_CTR_H_
1212

13-
#include <string.h>
14-
1513
#include "mcuboot_config/mcuboot_config.h"
1614

1715
#if (defined(MCUBOOT_USE_MBED_TLS) + \
1816
defined(MCUBOOT_USE_TINYCRYPT) + defined(MCUBOOT_USE_PSA_CRYPTO)) != 1
1917
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA"
2018
#endif
2119

22-
#include "bootutil/enc_key_public.h"
23-
2420
#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"
2722
#endif /* MCUBOOT_USE_MBED_TLS */
2823

2924
#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"
3826
#endif /* MCUBOOT_USE_TINYCRYPT */
3927

4028
#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"
15230
#endif
15331

15432
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This module provides a thin abstraction over some of the crypto
3+
* primitives to make it easier to swap out the used crypto library.
4+
*
5+
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6+
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7+
* one of these defined.
8+
*/
9+
10+
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_
11+
#define __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_
12+
13+
#include <string.h>
14+
#include <stdint.h>
15+
#include "mcuboot_config/mcuboot_config.h"
16+
#include "bootutil/enc_key_public.h"
17+
#include <mbedtls/aes.h>
18+
19+
#define BOOT_ENC_BLOCK_SIZE (16)
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
typedef mbedtls_aes_context bootutil_aes_ctr_context;
26+
27+
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
28+
{
29+
(void)mbedtls_aes_init(ctx);
30+
}
31+
32+
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
33+
{
34+
mbedtls_aes_free(ctx);
35+
}
36+
37+
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
38+
{
39+
return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8);
40+
}
41+
42+
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)
43+
{
44+
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
45+
return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
46+
}
47+
48+
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)
49+
{
50+
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
51+
return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
52+
}
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif
57+
58+
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_MBEDTLS_H_ */
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This module provides a thin abstraction over some of the crypto
3+
* primitives to make it easier to swap out the used crypto library.
4+
*
5+
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6+
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7+
* one of these defined.
8+
*/
9+
10+
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_PSA_H_
11+
#define __BOOTUTIL_CRYPTO_AES_CTR_PSA_H_
12+
13+
#include <string.h>
14+
#include <stdint.h>
15+
#include "mcuboot_config/mcuboot_config.h"
16+
#include "bootutil/enc_key_public.h"
17+
#include <psa/crypto.h>
18+
19+
#define BOOT_ENC_BLOCK_SIZE (16)
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
typedef struct {
26+
/* Fixme: This should not be, here, psa_key_id should be passed */
27+
uint8_t key[BOOT_ENC_KEY_SIZE];
28+
} bootutil_aes_ctr_context;
29+
30+
void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx);
31+
32+
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
33+
{
34+
memset(ctx, 0, sizeof(*ctx));
35+
}
36+
37+
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
38+
{
39+
memcpy(ctx->key, k, sizeof(ctx->key));
40+
41+
return 0;
42+
}
43+
44+
int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
45+
const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c);
46+
int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter,
47+
const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m);
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
53+
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* This module provides a thin abstraction over some of the crypto
3+
* primitives to make it easier to swap out the used crypto library.
4+
*
5+
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6+
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7+
* one of these defined.
8+
*/
9+
10+
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_
11+
#define __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_
12+
13+
#include <string.h>
14+
#include <stdint.h>
15+
#include "mcuboot_config/mcuboot_config.h"
16+
#include "bootutil/enc_key_public.h"
17+
18+
#include <tinycrypt/aes.h>
19+
#include <tinycrypt/ctr_mode.h>
20+
#include <tinycrypt/constants.h>
21+
22+
#if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE)
23+
#error "Cannot use AES-256 for encryption with Tinycrypt library."
24+
#endif
25+
26+
#define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
typedef struct tc_aes_key_sched_struct bootutil_aes_ctr_context;
33+
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
34+
{
35+
(void)ctx;
36+
}
37+
38+
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
39+
{
40+
(void)ctx;
41+
}
42+
43+
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
44+
{
45+
int rc;
46+
rc = tc_aes128_set_encrypt_key(ctx, k);
47+
if (rc != TC_CRYPTO_SUCCESS) {
48+
return -1;
49+
}
50+
return 0;
51+
}
52+
53+
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)
54+
{
55+
int rc;
56+
rc = tc_ctr_mode(out, inlen, in, inlen, counter, &blk_off, ctx);
57+
if (rc != TC_CRYPTO_SUCCESS) {
58+
return -1;
59+
}
60+
return 0;
61+
}
62+
63+
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)
64+
{
65+
return common_bootutil_aes_ctr_crypt(ctx, counter, m, mlen, blk_off, c);
66+
}
67+
68+
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)
69+
{
70+
return common_bootutil_aes_ctr_crypt(ctx, counter, c, clen, blk_off, m);
71+
}
72+
73+
#ifdef __cplusplus
74+
}
75+
#endif
76+
77+
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_TINYCRYPT_H_ */

0 commit comments

Comments
 (0)