summaryrefslogtreecommitdiff
path: root/src/lib/libc/crypt
diff options
context:
space:
mode:
authortedu <>2015-01-05 13:10:10 +0000
committertedu <>2015-01-05 13:10:10 +0000
commit759e72e15da1656a7a5920e17f955f6d719a56a3 (patch)
tree07dcd8b9809722dcdecf66976cb5938d444902e3 /src/lib/libc/crypt
parent188599682a12264dc9832b9139381811e79ccce7 (diff)
downloadopenbsd-759e72e15da1656a7a5920e17f955f6d719a56a3.tar.gz
openbsd-759e72e15da1656a7a5920e17f955f6d719a56a3.tar.bz2
openbsd-759e72e15da1656a7a5920e17f955f6d719a56a3.zip
convert clock() to clock_gettime() for improved precision (and accuracy?)
guenther suggested using thread time, which actually may improve accuracy if somebody puts this in a threaded program.
Diffstat (limited to 'src/lib/libc/crypt')
-rw-r--r--src/lib/libc/crypt/bcrypt.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
index abcbe138ca..1114eae44e 100644
--- a/src/lib/libc/crypt/bcrypt.c
+++ b/src/lib/libc/crypt/bcrypt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bcrypt.c,v 1.47 2014/12/30 10:27:24 tedu Exp $ */ 1/* $OpenBSD: bcrypt.c,v 1.48 2015/01/05 13:10:10 tedu Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> 4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
@@ -231,24 +231,26 @@ bcrypt_checkpass(const char *pass, const char *goodhash)
231int 231int
232bcrypt_autorounds(void) 232bcrypt_autorounds(void)
233{ 233{
234 clock_t before, after; 234 struct timespec before, after;
235 int r = 8; 235 int r = 8;
236 char buf[_PASSWORD_LEN]; 236 char buf[_PASSWORD_LEN];
237 int duration; 237 int duration;
238 238
239 before = clock(); 239 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &before);
240 bcrypt_newhash("testpassword", r, buf, sizeof(buf)); 240 bcrypt_newhash("testpassword", r, buf, sizeof(buf));
241 after = clock(); 241 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &after);
242 242
243 duration = after - before; 243 duration = after.tv_sec - before.tv_sec;
244 duration *= 1000000;
245 duration += (after.tv_nsec - before.tv_nsec) / 1000;
244 246
245 /* too quick? slow it down. */ 247 /* too quick? slow it down. */
246 while (r < 16 && duration <= CLOCKS_PER_SEC / 4) { 248 while (r < 16 && duration <= 1000000 / 4) {
247 r += 1; 249 r += 1;
248 duration *= 2; 250 duration *= 2;
249 } 251 }
250 /* too slow? speed it up. */ 252 /* too slow? speed it up. */
251 while (r > 4 && duration > CLOCKS_PER_SEC / 2) { 253 while (r > 4 && duration > 1000000 / 2) {
252 r -= 1; 254 r -= 1;
253 duration /= 2; 255 duration /= 2;
254 } 256 }