From d4f25ac2669c7ebf3d391b9ab159a901b5975d15 Mon Sep 17 00:00:00 2001 From: Mrityunjay Raj Date: Tue, 26 May 2026 16:52:50 +0530 Subject: [PATCH] legacy: move pbkdf2 static method from FlexiKey to Pbkdf2FileMixin, refs #9556 --- src/borg/crypto/key.py | 8 +------- src/borg/legacy/crypto/key.py | 7 +++++++ src/borg/testsuite/crypto/crypto_test.py | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/borg/crypto/key.py b/src/borg/crypto/key.py index 555d8b5ef0..d6236b8f3a 100644 --- a/src/borg/crypto/key.py +++ b/src/borg/crypto/key.py @@ -2,7 +2,7 @@ import hmac import os import textwrap -from hashlib import sha256, pbkdf2_hmac +from hashlib import sha256 from pathlib import Path from typing import Literal, ClassVar from collections.abc import Callable @@ -443,12 +443,6 @@ def decrypt_key_file(self, data, passphrase): else: raise UnsupportedKeyFormatError() - @staticmethod - def pbkdf2(passphrase, salt, iterations, output_len_in_bytes): - if os.environ.get("BORG_TESTONLY_WEAKEN_KDF") == "1": - iterations = 1 - return pbkdf2_hmac("sha256", passphrase.encode("utf-8"), salt, iterations, output_len_in_bytes) - @staticmethod def argon2( passphrase: str, diff --git a/src/borg/legacy/crypto/key.py b/src/borg/legacy/crypto/key.py index 9ce6d2ef35..27e4c71419 100644 --- a/src/borg/legacy/crypto/key.py +++ b/src/borg/legacy/crypto/key.py @@ -1,5 +1,6 @@ import hmac import os +from hashlib import pbkdf2_hmac from ...constants import * # NOQA from ...crypto.low_level import AES256_CTR_HMAC_SHA256, AES256_CTR_BLAKE2b, hmac_sha256 @@ -12,6 +13,12 @@ class Pbkdf2FileMixin: """Mixin for borg 1.x key files encrypted with PBKDF2 + AES-CTR.""" + @staticmethod + def pbkdf2(passphrase, salt, iterations, output_len_in_bytes): + if os.environ.get("BORG_TESTONLY_WEAKEN_KDF") == "1": + iterations = 1 + return pbkdf2_hmac("sha256", passphrase.encode("utf-8"), salt, iterations, output_len_in_bytes) + def decrypt_key_file(self, data, passphrase): unpacker = get_limited_unpacker("key") unpacker.feed(data) diff --git a/src/borg/testsuite/crypto/crypto_test.py b/src/borg/testsuite/crypto/crypto_test.py index 5d32e39e7b..76c9410ba5 100644 --- a/src/borg/testsuite/crypto/crypto_test.py +++ b/src/borg/testsuite/crypto/crypto_test.py @@ -9,7 +9,7 @@ from ...crypto.low_level import hmac_sha256 from ...legacy.crypto.low_level import AES from hashlib import sha256 -from ...crypto.key import CHPOKeyfileKey, AESOCBRepoKey, FlexiKey, KeyBase, PlaintextKey +from ...crypto.key import CHPOKeyfileKey, AESOCBRepoKey, KeyBase, PlaintextKey from ...legacy.crypto.key import KeyfileKey as LegacyKeyfileKey from ...helpers import msgpack, bin_to_hex @@ -228,7 +228,7 @@ def test_decrypt_key_file_pbkdf2_sha256_aes256_ctr_hmac_sha256(): plain = b"hello" salt = b"salt" * 4 passphrase = "hello, pass phrase" - key = FlexiKey.pbkdf2(passphrase, salt, 1, 32) + key = LegacyKeyfileKey.pbkdf2(passphrase, salt, 1, 32) hash = hmac_sha256(key, plain) data = AES(key, b"\0" * 16).encrypt(plain) encrypted = msgpack.packb(