Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argon2 the first part: Implement key derivation (was: part 0) #6468

Merged
merged 10 commits into from
Mar 21, 2022
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
# using any other version is not supported by borg development and
# any feedback related to issues caused by this will be ignored.
'packaging',
'argon2-cffi',
]

# note for package maintainers: if you package borgbackup for distribution,
Expand Down
28 changes: 28 additions & 0 deletions src/borg/helpers/passphrase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess
import sys
from hashlib import pbkdf2_hmac
from typing import Literal

from . import bin_to_hex
from . import Error
Expand All @@ -12,6 +13,8 @@

from ..logger import create_logger

import argon2.low_level

logger = create_logger()


Expand Down Expand Up @@ -139,3 +142,28 @@ def __repr__(self):

def kdf(self, salt, iterations, length):
return pbkdf2_hmac('sha256', self.encode('utf-8'), salt, iterations, length)

def argon2(
self,
output_len_in_bytes: int,
salt: bytes,
time_cost,
memory_cost,
parallelism,
type: Literal['i', 'd', 'id']
) -> bytes:
type_map = {
'i': argon2.low_level.Type.I,
'd': argon2.low_level.Type.D,
'id': argon2.low_level.Type.ID,
}
key = argon2.low_level.hash_secret_raw(
ThomasWaldmann marked this conversation as resolved.
Show resolved Hide resolved
secret=self.encode("utf-8"),
hash_len=output_len_in_bytes,
salt=salt,
time_cost=time_cost,
memory_cost=memory_cost,
parallelism=parallelism,
type=type_map[type],
)
return key