Skip to content

Commit

Permalink
More crypto.subtle implementations (margelo#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed Jan 12, 2024
1 parent 430ed77 commit 2cd2f96
Show file tree
Hide file tree
Showing 21 changed files with 1,671 additions and 561 deletions.
34 changes: 9 additions & 25 deletions cpp/Cipher/MGLGenerateKeyPairInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,16 @@ FieldDefinition getGenerateKeyPairFieldDefinition(
jsCallInvoker, config]() {
m.lock();
try {
const auto keys = generateRSAKeyPair(runtime, config);
jsCallInvoker->invokeAsync([&runtime, keys, jsCallInvoker,
auto keys = generateRSAKeyPair(runtime, config);
jsCallInvoker->invokeAsync([&runtime, &keys, jsCallInvoker,
resolve]() {
if (keys.first.isString && keys.second.isString) {
auto publicKey = jsi::String::createFromUtf8(
runtime, keys.first.stringValue);
auto privateKey = jsi::String::createFromUtf8(
runtime, keys.second.stringValue);
auto res = jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKey,
privateKey);
resolve->asObject(runtime).asFunction(runtime).call(
runtime, std::move(res));
} else {
MGLTypedArray<MGLTypedArrayKind::Uint8Array>
publicKeyBuffer(runtime, keys.first.vectorValue);
MGLTypedArray<MGLTypedArrayKind::Uint8Array>
privateKeyBuffer(runtime,
keys.second.vectorValue);

auto res = jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKeyBuffer,
privateKeyBuffer);
resolve->asObject(runtime).asFunction(runtime).call(
runtime, std::move(res));
}
auto publicKey = toJSI(runtime, keys.first);
auto privateKey = toJSI(runtime, keys.second);
auto res = jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKey,
privateKey);
resolve->asObject(runtime).asFunction(runtime).call(
runtime, std::move(res));
});
} catch (std::exception e) {
jsCallInvoker->invokeAsync(
Expand Down
21 changes: 4 additions & 17 deletions cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,10 @@ FieldDefinition getGenerateKeyPairSyncFieldDefinition(
auto config = std::make_shared<RsaKeyPairGenConfig>(
prepareRsaKeyGenConfig(runtime, arguments));
auto keys = generateRSAKeyPair(runtime, std::move(config));
if (keys.first.isString && keys.second.isString) {
auto publicKey =
jsi::String::createFromUtf8(runtime, keys.first.stringValue);
auto privateKey =
jsi::String::createFromUtf8(runtime, keys.second.stringValue);
return jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKey, privateKey);
} else {
MGLTypedArray<MGLTypedArrayKind::Uint8Array> publicKeyBuffer(
runtime, keys.first.vectorValue);
MGLTypedArray<MGLTypedArrayKind::Uint8Array> privateKeyBuffer(
runtime, keys.second.vectorValue);

return jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKeyBuffer,
privateKeyBuffer);
}
auto publicKey = toJSI(runtime, keys.first);
auto privateKey = toJSI(runtime, keys.second);
return jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKey, privateKey);
});
}
} // namespace margelo
8 changes: 4 additions & 4 deletions cpp/Cipher/MGLRsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
return config;
}

std::pair<StringOrBuffer, StringOrBuffer> generateRSAKeyPair(
std::pair<JSVariant, JSVariant> generateRSAKeyPair(
jsi::Runtime& runtime, std::shared_ptr<RsaKeyPairGenConfig> config) {
CheckEntropy();

Expand All @@ -165,10 +165,10 @@ std::pair<StringOrBuffer, StringOrBuffer> generateRSAKeyPair(

config->key = ManagedEVPPKey(EVPKeyPointer(pkey));

std::optional<StringOrBuffer> publicBuffer =
OptionJSVariant publicBuffer =
ManagedEVPPKey::ToEncodedPublicKey(runtime, std::move(config->key),
config->public_key_encoding);
std::optional<StringOrBuffer> privateBuffer =
OptionJSVariant privateBuffer =
ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
config->private_key_encoding);

Expand All @@ -177,7 +177,7 @@ std::pair<StringOrBuffer, StringOrBuffer> generateRSAKeyPair(
"Failed to encode public and/or private key");
}

return std::make_pair(publicBuffer.value(), privateBuffer.value());
return {std::move(publicBuffer.value()), std::move(privateBuffer.value())};
}

} // namespace margelo
6 changes: 3 additions & 3 deletions cpp/Cipher/MGLRsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum RSAKeyVariant {
// initialize the data in a generic manner this is to be later be used to
// generate the keys in a thread-safe manner (I think) I'm however too dumb and
// after ~4hrs I have given up on trying to replicate/extract the important
// parts For now I'm storing a single config param, a generic abstractino is
// parts For now I'm storing a single config param, a generic abstraction is
// necessary for more schemes. this struct is just a very simplified version
// meant to carry information around
struct RsaKeyPairGenConfig {
Expand All @@ -55,9 +55,9 @@ struct RsaKeyPairGenConfig {
};

RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
const jsi::Value* arguments);
const jsi::Value* arguments);

std::pair<StringOrBuffer, StringOrBuffer> generateRSAKeyPair(
std::pair<JSVariant, JSVariant> generateRSAKeyPair(
jsi::Runtime& runtime, std::shared_ptr<RsaKeyPairGenConfig> config);

} // namespace margelo
Expand Down
Loading

0 comments on commit 2cd2f96

Please sign in to comment.