diff --git a/bun.lock b/bun.lock index bbae5b197..c48a89bd2 100644 --- a/bun.lock +++ b/bun.lock @@ -85,6 +85,21 @@ "typescript": "5.8.3", }, }, + "packages/quick-crypto-nitro": { + "name": "quick-crypto-nitro", + "version": "0.0.1-alpha.0", + "dependencies": { + "buffer": "6.0.3", + }, + "devDependencies": { + "nitro-core": "1.0.0-alpha.nightly.120.g33ef045b96c0", + "nitrogen": "1.0.0-alpha.nightly.120.g33ef045b96c0", + "typescript": "5.8.3", + }, + "peerDependencies": { + "nitro-core": "1.0.0-alpha.nightly.120.g33ef045b96c0", + }, + }, "packages/react-native-quick-crypto": { "name": "react-native-quick-crypto", "version": "1.1.10", @@ -514,6 +529,8 @@ "@isaacs/fs-minipass": ["@isaacs/fs-minipass@4.0.1", "", { "dependencies": { "minipass": "^7.0.4" } }, "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w=="], + "@isaacs/nonrecursive-glob-match": ["@isaacs/nonrecursive-glob-match@1.1.0", "", {}, "sha512-GPm/Dbvk5RLmhRnxXIa7nTpK0xtl4E0wj0MslsuNiJ1FIROU+2+9lgUC/dgF7Ey4QCWhfEsgxilMRMCdHPfDfw=="], + "@isaacs/ttlcache": ["@isaacs/ttlcache@1.4.1", "", {}, "sha512-RQgQ4uQ+pLbqXfOmieB91ejmLwvSgv9nLx6sT6sD83s7umBypgg+OIBOBbEUiJXrfpnp9j0mRhYYdzp9uqq3lA=="], "@istanbuljs/load-nyc-config": ["@istanbuljs/load-nyc-config@1.1.0", "", { "dependencies": { "camelcase": "^5.3.1", "find-up": "^4.1.0", "get-package-type": "^0.1.0", "js-yaml": "^3.13.1", "resolve-from": "^5.0.0" } }, "sha512-VjeHSlIzpv/NyD3N0YuHfXOPDIixcA1q2ZV98wsMqcYlPmv2n3Yb2lYP9XMElnaFVXg5A7YLTeLu6V84uQDjmQ=="], @@ -1872,6 +1889,8 @@ "new-github-release-url": ["new-github-release-url@2.0.0", "", { "dependencies": { "type-fest": "^2.5.1" } }, "sha512-NHDDGYudnvRutt/VhKFlX26IotXe1w0cmkDm6JGquh5bz/bDTw0LufSmH/GxTjEdpHEO+bVKFTwdrcGa/9XlKQ=="], + "nitro-core": ["nitro-core@1.0.0-alpha.nightly.120.g33ef045b96c0", "", {}, "sha512-HJLx09kp/mgG4v81WdxQ50ymuh4uS1vhD4OTyjRX+FNVS0K6i1picnUiLzfkr0qFwcDAkfTdXmYtaKO7j+hZEg=="], + "nitrogen": ["nitrogen@0.33.2", "", { "dependencies": { "chalk": "^5.3.0", "react-native-nitro-modules": "^0.33.2", "ts-morph": "^27.0.0", "yargs": "^18.0.0", "zod": "^4.0.5" }, "bin": { "nitrogen": "lib/index.js" } }, "sha512-1fypSMqDU2vnRDmI8PYH0F3/ICLgRqsKCtOyNSMX7rQsG9D7G6zINvZH0Vk0unjzTude5SN8XNFO5im4LrGCvQ=="], "nocache": ["nocache@3.0.4", "", {}, "sha512-WDD0bdg9mbq6F4mRxEYcPWwfA1vxd0mrvKOyxI7Xj/atfRHVeutzuWByG//jfm4uPzp0y4Kj051EORCBSQMycw=="], @@ -2062,6 +2081,8 @@ "queue-microtask": ["queue-microtask@1.2.3", "", {}, "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A=="], + "quick-crypto-nitro": ["quick-crypto-nitro@workspace:packages/quick-crypto-nitro"], + "quickjs-wasi": ["quickjs-wasi@0.0.1", "", {}, "sha512-fBWNLTBkxkLAhe1AzF1hyXEvuA+N+vV1WMP2D6iiMUblvmOt8Pp5t8zUcgvz7aYA1ldUdxDlgUse15dmcKjkNg=="], "randombytes": ["randombytes@2.1.0", "", { "dependencies": { "safe-buffer": "^5.1.0" } }, "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ=="], @@ -2840,6 +2861,8 @@ "public-encrypt/bn.js": ["bn.js@4.12.2", "", {}, "sha512-n4DSx829VRTRByMRGdjQ9iqsN0Bh4OolPsFnaZBLcbi8iXcB+kJ9s7EnRt4wILZNV3kPLHkRVfOc/HvhC3ovDw=="], + "quick-crypto-nitro/nitrogen": ["nitrogen@1.0.0-alpha.nightly.120.g33ef045b96c0", "", { "dependencies": { "chalk": "^5.3.0", "nitro-core": "1.0.0-alpha.nightly.120.g33ef045b96c0", "ts-morph": "^28.0.0", "yargs": "^18.1.0", "zod": "^4.4.3" }, "bin": { "nitrogen": "bin/nitrogen.js" } }, "sha512-bFfD2HaVx+yKz0O3eXEbFRJexsIM7k+O1Ijfh58RWjLfdfa3de6JLp1tFDmButwDVPXiU6dgzGSB6H7eCowjPg=="], + "raw-body/iconv-lite": ["iconv-lite@0.4.24", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3" } }, "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA=="], "rc/ini": ["ini@1.3.8", "", {}, "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew=="], @@ -3152,6 +3175,14 @@ "pkg-up/find-up/locate-path": ["locate-path@3.0.0", "", { "dependencies": { "p-locate": "^3.0.0", "path-exists": "^3.0.0" } }, "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A=="], + "quick-crypto-nitro/nitrogen/chalk": ["chalk@5.6.2", "", {}, "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA=="], + + "quick-crypto-nitro/nitrogen/ts-morph": ["ts-morph@28.0.0", "", { "dependencies": { "@ts-morph/common": "~0.29.0", "code-block-writer": "^13.0.3" } }, "sha512-Wp3tnZ2bzwxyTZMtgWVzXDfm7lB1Drz+y9DmmYH/L702PQhPyVrp3pkou3yIz4qjS14GY9kcpmLiOOMvl8oG1g=="], + + "quick-crypto-nitro/nitrogen/yargs": ["yargs@18.2.0", "", { "dependencies": { "cliui": "^9.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "string-width": "^8.2.1", "y18n": "^5.0.5", "yargs-parser": "^22.0.0" } }, "sha512-9OpKOLeaoNFecEp7P6iYbzze/5CqWoH7N3SMs/6y1XF4nMvFMspEGZzJ6uFi9MmQwXhyzqYoSEKO3tvA3Z/o2w=="], + + "quick-crypto-nitro/nitrogen/zod": ["zod@4.6.7", "", {}, "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg=="], + "react-native-builder-bob/del/globby": ["globby@11.1.0", "", { "dependencies": { "array-union": "^2.1.0", "dir-glob": "^3.0.1", "fast-glob": "^3.2.9", "ignore": "^5.2.0", "merge2": "^1.4.1", "slash": "^3.0.0" } }, "sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g=="], "react-native-builder-bob/del/is-path-cwd": ["is-path-cwd@2.2.0", "", {}, "sha512-w942bTcih8fdJPJmQHFzkS76NEP8Kzzvmw92cXsazb8intwLqPibPPdXf4ANdKV3rYMuuQYGIWtvz9JilB3NFQ=="], @@ -3294,6 +3325,10 @@ "pkg-up/find-up/locate-path/path-exists": ["path-exists@3.0.0", "", {}, "sha512-bpC7GYwiDYQ4wYLe+FA8lhRjhQCMcQGuSgGGqDkg/QerRWw9CmGRT0iSOVRSZJ29NMLZgIzqaljJ63oaL4NIJQ=="], + "quick-crypto-nitro/nitrogen/ts-morph/@ts-morph/common": ["@ts-morph/common@0.29.1", "", { "dependencies": { "minimatch": "^11.0.1", "path-browserify": "^1.0.1", "tinyglobby": "^0.2.14" } }, "sha512-P6hGYQ+taPsxmoY4svs0uFNiaLdOPDE+F5FqTBwoA76x8HmEggQXbEhSo78ZsyrGzUxbQfdr/5P7lHYUHF0r2Q=="], + + "quick-crypto-nitro/nitrogen/yargs/string-width": ["string-width@8.3.0", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-ZbmZM0JCihQN91dWnxoipT2KOEyHqEyfRXUyjuRhW8b/xnqPDoq4gWEVApTVa9db2wN8mmoikgFBbjh71+cGeQ=="], + "react-native-builder-bob/del/globby/ignore": ["ignore@5.3.2", "", {}, "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g=="], "react-native-builder-bob/glob/minimatch/brace-expansion": ["brace-expansion@2.0.2", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ=="], @@ -3358,6 +3393,12 @@ "pkg-up/find-up/locate-path/p-locate/p-limit": ["p-limit@2.3.0", "", { "dependencies": { "p-try": "^2.0.0" } }, "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w=="], + "quick-crypto-nitro/nitrogen/ts-morph/@ts-morph/common/minimatch": ["minimatch@11.0.9", "", { "dependencies": { "@isaacs/nonrecursive-glob-match": "^1.1.0", "brace-expansion": "^5.0.12" } }, "sha512-KCFkfyjqerNlSstjlhIGNXIKZGA/6O4V4RlhZ+Dsr621SPPw0mzlypE9D9ZOoPiVefHID+j7/XuMMlqfD/F60Q=="], + + "quick-crypto-nitro/nitrogen/yargs/string-width/get-east-asian-width": ["get-east-asian-width@1.7.0", "", {}, "sha512-XjH1AECxf0giL2V1aU8vKyRR2ppRUb5c0EvT7zuJTokQ74bNo52zOtghqdWIqrhUD79fo3x0WfKZdOqxF6LG1Q=="], + + "quick-crypto-nitro/nitrogen/yargs/string-width/strip-ansi": ["strip-ansi@7.1.2", "", { "dependencies": { "ansi-regex": "^6.0.1" } }, "sha512-gmBGslpoQJtgnMAvOVqGZpEz9dyoKTCzy2nfz/n8aIFhN/jCE/rCmcxabB6jOOHV+0WNnylOxaxBQPSvcWklhA=="], + "@expo/cli/ora/chalk/ansi-styles/color-convert/color-name": ["color-name@1.1.3", "", {}, "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw=="], "@expo/cli/ora/cli-cursor/restore-cursor/onetime/mimic-fn": ["mimic-fn@1.2.0", "", {}, "sha512-jf84uxzwiuiIVKiOLpfYk7N46TSy8ubTonmneY9vrpHNAnp0QBt2BxWV9dO3/j+BoVAb+a5G6YDPW3M5HOdMWQ=="], @@ -3378,8 +3419,14 @@ "logkitty/yargs/find-up/locate-path/p-locate/p-limit": ["p-limit@2.3.0", "", { "dependencies": { "p-try": "^2.0.0" } }, "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w=="], + "quick-crypto-nitro/nitrogen/ts-morph/@ts-morph/common/minimatch/brace-expansion": ["brace-expansion@5.1.0", "", { "dependencies": { "balanced-match": "^4.0.2" } }, "sha512-aa11Dv491GKnYqxaul3aRJ0szew7yMJMfZPnqD44NjGrZXoj62l41OEDDpdpC4eE2P88vBFfLuSyRhpG91NUvg=="], + + "quick-crypto-nitro/nitrogen/yargs/string-width/strip-ansi/ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="], + "babel-preset-expo/@react-native/babel-preset/@react-native/babel-plugin-codegen/@react-native/codegen/yargs/cliui/wrap-ansi": ["wrap-ansi@7.0.0", "", { "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } }, "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q=="], "babel-preset-expo/@react-native/babel-preset/@react-native/babel-plugin-codegen/@react-native/codegen/yargs/string-width/emoji-regex": ["emoji-regex@8.0.0", "", {}, "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="], + + "quick-crypto-nitro/nitrogen/ts-morph/@ts-morph/common/minimatch/brace-expansion/balanced-match": ["balanced-match@4.0.4", "", {}, "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA=="], } } diff --git a/package.json b/package.json index 3a96a328c..fdc95d492 100644 --- a/package.json +++ b/package.json @@ -101,6 +101,7 @@ }, "workspaces": [ "packages/react-native-quick-crypto", + "packages/quick-crypto-nitro", "example" ], "dependencies": { diff --git a/packages/quick-crypto-nitro/.gitignore b/packages/quick-crypto-nitro/.gitignore new file mode 100644 index 000000000..6f665720a --- /dev/null +++ b/packages/quick-crypto-nitro/.gitignore @@ -0,0 +1,3 @@ +ios/openssl/ +ios/openssl.*/ +cpp/internal/ diff --git a/packages/quick-crypto-nitro/LICENSE b/packages/quick-crypto-nitro/LICENSE new file mode 100644 index 000000000..cae2f4306 --- /dev/null +++ b/packages/quick-crypto-nitro/LICENSE @@ -0,0 +1,27 @@ +**react-native-quick-crypto** + +MIT License + +Copyright (c) 2021 Margelo GmbH + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +**NodeJS Crypto** + +See https://github.com/nodejs/node/blob/main/LICENSE diff --git a/packages/quick-crypto-nitro/Package.swift b/packages/quick-crypto-nitro/Package.swift new file mode 100644 index 000000000..7a58684bc --- /dev/null +++ b/packages/quick-crypto-nitro/Package.swift @@ -0,0 +1,38 @@ +// swift-tools-version: 6.4 + +import PackageDescription + +let generated = "nitrogen/generated" +let package = Package( + name: "QuickCryptoNitro", + platforms: [.iOS(.v16), .macOS(.v13)], + products: [.library(name: "QuickCryptoNitro", targets: ["QuickCryptoNitro"])], + dependencies: [.package(path: "../nitro-core")], + targets: [ + .binaryTarget( + name: "QuickCryptoOpenSSL", + url: "https://github.com/margelo/react-native-quick-crypto/releases/download/openssl-apple-3.6.2/QuickCryptoOpenSSL-3.6.2.zip", + checksum: "a50e3c8473b0526b159ad8d105e97a90d1153a4a230388cc731ee23a7d0ad3a4" + ), + .target( + name: "QuickCryptoNitro", + dependencies: [.product(name: "NitroModules", package: "nitro-core"), "QuickCryptoOpenSSL"], + path: ".", + exclude: ["\(generated)/apple/c++/include/module.modulemap"], + sources: [ + "backend", "cpp", "\(generated)/shared/c++", "\(generated)/apple/c++", + "\(generated)/apple/QuickCryptoNitroAutolinking.cpp", + ], + publicHeadersPath: "\(generated)/apple/c++/include", + cxxSettings: [ + .headerSearchPath("backend"), + .headerSearchPath("cpp/include"), + .headerSearchPath("\(generated)/shared/c++/include"), + .headerSearchPath("ios/openssl/include"), + .headerSearchPath("ios/openssl"), + .unsafeFlags(["-include", "quickcrypto_openssl_prefix.h"]), + ] + ), + ], + cxxLanguageStandard: .cxx20 +) diff --git a/packages/quick-crypto-nitro/README.md b/packages/quick-crypto-nitro/README.md new file mode 100644 index 000000000..e4672a906 --- /dev/null +++ b/packages/quick-crypto-nitro/README.md @@ -0,0 +1,13 @@ +# Quick Crypto for standalone Nitro + +This package exposes incremental, fixed-length OpenSSL hashes to a standalone Nitro v1 runtime on iOS and Android. It shares the digest operations used by `react-native-quick-crypto`. The current API is `createHash(algorithm)`, `update(string | ArrayBufferView, inputEncoding?)`, and `digest(encoding?)`. Calling `digest()` returns a Buffer and finalizes that hash instance. + +```ts +import { createHash } from 'quick-crypto-nitro'; + +const hash = createHash('sha256'); +hash.update('abc'); +const hex = hash.digest('hex'); +``` + +When working from a checkout, run `bash scripts/prepare-sources.sh` and `bash scripts/prepare-openssl-apple.sh` in this directory before building a native app. The package tarball includes those headers. diff --git a/packages/quick-crypto-nitro/android/build.gradle b/packages/quick-crypto-nitro/android/build.gradle new file mode 100644 index 000000000..4b24f306c --- /dev/null +++ b/packages/quick-crypto-nitro/android/build.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation 'io.github.ronickg:openssl-static:3.6.2-3' +} diff --git a/packages/quick-crypto-nitro/android/react-nitro.cmake b/packages/quick-crypto-nitro/android/react-nitro.cmake new file mode 100644 index 000000000..7d434f090 --- /dev/null +++ b/packages/quick-crypto-nitro/android/react-nitro.cmake @@ -0,0 +1,13 @@ +find_package(opensslstatic REQUIRED CONFIG) + +# The host runtime's libwebsockets supplies OpenSSL-compatible symbols of its own. +# Give Quick Crypto a private copy of libcrypto behind the hash backend's ABI. +set(quickcrypto_backend_dir "${CMAKE_CURRENT_LIST_DIR}/../backend") +add_library(QuickCryptoHashBackend SHARED "${quickcrypto_backend_dir}/HashBackend.cpp") +set_target_properties(QuickCryptoHashBackend PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES) +target_include_directories(QuickCryptoHashBackend PUBLIC "${quickcrypto_backend_dir}") +target_link_libraries(QuickCryptoHashBackend PRIVATE opensslstatic::crypto) +target_link_options(QuickCryptoHashBackend PRIVATE "LINKER:--exclude-libs,libcrypto.a") + +target_include_directories(QuickCryptoNitro PRIVATE "${CMAKE_CURRENT_LIST_DIR}/../cpp/include") +target_link_libraries(QuickCryptoNitro QuickCryptoHashBackend) diff --git a/packages/quick-crypto-nitro/backend/HashBackend.cpp b/packages/quick-crypto-nitro/backend/HashBackend.cpp new file mode 100644 index 000000000..436ba7384 --- /dev/null +++ b/packages/quick-crypto-nitro/backend/HashBackend.cpp @@ -0,0 +1,32 @@ +#include "HashBackend.hpp" +#include "../cpp/internal/HashContext.hpp" + +namespace margelo::quickcrypto { + +HashBackend::HashBackend(const std::string& algorithm) : context(createDigestContext(algorithm)) {} + +void HashBackend::update(const uint8_t* data, size_t length) { + if (!context) { + throw std::logic_error("Digest already called"); + } + updateDigest(context.get(), data, length); +} + +std::vector HashBackend::digest() { + if (!context) { + throw std::logic_error("Digest already called"); + } + const int length = EVP_MD_CTX_get_size(context.get()); + if (length <= 0) { + throw std::runtime_error("Expected a fixed-length digest"); + } + std::vector output(length); + auto active = std::move(context); + const auto written = finishDigest(active.get(), output.data()); + if (written != output.size()) { + throw std::runtime_error("Unexpected hash digest length"); + } + return output; +} + +} // namespace margelo::quickcrypto diff --git a/packages/quick-crypto-nitro/backend/HashBackend.hpp b/packages/quick-crypto-nitro/backend/HashBackend.hpp new file mode 100644 index 000000000..a93e84f1a --- /dev/null +++ b/packages/quick-crypto-nitro/backend/HashBackend.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include +#include + +struct evp_md_ctx_st; + +namespace margelo::quickcrypto { + +class __attribute__((visibility("default"))) HashBackend final { + public: + explicit HashBackend(const std::string& algorithm); + void update(const uint8_t* data, size_t length); + std::vector digest(); + + private: + std::unique_ptr context; +}; + +} // namespace margelo::quickcrypto diff --git a/packages/quick-crypto-nitro/cpp/HybridHash.cpp b/packages/quick-crypto-nitro/cpp/HybridHash.cpp new file mode 100644 index 000000000..91696287a --- /dev/null +++ b/packages/quick-crypto-nitro/cpp/HybridHash.cpp @@ -0,0 +1,16 @@ +#include + +namespace margelo::nitro::quickcrypto { + +HybridHash::HybridHash(const std::string& algorithm) : backend(algorithm) {} + +void HybridHash::update(const ArrayBuffer& data) { + backend.update(data.data(), data.size()); +} + +ArrayBuffer HybridHash::digest() { + auto bytes = backend.digest(); + return ArrayBuffer::move(std::move(bytes)); +} + +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/cpp/include/QuickCryptoNitro/HybridHash.hpp b/packages/quick-crypto-nitro/cpp/include/QuickCryptoNitro/HybridHash.hpp new file mode 100644 index 000000000..b3a05ff0d --- /dev/null +++ b/packages/quick-crypto-nitro/cpp/include/QuickCryptoNitro/HybridHash.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "HashBackend.hpp" +#include + +namespace margelo::nitro::quickcrypto { + +class HybridHash final : public HybridHashSpec { + public: + explicit HybridHash(const std::string& algorithm); + void update(const ArrayBuffer& data) override; + ArrayBuffer digest() override; + + private: + ::margelo::quickcrypto::HashBackend backend; +}; + +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/ios/LICENSE-OpenSSL.txt b/packages/quick-crypto-nitro/ios/LICENSE-OpenSSL.txt new file mode 100644 index 000000000..49cc83d2e --- /dev/null +++ b/packages/quick-crypto-nitro/ios/LICENSE-OpenSSL.txt @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/packages/quick-crypto-nitro/nitro.json b/packages/quick-crypto-nitro/nitro.json new file mode 100644 index 000000000..064dbde33 --- /dev/null +++ b/packages/quick-crypto-nitro/nitro.json @@ -0,0 +1,21 @@ +{ + "$schema": "https://nitro.margelo.com/nitro.schema.json", + "environment": "bare", + "cxxNamespace": [ + "quickcrypto" + ], + "apple": { + "moduleName": "QuickCryptoNitro" + }, + "android": { + "namespace": [ + "quickcrypto" + ], + "cxxLibName": "QuickCryptoNitro" + }, + "ignorePaths": [ + "**/node_modules", + "**/.build", + "**/.swiftpm" + ] +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/.gitattributes b/packages/quick-crypto-nitro/nitrogen/generated/.gitattributes new file mode 100644 index 000000000..fb7a0d5a3 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/.gitattributes @@ -0,0 +1 @@ +** linguist-generated=true diff --git a/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitro+autolinking.cmake b/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitro+autolinking.cmake new file mode 100644 index 000000000..8ac270a7a --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitro+autolinking.cmake @@ -0,0 +1,27 @@ +# +# QuickCryptoNitro+autolinking.cmake +# This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +# https://github.com/margelo/nitro +# Copyright © Marc Rousavy @ Margelo +# + +target_compile_features(QuickCryptoNitro PUBLIC cxx_std_20) +target_include_directories(QuickCryptoNitro PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/../shared/c++/include" + "${CMAKE_CURRENT_LIST_DIR}/c++/include" + "${CMAKE_CURRENT_LIST_DIR}/include" +) +target_sources(QuickCryptoNitro PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/QuickCryptoNitroOnLoad.cpp" + "${CMAKE_CURRENT_LIST_DIR}/../android/c++/HybridHashSpecClass.cpp" + "${CMAKE_CURRENT_LIST_DIR}/../shared/c++/HybridHashSpec.cpp" +) + +find_package(fbjni REQUIRED CONFIG) +find_package(nitro-core REQUIRED CONFIG) + +target_link_libraries(QuickCryptoNitro + fbjni::fbjni + nitro-core::NitroModules + +) diff --git a/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitro+autolinking.gradle b/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitro+autolinking.gradle new file mode 100644 index 000000000..d6513c8f6 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitro+autolinking.gradle @@ -0,0 +1,10 @@ +/// +/// QuickCryptoNitro+autolinking.gradle +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +// Resolve beside this script so custom Nitrogen output directories work too. +def nitrogenDirectory = buildscript.sourceFile.parentFile +android.sourceSets.main.java.srcDirs += [new File(nitrogenDirectory, 'kotlin')] diff --git a/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitroOnLoad.cpp b/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitroOnLoad.cpp new file mode 100644 index 000000000..8b20e441c --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/android/QuickCryptoNitroOnLoad.cpp @@ -0,0 +1,18 @@ +/// +/// QuickCryptoNitroOnLoad.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include +#include +#include + +namespace margelo::nitro::quickcrypto { + void registerAllNatives() { + + + HybridClassRegistry::registerHybridClass("quickcrypto.Hash", HybridHashSpec::Class::get()); + } +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/android/c++/HybridHashSpecClass.cpp b/packages/quick-crypto-nitro/nitrogen/generated/android/c++/HybridHashSpecClass.cpp new file mode 100644 index 000000000..6ff72226f --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/android/c++/HybridHashSpecClass.cpp @@ -0,0 +1,37 @@ +/// +/// HybridHashSpecClass.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include +#include +#include +#include +#include +#include +#include + +namespace margelo::nitro::quickcrypto { + + namespace { + struct HybridHashSpecBindings final { + static std::shared_ptr constructor(const std::string& algorithm) { + static_assert(std::is_constructible_v<::margelo::nitro::quickcrypto::HybridHash, decltype(algorithm)>, + "Hash: ::margelo::nitro::quickcrypto::HybridHash must implement the declared public constructor."); + return std::make_shared<::margelo::nitro::quickcrypto::HybridHash>(std::forward(algorithm)); + } + }; + } + + const std::shared_ptr& HybridHashSpec::Class::get() { + static const auto hybridClass = HybridClass::create([](HybridClass& clazz) { + clazz.registerHybridMethod("update", &HybridHashSpec::update); + clazz.registerHybridMethod("digest", &HybridHashSpec::digest); + clazz.registerHybridConstructor(&HybridHashSpecBindings::constructor); + }); + return hybridClass; + } + +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/nitrogen/generated/android/include/QuickCryptoNitro/private/QuickCryptoNitroOnLoad.hpp b/packages/quick-crypto-nitro/nitrogen/generated/android/include/QuickCryptoNitro/private/QuickCryptoNitroOnLoad.hpp new file mode 100644 index 000000000..f7290a80a --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/android/include/QuickCryptoNitro/private/QuickCryptoNitroOnLoad.hpp @@ -0,0 +1,13 @@ +/// +/// QuickCryptoNitroOnLoad.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +namespace margelo::nitro::quickcrypto { + // Call inside facebook::jni::initialize() from JNI_OnLoad. + void registerAllNatives(); +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/android/kotlin/com/margelo/nitro/quickcrypto/QuickCryptoNitroOnLoad.kt b/packages/quick-crypto-nitro/nitrogen/generated/android/kotlin/com/margelo/nitro/quickcrypto/QuickCryptoNitroOnLoad.kt new file mode 100644 index 000000000..44e6294ec --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/android/kotlin/com/margelo/nitro/quickcrypto/QuickCryptoNitroOnLoad.kt @@ -0,0 +1,22 @@ +/// +/// QuickCryptoNitroOnLoad.kt +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +package com.margelo.nitro.quickcrypto + +internal class QuickCryptoNitroOnLoad { + companion object { + private var didLoad = false + + @JvmStatic + @Synchronized + fun initializeNative() { + if (didLoad) return + System.loadLibrary("QuickCryptoNitro") + didLoad = true + } + } +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitro-Swift-Cxx-Bridge.cpp b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitro-Swift-Cxx-Bridge.cpp new file mode 100644 index 000000000..a968330a4 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitro-Swift-Cxx-Bridge.cpp @@ -0,0 +1,12 @@ +/// +/// QuickCryptoNitro-Swift-Cxx-Bridge.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// +#include + + +namespace margelo::nitro::quickcrypto::swift_bridge { + +} // namespace margelo::nitro::quickcrypto::swift_bridge diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAppleCxx/QuickCryptoNitroAppleCxx.cpp b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAppleCxx/QuickCryptoNitroAppleCxx.cpp new file mode 100644 index 000000000..0aa6cf32b --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAppleCxx/QuickCryptoNitroAppleCxx.cpp @@ -0,0 +1,9 @@ +/// +/// QuickCryptoNitroAppleCxx.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +// Swift packages require at least one source file per C++ target. +#include diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAppleCxx/include/QuickCryptoNitroAppleCxx/QuickCryptoNitroAppleCxx.h b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAppleCxx/include/QuickCryptoNitroAppleCxx/QuickCryptoNitroAppleCxx.h new file mode 100644 index 000000000..76c16ac7a --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAppleCxx/include/QuickCryptoNitroAppleCxx/QuickCryptoNitroAppleCxx.h @@ -0,0 +1,14 @@ +/// +/// QuickCryptoNitroAppleCxx.h +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + + + +namespace margelo::nitro::quickcrypto::swift_bridge { + +} // namespace margelo::nitro::quickcrypto::swift_bridge diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAutolinking.cpp b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAutolinking.cpp new file mode 100644 index 000000000..579a5c000 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAutolinking.cpp @@ -0,0 +1,20 @@ +/// +/// QuickCryptoNitroAutolinking.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include +#include + +#include + +namespace margelo::nitro::quickcrypto { + void registerHybridClasses() { + using namespace margelo::nitro; + using namespace margelo::nitro::quickcrypto; + + HybridClassRegistry::registerHybridClass("quickcrypto.Hash", HybridHashSpec::Class::get()); + } +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAutolinking.mm b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAutolinking.mm new file mode 100644 index 000000000..88ac18c38 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/QuickCryptoNitroAutolinking.mm @@ -0,0 +1,24 @@ +/// +/// QuickCryptoNitroAutolinking.mm +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#import +#include + +/** + * Registers all HybridObject classes of QuickCryptoNitro + * when the app loads, so JS can look them up by name. + */ +@interface QuickCryptoNitroAutolinking : NSObject +@end + +@implementation QuickCryptoNitroAutolinking + ++ (void)load { + margelo::nitro::quickcrypto::registerHybridClasses(); +} + +@end diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/HybridHashSpecClass.cpp b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/HybridHashSpecClass.cpp new file mode 100644 index 000000000..6ff72226f --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/HybridHashSpecClass.cpp @@ -0,0 +1,37 @@ +/// +/// HybridHashSpecClass.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include +#include +#include +#include +#include +#include +#include + +namespace margelo::nitro::quickcrypto { + + namespace { + struct HybridHashSpecBindings final { + static std::shared_ptr constructor(const std::string& algorithm) { + static_assert(std::is_constructible_v<::margelo::nitro::quickcrypto::HybridHash, decltype(algorithm)>, + "Hash: ::margelo::nitro::quickcrypto::HybridHash must implement the declared public constructor."); + return std::make_shared<::margelo::nitro::quickcrypto::HybridHash>(std::forward(algorithm)); + } + }; + } + + const std::shared_ptr& HybridHashSpec::Class::get() { + static const auto hybridClass = HybridClass::create([](HybridClass& clazz) { + clazz.registerHybridMethod("update", &HybridHashSpec::update); + clazz.registerHybridMethod("digest", &HybridHashSpec::digest); + clazz.registerHybridConstructor(&HybridHashSpecBindings::constructor); + }); + return hybridClass; + } + +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/QuickCryptoNitro/QuickCryptoNitroAutolinking.hpp b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/QuickCryptoNitro/QuickCryptoNitroAutolinking.hpp new file mode 100644 index 000000000..3e82d474c --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/QuickCryptoNitro/QuickCryptoNitroAutolinking.hpp @@ -0,0 +1,13 @@ +/// +/// QuickCryptoNitroAutolinking.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +namespace margelo::nitro::quickcrypto { + // Register all generated HybridObject classes + void registerHybridClasses(); +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/QuickCryptoNitro/private/QuickCryptoNitro-Swift-Cxx-Umbrella.hpp b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/QuickCryptoNitro/private/QuickCryptoNitro-Swift-Cxx-Umbrella.hpp new file mode 100644 index 000000000..b2e83d756 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/QuickCryptoNitro/private/QuickCryptoNitro-Swift-Cxx-Umbrella.hpp @@ -0,0 +1,27 @@ +/// +/// QuickCryptoNitro-Swift-Cxx-Umbrella.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +// Private C++ implementation includes. Never import this umbrella from Swift. +// Include Swift defined types. They define swift::Optional and swift::Array, +// which the C++ helpers below only forward-declare for Swift's importer. +#include + +// C++ helpers for Swift +#include + +#if __has_include("QuickCryptoNitro-Swift.h") +// This header is generated by Xcode/Swift on every app build. +// If it cannot be found, make sure the Swift module's name (= podspec name) is actually "QuickCryptoNitro". +#include "QuickCryptoNitro-Swift.h" +// Same as above, but used when building with frameworks (`use_frameworks`) +#elif __has_include() +#include +#else +#error QuickCryptoNitro's autogenerated Swift header cannot be found! Make sure the Swift module's name (= podspec name) is actually "QuickCryptoNitro", and try building the app first. +#endif diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/module.modulemap b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/module.modulemap new file mode 100644 index 000000000..11688a62b --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/c++/include/module.modulemap @@ -0,0 +1,12 @@ +/// +/// module.modulemap +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +// Headers used by Swift belong to the AppleCxx module. +// This implementation target must not claim them through an umbrella directory. +module QuickCryptoNitroSwiftBridge { + requires cplusplus +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/apple/swift/QuickCryptoNitroModule.swift b/packages/quick-crypto-nitro/nitrogen/generated/apple/swift/QuickCryptoNitroModule.swift new file mode 100644 index 000000000..30ebff883 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/apple/swift/QuickCryptoNitroModule.swift @@ -0,0 +1,2 @@ +// Generated by nitrogen. Keeps the SwiftPM module available to its C++ bridge. +internal import NitroModules diff --git a/packages/quick-crypto-nitro/nitrogen/generated/hybrid-classes.json b/packages/quick-crypto-nitro/nitrogen/generated/hybrid-classes.json new file mode 100644 index 000000000..13d87f9cc --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/hybrid-classes.json @@ -0,0 +1,21 @@ +{ + "version": 2, + "classes": { + "Hash": { + "android": { + "implementation": { + "className": "HybridHash", + "include": "" + }, + "language": "c++" + }, + "apple": { + "implementation": { + "className": "HybridHash", + "include": "" + }, + "language": "c++" + } + } + } +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/shared/c++/HybridHashSpec.cpp b/packages/quick-crypto-nitro/nitrogen/generated/shared/c++/HybridHashSpec.cpp new file mode 100644 index 000000000..8a6f17993 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/shared/c++/HybridHashSpec.cpp @@ -0,0 +1,14 @@ +/// +/// HybridHashSpec.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include + +namespace margelo::nitro::quickcrypto { + + HybridHashSpec::~HybridHashSpec() = default; + +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/nitrogen/generated/shared/c++/include/QuickCryptoNitro/HybridHashSpec.hpp b/packages/quick-crypto-nitro/nitrogen/generated/shared/c++/include/QuickCryptoNitro/HybridHashSpec.hpp new file mode 100644 index 000000000..784fc3a39 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/shared/c++/include/QuickCryptoNitro/HybridHashSpec.hpp @@ -0,0 +1,62 @@ +/// +/// HybridHashSpec.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#include +#include +#include +#include + +namespace margelo::nitro::quickcrypto { + + using namespace margelo::nitro; + + /** + * An abstract base class for `Hash` + * Inherit this class to create instances of `HybridHashSpec` in C++. + * @example + * ```cpp + * class HybridHash : public HybridHashSpec { + * public: + * HybridHash(...) { ... } + * // ... + * }; + * ``` + */ + class HybridHashSpec : public virtual HybridObject { + public: + struct Class { + using Base = HybridObject::Class; + static constexpr const char* name = "Hash"; + static const std::shared_ptr& get(); + }; + + public: + // Constructor + HybridHashSpec() = default; + + // Destructor + ~HybridHashSpec() override; + + public: + // Properties + + + public: + // Methods + virtual void update(const ArrayBuffer& data) = 0; + virtual ArrayBuffer digest() = 0; + + protected: + // Hybrid Setup + const std::shared_ptr& getHybridClass() override { + return Class::get(); + } + }; + +} // namespace margelo::nitro::quickcrypto diff --git a/packages/quick-crypto-nitro/nitrogen/generated/shared/javascript/Hash.cjs b/packages/quick-crypto-nitro/nitrogen/generated/shared/javascript/Hash.cjs new file mode 100644 index 000000000..baf04ac9f --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/shared/javascript/Hash.cjs @@ -0,0 +1,19 @@ +/// +/// Hash.cjs +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +const { NitroModules } = require('nitro-core') + +let hybridClass + +module.exports = { + get Hash() { + if (hybridClass === undefined) { + hybridClass = NitroModules.getHybridClass('quickcrypto.Hash') + } + return hybridClass + }, +} diff --git a/packages/quick-crypto-nitro/nitrogen/generated/shared/javascript/Hash.d.cts b/packages/quick-crypto-nitro/nitrogen/generated/shared/javascript/Hash.d.cts new file mode 100644 index 000000000..56d8c7ce1 --- /dev/null +++ b/packages/quick-crypto-nitro/nitrogen/generated/shared/javascript/Hash.d.cts @@ -0,0 +1,9 @@ +/// +/// Hash.d.cts +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/margelo/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +// The spec's `export declare class Hash` types this runtime export. +export {} diff --git a/packages/quick-crypto-nitro/package.json b/packages/quick-crypto-nitro/package.json new file mode 100644 index 000000000..ce1ff1acd --- /dev/null +++ b/packages/quick-crypto-nitro/package.json @@ -0,0 +1,44 @@ +{ + "name": "quick-crypto-nitro", + "version": "0.0.1-alpha.0", + "description": "Quick Crypto hashing for standalone Nitro runtimes", + "license": "MIT", + "type": "module", + "exports": { + ".": { + "types": "./src/index.ts", + "default": "./src/index.ts" + }, + "./package.json": "./package.json" + }, + "imports": { + "#nitro/*": "./nitrogen/generated/shared/javascript/*.cjs" + }, + "scripts": { + "specs": "bun run --bun nitrogen", + "typescript": "tsc --noEmit", + "prepack": "bash scripts/prepare-sources.sh && bash scripts/prepare-openssl-apple.sh" + }, + "files": [ + "src", + "cpp", + "backend", + "android", + "ios", + "scripts", + "nitrogen/generated", + "Package.swift", + "nitro.json" + ], + "peerDependencies": { + "nitro-core": "1.0.0-alpha.nightly.120.g33ef045b96c0" + }, + "devDependencies": { + "nitrogen": "1.0.0-alpha.nightly.120.g33ef045b96c0", + "nitro-core": "1.0.0-alpha.nightly.120.g33ef045b96c0", + "typescript": "5.8.3" + }, + "dependencies": { + "buffer": "6.0.3" + } +} diff --git a/packages/quick-crypto-nitro/scripts/prepare-openssl-apple.sh b/packages/quick-crypto-nitro/scripts/prepare-openssl-apple.sh new file mode 100644 index 000000000..8c2f83e76 --- /dev/null +++ b/packages/quick-crypto-nitro/scripts/prepare-openssl-apple.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." +version=3.6.2 +checksum=a50e3c8473b0526b159ad8d105e97a90d1153a4a230388cc731ee23a7d0ad3a4 +mkdir -p ios +stage=$(mktemp -d ios/openssl.XXXXXXXX) +trap 'rm -rf "$stage"' EXIT +archive="$stage/QuickCryptoOpenSSL.zip" +curl --fail --location --retry 3 --output "$archive" \ + "https://github.com/margelo/react-native-quick-crypto/releases/download/openssl-apple-$version/QuickCryptoOpenSSL-$version.zip" +printf '%s %s\n' "$checksum" "$archive" | shasum -a 256 --check +# The release keeps these headers next to the XCFramework. SwiftPM only exposes the binary. +unzip -q "$archive" 'include/*' quickcrypto_openssl_prefix.h -d "$stage/openssl" +rm -rf ios/openssl +mv "$stage/openssl" ios/openssl diff --git a/packages/quick-crypto-nitro/scripts/prepare-sources.sh b/packages/quick-crypto-nitro/scripts/prepare-sources.sh new file mode 100644 index 000000000..07834787e --- /dev/null +++ b/packages/quick-crypto-nitro/scripts/prepare-sources.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." +mkdir -p cpp/internal +cp ../react-native-quick-crypto/cpp/hash/HashContext.hpp cpp/internal/ diff --git a/packages/quick-crypto-nitro/src/Hash.nitro.ts b/packages/quick-crypto-nitro/src/Hash.nitro.ts new file mode 100644 index 000000000..e60716dde --- /dev/null +++ b/packages/quick-crypto-nitro/src/Hash.nitro.ts @@ -0,0 +1,23 @@ +import { Android, iOS, type HybridObject } from 'nitro-core'; + +export * from '#nitro/Hash'; + +@Android({ + language: 'c++', + implementation: { + className: 'HybridHash', + include: '', + }, +}) +@iOS({ + language: 'c++', + implementation: { + className: 'HybridHash', + include: '', + }, +}) +export declare class Hash extends HybridObject { + public constructor(algorithm: string); + update(data: ArrayBuffer): void; + digest(): ArrayBuffer; +} diff --git a/packages/quick-crypto-nitro/src/index.ts b/packages/quick-crypto-nitro/src/index.ts new file mode 100644 index 000000000..e80e2491d --- /dev/null +++ b/packages/quick-crypto-nitro/src/index.ts @@ -0,0 +1,56 @@ +import { Buffer } from 'buffer'; +import { Hash as NativeHash } from './Hash.nitro'; + +type HashEncoding = NonNullable[0]>; + +class Hash { + private readonly native: NativeHash; + + constructor(algorithm: string) { + this.native = new NativeHash(algorithm); + } + + update(data: string | ArrayBufferView, inputEncoding?: HashEncoding): this { + let bytes: ArrayBufferView; + if (typeof data === 'string') { + bytes = Buffer.from(data, inputEncoding); + } else if (ArrayBuffer.isView(data)) { + bytes = data; + } else { + throw new TypeError('Expected a string, TypedArray, or DataView'); + } + + const { buffer, byteOffset, byteLength } = bytes; + if ( + buffer instanceof ArrayBuffer && + byteOffset === 0 && + byteLength === buffer.byteLength + ) { + this.native.update(buffer); + } else { + const view = new Uint8Array(buffer, byteOffset, byteLength); + const copy = new Uint8Array(view); + this.native.update(copy.buffer); + } + return this; + } + + digest(): Buffer; + digest(encoding: HashEncoding): string; + digest(encoding?: HashEncoding): Buffer | string { + const raw = this.native.digest(); + const bytes = Buffer.from(raw); + if (encoding === undefined) return bytes; + return bytes.toString(encoding); + } +} + +/** Creates an incremental fixed-length hash. */ +export function createHash(algorithm: string, options?: never): Hash { + if (options !== undefined) { + throw new TypeError('Hash options are not supported'); + } + return new Hash(algorithm); +} + +export default { createHash: createHash }; diff --git a/packages/quick-crypto-nitro/tsconfig.json b/packages/quick-crypto-nitro/tsconfig.json new file mode 100644 index 000000000..03474f463 --- /dev/null +++ b/packages/quick-crypto-nitro/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "strict": true, + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "Bundler", + "skipLibCheck": true, + "noEmit": true + }, + "include": [ + "src" + ] +} diff --git a/packages/react-native-quick-crypto/cpp/hash/HashContext.hpp b/packages/react-native-quick-crypto/cpp/hash/HashContext.hpp new file mode 100644 index 000000000..cf0bad35f --- /dev/null +++ b/packages/react-native-quick-crypto/cpp/hash/HashContext.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +#include +#include +#include + +namespace margelo::quickcrypto { + +using DigestContext = std::unique_ptr; + +inline DigestContext createDigestContext(const std::string& algorithm) { + ERR_clear_error(); + if (algorithm.find('\0') != std::string::npos) { + throw std::invalid_argument("Invalid hash algorithm"); + } + std::unique_ptr md(EVP_MD_fetch(nullptr, algorithm.c_str(), nullptr), EVP_MD_free); + if (!md) { + throw std::invalid_argument("Unknown hash algorithm: " + algorithm); + } + DigestContext ctx(EVP_MD_CTX_new(), EVP_MD_CTX_free); + if (!ctx || EVP_DigestInit_ex(ctx.get(), md.get(), nullptr) != 1) { + throw std::runtime_error("Failed to initialize hash: " + std::to_string(ERR_get_error())); + } + return ctx; +} + +inline void updateDigest(EVP_MD_CTX* ctx, const void* data, size_t size) { + if (EVP_DigestUpdate(ctx, data, size) != 1) { + throw std::runtime_error("Failed to update hash: " + std::to_string(ERR_get_error())); + } +} + +inline unsigned int finishDigest(EVP_MD_CTX* ctx, unsigned char* output) { + unsigned int written; + if (EVP_DigestFinal_ex(ctx, output, &written) != 1) { + throw std::runtime_error("Failed to finalize hash: " + std::to_string(ERR_get_error())); + } + return written; +} + +} // namespace margelo::quickcrypto diff --git a/packages/react-native-quick-crypto/cpp/hash/HybridHash.cpp b/packages/react-native-quick-crypto/cpp/hash/HybridHash.cpp index 7df66ec7f..244a5a9d1 100644 --- a/packages/react-native-quick-crypto/cpp/hash/HybridHash.cpp +++ b/packages/react-native-quick-crypto/cpp/hash/HybridHash.cpp @@ -2,70 +2,19 @@ #include #include #include +#include #include #include #include #include "HybridHash.hpp" -#include "QuickCryptoUtils.hpp" namespace margelo::nitro::crypto { -HybridHash::~HybridHash() { - if (ctx) { - EVP_MD_CTX_free(ctx); - ctx = nullptr; - } - if (md && md_fetched) { - EVP_MD_free(md); - md = nullptr; - } -} - void HybridHash::createHash(const std::string& hashAlgorithmArg, const std::optional outputLengthArg) { - // Clear any previous OpenSSL errors to prevent pollution - clearOpenSSLErrors(); - - // Clean up existing resources before creating new ones - if (ctx) { - EVP_MD_CTX_free(ctx); - ctx = nullptr; - } - if (md && md_fetched) { - EVP_MD_free(md); - md = nullptr; - md_fetched = false; - } - - algorithm = hashAlgorithmArg; + ctx.reset(); + ctx = ::margelo::quickcrypto::createDigestContext(hashAlgorithmArg); outputLength = outputLengthArg; - - // Create hash context - ctx = EVP_MD_CTX_new(); - if (!ctx) { - throw std::runtime_error("Failed to create hash context: " + std::to_string(ERR_get_error())); - } - - // Fetch the message digest using modern provider-based API - md = EVP_MD_fetch(nullptr, algorithm.c_str(), nullptr); - if (!md) { - EVP_MD_CTX_free(ctx); - ctx = nullptr; - throw std::runtime_error("Unknown hash algorithm: " + algorithm); - } - md_fetched = true; - - // Initialize the digest - if (EVP_DigestInit_ex(ctx, md, nullptr) != 1) { - EVP_MD_CTX_free(ctx); - ctx = nullptr; - if (md_fetched) { - EVP_MD_free(md); - md = nullptr; - md_fetched = false; - } - throw std::runtime_error("Failed to initialize hash digest: " + std::to_string(ERR_get_error())); - } } void HybridHash::update(const std::variant, std::string>& data) { @@ -75,14 +24,10 @@ void HybridHash::update(const std::variant, std::st if (std::holds_alternative(data)) { const std::string& str = std::get(data); - if (EVP_DigestUpdate(ctx, reinterpret_cast(str.data()), str.length()) != 1) { - throw std::runtime_error("Failed to update hash digest: " + std::to_string(ERR_get_error())); - } + ::margelo::quickcrypto::updateDigest(ctx.get(), str.data(), str.size()); } else { const std::shared_ptr& buffer = std::get>(data); - if (EVP_DigestUpdate(ctx, reinterpret_cast(buffer->data()), buffer->size()) != 1) { - throw std::runtime_error("Failed to update hash digest: " + std::to_string(ERR_get_error())); - } + ::margelo::quickcrypto::updateDigest(ctx.get(), buffer->data(), buffer->size()); } } @@ -94,7 +39,7 @@ std::shared_ptr HybridHash::digest(const std::optional setParams(); // Get the default digest size - const size_t defaultLen = EVP_MD_CTX_size(ctx); + const size_t defaultLen = EVP_MD_CTX_size(ctx.get()); const size_t digestSize = (outputLength.has_value()) ? static_cast(*outputLength) : defaultLen; if (digestSize < 0) { @@ -104,14 +49,9 @@ std::shared_ptr HybridHash::digest(const std::optional auto hashBuffer = std::make_unique(digestSize); size_t hashLength = digestSize; - int ret; if (digestSize == defaultLen) { - ret = EVP_DigestFinal_ex(ctx, hashBuffer.get(), reinterpret_cast(&hashLength)); - } else { - ret = EVP_DigestFinalXOF(ctx, hashBuffer.get(), hashLength); - } - - if (ret != 1) { + hashLength = ::margelo::quickcrypto::finishDigest(ctx.get(), hashBuffer.get()); + } else if (EVP_DigestFinalXOF(ctx.get(), hashBuffer.get(), hashLength) != 1) { throw std::runtime_error("Failed to finalize hash digest: " + std::to_string(ERR_get_error())); } @@ -125,18 +65,17 @@ std::shared_ptr HybridHash::copy(const s } // Create a new context - EVP_MD_CTX* newCtx = EVP_MD_CTX_new(); + ::margelo::quickcrypto::DigestContext newCtx(EVP_MD_CTX_new(), EVP_MD_CTX_free); if (!newCtx) { throw std::runtime_error("Failed to create new hash context: " + std::to_string(ERR_get_error())); } // Copy the existing context to the new one - if (EVP_MD_CTX_copy(newCtx, ctx) != 1) { - EVP_MD_CTX_free(newCtx); + if (EVP_MD_CTX_copy(newCtx.get(), ctx.get()) != 1) { throw std::runtime_error("Failed to copy hash context: " + std::to_string(ERR_get_error())); } - return std::make_shared(newCtx, md, algorithm, outputLengthArg, false); + return std::make_shared(std::move(newCtx), outputLengthArg); } std::vector HybridHash::getSupportedHashAlgorithms() { @@ -170,14 +109,8 @@ void HybridHash::setParams() { OSSL_PARAM params[] = {OSSL_PARAM_construct_uint("xoflen", &xoflen), OSSL_PARAM_END}; - if (EVP_MD_CTX_set_params(ctx, params) != 1) { - EVP_MD_CTX_free(ctx); - ctx = nullptr; - if (md && md_fetched) { - EVP_MD_free(md); - md = nullptr; - md_fetched = false; - } + if (EVP_MD_CTX_set_params(ctx.get(), params) != 1) { + ctx.reset(); throw std::runtime_error("Failed to set XOF length (outputLength) parameter: " + std::to_string(ERR_get_error())); } } diff --git a/packages/react-native-quick-crypto/cpp/hash/HybridHash.hpp b/packages/react-native-quick-crypto/cpp/hash/HybridHash.hpp index a71f512ce..df0004279 100644 --- a/packages/react-native-quick-crypto/cpp/hash/HybridHash.hpp +++ b/packages/react-native-quick-crypto/cpp/hash/HybridHash.hpp @@ -1,6 +1,6 @@ +#include "HashContext.hpp" #include #include -#include #include #include #include @@ -14,9 +14,8 @@ using namespace facebook; class HybridHash : public HybridHashSpec { public: HybridHash() : HybridObject(TAG) {} - HybridHash(EVP_MD_CTX* ctx, EVP_MD* md, const std::string& algorithm, const std::optional outputLength, bool md_fetched = false) - : HybridObject(TAG), ctx(ctx), md(md), md_fetched(md_fetched), algorithm(algorithm), outputLength(outputLength) {} - ~HybridHash(); + HybridHash(::margelo::quickcrypto::DigestContext context, std::optional outputLength) + : HybridObject(TAG), ctx(std::move(context)), outputLength(outputLength) {} public: // Methods @@ -33,10 +32,7 @@ class HybridHash : public HybridHashSpec { private: // Properties - EVP_MD_CTX* ctx = nullptr; - EVP_MD* md = nullptr; - bool md_fetched = false; - std::string algorithm = ""; + ::margelo::quickcrypto::DigestContext ctx{nullptr, EVP_MD_CTX_free}; std::optional outputLength = std::nullopt; };