@@ -132,12 +132,24 @@ def test_derive_key_with_different_hash(self):
132132
133133 def run_verify_full_size (self , curve_name : str , expected_base64_count : int ):
134134 """
135- Verifies that full base64url bytes is being emitted properly according to
136- https://datatracker.ietf.org/doc/html/rfc7518#section-6.2
135+ Verifies that the full-size keys (private and public) generated using the specified curve conform to the expected
136+ Base64-encoded string length for their respective components. The checks involve generating keys that could lead
137+ to truncated values when encoded and ensuring their lengths match the specified expectation.
138+
139+ See section: https://datatracker.ietf.org/doc/html/rfc7518#section-6.2
140+
141+ Parameters:
142+ curve_name (str): The name of the elliptic curve to use for key generation.
143+ expected_base64_count (int): The expected length of the Base64-encoded key components (x, y, d).
144+
145+ Raises:
146+ AssertionError: Raised if any of the generated private or public key components fail to match the expected lengths.
137147 """
138148 private_key = ECKey .generate_key (curve_name )
139149 # find the number which requires one less byte(octet) than a full padding
140- lower_cap = pow (2 , private_key .curve_key_size - 8 )
150+ byte_count = (private_key .curve_key_size + 7 ) // 8
151+ lower_cap = pow (2 , 8 * (byte_count - 1 ))
152+ attempts_remaining = 1000000
141153
142154 # now generate keys until we find a parameter which could be truncated
143155 while (
@@ -146,6 +158,9 @@ def run_verify_full_size(self, curve_name: str, expected_base64_count: int):
146158 and private_key .private_key .private_numbers ().private_value >= lower_cap
147159 ):
148160 private_key = ECKey .generate_key (curve_name )
161+ attempts_remaining -= 1
162+ if attempts_remaining == 0 :
163+ raise AssertionError ("Failed to find a key parameter that could be truncated" )
149164
150165 output_private = private_key .as_dict (private = True )
151166 self .assertEqual (expected_base64_count , len (output_private ["x" ]))
0 commit comments