LibCrypto: Cleanup Crypto::PK::RSA constructors to avoid pitfalls

- Removed the constructor taking a (n, d, e) tuple and moved
  it to `RSAPrivateKey`
- Removed default constructor with key generation because it was always
  misused and the default key size is quite small
- Added utility constructors to accept a key pair, public key, private
  key or both
- Made constructor parameters const
- Updated test to use generated random keys where possible
This commit is contained in:
devgianlu 2024-12-15 16:13:31 +01:00 committed by Ali Mohammad Pur
commit ec990d620f
Notes: github-actions[bot] 2024-12-15 22:32:47 +00:00
5 changed files with 31 additions and 30 deletions

View file

@ -7,7 +7,6 @@
#pragma once
#include <AK/Span.h>
#include <LibCrypto/ASN1/DER.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibCrypto/NumberTheory/ModularFunctions.h>
@ -64,6 +63,14 @@ private:
template<typename Integer = UnsignedBigInteger>
class RSAPrivateKey {
public:
RSAPrivateKey(Integer n, Integer d, Integer e)
: m_modulus(move(n))
, m_private_exponent(move(d))
, m_public_exponent(move(e))
, m_length(m_modulus.trimmed_length() * sizeof(u32))
{
}
RSAPrivateKey(Integer n, Integer d, Integer e, Integer p, Integer q)
: m_modulus(move(n))
, m_private_exponent(move(d))
@ -175,17 +182,27 @@ public:
return keys;
}
RSA(IntegerType n, IntegerType d, IntegerType e)
RSA(KeyPairType const& pair)
: PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>>(pair.public_key, pair.private_key)
{
m_public_key.set(n, e);
m_private_key = { n, d, e, 0, 0, 0, 0, 0 };
}
RSA(PublicKeyType& pubkey, PrivateKeyType& privkey)
RSA(PublicKeyType const& pubkey, PrivateKeyType const& privkey)
: PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>>(pubkey, privkey)
{
}
RSA(PrivateKeyType const& privkey)
{
m_private_key = privkey;
m_public_key.set(m_private_key.modulus(), m_private_key.public_exponent());
}
RSA(PublicKeyType const& pubkey)
{
m_public_key = pubkey;
}
RSA(ByteBuffer const& publicKeyPEM, ByteBuffer const& privateKeyPEM)
{
import_public_key(publicKeyPEM);
@ -198,14 +215,6 @@ public:
m_public_key.set(m_private_key.modulus(), m_private_key.public_exponent());
}
// create our own keys
RSA()
{
auto pair = generate_key_pair();
m_public_key = pair.public_key;
m_private_key = pair.private_key;
}
virtual void encrypt(ReadonlyBytes in, Bytes& out) override;
virtual void decrypt(ReadonlyBytes in, Bytes& out) override;