| 1 |
from abc import ABC, abstractmethod |
| 2 |
import enum |
| 3 |
from typing_extensions import assert_never |
| 4 |
|
| 5 |
|
| 6 |
__all__ = [ |
| 7 |
"CryptoProvider", |
| 8 |
"HashFunction" |
| 9 |
] |
| 10 |
|
| 11 |
|
| 12 |
@enum.unique |
| 13 |
class HashFunction(enum.Enum): |
| 14 |
""" |
| 15 |
Enumeration of the hash functions supported for the key derivation step of X3DH. |
| 16 |
""" |
| 17 |
|
| 18 |
SHA_256 = "SHA_256" |
| 19 |
SHA_512 = "SHA_512" |
| 20 |
|
| 21 |
@property |
| 22 |
def hash_size(self) -> int: |
| 23 |
""" |
| 24 |
Returns: |
| 25 |
The byte size of the hashes produced by this hash function. |
| 26 |
""" |
| 27 |
|
| 28 |
if self is HashFunction.SHA_256: |
| 29 |
return 32 |
| 30 |
if self is HashFunction.SHA_512: |
| 31 |
return 64 |
| 32 |
|
| 33 |
return assert_never(self) |
| 34 |
|
| 35 |
|
| 36 |
class CryptoProvider(ABC): |
| 37 |
""" |
| 38 |
Abstraction of the cryptographic operations needed by this package to allow for different backend |
| 39 |
implementations. |
| 40 |
""" |
| 41 |
|
| 42 |
@staticmethod |
| 43 |
@abstractmethod |
| 44 |
async def hkdf_derive( |
| 45 |
hash_function: HashFunction, |
| 46 |
length: int, |
| 47 |
salt: bytes, |
| 48 |
info: bytes, |
| 49 |
key_material: bytes |
| 50 |
) -> bytes: |
| 51 |
""" |
| 52 |
Args: |
| 53 |
hash_function: The hash function to parameterize the HKDF with. |
| 54 |
length: The number of bytes to derive. |
| 55 |
salt: The salt input for the HKDF. |
| 56 |
info: The info input for the HKDF. |
| 57 |
key_material: The input key material to derive from. |
| 58 |
|
| 59 |
Returns: |
| 60 |
The derived key material. |
| 61 |
""" |