-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathomut.c
144 lines (121 loc) · 3.54 KB
/
omut.c
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 William Wennerström
#include "crypt.h"
#include <ctype.h>
#include <errno.h>
#include <gcrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
enum direction { ENCRYPT, DECRYPT };
#define EXIT_INVALID_CHECKSUM 2
#define EXIT_BAD_FILE 3
#define EXIT_FAIL_READ 4
#define EXIT_FAIL_WRITE 5
#define EXIT_NO_MEM 6
int gcry_init(void) {
gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
return 0;
}
void print_crypto_material(char *type, unsigned char *material, int len) {
fprintf(stderr, "%s: ", type);
for (int i = 0; i < len; i++) {
fprintf(stderr, "%02x", material[i]);
}
fprintf(stderr, "\n");
}
int main(int argc, char **argv) {
int opt;
int direction = ENCRYPT;
char *output_path = NULL;
while ((opt = getopt(argc, argv, ":do:")) != -1) {
switch (opt) {
case 'd':
direction = DECRYPT;
continue;
case 'o':
output_path = optarg;
continue;
case ':':
fprintf(stderr, "Option '-%c' requires an argument\n", optopt);
break;
case '?':
if (isprint(optopt)) {
fprintf(stderr, "Unknown option '-%c'\n", optopt);
} else {
fprintf(stderr, "Unknown option character '\\x%x'\n", optopt);
}
break;
default:
abort();
}
exit(EXIT_FAILURE);
}
if (optind + 1 != argc) {
fprintf(stderr, "Usage: %s [-d] [-o FILE] URL\n", argv[0]);
exit(EXIT_FAILURE);
}
gcry_init();
unsigned char nonce[AES256_GCM_NONCE_LENGTH];
unsigned char *key = gcry_malloc_secure(AES256_GCM_KEY_LENGTH);
if (key == NULL) {
fputs("Out of memory\n", stderr);
exit(EXIT_NO_MEM);
}
STREAM *in_stream;
char *raw_url = strdup(argv[optind]);
int crypt_res = GPG_ERR_NO_ERROR;
int exit_status = EXIT_SUCCESS;
char *parsed_url;
parsed_url = parse_aesgcm_url(raw_url, nonce, AES256_GCM_NONCE_LENGTH, key,
AES256_GCM_KEY_LENGTH);
if (parsed_url == NULL && errno == ENOMEM) {
exit_status = EXIT_NO_MEM;
fputs("Out of memory\n", stderr);
goto out;
}
if (parsed_url == NULL) {
key = gcry_random_bytes_secure(AES256_GCM_KEY_LENGTH,
GCRY_VERY_STRONG_RANDOM);
gcry_create_nonce(nonce, AES256_GCM_NONCE_LENGTH);
in_stream = stream_open(raw_url);
} else {
in_stream = stream_open(parsed_url);
}
free(parsed_url);
free(raw_url);
if (in_stream == NULL) {
exit_status = EXIT_FAIL_READ;
fputs("Failed to read input\n", stderr);
goto out;
}
FILE *out_file = stdout;
if (output_path != NULL) {
out_file = fopen(output_path, "w");
if (out_file == NULL) {
exit_status = EXIT_FAIL_WRITE;
fprintf(stderr, "Failed to open '%s' for writing\n", output_path);
goto out;
}
}
if (direction == ENCRYPT) {
crypt_res = aes256gcm_encrypt(in_stream, out_file, key, nonce);
} else /* direction == DECRYPT */ {
crypt_res = aes256gcm_decrypt(in_stream, out_file, key, nonce);
}
if (crypt_res == GPG_ERR_CHECKSUM) {
fprintf(stderr, "Invalid checksum (#%d)\n", crypt_res);
exit_status = EXIT_INVALID_CHECKSUM;
} else if (crypt_res != GPG_ERR_NO_ERROR) {
fprintf(stderr, "Bad file (#%d)\n", crypt_res);
exit_status = EXIT_BAD_FILE;
}
print_crypto_material("Key", key, AES256_GCM_KEY_LENGTH);
print_crypto_material("Nonce", nonce, AES256_GCM_NONCE_LENGTH);
out:
gcry_free(key);
exit(exit_status);
}