@onekeyfe/react-native-lite-card 6.7.0-4 → 6.7.0-40

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 (237) hide show
  1. package/README.md +12 -2
  2. package/ReactNativeLiteCard.podspec +31 -2
  3. package/android/build.gradle +8 -0
  4. package/android/consumer-rules.pro +4 -0
  5. package/android/src/main/cpp/CMakeLists.txt +6 -2
  6. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +60 -49
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +117 -39
  9. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
  10. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +24 -2
  11. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +46 -19
  12. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
  13. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +38 -1
  14. package/ios/Classes/OKNFCBridge.mm +24 -1
  15. package/ios/Classes/OKNFTLite/OKLiteV1.m +20 -10
  16. package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
  17. package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
  18. package/ios/Classes/OKNFTLite/OKNFCManager.m +110 -18
  19. package/ios/ReactNativeLiteCard.mm +23 -11
  20. package/lib/module/index.js +4 -4
  21. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +4 -4
  22. package/lib/typescript/src/index.d.ts +2 -2
  23. package/package.json +8 -8
  24. package/sdk/CMakeLists.txt +121 -0
  25. package/sdk/README.md +190 -0
  26. package/sdk/android/implJni.cpp +216 -0
  27. package/sdk/deps/jsoncpp/LICENSE +55 -0
  28. package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
  29. package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
  30. package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
  31. package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
  32. package/sdk/deps/jsoncpp/include/json/config.h +187 -0
  33. package/sdk/deps/jsoncpp/include/json/features.h +61 -0
  34. package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
  35. package/sdk/deps/jsoncpp/include/json/json.h +15 -0
  36. package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
  37. package/sdk/deps/jsoncpp/include/json/value.h +888 -0
  38. package/sdk/deps/jsoncpp/include/json/version.h +20 -0
  39. package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
  40. package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
  41. package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
  42. package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
  43. package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
  44. package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
  45. package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
  46. package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
  47. package/sdk/deps/trezor-crypto/AUTHORS +2 -0
  48. package/sdk/deps/trezor-crypto/LICENSE +22 -0
  49. package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
  50. package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
  51. package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
  52. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
  53. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
  54. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
  55. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
  56. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
  57. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
  58. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
  59. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
  60. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
  61. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
  62. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
  63. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
  64. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
  65. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
  66. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
  67. package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
  68. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
  69. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
  70. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
  71. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
  72. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
  73. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
  74. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
  75. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
  76. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
  77. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
  78. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
  79. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
  80. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
  81. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
  82. package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
  83. package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
  84. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
  85. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
  86. package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
  87. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
  88. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
  89. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
  90. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
  91. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
  92. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
  93. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
  94. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
  95. package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
  96. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
  97. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
  98. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
  99. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
  100. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
  101. package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
  102. package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
  103. package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
  104. package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
  105. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
  106. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
  107. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
  108. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
  109. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
  110. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
  111. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
  112. package/sdk/deps/trezor-crypto/src/address.c +87 -0
  113. package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
  114. package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
  115. package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
  116. package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
  117. package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
  118. package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
  119. package/sdk/deps/trezor-crypto/src/base32.c +233 -0
  120. package/sdk/deps/trezor-crypto/src/base58.c +298 -0
  121. package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
  122. package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
  123. package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
  124. package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
  125. package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
  126. package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
  127. package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
  128. package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
  129. package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
  130. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
  131. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
  132. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
  133. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
  134. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
  135. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
  136. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
  137. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
  138. package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
  139. package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
  140. package/sdk/deps/trezor-crypto/src/curves.c +48 -0
  141. package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
  142. package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
  143. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
  144. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
  145. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
  146. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
  147. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
  148. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
  149. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
  150. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
  151. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
  152. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
  153. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
  154. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
  155. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
  156. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
  157. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
  158. package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
  159. package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
  160. package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
  161. package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
  162. package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
  163. package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
  164. package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
  165. package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
  166. package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
  167. package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
  168. package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
  169. package/sdk/deps/trezor-crypto/src/nano.c +151 -0
  170. package/sdk/deps/trezor-crypto/src/nem.c +569 -0
  171. package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
  172. package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
  173. package/sdk/deps/trezor-crypto/src/options.h +43 -0
  174. package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
  175. package/sdk/deps/trezor-crypto/src/rand.c +51 -0
  176. package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
  177. package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
  178. package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
  179. package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
  180. package/sdk/deps/trezor-crypto/src/script.c +64 -0
  181. package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
  182. package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
  183. package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
  184. package/sdk/deps/trezor-crypto/src/setup.py +38 -0
  185. package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
  186. package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
  187. package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
  188. package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
  189. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
  190. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
  191. package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
  192. package/sdk/gpc/include/scp03/scp03.hpp +80 -0
  193. package/sdk/gpc/include/scp11/scp11.hpp +492 -0
  194. package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
  195. package/sdk/gpc/include/tag.h +68 -0
  196. package/sdk/gpc/include/tlv.hpp +94 -0
  197. package/sdk/gpc/include/tlvparse.hpp +24 -0
  198. package/sdk/gpc/src/scp03.cpp +243 -0
  199. package/sdk/gpc/src/scp11.cpp +798 -0
  200. package/sdk/gpc/src/scp11c.cpp +136 -0
  201. package/sdk/gpc/src/tlv.cpp +159 -0
  202. package/sdk/gpc/src/tlvparse.c +174 -0
  203. package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
  204. package/sdk/include/GPChannelSDKCore.h +10 -0
  205. package/sdk/include/context/BaseContext.h +56 -0
  206. package/sdk/include/device/ApduBuilder.hpp +72 -0
  207. package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
  208. package/sdk/include/utility/Apdu.hpp +172 -0
  209. package/sdk/include/utility/Debug.hpp +3 -0
  210. package/sdk/include/utility/util.h +104 -0
  211. package/sdk/scripts/generate-source-id.rb +22 -0
  212. package/sdk/source-id-manifest.txt +19 -0
  213. package/sdk/source-id.cmake +31 -0
  214. package/sdk/src/ApduBuilder.cpp +332 -0
  215. package/sdk/src/GPChannelSDK.cpp +258 -0
  216. package/sdk/src/random.c +25 -0
  217. package/sdk/src/version.c +3 -0
  218. package/sdk/tests/android/GPChannelProxy.java +55 -0
  219. package/sdk/tests/android_verify.py +124 -0
  220. package/sdk/tests/safety.cpp +757 -0
  221. package/sdk/tests/verify.py +284 -0
  222. package/src/NativeReactNativeLiteCard.ts +4 -2
  223. package/src/index.tsx +14 -3
  224. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  225. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  226. package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
  227. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  228. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  229. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  230. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  231. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
  232. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  233. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
  234. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  235. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  236. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  237. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
@@ -0,0 +1,959 @@
1
+ /*
2
+ ---------------------------------------------------------------------------
3
+ Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
4
+
5
+ The redistribution and use of this software (with or without changes)
6
+ is allowed without the payment of fees or royalties provided that:
7
+
8
+ source code distributions include the above copyright notice, this
9
+ list of conditions and the following disclaimer;
10
+
11
+ binary distributions include the above copyright notice, this list
12
+ of conditions and the following disclaimer in their documentation.
13
+
14
+ This software is provided 'as is' with no explicit or implied warranties
15
+ in respect of its operation, including, but not limited to, correctness
16
+ and fitness for purpose.
17
+ ---------------------------------------------------------------------------
18
+ Issue Date: 20/12/2007
19
+
20
+ These subroutines implement multiple block AES modes for ECB, CBC, CFB,
21
+ OFB and CTR encryption, The code provides support for the VIA Advanced
22
+ Cryptography Engine (ACE).
23
+
24
+ NOTE: In the following subroutines, the AES contexts (ctx) must be
25
+ 16 byte aligned if VIA ACE is being used
26
+ */
27
+
28
+ #include <string.h>
29
+ #include <assert.h>
30
+ #include <stdint.h>
31
+
32
+ #include "aesopt.h"
33
+
34
+ #if defined( AES_MODES )
35
+ #if defined(__cplusplus)
36
+ extern "C"
37
+ {
38
+ #endif
39
+
40
+ #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
41
+ #pragma intrinsic(memcpy)
42
+ #endif
43
+
44
+ #define BFR_BLOCKS 8
45
+
46
+ /* These values are used to detect long word alignment in order to */
47
+ /* speed up some buffer operations. This facility may not work on */
48
+ /* some machines so this define can be commented out if necessary */
49
+
50
+ #define FAST_BUFFER_OPERATIONS
51
+
52
+ #define lp32(x) ((uint32_t*)(x))
53
+
54
+ #if defined( USE_VIA_ACE_IF_PRESENT )
55
+
56
+ #include "aes_via_ace.h"
57
+
58
+ #pragma pack(16)
59
+
60
+ aligned_array(unsigned long, enc_gen_table, 12, 16) = NEH_ENC_GEN_DATA;
61
+ aligned_array(unsigned long, enc_load_table, 12, 16) = NEH_ENC_LOAD_DATA;
62
+ aligned_array(unsigned long, enc_hybrid_table, 12, 16) = NEH_ENC_HYBRID_DATA;
63
+ aligned_array(unsigned long, dec_gen_table, 12, 16) = NEH_DEC_GEN_DATA;
64
+ aligned_array(unsigned long, dec_load_table, 12, 16) = NEH_DEC_LOAD_DATA;
65
+ aligned_array(unsigned long, dec_hybrid_table, 12, 16) = NEH_DEC_HYBRID_DATA;
66
+
67
+ /* NOTE: These control word macros must only be used after */
68
+ /* a key has been set up because they depend on key size */
69
+ /* See the VIA ACE documentation for key type information */
70
+ /* and aes_via_ace.h for non-default NEH_KEY_TYPE values */
71
+
72
+ #ifndef NEH_KEY_TYPE
73
+ # define NEH_KEY_TYPE NEH_HYBRID
74
+ #endif
75
+
76
+ #if NEH_KEY_TYPE == NEH_LOAD
77
+ #define kd_adr(c) ((uint8_t*)(c)->ks)
78
+ #elif NEH_KEY_TYPE == NEH_GENERATE
79
+ #define kd_adr(c) ((uint8_t*)(c)->ks + (c)->inf.b[0])
80
+ #elif NEH_KEY_TYPE == NEH_HYBRID
81
+ #define kd_adr(c) ((uint8_t*)(c)->ks + ((c)->inf.b[0] == 160 ? 160 : 0))
82
+ #else
83
+ #error no key type defined for VIA ACE
84
+ #endif
85
+
86
+ #else
87
+
88
+ #define aligned_array(type, name, no, stride) type name[no]
89
+ #define aligned_auto(type, name, no, stride) type name[no]
90
+
91
+ #endif
92
+
93
+ #if defined( _MSC_VER ) && _MSC_VER > 1200
94
+
95
+ #define via_cwd(cwd, ty, dir, len) \
96
+ unsigned long* cwd = (dir##_##ty##_table + ((len - 128) >> 4))
97
+
98
+ #else
99
+
100
+ #define via_cwd(cwd, ty, dir, len) \
101
+ aligned_auto(unsigned long, cwd, 4, 16); \
102
+ cwd[1] = cwd[2] = cwd[3] = 0; \
103
+ cwd[0] = neh_##dir##_##ty##_key(len)
104
+
105
+ #endif
106
+
107
+ /* test the code for detecting and setting pointer alignment */
108
+
109
+ AES_RETURN aes_test_alignment_detection(unsigned int n) /* 4 <= n <= 16 */
110
+ { uint8_t p[16];
111
+ uint32_t i, count_eq = 0, count_neq = 0;
112
+
113
+ if(n < 4 || n > 16)
114
+ return EXIT_FAILURE;
115
+
116
+ for(i = 0; i < n; ++i)
117
+ {
118
+ uint8_t *qf = ALIGN_FLOOR(p + i, n),
119
+ *qh = ALIGN_CEIL(p + i, n);
120
+
121
+ if(qh == qf)
122
+ ++count_eq;
123
+ else if(qh == qf + n)
124
+ ++count_neq;
125
+ else
126
+ return EXIT_FAILURE;
127
+ }
128
+ return (count_eq != 1 || count_neq != n - 1 ? EXIT_FAILURE : EXIT_SUCCESS);
129
+ }
130
+
131
+ AES_RETURN aes_mode_reset(aes_encrypt_ctx ctx[1])
132
+ {
133
+ ctx->inf.b[2] = 0;
134
+ return EXIT_SUCCESS;
135
+ }
136
+
137
+ AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
138
+ int len, const aes_encrypt_ctx ctx[1])
139
+ { int nb = len >> AES_BLOCK_SIZE_P2;
140
+
141
+ if(len & (AES_BLOCK_SIZE - 1))
142
+ return EXIT_FAILURE;
143
+
144
+ #if defined( USE_VIA_ACE_IF_PRESENT )
145
+
146
+ if(ctx->inf.b[1] == 0xff)
147
+ { uint8_t *ksp = (uint8_t*)(ctx->ks);
148
+ via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
149
+
150
+ if(ALIGN_OFFSET( ctx, 16 ))
151
+ return EXIT_FAILURE;
152
+
153
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))
154
+ {
155
+ via_ecb_op5(ksp, cwd, ibuf, obuf, nb);
156
+ }
157
+ else
158
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
159
+ uint8_t *ip, *op;
160
+
161
+ while(nb)
162
+ {
163
+ int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
164
+
165
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
166
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
167
+
168
+ if(ip != ibuf)
169
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
170
+
171
+ via_ecb_op5(ksp, cwd, ip, op, m);
172
+
173
+ if(op != obuf)
174
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
175
+
176
+ ibuf += m * AES_BLOCK_SIZE;
177
+ obuf += m * AES_BLOCK_SIZE;
178
+ nb -= m;
179
+ }
180
+ }
181
+
182
+ return EXIT_SUCCESS;
183
+ }
184
+
185
+ #endif
186
+
187
+ #if !defined( ASSUME_VIA_ACE_PRESENT )
188
+ while(nb--)
189
+ {
190
+ if(aes_encrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)
191
+ return EXIT_FAILURE;
192
+ ibuf += AES_BLOCK_SIZE;
193
+ obuf += AES_BLOCK_SIZE;
194
+ }
195
+ #endif
196
+ return EXIT_SUCCESS;
197
+ }
198
+
199
+ AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
200
+ int len, const aes_decrypt_ctx ctx[1])
201
+ { int nb = len >> AES_BLOCK_SIZE_P2;
202
+
203
+ if(len & (AES_BLOCK_SIZE - 1))
204
+ return EXIT_FAILURE;
205
+
206
+ #if defined( USE_VIA_ACE_IF_PRESENT )
207
+
208
+ if(ctx->inf.b[1] == 0xff)
209
+ { uint8_t *ksp = kd_adr(ctx);
210
+ via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);
211
+
212
+ if(ALIGN_OFFSET( ctx, 16 ))
213
+ return EXIT_FAILURE;
214
+
215
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))
216
+ {
217
+ via_ecb_op5(ksp, cwd, ibuf, obuf, nb);
218
+ }
219
+ else
220
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
221
+ uint8_t *ip, *op;
222
+
223
+ while(nb)
224
+ {
225
+ int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
226
+
227
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
228
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
229
+
230
+ if(ip != ibuf)
231
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
232
+
233
+ via_ecb_op5(ksp, cwd, ip, op, m);
234
+
235
+ if(op != obuf)
236
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
237
+
238
+ ibuf += m * AES_BLOCK_SIZE;
239
+ obuf += m * AES_BLOCK_SIZE;
240
+ nb -= m;
241
+ }
242
+ }
243
+
244
+ return EXIT_SUCCESS;
245
+ }
246
+
247
+ #endif
248
+
249
+ #if !defined( ASSUME_VIA_ACE_PRESENT )
250
+ while(nb--)
251
+ {
252
+ if(aes_decrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)
253
+ return EXIT_FAILURE;
254
+ ibuf += AES_BLOCK_SIZE;
255
+ obuf += AES_BLOCK_SIZE;
256
+ }
257
+ #endif
258
+ return EXIT_SUCCESS;
259
+ }
260
+
261
+ AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
262
+ int len, unsigned char *iv, const aes_encrypt_ctx ctx[1])
263
+ { int nb = len >> AES_BLOCK_SIZE_P2;
264
+
265
+ if(len & (AES_BLOCK_SIZE - 1))
266
+ return EXIT_FAILURE;
267
+
268
+ #if defined( USE_VIA_ACE_IF_PRESENT )
269
+
270
+ if(ctx->inf.b[1] == 0xff)
271
+ { uint8_t *ksp = (uint8_t*)(ctx->ks), *ivp = iv;
272
+ aligned_auto(uint8_t, liv, AES_BLOCK_SIZE, 16);
273
+ via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
274
+
275
+ if(ALIGN_OFFSET( ctx, 16 ))
276
+ return EXIT_FAILURE;
277
+
278
+ if(ALIGN_OFFSET( iv, 16 )) /* ensure an aligned iv */
279
+ {
280
+ ivp = liv;
281
+ memcpy(liv, iv, AES_BLOCK_SIZE);
282
+ }
283
+
284
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ) && !ALIGN_OFFSET( iv, 16 ))
285
+ {
286
+ via_cbc_op7(ksp, cwd, ibuf, obuf, nb, ivp, ivp);
287
+ }
288
+ else
289
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
290
+ uint8_t *ip, *op;
291
+
292
+ while(nb)
293
+ {
294
+ int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
295
+
296
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
297
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
298
+
299
+ if(ip != ibuf)
300
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
301
+
302
+ via_cbc_op7(ksp, cwd, ip, op, m, ivp, ivp);
303
+
304
+ if(op != obuf)
305
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
306
+
307
+ ibuf += m * AES_BLOCK_SIZE;
308
+ obuf += m * AES_BLOCK_SIZE;
309
+ nb -= m;
310
+ }
311
+ }
312
+
313
+ if(iv != ivp)
314
+ memcpy(iv, ivp, AES_BLOCK_SIZE);
315
+
316
+ return EXIT_SUCCESS;
317
+ }
318
+
319
+ #endif
320
+
321
+ #if !defined( ASSUME_VIA_ACE_PRESENT )
322
+ # ifdef FAST_BUFFER_OPERATIONS
323
+ if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))
324
+ while(nb--)
325
+ {
326
+ lp32(iv)[0] ^= lp32(ibuf)[0];
327
+ lp32(iv)[1] ^= lp32(ibuf)[1];
328
+ lp32(iv)[2] ^= lp32(ibuf)[2];
329
+ lp32(iv)[3] ^= lp32(ibuf)[3];
330
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
331
+ return EXIT_FAILURE;
332
+ memcpy(obuf, iv, AES_BLOCK_SIZE);
333
+ ibuf += AES_BLOCK_SIZE;
334
+ obuf += AES_BLOCK_SIZE;
335
+ }
336
+ else
337
+ # endif
338
+ while(nb--)
339
+ {
340
+ iv[ 0] ^= ibuf[ 0]; iv[ 1] ^= ibuf[ 1];
341
+ iv[ 2] ^= ibuf[ 2]; iv[ 3] ^= ibuf[ 3];
342
+ iv[ 4] ^= ibuf[ 4]; iv[ 5] ^= ibuf[ 5];
343
+ iv[ 6] ^= ibuf[ 6]; iv[ 7] ^= ibuf[ 7];
344
+ iv[ 8] ^= ibuf[ 8]; iv[ 9] ^= ibuf[ 9];
345
+ iv[10] ^= ibuf[10]; iv[11] ^= ibuf[11];
346
+ iv[12] ^= ibuf[12]; iv[13] ^= ibuf[13];
347
+ iv[14] ^= ibuf[14]; iv[15] ^= ibuf[15];
348
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
349
+ return EXIT_FAILURE;
350
+ memcpy(obuf, iv, AES_BLOCK_SIZE);
351
+ ibuf += AES_BLOCK_SIZE;
352
+ obuf += AES_BLOCK_SIZE;
353
+ }
354
+ #endif
355
+ return EXIT_SUCCESS;
356
+ }
357
+
358
+ AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
359
+ int len, unsigned char *iv, const aes_decrypt_ctx ctx[1])
360
+ { unsigned char tmp[AES_BLOCK_SIZE];
361
+ int nb = len >> AES_BLOCK_SIZE_P2;
362
+
363
+ if(len & (AES_BLOCK_SIZE - 1))
364
+ return EXIT_FAILURE;
365
+
366
+ #if defined( USE_VIA_ACE_IF_PRESENT )
367
+
368
+ if(ctx->inf.b[1] == 0xff)
369
+ { uint8_t *ksp = kd_adr(ctx), *ivp = iv;
370
+ aligned_auto(uint8_t, liv, AES_BLOCK_SIZE, 16);
371
+ via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);
372
+
373
+ if(ALIGN_OFFSET( ctx, 16 ))
374
+ return EXIT_FAILURE;
375
+
376
+ if(ALIGN_OFFSET( iv, 16 )) /* ensure an aligned iv */
377
+ {
378
+ ivp = liv;
379
+ memcpy(liv, iv, AES_BLOCK_SIZE);
380
+ }
381
+
382
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ) && !ALIGN_OFFSET( iv, 16 ))
383
+ {
384
+ via_cbc_op6(ksp, cwd, ibuf, obuf, nb, ivp);
385
+ }
386
+ else
387
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
388
+ uint8_t *ip, *op;
389
+
390
+ while(nb)
391
+ {
392
+ int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
393
+
394
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
395
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
396
+
397
+ if(ip != ibuf)
398
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
399
+
400
+ via_cbc_op6(ksp, cwd, ip, op, m, ivp);
401
+
402
+ if(op != obuf)
403
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
404
+
405
+ ibuf += m * AES_BLOCK_SIZE;
406
+ obuf += m * AES_BLOCK_SIZE;
407
+ nb -= m;
408
+ }
409
+ }
410
+
411
+ if(iv != ivp)
412
+ memcpy(iv, ivp, AES_BLOCK_SIZE);
413
+
414
+ return EXIT_SUCCESS;
415
+ }
416
+ #endif
417
+
418
+ #if !defined( ASSUME_VIA_ACE_PRESENT )
419
+ # ifdef FAST_BUFFER_OPERATIONS
420
+ if(!ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))
421
+ while(nb--)
422
+ {
423
+ memcpy(tmp, ibuf, AES_BLOCK_SIZE);
424
+ if(aes_decrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)
425
+ return EXIT_FAILURE;
426
+ lp32(obuf)[0] ^= lp32(iv)[0];
427
+ lp32(obuf)[1] ^= lp32(iv)[1];
428
+ lp32(obuf)[2] ^= lp32(iv)[2];
429
+ lp32(obuf)[3] ^= lp32(iv)[3];
430
+ memcpy(iv, tmp, AES_BLOCK_SIZE);
431
+ ibuf += AES_BLOCK_SIZE;
432
+ obuf += AES_BLOCK_SIZE;
433
+ }
434
+ else
435
+ # endif
436
+ while(nb--)
437
+ {
438
+ memcpy(tmp, ibuf, AES_BLOCK_SIZE);
439
+ if(aes_decrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)
440
+ return EXIT_FAILURE;
441
+ obuf[ 0] ^= iv[ 0]; obuf[ 1] ^= iv[ 1];
442
+ obuf[ 2] ^= iv[ 2]; obuf[ 3] ^= iv[ 3];
443
+ obuf[ 4] ^= iv[ 4]; obuf[ 5] ^= iv[ 5];
444
+ obuf[ 6] ^= iv[ 6]; obuf[ 7] ^= iv[ 7];
445
+ obuf[ 8] ^= iv[ 8]; obuf[ 9] ^= iv[ 9];
446
+ obuf[10] ^= iv[10]; obuf[11] ^= iv[11];
447
+ obuf[12] ^= iv[12]; obuf[13] ^= iv[13];
448
+ obuf[14] ^= iv[14]; obuf[15] ^= iv[15];
449
+ memcpy(iv, tmp, AES_BLOCK_SIZE);
450
+ ibuf += AES_BLOCK_SIZE;
451
+ obuf += AES_BLOCK_SIZE;
452
+ }
453
+ #endif
454
+ return EXIT_SUCCESS;
455
+ }
456
+
457
+ AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
458
+ int len, unsigned char *iv, aes_encrypt_ctx ctx[1])
459
+ { int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;
460
+
461
+ if(b_pos) /* complete any partial block */
462
+ {
463
+ while(b_pos < AES_BLOCK_SIZE && cnt < len)
464
+ {
465
+ *obuf++ = (iv[b_pos++] ^= *ibuf++);
466
+ cnt++;
467
+ }
468
+
469
+ b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
470
+ }
471
+
472
+ if((nb = (len - cnt) >> AES_BLOCK_SIZE_P2) != 0) /* process whole blocks */
473
+ {
474
+ #if defined( USE_VIA_ACE_IF_PRESENT )
475
+
476
+ if(ctx->inf.b[1] == 0xff)
477
+ { int m;
478
+ uint8_t *ksp = (uint8_t*)(ctx->ks), *ivp = iv;
479
+ aligned_auto(uint8_t, liv, AES_BLOCK_SIZE, 16);
480
+ via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
481
+
482
+ if(ALIGN_OFFSET( ctx, 16 ))
483
+ return EXIT_FAILURE;
484
+
485
+ if(ALIGN_OFFSET( iv, 16 )) /* ensure an aligned iv */
486
+ {
487
+ ivp = liv;
488
+ memcpy(liv, iv, AES_BLOCK_SIZE);
489
+ }
490
+
491
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))
492
+ {
493
+ via_cfb_op7(ksp, cwd, ibuf, obuf, nb, ivp, ivp);
494
+ ibuf += nb * AES_BLOCK_SIZE;
495
+ obuf += nb * AES_BLOCK_SIZE;
496
+ cnt += nb * AES_BLOCK_SIZE;
497
+ }
498
+ else /* input, output or both are unaligned */
499
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
500
+ uint8_t *ip, *op;
501
+
502
+ while(nb)
503
+ {
504
+ m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
505
+
506
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
507
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
508
+
509
+ if(ip != ibuf)
510
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
511
+
512
+ via_cfb_op7(ksp, cwd, ip, op, m, ivp, ivp);
513
+
514
+ if(op != obuf)
515
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
516
+
517
+ ibuf += m * AES_BLOCK_SIZE;
518
+ obuf += m * AES_BLOCK_SIZE;
519
+ cnt += m * AES_BLOCK_SIZE;
520
+ }
521
+ }
522
+
523
+ if(ivp != iv)
524
+ memcpy(iv, ivp, AES_BLOCK_SIZE);
525
+ }
526
+ #else
527
+ # ifdef FAST_BUFFER_OPERATIONS
528
+ if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))
529
+ while(cnt + AES_BLOCK_SIZE <= len)
530
+ {
531
+ assert(b_pos == 0);
532
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
533
+ return EXIT_FAILURE;
534
+ lp32(obuf)[0] = lp32(iv)[0] ^= lp32(ibuf)[0];
535
+ lp32(obuf)[1] = lp32(iv)[1] ^= lp32(ibuf)[1];
536
+ lp32(obuf)[2] = lp32(iv)[2] ^= lp32(ibuf)[2];
537
+ lp32(obuf)[3] = lp32(iv)[3] ^= lp32(ibuf)[3];
538
+ ibuf += AES_BLOCK_SIZE;
539
+ obuf += AES_BLOCK_SIZE;
540
+ cnt += AES_BLOCK_SIZE;
541
+ }
542
+ else
543
+ # endif
544
+ while(cnt + AES_BLOCK_SIZE <= len)
545
+ {
546
+ assert(b_pos == 0);
547
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
548
+ return EXIT_FAILURE;
549
+ obuf[ 0] = iv[ 0] ^= ibuf[ 0]; obuf[ 1] = iv[ 1] ^= ibuf[ 1];
550
+ obuf[ 2] = iv[ 2] ^= ibuf[ 2]; obuf[ 3] = iv[ 3] ^= ibuf[ 3];
551
+ obuf[ 4] = iv[ 4] ^= ibuf[ 4]; obuf[ 5] = iv[ 5] ^= ibuf[ 5];
552
+ obuf[ 6] = iv[ 6] ^= ibuf[ 6]; obuf[ 7] = iv[ 7] ^= ibuf[ 7];
553
+ obuf[ 8] = iv[ 8] ^= ibuf[ 8]; obuf[ 9] = iv[ 9] ^= ibuf[ 9];
554
+ obuf[10] = iv[10] ^= ibuf[10]; obuf[11] = iv[11] ^= ibuf[11];
555
+ obuf[12] = iv[12] ^= ibuf[12]; obuf[13] = iv[13] ^= ibuf[13];
556
+ obuf[14] = iv[14] ^= ibuf[14]; obuf[15] = iv[15] ^= ibuf[15];
557
+ ibuf += AES_BLOCK_SIZE;
558
+ obuf += AES_BLOCK_SIZE;
559
+ cnt += AES_BLOCK_SIZE;
560
+ }
561
+ #endif
562
+ }
563
+
564
+ while(cnt < len)
565
+ {
566
+ if(!b_pos && aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
567
+ return EXIT_FAILURE;
568
+
569
+ while(cnt < len && b_pos < AES_BLOCK_SIZE)
570
+ {
571
+ *obuf++ = (iv[b_pos++] ^= *ibuf++);
572
+ cnt++;
573
+ }
574
+
575
+ b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
576
+ }
577
+
578
+ ctx->inf.b[2] = (uint8_t)b_pos;
579
+ return EXIT_SUCCESS;
580
+ }
581
+
582
+ AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
583
+ int len, unsigned char *iv, aes_encrypt_ctx ctx[1])
584
+ { int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;
585
+
586
+ if(b_pos) /* complete any partial block */
587
+ { uint8_t t;
588
+
589
+ while(b_pos < AES_BLOCK_SIZE && cnt < len)
590
+ {
591
+ t = *ibuf++;
592
+ *obuf++ = t ^ iv[b_pos];
593
+ iv[b_pos++] = t;
594
+ cnt++;
595
+ }
596
+
597
+ b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
598
+ }
599
+
600
+ if((nb = (len - cnt) >> AES_BLOCK_SIZE_P2) != 0) /* process whole blocks */
601
+ {
602
+ #if defined( USE_VIA_ACE_IF_PRESENT )
603
+
604
+ if(ctx->inf.b[1] == 0xff)
605
+ { int m;
606
+ uint8_t *ksp = (uint8_t*)(ctx->ks), *ivp = iv;
607
+ aligned_auto(uint8_t, liv, AES_BLOCK_SIZE, 16);
608
+ via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);
609
+
610
+ if(ALIGN_OFFSET( ctx, 16 ))
611
+ return EXIT_FAILURE;
612
+
613
+ if(ALIGN_OFFSET( iv, 16 )) /* ensure an aligned iv */
614
+ {
615
+ ivp = liv;
616
+ memcpy(liv, iv, AES_BLOCK_SIZE);
617
+ }
618
+
619
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))
620
+ {
621
+ via_cfb_op6(ksp, cwd, ibuf, obuf, nb, ivp);
622
+ ibuf += nb * AES_BLOCK_SIZE;
623
+ obuf += nb * AES_BLOCK_SIZE;
624
+ cnt += nb * AES_BLOCK_SIZE;
625
+ }
626
+ else /* input, output or both are unaligned */
627
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
628
+ uint8_t *ip, *op;
629
+
630
+ while(nb)
631
+ {
632
+ m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
633
+
634
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
635
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
636
+
637
+ if(ip != ibuf) /* input buffer is not aligned */
638
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
639
+
640
+ via_cfb_op6(ksp, cwd, ip, op, m, ivp);
641
+
642
+ if(op != obuf) /* output buffer is not aligned */
643
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
644
+
645
+ ibuf += m * AES_BLOCK_SIZE;
646
+ obuf += m * AES_BLOCK_SIZE;
647
+ cnt += m * AES_BLOCK_SIZE;
648
+ }
649
+ }
650
+
651
+ if(ivp != iv)
652
+ memcpy(iv, ivp, AES_BLOCK_SIZE);
653
+ }
654
+ #else
655
+ # ifdef FAST_BUFFER_OPERATIONS
656
+ if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) &&!ALIGN_OFFSET( iv, 4 ))
657
+ while(cnt + AES_BLOCK_SIZE <= len)
658
+ { uint32_t t;
659
+
660
+ assert(b_pos == 0);
661
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
662
+ return EXIT_FAILURE;
663
+ t = lp32(ibuf)[0]; lp32(obuf)[0] = t ^ lp32(iv)[0]; lp32(iv)[0] = t;
664
+ t = lp32(ibuf)[1]; lp32(obuf)[1] = t ^ lp32(iv)[1]; lp32(iv)[1] = t;
665
+ t = lp32(ibuf)[2]; lp32(obuf)[2] = t ^ lp32(iv)[2]; lp32(iv)[2] = t;
666
+ t = lp32(ibuf)[3]; lp32(obuf)[3] = t ^ lp32(iv)[3]; lp32(iv)[3] = t;
667
+ ibuf += AES_BLOCK_SIZE;
668
+ obuf += AES_BLOCK_SIZE;
669
+ cnt += AES_BLOCK_SIZE;
670
+ }
671
+ else
672
+ # endif
673
+ while(cnt + AES_BLOCK_SIZE <= len)
674
+ { uint8_t t;
675
+
676
+ assert(b_pos == 0);
677
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
678
+ return EXIT_FAILURE;
679
+ t = ibuf[ 0]; obuf[ 0] = t ^ iv[ 0]; iv[ 0] = t;
680
+ t = ibuf[ 1]; obuf[ 1] = t ^ iv[ 1]; iv[ 1] = t;
681
+ t = ibuf[ 2]; obuf[ 2] = t ^ iv[ 2]; iv[ 2] = t;
682
+ t = ibuf[ 3]; obuf[ 3] = t ^ iv[ 3]; iv[ 3] = t;
683
+ t = ibuf[ 4]; obuf[ 4] = t ^ iv[ 4]; iv[ 4] = t;
684
+ t = ibuf[ 5]; obuf[ 5] = t ^ iv[ 5]; iv[ 5] = t;
685
+ t = ibuf[ 6]; obuf[ 6] = t ^ iv[ 6]; iv[ 6] = t;
686
+ t = ibuf[ 7]; obuf[ 7] = t ^ iv[ 7]; iv[ 7] = t;
687
+ t = ibuf[ 8]; obuf[ 8] = t ^ iv[ 8]; iv[ 8] = t;
688
+ t = ibuf[ 9]; obuf[ 9] = t ^ iv[ 9]; iv[ 9] = t;
689
+ t = ibuf[10]; obuf[10] = t ^ iv[10]; iv[10] = t;
690
+ t = ibuf[11]; obuf[11] = t ^ iv[11]; iv[11] = t;
691
+ t = ibuf[12]; obuf[12] = t ^ iv[12]; iv[12] = t;
692
+ t = ibuf[13]; obuf[13] = t ^ iv[13]; iv[13] = t;
693
+ t = ibuf[14]; obuf[14] = t ^ iv[14]; iv[14] = t;
694
+ t = ibuf[15]; obuf[15] = t ^ iv[15]; iv[15] = t;
695
+ ibuf += AES_BLOCK_SIZE;
696
+ obuf += AES_BLOCK_SIZE;
697
+ cnt += AES_BLOCK_SIZE;
698
+ }
699
+ #endif
700
+ }
701
+
702
+ while(cnt < len)
703
+ { uint8_t t;
704
+
705
+ if(!b_pos && aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
706
+ return EXIT_FAILURE;
707
+
708
+ while(cnt < len && b_pos < AES_BLOCK_SIZE)
709
+ {
710
+ t = *ibuf++;
711
+ *obuf++ = t ^ iv[b_pos];
712
+ iv[b_pos++] = t;
713
+ cnt++;
714
+ }
715
+
716
+ b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
717
+ }
718
+
719
+ ctx->inf.b[2] = (uint8_t)b_pos;
720
+ return EXIT_SUCCESS;
721
+ }
722
+
723
+ AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
724
+ int len, unsigned char *iv, aes_encrypt_ctx ctx[1])
725
+ { int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;
726
+
727
+ if(b_pos) /* complete any partial block */
728
+ {
729
+ while(b_pos < AES_BLOCK_SIZE && cnt < len)
730
+ {
731
+ *obuf++ = iv[b_pos++] ^ *ibuf++;
732
+ cnt++;
733
+ }
734
+
735
+ b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
736
+ }
737
+
738
+ if((nb = (len - cnt) >> AES_BLOCK_SIZE_P2) != 0) /* process whole blocks */
739
+ {
740
+ #if defined( USE_VIA_ACE_IF_PRESENT )
741
+
742
+ if(ctx->inf.b[1] == 0xff)
743
+ { int m;
744
+ uint8_t *ksp = (uint8_t*)(ctx->ks), *ivp = iv;
745
+ aligned_auto(uint8_t, liv, AES_BLOCK_SIZE, 16);
746
+ via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
747
+
748
+ if(ALIGN_OFFSET( ctx, 16 ))
749
+ return EXIT_FAILURE;
750
+
751
+ if(ALIGN_OFFSET( iv, 16 )) /* ensure an aligned iv */
752
+ {
753
+ ivp = liv;
754
+ memcpy(liv, iv, AES_BLOCK_SIZE);
755
+ }
756
+
757
+ if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))
758
+ {
759
+ via_ofb_op6(ksp, cwd, ibuf, obuf, nb, ivp);
760
+ ibuf += nb * AES_BLOCK_SIZE;
761
+ obuf += nb * AES_BLOCK_SIZE;
762
+ cnt += nb * AES_BLOCK_SIZE;
763
+ }
764
+ else /* input, output or both are unaligned */
765
+ { aligned_auto(uint8_t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);
766
+ uint8_t *ip, *op;
767
+
768
+ while(nb)
769
+ {
770
+ m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
771
+
772
+ ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);
773
+ op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);
774
+
775
+ if(ip != ibuf)
776
+ memcpy(buf, ibuf, m * AES_BLOCK_SIZE);
777
+
778
+ via_ofb_op6(ksp, cwd, ip, op, m, ivp);
779
+
780
+ if(op != obuf)
781
+ memcpy(obuf, buf, m * AES_BLOCK_SIZE);
782
+
783
+ ibuf += m * AES_BLOCK_SIZE;
784
+ obuf += m * AES_BLOCK_SIZE;
785
+ cnt += m * AES_BLOCK_SIZE;
786
+ }
787
+ }
788
+
789
+ if(ivp != iv)
790
+ memcpy(iv, ivp, AES_BLOCK_SIZE);
791
+ }
792
+ #else
793
+ # ifdef FAST_BUFFER_OPERATIONS
794
+ if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))
795
+ while(cnt + AES_BLOCK_SIZE <= len)
796
+ {
797
+ assert(b_pos == 0);
798
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
799
+ return EXIT_FAILURE;
800
+ lp32(obuf)[0] = lp32(iv)[0] ^ lp32(ibuf)[0];
801
+ lp32(obuf)[1] = lp32(iv)[1] ^ lp32(ibuf)[1];
802
+ lp32(obuf)[2] = lp32(iv)[2] ^ lp32(ibuf)[2];
803
+ lp32(obuf)[3] = lp32(iv)[3] ^ lp32(ibuf)[3];
804
+ ibuf += AES_BLOCK_SIZE;
805
+ obuf += AES_BLOCK_SIZE;
806
+ cnt += AES_BLOCK_SIZE;
807
+ }
808
+ else
809
+ # endif
810
+ while(cnt + AES_BLOCK_SIZE <= len)
811
+ {
812
+ assert(b_pos == 0);
813
+ if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
814
+ return EXIT_FAILURE;
815
+ obuf[ 0] = iv[ 0] ^ ibuf[ 0]; obuf[ 1] = iv[ 1] ^ ibuf[ 1];
816
+ obuf[ 2] = iv[ 2] ^ ibuf[ 2]; obuf[ 3] = iv[ 3] ^ ibuf[ 3];
817
+ obuf[ 4] = iv[ 4] ^ ibuf[ 4]; obuf[ 5] = iv[ 5] ^ ibuf[ 5];
818
+ obuf[ 6] = iv[ 6] ^ ibuf[ 6]; obuf[ 7] = iv[ 7] ^ ibuf[ 7];
819
+ obuf[ 8] = iv[ 8] ^ ibuf[ 8]; obuf[ 9] = iv[ 9] ^ ibuf[ 9];
820
+ obuf[10] = iv[10] ^ ibuf[10]; obuf[11] = iv[11] ^ ibuf[11];
821
+ obuf[12] = iv[12] ^ ibuf[12]; obuf[13] = iv[13] ^ ibuf[13];
822
+ obuf[14] = iv[14] ^ ibuf[14]; obuf[15] = iv[15] ^ ibuf[15];
823
+ ibuf += AES_BLOCK_SIZE;
824
+ obuf += AES_BLOCK_SIZE;
825
+ cnt += AES_BLOCK_SIZE;
826
+ }
827
+ #endif
828
+ }
829
+
830
+ while(cnt < len)
831
+ {
832
+ if(!b_pos && aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
833
+ return EXIT_FAILURE;
834
+
835
+ while(cnt < len && b_pos < AES_BLOCK_SIZE)
836
+ {
837
+ *obuf++ = iv[b_pos++] ^ *ibuf++;
838
+ cnt++;
839
+ }
840
+
841
+ b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);
842
+ }
843
+
844
+ ctx->inf.b[2] = (uint8_t)b_pos;
845
+ return EXIT_SUCCESS;
846
+ }
847
+
848
+ #define BFR_LENGTH (BFR_BLOCKS * AES_BLOCK_SIZE)
849
+
850
+ AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
851
+ int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx ctx[1])
852
+ { unsigned char *ip;
853
+ int i, blen, b_pos = (int)(ctx->inf.b[2]);
854
+
855
+ #if defined( USE_VIA_ACE_IF_PRESENT )
856
+ aligned_auto(uint8_t, buf, BFR_LENGTH, 16);
857
+ if(ctx->inf.b[1] == 0xff && ALIGN_OFFSET( ctx, 16 ))
858
+ return EXIT_FAILURE;
859
+ #else
860
+ uint8_t buf[BFR_LENGTH];
861
+ #endif
862
+
863
+ if(b_pos)
864
+ {
865
+ memcpy(buf, cbuf, AES_BLOCK_SIZE);
866
+ if(aes_ecb_encrypt(buf, buf, AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)
867
+ return EXIT_FAILURE;
868
+
869
+ while(b_pos < AES_BLOCK_SIZE && len)
870
+ {
871
+ *obuf++ = *ibuf++ ^ buf[b_pos++];
872
+ --len;
873
+ }
874
+
875
+ if(len) {
876
+ ctr_inc(cbuf); b_pos = 0;
877
+ }
878
+ }
879
+
880
+ while(len)
881
+ {
882
+ blen = (len > BFR_LENGTH ? BFR_LENGTH : len); len -= blen;
883
+
884
+ for(i = 0, ip = buf; i < (blen >> AES_BLOCK_SIZE_P2); ++i)
885
+ {
886
+ memcpy(ip, cbuf, AES_BLOCK_SIZE);
887
+ ctr_inc(cbuf);
888
+ ip += AES_BLOCK_SIZE;
889
+ }
890
+
891
+ if(blen & (AES_BLOCK_SIZE - 1)) {
892
+ memcpy(ip, cbuf, AES_BLOCK_SIZE); i++;
893
+ }
894
+
895
+ #if defined( USE_VIA_ACE_IF_PRESENT )
896
+ if(ctx->inf.b[1] == 0xff)
897
+ {
898
+ via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);
899
+ via_ecb_op5((ctx->ks), cwd, buf, buf, i);
900
+ }
901
+ else
902
+ #endif
903
+ if(aes_ecb_encrypt(buf, buf, i * AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)
904
+ return EXIT_FAILURE;
905
+
906
+ i = 0; ip = buf;
907
+ # ifdef FAST_BUFFER_OPERATIONS
908
+ if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( ip, 4 ))
909
+ while(i + AES_BLOCK_SIZE <= blen)
910
+ {
911
+ lp32(obuf)[0] = lp32(ibuf)[0] ^ lp32(ip)[0];
912
+ lp32(obuf)[1] = lp32(ibuf)[1] ^ lp32(ip)[1];
913
+ lp32(obuf)[2] = lp32(ibuf)[2] ^ lp32(ip)[2];
914
+ lp32(obuf)[3] = lp32(ibuf)[3] ^ lp32(ip)[3];
915
+ i += AES_BLOCK_SIZE;
916
+ ip += AES_BLOCK_SIZE;
917
+ ibuf += AES_BLOCK_SIZE;
918
+ obuf += AES_BLOCK_SIZE;
919
+ }
920
+ else
921
+ #endif
922
+ while(i + AES_BLOCK_SIZE <= blen)
923
+ {
924
+ obuf[ 0] = ibuf[ 0] ^ ip[ 0]; obuf[ 1] = ibuf[ 1] ^ ip[ 1];
925
+ obuf[ 2] = ibuf[ 2] ^ ip[ 2]; obuf[ 3] = ibuf[ 3] ^ ip[ 3];
926
+ obuf[ 4] = ibuf[ 4] ^ ip[ 4]; obuf[ 5] = ibuf[ 5] ^ ip[ 5];
927
+ obuf[ 6] = ibuf[ 6] ^ ip[ 6]; obuf[ 7] = ibuf[ 7] ^ ip[ 7];
928
+ obuf[ 8] = ibuf[ 8] ^ ip[ 8]; obuf[ 9] = ibuf[ 9] ^ ip[ 9];
929
+ obuf[10] = ibuf[10] ^ ip[10]; obuf[11] = ibuf[11] ^ ip[11];
930
+ obuf[12] = ibuf[12] ^ ip[12]; obuf[13] = ibuf[13] ^ ip[13];
931
+ obuf[14] = ibuf[14] ^ ip[14]; obuf[15] = ibuf[15] ^ ip[15];
932
+ i += AES_BLOCK_SIZE;
933
+ ip += AES_BLOCK_SIZE;
934
+ ibuf += AES_BLOCK_SIZE;
935
+ obuf += AES_BLOCK_SIZE;
936
+ }
937
+
938
+ while(i++ < blen)
939
+ *obuf++ = *ibuf++ ^ ip[b_pos++];
940
+ }
941
+
942
+ ctx->inf.b[2] = (uint8_t)b_pos;
943
+ return EXIT_SUCCESS;
944
+ }
945
+
946
+ void aes_ctr_cbuf_inc(unsigned char *cbuf)
947
+ {
948
+ int i = AES_BLOCK_SIZE - 1;
949
+ while (i >= 0) {
950
+ cbuf[i]++;
951
+ if (cbuf[i]) return; // if there was no overflow
952
+ i--;
953
+ }
954
+ }
955
+
956
+ #if defined(__cplusplus)
957
+ }
958
+ #endif
959
+ #endif