summaryrefslogtreecommitdiff
path: root/getkey.c
blob: 6e3da12429f2491f2bbadf72c7f483c53adf0e84 (plain)
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
#define _XOPEN_SOURCE 700
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "deonebook.h"

unsigned char *getkey(const char *keystring, const char *device)
{
	if (keystring) {
		static unsigned char key[KEY_SIZE] = "";
		if (strlen(keystring) != (KEY_SIZE * 2)) {
			fprintf(stderr, "key must be %d hex characters\n", KEY_SIZE * 2);
			return NULL;
		}

		for (size_t i = 0; i < sizeof(key); i++) {
			char hex[] = { keystring[i * 2], keystring[i * 2 + 1], '\0' };
			key[i] = (unsigned char)strtoul(hex, NULL, 16);
		}
		return key;
	}

	if (!device) {
		device = "mmcblk0";
	} else if (strstr(device, "mmcblk") != device) {
		fprintf(stderr, "device must be an mmcblk device\n");
		return NULL;
	}

	char path[256];
	snprintf(path, sizeof(path), "/sys/block/%s/device", device);
	DIR *d = opendir(path);
	if (d == NULL) {
		fprintf(stderr, "device '%s' not found\n", device);
		return NULL;
	}

	unsigned char *key = genkey(dirfd(d));
	if (key) {
		return key;
	}

	return readkey("./.shm");
}