2828
2929
3030from .low_level import AES , bytes_to_int , num_cipher_blocks , hmac_sha256 , blake2b_256
31- from .low_level import AES256_CTR_HMAC_SHA256 , AES256_CTR_BLAKE2b , AES256_OCB , CHACHA20_POLY1305
31+ from .low_level import AES256_CTR_HMAC_SHA256 , AES256_CTR_BLAKE2b_legacy , AES256_OCB , CHACHA20_POLY1305
3232from . import low_level
3333
3434# workaround for lost passphrase or key in "authenticated" or "authenticated-blake2" mode
@@ -122,7 +122,7 @@ def uses_same_id_hash(other_key, key):
122122 new_sha256_ids = (PlaintextKey ,)
123123 old_hmac_sha256_ids = (RepoKey , KeyfileKey , AuthenticatedKey )
124124 new_hmac_sha256_ids = (AESOCBRepoKey , AESOCBKeyfileKey , CHPORepoKey , CHPOKeyfileKey , AuthenticatedKey )
125- old_blake2_ids = ( Blake2RepoKey , Blake2KeyfileKey , Blake2AuthenticatedKey )
125+ old_blake2_ids = tuple () # empty tuple, old blake2 IDs are incompatible with new blake2 IDs
126126 new_blake2_ids = (
127127 Blake2AESOCBRepoKey ,
128128 Blake2AESOCBKeyfileKey ,
@@ -251,7 +251,7 @@ def decrypt(self, id, data):
251251 return memoryview (data )[1 :]
252252
253253
254- def random_blake2b_256_key ():
254+ def random_blake2b_256_key_legacy (): # borg 1.x created the key this way
255255 # This might look a bit curious, but is the same construction used in the keyed mode of BLAKE2b.
256256 # Why limit the key to 64 bytes and pad it with 64 nulls nonetheless? The answer is that BLAKE2b
257257 # has a 128 byte block size, but only 64 bytes of internal state (this is also referred to as a
@@ -265,22 +265,36 @@ def random_blake2b_256_key():
265265 return os .urandom (64 ) + bytes (64 )
266266
267267
268- class ID_BLAKE2b_256 :
268+ class ID_BLAKE2b_256_legacy : # borg 1.x
269269 """
270270 Key mix-in class for using BLAKE2b-256 for the id key.
271+ """
271272
272- The id_key length must be 32 bytes.
273+ def id_hash (self , data ):
274+ return blake2b_256 (self .id_key , data , legacy = True )
275+
276+ def init_from_random_data (self ):
277+ super ().init_from_random_data ()
278+ enc_key = os .urandom (32 )
279+ enc_hmac_key = random_blake2b_256_key_legacy ()
280+ self .crypt_key = enc_key + enc_hmac_key
281+ self .id_key = random_blake2b_256_key_legacy ()
282+
283+
284+ class ID_BLAKE2b_256 : # borg 2: either use the "fixed" blake2b or use blake3? see #8867
285+ """
286+ Key mix-in class for using BLAKE2b-256 for the id key.
273287 """
274288
275289 def id_hash (self , data ):
276- return blake2b_256 (self .id_key , data )
290+ return blake2b_256 (self .id_key , data , legacy = False )
277291
278292 def init_from_random_data (self ):
279293 super ().init_from_random_data ()
280294 enc_key = os .urandom (32 )
281- enc_hmac_key = random_blake2b_256_key ( )
295+ enc_hmac_key = os . urandom ( 64 )
282296 self .crypt_key = enc_key + enc_hmac_key
283- self .id_key = random_blake2b_256_key ( )
297+ self .id_key = os . urandom ( 64 )
284298
285299
286300class ID_HMAC_SHA_256 :
@@ -722,22 +736,22 @@ class RepoKey(ID_HMAC_SHA_256, AESKeyBase, FlexiKey):
722736 CIPHERSUITE = AES256_CTR_HMAC_SHA256
723737
724738
725- class Blake2KeyfileKey ( ID_BLAKE2b_256 , AESKeyBase , FlexiKey ):
726- TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO }
727- TYPE = KeyType .BLAKE2KEYFILE
728- NAME = "key file BLAKE2b"
729- ARG_NAME = "keyfile-blake2"
739+ class Blake2KeyfileKeyLegacy ( ID_BLAKE2b_256_legacy , AESKeyBase , FlexiKey ):
740+ TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO } # ???
741+ TYPE = KeyType .BLAKE2KEYFILE # ???
742+ NAME = "key file BLAKE2b (legacy) "
743+ ARG_NAME = "keyfile-blake2-legacy "
730744 STORAGE = KeyBlobStorage .KEYFILE
731- CIPHERSUITE = AES256_CTR_BLAKE2b
745+ CIPHERSUITE = AES256_CTR_BLAKE2b_legacy
732746
733747
734- class Blake2RepoKey ( ID_BLAKE2b_256 , AESKeyBase , FlexiKey ):
735- TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO }
736- TYPE = KeyType .BLAKE2REPO
737- NAME = "repokey BLAKE2b"
738- ARG_NAME = "repokey-blake2"
748+ class Blake2RepoKeyLegacy ( ID_BLAKE2b_256_legacy , AESKeyBase , FlexiKey ):
749+ TYPES_ACCEPTABLE = {KeyType .BLAKE2KEYFILE , KeyType .BLAKE2REPO } # ???
750+ TYPE = KeyType .BLAKE2REPO # ???
751+ NAME = "repokey BLAKE2b (legacy) "
752+ ARG_NAME = "repokey-blake2-legacy "
739753 STORAGE = KeyBlobStorage .REPO
740- CIPHERSUITE = AES256_CTR_BLAKE2b
754+ CIPHERSUITE = AES256_CTR_BLAKE2b_legacy
741755
742756
743757class AuthenticatedKeyBase (AESKeyBase , FlexiKey ):
@@ -786,16 +800,23 @@ class AuthenticatedKey(ID_HMAC_SHA_256, AuthenticatedKeyBase):
786800 ARG_NAME = "authenticated"
787801
788802
803+ class Blake2AuthenticatedKeyLegacy (ID_BLAKE2b_256_legacy , AuthenticatedKeyBase ):
804+ TYPE = KeyType .BLAKE2AUTHENTICATEDLEGACY
805+ TYPES_ACCEPTABLE = {TYPE }
806+ NAME = "authenticated BLAKE2b (legacy)"
807+ ARG_NAME = "authenticated-blake2-legacy"
808+
809+
810+ # ------------ new crypto ------------
811+
812+
789813class Blake2AuthenticatedKey (ID_BLAKE2b_256 , AuthenticatedKeyBase ):
790814 TYPE = KeyType .BLAKE2AUTHENTICATED
791815 TYPES_ACCEPTABLE = {TYPE }
792816 NAME = "authenticated BLAKE2b"
793817 ARG_NAME = "authenticated-blake2"
794818
795819
796- # ------------ new crypto ------------
797-
798-
799820class AEADKeyBase (KeyBase ):
800821 """
801822 Chunks are encrypted and authenticated using some AEAD ciphersuite
@@ -980,24 +1001,25 @@ class Blake2CHPORepoKey(ID_BLAKE2b_256, AEADKeyBase, FlexiKey):
9801001
9811002
9821003LEGACY_KEY_TYPES = (
983- # legacy (AES-CTR based) crypto
1004+ # legacy (AES-CTR or Blake2_legacy based) crypto
9841005 KeyfileKey ,
9851006 RepoKey ,
986- Blake2KeyfileKey ,
987- Blake2RepoKey ,
1007+ Blake2KeyfileKeyLegacy ,
1008+ Blake2RepoKeyLegacy ,
1009+ Blake2AuthenticatedKeyLegacy ,
9881010)
9891011
9901012AVAILABLE_KEY_TYPES = (
9911013 # these are available encryption modes for new repositories
9921014 # not encrypted modes
9931015 PlaintextKey ,
9941016 AuthenticatedKey ,
995- Blake2AuthenticatedKey ,
9961017 # new crypto
9971018 AESOCBKeyfileKey ,
9981019 AESOCBRepoKey ,
9991020 CHPOKeyfileKey ,
10001021 CHPORepoKey ,
1022+ Blake2AuthenticatedKey ,
10011023 Blake2AESOCBKeyfileKey ,
10021024 Blake2AESOCBRepoKey ,
10031025 Blake2CHPOKeyfileKey ,
0 commit comments