react-native-quick-crypto 0.2.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 (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -0
  3. package/android/.DS_Store +0 -0
  4. package/android/CMakeLists.txt +117 -0
  5. package/android/build.gradle +367 -0
  6. package/android/gradle/.DS_Store +0 -0
  7. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  8. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  9. package/android/gradle.properties +6 -0
  10. package/android/gradlew +183 -0
  11. package/android/gradlew.bat +100 -0
  12. package/android/src/.DS_Store +0 -0
  13. package/android/src/main/.DS_Store +0 -0
  14. package/android/src/main/AndroidManifest.xml +4 -0
  15. package/android/src/main/cpp/cpp-adapter.cpp +60 -0
  16. package/android/src/main/java/.DS_Store +0 -0
  17. package/android/src/main/java/com/.DS_Store +0 -0
  18. package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoModule.java +70 -0
  19. package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoPackage.java +26 -0
  20. package/cpp/.DS_Store +0 -0
  21. package/cpp/Cipher/MGLCipherHostObject.cpp +632 -0
  22. package/cpp/Cipher/MGLCipherHostObject.h +90 -0
  23. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +74 -0
  24. package/cpp/Cipher/MGLCreateCipherInstaller.h +17 -0
  25. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +74 -0
  26. package/cpp/Cipher/MGLCreateDecipherInstaller.h +17 -0
  27. package/cpp/HMAC/MGLHmacHostObject.cpp +95 -0
  28. package/cpp/HMAC/MGLHmacHostObject.h +39 -0
  29. package/cpp/HMAC/MGLHmacInstaller.cpp +43 -0
  30. package/cpp/HMAC/MGLHmacInstaller.h +20 -0
  31. package/cpp/Hash/MGLHashHostObject.cpp +147 -0
  32. package/cpp/Hash/MGLHashHostObject.h +53 -0
  33. package/cpp/Hash/MGLHashInstaller.cpp +46 -0
  34. package/cpp/Hash/MGLHashInstaller.h +20 -0
  35. package/cpp/JSIUtils/MGLJSIMacros.h +37 -0
  36. package/cpp/JSIUtils/MGLSmartHostObject.cpp +43 -0
  37. package/cpp/JSIUtils/MGLSmartHostObject.h +46 -0
  38. package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +24 -0
  39. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +43 -0
  40. package/cpp/JSIUtils/MGLTypedArray.cpp +325 -0
  41. package/cpp/JSIUtils/MGLTypedArray.h +160 -0
  42. package/cpp/MGLQuickCryptoHostObject.cpp +64 -0
  43. package/cpp/MGLQuickCryptoHostObject.h +30 -0
  44. package/cpp/Random/MGLRandomHostObject.cpp +89 -0
  45. package/cpp/Random/MGLRandomHostObject.h +27 -0
  46. package/cpp/Utils/MGLDispatchQueue.cpp +75 -0
  47. package/cpp/Utils/MGLDispatchQueue.h +55 -0
  48. package/cpp/Utils/logs.h +38 -0
  49. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +164 -0
  50. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +29 -0
  51. package/cpp/fastpbkdf2/fastpbkdf2.c +352 -0
  52. package/cpp/fastpbkdf2/fastpbkdf2.h +68 -0
  53. package/ios/.DS_Store +0 -0
  54. package/ios/QuickCrypto.xcodeproj/project.pbxproj +274 -0
  55. package/ios/QuickCryptoModule.h +5 -0
  56. package/ios/QuickCryptoModule.mm +42 -0
  57. package/lib/.DS_Store +0 -0
  58. package/lib/commonjs/Cipher.js +293 -0
  59. package/lib/commonjs/Cipher.js.map +1 -0
  60. package/lib/commonjs/Hash.js +102 -0
  61. package/lib/commonjs/Hash.js.map +1 -0
  62. package/lib/commonjs/Hmac.js +104 -0
  63. package/lib/commonjs/Hmac.js.map +1 -0
  64. package/lib/commonjs/NativeQuickCrypto/Cipher.js +6 -0
  65. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -0
  66. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +61 -0
  67. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -0
  68. package/lib/commonjs/NativeQuickCrypto/hash.js +2 -0
  69. package/lib/commonjs/NativeQuickCrypto/hash.js.map +1 -0
  70. package/lib/commonjs/NativeQuickCrypto/hmac.js +2 -0
  71. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +1 -0
  72. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +2 -0
  73. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +1 -0
  74. package/lib/commonjs/NativeQuickCrypto/random.js +2 -0
  75. package/lib/commonjs/NativeQuickCrypto/random.js.map +1 -0
  76. package/lib/commonjs/QuickCrypto.js +35 -0
  77. package/lib/commonjs/QuickCrypto.js.map +1 -0
  78. package/lib/commonjs/Utils.js +152 -0
  79. package/lib/commonjs/Utils.js.map +1 -0
  80. package/lib/commonjs/index.js +19 -0
  81. package/lib/commonjs/index.js.map +1 -0
  82. package/lib/commonjs/pbkdf2.js +64 -0
  83. package/lib/commonjs/pbkdf2.js.map +1 -0
  84. package/lib/commonjs/random.js +213 -0
  85. package/lib/commonjs/random.js.map +1 -0
  86. package/lib/module/Cipher.js +287 -0
  87. package/lib/module/Cipher.js.map +1 -0
  88. package/lib/module/Hash.js +87 -0
  89. package/lib/module/Hash.js.map +1 -0
  90. package/lib/module/Hmac.js +90 -0
  91. package/lib/module/Hmac.js.map +1 -0
  92. package/lib/module/NativeQuickCrypto/Cipher.js +2 -0
  93. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -0
  94. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +53 -0
  95. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -0
  96. package/lib/module/NativeQuickCrypto/hash.js +2 -0
  97. package/lib/module/NativeQuickCrypto/hash.js.map +1 -0
  98. package/lib/module/NativeQuickCrypto/hmac.js +2 -0
  99. package/lib/module/NativeQuickCrypto/hmac.js.map +1 -0
  100. package/lib/module/NativeQuickCrypto/pbkdf2.js +2 -0
  101. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +1 -0
  102. package/lib/module/NativeQuickCrypto/random.js +2 -0
  103. package/lib/module/NativeQuickCrypto/random.js.map +1 -0
  104. package/lib/module/QuickCrypto.js +18 -0
  105. package/lib/module/QuickCrypto.js.map +1 -0
  106. package/lib/module/Utils.js +131 -0
  107. package/lib/module/Utils.js.map +1 -0
  108. package/lib/module/index.js +2 -0
  109. package/lib/module/index.js.map +1 -0
  110. package/lib/module/pbkdf2.js +51 -0
  111. package/lib/module/pbkdf2.js.map +1 -0
  112. package/lib/module/random.js +190 -0
  113. package/lib/module/random.js.map +1 -0
  114. package/lib/typescript/Cipher.d.ts +30 -0
  115. package/lib/typescript/Hash.d.ts +44 -0
  116. package/lib/typescript/Hmac.d.ts +37 -0
  117. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +22 -0
  118. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +19 -0
  119. package/lib/typescript/NativeQuickCrypto/hash.d.ts +6 -0
  120. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +5 -0
  121. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +4 -0
  122. package/lib/typescript/NativeQuickCrypto/random.d.ts +4 -0
  123. package/lib/typescript/QuickCrypto.d.ts +31 -0
  124. package/lib/typescript/Utils.d.ts +13 -0
  125. package/lib/typescript/index.d.ts +1 -0
  126. package/lib/typescript/pbkdf2.d.ts +9 -0
  127. package/lib/typescript/random.d.ts +20 -0
  128. package/package.json +168 -0
  129. package/react-native-quick-crypto.podspec +44 -0
  130. package/src/Cipher.ts +322 -0
  131. package/src/Hash.ts +98 -0
  132. package/src/Hmac.ts +107 -0
  133. package/src/NativeQuickCrypto/Cipher.ts +25 -0
  134. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +79 -0
  135. package/src/NativeQuickCrypto/hash.ts +10 -0
  136. package/src/NativeQuickCrypto/hmac.ts +9 -0
  137. package/src/NativeQuickCrypto/pbkdf2.ts +16 -0
  138. package/src/NativeQuickCrypto/random.ts +12 -0
  139. package/src/QuickCrypto.ts +23 -0
  140. package/src/Utils.ts +151 -0
  141. package/src/index.ts +1 -0
  142. package/src/pbkdf2.ts +96 -0
  143. package/src/random.ts +277 -0
@@ -0,0 +1,164 @@
1
+ //
2
+ // Created by Szymon on 25/02/2022.
3
+ //
4
+
5
+ #include "MGLPbkdf2HostObject.h"
6
+
7
+ #ifdef ANDROID
8
+ #include "JSIUtils/MGLTypedArray.h"
9
+ #else
10
+ #include "MGLTypedArray.h"
11
+ #endif
12
+ #include <openssl/dsa.h>
13
+ #include <openssl/ec.h>
14
+ #include <openssl/err.h>
15
+ #include <openssl/evp.h>
16
+ #include <openssl/kdf.h>
17
+ #include <openssl/rsa.h>
18
+ #include <openssl/ssl.h>
19
+
20
+ #include <memory>
21
+ #include <utility>
22
+
23
+ #include "fastpbkdf2.h"
24
+
25
+ namespace margelo {
26
+ namespace jsi = facebook::jsi;
27
+ namespace react = facebook::react;
28
+
29
+ MGLPbkdf2HostObject::MGLPbkdf2HostObject(
30
+ std::shared_ptr<react::CallInvoker> jsCallInvoker,
31
+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
32
+ : MGLSmartHostObject(jsCallInvoker, workerQueue) {
33
+ this->fields.push_back(buildPair(
34
+ "pbkdf2", JSIF([this]) {
35
+ if (count != 5) {
36
+ throw jsi::JSError(runtime,
37
+ "fastpbkdf2(..) expects exactly 5 arguments!");
38
+ }
39
+
40
+ auto password = arguments[0].asObject(runtime).getArrayBuffer(runtime);
41
+ auto passwordSize = password.size(runtime);
42
+ auto *passwordData = password.data(runtime);
43
+ auto passwordPreventGC =
44
+ std::make_shared<jsi::ArrayBuffer>(std::move(password));
45
+
46
+ auto salt = arguments[1].asObject(runtime).getArrayBuffer(runtime);
47
+ auto saltSize = salt.size(runtime);
48
+ auto *saltData = salt.data(runtime);
49
+ auto saltPreventGC =
50
+ std::make_shared<jsi::ArrayBuffer>(std::move(salt));
51
+
52
+ auto iterations = arguments[2].asNumber();
53
+ auto keyLength = arguments[3].asNumber();
54
+ auto hashAlgorithm = arguments[4].asString(runtime).utf8(runtime);
55
+
56
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array> resultArray(
57
+ runtime, static_cast<size_t>(keyLength));
58
+ auto result = resultArray.getBuffer(runtime);
59
+ auto resultSize = result.size(runtime);
60
+ auto *resultData = result.data(runtime);
61
+ auto resultPreventGC =
62
+ std::make_shared<jsi::ArrayBuffer>(std::move(result));
63
+
64
+ return react::createPromiseAsJSIValue(
65
+ runtime, [=](jsi::Runtime &runtime,
66
+ std::shared_ptr<react::Promise> promise) {
67
+ // TODO(Szymon) implement proper errors
68
+ this->runOnWorkerThread([=]() {
69
+ if (hashAlgorithm == "sha1") {
70
+ fastpbkdf2_hmac_sha1(passwordData, passwordSize, saltData,
71
+ saltSize,
72
+ static_cast<uint32_t>(iterations),
73
+ resultData, resultSize);
74
+ } else if (hashAlgorithm == "sha256") {
75
+ fastpbkdf2_hmac_sha256(passwordData, passwordSize, saltData,
76
+ saltSize,
77
+ static_cast<uint32_t>(iterations),
78
+ resultData, resultSize);
79
+ } else if (hashAlgorithm == "sha512") {
80
+ fastpbkdf2_hmac_sha512(passwordData, passwordSize, saltData,
81
+ saltSize,
82
+ static_cast<uint32_t>(iterations),
83
+ resultData, resultSize);
84
+ } else {
85
+ auto *digest = EVP_get_digestbyname(hashAlgorithm.c_str());
86
+ if (digest == nullptr) {
87
+ this->runOnJSThread([=]() {
88
+ promise->reject("Invalid hash-algorithm!");
89
+ auto preventGC = passwordPreventGC;
90
+ auto preventGC2 = saltPreventGC;
91
+ });
92
+ }
93
+ char *passAsCharA = reinterpret_cast<char *>(passwordData);
94
+ const unsigned char *saltAsCharA =
95
+ reinterpret_cast<const unsigned char *>(saltData);
96
+ unsigned char *resultAsCharA =
97
+ reinterpret_cast<unsigned char *>(resultData);
98
+ PKCS5_PBKDF2_HMAC(passAsCharA, passwordSize, saltAsCharA,
99
+ saltSize, static_cast<uint32_t>(iterations),
100
+ digest, resultSize, resultAsCharA);
101
+ }
102
+ this->runOnJSThread([=]() {
103
+ promise->resolve(
104
+ jsi::ArrayBuffer(std::move(*resultPreventGC)));
105
+ auto preventGC = passwordPreventGC;
106
+ auto preventGC2 = saltPreventGC;
107
+ });
108
+ });
109
+ });
110
+
111
+ return resultArray;
112
+ }));
113
+
114
+ this->fields.push_back(HOST_LAMBDA("pbkdf2Sync", {
115
+ if (count != 5) {
116
+ throw jsi::JSError(runtime,
117
+ "fastpbkdf2Sync(..) expects exactly 5 arguments!");
118
+ }
119
+
120
+ auto password = arguments[0].asObject(runtime).getArrayBuffer(runtime);
121
+ auto salt = arguments[1].asObject(runtime).getArrayBuffer(runtime);
122
+ auto iterations = arguments[2].asNumber();
123
+ auto keyLength = arguments[3].asNumber();
124
+ auto hashAlgorithm = arguments[4].asString(runtime).utf8(runtime);
125
+
126
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array> resultArray(
127
+ runtime, static_cast<size_t>(keyLength));
128
+ auto result = resultArray.getBuffer(runtime);
129
+
130
+ if (hashAlgorithm == "sha1") {
131
+ fastpbkdf2_hmac_sha1(password.data(runtime), password.size(runtime),
132
+ salt.data(runtime), salt.size(runtime),
133
+ static_cast<uint32_t>(iterations),
134
+ result.data(runtime), result.size(runtime));
135
+ } else if (hashAlgorithm == "sha256") {
136
+ fastpbkdf2_hmac_sha256(password.data(runtime), password.size(runtime),
137
+ salt.data(runtime), salt.size(runtime),
138
+ static_cast<uint32_t>(iterations),
139
+ result.data(runtime), result.size(runtime));
140
+ } else if (hashAlgorithm == "sha512") {
141
+ fastpbkdf2_hmac_sha512(password.data(runtime), password.size(runtime),
142
+ salt.data(runtime), salt.size(runtime),
143
+ static_cast<uint32_t>(iterations),
144
+ result.data(runtime), result.size(runtime));
145
+ } else {
146
+ auto *digest = EVP_get_digestbyname(hashAlgorithm.c_str());
147
+ if (digest == nullptr) {
148
+ throw jsi::JSError(runtime, "Invalid hash-algorithm!");
149
+ }
150
+ char *passAsCharA = reinterpret_cast<char *>(password.data(runtime));
151
+ const unsigned char *saltAsCharA =
152
+ reinterpret_cast<const unsigned char *>(salt.data(runtime));
153
+ unsigned char *resultAsCharA =
154
+ reinterpret_cast<unsigned char *>(result.data(runtime));
155
+ PKCS5_PBKDF2_HMAC(passAsCharA, password.size(runtime), saltAsCharA,
156
+ salt.size(runtime), static_cast<uint32_t>(iterations),
157
+ digest, result.size(runtime), resultAsCharA);
158
+ }
159
+
160
+ return resultArray;
161
+ }));
162
+ }
163
+
164
+ } // namespace margelo
@@ -0,0 +1,29 @@
1
+ //
2
+ // Created by Szymon on 25/02/2022.
3
+ //
4
+
5
+ #ifndef MGL_PBKDF2HOSTOBJECT_H
6
+ #define MGL_PBKDF2HOSTOBJECT_H
7
+
8
+ #include <memory>
9
+
10
+ #ifdef ANDROID
11
+ #include "JSIUtils/MGLSmartHostObject.h"
12
+ #include "fastpbkdf2/fastpbkdf2.h"
13
+ #else
14
+ #include "MGLSmartHostObject.h"
15
+ #include "fastpbkdf2.h"
16
+ #endif
17
+
18
+ namespace margelo {
19
+ namespace jsi = facebook::jsi;
20
+
21
+ class MGLPbkdf2HostObject : public MGLSmartHostObject {
22
+ public:
23
+ MGLPbkdf2HostObject(
24
+ std::shared_ptr<react::CallInvoker> jsCallInvoker,
25
+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
26
+ };
27
+
28
+ } // namespace margelo
29
+ #endif // MGL_PBKDF2HOSTOBJECT_H
@@ -0,0 +1,352 @@
1
+ /*
2
+ * fast-pbkdf2 - Optimal PBKDF2-HMAC calculation
3
+ * Written in 2015 by Joseph Birr-Pixton <jpixton@gmail.com>
4
+ *
5
+ * To the extent possible under law, the author(s) have dedicated all
6
+ * copyright and related and neighboring rights to this software to the
7
+ * public domain worldwide. This software is distributed without any
8
+ * warranty.
9
+ *
10
+ * You should have received a copy of the CC0 Public Domain Dedication
11
+ * along with this software. If not, see
12
+ * <http://creativecommons.org/publicdomain/zero/1.0/>.
13
+ */
14
+
15
+ #include "fastpbkdf2.h"
16
+
17
+ #include <assert.h>
18
+ #include <string.h>
19
+ #if defined(__GNUC__)
20
+ #include <endian.h>
21
+ #endif
22
+
23
+ #include <openssl/sha.h>
24
+
25
+ /* --- MSVC doesn't support C99 --- */
26
+ #ifdef _MSC_VER
27
+ #define restrict
28
+ #define _Pragma __pragma
29
+ #endif
30
+
31
+ /* --- Common useful things --- */
32
+ #define MIN(a, b) ((a) > (b)) ? (b) : (a)
33
+
34
+ static inline void write32_be(uint32_t n, uint8_t out[4]) {
35
+ #if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
36
+ *(uint32_t *)(out) = __builtin_bswap32(n);
37
+ #else
38
+ out[0] = (n >> 24) & 0xff;
39
+ out[1] = (n >> 16) & 0xff;
40
+ out[2] = (n >> 8) & 0xff;
41
+ out[3] = n & 0xff;
42
+ #endif
43
+ }
44
+
45
+ static inline void write64_be(uint64_t n, uint8_t out[8]) {
46
+ #if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
47
+ *(uint64_t *)(out) = __builtin_bswap64(n);
48
+ #else
49
+ write32_be((n >> 32) & 0xffffffff, out);
50
+ write32_be(n & 0xffffffff, out + 4);
51
+ #endif
52
+ }
53
+
54
+ /* --- Optional OpenMP parallelisation of consecutive blocks --- */
55
+ #ifdef WITH_OPENMP
56
+ #define OPENMP_PARALLEL_FOR _Pragma("omp parallel for")
57
+ #else
58
+ #define OPENMP_PARALLEL_FOR
59
+ #endif
60
+
61
+ /* Prepare block (of blocksz bytes) to contain md padding denoting a msg-size
62
+ * message (in bytes). block has a prefix of used bytes.
63
+ *
64
+ * Message length is expressed in 32 bits (so suitable for sha1, sha256,
65
+ * sha512). */
66
+ static inline void md_pad(uint8_t *block, size_t blocksz, size_t used,
67
+ size_t msg) {
68
+ memset(block + used, 0, blocksz - used - 4);
69
+ block[used] = 0x80;
70
+ block += blocksz - 4;
71
+ write32_be((uint32_t)(msg * 8), block);
72
+ }
73
+
74
+ /* Internal function/type names for hash-specific things. */
75
+ #define HMAC_CTX(_name) HMAC_ ## _name ## _ctx
76
+ #define HMAC_INIT(_name) HMAC_ ## _name ## _init
77
+ #define HMAC_UPDATE(_name) HMAC_ ## _name ## _update
78
+ #define HMAC_FINAL(_name) HMAC_ ## _name ## _final
79
+
80
+ #define PBKDF2_F(_name) pbkdf2_f_ ## _name
81
+ #define PBKDF2(_name) pbkdf2_ ## _name
82
+
83
+ /* This macro expands to decls for the whole implementation for a given
84
+ * hash function. Arguments are:
85
+ *
86
+ * _name like 'sha1', added to symbol names
87
+ * _blocksz block size, in bytes
88
+ * _hashsz digest output, in bytes
89
+ * _ctx hash context type
90
+ * _init hash context initialisation function
91
+ * args: (_ctx *c)
92
+ * _update hash context update function
93
+ * args: (_ctx *c, const void *data, size_t ndata)
94
+ * _final hash context finish function
95
+ * args: (void *out, _ctx *c)
96
+ * _xform hash context raw block update function
97
+ * args: (_ctx *c, const void *data)
98
+ * _xcpy hash context raw copy function (only need copy hash state)
99
+ * args: (_ctx * restrict out, const _ctx *restrict in)
100
+ * _xtract hash context state extraction
101
+ * args: args (_ctx *restrict c, uint8_t *restrict out)
102
+ * _xxor hash context xor function (only need xor hash state)
103
+ * args: (_ctx *restrict out, const _ctx *restrict in)
104
+ *
105
+ * The resulting function is named PBKDF2(_name).
106
+ */
107
+ #define DECL_PBKDF2(_name, _blocksz, _hashsz, _ctx, _init, _update, _xform, \
108
+ _final, _xcpy, _xtract, _xxor) \
109
+ typedef struct { \
110
+ _ctx inner; \
111
+ _ctx outer; \
112
+ } HMAC_CTX(_name); \
113
+ \
114
+ static inline void HMAC_INIT(_name)(HMAC_CTX(_name) * ctx, \
115
+ const uint8_t *key, size_t nkey) { \
116
+ /* Prepare key: */ \
117
+ uint8_t k[_blocksz]; \
118
+ \
119
+ /* Shorten long keys. */ \
120
+ if (nkey > _blocksz) { \
121
+ _init(&ctx->inner); \
122
+ _update(&ctx->inner, key, nkey); \
123
+ _final(k, &ctx->inner); \
124
+ \
125
+ key = k; \
126
+ nkey = _hashsz; \
127
+ } \
128
+ \
129
+ /* Standard doesn't cover case where blocksz < hashsz. */ \
130
+ assert(nkey <= _blocksz); \
131
+ \
132
+ /* Right zero-pad short keys. */ \
133
+ if (k != key) memcpy(k, key, nkey); \
134
+ if (_blocksz > nkey) memset(k + nkey, 0, _blocksz - nkey); \
135
+ \
136
+ /* Start inner hash computation */ \
137
+ uint8_t blk_inner[_blocksz]; \
138
+ uint8_t blk_outer[_blocksz]; \
139
+ \
140
+ for (size_t i = 0; i < _blocksz; i++) { \
141
+ blk_inner[i] = 0x36 ^ k[i]; \
142
+ blk_outer[i] = 0x5c ^ k[i]; \
143
+ } \
144
+ \
145
+ _init(&ctx->inner); \
146
+ _update(&ctx->inner, blk_inner, sizeof blk_inner); \
147
+ \
148
+ /* And outer. */ \
149
+ _init(&ctx->outer); \
150
+ _update(&ctx->outer, blk_outer, sizeof blk_outer); \
151
+ } \
152
+ \
153
+ static inline void HMAC_UPDATE(_name)(HMAC_CTX(_name) * ctx, \
154
+ const void *data, size_t ndata) { \
155
+ _update(&ctx->inner, data, ndata); \
156
+ } \
157
+ \
158
+ static inline void HMAC_FINAL(_name)(HMAC_CTX(_name) * ctx, \
159
+ uint8_t out[_hashsz]) { \
160
+ _final(out, &ctx->inner); \
161
+ _update(&ctx->outer, out, _hashsz); \
162
+ _final(out, &ctx->outer); \
163
+ } \
164
+ \
165
+ /* --- PBKDF2 --- */ \
166
+ static inline void PBKDF2_F(_name)( \
167
+ const HMAC_CTX(_name) * startctx, uint32_t counter, const uint8_t *salt, \
168
+ size_t nsalt, uint32_t iterations, uint8_t *out) { \
169
+ uint8_t countbuf[4]; \
170
+ write32_be(counter, countbuf); \
171
+ \
172
+ /* Prepare loop-invariant padding block. */ \
173
+ uint8_t Ublock[_blocksz]; \
174
+ md_pad(Ublock, _blocksz, _hashsz, _blocksz + _hashsz); \
175
+ \
176
+ /* First iteration: \
177
+ * U_1 = PRF(P, S || INT_32_BE(i)) \
178
+ */ \
179
+ HMAC_CTX(_name) ctx = *startctx; \
180
+ HMAC_UPDATE(_name)(&ctx, salt, nsalt); \
181
+ HMAC_UPDATE(_name)(&ctx, countbuf, sizeof countbuf); \
182
+ HMAC_FINAL(_name)(&ctx, Ublock); \
183
+ _ctx result = ctx.outer; \
184
+ \
185
+ /* Subsequent iterations: \
186
+ * U_c = PRF(P, U_{c-1}) \
187
+ */ \
188
+ for (uint32_t i = 1; i < iterations; i++) { \
189
+ /* Complete inner hash with previous U */ \
190
+ _xcpy(&ctx.inner, &startctx->inner); \
191
+ _xform(&ctx.inner, Ublock); \
192
+ _xtract(&ctx.inner, Ublock); \
193
+ /* Complete outer hash with inner output */ \
194
+ _xcpy(&ctx.outer, &startctx->outer); \
195
+ _xform(&ctx.outer, Ublock); \
196
+ _xtract(&ctx.outer, Ublock); \
197
+ _xxor(&result, &ctx.outer); \
198
+ } \
199
+ \
200
+ /* Reform result into output buffer. */ \
201
+ _xtract(&result, out); \
202
+ } \
203
+ \
204
+ static inline void PBKDF2(_name)( \
205
+ const uint8_t *pw, size_t npw, const uint8_t *salt, size_t nsalt, \
206
+ uint32_t iterations, uint8_t *out, size_t nout) { \
207
+ assert(iterations); \
208
+ assert(out &&nout); \
209
+ \
210
+ /* Starting point for inner loop. */ \
211
+ HMAC_CTX(_name) ctx; \
212
+ HMAC_INIT(_name)(&ctx, pw, npw); \
213
+ \
214
+ /* How many blocks do we need? */ \
215
+ uint32_t blocks_needed = (uint32_t)(nout + _hashsz - 1) / _hashsz; \
216
+ \
217
+ OPENMP_PARALLEL_FOR \
218
+ for (uint32_t counter = 1; counter <= blocks_needed; counter++) { \
219
+ uint8_t block[_hashsz]; \
220
+ PBKDF2_F(_name)(&ctx, counter, salt, nsalt, iterations, block); \
221
+ \
222
+ size_t offset = (counter - 1) * _hashsz; \
223
+ size_t taken = MIN(nout - offset, _hashsz); \
224
+ memcpy(out + offset, block, taken); \
225
+ } \
226
+ }
227
+
228
+ static inline void sha1_extract(SHA_CTX *restrict ctx, uint8_t *restrict out) {
229
+ write32_be(ctx->h0, out);
230
+ write32_be(ctx->h1, out + 4);
231
+ write32_be(ctx->h2, out + 8);
232
+ write32_be(ctx->h3, out + 12);
233
+ write32_be(ctx->h4, out + 16);
234
+ }
235
+
236
+ static inline void sha1_cpy(SHA_CTX *restrict out, const SHA_CTX *restrict in) {
237
+ out->h0 = in->h0;
238
+ out->h1 = in->h1;
239
+ out->h2 = in->h2;
240
+ out->h3 = in->h3;
241
+ out->h4 = in->h4;
242
+ }
243
+
244
+ static inline void sha1_xor(SHA_CTX *restrict out, const SHA_CTX *restrict in) {
245
+ out->h0 ^= in->h0;
246
+ out->h1 ^= in->h1;
247
+ out->h2 ^= in->h2;
248
+ out->h3 ^= in->h3;
249
+ out->h4 ^= in->h4;
250
+ }
251
+
252
+ DECL_PBKDF2(sha1, SHA_CBLOCK, SHA_DIGEST_LENGTH, SHA_CTX, SHA1_Init,
253
+ SHA1_Update, SHA1_Transform, SHA1_Final, sha1_cpy, sha1_extract,
254
+ sha1_xor)
255
+
256
+ static inline void sha256_extract(SHA256_CTX *restrict ctx,
257
+ uint8_t *restrict out) {
258
+ write32_be(ctx->h[0], out);
259
+ write32_be(ctx->h[1], out + 4);
260
+ write32_be(ctx->h[2], out + 8);
261
+ write32_be(ctx->h[3], out + 12);
262
+ write32_be(ctx->h[4], out + 16);
263
+ write32_be(ctx->h[5], out + 20);
264
+ write32_be(ctx->h[6], out + 24);
265
+ write32_be(ctx->h[7], out + 28);
266
+ }
267
+
268
+ static inline void sha256_cpy(SHA256_CTX *restrict out,
269
+ const SHA256_CTX *restrict in) {
270
+ out->h[0] = in->h[0];
271
+ out->h[1] = in->h[1];
272
+ out->h[2] = in->h[2];
273
+ out->h[3] = in->h[3];
274
+ out->h[4] = in->h[4];
275
+ out->h[5] = in->h[5];
276
+ out->h[6] = in->h[6];
277
+ out->h[7] = in->h[7];
278
+ }
279
+
280
+ static inline void sha256_xor(SHA256_CTX *restrict out,
281
+ const SHA256_CTX *restrict in) {
282
+ out->h[0] ^= in->h[0];
283
+ out->h[1] ^= in->h[1];
284
+ out->h[2] ^= in->h[2];
285
+ out->h[3] ^= in->h[3];
286
+ out->h[4] ^= in->h[4];
287
+ out->h[5] ^= in->h[5];
288
+ out->h[6] ^= in->h[6];
289
+ out->h[7] ^= in->h[7];
290
+ }
291
+
292
+ DECL_PBKDF2(sha256, SHA256_CBLOCK, SHA256_DIGEST_LENGTH, SHA256_CTX,
293
+ SHA256_Init, SHA256_Update, SHA256_Transform, SHA256_Final,
294
+ sha256_cpy, sha256_extract, sha256_xor)
295
+
296
+ static inline void sha512_extract(SHA512_CTX *restrict ctx,
297
+ uint8_t *restrict out) {
298
+ write64_be(ctx->h[0], out);
299
+ write64_be(ctx->h[1], out + 8);
300
+ write64_be(ctx->h[2], out + 16);
301
+ write64_be(ctx->h[3], out + 24);
302
+ write64_be(ctx->h[4], out + 32);
303
+ write64_be(ctx->h[5], out + 40);
304
+ write64_be(ctx->h[6], out + 48);
305
+ write64_be(ctx->h[7], out + 56);
306
+ }
307
+
308
+ static inline void sha512_cpy(SHA512_CTX *restrict out,
309
+ const SHA512_CTX *restrict in) {
310
+ out->h[0] = in->h[0];
311
+ out->h[1] = in->h[1];
312
+ out->h[2] = in->h[2];
313
+ out->h[3] = in->h[3];
314
+ out->h[4] = in->h[4];
315
+ out->h[5] = in->h[5];
316
+ out->h[6] = in->h[6];
317
+ out->h[7] = in->h[7];
318
+ }
319
+
320
+ static inline void sha512_xor(SHA512_CTX *restrict out,
321
+ const SHA512_CTX *restrict in) {
322
+ out->h[0] ^= in->h[0];
323
+ out->h[1] ^= in->h[1];
324
+ out->h[2] ^= in->h[2];
325
+ out->h[3] ^= in->h[3];
326
+ out->h[4] ^= in->h[4];
327
+ out->h[5] ^= in->h[5];
328
+ out->h[6] ^= in->h[6];
329
+ out->h[7] ^= in->h[7];
330
+ }
331
+
332
+ DECL_PBKDF2(sha512, SHA512_CBLOCK, SHA512_DIGEST_LENGTH, SHA512_CTX,
333
+ SHA512_Init, SHA512_Update, SHA512_Transform, SHA512_Final,
334
+ sha512_cpy, sha512_extract, sha512_xor)
335
+
336
+ void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw, const uint8_t *salt,
337
+ size_t nsalt, uint32_t iterations, uint8_t *out,
338
+ size_t nout) {
339
+ PBKDF2(sha1)(pw, npw, salt, nsalt, iterations, out, nout);
340
+ }
341
+
342
+ void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw, const uint8_t *salt,
343
+ size_t nsalt, uint32_t iterations, uint8_t *out,
344
+ size_t nout) {
345
+ PBKDF2(sha256)(pw, npw, salt, nsalt, iterations, out, nout);
346
+ }
347
+
348
+ void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw, const uint8_t *salt,
349
+ size_t nsalt, uint32_t iterations, uint8_t *out,
350
+ size_t nout) {
351
+ PBKDF2(sha512)(pw, npw, salt, nsalt, iterations, out, nout);
352
+ }
@@ -0,0 +1,68 @@
1
+ /*
2
+ * fastpbkdf2 - Faster PBKDF2-HMAC calculation
3
+ * Written in 2015 by Joseph Birr-Pixton <jpixton@gmail.com>
4
+ *
5
+ * To the extent possible under law, the author(s) have dedicated all
6
+ * copyright and related and neighboring rights to this software to the
7
+ * public domain worldwide. This software is distributed without any
8
+ * warranty.
9
+ *
10
+ * You should have received a copy of the CC0 Public Domain Dedication
11
+ * along with this software. If not, see
12
+ * <http://creativecommons.org/publicdomain/zero/1.0/>.
13
+ */
14
+
15
+ #ifndef FASTPBKDF2_H
16
+ #define FASTPBKDF2_H
17
+
18
+ #include <stdint.h>
19
+ #include <stdlib.h>
20
+
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ /** Calculates PBKDF2-HMAC-SHA1.
26
+ *
27
+ * @p npw bytes at @p pw are the password input.
28
+ * @p nsalt bytes at @p salt are the salt input.
29
+ * @p iterations is the PBKDF2 iteration count and must be non-zero.
30
+ * @p nout bytes of output are written to @p out. @p nout must be non-zero.
31
+ *
32
+ * This function cannot fail; it does not report errors.
33
+ */
34
+ void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw, const uint8_t *salt,
35
+ size_t nsalt, uint32_t iterations, uint8_t *out,
36
+ size_t nout);
37
+
38
+ /** Calculates PBKDF2-HMAC-SHA256.
39
+ *
40
+ * @p npw bytes at @p pw are the password input.
41
+ * @p nsalt bytes at @p salt are the salt input.
42
+ * @p iterations is the PBKDF2 iteration count and must be non-zero.
43
+ * @p nout bytes of output are written to @p out. @p nout must be non-zero.
44
+ *
45
+ * This function cannot fail; it does not report errors.
46
+ */
47
+ void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw, const uint8_t *salt,
48
+ size_t nsalt, uint32_t iterations, uint8_t *out,
49
+ size_t nout);
50
+
51
+ /** Calculates PBKDF2-HMAC-SHA512.
52
+ *
53
+ * @p npw bytes at @p pw are the password input.
54
+ * @p nsalt bytes at @p salt are the salt input.
55
+ * @p iterations is the PBKDF2 iteration count and must be non-zero.
56
+ * @p nout bytes of output are written to @p out. @p nout must be non-zero.
57
+ *
58
+ * This function cannot fail; it does not report errors.
59
+ */
60
+ void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw, const uint8_t *salt,
61
+ size_t nsalt, uint32_t iterations, uint8_t *out,
62
+ size_t nout);
63
+
64
+ #ifdef __cplusplus
65
+ }
66
+ #endif
67
+
68
+ #endif
package/ios/.DS_Store ADDED
Binary file