Exploit database

GH-d040af49f7627d91b0e314e6400f7010

creeeeger/cve-2024-56426

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-56426

2 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars.

Repository files19
creeeeger/cve-2024-56426Revision 3d6d94a21066

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

external/tools/signature_crypto.py

Python · 3,730 bytes
# SPDX-License-Identifier: GPL-2.0-only
# SPDX-FileCopyrightText: 2026 Umer Uddin <[email protected]>
# SPDX-FileCopyrightText: 2026 Creeeeger <[email protected]>

"""ECDSA key and signature encoding helpers for Exynos boot images."""

from __future__ import annotations

from pathlib import Path

try:
    from .common import (
        ECDSA_COORD_SIZE,
        ECDSA_FIELD_SIZE,
        PUBKEY_BLOB_SIZE,
        SIGNATURE_BLOB_SIZE,
    )
except ImportError:
    from common import (
        ECDSA_COORD_SIZE,
        ECDSA_FIELD_SIZE,
        PUBKEY_BLOB_SIZE,
        SIGNATURE_BLOB_SIZE,
    )
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec, utils
from cryptography.hazmat.primitives.asymmetric.utils import (
    decode_dss_signature,
    encode_dss_signature,
)


def generate_padded_pub_key(public_key) -> bytes:
    numbers = public_key.public_numbers()

    x = numbers.x.to_bytes(ECDSA_COORD_SIZE, byteorder="big")
    y = numbers.y.to_bytes(ECDSA_COORD_SIZE, byteorder="big")
    blob = x.rjust(ECDSA_FIELD_SIZE, b"\x00") + y.rjust(ECDSA_FIELD_SIZE, b"\x00")
    return blob.ljust(PUBKEY_BLOB_SIZE, b"\x00")


def generate_padded_signature(r: int, s: int) -> bytes:
    r_bytes = r.to_bytes(ECDSA_COORD_SIZE, byteorder="big").rjust(ECDSA_FIELD_SIZE, b"\x00")
    s_bytes = s.to_bytes(ECDSA_COORD_SIZE, byteorder="big").rjust(ECDSA_FIELD_SIZE, b"\x00")
    return (r_bytes + s_bytes).ljust(SIGNATURE_BLOB_SIZE, b"\x00")


def load_private_key(path: str | Path):
    return serialization.load_pem_private_key(Path(path).read_bytes(), password=None)


def load_pubkey_blob(path: str | Path) -> bytes:
    blob = Path(path).read_bytes()
    if len(blob) != PUBKEY_BLOB_SIZE:
        raise ValueError(
            f"Public key blob must be {PUBKEY_BLOB_SIZE} bytes, got {len(blob)}"
        )
    return blob


def public_key_from_blob(pubkey_blob: bytes):
    if len(pubkey_blob) < ECDSA_FIELD_SIZE * 2:
        raise ValueError("Public key blob is too short")

    x = int.from_bytes(
        pubkey_blob[ECDSA_FIELD_SIZE - ECDSA_COORD_SIZE:ECDSA_FIELD_SIZE], "big"
    )
    y_start = ECDSA_FIELD_SIZE
    y = int.from_bytes(
        pubkey_blob[
            y_start + ECDSA_FIELD_SIZE - ECDSA_COORD_SIZE:y_start + ECDSA_FIELD_SIZE
        ],
        "big",
    )
    return ec.EllipticCurvePublicNumbers(x, y, ec.SECP384R1()).public_key()


def padded_signature_to_der(signature_blob: bytes) -> bytes:
    if len(signature_blob) != SIGNATURE_BLOB_SIZE:
        raise ValueError(
            f"Signature blob must be {SIGNATURE_BLOB_SIZE} bytes, got {len(signature_blob)}"
        )

    r = int.from_bytes(
        signature_blob[ECDSA_FIELD_SIZE - ECDSA_COORD_SIZE:ECDSA_FIELD_SIZE], "big"
    )
    s_start = ECDSA_FIELD_SIZE
    s = int.from_bytes(
        signature_blob[
            s_start + ECDSA_FIELD_SIZE - ECDSA_COORD_SIZE:s_start + ECDSA_FIELD_SIZE
        ],
        "big",
    )
    return encode_dss_signature(r, s)


def sign_digest(private_key, digest: bytes) -> bytes:
    signature = private_key.sign(digest, ec.ECDSA(utils.Prehashed(hashes.SHA512())))
    r, s = decode_dss_signature(signature)
    return generate_padded_signature(r, s)


def verify_digest(
    public_key_blob: bytes,
    signature_blob: bytes,
    digest: bytes,
) -> bool:
    public_key = public_key_from_blob(public_key_blob)
    signature = padded_signature_to_der(signature_blob)
    try:
        public_key.verify(signature, digest, ec.ECDSA(utils.Prehashed(hashes.SHA512())))
        return True
    except InvalidSignature:
        return False