Source code for aeat.adapters.persistence.storage.master_key._kdf_params
"""Canonical Argon2id parameter record.Strict pydantic v2 model declaring the Argon2id KEK-derivation parametersthe substrate accepts. The :meth:`KdfParams.default` classmethodmaterialises the OWASP 2024 Password Storage Cheat Sheet baseline:- ``algorithm`` = ``"argon2id"``- ``version`` = ``19`` (Argon2 v1.3)- ``memory_cost`` = ``19 * 1024`` KiB (19 MiB)- ``time_cost`` = ``2`` iterations- ``parallelism`` = ``1`` lane- ``salt`` = 16 bytes- ``output_length`` = 32 bytesValidators reject parameter sets outside the supported window so atampered manifest cannot drive the KDF into a weaker regime at unlock."""from__future__importannotationsimportsecretsfromtypingimportTYPE_CHECKING,LiteralfrompydanticimportBaseModel,Field,field_serializer,field_validatorfrom.._kdf_saltimportKDF_SALT_BYTES,decode_kdf_salt,encode_kdf_salt,require_kdf_salt_lengthfrom..errorsimportStorageValidationErrorifTYPE_CHECKING:from..bucketimportManifestKdfParamsfrom.....coreimportSTRICT_FROZEN_CONFIGas_STRICT_FROZEN_SALT_BYTES=KDF_SALT_BYTES_OUTPUT_BYTES=32_ARGON2_V13=19_MIN_MEMORY_COST_KIB=19*1024_MAX_MEMORY_COST_KIB=1024*1024_MIN_TIME_COST=2_MAX_TIME_COST=16_MIN_PARALLELISM=1_MAX_PARALLELISM=8
[docs]classKdfParams(BaseModel):"""OWASP-baseline Argon2id parameters with strict validation. Distinct from the manifest-side :class:`adapters.persistence.storage.bucket.ManifestKdfParams` record: that record carries whatever parameter set the bucket was enrolled under (so a future cost-bump is non-breaking); this record pins the parameter window the substrate accepts for new enrolments and rejects anything outside it. """model_config=_STRICT_FROZENalgorithm:Literal["argon2id"]version:Literal[19]memory_cost:int=Field(ge=_MIN_MEMORY_COST_KIB,le=_MAX_MEMORY_COST_KIB)time_cost:int=Field(ge=_MIN_TIME_COST,le=_MAX_TIME_COST)parallelism:int=Field(ge=_MIN_PARALLELISM,le=_MAX_PARALLELISM)salt:bytesoutput_length:Literal[32]@field_validator("salt")@classmethoddef_check_salt_length(cls,value:bytes)->bytes:returnrequire_kdf_salt_length(value,error_type=StorageValidationError)@field_serializer("salt")def_serialise_salt(self,value:bytes)->str:returnencode_kdf_salt(value)@field_validator("salt",mode="before")@classmethoddef_decode_salt(cls,value:object)->bytes:returndecode_kdf_salt(value,error_type=StorageValidationError)
[docs]@classmethoddefdefault(cls)->KdfParams:"""Return a :class:`KdfParams` instance with the canonical OWASP 2024 Argon2id baseline parameters."""returncls(algorithm="argon2id",version=_ARGON2_V13,memory_cost=_MIN_MEMORY_COST_KIB,time_cost=_MIN_TIME_COST,parallelism=_MIN_PARALLELISM,salt=secrets.token_bytes(_SALT_BYTES),output_length=_OUTPUT_BYTES,)
[docs]defto_manifest_params(self)->ManifestKdfParams:"""Return this canonical parameter set as a :class:`ManifestKdfParams` bucket-manifest shape."""from..bucketimportManifestKdfParamsreturnManifestKdfParams.model_validate(self.model_dump())