Created
June 7, 2012 19:07
-
-
Save developernotes/2890887 to your computer and use it in GitHub Desktop.
Sampling PKCS5_PBKDF2_HMAC_SHA1 for adaptive cycles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdbool.h> | |
| #include <pthread.h> | |
| #include <semaphore.h> | |
| #include <signal.h> | |
| #include <errno.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <openssl/hmac.h> | |
| static sem_t * semAlaram; | |
| typedef void (*sighandler_t)(int); | |
| static sighandler_t handler = NULL; | |
| static int runCount = 0; | |
| static int secondsToRun = 1; | |
| static int cycleCount = 0; | |
| int runResults = 0; | |
| int numberOfRuns = 5; | |
| static void* waitForAlaram(void* arg) | |
| { | |
| while(true){ | |
| sem_wait(semAlaram); | |
| runResults += cycleCount; | |
| printf("Run %d complete - cycle count:%d\n", runCount, cycleCount); | |
| cycleCount = 0; | |
| } | |
| return NULL; | |
| } | |
| static void sighandler(int signal){ | |
| if(signal == SIGALRM){ | |
| runCount++; | |
| sem_post(semAlaram); | |
| alarm(secondsToRun); | |
| }else if (handler){ | |
| handler(signal); | |
| } | |
| } | |
| void calculateCycles(){ | |
| char *password = "5^ra6fU&jHWXad"; | |
| char *salt = "123456789"; | |
| int iterationCount = 1; | |
| int keySize = 256; | |
| unsigned char buffer[1024]; | |
| PKCS5_PBKDF2_HMAC_SHA1(password, strlen(password), (unsigned char *)salt, strlen(salt), | |
| iterationCount, keySize, buffer); | |
| cycleCount++; | |
| } | |
| int main(int argc, char *argv[]){ | |
| pthread_t thread; | |
| if(argc == 2){ | |
| numberOfRuns = atoi(argv[1]); | |
| } | |
| semAlaram = sem_open("semaphore", O_CREAT, S_IRUSR | S_IWUSR, 0); | |
| if(semAlaram == SEM_FAILED){ | |
| printf("error initializing semaphore: %s", strerror(errno)); | |
| return -1; | |
| } | |
| if(pthread_create(&thread, NULL, waitForAlaram, NULL) != 0){ | |
| printf("error creating thread: %s", strerror(errno)); | |
| return -1; | |
| } | |
| handler = signal(SIGALRM, sighandler); | |
| alarm(secondsToRun); | |
| while(runCount < numberOfRuns){ | |
| calculateCycles(); | |
| } | |
| printf("Sampling done - average cycle count:%d\n", runResults/numberOfRuns); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment