diff --git a/libfreefare/mifare_desfire_authenticate.c b/libfreefare/mifare_desfire_authenticate.c index b851818..91d3dca 100644 --- a/libfreefare/mifare_desfire_authenticate.c +++ b/libfreefare/mifare_desfire_authenticate.c @@ -236,7 +236,7 @@ key_macing_length (const MifareDESFireKey key) size_t padded_data_length (const size_t nbytes, const size_t block_size) { - if (nbytes % block_size) + if ((!nbytes) || (nbytes % block_size)) return ((nbytes / block_size) + 1) * block_size; else return nbytes; diff --git a/test/test_mifare_desfire_des.c b/test/test_mifare_desfire_des.c index 54b63f8..a4b9404 100644 --- a/test/test_mifare_desfire_des.c +++ b/test/test_mifare_desfire_des.c @@ -71,3 +71,22 @@ test_mifare_desfire_des_send (void) mifare_desfire_key_free (key); } + +void +test_mifare_desfire_padded_data_length (void) +{ + size_t res; + + res = padded_data_length (0, 8); + cut_assert_equal_int (res, 8, cut_message ("Invalid size")); + res = padded_data_length (1, 8); + cut_assert_equal_int (res, 8, cut_message ("Invalid size")); + res = padded_data_length (8, 8); + cut_assert_equal_int (res, 8, cut_message ("Invalid size")); + res = padded_data_length (9, 8); + cut_assert_equal_int (res, 16, cut_message ("Invalid size")); + res = padded_data_length (0, 16); + cut_assert_equal_int (res, 16, cut_message ("Invalid size")); + res = padded_data_length (33, 16); + cut_assert_equal_int (res, 48, cut_message ("Invalid size")); +}