@@ -235,7 +235,6 @@ int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
235235 ctx -> cmac_ctx = cmac_ctx ;
236236
237237 mbedtls_zeroize ( cmac_ctx -> state , sizeof ( cmac_ctx -> state ) );
238- cmac_ctx -> padding_flag = 1 ;
239238
240239 return 0 ;
241240}
@@ -256,8 +255,8 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
256255 block_size = ctx -> cipher_info -> block_size ;
257256 state = ctx -> cmac_ctx -> state ;
258257
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? */
261260 if ( cmac_ctx -> unprocessed_len > 0 &&
262261 ilen > block_size - cmac_ctx -> unprocessed_len )
263262 {
@@ -273,9 +272,8 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
273272 goto exit ;
274273 }
275274
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 ;
279277 cmac_ctx -> unprocessed_len = 0 ;
280278 }
281279
@@ -293,20 +291,15 @@ int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
293291
294292 ilen -= block_size ;
295293 input += block_size ;
296-
297- cmac_ctx -> padding_flag = 0 ;
298294 }
299295
300296 /* If there is data left over that wasn't aligned to a block */
301297 if ( ilen > 0 )
302298 {
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 ;
310303 }
311304
312305exit :
@@ -339,7 +332,7 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
339332 last_block = cmac_ctx -> unprocessed_block ;
340333
341334 /* Calculate last block */
342- if ( cmac_ctx -> padding_flag )
335+ if ( cmac_ctx -> unprocessed_len < block_size )
343336 {
344337 cmac_pad ( M_last , block_size , last_block , cmac_ctx -> unprocessed_len );
345338 cmac_xor_block ( M_last , M_last , K2 , block_size );
@@ -366,7 +359,6 @@ int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
366359 mbedtls_zeroize ( K1 , sizeof ( K1 ) );
367360 mbedtls_zeroize ( K2 , sizeof ( K2 ) );
368361
369- cmac_ctx -> padding_flag = 1 ;
370362 cmac_ctx -> unprocessed_len = 0 ;
371363 mbedtls_zeroize ( cmac_ctx -> unprocessed_block ,
372364 sizeof ( cmac_ctx -> unprocessed_block ) );
@@ -390,7 +382,6 @@ int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
390382 sizeof ( cmac_ctx -> unprocessed_block ) );
391383 mbedtls_zeroize ( cmac_ctx -> state ,
392384 sizeof ( cmac_ctx -> state ) );
393- cmac_ctx -> padding_flag = 1 ;
394385
395386 return ( 0 );
396387}
@@ -746,19 +737,19 @@ static int cmac_test_subkeys( int verbose,
746737 return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
747738 }
748739
749- mbedtls_cipher_init ( & ctx );
750-
751740 for ( i = 0 ; i < num_tests ; i ++ )
752741 {
753742 if ( verbose != 0 )
754743 mbedtls_printf ( " %s CMAC subkey #%u: " , testname , i + 1 );
755744
745+ mbedtls_cipher_init ( & ctx );
746+
756747 if ( ( ret = mbedtls_cipher_setup ( & ctx , cipher_info ) ) != 0 )
757748 {
758749 if ( verbose != 0 )
759750 mbedtls_printf ( "test execution failed\n" );
760751
761- goto exit ;
752+ goto cleanup ;
762753 }
763754
764755 if ( ( ret = mbedtls_cipher_setkey ( & ctx , key , keybits ,
@@ -767,32 +758,39 @@ static int cmac_test_subkeys( int verbose,
767758 if ( verbose != 0 )
768759 mbedtls_printf ( "test execution failed\n" );
769760
770- goto exit ;
761+ goto cleanup ;
771762 }
772763
773764 ret = cmac_generate_subkeys ( & ctx , K1 , K2 );
774765 if ( ret != 0 )
775766 {
776767 if ( verbose != 0 )
777768 mbedtls_printf ( "failed\n" );
778- goto exit ;
769+
770+ goto cleanup ;
779771 }
780772
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 )
783775 {
784776 if ( verbose != 0 )
785777 mbedtls_printf ( "failed\n" );
786- goto exit ;
778+
779+ goto cleanup ;
787780 }
788781
789782 if ( verbose != 0 )
790783 mbedtls_printf ( "passed\n" );
784+
785+ mbedtls_cipher_free ( & ctx );
791786 }
792787
793- exit :
788+ goto exit ;
789+
790+ cleanup :
794791 mbedtls_cipher_free ( & ctx );
795792
793+ exit :
796794 return ( ret );
797795}
798796
@@ -889,7 +887,7 @@ int mbedtls_cmac_self_test( int verbose )
889887 (const unsigned char * )aes_128_subkeys ,
890888 MBEDTLS_CIPHER_AES_128_ECB ,
891889 MBEDTLS_AES_BLOCK_SIZE ,
892- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
890+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
893891 {
894892 return ( ret );
895893 }
@@ -903,7 +901,7 @@ int mbedtls_cmac_self_test( int verbose )
903901 (const unsigned char * )aes_128_expected_result ,
904902 MBEDTLS_CIPHER_AES_128_ECB ,
905903 MBEDTLS_AES_BLOCK_SIZE ,
906- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
904+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
907905 {
908906 return ( ret );
909907 }
@@ -916,7 +914,7 @@ int mbedtls_cmac_self_test( int verbose )
916914 (const unsigned char * )aes_192_subkeys ,
917915 MBEDTLS_CIPHER_AES_192_ECB ,
918916 MBEDTLS_AES_BLOCK_SIZE ,
919- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
917+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
920918 {
921919 return ( ret );
922920 }
@@ -930,7 +928,7 @@ int mbedtls_cmac_self_test( int verbose )
930928 (const unsigned char * )aes_192_expected_result ,
931929 MBEDTLS_CIPHER_AES_192_ECB ,
932930 MBEDTLS_AES_BLOCK_SIZE ,
933- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
931+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
934932 {
935933 return ( ret );
936934 }
@@ -943,7 +941,7 @@ int mbedtls_cmac_self_test( int verbose )
943941 (const unsigned char * )aes_256_subkeys ,
944942 MBEDTLS_CIPHER_AES_256_ECB ,
945943 MBEDTLS_AES_BLOCK_SIZE ,
946- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
944+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
947945 {
948946 return ( ret );
949947 }
@@ -957,7 +955,7 @@ int mbedtls_cmac_self_test( int verbose )
957955 (const unsigned char * )aes_256_expected_result ,
958956 MBEDTLS_CIPHER_AES_256_ECB ,
959957 MBEDTLS_AES_BLOCK_SIZE ,
960- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
958+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
961959 {
962960 return ( ret );
963961 }
@@ -972,7 +970,7 @@ int mbedtls_cmac_self_test( int verbose )
972970 (const unsigned char * )des3_2key_subkeys ,
973971 MBEDTLS_CIPHER_DES_EDE3_ECB ,
974972 MBEDTLS_DES3_BLOCK_SIZE ,
975- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
973+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
976974 {
977975 return ( ret );
978976 }
@@ -986,7 +984,7 @@ int mbedtls_cmac_self_test( int verbose )
986984 (const unsigned char * )des3_2key_expected_result ,
987985 MBEDTLS_CIPHER_DES_EDE3_ECB ,
988986 MBEDTLS_DES3_BLOCK_SIZE ,
989- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
987+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
990988 {
991989 return ( ret );
992990 }
@@ -999,7 +997,7 @@ int mbedtls_cmac_self_test( int verbose )
999997 (const unsigned char * )des3_3key_subkeys ,
1000998 MBEDTLS_CIPHER_DES_EDE3_ECB ,
1001999 MBEDTLS_DES3_BLOCK_SIZE ,
1002- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
1000+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
10031001 {
10041002 return ( ret );
10051003 }
@@ -1013,14 +1011,14 @@ int mbedtls_cmac_self_test( int verbose )
10131011 (const unsigned char * )des3_3key_expected_result ,
10141012 MBEDTLS_CIPHER_DES_EDE3_ECB ,
10151013 MBEDTLS_DES3_BLOCK_SIZE ,
1016- NB_CMAC_TESTS_PER_KEY ) != 0 ) )
1014+ NB_CMAC_TESTS_PER_KEY ) ) != 0 )
10171015 {
10181016 return ( ret );
10191017 }
10201018#endif /* MBEDTLS_DES_C */
10211019
10221020#if defined(MBEDTLS_AES_C )
1023- if ( ( ret = test_aes128_cmac_prf ( verbose ) != 0 ) )
1021+ if ( ( ret = test_aes128_cmac_prf ( verbose ) ) != 0 )
10241022 return ( ret );
10251023#endif /* MBEDTLS_AES_C */
10261024
0 commit comments