@@ -462,55 +462,72 @@ static int mtls_session_free(const struct device *dev, struct cipher_ctx *ctx)
462
462
return 0 ;
463
463
}
464
464
465
- static int mtls_sha256_compute (struct hash_ctx * ctx , struct hash_pkt * pkt )
465
+ static int mtls_sha256_compute (struct hash_ctx * ctx , struct hash_pkt * pkt ,
466
+ bool finish )
466
467
{
467
468
int ret ;
468
469
mbedtls_sha256_context * sha256_ctx = MTLS_GET_CTX (ctx , sha256 );
469
470
470
471
471
- ret = mbedtls_sha256_starts (sha256_ctx ,
472
- MTLS_GET_ALGO (ctx ) == CRYPTO_HASH_ALGO_SHA224 );
473
- if (ret != 0 ) {
474
- LOG_ERR ("Could not compute the hash" );
475
- return - EINVAL ;
472
+ if (!ctx -> started ) {
473
+ ret = mbedtls_sha256_starts (sha256_ctx ,
474
+ MTLS_GET_ALGO (ctx ) == CRYPTO_HASH_ALGO_SHA224 );
475
+ if (ret != 0 ) {
476
+ LOG_ERR ("Could not compute the hash" );
477
+ return - EINVAL ;
478
+ }
479
+ ctx -> started = true;
476
480
}
477
481
478
482
ret = mbedtls_sha256_update (sha256_ctx , pkt -> in_buf , pkt -> in_len );
479
483
if (ret != 0 ) {
480
- LOG_ERR ("Could not compute the hash" );
484
+ LOG_ERR ("Could not update the hash" );
485
+ ctx -> started = false;
481
486
return - EINVAL ;
482
487
}
483
488
484
- ret = mbedtls_sha256_finish (sha256_ctx , pkt -> out_buf );
485
- if (ret != 0 ) {
486
- LOG_ERR ("Could not compute the hash" );
487
- return - EINVAL ;
489
+ if (finish ) {
490
+ ctx -> started = false;
491
+ ret = mbedtls_sha256_finish (sha256_ctx , pkt -> out_buf );
492
+ if (ret != 0 ) {
493
+ LOG_ERR ("Could not compute the hash" );
494
+ return - EINVAL ;
495
+ }
488
496
}
489
497
490
498
return 0 ;
491
499
}
492
500
493
- static int mtls_sha512_compute (struct hash_ctx * ctx , struct hash_pkt * pkt )
501
+ static int mtls_sha512_compute (struct hash_ctx * ctx , struct hash_pkt * pkt ,
502
+ bool finish )
494
503
{
495
504
int ret ;
496
505
mbedtls_sha512_context * sha512_ctx = MTLS_GET_CTX (ctx , sha512 );
497
506
498
- ret = mbedtls_sha512_starts (sha512_ctx ,
499
- MTLS_GET_ALGO (ctx ) == CRYPTO_HASH_ALGO_SHA384 );
500
- if (ret != 0 ) {
501
- LOG_ERR ("Could not compute the hash" );
502
- return - EINVAL ;
507
+ if (!ctx -> started ) {
508
+ ret = mbedtls_sha512_starts (sha512_ctx ,
509
+ MTLS_GET_ALGO (ctx ) == CRYPTO_HASH_ALGO_SHA384 );
510
+ if (ret != 0 ) {
511
+ LOG_ERR ("Could not compute the hash" );
512
+ return - EINVAL ;
513
+ }
514
+ ctx -> started = true;
503
515
}
516
+
504
517
ret = mbedtls_sha512_update (sha512_ctx , pkt -> in_buf , pkt -> in_len );
505
518
if (ret != 0 ) {
506
- LOG_ERR ("Could not compute the hash" );
519
+ LOG_ERR ("Could not update the hash" );
520
+ ctx -> started = false;
507
521
return - EINVAL ;
508
522
}
509
523
510
- ret = mbedtls_sha512_finish (sha512_ctx , pkt -> out_buf );
511
- if (ret != 0 ) {
512
- LOG_ERR ("Could not compute the hash" );
513
- return - EINVAL ;
524
+ if (finish ) {
525
+ ctx -> started = false;
526
+ ret = mbedtls_sha512_finish (sha512_ctx , pkt -> out_buf );
527
+ if (ret != 0 ) {
528
+ LOG_ERR ("Could not compute the hash" );
529
+ return - EINVAL ;
530
+ }
514
531
}
515
532
516
533
return 0 ;
@@ -543,6 +560,7 @@ static int mtls_hash_session_setup(const struct device *dev,
543
560
544
561
mtls_sessions [ctx_idx ].algo = algo ;
545
562
ctx -> drv_sessn_state = & mtls_sessions [ctx_idx ];
563
+ ctx -> started = false;
546
564
547
565
if ((algo == CRYPTO_HASH_ALGO_SHA224 ) ||
548
566
(algo == CRYPTO_HASH_ALGO_SHA256 )) {
0 commit comments