mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
LibCrypto: Make RSA::generate_key_pair
return ErrorOr
Not currently needed as it cannot fail, but useful for future commits.
This commit is contained in:
parent
d5e3a557fd
commit
9e08f71fd9
Notes:
github-actions[bot]
2025-01-12 00:15:00 +00:00
Author: https://github.com/devgianlu
Commit: 9e08f71fd9
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3225
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/gmta ✅
4 changed files with 33 additions and 24 deletions
|
@ -114,6 +114,28 @@ ErrorOr<RSA::KeyPairType> RSA::parse_rsa_key(ReadonlyBytes der, bool is_private,
|
|||
}
|
||||
}
|
||||
|
||||
ErrorOr<RSA::KeyPairType> RSA::generate_key_pair(size_t bits, IntegerType e)
|
||||
{
|
||||
IntegerType p;
|
||||
IntegerType q;
|
||||
IntegerType lambda;
|
||||
|
||||
do {
|
||||
p = NumberTheory::random_big_prime(bits / 2);
|
||||
q = NumberTheory::random_big_prime(bits / 2);
|
||||
lambda = NumberTheory::LCM(p.minus(1), q.minus(1));
|
||||
} while (!(NumberTheory::GCD(e, lambda) == 1));
|
||||
|
||||
auto n = p.multiplied_by(q);
|
||||
|
||||
auto d = NumberTheory::ModularInverse(e, lambda);
|
||||
RSAKeyPair<PublicKeyType, PrivateKeyType> keys {
|
||||
{ n, e },
|
||||
{ n, d, e, p, q }
|
||||
};
|
||||
return keys;
|
||||
}
|
||||
|
||||
void RSA::encrypt(ReadonlyBytes in, Bytes& out)
|
||||
{
|
||||
dbgln_if(CRYPTO_DEBUG, "in size: {}", in.size());
|
||||
|
|
|
@ -160,27 +160,7 @@ public:
|
|||
using KeyPairType = RSAKeyPair<PublicKeyType, PrivateKeyType>;
|
||||
|
||||
static ErrorOr<KeyPairType> parse_rsa_key(ReadonlyBytes der, bool is_private, Vector<StringView> current_scope);
|
||||
static KeyPairType generate_key_pair(size_t bits = 256, IntegerType e = 65537)
|
||||
{
|
||||
IntegerType p;
|
||||
IntegerType q;
|
||||
IntegerType lambda;
|
||||
|
||||
do {
|
||||
p = NumberTheory::random_big_prime(bits / 2);
|
||||
q = NumberTheory::random_big_prime(bits / 2);
|
||||
lambda = NumberTheory::LCM(p.minus(1), q.minus(1));
|
||||
} while (!(NumberTheory::GCD(e, lambda) == 1));
|
||||
|
||||
auto n = p.multiplied_by(q);
|
||||
|
||||
auto d = NumberTheory::ModularInverse(e, lambda);
|
||||
RSAKeyPair<PublicKeyType, PrivateKeyType> keys {
|
||||
{ n, e },
|
||||
{ n, d, e, p, q }
|
||||
};
|
||||
return keys;
|
||||
}
|
||||
static ErrorOr<KeyPairType> generate_key_pair(size_t bits = 256, IntegerType e = 65537);
|
||||
|
||||
RSA(KeyPairType const& pair)
|
||||
: PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>>(pair.public_key, pair.private_key)
|
||||
|
|
|
@ -764,7 +764,11 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> RSAOAEP
|
|||
// and RSA public exponent equal to the publicExponent member of normalizedAlgorithm.
|
||||
// 3. If performing the operation results in an error, then throw an OperationError.
|
||||
auto const& normalized_algorithm = static_cast<RsaHashedKeyGenParams const&>(params);
|
||||
auto key_pair = ::Crypto::PK::RSA::generate_key_pair(normalized_algorithm.modulus_length, normalized_algorithm.public_exponent);
|
||||
auto maybe_key_pair = ::Crypto::PK::RSA::generate_key_pair(normalized_algorithm.modulus_length, normalized_algorithm.public_exponent);
|
||||
if (maybe_key_pair.is_error())
|
||||
return WebIDL::OperationError::create(m_realm, "Failed generating RSA key pair"_string);
|
||||
|
||||
auto key_pair = maybe_key_pair.release_value();
|
||||
|
||||
// 4. Let algorithm be a new RsaHashedKeyAlgorithm object.
|
||||
auto algorithm = RsaHashedKeyAlgorithm::create(m_realm);
|
||||
|
|
|
@ -36,7 +36,9 @@ TEST_CASE(test_RSA_raw_encrypt)
|
|||
TEST_CASE(test_RSA_PKCS_1_encrypt)
|
||||
{
|
||||
ByteBuffer data { "hellohellohellohellohellohellohellohellohello123-"_b };
|
||||
Crypto::PK::RSA_PKCS1_EME rsa(Crypto::PK::RSA::generate_key_pair(1024));
|
||||
|
||||
auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
|
||||
Crypto::PK::RSA_PKCS1_EME rsa(keypair);
|
||||
ByteBuffer buffer = {};
|
||||
buffer.resize(rsa.output_size());
|
||||
auto buf = buffer.bytes();
|
||||
|
@ -155,7 +157,8 @@ c8yGzl89pYST
|
|||
|
||||
TEST_CASE(test_RSA_encrypt_decrypt)
|
||||
{
|
||||
Crypto::PK::RSA rsa(Crypto::PK::RSA::generate_key_pair(1024));
|
||||
auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
|
||||
Crypto::PK::RSA rsa(keypair);
|
||||
|
||||
ByteBuffer enc_buffer = {};
|
||||
enc_buffer.resize(rsa.output_size());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue