UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

392 lines (357 loc) 10.2 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/crypto.js import * as __hoisted_internal_crypto_webcrypto__ from "nstdlib/lib/internal/crypto/webcrypto"; import * as __hoisted_internal_worker__ from "nstdlib/lib/internal/worker"; import { assertCrypto, deprecate } from "nstdlib/lib/internal/util"; import { codes as __codes__ } from "nstdlib/lib/internal/errors"; import { crypto as constants } from "nstdlib/stub/binding/constants"; import { getOptionValue } from "nstdlib/lib/internal/options"; import { getFipsCrypto, setFipsCrypto, timingSafeEqual, } from "nstdlib/stub/binding/crypto"; import { checkPrime, checkPrimeSync, generatePrime, generatePrimeSync, randomBytes, randomFill, randomFillSync, randomInt, randomUUID, } from "nstdlib/lib/internal/crypto/random"; import { pbkdf2, pbkdf2Sync } from "nstdlib/lib/internal/crypto/pbkdf2"; import { scrypt, scryptSync } from "nstdlib/lib/internal/crypto/scrypt"; import { hkdf, hkdfSync } from "nstdlib/lib/internal/crypto/hkdf"; import { generateKeyPair, generateKeyPairSync, generateKey, generateKeySync, } from "nstdlib/lib/internal/crypto/keygen"; import { createSecretKey, createPublicKey, createPrivateKey, KeyObject, } from "nstdlib/lib/internal/crypto/keys"; import { DiffieHellman, DiffieHellmanGroup, ECDH, diffieHellman, } from "nstdlib/lib/internal/crypto/diffiehellman"; import { Cipher, Cipheriv, Decipher, Decipheriv, privateDecrypt, privateEncrypt, publicDecrypt, publicEncrypt, getCipherInfo, } from "nstdlib/lib/internal/crypto/cipher"; import { Sign, signOneShot, Verify, verifyOneShot, } from "nstdlib/lib/internal/crypto/sig"; import { Hash, Hmac, hash } from "nstdlib/lib/internal/crypto/hash"; import { X509Certificate } from "nstdlib/lib/internal/crypto/x509"; import { getCiphers, getCurves, getHashes, setEngine, secureHeapUsed, } from "nstdlib/lib/internal/crypto/util"; import * as Certificate from "nstdlib/lib/internal/crypto/certificate"; let _module_exports_ = {}; export { _module_exports_ as default }; // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. // Note: In 0.8 and before, crypto functions all defaulted to using // binary-encoded strings rather than buffers. assertCrypto(); const { ERR_CRYPTO_FIPS_FORCED, ERR_WORKER_UNSUPPORTED_OPERATION } = __codes__; let webcrypto; function lazyWebCrypto() { webcrypto ??= __hoisted_internal_crypto_webcrypto__; return webcrypto; } let ownsProcessState; function lazyOwnsProcessState() { ownsProcessState ??= __hoisted_internal_worker__.ownsProcessState; return ownsProcessState; } // These helper functions are needed because the constructors can // use new, in which case V8 cannot inline the recursive constructor call function createHash(algorithm, options) { return new Hash(algorithm, options); } function createCipheriv(cipher, key, iv, options) { return new Cipheriv(cipher, key, iv, options); } function createDecipheriv(cipher, key, iv, options) { return new Decipheriv(cipher, key, iv, options); } function createDiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) { return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding); } function createDiffieHellmanGroup(name) { return new DiffieHellmanGroup(name); } function createECDH(curve) { return new ECDH(curve); } function createHmac(hmac, key, options) { return new Hmac(hmac, key, options); } function createSign(algorithm, options) { return new Sign(algorithm, options); } function createVerify(algorithm, options) { return new Verify(algorithm, options); } _module_exports_ = { // Methods checkPrime, checkPrimeSync, createCipheriv, createDecipheriv, createDiffieHellman, createDiffieHellmanGroup, createECDH, createHash, createHmac, createPrivateKey, createPublicKey, createSecretKey, createSign, createVerify, diffieHellman, generatePrime, generatePrimeSync, getCiphers, getCipherInfo, getCurves, getDiffieHellman: createDiffieHellmanGroup, getHashes, hkdf, hkdfSync, pbkdf2, pbkdf2Sync, generateKeyPair, generateKeyPairSync, generateKey, generateKeySync, privateDecrypt, privateEncrypt, publicDecrypt, publicEncrypt, randomBytes, randomFill, randomFillSync, randomInt, randomUUID, scrypt, scryptSync, sign: signOneShot, setEngine, timingSafeEqual, getFips, setFips, verify: verifyOneShot, hash, // Classes Certificate, Cipher, Cipheriv, Decipher, Decipheriv, DiffieHellman, DiffieHellmanGroup, ECDH, Hash: deprecate(Hash, "crypto.Hash constructor is deprecated.", "DEP0179"), Hmac: deprecate(Hmac, "crypto.Hmac constructor is deprecated.", "DEP0181"), KeyObject, Sign, Verify, X509Certificate, secureHeapUsed, }; function getFips() { return getOptionValue("--force-fips") ? 1 : getFipsCrypto(); } function setFips(val) { if (getOptionValue("--force-fips")) { if (val) return; throw new ERR_CRYPTO_FIPS_FORCED(); } else { if (!lazyOwnsProcessState()) { throw new ERR_WORKER_UNSUPPORTED_OPERATION("Calling crypto.setFips()"); } setFipsCrypto(val); } } function getRandomValues(array) { return lazyWebCrypto().crypto.getRandomValues(array); } Object.defineProperty(constants, "defaultCipherList", { __proto__: null, get() { const value = getOptionValue("--tls-cipher-list"); Object.defineProperty(this, "defaultCipherList", { __proto__: null, writable: true, configurable: true, enumerable: true, value, }); return value; }, set(val) { Object.defineProperty(this, "defaultCipherList", { __proto__: null, writable: true, configurable: true, enumerable: true, value: val, }); }, configurable: true, enumerable: true, }); function getRandomBytesAlias(key) { return { enumerable: false, configurable: true, get() { let value; if (getOptionValue("--pending-deprecation")) { value = deprecate( randomBytes, `crypto.${key} is deprecated.`, "DEP0115", ); } else { value = randomBytes; } Object.defineProperty(this, key, { __proto__: null, enumerable: false, configurable: true, writable: true, value: value, }); return value; }, set(value) { Object.defineProperty(this, key, { __proto__: null, enumerable: true, configurable: true, writable: true, value, }); }, }; } const __fips_constants_webcrypto_subtl__ = {}; Object.defineProperties(__fips_constants_webcrypto_subtl__, { // crypto.fips is deprecated. DEP0093. Use crypto.getFips()/crypto.setFips() fips: { __proto__: null, get: getFips, set: setFips, }, constants: { __proto__: null, configurable: false, enumerable: true, value: constants, }, webcrypto: { __proto__: null, configurable: false, enumerable: true, get() { return lazyWebCrypto().crypto; }, set: undefined, }, subtle: { __proto__: null, configurable: false, enumerable: true, get() { return lazyWebCrypto().crypto.subtle; }, set: undefined, }, getRandomValues: { __proto__: null, configurable: false, enumerable: true, get: () => getRandomValues, set: undefined, }, // Aliases for randomBytes are deprecated. // The ecosystem needs those to exist for backwards compatibility. prng: getRandomBytesAlias("prng"), pseudoRandomBytes: getRandomBytesAlias("pseudoRandomBytes"), rng: getRandomBytesAlias("rng"), }); _module_exports_.fips = __fips_constants_webcrypto_subtl__.fips; _module_exports_.constants = __fips_constants_webcrypto_subtl__.constants; _module_exports_.webcrypto = __fips_constants_webcrypto_subtl__.webcrypto; _module_exports_.subtle = __fips_constants_webcrypto_subtl__.subtle; _module_exports_.getRandomValues = __fips_constants_webcrypto_subtl__.getRandomValues; _module_exports_.prng = __fips_constants_webcrypto_subtl__.prng; _module_exports_.pseudoRandomBytes = __fips_constants_webcrypto_subtl__.pseudoRandomBytes; _module_exports_.rng = __fips_constants_webcrypto_subtl__.rng; export { createCipheriv }; export { createDecipheriv }; export { createDiffieHellman }; export { createDiffieHellmanGroup }; export { createECDH }; export { createHash }; export { createHmac }; export { createSign }; export { createVerify }; export const getDiffieHellman = createDiffieHellmanGroup; export const sign = signOneShot; export { getFips }; export { setFips }; export const verify = verifyOneShot; export const fips = __fips_constants_webcrypto_subtl__.fips; export { webcrypto }; export const subtle = __fips_constants_webcrypto_subtl__.subtle; export { getRandomValues }; export const prng = __fips_constants_webcrypto_subtl__.prng; export const pseudoRandomBytes = __fips_constants_webcrypto_subtl__.pseudoRandomBytes; export const rng = __fips_constants_webcrypto_subtl__.rng;