@onekeyfe/react-native-lite-card 6.7.0-20 → 6.7.0-22

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 (227) 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/ReactNativeLiteCardModule.kt +29 -11
  6. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +14 -3
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
  9. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +5 -1
  10. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +36 -17
  11. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
  12. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +4 -1
  13. package/ios/Classes/OKNFCBridge.mm +1 -1
  14. package/ios/Classes/OKNFTLite/OKLiteV1.m +6 -3
  15. package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
  16. package/ios/Classes/OKNFTLite/OKNFCManager.m +26 -6
  17. package/ios/ReactNativeLiteCard.mm +16 -7
  18. package/package.json +8 -8
  19. package/sdk/CMakeLists.txt +120 -0
  20. package/sdk/README.md +175 -0
  21. package/sdk/android/implJni.cpp +177 -0
  22. package/sdk/deps/jsoncpp/LICENSE +55 -0
  23. package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
  24. package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
  25. package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
  26. package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
  27. package/sdk/deps/jsoncpp/include/json/config.h +187 -0
  28. package/sdk/deps/jsoncpp/include/json/features.h +61 -0
  29. package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
  30. package/sdk/deps/jsoncpp/include/json/json.h +15 -0
  31. package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
  32. package/sdk/deps/jsoncpp/include/json/value.h +888 -0
  33. package/sdk/deps/jsoncpp/include/json/version.h +20 -0
  34. package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
  35. package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
  36. package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
  37. package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
  38. package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
  39. package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
  40. package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
  41. package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
  42. package/sdk/deps/trezor-crypto/AUTHORS +2 -0
  43. package/sdk/deps/trezor-crypto/LICENSE +22 -0
  44. package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
  45. package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
  46. package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
  47. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
  48. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
  49. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
  50. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
  51. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
  52. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
  53. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
  54. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
  55. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
  56. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
  57. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
  58. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
  59. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
  60. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
  61. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
  62. package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
  63. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
  64. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
  65. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
  66. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
  67. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
  68. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
  69. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
  70. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
  71. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
  72. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
  73. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
  74. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
  75. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
  76. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
  77. package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
  78. package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
  79. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
  80. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
  81. package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
  82. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
  83. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
  84. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
  85. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
  86. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
  87. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
  88. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
  89. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
  90. package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
  91. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
  92. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
  93. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
  94. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
  95. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
  96. package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
  97. package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
  98. package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
  99. package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
  100. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
  101. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
  102. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
  103. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
  104. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
  105. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
  106. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
  107. package/sdk/deps/trezor-crypto/src/address.c +87 -0
  108. package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
  109. package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
  110. package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
  111. package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
  112. package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
  113. package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
  114. package/sdk/deps/trezor-crypto/src/base32.c +233 -0
  115. package/sdk/deps/trezor-crypto/src/base58.c +298 -0
  116. package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
  117. package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
  118. package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
  119. package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
  120. package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
  121. package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
  122. package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
  123. package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
  124. package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
  125. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
  126. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
  127. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
  128. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
  129. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
  130. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
  131. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
  132. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
  133. package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
  134. package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
  135. package/sdk/deps/trezor-crypto/src/curves.c +48 -0
  136. package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
  137. package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
  138. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
  139. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
  140. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
  141. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
  142. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
  143. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
  144. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
  145. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
  146. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
  147. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
  148. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
  149. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
  150. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
  151. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
  152. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
  153. package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
  154. package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
  155. package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
  156. package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
  157. package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
  158. package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
  159. package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
  160. package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
  161. package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
  162. package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
  163. package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
  164. package/sdk/deps/trezor-crypto/src/nano.c +151 -0
  165. package/sdk/deps/trezor-crypto/src/nem.c +569 -0
  166. package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
  167. package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
  168. package/sdk/deps/trezor-crypto/src/options.h +43 -0
  169. package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
  170. package/sdk/deps/trezor-crypto/src/rand.c +51 -0
  171. package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
  172. package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
  173. package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
  174. package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
  175. package/sdk/deps/trezor-crypto/src/script.c +64 -0
  176. package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
  177. package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
  178. package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
  179. package/sdk/deps/trezor-crypto/src/setup.py +38 -0
  180. package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
  181. package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
  182. package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
  183. package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
  184. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
  185. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
  186. package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
  187. package/sdk/gpc/include/scp03/scp03.hpp +80 -0
  188. package/sdk/gpc/include/scp11/scp11.hpp +492 -0
  189. package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
  190. package/sdk/gpc/include/tag.h +68 -0
  191. package/sdk/gpc/include/tlv.hpp +94 -0
  192. package/sdk/gpc/include/tlvparse.hpp +24 -0
  193. package/sdk/gpc/src/scp03.cpp +243 -0
  194. package/sdk/gpc/src/scp11.cpp +798 -0
  195. package/sdk/gpc/src/scp11c.cpp +136 -0
  196. package/sdk/gpc/src/tlv.cpp +159 -0
  197. package/sdk/gpc/src/tlvparse.c +174 -0
  198. package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
  199. package/sdk/include/GPChannelSDKCore.h +10 -0
  200. package/sdk/include/context/BaseContext.h +56 -0
  201. package/sdk/include/device/ApduBuilder.hpp +72 -0
  202. package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
  203. package/sdk/include/utility/Apdu.hpp +172 -0
  204. package/sdk/include/utility/Debug.hpp +3 -0
  205. package/sdk/include/utility/util.h +104 -0
  206. package/sdk/src/ApduBuilder.cpp +332 -0
  207. package/sdk/src/GPChannelSDK.cpp +252 -0
  208. package/sdk/src/random.c +25 -0
  209. package/sdk/src/version.c +2 -0
  210. package/sdk/tests/android/GPChannelProxy.java +55 -0
  211. package/sdk/tests/android_verify.py +121 -0
  212. package/sdk/tests/safety.cpp +603 -0
  213. package/sdk/tests/verify.py +267 -0
  214. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  215. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  216. package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
  217. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  218. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  219. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  220. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  221. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
  222. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  223. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
  224. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  225. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  226. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  227. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
@@ -0,0 +1,784 @@
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
+ This file contains the compilation options for AES (Rijndael) and code
21
+ that is common across encryption, key scheduling and table generation.
22
+
23
+ OPERATION
24
+
25
+ These source code files implement the AES algorithm Rijndael designed by
26
+ Joan Daemen and Vincent Rijmen. This version is designed for the standard
27
+ block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24
28
+ and 32 bytes).
29
+
30
+ This version is designed for flexibility and speed using operations on
31
+ 32-bit words rather than operations on bytes. It can be compiled with
32
+ either big or little endian internal byte order but is faster when the
33
+ native byte order for the processor is used.
34
+
35
+ THE CIPHER INTERFACE
36
+
37
+ The cipher interface is implemented as an array of bytes in which lower
38
+ AES bit sequence indexes map to higher numeric significance within bytes.
39
+
40
+ uint8_t (an unsigned 8-bit type)
41
+ uint32_t (an unsigned 32-bit type)
42
+ struct aes_encrypt_ctx (structure for the cipher encryption context)
43
+ struct aes_decrypt_ctx (structure for the cipher decryption context)
44
+ AES_RETURN the function return type
45
+
46
+ C subroutine calls:
47
+
48
+ AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
49
+ AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
50
+ AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
51
+ AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out,
52
+ const aes_encrypt_ctx cx[1]);
53
+
54
+ AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
55
+ AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
56
+ AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
57
+ AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out,
58
+ const aes_decrypt_ctx cx[1]);
59
+
60
+ IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that
61
+ you call aes_init() before AES is used so that the tables are initialised.
62
+
63
+ C++ aes class subroutines:
64
+
65
+ Class AESencrypt for encryption
66
+
67
+ Constructors:
68
+ AESencrypt(void)
69
+ AESencrypt(const unsigned char *key) - 128 bit key
70
+ Members:
71
+ AES_RETURN key128(const unsigned char *key)
72
+ AES_RETURN key192(const unsigned char *key)
73
+ AES_RETURN key256(const unsigned char *key)
74
+ AES_RETURN encrypt(const unsigned char *in, unsigned char *out) const
75
+
76
+ Class AESdecrypt for encryption
77
+ Constructors:
78
+ AESdecrypt(void)
79
+ AESdecrypt(const unsigned char *key) - 128 bit key
80
+ Members:
81
+ AES_RETURN key128(const unsigned char *key)
82
+ AES_RETURN key192(const unsigned char *key)
83
+ AES_RETURN key256(const unsigned char *key)
84
+ AES_RETURN decrypt(const unsigned char *in, unsigned char *out) const
85
+ */
86
+
87
+ #if !defined( _AESOPT_H )
88
+ #define _AESOPT_H
89
+
90
+ #if defined( __cplusplus )
91
+ #include "aescpp.h"
92
+ #else
93
+ #include <TrezorCrypto/aes.h>
94
+ #endif
95
+
96
+ /* PLATFORM SPECIFIC INCLUDES */
97
+
98
+ #define IS_BIG_ENDIAN 4321
99
+ #define IS_LITTLE_ENDIAN 1234
100
+ #define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
101
+
102
+ /* CONFIGURATION - THE USE OF DEFINES
103
+
104
+ Later in this section there are a number of defines that control the
105
+ operation of the code. In each section, the purpose of each define is
106
+ explained so that the relevant form can be included or excluded by
107
+ setting either 1's or 0's respectively on the branches of the related
108
+ #if clauses. The following local defines should not be changed.
109
+ */
110
+
111
+ #define ENCRYPTION_IN_C 1
112
+ #define DECRYPTION_IN_C 2
113
+ #define ENC_KEYING_IN_C 4
114
+ #define DEC_KEYING_IN_C 8
115
+
116
+ #define NO_TABLES 0
117
+ #define ONE_TABLE 1
118
+ #define FOUR_TABLES 4
119
+ #define NONE 0
120
+ #define PARTIAL 1
121
+ #define FULL 2
122
+
123
+ /* --- START OF USER CONFIGURED OPTIONS --- */
124
+
125
+ /* 1. BYTE ORDER WITHIN 32 BIT WORDS
126
+
127
+ The fundamental data processing units in Rijndael are 8-bit bytes. The
128
+ input, output and key input are all enumerated arrays of bytes in which
129
+ bytes are numbered starting at zero and increasing to one less than the
130
+ number of bytes in the array in question. This enumeration is only used
131
+ for naming bytes and does not imply any adjacency or order relationship
132
+ from one byte to another. When these inputs and outputs are considered
133
+ as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to
134
+ byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.
135
+ In this implementation bits are numbered from 0 to 7 starting at the
136
+ numerically least significant end of each byte (bit n represents 2^n).
137
+
138
+ However, Rijndael can be implemented more efficiently using 32-bit
139
+ words by packing bytes into words so that bytes 4*n to 4*n+3 are placed
140
+ into word[n]. While in principle these bytes can be assembled into words
141
+ in any positions, this implementation only supports the two formats in
142
+ which bytes in adjacent positions within words also have adjacent byte
143
+ numbers. This order is called big-endian if the lowest numbered bytes
144
+ in words have the highest numeric significance and little-endian if the
145
+ opposite applies.
146
+
147
+ This code can work in either order irrespective of the order used by the
148
+ machine on which it runs. Normally the internal byte order will be set
149
+ to the order of the processor on which the code is to be run but this
150
+ define can be used to reverse this in special situations
151
+
152
+ WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set.
153
+ This define will hence be redefined later (in section 4) if necessary
154
+ */
155
+
156
+ #if 1
157
+ # define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
158
+ #elif 0
159
+ # define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN
160
+ #elif 0
161
+ # define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN
162
+ #else
163
+ # error The algorithm byte order is not defined
164
+ #endif
165
+
166
+ /* 2. Intel AES AND VIA ACE SUPPORT */
167
+
168
+ #if defined( __GNUC__ ) && defined( __i386__ ) && !defined(__BEOS__) \
169
+ || defined( _WIN32 ) && defined( _M_IX86 ) && !(defined( _WIN64 ) \
170
+ || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 ))
171
+ # define VIA_ACE_POSSIBLE
172
+ #endif
173
+
174
+ /* AESNI is supported by all Windows x64 compilers, but for Linux/GCC
175
+ we have to test for SSE 2, SSE 3, and AES to before enabling it; */
176
+ #if !defined( INTEL_AES_POSSIBLE )
177
+ # if defined( _WIN64 ) && defined( _MSC_VER ) \
178
+ || defined( __GNUC__ ) && defined( __x86_64__ ) && \
179
+ defined( __SSE2__ ) && defined( __SSE3__ ) && \
180
+ defined( __AES__ )
181
+ # define INTEL_AES_POSSIBLE
182
+ # endif
183
+ #endif
184
+
185
+ /* Define this option if support for the Intel AESNI is required
186
+ If USE_INTEL_AES_IF_PRESENT is defined then AESNI will be used
187
+ if it is detected (both present and enabled).
188
+
189
+ AESNI uses a decryption key schedule with the first decryption
190
+ round key at the high end of the key scedule with the following
191
+ round keys at lower positions in memory. So AES_REV_DKS must NOT
192
+ be defined when AESNI will be used. Although it is unlikely that
193
+ assembler code will be used with an AESNI build, if it is then
194
+ AES_REV_DKS must NOT be defined when the assembler files are
195
+ built (the definition of USE_INTEL_AES_IF_PRESENT in the assembler
196
+ code files must match that here if they are used).
197
+ */
198
+
199
+ #if 0 && defined( INTEL_AES_POSSIBLE ) && !defined( USE_INTEL_AES_IF_PRESENT )
200
+ # define USE_INTEL_AES_IF_PRESENT
201
+ #endif
202
+
203
+ /* Define this option if support for the VIA ACE is required. This uses
204
+ inline assembler instructions and is only implemented for the Microsoft,
205
+ Intel and GCC compilers. If VIA ACE is known to be present, then defining
206
+ ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption
207
+ code. If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if
208
+ it is detected (both present and enabled) but the normal AES code will
209
+ also be present.
210
+
211
+ When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte
212
+ aligned; other input/output buffers do not need to be 16 byte aligned
213
+ but there are very large performance gains if this can be arranged.
214
+ VIA ACE also requires the decryption key schedule to be in reverse
215
+ order (which later checks below ensure).
216
+
217
+ AES_REV_DKS must be set for assembler code used with a VIA ACE build
218
+ */
219
+
220
+ #if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT )
221
+ # define USE_VIA_ACE_IF_PRESENT
222
+ #endif
223
+
224
+ #if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT )
225
+ # define ASSUME_VIA_ACE_PRESENT
226
+ # endif
227
+
228
+ /* 3. ASSEMBLER SUPPORT
229
+
230
+ This define (which can be on the command line) enables the use of the
231
+ assembler code routines for encryption, decryption and key scheduling
232
+ as follows:
233
+
234
+ ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for
235
+ encryption and decryption and but with key scheduling in C
236
+ ASM_X86_V2 uses assembler (aes_x86_v2.asm) with compressed tables for
237
+ encryption, decryption and key scheduling
238
+ ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for
239
+ encryption and decryption and but with key scheduling in C
240
+ ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for
241
+ encryption and decryption and but with key scheduling in C
242
+
243
+ Change one 'if 0' below to 'if 1' to select the version or define
244
+ as a compilation option.
245
+ */
246
+
247
+ #if 0 && !defined( ASM_X86_V1C )
248
+ # define ASM_X86_V1C
249
+ #elif 0 && !defined( ASM_X86_V2 )
250
+ # define ASM_X86_V2
251
+ #elif 0 && !defined( ASM_X86_V2C )
252
+ # define ASM_X86_V2C
253
+ #elif 0 && !defined( ASM_AMD64_C )
254
+ # define ASM_AMD64_C
255
+ #endif
256
+
257
+ #if defined( __i386 ) || defined( _M_IX86 )
258
+ # define A32_
259
+ #elif defined( __x86_64__ ) || defined( _M_X64 )
260
+ # define A64_
261
+ #endif
262
+
263
+ #if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \
264
+ && !defined( A32_ ) || defined( ASM_AMD64_C ) && !defined( A64_ )
265
+ # error Assembler code is only available for x86 and AMD64 systems
266
+ #endif
267
+
268
+ /* 4. FAST INPUT/OUTPUT OPERATIONS.
269
+
270
+ On some machines it is possible to improve speed by transferring the
271
+ bytes in the input and output arrays to and from the internal 32-bit
272
+ variables by addressing these arrays as if they are arrays of 32-bit
273
+ words. On some machines this will always be possible but there may
274
+ be a large performance penalty if the byte arrays are not aligned on
275
+ the normal word boundaries. On other machines this technique will
276
+ lead to memory access errors when such 32-bit word accesses are not
277
+ properly aligned. The option SAFE_IO avoids such problems but will
278
+ often be slower on those machines that support misaligned access
279
+ (especially so if care is taken to align the input and output byte
280
+ arrays on 32-bit word boundaries). If SAFE_IO is not defined it is
281
+ assumed that access to byte arrays as if they are arrays of 32-bit
282
+ words will not cause problems when such accesses are misaligned.
283
+ */
284
+ #if 1 && !defined( _MSC_VER )
285
+ # define SAFE_IO
286
+ #endif
287
+
288
+ /* 5. LOOP UNROLLING
289
+
290
+ The code for encryption and decrytpion cycles through a number of rounds
291
+ that can be implemented either in a loop or by expanding the code into a
292
+ long sequence of instructions, the latter producing a larger program but
293
+ one that will often be much faster. The latter is called loop unrolling.
294
+ There are also potential speed advantages in expanding two iterations in
295
+ a loop with half the number of iterations, which is called partial loop
296
+ unrolling. The following options allow partial or full loop unrolling
297
+ to be set independently for encryption and decryption
298
+ */
299
+ #if 1
300
+ # define ENC_UNROLL FULL
301
+ #elif 0
302
+ # define ENC_UNROLL PARTIAL
303
+ #else
304
+ # define ENC_UNROLL NONE
305
+ #endif
306
+
307
+ #if 1
308
+ # define DEC_UNROLL FULL
309
+ #elif 0
310
+ # define DEC_UNROLL PARTIAL
311
+ #else
312
+ # define DEC_UNROLL NONE
313
+ #endif
314
+
315
+ #if 1
316
+ # define ENC_KS_UNROLL
317
+ #endif
318
+
319
+ #if 1
320
+ # define DEC_KS_UNROLL
321
+ #endif
322
+
323
+ /* 6. FAST FINITE FIELD OPERATIONS
324
+
325
+ If this section is included, tables are used to provide faster finite
326
+ field arithmetic (this has no effect if STATIC_TABLES is defined).
327
+ */
328
+ #if 1
329
+ # define FF_TABLES
330
+ #endif
331
+
332
+ /* 7. INTERNAL STATE VARIABLE FORMAT
333
+
334
+ The internal state of Rijndael is stored in a number of local 32-bit
335
+ word varaibles which can be defined either as an array or as individual
336
+ names variables. Include this section if you want to store these local
337
+ varaibles in arrays. Otherwise individual local variables will be used.
338
+ */
339
+ #if 1
340
+ # define ARRAYS
341
+ #endif
342
+
343
+ /* 8. FIXED OR DYNAMIC TABLES
344
+
345
+ When this section is included the tables used by the code are compiled
346
+ statically into the binary file. Otherwise the subroutine aes_init()
347
+ must be called to compute them before the code is first used.
348
+ */
349
+ #if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 ))
350
+ # define STATIC_TABLES
351
+ #endif
352
+
353
+ /* 9. MASKING OR CASTING FROM LONGER VALUES TO BYTES
354
+
355
+ In some systems it is better to mask longer values to extract bytes
356
+ rather than using a cast. This option allows this choice.
357
+ */
358
+ #if 0
359
+ # define to_byte(x) ((uint8_t)(x))
360
+ #else
361
+ # define to_byte(x) ((x) & 0xff)
362
+ #endif
363
+
364
+ /* 10. TABLE ALIGNMENT
365
+
366
+ On some sytsems speed will be improved by aligning the AES large lookup
367
+ tables on particular boundaries. This define should be set to a power of
368
+ two giving the desired alignment. It can be left undefined if alignment
369
+ is not needed. This option is specific to the Microsft VC++ compiler -
370
+ it seems to sometimes cause trouble for the VC++ version 6 compiler.
371
+ */
372
+
373
+ #if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
374
+ # define TABLE_ALIGN 32
375
+ #endif
376
+
377
+ /* 11. REDUCE CODE AND TABLE SIZE
378
+
379
+ This replaces some expanded macros with function calls if AES_ASM_V2 or
380
+ AES_ASM_V2C are defined
381
+ */
382
+
383
+ #if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))
384
+ # define REDUCE_CODE_SIZE
385
+ #endif
386
+
387
+ /* 12. TABLE OPTIONS
388
+
389
+ This cipher proceeds by repeating in a number of cycles known as 'rounds'
390
+ which are implemented by a round function which can optionally be speeded
391
+ up using tables. The basic tables are each 256 32-bit words, with either
392
+ one or four tables being required for each round function depending on
393
+ how much speed is required. The encryption and decryption round functions
394
+ are different and the last encryption and decrytpion round functions are
395
+ different again making four different round functions in all.
396
+
397
+ This means that:
398
+ 1. Normal encryption and decryption rounds can each use either 0, 1
399
+ or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
400
+ 2. The last encryption and decryption rounds can also use either 0, 1
401
+ or 4 tables and table spaces of 0, 1024 or 4096 bytes each.
402
+
403
+ Include or exclude the appropriate definitions below to set the number
404
+ of tables used by this implementation.
405
+ */
406
+
407
+ #if 1 /* set tables for the normal encryption round */
408
+ # define ENC_ROUND FOUR_TABLES
409
+ #elif 0
410
+ # define ENC_ROUND ONE_TABLE
411
+ #else
412
+ # define ENC_ROUND NO_TABLES
413
+ #endif
414
+
415
+ #if 1 /* set tables for the last encryption round */
416
+ # define LAST_ENC_ROUND FOUR_TABLES
417
+ #elif 0
418
+ # define LAST_ENC_ROUND ONE_TABLE
419
+ #else
420
+ # define LAST_ENC_ROUND NO_TABLES
421
+ #endif
422
+
423
+ #if 1 /* set tables for the normal decryption round */
424
+ # define DEC_ROUND FOUR_TABLES
425
+ #elif 0
426
+ # define DEC_ROUND ONE_TABLE
427
+ #else
428
+ # define DEC_ROUND NO_TABLES
429
+ #endif
430
+
431
+ #if 1 /* set tables for the last decryption round */
432
+ # define LAST_DEC_ROUND FOUR_TABLES
433
+ #elif 0
434
+ # define LAST_DEC_ROUND ONE_TABLE
435
+ #else
436
+ # define LAST_DEC_ROUND NO_TABLES
437
+ #endif
438
+
439
+ /* The decryption key schedule can be speeded up with tables in the same
440
+ way that the round functions can. Include or exclude the following
441
+ defines to set this requirement.
442
+ */
443
+ #if 1
444
+ # define KEY_SCHED FOUR_TABLES
445
+ #elif 0
446
+ # define KEY_SCHED ONE_TABLE
447
+ #else
448
+ # define KEY_SCHED NO_TABLES
449
+ #endif
450
+
451
+ /* ---- END OF USER CONFIGURED OPTIONS ---- */
452
+
453
+ /* VIA ACE support is only available for VC++ and GCC */
454
+
455
+ #if !defined( _MSC_VER ) && !defined( __GNUC__ )
456
+ # if defined( ASSUME_VIA_ACE_PRESENT )
457
+ # undef ASSUME_VIA_ACE_PRESENT
458
+ # endif
459
+ # if defined( USE_VIA_ACE_IF_PRESENT )
460
+ # undef USE_VIA_ACE_IF_PRESENT
461
+ # endif
462
+ #endif
463
+
464
+ #if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT )
465
+ # define USE_VIA_ACE_IF_PRESENT
466
+ #endif
467
+
468
+ /* define to reverse decryption key schedule */
469
+ #if 1 || defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS )
470
+ # define AES_REV_DKS
471
+ #endif
472
+
473
+ /* Intel AESNI uses a decryption key schedule in the encryption order */
474
+ #if defined( USE_INTEL_AES_IF_PRESENT ) && defined ( AES_REV_DKS )
475
+ # undef AES_REV_DKS
476
+ #endif
477
+
478
+ /* Assembler support requires the use of platform byte order */
479
+
480
+ #if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \
481
+ && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)
482
+ # undef ALGORITHM_BYTE_ORDER
483
+ # define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
484
+ #endif
485
+
486
+ /* In this implementation the columns of the state array are each held in
487
+ 32-bit words. The state array can be held in various ways: in an array
488
+ of words, in a number of individual word variables or in a number of
489
+ processor registers. The following define maps a variable name x and
490
+ a column number c to the way the state array variable is to be held.
491
+ The first define below maps the state into an array x[c] whereas the
492
+ second form maps the state into a number of individual variables x0,
493
+ x1, etc. Another form could map individual state colums to machine
494
+ register names.
495
+ */
496
+
497
+ #if defined( ARRAYS )
498
+ # define s(x,c) x[c]
499
+ #else
500
+ # define s(x,c) x##c
501
+ #endif
502
+
503
+ /* This implementation provides subroutines for encryption, decryption
504
+ and for setting the three key lengths (separately) for encryption
505
+ and decryption. Since not all functions are needed, masks are set
506
+ up here to determine which will be implemented in C
507
+ */
508
+
509
+ #if !defined( AES_ENCRYPT )
510
+ # define EFUNCS_IN_C 0
511
+ #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
512
+ || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
513
+ # define EFUNCS_IN_C ENC_KEYING_IN_C
514
+ #elif !defined( ASM_X86_V2 )
515
+ # define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C )
516
+ #else
517
+ # define EFUNCS_IN_C 0
518
+ #endif
519
+
520
+ #if !defined( AES_DECRYPT )
521
+ # define DFUNCS_IN_C 0
522
+ #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
523
+ || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
524
+ # define DFUNCS_IN_C DEC_KEYING_IN_C
525
+ #elif !defined( ASM_X86_V2 )
526
+ # define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C )
527
+ #else
528
+ # define DFUNCS_IN_C 0
529
+ #endif
530
+
531
+ #define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C )
532
+
533
+ /* END OF CONFIGURATION OPTIONS */
534
+
535
+ #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))
536
+
537
+ /* Disable or report errors on some combinations of options */
538
+
539
+ #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
540
+ # undef LAST_ENC_ROUND
541
+ # define LAST_ENC_ROUND NO_TABLES
542
+ #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
543
+ # undef LAST_ENC_ROUND
544
+ # define LAST_ENC_ROUND ONE_TABLE
545
+ #endif
546
+
547
+ #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
548
+ # undef ENC_UNROLL
549
+ # define ENC_UNROLL NONE
550
+ #endif
551
+
552
+ #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
553
+ # undef LAST_DEC_ROUND
554
+ # define LAST_DEC_ROUND NO_TABLES
555
+ #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
556
+ # undef LAST_DEC_ROUND
557
+ # define LAST_DEC_ROUND ONE_TABLE
558
+ #endif
559
+
560
+ #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
561
+ # undef DEC_UNROLL
562
+ # define DEC_UNROLL NONE
563
+ #endif
564
+
565
+ #if defined( bswap32 )
566
+ # define aes_sw32 bswap32
567
+ #elif defined( bswap_32 )
568
+ # define aes_sw32 bswap_32
569
+ #else
570
+ # define brot(x,n) (((uint32_t)(x) << n) | ((uint32_t)(x) >> (32 - n)))
571
+ # define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))
572
+ #endif
573
+
574
+ /* upr(x,n): rotates bytes within words by n positions, moving bytes to
575
+ higher index positions with wrap around into low positions
576
+ ups(x,n): moves bytes by n positions to higher index positions in
577
+ words but without wrap around
578
+ bval(x,n): extracts a byte from a word
579
+
580
+ WARNING: The definitions given here are intended only for use with
581
+ unsigned variables and with shift counts that are compile
582
+ time constants
583
+ */
584
+
585
+ #if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN )
586
+ # define upr(x,n) (((uint32_t)(x) << (8 * (n))) | ((uint32_t)(x) >> (32 - 8 * (n))))
587
+ # define ups(x,n) ((uint32_t) (x) << (8 * (n)))
588
+ # define bval(x,n) to_byte((x) >> (8 * (n)))
589
+ # define bytes2word(b0, b1, b2, b3) \
590
+ (((uint32_t)(b3) << 24) | ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | (b0))
591
+ #endif
592
+
593
+ #if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN )
594
+ # define upr(x,n) (((uint32_t)(x) >> (8 * (n))) | ((uint32_t)(x) << (32 - 8 * (n))))
595
+ # define ups(x,n) ((uint32_t) (x) >> (8 * (n)))
596
+ # define bval(x,n) to_byte((x) >> (24 - 8 * (n)))
597
+ # define bytes2word(b0, b1, b2, b3) \
598
+ (((uint32_t)(b0) << 24) | ((uint32_t)(b1) << 16) | ((uint32_t)(b2) << 8) | (b3))
599
+ #endif
600
+
601
+ #if defined( SAFE_IO )
602
+ # define word_in(x,c) bytes2word(((const uint8_t*)(x)+4*c)[0], ((const uint8_t*)(x)+4*c)[1], \
603
+ ((const uint8_t*)(x)+4*c)[2], ((const uint8_t*)(x)+4*c)[3])
604
+ # define word_out(x,c,v) { ((uint8_t*)(x)+4*c)[0] = bval(v,0); ((uint8_t*)(x)+4*c)[1] = bval(v,1); \
605
+ ((uint8_t*)(x)+4*c)[2] = bval(v,2); ((uint8_t*)(x)+4*c)[3] = bval(v,3); }
606
+ #elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER )
607
+ # define word_in(x,c) (*((uint32_t*)(x)+(c)))
608
+ # define word_out(x,c,v) (*((uint32_t*)(x)+(c)) = (v))
609
+ #else
610
+ # define word_in(x,c) aes_sw32(*((uint32_t*)(x)+(c)))
611
+ # define word_out(x,c,v) (*((uint32_t*)(x)+(c)) = aes_sw32(v))
612
+ #endif
613
+
614
+ /* the finite field modular polynomial and elements */
615
+
616
+ #define WPOLY 0x011b
617
+ #define BPOLY 0x1b
618
+
619
+ /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
620
+
621
+ #define gf_c1 0x80808080
622
+ #define gf_c2 0x7f7f7f7f
623
+ #define gf_mulx(x) ((((x) & gf_c2) << 1) ^ ((((x) & gf_c1) >> 7) * BPOLY))
624
+
625
+ /* The following defines provide alternative definitions of gf_mulx that might
626
+ give improved performance if a fast 32-bit multiply is not available. Note
627
+ that a temporary variable u needs to be defined where gf_mulx is used.
628
+
629
+ #define gf_mulx(x) (u = (x) & gf_c1, u |= (u >> 1), ((x) & gf_c2) << 1) ^ ((u >> 3) | (u >> 6))
630
+ #define gf_c4 (0x01010101 * BPOLY)
631
+ #define gf_mulx(x) (u = (x) & gf_c1, ((x) & gf_c2) << 1) ^ ((u - (u >> 7)) & gf_c4)
632
+ */
633
+
634
+ /* Work out which tables are needed for the different options */
635
+
636
+ #if defined( ASM_X86_V1C )
637
+ # if defined( ENC_ROUND )
638
+ # undef ENC_ROUND
639
+ # endif
640
+ # define ENC_ROUND FOUR_TABLES
641
+ # if defined( LAST_ENC_ROUND )
642
+ # undef LAST_ENC_ROUND
643
+ # endif
644
+ # define LAST_ENC_ROUND FOUR_TABLES
645
+ # if defined( DEC_ROUND )
646
+ # undef DEC_ROUND
647
+ # endif
648
+ # define DEC_ROUND FOUR_TABLES
649
+ # if defined( LAST_DEC_ROUND )
650
+ # undef LAST_DEC_ROUND
651
+ # endif
652
+ # define LAST_DEC_ROUND FOUR_TABLES
653
+ # if defined( KEY_SCHED )
654
+ # undef KEY_SCHED
655
+ # define KEY_SCHED FOUR_TABLES
656
+ # endif
657
+ #endif
658
+
659
+ #if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C )
660
+ # if ENC_ROUND == ONE_TABLE
661
+ # define FT1_SET
662
+ # elif ENC_ROUND == FOUR_TABLES
663
+ # define FT4_SET
664
+ # else
665
+ # define SBX_SET
666
+ # endif
667
+ # if LAST_ENC_ROUND == ONE_TABLE
668
+ # define FL1_SET
669
+ # elif LAST_ENC_ROUND == FOUR_TABLES
670
+ # define FL4_SET
671
+ # elif !defined( SBX_SET )
672
+ # define SBX_SET
673
+ # endif
674
+ #endif
675
+
676
+ #if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C )
677
+ # if DEC_ROUND == ONE_TABLE
678
+ # define IT1_SET
679
+ # elif DEC_ROUND == FOUR_TABLES
680
+ # define IT4_SET
681
+ # else
682
+ # define ISB_SET
683
+ # endif
684
+ # if LAST_DEC_ROUND == ONE_TABLE
685
+ # define IL1_SET
686
+ # elif LAST_DEC_ROUND == FOUR_TABLES
687
+ # define IL4_SET
688
+ # elif !defined(ISB_SET)
689
+ # define ISB_SET
690
+ # endif
691
+ #endif
692
+
693
+ #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))
694
+ # if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C))
695
+ # if KEY_SCHED == ONE_TABLE
696
+ # if !defined( FL1_SET ) && !defined( FL4_SET )
697
+ # define LS1_SET
698
+ # endif
699
+ # elif KEY_SCHED == FOUR_TABLES
700
+ # if !defined( FL4_SET )
701
+ # define LS4_SET
702
+ # endif
703
+ # elif !defined( SBX_SET )
704
+ # define SBX_SET
705
+ # endif
706
+ # endif
707
+ # if (FUNCS_IN_C & DEC_KEYING_IN_C)
708
+ # if KEY_SCHED == ONE_TABLE
709
+ # define IM1_SET
710
+ # elif KEY_SCHED == FOUR_TABLES
711
+ # define IM4_SET
712
+ # elif !defined( SBX_SET )
713
+ # define SBX_SET
714
+ # endif
715
+ # endif
716
+ #endif
717
+
718
+ /* generic definitions of Rijndael macros that use tables */
719
+
720
+ #define no_table(x,box,vf,rf,c) bytes2word( \
721
+ box[bval(vf(x,0,c),rf(0,c))], \
722
+ box[bval(vf(x,1,c),rf(1,c))], \
723
+ box[bval(vf(x,2,c),rf(2,c))], \
724
+ box[bval(vf(x,3,c),rf(3,c))])
725
+
726
+ #define one_table(x,op,tab,vf,rf,c) \
727
+ ( tab[bval(vf(x,0,c),rf(0,c))] \
728
+ ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
729
+ ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
730
+ ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
731
+
732
+ #define four_tables(x,tab,vf,rf,c) \
733
+ ( tab[0][bval(vf(x,0,c),rf(0,c))] \
734
+ ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
735
+ ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
736
+ ^ tab[3][bval(vf(x,3,c),rf(3,c))])
737
+
738
+ #define vf1(x,r,c) (x)
739
+ #define rf1(r,c) (r)
740
+ #define rf2(r,c) ((8+r-c)&3)
741
+
742
+ /* perform forward and inverse column mix operation on four bytes in long word x in */
743
+ /* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */
744
+
745
+ #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))
746
+
747
+ #if defined( FM4_SET ) /* not currently used */
748
+ # define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)
749
+ #elif defined( FM1_SET ) /* not currently used */
750
+ # define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)
751
+ #else
752
+ # define dec_fmvars uint32_t g2
753
+ # define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))
754
+ #endif
755
+
756
+ #if defined( IM4_SET )
757
+ # define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)
758
+ #elif defined( IM1_SET )
759
+ # define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)
760
+ #else
761
+ # define dec_imvars uint32_t g2, g4, g9
762
+ # define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \
763
+ (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))
764
+ #endif
765
+
766
+ #if defined( FL4_SET )
767
+ # define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)
768
+ #elif defined( LS4_SET )
769
+ # define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)
770
+ #elif defined( FL1_SET )
771
+ # define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)
772
+ #elif defined( LS1_SET )
773
+ # define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)
774
+ #else
775
+ # define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)
776
+ #endif
777
+
778
+ #endif
779
+
780
+ #if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )
781
+ # define ISB_SET
782
+ #endif
783
+
784
+ #endif