Skip to content

Commit

Permalink
ed448 stuff, needs sha3
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 18, 2024
1 parent ce65457 commit f0a957c
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 6 deletions.
1 change: 1 addition & 0 deletions Libraries/LibCrypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(SOURCES
Cipher/ChaCha20.cpp
Curves/Curve25519.cpp
Curves/Ed25519.cpp
Curves/Ed448.cpp
Curves/X25519.cpp
Curves/X448.cpp
Hash/BLAKE2b.cpp
Expand Down
3 changes: 2 additions & 1 deletion Libraries/LibCrypto/Certificate/Certificate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static ErrorOr<AlgorithmIdentifier> parse_algorithm_identifier(Crypto::ASN1::Dec
READ_OBJECT(ObjectIdentifier, Vector<int>, algorithm);
POP_SCOPE();

constexpr static Array<Span<int const>, 12> known_algorithm_identifiers {
constexpr static Array<Span<int const>, 13> known_algorithm_identifiers {
ASN1::rsa_encryption_oid,
ASN1::rsa_md5_encryption_oid,
ASN1::rsa_sha1_encryption_oid,
Expand All @@ -95,6 +95,7 @@ static ErrorOr<AlgorithmIdentifier> parse_algorithm_identifier(Crypto::ASN1::Dec
ASN1::x25519_oid,
ASN1::ed25519_oid,
ASN1::x448_oid,
ASN1::ed448_oid,
};

bool is_known_algorithm = false;
Expand Down
32 changes: 32 additions & 0 deletions Libraries/LibCrypto/Curves/Ed448.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Random.h>
#include <LibCrypto/Curves/Ed448.h>

namespace Crypto::Curves {

// https://datatracker.ietf.org/doc/html/rfc8032#section-5.2.5
ErrorOr<ByteBuffer> Ed448::generate_private_key()
{
// The private key is 57 octets (456 bits, corresponding to b) of
// cryptographically secure random data. See [RFC4086] for a discussion
// about randomness.

auto buffer = TRY(ByteBuffer::create_uninitialized(key_size()));
fill_with_random(buffer);
return buffer;
}

// https://datatracker.ietf.org/doc/html/rfc8032#section-5.2.5
ErrorOr<ByteBuffer> Ed448::generate_public_key(ReadonlyBytes private_key)
{
// The 57-byte public key is generated by the following steps:

return TRY(ByteBuffer::copy(private_key)); // FIXME
}

}
22 changes: 22 additions & 0 deletions Libraries/LibCrypto/Curves/Ed448.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/ByteBuffer.h>

namespace Crypto::Curves {

class Ed448 {
public:

size_t key_size() { return 57; }
size_t signature_size() { return 0; }
ErrorOr<ByteBuffer> generate_private_key();
ErrorOr<ByteBuffer> generate_public_key(ReadonlyBytes private_key);
};

}
483 changes: 483 additions & 0 deletions Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions Libraries/LibWeb/Crypto/CryptoAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,24 @@ class ED25519 : public AlgorithmMethods {
}
};

class ED448 : public AlgorithmMethods {
public:
virtual WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> sign(AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;
virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;

virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<GC::Ref<JS::Object>> export_key(Bindings::KeyFormat, GC::Ref<CryptoKey>) override;

static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new ED448(realm)); }

private:
explicit ED448(JS::Realm& realm)
: AlgorithmMethods(realm)
{
}
};

class X25519 : public AlgorithmMethods {
public:
virtual WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;
Expand Down Expand Up @@ -620,6 +638,20 @@ struct EcKeyImportParams : public AlgorithmParams {
static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
};

// https://wicg.github.io/webcrypto-secure-curves/#dfn-Ed448Params
struct Ed448Params : public AlgorithmParams {
virtual ~Ed448Params() override;

Ed448Params(Optional<ByteBuffer>& context)
: context(context)
{
}

Optional<ByteBuffer> context;

static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
};

ErrorOr<String> base64_url_uint_encode(::Crypto::UnsignedBigInteger);
WebIDL::ExceptionOr<ByteBuffer> base64_url_bytes_decode(JS::Realm&, String const& base64_url_string);
WebIDL::ExceptionOr<::Crypto::UnsignedBigInteger> base64_url_uint_decode(JS::Realm&, String const& base64_url_string);
Expand Down
10 changes: 5 additions & 5 deletions Libraries/LibWeb/Crypto/SubtleCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,11 +1181,11 @@ SupportedAlgorithmsMap const& supported_algorithms()
define_an_algorithm<ED25519>("exportKey"_string, "Ed25519"_string);

// https://wicg.github.io/webcrypto-secure-curves/#ed448-registration
// FIXME: define_an_algorithm<ED448, Ed448Params>("sign"_string, "Ed448"_string);
// FIXME: define_an_algorithm<ED448, Ed448Params>("verify"_string, "Ed448"_string);
// FIXME: define_an_algorithm<ED448>("generateKey"_string, "Ed448"_string);
// FIXME: define_an_algorithm<ED448>("importKey"_string, "Ed448"_string);
// FIXME: define_an_algorithm<ED448>("exportKey"_string, "Ed448"_string);
define_an_algorithm<ED448, Ed448Params>("sign"_string, "Ed448"_string);
define_an_algorithm<ED448, Ed448Params>("verify"_string, "Ed448"_string);
define_an_algorithm<ED448>("generateKey"_string, "Ed448"_string);
define_an_algorithm<ED448>("importKey"_string, "Ed448"_string);
define_an_algorithm<ED448>("exportKey"_string, "Ed448"_string);

return internal_object;
}
Expand Down

0 comments on commit f0a957c

Please sign in to comment.