From c9680fd7c7c4584d5659324173b50f10435780ed Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Fri, 17 Dec 2010 22:53:40 +0000 Subject: [PATCH] Fix key_macing_length() return value when nbytes is 0. --- libfreefare/mifare_desfire_authenticate.c | 2 +- test/test_mifare_desfire_des.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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")); +}