key.py 28 KB

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