Skip to content

Commit ad8f011

Browse files
mrfuchsjhedberg
authored andcommitted
drivers: crypto: crypto_mtls_shim: Add AES-GCM support
Add support for AES Galois/Counter Mode (GCM) of operation to the mbed TLS shim driver. Signed-off-by: Markus Fuchs <[email protected]>
1 parent e1948ae commit ad8f011

File tree

1 file changed

+107
-3
lines changed

1 file changed

+107
-3
lines changed

drivers/crypto/crypto_mtls_shim.c

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#endif /* CONFIG_MBEDTLS_CFG_FILE */
2222

2323
#include <mbedtls/ccm.h>
24+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
25+
#include <mbedtls/gcm.h>
26+
#endif
2427
#include <mbedtls/aes.h>
2528

2629
#define MTLS_SUPPORT (CAP_RAW_KEY | CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS | \
@@ -33,6 +36,9 @@ LOG_MODULE_REGISTER(mbedtls);
3336
struct mtls_shim_session {
3437
union {
3538
mbedtls_ccm_context mtls_ccm;
39+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
40+
mbedtls_gcm_context mtls_gcm;
41+
#endif
3642
mbedtls_aes_context mtls_aes;
3743
};
3844
bool in_use;
@@ -203,7 +209,12 @@ static int mtls_ccm_decrypt_auth(struct cipher_ctx *ctx,
203209
apkt->pkt->out_buf, apkt->tag,
204210
ctx->mode_params.ccm_info.tag_len);
205211
if (ret) {
206-
LOG_ERR("Could non decrypt/auth (%d)", ret);
212+
if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
213+
LOG_ERR("Message authentication failed");
214+
return -EFAULT;
215+
}
216+
217+
LOG_ERR("Could not decrypt/auth (%d)", ret);
207218

208219
/*ToDo: try to return relevant code depending on ret? */
209220
return -EINVAL;
@@ -215,6 +226,66 @@ static int mtls_ccm_decrypt_auth(struct cipher_ctx *ctx,
215226
return 0;
216227
}
217228

229+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
230+
static int mtls_gcm_encrypt_auth(struct cipher_ctx *ctx,
231+
struct cipher_aead_pkt *apkt,
232+
u8_t *nonce)
233+
{
234+
mbedtls_gcm_context *mtls_ctx = MTLS_GET_CTX(ctx, gcm);
235+
int ret;
236+
237+
ret = mbedtls_gcm_crypt_and_tag(mtls_ctx, MBEDTLS_GCM_ENCRYPT,
238+
apkt->pkt->in_len, nonce,
239+
ctx->mode_params.gcm_info.nonce_len,
240+
apkt->ad, apkt->ad_len,
241+
apkt->pkt->in_buf,
242+
apkt->pkt->out_buf,
243+
ctx->mode_params.gcm_info.tag_len,
244+
apkt->tag);
245+
if (ret) {
246+
LOG_ERR("Could not encrypt/auth (%d)", ret);
247+
248+
return -EINVAL;
249+
}
250+
251+
/* This is equivalent to what is done in mtls_ccm_encrypt_auth(). */
252+
apkt->pkt->out_len = apkt->pkt->in_len;
253+
apkt->pkt->out_len += ctx->mode_params.gcm_info.tag_len;
254+
255+
return 0;
256+
}
257+
258+
static int mtls_gcm_decrypt_auth(struct cipher_ctx *ctx,
259+
struct cipher_aead_pkt *apkt,
260+
u8_t *nonce)
261+
{
262+
mbedtls_gcm_context *mtls_ctx = MTLS_GET_CTX(ctx, gcm);
263+
int ret;
264+
265+
ret = mbedtls_gcm_auth_decrypt(mtls_ctx, apkt->pkt->in_len, nonce,
266+
ctx->mode_params.gcm_info.nonce_len,
267+
apkt->ad, apkt->ad_len,
268+
apkt->tag,
269+
ctx->mode_params.gcm_info.tag_len,
270+
apkt->pkt->in_buf,
271+
apkt->pkt->out_buf);
272+
if (ret) {
273+
if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
274+
LOG_ERR("Message authentication failed");
275+
return -EFAULT;
276+
}
277+
278+
LOG_ERR("Could not decrypt/auth (%d)", ret);
279+
return -EINVAL;
280+
}
281+
282+
apkt->pkt->out_len = apkt->pkt->in_len;
283+
apkt->pkt->out_len += ctx->mode_params.gcm_info.tag_len;
284+
285+
return 0;
286+
}
287+
#endif /* CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED */
288+
218289
static int mtls_get_unused_session_index(void)
219290
{
220291
int i;
@@ -233,8 +304,11 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
233304
enum cipher_algo algo, enum cipher_mode mode,
234305
enum cipher_op op_type)
235306
{
236-
mbedtls_ccm_context *ccm_ctx;
237307
mbedtls_aes_context *aes_ctx;
308+
mbedtls_ccm_context *ccm_ctx;
309+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
310+
mbedtls_gcm_context *gcm_ctx;
311+
#endif
238312
int ctx_idx;
239313
int ret;
240314

@@ -250,6 +324,9 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
250324

251325
if (mode != CRYPTO_CIPHER_MODE_CCM &&
252326
mode != CRYPTO_CIPHER_MODE_CBC &&
327+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
328+
mode != CRYPTO_CIPHER_MODE_GCM &&
329+
#endif
253330
mode != CRYPTO_CIPHER_MODE_ECB) {
254331
LOG_ERR("Unsupported mode");
255332
return -EINVAL;
@@ -314,7 +391,7 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
314391
ret = mbedtls_ccm_setkey(ccm_ctx, MBEDTLS_CIPHER_ID_AES,
315392
ctx->key.bit_stream, ctx->keylen * 8U);
316393
if (ret) {
317-
LOG_ERR("Could not setup the key (%d)", ret);
394+
LOG_ERR("AES_CCM: failed at setkey (%d)", ret);
318395
mtls_sessions[ctx_idx].in_use = false;
319396

320397
return -EINVAL;
@@ -325,6 +402,29 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
325402
ctx->ops.ccm_crypt_hndlr = mtls_ccm_decrypt_auth;
326403
}
327404
break;
405+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
406+
case CRYPTO_CIPHER_MODE_GCM:
407+
gcm_ctx = &mtls_sessions[ctx_idx].mtls_gcm;
408+
mbedtls_gcm_init(gcm_ctx);
409+
ret = mbedtls_gcm_setkey(gcm_ctx, MBEDTLS_CIPHER_ID_AES,
410+
ctx->key.bit_stream, ctx->keylen * 8U);
411+
if (ret) {
412+
LOG_ERR("AES_GCM: failed at setkey (%d)", ret);
413+
mtls_sessions[ctx_idx].in_use = false;
414+
415+
return -EINVAL;
416+
}
417+
if (op_type == CRYPTO_CIPHER_OP_ENCRYPT) {
418+
ctx->ops.gcm_crypt_hndlr = mtls_gcm_encrypt_auth;
419+
} else {
420+
ctx->ops.gcm_crypt_hndlr = mtls_gcm_decrypt_auth;
421+
}
422+
break;
423+
#endif /* CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED */
424+
default:
425+
LOG_ERR("Unhandled mode");
426+
mtls_sessions[ctx_idx].in_use = false;
427+
return -EINVAL;
328428
}
329429

330430
mtls_sessions[ctx_idx].mode = mode;
@@ -340,6 +440,10 @@ static int mtls_session_free(struct device *dev, struct cipher_ctx *ctx)
340440

341441
if (mtls_session->mode == CRYPTO_CIPHER_MODE_CCM) {
342442
mbedtls_ccm_free(&mtls_session->mtls_ccm);
443+
#ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
444+
} else if (mtls_session->mode == CRYPTO_CIPHER_MODE_GCM) {
445+
mbedtls_gcm_free(&mtls_session->mtls_gcm);
446+
#endif
343447
} else {
344448
mbedtls_aes_free(&mtls_session->mtls_aes);
345449
}

0 commit comments

Comments
 (0)