Skip to content

Commit

Permalink
Merge pull request #92 from Puellaquae/crypto-dev
Browse files Browse the repository at this point in the history
feat: node crypto module
  • Loading branch information
L-jasmine authored Jan 25, 2024
2 parents c5a1daa + 7a1999d commit e775c71
Show file tree
Hide file tree
Showing 268 changed files with 21,355 additions and 23 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install WasmEdge
run: |
VERSION=0.13.4
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s -- -e all --version=$VERSION --plugins=wasi_nn-tensorflowlite -p /usr/local
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s -- -e all --version=$VERSION --plugins=wasi_nn-tensorflowlite --plugins=wasi_crypto -p /usr/local
wget https://github.com/WasmEdge/WasmEdge/releases/download/$VERSION/WasmEdge-plugin-wasmedge_rustls-$VERSION-ubuntu20.04_x86_64.tar.gz
sudo chmod +x /usr/local/lib/wasmedge
Expand Down Expand Up @@ -191,8 +191,7 @@ jobs:
run: |
cargo test test_fs --target=wasm32-wasi --release
#- name: Node fs module test (maybe timeout)
# timeout-minutes: 5
# continue-on-error: true
# run: |
# cargo test test_fs --target=wasm32-wasi --release -- --ignored
- name: Node crypto module test
#timeout-minutes: 10
run: |
cargo test test_crypto --target=wasm32-wasi --release --features=nodejs_crypto
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ env_logger = "0.10.0"
rustls = { version = "0.21.0", optional = true }
tokio-rustls-wasi = { version = "0.24.1", optional = true }
webpki-roots = { version = "0.25.0", optional = true }
crypto-wasi = { version = "0.1.1", optional = true }

[features]
default = ["tls"]
Expand All @@ -38,3 +39,4 @@ img = ["image", "imageproc"]
tensorflow = ["img"]
wasi_nn = ["img"]
cjs = []
nodejs_crypto = ["crypto-wasi"]
1 change: 1 addition & 0 deletions modules/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ function parseCode(code, offset) {
}

function getErrMessage(message, fn) {
return "assert.getErrMessage unsupported";
const tmpLimit = Error.stackTraceLimit;
const errorStackTraceLimitIsWritable = isErrorStackTraceLimitWritable();
// Make sure the limit is set to 1. Otherwise it could fail (<= 0) or it
Expand Down
11 changes: 10 additions & 1 deletion modules/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2328,4 +2328,13 @@ var kMaxLength = exports.kMaxLength;

globalThis.Buffer = Buffer

export { Buffer, INSPECT_MAX_BYTES, exports as default, kMaxLength };
class FastBuffer extends Uint8Array {
// Using an explicit constructor here is necessary to avoid relying on
// `Array.prototype[Symbol.iterator]`, which can be mutated by users.
// eslint-disable-next-line no-useless-constructor
constructor(bufferOrLength, byteOffset, length) {
super(bufferOrLength, byteOffset, length);
}
}

export { Buffer, INSPECT_MAX_BYTES, exports as default, kMaxLength, FastBuffer };
285 changes: 285 additions & 0 deletions modules/crypto.js
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";*/
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,*/
};
Loading

0 comments on commit e775c71

Please sign in to comment.