key.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. from binascii import hexlify, a2b_base64, b2a_base64
  2. from getpass import getpass
  3. import os
  4. import msgpack
  5. import textwrap
  6. from collections import namedtuple
  7. import hmac
  8. from hashlib import sha1, sha256, sha512
  9. import zlib
  10. try:
  11. import lzma # python >= 3.3
  12. except ImportError:
  13. try:
  14. from backports import lzma # backports.lzma from pypi
  15. except ImportError:
  16. lzma = None
  17. try:
  18. import blosc
  19. except ImportError:
  20. blosc = None
  21. from attic.crypto import pbkdf2_sha256, get_random_bytes, AES, AES_CTR_MODE, AES_GCM_MODE, \
  22. bytes_to_int, increment_iv
  23. from attic.helpers import IntegrityError, get_keys_dir, Error
  24. # TODO fix cyclic import:
  25. #from attic.archive import CHUNK_MAX
  26. CHUNK_MAX = 10 * 1024 * 1024
  27. Meta = namedtuple('Meta', 'compr_type, key_type, mac_type, cipher_type, iv, legacy')
  28. class UnsupportedPayloadError(Error):
  29. """Unsupported payload type {}. A newer version is required to access this repository.
  30. """
  31. class sha512_256(object): # note: can't subclass sha512
  32. """sha512, but digest truncated to 256bit - faster than sha256 on 64bit platforms"""
  33. digestsize = digest_size = 32
  34. block_size = 64
  35. def __init__(self, data=None):
  36. self.name = 'sha512-256'
  37. self._h = sha512()
  38. if data:
  39. self.update(data)
  40. def update(self, data):
  41. self._h.update(data)
  42. def digest(self):
  43. return self._h.digest()[:self.digest_size]
  44. def hexdigest(self):
  45. return self._h.hexdigest()[:self.digest_size * 2]
  46. def copy(self):
  47. new = sha512_256.__new__(sha512_256)
  48. new._h = self._h.copy()
  49. return new
  50. # HASH / MAC stuff below all has a mac-like interface, so it can be used in the same way.
  51. # special case: hashes do not use keys (and thus, do not sign/authenticate)
  52. class HASH: # note: we can't subclass sha1/sha256/sha512
  53. TYPE = 0 # override in subclass
  54. digest_size = 0 # override in subclass
  55. hash_func = None # override in subclass
  56. def __init__(self, key, data=b''):
  57. # signature is like for a MAC, we ignore the key as this is a simple hash
  58. if key is not None:
  59. raise Exception("use a HMAC if you have a key")
  60. self.h = self.hash_func(data)
  61. def update(self, data):
  62. self.h.update(data)
  63. def digest(self):
  64. return self.h.digest()
  65. def hexdigest(self):
  66. return self.h.hexdigest()
  67. class SHA256(HASH):
  68. TYPE = 0
  69. digest_size = 32
  70. hash_func = sha256
  71. class SHA512_256(HASH):
  72. TYPE = 1
  73. digest_size = 32
  74. hash_func = sha512_256
  75. class GHASH:
  76. TYPE = 2
  77. digest_size = 16
  78. def __init__(self, key, data=b''):
  79. # signature is like for a MAC, we ignore the key as this is a simple hash
  80. if key is not None:
  81. raise Exception("use a MAC if you have a key")
  82. self.mac_cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=b'\0' * 32, iv=b'\0' * 16)
  83. if data:
  84. self.update(data)
  85. def update(self, data):
  86. # GMAC = aes-gcm with all data as AAD, no data as to-be-encrypted data
  87. self.mac_cipher.add(bytes(data))
  88. def digest(self):
  89. hash, _ = self.mac_cipher.compute_mac_and_encrypt(b'')
  90. return hash
  91. class SHA1(HASH):
  92. TYPE = 3
  93. digest_size = 20
  94. hash_func = sha1
  95. class SHA512(HASH):
  96. TYPE = 4
  97. digest_size = 64
  98. hash_func = sha512
  99. class HMAC(hmac.HMAC):
  100. TYPE = 0 # override in subclass
  101. digest_size = 0 # override in subclass
  102. hash_func = None # override in subclass
  103. def __init__(self, key, data):
  104. if key is None:
  105. raise Exception("do not use HMAC if you don't have a key")
  106. super().__init__(key, data, self.hash_func)
  107. def update(self, msg):
  108. # Workaround a bug in Python < 3.4 Where HMAC does not accept memoryviews
  109. self.inner.update(msg)
  110. class HMAC_SHA256(HMAC):
  111. TYPE = 10
  112. digest_size = 32
  113. hash_func = sha256
  114. class HMAC_SHA512_256(HMAC):
  115. TYPE = 11
  116. digest_size = 32
  117. hash_func = sha512_256
  118. class HMAC_SHA1(HMAC):
  119. TYPE = 13
  120. digest_size = 20
  121. hash_func = sha1
  122. class HMAC_SHA512(HMAC):
  123. TYPE = 14
  124. digest_size = 64
  125. hash_func = sha512
  126. class GMAC(GHASH):
  127. TYPE = 20
  128. digest_size = 16
  129. def __init__(self, key, data=b''):
  130. if key is None:
  131. raise Exception("do not use GMAC if you don't have a key")
  132. self.mac_cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=key, iv=b'\0' * 16)
  133. if data:
  134. self.update(data)
  135. # defaults are optimized for speed on modern CPUs with AES hw support
  136. HASH_DEFAULT = GHASH.TYPE
  137. MAC_DEFAULT = GMAC.TYPE
  138. # compressor classes, all same interface
  139. class NullCompressor(object): # uses 0 in the mapping
  140. TYPE = 0
  141. def compress(self, data):
  142. return bytes(data)
  143. def decompress(self, data):
  144. return bytes(data)
  145. class ZlibCompressor(object): # uses 1..9 in the mapping
  146. TYPE = 0
  147. LEVELS = range(10)
  148. def compress(self, data):
  149. level = self.TYPE - ZlibCompressor.TYPE
  150. return zlib.compress(data, level)
  151. def decompress(self, data):
  152. return zlib.decompress(data)
  153. class LzmaCompressor(object): # uses 10..19 in the mapping
  154. TYPE = 10
  155. PRESETS = range(10)
  156. def __init__(self):
  157. if lzma is None:
  158. raise NotImplemented("lzma compression needs Python >= 3.3 or backports.lzma from PyPi")
  159. def compress(self, data):
  160. preset = self.TYPE - LzmaCompressor.TYPE
  161. return lzma.compress(data, preset=preset)
  162. def decompress(self, data):
  163. return lzma.decompress(data)
  164. class BLOSCCompressor(object):
  165. TYPE = 0 # override in subclass
  166. LEVELS = range(10)
  167. CNAME = '' # override in subclass
  168. def __init__(self):
  169. if blosc is None:
  170. raise NotImplemented("%s compression needs blosc from PyPi" % self.CNAME)
  171. if self.CNAME not in blosc.compressor_list():
  172. raise NotImplemented("%s compression is not supported by blosc" % self.CNAME)
  173. blosc.set_blocksize(16384) # 16kiB is the minimum, so 64kiB are enough for 4 threads
  174. def _get_level(self):
  175. raise NotImplemented
  176. def compress(self, data):
  177. return blosc.compress(bytes(data), 1, cname=self.CNAME, clevel=self._get_level())
  178. def decompress(self, data):
  179. return blosc.decompress(data)
  180. class LZ4Compressor(BLOSCCompressor):
  181. TYPE = 20
  182. CNAME = 'lz4'
  183. def _get_level(self):
  184. return self.TYPE - LZ4Compressor.TYPE
  185. class LZ4HCCompressor(BLOSCCompressor):
  186. TYPE = 30
  187. CNAME = 'lz4hc'
  188. def _get_level(self):
  189. return self.TYPE - LZ4HCCompressor.TYPE
  190. class BLOSCLZCompressor(BLOSCCompressor):
  191. TYPE = 40
  192. CNAME = 'blosclz'
  193. def _get_level(self):
  194. return self.TYPE - BLOSCLZCompressor.TYPE
  195. class SnappyCompressor(BLOSCCompressor):
  196. TYPE = 50
  197. CNAME = 'snappy'
  198. def _get_level(self):
  199. return self.TYPE - SnappyCompressor.TYPE
  200. class BLOSCZlibCompressor(BLOSCCompressor):
  201. TYPE = 60
  202. CNAME = 'zlib'
  203. def _get_level(self):
  204. return self.TYPE - BLOSCZlibCompressor.TYPE
  205. # default is optimized for speed
  206. COMPR_DEFAULT = NullCompressor.TYPE # no compression
  207. # ciphers - AEAD (authenticated encryption with assoc. data) style interface
  208. # special case: PLAIN dummy does not encrypt / authenticate
  209. class PLAIN:
  210. TYPE = 0
  211. enc_iv = None # dummy
  212. def __init__(self, **kw):
  213. pass
  214. def compute_mac_and_encrypt(self, meta, data):
  215. return None, data
  216. def check_mac_and_decrypt(self, mac, meta, data):
  217. return data
  218. def get_aad(meta):
  219. """get additional authenticated data for AEAD ciphers"""
  220. if meta.legacy:
  221. # legacy format computed the mac over (iv_last8 + data)
  222. return meta.iv[8:]
  223. else:
  224. return msgpack.packb(meta)
  225. class AES_CTR_HMAC:
  226. TYPE = 1
  227. def __init__(self, enc_key=b'\0' * 32, enc_iv=b'\0' * 16, enc_hmac_key=b'\0' * 32, **kw):
  228. self.hmac_key = enc_hmac_key
  229. self.enc_iv = enc_iv
  230. self.enc_cipher = AES(mode=AES_CTR_MODE, is_encrypt=True, key=enc_key, iv=enc_iv)
  231. self.dec_cipher = AES(mode=AES_CTR_MODE, is_encrypt=False, key=enc_key)
  232. def compute_mac_and_encrypt(self, meta, data):
  233. self.enc_cipher.reset(iv=meta.iv)
  234. _, data = self.enc_cipher.compute_mac_and_encrypt(data)
  235. self.enc_iv = increment_iv(meta.iv, len(data))
  236. aad = get_aad(meta)
  237. mac = HMAC(self.hmac_key, aad + data, sha256).digest() # XXX mac / hash flexibility
  238. return mac, data
  239. def check_mac_and_decrypt(self, mac, meta, data):
  240. aad = get_aad(meta)
  241. if HMAC(self.hmac_key, aad + data, sha256).digest() != mac:
  242. raise IntegrityError('Encryption envelope checksum mismatch')
  243. self.dec_cipher.reset(iv=meta.iv)
  244. data = self.dec_cipher.check_mac_and_decrypt(None, data)
  245. return data
  246. class AES_GCM:
  247. TYPE = 2
  248. def __init__(self, enc_key=b'\0' * 32, enc_iv=b'\0' * 16, **kw):
  249. # note: hmac_key is not used for aes-gcm, it does aes+gmac in 1 pass
  250. self.enc_iv = enc_iv
  251. self.enc_cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=enc_key, iv=enc_iv)
  252. self.dec_cipher = AES(mode=AES_GCM_MODE, is_encrypt=False, key=enc_key)
  253. def compute_mac_and_encrypt(self, meta, data):
  254. self.enc_cipher.reset(iv=meta.iv)
  255. aad = get_aad(meta)
  256. self.enc_cipher.add(aad)
  257. mac, data = self.enc_cipher.compute_mac_and_encrypt(data)
  258. self.enc_iv = increment_iv(meta.iv, len(data))
  259. return mac, data
  260. def check_mac_and_decrypt(self, mac, meta, data):
  261. self.dec_cipher.reset(iv=meta.iv)
  262. aad = get_aad(meta)
  263. self.dec_cipher.add(aad)
  264. try:
  265. data = self.dec_cipher.check_mac_and_decrypt(mac, data)
  266. except Exception:
  267. raise IntegrityError('Encryption envelope checksum mismatch')
  268. return data
  269. # cipher default is optimized for speed on modern CPUs with AES hw support
  270. PLAIN_DEFAULT = PLAIN.TYPE
  271. CIPHER_DEFAULT = AES_GCM.TYPE
  272. # misc. types of keys
  273. # special case: no keys (thus: no encryption, no signing/authentication)
  274. class KeyBase(object):
  275. TYPE = 0x00 # override in derived classes
  276. def __init__(self, compressor_cls, maccer_cls, cipher_cls):
  277. self.compressor = compressor_cls()
  278. self.maccer_cls = maccer_cls # hasher/maccer used by id_hash
  279. self.cipher_cls = cipher_cls # plaintext dummy or AEAD cipher
  280. self.cipher = cipher_cls()
  281. self.id_key = None
  282. def id_hash(self, data):
  283. """Return a HASH (no id_key) or a MAC (using the "id_key" key)
  284. XXX do we need a cryptographic hash function here or is a keyed hash
  285. function like GMAC / GHASH good enough? See NIST SP 800-38D.
  286. IMPORTANT: in 1 repo, there should be only 1 kind of id_hash, otherwise
  287. data hashed/maced with one id_hash might result in same ID as already
  288. exists in the repo for other data created with another id_hash method.
  289. somehow unlikely considering 128 or 256bits, but still.
  290. """
  291. return self.maccer_cls(self.id_key, data).digest()
  292. def encrypt(self, data):
  293. data = self.compressor.compress(data)
  294. meta = Meta(compr_type=self.compressor.TYPE, key_type=self.TYPE,
  295. mac_type=self.maccer_cls.TYPE, cipher_type=self.cipher.TYPE,
  296. iv=self.cipher.enc_iv, legacy=False)
  297. mac, data = self.cipher.compute_mac_and_encrypt(meta, data)
  298. return generate(mac, meta, data)
  299. def decrypt(self, id, data):
  300. mac, meta, data = parser(data)
  301. compressor, keyer, maccer, cipher = get_implementations(meta)
  302. assert isinstance(self, keyer)
  303. assert self.maccer_cls is maccer
  304. assert self.cipher_cls is cipher
  305. data = self.cipher.check_mac_and_decrypt(mac, meta, data)
  306. data = self.compressor.decompress(data)
  307. if id and self.id_hash(data) != id:
  308. raise IntegrityError('Chunk id verification failed')
  309. return data
  310. class PlaintextKey(KeyBase):
  311. TYPE = 0x02
  312. chunk_seed = 0
  313. @classmethod
  314. def create(cls, repository, args):
  315. print('Encryption NOT enabled.\nUse the "--encryption=passphrase|keyfile" to enable encryption.')
  316. compressor = compressor_creator(args)
  317. maccer = maccer_creator(args, cls)
  318. cipher = cipher_creator(args, cls)
  319. return cls(compressor, maccer, cipher)
  320. @classmethod
  321. def detect(cls, repository, manifest_data):
  322. mac, meta, data = parser(manifest_data)
  323. compressor, keyer, maccer, cipher = get_implementations(meta)
  324. return cls(compressor, maccer, cipher)
  325. class AESKeyBase(KeyBase):
  326. """Common base class shared by KeyfileKey and PassphraseKey
  327. Chunks are encrypted using 256bit AES in CTR or GCM mode.
  328. Chunks are authenticated by a GCM GMAC or a HMAC.
  329. Payload layout: TYPE(1) + MAC(32) + NONCE(8) + CIPHERTEXT
  330. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  331. in the payload, the first 8 bytes are always zeros. This does not
  332. affect security but limits the maximum repository capacity to
  333. only 295 exabytes!
  334. """
  335. def extract_iv(self, payload):
  336. _, meta, _ = parser(payload)
  337. return meta.iv
  338. def init_from_random_data(self, data):
  339. self.enc_key = data[0:32]
  340. self.enc_hmac_key = data[32:64]
  341. self.id_key = data[64:96]
  342. self.chunk_seed = bytes_to_int(data[96:100])
  343. # Convert to signed int32
  344. if self.chunk_seed & 0x80000000:
  345. self.chunk_seed = self.chunk_seed - 0xffffffff - 1
  346. def init_ciphers(self, enc_iv=b'\0' * 16):
  347. self.cipher = self.cipher_cls(enc_key=self.enc_key, enc_iv=enc_iv,
  348. enc_hmac_key=self.enc_hmac_key)
  349. @property
  350. def enc_iv(self):
  351. return self.cipher.enc_iv
  352. class PassphraseKey(AESKeyBase):
  353. TYPE = 0x01
  354. iterations = 100000
  355. @classmethod
  356. def create(cls, repository, args):
  357. compressor = compressor_creator(args)
  358. maccer = maccer_creator(args, cls)
  359. cipher = cipher_creator(args, cls)
  360. key = cls(compressor, maccer, cipher)
  361. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  362. if passphrase is not None:
  363. passphrase2 = passphrase
  364. else:
  365. passphrase, passphrase2 = 1, 2
  366. while passphrase != passphrase2:
  367. passphrase = getpass('Enter passphrase: ')
  368. if not passphrase:
  369. print('Passphrase must not be blank')
  370. continue
  371. passphrase2 = getpass('Enter same passphrase again: ')
  372. if passphrase != passphrase2:
  373. print('Passphrases do not match')
  374. key.init(repository, passphrase)
  375. if passphrase:
  376. print('Remember your passphrase. Your data will be inaccessible without it.')
  377. return key
  378. @classmethod
  379. def detect(cls, repository, manifest_data):
  380. prompt = 'Enter passphrase for %s: ' % repository._location.orig
  381. mac, meta, data = parser(manifest_data)
  382. compressor, keyer, maccer, cipher = get_implementations(meta)
  383. key = cls(compressor, maccer, cipher)
  384. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  385. if passphrase is None:
  386. passphrase = getpass(prompt)
  387. while True:
  388. key.init(repository, passphrase)
  389. try:
  390. key.decrypt(None, manifest_data)
  391. key.init_ciphers(increment_iv(key.extract_iv(manifest_data), len(data)))
  392. return key
  393. except IntegrityError:
  394. passphrase = getpass(prompt)
  395. def change_passphrase(self):
  396. class ImmutablePassphraseError(Error):
  397. """The passphrase for this encryption key type can't be changed."""
  398. raise ImmutablePassphraseError
  399. def init(self, repository, passphrase):
  400. self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
  401. self.init_ciphers()
  402. class KeyfileKey(AESKeyBase):
  403. FILE_ID = 'ATTIC KEY'
  404. TYPE = 0x00
  405. @classmethod
  406. def detect(cls, repository, manifest_data):
  407. mac, meta, data = parser(manifest_data)
  408. compressor, keyer, maccer, cipher = get_implementations(meta)
  409. key = cls(compressor, maccer, cipher)
  410. path = cls.find_key_file(repository)
  411. prompt = 'Enter passphrase for key file %s: ' % path
  412. passphrase = os.environ.get('ATTIC_PASSPHRASE', '')
  413. while not key.load(path, passphrase):
  414. passphrase = getpass(prompt)
  415. key.init_ciphers(increment_iv(key.extract_iv(manifest_data), len(data)))
  416. return key
  417. @classmethod
  418. def find_key_file(cls, repository):
  419. id = hexlify(repository.id).decode('ascii')
  420. keys_dir = get_keys_dir()
  421. for name in os.listdir(keys_dir):
  422. filename = os.path.join(keys_dir, name)
  423. with open(filename, 'r') as fd:
  424. line = fd.readline().strip()
  425. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  426. return filename
  427. raise Exception('Key file for repository with ID %s not found' % id)
  428. def load(self, filename, passphrase):
  429. with open(filename, 'r') as fd:
  430. cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
  431. data = self.decrypt_key_file(cdata, passphrase)
  432. if data:
  433. key = msgpack.unpackb(data)
  434. if key[b'version'] != 1:
  435. raise IntegrityError('Invalid key file header')
  436. self.repository_id = key[b'repository_id']
  437. self.enc_key = key[b'enc_key']
  438. self.enc_hmac_key = key[b'enc_hmac_key']
  439. self.id_key = key[b'id_key']
  440. self.chunk_seed = key[b'chunk_seed']
  441. self.path = filename
  442. return True
  443. def decrypt_key_file(self, data, passphrase):
  444. d = msgpack.unpackb(data)
  445. assert d[b'version'] == 1
  446. assert d[b'algorithm'] == b'gmac'
  447. key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
  448. try:
  449. cipher = AES(mode=AES_GCM_MODE, is_encrypt=False, key=key, iv=b'\0'*16)
  450. data = cipher.check_mac_and_decrypt(d[b'hash'], d[b'data'])
  451. return data
  452. except Exception:
  453. return None
  454. def encrypt_key_file(self, data, passphrase):
  455. salt = get_random_bytes(32)
  456. iterations = 100000
  457. key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
  458. cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=key, iv=b'\0'*16)
  459. mac, cdata = cipher.compute_mac_and_encrypt(data)
  460. d = {
  461. 'version': 1,
  462. 'salt': salt,
  463. 'iterations': iterations,
  464. 'algorithm': 'gmac',
  465. 'hash': mac,
  466. 'data': cdata,
  467. }
  468. return msgpack.packb(d)
  469. def save(self, path, passphrase):
  470. key = {
  471. 'version': 1,
  472. 'repository_id': self.repository_id,
  473. 'enc_key': self.enc_key,
  474. 'enc_hmac_key': self.enc_hmac_key,
  475. 'id_key': self.id_key,
  476. 'chunk_seed': self.chunk_seed,
  477. }
  478. data = self.encrypt_key_file(msgpack.packb(key), passphrase)
  479. with open(path, 'w') as fd:
  480. fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
  481. fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
  482. fd.write('\n')
  483. self.path = path
  484. def change_passphrase(self):
  485. passphrase, passphrase2 = 1, 2
  486. while passphrase != passphrase2:
  487. passphrase = getpass('New passphrase: ')
  488. passphrase2 = getpass('Enter same passphrase again: ')
  489. if passphrase != passphrase2:
  490. print('Passphrases do not match')
  491. self.save(self.path, passphrase)
  492. print('Key file "%s" updated' % self.path)
  493. @classmethod
  494. def create(cls, repository, args):
  495. filename = args.repository.to_key_filename()
  496. path = filename
  497. i = 1
  498. while os.path.exists(path):
  499. i += 1
  500. path = filename + '.%d' % i
  501. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  502. if passphrase is not None:
  503. passphrase2 = passphrase
  504. else:
  505. passphrase, passphrase2 = 1, 2
  506. while passphrase != passphrase2:
  507. passphrase = getpass('Enter passphrase (empty for no passphrase):')
  508. passphrase2 = getpass('Enter same passphrase again: ')
  509. if passphrase != passphrase2:
  510. print('Passphrases do not match')
  511. compressor = compressor_creator(args)
  512. maccer = maccer_creator(args, cls)
  513. cipher = cipher_creator(args, cls)
  514. key = cls(compressor, maccer, cipher)
  515. key.repository_id = repository.id
  516. key.init_from_random_data(get_random_bytes(100))
  517. key.init_ciphers()
  518. key.save(path, passphrase)
  519. print('Key file "%s" created.' % key.path)
  520. print('Keep this file safe. Your data will be inaccessible without it.')
  521. return key
  522. # note: key 0 nicely maps to a zlib compressor with level 0 which means "no compression"
  523. compressor_mapping = {}
  524. for level in ZlibCompressor.LEVELS:
  525. compressor_mapping[ZlibCompressor.TYPE + level] = \
  526. type('ZlibCompressorLevel%d' % level, (ZlibCompressor, ), dict(TYPE=ZlibCompressor.TYPE + level))
  527. for preset in LzmaCompressor.PRESETS:
  528. compressor_mapping[LzmaCompressor.TYPE + preset] = \
  529. type('LzmaCompressorPreset%d' % preset, (LzmaCompressor, ), dict(TYPE=LzmaCompressor.TYPE + preset))
  530. for level in LZ4Compressor.LEVELS:
  531. compressor_mapping[LZ4Compressor.TYPE + level] = \
  532. type('LZ4CompressorLevel%d' % level, (LZ4Compressor, ), dict(TYPE=LZ4Compressor.TYPE + level))
  533. for level in LZ4HCCompressor.LEVELS:
  534. compressor_mapping[LZ4HCCompressor.TYPE + level] = \
  535. type('LZ4HCCompressorLevel%d' % level, (LZ4HCCompressor, ), dict(TYPE=LZ4HCCompressor.TYPE + level))
  536. for level in BLOSCLZCompressor.LEVELS:
  537. compressor_mapping[BLOSCLZCompressor.TYPE + level] = \
  538. type('BLOSCLZCompressorLevel%d' % level, (BLOSCLZCompressor, ), dict(TYPE=BLOSCLZCompressor.TYPE + level))
  539. for level in SnappyCompressor.LEVELS:
  540. compressor_mapping[SnappyCompressor.TYPE + level] = \
  541. type('SnappyCompressorLevel%d' % level, (SnappyCompressor, ), dict(TYPE=SnappyCompressor.TYPE + level))
  542. for level in BLOSCZlibCompressor.LEVELS:
  543. compressor_mapping[BLOSCZlibCompressor.TYPE + level] = \
  544. type('BLOSCZlibCompressorLevel%d' % level, (BLOSCZlibCompressor, ), dict(TYPE=BLOSCZlibCompressor.TYPE + level))
  545. # overwrite 0 with NullCompressor
  546. compressor_mapping[NullCompressor.TYPE] = NullCompressor
  547. keyer_mapping = {
  548. KeyfileKey.TYPE: KeyfileKey,
  549. PassphraseKey.TYPE: PassphraseKey,
  550. PlaintextKey.TYPE: PlaintextKey,
  551. }
  552. maccer_mapping = {
  553. # simple hashes, not MACs (but MAC-like class __init__ method signature):
  554. SHA1.TYPE: SHA1,
  555. SHA256.TYPE: SHA256,
  556. SHA512_256.TYPE: SHA512_256,
  557. SHA512.TYPE: SHA512,
  558. GHASH.TYPE: GHASH,
  559. # MACs:
  560. HMAC_SHA1.TYPE: HMAC_SHA1,
  561. HMAC_SHA256.TYPE: HMAC_SHA256,
  562. HMAC_SHA512_256.TYPE: HMAC_SHA512_256,
  563. HMAC_SHA512.TYPE: HMAC_SHA512,
  564. GMAC.TYPE: GMAC,
  565. }
  566. cipher_mapping = {
  567. # no cipher (but cipher-like class __init__ method signature):
  568. PLAIN.TYPE: PLAIN,
  569. # AEAD cipher implementations
  570. AES_CTR_HMAC.TYPE: AES_CTR_HMAC,
  571. AES_GCM.TYPE: AES_GCM,
  572. }
  573. def get_implementations(meta):
  574. try:
  575. compressor = compressor_mapping[meta.compr_type]
  576. keyer = keyer_mapping[meta.key_type]
  577. maccer = maccer_mapping[meta.mac_type]
  578. cipher = cipher_mapping[meta.cipher_type]
  579. except KeyError:
  580. raise UnsupportedPayloadError("compr_type %x key_type %x mac_type %x cipher_type %x" % (
  581. meta.compr_type, meta.key_type, meta.mac_type, meta.cipher_type))
  582. return compressor, keyer, maccer, cipher
  583. def legacy_parser(all_data, key_type): # all rather hardcoded
  584. """
  585. Payload layout:
  586. no encryption: TYPE(1) + data
  587. with encryption: TYPE(1) + HMAC(32) + NONCE(8) + data
  588. data is compressed with zlib level 6 and (in the 2nd case) encrypted.
  589. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  590. in the payload, the first 8 bytes are always zeros. This does not
  591. affect security but limits the maximum repository capacity to
  592. only 295 exabytes!
  593. """
  594. offset = 1
  595. if key_type == PlaintextKey.TYPE:
  596. mac_type = SHA256.TYPE
  597. mac = None
  598. cipher_type = PLAIN.TYPE
  599. iv = None
  600. data = all_data[offset:]
  601. else:
  602. mac_type = HMAC_SHA256.TYPE
  603. mac = all_data[offset:offset+32]
  604. cipher_type = AES_CTR_HMAC.TYPE
  605. # legacy attic did not store the full IV on disk, as the upper 8 bytes
  606. # are expected to be zero anyway as the full IV is a 128bit counter.
  607. iv = b'\0' * 8 + all_data[offset+32:offset+40]
  608. data = all_data[offset+40:]
  609. meta = Meta(compr_type=6, key_type=key_type, mac_type=mac_type,
  610. cipher_type=cipher_type, iv=iv, legacy=True)
  611. return mac, meta, data
  612. def parser00(all_data):
  613. return legacy_parser(all_data, KeyfileKey.TYPE)
  614. def parser01(all_data):
  615. return legacy_parser(all_data, PassphraseKey.TYPE)
  616. def parser02(all_data):
  617. return legacy_parser(all_data, PlaintextKey.TYPE)
  618. def parser03(all_data): # new & flexible
  619. """
  620. Payload layout:
  621. always: TYPE(1) + MSGPACK((mac, meta, data))
  622. meta is a Meta namedtuple and contains all required information about data.
  623. data is maybe compressed (see meta) and maybe encrypted (see meta).
  624. """
  625. unpacker = msgpack.Unpacker(
  626. use_list=False,
  627. # avoid memory allocation issues causes by tampered input data.
  628. max_buffer_size=CHUNK_MAX + 1000, # does not work in 0.4.6 unpackb C implementation
  629. max_array_len=10, # meta_tuple
  630. max_bin_len=CHUNK_MAX, # data
  631. max_str_len=0, # not used yet
  632. max_map_len=0, # not used yet
  633. max_ext_len=0, # not used yet
  634. )
  635. unpacker.feed(all_data[1:])
  636. mac, meta_tuple, data = unpacker.unpack()
  637. meta = Meta(*meta_tuple)
  638. return mac, meta, data
  639. def parser(data):
  640. parser_mapping = {
  641. 0x00: parser00,
  642. 0x01: parser01,
  643. 0x02: parser02,
  644. 0x03: parser03,
  645. }
  646. header_type = data[0]
  647. parser_func = parser_mapping[header_type]
  648. return parser_func(data)
  649. def key_factory(repository, manifest_data):
  650. mac, meta, data = parser(manifest_data)
  651. compressor, keyer, maccer, cipher = get_implementations(meta)
  652. return keyer.detect(repository, manifest_data)
  653. def generate(mac, meta, data):
  654. # always create new-style 0x03 format
  655. return b'\x03' + msgpack.packb((mac, meta, data), use_bin_type=True)
  656. def compressor_creator(args):
  657. # args == None is used by unit tests
  658. compression = COMPR_DEFAULT if args is None else args.compression
  659. compressor = compressor_mapping.get(compression)
  660. if compressor is None:
  661. raise NotImplementedError("no compression %d" % args.compression)
  662. return compressor
  663. def key_creator(args):
  664. if args.encryption == 'keyfile':
  665. return KeyfileKey
  666. if args.encryption == 'passphrase':
  667. return PassphraseKey
  668. if args.encryption == 'none':
  669. return PlaintextKey
  670. raise NotImplemented("no encryption %s" % args.encryption)
  671. def maccer_creator(args, key_cls):
  672. # args == None is used by unit tests
  673. mac = None if args is None else args.mac
  674. if mac is None:
  675. if key_cls is PlaintextKey:
  676. mac = HASH_DEFAULT
  677. elif key_cls in (KeyfileKey, PassphraseKey):
  678. mac = MAC_DEFAULT
  679. else:
  680. raise NotImplementedError("unknown key class")
  681. maccer = maccer_mapping.get(mac)
  682. if maccer is None:
  683. raise NotImplementedError("no mac %d" % args.mac)
  684. return maccer
  685. def cipher_creator(args, key_cls):
  686. # args == None is used by unit tests
  687. cipher = None if args is None else args.cipher
  688. if cipher is None:
  689. if key_cls is PlaintextKey:
  690. cipher = PLAIN_DEFAULT
  691. elif key_cls in (KeyfileKey, PassphraseKey):
  692. cipher = CIPHER_DEFAULT
  693. else:
  694. raise NotImplementedError("unknown key class")
  695. cipher = cipher_mapping.get(cipher)
  696. if cipher is None:
  697. raise NotImplementedError("no cipher %d" % args.cipher)
  698. return cipher