Skip to content

Commit 110063d

Browse files
jasnelltargos
authored andcommitted
crypto: add generatePrime/checkPrime
APIs for generating and checking pseudo-random primes Signed-off-by: James M Snell <[email protected]> PR-URL: #36997 Reviewed-By: Tobias Nießen <[email protected]>
1 parent b7c3f99 commit 110063d

File tree

9 files changed

+769
-0
lines changed

9 files changed

+769
-0
lines changed

Diff for: doc/api/crypto.md

+115
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,48 @@ is currently in use. Setting to true requires a FIPS build of Node.js.
19611961
This property is deprecated. Please use `crypto.setFips()` and
19621962
`crypto.getFips()` instead.
19631963

1964+
### `crypto.checkPrime(candidate[, options, [callback]])`
1965+
<!-- YAML
1966+
added: REPLACEME
1967+
-->
1968+
1969+
* `candidate` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint}
1970+
A possible prime encoded as a sequence of big endian octets of arbitrary
1971+
length.
1972+
* `options` {Object}
1973+
* `checks` {number} The number of Miller-Rabin probabilistic primality
1974+
iterations to perform. When the value is `0` (zero), a number of checks
1975+
is used that yields a false positive rate of at most 2<sup>-64</sup> for
1976+
random input. Care must be used when selecting a number of checks. Refer
1977+
to the OpenSSL documentation for the [`BN_is_prime_ex`][] function `nchecks`
1978+
options for more details. **Defaults**: `0`
1979+
* `callback` {Function}
1980+
* `err` {Error} Set to an {Error} object if an error occured during check.
1981+
* `result` {boolean} `true` if the candidate is a prime with an error
1982+
probability less than `0.25 ** options.checks`.
1983+
1984+
Checks the primality of the `candidate`.
1985+
1986+
### `crypto.checkPrimeSync(candidate[, options])`
1987+
<!-- YAML
1988+
added: REPLACEME
1989+
-->
1990+
1991+
* `candidate` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint}
1992+
A possible prime encoded as a sequence of big endian octets of arbitrary
1993+
length.
1994+
* `options` {Object}
1995+
* `checks` {number} The number of Miller-Rabin probabilistic primality
1996+
iterations to perform. When the value is `0` (zero), a number of checks
1997+
is used that yields a false positive rate of at most 2<sup>-64</sup> for
1998+
random input. Care must be used when selecting a number of checks. Refer
1999+
to the OpenSSL documentation for the [`BN_is_prime_ex`][] function `nchecks`
2000+
options for more details. **Defaults**: `0`
2001+
* Returns: {boolean} `true` if the candidate is a prime with an error
2002+
probability less than `0.25 ** options.checks`.
2003+
2004+
Checks the primality of the `candidate`.
2005+
19642006
### `crypto.createCipher(algorithm, password[, options])`
19652007
<!-- YAML
19662008
added: v0.1.94
@@ -2694,6 +2736,78 @@ The return value `{ publicKey, privateKey }` represents the generated key pair.
26942736
When PEM encoding was selected, the respective key will be a string, otherwise
26952737
it will be a buffer containing the data encoded as DER.
26962738

2739+
### `crypto.generatePrime(size[, options[, callback]])`
2740+
<!-- YAML
2741+
added: REPLACEME
2742+
-->
2743+
2744+
* `size` {number} The size (in bits) of the prime to generate.
2745+
* `options` {Object}
2746+
* `add` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint}
2747+
* `rem` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint}
2748+
* `safe` {boolean} **Defaults**: `false`.
2749+
* `bigint` {boolean} When `true`, the generated prime is returned
2750+
as a `bigint`.
2751+
* `callback` {Function}
2752+
* `err` {Error}
2753+
* `prime` {ArrayBuffer|bigint}
2754+
2755+
Generates a pseudo-random prime of `size` bits.
2756+
2757+
If `options.safe` is `true`, the prime will be a safe prime -- that is,
2758+
`(prime - 1) / 2` will also be a prime.
2759+
2760+
If `options.add` and `options.rem` are set, the prime will satisfy the
2761+
condition that `prime % add = rem`. The `options.rem` is ignored if
2762+
`options.add` is not given. If `options.safe` is `true`, `options.add`
2763+
is given, and `options.rem` is `undefined`, then the prime generated
2764+
will satisfy the condition `prime % add = 3`. Otherwise if `options.safe`
2765+
is `false` and `options.rem` is `undefined`, `options.add` will be
2766+
ignored.
2767+
2768+
Both `options.add` and `options.rem` must be encoded as big-endian sequences
2769+
if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or
2770+
`DataView`.
2771+
2772+
By default, the prime is encoded as a big-endian sequence of octets
2773+
in an {ArrayBuffer}. If the `bigint` option is `true`, then a {bigint}
2774+
is provided.
2775+
2776+
### `crypto.generatePrimeSync(size[, options])`
2777+
<!-- YAML
2778+
added: REPLACEME
2779+
-->
2780+
2781+
* `size` {number} The size (in bits) of the prime to generate.
2782+
* `options` {Object}
2783+
* `add` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint}
2784+
* `rem` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint}
2785+
* `safe` {boolean} **Defaults**: `false`.
2786+
* `bigint` {boolean} When `true`, the generated prime is returned
2787+
as a `bigint`.
2788+
* Returns: {ArrayBuffer|bigint}
2789+
2790+
Generates a pseudo-random prime of `size` bits.
2791+
2792+
If `options.safe` is `true`, the prime will be a safe prime -- that is,
2793+
`(prime - 1)` / 2 will also be a prime.
2794+
2795+
If `options.add` and `options.rem` are set, the prime will satisfy the
2796+
condition that `prime % add = rem`. The `options.rem` is ignored if
2797+
`options.add` is not given. If `options.safe` is `true`, `options.add`
2798+
is given, and `options.rem` is `undefined`, then the prime generated
2799+
will satisfy the condition `prime % add = 3`. Otherwise if `options.safe`
2800+
is `false` and `options.rem` is `undefined`, `options.add` will be
2801+
ignored.
2802+
2803+
Both `options.add` and `options.rem` must be encoded as big-endian sequences
2804+
if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or
2805+
`DataView`.
2806+
2807+
By default, the prime is encoded as a big-endian sequence of octets
2808+
in an {ArrayBuffer}. If the `bigint` option is `true`, then a {bigint}
2809+
is provided.
2810+
26972811
### `crypto.getCiphers()`
26982812
<!-- YAML
26992813
added: v0.9.3
@@ -4234,6 +4348,7 @@ See the [list of SSL OP Flags][] for details.
42344348
[RFC 4122]: https://www.rfc-editor.org/rfc/rfc4122.txt
42354349
[RFC 5208]: https://www.rfc-editor.org/rfc/rfc5208.txt
42364350
[Web Crypto API documentation]: webcrypto.md
4351+
[`BN_is_prime_ex`]: https://www.openssl.org/docs/man1.1.1/man3/BN_is_prime_ex.html
42374352
[`Buffer`]: buffer.md
42384353
[`EVP_BytesToKey`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html
42394354
[`KeyObject`]: #crypto_class_keyobject

Diff for: lib/crypto.js

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const {
5050
timingSafeEqual,
5151
} = internalBinding('crypto');
5252
const {
53+
checkPrime,
54+
checkPrimeSync,
55+
generatePrime,
56+
generatePrimeSync,
5357
randomBytes,
5458
randomFill,
5559
randomFillSync,
@@ -170,6 +174,8 @@ function createVerify(algorithm, options) {
170174

171175
module.exports = {
172176
// Methods
177+
checkPrime,
178+
checkPrimeSync,
173179
createCipheriv,
174180
createDecipheriv,
175181
createDiffieHellman,
@@ -183,6 +189,8 @@ module.exports = {
183189
createSign,
184190
createVerify,
185191
diffieHellman,
192+
generatePrime,
193+
generatePrimeSync,
186194
getCiphers,
187195
getCipherInfo,
188196
getCurves,

0 commit comments

Comments
 (0)