Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 0 additions & 53 deletions src/js/internal/streams/lazy_transform.ts

This file was deleted.

190 changes: 13 additions & 177 deletions src/js/node/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Hardcoded module "node:crypto"
const StringDecoder = require("node:string_decoder").StringDecoder;
const LazyTransform = require("internal/streams/lazy_transform");
const { defineCustomPromisifyArgs } = require("internal/promisify");
const Writable = require("internal/streams/writable");
const { CryptoHasher } = Bun;
Expand All @@ -16,8 +14,8 @@ const {
sign,
Verify: _Verify,
verify,
Hmac: _Hmac,
Hash: _Hash,
Hmac,
Hash,
ECDH,
DiffieHellman,
DiffieHellmanGroup,
Expand All @@ -26,7 +24,8 @@ const {
checkPrimeSync,
generatePrime,
generatePrimeSync,
Cipher,
Cipheriv,
Decipheriv,
hkdf,
hkdfSync,

Expand Down Expand Up @@ -70,8 +69,6 @@ const {
argon2Sync: _argon2Sync,
} = $rust("node_crypto_binding.rs", "createNodeCryptoBindingZig");

const normalizeEncoding = $newRustFunction("node_util_binding.rs", "normalizeEncoding", 1);

const {
validateFunction,
validateInteger,
Expand Down Expand Up @@ -229,72 +226,11 @@ function createVerify(algorithm, options?) {

crypto_exports.createVerify = createVerify;

function Hash(algorithm, options?): void {
if (!new.target) {
return new Hash(algorithm, options);
}

const handle = new _Hash(algorithm, options);
this[kHandle] = handle;

LazyTransform.$apply(this, [options]);
}
$toClass(Hash, "Hash", LazyTransform);

Object.assign(Hash.prototype, {
copy: function (options) {
return new Hash(this[kHandle], options);
},
_transform: function (chunk, encoding, callback) {
this[kHandle].update(this, chunk, encoding);
callback();
},
_flush: function (callback) {
this.push(this[kHandle].digest(null, false));
callback();
},
update: function (data, encoding) {
return this[kHandle].update(this, data, encoding);
},
digest: function (outputEncoding) {
return this[kHandle].digest(outputEncoding);
},
});

crypto_exports.Hash = deprecate(Hash, "crypto.Hash constructor is deprecated.", "DEP0179");
crypto_exports.createHash = function createHash(algorithm, options) {
return new Hash(algorithm, options);
};

function Hmac(hmac, key, options?): void {
if (!new.target) {
return new Hmac(hmac, key, options);
}

const handle = new _Hmac(hmac, key, options);
this[kHandle] = handle;

LazyTransform.$apply(this, [options]);
}
$toClass(Hmac, "Hmac", LazyTransform);

Object.assign(Hmac.prototype, {
update: function (data, encoding) {
return this[kHandle].update(this, data, encoding);
},
digest: function (outputEncoding) {
return this[kHandle].digest(outputEncoding);
},
_transform: function (chunk, encoding, callback) {
this[kHandle].update(this, chunk, encoding);
callback();
},
_flush: function (callback) {
this.push(this[kHandle].digest());
callback();
},
});

crypto_exports.Hmac = deprecate(Hmac, "crypto.Hmac constructor is deprecated.", "DEP0181");
crypto_exports.createHmac = function createHmac(hmac, key, options) {
return new Hmac(hmac, key, options);
Expand Down Expand Up @@ -441,115 +377,15 @@ crypto_exports.createECDH = function createECDH(curve) {
return new ECDH(curve);
};

{
function getDecoder(decoder, encoding) {
const normalizedEncoding = normalizeEncoding(encoding);
decoder ||= new StringDecoder(encoding);
if (decoder.encoding !== normalizedEncoding) {
if (normalizedEncoding === undefined) {
throw $ERR_UNKNOWN_ENCODING(encoding);
}

// there's a test for this
// https://github.com/nodejs/node/blob/6b4255434226491449b7d925038008439e5586b2/lib/internal/crypto/cipher.js#L100
// https://github.com/nodejs/node/blob/6b4255434226491449b7d925038008439e5586b2/test/parallel/test-crypto-encoding-validation-error.js#L31
throw $ERR_INTERNAL_ASSERTION("Cannot change encoding");
}
return decoder;
}

function Cipheriv(cipher, key, iv, options): void {
if (!new.target) {
return new Cipheriv(cipher, key, iv, options);
}

this[kHandle] = new Cipher(false, cipher, key, iv, options);
LazyTransform.$apply(this, [options]);
this._decoder = null;
}
$toClass(Cipheriv, "Cipheriv", LazyTransform);

Object.assign(Cipheriv.prototype, {
setAutoPadding: function (ap) {
this[kHandle].setAutoPadding(ap);
return this;
},
getAuthTag: function () {
return this[kHandle].getAuthTag();
},
setAAD: function (aadbuf, options) {
this[kHandle].setAAD(aadbuf, options);
return this;
},
_transform: function (chunk, encoding, callback) {
this.push(this[kHandle].update(chunk, encoding));
callback();
},
_flush: function (callback) {
try {
this.push(this[kHandle].final());
} catch (e) {
callback(e);
return;
}
callback();
},
update: function (data, inputEncoding, outputEncoding) {
const res = this[kHandle].update(data, inputEncoding);

if (outputEncoding && outputEncoding !== "buffer") {
this._decoder = getDecoder(this._decoder, outputEncoding);
return this._decoder.write(res);
}

return res;
},
final: function (outputEncoding) {
const res = this[kHandle].final();

if (outputEncoding && outputEncoding !== "buffer") {
this._decoder = getDecoder(this._decoder, outputEncoding);
return this._decoder.end(res);
}

return res;
},
});

function Decipheriv(cipher, key, iv, options): void {
if (!new.target) {
return new Decipheriv(cipher, key, iv, options);
}

this[kHandle] = new Cipher(true, cipher, key, iv, options);
LazyTransform.$apply(this, [options]);
this._decoder = null;
}
$toClass(Decipheriv, "Decipheriv", LazyTransform);

Object.assign(Decipheriv.prototype, {
setAutoPadding: Cipheriv.prototype.setAutoPadding,
setAuthTag: function (tagbuf, encoding) {
this[kHandle].setAuthTag(tagbuf, encoding);
return this;
},
setAAD: Cipheriv.prototype.setAAD,
_transform: Cipheriv.prototype._transform,
_flush: Cipheriv.prototype._flush,
update: Cipheriv.prototype.update,
final: Cipheriv.prototype.final,
});

crypto_exports.Cipheriv = Cipheriv;
crypto_exports.Decipheriv = Decipheriv;
crypto_exports.createCipheriv = function createCipheriv(cipher, key, iv, options) {
return new Cipheriv(cipher, key, iv, options);
};
crypto_exports.createDecipheriv = function createDecipheriv(cipher, key, iv, options) {
return new Decipheriv(cipher, key, iv, options);
};
crypto_exports.getCiphers = getCiphers;
}
crypto_exports.Cipheriv = Cipheriv;
crypto_exports.Decipheriv = Decipheriv;
crypto_exports.createCipheriv = function createCipheriv(cipher, key, iv, options) {
return new Cipheriv(cipher, key, iv, options);
};
crypto_exports.createDecipheriv = function createDecipheriv(cipher, key, iv, options) {
return new Decipheriv(cipher, key, iv, options);
};
crypto_exports.getCiphers = getCiphers;

crypto_exports.scrypt = scrypt;
crypto_exports.scryptSync = scryptSync;
Expand Down
5 changes: 4 additions & 1 deletion src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2073,7 +2073,10 @@ void GlobalObject::finishCreation(VM& vm)
setupJSHashClassStructure(init);
} },
{ OBJECT_OFFSETOF(GlobalObject, m_JSCipherClassStructure), [](LazyClassStructure::Initializer& init) {
setupCipherClassStructure(init);
setupCipherivClassStructure(init);
} },
{ OBJECT_OFFSETOF(GlobalObject, m_JSDecipherClassStructure), [](LazyClassStructure::Initializer& init) {
setupDecipherivClassStructure(init);
} },
{ OBJECT_OFFSETOF(GlobalObject, m_JSKeyObjectClassStructure), [](LazyClassStructure::Initializer& init) {
setupKeyObjectClassStructure(init);
Expand Down
1 change: 1 addition & 0 deletions src/jsc/bindings/ZigGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ class GlobalObject : public Bun::GlobalScope {
V(public, LazyClassStructure, m_JSHashClassStructure) \
V(public, LazyClassStructure, m_JSECDHClassStructure) \
V(public, LazyClassStructure, m_JSCipherClassStructure) \
V(public, LazyClassStructure, m_JSDecipherClassStructure) \
V(public, LazyClassStructure, m_JSKeyObjectClassStructure) \
V(public, LazyClassStructure, m_JSSecretKeyObjectClassStructure) \
V(public, LazyPropertyOfGlobalObject<JSObject>, m_JSAsymmetricKeyObjectPrototype) \
Expand Down
27 changes: 22 additions & 5 deletions src/jsc/bindings/node/crypto/JSCipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "openssl/rsa.h"
#include "NodeValidator.h"
#include "KeyObject.h"
#include "LazyTransform.h"

namespace Bun {

Expand Down Expand Up @@ -40,20 +41,36 @@ void JSCipher::visitChildrenImpl(JSCell* cell, Visitor& visitor)

DEFINE_VISIT_CHILDREN(JSCipher);

void setupCipherClassStructure(JSC::LazyClassStructure::Initializer& init)
static void setupCipherClassStructure(JSC::LazyClassStructure::Initializer& init, CipherKind kind)
{
auto* prototypeStructure = JSCipherPrototype::createStructure(init.vm, init.global, init.global->objectPrototype());
auto* prototype = JSCipherPrototype::create(init.vm, init.global, prototypeStructure);
// class Cipheriv / Decipheriv extends Transform (internal/streams/transform)
JSObject* transform = transformConstructor(init.global);
RELEASE_ASSERT(transform);
JSValue transformPrototype = transform->getDirect(init.vm, init.vm.propertyNames->prototype);
RELEASE_ASSERT(transformPrototype && transformPrototype.isObject());

auto* constructorStructure = JSCipherConstructor::createStructure(init.vm, init.global, init.global->functionPrototype());
auto* constructor = JSCipherConstructor::create(init.vm, constructorStructure, prototype);
auto* prototypeStructure = JSCipherPrototype::createStructure(init.vm, init.global, transformPrototype);
auto* prototype = JSCipherPrototype::create(init.vm, init.global, prototypeStructure, kind);

auto* constructorStructure = JSCipherConstructor::createStructure(init.vm, init.global, transform);
auto* constructor = JSCipherConstructor::create(init.vm, constructorStructure, prototype, kind);

auto* structure = JSCipher::createStructure(init.vm, init.global, prototype);
init.setPrototype(prototype);
init.setStructure(structure);
init.setConstructor(constructor);
}

void setupCipherivClassStructure(JSC::LazyClassStructure::Initializer& init)
{
setupCipherClassStructure(init, CipherKind::Cipher);
}

void setupDecipherivClassStructure(JSC::LazyClassStructure::Initializer& init)
{
setupCipherClassStructure(init, CipherKind::Decipher);
}

enum class KeyType {
Public,
Private,
Expand Down
5 changes: 4 additions & 1 deletion src/jsc/bindings/node/crypto/JSCipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ class JSCipher final : public JSC::JSDestructibleObject {
static void destroy(JSC::JSCell* cell) { static_cast<JSCipher*>(cell)->~JSCipher(); }
};

void setupCipherClassStructure(JSC::LazyClassStructure::Initializer&);
// node:crypto Cipheriv / Decipheriv: both are JSCipher cells (m_kind tells them apart) with
// their own prototype/constructor, chained under Transform.prototype.
void setupCipherivClassStructure(JSC::LazyClassStructure::Initializer&);
void setupDecipherivClassStructure(JSC::LazyClassStructure::Initializer&);

JSC_DECLARE_HOST_FUNCTION(jsPublicEncrypt);
JSC_DECLARE_HOST_FUNCTION(jsPublicDecrypt);
Expand Down
Loading