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

18 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 14:58 +0000

1"""An abstract Base Class for Asymmetric Secret Keys 

2 

3This module contains only the interface specification for all key 

4classes implementations. 

5 

6""" 

7 

8from typing import Protocol, abstractmethod, runtime_checkable 

9 

10""" The Mongomery Curve X25519 group generator point. It is the point 

11with the smallest X coordinate which has the order greater than curve 

12cofactor (8). It has been proven that X=9. """ 

13key_x25519_generator_point = ( 

14 b"\x09\x00\x00\x00\x00\x00\x00\x00" 

15 b"\x00\x00\x00\x00\x00\x00\x00\x00" 

16 b"\x00\x00\x00\x00\x00\x00\x00\x00" 

17 b"\x00\x00\x00\x00\x00\x00\x00\x00" 

18) 

19 

20 

21@runtime_checkable 

22class Key(Protocol): 

23 """This is an abstract class, containing only abstract methods 

24 used to compute the Diffie-Hellman key exchange over the 

25 Montgomery curve Curve25519 as specified by the X25519 standard 

26 and auxilliary informative helpers. 

27 

28 """ 

29 

30 @property 

31 @abstractmethod 

32 def public_key(self) -> bytes: 

33 """The derived classes must implement providing corresponding 

34 public key in this method. 

35 

36 Returns: 

37 The 32 bytes of the public key. 

38 

39 """ 

40 ... 

41 

42 @abstractmethod 

43 def compute_write_key(self, reader_public_key: bytes) -> bytes: 

44 """Accepts the intended reader public key and computes the 

45 shared secret based on the public and secret key (this key) of 

46 the writer particular key source implementation. 

47 

48 Parameters: 

49 reader_public_key: the 32 bytes of the reader public key 

50 

51 Returns: 

52 The shared secret as 32 bytes - usable as symmetric key. 

53 

54 """ 

55 ... 

56 

57 @abstractmethod 

58 def compute_read_key(self, writer_public_key: bytes) -> bytes: 

59 """Accepts the writer public key and computes the shared 

60 secret based on the public and secret key (this key) of the 

61 reader particular key source implementation. 

62 

63 Parameters: 

64 writer_public_key: the 32 bytes of the writer public key 

65 

66 Returns: 

67 The shared secret as 32 bytes - usable as symmetric key. 

68 

69 """ 

70 ... 

71 

72 @property 

73 @abstractmethod 

74 def can_compute_symmetric_keys(self) -> bool: 

75 """A predicate returning true if this key instance can perform 

76 read/write key derivation. This is usually determined by 

77 having access to the private key (for software implementation) 

78 or some other means of working with the private key (for HSM). 

79 

80 Returns: 

81 true if it can perform symmetric key derivation 

82 

83 """ 

84 return False 

85 

86 def __bytes__(self) -> bytes: 

87 """Default converter to bytes returns the public key bytes.""" 

88 return self.public_key