GeezerVison is showing github.com — crypto: minimize RSA public exponent metadata · nodejs/node@2ac8c82
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/rsa.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | TypedArrayPrototypeGetBuffer, | | 5 | + TypedArrayPrototypeGetByteOffset, | | 6 | + TypedArrayPrototypeGetLength, | 5 | 7 | Uint8Array, | 6 | 8 | } = primordials; | 7 | 9 |
| @@ -111,11 +113,17 @@ function rsaKeyGenerate( | 111 | 113 | const allowedUsages = kUsages[name]; | 112 | 114 | const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name); | 113 | 115 | const publicExponentConverted = bigIntArrayToUnsignedInt(publicExponent); | | 116 | + let firstNonzeroByte = 0; | | 117 | + while (publicExponent[firstNonzeroByte] === 0) | | 118 | + firstNonzeroByte++; | 114 | 119 |
| 115 | 120 | const keyAlgorithm = { | 116 | 121 | name, | 117 | 122 | modulusLength, | 118 | | - publicExponent, | | 123 | + publicExponent: new Uint8Array( | | 124 | + TypedArrayPrototypeGetBuffer(publicExponent), | | 125 | + TypedArrayPrototypeGetByteOffset(publicExponent) + firstNonzeroByte, | | 126 | + TypedArrayPrototypeGetLength(publicExponent) - firstNonzeroByte), | 119 | 127 | hash, | 120 | 128 | }; | 121 | 129 |
| |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { getFips } = require('crypto'); | | 9 | +const { subtle } = globalThis.crypto; | | 10 | + | | 11 | +(async () => { | | 12 | + const exponents = getFips() ? [[1, 0, 1]] : [[3], [1, 0, 1]]; | | 13 | + for (const name of ['RSA-PSS', 'RSASSA-PKCS1-v1_5', 'RSA-OAEP']) { | | 14 | + const usages = name === 'RSA-OAEP' ? ['encrypt', 'decrypt'] : ['sign', 'verify']; | | 15 | + for (const exponent of exponents) { | | 16 | + const publicExponent = new Uint8Array([0, 0, 0, 0, ...exponent]); | | 17 | + const pair = await subtle.generateKey({ | | 18 | + name, modulusLength: 2048, publicExponent, hash: 'SHA-256', | | 19 | + }, true, usages); | | 20 | + for (const key of [pair.publicKey, pair.privateKey]) { | | 21 | + assert.deepStrictEqual(key.algorithm.publicExponent, new Uint8Array(exponent)); | | 22 | + } | | 23 | + assert.deepStrictEqual(publicExponent, new Uint8Array([0, 0, 0, 0, ...exponent])); | | 24 | + } | | 25 | + } | | 26 | +})().then(common.mustCall()); |
|