Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EdDSA signature verification step to detect and avoid outputting faulty signatures #11

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openpgp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ interface Config {
rejectPublicKeyAlgorithms: Set<enums.publicKey>;
rejectCurves: Set<enums.curve>;
}
export var config: Config;
export var config: Config & { checkEdDSAFaultySignatures: boolean }; // option only supported if set at the global openpgp.config level

// PartialConfig has the same properties as Config, but declared as optional.
// This interface is relevant for top-level functions, which accept a subset of configuration options
Expand Down
9 changes: 8 additions & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,12 @@ export default {
* @memberof module:config
* @property {Set<String>} rejectCurves {@link module:enums.curve}
*/
rejectCurves: new Set([enums.curve.secp256k1])
rejectCurves: new Set([enums.curve.secp256k1]),
/**
* Whether to validate generated EdDSA signatures before returning them, to ensure they are not faulty signatures.
* This check will make signing 2-3 times slower.
* Faulty signatures may be generated (in principle) if random bitflips occur at specific points in the signature
* computation, and could be used to recover the signer's secret key given a second signature over the same data.
*/
checkEdDSAFaultySignatures: true
};
15 changes: 15 additions & 0 deletions src/crypto/public_key/elliptic/eddsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import util from '../../../util';
import enums from '../../../enums';
import hash from '../../hash';
import { getRandomBytes } from '../../random';
import defaultConfig from '../../../config';

nacl.hash = bytes => new Uint8Array(sha512().update(bytes).digest());

Expand Down Expand Up @@ -68,6 +69,20 @@ export async function sign(algo, hashAlgo, message, publicKey, privateKey, hashe
case enums.publicKey.ed25519: {
const secretKey = util.concatUint8Array([privateKey, publicKey]);
const signature = nacl.sign.detached(hashed, secretKey);
if (defaultConfig.checkEdDSAFaultySignatures && !nacl.sign.detached.verify(hashed, signature, publicKey)) {
/**
* Detect faulty signatures caused by random bitflips during `crypto_sign` which could lead to private key extraction
* if two signatures over the same message are obtained.
* See https://github.com/jedisct1/libsodium/issues/170.
* If the input data is not deterministic, e.g. thanks to the random salt in v6 OpenPGP signatures (not yet implemented),
* then the generated signature is always safe, and the verification step is skipped.
* Otherwise, we need to verify the generated to ensure that no bitflip occured:
* - in M between the computation of `r` and `h`.
* - in the public key before computing `h`
* The verification step is almost 2-3 times as slow as signing, but it's faster than re-signing + re-deriving the public key for separate checks.
*/
throw new Error('Transient signing failure');
}
return { RS: signature };
}
case enums.publicKey.ed448:
Expand Down
15 changes: 15 additions & 0 deletions src/crypto/public_key/elliptic/eddsa_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import nacl from '@openpgp/tweetnacl/nacl-fast-light';
import util from '../../../util';
import enums from '../../../enums';
import hash from '../../hash';
import defaultConfig from '../../../config';

nacl.hash = bytes => new Uint8Array(sha512().update(bytes).digest());

Expand All @@ -51,6 +52,20 @@ export async function sign(oid, hashAlgo, message, publicKey, privateKey, hashed
}
const secretKey = util.concatUint8Array([privateKey, publicKey.subarray(1)]);
const signature = nacl.sign.detached(hashed, secretKey);
if (defaultConfig.checkEdDSAFaultySignatures && !nacl.sign.detached.verify(hashed, signature, publicKey.subarray(1))) {
/**
* Detect faulty signatures caused by random bitflips during `crypto_sign` which could lead to private key extraction
* if two signatures over the same message are obtained.
* See https://github.com/jedisct1/libsodium/issues/170.
* If the input data is not deterministic, e.g. thanks to the random salt in v6 OpenPGP signatures (not yet implemented),
* then the generated signature is always safe, and the verification step is skipped.
* Otherwise, we need to verify the generated to ensure that no bitflip occured:
* - in M between the computation of `r` and `h`.
* - in the public key before computing `h`
* The verification step is almost 2-3 times as slow as signing, but it's faster than re-signing + re-deriving the public key for separate checks.
*/
throw new Error('Transient signing failure');
}
// EdDSA signature params are returned in little-endian format
return {
r: signature.subarray(0, 32),
Expand Down
Loading