Skip to content

Instantly share code, notes, and snippets.

@developernotes
Created June 7, 2012 19:07
Show Gist options
  • Select an option

  • Save developernotes/2890887 to your computer and use it in GitHub Desktop.

Select an option

Save developernotes/2890887 to your computer and use it in GitHub Desktop.
Sampling PKCS5_PBKDF2_HMAC_SHA1 for adaptive cycles
#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