~jkaivo/deonebook

ref: 78d6a052fc6431ad45ff3a24461fd7744a3c1c28 deonebook/deonebook.c -rw-r--r-- 1.3 KiB
78d6a052 — Jakob Kaivo enforce device type of mmcblk, fail if /sys/block/<device>/device is not a readable directory 1 year, 10 months ago
                                                                                
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
67
68
69
70
71
72
73
74
75
76
77
78
#define _XOPEN_SOURCE 700
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "deonebook.h"

int main(int argc, char *argv[])
{
	enum { DECRYPT, GENERATE, READ } mode = DECRYPT;

	int c;
	while ((c = getopt(argc, argv, "RGk:")) != -1) {
		switch (c) {
		case 'R':
			mode = READ;
			break;

		case 'G':
			mode = GENERATE;
			break;

		case 'k':
			/* keyfile = optarg; */
			break;

		default:
			return 1;
		}
	}

	if (mode != DECRYPT) {
		unsigned char *key = NULL;
		if (mode == READ) {
			key = readkey("./.shm");
		} else {
			char *device = "mmcblk0";
			if (optind < argc) {
				device = argv[optind];
			}

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

			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 1;
			}

			key = genkey(dirfd(d));
		}

		if (key == NULL) {
			return 1;
		}

		for (int i = 0; i < KEY_SIZE; i++) {
			printf("%02hhx", key[i]);
		}
		printf("\n");

		return 0;
	}

	if (optind >= argc) {
		fprintf(stderr, "%s: missing operands\n", argv[0]);
		return 1;
	}

	do {
		printf("decrypting %s\n", argv[optind]);
	} while (++optind < argc);
}