mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-17 06:52:23 +00:00
LibCrypto: Add utility functions to SECPxxxr1
This allows to move ASN1 logic from inside the `SECPxxxr1` curve itself to the data structures. It makes more sense to have dedicated and explicit methods to handle transformation between formats.
This commit is contained in:
parent
0cd4c26ae8
commit
bce2893638
Notes:
github-actions[bot]
2024-12-07 18:09:49 +00:00
Author: https://github.com/devgianlu
Commit: bce2893638
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2823
Reviewed-by: https://github.com/alimpfard ✅
1 changed files with 39 additions and 0 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
#include <AK/Endian.h>
|
#include <AK/Endian.h>
|
||||||
|
#include <AK/Error.h>
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <AK/Random.h>
|
#include <AK/Random.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
|
@ -17,6 +18,11 @@
|
||||||
#include <LibCrypto/ASN1/DER.h>
|
#include <LibCrypto/ASN1/DER.h>
|
||||||
#include <LibCrypto/Curves/EllipticCurve.h>
|
#include <LibCrypto/Curves/EllipticCurve.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// Used by ASN1 macros
|
||||||
|
static String s_error_string;
|
||||||
|
}
|
||||||
|
|
||||||
namespace Crypto::Curves {
|
namespace Crypto::Curves {
|
||||||
|
|
||||||
struct SECPxxxr1CurveParameters {
|
struct SECPxxxr1CurveParameters {
|
||||||
|
@ -31,6 +37,18 @@ struct SECPxxxr1Point {
|
||||||
UnsignedBigInteger x;
|
UnsignedBigInteger x;
|
||||||
UnsignedBigInteger y;
|
UnsignedBigInteger y;
|
||||||
|
|
||||||
|
static ErrorOr<SECPxxxr1Point> from_uncompressed(ReadonlyBytes data)
|
||||||
|
{
|
||||||
|
if (data.size() < 1 || data[0] != 0x04)
|
||||||
|
return Error::from_string_literal("Invalid length or not an uncompressed SECPxxxr1 point");
|
||||||
|
|
||||||
|
auto half_size = (data.size() - 1) / 2;
|
||||||
|
return SECPxxxr1Point {
|
||||||
|
UnsignedBigInteger::import_data(data.slice(1, half_size)),
|
||||||
|
UnsignedBigInteger::import_data(data.slice(1 + half_size, half_size)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> to_uncompressed() const
|
ErrorOr<ByteBuffer> to_uncompressed() const
|
||||||
{
|
{
|
||||||
auto bytes = TRY(ByteBuffer::create_uninitialized(1 + x.byte_length() + y.byte_length()));
|
auto bytes = TRY(ByteBuffer::create_uninitialized(1 + x.byte_length() + y.byte_length()));
|
||||||
|
@ -44,6 +62,27 @@ struct SECPxxxr1Point {
|
||||||
struct SECPxxxr1Signature {
|
struct SECPxxxr1Signature {
|
||||||
UnsignedBigInteger r;
|
UnsignedBigInteger r;
|
||||||
UnsignedBigInteger s;
|
UnsignedBigInteger s;
|
||||||
|
|
||||||
|
static ErrorOr<SECPxxxr1Signature> from_asn(ReadonlyBytes signature, Vector<StringView> current_scope)
|
||||||
|
{
|
||||||
|
ASN1::Decoder decoder(signature);
|
||||||
|
ENTER_TYPED_SCOPE(Sequence, "SECPxxxr1Signature");
|
||||||
|
READ_OBJECT(Integer, UnsignedBigInteger, r_big_int);
|
||||||
|
READ_OBJECT(Integer, UnsignedBigInteger, s_big_int);
|
||||||
|
return SECPxxxr1Signature { r_big_int, s_big_int };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<ByteBuffer> to_asn()
|
||||||
|
{
|
||||||
|
ASN1::Encoder encoder;
|
||||||
|
TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {
|
||||||
|
TRY(encoder.write(r));
|
||||||
|
TRY(encoder.write(s));
|
||||||
|
return {};
|
||||||
|
}));
|
||||||
|
|
||||||
|
return encoder.finish();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t bit_size, SECPxxxr1CurveParameters const& CURVE_PARAMETERS>
|
template<size_t bit_size, SECPxxxr1CurveParameters const& CURVE_PARAMETERS>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue