@@ -235,7 +235,6 @@ int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
235
235
ctx -> cmac_ctx = cmac_ctx ;
236
236
237
237
mbedtls_zeroize ( cmac_ctx -> state , sizeof ( cmac_ctx -> state ) );
238
- cmac_ctx -> padding_flag = 1 ;
239
238
240
239
return 0 ;
241
240
}
@@ -256,8 +255,8 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
256
255
block_size = ctx -> cipher_info -> block_size ;
257
256
state = ctx -> cmac_ctx -> state ;
258
257
259
- /* Is their data still to process from the last call, that's equal to
260
- * or greater than a block? */
258
+ /* Is there data still to process from the last call, that's greater in
259
+ * size than a block? */
261
260
if ( cmac_ctx -> unprocessed_len > 0 &&
262
261
ilen > block_size - cmac_ctx -> unprocessed_len )
263
262
{
@@ -273,9 +272,8 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
273
272
goto exit ;
274
273
}
275
274
276
- ilen -= block_size ;
277
- input += cmac_ctx -> unprocessed_len ;
278
-
275
+ input += block_size - cmac_ctx -> unprocessed_len ;
276
+ ilen -= block_size - cmac_ctx -> unprocessed_len ;
279
277
cmac_ctx -> unprocessed_len = 0 ;
280
278
}
281
279
@@ -293,20 +291,15 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
293
291
294
292
ilen -= block_size ;
295
293
input += block_size ;
296
-
297
- cmac_ctx -> padding_flag = 0 ;
298
294
}
299
295
300
296
/* If there is data left over that wasn't aligned to a block */
301
297
if ( ilen > 0 )
302
298
{
303
- memcpy ( & cmac_ctx -> unprocessed_block , input , ilen );
304
- cmac_ctx -> unprocessed_len = ilen ;
305
-
306
- if ( ilen % block_size > 0 )
307
- cmac_ctx -> padding_flag = 1 ;
308
- else
309
- cmac_ctx -> padding_flag = 0 ;
299
+ memcpy ( & cmac_ctx -> unprocessed_block [cmac_ctx -> unprocessed_len ],
300
+ input ,
301
+ ilen );
302
+ cmac_ctx -> unprocessed_len += ilen ;
310
303
}
311
304
312
305
exit :
@@ -339,7 +332,7 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
339
332
last_block = cmac_ctx -> unprocessed_block ;
340
333
341
334
/* Calculate last block */
342
- if ( cmac_ctx -> padding_flag )
335
+ if ( cmac_ctx -> unprocessed_len < block_size )
343
336
{
344
337
cmac_pad ( M_last , block_size , last_block , cmac_ctx -> unprocessed_len );
345
338
cmac_xor_block ( M_last , M_last , K2 , block_size );
@@ -366,7 +359,6 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
366
359
mbedtls_zeroize ( K1 , sizeof ( K1 ) );
367
360
mbedtls_zeroize ( K2 , sizeof ( K2 ) );
368
361
369
- cmac_ctx -> padding_flag = 1 ;
370
362
cmac_ctx -> unprocessed_len = 0 ;
371
363
mbedtls_zeroize ( cmac_ctx -> unprocessed_block ,
372
364
sizeof ( cmac_ctx -> unprocessed_block ) );
@@ -390,7 +382,6 @@ int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
390
382
sizeof ( cmac_ctx -> unprocessed_block ) );
391
383
mbedtls_zeroize ( cmac_ctx -> state ,
392
384
sizeof ( cmac_ctx -> state ) );
393
- cmac_ctx -> padding_flag = 1 ;
394
385
395
386
return ( 0 );
396
387
}
@@ -746,19 +737,19 @@ static int cmac_test_subkeys( int verbose,
746
737
return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
747
738
}
748
739
749
- mbedtls_cipher_init ( & ctx );
750
-
751
740
for ( i = 0 ; i < num_tests ; i ++ )
752
741
{
753
742
if ( verbose != 0 )
754
743
mbedtls_printf ( " %s CMAC subkey #%u: " , testname , i + 1 );
755
744
745
+ mbedtls_cipher_init ( & ctx );
746
+
756
747
if ( ( ret = mbedtls_cipher_setup ( & ctx , cipher_info ) ) != 0 )
757
748
{
758
749
if ( verbose != 0 )
759
750
mbedtls_printf ( "test execution failed\n" );
760
751
761
- goto exit ;
752
+ goto cleanup ;
762
753
}
763
754
764
755
if ( ( ret = mbedtls_cipher_setkey ( & ctx , key , keybits ,
@@ -767,32 +758,39 @@ static int cmac_test_subkeys( int verbose,
767
758
if ( verbose != 0 )
768
759
mbedtls_printf ( "test execution failed\n" );
769
760
770
- goto exit ;
761
+ goto cleanup ;
771
762
}
772
763
773
764
ret = cmac_generate_subkeys ( & ctx , K1 , K2 );
774
765
if ( ret != 0 )
775
766
{
776
767
if ( verbose != 0 )
777
768
mbedtls_printf ( "failed\n" );
778
- goto exit ;
769
+
770
+ goto cleanup ;
779
771
}
780
772
781
- if ( ( ret = memcmp ( K1 , subkeys , block_size ) != 0 ) ||
782
- ( ret = memcmp ( K2 , & subkeys [block_size ], block_size ) != 0 ) )
773
+ if ( ( ret = memcmp ( K1 , subkeys , block_size ) ) != 0 ||
774
+ ( ret = memcmp ( K2 , & subkeys [block_size ], block_size ) ) != 0 )
783
775
{
784
776
if ( verbose != 0 )
785
777
mbedtls_printf ( "failed\n" );
786
- goto exit ;
778
+
779
+ goto cleanup ;
787
780
}
788
781
789
782
if ( verbose != 0 )
790
783
mbedtls_printf ( "passed\n" );
784
+
785
+ mbedtls_cipher_free ( & ctx );
791
786
}
792
787
793
- exit :
788
+ goto exit ;
789
+
790
+ cleanup :
794
791
mbedtls_cipher_free ( & ctx );
795
792
793
+ exit :
796
794
return ( ret );
797
795
}
798
796
@@ -889,7 +887,7 @@ int mbedtls_cmac_self_test( int verbose )
889
887
(const unsigned char * )aes_128_subkeys ,
890
888
MBEDTLS_CIPHER_AES_128_ECB ,
891
889
MBEDTLS_AES_BLOCK_SIZE ,
892
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
890
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
893
891
{
894
892
return ( ret );
895
893
}
@@ -903,7 +901,7 @@ int mbedtls_cmac_self_test( int verbose )
903
901
(const unsigned char * )aes_128_expected_result ,
904
902
MBEDTLS_CIPHER_AES_128_ECB ,
905
903
MBEDTLS_AES_BLOCK_SIZE ,
906
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
904
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
907
905
{
908
906
return ( ret );
909
907
}
@@ -916,7 +914,7 @@ int mbedtls_cmac_self_test( int verbose )
916
914
(const unsigned char * )aes_192_subkeys ,
917
915
MBEDTLS_CIPHER_AES_192_ECB ,
918
916
MBEDTLS_AES_BLOCK_SIZE ,
919
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
917
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
920
918
{
921
919
return ( ret );
922
920
}
@@ -930,7 +928,7 @@ int mbedtls_cmac_self_test( int verbose )
930
928
(const unsigned char * )aes_192_expected_result ,
931
929
MBEDTLS_CIPHER_AES_192_ECB ,
932
930
MBEDTLS_AES_BLOCK_SIZE ,
933
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
931
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
934
932
{
935
933
return ( ret );
936
934
}
@@ -943,7 +941,7 @@ int mbedtls_cmac_self_test( int verbose )
943
941
(const unsigned char * )aes_256_subkeys ,
944
942
MBEDTLS_CIPHER_AES_256_ECB ,
945
943
MBEDTLS_AES_BLOCK_SIZE ,
946
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
944
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
947
945
{
948
946
return ( ret );
949
947
}
@@ -957,7 +955,7 @@ int mbedtls_cmac_self_test( int verbose )
957
955
(const unsigned char * )aes_256_expected_result ,
958
956
MBEDTLS_CIPHER_AES_256_ECB ,
959
957
MBEDTLS_AES_BLOCK_SIZE ,
960
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
958
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
961
959
{
962
960
return ( ret );
963
961
}
@@ -972,7 +970,7 @@ int mbedtls_cmac_self_test( int verbose )
972
970
(const unsigned char * )des3_2key_subkeys ,
973
971
MBEDTLS_CIPHER_DES_EDE3_ECB ,
974
972
MBEDTLS_DES3_BLOCK_SIZE ,
975
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
973
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
976
974
{
977
975
return ( ret );
978
976
}
@@ -986,7 +984,7 @@ int mbedtls_cmac_self_test( int verbose )
986
984
(const unsigned char * )des3_2key_expected_result ,
987
985
MBEDTLS_CIPHER_DES_EDE3_ECB ,
988
986
MBEDTLS_DES3_BLOCK_SIZE ,
989
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
987
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
990
988
{
991
989
return ( ret );
992
990
}
@@ -999,7 +997,7 @@ int mbedtls_cmac_self_test( int verbose )
999
997
(const unsigned char * )des3_3key_subkeys ,
1000
998
MBEDTLS_CIPHER_DES_EDE3_ECB ,
1001
999
MBEDTLS_DES3_BLOCK_SIZE ,
1002
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
1000
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
1003
1001
{
1004
1002
return ( ret );
1005
1003
}
@@ -1013,14 +1011,14 @@ int mbedtls_cmac_self_test( int verbose )
1013
1011
(const unsigned char * )des3_3key_expected_result ,
1014
1012
MBEDTLS_CIPHER_DES_EDE3_ECB ,
1015
1013
MBEDTLS_DES3_BLOCK_SIZE ,
1016
- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
1014
+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
1017
1015
{
1018
1016
return ( ret );
1019
1017
}
1020
1018
#endif /* MBEDTLS_DES_C */
1021
1019
1022
1020
#if defined(MBEDTLS_AES_C )
1023
- if ( ( ret = test_aes128_cmac_prf ( verbose ) != 0 ) )
1021
+ if ( ( ret = test_aes128_cmac_prf ( verbose ) ) != 0 )
1024
1022
return ( ret );
1025
1023
#endif /* MBEDTLS_AES_C */
1026
1024
0 commit comments