key.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. from binascii import hexlify, a2b_base64, b2a_base64
  2. from getpass import getpass
  3. import os
  4. import msgpack
  5. import textwrap
  6. import hmac
  7. from hashlib import sha256, sha512
  8. import zlib
  9. try:
  10. import lzma # python >= 3.3
  11. except ImportError:
  12. try:
  13. from backports import lzma # backports.lzma from pypi
  14. except ImportError:
  15. lzma = None
  16. from attic.crypto import pbkdf2_sha256, get_random_bytes, AES, bytes_to_long, long_to_bytes, bytes_to_int, num_aes_blocks
  17. from attic.helpers import IntegrityError, get_keys_dir, Error
  18. PREFIX = b'\0' * 8
  19. class UnsupportedPayloadError(Error):
  20. """Unsupported payload type {}. A newer version is required to access this repository.
  21. """
  22. class sha512_256(object): # note: can't subclass sha512
  23. """sha512, but digest truncated to 256bit - faster than sha256 on 64bit platforms"""
  24. digestsize = digest_size = 32
  25. block_size = 64
  26. def __init__(self, data=None):
  27. self.name = 'sha512-256'
  28. self._h = sha512()
  29. if data:
  30. self.update(data)
  31. def update(self, data):
  32. self._h.update(data)
  33. def digest(self):
  34. return self._h.digest()[:self.digest_size]
  35. def hexdigest(self):
  36. return self._h.hexdigest()[:self.digest_size * 2]
  37. def copy(self):
  38. new = sha512_256.__new__(sha512_256)
  39. new._h = self._h.copy()
  40. return new
  41. class HMAC(hmac.HMAC):
  42. """Workaround a bug in Python < 3.4 Where HMAC does not accept memoryviews
  43. """
  44. def update(self, msg):
  45. self.inner.update(msg)
  46. class SHA256(object): # note: can't subclass sha256
  47. TYPE = 0x00
  48. def __init__(self, key, data=b''):
  49. # signature is like for a MAC, we ignore the key as this is a simple hash
  50. if key is not None:
  51. raise Exception("use a HMAC if you have a key")
  52. self.h = sha256(data)
  53. def update(self, data):
  54. self.h.update(data)
  55. def digest(self):
  56. return self.h.digest()
  57. def hexdigest(self):
  58. return self.h.hexdigest()
  59. class SHA512_256(sha512_256):
  60. """sha512, but digest truncated to 256bit - faster than sha256 on 64bit platforms"""
  61. TYPE = 0x01
  62. def __init__(self, key, data):
  63. # signature is like for a MAC, we ignore the key as this is a simple hash
  64. if key is not None:
  65. raise Exception("use a HMAC if you have a key")
  66. super().__init__(data)
  67. HASH_DEFAULT = SHA256.TYPE
  68. class HMAC_SHA256(HMAC):
  69. TYPE = 0x02
  70. def __init__(self, key, data):
  71. if key is None:
  72. raise Exception("do not use HMAC if you don't have a key")
  73. super().__init__(key, data, sha256)
  74. class HMAC_SHA512_256(HMAC):
  75. TYPE = 0x03
  76. def __init__(self, key, data):
  77. if key is None:
  78. raise Exception("do not use HMAC if you don't have a key")
  79. super().__init__(key, data, sha512_256)
  80. MAC_DEFAULT = HMAC_SHA256.TYPE
  81. class ZlibCompressor(object): # uses 0..9 in the mapping
  82. TYPE = 0
  83. LEVELS = range(10)
  84. def compress(self, data):
  85. level = self.TYPE - ZlibCompressor.TYPE
  86. return zlib.compress(data, level)
  87. def decompress(self, data):
  88. return zlib.decompress(data)
  89. class LzmaCompressor(object): # uses 10..19 in the mapping
  90. TYPE = 10
  91. PRESETS = range(10)
  92. def __init__(self):
  93. if lzma is None:
  94. raise NotImplemented("lzma compression needs Python >= 3.3 or backports.lzma from PyPi")
  95. def compress(self, data):
  96. preset = self.TYPE - LzmaCompressor.TYPE
  97. return lzma.compress(data, preset=preset)
  98. def decompress(self, data):
  99. return lzma.decompress(data)
  100. COMPR_DEFAULT = ZlibCompressor.TYPE + 6 # zlib level 6
  101. class KeyBase(object):
  102. TYPE = 0x00 # override in derived classes
  103. def __init__(self, compressor, maccer):
  104. self.compressor = compressor()
  105. self.maccer = maccer
  106. def id_hash(self, data):
  107. """Return HMAC hash using the "id" HMAC key
  108. """
  109. def encrypt(self, data):
  110. pass
  111. def decrypt(self, id, data):
  112. pass
  113. class PlaintextKey(KeyBase):
  114. TYPE = 0x02
  115. chunk_seed = 0
  116. @classmethod
  117. def create(cls, repository, args):
  118. print('Encryption NOT enabled.\nUse the "--encryption=passphrase|keyfile" to enable encryption.')
  119. compressor = compressor_creator(args)
  120. maccer = maccer_creator(args, cls)
  121. return cls(compressor, maccer)
  122. @classmethod
  123. def detect(cls, repository, manifest_data):
  124. offset, compressor, crypter, maccer = parser(manifest_data)
  125. return cls(compressor, maccer)
  126. def id_hash(self, data):
  127. return self.maccer(None, data).digest()
  128. def encrypt(self, data):
  129. header = make_header(compr_type=self.compressor.TYPE, crypt_type=self.TYPE, mac_type=self.maccer.TYPE)
  130. return b''.join([header, self.compressor.compress(data)])
  131. def decrypt(self, id, data):
  132. offset, compressor, crypter, maccer = parser(data)
  133. assert isinstance(self, crypter)
  134. assert self.maccer is maccer
  135. data = self.compressor.decompress(memoryview(data)[offset:])
  136. if id and self.id_hash(data) != id:
  137. raise IntegrityError('Chunk id verification failed')
  138. return data
  139. class AESKeyBase(KeyBase):
  140. """Common base class shared by KeyfileKey and PassphraseKey
  141. Chunks are encrypted using 256bit AES in Counter Mode (CTR)
  142. Payload layout: HEADER(4) + HMAC(32) + NONCE(8) + CIPHERTEXT
  143. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  144. in the payload, the first 8 bytes are always zeros. This does not
  145. affect security but limits the maximum repository capacity to
  146. only 295 exabytes!
  147. """
  148. PAYLOAD_OVERHEAD = 4 + 32 + 8 # HEADER + HMAC + NONCE
  149. def id_hash(self, data):
  150. """Return HMAC hash using the "id" HMAC key
  151. """
  152. return self.maccer(self.id_key, data).digest()
  153. def encrypt(self, data):
  154. data = self.compressor.compress(data)
  155. self.enc_cipher.reset()
  156. data = b''.join((self.enc_cipher.iv[8:], self.enc_cipher.encrypt(data)))
  157. hmac = self.maccer(self.enc_hmac_key, data).digest()
  158. header = make_header(compr_type=self.compressor.TYPE, crypt_type=self.TYPE, mac_type=self.maccer.TYPE)
  159. return b''.join((header, hmac, data))
  160. def decrypt(self, id, data):
  161. offset, compressor, crypter, maccer = parser(data)
  162. assert isinstance(self, crypter)
  163. assert self.maccer is maccer
  164. hmac = memoryview(data)[offset:offset+32]
  165. computed_hmac = memoryview(self.maccer(self.enc_hmac_key, memoryview(data)[offset+32:]).digest())
  166. if computed_hmac != hmac:
  167. raise IntegrityError('Encryption envelope checksum mismatch')
  168. self.dec_cipher.reset(iv=PREFIX + data[offset+32:offset+40])
  169. data = self.compressor.decompress(self.dec_cipher.decrypt(data[offset+40:])) # should use memoryview
  170. if id and self.id_hash(data) != id:
  171. raise IntegrityError('Chunk id verification failed')
  172. return data
  173. def extract_nonce(self, payload):
  174. offset, compressor, crypter, maccer = parser(payload)
  175. assert isinstance(self, crypter)
  176. nonce = bytes_to_long(payload[offset+32:offset+40])
  177. return nonce
  178. def init_from_random_data(self, data):
  179. self.enc_key = data[0:32]
  180. self.enc_hmac_key = data[32:64]
  181. self.id_key = data[64:96]
  182. self.chunk_seed = bytes_to_int(data[96:100])
  183. # Convert to signed int32
  184. if self.chunk_seed & 0x80000000:
  185. self.chunk_seed = self.chunk_seed - 0xffffffff - 1
  186. def init_ciphers(self, enc_iv=b''):
  187. self.enc_cipher = AES(self.enc_key, enc_iv)
  188. self.dec_cipher = AES(self.enc_key)
  189. class PassphraseKey(AESKeyBase):
  190. TYPE = 0x01
  191. iterations = 100000
  192. @classmethod
  193. def create(cls, repository, args):
  194. compressor = compressor_creator(args)
  195. maccer = maccer_creator(args, cls)
  196. key = cls(compressor, maccer)
  197. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  198. if passphrase is not None:
  199. passphrase2 = passphrase
  200. else:
  201. passphrase, passphrase2 = 1, 2
  202. while passphrase != passphrase2:
  203. passphrase = getpass('Enter passphrase: ')
  204. if not passphrase:
  205. print('Passphrase must not be blank')
  206. continue
  207. passphrase2 = getpass('Enter same passphrase again: ')
  208. if passphrase != passphrase2:
  209. print('Passphrases do not match')
  210. key.init(repository, passphrase)
  211. if passphrase:
  212. print('Remember your passphrase. Your data will be inaccessible without it.')
  213. return key
  214. @classmethod
  215. def detect(cls, repository, manifest_data):
  216. prompt = 'Enter passphrase for %s: ' % repository._location.orig
  217. offset, compressor, crypter, maccer = parser(manifest_data)
  218. key = cls(compressor, maccer)
  219. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  220. if passphrase is None:
  221. passphrase = getpass(prompt)
  222. while True:
  223. key.init(repository, passphrase)
  224. try:
  225. key.decrypt(None, manifest_data)
  226. num_blocks = num_aes_blocks(len(manifest_data) - offset - 40)
  227. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  228. return key
  229. except IntegrityError:
  230. passphrase = getpass(prompt)
  231. def init(self, repository, passphrase):
  232. self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
  233. self.init_ciphers()
  234. class KeyfileKey(AESKeyBase):
  235. FILE_ID = 'ATTIC KEY'
  236. TYPE = 0x00
  237. @classmethod
  238. def detect(cls, repository, manifest_data):
  239. offset, compressor, crypter, maccer = parser(manifest_data)
  240. key = cls(compressor, maccer)
  241. path = cls.find_key_file(repository)
  242. prompt = 'Enter passphrase for key file %s: ' % path
  243. passphrase = os.environ.get('ATTIC_PASSPHRASE', '')
  244. while not key.load(path, passphrase):
  245. passphrase = getpass(prompt)
  246. num_blocks = num_aes_blocks(len(manifest_data) - offset - 40)
  247. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  248. return key
  249. @classmethod
  250. def find_key_file(cls, repository):
  251. id = hexlify(repository.id).decode('ascii')
  252. keys_dir = get_keys_dir()
  253. for name in os.listdir(keys_dir):
  254. filename = os.path.join(keys_dir, name)
  255. with open(filename, 'r') as fd:
  256. line = fd.readline().strip()
  257. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  258. return filename
  259. raise Exception('Key file for repository with ID %s not found' % id)
  260. def load(self, filename, passphrase):
  261. with open(filename, 'r') as fd:
  262. cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
  263. data = self.decrypt_key_file(cdata, passphrase)
  264. if data:
  265. key = msgpack.unpackb(data)
  266. if key[b'version'] != 1:
  267. raise IntegrityError('Invalid key file header')
  268. self.repository_id = key[b'repository_id']
  269. self.enc_key = key[b'enc_key']
  270. self.enc_hmac_key = key[b'enc_hmac_key']
  271. self.id_key = key[b'id_key']
  272. self.chunk_seed = key[b'chunk_seed']
  273. self.path = filename
  274. return True
  275. def decrypt_key_file(self, data, passphrase):
  276. d = msgpack.unpackb(data)
  277. assert d[b'version'] == 1
  278. assert d[b'algorithm'] == b'sha256'
  279. key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
  280. data = AES(key).decrypt(d[b'data'])
  281. if HMAC(key, data, sha256).digest() != d[b'hash']:
  282. return None
  283. return data
  284. def encrypt_key_file(self, data, passphrase):
  285. salt = get_random_bytes(32)
  286. iterations = 100000
  287. key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
  288. hash = HMAC(key, data, sha256).digest()
  289. cdata = AES(key).encrypt(data)
  290. d = {
  291. 'version': 1,
  292. 'salt': salt,
  293. 'iterations': iterations,
  294. 'algorithm': 'sha256',
  295. 'hash': hash,
  296. 'data': cdata,
  297. }
  298. return msgpack.packb(d)
  299. def save(self, path, passphrase):
  300. key = {
  301. 'version': 1,
  302. 'repository_id': self.repository_id,
  303. 'enc_key': self.enc_key,
  304. 'enc_hmac_key': self.enc_hmac_key,
  305. 'id_key': self.id_key,
  306. 'chunk_seed': self.chunk_seed,
  307. }
  308. data = self.encrypt_key_file(msgpack.packb(key), passphrase)
  309. with open(path, 'w') as fd:
  310. fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
  311. fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
  312. fd.write('\n')
  313. self.path = path
  314. def change_passphrase(self):
  315. passphrase, passphrase2 = 1, 2
  316. while passphrase != passphrase2:
  317. passphrase = getpass('New passphrase: ')
  318. passphrase2 = getpass('Enter same passphrase again: ')
  319. if passphrase != passphrase2:
  320. print('Passphrases do not match')
  321. self.save(self.path, passphrase)
  322. print('Key file "%s" updated' % self.path)
  323. @classmethod
  324. def create(cls, repository, args):
  325. filename = args.repository.to_key_filename()
  326. path = filename
  327. i = 1
  328. while os.path.exists(path):
  329. i += 1
  330. path = filename + '.%d' % i
  331. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  332. if passphrase is not None:
  333. passphrase2 = passphrase
  334. else:
  335. passphrase, passphrase2 = 1, 2
  336. while passphrase != passphrase2:
  337. passphrase = getpass('Enter passphrase (empty for no passphrase):')
  338. passphrase2 = getpass('Enter same passphrase again: ')
  339. if passphrase != passphrase2:
  340. print('Passphrases do not match')
  341. compressor = compressor_creator(args)
  342. maccer = maccer_creator(args, cls)
  343. key = cls(compressor, maccer)
  344. key.repository_id = repository.id
  345. key.init_from_random_data(get_random_bytes(100))
  346. key.init_ciphers()
  347. key.save(path, passphrase)
  348. print('Key file "%s" created.' % key.path)
  349. print('Keep this file safe. Your data will be inaccessible without it.')
  350. return key
  351. # note: key 0 nicely maps to a zlib compressor with level 0 which means "no compression"
  352. compressor_mapping = {}
  353. for level in ZlibCompressor.LEVELS:
  354. compressor_mapping[ZlibCompressor.TYPE + level] = \
  355. type('ZlibCompressorLevel%d' % level, (ZlibCompressor, ), dict(TYPE=ZlibCompressor.TYPE + level))
  356. for preset in LzmaCompressor.PRESETS:
  357. compressor_mapping[LzmaCompressor.TYPE + preset] = \
  358. type('LzmaCompressorPreset%d' % preset, (LzmaCompressor, ), dict(TYPE=LzmaCompressor.TYPE + preset))
  359. crypter_mapping = {
  360. KeyfileKey.TYPE: KeyfileKey,
  361. PassphraseKey.TYPE: PassphraseKey,
  362. PlaintextKey.TYPE: PlaintextKey,
  363. }
  364. maccer_mapping = {
  365. # simple hashes, not MACs (but MAC-like class __init__ method signature):
  366. SHA256.TYPE: SHA256,
  367. SHA512_256.TYPE: SHA512_256,
  368. # MACs:
  369. HMAC_SHA256.TYPE: HMAC_SHA256,
  370. HMAC_SHA512_256.TYPE: HMAC_SHA512_256,
  371. }
  372. from collections import namedtuple
  373. Meta = namedtuple('Meta', 'compr_type, crypt_type, mac_type')
  374. def get_implementations(meta):
  375. try:
  376. compressor = compressor_mapping[meta.compr_type]
  377. crypter = crypter_mapping[meta.crypt_type]
  378. maccer = maccer_mapping[meta.mac_type]
  379. except KeyError:
  380. raise UnsupportedPayloadError("compr_type %x crypt_type %x mac_type %x" % (
  381. meta.compr_type, meta.crypt_type, meta.mac_type))
  382. return compressor, crypter, maccer
  383. def parser00(data): # legacy, hardcoded
  384. offset = 1
  385. meta = Meta(compr_type=6, crypt_type=KeyfileKey.TYPE, mac_type=HMAC_SHA256.TYPE)
  386. compressor, crypter, maccer = get_implementations(meta)
  387. return offset, compressor, crypter, maccer
  388. def parser01(data): # legacy, hardcoded
  389. offset = 1
  390. meta = Meta(compr_type=6, crypt_type=PassphraseKey.TYPE, mac_type=HMAC_SHA256.TYPE)
  391. compressor, crypter, maccer = get_implementations(meta)
  392. return offset, compressor, crypter, maccer
  393. def parser02(data): # legacy, hardcoded
  394. offset = 1
  395. meta = Meta(compr_type=6, crypt_type=PlaintextKey.TYPE, mac_type=SHA256.TYPE)
  396. compressor, crypter, maccer = get_implementations(meta)
  397. return offset, compressor, crypter, maccer
  398. def parser03(data): # new & flexible
  399. offset = 4
  400. compr_type, crypt_type, mac_type = data[1:offset]
  401. meta = Meta(compr_type=compr_type, crypt_type=crypt_type, mac_type=mac_type)
  402. compressor, crypter, maccer = get_implementations(meta)
  403. return offset, compressor, crypter, maccer
  404. def parser(data):
  405. parser_mapping = {
  406. 0x00: parser00,
  407. 0x01: parser01,
  408. 0x02: parser02,
  409. 0x03: parser03,
  410. }
  411. header_type = data[0]
  412. parser_func = parser_mapping[header_type]
  413. return parser_func(data)
  414. def key_factory(repository, manifest_data):
  415. offset, compressor, crypter, maccer = parser(manifest_data)
  416. return crypter.detect(repository, manifest_data)
  417. def make_header(compr_type, crypt_type, mac_type):
  418. # always create new-style 0x03 headers
  419. return bytes([0x03, compr_type, crypt_type, mac_type])
  420. def compressor_creator(args):
  421. # args == None is used by unit tests
  422. compression = COMPR_DEFAULT if args is None else args.compression
  423. compressor = compressor_mapping.get(compression)
  424. if compressor is None:
  425. raise NotImplementedError("no compression %d" % args.compression)
  426. return compressor
  427. def key_creator(repository, args):
  428. if args.encryption == 'keyfile':
  429. return KeyfileKey.create(repository, args)
  430. if args.encryption == 'passphrase':
  431. return PassphraseKey.create(repository, args)
  432. if args.encryption == 'none':
  433. return PlaintextKey.create(repository, args)
  434. raise NotImplemented("no encryption %s" % args.encryption)
  435. def maccer_creator(args, key_cls):
  436. # args == None is used by unit tests
  437. mac = None if args is None else args.mac
  438. if mac is None:
  439. if key_cls is PlaintextKey:
  440. mac = HASH_DEFAULT
  441. elif key_cls in (KeyfileKey, PassphraseKey):
  442. mac = MAC_DEFAULT
  443. else:
  444. raise NotImplementedError("unknown key class")
  445. maccer = maccer_mapping.get(mac)
  446. if maccer is None:
  447. raise NotImplementedError("no mac %d" % args.mac)
  448. return maccer