@@ -170,15 +170,57 @@ namespace libp2p::crypto::validator {
170170
171171 outcome::result<void > KeyValidatorImpl::validateEcdsa (
172172 const PrivateKey &key) const {
173- // TODO(xDimon): Check if it possible to validate ECDSA key by some way.
174- // issue: https://github.com/libp2p/cpp-libp2p/issues/103
173+ // Basic ECDSA private key validation
174+ // ECDSA private keys are typically 32 bytes for P-256, 48 bytes for P-384, 66 bytes for P-521
175+ if (key.data .empty ()) {
176+ return KeyValidatorError::INVALID_PRIVATE_KEY;
177+ }
178+
179+ // Check for reasonable key sizes (32-66 bytes covers most common curves)
180+ if (key.data .size () < 32 || key.data .size () > 66 ) {
181+ return KeyValidatorError::WRONG_PRIVATE_KEY_SIZE;
182+ }
183+
184+ // Check that the key is not all zeros (invalid private key)
185+ bool all_zeros = true ;
186+ for (const auto & byte : key.data ) {
187+ if (byte != 0 ) {
188+ all_zeros = false ;
189+ break ;
190+ }
191+ }
192+ if (all_zeros) {
193+ return KeyValidatorError::INVALID_PRIVATE_KEY;
194+ }
195+
175196 return outcome::success ();
176197 }
177198
178199 outcome::result<void > KeyValidatorImpl::validateEcdsa (
179200 const PublicKey &key) const {
180- // TODO(xDimon): Check if it possible to validate ECDSA key by some way.
181- // issue: https://github.com/libp2p/cpp-libp2p/issues/103
201+ // Basic ECDSA public key validation
202+ if (key.data .empty ()) {
203+ return KeyValidatorError::INVALID_PUBLIC_KEY;
204+ }
205+
206+ // ECDSA public keys are typically 64 bytes (uncompressed) or 33/49/67 bytes (compressed)
207+ // for P-256/P-384/P-521 respectively
208+ if (key.data .size () < 33 || key.data .size () > 133 ) {
209+ return KeyValidatorError::WRONG_PUBLIC_KEY_SIZE;
210+ }
211+
212+ // Check that the key is not all zeros (invalid public key)
213+ bool all_zeros = true ;
214+ for (const auto & byte : key.data ) {
215+ if (byte != 0 ) {
216+ all_zeros = false ;
217+ break ;
218+ }
219+ }
220+ if (all_zeros) {
221+ return KeyValidatorError::INVALID_PUBLIC_KEY;
222+ }
223+
182224 return outcome::success ();
183225 }
184226
0 commit comments