Fossil SCM

Rename constant_time_eq to constant_time_cmp to better indicate that these functions return 0 when values are equal, like memcmp, strcmp, etc., not truth, to avoid possible mistakes.

dmitry 2011-10-04 14:34 dmitry-security
Commit d244c484e73e5eb18eef0cc1f8e5fb04632fc2a8
3 files changed +1 -1 +10 -10 +2 -2
+1 -1
--- src/blob.c
+++ src/blob.c
@@ -319,11 +319,11 @@
319319
/*
320320
** Compare two blobs in constant time and return zero if they are equal.
321321
** Constant time comparison only applies for blobs of the same length.
322322
** If lengths are different, immediately returns 1.
323323
*/
324
-int blob_constant_time_eq(Blob *pA, Blob *pB){
324
+int blob_constant_time_cmp(Blob *pA, Blob *pB){
325325
int szA, szB, i;
326326
unsigned char *buf1, *buf2;
327327
unsigned char rc = 0;
328328
329329
blob_is_init(pA);
330330
--- src/blob.c
+++ src/blob.c
@@ -319,11 +319,11 @@
319 /*
320 ** Compare two blobs in constant time and return zero if they are equal.
321 ** Constant time comparison only applies for blobs of the same length.
322 ** If lengths are different, immediately returns 1.
323 */
324 int blob_constant_time_eq(Blob *pA, Blob *pB){
325 int szA, szB, i;
326 unsigned char *buf1, *buf2;
327 unsigned char rc = 0;
328
329 blob_is_init(pA);
330
--- src/blob.c
+++ src/blob.c
@@ -319,11 +319,11 @@
319 /*
320 ** Compare two blobs in constant time and return zero if they are equal.
321 ** Constant time comparison only applies for blobs of the same length.
322 ** If lengths are different, immediately returns 1.
323 */
324 int blob_constant_time_cmp(Blob *pA, Blob *pB){
325 int szA, szB, i;
326 unsigned char *buf1, *buf2;
327 unsigned char rc = 0;
328
329 blob_is_init(pA);
330
+10 -10
--- src/login.c
+++ src/login.c
@@ -231,12 +231,12 @@
231231
if( g.perm.Password && zPasswd && (zNew1 = P("n1"))!=0 && (zNew2 = P("n2"))!=0 ){
232232
/* The user requests a password change */
233233
zSha1Pw = sha1_shared_secret(zPasswd, g.zLogin, 0);
234234
if( db_int(1, "SELECT 0 FROM user"
235235
" WHERE uid=%d"
236
- " AND (constant_time_eq(pw,%Q)=0"
237
- " OR constant_time_eq(pw,%Q)=0)",
236
+ " AND (constant_time_cmp(pw,%Q)=0"
237
+ " OR constant_time_cmp(pw,%Q)=0)",
238238
g.userUid, zSha1Pw, zPasswd) ){
239239
sleep(1);
240240
zErrMsg =
241241
@ <p><span class="loginError">
242242
@ You entered an incorrect old password while attempting to change
@@ -310,11 +310,11 @@
310310
uid = db_int(0,
311311
"SELECT uid FROM user"
312312
" WHERE login=%Q"
313313
" AND length(cap)>0 AND length(pw)>0"
314314
" AND login NOT IN ('anonymous','nobody','developer','reader')"
315
- " AND (constant_time_eq(pw,%Q)=0 OR constant_time_eq(pw,%Q)=0)",
315
+ " AND (constant_time_cmp(pw,%Q)=0 OR constant_time_cmp(pw,%Q)=0)",
316316
zUsername, zSha1Pw, zPasswd
317317
);
318318
if( uid<=0 ){
319319
sleep(1);
320320
zErrMsg =
@@ -458,11 +458,11 @@
458458
459459
/*
460460
** SQL function for constant time comparison of two values.
461461
** Sets result to 0 if two values are equal.
462462
*/
463
-static void constant_time_eq_function(
463
+static void constant_time_cmp_function(
464464
sqlite3_context *context,
465465
int argc,
466466
sqlite3_value **argv
467467
){
468468
const unsigned char *buf1, *buf2;
@@ -510,21 +510,21 @@
510510
if( zOtherRepo==0 ) return 0; /* No such peer repository */
511511
512512
rc = sqlite3_open(zOtherRepo, &pOther);
513513
if( rc==SQLITE_OK ){
514514
sqlite3_create_function(pOther,"now",0,SQLITE_ANY,0,db_now_function,0,0);
515
- sqlite3_create_function(pOther, "constant_time_eq", 2, SQLITE_UTF8, 0,
516
- constant_time_eq_function, 0, 0);
515
+ sqlite3_create_function(pOther, "constant_time_cmp", 2, SQLITE_UTF8, 0,
516
+ constant_time_cmp_function, 0, 0);
517517
sqlite3_busy_timeout(pOther, 5000);
518518
zSQL = mprintf(
519519
"SELECT cexpire FROM user"
520520
" WHERE login=%Q"
521521
" AND ipaddr=%Q"
522522
" AND length(cap)>0"
523523
" AND length(pw)>0"
524524
" AND cexpire>julianday('now')"
525
- " AND constant_time_eq(cookie,%Q)=0",
525
+ " AND constant_time_cmp(cookie,%Q)=0",
526526
zLogin, zRemoteAddr, zHash
527527
);
528528
pStmt = 0;
529529
rc = sqlite3_prepare_v2(pOther, zSQL, -1, &pStmt, 0);
530530
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
@@ -562,11 +562,11 @@
562562
" WHERE login=%Q"
563563
" AND ipaddr=%Q"
564564
" AND cexpire>julianday('now')"
565565
" AND length(cap)>0"
566566
" AND length(pw)>0"
567
- " AND constant_time_eq(cookie,%Q)=0",
567
+ " AND constant_time_cmp(cookie,%Q)=0",
568568
zLogin, zRemoteAddr, zCookie
569569
);
570570
return uid;
571571
}
572572
@@ -586,12 +586,12 @@
586586
const char *zCap = 0; /* Capability string */
587587
588588
/* Only run this check once. */
589589
if( g.userUid!=0 ) return;
590590
591
- sqlite3_create_function(g.db, "constant_time_eq", 2, SQLITE_UTF8, 0,
592
- constant_time_eq_function, 0, 0);
591
+ sqlite3_create_function(g.db, "constant_time_cmp", 2, SQLITE_UTF8, 0,
592
+ constant_time_cmp_function, 0, 0);
593593
594594
/* If the HTTP connection is coming over 127.0.0.1 and if
595595
** local login is disabled and if we are using HTTP and not HTTPS,
596596
** then there is no need to check user credentials.
597597
**
598598
--- src/login.c
+++ src/login.c
@@ -231,12 +231,12 @@
231 if( g.perm.Password && zPasswd && (zNew1 = P("n1"))!=0 && (zNew2 = P("n2"))!=0 ){
232 /* The user requests a password change */
233 zSha1Pw = sha1_shared_secret(zPasswd, g.zLogin, 0);
234 if( db_int(1, "SELECT 0 FROM user"
235 " WHERE uid=%d"
236 " AND (constant_time_eq(pw,%Q)=0"
237 " OR constant_time_eq(pw,%Q)=0)",
238 g.userUid, zSha1Pw, zPasswd) ){
239 sleep(1);
240 zErrMsg =
241 @ <p><span class="loginError">
242 @ You entered an incorrect old password while attempting to change
@@ -310,11 +310,11 @@
310 uid = db_int(0,
311 "SELECT uid FROM user"
312 " WHERE login=%Q"
313 " AND length(cap)>0 AND length(pw)>0"
314 " AND login NOT IN ('anonymous','nobody','developer','reader')"
315 " AND (constant_time_eq(pw,%Q)=0 OR constant_time_eq(pw,%Q)=0)",
316 zUsername, zSha1Pw, zPasswd
317 );
318 if( uid<=0 ){
319 sleep(1);
320 zErrMsg =
@@ -458,11 +458,11 @@
458
459 /*
460 ** SQL function for constant time comparison of two values.
461 ** Sets result to 0 if two values are equal.
462 */
463 static void constant_time_eq_function(
464 sqlite3_context *context,
465 int argc,
466 sqlite3_value **argv
467 ){
468 const unsigned char *buf1, *buf2;
@@ -510,21 +510,21 @@
510 if( zOtherRepo==0 ) return 0; /* No such peer repository */
511
512 rc = sqlite3_open(zOtherRepo, &pOther);
513 if( rc==SQLITE_OK ){
514 sqlite3_create_function(pOther,"now",0,SQLITE_ANY,0,db_now_function,0,0);
515 sqlite3_create_function(pOther, "constant_time_eq", 2, SQLITE_UTF8, 0,
516 constant_time_eq_function, 0, 0);
517 sqlite3_busy_timeout(pOther, 5000);
518 zSQL = mprintf(
519 "SELECT cexpire FROM user"
520 " WHERE login=%Q"
521 " AND ipaddr=%Q"
522 " AND length(cap)>0"
523 " AND length(pw)>0"
524 " AND cexpire>julianday('now')"
525 " AND constant_time_eq(cookie,%Q)=0",
526 zLogin, zRemoteAddr, zHash
527 );
528 pStmt = 0;
529 rc = sqlite3_prepare_v2(pOther, zSQL, -1, &pStmt, 0);
530 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
@@ -562,11 +562,11 @@
562 " WHERE login=%Q"
563 " AND ipaddr=%Q"
564 " AND cexpire>julianday('now')"
565 " AND length(cap)>0"
566 " AND length(pw)>0"
567 " AND constant_time_eq(cookie,%Q)=0",
568 zLogin, zRemoteAddr, zCookie
569 );
570 return uid;
571 }
572
@@ -586,12 +586,12 @@
586 const char *zCap = 0; /* Capability string */
587
588 /* Only run this check once. */
589 if( g.userUid!=0 ) return;
590
591 sqlite3_create_function(g.db, "constant_time_eq", 2, SQLITE_UTF8, 0,
592 constant_time_eq_function, 0, 0);
593
594 /* If the HTTP connection is coming over 127.0.0.1 and if
595 ** local login is disabled and if we are using HTTP and not HTTPS,
596 ** then there is no need to check user credentials.
597 **
598
--- src/login.c
+++ src/login.c
@@ -231,12 +231,12 @@
231 if( g.perm.Password && zPasswd && (zNew1 = P("n1"))!=0 && (zNew2 = P("n2"))!=0 ){
232 /* The user requests a password change */
233 zSha1Pw = sha1_shared_secret(zPasswd, g.zLogin, 0);
234 if( db_int(1, "SELECT 0 FROM user"
235 " WHERE uid=%d"
236 " AND (constant_time_cmp(pw,%Q)=0"
237 " OR constant_time_cmp(pw,%Q)=0)",
238 g.userUid, zSha1Pw, zPasswd) ){
239 sleep(1);
240 zErrMsg =
241 @ <p><span class="loginError">
242 @ You entered an incorrect old password while attempting to change
@@ -310,11 +310,11 @@
310 uid = db_int(0,
311 "SELECT uid FROM user"
312 " WHERE login=%Q"
313 " AND length(cap)>0 AND length(pw)>0"
314 " AND login NOT IN ('anonymous','nobody','developer','reader')"
315 " AND (constant_time_cmp(pw,%Q)=0 OR constant_time_cmp(pw,%Q)=0)",
316 zUsername, zSha1Pw, zPasswd
317 );
318 if( uid<=0 ){
319 sleep(1);
320 zErrMsg =
@@ -458,11 +458,11 @@
458
459 /*
460 ** SQL function for constant time comparison of two values.
461 ** Sets result to 0 if two values are equal.
462 */
463 static void constant_time_cmp_function(
464 sqlite3_context *context,
465 int argc,
466 sqlite3_value **argv
467 ){
468 const unsigned char *buf1, *buf2;
@@ -510,21 +510,21 @@
510 if( zOtherRepo==0 ) return 0; /* No such peer repository */
511
512 rc = sqlite3_open(zOtherRepo, &pOther);
513 if( rc==SQLITE_OK ){
514 sqlite3_create_function(pOther,"now",0,SQLITE_ANY,0,db_now_function,0,0);
515 sqlite3_create_function(pOther, "constant_time_cmp", 2, SQLITE_UTF8, 0,
516 constant_time_cmp_function, 0, 0);
517 sqlite3_busy_timeout(pOther, 5000);
518 zSQL = mprintf(
519 "SELECT cexpire FROM user"
520 " WHERE login=%Q"
521 " AND ipaddr=%Q"
522 " AND length(cap)>0"
523 " AND length(pw)>0"
524 " AND cexpire>julianday('now')"
525 " AND constant_time_cmp(cookie,%Q)=0",
526 zLogin, zRemoteAddr, zHash
527 );
528 pStmt = 0;
529 rc = sqlite3_prepare_v2(pOther, zSQL, -1, &pStmt, 0);
530 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
@@ -562,11 +562,11 @@
562 " WHERE login=%Q"
563 " AND ipaddr=%Q"
564 " AND cexpire>julianday('now')"
565 " AND length(cap)>0"
566 " AND length(pw)>0"
567 " AND constant_time_cmp(cookie,%Q)=0",
568 zLogin, zRemoteAddr, zCookie
569 );
570 return uid;
571 }
572
@@ -586,12 +586,12 @@
586 const char *zCap = 0; /* Capability string */
587
588 /* Only run this check once. */
589 if( g.userUid!=0 ) return;
590
591 sqlite3_create_function(g.db, "constant_time_cmp", 2, SQLITE_UTF8, 0,
592 constant_time_cmp_function, 0, 0);
593
594 /* If the HTTP connection is coming over 127.0.0.1 and if
595 ** local login is disabled and if we are using HTTP and not HTTPS,
596 ** then there is no need to check user credentials.
597 **
598
+2 -2
--- src/xfer.c
+++ src/xfer.c
@@ -573,11 +573,11 @@
573573
blob_zero(&combined);
574574
blob_copy(&combined, pNonce);
575575
blob_append(&combined, blob_buffer(&pw), szPw);
576576
sha1sum_blob(&combined, &hash);
577577
assert( blob_size(&hash)==40 );
578
- rc = blob_constant_time_eq(&hash, pSig);
578
+ rc = blob_constant_time_cmp(&hash, pSig);
579579
blob_reset(&hash);
580580
blob_reset(&combined);
581581
if( rc!=0 && szPw!=40 ){
582582
/* If this server stores cleartext passwords and the password did not
583583
** match, then perhaps the client is sending SHA1 passwords. Try
@@ -588,11 +588,11 @@
588588
blob_zero(&combined);
589589
blob_copy(&combined, pNonce);
590590
blob_append(&combined, zSecret, -1);
591591
free(zSecret);
592592
sha1sum_blob(&combined, &hash);
593
- rc = blob_constant_time_eq(&hash, pSig);
593
+ rc = blob_constant_time_cmp(&hash, pSig);
594594
blob_reset(&hash);
595595
blob_reset(&combined);
596596
}
597597
if( rc==0 ){
598598
const char *zCap;
599599
--- src/xfer.c
+++ src/xfer.c
@@ -573,11 +573,11 @@
573 blob_zero(&combined);
574 blob_copy(&combined, pNonce);
575 blob_append(&combined, blob_buffer(&pw), szPw);
576 sha1sum_blob(&combined, &hash);
577 assert( blob_size(&hash)==40 );
578 rc = blob_constant_time_eq(&hash, pSig);
579 blob_reset(&hash);
580 blob_reset(&combined);
581 if( rc!=0 && szPw!=40 ){
582 /* If this server stores cleartext passwords and the password did not
583 ** match, then perhaps the client is sending SHA1 passwords. Try
@@ -588,11 +588,11 @@
588 blob_zero(&combined);
589 blob_copy(&combined, pNonce);
590 blob_append(&combined, zSecret, -1);
591 free(zSecret);
592 sha1sum_blob(&combined, &hash);
593 rc = blob_constant_time_eq(&hash, pSig);
594 blob_reset(&hash);
595 blob_reset(&combined);
596 }
597 if( rc==0 ){
598 const char *zCap;
599
--- src/xfer.c
+++ src/xfer.c
@@ -573,11 +573,11 @@
573 blob_zero(&combined);
574 blob_copy(&combined, pNonce);
575 blob_append(&combined, blob_buffer(&pw), szPw);
576 sha1sum_blob(&combined, &hash);
577 assert( blob_size(&hash)==40 );
578 rc = blob_constant_time_cmp(&hash, pSig);
579 blob_reset(&hash);
580 blob_reset(&combined);
581 if( rc!=0 && szPw!=40 ){
582 /* If this server stores cleartext passwords and the password did not
583 ** match, then perhaps the client is sending SHA1 passwords. Try
@@ -588,11 +588,11 @@
588 blob_zero(&combined);
589 blob_copy(&combined, pNonce);
590 blob_append(&combined, zSecret, -1);
591 free(zSecret);
592 sha1sum_blob(&combined, &hash);
593 rc = blob_constant_time_cmp(&hash, pSig);
594 blob_reset(&hash);
595 blob_reset(&combined);
596 }
597 if( rc==0 ){
598 const char *zCap;
599

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button