tomb-kdb-pbkdf2-getiter.c (1628B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include <sys/time.h> 6 7 #include <gcrypt.h> 8 9 static long bench(int ic) { 10 char *pass = "mypass"; 11 unsigned char *salt = "abcdefghijklmno"; 12 int salt_len = strlen(salt); 13 int result_len = 64; 14 unsigned char *result = calloc(result_len, sizeof(char)); 15 struct timeval start, end; 16 long microtime; 17 18 gettimeofday(&start, NULL); 19 gcry_kdf_derive( pass, strlen(pass), GCRY_KDF_PBKDF2, GCRY_MD_SHA1, salt, salt_len, ic, result_len, result); 20 gettimeofday(&end, NULL); 21 microtime = 1000000*end.tv_sec+end.tv_usec - (1000000*start.tv_sec+start.tv_usec); 22 23 return (long)microtime; 24 } 25 int main(int argc, char *argv[]) 26 { 27 long desired_time = 1000000; 28 long microtime; 29 int ic=100; 30 int tries=0; 31 if(argc >= 2) 32 sscanf(argv[1], "%ld", &desired_time); 33 if (!gcry_check_version ("1.5.0")) { 34 fputs ("libgcrypt version mismatch\n", stderr); 35 exit (2); 36 } 37 /* Allocate a pool of 16k secure memory. This make the secure memory 38 available and also drops privileges where needed. */ 39 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); 40 /* It is now okay to let Libgcrypt complain when there was/is 41 a problem with the secure memory. */ 42 gcry_control (GCRYCTL_RESUME_SECMEM_WARN); 43 /* Tell Libgcrypt that initialization has completed. */ 44 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 45 46 47 microtime = bench(ic); 48 while( abs(desired_time-microtime) > (desired_time/10) /*little difference */ 49 && tries++ <= 5) { 50 float ratio = (float)desired_time/microtime; 51 if(ratio > 1000) ratio=1000.0; 52 ic*=ratio; 53 if(ic<1) ic=1; 54 microtime = bench(ic); 55 } 56 printf("%d\n", ic); 57 return 0; 58 59 }