Coverage for oarepo_c4gh/key/external_software.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-07 12:05 +0000

1"""This module implemens a virtual external key that is actually 

2backed by SoftwareKey and uses its private key directly. 

3 

4This module is intended ONLY for testing related functionality and 

5should NEVER be used in production. 

6 

7""" 

8 

9from nacl.bindings import crypto_scalarmult 

10from .external import ExternalKey 

11from ..exceptions import Crypt4GHKeyException 

12from .software import SoftwareKey 

13 

14 

15class ExternalSoftwareKey(ExternalKey): 

16 """This is a virtual external key backed by any SoftwareKey 

17 implementation. 

18 

19 Do NOT use this class in any production code. 

20 

21 """ 

22 

23 def __init__(self, softkey: SoftwareKey) -> None: 

24 """Gets its backing private+public key pair from the provided 

25 SoftwareKey implementation. 

26 

27 Do NOT use in production code. 

28 

29 Parameters: 

30 softkey: the backing key which must include private key 

31 

32 """ 

33 if not softkey.can_compute_symmetric_keys: 

34 raise Crypt4GHKeyException( 

35 "ExternalSoftwareKey needs a private key" 

36 ) 

37 self._private_key = softkey._private_key 

38 self._public_key = softkey._public_key 

39 

40 def compute_ecdh(self, public_point: bytes) -> bytes: 

41 """Computes directly the final result of ECDH from given 

42 public point. This implementation is using crypto_scalarmult 

43 from nacl.bindings. 

44 

45 Do NOT use in production code. 

46 

47 """ 

48 return crypto_scalarmult(self._private_key, public_point) 

49 

50 @property 

51 def public_key(self) -> bytes: 

52 """Returns the underlying public key.""" 

53 return self._public_key