Skip to content

Commit

Permalink
remove memcmp
Browse files Browse the repository at this point in the history
  • Loading branch information
pct960 authored and arun-babu committed Mar 30, 2022
1 parent ee38ee3 commit d1d9a6f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
20 changes: 15 additions & 5 deletions freestyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ static void freestyle_ivsetup (
const u8* const iv,
const u32 counter)
{
x->input[COUNTER] = counter;
x->input[COUNTER] = counter;

x->input[IV0] = U8TO32_LITTLE(iv + 0);
x->input[IV1] = U8TO32_LITTLE(iv + 4);
x->input[IV2] = U8TO32_LITTLE(iv + 8);
x->input[IV0] = U8TO32_LITTLE(iv + 0);
x->input[IV1] = U8TO32_LITTLE(iv + 4);
x->input[IV2] = U8TO32_LITTLE(iv + 8);
}

static void freestyle_roundsetup (
Expand Down Expand Up @@ -889,6 +889,16 @@ void freestyle_hash_password_with_pepper (
);
}

static bool safe_bcmp (const u8 *a, const u8 *b, const size_t length)
{
u8 diff = 0;

for (size_t i = 0; i < length; ++i)
diff |= (a[i] ^ b[i]);

return diff;
}

bool freestyle_verify_password_hash (
const char* const password,
const u8* const salt,
Expand Down Expand Up @@ -961,5 +971,5 @@ bool freestyle_verify_password_hash (
&expected_hash
);

return (0 == memcmp(plaintext,salt,hash_len));
return (0 == safe_bcmp(plaintext,salt,hash_len));
}
12 changes: 11 additions & 1 deletion optimized/8-32/freestyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,16 @@ void freestyle_hash_password_with_pepper (
);
}

static bool safe_bcmp (const u8 *a, const u8 *b, const size_t length)
{
u8 diff = 0;

for (size_t i = 0; i < length; ++i)
diff |= (a[i] ^ b[i]);

return diff;
}

bool freestyle_verify_password_hash (
const char* const password,
const u8* const salt,
Expand Down Expand Up @@ -1216,5 +1226,5 @@ bool freestyle_verify_password_hash (
&expected_hash
);

return (0 == memcmp(plaintext,salt,hash_len));
return (0 == safe_bcmp(plaintext,salt,hash_len));
}
12 changes: 11 additions & 1 deletion optimized/merged/freestyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,16 @@ void freestyle_hash_password_with_pepper (
);
}

static bool safe_bcmp (const u8 *a, const u8 *b, const size_t length)
{
u8 diff = 0;

for (size_t i = 0; i < length; ++i)
diff |= (a[i] ^ b[i]);

return diff;
}

bool freestyle_verify_password_hash (
const char* const password,
const u8* const salt,
Expand Down Expand Up @@ -1201,5 +1211,5 @@ bool freestyle_verify_password_hash (
&expected_hash
);

return (0 == memcmp(plaintext,salt,hash_len));
return (0 == safe_bcmp(plaintext,salt,hash_len));
}
12 changes: 11 additions & 1 deletion side-channel-attack-resistance/freestyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,16 @@ void freestyle_hash_password_with_pepper (
);
}

static bool safe_bcmp (const u8 *a, const u8 *b, const size_t length)
{
u8 diff = 0;

for (size_t i = 0; i < length; ++i)
diff |= (a[i] ^ b[i]);

return diff;
}

bool freestyle_verify_password_hash (
const char* const password,
const u8* const salt,
Expand Down Expand Up @@ -997,5 +1007,5 @@ bool freestyle_verify_password_hash (
&expected_hash
);

return (0 == memcmp(plaintext,salt,hash_len));
return (0 == safe_bcmp(plaintext,salt,hash_len));
}

0 comments on commit d1d9a6f

Please sign in to comment.