react-native-quick-crypto 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +23 -6
  2. package/cpp/Cipher/MGLCipherHostObject.cpp +64 -48
  3. package/cpp/Cipher/MGLCipherKeys.cpp +1469 -0
  4. package/cpp/Cipher/MGLCipherKeys.h +124 -0
  5. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +56 -53
  6. package/cpp/Cipher/MGLCreateCipherInstaller.h +5 -0
  7. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +56 -53
  8. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +107 -0
  9. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +32 -0
  10. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +60 -0
  11. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +35 -0
  12. package/cpp/Cipher/MGLPublicCipher.h +120 -0
  13. package/cpp/Cipher/MGLPublicCipherInstaller.h +113 -0
  14. package/cpp/Cipher/MGLRsa.cpp +188 -0
  15. package/cpp/Cipher/MGLRsa.h +61 -0
  16. package/cpp/JSIUtils/MGLJSIUtils.h +24 -0
  17. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
  18. package/cpp/MGLQuickCryptoHostObject.cpp +42 -3
  19. package/cpp/Utils/MGLUtils.cpp +156 -0
  20. package/cpp/Utils/MGLUtils.h +254 -0
  21. package/lib/commonjs/Cipher.js +307 -0
  22. package/lib/commonjs/Cipher.js.map +1 -1
  23. package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -0
  24. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  25. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  26. package/lib/commonjs/QuickCrypto.js +8 -0
  27. package/lib/commonjs/QuickCrypto.js.map +1 -1
  28. package/lib/commonjs/Utils.js +82 -1
  29. package/lib/commonjs/Utils.js.map +1 -1
  30. package/lib/commonjs/constants.js +86 -0
  31. package/lib/commonjs/constants.js.map +1 -0
  32. package/lib/commonjs/index.js +5 -0
  33. package/lib/commonjs/index.js.map +1 -1
  34. package/lib/commonjs/keys.js +207 -0
  35. package/lib/commonjs/keys.js.map +1 -0
  36. package/lib/module/Cipher.js +296 -3
  37. package/lib/module/Cipher.js.map +1 -1
  38. package/lib/module/NativeQuickCrypto/Cipher.js +9 -1
  39. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  40. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  41. package/lib/module/QuickCrypto.js +8 -1
  42. package/lib/module/QuickCrypto.js.map +1 -1
  43. package/lib/module/Utils.js +67 -1
  44. package/lib/module/Utils.js.map +1 -1
  45. package/lib/module/constants.js +79 -0
  46. package/lib/module/constants.js.map +1 -0
  47. package/lib/module/index.js +2 -0
  48. package/lib/module/index.js.map +1 -1
  49. package/lib/module/keys.js +193 -0
  50. package/lib/module/keys.js.map +1 -0
  51. package/lib/typescript/Cipher.d.ts +58 -1
  52. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +10 -0
  53. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +6 -1
  54. package/lib/typescript/QuickCrypto.d.ts +105 -1
  55. package/lib/typescript/Utils.d.ts +11 -0
  56. package/lib/typescript/constants.d.ts +75 -0
  57. package/lib/typescript/index.d.ts +2 -0
  58. package/lib/typescript/keys.d.ts +60 -0
  59. package/package.json +5 -5
  60. package/react-native-quick-crypto.podspec +1 -1
  61. package/src/.DS_Store +0 -0
  62. package/src/Cipher.ts +444 -3
  63. package/src/NativeQuickCrypto/Cipher.ts +44 -0
  64. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +13 -1
  65. package/src/QuickCrypto.ts +12 -0
  66. package/src/Utils.ts +91 -0
  67. package/src/constants.ts +79 -0
  68. package/src/index.ts +4 -0
  69. package/src/keys.ts +297 -0
@@ -0,0 +1,156 @@
1
+ //
2
+ // MGLUtils.cpp
3
+ // react-native-quick-crypto
4
+ //
5
+ // Created by Oscar on 21.06.22.
6
+ //
7
+
8
+ #include "MGLUtils.h"
9
+
10
+ #include <jsi/jsi.h>
11
+
12
+ namespace margelo {
13
+
14
+ namespace jsi = facebook::jsi;
15
+
16
+ ByteSource::ByteSource(ByteSource&& other) noexcept
17
+ : data_(other.data_),
18
+ allocated_data_(other.allocated_data_),
19
+ size_(other.size_) {
20
+ other.allocated_data_ = nullptr;
21
+ }
22
+
23
+ ByteSource::~ByteSource() { OPENSSL_clear_free(allocated_data_, size_); }
24
+
25
+ ByteSource& ByteSource::operator=(ByteSource&& other) noexcept {
26
+ if (&other != this) {
27
+ OPENSSL_clear_free(allocated_data_, size_);
28
+ data_ = other.data_;
29
+ allocated_data_ = other.allocated_data_;
30
+ other.allocated_data_ = nullptr;
31
+ size_ = other.size_;
32
+ }
33
+ return *this;
34
+ }
35
+
36
+ // std::unique_ptr<BackingStore> ByteSource::ReleaseToBackingStore() {
37
+ // // It's ok for allocated_data_ to be nullptr but
38
+ // // only if size_ is zero.
39
+ // CHECK_IMPLIES(size_ > 0, allocated_data_ != nullptr);
40
+ // std::unique_ptr<BackingStore> ptr = ArrayBuffer::NewBackingStore(
41
+ // allocated_data_,
42
+ // size(),
43
+ // [](void*
44
+ // data,
45
+ // size_t
46
+ // length,
47
+ // void*
48
+ // deleter_data)
49
+ // {
50
+ // OPENSSL_clear_free(deleter_data,
51
+ // length);
52
+ // },
53
+ // allocated_data_);
54
+ // CHECK(ptr);
55
+ // allocated_data_ = nullptr;
56
+ // data_ = nullptr;
57
+ // size_ = 0;
58
+ // return ptr;
59
+ // }
60
+ //
61
+ // Local<ArrayBuffer> ByteSource::ToArrayBuffer(Environment* env) {
62
+ // std::unique_ptr<BackingStore> store = ReleaseToBackingStore();
63
+ // return ArrayBuffer::New(env->isolate(), std::move(store));
64
+ // }
65
+ //
66
+ // MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) {
67
+ // Local<ArrayBuffer> ab = ToArrayBuffer(env);
68
+ // return Buffer::New(env, ab, 0, ab->ByteLength());
69
+ // }
70
+
71
+ // ByteSource ByteSource::FromBIO(const BIOPointer& bio) {
72
+ //// CHECK(bio);
73
+ // BUF_MEM* bptr;
74
+ // BIO_get_mem_ptr(bio.get(), &bptr);
75
+ // ByteSource::Builder out(bptr->length);
76
+ // memcpy(out.data<void>(), bptr->data, bptr->length);
77
+ // return std::move(out).release();
78
+ //}
79
+
80
+ // ByteSource ByteSource::FromEncodedString(Environment* env,
81
+ // Local<String> key,
82
+ // enum encoding enc) {
83
+ // size_t length = 0;
84
+ // ByteSource out;
85
+ //
86
+ // if (StringBytes::Size(env->isolate(), key, enc).To(&length) && length >
87
+ // 0)
88
+ // {
89
+ // ByteSource::Builder buf(length);
90
+ // size_t actual =
91
+ // StringBytes::Write(env->isolate(), buf.data<char>(), length, key, enc);
92
+ // out = std::move(buf).release(actual);
93
+ // }
94
+ //
95
+ // return out;
96
+ // }
97
+ //
98
+ // ByteSource ByteSource::FromStringOrBuffer(Environment* env,
99
+ // Local<Value> value) {
100
+ // return IsAnyByteSource(value) ? FromBuffer(value)
101
+ // : FromString(env, value.As<String>());
102
+ // }
103
+ //
104
+ // ByteSource ByteSource::FromString(Environment* env, Local<String> str,
105
+ // bool ntc) {
106
+ // CHECK(str->IsString());
107
+ // size_t size = str->Utf8Length(env->isolate());
108
+ // size_t alloc_size = ntc ? size + 1 : size;
109
+ // ByteSource::Builder out(alloc_size);
110
+ // int opts = String::NO_OPTIONS;
111
+ // if (!ntc) opts |= String::NO_NULL_TERMINATION;
112
+ // str->WriteUtf8(env->isolate(), out.data<char>(), alloc_size, nullptr,
113
+ // opts); return std::move(out).release();
114
+ // }
115
+ //
116
+ // ByteSource ByteSource::FromBuffer(Local<Value> buffer, bool ntc) {
117
+ // ArrayBufferOrViewContents<char> buf(buffer);
118
+ // return ntc ? buf.ToNullTerminatedCopy() : buf.ToByteSource();
119
+ // }
120
+ //
121
+ // ByteSource ByteSource::FromSecretKeyBytes(
122
+ // Environment* env,
123
+ // Local<Value> value) {
124
+ // // A key can be passed as a string, buffer or KeyObject with type
125
+ // 'secret'.
126
+ // // If it is a string, we need to convert it to a buffer. We are not doing
127
+ // that
128
+ // // in JS to avoid creating an unprotected copy on the heap.
129
+ // return value->IsString() || IsAnyByteSource(value) ?
130
+ // ByteSource::FromStringOrBuffer(env, value) :
131
+ // ByteSource::FromSymmetricKeyObjectHandle(value);
132
+ // }
133
+
134
+ // ByteSource ByteSource::NullTerminatedCopy(Environment* env,
135
+ // Local<Value> value) {
136
+ // return Buffer::HasInstance(value) ? FromBuffer(value, true)
137
+ // : FromString(env, value.As<String>(), true);
138
+ // }
139
+
140
+ // ByteSource ByteSource::FromSymmetricKeyObjectHandle(Local<Value> handle) {
141
+ // CHECK(handle->IsObject());
142
+ // KeyObjectHandle* key = Unwrap<KeyObjectHandle>(handle.As<Object>());
143
+ // CHECK_NOT_NULL(key);
144
+ // return Foreign(key->Data()->GetSymmetricKey(),
145
+ // key->Data()->GetSymmetricKeySize());
146
+ // }
147
+
148
+ ByteSource ByteSource::Allocated(void* data, size_t size) {
149
+ return ByteSource(data, data, size);
150
+ }
151
+
152
+ ByteSource ByteSource::Foreign(const void* data, size_t size) {
153
+ return ByteSource(data, nullptr, size);
154
+ }
155
+
156
+ } // namespace margelo
@@ -0,0 +1,254 @@
1
+ //
2
+ // MGLUtils.h
3
+ // Pods
4
+ //
5
+ // Created by Oscar on 20.06.22.
6
+ //
7
+
8
+ #ifndef MGLUtils_h
9
+ #define MGLUtils_h
10
+
11
+ #include <openssl/dsa.h>
12
+ #include <openssl/ec.h>
13
+ #include <openssl/err.h>
14
+ #include <openssl/evp.h>
15
+ #include <openssl/kdf.h>
16
+ #include <openssl/rand.h>
17
+ #include <openssl/rsa.h>
18
+ #include <openssl/ssl.h>
19
+ #ifndef OPENSSL_NO_ENGINE
20
+ #include <openssl/engine.h>
21
+ #endif // !OPENSSL_NO_ENGINE
22
+
23
+ #include <jsi/jsi.h>
24
+
25
+ #include <memory>
26
+ #include <optional>
27
+ #include <string>
28
+ #include <utility>
29
+ #include <vector>
30
+
31
+ namespace margelo {
32
+
33
+ namespace jsi = facebook::jsi;
34
+
35
+ struct StringOrBuffer {
36
+ bool isString;
37
+ std::string stringValue;
38
+ std::vector<unsigned char> vectorValue;
39
+ };
40
+
41
+ template <typename T, void (*function)(T*)>
42
+ struct FunctionDeleter {
43
+ void operator()(T* pointer) const { function(pointer); }
44
+ typedef std::unique_ptr<T, FunctionDeleter> Pointer;
45
+ };
46
+
47
+ template <typename T, void (*function)(T*)>
48
+ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
49
+ using X509Pointer = DeleteFnPtr<X509, X509_free>;
50
+ using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>;
51
+ using PKCS8Pointer = DeleteFnPtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>;
52
+ using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
53
+ using EVPKeyPointer = DeleteFnPtr<EVP_PKEY, EVP_PKEY_free>;
54
+ using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>;
55
+ using RSAPointer = DeleteFnPtr<RSA, RSA_free>;
56
+
57
+ template <typename T>
58
+ class NonCopyableMaybe {
59
+ public:
60
+ NonCopyableMaybe() : empty_(true) {}
61
+ explicit NonCopyableMaybe(T&& value)
62
+ : empty_(false), value_(std::move(value)) {}
63
+
64
+ bool IsEmpty() const { return empty_; }
65
+
66
+ const T* get() const { return empty_ ? nullptr : &value_; }
67
+
68
+ const T* operator->() const {
69
+ // CHECK(!empty_);
70
+ return &value_;
71
+ }
72
+
73
+ T&& Release() {
74
+ // CHECK_EQ(empty_, false);
75
+ empty_ = true;
76
+ return std::move(value_);
77
+ }
78
+
79
+ private:
80
+ bool empty_;
81
+ T value_;
82
+ };
83
+
84
+ template <typename T>
85
+ inline T MultiplyWithOverflowCheck(T a, T b) {
86
+ auto ret = a * b;
87
+ // if (a != 0)
88
+ // CHECK_EQ(b, ret / a);
89
+
90
+ return ret;
91
+ }
92
+
93
+ template <typename T>
94
+ T* MallocOpenSSL(size_t count) {
95
+ void* mem = OPENSSL_malloc(MultiplyWithOverflowCheck(count, sizeof(T)));
96
+ // CHECK_IMPLIES(mem == nullptr, count == 0);
97
+ return static_cast<T*>(mem);
98
+ }
99
+
100
+ // A helper class representing a read-only byte array. When deallocated, its
101
+ // contents are zeroed.
102
+ class ByteSource {
103
+ public:
104
+ class Builder {
105
+ public:
106
+ // Allocates memory using OpenSSL's memory allocator.
107
+ explicit Builder(size_t size)
108
+ : data_(MallocOpenSSL<char>(size)), size_(size) {}
109
+
110
+ Builder(Builder&& other) = delete;
111
+ Builder& operator=(Builder&& other) = delete;
112
+ Builder(const Builder&) = delete;
113
+ Builder& operator=(const Builder&) = delete;
114
+
115
+ ~Builder() { OPENSSL_clear_free(data_, size_); }
116
+
117
+ // Returns the underlying non-const pointer.
118
+ template <typename T>
119
+ T* data() {
120
+ return reinterpret_cast<T*>(data_);
121
+ }
122
+
123
+ // Returns the (allocated) size in bytes.
124
+ size_t size() const { return size_; }
125
+
126
+ // Finalizes the Builder and returns a read-only view that is optionally
127
+ // truncated.
128
+ ByteSource release(std::optional<size_t> resize = std::nullopt) && {
129
+ if (resize) {
130
+ // CHECK_LE(*resize, size_);
131
+ if (*resize == 0) {
132
+ OPENSSL_clear_free(data_, size_);
133
+ data_ = nullptr;
134
+ }
135
+ size_ = *resize;
136
+ }
137
+ ByteSource out = ByteSource::Allocated(data_, size_);
138
+ data_ = nullptr;
139
+ size_ = 0;
140
+ return out;
141
+ }
142
+
143
+ private:
144
+ void* data_;
145
+ size_t size_;
146
+ };
147
+
148
+ ByteSource() = default;
149
+ ByteSource(ByteSource&& other) noexcept;
150
+ ~ByteSource();
151
+
152
+ ByteSource& operator=(ByteSource&& other) noexcept;
153
+
154
+ ByteSource(const ByteSource&) = delete;
155
+ ByteSource& operator=(const ByteSource&) = delete;
156
+
157
+ template <typename T = void>
158
+ const T* data() const {
159
+ return reinterpret_cast<const T*>(data_);
160
+ }
161
+
162
+ size_t size() const { return size_; }
163
+
164
+ operator bool() const { return data_ != nullptr; }
165
+
166
+ // BignumPointer ToBN() const {
167
+ // return BignumPointer(BN_bin2bn(data<unsigned char>(), size(), nullptr));
168
+ // }
169
+
170
+ // Creates a v8::BackingStore that takes over responsibility for
171
+ // any allocated data. The ByteSource will be reset with size = 0
172
+ // after being called.
173
+ // std::unique_ptr<v8::BackingStore> ReleaseToBackingStore();
174
+ //
175
+ // v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env);
176
+ //
177
+ // v8::MaybeLocal<v8::Uint8Array> ToBuffer(Environment* env);
178
+
179
+ static ByteSource Allocated(void* data, size_t size);
180
+ static ByteSource Foreign(const void* data, size_t size);
181
+
182
+ // static ByteSource FromEncodedString(Environment* env,
183
+ // v8::Local<v8::String> value,
184
+ // enum encoding enc = BASE64);
185
+ //
186
+ // static ByteSource FromStringOrBuffer(Environment* env,
187
+ // v8::Local<v8::Value> value);
188
+ //
189
+ // static ByteSource FromString(Environment* env,
190
+ // v8::Local<v8::String> str,
191
+ // bool ntc = false);
192
+
193
+ // static ByteSource FromBuffer(v8::Local<v8::Value> buffer,
194
+ // bool ntc = false);
195
+
196
+ // static ByteSource FromBIO(const BIOPointer& bio);
197
+ //
198
+ // static ByteSource NullTerminatedCopy(Environment* env,
199
+ // v8::Local<v8::Value> value);
200
+ //
201
+ // static ByteSource FromSymmetricKeyObjectHandle(v8::Local<v8::Value>
202
+ // handle);
203
+
204
+ // static ByteSource FromSecretKeyBytes(
205
+ // Environment* env,
206
+ // v8::Local<v8::Value> value);
207
+
208
+ private:
209
+ const void* data_ = nullptr;
210
+ void* allocated_data_ = nullptr;
211
+ size_t size_ = 0;
212
+
213
+ ByteSource(const void* data, void* allocated_data, size_t size)
214
+ : data_(data), allocated_data_(allocated_data), size_(size) {}
215
+ };
216
+
217
+ // Originally part of the ArrayBufferContentOrView class
218
+ inline ByteSource ToNullTerminatedByteSource(jsi::Runtime& runtime,
219
+ jsi::ArrayBuffer& buffer) {
220
+ if (buffer.size(runtime) == 0) return ByteSource();
221
+ char* buf = MallocOpenSSL<char>(buffer.size(runtime) + 1);
222
+ // CHECK_NOT_NULL(buf);
223
+ buf[buffer.size(runtime)] = 0;
224
+ memcpy(buf, buffer.data(runtime), buffer.size(runtime));
225
+ return ByteSource::Allocated(buf, buffer.size(runtime));
226
+ }
227
+
228
+ int PasswordCallback(char* buf, int size, int rwflag, void* u) {
229
+ const ByteSource* passphrase = *static_cast<const ByteSource**>(u);
230
+ if (passphrase != nullptr) {
231
+ size_t buflen = static_cast<size_t>(size);
232
+ size_t len = passphrase->size();
233
+ if (buflen < len) return -1;
234
+ memcpy(buf, passphrase->data(), len);
235
+ return len;
236
+ }
237
+
238
+ return -1;
239
+ }
240
+
241
+ void CheckEntropy() {
242
+ for (;;) {
243
+ int status = RAND_status();
244
+ // CHECK_GE(status, 0); // Cannot fail.
245
+ if (status != 0) break;
246
+
247
+ // Give up, RAND_poll() not supported.
248
+ if (RAND_poll() == 0) break;
249
+ }
250
+ }
251
+
252
+ } // namespace margelo
253
+
254
+ #endif /* MGLUtils_h */