diff --git a/libretroshare/src/util/i2pcommon.cpp b/libretroshare/src/util/i2pcommon.cpp index 0733a807d..524219eb3 100644 --- a/libretroshare/src/util/i2pcommon.cpp +++ b/libretroshare/src/util/i2pcommon.cpp @@ -50,8 +50,10 @@ std::string publicKeyFromPrivate(std::string const &priv) * https://geti2p.net/spec/common-structures#keysandcert * https://geti2p.net/spec/common-structures#certificate */ - if (priv.empty() || priv.length() < 884) // base64 ( = 663 bytes = KeyCert + priv Keys) + if (priv.length() < privKeyMinLenth_b64) { + RS_WARN("key to short!"); return std::string(); + } // creat a copy to work on, need to convert it to standard base64 auto priv_copy(priv); @@ -163,8 +165,10 @@ std::string publicKeyFromPrivate(std::string const &priv) bool getKeyTypes(const std::string &key, std::string &signingKey, std::string &cryptoKey) { - if (key.length() < 522) // base64 (391 bytes = 384 bytes + 7 bytes = KeysAndCert + Certificate) + if (key.length() < pubKeyMinLenth_b64) { + RS_WARN("key to short!"); return false; + } // creat a copy to work on, need to convert it to standard base64 auto key_copy(key); diff --git a/libretroshare/src/util/i2pcommon.h b/libretroshare/src/util/i2pcommon.h index 0a76fa080..f0da0322b 100644 --- a/libretroshare/src/util/i2pcommon.h +++ b/libretroshare/src/util/i2pcommon.h @@ -186,6 +186,39 @@ static const std::array, 12> signingKeyLengths { /*SigningKeyType::RedDSA_SHA512_Ed25519 */ std::make_pair( 32, 32), }; +/* + * Key length infos: + * + * BOB private key + * len b64: 884 + * len pln: 663 + * + * BOB public key / destination + * len b64: 516 + * len pln: 387 + * + * SAMv3 private key + * len b64: 908 + * len pln: 679 + * + * SAMv3 public key + * len b64: 516 + * len pln: 387 + * + * Example: + * in bytes, public key only + * 384 (Key) + 3 (Null certificate) = 387 bytes + * 384 (Key) + 7 (key certificate) = 391 bytes + * + * in bytes public + private key + * 384 (Key) + 3 (Null certificate) + 256 (ElGamal) + 20 (DSA_SHA1) = 663 bytes + * 384 (Key) + 7 (key certificate) + 256 (ElGamal) + 32 (EdDSA_SHA512_Ed25519) = 679 bytes + */ +constexpr size_t pubKeyMinLenth_b64 = 516; +constexpr size_t pubKeyMinLenth_bin = 387; +constexpr size_t privKeyMinLenth_b64 = 884; +constexpr size_t privKeyMinLenth_bin = 663; + /** * @brief makeOption Creates the string "lhs=rhs" used by BOB and SAM. Converts rhs * @param lhs option to set