Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibCrypto: RSA encryption/decryption and modes with OpenSSL #3234

Merged
merged 10 commits into from
Jan 13, 2025
4 changes: 4 additions & 0 deletions Libraries/LibCrypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ set(SOURCES
Curves/Ed448.cpp
Curves/X25519.cpp
Curves/X448.cpp
Hash/BLAKE2b.cpp
Hash/MD5.cpp
Hash/SHA1.cpp
Hash/SHA2.cpp
NumberTheory/ModularFunctions.cpp
PK/RSA.cpp
PK/EC.cpp
Expand Down
18 changes: 18 additions & 0 deletions Libraries/LibCrypto/Hash/BLAKE2b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2025, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibCrypto/Hash/BLAKE2b.h>

#include <openssl/evp.h>

namespace Crypto::Hash {

BLAKE2b::BLAKE2b(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_blake2b512(), context)
{
}

}
5 changes: 1 addition & 4 deletions Libraries/LibCrypto/Hash/BLAKE2b.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class BLAKE2b final : public OpenSSLHashFunction<BLAKE2b, 1024, 512> {
AK_MAKE_NONCOPYABLE(BLAKE2b);

public:
explicit BLAKE2b(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_blake2b512(), context)
{
}
explicit BLAKE2b(EVP_MD_CTX* context);

virtual ByteString class_name() const override
{
Expand Down
18 changes: 18 additions & 0 deletions Libraries/LibCrypto/Hash/MD5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2025, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibCrypto/Hash/MD5.h>

#include <openssl/evp.h>

namespace Crypto::Hash {

MD5::MD5(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_md5(), context)
{
}

}
5 changes: 1 addition & 4 deletions Libraries/LibCrypto/Hash/MD5.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class MD5 final : public OpenSSLHashFunction<MD5, 512, 128> {
AK_MAKE_NONCOPYABLE(MD5);

public:
explicit MD5(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_md5(), context)
{
}
explicit MD5(EVP_MD_CTX* context);

virtual ByteString class_name() const override
{
Expand Down
3 changes: 1 addition & 2 deletions Libraries/LibCrypto/Hash/OpenSSLHashFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include <AK/NonnullOwnPtr.h>
#include <AK/Types.h>
#include <LibCrypto/Hash/HashFunction.h>

#include <openssl/evp.h>
#include <LibCrypto/OpenSSLForward.h>

namespace Crypto::Hash {

Expand Down
18 changes: 18 additions & 0 deletions Libraries/LibCrypto/Hash/SHA1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2025, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibCrypto/Hash/SHA1.h>

#include <openssl/evp.h>

namespace Crypto::Hash {

SHA1::SHA1(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha1(), context)
{
}

}
5 changes: 1 addition & 4 deletions Libraries/LibCrypto/Hash/SHA1.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class SHA1 final : public OpenSSLHashFunction<SHA1, 512, 160> {
AK_MAKE_NONCOPYABLE(SHA1);

public:
explicit SHA1(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha1(), context)
{
}
explicit SHA1(EVP_MD_CTX* context);

virtual ByteString class_name() const override
{
Expand Down
28 changes: 28 additions & 0 deletions Libraries/LibCrypto/Hash/SHA2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibCrypto/Hash/SHA2.h>

#include <openssl/evp.h>

namespace Crypto::Hash {

SHA256::SHA256(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha256(), context)
{
}

SHA384::SHA384(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha384(), context)
{
}

SHA512::SHA512(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha512(), context)
{
}

}
15 changes: 3 additions & 12 deletions Libraries/LibCrypto/Hash/SHA2.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ class SHA256 final : public OpenSSLHashFunction<SHA256, 512, 256> {
AK_MAKE_NONCOPYABLE(SHA256);

public:
explicit SHA256(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha256(), context)
{
}
explicit SHA256(EVP_MD_CTX* context);

virtual ByteString class_name() const override
{
Expand All @@ -30,10 +27,7 @@ class SHA384 final : public OpenSSLHashFunction<SHA384, 1024, 384> {
AK_MAKE_NONCOPYABLE(SHA384);

public:
explicit SHA384(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha384(), context)
{
}
explicit SHA384(EVP_MD_CTX* context);

virtual ByteString class_name() const override
{
Expand All @@ -45,10 +39,7 @@ class SHA512 final : public OpenSSLHashFunction<SHA512, 1024, 512> {
AK_MAKE_NONCOPYABLE(SHA512);

public:
explicit SHA512(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha512(), context)
{
}
explicit SHA512(EVP_MD_CTX* context);

virtual ByteString class_name() const override
{
Expand Down
17 changes: 17 additions & 0 deletions Libraries/LibCrypto/OpenSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@

#include <AK/ByteBuffer.h>
#include <LibCrypto/OpenSSL.h>

#include <openssl/bn.h>
#include <openssl/evp.h>

namespace Crypto {

ErrorOr<OpenSSL_BN> OpenSSL_BN::create()
{
return OpenSSL_BN(OPENSSL_TRY_PTR(BN_new()));
}

ErrorOr<OpenSSL_PKEY> OpenSSL_PKEY::create()
{
return OpenSSL_PKEY(OPENSSL_TRY_PTR(EVP_PKEY_new()));
}

ErrorOr<OpenSSL_MD_CTX> OpenSSL_MD_CTX::create()
{
return OpenSSL_MD_CTX(OPENSSL_TRY_PTR(EVP_MD_CTX_new()));
}

ErrorOr<OpenSSL_BN> unsigned_big_integer_to_openssl_bignum(UnsignedBigInteger const& integer)
{
auto bn = TRY(OpenSSL_BN::create());
Expand Down
20 changes: 4 additions & 16 deletions Libraries/LibCrypto/OpenSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
#include <AK/Error.h>
#include <AK/Format.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>

#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <LibCrypto/OpenSSLForward.h>

namespace Crypto {

Expand Down Expand Up @@ -88,20 +85,14 @@ class OpenSSL_BN {
OPENSSL_WRAPPER_CLASS(OpenSSL_BN, BIGNUM, BN);

public:
static ErrorOr<OpenSSL_BN> create()
{
return OpenSSL_BN(OPENSSL_TRY_PTR(BN_new()));
}
static ErrorOr<OpenSSL_BN> create();
};

class OpenSSL_PKEY {
OPENSSL_WRAPPER_CLASS(OpenSSL_PKEY, EVP_PKEY, EVP_PKEY);

public:
static ErrorOr<OpenSSL_PKEY> create()
{
return OpenSSL_PKEY(OPENSSL_TRY_PTR(EVP_PKEY_new()));
}
static ErrorOr<OpenSSL_PKEY> create();
};

class OpenSSL_PKEY_CTX {
Expand All @@ -112,10 +103,7 @@ class OpenSSL_MD_CTX {
OPENSSL_WRAPPER_CLASS(OpenSSL_MD_CTX, EVP_MD_CTX, EVP_MD_CTX);

public:
static ErrorOr<OpenSSL_MD_CTX> create()
{
return OpenSSL_MD_CTX(OPENSSL_TRY_PTR(EVP_MD_CTX_new()));
}
static ErrorOr<OpenSSL_MD_CTX> create();
};

#undef OPENSSL_WRAPPER_CLASS
Expand Down
32 changes: 32 additions & 0 deletions Libraries/LibCrypto/OpenSSLForward.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2025, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

extern "C" {
typedef struct engine_st ENGINE;
typedef struct bignum_st BIGNUM;
typedef struct evp_md_st EVP_MD;
typedef struct evp_md_ctx_st EVP_MD_CTX;
typedef struct evp_pkey_st EVP_PKEY;
typedef struct evp_pkey_ctx_st EVP_PKEY_CTX;

long unsigned int ERR_get_error();
char* ERR_error_string(long unsigned int, char*);

EVP_MD_CTX* EVP_MD_CTX_new();
void EVP_MD_CTX_free(EVP_MD_CTX*);
int EVP_DigestUpdate(EVP_MD_CTX*, void const*, size_t);
int EVP_DigestInit_ex(EVP_MD_CTX*, const EVP_MD*, ENGINE*);
int EVP_DigestFinal_ex(EVP_MD_CTX*, unsigned char*, unsigned int*);
int EVP_MD_CTX_copy_ex(EVP_MD_CTX*, EVP_MD_CTX const*);

void EVP_PKEY_CTX_free(EVP_PKEY_CTX*);

void EVP_PKEY_free(EVP_PKEY*);

void BN_free(BIGNUM*);
}
35 changes: 0 additions & 35 deletions Libraries/LibCrypto/PK/Code/Code.h

This file was deleted.

Loading
Loading