File:  [DVB] / dietlibc / lib / consttime_memequal.c
Revision 1.1: download - view: text, annotated - select for diffs
Mon Oct 21 12:30:54 2019 UTC (4 years, 7 months ago) by leitner
Branches: MAIN
CVS tags: HEAD
  add consttime_memequal and explicit_memset from NetBSD

#include <string.h>

/* return 1 if the regions match */
/* always compare all bytes to avoid timing side channels */
int consttime_memequal(void* a,void* b,size_t n) {
  size_t i;
  const unsigned char* x = a;
  const unsigned char* y = b;
  int r;
  for (i=r=0; i<n; ++i)
    r |= x[i] ^ y[i];	/* x[i]^y[i] is 0 iff x[i]==y[i] */
  return r==0;
}

#ifdef UNITTEST
#include <assert.h>
int main() {
  assert(consttime_memequal("fnord","fnord",5)==1);
  assert(consttime_memequal("fnord","fnorD",5)==0);
}
#endif

LinuxTV legacy CVS <linuxtv.org/cvs>