@authon/react-native-wallet 0.0.0-stage → 0.1.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 (159) hide show
  1. package/README.md +221 -2
  2. package/android/build.gradle +40 -0
  3. package/android/consumer-rules.pro +10 -0
  4. package/android/src/main/AndroidManifest.xml +4 -0
  5. package/android/src/main/java/dev/authon/reactnative/wallet/AuthonWalletModule.kt +181 -0
  6. package/android/src/vendor/GENERATED.md +4 -0
  7. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/AuthonWallet.kt +191 -0
  8. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Bytes.kt +74 -0
  9. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/CredentialManagerPasskeys.kt +192 -0
  10. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DeviceStore.kt +170 -0
  11. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/DialogWalletApprover.kt +141 -0
  12. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Eip712.kt +176 -0
  13. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmAccount.kt +45 -0
  14. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/EvmTransaction.kt +180 -0
  15. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/GfTables.kt +44 -0
  16. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/HdWallet.kt +130 -0
  17. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Http.kt +82 -0
  18. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/JsValues.kt +83 -0
  19. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/KeystoreDeviceStore.kt +232 -0
  20. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Prf.kt +111 -0
  21. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Proof.kt +134 -0
  22. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/Shamir.kt +106 -0
  23. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/SignSummary.kt +335 -0
  24. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletApi.kt +353 -0
  25. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletError.kt +58 -0
  26. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletOps.kt +449 -0
  27. package/android/src/vendor/kotlin/dev/authon/sdk/wallet/WalletTypes.kt +71 -0
  28. package/build/authonContext.d.ts +19 -0
  29. package/build/authonContext.d.ts.map +1 -0
  30. package/build/authonContext.js +20 -0
  31. package/build/authonContext.js.map +1 -0
  32. package/build/client.d.ts +76 -0
  33. package/build/client.d.ts.map +1 -0
  34. package/build/client.js +349 -0
  35. package/build/client.js.map +1 -0
  36. package/build/errors.d.ts +18 -0
  37. package/build/errors.d.ts.map +1 -0
  38. package/build/errors.js +9 -0
  39. package/build/errors.js.map +1 -0
  40. package/build/index.d.ts +7 -0
  41. package/build/index.d.ts.map +1 -0
  42. package/build/index.js +4 -0
  43. package/build/index.js.map +1 -0
  44. package/build/native.d.ts +39 -0
  45. package/build/native.d.ts.map +1 -0
  46. package/build/native.js +12 -0
  47. package/build/native.js.map +1 -0
  48. package/build/types.d.ts +121 -0
  49. package/build/types.d.ts.map +1 -0
  50. package/build/types.js +3 -0
  51. package/build/types.js.map +1 -0
  52. package/build/useAuthonWallet.d.ts +25 -0
  53. package/build/useAuthonWallet.d.ts.map +1 -0
  54. package/build/useAuthonWallet.js +77 -0
  55. package/build/useAuthonWallet.js.map +1 -0
  56. package/expo-module.config.json +9 -0
  57. package/ios/AuthonReactNativeWallet.podspec +37 -0
  58. package/ios/AuthonWalletModule.swift +190 -0
  59. package/ios/vendor/AuthonWallet/AuthonWallet.swift +212 -0
  60. package/ios/vendor/AuthonWallet/Crypto/BIP39Wordlist.swift +262 -0
  61. package/ios/vendor/AuthonWallet/Crypto/HDKey.swift +108 -0
  62. package/ios/vendor/AuthonWallet/Crypto/Secp256k1.swift +104 -0
  63. package/ios/vendor/AuthonWallet/Crypto/Shamir.swift +123 -0
  64. package/ios/vendor/AuthonWallet/EVM/Address.swift +57 -0
  65. package/ios/vendor/AuthonWallet/EVM/EvmAccount.swift +48 -0
  66. package/ios/vendor/AuthonWallet/EVM/Transaction.swift +165 -0
  67. package/ios/vendor/AuthonWallet/EVM/TypedData.swift +193 -0
  68. package/ios/vendor/AuthonWallet/Support/BigInteger.swift +220 -0
  69. package/ios/vendor/AuthonWallet/Support/Bytes.swift +89 -0
  70. package/ios/vendor/AuthonWallet/Support/JSONValue.swift +341 -0
  71. package/ios/vendor/AuthonWallet/UI/WalletApprover.swift +250 -0
  72. package/ios/vendor/AuthonWallet/Wallet/Backup.swift +69 -0
  73. package/ios/vendor/AuthonWallet/Wallet/DeviceStore.swift +351 -0
  74. package/ios/vendor/AuthonWallet/Wallet/Passkeys.swift +190 -0
  75. package/ios/vendor/AuthonWallet/Wallet/Proof.swift +143 -0
  76. package/ios/vendor/AuthonWallet/Wallet/Shares.swift +116 -0
  77. package/ios/vendor/AuthonWallet/Wallet/Summary.swift +342 -0
  78. package/ios/vendor/AuthonWallet/Wallet/WalletAPI.swift +350 -0
  79. package/ios/vendor/AuthonWallet/Wallet/WalletOps.swift +555 -0
  80. package/ios/vendor/AuthonWallet/WalletError.swift +54 -0
  81. package/ios/vendor/CKeccak/LICENSE +22 -0
  82. package/ios/vendor/CKeccak/README.md +27 -0
  83. package/ios/vendor/CKeccak/byte_order.h +59 -0
  84. package/ios/vendor/CKeccak/include/ckeccak.h +9 -0
  85. package/ios/vendor/CKeccak/memzero.c +75 -0
  86. package/ios/vendor/CKeccak/memzero.h +8 -0
  87. package/ios/vendor/CKeccak/options.h +99 -0
  88. package/ios/vendor/CKeccak/sha3.c +413 -0
  89. package/ios/vendor/CKeccak/sha3.h +89 -0
  90. package/ios/vendor/CSecp256k1/COPYING +19 -0
  91. package/ios/vendor/CSecp256k1/README.md +35 -0
  92. package/ios/vendor/CSecp256k1/build/precomputed_ecmult.c +3 -0
  93. package/ios/vendor/CSecp256k1/build/precomputed_ecmult_gen.c +3 -0
  94. package/ios/vendor/CSecp256k1/build/secp256k1.c +3 -0
  95. package/ios/vendor/CSecp256k1/include/secp256k1.h +899 -0
  96. package/ios/vendor/CSecp256k1/include/secp256k1_preallocated.h +134 -0
  97. package/ios/vendor/CSecp256k1/include/secp256k1_recovery.h +113 -0
  98. package/ios/vendor/CSecp256k1/src/assumptions.h +87 -0
  99. package/ios/vendor/CSecp256k1/src/checkmem.h +102 -0
  100. package/ios/vendor/CSecp256k1/src/ecdsa.h +21 -0
  101. package/ios/vendor/CSecp256k1/src/ecdsa_impl.h +304 -0
  102. package/ios/vendor/CSecp256k1/src/eckey.h +25 -0
  103. package/ios/vendor/CSecp256k1/src/eckey_impl.h +92 -0
  104. package/ios/vendor/CSecp256k1/src/ecmult.h +61 -0
  105. package/ios/vendor/CSecp256k1/src/ecmult_const.h +38 -0
  106. package/ios/vendor/CSecp256k1/src/ecmult_const_impl.h +399 -0
  107. package/ios/vendor/CSecp256k1/src/ecmult_gen.h +143 -0
  108. package/ios/vendor/CSecp256k1/src/ecmult_gen_impl.h +341 -0
  109. package/ios/vendor/CSecp256k1/src/ecmult_impl.h +853 -0
  110. package/ios/vendor/CSecp256k1/src/field.h +350 -0
  111. package/ios/vendor/CSecp256k1/src/field_10x26.h +57 -0
  112. package/ios/vendor/CSecp256k1/src/field_10x26_impl.h +1232 -0
  113. package/ios/vendor/CSecp256k1/src/field_5x52.h +62 -0
  114. package/ios/vendor/CSecp256k1/src/field_5x52_impl.h +522 -0
  115. package/ios/vendor/CSecp256k1/src/field_5x52_int128_impl.h +274 -0
  116. package/ios/vendor/CSecp256k1/src/field_impl.h +457 -0
  117. package/ios/vendor/CSecp256k1/src/group.h +212 -0
  118. package/ios/vendor/CSecp256k1/src/group_impl.h +974 -0
  119. package/ios/vendor/CSecp256k1/src/hash.h +44 -0
  120. package/ios/vendor/CSecp256k1/src/hash_impl.h +299 -0
  121. package/ios/vendor/CSecp256k1/src/hsort.h +33 -0
  122. package/ios/vendor/CSecp256k1/src/hsort_impl.h +125 -0
  123. package/ios/vendor/CSecp256k1/src/int128.h +90 -0
  124. package/ios/vendor/CSecp256k1/src/int128_impl.h +18 -0
  125. package/ios/vendor/CSecp256k1/src/int128_native.h +19 -0
  126. package/ios/vendor/CSecp256k1/src/int128_native_impl.h +94 -0
  127. package/ios/vendor/CSecp256k1/src/int128_struct.h +14 -0
  128. package/ios/vendor/CSecp256k1/src/int128_struct_impl.h +205 -0
  129. package/ios/vendor/CSecp256k1/src/modinv32.h +43 -0
  130. package/ios/vendor/CSecp256k1/src/modinv32_impl.h +725 -0
  131. package/ios/vendor/CSecp256k1/src/modinv64.h +47 -0
  132. package/ios/vendor/CSecp256k1/src/modinv64_impl.h +780 -0
  133. package/ios/vendor/CSecp256k1/src/modules/recovery/main_impl.h +159 -0
  134. package/ios/vendor/CSecp256k1/src/precomputed_ecmult.c +16456 -0
  135. package/ios/vendor/CSecp256k1/src/precomputed_ecmult.h +38 -0
  136. package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.c +1779 -0
  137. package/ios/vendor/CSecp256k1/src/precomputed_ecmult_gen.h +26 -0
  138. package/ios/vendor/CSecp256k1/src/scalar.h +105 -0
  139. package/ios/vendor/CSecp256k1/src/scalar_4x64.h +19 -0
  140. package/ios/vendor/CSecp256k1/src/scalar_4x64_impl.h +1000 -0
  141. package/ios/vendor/CSecp256k1/src/scalar_8x32.h +19 -0
  142. package/ios/vendor/CSecp256k1/src/scalar_8x32_impl.h +816 -0
  143. package/ios/vendor/CSecp256k1/src/scalar_impl.h +321 -0
  144. package/ios/vendor/CSecp256k1/src/scalar_low.h +24 -0
  145. package/ios/vendor/CSecp256k1/src/scalar_low_impl.h +206 -0
  146. package/ios/vendor/CSecp256k1/src/scratch.h +44 -0
  147. package/ios/vendor/CSecp256k1/src/scratch_impl.h +99 -0
  148. package/ios/vendor/CSecp256k1/src/secp256k1.c +831 -0
  149. package/ios/vendor/CSecp256k1/src/selftest.h +32 -0
  150. package/ios/vendor/CSecp256k1/src/util.h +451 -0
  151. package/ios/vendor/GENERATED.md +4 -0
  152. package/package.json +65 -3
  153. package/src/authonContext.ts +30 -0
  154. package/src/client.ts +375 -0
  155. package/src/errors.ts +52 -0
  156. package/src/index.ts +21 -0
  157. package/src/native.ts +50 -0
  158. package/src/types.ts +120 -0
  159. package/src/useAuthonWallet.ts +103 -0
@@ -0,0 +1,59 @@
1
+ #ifndef __BYTE_ORDER_H__
2
+ #define __BYTE_ORDER_H__
3
+
4
+ // FROM sha2.h:
5
+ /*
6
+ * BYTE_ORDER NOTE:
7
+ *
8
+ * Please make sure that your system defines BYTE_ORDER. If your
9
+ * architecture is little-endian, make sure it also defines
10
+ * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
11
+ * equivalent.
12
+ *
13
+ * If your system does not define the above, then you can do so by
14
+ * hand like this:
15
+ *
16
+ * #define LITTLE_ENDIAN 1234
17
+ * #define BIG_ENDIAN 4321
18
+ *
19
+ * And for little-endian machines, add:
20
+ *
21
+ * #define BYTE_ORDER LITTLE_ENDIAN
22
+ *
23
+ * Or for big-endian machines:
24
+ *
25
+ * #define BYTE_ORDER BIG_ENDIAN
26
+ *
27
+ * The FreeBSD machine this was written on defines BYTE_ORDER
28
+ * appropriately by including <sys/types.h> (which in turn includes
29
+ * <machine/endian.h> where the appropriate definitions are actually
30
+ * made).
31
+ */
32
+
33
+ #ifndef LITTLE_ENDIAN
34
+ #define LITTLE_ENDIAN 1234
35
+ #define BIG_ENDIAN 4321
36
+ #endif
37
+
38
+ #ifndef BYTE_ORDER
39
+ #define BYTE_ORDER LITTLE_ENDIAN
40
+ #endif
41
+
42
+ #define REVERSE32(w, x) \
43
+ { \
44
+ uint32_t tmp = (w); \
45
+ tmp = (tmp >> 16) | (tmp << 16); \
46
+ (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
47
+ }
48
+
49
+ #define REVERSE64(w, x) \
50
+ { \
51
+ uint64_t tmp = (w); \
52
+ tmp = (tmp >> 32) | (tmp << 32); \
53
+ tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
54
+ ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
55
+ (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
56
+ ((tmp & 0x0000ffff0000ffffULL) << 16); \
57
+ }
58
+
59
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef CKECCAK_H
2
+ #define CKECCAK_H
3
+
4
+ #include <stddef.h>
5
+
6
+ /* Original Keccak-256 (Ethereum), implemented in ../sha3.c. */
7
+ void keccak_256(const unsigned char *data, size_t len, unsigned char *digest);
8
+
9
+ #endif
@@ -0,0 +1,75 @@
1
+ #ifndef __STDC_WANT_LIB_EXT1__
2
+ #define __STDC_WANT_LIB_EXT1__ 1 // C11's bounds-checking interface.
3
+ #endif
4
+ #include <string.h>
5
+
6
+ #ifdef _WIN32
7
+ #include <windows.h>
8
+ #endif
9
+
10
+ #ifdef __unix__
11
+ #include <strings.h>
12
+ #include <sys/param.h>
13
+ #endif
14
+
15
+ // C11's bounds-checking interface.
16
+ #if defined(__STDC_LIB_EXT1__)
17
+ #define HAVE_MEMSET_S 1
18
+ #endif
19
+
20
+ // GNU C Library version 2.25 or later.
21
+ #if defined(__GLIBC__) && \
22
+ (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
23
+ #define HAVE_EXPLICIT_BZERO 1
24
+ #endif
25
+
26
+ // Newlib
27
+ #if defined(__NEWLIB__)
28
+ #define HAVE_EXPLICIT_BZERO 1
29
+ #endif
30
+
31
+ // FreeBSD version 11.0 or later.
32
+ #if defined(__FreeBSD__) && __FreeBSD_version >= 1100037
33
+ #define HAVE_EXPLICIT_BZERO 1
34
+ #endif
35
+
36
+ // OpenBSD version 5.5 or later.
37
+ #if defined(__OpenBSD__) && OpenBSD >= 201405
38
+ #define HAVE_EXPLICIT_BZERO 1
39
+ #endif
40
+
41
+ // NetBSD version 7.2 or later.
42
+ #if defined(__NetBSD__) && __NetBSD_Version__ >= 702000000
43
+ #define HAVE_EXPLICIT_MEMSET 1
44
+ #endif
45
+
46
+ // Adapted from
47
+ // https://github.com/jedisct1/libsodium/blob/1647f0d53ae0e370378a9195477e3df0a792408f/src/libsodium/sodium/utils.c#L102-L130
48
+
49
+ void memzero(void *const pnt, const size_t len) {
50
+ #ifdef _WIN32
51
+ SecureZeroMemory(pnt, len);
52
+ #elif defined(HAVE_MEMSET_S)
53
+ memset_s(pnt, (rsize_t)len, 0, (rsize_t)len);
54
+ #elif defined(HAVE_EXPLICIT_BZERO)
55
+ explicit_bzero(pnt, len);
56
+ #elif defined(HAVE_EXPLICIT_MEMSET)
57
+ explicit_memset(pnt, 0, len);
58
+ #else
59
+ volatile unsigned char *volatile pnt_ = (volatile unsigned char *volatile)pnt;
60
+ size_t i = (size_t)0U;
61
+
62
+ while (i < len) {
63
+ pnt_[i++] = 0U;
64
+ }
65
+ #endif
66
+
67
+ // explicitly mark the memory as overwritten for the Clang MemorySanitizer
68
+ // this is only included at compile time if MemorySanitizer is enabled and
69
+ // should not come with any downsides during regular builds
70
+ #if defined(__has_feature)
71
+ #if __has_feature(memory_sanitizer)
72
+ memset(pnt, 0, len);
73
+ #endif
74
+ #endif
75
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef __MEMZERO_H__
2
+ #define __MEMZERO_H__
3
+
4
+ #include <stddef.h>
5
+
6
+ void memzero(void* const pnt, const size_t len);
7
+
8
+ #endif
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Copyright (c) 2013-2014 Pavol Rusnak
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files (the "Software"),
6
+ * to deal in the Software without restriction, including without limitation
7
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
+ * and/or sell copies of the Software, and to permit persons to whom the
9
+ * Software is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included
12
+ * in all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
18
+ * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ * OTHER DEALINGS IN THE SOFTWARE.
21
+ */
22
+
23
+ #ifndef __OPTIONS_H__
24
+ #define __OPTIONS_H__
25
+
26
+ // use precomputed Curve Points (some scalar multiples of curve base point G)
27
+ #ifndef USE_PRECOMPUTED_CP
28
+ #define USE_PRECOMPUTED_CP 1
29
+ #endif
30
+
31
+ // use fast inverse method
32
+ #ifndef USE_INVERSE_FAST
33
+ #define USE_INVERSE_FAST 1
34
+ #endif
35
+
36
+ // support for printing bignum256 structures via printf
37
+ #ifndef USE_BN_PRINT
38
+ #define USE_BN_PRINT 0
39
+ #endif
40
+
41
+ // use deterministic signatures
42
+ #ifndef USE_RFC6979
43
+ #define USE_RFC6979 1
44
+ #endif
45
+
46
+ // implement BIP32 caching
47
+ #ifndef USE_BIP32_CACHE
48
+ #define USE_BIP32_CACHE 1
49
+ #define BIP32_CACHE_SIZE 10
50
+ #define BIP32_CACHE_MAXDEPTH 8
51
+ #endif
52
+
53
+ // support constructing BIP32 nodes from ed25519 and curve25519 curves.
54
+ #ifndef USE_BIP32_25519_CURVES
55
+ #define USE_BIP32_25519_CURVES 1
56
+ #endif
57
+
58
+ // implement BIP39 caching
59
+ #ifndef USE_BIP39_CACHE
60
+ #define USE_BIP39_CACHE 1
61
+ #define BIP39_CACHE_SIZE 4
62
+ #endif
63
+
64
+ // support Ethereum operations
65
+ #ifndef USE_ETHEREUM
66
+ #define USE_ETHEREUM 0
67
+ #endif
68
+
69
+ // support NEM operations
70
+ #ifndef USE_NEM
71
+ #define USE_NEM 0
72
+ #endif
73
+
74
+ // support MONERO operations
75
+ #ifndef USE_MONERO
76
+ #define USE_MONERO 0
77
+ #endif
78
+
79
+ // support CARDANO operations
80
+ #ifndef USE_CARDANO
81
+ #define USE_CARDANO 0
82
+ #endif
83
+
84
+ // support Keccak hashing
85
+ #ifndef USE_KECCAK
86
+ #define USE_KECCAK 1
87
+ #endif
88
+
89
+ // add way how to mark confidential data
90
+ #ifndef CONFIDENTIAL
91
+ #define CONFIDENTIAL
92
+ #endif
93
+
94
+ // add way to mark functions whose return value should always be checked
95
+ #ifndef __wur
96
+ #define __wur __attribute__((warn_unused_result))
97
+ #endif
98
+
99
+ #endif
@@ -0,0 +1,413 @@
1
+ /* sha3.c - an implementation of Secure Hash Algorithm 3 (Keccak).
2
+ * based on the
3
+ * The Keccak SHA-3 submission. Submission to NIST (Round 3), 2011
4
+ * by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche
5
+ *
6
+ * Copyright: 2013 Aleksey Kravchenko <rhash.admin@gmail.com>
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a
9
+ * copy of this software and associated documentation files (the "Software"),
10
+ * to deal in the Software without restriction, including without limitation
11
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the
13
+ * Software is furnished to do so.
14
+ *
15
+ * This program is distributed in the hope that it will be useful, but
16
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
+ * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
18
+ */
19
+
20
+ #include <assert.h>
21
+ #include <string.h>
22
+
23
+ #include "sha3.h"
24
+ #include "memzero.h"
25
+ #include "byte_order.h"
26
+
27
+ #define I64(x) x##LL
28
+ #define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n))))
29
+ #define le2me_64(x) (x)
30
+ #define IS_ALIGNED_64(p) (0 == (((uintptr_t)(const void *)(p) & 0x7)))
31
+ # define me64_to_le_str(to, from, length) memcpy((to), (from), (length))
32
+
33
+ /* constants */
34
+ #define NumberOfRounds 24
35
+
36
+ /* SHA3 (Keccak) constants for 24 rounds */
37
+ static uint64_t keccak_round_constants[NumberOfRounds] = {
38
+ I64(0x0000000000000001), I64(0x0000000000008082), I64(0x800000000000808A), I64(0x8000000080008000),
39
+ I64(0x000000000000808B), I64(0x0000000080000001), I64(0x8000000080008081), I64(0x8000000000008009),
40
+ I64(0x000000000000008A), I64(0x0000000000000088), I64(0x0000000080008009), I64(0x000000008000000A),
41
+ I64(0x000000008000808B), I64(0x800000000000008B), I64(0x8000000000008089), I64(0x8000000000008003),
42
+ I64(0x8000000000008002), I64(0x8000000000000080), I64(0x000000000000800A), I64(0x800000008000000A),
43
+ I64(0x8000000080008081), I64(0x8000000000008080), I64(0x0000000080000001), I64(0x8000000080008008)
44
+ };
45
+
46
+ /* Initializing a sha3 context for given number of output bits */
47
+ static void keccak_Init(SHA3_CTX *ctx, unsigned bits)
48
+ {
49
+ /* NB: The Keccak capacity parameter = bits * 2 */
50
+ unsigned rate = 1600 - bits * 2;
51
+
52
+ memzero(ctx, sizeof(SHA3_CTX));
53
+ ctx->block_size = rate / 8;
54
+ assert(rate <= 1600 && (rate % 64) == 0);
55
+ }
56
+
57
+ /**
58
+ * Initialize context before calculating hash.
59
+ *
60
+ * @param ctx context to initialize
61
+ */
62
+ void sha3_224_Init(SHA3_CTX *ctx)
63
+ {
64
+ keccak_Init(ctx, 224);
65
+ }
66
+
67
+ /**
68
+ * Initialize context before calculating hash.
69
+ *
70
+ * @param ctx context to initialize
71
+ */
72
+ void sha3_256_Init(SHA3_CTX *ctx)
73
+ {
74
+ keccak_Init(ctx, 256);
75
+ }
76
+
77
+ /**
78
+ * Initialize context before calculating hash.
79
+ *
80
+ * @param ctx context to initialize
81
+ */
82
+ void sha3_384_Init(SHA3_CTX *ctx)
83
+ {
84
+ keccak_Init(ctx, 384);
85
+ }
86
+
87
+ /**
88
+ * Initialize context before calculating hash.
89
+ *
90
+ * @param ctx context to initialize
91
+ */
92
+ void sha3_512_Init(SHA3_CTX *ctx)
93
+ {
94
+ keccak_Init(ctx, 512);
95
+ }
96
+
97
+ /* Keccak theta() transformation */
98
+ static void keccak_theta(uint64_t *A)
99
+ {
100
+ unsigned int x = 0;
101
+ uint64_t C[5] = {0}, D[5] = {0};
102
+
103
+ for (x = 0; x < 5; x++) {
104
+ C[x] = A[x] ^ A[x + 5] ^ A[x + 10] ^ A[x + 15] ^ A[x + 20];
105
+ }
106
+ D[0] = ROTL64(C[1], 1) ^ C[4];
107
+ D[1] = ROTL64(C[2], 1) ^ C[0];
108
+ D[2] = ROTL64(C[3], 1) ^ C[1];
109
+ D[3] = ROTL64(C[4], 1) ^ C[2];
110
+ D[4] = ROTL64(C[0], 1) ^ C[3];
111
+
112
+ for (x = 0; x < 5; x++) {
113
+ A[x] ^= D[x];
114
+ A[x + 5] ^= D[x];
115
+ A[x + 10] ^= D[x];
116
+ A[x + 15] ^= D[x];
117
+ A[x + 20] ^= D[x];
118
+ }
119
+ }
120
+
121
+ /* Keccak pi() transformation */
122
+ static void keccak_pi(uint64_t *A)
123
+ {
124
+ uint64_t A1 = 0;
125
+ A1 = A[1];
126
+ A[ 1] = A[ 6];
127
+ A[ 6] = A[ 9];
128
+ A[ 9] = A[22];
129
+ A[22] = A[14];
130
+ A[14] = A[20];
131
+ A[20] = A[ 2];
132
+ A[ 2] = A[12];
133
+ A[12] = A[13];
134
+ A[13] = A[19];
135
+ A[19] = A[23];
136
+ A[23] = A[15];
137
+ A[15] = A[ 4];
138
+ A[ 4] = A[24];
139
+ A[24] = A[21];
140
+ A[21] = A[ 8];
141
+ A[ 8] = A[16];
142
+ A[16] = A[ 5];
143
+ A[ 5] = A[ 3];
144
+ A[ 3] = A[18];
145
+ A[18] = A[17];
146
+ A[17] = A[11];
147
+ A[11] = A[ 7];
148
+ A[ 7] = A[10];
149
+ A[10] = A1;
150
+ /* note: A[ 0] is left as is */
151
+ }
152
+
153
+ /* Keccak chi() transformation */
154
+ static void keccak_chi(uint64_t *A)
155
+ {
156
+ int i = 0;
157
+ for (i = 0; i < 25; i += 5) {
158
+ uint64_t A0 = A[0 + i], A1 = A[1 + i];
159
+ A[0 + i] ^= ~A1 & A[2 + i];
160
+ A[1 + i] ^= ~A[2 + i] & A[3 + i];
161
+ A[2 + i] ^= ~A[3 + i] & A[4 + i];
162
+ A[3 + i] ^= ~A[4 + i] & A0;
163
+ A[4 + i] ^= ~A0 & A1;
164
+ }
165
+ }
166
+
167
+ static void sha3_permutation(uint64_t *state)
168
+ {
169
+ #if BYTE_ORDER == BIG_ENDIAN
170
+ int i;
171
+ for (i = 0; i < 25; i++)
172
+ {
173
+ REVERSE64(state[i], state[i]);
174
+ }
175
+ #endif
176
+ int round = 0;
177
+ for (round = 0; round < NumberOfRounds; round++)
178
+ {
179
+ keccak_theta(state);
180
+
181
+ /* apply Keccak rho() transformation */
182
+ state[ 1] = ROTL64(state[ 1], 1);
183
+ state[ 2] = ROTL64(state[ 2], 62);
184
+ state[ 3] = ROTL64(state[ 3], 28);
185
+ state[ 4] = ROTL64(state[ 4], 27);
186
+ state[ 5] = ROTL64(state[ 5], 36);
187
+ state[ 6] = ROTL64(state[ 6], 44);
188
+ state[ 7] = ROTL64(state[ 7], 6);
189
+ state[ 8] = ROTL64(state[ 8], 55);
190
+ state[ 9] = ROTL64(state[ 9], 20);
191
+ state[10] = ROTL64(state[10], 3);
192
+ state[11] = ROTL64(state[11], 10);
193
+ state[12] = ROTL64(state[12], 43);
194
+ state[13] = ROTL64(state[13], 25);
195
+ state[14] = ROTL64(state[14], 39);
196
+ state[15] = ROTL64(state[15], 41);
197
+ state[16] = ROTL64(state[16], 45);
198
+ state[17] = ROTL64(state[17], 15);
199
+ state[18] = ROTL64(state[18], 21);
200
+ state[19] = ROTL64(state[19], 8);
201
+ state[20] = ROTL64(state[20], 18);
202
+ state[21] = ROTL64(state[21], 2);
203
+ state[22] = ROTL64(state[22], 61);
204
+ state[23] = ROTL64(state[23], 56);
205
+ state[24] = ROTL64(state[24], 14);
206
+
207
+ keccak_pi(state);
208
+ keccak_chi(state);
209
+
210
+ /* apply iota(state, round) */
211
+ *state ^= keccak_round_constants[round];
212
+ }
213
+ #if BYTE_ORDER == BIG_ENDIAN
214
+ for (i = 0; i < 25; i++)
215
+ {
216
+ REVERSE64(state[i], state[i]);
217
+ }
218
+ #endif
219
+ }
220
+
221
+ /**
222
+ * The core transformation. Process the specified block of data.
223
+ *
224
+ * @param hash the algorithm state
225
+ * @param block the message block to process
226
+ * @param block_size the size of the processed block in bytes
227
+ */
228
+ static void sha3_process_block(uint64_t hash[25], const uint64_t *block, size_t block_size)
229
+ {
230
+ /* expanded loop */
231
+ hash[ 0] ^= le2me_64(block[ 0]);
232
+ hash[ 1] ^= le2me_64(block[ 1]);
233
+ hash[ 2] ^= le2me_64(block[ 2]);
234
+ hash[ 3] ^= le2me_64(block[ 3]);
235
+ hash[ 4] ^= le2me_64(block[ 4]);
236
+ hash[ 5] ^= le2me_64(block[ 5]);
237
+ hash[ 6] ^= le2me_64(block[ 6]);
238
+ hash[ 7] ^= le2me_64(block[ 7]);
239
+ hash[ 8] ^= le2me_64(block[ 8]);
240
+ /* if not sha3-512 */
241
+ if (block_size > 72) {
242
+ hash[ 9] ^= le2me_64(block[ 9]);
243
+ hash[10] ^= le2me_64(block[10]);
244
+ hash[11] ^= le2me_64(block[11]);
245
+ hash[12] ^= le2me_64(block[12]);
246
+ /* if not sha3-384 */
247
+ if (block_size > 104) {
248
+ hash[13] ^= le2me_64(block[13]);
249
+ hash[14] ^= le2me_64(block[14]);
250
+ hash[15] ^= le2me_64(block[15]);
251
+ hash[16] ^= le2me_64(block[16]);
252
+ /* if not sha3-256 */
253
+ if (block_size > 136) {
254
+ hash[17] ^= le2me_64(block[17]);
255
+ #ifdef FULL_SHA3_FAMILY_SUPPORT
256
+ /* if not sha3-224 */
257
+ if (block_size > 144) {
258
+ hash[18] ^= le2me_64(block[18]);
259
+ hash[19] ^= le2me_64(block[19]);
260
+ hash[20] ^= le2me_64(block[20]);
261
+ hash[21] ^= le2me_64(block[21]);
262
+ hash[22] ^= le2me_64(block[22]);
263
+ hash[23] ^= le2me_64(block[23]);
264
+ hash[24] ^= le2me_64(block[24]);
265
+ }
266
+ #endif
267
+ }
268
+ }
269
+ }
270
+ /* make a permutation of the hash */
271
+ sha3_permutation(hash);
272
+ }
273
+
274
+ #define SHA3_FINALIZED 0x80000000
275
+
276
+ /**
277
+ * Calculate message hash.
278
+ * Can be called repeatedly with chunks of the message to be hashed.
279
+ *
280
+ * @param ctx the algorithm context containing current hashing state
281
+ * @param msg message chunk
282
+ * @param size length of the message chunk
283
+ */
284
+ void sha3_Update(SHA3_CTX *ctx, const unsigned char *msg, size_t size)
285
+ {
286
+ if (size == 0) return;
287
+
288
+ size_t idx = (size_t)ctx->rest;
289
+ size_t block_size = (size_t)ctx->block_size;
290
+
291
+ if (ctx->rest & SHA3_FINALIZED) return; /* too late for additional input */
292
+ ctx->rest = (unsigned)((ctx->rest + size) % block_size);
293
+
294
+ /* fill partial block */
295
+ if (idx) {
296
+ size_t left = block_size - idx;
297
+ memcpy((char*)ctx->message + idx, msg, (size < left ? size : left));
298
+ if (size < left) return;
299
+
300
+ /* process partial block */
301
+ sha3_process_block(ctx->hash, ctx->message, block_size);
302
+ msg += left;
303
+ size -= left;
304
+ }
305
+ while (size >= block_size) {
306
+ uint64_t *aligned_message_block = NULL;
307
+ if (IS_ALIGNED_64(msg)) {
308
+ /* the most common case is processing of an already aligned message
309
+ without copying it */
310
+ aligned_message_block = (uint64_t*)(void*)msg;
311
+ } else {
312
+ memcpy(ctx->message, msg, block_size);
313
+ aligned_message_block = ctx->message;
314
+ }
315
+
316
+ sha3_process_block(ctx->hash, aligned_message_block, block_size);
317
+ msg += block_size;
318
+ size -= block_size;
319
+ }
320
+ if (size) {
321
+ memcpy(ctx->message, msg, size); /* save leftovers */
322
+ }
323
+ }
324
+
325
+ /**
326
+ * Store calculated hash into the given array.
327
+ *
328
+ * @param ctx the algorithm context containing current hashing state
329
+ * @param result calculated hash in binary form
330
+ */
331
+ void sha3_Final(SHA3_CTX *ctx, unsigned char* result)
332
+ {
333
+ size_t digest_length = 100 - ctx->block_size / 2;
334
+ const size_t block_size = ctx->block_size;
335
+
336
+ if (!(ctx->rest & SHA3_FINALIZED))
337
+ {
338
+ /* clear the rest of the data queue */
339
+ memzero((char*)ctx->message + ctx->rest, block_size - ctx->rest);
340
+ ((char*)ctx->message)[ctx->rest] |= 0x06;
341
+ ((char*)ctx->message)[block_size - 1] |= 0x80;
342
+
343
+ /* process final block */
344
+ sha3_process_block(ctx->hash, ctx->message, block_size);
345
+ ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
346
+ }
347
+
348
+ assert(block_size > digest_length);
349
+ if (result) me64_to_le_str(result, ctx->hash, digest_length);
350
+ memzero(ctx, sizeof(SHA3_CTX));
351
+ }
352
+
353
+ #if USE_KECCAK
354
+ /**
355
+ * Store calculated hash into the given array.
356
+ *
357
+ * @param ctx the algorithm context containing current hashing state
358
+ * @param result calculated hash in binary form
359
+ */
360
+ void keccak_Final(SHA3_CTX *ctx, unsigned char* result)
361
+ {
362
+ size_t digest_length = 100 - ctx->block_size / 2;
363
+ const size_t block_size = ctx->block_size;
364
+
365
+ if (!(ctx->rest & SHA3_FINALIZED))
366
+ {
367
+ /* clear the rest of the data queue */
368
+ memzero((char*)ctx->message + ctx->rest, block_size - ctx->rest);
369
+ ((char*)ctx->message)[ctx->rest] |= 0x01;
370
+ ((char*)ctx->message)[block_size - 1] |= 0x80;
371
+
372
+ /* process final block */
373
+ sha3_process_block(ctx->hash, ctx->message, block_size);
374
+ ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
375
+ }
376
+
377
+ assert(block_size > digest_length);
378
+ if (result) me64_to_le_str(result, ctx->hash, digest_length);
379
+ memzero(ctx, sizeof(SHA3_CTX));
380
+ }
381
+
382
+ void keccak_256(const unsigned char* data, size_t len, unsigned char* digest)
383
+ {
384
+ SHA3_CTX ctx = {0};
385
+ keccak_256_Init(&ctx);
386
+ keccak_Update(&ctx, data, len);
387
+ keccak_Final(&ctx, digest);
388
+ }
389
+
390
+ void keccak_512(const unsigned char* data, size_t len, unsigned char* digest)
391
+ {
392
+ SHA3_CTX ctx = {0};
393
+ keccak_512_Init(&ctx);
394
+ keccak_Update(&ctx, data, len);
395
+ keccak_Final(&ctx, digest);
396
+ }
397
+ #endif /* USE_KECCAK */
398
+
399
+ void sha3_256(const unsigned char* data, size_t len, unsigned char* digest)
400
+ {
401
+ SHA3_CTX ctx = {0};
402
+ sha3_256_Init(&ctx);
403
+ sha3_Update(&ctx, data, len);
404
+ sha3_Final(&ctx, digest);
405
+ }
406
+
407
+ void sha3_512(const unsigned char* data, size_t len, unsigned char* digest)
408
+ {
409
+ SHA3_CTX ctx = {0};
410
+ sha3_512_Init(&ctx);
411
+ sha3_Update(&ctx, data, len);
412
+ sha3_Final(&ctx, digest);
413
+ }