qemu-aes_guest.c (1286B)
1#include "cachepc/uapi.h" 2#include "kcapi.h" 3 4#include <sys/random.h> 5#include <err.h> 6#include <time.h> 7#include <assert.h> 8#include <unistd.h> 9#include <string.h> 10#include <stdio.h> 11#include <stdint.h> 12#include <stdlib.h> 13 14static uint8_t key[16]; 15 16void 17printhex(uint8_t *buf, size_t size) 18{ 19 size_t i; 20 21 for (i = 0; i < size; i++) 22 printf("%02X", buf[i]); 23 printf("\n"); 24} 25 26int 27main(int argc, const char **argv) 28{ 29 struct kcapi_handle *kcapi; 30 uint8_t block[128]; 31 uint8_t *buf; 32 size_t n; 33 34 buf = NULL; 35 if (posix_memalign((void *)&buf, L1_LINESIZE * L1_SETS, L1_LINESIZE * L1_SETS)) 36 err(1, "memalign"); 37 memset(buf, 0, L1_LINESIZE * L1_SETS); 38 39 kcapi = NULL; 40 if (kcapi_cipher_init(&kcapi, "ecb(aes)", 0)) 41 err(1, "kcapi init"); 42 43 for (n = 0; n < 16; n++) 44 key[n] = (uint8_t) n; 45 46 if (kcapi_cipher_setkey(kcapi, key, sizeof(key))) 47 err(1, "kcapi setkey"); 48 49 while (1) { 50 printf("RUN %li\n", time(NULL)); 51 52 memset(block, 0, sizeof(block)); 53 strncpy((char *) block, "Hello world", sizeof(block)); 54 55 printhex(block, sizeof(block)); 56 n = kcapi_cipher_encrypt(kcapi, block, sizeof(block), NULL, 57 block, sizeof(block), KCAPI_ACCESS_HEURISTIC); 58 if (n != sizeof(block)) 59 err(1, "encrypt"); 60 printhex(block, sizeof(block)); 61 62 sleep(1); 63 } 64 65 kcapi_cipher_destroy(kcapi); 66}