Skip to content

fix(okp): refuse the Ed448 public keys of small order - #835

Merged
Spomky merged 5 commits into
4.3.xfrom
fix/okp-ed448-small-order
Nov 29, 2026
Merged

Spomky merged 5 commits into
4.3.xfrom
fix/okp-ed448-small-order

Conversation

@Spomky

@Spomky Spomky commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Target branch: 4.3.x

  • It is a Bug fix
  • It is a New feature
  • It is related to dependencies

Includes:

  • Breaks BC
  • Deprecations

Follow-up of #827, stacked on it: the commits up to d63ca5b are those of #827, and a7f5644 is the change of this
pull request. To be merged after #827.

The defect

The defect #827 fixes for Ed25519 also exists for Ed448, in code that is on 4.3.x only and not released yet (2bd3f62).
OpenSSL accepts a forged signature for an Ed448 public key of order 4: with x the encoding of y = 0 (x = 1 or −1),
the signature R = that point, S = 0 verifies for any input. This was checked on OpenSSL 3.5.9 (phpqa:8.4) and 4.1.0.
Ed448 has no sodium backend, so Ed448 accepted such a signature whatever the extensions loaded.

OpenSSL currently refuses the neutral point, (0, −1) and the non-canonical encodings. They are refused before OpenSSL
is called anyway, as libsodium does for Ed25519.

The fix

  • OKPKey::isSmallOrderEd448Point() compares x, sign bit cleared, with the encodings of y = 0, 1 and p − 1 and the
    non-canonical encodings of y = 0 and y = 1 (y = p and p + 1). It needs no extension. An x with one of the other
    bits of its last byte set is not in the list; OpenSSL refuses such a key.
  • verifyWithOpenSSL() returns false for such a key.
  • OKPKeyAnalyzer reports it with Invalid key. The point of the Ed448 public key is of small order.

Not in this pull request: an Ed448 x that decodes to no point of the curve is still not reported, unlike what #827
does for Ed25519. Every verification with such a key fails.

Tests

  • OKPKeyTest: the five encodings and two sign-bit variants are of small order. The RFC 8032 key, an x with
    another bit of its last byte set and a short string are not. verifyWithOpenSSL() refuses the forged signature
    for both keys of order 4.
  • FullySpecifiedEdDSATest: Ed448 returns false for the forged signature.
  • OKPKeyAnalyzerTest: the message for the neutral point and for (1, 0).

Without the fix, the 13 new cases fail. With ghcr.io/spomky-labs/phpqa:8.4:

lav45 and others added 3 commits November 29, 2026 11:29
OpenSSL loads an Ed25519 public key whose point is of small order, and with
the neutral point as key it verifies the signature R = the neutral point,
S = 0 for any input. Without ext-sodium, AbstractEdDSA::verify() goes through
OKPKey::verifyWithOpenSSL(), so Ed25519 and EdDSA accepted such a forged
signature. With sodium, sodium_crypto_sign_verify_detached() refuses it.

OKPKey::isSmallOrderEd25519Point() compares "x", sign bit cleared, with the 7
encodings libsodium refuses (ge25519_has_small_order()): the 8 points of order
1, 2, 4 and 8 and the non-canonical encodings of y = 0 and y = 1. It needs no
extension. verifyWithOpenSSL() returns false for such a key, and
OKPKeyAnalyzer reports it whichever backend is loaded.
OKPKeyAnalyzer only checked the size of "x" and the points of small order, so
an Ed25519 key whose "x" decodes to no point of the curve raised no message,
while every signature verification with it fails.

OKPKey::isValidEd25519PublicKey() uses sodium when it is loaded:
sodium_crypto_sign_ed25519_pk_to_curve25519() also requires the point to be in
the prime-order subgroup. Without sodium, OKPKey::isEd25519Point() decodes
"x" as RFC 8032 section 5.1.3 does, with brick/math: y lower than p and
(y^2 - 1) / (d y^2 + 1) a square modulo p. A point outside the prime-order
subgroup is then only reported with sodium.

The tests build the "x" of a well-formed Ed25519 key from the RFC 8037 key, as
random bytes are a point of the curve only half of the time.
OpenSSL accepts a forged Ed448 signature for a public key of order 4: with
"x" the encoding of y = 0 (x = 1 or -1), the signature R = that point, S = 0
verifies for any input, on OpenSSL 3.5 and 4.1. Ed448 has no sodium backend,
so the Ed448 algorithm accepted it whatever the extensions loaded.

OKPKey::isSmallOrderEd448Point() compares "x", sign bit cleared, with the
encodings of y = 0, 1 and p - 1 and the non-canonical encodings of y = 0 and
y = 1. verifyWithOpenSSL() returns false for such a key, and OKPKeyAnalyzer
reports it.
@Spomky Spomky added this to the 4.3.0 milestone Nov 29, 2026
@Spomky Spomky added the bug label Nov 29, 2026
isEd25519Point() inverted v by Fermat and then raised u / v to (p - 1) / 2:
two modPow on 255-bit numbers, 8.4 s with the native calculator of
brick/math. As v = d y^2 + 1 is never zero, u / v is a square exactly when
u * v is one: the inversion goes away and the call takes half the time.

isValidEd25519PublicKey() checks the points of small order before decoding
"x", and its docblock says that the prime-order subgroup is only checked with
sodium. The comment of the test helper moves to its docblock.
* complete/pr-827:
  fix(okp): decode an Ed25519 point with a single exponentiation

# Conflicts:
#	src/Library/Core/Util/OKPKey.php
@Spomky
Spomky merged commit fe4867b into 4.3.x Nov 29, 2026
18 checks passed
@Spomky
Spomky deleted the fix/okp-ed448-small-order branch November 29, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants