1313from common .user_key import UserKey
1414from .peer_hub import PeerHub
1515
16- # TODO: Make this configurable
17- # TODO: The Shamir code also has a max (is that really needed?)
18- _MIN_NR_SHARES = 3 # The minimum number of key shares required to reconstruct the key.
19-
2016
2117class Client :
2218 """
@@ -29,40 +25,42 @@ class Client:
2925 _MAX_STORED_KEY_COUNT = 1_000 # Arbitrary large value
3026 _MAX_KEYS_PER_REQUEST = 1 # We don't support the number parameter for Get Key calls
3127
32- _name : str
33- _encryptor_names : list [str ]
34- _peer_hubs : list [PeerHub ]
28+ name : str
29+ encryptor_names : list [str ]
30+ peer_hubs : list [PeerHub ]
3531
36- def __init__ (self , name : str , encryptor_names : list [str ], peer_hub_urls : list [str ]):
37- self ._name = name
38- self ._encryptor_names = encryptor_names
39- self ._peer_hubs = []
32+ def __init__ (
33+ self ,
34+ name : str ,
35+ start_request_psrd_threshold : int ,
36+ stop_request_psrd_threshold : int ,
37+ get_psrd_block_size : int ,
38+ min_nr_shares : int ,
39+ encryptor_names : list [str ],
40+ peer_hub_urls : list [str ],
41+ ):
42+ self .name = name
43+ self .start_request_psrd_threshold = start_request_psrd_threshold
44+ self .stop_request_psrd_threshold = stop_request_psrd_threshold
45+ self .get_psrd_block_size = get_psrd_block_size
46+ self .min_nr_shares = min_nr_shares
47+ self .encryptor_names = encryptor_names
48+ self .peer_hubs = []
4049 for peer_hub_url in peer_hub_urls :
4150 peer_hub = PeerHub (self , peer_hub_url )
42- self ._peer_hubs .append (peer_hub )
43-
44- @property
45- def name (self ):
46- """
47- Get the name.
48- """
49- return self ._name
50-
51- @property
52- def encryptor_names (self ):
53- """
54- Get the encryptor names.
55- """
56- return self ._encryptor_names
51+ self .peer_hubs .append (peer_hub )
5752
5853 def to_mgmt (self ):
5954 """
6055 Get the management status.
6156 """
62- peer_hubs_status = [peer_hub .to_mgmt () for peer_hub in self ._peer_hubs ]
57+ peer_hubs_status = [peer_hub .to_mgmt () for peer_hub in self .peer_hubs ]
6358 return {
64- "name" : self ._name ,
65- "encryptor_names" : self ._encryptor_names ,
59+ "name" : self .name ,
60+ "start_request_psrd_threshold" : self .start_request_psrd_threshold ,
61+ "stop_request_psrd_threshold" : self .stop_request_psrd_threshold ,
62+ "get_psrd_block_size" : self .get_psrd_block_size ,
63+ "encryptor_names" : self .encryptor_names ,
6664 "peer_hubs" : peer_hubs_status ,
6765 }
6866
@@ -85,7 +83,7 @@ async def etsi_status(self, master_sae_id: str, slave_sae_id: str):
8583 # it is). For that reason, we return an arbitrary number as the stored key count.
8684 #
8785 return {
88- "source_kme_id" : self ._name ,
86+ "source_kme_id" : self .name ,
8987 "target_kme_id" : "" , # See comment above
9088 "master_sae_id" : master_sae_id ,
9189 "slave_sae_id" : slave_sae_id ,
@@ -154,7 +152,7 @@ def start_all_peer_hubs(self) -> None:
154152 """
155153 Start all peer hubs.
156154 """
157- for peer_hub in self ._peer_hubs :
155+ for peer_hub in self .peer_hubs :
158156 peer_hub .start_register_task ()
159157
160158 async def scatter_key_amongst_peer_hubs (
@@ -166,14 +164,14 @@ async def scatter_key_amongst_peer_hubs(
166164 """
167165 Split the key into key shares, and send each key share to a peer hub.
168166 """
169- nr_shares = len (self ._peer_hubs )
167+ nr_shares = len (self .peer_hubs )
170168 shares = key .split_into_shares (
171- master_sae_id , slave_sae_id , nr_shares , _MIN_NR_SHARES
169+ master_sae_id , slave_sae_id , nr_shares , self . min_nr_shares
172170 )
173171 assert len (shares ) == nr_shares
174172 coroutines = [
175173 peer_hub .post_share (master_sae_id , slave_sae_id , share )
176- for peer_hub , share in zip (self ._peer_hubs , shares )
174+ for peer_hub , share in zip (self .peer_hubs , shares )
177175 ]
178176 results = await asyncio .gather (* coroutines , return_exceptions = True )
179177 success_results = [
@@ -184,12 +182,12 @@ async def scatter_key_amongst_peer_hubs(
184182 f"Successfully scattered { nr_shares_successfully_scattered } out of { nr_shares } shares "
185183 f"for key ID { key .key_id } "
186184 )
187- if nr_shares_successfully_scattered < _MIN_NR_SHARES :
185+ if nr_shares_successfully_scattered < self . min_nr_shares :
188186 causes , status_code = self .summarize_failure (results )
189187 raise exceptions .CouldNotScatterEnoughSharesError (
190188 key .key_id ,
191189 nr_shares_successfully_scattered ,
192- _MIN_NR_SHARES ,
190+ self . min_nr_shares ,
193191 status_code ,
194192 causes ,
195193 )
@@ -204,10 +202,10 @@ async def gather_key_from_peer_hubs(
204202 Gather key shares from the peer hubs, and reconstruct the key out of (a subset of)
205203 the key shares.
206204 """
207- nr_shares_attempted_to_gather = len (self ._peer_hubs )
205+ nr_shares_attempted_to_gather = len (self .peer_hubs )
208206 coroutines = [
209207 peer_hub .get_share (master_sae_id , slave_sae_id , key_id )
210- for peer_hub in self ._peer_hubs
208+ for peer_hub in self .peer_hubs
211209 ]
212210 results = await asyncio .gather (* coroutines , return_exceptions = True )
213211 shares = [result for result in results if not isinstance (result , Exception )]
@@ -217,19 +215,19 @@ async def gather_key_from_peer_hubs(
217215 f"out of { nr_shares_attempted_to_gather } attempted "
218216 f"for key ID { key_id } "
219217 )
220- if nr_shares_successfully_gathered < _MIN_NR_SHARES :
218+ if nr_shares_successfully_gathered < self . min_nr_shares :
221219 causes , status_code = self .summarize_failure (results )
222220 raise exceptions .CouldNotGatherEnoughSharesError (
223221 key_id ,
224222 nr_shares_successfully_gathered ,
225- _MIN_NR_SHARES ,
223+ self . min_nr_shares ,
226224 status_code ,
227225 causes ,
228226 )
229227 shamir_input = [(share .share_index , share .value ) for share in shares ]
230228 try :
231229 key_value = shamir .reconstruct_binary_secret_from_shares (
232- _MIN_NR_SHARES , shamir_input
230+ self . min_nr_shares , shamir_input
233231 )
234232 except ValueError as exc :
235233 raise exceptions .ShamirReconstructError (key_id , str (exc )) from exc
0 commit comments