[Cryptography] Implementing constant-time string comparison

Dave Horsfall dave at horsfall.org
Wed Jun 18 11:37:17 EDT 2014


> static int vn(const u8 *x,const u8 *y,int n)
> {
>  u32 i,d = 0;
>  for (i = 0,i < n,++i) d |= x[i]^y[i];
>  return (1 & ((d - 1) >> 8)) - 1;
> }

I nominate this fragment for the IOCCC.  You'll want to fix the obvious 
syntax error in the "for" statement, though (unless your compiler is more 
forgiving than mine).

-- Dave


More information about the cryptography mailing list