2121namespace TW {
2222
2323bool validateSignatureLength (TWPublicKeyType type, const Data& signature) {
24- std::size_t minLength = 64 ;
25- std::size_t maxLength = 64 ;
26-
2724 switch (type) {
2825 case TWPublicKeyTypeSECP256k1:
2926 case TWPublicKeyTypeSECP256k1Extended:
3027 case TWPublicKeyTypeNIST256p1:
3128 case TWPublicKeyTypeNIST256p1Extended: {
32- maxLength = 65 ;
33- break ;
29+ return signature. size () == PublicKey::signatureSize
30+ || signature. size () == PublicKey::secp256k1SignatureSize ;
3431 }
3532 default : {
36- break ;
33+ return signature. size () == PublicKey::signatureSize ;
3734 }
3835 }
39-
40- return minLength <= signature.size () && signature.size () <= maxLength;
4136}
4237
4338// / Determines if a collection of bytes makes a valid public key of the
@@ -97,7 +92,10 @@ PublicKey::PublicKey(const Data& data, enum TWPublicKeyType type)
9792 break ;
9893 case TWPublicKeyTypeED25519Blake2b:
9994 bytes.reserve (ed25519Size);
100- assert (data.size () == ed25519Size); // ensured by isValid() above
95+ // Sanity check, ensured by isValid() above.
96+ if (data.size () != ed25519Size) {
97+ throw std::invalid_argument (" Invalid public key size" );
98+ }
10199 std::copy (std::begin (data), std::end (data), std::back_inserter (bytes));
102100 break ;
103101 case TWPublicKeyTypeED25519Cardano:
@@ -110,21 +108,23 @@ PublicKey PublicKey::compressed() const {
110108 if (type != TWPublicKeyTypeSECP256k1Extended && type != TWPublicKeyTypeNIST256p1Extended) {
111109 return *this ;
112110 }
111+ if (bytes.size () < secp256k1ExtendedSize) {
112+ throw std::invalid_argument (" Invalid public key size" );
113+ }
113114
114115 Data newBytes (secp256k1Size);
115- assert (bytes.size () >= 65 );
116116 newBytes[0 ] = 0x02 | (bytes[64 ] & 0x01 );
117117
118- assert (type == TWPublicKeyTypeSECP256k1Extended || type == TWPublicKeyTypeNIST256p1Extended);
119118 switch (type) {
120119 case TWPublicKeyTypeSECP256k1Extended:
121120 std::copy (bytes.begin () + 1 , bytes.begin () + secp256k1Size, newBytes.begin () + 1 );
122121 return PublicKey (newBytes, TWPublicKeyTypeSECP256k1);
123122
124123 case TWPublicKeyTypeNIST256p1Extended:
125- default :
126124 std::copy (bytes.begin () + 1 , bytes.begin () + secp256k1Size, newBytes.begin () + 1 );
127125 return PublicKey (newBytes, TWPublicKeyTypeNIST256p1);
126+ default :
127+ return *this ;
128128 }
129129}
130130
@@ -220,6 +220,10 @@ bool PublicKey::verifyAsDER(const Data& signature, const Data& message) const {
220220}
221221
222222bool PublicKey::verifyZilliqa (const Data& signature, const Data& message) const {
223+ if (signature.size () != signatureSize && signature.size () != secp256k1SignatureSize) {
224+ return false ;
225+ }
226+
223227 switch (type) {
224228 case TWPublicKeyTypeSECP256k1:
225229 case TWPublicKeyTypeSECP256k1Extended:
@@ -279,7 +283,9 @@ bool PublicKey::isValidED25519() const {
279283 if (type != TWPublicKeyTypeED25519) {
280284 return false ;
281285 }
282- assert (bytes.size () == ed25519Size);
286+ if (bytes.size () != ed25519Size) {
287+ return false ;
288+ }
283289 ge25519 r;
284290 return ge25519_unpack_negative_vartime (&r, bytes.data ()) != 0 ;
285291}
0 commit comments