Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/misc/mem_neq.c
5971 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
#include "tomcrypt.h"
10
11
/**
12
@file mem_neq.c
13
Compare two blocks of memory for inequality in constant time.
14
Steffen Jaeckel
15
*/
16
17
/**
18
Compare two blocks of memory for inequality in constant time.
19
20
The usage is similar to that of standard memcmp, but you can only test
21
if the memory is equal or not - you can not determine by how much the
22
first different byte differs.
23
24
This function shall be used to compare results of cryptographic
25
operations where inequality means most likely usage of a wrong key.
26
The execution time has therefore to be constant as otherwise
27
timing attacks could be possible.
28
29
@param a The first memory region
30
@param b The second memory region
31
@param len The length of the area to compare (octets)
32
33
@return 0 when a and b are equal for len bytes, 1 they are not equal.
34
*/
35
int mem_neq(const void *a, const void *b, size_t len)
36
{
37
unsigned char ret = 0;
38
const unsigned char* pa;
39
const unsigned char* pb;
40
41
LTC_ARGCHK(a != NULL);
42
LTC_ARGCHK(b != NULL);
43
44
pa = a;
45
pb = b;
46
47
while (len-- > 0) {
48
ret |= *pa ^ *pb;
49
++pa;
50
++pb;
51
}
52
53
ret |= ret >> 4;
54
ret |= ret >> 2;
55
ret |= ret >> 1;
56
ret &= 1;
57
58
return ret;
59
}
60
61