cmac+hash: change parameter types

For consistency and safety, change the CMC and HSH functions to accept
signed lengths and handle negative values as errors. Also, change the
input data type to void * to not require casting in the caller.
This commit is contained in:
Miroslav Lichvar
2020-07-08 12:02:12 +02:00
parent de4ecc72d1
commit d93aa10bac
13 changed files with 64 additions and 53 deletions

View File

@@ -31,9 +31,9 @@
struct cmac_test {
const char *name;
const unsigned char key[MAX_KEY_LENGTH];
unsigned int key_length;
int key_length;
const unsigned char hash[MAX_HASH_LENGTH];
unsigned int hash_length;
int hash_length;
};
void
@@ -52,8 +52,7 @@ test_unit(void)
CMC_Algorithm algorithm;
CMC_Instance inst;
unsigned int length;
int i, j;
int i, j, length;
#ifndef HAVE_CMAC
TEST_REQUIRE(0);
@@ -68,7 +67,7 @@ test_unit(void)
DEBUG_LOG("testing %s", tests[i].name);
for (j = 0; j <= 128; j++) {
for (j = -1; j <= 128; j++) {
if (j == tests[i].key_length)
continue;
TEST_CHECK(!CMC_CreateInstance(algorithm, tests[i].key, j));
@@ -79,6 +78,9 @@ test_unit(void)
TEST_CHECK(!CMC_CreateInstance(0, tests[i].key, tests[i].key_length));
TEST_CHECK(CMC_Hash(inst, data, -1, hash, sizeof (hash)) == 0);
TEST_CHECK(CMC_Hash(inst, data, sizeof (data) - 1, hash, -1) == 0);
for (j = 0; j <= sizeof (hash); j++) {
memset(hash, 0, sizeof (hash));
length = CMC_Hash(inst, data, sizeof (data) - 1, hash, j);

View File

@@ -28,7 +28,7 @@
struct hash_test {
const char *name;
const unsigned char out[MAX_HASH_LENGTH];
unsigned int length;
int length;
};
void
@@ -71,8 +71,7 @@ test_unit(void)
};
HSH_Algorithm algorithm;
unsigned int length;
int i, j, hash_id;
int i, j, hash_id, length;
TEST_CHECK(HSH_INVALID == 0);
@@ -93,6 +92,10 @@ test_unit(void)
DEBUG_LOG("testing %s", tests[i].name);
TEST_CHECK(HSH_Hash(hash_id, data1, -1, NULL, 0, out, sizeof (out)) == 0);
TEST_CHECK(HSH_Hash(hash_id, data1, sizeof (data1) - 1, data2, -1, out, sizeof (out)) == 0);
TEST_CHECK(HSH_Hash(hash_id, data1, sizeof (data1) - 1, NULL, 0, out, -1) == 0);
for (j = 0; j <= sizeof (out); j++) {
TEST_CHECK(HSH_GetHashId(algorithm) == hash_id);
TEST_CHECK(HSH_GetHashId(0) < 0);