static struct crypt *setup_cipher(int type, int id, int mac) { struct cipher_list *c; int klen = 20; void *key; struct crypt_sym *cs; struct crypt *ci; c = crypt_find_cipher(type, id); if (!c) errx(1, "Can't find cipher %d (type %d)", id, type); cs = crypt_new(c->c_ctr); if (mac) ci = cs->cs_mac; else ci = cs->cs_cipher; key = alloca(klen); assert(key); memset(key, 0, klen); crypt_set_key(ci, key, klen); /* XXX cs is leaked */ return ci; }
static int hkdf_set_key(struct crypt *c, void *data, int len) { struct hkdf_priv *hk = crypt_priv(c); crypt_set_key(hk->hk_hmac, data, len); return 0; }
int main(void) { int i, j; struct timeval then, now; suseconds_t totalHash = 0; suseconds_t totalAes = 0; FILE *opf; crypt_context_t context; char readings[MSG_LEN + 1]; char hashBuff[32]; char encBuff[32]; opf = fopen("data.out", "w"); srand(time(NULL)); crypt_initialise(&context); /* For test purposes, the key is left wide open here */ crypt_set_key(&context, "abcdefghijklmnopqrstuvwxyz012345"); for(i = 0; i < MSG_LEN; i++) readings[i] = 0; for(i = 0; i < ITERS; i++) { /* Generate data randomly (since there's nothing connected on RPi to probe */ sprintf(&readings[0], "%04x", rand() & 0xffff); sprintf(&readings[4], "%04x", rand() & 0xffff); sprintf(&readings[8], "%04x", rand() & 0xffff); sprintf(&readings[12], "%04x", rand() & 0xffff); sprintf(&readings[16], "%04x", rand() & 0xffff); sprintf(&readings[20], "%04x", rand() & 0xffff); sprintf(&readings[24], "%04x", rand() & 0xffff); sprintf(&readings[28], "%04x", rand() & 0xffff); //sprintf(readings, "abcdefghijklmnopqrstuvwxyz012345"); /* Digest data */ gettimeofday(&then, NULL); crypt_digest(&context, readings, MSG_LEN, hashBuff); gettimeofday(&now, NULL); totalHash += (now.tv_usec - then.tv_usec); /* Cipher digest */ gettimeofday(&then, NULL); crypt_aes_enc(&context, hashBuff, encBuff, 32, "0123456789abcdef"); gettimeofday(&now, NULL); totalAes += (now.tv_usec - then.tv_usec); /* Save findings to file */ fprintf(opf, "%s\n", readings); for(j = 0; j < 32; j++) fprintf(opf, "%02x", hashBuff[j] & 0xff); fprintf(opf, "\n"); for(j = 0; j < 32; j++) fprintf(opf, "%02x", encBuff[j] & 0xff); fprintf(opf, "\n"); } /* Print statistics */ printf("Done. Elapsed hash time: %ld us\n", totalHash); printf("Done. Elapsed hash time per iter: %ld us\n", totalHash / ITERS); printf("Done. Elapsed cipher time: %ld us\n", totalAes); printf("Done. Elapsed total time: %ld us\n", totalHash + totalAes); crypt_terminate(&context); fclose(opf); return 0; }