mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:37:45 +00:00
LibCrypto+LibTLS: Reformat everything
I have no idea how I'll squash _this_ one...
This commit is contained in:
parent
a1e1570552
commit
05e2c7d9cf
14 changed files with 1434 additions and 1426 deletions
|
@ -34,201 +34,201 @@
|
|||
|
||||
namespace Crypto {
|
||||
namespace PK {
|
||||
template <typename Integer = u64>
|
||||
class RSAPublicKey {
|
||||
public:
|
||||
RSAPublicKey(const Integer& n, const Integer& e)
|
||||
: m_modulus(n)
|
||||
, m_public_exponent(e)
|
||||
{
|
||||
}
|
||||
template<typename Integer = u64>
|
||||
class RSAPublicKey {
|
||||
public:
|
||||
RSAPublicKey(const Integer& n, const Integer& e)
|
||||
: m_modulus(n)
|
||||
, m_public_exponent(e)
|
||||
{
|
||||
}
|
||||
|
||||
RSAPublicKey()
|
||||
: m_modulus(0)
|
||||
, m_public_exponent(0)
|
||||
{
|
||||
}
|
||||
RSAPublicKey()
|
||||
: m_modulus(0)
|
||||
, m_public_exponent(0)
|
||||
{
|
||||
}
|
||||
|
||||
//--stuff it should do
|
||||
//--stuff it should do
|
||||
|
||||
const Integer& modulus() const { return m_modulus; }
|
||||
const Integer& public_exponent() const { return m_public_exponent; }
|
||||
size_t length() const { return m_length; }
|
||||
void set_length(size_t length) { m_length = length; }
|
||||
const Integer& modulus() const { return m_modulus; }
|
||||
const Integer& public_exponent() const { return m_public_exponent; }
|
||||
size_t length() const { return m_length; }
|
||||
void set_length(size_t length) { m_length = length; }
|
||||
|
||||
void set(const Integer& n, const Integer& e)
|
||||
{
|
||||
m_modulus = n;
|
||||
m_public_exponent = e;
|
||||
m_length = (n.trimmed_length() * sizeof(u32));
|
||||
}
|
||||
void set(const Integer& n, const Integer& e)
|
||||
{
|
||||
m_modulus = n;
|
||||
m_public_exponent = e;
|
||||
m_length = (n.trimmed_length() * sizeof(u32));
|
||||
}
|
||||
|
||||
private:
|
||||
Integer m_modulus;
|
||||
Integer m_public_exponent;
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
private:
|
||||
Integer m_modulus;
|
||||
Integer m_public_exponent;
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
|
||||
template <typename Integer = UnsignedBigInteger>
|
||||
class RSAPrivateKey {
|
||||
public:
|
||||
RSAPrivateKey(const Integer& n, const Integer& d, const Integer& e)
|
||||
: m_modulus(n)
|
||||
, m_private_exponent(d)
|
||||
, m_public_exponent(e)
|
||||
{
|
||||
}
|
||||
template<typename Integer = UnsignedBigInteger>
|
||||
class RSAPrivateKey {
|
||||
public:
|
||||
RSAPrivateKey(const Integer& n, const Integer& d, const Integer& e)
|
||||
: m_modulus(n)
|
||||
, m_private_exponent(d)
|
||||
, m_public_exponent(e)
|
||||
{
|
||||
}
|
||||
|
||||
RSAPrivateKey()
|
||||
{
|
||||
}
|
||||
RSAPrivateKey()
|
||||
{
|
||||
}
|
||||
|
||||
//--stuff it should do
|
||||
const Integer& modulus() const { return m_modulus; }
|
||||
const Integer& private_exponent() const { return m_private_exponent; }
|
||||
const Integer& public_exponent() const { return m_public_exponent; }
|
||||
size_t length() const { return m_length; }
|
||||
void set_length(size_t length) { m_length = length; }
|
||||
//--stuff it should do
|
||||
const Integer& modulus() const { return m_modulus; }
|
||||
const Integer& private_exponent() const { return m_private_exponent; }
|
||||
const Integer& public_exponent() const { return m_public_exponent; }
|
||||
size_t length() const { return m_length; }
|
||||
void set_length(size_t length) { m_length = length; }
|
||||
|
||||
void set(const Integer& n, const Integer& d, const Integer& e)
|
||||
{
|
||||
m_modulus = n;
|
||||
m_private_exponent = d;
|
||||
m_public_exponent = e;
|
||||
m_length = (n.length() * sizeof(u32));
|
||||
}
|
||||
void set(const Integer& n, const Integer& d, const Integer& e)
|
||||
{
|
||||
m_modulus = n;
|
||||
m_private_exponent = d;
|
||||
m_public_exponent = e;
|
||||
m_length = (n.length() * sizeof(u32));
|
||||
}
|
||||
|
||||
private:
|
||||
Integer m_modulus;
|
||||
Integer m_private_exponent;
|
||||
Integer m_public_exponent;
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
private:
|
||||
Integer m_modulus;
|
||||
Integer m_private_exponent;
|
||||
Integer m_public_exponent;
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
|
||||
template <typename PubKey, typename PrivKey>
|
||||
struct RSAKeyPair {
|
||||
PubKey public_key;
|
||||
PrivKey private_key;
|
||||
};
|
||||
template<typename PubKey, typename PrivKey>
|
||||
struct RSAKeyPair {
|
||||
PubKey public_key;
|
||||
PrivKey private_key;
|
||||
};
|
||||
|
||||
using IntegerType = UnsignedBigInteger;
|
||||
class RSA : public PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>> {
|
||||
template <typename T>
|
||||
friend class RSA_EMSA_PSS;
|
||||
using IntegerType = UnsignedBigInteger;
|
||||
class RSA : public PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>> {
|
||||
template<typename T>
|
||||
friend class RSA_EMSA_PSS;
|
||||
|
||||
public:
|
||||
using KeyPairType = RSAKeyPair<PublicKeyType, PrivateKeyType>;
|
||||
public:
|
||||
using KeyPairType = RSAKeyPair<PublicKeyType, PrivateKeyType>;
|
||||
|
||||
static KeyPairType parse_rsa_key(const ByteBuffer&);
|
||||
static KeyPairType generate_key_pair(size_t bits = 256)
|
||||
{
|
||||
IntegerType e { 65537 }; // :P
|
||||
IntegerType p, q;
|
||||
IntegerType lambda;
|
||||
static KeyPairType parse_rsa_key(const ByteBuffer&);
|
||||
static KeyPairType generate_key_pair(size_t bits = 256)
|
||||
{
|
||||
IntegerType e { 65537 }; // :P
|
||||
IntegerType p, q;
|
||||
IntegerType lambda;
|
||||
|
||||
do {
|
||||
p = NumberTheory::random_big_prime(bits / 2);
|
||||
q = NumberTheory::random_big_prime(bits / 2);
|
||||
lambda = NumberTheory::LCM(p.sub(1), q.sub(1));
|
||||
dbg() << "checking combination p=" << p << ", q=" << q << ", lambda=" << lambda.length();
|
||||
} while (!(NumberTheory::GCD(e, lambda) == 1));
|
||||
do {
|
||||
p = NumberTheory::random_big_prime(bits / 2);
|
||||
q = NumberTheory::random_big_prime(bits / 2);
|
||||
lambda = NumberTheory::LCM(p.sub(1), q.sub(1));
|
||||
dbg() << "checking combination p=" << p << ", q=" << q << ", lambda=" << lambda.length();
|
||||
} while (!(NumberTheory::GCD(e, lambda) == 1));
|
||||
|
||||
auto n = p.multiply(q);
|
||||
auto n = p.multiply(q);
|
||||
|
||||
auto d = NumberTheory::ModularInverse(e, lambda);
|
||||
dbg() << "Your keys are Pub{n=" << n << ", e=" << e << "} and Priv{n=" << n << ", d=" << d << "}";
|
||||
RSAKeyPair<PublicKeyType, PrivateKeyType> keys {
|
||||
{ n, e },
|
||||
{ n, d, e }
|
||||
};
|
||||
keys.public_key.set_length(bits / 2 / 8);
|
||||
keys.private_key.set_length(bits / 2 / 8);
|
||||
return keys;
|
||||
}
|
||||
auto d = NumberTheory::ModularInverse(e, lambda);
|
||||
dbg() << "Your keys are Pub{n=" << n << ", e=" << e << "} and Priv{n=" << n << ", d=" << d << "}";
|
||||
RSAKeyPair<PublicKeyType, PrivateKeyType> keys {
|
||||
{ n, e },
|
||||
{ n, d, e }
|
||||
};
|
||||
keys.public_key.set_length(bits / 2 / 8);
|
||||
keys.private_key.set_length(bits / 2 / 8);
|
||||
return keys;
|
||||
}
|
||||
|
||||
RSA(IntegerType n, IntegerType d, IntegerType e)
|
||||
{
|
||||
m_public_key.set(n, e);
|
||||
m_private_key.set(n, d, e);
|
||||
}
|
||||
RSA(IntegerType n, IntegerType d, IntegerType e)
|
||||
{
|
||||
m_public_key.set(n, e);
|
||||
m_private_key.set(n, d, e);
|
||||
}
|
||||
|
||||
RSA(PublicKeyType& pubkey, PrivateKeyType& privkey)
|
||||
: PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>>(pubkey, privkey)
|
||||
{
|
||||
}
|
||||
RSA(PublicKeyType& pubkey, PrivateKeyType& privkey)
|
||||
: PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>>(pubkey, privkey)
|
||||
{
|
||||
}
|
||||
|
||||
RSA(const ByteBuffer& publicKeyPEM, const ByteBuffer& privateKeyPEM)
|
||||
{
|
||||
import_public_key(publicKeyPEM);
|
||||
import_private_key(privateKeyPEM);
|
||||
}
|
||||
RSA(const ByteBuffer& publicKeyPEM, const ByteBuffer& privateKeyPEM)
|
||||
{
|
||||
import_public_key(publicKeyPEM);
|
||||
import_private_key(privateKeyPEM);
|
||||
}
|
||||
|
||||
RSA(const StringView& privKeyPEM)
|
||||
{
|
||||
import_private_key(ByteBuffer::wrap(privKeyPEM.characters_without_null_termination(), privKeyPEM.length()));
|
||||
m_public_key.set(m_private_key.modulus(), m_private_key.public_exponent());
|
||||
}
|
||||
RSA(const StringView& privKeyPEM)
|
||||
{
|
||||
import_private_key(ByteBuffer::wrap(privKeyPEM.characters_without_null_termination(), privKeyPEM.length()));
|
||||
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;
|
||||
}
|
||||
// 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(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void encrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
|
||||
virtual void sign(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void verify(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void sign(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void verify(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
|
||||
virtual String class_name() const override { return "RSA"; }
|
||||
virtual String class_name() const override { return "RSA"; }
|
||||
|
||||
virtual size_t output_size() const override { return m_public_key.length(); }
|
||||
virtual size_t output_size() const override { return m_public_key.length(); }
|
||||
|
||||
void import_public_key(const ByteBuffer& buffer, bool pem = true);
|
||||
void import_private_key(const ByteBuffer& buffer, bool pem = true);
|
||||
void import_public_key(const ByteBuffer& buffer, bool pem = true);
|
||||
void import_private_key(const ByteBuffer& buffer, bool pem = true);
|
||||
|
||||
const PrivateKeyType& private_key() const { return m_private_key; }
|
||||
const PublicKeyType& public_key() const { return m_public_key; }
|
||||
};
|
||||
const PrivateKeyType& private_key() const { return m_private_key; }
|
||||
const PublicKeyType& public_key() const { return m_public_key; }
|
||||
};
|
||||
|
||||
template <typename HashFunction>
|
||||
class RSA_EMSA_PSS {
|
||||
public:
|
||||
RSA_EMSA_PSS(RSA& rsa)
|
||||
: m_rsa(rsa)
|
||||
{
|
||||
}
|
||||
template<typename HashFunction>
|
||||
class RSA_EMSA_PSS {
|
||||
public:
|
||||
RSA_EMSA_PSS(RSA& rsa)
|
||||
: m_rsa(rsa)
|
||||
{
|
||||
}
|
||||
|
||||
void sign(const ByteBuffer& in, ByteBuffer& out);
|
||||
VerificationConsistency verify(const ByteBuffer& in);
|
||||
void sign(const ByteBuffer& in, ByteBuffer& out);
|
||||
VerificationConsistency verify(const ByteBuffer& in);
|
||||
|
||||
private:
|
||||
EMSA_PSS<HashFunction, HashFunction::DigestSize> m_emsa_pss;
|
||||
RSA m_rsa;
|
||||
};
|
||||
private:
|
||||
EMSA_PSS<HashFunction, HashFunction::DigestSize> m_emsa_pss;
|
||||
RSA m_rsa;
|
||||
};
|
||||
|
||||
class RSA_PKCS1_EME : public RSA {
|
||||
public:
|
||||
// forward all constructions to RSA
|
||||
template <typename... Args>
|
||||
RSA_PKCS1_EME(Args... args)
|
||||
: RSA(args...)
|
||||
{
|
||||
}
|
||||
class RSA_PKCS1_EME : public RSA {
|
||||
public:
|
||||
// forward all constructions to RSA
|
||||
template<typename... Args>
|
||||
RSA_PKCS1_EME(Args... args)
|
||||
: RSA(args...)
|
||||
{
|
||||
}
|
||||
|
||||
~RSA_PKCS1_EME() {}
|
||||
~RSA_PKCS1_EME() {}
|
||||
|
||||
virtual void encrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void encrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out) override;
|
||||
|
||||
virtual void sign(const ByteBuffer&, ByteBuffer&) override;
|
||||
virtual void verify(const ByteBuffer&, ByteBuffer&) override;
|
||||
virtual void sign(const ByteBuffer&, ByteBuffer&) override;
|
||||
virtual void verify(const ByteBuffer&, ByteBuffer&) override;
|
||||
|
||||
virtual String class_name() const override { return "RSA_PKCS1-EME"; }
|
||||
virtual size_t output_size() const override { return m_public_key.length(); }
|
||||
};
|
||||
virtual String class_name() const override { return "RSA_PKCS1-EME"; }
|
||||
virtual size_t output_size() const override { return m_public_key.length(); }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue