MCPcopy Create free account
hub / github.com/F-Stack/f-stack / time_independent_strcmp

Function time_independent_strcmp

app/redis-6.2.6/src/acl.c:125–156  ·  view source on GitHub ↗

Return zero if strings are the same, non-zero if they are not. * The comparison is performed in a way that prevents an attacker to obtain * information about the nature of the strings just monitoring the execution * time of the function. * * Note that limiting the comparison length to strings up to 512 bytes we * can avoid leaking any information about the password length and any * possible

Source from the content-addressed store, hash-verified

123 * possible branch misprediction related leak.
124 */
125int time_independent_strcmp(char *a, char *b) {
126 char bufa[CONFIG_AUTHPASS_MAX_LEN], bufb[CONFIG_AUTHPASS_MAX_LEN];
127 /* The above two strlen perform len(a) + len(b) operations where either
128 * a or b are fixed (our password) length, and the difference is only
129 * relative to the length of the user provided string, so no information
130 * leak is possible in the following two lines of code. */
131 unsigned int alen = strlen(a);
132 unsigned int blen = strlen(b);
133 unsigned int j;
134 int diff = 0;
135
136 /* We can't compare strings longer than our static buffers.
137 * Note that this will never pass the first test in practical circumstances
138 * so there is no info leak. */
139 if (alen > sizeof(bufa) || blen > sizeof(bufb)) return 1;
140
141 memset(bufa,0,sizeof(bufa)); /* Constant time. */
142 memset(bufb,0,sizeof(bufb)); /* Constant time. */
143 /* Again the time of the following two copies is proportional to
144 * len(a) + len(b) so no info is leaked. */
145 memcpy(bufa,a,alen);
146 memcpy(bufb,b,blen);
147
148 /* Always compare all the chars in the two buffers without
149 * conditional expressions. */
150 for (j = 0; j < sizeof(bufa); j++) {
151 diff |= (bufa[j] ^ bufb[j]);
152 }
153 /* Length must be equal as well. */
154 diff |= alen ^ blen;
155 return diff; /* If zero strings are the same. */
156}
157
158/* Given an SDS string, returns the SHA256 hex representation as a
159 * new SDS string. */

Callers 1

ACLCheckUserCredentialsFunction · 0.85

Calls 2

memsetFunction · 0.85
memcpyFunction · 0.50

Tested by

no test coverage detected