From 9c95cd27f9f910ef7423838d4dab0bea7e2d55d0 Mon Sep 17 00:00:00 2001 From: Love Hornquist Astrand Date: Tue, 18 Aug 2009 13:10:23 +0200 Subject: [PATCH] doxygen, make constant time for real. --- lib/roken/ct.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/roken/ct.c b/lib/roken/ct.c index 68f9aacd0..5633dcef8 100644 --- a/lib/roken/ct.c +++ b/lib/roken/ct.c @@ -35,6 +35,23 @@ #include +/** + * Constant time compare to memory regions. The reason for making it + * constant time is to make sure that timeing information leak from + * where in the function the diffrence is. + * + * ct_memcmp() can't be used to order memory regions like memcmp(), + * for example, use ct_memcmp() with qsort(). + * + * @param p1 memory region 1 to compare + * @param p2 memory region 2 to compare + * @param len length of memory + * + * @return 0 when the memory regions are equal, non zero if not + * + * @ingroup roken + */ + int ct_memcmp(const void *p1, const void *p2, size_t len) { @@ -43,7 +60,6 @@ ct_memcmp(const void *p1, const void *p2, size_t len) int r = 0; for (i = 0; i < len; i++) - if (s1[i] != s2[i] && r == 0) - r = s1[i] - s2[i]; - return r; + r |= (s1[i] ^ s2[i]); + return !!r; }