react-native-quick-crypto 1.1.1 → 1.1.2

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 (181) hide show
  1. package/QuickCrypto.podspec +1 -0
  2. package/android/CMakeLists.txt +4 -0
  3. package/cpp/cipher/CCMCipher.cpp +7 -11
  4. package/cpp/cipher/ChaCha20Cipher.cpp +6 -10
  5. package/cpp/cipher/ChaCha20Poly1305Cipher.cpp +10 -16
  6. package/cpp/cipher/GCMCipher.cpp +3 -5
  7. package/cpp/cipher/HybridCipher.cpp +7 -13
  8. package/cpp/cipher/HybridRsaCipher.cpp +19 -27
  9. package/cpp/cipher/OCBCipher.cpp +2 -3
  10. package/cpp/cipher/XChaCha20Poly1305Cipher.cpp +13 -19
  11. package/cpp/cipher/XSalsa20Cipher.cpp +8 -12
  12. package/cpp/cipher/XSalsa20Poly1305Cipher.cpp +11 -16
  13. package/cpp/keys/HybridKeyObjectHandle.cpp +630 -2
  14. package/cpp/keys/HybridKeyObjectHandle.hpp +21 -1
  15. package/cpp/sign/HybridSignHandle.cpp +26 -8
  16. package/cpp/sign/HybridVerifyHandle.cpp +28 -11
  17. package/cpp/slhdsa/HybridSlhDsaKeyPair.cpp +245 -0
  18. package/cpp/slhdsa/HybridSlhDsaKeyPair.hpp +48 -0
  19. package/cpp/turboshake/HybridTurboShake.cpp +379 -0
  20. package/cpp/turboshake/HybridTurboShake.hpp +28 -0
  21. package/cpp/utils/HybridUtils.cpp +26 -14
  22. package/deps/blake3/README.md +6 -7
  23. package/deps/blake3/c/blake3.c +3 -2
  24. package/deps/blake3/c/blake3.h +2 -2
  25. package/deps/blake3/c/blake3_dispatch.c +2 -2
  26. package/deps/blake3/c/blake3_impl.h +1 -1
  27. package/deps/blake3/c/blake3_neon.c +5 -4
  28. package/deps/ncrypto/include/ncrypto/version.h +2 -2
  29. package/deps/ncrypto/include/ncrypto.h +9 -2
  30. package/deps/ncrypto/src/ncrypto.cpp +130 -35
  31. package/lib/commonjs/dhKeyPair.js +3 -0
  32. package/lib/commonjs/dhKeyPair.js.map +1 -1
  33. package/lib/commonjs/dsa.js +3 -0
  34. package/lib/commonjs/dsa.js.map +1 -1
  35. package/lib/commonjs/ec.js +37 -30
  36. package/lib/commonjs/ec.js.map +1 -1
  37. package/lib/commonjs/ed.js +60 -6
  38. package/lib/commonjs/ed.js.map +1 -1
  39. package/lib/commonjs/hash.js +52 -5
  40. package/lib/commonjs/hash.js.map +1 -1
  41. package/lib/commonjs/keys/classes.js +33 -7
  42. package/lib/commonjs/keys/classes.js.map +1 -1
  43. package/lib/commonjs/keys/generateKeyPair.js +85 -4
  44. package/lib/commonjs/keys/generateKeyPair.js.map +1 -1
  45. package/lib/commonjs/keys/index.js +50 -2
  46. package/lib/commonjs/keys/index.js.map +1 -1
  47. package/lib/commonjs/keys/signVerify.js +9 -2
  48. package/lib/commonjs/keys/signVerify.js.map +1 -1
  49. package/lib/commonjs/keys/utils.js +59 -1
  50. package/lib/commonjs/keys/utils.js.map +1 -1
  51. package/lib/commonjs/random.js +63 -9
  52. package/lib/commonjs/random.js.map +1 -1
  53. package/lib/commonjs/rsa.js +3 -0
  54. package/lib/commonjs/rsa.js.map +1 -1
  55. package/lib/commonjs/slhdsa.js +70 -0
  56. package/lib/commonjs/slhdsa.js.map +1 -0
  57. package/lib/commonjs/specs/slhDsaKeyPair.nitro.js +6 -0
  58. package/lib/commonjs/specs/slhDsaKeyPair.nitro.js.map +1 -0
  59. package/lib/commonjs/specs/turboshake.nitro.js +6 -0
  60. package/lib/commonjs/specs/turboshake.nitro.js.map +1 -0
  61. package/lib/commonjs/subtle.js +926 -275
  62. package/lib/commonjs/subtle.js.map +1 -1
  63. package/lib/commonjs/utils/conversion.js +53 -19
  64. package/lib/commonjs/utils/conversion.js.map +1 -1
  65. package/lib/commonjs/utils/errors.js +63 -4
  66. package/lib/commonjs/utils/errors.js.map +1 -1
  67. package/lib/commonjs/utils/types.js.map +1 -1
  68. package/lib/commonjs/utils/validation.js +46 -0
  69. package/lib/commonjs/utils/validation.js.map +1 -1
  70. package/lib/module/dhKeyPair.js +3 -0
  71. package/lib/module/dhKeyPair.js.map +1 -1
  72. package/lib/module/dsa.js +3 -0
  73. package/lib/module/dsa.js.map +1 -1
  74. package/lib/module/ec.js +38 -31
  75. package/lib/module/ec.js.map +1 -1
  76. package/lib/module/ed.js +61 -7
  77. package/lib/module/ed.js.map +1 -1
  78. package/lib/module/hash.js +52 -5
  79. package/lib/module/hash.js.map +1 -1
  80. package/lib/module/keys/classes.js +31 -5
  81. package/lib/module/keys/classes.js.map +1 -1
  82. package/lib/module/keys/generateKeyPair.js +86 -5
  83. package/lib/module/keys/generateKeyPair.js.map +1 -1
  84. package/lib/module/keys/index.js +50 -2
  85. package/lib/module/keys/index.js.map +1 -1
  86. package/lib/module/keys/signVerify.js +9 -2
  87. package/lib/module/keys/signVerify.js.map +1 -1
  88. package/lib/module/keys/utils.js +57 -1
  89. package/lib/module/keys/utils.js.map +1 -1
  90. package/lib/module/random.js +63 -10
  91. package/lib/module/random.js.map +1 -1
  92. package/lib/module/rsa.js +3 -0
  93. package/lib/module/rsa.js.map +1 -1
  94. package/lib/module/slhdsa.js +64 -0
  95. package/lib/module/slhdsa.js.map +1 -0
  96. package/lib/module/specs/slhDsaKeyPair.nitro.js +4 -0
  97. package/lib/module/specs/slhDsaKeyPair.nitro.js.map +1 -0
  98. package/lib/module/specs/turboshake.nitro.js +4 -0
  99. package/lib/module/specs/turboshake.nitro.js.map +1 -0
  100. package/lib/module/subtle.js +927 -276
  101. package/lib/module/subtle.js.map +1 -1
  102. package/lib/module/utils/conversion.js +51 -19
  103. package/lib/module/utils/conversion.js.map +1 -1
  104. package/lib/module/utils/errors.js +61 -4
  105. package/lib/module/utils/errors.js.map +1 -1
  106. package/lib/module/utils/types.js.map +1 -1
  107. package/lib/module/utils/validation.js +44 -0
  108. package/lib/module/utils/validation.js.map +1 -1
  109. package/lib/typescript/dhKeyPair.d.ts.map +1 -1
  110. package/lib/typescript/dsa.d.ts.map +1 -1
  111. package/lib/typescript/ec.d.ts.map +1 -1
  112. package/lib/typescript/ed.d.ts.map +1 -1
  113. package/lib/typescript/hash.d.ts.map +1 -1
  114. package/lib/typescript/index.d.ts +12 -7
  115. package/lib/typescript/index.d.ts.map +1 -1
  116. package/lib/typescript/keys/classes.d.ts +10 -1
  117. package/lib/typescript/keys/classes.d.ts.map +1 -1
  118. package/lib/typescript/keys/generateKeyPair.d.ts +12 -1
  119. package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -1
  120. package/lib/typescript/keys/index.d.ts +3 -1
  121. package/lib/typescript/keys/index.d.ts.map +1 -1
  122. package/lib/typescript/keys/signVerify.d.ts.map +1 -1
  123. package/lib/typescript/keys/utils.d.ts +21 -4
  124. package/lib/typescript/keys/utils.d.ts.map +1 -1
  125. package/lib/typescript/random.d.ts +5 -1
  126. package/lib/typescript/random.d.ts.map +1 -1
  127. package/lib/typescript/rsa.d.ts.map +1 -1
  128. package/lib/typescript/slhdsa.d.ts +19 -0
  129. package/lib/typescript/slhdsa.d.ts.map +1 -0
  130. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +9 -0
  131. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -1
  132. package/lib/typescript/specs/slhDsaKeyPair.nitro.d.ts +16 -0
  133. package/lib/typescript/specs/slhDsaKeyPair.nitro.d.ts.map +1 -0
  134. package/lib/typescript/specs/turboshake.nitro.d.ts +11 -0
  135. package/lib/typescript/specs/turboshake.nitro.d.ts.map +1 -0
  136. package/lib/typescript/subtle.d.ts +3 -2
  137. package/lib/typescript/subtle.d.ts.map +1 -1
  138. package/lib/typescript/utils/conversion.d.ts +4 -3
  139. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  140. package/lib/typescript/utils/errors.d.ts +12 -0
  141. package/lib/typescript/utils/errors.d.ts.map +1 -1
  142. package/lib/typescript/utils/types.d.ts +32 -15
  143. package/lib/typescript/utils/types.d.ts.map +1 -1
  144. package/lib/typescript/utils/validation.d.ts +3 -1
  145. package/lib/typescript/utils/validation.d.ts.map +1 -1
  146. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +2 -0
  147. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +20 -0
  148. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +20 -0
  149. package/nitrogen/generated/shared/c++/AsymmetricKeyType.hpp +48 -0
  150. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +9 -0
  151. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +9 -0
  152. package/nitrogen/generated/shared/c++/HybridSlhDsaKeyPairSpec.cpp +29 -0
  153. package/nitrogen/generated/shared/c++/HybridSlhDsaKeyPairSpec.hpp +72 -0
  154. package/nitrogen/generated/shared/c++/HybridTurboShakeSpec.cpp +22 -0
  155. package/nitrogen/generated/shared/c++/HybridTurboShakeSpec.hpp +70 -0
  156. package/nitrogen/generated/shared/c++/JWK.hpp +9 -1
  157. package/nitrogen/generated/shared/c++/JWKkty.hpp +4 -0
  158. package/nitrogen/generated/shared/c++/KangarooTwelveVariant.hpp +76 -0
  159. package/nitrogen/generated/shared/c++/TurboShakeVariant.hpp +76 -0
  160. package/package.json +2 -3
  161. package/src/dhKeyPair.ts +8 -0
  162. package/src/dsa.ts +8 -0
  163. package/src/ec.ts +52 -29
  164. package/src/ed.ts +95 -16
  165. package/src/hash.ts +108 -5
  166. package/src/keys/classes.ts +46 -5
  167. package/src/keys/generateKeyPair.ts +151 -5
  168. package/src/keys/index.ts +73 -3
  169. package/src/keys/signVerify.ts +13 -2
  170. package/src/keys/utils.ts +78 -5
  171. package/src/random.ts +93 -9
  172. package/src/rsa.ts +8 -0
  173. package/src/slhdsa.ts +146 -0
  174. package/src/specs/keyObjectHandle.nitro.ts +17 -0
  175. package/src/specs/slhDsaKeyPair.nitro.ts +29 -0
  176. package/src/specs/turboshake.nitro.ts +21 -0
  177. package/src/subtle.ts +1191 -360
  178. package/src/utils/conversion.ts +72 -21
  179. package/src/utils/errors.ts +72 -4
  180. package/src/utils/types.ts +80 -15
  181. package/src/utils/validation.ts +70 -1
@@ -0,0 +1,70 @@
1
+ ///
2
+ /// HybridTurboShakeSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+ // Forward declaration of `TurboShakeVariant` to properly resolve imports.
17
+ namespace margelo::nitro::crypto { enum class TurboShakeVariant; }
18
+ // Forward declaration of `KangarooTwelveVariant` to properly resolve imports.
19
+ namespace margelo::nitro::crypto { enum class KangarooTwelveVariant; }
20
+
21
+ #include <NitroModules/ArrayBuffer.hpp>
22
+ #include <NitroModules/Promise.hpp>
23
+ #include "TurboShakeVariant.hpp"
24
+ #include "KangarooTwelveVariant.hpp"
25
+ #include <optional>
26
+
27
+ namespace margelo::nitro::crypto {
28
+
29
+ using namespace margelo::nitro;
30
+
31
+ /**
32
+ * An abstract base class for `TurboShake`
33
+ * Inherit this class to create instances of `HybridTurboShakeSpec` in C++.
34
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
35
+ * @example
36
+ * ```cpp
37
+ * class HybridTurboShake: public HybridTurboShakeSpec {
38
+ * public:
39
+ * HybridTurboShake(...): HybridObject(TAG) { ... }
40
+ * // ...
41
+ * };
42
+ * ```
43
+ */
44
+ class HybridTurboShakeSpec: public virtual HybridObject {
45
+ public:
46
+ // Constructor
47
+ explicit HybridTurboShakeSpec(): HybridObject(TAG) { }
48
+
49
+ // Destructor
50
+ ~HybridTurboShakeSpec() override = default;
51
+
52
+ public:
53
+ // Properties
54
+
55
+
56
+ public:
57
+ // Methods
58
+ virtual std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> turboShake(TurboShakeVariant variant, double domainSeparation, double outputLength, const std::shared_ptr<ArrayBuffer>& data) = 0;
59
+ virtual std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> kangarooTwelve(KangarooTwelveVariant variant, double outputLength, const std::shared_ptr<ArrayBuffer>& data, const std::optional<std::shared_ptr<ArrayBuffer>>& customization) = 0;
60
+
61
+ protected:
62
+ // Hybrid Setup
63
+ void loadHybridMethods() override;
64
+
65
+ protected:
66
+ // Tag for logging
67
+ static constexpr auto TAG = "TurboShake";
68
+ };
69
+
70
+ } // namespace margelo::nitro::crypto
@@ -70,11 +70,13 @@ namespace margelo::nitro::crypto {
70
70
  std::optional<std::string> dp SWIFT_PRIVATE;
71
71
  std::optional<std::string> dq SWIFT_PRIVATE;
72
72
  std::optional<std::string> qi SWIFT_PRIVATE;
73
+ std::optional<std::string> pub SWIFT_PRIVATE;
74
+ std::optional<std::string> priv SWIFT_PRIVATE;
73
75
  std::optional<bool> ext SWIFT_PRIVATE;
74
76
 
75
77
  public:
76
78
  JWK() = default;
77
- explicit JWK(std::optional<JWKkty> kty, std::optional<JWKuse> use, std::optional<std::vector<KeyUsage>> key_ops, std::optional<std::string> alg, std::optional<std::string> crv, std::optional<std::string> kid, std::optional<std::string> x5u, std::optional<std::vector<std::string>> x5c, std::optional<std::string> x5t, std::optional<std::string> x5t_256, std::optional<std::string> n, std::optional<std::string> e, std::optional<std::string> d, std::optional<std::string> p, std::optional<std::string> q, std::optional<std::string> x, std::optional<std::string> y, std::optional<std::string> k, std::optional<std::string> dp, std::optional<std::string> dq, std::optional<std::string> qi, std::optional<bool> ext): kty(kty), use(use), key_ops(key_ops), alg(alg), crv(crv), kid(kid), x5u(x5u), x5c(x5c), x5t(x5t), x5t_256(x5t_256), n(n), e(e), d(d), p(p), q(q), x(x), y(y), k(k), dp(dp), dq(dq), qi(qi), ext(ext) {}
79
+ explicit JWK(std::optional<JWKkty> kty, std::optional<JWKuse> use, std::optional<std::vector<KeyUsage>> key_ops, std::optional<std::string> alg, std::optional<std::string> crv, std::optional<std::string> kid, std::optional<std::string> x5u, std::optional<std::vector<std::string>> x5c, std::optional<std::string> x5t, std::optional<std::string> x5t_256, std::optional<std::string> n, std::optional<std::string> e, std::optional<std::string> d, std::optional<std::string> p, std::optional<std::string> q, std::optional<std::string> x, std::optional<std::string> y, std::optional<std::string> k, std::optional<std::string> dp, std::optional<std::string> dq, std::optional<std::string> qi, std::optional<std::string> pub, std::optional<std::string> priv, std::optional<bool> ext): kty(kty), use(use), key_ops(key_ops), alg(alg), crv(crv), kid(kid), x5u(x5u), x5c(x5c), x5t(x5t), x5t_256(x5t_256), n(n), e(e), d(d), p(p), q(q), x(x), y(y), k(k), dp(dp), dq(dq), qi(qi), pub(pub), priv(priv), ext(ext) {}
78
80
 
79
81
  public:
80
82
  friend bool operator==(const JWK& lhs, const JWK& rhs) = default;
@@ -111,6 +113,8 @@ namespace margelo::nitro {
111
113
  JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "dp"))),
112
114
  JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "dq"))),
113
115
  JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "qi"))),
116
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "pub"))),
117
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "priv"))),
114
118
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ext")))
115
119
  );
116
120
  }
@@ -137,6 +141,8 @@ namespace margelo::nitro {
137
141
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "dp"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.dp));
138
142
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "dq"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.dq));
139
143
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "qi"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.qi));
144
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "pub"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.pub));
145
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "priv"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.priv));
140
146
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "ext"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.ext));
141
147
  return obj;
142
148
  }
@@ -169,6 +175,8 @@ namespace margelo::nitro {
169
175
  if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "dp")))) return false;
170
176
  if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "dq")))) return false;
171
177
  if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "qi")))) return false;
178
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "pub")))) return false;
179
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "priv")))) return false;
172
180
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "ext")))) return false;
173
181
  return true;
174
182
  }
@@ -34,6 +34,7 @@ namespace margelo::nitro::crypto {
34
34
  EC SWIFT_NAME(ec) = 2,
35
35
  OCT SWIFT_NAME(oct) = 3,
36
36
  OKP SWIFT_NAME(okp) = 4,
37
+ AKP SWIFT_NAME(akp) = 5,
37
38
  } CLOSED_ENUM;
38
39
 
39
40
  } // namespace margelo::nitro::crypto
@@ -51,6 +52,7 @@ namespace margelo::nitro {
51
52
  case hashString("EC"): return margelo::nitro::crypto::JWKkty::EC;
52
53
  case hashString("oct"): return margelo::nitro::crypto::JWKkty::OCT;
53
54
  case hashString("OKP"): return margelo::nitro::crypto::JWKkty::OKP;
55
+ case hashString("AKP"): return margelo::nitro::crypto::JWKkty::AKP;
54
56
  default: [[unlikely]]
55
57
  throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum JWKkty - invalid value!");
56
58
  }
@@ -62,6 +64,7 @@ namespace margelo::nitro {
62
64
  case margelo::nitro::crypto::JWKkty::EC: return JSIConverter<std::string>::toJSI(runtime, "EC");
63
65
  case margelo::nitro::crypto::JWKkty::OCT: return JSIConverter<std::string>::toJSI(runtime, "oct");
64
66
  case margelo::nitro::crypto::JWKkty::OKP: return JSIConverter<std::string>::toJSI(runtime, "OKP");
67
+ case margelo::nitro::crypto::JWKkty::AKP: return JSIConverter<std::string>::toJSI(runtime, "AKP");
65
68
  default: [[unlikely]]
66
69
  throw std::invalid_argument("Cannot convert JWKkty to JS - invalid value: "
67
70
  + std::to_string(static_cast<int>(arg)) + "!");
@@ -78,6 +81,7 @@ namespace margelo::nitro {
78
81
  case hashString("EC"):
79
82
  case hashString("oct"):
80
83
  case hashString("OKP"):
84
+ case hashString("AKP"):
81
85
  return true;
82
86
  default:
83
87
  return false;
@@ -0,0 +1,76 @@
1
+ ///
2
+ /// KangarooTwelveVariant.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::crypto {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (KangarooTwelveVariant).
30
+ */
31
+ enum class KangarooTwelveVariant {
32
+ KT128 SWIFT_NAME(kt128) = 0,
33
+ KT256 SWIFT_NAME(kt256) = 1,
34
+ } CLOSED_ENUM;
35
+
36
+ } // namespace margelo::nitro::crypto
37
+
38
+ namespace margelo::nitro {
39
+
40
+ // C++ KangarooTwelveVariant <> JS KangarooTwelveVariant (union)
41
+ template <>
42
+ struct JSIConverter<margelo::nitro::crypto::KangarooTwelveVariant> final {
43
+ static inline margelo::nitro::crypto::KangarooTwelveVariant fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
44
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
45
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
46
+ case hashString("KT128"): return margelo::nitro::crypto::KangarooTwelveVariant::KT128;
47
+ case hashString("KT256"): return margelo::nitro::crypto::KangarooTwelveVariant::KT256;
48
+ default: [[unlikely]]
49
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum KangarooTwelveVariant - invalid value!");
50
+ }
51
+ }
52
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::crypto::KangarooTwelveVariant arg) {
53
+ switch (arg) {
54
+ case margelo::nitro::crypto::KangarooTwelveVariant::KT128: return JSIConverter<std::string>::toJSI(runtime, "KT128");
55
+ case margelo::nitro::crypto::KangarooTwelveVariant::KT256: return JSIConverter<std::string>::toJSI(runtime, "KT256");
56
+ default: [[unlikely]]
57
+ throw std::invalid_argument("Cannot convert KangarooTwelveVariant to JS - invalid value: "
58
+ + std::to_string(static_cast<int>(arg)) + "!");
59
+ }
60
+ }
61
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
62
+ if (!value.isString()) {
63
+ return false;
64
+ }
65
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
66
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
67
+ case hashString("KT128"):
68
+ case hashString("KT256"):
69
+ return true;
70
+ default:
71
+ return false;
72
+ }
73
+ }
74
+ };
75
+
76
+ } // namespace margelo::nitro
@@ -0,0 +1,76 @@
1
+ ///
2
+ /// TurboShakeVariant.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::crypto {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (TurboShakeVariant).
30
+ */
31
+ enum class TurboShakeVariant {
32
+ TURBOSHAKE128 SWIFT_NAME(turboshake128) = 0,
33
+ TURBOSHAKE256 SWIFT_NAME(turboshake256) = 1,
34
+ } CLOSED_ENUM;
35
+
36
+ } // namespace margelo::nitro::crypto
37
+
38
+ namespace margelo::nitro {
39
+
40
+ // C++ TurboShakeVariant <> JS TurboShakeVariant (union)
41
+ template <>
42
+ struct JSIConverter<margelo::nitro::crypto::TurboShakeVariant> final {
43
+ static inline margelo::nitro::crypto::TurboShakeVariant fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
44
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
45
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
46
+ case hashString("TurboSHAKE128"): return margelo::nitro::crypto::TurboShakeVariant::TURBOSHAKE128;
47
+ case hashString("TurboSHAKE256"): return margelo::nitro::crypto::TurboShakeVariant::TURBOSHAKE256;
48
+ default: [[unlikely]]
49
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum TurboShakeVariant - invalid value!");
50
+ }
51
+ }
52
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::crypto::TurboShakeVariant arg) {
53
+ switch (arg) {
54
+ case margelo::nitro::crypto::TurboShakeVariant::TURBOSHAKE128: return JSIConverter<std::string>::toJSI(runtime, "TurboSHAKE128");
55
+ case margelo::nitro::crypto::TurboShakeVariant::TURBOSHAKE256: return JSIConverter<std::string>::toJSI(runtime, "TurboSHAKE256");
56
+ default: [[unlikely]]
57
+ throw std::invalid_argument("Cannot convert TurboShakeVariant to JS - invalid value: "
58
+ + std::to_string(static_cast<int>(arg)) + "!");
59
+ }
60
+ }
61
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
62
+ if (!value.isString()) {
63
+ return false;
64
+ }
65
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
66
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
67
+ case hashString("TurboSHAKE128"):
68
+ case hashString("TurboSHAKE256"):
69
+ return true;
70
+ default:
71
+ return false;
72
+ }
73
+ }
74
+ };
75
+
76
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-quick-crypto",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -164,8 +164,7 @@
164
164
  {
165
165
  "file": "../../example/package.json",
166
166
  "path": [
167
- "version",
168
- "dependencies.react-native-quick-crypto"
167
+ "version"
169
168
  ]
170
169
  }
171
170
  ]
package/src/dhKeyPair.ts CHANGED
@@ -97,6 +97,14 @@ function dh_formatKeyPairOutput(
97
97
  let publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
98
98
  let privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
99
99
 
100
+ if (
101
+ publicFormat === 'raw-public' ||
102
+ privateFormat === 'raw-private' ||
103
+ privateFormat === 'raw-seed'
104
+ ) {
105
+ throw new Error('Raw key formats are not supported for DH keys');
106
+ }
107
+
100
108
  if (publicFormat === -1) {
101
109
  publicKey = pub;
102
110
  } else {
package/src/dsa.ts CHANGED
@@ -79,6 +79,14 @@ function dsa_formatKeyPairOutput(
79
79
  let publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
80
80
  let privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
81
81
 
82
+ if (
83
+ publicFormat === 'raw-public' ||
84
+ privateFormat === 'raw-private' ||
85
+ privateFormat === 'raw-seed'
86
+ ) {
87
+ throw new Error('Raw key formats are not supported for DSA keys');
88
+ }
89
+
82
90
  if (publicFormat === -1) {
83
91
  publicKey = pub;
84
92
  } else {
package/src/ec.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  kNamedCurveAliases,
28
28
  lazyDOMException,
29
29
  normalizeHashName,
30
+ validateJwkStructure,
30
31
  HashContext,
31
32
  KeyEncoding,
32
33
  KFormatType,
@@ -93,36 +94,27 @@ export function ecImportKey(
93
94
  throw lazyDOMException('Unrecognized namedCurve', 'NotSupportedError');
94
95
  }
95
96
 
96
- // Handle JWK format
97
97
  if (format === 'jwk') {
98
98
  const jwk = keyData as JWK;
99
99
 
100
- // Validate JWK
100
+ if (!jwk || typeof jwk !== 'object') {
101
+ throw lazyDOMException('Invalid keyData', 'DataError');
102
+ }
101
103
  if (jwk.kty !== 'EC') {
102
104
  throw lazyDOMException('Invalid JWK "kty" Parameter', 'DataError');
103
105
  }
104
-
105
106
  if (jwk.crv !== namedCurve) {
106
107
  throw lazyDOMException(
107
108
  'JWK "crv" does not match the requested algorithm',
108
109
  'DataError',
109
110
  );
110
111
  }
112
+ const expectedUse = name === 'ECDH' ? 'enc' : 'sig';
113
+ validateJwkStructure(jwk, extractable, keyUsages, expectedUse);
111
114
 
112
- // Check use parameter if present
113
- if (jwk.use !== undefined) {
114
- const expectedUse = name === 'ECDH' ? 'enc' : 'sig';
115
- if (jwk.use !== expectedUse) {
116
- throw lazyDOMException('Invalid JWK "use" Parameter', 'DataError');
117
- }
118
- }
119
-
120
- // Check alg parameter if present
121
115
  if (jwk.alg !== undefined) {
122
116
  let expectedAlg: string | undefined;
123
-
124
117
  if (name === 'ECDSA') {
125
- // Map namedCurve to expected ECDSA algorithm
126
118
  expectedAlg =
127
119
  namedCurve === 'P-256'
128
120
  ? 'ES256'
@@ -132,11 +124,9 @@ export function ecImportKey(
132
124
  ? 'ES512'
133
125
  : undefined;
134
126
  } else if (name === 'ECDH') {
135
- // ECDH uses ECDH-ES algorithm
136
127
  expectedAlg = 'ECDH-ES';
137
128
  }
138
-
139
- if (expectedAlg && jwk.alg !== undefined && jwk.alg !== expectedAlg) {
129
+ if (expectedAlg && jwk.alg !== expectedAlg) {
140
130
  throw lazyDOMException(
141
131
  'JWK "alg" does not match the requested algorithm',
142
132
  'DataError',
@@ -144,32 +134,49 @@ export function ecImportKey(
144
134
  }
145
135
  }
146
136
 
147
- // Import using C++ layer
137
+ const isPublicJwk = jwk.d === undefined;
138
+ const validUsagesJwk: KeyUsage[] =
139
+ name === 'ECDSA'
140
+ ? isPublicJwk
141
+ ? ['verify']
142
+ : ['sign']
143
+ : isPublicJwk
144
+ ? []
145
+ : ['deriveKey', 'deriveBits'];
146
+ if (hasAnyNotIn(keyUsages, validUsagesJwk)) {
147
+ throw lazyDOMException(
148
+ `Unsupported key usage for a ${name} key`,
149
+ 'SyntaxError',
150
+ );
151
+ }
152
+
148
153
  const handle =
149
154
  NitroModules.createHybridObject<KeyObjectHandle>('KeyObjectHandle');
150
- const keyType = handle.initJwk(jwk, namedCurve as NamedCurve);
151
-
155
+ let keyType: number | undefined;
156
+ try {
157
+ keyType = handle.initJwk(jwk, namedCurve as NamedCurve);
158
+ } catch (err) {
159
+ throw lazyDOMException('Invalid keyData', {
160
+ name: 'DataError',
161
+ cause: err,
162
+ });
163
+ }
152
164
  if (keyType === undefined) {
153
- throw lazyDOMException('Invalid JWK', 'DataError');
165
+ throw lazyDOMException('Invalid keyData', 'DataError');
154
166
  }
155
167
 
156
- // Create the appropriate KeyObject based on type
157
168
  let keyObject: KeyObject;
158
169
  if (keyType === 1) {
159
170
  keyObject = new PublicKeyObject(handle);
160
171
  } else if (keyType === 2) {
161
172
  keyObject = new PrivateKeyObject(handle);
162
173
  } else {
163
- throw lazyDOMException(
164
- 'Unexpected key type from JWK import',
165
- 'DataError',
166
- );
174
+ throw lazyDOMException('Invalid keyData', 'DataError');
167
175
  }
168
176
 
169
177
  return new CryptoKey(keyObject, algorithm, keyUsages, extractable);
170
178
  }
171
179
 
172
- // Handle binary formats (spki, pkcs8, raw)
173
180
  if (format !== 'spki' && format !== 'pkcs8' && format !== 'raw') {
174
181
  throw lazyDOMException(
175
182
  `Unsupported format: ${format}`,
@@ -177,11 +184,9 @@ export function ecImportKey(
177
184
  );
178
185
  }
179
186
 
180
- // Determine expected key type based on format
181
187
  const expectedKeyType =
182
188
  format === 'spki' || format === 'raw' ? 'public' : 'private';
183
189
 
184
- // Validate usages for the key type
185
190
  const isPublicKey = expectedKeyType === 'public';
186
191
  let validUsages: KeyUsage[];
187
192
 
@@ -225,6 +230,18 @@ export function ecImportKey(
225
230
  KFormatType.DER,
226
231
  format === 'spki' ? KeyEncoding.SPKI : KeyEncoding.PKCS8,
227
232
  );
233
+
234
+ // Validate the imported curve matches the requested algorithm.namedCurve.
235
+ const expectedAlias =
236
+ kNamedCurveAliases[namedCurve as keyof typeof kNamedCurveAliases];
237
+ if (keyObject.handle.keyDetail().namedCurve !== expectedAlias) {
238
+ throw lazyDOMException('Named curve mismatch', 'DataError');
239
+ }
240
+ }
241
+
242
+ // Verify the public/private point lies on the named curve.
243
+ if (!keyObject.handle.checkEcKeyData()) {
244
+ throw lazyDOMException('Invalid keyData', 'DataError');
228
245
  }
229
246
 
230
247
  return new CryptoKey(keyObject, algorithm, keyUsages, extractable);
@@ -433,6 +450,10 @@ function ec_formatKeyPairOutput(
433
450
 
434
451
  if (publicFormat === -1) {
435
452
  publicKey = pub;
453
+ } else if (publicFormat === 'raw-public') {
454
+ publicKey = Buffer.from(
455
+ pub.handle.exportECPublicRaw(publicType === 'compressed'),
456
+ );
436
457
  } else {
437
458
  const format =
438
459
  publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
@@ -448,6 +469,8 @@ function ec_formatKeyPairOutput(
448
469
 
449
470
  if (privateFormat === -1) {
450
471
  privateKey = priv;
472
+ } else if (privateFormat === 'raw-private') {
473
+ privateKey = Buffer.from(priv.handle.exportECPrivateRaw());
451
474
  } else {
452
475
  const format =
453
476
  privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;