Skip to content

Commit 7abf3c5

Browse files
Tomasz BursztykaAnas Nashif
authored andcommitted
drivers/crypto: Add mbedTLS shim crypto driver
This exposes the CCM operations through generic Crypto API. Change-Id: I09346e77bf8821c208305a7aa2805cf49cb42d71 Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 422a769 commit 7abf3c5

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

drivers/crypto/Kconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ config CRYPTO_TINYCRYPT_SHIM_DRV_NAME
6161
help
6262
Device name for TinyCrypt Pseudo device.
6363

64+
config CRYPTO_MBEDTLS_SHIM
65+
bool "Enable mbedTLS shim driver [EXPERIMENTAL] "
66+
default n
67+
select MBEDTLS
68+
select MBEDTLS_ENABLE_HEAP
69+
help
70+
Enable mbedTLS shim layer compliant with crypto APIs. You will need
71+
to fill in a relevant value to CONFIG_MBEDTLS_HEAP_SIZE.
72+
73+
config CRYPTO_MBEDTLS_SHIM_DRV_NAME
74+
string "Device name for mbedTLS Pseudo device"
75+
default "CRYPTO_MTLS"
76+
depends on CRYPTO_MBEDTLS_SHIM
77+
help
78+
Device name for mbedTLS Pseudo device.
79+
80+
config CRYPTO_MBEDTLS_SHIM_MAX_SESSION
81+
int "Maximum of sessions mbedTLS shim driver can handle"
82+
default 2
83+
help
84+
This can be used to tweak the amount of sessions the driver
85+
can handle in parallel.
86+
6487
source "drivers/crypto/Kconfig.ataes132a"
6588

6689
endif # CRYPTO

drivers/crypto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ccflags-$(CONFIG_CRYPTO_TINYCRYPT_SHIM) += -I${srctree}/ext/lib/crypto/tinycrypt
22

33
obj-$(CONFIG_CRYPTO_TINYCRYPT_SHIM) += crypto_tc_shim.o
44
obj-$(CONFIG_CRYPTO_ATAES132A) += crypto_ataes132a.o
5+
obj-$(CONFIG_CRYPTO_MBEDTLS_SHIM) += crypto_mtls_shim.o

drivers/crypto/crypto_mtls_shim.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright (c) 2017 Intel Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file Shim layer for mbedTLS, crypto API compliant.
9+
*/
10+
11+
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_CRYPTO_LEVEL
12+
#include <logging/sys_log.h>
13+
14+
#include <kernel.h>
15+
#include <init.h>
16+
#include <errno.h>
17+
#include <crypto/cipher.h>
18+
19+
#if !defined(CONFIG_MBEDTLS_CFG_FILE)
20+
#include "mbedtls/config.h"
21+
#else
22+
#include CONFIG_MBEDTLS_CFG_FILE
23+
#endif /* CONFIG_MBEDTLS_CFG_FILE */
24+
25+
#include <mbedtls/ccm.h>
26+
#include <mbedtls/aes.h>
27+
28+
#define MTLS_SUPPORT (CAP_RAW_KEY | CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS)
29+
30+
struct mtls_shim_session {
31+
mbedtls_ccm_context mtls;
32+
bool in_use;
33+
};
34+
35+
#define CRYPTO_MAX_SESSION CONFIG_CRYPTO_MBEDTLS_SHIM_MAX_SESSION
36+
37+
struct mtls_shim_session mtls_sessions[CRYPTO_MAX_SESSION];
38+
39+
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
40+
#include "mbedtls/memory_buffer_alloc.h"
41+
#else
42+
#error "You need to define MBEDTLS_MEMORY_BUFFER_ALLOC_C"
43+
#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
44+
45+
static int mtls_ccm_encrypt_auth(struct cipher_ctx *ctx,
46+
struct cipher_aead_pkt *apkt,
47+
u8_t *nonce)
48+
{
49+
mbedtls_ccm_context *mtls_ctx =
50+
&((struct mtls_shim_session *)ctx->drv_sessn_state)->mtls;
51+
int ret;
52+
53+
ret = mbedtls_ccm_encrypt_and_tag(mtls_ctx, apkt->pkt->in_len, nonce,
54+
ctx->mode_params.ccm_info.nonce_len,
55+
apkt->ad, apkt->ad_len,
56+
apkt->pkt->in_buf,
57+
apkt->pkt->out_buf, apkt->tag,
58+
ctx->mode_params.ccm_info.tag_len);
59+
if (ret) {
60+
SYS_LOG_ERR("Could non encrypt/auth (%d)", ret);
61+
62+
/*ToDo: try to return relevant code depending on ret? */
63+
return -EINVAL;
64+
}
65+
66+
return 0;
67+
}
68+
69+
static int mtls_ccm_decrypt_auth(struct cipher_ctx *ctx,
70+
struct cipher_aead_pkt *apkt,
71+
u8_t *nonce)
72+
{
73+
mbedtls_ccm_context *mtls_ctx =
74+
&((struct mtls_shim_session *)ctx->drv_sessn_state)->mtls;
75+
int ret;
76+
77+
ret = mbedtls_ccm_auth_decrypt(mtls_ctx, apkt->pkt->in_len, nonce,
78+
ctx->mode_params.ccm_info.nonce_len,
79+
apkt->ad, apkt->ad_len,
80+
apkt->pkt->in_buf,
81+
apkt->pkt->out_buf, apkt->tag,
82+
ctx->mode_params.ccm_info.tag_len);
83+
if (ret) {
84+
SYS_LOG_ERR("Could non decrypt/auth (%d)", ret);
85+
86+
/*ToDo: try to return relevant code depending on ret? */
87+
return -EINVAL;
88+
}
89+
90+
return 0;
91+
}
92+
93+
static int mtls_get_unused_session_index(void)
94+
{
95+
int i;
96+
97+
for (i = 0; i < CRYPTO_MAX_SESSION; i++) {
98+
if (!mtls_sessions[i].in_use) {
99+
mtls_sessions[i].in_use = true;
100+
return i;
101+
}
102+
}
103+
104+
return -1;
105+
}
106+
107+
int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
108+
enum cipher_algo algo, enum cipher_mode mode,
109+
enum cipher_op op_type)
110+
{
111+
mbedtls_ccm_context *mtls_ctx;
112+
int ctx_idx;
113+
int ret;
114+
115+
if (ctx->flags & ~(MTLS_SUPPORT)) {
116+
SYS_LOG_ERR("Unsupported flag");
117+
return -EINVAL;
118+
}
119+
120+
if (algo != CRYPTO_CIPHER_ALGO_AES) {
121+
SYS_LOG_ERR("Unsupported algo");
122+
return -EINVAL;
123+
}
124+
125+
if (mode != CRYPTO_CIPHER_MODE_CCM) {
126+
SYS_LOG_ERR("Unsupported mode");
127+
return -EINVAL;
128+
}
129+
130+
if (ctx->keylen != 16) {
131+
SYS_LOG_ERR("%u key size is not supported", ctx->keylen);
132+
return -EINVAL;
133+
}
134+
135+
ctx_idx = mtls_get_unused_session_index();
136+
if (ctx_idx < 0) {
137+
SYS_LOG_ERR("No free session for now");
138+
return -ENOSPC;
139+
}
140+
141+
mtls_ctx = &mtls_sessions[ctx_idx].mtls;
142+
143+
mbedtls_ccm_init(mtls_ctx);
144+
145+
ret = mbedtls_ccm_setkey(mtls_ctx, MBEDTLS_CIPHER_ID_AES,
146+
ctx->key.bit_stream, ctx->keylen * 8);
147+
if (ret) {
148+
SYS_LOG_ERR("Could not setup the key (%d)", ret);
149+
mtls_sessions[ctx_idx].in_use = false;
150+
151+
return -EINVAL;
152+
}
153+
154+
ctx->drv_sessn_state = &mtls_sessions[ctx_idx];
155+
156+
if (op_type == CRYPTO_CIPHER_OP_ENCRYPT) {
157+
ctx->ops.ccm_crypt_hndlr = mtls_ccm_encrypt_auth;
158+
} else {
159+
ctx->ops.ccm_crypt_hndlr = mtls_ccm_decrypt_auth;
160+
}
161+
162+
return ret;
163+
}
164+
165+
int mtls_session_free(struct device *dev, struct cipher_ctx *ctx)
166+
{
167+
struct mtls_shim_session *mtls_session =
168+
(struct mtls_shim_session *)ctx->drv_sessn_state;
169+
170+
mbedtls_ccm_free(&mtls_session->mtls);
171+
mtls_session->in_use = false;
172+
173+
return 0;
174+
}
175+
176+
int mtls_query_caps(struct device *dev)
177+
{
178+
return MTLS_SUPPORT;
179+
}
180+
181+
static int mtls_shim_init(struct device *dev)
182+
{
183+
return 0;
184+
}
185+
186+
static struct crypto_driver_api mtls_crypto_funcs = {
187+
.begin_session = mtls_session_setup,
188+
.free_session = mtls_session_free,
189+
.crypto_async_callback_set = NULL,
190+
.query_hw_caps = mtls_query_caps,
191+
};
192+
193+
DEVICE_AND_API_INIT(crypto_mtls, CONFIG_CRYPTO_MBEDTLS_SHIM_DRV_NAME,
194+
&mtls_shim_init, NULL, NULL,
195+
POST_KERNEL, CONFIG_CRYPTO_INIT_PRIORITY,
196+
(void *)&mtls_crypto_funcs);

0 commit comments

Comments
 (0)