◐ Shell
clean mode source ↗

crypto: fix unsigned conversion of 4-byte RSA publicExponent · nodejs/node@16e8c2b

@@ -8,10 +8,29 @@ if (!common.hasCrypto)

88

const assert = require('assert');

991010

const {

11+

bigIntArrayToUnsignedInt,

1112

normalizeAlgorithm,

1213

validateKeyOps,

1314

} = require('internal/crypto/util');

141516+

// bigIntArrayToUnsignedInt must return an unsigned 32-bit value even when

17+

// the most significant byte has its top bit set. Otherwise the signed `<<`

18+

// operator yields a negative Int32 for inputs like [0x80, 0x00, 0x00, 0x01].

19+

{

20+

assert.strictEqual(

21+

bigIntArrayToUnsignedInt(new Uint8Array([0x80, 0x00, 0x00, 0x01])),

22+

0x80000001);

23+

assert.strictEqual(

24+

bigIntArrayToUnsignedInt(new Uint8Array([0xff, 0xff, 0xff, 0xff])),

25+

0xffffffff);

26+

assert.strictEqual(

27+

bigIntArrayToUnsignedInt(new Uint8Array([1, 0, 1])),

28+

65537);

29+

assert.strictEqual(

30+

bigIntArrayToUnsignedInt(new Uint8Array([1, 0, 0, 0, 0])),

31+

undefined);

32+

}

33+1534

{

1635

// Check that normalizeAlgorithm does not mutate object inputs.

1736

const algorithm = { name: 'ECDSA', hash: 'SHA-256' };