react-native-quick-crypto 0.7.2 → 1.0.0-beta.1

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 (304) hide show
  1. package/QuickCrypto.podspec +34 -0
  2. package/android/CMakeLists.txt +32 -64
  3. package/android/build.gradle +52 -125
  4. package/android/gradle.properties +3 -3
  5. package/android/src/main/cpp/cpp-adapter.cpp +9 -54
  6. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
  7. package/ios/QuickCryptoOnLoad.mm +19 -0
  8. package/lib/commonjs/index.js +39 -39
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/package.json +1 -0
  11. package/lib/commonjs/random.js +26 -12
  12. package/lib/commonjs/random.js.map +1 -1
  13. package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
  14. package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
  15. package/lib/commonjs/utils/conversion.js +22 -0
  16. package/lib/commonjs/utils/conversion.js.map +1 -0
  17. package/lib/commonjs/utils/index.js +12 -0
  18. package/lib/commonjs/utils/index.js.map +1 -0
  19. package/lib/commonjs/utils/types.js +2 -0
  20. package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
  21. package/lib/module/index.js +40 -38
  22. package/lib/module/index.js.map +1 -1
  23. package/lib/module/package.json +1 -0
  24. package/lib/module/random.js +28 -12
  25. package/lib/module/random.js.map +1 -1
  26. package/lib/module/specs/random.nitro.js +4 -0
  27. package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
  28. package/lib/module/utils/conversion.js +16 -0
  29. package/lib/module/utils/conversion.js.map +1 -0
  30. package/lib/module/utils/index.js +8 -0
  31. package/lib/module/utils/index.js.map +1 -0
  32. package/lib/module/utils/types.js +2 -0
  33. package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
  34. package/lib/tsconfig.tsbuildinfo +1 -0
  35. package/lib/typescript/index.d.ts +10 -141
  36. package/lib/typescript/index.d.ts.map +1 -1
  37. package/lib/typescript/random.d.ts +4 -5
  38. package/lib/typescript/random.d.ts.map +1 -1
  39. package/lib/typescript/specs/random.nitro.d.ts +9 -0
  40. package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
  41. package/lib/typescript/utils/conversion.d.ts +4 -0
  42. package/lib/typescript/utils/conversion.d.ts.map +1 -0
  43. package/lib/typescript/utils/index.d.ts +6 -0
  44. package/lib/typescript/utils/index.d.ts.map +1 -0
  45. package/lib/typescript/utils/types.d.ts +5 -0
  46. package/lib/typescript/utils/types.d.ts.map +1 -0
  47. package/package.json +58 -108
  48. package/src/index.ts +38 -48
  49. package/src/random.ts +51 -48
  50. package/src/specs/random.nitro.ts +14 -0
  51. package/src/utils/conversion.ts +16 -0
  52. package/src/utils/index.ts +6 -0
  53. package/src/utils/types.ts +15 -0
  54. package/LICENSE +0 -27
  55. package/README.md +0 -203
  56. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
  57. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
  58. package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
  59. package/cpp/Cipher/MGLCipherHostObject.h +0 -90
  60. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
  61. package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
  62. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
  63. package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
  64. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
  65. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
  66. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
  67. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
  68. package/cpp/Cipher/MGLPublicCipher.h +0 -120
  69. package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
  70. package/cpp/Cipher/MGLRsa.cpp +0 -573
  71. package/cpp/Cipher/MGLRsa.h +0 -109
  72. package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
  73. package/cpp/HMAC/MGLHmacHostObject.h +0 -39
  74. package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
  75. package/cpp/HMAC/MGLHmacInstaller.h +0 -20
  76. package/cpp/Hash/MGLHashHostObject.cpp +0 -147
  77. package/cpp/Hash/MGLHashHostObject.h +0 -53
  78. package/cpp/Hash/MGLHashInstaller.cpp +0 -44
  79. package/cpp/Hash/MGLHashInstaller.h +0 -20
  80. package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
  81. package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
  82. package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
  83. package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
  84. package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
  85. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
  86. package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
  87. package/cpp/JSIUtils/MGLTypedArray.h +0 -160
  88. package/cpp/MGLKeys.cpp +0 -1426
  89. package/cpp/MGLKeys.h +0 -203
  90. package/cpp/MGLQuickCryptoHostObject.cpp +0 -124
  91. package/cpp/MGLQuickCryptoHostObject.h +0 -30
  92. package/cpp/Random/MGLRandomHostObject.cpp +0 -96
  93. package/cpp/Random/MGLRandomHostObject.h +0 -27
  94. package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
  95. package/cpp/Sig/MGLSignHostObjects.h +0 -128
  96. package/cpp/Sig/MGLSignInstaller.cpp +0 -24
  97. package/cpp/Sig/MGLSignInstaller.h +0 -29
  98. package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
  99. package/cpp/Sig/MGLVerifyInstaller.h +0 -22
  100. package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
  101. package/cpp/Utils/MGLDispatchQueue.h +0 -55
  102. package/cpp/Utils/MGLUtils.cpp +0 -295
  103. package/cpp/Utils/MGLUtils.h +0 -358
  104. package/cpp/Utils/base64.h +0 -320
  105. package/cpp/Utils/logs.h +0 -38
  106. package/cpp/Utils/node.h +0 -13
  107. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
  108. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
  109. package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
  110. package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
  111. package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
  112. package/cpp/webcrypto/MGLWebCrypto.h +0 -32
  113. package/cpp/webcrypto/crypto_aes.cpp +0 -516
  114. package/cpp/webcrypto/crypto_aes.h +0 -79
  115. package/cpp/webcrypto/crypto_ec.cpp +0 -424
  116. package/cpp/webcrypto/crypto_ec.h +0 -78
  117. package/cpp/webcrypto/crypto_keygen.cpp +0 -86
  118. package/cpp/webcrypto/crypto_keygen.h +0 -38
  119. package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
  120. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  121. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  122. package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  123. package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  124. package/ios/QuickCryptoModule.h +0 -5
  125. package/ios/QuickCryptoModule.mm +0 -42
  126. package/lib/commonjs/Cipher.js +0 -502
  127. package/lib/commonjs/Cipher.js.map +0 -1
  128. package/lib/commonjs/Hash.js +0 -99
  129. package/lib/commonjs/Hash.js.map +0 -1
  130. package/lib/commonjs/Hashnames.js +0 -87
  131. package/lib/commonjs/Hashnames.js.map +0 -1
  132. package/lib/commonjs/Hmac.js +0 -79
  133. package/lib/commonjs/Hmac.js.map +0 -1
  134. package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
  135. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
  136. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
  137. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  138. package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
  139. package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
  140. package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
  141. package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
  142. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
  143. package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
  144. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
  145. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
  146. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
  147. package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
  148. package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
  149. package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
  150. package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
  151. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
  152. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
  153. package/lib/commonjs/Utils.js +0 -608
  154. package/lib/commonjs/Utils.js.map +0 -1
  155. package/lib/commonjs/aes.js +0 -281
  156. package/lib/commonjs/aes.js.map +0 -1
  157. package/lib/commonjs/constants.js +0 -85
  158. package/lib/commonjs/constants.js.map +0 -1
  159. package/lib/commonjs/ec.js +0 -276
  160. package/lib/commonjs/ec.js.map +0 -1
  161. package/lib/commonjs/keygen.js +0 -56
  162. package/lib/commonjs/keygen.js.map +0 -1
  163. package/lib/commonjs/keys.js +0 -492
  164. package/lib/commonjs/keys.js.map +0 -1
  165. package/lib/commonjs/pbkdf2.js +0 -90
  166. package/lib/commonjs/pbkdf2.js.map +0 -1
  167. package/lib/commonjs/rsa.js +0 -248
  168. package/lib/commonjs/rsa.js.map +0 -1
  169. package/lib/commonjs/sig.js +0 -129
  170. package/lib/commonjs/sig.js.map +0 -1
  171. package/lib/commonjs/subtle.js +0 -448
  172. package/lib/commonjs/subtle.js.map +0 -1
  173. package/lib/commonjs/webcrypto.js +0 -14
  174. package/lib/commonjs/webcrypto.js.map +0 -1
  175. package/lib/module/Cipher.js +0 -489
  176. package/lib/module/Cipher.js.map +0 -1
  177. package/lib/module/Hash.js +0 -91
  178. package/lib/module/Hash.js.map +0 -1
  179. package/lib/module/Hashnames.js +0 -83
  180. package/lib/module/Hashnames.js.map +0 -1
  181. package/lib/module/Hmac.js +0 -72
  182. package/lib/module/Hmac.js.map +0 -1
  183. package/lib/module/NativeQuickCrypto/Cipher.js +0 -24
  184. package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
  185. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -47
  186. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  187. package/lib/module/NativeQuickCrypto/aes.js +0 -2
  188. package/lib/module/NativeQuickCrypto/hash.js +0 -2
  189. package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
  190. package/lib/module/NativeQuickCrypto/hmac.js +0 -2
  191. package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
  192. package/lib/module/NativeQuickCrypto/keygen.js +0 -2
  193. package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
  194. package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
  195. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
  196. package/lib/module/NativeQuickCrypto/random.js +0 -2
  197. package/lib/module/NativeQuickCrypto/random.js.map +0 -1
  198. package/lib/module/NativeQuickCrypto/rsa.js +0 -2
  199. package/lib/module/NativeQuickCrypto/sig.js +0 -15
  200. package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
  201. package/lib/module/NativeQuickCrypto/webcrypto.js +0 -2
  202. package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
  203. package/lib/module/Utils.js +0 -537
  204. package/lib/module/Utils.js.map +0 -1
  205. package/lib/module/aes.js +0 -272
  206. package/lib/module/aes.js.map +0 -1
  207. package/lib/module/constants.js +0 -79
  208. package/lib/module/constants.js.map +0 -1
  209. package/lib/module/ec.js +0 -265
  210. package/lib/module/ec.js.map +0 -1
  211. package/lib/module/keygen.js +0 -47
  212. package/lib/module/keygen.js.map +0 -1
  213. package/lib/module/keys.js +0 -475
  214. package/lib/module/keys.js.map +0 -1
  215. package/lib/module/pbkdf2.js +0 -82
  216. package/lib/module/pbkdf2.js.map +0 -1
  217. package/lib/module/rsa.js +0 -240
  218. package/lib/module/rsa.js.map +0 -1
  219. package/lib/module/sig.js +0 -122
  220. package/lib/module/sig.js.map +0 -1
  221. package/lib/module/subtle.js +0 -441
  222. package/lib/module/subtle.js.map +0 -1
  223. package/lib/module/webcrypto.js +0 -8
  224. package/lib/module/webcrypto.js.map +0 -1
  225. package/lib/typescript/Cipher.d.ts +0 -81
  226. package/lib/typescript/Cipher.d.ts.map +0 -1
  227. package/lib/typescript/Hash.d.ts +0 -44
  228. package/lib/typescript/Hash.d.ts.map +0 -1
  229. package/lib/typescript/Hashnames.d.ts +0 -11
  230. package/lib/typescript/Hashnames.d.ts.map +0 -1
  231. package/lib/typescript/Hmac.d.ts +0 -37
  232. package/lib/typescript/Hmac.d.ts.map +0 -1
  233. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
  234. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  235. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
  236. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  237. package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
  238. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  239. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
  240. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  241. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
  242. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  243. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
  244. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  245. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
  246. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  247. package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
  248. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  249. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
  250. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  251. package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
  252. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  253. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
  254. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  255. package/lib/typescript/Utils.d.ts +0 -48
  256. package/lib/typescript/Utils.d.ts.map +0 -1
  257. package/lib/typescript/aes.d.ts +0 -22
  258. package/lib/typescript/aes.d.ts.map +0 -1
  259. package/lib/typescript/constants.d.ts +0 -76
  260. package/lib/typescript/constants.d.ts.map +0 -1
  261. package/lib/typescript/ec.d.ts +0 -7
  262. package/lib/typescript/ec.d.ts.map +0 -1
  263. package/lib/typescript/keygen.d.ts +0 -6
  264. package/lib/typescript/keygen.d.ts.map +0 -1
  265. package/lib/typescript/keys.d.ts +0 -206
  266. package/lib/typescript/keys.d.ts.map +0 -1
  267. package/lib/typescript/pbkdf2.d.ts +0 -12
  268. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  269. package/lib/typescript/rsa.d.ts +0 -12
  270. package/lib/typescript/rsa.d.ts.map +0 -1
  271. package/lib/typescript/sig.d.ts +0 -21
  272. package/lib/typescript/sig.d.ts.map +0 -1
  273. package/lib/typescript/subtle.d.ts +0 -15
  274. package/lib/typescript/subtle.d.ts.map +0 -1
  275. package/lib/typescript/webcrypto.d.ts +0 -9
  276. package/lib/typescript/webcrypto.d.ts.map +0 -1
  277. package/react-native-quick-crypto.podspec +0 -40
  278. package/src/Cipher.ts +0 -832
  279. package/src/Hash.ts +0 -132
  280. package/src/Hashnames.ts +0 -93
  281. package/src/Hmac.ts +0 -106
  282. package/src/NativeQuickCrypto/Cipher.ts +0 -102
  283. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
  284. package/src/NativeQuickCrypto/aes.ts +0 -14
  285. package/src/NativeQuickCrypto/hash.ts +0 -10
  286. package/src/NativeQuickCrypto/hmac.ts +0 -9
  287. package/src/NativeQuickCrypto/keygen.ts +0 -7
  288. package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
  289. package/src/NativeQuickCrypto/random.ts +0 -12
  290. package/src/NativeQuickCrypto/rsa.ts +0 -12
  291. package/src/NativeQuickCrypto/sig.ts +0 -44
  292. package/src/NativeQuickCrypto/webcrypto.ts +0 -72
  293. package/src/Utils.ts +0 -777
  294. package/src/aes.ts +0 -402
  295. package/src/constants.ts +0 -79
  296. package/src/ec.ts +0 -375
  297. package/src/keygen.ts +0 -80
  298. package/src/keys.ts +0 -787
  299. package/src/pbkdf2.ts +0 -169
  300. package/src/rsa.ts +0 -370
  301. package/src/sig.ts +0 -164
  302. package/src/subtle.ts +0 -639
  303. package/src/webcrypto.ts +0 -8
  304. /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
@@ -1,757 +0,0 @@
1
- #include "MGLSignHostObjects.h"
2
-
3
- #include <openssl/evp.h>
4
-
5
- #include <optional>
6
-
7
- #include "MGLKeys.h"
8
- #ifdef ANDROID
9
- #include "JSIUtils/MGLJSIUtils.h"
10
- #include "JSIUtils/MGLTypedArray.h"
11
- #include "Utils/MGLUtils.h"
12
- #else
13
- #include "MGLJSIUtils.h"
14
- #include "MGLTypedArray.h"
15
- #include "MGLUtils.h"
16
- #endif
17
-
18
- namespace margelo {
19
-
20
- bool ValidateDSAParameters(EVP_PKEY* key) {
21
- /* Validate DSA2 parameters from FIPS 186-4 */
22
- #if OPENSSL_VERSION_MAJOR >= 3
23
- if (EVP_default_properties_is_fips_enabled(nullptr) &&
24
- EVP_PKEY_DSA == EVP_PKEY_base_id(key)) {
25
- #else
26
- if (FIPS_mode() && EVP_PKEY_DSA == EVP_PKEY_base_id(key)) {
27
- #endif
28
- const DSA* dsa = EVP_PKEY_get0_DSA(key);
29
- const BIGNUM* p;
30
- DSA_get0_pqg(dsa, &p, nullptr, nullptr);
31
- size_t L = BN_num_bits(p);
32
- const BIGNUM* q;
33
- DSA_get0_pqg(dsa, nullptr, &q, nullptr);
34
- size_t N = BN_num_bits(q);
35
-
36
- return (L == 1024 && N == 160) || (L == 2048 && N == 224) ||
37
- (L == 2048 && N == 256) || (L == 3072 && N == 256);
38
- }
39
-
40
- return true;
41
- }
42
-
43
- bool ApplyRSAOptions(const ManagedEVPPKey& pkey, EVP_PKEY_CTX* pkctx,
44
- int padding, std::optional<int> salt_len) {
45
- if (EVP_PKEY_id(pkey.get()) == EVP_PKEY_RSA ||
46
- EVP_PKEY_id(pkey.get()) == EVP_PKEY_RSA2 ||
47
- EVP_PKEY_id(pkey.get()) == EVP_PKEY_RSA_PSS) {
48
- if (EVP_PKEY_CTX_set_rsa_padding(pkctx, padding) <= 0) return false;
49
- if (padding == RSA_PKCS1_PSS_PADDING && salt_len.has_value()) {
50
- if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, salt_len.value()) <= 0)
51
- return false;
52
- }
53
- }
54
-
55
- return true;
56
- }
57
-
58
- std::optional<MGLTypedArray<MGLTypedArrayKind::Uint8Array>> Node_SignFinal(
59
- jsi::Runtime& runtime,
60
- EVPMDPointer&& mdctx,
61
- const ManagedEVPPKey& pkey,
62
- int padding,
63
- std::optional<int> pss_salt_len
64
- ) {
65
- std::optional<MGLTypedArray<MGLTypedArrayKind::Uint8Array>> ret;
66
-
67
- unsigned char m[EVP_MAX_MD_SIZE];
68
- unsigned int m_len;
69
-
70
- if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len)) return {};
71
-
72
- int signed_sig_len = EVP_PKEY_size(pkey.get());
73
- CHECK_GE(signed_sig_len, 0);
74
- size_t sig_len = static_cast<size_t>(signed_sig_len);
75
-
76
- MGLTypedArray<MGLTypedArrayKind::Uint8Array> sig(runtime, sig_len);
77
-
78
- EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
79
- if (pkctx && EVP_PKEY_sign_init(pkctx.get()) &&
80
- ApplyRSAOptions(pkey, pkctx.get(), padding, pss_salt_len) &&
81
- EVP_PKEY_CTX_set_signature_md(pkctx.get(), EVP_MD_CTX_md(mdctx.get())) &&
82
- EVP_PKEY_sign(
83
- pkctx.get(),
84
- static_cast<unsigned char*>(sig.getBuffer(runtime).data(runtime)),
85
- &sig_len, m, m_len)) {
86
- CHECK_LE(sig_len, sig.size(runtime));
87
-
88
- // (osp) do these bits need to be trimmed? I think so
89
- // if (sig_len == 0)
90
- // sig = ArrayBuffer::NewBackingStore(env->isolate(), 0);
91
- // else
92
- // sig = BackingStore::Reallocate(env->isolate(), std::move(sig),
93
- // sig_len);
94
- ret.emplace(std::move(sig));
95
- return ret;
96
- }
97
-
98
- return {};
99
- }
100
-
101
- // int GetDefaultSignPadding(const ManagedEVPPKey& m_pkey) {
102
- // return EVP_PKEY_id(m_pkey.get()) == EVP_PKEY_RSA_PSS ?
103
- // RSA_PKCS1_PSS_PADDING : RSA_PKCS1_PADDING;
104
- // }
105
- //
106
- unsigned int GetBytesOfRS(const ManagedEVPPKey& pkey) {
107
- int bits, base_id = EVP_PKEY_base_id(pkey.get());
108
-
109
- if (base_id == EVP_PKEY_DSA) {
110
- const DSA* dsa_key = EVP_PKEY_get0_DSA(pkey.get());
111
- // Both r and s are computed mod q, so their width is limited by that of
112
- bits = BN_num_bits(DSA_get0_q(dsa_key));
113
- } else if (base_id == EVP_PKEY_EC) {
114
- const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(pkey.get());
115
- const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
116
- bits = EC_GROUP_order_bits(ec_group);
117
- } else {
118
- return kNoDsaSignature;
119
- }
120
-
121
- return (bits + 7) / 8;
122
- }
123
-
124
- bool ExtractP1363(const unsigned char* sig_data,
125
- unsigned char* out,
126
- size_t len,
127
- size_t n) {
128
- ECDSASigPointer asn1_sig(d2i_ECDSA_SIG(nullptr,
129
- &sig_data, len)); if (!asn1_sig)
130
- return false;
131
-
132
- const BIGNUM* pr = ECDSA_SIG_get0_r(asn1_sig.get());
133
- const BIGNUM* ps = ECDSA_SIG_get0_s(asn1_sig.get());
134
-
135
- return BN_bn2binpad(pr, out, n) > 0 && BN_bn2binpad(ps,
136
- out + n, n) > 0;
137
- }
138
-
139
- // // Returns the maximum size of each of the integers (r, s) of the DSA signature.
140
- // std::unique_ptr<BackingStore> ConvertSignatureToP1363(Environment* env,
141
- // const ManagedEVPPKey&
142
- // pkey,
143
- // std::unique_ptr<BackingStore>&&
144
- // signature) {
145
- // unsigned int n = GetBytesOfRS(pkey);
146
- // if (n == kNoDsaSignature)
147
- // return std::move(signature);
148
- //
149
- // std::unique_ptr<BackingStore> buf;
150
- // {
151
- // NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data());
152
- // buf = ArrayBuffer::NewBackingStore(env->isolate(), 2 * n);
153
- // }
154
- // if (!ExtractP1363(static_cast<unsigned char*>(signature->Data()),
155
- // static_cast<unsigned char*>(buf->Data()),
156
- // signature->ByteLength(), n))
157
- // return std::move(signature);
158
- //
159
- // return buf;
160
- // }
161
-
162
- // Returns the maximum size of each of the integers (r, s) of the DSA signature.
163
- ByteSource ConvertSignatureToP1363(const ManagedEVPPKey& pkey,
164
- const ByteSource& signature) {
165
- unsigned int n = GetBytesOfRS(pkey);
166
- if (n == kNoDsaSignature)
167
- return ByteSource();
168
-
169
- const unsigned char* sig_data =
170
- signature.data<unsigned char>();
171
-
172
- ByteSource::Builder out(n * 2);
173
- memset(out.data<void>(), 0, n * 2);
174
-
175
- if (!ExtractP1363(sig_data,
176
- out.data<unsigned char>(),
177
- signature.size(), n))
178
- return ByteSource();
179
-
180
- return std::move(out).release();
181
- }
182
-
183
- ByteSource ConvertSignatureToDER(const ManagedEVPPKey& pkey, ByteSource&& out) {
184
- unsigned int n = GetBytesOfRS(pkey);
185
- if (n == kNoDsaSignature) return std::move(out);
186
-
187
- const unsigned char* sig_data = out.data<unsigned char>();
188
-
189
- if (out.size() != 2 * n) return ByteSource();
190
-
191
- ECDSASigPointer asn1_sig(ECDSA_SIG_new());
192
- CHECK(asn1_sig);
193
- BIGNUM* r = BN_new();
194
- CHECK_NOT_NULL(r);
195
- BIGNUM* s = BN_new();
196
- CHECK_NOT_NULL(s);
197
- CHECK_EQ(r, BN_bin2bn(sig_data, n, r));
198
- CHECK_EQ(s, BN_bin2bn(sig_data + n, n, s));
199
- CHECK_EQ(1, ECDSA_SIG_set0(asn1_sig.get(), r, s));
200
-
201
- unsigned char* data = nullptr;
202
- int len = i2d_ECDSA_SIG(asn1_sig.get(), &data);
203
-
204
- if (len <= 0) return ByteSource();
205
-
206
- CHECK_NOT_NULL(data);
207
-
208
- return ByteSource::Allocated(reinterpret_cast<char*>(data), len);
209
- }
210
-
211
- // void CheckThrow(Environment* env, SignBase::Error error) {
212
- // HandleScope scope(env->isolate());
213
- //
214
- // switch (error) {
215
- // case SignBase::Error::kSignUnknownDigest:
216
- // return THROW_ERR_CRYPTO_INVALID_DIGEST(env);
217
- //
218
- // case SignBase::Error::kSignNotInitialised:
219
- // return THROW_ERR_CRYPTO_INVALID_STATE(env, "Not initialised");
220
- //
221
- // case SignBase::Error::kSignMalformedSignature:
222
- // return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Malformed signature");
223
- //
224
- // case SignBase::Error::kSignInit:
225
- // case SignBase::Error::kSignUpdate:
226
- // case SignBase::Error::kSignPrivateKey:
227
- // case SignBase::Error::kSignPublicKey:
228
- // {
229
- // unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
230
- // if (err)
231
- // return ThrowCryptoError(env, err);
232
- // switch (error) {
233
- // case SignBase::Error::kSignInit:
234
- // return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
235
- // "EVP_SignInit_ex
236
- // failed");
237
- // case SignBase::Error::kSignUpdate:
238
- // return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
239
- // "EVP_SignUpdate failed");
240
- // case SignBase::Error::kSignPrivateKey:
241
- // return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
242
- // "PEM_read_bio_PrivateKey
243
- // failed");
244
- // case SignBase::Error::kSignPublicKey:
245
- // return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
246
- // "PEM_read_bio_PUBKEY
247
- // failed");
248
- // default:
249
- // ABORT();
250
- // }
251
- // }
252
- //
253
- // case SignBase::Error::kSignOk:
254
- // return;
255
- // }
256
- // }
257
-
258
- bool IsOneShot(const ManagedEVPPKey& key) {
259
- switch (EVP_PKEY_id(key.get())) {
260
- case EVP_PKEY_ED25519:
261
- case EVP_PKEY_ED448:
262
- return true;
263
- default:
264
- return false;
265
- }
266
- }
267
-
268
- bool UseP1363Encoding(const ManagedEVPPKey& key,
269
- const DSASigEnc& dsa_encoding) {
270
- switch (EVP_PKEY_id(key.get())) {
271
- case EVP_PKEY_EC:
272
- case EVP_PKEY_DSA:
273
- return dsa_encoding == kSigEncP1363;
274
- default:
275
- return false;
276
- }
277
- }
278
-
279
- SignBase::SignResult SignBase::SignFinal(jsi::Runtime& runtime,
280
- const ManagedEVPPKey& pkey,
281
- int padding,
282
- std::optional<int>& salt_len,
283
- DSASigEnc dsa_sig_enc) {
284
- if (!mdctx_) return SignResult(kSignNotInitialised);
285
-
286
- EVPMDPointer mdctx = std::move(mdctx_);
287
-
288
- if (!ValidateDSAParameters(pkey.get())) return SignResult(kSignPrivateKey);
289
- ByteSource bs;
290
- std::optional<MGLTypedArray<MGLTypedArrayKind::Uint8Array>> buffer =
291
- Node_SignFinal(runtime, std::move(mdctx), pkey, padding, salt_len);
292
- Error error = buffer.has_value() ? kSignOk : kSignPrivateKey;
293
- if (error == kSignOk) {
294
- bs = ByteSource::FromBuffer(runtime, buffer.value().getBuffer(runtime));
295
- if (dsa_sig_enc == kSigEncP1363) {
296
- bs = ConvertSignatureToP1363(pkey, std::move(bs));
297
- }
298
- CHECK_NOT_NULL(bs.data<unsigned char>());
299
- }
300
- return SignResult(error, toJSI(runtime, std::move(bs)));
301
- }
302
-
303
- SignBase::Error SignBase::VerifyFinal(const ManagedEVPPKey& pkey,
304
- const ByteSource& sig, int padding,
305
- std::optional<int>& saltlen,
306
- bool* verify_result) {
307
- if (!mdctx_) return kSignNotInitialised;
308
-
309
- unsigned char m[EVP_MAX_MD_SIZE];
310
- unsigned int m_len;
311
- *verify_result = false;
312
- EVPMDPointer mdctx = std::move(mdctx_);
313
-
314
- if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len)) return kSignPublicKey;
315
-
316
- EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
317
- if (pkctx && EVP_PKEY_verify_init(pkctx.get()) > 0 &&
318
- ApplyRSAOptions(pkey, pkctx.get(), padding, saltlen) &&
319
- EVP_PKEY_CTX_set_signature_md(pkctx.get(), EVP_MD_CTX_md(mdctx.get())) >
320
- 0) {
321
- const unsigned char* s = sig.data<unsigned char>();
322
- const int r = EVP_PKEY_verify(pkctx.get(), s, sig.size(), m, m_len);
323
- *verify_result = r == 1;
324
- }
325
-
326
- return kSignOk;
327
- }
328
-
329
- SignBase::SignBase(std::shared_ptr<react::CallInvoker> jsCallInvoker,
330
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
331
- : MGLSmartHostObject(jsCallInvoker, workerQueue) {}
332
-
333
- MGLSignHostObject::MGLSignHostObject(
334
- std::shared_ptr<react::CallInvoker> jsCallInvoker,
335
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
336
- : SignBase(jsCallInvoker, workerQueue) {
337
- InstallMethods(kModeSign);
338
- }
339
-
340
- MGLVerifyHostObject::MGLVerifyHostObject(
341
- std::shared_ptr<react::CallInvoker> jsCallInvoker,
342
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
343
- : SignBase(jsCallInvoker, workerQueue) {
344
- InstallMethods(kModeVerify);
345
- }
346
-
347
- int GetDefaultSignPadding(const ManagedEVPPKey& m_pkey) {
348
- return EVP_PKEY_id(m_pkey.get()) == EVP_PKEY_RSA_PSS ? RSA_PKCS1_PSS_PADDING
349
- : RSA_PKCS1_PADDING;
350
- }
351
-
352
- void SignBase::InstallMethods(mode mode) {
353
- this->fields.push_back(buildPair(
354
- "init", JSIF([=]) {
355
- if (count != 1 || !arguments[0].isString()) {
356
- throw jsi::JSError(runtime, "init requires algorithm param");
357
- return {};
358
- }
359
-
360
- std::string sign_type = arguments[0].asString(runtime).utf8(runtime);
361
- CHECK_NULL(mdctx_);
362
-
363
- // Historically, "dss1" and "DSS1" were DSA aliases for SHA-1
364
- // exposed through the public API.
365
- if (sign_type.compare("dss1") == 0 || sign_type.compare("DSS1") == 0) {
366
- sign_type = "SHA1";
367
- }
368
-
369
- const EVP_MD* md = EVP_get_digestbyname(sign_type.c_str());
370
- if (md == nullptr) return jsi::Value((int)kSignUnknownDigest);
371
-
372
- mdctx_.reset(EVP_MD_CTX_new());
373
- if (!mdctx_ || !EVP_DigestInit_ex(mdctx_.get(), md, nullptr)) {
374
- mdctx_.reset();
375
- return jsi::Value((int)kSignInit);
376
- }
377
-
378
- return jsi::Value((int)kSignOk);
379
- }));
380
-
381
- this->fields.push_back(buildPair(
382
- "update", JSIF([=]) {
383
- if (count != 1) {
384
- throw jsi::JSError(runtime, "update requires 2 arguments");
385
- }
386
-
387
- if (!arguments[0].isObject() ||
388
- !arguments[0].asObject(runtime).isArrayBuffer(runtime)) {
389
- throw jsi::JSError(
390
- runtime, "First argument (data) needs to be an array buffer");
391
- }
392
-
393
- auto data = arguments[0].asObject(runtime).getArrayBuffer(runtime);
394
-
395
- if (!CheckSizeInt32(runtime, data)) {
396
- throw jsi::JSError(runtime, "data is too large");
397
- }
398
-
399
- if (mdctx_ == nullptr) return (int)kSignNotInitialised;
400
- if (!EVP_DigestUpdate(mdctx_.get(), data.data(runtime),
401
- data.size(runtime)))
402
- return (int)kSignUpdate;
403
- return (int)kSignOk;
404
- }));
405
-
406
- if (mode == kModeSign) {
407
- this->fields.push_back(buildPair(
408
- "sign", JSIF([=]) {
409
- unsigned int offset = 0;
410
- ManagedEVPPKey key = ManagedEVPPKey::GetPrivateKeyFromJs(
411
- runtime, arguments, &offset, true);
412
- if (!key) {
413
- return {};
414
- }
415
-
416
- int padding = GetDefaultSignPadding(key);
417
- if (!arguments[offset].isUndefined()) {
418
- // TODO(osp) need to add a check for int32
419
- CHECK(arguments[offset].isNumber());
420
- padding = static_cast<int>(arguments[offset].asNumber());
421
- }
422
-
423
- std::optional<int> salt_len;
424
- if (!arguments[offset + 1].isUndefined()) {
425
- // TODO(osp) add check for int32
426
- CHECK(arguments[offset + 1].isNumber());
427
- salt_len = static_cast<int>(arguments[offset + 1].asNumber());
428
- }
429
-
430
- // TODO(osp) add check for int32
431
- CHECK(arguments[offset + 2].isNumber());
432
- DSASigEnc dsa_sig_enc = static_cast<DSASigEnc>(
433
- static_cast<int>(arguments[offset + 2].asNumber()));
434
-
435
- SignResult ret =
436
- this->SignFinal(runtime, key, padding, salt_len, dsa_sig_enc);
437
-
438
- if (ret.error != kSignOk) {
439
- throw jsi::JSError(runtime, "Error signing");
440
- }
441
-
442
- return std::move(ret.signature.value());
443
- }));
444
- } else {
445
- this->fields.push_back(buildPair(
446
- "verify", JSIF([=]) {
447
- // Verify* verify;
448
- // ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder());
449
-
450
- unsigned int offset = 0;
451
- ManagedEVPPKey pkey = ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(
452
- runtime, arguments, &offset);
453
- if (!pkey) {
454
- return {};
455
- }
456
-
457
- jsi::ArrayBuffer hbuf =
458
- arguments[offset].asObject(runtime).getArrayBuffer(runtime);
459
- if (!CheckSizeInt32(runtime, hbuf)) {
460
- throw jsi::JSError(runtime, "buffer is too big");
461
- }
462
-
463
- int padding = GetDefaultSignPadding(pkey);
464
- if (!arguments[offset + 1].isUndefined()) {
465
- CHECK(arguments[offset + 1].isNumber());
466
- padding = static_cast<int>(arguments[offset + 1].asNumber());
467
- }
468
-
469
- std::optional<int> salt_len;
470
- if (!arguments[offset + 2].isUndefined()) {
471
- // TODO(osp) add check for int32
472
- CHECK(arguments[offset + 2].isNumber());
473
- salt_len = static_cast<int>(arguments[offset + 2].asNumber());
474
- }
475
-
476
- // TODO(osp) add check for int32
477
- CHECK(arguments[offset + 3].isNumber());
478
- DSASigEnc dsa_sig_enc = static_cast<DSASigEnc>(
479
- static_cast<int>(arguments[offset + 3].asNumber()));
480
-
481
- ByteSource signature = ArrayBufferToByteSource(runtime, hbuf);
482
- if (dsa_sig_enc == kSigEncP1363) {
483
- signature = ConvertSignatureToDER(
484
- pkey, ArrayBufferToByteSource(runtime, hbuf));
485
- if (signature.data() == nullptr) {
486
- throw jsi::JSError(runtime, "kSignMalformedSignature");
487
- }
488
- // return crypto::CheckThrow(env,
489
- // Error::kSignMalformedSignature);
490
- }
491
-
492
- bool verify_result;
493
- Error err = this->VerifyFinal(pkey, signature, padding, salt_len,
494
- &verify_result);
495
- if (err != kSignOk) {
496
- throw jsi::JSError(runtime, "Error on verify");
497
- }
498
-
499
- return verify_result;
500
- }));
501
- }
502
- }
503
-
504
- // SignConfiguration::SignConfiguration(SignConfiguration&& other) noexcept
505
- //: job_mode(other.job_mode),
506
- // mode(other.mode),
507
- // key(std::move(other.key)),
508
- // data(std::move(other.data)),
509
- // signature(std::move(other.signature)),
510
- // digest(other.digest),
511
- // flags(other.flags),
512
- // padding(other.padding),
513
- // salt_length(other.salt_length),
514
- // dsa_encoding(other.dsa_encoding) {}
515
- //
516
- // SignConfiguration& SignConfiguration::operator=(
517
- // SignConfiguration&& other)
518
- // noexcept {
519
- // if (&other == this) return
520
- // *this;
521
- // this->~SignConfiguration();
522
- // return *new (this)
523
- // SignConfiguration(std::move(other));
524
- // }
525
- //
526
- // void SignConfiguration::MemoryInfo(MemoryTracker* tracker) const {
527
- // tracker->TrackField("key", key);
528
- // if (job_mode == kCryptoJobAsync) {
529
- // tracker->TrackFieldWithSize("data", data.size());
530
- // tracker->TrackFieldWithSize("signature", signature.size());
531
- // }
532
- // }
533
-
534
- SignConfiguration SubtleSignVerify::GetParamsFromJS(jsi::Runtime &rt,
535
- const jsi::Value *args) {
536
- SignConfiguration params;
537
- unsigned int offset = 0;
538
-
539
- // mode (sign/verify)
540
- params.mode = static_cast<SignConfiguration::Mode>((int)args[offset].getNumber());
541
- offset++;
542
-
543
- // key
544
- ManagedEVPPKey key;
545
- unsigned int keyParamOffset = offset;
546
- if (params.mode == SignConfiguration::kVerify) {
547
- key = ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(rt, args, &keyParamOffset);
548
- } else {
549
- key = ManagedEVPPKey::GetPrivateKeyFromJs(rt, args, &keyParamOffset, true);
550
- }
551
- if (!key) {
552
- return params;
553
- }
554
- params.key = key;
555
- offset = 5;
556
-
557
- // data
558
- if (!args[offset].isObject() || !args[offset].asObject(rt).isArrayBuffer(rt)) {
559
- throw jsi::JSError(rt, "data is not an array buffer");
560
- return params;
561
- }
562
- ByteSource data = ByteSource::FromStringOrBuffer(rt, args[offset]);
563
- if (data.size() > INT_MAX) {
564
- throw jsi::JSError(rt, "data is too big (> int32)");
565
- return params;
566
- }
567
- params.data = std::move(data);
568
- offset++;
569
-
570
- // digest
571
- if (args[offset].isString()) {
572
- std::string digest = args[offset].asString(rt).utf8(rt);
573
- params.digest = EVP_get_digestbyname(digest.c_str());
574
- if (params.digest == nullptr) {
575
- throw jsi::JSError(rt, "invalid digest");
576
- return params;
577
- }
578
- }
579
- offset++;
580
-
581
- // salt length
582
- if (CheckIsInt32(args[offset])) {
583
- params.flags |= SignConfiguration::kHasSaltLength;
584
- params.salt_length = args[offset].asNumber();
585
- }
586
- offset++;
587
-
588
- // padding
589
- if (CheckIsInt32(args[offset])) {
590
- params.flags |= SignConfiguration::kHasPadding;
591
- params.padding = args[offset].asNumber();
592
-
593
- }
594
- offset++;
595
-
596
- // dsa encoding
597
- if (args[offset].isNumber()) {
598
- params.dsa_encoding =
599
- static_cast<DSASigEnc>(args[offset].asNumber());
600
- if (params.dsa_encoding != kSigEncDER &&
601
- params.dsa_encoding != kSigEncP1363) {
602
- throw jsi::JSError(rt, "invalid signature encoding");
603
- return params;
604
- }
605
- }
606
- offset++;
607
-
608
- // signature
609
- if (params.mode == SignConfiguration::kVerify) {
610
- ByteSource signature = ByteSource::FromStringOrBuffer(rt, args[offset]);
611
- if (signature.size() > INT_MAX) {
612
- throw jsi::JSError(rt, "signature is too big (> int32)");
613
- return params;
614
- }
615
- // If this is an EC key (assuming ECDSA) we need to convert the
616
- // the signature from WebCrypto format into DER format...
617
- ManagedEVPPKey m_pkey = params.key;
618
- // Mutex::ScopedLock lock(*m_pkey.mutex());
619
- if (UseP1363Encoding(m_pkey, params.dsa_encoding)) {
620
- params.signature =
621
- ConvertSignatureToDER(m_pkey, std::move(signature));
622
- } else {
623
- params.signature = std::move(signature);
624
- }
625
- }
626
-
627
- return params;
628
- }
629
-
630
- // Subtle Sign/Verify
631
-
632
- void SubtleSignVerify::DoSignVerify(jsi::Runtime &rt,
633
- const SignConfiguration &params,
634
- ByteSource &out) {
635
-
636
- EVPMDPointer context(EVP_MD_CTX_new());
637
- EVP_PKEY_CTX* ctx = nullptr;
638
-
639
- switch (params.mode) {
640
- case SignConfiguration::kSign:
641
- if (!EVP_DigestSignInit(
642
- context.get(),
643
- &ctx,
644
- params.digest,
645
- nullptr,
646
- params.key.get())) {
647
- throw jsi::JSError(rt, "EVP_DigestSignInit failed");
648
- }
649
- break;
650
- case SignConfiguration::kVerify:
651
- if (!EVP_DigestVerifyInit(
652
- context.get(),
653
- &ctx,
654
- params.digest,
655
- nullptr,
656
- params.key.get())) {
657
- throw jsi::JSError(rt, "EVP_DigestVerifyInit failed");
658
- }
659
- break;
660
- }
661
-
662
- int padding = params.flags & SignConfiguration::kHasPadding
663
- ? params.padding
664
- : GetDefaultSignPadding(params.key);
665
-
666
- std::optional<int> salt_length = params.flags & SignConfiguration::kHasSaltLength
667
- ? std::optional(params.salt_length) : std::nullopt;
668
-
669
- if (!ApplyRSAOptions(params.key,
670
- ctx,
671
- padding,
672
- salt_length)) {
673
- throw jsi::JSError(rt, "PEM_read_bio_PrivateKey failed");
674
- }
675
-
676
- switch (params.mode) {
677
- case SignConfiguration::kSign: {
678
- if (IsOneShot(params.key)) {
679
- size_t len;
680
- if (!EVP_DigestSign(
681
- context.get(),
682
- nullptr,
683
- &len,
684
- params.data.data<unsigned char>(),
685
- params.data.size())) {
686
- throw jsi::JSError(rt, "PEM_read_bio_PrivateKey failed");
687
- }
688
- ByteSource::Builder buf(len);
689
- if (!EVP_DigestSign(context.get(),
690
- buf.data<unsigned char>(),
691
- &len,
692
- params.data.data<unsigned char>(),
693
- params.data.size())) {
694
- throw jsi::JSError(rt, "PEM_read_bio_PrivateKey failed");
695
- }
696
- out = std::move(buf).release(len);
697
- } else {
698
- size_t len;
699
- if (!EVP_DigestSignUpdate(
700
- context.get(),
701
- params.data.data<unsigned char>(),
702
- params.data.size()) ||
703
- !EVP_DigestSignFinal(context.get(), nullptr, &len)) {
704
- throw jsi::JSError(rt, "PEM_read_bio_PrivateKey failed");
705
- }
706
- ByteSource::Builder buf(len);
707
- if (!EVP_DigestSignFinal(
708
- context.get(), buf.data<unsigned char>(),
709
- &len)) {
710
- throw jsi::JSError(rt, "PEM_read_bio_PrivateKey failed");
711
- }
712
-
713
- if (UseP1363Encoding(params.key, params.dsa_encoding)) {
714
- out = ConvertSignatureToP1363(params.key,
715
- std::move(buf).release());
716
- } else {
717
- out = std::move(buf).release(len);
718
- }
719
- }
720
- break;
721
- }
722
- case SignConfiguration::kVerify: {
723
- ByteSource::Builder buf(1);
724
- buf.data<char>()[0] = 0;
725
- if (EVP_DigestVerify(
726
- context.get(),
727
- params.signature.data<unsigned char>(),
728
- params.signature.size(),
729
- params.data.data<unsigned char>(),
730
- params.data.size()) == 1) {
731
- buf.data<char>()[0] = 1;
732
- }
733
- out = std::move(buf).release();
734
- }
735
- }
736
-
737
- // return out;
738
- }
739
-
740
- jsi::Value SubtleSignVerify::EncodeOutput(jsi::Runtime &rt,
741
- const SignConfiguration &params,
742
- ByteSource &output) {
743
- jsi::Value result;
744
- switch (params.mode) {
745
- case SignConfiguration::kSign:
746
- result = toJSI(rt, std::move(output));
747
- break;
748
- case SignConfiguration::kVerify:
749
- result = jsi::Value(output.data<char>()[0] == 1);
750
- break;
751
- default:
752
- throw jsi::JSError(rt, "unreachable code in SubtleSignVerify::EncodeOutput");
753
- }
754
- return result;
755
- }
756
-
757
- } // namespace margelo