/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file mem_neq.c12Compare two blocks of memory for inequality in constant time.13Steffen Jaeckel14*/1516/**17Compare two blocks of memory for inequality in constant time.1819The usage is similar to that of standard memcmp, but you can only test20if the memory is equal or not - you can not determine by how much the21first different byte differs.2223This function shall be used to compare results of cryptographic24operations where inequality means most likely usage of a wrong key.25The execution time has therefore to be constant as otherwise26timing attacks could be possible.2728@param a The first memory region29@param b The second memory region30@param len The length of the area to compare (octets)3132@return 0 when a and b are equal for len bytes, 1 they are not equal.33*/34int mem_neq(const void *a, const void *b, size_t len)35{36unsigned char ret = 0;37const unsigned char* pa;38const unsigned char* pb;3940LTC_ARGCHK(a != NULL);41LTC_ARGCHK(b != NULL);4243pa = a;44pb = b;4546while (len-- > 0) {47ret |= *pa ^ *pb;48++pa;49++pb;50}5152ret |= ret >> 4;53ret |= ret >> 2;54ret |= ret >> 1;55ret &= 1;5657return ret;58}596061