summarylogtreecommitdiffstats
path: root/aes.cc
blob: 263757995d93f70b99e88f25e134d9c7b5e76ad2 (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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <docopt/docopt.h>
#include <docopt/docopt_value.h>
#include <fcntl.h>
#include <gcrypt.h>
#include <termios.h>
#include <unistd.h>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>

#ifndef PROJECT_VERSION
#warning Use hardcoded version
#define PROJECT_VERSION "0.1.0"
#endif

auto get_version() {
	return "aes v" PROJECT_VERSION;
}

auto get_usage() {
	return get_version() + std::string(R"(

Usage:
	aes [options]

Options:
    -h, --help                     Pring this message
    -v, --version                  Print version and exit
    -d, --decrypt                  Decrypt data
    -i, --input=FILE               Set input file [default: /dev/stdin]
    -o, --output=FILE              Set output file [default: /dev/stdout]
    -p, --password=PASSWORD        Use password [default: prompt]
    -k, --key=KEY                  Use key instead password [default: ]
    -l, --last=LAST                Size of last block [default: 0]
    -b, --bits=BITS                Use aes with specified bits [default: 256]
)");
}

gcry_error_t proccess_encrypt(gcry_cipher_hd_t hd,
							  const char* block_in,
							  char* block_out) {
	return gcry_cipher_encrypt(hd, block_out, 16, block_in, 16);
}

gcry_error_t proccess_decrypt(gcry_cipher_hd_t hd,
							  const char* block_in,
							  char* block_out) {
	return gcry_cipher_decrypt(hd, block_out, 16, block_in, 16);
}

void try_gcry(gcry_error_t err) {
	auto code = gcry_err_code(err);

	if (code != 0) {
		throw std::runtime_error((const char*)gcry_err_source(err));
	}
}

int main(int argc, const char** argv) {
	auto args = docopt::docopt(get_usage(), {argv + 1, argv + argc}, true,
							   get_version());

	auto decrypt = args.at("--decrypt").asBool();
	auto password = args.at("--password").asString();
	auto key = args.at("--key").asString();
	auto bits = args.at("--bits").asLong();
	auto last = args.at("--last").asLong();
	auto input = args.at("--input").asString();
	auto output = args.at("--output").asString();
	try {
		if (password != "prompt" && key != "") {
			throw std::invalid_argument(
				"using --password and --key at some time is imposible.");
		}

		if (last > 15 || last < 0) {
			throw std::invalid_argument("--last should be between 0 and 15.");
		}

		if (bits != 256 && bits != 128) {
			throw std::invalid_argument("--bits should be 128 or 256.");
		}

		if (key.size() != 0 && key.size() != (bits >> 1)) {
			throw std::invalid_argument("--key size should be equal BITS/2");
		}

		if (decrypt == false && last != 0) {
			std::cerr << "Note: using --last possible only with --decrypt\n";
		}
	} catch (const std::invalid_argument& e) {
		std::cerr << e.what() << '\n';
		return 1;
	}

	std::ifstream fin;
	std::ofstream fout;

	if (input != "/dev/stdin") {
		fin.open(input);
		std::cin.rdbuf(fin.rdbuf());
	}
	if (output != "/dev/stdout") {
		fout.open(output);
		std::cout.rdbuf(fout.rdbuf());
	}

	size_t key_size = bits >> 2;
	std::unique_ptr<char[]> key_arr(new char[key_size]);

	if (key == "") {
		if (password == "prompt") {
			password = getpass("Enter password:");
		}
		gcry_md_hd_t md_handle;
		try_gcry(gcry_md_open(&md_handle, GCRY_MD_SHA512, 0));

		gcry_md_write(md_handle, password.data(), password.size());

		std::memcpy(key_arr.get(), gcry_md_read(md_handle, GCRY_MD_SHA512),
					key_size);

		gcry_md_close(md_handle);
	} else {
		try {
			for (size_t i = 0; i < key_size; ++i) {
				char buf[3]{0, 0, 0};

				buf[0] = key[(i << 1) + 0];
				buf[1] = key[(i << 1) + 1];
				buf[2] = 0;

				key_arr[i] = std::strtol(buf, nullptr, 16);
			}
		} catch (const std::invalid_argument& e) {
			std::cerr << "Error parsing key: " << e.what() << '\n';
			return 1;
		}
	}

	int algo = GCRY_CIPHER_AES256;
	if (bits == 128) {
		algo = GCRY_CIPHER_AES128;
	}

	gcry_cipher_hd_t hd;
	try_gcry(gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_XTS, 0));
	try_gcry(gcry_cipher_setkey(hd, key_arr.get(), key_size));

	auto proccess = decrypt ? proccess_decrypt : proccess_encrypt;

	while (std::cin.good()) {
		char block_in[16];
		char block_out[16];

		std::cin.read(block_in, 16);
		size_t readed = std::cin.gcount();

		if (readed == 0)
			break;

		if (readed != 16) {
			std::cerr << "last block size: " << readed << '\n';
			std::memset(block_in + readed, 0, 16 - readed);
		}

		try_gcry(proccess(hd, block_in, block_out));

		if (std::cin.peek() == EOF) {
			if (last != 0) {
				std::cout.write(block_out, last);
				break;
			}
		}

		std::cout.write(block_out, 16);
	}

	gcry_cipher_close(hd);

	return 0;
}

// vim: set ts=4 sw=4 :