21
21
#endif /* CONFIG_MBEDTLS_CFG_FILE */
22
22
23
23
#include <mbedtls/ccm.h>
24
+ #ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
25
+ #include <mbedtls/gcm.h>
26
+ #endif
24
27
#include <mbedtls/aes.h>
25
28
26
29
#define MTLS_SUPPORT (CAP_RAW_KEY | CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS | \
@@ -33,6 +36,9 @@ LOG_MODULE_REGISTER(mbedtls);
33
36
struct mtls_shim_session {
34
37
union {
35
38
mbedtls_ccm_context mtls_ccm ;
39
+ #ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
40
+ mbedtls_gcm_context mtls_gcm ;
41
+ #endif
36
42
mbedtls_aes_context mtls_aes ;
37
43
};
38
44
bool in_use ;
@@ -203,7 +209,12 @@ static int mtls_ccm_decrypt_auth(struct cipher_ctx *ctx,
203
209
apkt -> pkt -> out_buf , apkt -> tag ,
204
210
ctx -> mode_params .ccm_info .tag_len );
205
211
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 );
207
218
208
219
/*ToDo: try to return relevant code depending on ret? */
209
220
return - EINVAL ;
@@ -215,6 +226,66 @@ static int mtls_ccm_decrypt_auth(struct cipher_ctx *ctx,
215
226
return 0 ;
216
227
}
217
228
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
+
218
289
static int mtls_get_unused_session_index (void )
219
290
{
220
291
int i ;
@@ -233,8 +304,11 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
233
304
enum cipher_algo algo , enum cipher_mode mode ,
234
305
enum cipher_op op_type )
235
306
{
236
- mbedtls_ccm_context * ccm_ctx ;
237
307
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
238
312
int ctx_idx ;
239
313
int ret ;
240
314
@@ -250,6 +324,9 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
250
324
251
325
if (mode != CRYPTO_CIPHER_MODE_CCM &&
252
326
mode != CRYPTO_CIPHER_MODE_CBC &&
327
+ #ifdef CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED
328
+ mode != CRYPTO_CIPHER_MODE_GCM &&
329
+ #endif
253
330
mode != CRYPTO_CIPHER_MODE_ECB ) {
254
331
LOG_ERR ("Unsupported mode" );
255
332
return - EINVAL ;
@@ -314,7 +391,7 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
314
391
ret = mbedtls_ccm_setkey (ccm_ctx , MBEDTLS_CIPHER_ID_AES ,
315
392
ctx -> key .bit_stream , ctx -> keylen * 8U );
316
393
if (ret ) {
317
- LOG_ERR ("Could not setup the key (%d)" , ret );
394
+ LOG_ERR ("AES_CCM: failed at setkey (%d)" , ret );
318
395
mtls_sessions [ctx_idx ].in_use = false;
319
396
320
397
return - EINVAL ;
@@ -325,6 +402,29 @@ static int mtls_session_setup(struct device *dev, struct cipher_ctx *ctx,
325
402
ctx -> ops .ccm_crypt_hndlr = mtls_ccm_decrypt_auth ;
326
403
}
327
404
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 ;
328
428
}
329
429
330
430
mtls_sessions [ctx_idx ].mode = mode ;
@@ -340,6 +440,10 @@ static int mtls_session_free(struct device *dev, struct cipher_ctx *ctx)
340
440
341
441
if (mtls_session -> mode == CRYPTO_CIPHER_MODE_CCM ) {
342
442
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
343
447
} else {
344
448
mbedtls_aes_free (& mtls_session -> mtls_aes );
345
449
}
0 commit comments