-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwingman.py
executable file
·288 lines (212 loc) · 7.12 KB
/
wingman.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
import hmac
from os import environ, mkdir, urandom
from sys import argv, stdin, stderr, exit
from pathlib import Path
from functools import partial
from hashlib import sha256
from struct import pack
eprint = partial(print, file=stderr)
# We depend on argon2 for safe hashing
# We also need AES
try:
from argon2 import argon2_hash
from Crypto.Cipher import AES
except ImportError as exc:
eprint("couldn't find cryto libraries - {!s}", exc)
VERSION = b"\x00"
class WingmanException(Exception):
pass
class InvalidUserKey(WingmanException, ValueError):
pass
class InvalidArgument(WingmanException):
pass
class UnrecognisedVersion(WingmanException):
pass
class InvalidFileHash(WingmanException, ValueError):
pass
class AmbiguousFileHash(WingmanException):
pass
def encrypt(enc_key, file_data):
# Compute the padding length
# Make the file_data a multiple of 32 bytes
# long.
pad_length = 32 - (len(file_data) % 32)
file_data = urandom(pad_length) + file_data
meta_data = VERSION + pack("B", pad_length) + urandom(14)
key = urandom(32)
iv = urandom(16)
# Encrypt the header
aes = AES.new(enc_key, AES.MODE_ECB)
enc_header = meta_data + aes.encrypt(key + iv)
# Okay - encyrpt the data
aes = AES.new(key, AES.MODE_CBC, iv)
enc_data = aes.encrypt(file_data)
return enc_header + enc_data
def decrypt(enc_key, enc_data):
meta_data = enc_data[:16]
if meta_data[0:1] != VERSION:
err_str = "unsupported version {!s}".format(VERSION)
raise UnrecognisedVersion(err_str)
pad_length = meta_data[1]
# Grab the key + iv
enc_keys = enc_data[16:64]
aes = AES.new(enc_key, AES.MODE_ECB)
keys = aes.decrypt(enc_keys)
key, iv = keys[:32], keys[32:48]
# Decrypt the data
aes = AES.new(key, AES.MODE_CBC, iv)
data = aes.decrypt(enc_data[64:])
# return the decrypted data with the padding
# stripped.
return data[pad_length:]
def cmd_ls(enc_dir, *args):
# Call the ls implementation
def is_encfile(fname):
if len(fname) != 64:
return False
for c in fname.lower():
if c not in "abcdef0123456789":
return False
return True
iterator = map(lambda x: x.name, enc_dir.glob('*'))
return "\n".join(filter(is_encfile, iterator))
def cmd_add(user_key, enc_dir, *args):
# Args should be of length 1
# The file which we wish to encrypt
if len(args) != 1 or not isinstance(args[0], str):
err_str = "add command requires one argument, a str filepath"
raise InvalidArgument(err_str)
# We support the magic values '-' and '-0' for stdin
if args[0] == '-' or args[0] == '-0':
file_data = stdin.buffer.read()
else:
file = Path(args[0]).expanduser().absolute()
assert file.exists(), "{!s} does not exist".format(file)
file_data = open(file, 'rb').read()
# Okay - read the salt
salt = open(enc_dir / "salt", 'rb').read()
# Generate the hash
ahash = argon2_hash(user_key, salt)
# enc_key is used to encrypt the file header
enc_key = ahash[:32]
# file_salt to used to salt the hashes
file_salt = ahash[32:64]
# Encrypt the data
enc_data = encrypt(enc_key, file_data)
# Generate the file_hash
file_hash = hmac.new(
key=file_salt,
msg=file_data,
digestmod=sha256,
).digest().hex()
with open(enc_dir / file_hash, 'wb') as file:
file.write(enc_data)
return file_hash
def cmd_cat(user_key, enc_dir, *args):
# Args should be of length 1
# and should be a lowercase hexstring
if len(args) != 1 or not isinstance(args[0], str):
err_str = "cat command requires one argument, a str hexstring"
raise InvalidArgument(err_str)
if len(args[0]) < 6 or len(args[0]) > 64:
err_str = "cat command requires unique id between 6 and 64 chars"
raise InvalidArgument(err_str)
# Grab the filenames
filenames = cmd_ls(enc_dir).split("\n")
# How many filenames start with args[0]?
filenames = list(
filter(lambda x: x.startswith(args[0]), filenames)
)
if len(filenames) == 0:
err_str = "{!s} does not exist"
raise InvalidFileHash(err_str)
elif len(filenames) >= 2:
err_str = "ambiguous file hash"
raise AmbiguousFileHash(err_str)
# Read the file
filename = filenames[0]
enc_data = open(enc_dir / filename, 'rb').read()
# Okay - read the salt
salt = open(enc_dir / "salt", 'rb').read()
# Generate the hash
ahash = argon2_hash(user_key, salt)
# enc_key is used to encrypt the file header
enc_key = ahash[:32]
# Decrypt the data
return decrypt(enc_key, enc_data)
def validate_user_key(user_key):
"""
validate_user_key checks the key is a hexstring
of length 64 (or 32 bytes).
"""
if not isinstance(user_key, str):
raise TypeError("user_key should be a str")
err_str = "user_key must be a hexstring of length 64"
if len(user_key) != 64:
raise InvalidUserKey(err_str)
for c in user_key.lower():
if c not in "abcdef0123456789":
raise InvalidUserKey(err_str)
def output(out):
"""
output prints the output to stdout.
"""
if not out:
# Don't bother if out is empty
return
# Attempt to UTF-8 decode - assuming
# the data is text. This displays newlines
# correctly.
if isinstance(out, (bytes, bytearray)):
try:
out = str(out, encoding='utf8')
except UnicodeDecodeError:
pass
end = ""
if isinstance(out, str) and out[-1] != "\n":
end = "\n"
print(out, end=end)
def wingman(user_key, enc_dir, cmd, *args):
# Sanity check the user_key
validate_user_key(user_key)
user_key = bytes.fromhex(user_key)
# Check + create the enc_dir
enc_dir = Path(enc_dir).expanduser().absolute()
if not enc_dir.exists():
mkdir(enc_dir)
if not (enc_dir / "salt").exists():
with open(enc_dir / "salt", 'wb') as file:
file.write(urandom(32))
else:
# TODO: Sanity check the salt
pass
# Now we need to switch on the cmd
try:
if cmd == "ls":
output(cmd_ls(enc_dir, *args))
elif cmd == "add":
output(cmd_add(user_key, enc_dir, *args))
elif cmd == "cat":
output(cmd_cat(user_key, enc_dir, *args))
except WingmanException as exc:
# TODO: How to handle this error?
eprint("invalid argument - {!s}".format(exc))
if __name__ == '__main__':
# Grab the key
user_key = environ.get("WINGMAN_USER_KEY")
if not user_key:
eprint("WINGMAN_USER_KEY environment variable not set")
exit(1)
# Grab the directory
enc_dir = environ.get("WINGMAN_ENC_DIR", "/tmp/wingman")
if len(argv) < 2:
eprint("argument required - try -h for help")
exit(1)
try:
wingman(user_key, enc_dir, argv[1], *argv[2:])
except Exception as exc:
# Something went wrong
eprint("something went wrong - {!s}".format(exc))
exit(1)
__all__ = ['wingman']