[*] Improve PKCS1 OAEP support

This commit is contained in:
Reece Wilson 2024-02-19 11:15:10 +00:00
parent 600c7b68dc
commit 0461b54045
6 changed files with 48 additions and 8 deletions

View File

@ -17,10 +17,16 @@ namespace Aurora::Crypto::RSA
EPaddingType type,
Memory::ByteBuffer &out) = 0;
virtual bool Decrypt(const Memory::MemoryViewRead &payload,
EPaddingType type,
virtual bool Decrypt(const Memory::MemoryViewRead &payload,
EPaddingType type,
Memory::ByteBuffer &out) = 0;
// PKCS1 OAEP
virtual bool DecryptEx(const Memory::MemoryViewRead &payload,
EPaddingType type,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) = 0;
virtual AuSPtr<IRSAPublic> ToPublic() = 0;
virtual bool ToKey(const RSAMeta &meta, Memory::ByteBuffer &out) = 0;

View File

@ -9,7 +9,7 @@
namespace Aurora::Crypto::RSA
{
// Rememeber: there is no such thing as public decryption
// Remember: there is no such thing as public decryption
struct IRSAPublic
{
virtual bool Verify(const Memory::MemoryViewRead &plainText,
@ -18,9 +18,15 @@ namespace Aurora::Crypto::RSA
EPaddingType type) = 0;
virtual bool Encrypt(const Memory::MemoryViewRead &plainText,
EPaddingType type,
EPaddingType type,
Memory::ByteBuffer &out) = 0;
// PKCS1 OAEP
virtual bool EncryptEx(const Memory::MemoryViewRead &plainText,
EPaddingType type,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) = 0;
virtual bool ToKey(ERSAKeyType type, Memory::ByteBuffer &out) = 0;
};
}

View File

@ -103,8 +103,16 @@ namespace Aurora::Crypto::RSA
}
bool PrivateRSA::Decrypt(const AuMemoryViewRead &payload,
EPaddingType type,
EPaddingType type,
AuByteBuffer &out)
{
return DecryptEx(payload, type, AuHashing::kEHashTypeInvalid, out);
}
bool PrivateRSA::DecryptEx(const Memory::MemoryViewRead &payload,
EPaddingType type,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out)
{
if (!payload.HasMemory())
{
@ -119,6 +127,7 @@ namespace Aurora::Crypto::RSA
return false;
}
bool bAnnoying = padding == LTC_PKCS_1_OAEP;
const int prng_idx = padding == LTC_PKCS_1_PSS ? ::Crypto::gPrngYarrow : 0;
if (prng_idx < 0)
{
@ -140,7 +149,7 @@ namespace Aurora::Crypto::RSA
payload.length,
out.writePtr, &len,
NULL, 0,
0, 0, // hash? excuse me?
bAnnoying ? ::Crypto::HashMethodToId(method) : 0, -1,
padding,
&stat,
&this->key_);

View File

@ -23,6 +23,11 @@ namespace Aurora::Crypto::RSA
EPaddingType type,
Memory::ByteBuffer &out) override;
bool DecryptEx(const Memory::MemoryViewRead &payload,
EPaddingType type,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) override;
AuSPtr<IRSAPublic> ToPublic() override;
bool ToKey(const RSAMeta &meta,

View File

@ -86,8 +86,16 @@ namespace Aurora::Crypto::RSA
}
bool PublicRSA::Encrypt(const Memory::MemoryViewRead &plainText,
EPaddingType type,
EPaddingType type,
AuMemory::ByteBuffer &out)
{
return EncryptEx(plainText, type, AuHashing::kEHashTypeInvalid, out);
}
bool PublicRSA::EncryptEx(const Memory::MemoryViewRead &plainText,
EPaddingType type,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out)
{
int iRet {};
prng_state yarrow_prng;
@ -105,6 +113,7 @@ namespace Aurora::Crypto::RSA
return false;
}
bool bAnnoying = padding == LTC_PKCS_1_OAEP;
const int prng_idx = padding == LTC_PKCS_1_PSS ? ::Crypto::gPrngYarrow : 0;
if (prng_idx < 0)
{
@ -133,7 +142,7 @@ namespace Aurora::Crypto::RSA
out.writePtr, &len,
NULL, 0,
&yarrow_prng, prng_idx,
0, 0,
bAnnoying ? ::Crypto::HashMethodToId(method) : 0, -1,
padding,
&this->key_);
if (iRet != CRYPT_OK)

View File

@ -24,6 +24,11 @@ namespace Aurora::Crypto::RSA
EPaddingType type,
Memory::ByteBuffer &out) override;
bool EncryptEx(const Memory::MemoryViewRead &plainText,
EPaddingType type,
Aurora::Hashing::EHashType method,
Memory::ByteBuffer &out) override;
bool ToKey(ERSAKeyType type, Memory::ByteBuffer &out) override;
private: