Skip to content

Commit

Permalink
Revert "improve encodeBase64Url performance (#2774)"
Browse files Browse the repository at this point in the history
This reverts commit b3d98e0.
  • Loading branch information
anonrig committed Sep 23, 2024
1 parent e1eb9b6 commit d0f8e89
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/workerd/api/crypto/aes.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// https://opensource.org/licenses/Apache-2.0

#include "impl.h"
#include "util.h"

#include <workerd/io/io-context.h>

Expand Down Expand Up @@ -167,7 +166,7 @@ private:

SubtleCrypto::JsonWebKey jwk;
jwk.kty = kj::str("oct");
jwk.k = fastEncodeBase64Url(keyData);
jwk.k = kj::encodeBase64Url(keyData);
jwk.alg = kj::str("A", lengthInBytes * 8, aesMode);
jwk.key_ops = getUsages().map([](auto usage) { return kj::str(usage.name()); });
// I don't know why the spec says:
Expand Down
3 changes: 1 addition & 2 deletions src/workerd/api/crypto/digest.c++
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "digest.h"

#include "impl.h"
#include "util.h"

#include <workerd/api/crypto/crypto.h>
#include <workerd/io/io-context.h>
Expand Down Expand Up @@ -78,7 +77,7 @@ private:

SubtleCrypto::JsonWebKey jwk;
jwk.kty = kj::str("oct");
jwk.k = fastEncodeBase64Url(keyData);
jwk.k = kj::encodeBase64Url(keyData);
jwk.alg = kj::str("HS", keyAlgorithm.hash.name.slice(4));
jwk.key_ops = getUsages().map([](auto usage) { return kj::str(usage.name()); });
// I don't know why the spec says:
Expand Down
10 changes: 5 additions & 5 deletions src/workerd/api/crypto/ec.c++
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ SubtleCrypto::JsonWebKey Ec::toJwk(KeyType keyType, kj::StringPtr curveName) con
auto xa = handleBn(*x, groupDegreeInBytes);
auto ya = handleBn(*y, groupDegreeInBytes);

jwk.x = fastEncodeBase64Url(xa);
jwk.y = fastEncodeBase64Url(ya);
jwk.x = kj::encodeBase64Url(xa);
jwk.y = kj::encodeBase64Url(ya);

if (keyType == KeyType::PRIVATE) {
const auto privateKey = getPrivateKey();
JSG_REQUIRE(privateKey != nullptr, InternalDOMOperationError,
"Error getting private key material for JSON Web Key export",
internalDescribeOpensslErrors());
auto pk = handleBn(*privateKey, groupDegreeInBytes);
jwk.d = fastEncodeBase64Url(pk);
jwk.d = kj::encodeBase64Url(pk);
}
return jwk;
}
Expand Down Expand Up @@ -993,7 +993,7 @@ private:
SubtleCrypto::JsonWebKey jwk;
jwk.kty = kj::str("OKP");
jwk.crv = kj::str(getAlgorithmName() == "X25519"_kj ? "X25519"_kj : "Ed25519"_kj);
jwk.x = fastEncodeBase64Url(kj::arrayPtr(rawPublicKey, publicKeyLen));
jwk.x = kj::encodeBase64Url(kj::arrayPtr(rawPublicKey, publicKeyLen));
if (getAlgorithmName() == "Ed25519"_kj) {
jwk.alg = kj::str("EdDSA");
}
Expand All @@ -1011,7 +1011,7 @@ private:

KJ_ASSERT(privateKeyLen == 32, privateKeyLen);

jwk.d = fastEncodeBase64Url(kj::arrayPtr(rawPrivateKey, privateKeyLen));
jwk.d = kj::encodeBase64Url(kj::arrayPtr(rawPrivateKey, privateKeyLen));
}

return jwk;
Expand Down
17 changes: 8 additions & 9 deletions src/workerd/api/crypto/rsa.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "impl.h"
#include "keys.h"
#include "simdutf.h"
#include "util.h"

#include <openssl/bn.h>
#include <openssl/crypto.h>
Expand Down Expand Up @@ -236,20 +235,20 @@ SubtleCrypto::JsonWebKey Rsa::toJwk(
jwk.alg = kj::mv(name);
}

jwk.n = fastEncodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(n))));
jwk.e = fastEncodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(e))));
jwk.n = kj::encodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(n))));
jwk.e = kj::encodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(e))));

if (keyType == KeyType::PRIVATE) {
jwk.d = fastEncodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(d))));
jwk.d = kj::encodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(d))));
jwk.p =
fastEncodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_p(rsa)))));
kj::encodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_p(rsa)))));
jwk.q =
fastEncodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_q(rsa)))));
jwk.dp = fastEncodeBase64Url(
kj::encodeBase64Url(KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_q(rsa)))));
jwk.dp = kj::encodeBase64Url(
KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_dmp1(rsa)))));
jwk.dq = fastEncodeBase64Url(
jwk.dq = kj::encodeBase64Url(
KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_dmq1(rsa)))));
jwk.qi = fastEncodeBase64Url(
jwk.qi = kj::encodeBase64Url(
KJ_REQUIRE_NONNULL(bignumToArray(KJ_REQUIRE_NONNULL(RSA_get0_iqmp(rsa)))));
}

Expand Down
3 changes: 1 addition & 2 deletions src/workerd/api/node/crypto-keys.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#include "crypto.h"
#include "util.h"

#include <workerd/api/crypto/impl.h>

Expand Down Expand Up @@ -47,7 +46,7 @@ public:
if (format == "jwk") {
SubtleCrypto::JsonWebKey jwk;
jwk.kty = kj::str("oct");
jwk.k = fastEncodeBase64Url(keyData);
jwk.k = kj::encodeBase64Url(keyData);
jwk.ext = true;
return jwk;
}
Expand Down
12 changes: 0 additions & 12 deletions src/workerd/api/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "util.h"

#include "simdutf.h"

#include <workerd/io/io-context.h>
#include <workerd/util/mimetype.h>
#include <workerd/util/thread-scopes.h>
Expand Down Expand Up @@ -290,14 +288,4 @@ void maybeWarnIfNotText(jsg::Lock& js, kj::StringPtr str) {
"\". The result will probably be corrupted. Consider "
"checking the Content-Type header before interpreting entities as text."));
}

kj::String fastEncodeBase64Url(kj::ArrayPtr<const byte> bytes) {
auto expected_length = simdutf::base64_length_from_binary(bytes.size(), simdutf::base64_url);
auto output = kj::heapArray<char>(expected_length);
auto actual_length = simdutf::binary_to_base64(
bytes.asChars().begin(), bytes.size(), output.asChars().begin(), simdutf::base64_url);
KJ_ASSERT(expected_length == actual_length);
return kj::String(kj::mv(output));
}

} // namespace workerd::api
2 changes: 0 additions & 2 deletions src/workerd/api/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,4 @@ double dateNow();

void maybeWarnIfNotText(jsg::Lock& js, kj::StringPtr str);

kj::String fastEncodeBase64Url(kj::ArrayPtr<const byte> bytes);

} // namespace workerd::api

0 comments on commit d0f8e89

Please sign in to comment.