-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: node crypto module #92
Changes from all commits
e7d8b1f
ed66014
9d486a9
d4b7db1
1b1f736
8c5fdcb
0f92de6
eef9d3f
7d52886
3ebea94
4889ad3
d2cd612
2619b7b
349f66b
98903ad
7d3d5b1
b901ff6
585f2d3
c105d3c
3b8e22d
16a0369
026898e
920f2c7
73e7cab
906f4c8
b98e71a
7a1999d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license. | ||
|
||
import { ERR_CRYPTO_FIPS_FORCED, ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, ERR_INVALID_ARG_TYPE } from "./internal/errors"; | ||
import { crypto as constants } from "./internal_binding/constants"; | ||
import { getOptionValue } from "./internal/options"; | ||
import { isAnyArrayBuffer, isArrayBufferView } from "./internal/util/types"; | ||
import { | ||
timing_safe_equal, | ||
} from "_node:crypto"; | ||
function timingSafeEqual(a, b) { | ||
if (!isAnyArrayBuffer(a) && !isArrayBufferView(a)) { | ||
throw new ERR_INVALID_ARG_TYPE("buf1", ["ArrayBuffer", "Buffer", "TypedArray", "DataView"], a); | ||
} | ||
if (!isAnyArrayBuffer(b) && !isArrayBufferView(b)) { | ||
throw new ERR_INVALID_ARG_TYPE("buf2", ["ArrayBuffer", "Buffer", "TypedArray", "DataView"], b); | ||
} | ||
if (a.byteLength != b.byteLength) { | ||
throw new ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(); | ||
} | ||
return timing_safe_equal(a.buffer, b.buffer); | ||
} | ||
|
||
import { | ||
checkPrime, | ||
checkPrimeSync, | ||
generatePrime, | ||
generatePrimeSync, | ||
randomBytes, | ||
randomFill, | ||
randomFillSync, | ||
randomInt, | ||
randomUUID, | ||
} from "./internal/crypto/random"; | ||
import { pbkdf2, pbkdf2Sync } from "./internal/crypto/pbkdf2"; | ||
import { scrypt, scryptSync } from "./internal/crypto/scrypt"; | ||
import { hkdf, hkdfSync } from "./internal/crypto/hkdf"; | ||
/*import { | ||
generateKey, | ||
generateKeyPair, | ||
generateKeyPairSync, | ||
generateKeySync, | ||
} from "./internal/crypto/keygen";*/ | ||
import { | ||
createPrivateKey, | ||
createPublicKey, | ||
createSecretKey, | ||
KeyObject, | ||
} from "./internal/crypto/keys";/* | ||
import { | ||
DiffieHellman, | ||
diffieHellman, | ||
DiffieHellmanGroup, | ||
ECDH, | ||
} from "./internal/crypto/diffiehellman";*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why comment this code when you have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It hasn't implemented for wasi and tested. It's just copied from nodejs now. |
||
import { | ||
Cipheriv, | ||
Decipheriv, | ||
getCipherInfo, | ||
privateDecrypt, | ||
privateEncrypt, | ||
publicDecrypt, | ||
publicEncrypt, | ||
} from "./internal/crypto/cipher"; | ||
/* | ||
import { | ||
Sign, | ||
signOneShot, | ||
Verify, | ||
verifyOneShot, | ||
} from "./internal/crypto/sig";*/ | ||
import { Hash, Hmac } from "./internal/crypto/hash";/* | ||
import { X509Certificate } from "./internal/crypto/x509"; | ||
*/import { | ||
getCiphers, | ||
getCurves, | ||
getHashes, | ||
secureHeapUsed, | ||
setEngine, | ||
} from "./internal/crypto/util";/* | ||
import Certificate from "./internal/crypto/certificate"; | ||
*/ | ||
const webcrypto = undefined; | ||
const fipsForced = getOptionValue("--force-fips"); | ||
|
||
function createCipheriv(cipher, key, iv, options) { | ||
return new Cipheriv(cipher, key, iv, options); | ||
} | ||
|
||
function createDecipheriv(algorithm, key, iv, options) { | ||
return new Decipheriv(algorithm, key, iv, options); | ||
} | ||
/* | ||
function createDiffieHellman(sizeOrKey, keyEncoding, generator, generatorEncoding) { | ||
return new DiffieHellman( | ||
sizeOrKey, | ||
keyEncoding, | ||
generator, | ||
generatorEncoding, | ||
); | ||
} | ||
|
||
function createDiffieHellmanGroup(name) { | ||
return new DiffieHellmanGroup(name); | ||
} | ||
|
||
function createECDH(curve) { | ||
return new ECDH(curve); | ||
} | ||
*/ | ||
function createHash(hash, options) { | ||
return new Hash(hash, options); | ||
} | ||
|
||
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); | ||
} | ||
*/ | ||
function setFipsForced(val) { | ||
if (val) { | ||
return; | ||
} | ||
|
||
throw new ERR_CRYPTO_FIPS_FORCED(); | ||
} | ||
|
||
function getFipsForced() { | ||
return 1; | ||
} | ||
|
||
Object.defineProperty(constants, "defaultCipherList", { | ||
value: getOptionValue("--tls-cipher-list"), | ||
}); | ||
/* | ||
const getDiffieHellman = createDiffieHellmanGroup; | ||
*/ | ||
function getFipsCrypto() { | ||
throw new Error("crypto.getFipsCrypto is unimplemented") | ||
} | ||
function setFipsCrypto(_val) { | ||
throw new Error("crypto.setFipsCrypto is unimplemented") | ||
} | ||
const getFips = fipsForced ? getFipsForced : getFipsCrypto; | ||
const setFips = fipsForced ? setFipsForced : setFipsCrypto; | ||
/* | ||
const sign = signOneShot; | ||
const verify = verifyOneShot; | ||
*/ | ||
export default { | ||
/*Certificate,*/ | ||
checkPrime, | ||
checkPrimeSync, | ||
Cipheriv, | ||
constants, | ||
createCipheriv, | ||
createDecipheriv,/* | ||
createDiffieHellman, | ||
createDiffieHellmanGroup, | ||
createECDH,*/ | ||
createHash, | ||
createHmac, | ||
createPrivateKey, | ||
createPublicKey, | ||
createSecretKey,/* | ||
createSign, | ||
createVerify,*/ | ||
Decipheriv,/* | ||
DiffieHellman, | ||
diffieHellman, | ||
DiffieHellmanGroup, | ||
ECDH, | ||
generateKey, | ||
generateKeyPair, | ||
generateKeyPairSync, | ||
generateKeySync,*/ | ||
generatePrime, | ||
generatePrimeSync, | ||
getCipherInfo, | ||
getCiphers, | ||
getCurves,/* | ||
getDiffieHellman,*/ | ||
getFips, | ||
getHashes, | ||
Hash, | ||
hkdf, | ||
hkdfSync, | ||
Hmac,/* | ||
KeyObject,*/ | ||
pbkdf2, | ||
pbkdf2Sync, | ||
privateDecrypt, | ||
privateEncrypt, | ||
publicDecrypt, | ||
publicEncrypt, | ||
randomBytes, | ||
randomFill, | ||
randomFillSync, | ||
randomInt, | ||
randomUUID, | ||
scrypt, | ||
scryptSync, | ||
secureHeapUsed, | ||
setEngine, | ||
setFips,/* | ||
Sign, | ||
sign,*/ | ||
timingSafeEqual, | ||
/*Verify, | ||
verify, | ||
webcrypto, | ||
X509Certificate,*/ | ||
}; | ||
|
||
export { | ||
/*Certificate,*/ | ||
checkPrime, | ||
checkPrimeSync, | ||
Cipheriv, | ||
constants, | ||
createCipheriv, | ||
createDecipheriv, | ||
/*createDiffieHellman, | ||
createDiffieHellmanGroup, | ||
createECDH,*/ | ||
createHash, | ||
createHmac, | ||
createPrivateKey, | ||
createPublicKey, | ||
createSecretKey,/* | ||
createSign, | ||
createVerify,*/ | ||
Decipheriv,/* | ||
DiffieHellman, | ||
diffieHellman, | ||
DiffieHellmanGroup, | ||
ECDH, | ||
generateKey, | ||
generateKeyPair, | ||
generateKeyPairSync, | ||
generateKeySync,*/ | ||
generatePrime, | ||
generatePrimeSync, | ||
getCipherInfo, | ||
getCiphers, | ||
getCurves,/* | ||
getDiffieHellman,*/ | ||
getFips, | ||
getHashes, | ||
Hash, | ||
hkdf, | ||
hkdfSync, | ||
Hmac,/* | ||
KeyObject,*/ | ||
pbkdf2, | ||
pbkdf2Sync, | ||
privateDecrypt, | ||
privateEncrypt, | ||
publicDecrypt, | ||
publicEncrypt, | ||
randomBytes, | ||
randomFill, | ||
randomFillSync, | ||
randomInt, | ||
randomUUID, | ||
scrypt, | ||
scryptSync, | ||
secureHeapUsed, | ||
setEngine, | ||
setFips, | ||
/*Sign, | ||
sign,*/ | ||
timingSafeEqual, | ||
/*Verify, | ||
verify,*/ | ||
webcrypto, | ||
/*X509Certificate,*/ | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What support is missing for this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message format from quickjs is a little different from v8, this function can't get the correct result.