Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Timing attack
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Algorithm === The following [[C (programming language)|C]] code demonstrates a typical insecure string comparison which stops testing as soon as a character doesn't match. For example, when comparing "ABCDE" with "ABxDE" it will return after 3 loop iterations: <syntaxhighlight lang="c"> bool insecureStringCompare(const void *a, const void *b, size_t length) { const char *ca = a, *cb = b; for (size_t i = 0; i < length; i++) if (ca[i] != cb[i]) return false; return true; } </syntaxhighlight> By comparison, the following version runs in constant-time by testing all characters and using a [[bitwise operation]] to accumulate the result: <syntaxhighlight lang="c"> bool constantTimeStringCompare(const void *a, const void *b, size_t length) { const char *ca = a, *cb = b; bool result = true; for (size_t i = 0; i < length; i++) result &= ca[i] == cb[i]; return result; } </syntaxhighlight> In the world of C library functions, the first function is analogous to {{code|memcmp()}}, while the latter is analogous to NetBSD's {{code|consttime_memequal()}} or<ref>{{Cite web|url=https://www.freebsd.org/cgi/man.cgi?query=consttime_memequal&manpath=NetBSD+7.0|title = Consttime_memequal}}</ref> OpenBSD's {{code|timingsafe_bcmp()}} and {{code|timingsafe_memcmp}}. On other systems, the comparison function from cryptographic libraries like [[OpenSSL]] and [[libsodium]] can be used.<!-- There should be a big flashy "don't roll your own crypto" paragraph here, but I have no idea how to phrase it. Whatever. -->
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)