key.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 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. from attic.crypto import pbkdf2_sha256, get_random_bytes, AES, AES_CTR_MODE, AES_GCM_MODE, \
  18. bytes_to_long, long_to_bytes, bytes_to_int, num_aes_blocks
  19. from attic.helpers import IntegrityError, get_keys_dir, Error
  20. # we do not store the full IV on disk, as the upper 8 bytes are expected to be
  21. # zero anyway as the full IV is a 128bit counter. PREFIX are the upper 8 bytes,
  22. # stored_iv are the lower 8 Bytes.
  23. PREFIX = b'\0' * 8
  24. Meta = namedtuple('Meta', 'compr_type, key_type, mac_type, cipher_type, hmac, stored_iv')
  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. class HMAC(hmac.HMAC):
  48. """Workaround a bug in Python < 3.4 Where HMAC does not accept memoryviews
  49. """
  50. def update(self, msg):
  51. self.inner.update(msg)
  52. # HASH / MAC stuff below all has a mac-like interface, so it can be used in the same way.
  53. # special case: hashes do not use keys (and thus, do not sign/authenticate)
  54. class SHA256(object): # note: can't subclass sha256
  55. TYPE = 0
  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 = sha256(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 SHA512_256(sha512_256):
  68. """sha512, but digest truncated to 256bit - faster than sha256 on 64bit platforms"""
  69. TYPE = 1
  70. def __init__(self, key, data):
  71. # signature is like for a MAC, we ignore the key as this is a simple hash
  72. if key is not None:
  73. raise Exception("use a HMAC if you have a key")
  74. super().__init__(data)
  75. class GHASH:
  76. TYPE = 2
  77. def __init__(self, key, data):
  78. # signature is like for a MAC, we ignore the key as this is a simple hash
  79. if key is not None:
  80. raise Exception("use a MAC if you have a key")
  81. self.key = b'\0' * 32
  82. self.data = data
  83. def digest(self):
  84. mac_cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=self.key, iv=b'\0' * 16)
  85. # GMAC = aes-gcm with all data as AAD, no data as to-be-encrypted data
  86. mac_cipher.add(bytes(self.data))
  87. tag, _ = mac_cipher.compute_tag_and_encrypt(b'')
  88. return tag
  89. class HMAC_SHA256(HMAC):
  90. TYPE = 10
  91. def __init__(self, key, data):
  92. if key is None:
  93. raise Exception("do not use HMAC if you don't have a key")
  94. super().__init__(key, data, sha256)
  95. class HMAC_SHA512_256(HMAC):
  96. TYPE = 11
  97. def __init__(self, key, data):
  98. if key is None:
  99. raise Exception("do not use HMAC if you don't have a key")
  100. super().__init__(key, data, sha512_256)
  101. class GMAC(GHASH):
  102. TYPE = 20
  103. def __init__(self, key, data):
  104. super().__init__(None, data)
  105. if key is None:
  106. raise Exception("do not use GMAC if you don't have a key")
  107. self.key = key
  108. # defaults are optimized for speed on modern CPUs with AES hw support
  109. HASH_DEFAULT = GHASH.TYPE
  110. MAC_DEFAULT = GMAC.TYPE
  111. # compressor classes, all same interface
  112. # special case: zlib level 0 is "no compression"
  113. class ZlibCompressor(object): # uses 0..9 in the mapping
  114. TYPE = 0
  115. LEVELS = range(10)
  116. def compress(self, data):
  117. level = self.TYPE - ZlibCompressor.TYPE
  118. return zlib.compress(data, level)
  119. def decompress(self, data):
  120. return zlib.decompress(data)
  121. class LzmaCompressor(object): # uses 10..19 in the mapping
  122. TYPE = 10
  123. PRESETS = range(10)
  124. def __init__(self):
  125. if lzma is None:
  126. raise NotImplemented("lzma compression needs Python >= 3.3 or backports.lzma from PyPi")
  127. def compress(self, data):
  128. preset = self.TYPE - LzmaCompressor.TYPE
  129. return lzma.compress(data, preset=preset)
  130. def decompress(self, data):
  131. return lzma.decompress(data)
  132. # default is optimized for speed (and a little compression)
  133. COMPR_DEFAULT = ZlibCompressor.TYPE + 1 # zlib level 1
  134. # ciphers - AEAD (authenticated encryption with assoc. data) style interface
  135. # special case: PLAIN dummy does not encrypt / authenticate
  136. class PLAIN:
  137. TYPE = 0
  138. def __init__(self, **kw):
  139. pass
  140. def compute_tag_and_encrypt(self, data):
  141. return b'', b'', data
  142. def check_tag_and_decrypt(self, tag, iv_last8, data):
  143. return data
  144. class AES_CTR_HMAC:
  145. TYPE = 1
  146. def __init__(self, enc_key=b'\0' * 32, enc_iv=b'\0' * 16, enc_hmac_key=b'\0' * 32, **kw):
  147. self.hmac_key = enc_hmac_key
  148. self.enc_iv = enc_iv
  149. self.enc_cipher = AES(mode=AES_CTR_MODE, is_encrypt=True, key=enc_key, iv=enc_iv)
  150. self.dec_cipher = AES(mode=AES_CTR_MODE, is_encrypt=False, key=enc_key)
  151. def compute_tag_and_encrypt(self, data):
  152. self.enc_cipher.reset(iv=self.enc_iv)
  153. iv_last8 = self.enc_iv[8:]
  154. _, data = self.enc_cipher.compute_tag_and_encrypt(data)
  155. # increase the IV (counter) value so same value is never used twice
  156. current_iv = bytes_to_long(iv_last8)
  157. self.enc_iv = PREFIX + long_to_bytes(current_iv + num_aes_blocks(len(data)))
  158. tag = HMAC(self.hmac_key, iv_last8 + data, sha256).digest() # XXX mac / hash flexibility
  159. return tag, iv_last8, data
  160. def check_tag_and_decrypt(self, tag, iv_last8, data):
  161. iv = PREFIX + iv_last8
  162. if HMAC(self.hmac_key, iv_last8 + data, sha256).digest() != tag:
  163. raise IntegrityError('Encryption envelope checksum mismatch')
  164. self.dec_cipher.reset(iv=iv)
  165. data = self.dec_cipher.check_tag_and_decrypt(None, data)
  166. return data
  167. class AES_GCM:
  168. TYPE = 2
  169. def __init__(self, enc_key=b'\0' * 32, enc_iv=b'\0' * 16, **kw):
  170. # note: hmac_key is not used for aes-gcm, it does aes+gmac in 1 pass
  171. self.enc_iv = enc_iv
  172. self.enc_cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=enc_key, iv=enc_iv)
  173. self.dec_cipher = AES(mode=AES_GCM_MODE, is_encrypt=False, key=enc_key)
  174. def compute_tag_and_encrypt(self, data):
  175. self.enc_cipher.reset(iv=self.enc_iv)
  176. iv_last8 = self.enc_iv[8:]
  177. self.enc_cipher.add(iv_last8)
  178. tag, data = self.enc_cipher.compute_tag_and_encrypt(data)
  179. # increase the IV (counter) value so same value is never used twice
  180. current_iv = bytes_to_long(iv_last8)
  181. self.enc_iv = PREFIX + long_to_bytes(current_iv + num_aes_blocks(len(data)))
  182. return tag, iv_last8, data
  183. def check_tag_and_decrypt(self, tag, iv_last8, data):
  184. iv = PREFIX + iv_last8
  185. self.dec_cipher.reset(iv=iv)
  186. self.dec_cipher.add(iv_last8)
  187. try:
  188. data = self.dec_cipher.check_tag_and_decrypt(tag, data)
  189. except Exception:
  190. raise IntegrityError('Encryption envelope checksum mismatch')
  191. return data
  192. # cipher default is optimized for speed on modern CPUs with AES hw support
  193. PLAIN_DEFAULT = PLAIN.TYPE
  194. CIPHER_DEFAULT = AES_GCM.TYPE
  195. # misc. types of keys
  196. # special case: no keys (thus: no encryption, no signing/authentication)
  197. class KeyBase(object):
  198. TYPE = 0x00 # override in derived classes
  199. def __init__(self, compressor_cls, maccer_cls, cipher_cls):
  200. self.compressor = compressor_cls()
  201. self.maccer_cls = maccer_cls # hasher/maccer used by id_hash
  202. self.cipher_cls = cipher_cls # plaintext dummy or AEAD cipher
  203. self.cipher = cipher_cls()
  204. self.id_key = None
  205. def id_hash(self, data):
  206. """Return a HASH (no id_key) or a MAC (using the "id_key" key)
  207. XXX do we need a cryptographic hash function here or is a keyed hash
  208. function like GMAC / GHASH good enough? See NIST SP 800-38D.
  209. IMPORTANT: in 1 repo, there should be only 1 kind of id_hash, otherwise
  210. data hashed/maced with one id_hash might result in same ID as already
  211. exists in the repo for other data created with another id_hash method.
  212. somehow unlikely considering 128 or 256bits, but still.
  213. """
  214. return self.maccer_cls(self.id_key, data).digest()
  215. def encrypt(self, data):
  216. data = self.compressor.compress(data)
  217. tag, iv_last8, data = self.cipher.compute_tag_and_encrypt(data)
  218. meta = Meta(compr_type=self.compressor.TYPE, key_type=self.TYPE,
  219. mac_type=self.maccer_cls.TYPE, cipher_type=self.cipher.TYPE,
  220. hmac=tag, stored_iv=iv_last8)
  221. return generate(meta, data)
  222. def decrypt(self, id, data):
  223. meta, data, compressor, keyer, maccer, cipher = parser(data)
  224. assert isinstance(self, keyer)
  225. assert self.maccer_cls is maccer
  226. assert self.cipher_cls is cipher
  227. data = self.cipher.check_tag_and_decrypt(meta.hmac, meta.stored_iv, data)
  228. data = self.compressor.decompress(data)
  229. if id and self.id_hash(data) != id:
  230. raise IntegrityError('Chunk id verification failed')
  231. return data
  232. class PlaintextKey(KeyBase):
  233. TYPE = 0x02
  234. chunk_seed = 0
  235. @classmethod
  236. def create(cls, repository, args):
  237. print('Encryption NOT enabled.\nUse the "--encryption=passphrase|keyfile" to enable encryption.')
  238. compressor = compressor_creator(args)
  239. maccer = maccer_creator(args, cls)
  240. cipher = cipher_creator(args, cls)
  241. return cls(compressor, maccer, cipher)
  242. @classmethod
  243. def detect(cls, repository, manifest_data):
  244. meta, data, compressor, keyer, maccer, cipher = parser(manifest_data)
  245. return cls(compressor, maccer, cipher)
  246. class AESKeyBase(KeyBase):
  247. """Common base class shared by KeyfileKey and PassphraseKey
  248. Chunks are encrypted using 256bit AES in CTR or GCM mode.
  249. Chunks are authenticated by a GCM GMAC or a HMAC.
  250. Payload layout: TYPE(1) + MAC(32) + NONCE(8) + CIPHERTEXT
  251. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  252. in the payload, the first 8 bytes are always zeros. This does not
  253. affect security but limits the maximum repository capacity to
  254. only 295 exabytes!
  255. """
  256. def extract_nonce(self, payload):
  257. meta, data, compressor, keyer, maccer, cipher = parser(payload)
  258. assert isinstance(self, keyer)
  259. nonce = bytes_to_long(meta.stored_iv)
  260. return nonce
  261. def init_from_random_data(self, data):
  262. self.enc_key = data[0:32]
  263. self.enc_hmac_key = data[32:64]
  264. self.id_key = data[64:96]
  265. self.chunk_seed = bytes_to_int(data[96:100])
  266. # Convert to signed int32
  267. if self.chunk_seed & 0x80000000:
  268. self.chunk_seed = self.chunk_seed - 0xffffffff - 1
  269. def init_ciphers(self, enc_iv=b'\0' * 16):
  270. self.cipher = self.cipher_cls(enc_key=self.enc_key, enc_iv=enc_iv,
  271. enc_hmac_key=self.enc_hmac_key)
  272. @property
  273. def enc_iv(self):
  274. return self.cipher.enc_iv
  275. class PassphraseKey(AESKeyBase):
  276. TYPE = 0x01
  277. iterations = 100000
  278. @classmethod
  279. def create(cls, repository, args):
  280. compressor = compressor_creator(args)
  281. maccer = maccer_creator(args, cls)
  282. cipher = cipher_creator(args, cls)
  283. key = cls(compressor, maccer, cipher)
  284. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  285. if passphrase is not None:
  286. passphrase2 = passphrase
  287. else:
  288. passphrase, passphrase2 = 1, 2
  289. while passphrase != passphrase2:
  290. passphrase = getpass('Enter passphrase: ')
  291. if not passphrase:
  292. print('Passphrase must not be blank')
  293. continue
  294. passphrase2 = getpass('Enter same passphrase again: ')
  295. if passphrase != passphrase2:
  296. print('Passphrases do not match')
  297. key.init(repository, passphrase)
  298. if passphrase:
  299. print('Remember your passphrase. Your data will be inaccessible without it.')
  300. return key
  301. @classmethod
  302. def detect(cls, repository, manifest_data):
  303. prompt = 'Enter passphrase for %s: ' % repository._location.orig
  304. meta, data, compressor, keyer, maccer, cipher = parser(manifest_data)
  305. key = cls(compressor, maccer, cipher)
  306. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  307. if passphrase is None:
  308. passphrase = getpass(prompt)
  309. while True:
  310. key.init(repository, passphrase)
  311. try:
  312. key.decrypt(None, manifest_data)
  313. num_blocks = num_aes_blocks(len(data))
  314. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  315. return key
  316. except IntegrityError:
  317. passphrase = getpass(prompt)
  318. def change_passphrase(self):
  319. class ImmutablePassphraseError(Error):
  320. """The passphrase for this encryption key type can't be changed."""
  321. raise ImmutablePassphraseError
  322. def init(self, repository, passphrase):
  323. self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
  324. self.init_ciphers()
  325. class KeyfileKey(AESKeyBase):
  326. FILE_ID = 'ATTIC KEY'
  327. TYPE = 0x00
  328. @classmethod
  329. def detect(cls, repository, manifest_data):
  330. meta, data, compressor, keyer, maccer, cipher = parser(manifest_data)
  331. key = cls(compressor, maccer, cipher)
  332. path = cls.find_key_file(repository)
  333. prompt = 'Enter passphrase for key file %s: ' % path
  334. passphrase = os.environ.get('ATTIC_PASSPHRASE', '')
  335. while not key.load(path, passphrase):
  336. passphrase = getpass(prompt)
  337. num_blocks = num_aes_blocks(len(data))
  338. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  339. return key
  340. @classmethod
  341. def find_key_file(cls, repository):
  342. id = hexlify(repository.id).decode('ascii')
  343. keys_dir = get_keys_dir()
  344. for name in os.listdir(keys_dir):
  345. filename = os.path.join(keys_dir, name)
  346. with open(filename, 'r') as fd:
  347. line = fd.readline().strip()
  348. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  349. return filename
  350. raise Exception('Key file for repository with ID %s not found' % id)
  351. def load(self, filename, passphrase):
  352. with open(filename, 'r') as fd:
  353. cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
  354. data = self.decrypt_key_file(cdata, passphrase)
  355. if data:
  356. key = msgpack.unpackb(data)
  357. if key[b'version'] != 1:
  358. raise IntegrityError('Invalid key file header')
  359. self.repository_id = key[b'repository_id']
  360. self.enc_key = key[b'enc_key']
  361. self.enc_hmac_key = key[b'enc_hmac_key']
  362. self.id_key = key[b'id_key']
  363. self.chunk_seed = key[b'chunk_seed']
  364. self.path = filename
  365. return True
  366. def decrypt_key_file(self, data, passphrase):
  367. d = msgpack.unpackb(data)
  368. assert d[b'version'] == 1
  369. assert d[b'algorithm'] == b'gmac'
  370. key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
  371. try:
  372. cipher = AES(mode=AES_GCM_MODE, is_encrypt=False, key=key, iv=b'\0'*16)
  373. data = cipher.check_tag_and_decrypt(d[b'hash'], d[b'data'])
  374. return data
  375. except Exception:
  376. return None
  377. def encrypt_key_file(self, data, passphrase):
  378. salt = get_random_bytes(32)
  379. iterations = 100000
  380. key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
  381. cipher = AES(mode=AES_GCM_MODE, is_encrypt=True, key=key, iv=b'\0'*16)
  382. tag, cdata = cipher.compute_tag_and_encrypt(data)
  383. d = {
  384. 'version': 1,
  385. 'salt': salt,
  386. 'iterations': iterations,
  387. 'algorithm': 'gmac',
  388. 'hash': tag,
  389. 'data': cdata,
  390. }
  391. return msgpack.packb(d)
  392. def save(self, path, passphrase):
  393. key = {
  394. 'version': 1,
  395. 'repository_id': self.repository_id,
  396. 'enc_key': self.enc_key,
  397. 'enc_hmac_key': self.enc_hmac_key,
  398. 'id_key': self.id_key,
  399. 'chunk_seed': self.chunk_seed,
  400. }
  401. data = self.encrypt_key_file(msgpack.packb(key), passphrase)
  402. with open(path, 'w') as fd:
  403. fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
  404. fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
  405. fd.write('\n')
  406. self.path = path
  407. def change_passphrase(self):
  408. passphrase, passphrase2 = 1, 2
  409. while passphrase != passphrase2:
  410. passphrase = getpass('New passphrase: ')
  411. passphrase2 = getpass('Enter same passphrase again: ')
  412. if passphrase != passphrase2:
  413. print('Passphrases do not match')
  414. self.save(self.path, passphrase)
  415. print('Key file "%s" updated' % self.path)
  416. @classmethod
  417. def create(cls, repository, args):
  418. filename = args.repository.to_key_filename()
  419. path = filename
  420. i = 1
  421. while os.path.exists(path):
  422. i += 1
  423. path = filename + '.%d' % i
  424. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  425. if passphrase is not None:
  426. passphrase2 = passphrase
  427. else:
  428. passphrase, passphrase2 = 1, 2
  429. while passphrase != passphrase2:
  430. passphrase = getpass('Enter passphrase (empty for no passphrase):')
  431. passphrase2 = getpass('Enter same passphrase again: ')
  432. if passphrase != passphrase2:
  433. print('Passphrases do not match')
  434. compressor = compressor_creator(args)
  435. maccer = maccer_creator(args, cls)
  436. cipher = cipher_creator(args, cls)
  437. key = cls(compressor, maccer, cipher)
  438. key.repository_id = repository.id
  439. key.init_from_random_data(get_random_bytes(100))
  440. key.init_ciphers()
  441. key.save(path, passphrase)
  442. print('Key file "%s" created.' % key.path)
  443. print('Keep this file safe. Your data will be inaccessible without it.')
  444. return key
  445. # note: key 0 nicely maps to a zlib compressor with level 0 which means "no compression"
  446. compressor_mapping = {}
  447. for level in ZlibCompressor.LEVELS:
  448. compressor_mapping[ZlibCompressor.TYPE + level] = \
  449. type('ZlibCompressorLevel%d' % level, (ZlibCompressor, ), dict(TYPE=ZlibCompressor.TYPE + level))
  450. for preset in LzmaCompressor.PRESETS:
  451. compressor_mapping[LzmaCompressor.TYPE + preset] = \
  452. type('LzmaCompressorPreset%d' % preset, (LzmaCompressor, ), dict(TYPE=LzmaCompressor.TYPE + preset))
  453. keyer_mapping = {
  454. KeyfileKey.TYPE: KeyfileKey,
  455. PassphraseKey.TYPE: PassphraseKey,
  456. PlaintextKey.TYPE: PlaintextKey,
  457. }
  458. maccer_mapping = {
  459. # simple hashes, not MACs (but MAC-like class __init__ method signature):
  460. SHA256.TYPE: SHA256,
  461. SHA512_256.TYPE: SHA512_256,
  462. GHASH.TYPE: GHASH,
  463. # MACs:
  464. HMAC_SHA256.TYPE: HMAC_SHA256,
  465. HMAC_SHA512_256.TYPE: HMAC_SHA512_256,
  466. GMAC.TYPE: GMAC,
  467. }
  468. cipher_mapping = {
  469. # no cipher (but cipher-like class __init__ method signature):
  470. PLAIN.TYPE: PLAIN,
  471. # AEAD cipher implementations
  472. AES_CTR_HMAC.TYPE: AES_CTR_HMAC,
  473. AES_GCM.TYPE: AES_GCM,
  474. }
  475. def get_implementations(meta):
  476. try:
  477. compressor = compressor_mapping[meta.compr_type]
  478. keyer = keyer_mapping[meta.key_type]
  479. maccer = maccer_mapping[meta.mac_type]
  480. cipher = cipher_mapping[meta.cipher_type]
  481. except KeyError:
  482. raise UnsupportedPayloadError("compr_type %x key_type %x mac_type %x" % (
  483. meta.compr_type, meta.key_type, meta.mac_type, meta.cipher_type))
  484. return compressor, keyer, maccer, cipher
  485. def legacy_parser(all_data, key_type): # all rather hardcoded
  486. """
  487. Payload layout:
  488. no encryption: TYPE(1) + data
  489. with encryption: TYPE(1) + HMAC(32) + NONCE(8) + data
  490. data is compressed with zlib level 6 and (in the 2nd case) encrypted.
  491. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  492. in the payload, the first 8 bytes are always zeros. This does not
  493. affect security but limits the maximum repository capacity to
  494. only 295 exabytes!
  495. """
  496. offset = 1
  497. if key_type == PlaintextKey.TYPE:
  498. hmac = None
  499. iv = stored_iv = None
  500. data = all_data[offset:]
  501. else:
  502. hmac = all_data[offset:offset+32]
  503. stored_iv = all_data[offset+32:offset+40]
  504. data = all_data[offset+40:]
  505. meta = Meta(compr_type=6, key_type=key_type,
  506. mac_type=HMAC_SHA256.TYPE, cipher_type=AES_CTR_HMAC.TYPE,
  507. hmac=hmac, stored_iv=stored_iv)
  508. compressor, keyer, maccer, cipher = get_implementations(meta)
  509. return meta, data, compressor, keyer, maccer, cipher
  510. def parser00(all_data):
  511. return legacy_parser(all_data, KeyfileKey.TYPE)
  512. def parser01(all_data):
  513. return legacy_parser(all_data, PassphraseKey.TYPE)
  514. def parser02(all_data):
  515. return legacy_parser(all_data, PlaintextKey.TYPE)
  516. def parser03(all_data): # new & flexible
  517. """
  518. Payload layout:
  519. always: TYPE(1) + MSGPACK((meta, data))
  520. meta is a Meta namedtuple and contains all required information about data.
  521. data is maybe compressed (see meta) and maybe encrypted (see meta).
  522. """
  523. # TODO use Unpacker(..., max_*_len=NOTMORETHANNEEDED) to avoid any memory
  524. # allocation issues on untrusted and potentially tampered input data.
  525. # Problem: we currently must use older msgpack because pure python impl.
  526. # is broken in 0.4.2 < version <= 0.4.5, but this api is only offered by
  527. # more recent ones, not by 0.4.2. So, fix here when 0.4.6 is out. :-(
  528. meta_tuple, data = msgpack.unpackb(all_data[1:])
  529. meta = Meta(*meta_tuple)
  530. compressor, keyer, maccer, cipher = get_implementations(meta)
  531. return meta, data, compressor, keyer, maccer, cipher
  532. def parser(data):
  533. parser_mapping = {
  534. 0x00: parser00,
  535. 0x01: parser01,
  536. 0x02: parser02,
  537. 0x03: parser03,
  538. }
  539. header_type = data[0]
  540. parser_func = parser_mapping[header_type]
  541. return parser_func(data)
  542. def key_factory(repository, manifest_data):
  543. meta, data, compressor, keyer, maccer, cipher = parser(manifest_data)
  544. return keyer.detect(repository, manifest_data)
  545. def generate(meta, data):
  546. # always create new-style 0x03 format
  547. return b'\x03' + msgpack.packb((meta, data))
  548. def compressor_creator(args):
  549. # args == None is used by unit tests
  550. compression = COMPR_DEFAULT if args is None else args.compression
  551. compressor = compressor_mapping.get(compression)
  552. if compressor is None:
  553. raise NotImplementedError("no compression %d" % args.compression)
  554. return compressor
  555. def key_creator(repository, args):
  556. if args.encryption == 'keyfile':
  557. return KeyfileKey.create(repository, args)
  558. if args.encryption == 'passphrase':
  559. return PassphraseKey.create(repository, args)
  560. if args.encryption == 'none':
  561. return PlaintextKey.create(repository, args)
  562. raise NotImplemented("no encryption %s" % args.encryption)
  563. def maccer_creator(args, key_cls):
  564. # args == None is used by unit tests
  565. mac = None if args is None else args.mac
  566. if mac is None:
  567. if key_cls is PlaintextKey:
  568. mac = HASH_DEFAULT
  569. elif key_cls in (KeyfileKey, PassphraseKey):
  570. mac = MAC_DEFAULT
  571. else:
  572. raise NotImplementedError("unknown key class")
  573. maccer = maccer_mapping.get(mac)
  574. if maccer is None:
  575. raise NotImplementedError("no mac %d" % args.mac)
  576. return maccer
  577. def cipher_creator(args, key_cls):
  578. # args == None is used by unit tests
  579. cipher = None if args is None else args.cipher
  580. if cipher is None:
  581. if key_cls is PlaintextKey:
  582. cipher = PLAIN_DEFAULT
  583. elif key_cls in (KeyfileKey, PassphraseKey):
  584. cipher = CIPHER_DEFAULT
  585. else:
  586. raise NotImplementedError("unknown key class")
  587. cipher = cipher_mapping.get(cipher)
  588. if cipher is None:
  589. raise NotImplementedError("no cipher %d" % args.cipher)
  590. return cipher