Source code for cadrumo.adapters.persistence.storage.master_key.master_key_derivation
"""Argon2id derivation with explicit, caller-supplied parameters.The cost constants and the no-parameter convenience wrapper that used them aredeleted with the file-backed master-key provider they belonged to. Everysurviving caller states its own Argon2id parameters, because those parametersare persisted beside the material they protect and must be read back from itrather than re-derived from a module constant that may since have moved."""from__future__importannotationsfromargon2.exceptionsimportArgon2Errorfromargon2.low_levelimportTypeas_Argon2Typefromargon2.low_levelimporthash_secret_rawas_argon2_hash_secret_rawfrom..crypto.aes_gcmimportKEY_SIZEfrom..errorsimportStorageValidationError
[docs]defderive_kek_with_params(passphrase:bytes,salt:bytes,*,memory_cost:int,time_cost:int,parallelism:int,)->bytes:"""Derive a 32-byte KEK with explicit persisted Argon2id parameters. Raises: StorageValidationError: When the library refuses the supplied material. The callers' records validate their parameters before reaching here, so this is the backstop for whatever those records do not constrain -- it exists because the alternative is an ``argon2.exceptions`` type escaping the storage boundary into a CLI that cannot render it. """try:return_argon2_hash_secret_raw(secret=passphrase,salt=salt,time_cost=time_cost,memory_cost=memory_cost,parallelism=parallelism,hash_len=KEY_SIZE,type=_Argon2Type.ID,)exceptArgon2Errorasexc:raiseStorageValidationError(f"Argon2id refused the supplied KDF material: {exc}")fromexc