29
29
30
30
31
31
from .low_level import AES , bytes_to_int , num_cipher_blocks , hmac_sha256 , blake2b_256
32
- from .low_level import AES256_CTR_HMAC_SHA256 , AES256_CTR_BLAKE2b , AES256_OCB , CHACHA20_POLY1305
32
+ from .low_level import AES256_CTR_HMAC_SHA256 , AES256_CTR_BLAKE2b_legacy , AES256_OCB , CHACHA20_POLY1305
33
33
from . import low_level
34
34
35
35
# workaround for lost passphrase or key in "authenticated" or "authenticated-blake2" mode
@@ -123,7 +123,7 @@ def uses_same_id_hash(other_key, key):
123
123
new_sha256_ids = (PlaintextKey ,)
124
124
old_hmac_sha256_ids = (RepoKey , KeyfileKey , AuthenticatedKey )
125
125
new_hmac_sha256_ids = (AESOCBRepoKey , AESOCBKeyfileKey , CHPORepoKey , CHPOKeyfileKey , AuthenticatedKey )
126
- old_blake2_ids = ( Blake2RepoKey , Blake2KeyfileKey , Blake2AuthenticatedKey )
126
+ old_blake2_ids = tuple () # empty tuple, old blake2 IDs are incompatible with new blake2 IDs
127
127
new_blake2_ids = (
128
128
Blake2AESOCBRepoKey ,
129
129
Blake2AESOCBKeyfileKey ,
@@ -276,7 +276,7 @@ def decrypt(self, id, data):
276
276
return memoryview (data )[1 :]
277
277
278
278
279
- def random_blake2b_256_key ():
279
+ def random_blake2b_256_key_legacy (): # borg 1.x created the key this way
280
280
# This might look a bit curious, but is the same construction used in the keyed mode of BLAKE2b.
281
281
# Why limit the key to 64 bytes and pad it with 64 nulls nonetheless? The answer is that BLAKE2b
282
282
# has a 128 byte block size, but only 64 bytes of internal state (this is also referred to as a
@@ -290,22 +290,36 @@ def random_blake2b_256_key():
290
290
return os .urandom (64 ) + bytes (64 )
291
291
292
292
293
- class ID_BLAKE2b_256 :
293
+ class ID_BLAKE2b_256_legacy : # borg 1.x
294
294
"""
295
295
Key mix-in class for using BLAKE2b-256 for the id key.
296
+ """
296
297
297
- The id_key length must be 32 bytes.
298
+ def id_hash (self , data ):
299
+ return blake2b_256 (self .id_key , data , legacy = True )
300
+
301
+ def init_from_random_data (self ):
302
+ super ().init_from_random_data ()
303
+ enc_key = os .urandom (32 )
304
+ enc_hmac_key = random_blake2b_256_key_legacy ()
305
+ self .crypt_key = enc_key + enc_hmac_key
306
+ self .id_key = random_blake2b_256_key_legacy ()
307
+
308
+
309
+ class ID_BLAKE2b_256 : # borg 2: either use the "fixed" blake2b or use blake3? see #8867
310
+ """
311
+ Key mix-in class for using BLAKE2b-256 for the id key.
298
312
"""
299
313
300
314
def id_hash (self , data ):
301
- return blake2b_256 (self .id_key , data )
315
+ return blake2b_256 (self .id_key , data , legacy = False )
302
316
303
317
def init_from_random_data (self ):
304
318
super ().init_from_random_data ()
305
319
enc_key = os .urandom (32 )
306
- enc_hmac_key = random_blake2b_256_key ( )
320
+ enc_hmac_key = os . urandom ( 64 )
307
321
self .crypt_key = enc_key + enc_hmac_key
308
- self .id_key = random_blake2b_256_key ( )
322
+ self .id_key = os . urandom ( 64 )
309
323
310
324
311
325
class ID_HMAC_SHA_256 :
@@ -747,22 +761,22 @@ class RepoKey(ID_HMAC_SHA_256, AESKeyBase, FlexiKey):
747
761
CIPHERSUITE = AES256_CTR_HMAC_SHA256
748
762
749
763
750
- class Blake2KeyfileKey ( ID_BLAKE2b_256 , AESKeyBase , FlexiKey ):
751
- TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO }
752
- TYPE = KeyType .BLAKE2KEYFILE
753
- NAME = "key file BLAKE2b"
754
- ARG_NAME = "keyfile-blake2"
764
+ class Blake2KeyfileKeyLegacy ( ID_BLAKE2b_256_legacy , AESKeyBase , FlexiKey ):
765
+ TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO } # ???
766
+ TYPE = KeyType .BLAKE2KEYFILE # ???
767
+ NAME = "key file BLAKE2b (legacy) "
768
+ ARG_NAME = "keyfile-blake2-legacy "
755
769
STORAGE = KeyBlobStorage .KEYFILE
756
- CIPHERSUITE = AES256_CTR_BLAKE2b
770
+ CIPHERSUITE = AES256_CTR_BLAKE2b_legacy
757
771
758
772
759
- class Blake2RepoKey ( ID_BLAKE2b_256 , AESKeyBase , FlexiKey ):
760
- TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO }
761
- TYPE = KeyType .BLAKE2REPO
762
- NAME = "repokey BLAKE2b"
763
- ARG_NAME = "repokey-blake2"
773
+ class Blake2RepoKeyLegacy ( ID_BLAKE2b_256_legacy , AESKeyBase , FlexiKey ):
774
+ TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO } # ???
775
+ TYPE = KeyType .BLAKE2REPO # ???
776
+ NAME = "repokey BLAKE2b (legacy) "
777
+ ARG_NAME = "repokey-blake2-legacy "
764
778
STORAGE = KeyBlobStorage .REPO
765
- CIPHERSUITE = AES256_CTR_BLAKE2b
779
+ CIPHERSUITE = AES256_CTR_BLAKE2b_legacy
766
780
767
781
768
782
class AuthenticatedKeyBase (AESKeyBase , FlexiKey ):
@@ -811,16 +825,23 @@ class AuthenticatedKey(ID_HMAC_SHA_256, AuthenticatedKeyBase):
811
825
ARG_NAME = "authenticated"
812
826
813
827
828
+ class Blake2AuthenticatedKeyLegacy (ID_BLAKE2b_256_legacy , AuthenticatedKeyBase ):
829
+ TYPE = KeyType .BLAKE2AUTHENTICATEDLEGACY
830
+ TYPES_ACCEPTABLE = {TYPE }
831
+ NAME = "authenticated BLAKE2b (legacy)"
832
+ ARG_NAME = "authenticated-blake2-legacy"
833
+
834
+
835
+ # ------------ new crypto ------------
836
+
837
+
814
838
class Blake2AuthenticatedKey (ID_BLAKE2b_256 , AuthenticatedKeyBase ):
815
839
TYPE = KeyType .BLAKE2AUTHENTICATED
816
840
TYPES_ACCEPTABLE = {TYPE }
817
841
NAME = "authenticated BLAKE2b"
818
842
ARG_NAME = "authenticated-blake2"
819
843
820
844
821
- # ------------ new crypto ------------
822
-
823
-
824
845
class AEADKeyBase (KeyBase ):
825
846
"""
826
847
Chunks are encrypted and authenticated using some AEAD ciphersuite
@@ -1003,24 +1024,25 @@ class Blake2CHPORepoKey(ID_BLAKE2b_256, AEADKeyBase, FlexiKey):
1003
1024
1004
1025
1005
1026
LEGACY_KEY_TYPES = (
1006
- # legacy (AES-CTR based) crypto
1027
+ # legacy (AES-CTR or Blake2_legacy based) crypto
1007
1028
KeyfileKey ,
1008
1029
RepoKey ,
1009
- Blake2KeyfileKey ,
1010
- Blake2RepoKey ,
1030
+ Blake2KeyfileKeyLegacy ,
1031
+ Blake2RepoKeyLegacy ,
1032
+ Blake2AuthenticatedKeyLegacy ,
1011
1033
)
1012
1034
1013
1035
AVAILABLE_KEY_TYPES = (
1014
1036
# these are available encryption modes for new repositories
1015
1037
# not encrypted modes
1016
1038
PlaintextKey ,
1017
1039
AuthenticatedKey ,
1018
- Blake2AuthenticatedKey ,
1019
1040
# new crypto
1020
1041
AESOCBKeyfileKey ,
1021
1042
AESOCBRepoKey ,
1022
1043
CHPOKeyfileKey ,
1023
1044
CHPORepoKey ,
1045
+ Blake2AuthenticatedKey ,
1024
1046
Blake2AESOCBKeyfileKey ,
1025
1047
Blake2AESOCBRepoKey ,
1026
1048
Blake2CHPOKeyfileKey ,
0 commit comments