Repository navigation
fix(okp): refuse the Ed448 public keys of small order - #835
Merged
Merged
Conversation
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Target branch: 4.3.x
Includes:
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
xthe 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
Ed448accepted 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()comparesx, sign bit cleared, with the encodings of y = 0, 1 and p − 1 and thenon-canonical encodings of y = 0 and y = 1 (y = p and p + 1). It needs no extension. An
xwith one of the otherbits of its last byte set is not in the list; OpenSSL refuses such a key.
verifyWithOpenSSL()returns false for such a key.OKPKeyAnalyzerreports it withInvalid key. The point of the Ed448 public key is of small order.Not in this pull request: an Ed448
xthat decodes to no point of the curve is still not reported, unlike what #827does 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, anxwithanother bit of its last byte set and a short string are not.
verifyWithOpenSSL()refuses the forged signaturefor both keys of order 4.
FullySpecifiedEdDSATest:Ed448returns 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:php -lpasses on 8.2. The Ed448 tests also passwithout sodium (
php:8.4-cli-alpine,php -n).--git-diff-lines, base 4.3.x at 0378780): 107 mutants,fix(okp): refuse the Ed25519 public keys of small order and report the invalid ones #827's included, 107 killed.