@onekeyfe/react-native-lite-card 6.7.0-alpha.407 → 6.7.0-alpha.411

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 (223) hide show
  1. package/README.md +9 -1
  2. package/ReactNativeLiteCard.podspec +25 -2
  3. package/android/build.gradle +7 -0
  4. package/android/src/main/cpp/CMakeLists.txt +6 -2
  5. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
  6. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +5 -1
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +21 -7
  9. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
  10. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +4 -1
  11. package/ios/Classes/OKNFCBridge.mm +1 -1
  12. package/ios/Classes/OKNFTLite/OKLiteV1.m +6 -3
  13. package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
  14. package/package.json +2 -2
  15. package/sdk/CMakeLists.txt +120 -0
  16. package/sdk/README.md +175 -0
  17. package/sdk/android/implJni.cpp +177 -0
  18. package/sdk/deps/jsoncpp/LICENSE +55 -0
  19. package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
  20. package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
  21. package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
  22. package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
  23. package/sdk/deps/jsoncpp/include/json/config.h +187 -0
  24. package/sdk/deps/jsoncpp/include/json/features.h +61 -0
  25. package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
  26. package/sdk/deps/jsoncpp/include/json/json.h +15 -0
  27. package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
  28. package/sdk/deps/jsoncpp/include/json/value.h +888 -0
  29. package/sdk/deps/jsoncpp/include/json/version.h +20 -0
  30. package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
  31. package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
  32. package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
  33. package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
  34. package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
  35. package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
  36. package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
  37. package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
  38. package/sdk/deps/trezor-crypto/AUTHORS +2 -0
  39. package/sdk/deps/trezor-crypto/LICENSE +22 -0
  40. package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
  41. package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
  42. package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
  43. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
  44. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
  45. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
  46. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
  47. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
  48. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
  49. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
  50. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
  51. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
  52. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
  53. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
  54. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
  55. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
  56. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
  57. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
  58. package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
  59. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
  60. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
  61. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
  62. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
  63. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
  64. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
  65. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
  66. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
  67. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
  68. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
  69. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
  70. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
  71. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
  72. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
  73. package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
  74. package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
  75. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
  76. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
  77. package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
  78. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
  79. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
  80. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
  81. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
  82. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
  83. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
  84. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
  85. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
  86. package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
  87. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
  88. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
  89. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
  90. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
  91. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
  92. package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
  93. package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
  94. package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
  95. package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
  96. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
  97. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
  98. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
  99. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
  100. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
  101. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
  102. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
  103. package/sdk/deps/trezor-crypto/src/address.c +87 -0
  104. package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
  105. package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
  106. package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
  107. package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
  108. package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
  109. package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
  110. package/sdk/deps/trezor-crypto/src/base32.c +233 -0
  111. package/sdk/deps/trezor-crypto/src/base58.c +298 -0
  112. package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
  113. package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
  114. package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
  115. package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
  116. package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
  117. package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
  118. package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
  119. package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
  120. package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
  121. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
  122. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
  123. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
  124. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
  125. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
  126. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
  127. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
  128. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
  129. package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
  130. package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
  131. package/sdk/deps/trezor-crypto/src/curves.c +48 -0
  132. package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
  133. package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
  134. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
  135. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
  136. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
  137. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
  138. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
  139. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
  140. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
  141. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
  142. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
  143. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
  144. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
  145. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
  146. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
  147. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
  148. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
  149. package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
  150. package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
  151. package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
  152. package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
  153. package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
  154. package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
  155. package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
  156. package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
  157. package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
  158. package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
  159. package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
  160. package/sdk/deps/trezor-crypto/src/nano.c +151 -0
  161. package/sdk/deps/trezor-crypto/src/nem.c +569 -0
  162. package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
  163. package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
  164. package/sdk/deps/trezor-crypto/src/options.h +43 -0
  165. package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
  166. package/sdk/deps/trezor-crypto/src/rand.c +51 -0
  167. package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
  168. package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
  169. package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
  170. package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
  171. package/sdk/deps/trezor-crypto/src/script.c +64 -0
  172. package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
  173. package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
  174. package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
  175. package/sdk/deps/trezor-crypto/src/setup.py +38 -0
  176. package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
  177. package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
  178. package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
  179. package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
  180. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
  181. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
  182. package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
  183. package/sdk/gpc/include/scp03/scp03.hpp +80 -0
  184. package/sdk/gpc/include/scp11/scp11.hpp +492 -0
  185. package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
  186. package/sdk/gpc/include/tag.h +68 -0
  187. package/sdk/gpc/include/tlv.hpp +94 -0
  188. package/sdk/gpc/include/tlvparse.hpp +24 -0
  189. package/sdk/gpc/src/scp03.cpp +243 -0
  190. package/sdk/gpc/src/scp11.cpp +798 -0
  191. package/sdk/gpc/src/scp11c.cpp +136 -0
  192. package/sdk/gpc/src/tlv.cpp +159 -0
  193. package/sdk/gpc/src/tlvparse.c +174 -0
  194. package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
  195. package/sdk/include/GPChannelSDKCore.h +10 -0
  196. package/sdk/include/context/BaseContext.h +56 -0
  197. package/sdk/include/device/ApduBuilder.hpp +72 -0
  198. package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
  199. package/sdk/include/utility/Apdu.hpp +172 -0
  200. package/sdk/include/utility/Debug.hpp +3 -0
  201. package/sdk/include/utility/util.h +104 -0
  202. package/sdk/src/ApduBuilder.cpp +332 -0
  203. package/sdk/src/GPChannelSDK.cpp +252 -0
  204. package/sdk/src/random.c +25 -0
  205. package/sdk/src/version.c +2 -0
  206. package/sdk/tests/android/GPChannelProxy.java +55 -0
  207. package/sdk/tests/android_verify.py +121 -0
  208. package/sdk/tests/safety.cpp +603 -0
  209. package/sdk/tests/verify.py +267 -0
  210. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  211. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  212. package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
  213. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  214. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  215. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  216. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  217. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
  218. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  219. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
  220. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  221. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  222. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  223. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
@@ -0,0 +1,318 @@
1
+ /*
2
+ BLAKE2 reference source code package - reference C implementations
3
+
4
+ Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5
+ terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6
+ your option. The terms of these licenses can be found at:
7
+
8
+ - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9
+ - OpenSSL license : https://www.openssl.org/source/license.html
10
+ - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ More information about the BLAKE2 hash function can be found at
13
+ https://blake2.net.
14
+ */
15
+
16
+ #include <string.h>
17
+
18
+ #include <TrezorCrypto/blake2s.h>
19
+ #include "blake2_common.h"
20
+ #include <TrezorCrypto/memzero.h>
21
+
22
+ typedef struct blake2s_param__
23
+ {
24
+ uint8_t digest_length; /* 1 */
25
+ uint8_t key_length; /* 2 */
26
+ uint8_t fanout; /* 3 */
27
+ uint8_t depth; /* 4 */
28
+ uint32_t leaf_length; /* 8 */
29
+ uint32_t node_offset; /* 12 */
30
+ uint16_t xof_length; /* 14 */
31
+ uint8_t node_depth; /* 15 */
32
+ uint8_t inner_length; /* 16 */
33
+ /* uint8_t reserved[0]; */
34
+ uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */
35
+ uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
36
+ } __attribute__((packed)) blake2s_param;
37
+
38
+ static const uint32_t blake2s_IV[8] =
39
+ {
40
+ 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
41
+ 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
42
+ };
43
+
44
+ static const uint8_t blake2s_sigma[10][16] =
45
+ {
46
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
47
+ { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
48
+ { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
49
+ { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
50
+ { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
51
+ { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
52
+ { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
53
+ { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
54
+ { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
55
+ { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
56
+ };
57
+
58
+ static void blake2s_set_lastnode( blake2s_state *S )
59
+ {
60
+ S->f[1] = (uint32_t)-1;
61
+ }
62
+
63
+ /* Some helper functions, not necessarily useful */
64
+ static int blake2s_is_lastblock( const blake2s_state *S )
65
+ {
66
+ return S->f[0] != 0;
67
+ }
68
+
69
+ static void blake2s_set_lastblock( blake2s_state *S )
70
+ {
71
+ if( S->last_node ) blake2s_set_lastnode( S );
72
+
73
+ S->f[0] = (uint32_t)-1;
74
+ }
75
+
76
+ static void blake2s_increment_counter( blake2s_state *S, const uint32_t inc )
77
+ {
78
+ S->t[0] += inc;
79
+ S->t[1] += ( S->t[0] < inc );
80
+ }
81
+
82
+ static void blake2s_init0( blake2s_state *S )
83
+ {
84
+ size_t i;
85
+ memset( S, 0, sizeof( blake2s_state ) );
86
+
87
+ for( i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
88
+ }
89
+
90
+ /* init2 xors IV with input parameter block */
91
+ int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
92
+ {
93
+ const unsigned char *p = ( const unsigned char * )( P );
94
+ size_t i;
95
+
96
+ blake2s_init0( S );
97
+
98
+ /* IV XOR ParamBlock */
99
+ for( i = 0; i < 8; ++i )
100
+ S->h[i] ^= load32( &p[i * 4] );
101
+
102
+ S->outlen = P->digest_length;
103
+ return 0;
104
+ }
105
+
106
+
107
+ /* Sequential blake2s initialization */
108
+ int blake2s_Init( blake2s_state *S, size_t outlen )
109
+ {
110
+ blake2s_param P[1];
111
+
112
+ if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
113
+
114
+ P->digest_length = (uint8_t)outlen;
115
+ P->key_length = 0;
116
+ P->fanout = 1;
117
+ P->depth = 1;
118
+ store32( &P->leaf_length, 0 );
119
+ store32( &P->node_offset, 0 );
120
+ store16( &P->xof_length, 0 );
121
+ P->node_depth = 0;
122
+ P->inner_length = 0;
123
+ /* memset(P->reserved, 0, sizeof(P->reserved) ); */
124
+ memset( P->salt, 0, sizeof( P->salt ) );
125
+ memset( P->personal, 0, sizeof( P->personal ) );
126
+ return blake2s_init_param( S, P );
127
+ }
128
+
129
+ int blake2s_InitPersonal( blake2s_state *S, size_t outlen, const void *personal, size_t personal_len)
130
+ {
131
+ blake2s_param P[1];
132
+
133
+ if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
134
+ if ( ( !personal ) || ( personal_len != BLAKE2S_PERSONALBYTES ) ) return -1;
135
+
136
+ P->digest_length = (uint8_t)outlen;
137
+ P->key_length = 0;
138
+ P->fanout = 1;
139
+ P->depth = 1;
140
+ store32( &P->leaf_length, 0 );
141
+ store32( &P->node_offset, 0 );
142
+ store16( &P->xof_length, 0 );
143
+ P->node_depth = 0;
144
+ P->inner_length = 0;
145
+ /* memset(P->reserved, 0, sizeof(P->reserved) ); */
146
+ memset( P->salt, 0, sizeof( P->salt ) );
147
+ memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES );
148
+ return blake2s_init_param( S, P );
149
+ }
150
+
151
+
152
+ int blake2s_InitKey( blake2s_state *S, size_t outlen, const void *key, size_t keylen )
153
+ {
154
+ blake2s_param P[1];
155
+
156
+ if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
157
+
158
+ if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
159
+
160
+ P->digest_length = (uint8_t)outlen;
161
+ P->key_length = (uint8_t)keylen;
162
+ P->fanout = 1;
163
+ P->depth = 1;
164
+ store32( &P->leaf_length, 0 );
165
+ store32( &P->node_offset, 0 );
166
+ store16( &P->xof_length, 0 );
167
+ P->node_depth = 0;
168
+ P->inner_length = 0;
169
+ /* memset(P->reserved, 0, sizeof(P->reserved) ); */
170
+ memset( P->salt, 0, sizeof( P->salt ) );
171
+ memset( P->personal, 0, sizeof( P->personal ) );
172
+
173
+ if( blake2s_init_param( S, P ) < 0 ) return -1;
174
+
175
+ {
176
+ uint8_t block[BLAKE2S_BLOCKBYTES];
177
+ memset( block, 0, BLAKE2S_BLOCKBYTES );
178
+ memcpy( block, key, keylen );
179
+ blake2s_Update( S, block, BLAKE2S_BLOCKBYTES );
180
+ memzero( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
181
+ }
182
+ return 0;
183
+ }
184
+
185
+ #define G(r,i,a,b,c,d) \
186
+ do { \
187
+ a = a + b + m[blake2s_sigma[r][2*i+0]]; \
188
+ d = rotr32(d ^ a, 16); \
189
+ c = c + d; \
190
+ b = rotr32(b ^ c, 12); \
191
+ a = a + b + m[blake2s_sigma[r][2*i+1]]; \
192
+ d = rotr32(d ^ a, 8); \
193
+ c = c + d; \
194
+ b = rotr32(b ^ c, 7); \
195
+ } while(0)
196
+
197
+ #define ROUND(r) \
198
+ do { \
199
+ G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
200
+ G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
201
+ G(r,2,v[ 2],v[ 6],v[10],v[14]); \
202
+ G(r,3,v[ 3],v[ 7],v[11],v[15]); \
203
+ G(r,4,v[ 0],v[ 5],v[10],v[15]); \
204
+ G(r,5,v[ 1],v[ 6],v[11],v[12]); \
205
+ G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
206
+ G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
207
+ } while(0)
208
+
209
+ static void blake2s_compress( blake2s_state *S, const uint8_t in[BLAKE2S_BLOCKBYTES] )
210
+ {
211
+ uint32_t m[16];
212
+ uint32_t v[16];
213
+ size_t i;
214
+
215
+ for( i = 0; i < 16; ++i ) {
216
+ m[i] = load32( in + i * sizeof( m[i] ) );
217
+ }
218
+
219
+ for( i = 0; i < 8; ++i ) {
220
+ v[i] = S->h[i];
221
+ }
222
+
223
+ v[ 8] = blake2s_IV[0];
224
+ v[ 9] = blake2s_IV[1];
225
+ v[10] = blake2s_IV[2];
226
+ v[11] = blake2s_IV[3];
227
+ v[12] = S->t[0] ^ blake2s_IV[4];
228
+ v[13] = S->t[1] ^ blake2s_IV[5];
229
+ v[14] = S->f[0] ^ blake2s_IV[6];
230
+ v[15] = S->f[1] ^ blake2s_IV[7];
231
+
232
+ ROUND( 0 );
233
+ ROUND( 1 );
234
+ ROUND( 2 );
235
+ ROUND( 3 );
236
+ ROUND( 4 );
237
+ ROUND( 5 );
238
+ ROUND( 6 );
239
+ ROUND( 7 );
240
+ ROUND( 8 );
241
+ ROUND( 9 );
242
+
243
+ for( i = 0; i < 8; ++i ) {
244
+ S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
245
+ }
246
+ }
247
+
248
+ #undef G
249
+ #undef ROUND
250
+
251
+ int blake2s_Update( blake2s_state *S, const void *pin, size_t inlen )
252
+ {
253
+ const unsigned char * in = (const unsigned char *)pin;
254
+ if( inlen > 0 )
255
+ {
256
+ size_t left = S->buflen;
257
+ size_t fill = BLAKE2S_BLOCKBYTES - left;
258
+ if( inlen > fill )
259
+ {
260
+ S->buflen = 0;
261
+ memcpy( S->buf + left, in, fill ); /* Fill buffer */
262
+ blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
263
+ blake2s_compress( S, S->buf ); /* Compress */
264
+ in += fill; inlen -= fill;
265
+ while(inlen > BLAKE2S_BLOCKBYTES) {
266
+ blake2s_increment_counter(S, BLAKE2S_BLOCKBYTES);
267
+ blake2s_compress( S, in );
268
+ in += BLAKE2S_BLOCKBYTES;
269
+ inlen -= BLAKE2S_BLOCKBYTES;
270
+ }
271
+ }
272
+ memcpy( S->buf + S->buflen, in, inlen );
273
+ S->buflen += inlen;
274
+ }
275
+ return 0;
276
+ }
277
+
278
+ int blake2s_Final( blake2s_state *S, void *out, size_t outlen )
279
+ {
280
+ uint8_t buffer[BLAKE2S_OUTBYTES] = {0};
281
+ size_t i;
282
+
283
+ if( out == NULL || outlen < S->outlen )
284
+ return -1;
285
+
286
+ if( blake2s_is_lastblock( S ) )
287
+ return -1;
288
+
289
+ blake2s_increment_counter( S, ( uint32_t )S->buflen );
290
+ blake2s_set_lastblock( S );
291
+ memset( S->buf + S->buflen, 0, BLAKE2S_BLOCKBYTES - S->buflen ); /* Padding */
292
+ blake2s_compress( S, S->buf );
293
+
294
+ for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
295
+ store32( buffer + sizeof( S->h[i] ) * i, S->h[i] );
296
+
297
+ memcpy( out, buffer, outlen );
298
+ memzero(buffer, sizeof(buffer));
299
+ return 0;
300
+ }
301
+
302
+ int blake2s(const uint8_t *msg, uint32_t msg_len, void *out, size_t outlen)
303
+ {
304
+ BLAKE2S_CTX ctx;
305
+ if (0 != blake2s_Init(&ctx, outlen)) return -1;
306
+ if (0 != blake2s_Update(&ctx, msg, msg_len)) return -1;
307
+ if (0 != blake2s_Final(&ctx, out, outlen)) return -1;
308
+ return 0;
309
+ }
310
+
311
+ int blake2s_Key(const uint8_t *msg, uint32_t msg_len, const void *key, size_t keylen, void *out, size_t outlen)
312
+ {
313
+ BLAKE2S_CTX ctx;
314
+ if (0 != blake2s_InitKey(&ctx, outlen, key, keylen)) return -1;
315
+ if (0 != blake2s_Update(&ctx, msg, msg_len)) return -1;
316
+ if (0 != blake2s_Final(&ctx, out, outlen)) return -1;
317
+ return 0;
318
+ }
@@ -0,0 +1,195 @@
1
+ /* Copyright (c) 2017 Jochen Hoenicke
2
+ * based on code Copyright (c) 2017 Peter Wuille
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+ #include <stdlib.h>
23
+ #include <stdint.h>
24
+ #include <string.h>
25
+
26
+ #include <TrezorCrypto/cash_addr.h>
27
+
28
+ #define MAX_CASHADDR_SIZE 129
29
+ #define MAX_BASE32_SIZE 104
30
+ #define MAX_DATA_SIZE 65
31
+ #define MAX_HRP_SIZE 20
32
+ #define CHECKSUM_SIZE 8
33
+
34
+ uint64_t cashaddr_polymod_step(uint64_t pre) {
35
+ uint8_t b = pre >> 35;
36
+ return ((pre & 0x7FFFFFFFFULL) << 5) ^
37
+ (-((b >> 0) & 1) & 0x98f2bc8e61ULL) ^
38
+ (-((b >> 1) & 1) & 0x79b76d99e2ULL) ^
39
+ (-((b >> 2) & 1) & 0xf33e5fb3c4ULL) ^
40
+ (-((b >> 3) & 1) & 0xae2eabe2a8ULL) ^
41
+ (-((b >> 4) & 1) & 0x1e4f43e470ULL);
42
+ }
43
+
44
+ static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
45
+
46
+ static const int8_t charset_rev[128] = {
47
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50
+ 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
51
+ -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
52
+ 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
53
+ -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
54
+ 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
55
+ };
56
+
57
+ int cash_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) {
58
+ uint64_t chk = 1;
59
+ size_t i = 0;
60
+ while (hrp[i] != 0) {
61
+ int ch = hrp[i];
62
+ if (ch < 33 || ch > 126) {
63
+ return 0;
64
+ }
65
+ *(output++) = ch;
66
+ chk = cashaddr_polymod_step(chk) ^ (ch & 0x1f);
67
+ ++i;
68
+ }
69
+ if (i + 1 + data_len + CHECKSUM_SIZE > MAX_CASHADDR_SIZE) {
70
+ return 0;
71
+ }
72
+ chk = cashaddr_polymod_step(chk);
73
+ *(output++) = ':';
74
+ for (i = 0; i < data_len; ++i) {
75
+ if (*data >> 5) return 0;
76
+ chk = cashaddr_polymod_step(chk) ^ (*data);
77
+ *(output++) = charset[*(data++)];
78
+ }
79
+ for (i = 0; i < CHECKSUM_SIZE; ++i) {
80
+ chk = cashaddr_polymod_step(chk);
81
+ }
82
+ chk ^= 1;
83
+ for (i = 0; i < CHECKSUM_SIZE; ++i) {
84
+ *(output++) = charset[(chk >> ((CHECKSUM_SIZE - 1 - i) * 5)) & 0x1f];
85
+ }
86
+ *output = 0;
87
+ return 1;
88
+ }
89
+
90
+ int cash_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input) {
91
+ uint64_t chk = 1;
92
+ size_t i;
93
+ size_t input_len = strlen(input);
94
+ size_t hrp_len;
95
+ int have_lower = 0, have_upper = 0;
96
+ if (input_len < CHECKSUM_SIZE || input_len > MAX_CASHADDR_SIZE) {
97
+ return 0;
98
+ }
99
+ *data_len = 0;
100
+ while (*data_len < input_len && input[(input_len - 1) - *data_len] != ':') {
101
+ ++(*data_len);
102
+ }
103
+ hrp_len = input_len - (1 + *data_len);
104
+ if (1 + *data_len >= input_len || hrp_len > MAX_HRP_SIZE ||
105
+ *data_len < CHECKSUM_SIZE || *data_len > CHECKSUM_SIZE + MAX_BASE32_SIZE) {
106
+ return 0;
107
+ }
108
+ // subtract checksum
109
+ *(data_len) -= CHECKSUM_SIZE;
110
+ for (i = 0; i < hrp_len; ++i) {
111
+ int ch = input[i];
112
+ if (ch < 33 || ch > 126) {
113
+ return 0;
114
+ }
115
+ if (ch >= 'a' && ch <= 'z') {
116
+ have_lower = 1;
117
+ } else if (ch >= 'A' && ch <= 'Z') {
118
+ have_upper = 1;
119
+ ch = (ch - 'A') + 'a';
120
+ }
121
+ hrp[i] = ch;
122
+ chk = cashaddr_polymod_step(chk) ^ (ch & 0x1f);
123
+ }
124
+ hrp[i] = 0;
125
+ chk = cashaddr_polymod_step(chk);
126
+ ++i;
127
+ while (i < input_len) {
128
+ int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]];
129
+ if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
130
+ if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
131
+ if (v == -1) {
132
+ return 0;
133
+ }
134
+ chk = cashaddr_polymod_step(chk) ^ v;
135
+ if (i + CHECKSUM_SIZE < input_len) {
136
+ data[i - (1 + hrp_len)] = v;
137
+ }
138
+ ++i;
139
+ }
140
+ if (have_lower && have_upper) {
141
+ return 0;
142
+ }
143
+ return chk == 1;
144
+ }
145
+
146
+ static int convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) {
147
+ uint32_t val = 0;
148
+ int bits = 0;
149
+ uint32_t maxv = (((uint32_t)1) << outbits) - 1;
150
+ while (inlen--) {
151
+ val = (val << inbits) | *(in++);
152
+ bits += inbits;
153
+ while (bits >= outbits) {
154
+ bits -= outbits;
155
+ out[(*outlen)++] = (val >> bits) & maxv;
156
+ }
157
+ }
158
+ if (pad) {
159
+ if (bits) {
160
+ out[(*outlen)++] = (val << (outbits - bits)) & maxv;
161
+ }
162
+ } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) {
163
+ return 0;
164
+ }
165
+ return 1;
166
+ }
167
+
168
+ int cash_addr_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) {
169
+ uint8_t base32[MAX_BASE32_SIZE];
170
+ size_t base32len = 0;
171
+ cash_addr_to_data(base32, &base32len, data, data_len);
172
+ return cash_encode(output, hrp, base32, base32len);
173
+ }
174
+
175
+ int cash_addr_decode(uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) {
176
+ uint8_t data[MAX_BASE32_SIZE];
177
+ char hrp_actual[MAX_HRP_SIZE+1];
178
+ size_t data_len;
179
+ if (!cash_decode(hrp_actual, data, &data_len, addr)) return 0;
180
+ if (strncmp(hrp, hrp_actual, MAX_HRP_SIZE + 1) != 0) return 0;
181
+ return cash_data_to_addr(witdata, witdata_len, data, data_len);
182
+ }
183
+
184
+ int cash_addr_to_data(uint8_t *output, size_t *output_len, const uint8_t *data, size_t data_len) {
185
+ if (data_len < 2 || data_len > MAX_DATA_SIZE) return 0;
186
+ return convert_bits(output, output_len, 5, data, data_len, 8, 1);
187
+ }
188
+
189
+ int cash_data_to_addr(uint8_t* witdata, size_t* witdata_len, const uint8_t* data, size_t data_len) {
190
+ if (data_len == 0 || data_len > MAX_BASE32_SIZE) return 0;
191
+ *witdata_len = 0;
192
+ if (!convert_bits(witdata, witdata_len, 8, data, data_len, 5, 0)) return 0;
193
+ if (*witdata_len < 2 || *witdata_len > MAX_DATA_SIZE) return 0;
194
+ return 1;
195
+ }
@@ -0,0 +1,60 @@
1
+ // Implementations of the XChaCha20 + Poly1305 and ChaCha20 + Poly1305
2
+ // AEAD constructions with a goal of simplicity and correctness rather
3
+ // than performance.
4
+
5
+ #include <TrezorCrypto/chacha20poly1305.h>
6
+ #include "ecrypt-portable.h"
7
+
8
+ void hchacha20(ECRYPT_ctx *x,u8 *c);
9
+
10
+ // Initialize the XChaCha20 + Poly1305 context for encryption or decryption
11
+ // using a 32 byte key and 24 byte nonce. The key and the first 16 bytes of
12
+ // the nonce are used as input to HChaCha20 to derive the Chacha20 key.
13
+ void xchacha20poly1305_init(chacha20poly1305_ctx *ctx, uint8_t key[32], uint8_t nonce[24]) {
14
+ unsigned char subkey[32] = {0};
15
+ unsigned char block0[64] = {0};
16
+ ECRYPT_ctx tmp;
17
+
18
+ // Generate the Chacha20 key by applying HChaCha20 to the
19
+ // original key and the first 16 bytes of the nonce.
20
+ ECRYPT_keysetup(&tmp, key, 256, 16);
21
+ tmp.input[12] = U8TO32_LITTLE(nonce + 0);
22
+ tmp.input[13] = U8TO32_LITTLE(nonce + 4);
23
+ tmp.input[14] = U8TO32_LITTLE(nonce + 8);
24
+ tmp.input[15] = U8TO32_LITTLE(nonce + 12);
25
+ hchacha20(&tmp, subkey);
26
+
27
+ // Initialize Chacha20 with the newly generated key and
28
+ // the last 8 bytes of the nonce.
29
+ ECRYPT_keysetup(&ctx->chacha20, subkey, 256, 16);
30
+ ECRYPT_ivsetup(&ctx->chacha20, nonce+16);
31
+
32
+ // Encrypt 64 bytes of zeros and use the first 32 bytes
33
+ // as the Poly1305 key.
34
+ ECRYPT_encrypt_bytes(&ctx->chacha20, block0, block0, 64);
35
+ poly1305_init(&ctx->poly1305, block0);
36
+ }
37
+
38
+ // Encrypt n bytes of plaintext where n must be evenly divisible by the
39
+ // Chacha20 blocksize of 64, except for the final n bytes of plaintext.
40
+ void chacha20poly1305_encrypt(chacha20poly1305_ctx *ctx, uint8_t *in, uint8_t *out, size_t n) {
41
+ ECRYPT_encrypt_bytes(&ctx->chacha20, in, out, n);
42
+ poly1305_update(&ctx->poly1305, out, n);
43
+ }
44
+
45
+ // Decrypt n bytes of ciphertext where n must be evenly divisible by the
46
+ // Chacha20 blocksize of 64, except for the final n bytes of ciphertext.
47
+ void chacha20poly1305_decrypt(chacha20poly1305_ctx *ctx, uint8_t *in, uint8_t *out, size_t n) {
48
+ poly1305_update(&ctx->poly1305, in, n);
49
+ ECRYPT_encrypt_bytes(&ctx->chacha20, in, out, n);
50
+ }
51
+
52
+ // Include authenticated data in the Poly1305 MAC.
53
+ void chacha20poly1305_auth(chacha20poly1305_ctx *ctx, uint8_t *in, size_t n) {
54
+ poly1305_update(&ctx->poly1305, in, n);
55
+ }
56
+
57
+ // Compute NaCl secretbox-style Poly1305 MAC.
58
+ void chacha20poly1305_finish(chacha20poly1305_ctx *ctx, uint8_t mac[16]) {
59
+ poly1305_finish(&ctx->poly1305, mac);
60
+ }