summaryrefslogtreecommitdiffstats
path: root/test/qemu-aes_guest.c
blob: c52e5ea07f24e3c4d603f4e3951343f62dd2361c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "cachepc/uapi.h"
#include "kcapi.h"

#include <sys/random.h>
#include <err.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

static uint8_t key[16];

void
printhex(uint8_t *buf, size_t size)
{
	size_t i;

	for (i = 0; i < size; i++)
		printf("%02X", buf[i]);
	printf("\n");
}

int
main(int argc, const char **argv)
{
	struct kcapi_handle *kcapi;
	uint8_t block[128];
	uint8_t *buf;
	size_t n;

	buf = NULL;
	if (posix_memalign((void *)&buf, L1_LINESIZE * L1_SETS, L1_LINESIZE * L1_SETS))
		err(1, "memalign");
	memset(buf, 0, L1_LINESIZE * L1_SETS);

	kcapi = NULL;
	if (kcapi_cipher_init(&kcapi, "ecb(aes)", 0))
		err(1, "kcapi init");

	for (n = 0; n < 16; n++)
		key[n] = (uint8_t) n;

	if (kcapi_cipher_setkey(kcapi, key, sizeof(key)))
		err(1, "kcapi setkey");

	while (1) {
		printf("RUN %li\n", time(NULL));

		memset(block, 0, sizeof(block));
		strncpy((char *) block, "Hello world", sizeof(block));

		printhex(block, sizeof(block));
		n = kcapi_cipher_encrypt(kcapi, block, sizeof(block), NULL,
			block, sizeof(block), KCAPI_ACCESS_HEURISTIC);
		if (n != sizeof(block))
			err(1, "encrypt");
		printhex(block, sizeof(block));

		sleep(1);
	}

	kcapi_cipher_destroy(kcapi);
}