@leocuvee/wrkzcoin-multi-hashing 0.0.20
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.
- package/.travis.yml +5 -0
- package/LICENSE +674 -0
- package/README.md +87 -0
- package/appveyor.yml +12 -0
- package/argon2/.gitattributes +10 -0
- package/argon2/.travis.yml +25 -0
- package/argon2/Argon2.sln +160 -0
- package/argon2/CHANGELOG.md +32 -0
- package/argon2/CMakeLists.txt +87 -0
- package/argon2/LICENSE +314 -0
- package/argon2/Makefile +196 -0
- package/argon2/README.md +297 -0
- package/argon2/appveyor.yml +40 -0
- package/argon2/argon2-specs.pdf +0 -0
- package/argon2/export.sh +7 -0
- package/argon2/include/argon2.h +427 -0
- package/argon2/latex/CMakeLists.txt +34 -0
- package/argon2/latex/IEEEtran.cls +6347 -0
- package/argon2/latex/Makefile +18 -0
- package/argon2/latex/argon2-specs.tex +920 -0
- package/argon2/latex/pics/argon2-par.pdf +0 -0
- package/argon2/latex/pics/compression.pdf +0 -0
- package/argon2/latex/pics/generic.pdf +0 -0
- package/argon2/latex/pics/power-distribution.jpg +0 -0
- package/argon2/latex/tradeoff.bib +822 -0
- package/argon2/libargon2.pc +16 -0
- package/argon2/man/CMakeLists.txt +8 -0
- package/argon2/man/argon2.1 +57 -0
- package/argon2/meson.build +16 -0
- package/argon2/meson_options.txt +1 -0
- package/argon2/src/CMakeLists.txt +147 -0
- package/argon2/src/argon2.c +452 -0
- package/argon2/src/argon2.pc.in +11 -0
- package/argon2/src/blake2/blake2-impl.h +156 -0
- package/argon2/src/blake2/blake2.h +89 -0
- package/argon2/src/blake2/blake2b.c +390 -0
- package/argon2/src/blake2/blamka-round-opt.h +471 -0
- package/argon2/src/blake2/blamka-round-ref.h +56 -0
- package/argon2/src/core.c +634 -0
- package/argon2/src/core.h +228 -0
- package/argon2/src/encoding.c +467 -0
- package/argon2/src/encoding.h +57 -0
- package/argon2/src/genkat.h +51 -0
- package/argon2/src/meson.build +68 -0
- package/argon2/src/opt.c +283 -0
- package/argon2/src/optimization/CMakeLists.txt +10 -0
- package/argon2/src/ref.c +194 -0
- package/argon2/src/thread.c +57 -0
- package/argon2/src/thread.h +67 -0
- package/argon2/tests/CMakeLists.txt +43 -0
- package/argon2/tests/bench.c +111 -0
- package/argon2/tests/genkat.c +207 -0
- package/argon2/tests/kats/argon2d +12304 -0
- package/argon2/tests/kats/argon2d.shasum +1 -0
- package/argon2/tests/kats/argon2d_v16 +12304 -0
- package/argon2/tests/kats/argon2d_v16.shasum +1 -0
- package/argon2/tests/kats/argon2i +12304 -0
- package/argon2/tests/kats/argon2i.shasum +1 -0
- package/argon2/tests/kats/argon2i_v16 +12304 -0
- package/argon2/tests/kats/argon2i_v16.shasum +1 -0
- package/argon2/tests/kats/argon2id +12304 -0
- package/argon2/tests/kats/argon2id.shasum +1 -0
- package/argon2/tests/kats/argon2id_v16 +12304 -0
- package/argon2/tests/kats/argon2id_v16.shasum +1 -0
- package/argon2/tests/kats/check-sums.ps1 +48 -0
- package/argon2/tests/kats/check-sums.sh +16 -0
- package/argon2/tests/kats/test.ps1 +132 -0
- package/argon2/tests/kats/test.sh +117 -0
- package/argon2/tests/meson.build +34 -0
- package/argon2/tests/test.c +289 -0
- package/argon2/tool/CMakeLists.txt +7 -0
- package/argon2/tool/main.c +339 -0
- package/argon2/tool/meson.build +8 -0
- package/argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj +226 -0
- package/argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj.filters +69 -0
- package/argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj +226 -0
- package/argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj.filters +69 -0
- package/argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj +225 -0
- package/argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj.filters +66 -0
- package/argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj +239 -0
- package/argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj.filters +72 -0
- package/argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj +227 -0
- package/argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj.filters +69 -0
- package/argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj +226 -0
- package/argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj.filters +69 -0
- package/argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj +226 -0
- package/argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj.filters +69 -0
- package/argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj +225 -0
- package/argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj.filters +66 -0
- package/argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj +227 -0
- package/argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj.filters +72 -0
- package/argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj +226 -0
- package/argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj.filters +69 -0
- package/bcrypt.c +566 -0
- package/bcrypt.h +14 -0
- package/binding.gyp +93 -0
- package/blake.c +17 -0
- package/blake.h +16 -0
- package/boolberry.cc +11 -0
- package/boolberry.h +6 -0
- package/build/Makefile +354 -0
- package/build/Release/.deps/Release/multihashing.node.d +1 -0
- package/build/Release/.deps/Release/obj.target/multihashing/argon2/src/argon2.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/argon2/src/blake2/blake2b.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/argon2/src/core.o.d +10 -0
- package/build/Release/.deps/Release/obj.target/multihashing/argon2/src/encoding.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/argon2/src/ref.o.d +14 -0
- package/build/Release/.deps/Release/obj.target/multihashing/argon2/src/thread.o.d +5 -0
- package/build/Release/.deps/Release/obj.target/multihashing/bcrypt.o.d +4 -0
- package/build/Release/.deps/Release/obj.target/multihashing/blake.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/boolberry.o.d +12 -0
- package/build/Release/.deps/Release/obj.target/multihashing/c11.o.d +20 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/aesb.o.d +3 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/c_blake256.o.d +5 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/c_groestl.o.d +10 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/c_jh.o.d +9 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/c_keccak.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/c_skein.o.d +10 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/hash.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/oaes_lib.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/crypto/wild_keccak.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_dark.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_dark_lite.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_fast.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_lite.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_soft_shell.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_turtle.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/cryptonight_turtle_lite.o.d +18 -0
- package/build/Release/.deps/Release/obj.target/multihashing/fresh.o.d +10 -0
- package/build/Release/.deps/Release/obj.target/multihashing/fugue.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/groestl.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/hefty1.o.d +12 -0
- package/build/Release/.deps/Release/obj.target/multihashing/keccak.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/multihashing.o.d +155 -0
- package/build/Release/.deps/Release/obj.target/multihashing/nist5.o.d +12 -0
- package/build/Release/.deps/Release/obj.target/multihashing/quark.o.d +14 -0
- package/build/Release/.deps/Release/obj.target/multihashing/qubit.o.d +12 -0
- package/build/Release/.deps/Release/obj.target/multihashing/scryptjane.o.d +30 -0
- package/build/Release/.deps/Release/obj.target/multihashing/scryptn.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha1.o.d +24 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/aes_helper.o.d +5 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/hamsi.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_blake.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_bmw.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_cubehash.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_echo.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_fugue.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_groestl.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_hefty1.o.d +5 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_jh.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_keccak.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_luffa.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_shabal.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_shavite.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_simd.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_skein.o.d +6 -0
- package/build/Release/.deps/Release/obj.target/multihashing/sha3/sph_whirlpool.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/shavite3.o.d +7 -0
- package/build/Release/.deps/Release/obj.target/multihashing/skein.o.d +8 -0
- package/build/Release/.deps/Release/obj.target/multihashing/x11.o.d +20 -0
- package/build/Release/.deps/Release/obj.target/multihashing/x13.o.d +23 -0
- package/build/Release/.deps/Release/obj.target/multihashing/x15.o.d +26 -0
- package/build/Release/.deps/Release/obj.target/multihashing.node.d +1 -0
- package/build/Release/multihashing.node +0 -0
- package/build/binding.Makefile +6 -0
- package/build/multihashing.target.mk +255 -0
- package/c11.c +85 -0
- package/c11.h +17 -0
- package/crypto/aesb.c +177 -0
- package/crypto/c_blake256.c +326 -0
- package/crypto/c_blake256.h +43 -0
- package/crypto/c_groestl.c +360 -0
- package/crypto/c_groestl.h +56 -0
- package/crypto/c_jh.c +367 -0
- package/crypto/c_jh.h +20 -0
- package/crypto/c_keccak.c +112 -0
- package/crypto/c_keccak.h +26 -0
- package/crypto/c_skein.c +2036 -0
- package/crypto/c_skein.h +45 -0
- package/crypto/crypto.h +186 -0
- package/crypto/cryptonote_core/account.cpp +50 -0
- package/crypto/cryptonote_core/account.h +61 -0
- package/crypto/cryptonote_core/cryptonote_basic_impl.cpp +186 -0
- package/crypto/cryptonote_core/cryptonote_basic_impl.h +65 -0
- package/crypto/cryptonote_core/cryptonote_format_utils.cpp +766 -0
- package/crypto/cryptonote_core/cryptonote_format_utils.h +30 -0
- package/crypto/cryptonote_protocol/cryptonote_protocol_defs.h +152 -0
- package/crypto/groestl_tables.h +38 -0
- package/crypto/hash-ops.h +57 -0
- package/crypto/hash.c +24 -0
- package/crypto/hash.h +22 -0
- package/crypto/int-util.h +230 -0
- package/crypto/oaes_config.h +50 -0
- package/crypto/oaes_lib.c +1468 -0
- package/crypto/oaes_lib.h +215 -0
- package/crypto/skein_port.h +190 -0
- package/crypto/variant2_int_sqrt.h +168 -0
- package/crypto/wild_keccak.cpp +119 -0
- package/crypto/wild_keccak.h +168 -0
- package/cryptonight.c +300 -0
- package/cryptonight.h +17 -0
- package/cryptonight_dark.c +300 -0
- package/cryptonight_dark.h +17 -0
- package/cryptonight_dark_lite.c +300 -0
- package/cryptonight_dark_lite.h +17 -0
- package/cryptonight_fast.c +300 -0
- package/cryptonight_fast.h +17 -0
- package/cryptonight_lite.c +300 -0
- package/cryptonight_lite.h +17 -0
- package/cryptonight_soft_shell.c +298 -0
- package/cryptonight_soft_shell.h +17 -0
- package/cryptonight_turtle.c +300 -0
- package/cryptonight_turtle.h +17 -0
- package/cryptonight_turtle_lite.c +300 -0
- package/cryptonight_turtle_lite.h +17 -0
- package/fresh.c +42 -0
- package/fresh.h +16 -0
- package/fugue.c +12 -0
- package/fugue.h +16 -0
- package/groestl.c +40 -0
- package/groestl.h +17 -0
- package/hefty1.c +63 -0
- package/hefty1.h +16 -0
- package/index.js +1 -0
- package/keccak.c +14 -0
- package/keccak.h +16 -0
- package/leocuvee-wrkzcoin-multi-hashing-0.0.20.tgz +0 -0
- package/multihashing.cc +699 -0
- package/nist5.c +46 -0
- package/nist5.h +16 -0
- package/package.json +56 -0
- package/quark.c +210 -0
- package/quark.h +16 -0
- package/qubit.c +45 -0
- package/qubit.h +16 -0
- package/scryptjane/scrypt-jane-chacha.h +132 -0
- package/scryptjane/scrypt-jane-hash.h +48 -0
- package/scryptjane/scrypt-jane-hash_keccak.h +168 -0
- package/scryptjane/scrypt-jane-hash_sha256.h +135 -0
- package/scryptjane/scrypt-jane-mix_chacha-avx.h +340 -0
- package/scryptjane/scrypt-jane-mix_chacha-sse2.h +371 -0
- package/scryptjane/scrypt-jane-mix_chacha-ssse3.h +348 -0
- package/scryptjane/scrypt-jane-mix_chacha.h +69 -0
- package/scryptjane/scrypt-jane-mix_salsa-avx.h +381 -0
- package/scryptjane/scrypt-jane-mix_salsa-sse2.h +443 -0
- package/scryptjane/scrypt-jane-mix_salsa.h +70 -0
- package/scryptjane/scrypt-jane-pbkdf2.h +112 -0
- package/scryptjane/scrypt-jane-portable-x86.h +364 -0
- package/scryptjane/scrypt-jane-portable.h +281 -0
- package/scryptjane/scrypt-jane-romix-basic.h +67 -0
- package/scryptjane/scrypt-jane-romix-template.h +118 -0
- package/scryptjane/scrypt-jane-romix.h +27 -0
- package/scryptjane/scrypt-jane-salsa.h +106 -0
- package/scryptjane/scrypt-jane-test-vectors.h +261 -0
- package/scryptjane.c +223 -0
- package/scryptjane.h +36 -0
- package/scryptn.c +258 -0
- package/scryptn.h +16 -0
- package/sha1.c +65 -0
- package/sha1.h +16 -0
- package/sha256.h +440 -0
- package/sha3/aes_helper.c +392 -0
- package/sha3/hamsi.c +867 -0
- package/sha3/hamsi_helper.c +39648 -0
- package/sha3/md_helper.c +347 -0
- package/sha3/sph_blake.c +1114 -0
- package/sha3/sph_blake.h +327 -0
- package/sha3/sph_bmw.c +965 -0
- package/sha3/sph_bmw.h +328 -0
- package/sha3/sph_cubehash.c +723 -0
- package/sha3/sph_cubehash.h +292 -0
- package/sha3/sph_echo.c +1031 -0
- package/sha3/sph_echo.h +320 -0
- package/sha3/sph_fugue.c +1208 -0
- package/sha3/sph_fugue.h +81 -0
- package/sha3/sph_groestl.c +3119 -0
- package/sha3/sph_groestl.h +329 -0
- package/sha3/sph_hamsi.h +321 -0
- package/sha3/sph_hefty1.c +378 -0
- package/sha3/sph_hefty1.h +66 -0
- package/sha3/sph_jh.c +1116 -0
- package/sha3/sph_jh.h +298 -0
- package/sha3/sph_keccak.c +1824 -0
- package/sha3/sph_keccak.h +293 -0
- package/sha3/sph_luffa.c +1426 -0
- package/sha3/sph_luffa.h +296 -0
- package/sha3/sph_shabal.c +806 -0
- package/sha3/sph_shabal.h +344 -0
- package/sha3/sph_shavite.c +1764 -0
- package/sha3/sph_shavite.h +314 -0
- package/sha3/sph_simd.c +1799 -0
- package/sha3/sph_simd.h +309 -0
- package/sha3/sph_skein.c +1254 -0
- package/sha3/sph_skein.h +298 -0
- package/sha3/sph_types.h +1976 -0
- package/sha3/sph_whirlpool.c +3480 -0
- package/sha3/sph_whirlpool.h +209 -0
- package/shavite3.c +24 -0
- package/shavite3.h +16 -0
- package/skein.c +26 -0
- package/skein.h +16 -0
- package/stdint.h +259 -0
- package/tests/argon2-tests.js +16 -0
- package/tests/benchmark.js +36 -0
- package/tests/cryptonight-tests.js +189 -0
- package/tests/cryptonight_monero.js +53 -0
- package/tests/test.js +16 -0
- package/x11.c +85 -0
- package/x11.h +16 -0
- package/x13.c +97 -0
- package/x13.h +5 -0
- package/x15.c +106 -0
- package/x15.h +16 -0
|
@@ -0,0 +1,1468 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* ---------------------------------------------------------------------------
|
|
3
|
+
* OpenAES License
|
|
4
|
+
* ---------------------------------------------------------------------------
|
|
5
|
+
* Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com
|
|
6
|
+
* All rights reserved.
|
|
7
|
+
*
|
|
8
|
+
* Redistribution and use in source and binary forms, with or without
|
|
9
|
+
* modification, are permitted provided that the following conditions are met:
|
|
10
|
+
*
|
|
11
|
+
* - Redistributions of source code must retain the above copyright notice,
|
|
12
|
+
* this list of conditions and the following disclaimer.
|
|
13
|
+
* - Redistributions in binary form must reproduce the above copyright
|
|
14
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
15
|
+
* documentation and/or other materials provided with the distribution.
|
|
16
|
+
*
|
|
17
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
18
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
20
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
21
|
+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
22
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
23
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
24
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
25
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
26
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
|
28
|
+
* ---------------------------------------------------------------------------
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
static const char _NR[] = {
|
|
32
|
+
0x4e,0x61,0x62,0x69,0x6c,0x20,0x53,0x2e,0x20,
|
|
33
|
+
0x41,0x6c,0x20,0x52,0x61,0x6d,0x6c,0x69,0x00
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
#include <stddef.h>
|
|
37
|
+
#include <time.h>
|
|
38
|
+
#include <sys/timeb.h>
|
|
39
|
+
#ifdef __APPLE__
|
|
40
|
+
#include <malloc/malloc.h>
|
|
41
|
+
#else
|
|
42
|
+
#include <malloc.h>
|
|
43
|
+
#endif
|
|
44
|
+
#include <string.h>
|
|
45
|
+
#include <stdlib.h>
|
|
46
|
+
#include <stdio.h>
|
|
47
|
+
|
|
48
|
+
#ifdef WIN32
|
|
49
|
+
#include <process.h>
|
|
50
|
+
#else
|
|
51
|
+
#include <sys/types.h>
|
|
52
|
+
#include <unistd.h>
|
|
53
|
+
#endif
|
|
54
|
+
|
|
55
|
+
#include "oaes_config.h"
|
|
56
|
+
#include "oaes_lib.h"
|
|
57
|
+
|
|
58
|
+
#ifdef OAES_HAVE_ISAAC
|
|
59
|
+
#include "rand.h"
|
|
60
|
+
#endif // OAES_HAVE_ISAAC
|
|
61
|
+
|
|
62
|
+
#define OAES_RKEY_LEN 4
|
|
63
|
+
#define OAES_COL_LEN 4
|
|
64
|
+
#define OAES_ROUND_BASE 7
|
|
65
|
+
|
|
66
|
+
// the block is padded
|
|
67
|
+
#define OAES_FLAG_PAD 0x01
|
|
68
|
+
|
|
69
|
+
#ifndef min
|
|
70
|
+
# define min(a,b) (((a)<(b)) ? (a) : (b))
|
|
71
|
+
#endif /* min */
|
|
72
|
+
|
|
73
|
+
// "OAES<8-bit header version><8-bit type><16-bit options><8-bit flags><56-bit reserved>"
|
|
74
|
+
static uint8_t oaes_header[OAES_BLOCK_SIZE] = {
|
|
75
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
76
|
+
/*0*/ 0x4f, 0x41, 0x45, 0x53, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
77
|
+
};
|
|
78
|
+
static uint8_t oaes_gf_8[] = {
|
|
79
|
+
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
|
|
80
|
+
|
|
81
|
+
static uint8_t oaes_sub_byte_value[16][16] = {
|
|
82
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
83
|
+
/*0*/ { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76 },
|
|
84
|
+
/*1*/ { 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0 },
|
|
85
|
+
/*2*/ { 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15 },
|
|
86
|
+
/*3*/ { 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75 },
|
|
87
|
+
/*4*/ { 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84 },
|
|
88
|
+
/*5*/ { 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf },
|
|
89
|
+
/*6*/ { 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8 },
|
|
90
|
+
/*7*/ { 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2 },
|
|
91
|
+
/*8*/ { 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73 },
|
|
92
|
+
/*9*/ { 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb },
|
|
93
|
+
/*a*/ { 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79 },
|
|
94
|
+
/*b*/ { 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08 },
|
|
95
|
+
/*c*/ { 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a },
|
|
96
|
+
/*d*/ { 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e },
|
|
97
|
+
/*e*/ { 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf },
|
|
98
|
+
/*f*/ { 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 },
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
static uint8_t oaes_inv_sub_byte_value[16][16] = {
|
|
102
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
103
|
+
/*0*/ { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb },
|
|
104
|
+
/*1*/ { 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb },
|
|
105
|
+
/*2*/ { 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e },
|
|
106
|
+
/*3*/ { 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25 },
|
|
107
|
+
/*4*/ { 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92 },
|
|
108
|
+
/*5*/ { 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84 },
|
|
109
|
+
/*6*/ { 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06 },
|
|
110
|
+
/*7*/ { 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b },
|
|
111
|
+
/*8*/ { 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73 },
|
|
112
|
+
/*9*/ { 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e },
|
|
113
|
+
/*a*/ { 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b },
|
|
114
|
+
/*b*/ { 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4 },
|
|
115
|
+
/*c*/ { 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f },
|
|
116
|
+
/*d*/ { 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef },
|
|
117
|
+
/*e*/ { 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61 },
|
|
118
|
+
/*f*/ { 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d },
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
static uint8_t oaes_gf_mul_2[16][16] = {
|
|
122
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
123
|
+
/*0*/ { 0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e },
|
|
124
|
+
/*1*/ { 0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e },
|
|
125
|
+
/*2*/ { 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e },
|
|
126
|
+
/*3*/ { 0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e },
|
|
127
|
+
/*4*/ { 0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e },
|
|
128
|
+
/*5*/ { 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe },
|
|
129
|
+
/*6*/ { 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde },
|
|
130
|
+
/*7*/ { 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe },
|
|
131
|
+
/*8*/ { 0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05 },
|
|
132
|
+
/*9*/ { 0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25 },
|
|
133
|
+
/*a*/ { 0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45 },
|
|
134
|
+
/*b*/ { 0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65 },
|
|
135
|
+
/*c*/ { 0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85 },
|
|
136
|
+
/*d*/ { 0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5 },
|
|
137
|
+
/*e*/ { 0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5 },
|
|
138
|
+
/*f*/ { 0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5 },
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
static uint8_t oaes_gf_mul_3[16][16] = {
|
|
142
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
143
|
+
/*0*/ { 0x00, 0x03, 0x06, 0x05, 0x0c, 0x0f, 0x0a, 0x09, 0x18, 0x1b, 0x1e, 0x1d, 0x14, 0x17, 0x12, 0x11 },
|
|
144
|
+
/*1*/ { 0x30, 0x33, 0x36, 0x35, 0x3c, 0x3f, 0x3a, 0x39, 0x28, 0x2b, 0x2e, 0x2d, 0x24, 0x27, 0x22, 0x21 },
|
|
145
|
+
/*2*/ { 0x60, 0x63, 0x66, 0x65, 0x6c, 0x6f, 0x6a, 0x69, 0x78, 0x7b, 0x7e, 0x7d, 0x74, 0x77, 0x72, 0x71 },
|
|
146
|
+
/*3*/ { 0x50, 0x53, 0x56, 0x55, 0x5c, 0x5f, 0x5a, 0x59, 0x48, 0x4b, 0x4e, 0x4d, 0x44, 0x47, 0x42, 0x41 },
|
|
147
|
+
/*4*/ { 0xc0, 0xc3, 0xc6, 0xc5, 0xcc, 0xcf, 0xca, 0xc9, 0xd8, 0xdb, 0xde, 0xdd, 0xd4, 0xd7, 0xd2, 0xd1 },
|
|
148
|
+
/*5*/ { 0xf0, 0xf3, 0xf6, 0xf5, 0xfc, 0xff, 0xfa, 0xf9, 0xe8, 0xeb, 0xee, 0xed, 0xe4, 0xe7, 0xe2, 0xe1 },
|
|
149
|
+
/*6*/ { 0xa0, 0xa3, 0xa6, 0xa5, 0xac, 0xaf, 0xaa, 0xa9, 0xb8, 0xbb, 0xbe, 0xbd, 0xb4, 0xb7, 0xb2, 0xb1 },
|
|
150
|
+
/*7*/ { 0x90, 0x93, 0x96, 0x95, 0x9c, 0x9f, 0x9a, 0x99, 0x88, 0x8b, 0x8e, 0x8d, 0x84, 0x87, 0x82, 0x81 },
|
|
151
|
+
/*8*/ { 0x9b, 0x98, 0x9d, 0x9e, 0x97, 0x94, 0x91, 0x92, 0x83, 0x80, 0x85, 0x86, 0x8f, 0x8c, 0x89, 0x8a },
|
|
152
|
+
/*9*/ { 0xab, 0xa8, 0xad, 0xae, 0xa7, 0xa4, 0xa1, 0xa2, 0xb3, 0xb0, 0xb5, 0xb6, 0xbf, 0xbc, 0xb9, 0xba },
|
|
153
|
+
/*a*/ { 0xfb, 0xf8, 0xfd, 0xfe, 0xf7, 0xf4, 0xf1, 0xf2, 0xe3, 0xe0, 0xe5, 0xe6, 0xef, 0xec, 0xe9, 0xea },
|
|
154
|
+
/*b*/ { 0xcb, 0xc8, 0xcd, 0xce, 0xc7, 0xc4, 0xc1, 0xc2, 0xd3, 0xd0, 0xd5, 0xd6, 0xdf, 0xdc, 0xd9, 0xda },
|
|
155
|
+
/*c*/ { 0x5b, 0x58, 0x5d, 0x5e, 0x57, 0x54, 0x51, 0x52, 0x43, 0x40, 0x45, 0x46, 0x4f, 0x4c, 0x49, 0x4a },
|
|
156
|
+
/*d*/ { 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62, 0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a },
|
|
157
|
+
/*e*/ { 0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a },
|
|
158
|
+
/*f*/ { 0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a },
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
static uint8_t oaes_gf_mul_9[16][16] = {
|
|
162
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
163
|
+
/*0*/ { 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77 },
|
|
164
|
+
/*1*/ { 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7 },
|
|
165
|
+
/*2*/ { 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c },
|
|
166
|
+
/*3*/ { 0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc },
|
|
167
|
+
/*4*/ { 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01 },
|
|
168
|
+
/*5*/ { 0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91 },
|
|
169
|
+
/*6*/ { 0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a },
|
|
170
|
+
/*7*/ { 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa },
|
|
171
|
+
/*8*/ { 0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b },
|
|
172
|
+
/*9*/ { 0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b },
|
|
173
|
+
/*a*/ { 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0 },
|
|
174
|
+
/*b*/ { 0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30 },
|
|
175
|
+
/*c*/ { 0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed },
|
|
176
|
+
/*d*/ { 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d },
|
|
177
|
+
/*e*/ { 0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6 },
|
|
178
|
+
/*f*/ { 0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46 },
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
static uint8_t oaes_gf_mul_b[16][16] = {
|
|
182
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
183
|
+
/*0*/ { 0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69 },
|
|
184
|
+
/*1*/ { 0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9 },
|
|
185
|
+
/*2*/ { 0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12 },
|
|
186
|
+
/*3*/ { 0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2 },
|
|
187
|
+
/*4*/ { 0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f },
|
|
188
|
+
/*5*/ { 0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f },
|
|
189
|
+
/*6*/ { 0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4 },
|
|
190
|
+
/*7*/ { 0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54 },
|
|
191
|
+
/*8*/ { 0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e },
|
|
192
|
+
/*9*/ { 0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e },
|
|
193
|
+
/*a*/ { 0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5 },
|
|
194
|
+
/*b*/ { 0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55 },
|
|
195
|
+
/*c*/ { 0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68 },
|
|
196
|
+
/*d*/ { 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8 },
|
|
197
|
+
/*e*/ { 0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13 },
|
|
198
|
+
/*f*/ { 0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3 },
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
static uint8_t oaes_gf_mul_d[16][16] = {
|
|
202
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
203
|
+
/*0*/ { 0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b },
|
|
204
|
+
/*1*/ { 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b },
|
|
205
|
+
/*2*/ { 0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0 },
|
|
206
|
+
/*3*/ { 0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20 },
|
|
207
|
+
/*4*/ { 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26 },
|
|
208
|
+
/*5*/ { 0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6 },
|
|
209
|
+
/*6*/ { 0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d },
|
|
210
|
+
/*7*/ { 0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d },
|
|
211
|
+
/*8*/ { 0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91 },
|
|
212
|
+
/*9*/ { 0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41 },
|
|
213
|
+
/*a*/ { 0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a },
|
|
214
|
+
/*b*/ { 0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa },
|
|
215
|
+
/*c*/ { 0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc },
|
|
216
|
+
/*d*/ { 0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c },
|
|
217
|
+
/*e*/ { 0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47 },
|
|
218
|
+
/*f*/ { 0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97 },
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
static uint8_t oaes_gf_mul_e[16][16] = {
|
|
222
|
+
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,
|
|
223
|
+
/*0*/ { 0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a },
|
|
224
|
+
/*1*/ { 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba },
|
|
225
|
+
/*2*/ { 0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81 },
|
|
226
|
+
/*3*/ { 0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61 },
|
|
227
|
+
/*4*/ { 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7 },
|
|
228
|
+
/*5*/ { 0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17 },
|
|
229
|
+
/*6*/ { 0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c },
|
|
230
|
+
/*7*/ { 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc },
|
|
231
|
+
/*8*/ { 0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b },
|
|
232
|
+
/*9*/ { 0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb },
|
|
233
|
+
/*a*/ { 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0 },
|
|
234
|
+
/*b*/ { 0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20 },
|
|
235
|
+
/*c*/ { 0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6 },
|
|
236
|
+
/*d*/ { 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56 },
|
|
237
|
+
/*e*/ { 0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d },
|
|
238
|
+
/*f*/ { 0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d },
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
static OAES_RET oaes_sub_byte( uint8_t * byte )
|
|
242
|
+
{
|
|
243
|
+
size_t _x, _y;
|
|
244
|
+
|
|
245
|
+
if( NULL == byte )
|
|
246
|
+
return OAES_RET_ARG1;
|
|
247
|
+
|
|
248
|
+
_x = _y = *byte;
|
|
249
|
+
_x &= 0x0f;
|
|
250
|
+
_y &= 0xf0;
|
|
251
|
+
_y >>= 4;
|
|
252
|
+
*byte = oaes_sub_byte_value[_y][_x];
|
|
253
|
+
|
|
254
|
+
return OAES_RET_SUCCESS;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
static OAES_RET oaes_inv_sub_byte( uint8_t * byte )
|
|
258
|
+
{
|
|
259
|
+
size_t _x, _y;
|
|
260
|
+
|
|
261
|
+
if( NULL == byte )
|
|
262
|
+
return OAES_RET_ARG1;
|
|
263
|
+
|
|
264
|
+
_x = _y = *byte;
|
|
265
|
+
_x &= 0x0f;
|
|
266
|
+
_y &= 0xf0;
|
|
267
|
+
_y >>= 4;
|
|
268
|
+
*byte = oaes_inv_sub_byte_value[_y][_x];
|
|
269
|
+
|
|
270
|
+
return OAES_RET_SUCCESS;
|
|
271
|
+
}
|
|
272
|
+
/*
|
|
273
|
+
static OAES_RET oaes_word_rot_right( uint8_t word[OAES_COL_LEN] )
|
|
274
|
+
{
|
|
275
|
+
uint8_t _temp[OAES_COL_LEN];
|
|
276
|
+
|
|
277
|
+
if( NULL == word )
|
|
278
|
+
return OAES_RET_ARG1;
|
|
279
|
+
|
|
280
|
+
memcpy( _temp + 1, word, OAES_COL_LEN - 1 );
|
|
281
|
+
_temp[0] = word[OAES_COL_LEN - 1];
|
|
282
|
+
memcpy( word, _temp, OAES_COL_LEN );
|
|
283
|
+
|
|
284
|
+
return OAES_RET_SUCCESS;
|
|
285
|
+
}
|
|
286
|
+
*/
|
|
287
|
+
static OAES_RET oaes_word_rot_left( uint8_t word[OAES_COL_LEN] )
|
|
288
|
+
{
|
|
289
|
+
uint8_t _temp[OAES_COL_LEN];
|
|
290
|
+
|
|
291
|
+
if( NULL == word )
|
|
292
|
+
return OAES_RET_ARG1;
|
|
293
|
+
|
|
294
|
+
memcpy( _temp, word + 1, OAES_COL_LEN - 1 );
|
|
295
|
+
_temp[OAES_COL_LEN - 1] = word[0];
|
|
296
|
+
memcpy( word, _temp, OAES_COL_LEN );
|
|
297
|
+
|
|
298
|
+
return OAES_RET_SUCCESS;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
static OAES_RET oaes_shift_rows( uint8_t block[OAES_BLOCK_SIZE] )
|
|
302
|
+
{
|
|
303
|
+
uint8_t _temp[OAES_BLOCK_SIZE];
|
|
304
|
+
|
|
305
|
+
if( NULL == block )
|
|
306
|
+
return OAES_RET_ARG1;
|
|
307
|
+
|
|
308
|
+
_temp[0x00] = block[0x00];
|
|
309
|
+
_temp[0x01] = block[0x05];
|
|
310
|
+
_temp[0x02] = block[0x0a];
|
|
311
|
+
_temp[0x03] = block[0x0f];
|
|
312
|
+
_temp[0x04] = block[0x04];
|
|
313
|
+
_temp[0x05] = block[0x09];
|
|
314
|
+
_temp[0x06] = block[0x0e];
|
|
315
|
+
_temp[0x07] = block[0x03];
|
|
316
|
+
_temp[0x08] = block[0x08];
|
|
317
|
+
_temp[0x09] = block[0x0d];
|
|
318
|
+
_temp[0x0a] = block[0x02];
|
|
319
|
+
_temp[0x0b] = block[0x07];
|
|
320
|
+
_temp[0x0c] = block[0x0c];
|
|
321
|
+
_temp[0x0d] = block[0x01];
|
|
322
|
+
_temp[0x0e] = block[0x06];
|
|
323
|
+
_temp[0x0f] = block[0x0b];
|
|
324
|
+
memcpy( block, _temp, OAES_BLOCK_SIZE );
|
|
325
|
+
|
|
326
|
+
return OAES_RET_SUCCESS;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
static OAES_RET oaes_inv_shift_rows( uint8_t block[OAES_BLOCK_SIZE] )
|
|
330
|
+
{
|
|
331
|
+
uint8_t _temp[OAES_BLOCK_SIZE];
|
|
332
|
+
|
|
333
|
+
if( NULL == block )
|
|
334
|
+
return OAES_RET_ARG1;
|
|
335
|
+
|
|
336
|
+
_temp[0x00] = block[0x00];
|
|
337
|
+
_temp[0x01] = block[0x0d];
|
|
338
|
+
_temp[0x02] = block[0x0a];
|
|
339
|
+
_temp[0x03] = block[0x07];
|
|
340
|
+
_temp[0x04] = block[0x04];
|
|
341
|
+
_temp[0x05] = block[0x01];
|
|
342
|
+
_temp[0x06] = block[0x0e];
|
|
343
|
+
_temp[0x07] = block[0x0b];
|
|
344
|
+
_temp[0x08] = block[0x08];
|
|
345
|
+
_temp[0x09] = block[0x05];
|
|
346
|
+
_temp[0x0a] = block[0x02];
|
|
347
|
+
_temp[0x0b] = block[0x0f];
|
|
348
|
+
_temp[0x0c] = block[0x0c];
|
|
349
|
+
_temp[0x0d] = block[0x09];
|
|
350
|
+
_temp[0x0e] = block[0x06];
|
|
351
|
+
_temp[0x0f] = block[0x03];
|
|
352
|
+
memcpy( block, _temp, OAES_BLOCK_SIZE );
|
|
353
|
+
|
|
354
|
+
return OAES_RET_SUCCESS;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
static uint8_t oaes_gf_mul(uint8_t left, uint8_t right)
|
|
358
|
+
{
|
|
359
|
+
size_t _x, _y;
|
|
360
|
+
|
|
361
|
+
_x = _y = left;
|
|
362
|
+
_x &= 0x0f;
|
|
363
|
+
_y &= 0xf0;
|
|
364
|
+
_y >>= 4;
|
|
365
|
+
|
|
366
|
+
switch( right )
|
|
367
|
+
{
|
|
368
|
+
case 0x02:
|
|
369
|
+
return oaes_gf_mul_2[_y][_x];
|
|
370
|
+
break;
|
|
371
|
+
case 0x03:
|
|
372
|
+
return oaes_gf_mul_3[_y][_x];
|
|
373
|
+
break;
|
|
374
|
+
case 0x09:
|
|
375
|
+
return oaes_gf_mul_9[_y][_x];
|
|
376
|
+
break;
|
|
377
|
+
case 0x0b:
|
|
378
|
+
return oaes_gf_mul_b[_y][_x];
|
|
379
|
+
break;
|
|
380
|
+
case 0x0d:
|
|
381
|
+
return oaes_gf_mul_d[_y][_x];
|
|
382
|
+
break;
|
|
383
|
+
case 0x0e:
|
|
384
|
+
return oaes_gf_mul_e[_y][_x];
|
|
385
|
+
break;
|
|
386
|
+
default:
|
|
387
|
+
return left;
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
static OAES_RET oaes_mix_cols( uint8_t word[OAES_COL_LEN] )
|
|
393
|
+
{
|
|
394
|
+
uint8_t _temp[OAES_COL_LEN];
|
|
395
|
+
|
|
396
|
+
if( NULL == word )
|
|
397
|
+
return OAES_RET_ARG1;
|
|
398
|
+
|
|
399
|
+
_temp[0] = oaes_gf_mul(word[0], 0x02) ^ oaes_gf_mul( word[1], 0x03 ) ^
|
|
400
|
+
word[2] ^ word[3];
|
|
401
|
+
_temp[1] = word[0] ^ oaes_gf_mul( word[1], 0x02 ) ^
|
|
402
|
+
oaes_gf_mul( word[2], 0x03 ) ^ word[3];
|
|
403
|
+
_temp[2] = word[0] ^ word[1] ^
|
|
404
|
+
oaes_gf_mul( word[2], 0x02 ) ^ oaes_gf_mul( word[3], 0x03 );
|
|
405
|
+
_temp[3] = oaes_gf_mul( word[0], 0x03 ) ^ word[1] ^
|
|
406
|
+
word[2] ^ oaes_gf_mul( word[3], 0x02 );
|
|
407
|
+
memcpy( word, _temp, OAES_COL_LEN );
|
|
408
|
+
|
|
409
|
+
return OAES_RET_SUCCESS;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
static OAES_RET oaes_inv_mix_cols( uint8_t word[OAES_COL_LEN] )
|
|
413
|
+
{
|
|
414
|
+
uint8_t _temp[OAES_COL_LEN];
|
|
415
|
+
|
|
416
|
+
if( NULL == word )
|
|
417
|
+
return OAES_RET_ARG1;
|
|
418
|
+
|
|
419
|
+
_temp[0] = oaes_gf_mul( word[0], 0x0e ) ^ oaes_gf_mul( word[1], 0x0b ) ^
|
|
420
|
+
oaes_gf_mul( word[2], 0x0d ) ^ oaes_gf_mul( word[3], 0x09 );
|
|
421
|
+
_temp[1] = oaes_gf_mul( word[0], 0x09 ) ^ oaes_gf_mul( word[1], 0x0e ) ^
|
|
422
|
+
oaes_gf_mul( word[2], 0x0b ) ^ oaes_gf_mul( word[3], 0x0d );
|
|
423
|
+
_temp[2] = oaes_gf_mul( word[0], 0x0d ) ^ oaes_gf_mul( word[1], 0x09 ) ^
|
|
424
|
+
oaes_gf_mul( word[2], 0x0e ) ^ oaes_gf_mul( word[3], 0x0b );
|
|
425
|
+
_temp[3] = oaes_gf_mul( word[0], 0x0b ) ^ oaes_gf_mul( word[1], 0x0d ) ^
|
|
426
|
+
oaes_gf_mul( word[2], 0x09 ) ^ oaes_gf_mul( word[3], 0x0e );
|
|
427
|
+
memcpy( word, _temp, OAES_COL_LEN );
|
|
428
|
+
|
|
429
|
+
return OAES_RET_SUCCESS;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
OAES_RET oaes_sprintf(
|
|
433
|
+
char * buf, size_t * buf_len, const uint8_t * data, size_t data_len )
|
|
434
|
+
{
|
|
435
|
+
size_t _i, _buf_len_in;
|
|
436
|
+
char _temp[4];
|
|
437
|
+
|
|
438
|
+
if( NULL == buf_len )
|
|
439
|
+
return OAES_RET_ARG2;
|
|
440
|
+
|
|
441
|
+
_buf_len_in = *buf_len;
|
|
442
|
+
*buf_len = data_len * 3 + data_len / OAES_BLOCK_SIZE + 1;
|
|
443
|
+
|
|
444
|
+
if( NULL == buf )
|
|
445
|
+
return OAES_RET_SUCCESS;
|
|
446
|
+
|
|
447
|
+
if( *buf_len > _buf_len_in )
|
|
448
|
+
return OAES_RET_BUF;
|
|
449
|
+
|
|
450
|
+
if( NULL == data )
|
|
451
|
+
return OAES_RET_ARG3;
|
|
452
|
+
|
|
453
|
+
strcpy( buf, "" );
|
|
454
|
+
|
|
455
|
+
for( _i = 0; _i < data_len; _i++ )
|
|
456
|
+
{
|
|
457
|
+
sprintf( _temp, "%02x ", data[_i] );
|
|
458
|
+
strcat( buf, _temp );
|
|
459
|
+
if( _i && 0 == ( _i + 1 ) % OAES_BLOCK_SIZE )
|
|
460
|
+
strcat( buf, "\n" );
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return OAES_RET_SUCCESS;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
#ifdef OAES_HAVE_ISAAC
|
|
467
|
+
static void oaes_get_seed( char buf[RANDSIZ + 1] )
|
|
468
|
+
{
|
|
469
|
+
struct timeb timer;
|
|
470
|
+
struct tm *gmTimer;
|
|
471
|
+
char * _test = NULL;
|
|
472
|
+
|
|
473
|
+
ftime (&timer);
|
|
474
|
+
gmTimer = gmtime( &timer.time );
|
|
475
|
+
_test = (char *) calloc( sizeof( char ), timer.millitm );
|
|
476
|
+
sprintf( buf, "%04d%02d%02d%02d%02d%02d%03d%p%d",
|
|
477
|
+
gmTimer->tm_year + 1900, gmTimer->tm_mon + 1, gmTimer->tm_mday,
|
|
478
|
+
gmTimer->tm_hour, gmTimer->tm_min, gmTimer->tm_sec, timer.millitm,
|
|
479
|
+
_test + timer.millitm, getpid() );
|
|
480
|
+
|
|
481
|
+
if( _test )
|
|
482
|
+
free( _test );
|
|
483
|
+
}
|
|
484
|
+
#else
|
|
485
|
+
static uint32_t oaes_get_seed(void)
|
|
486
|
+
{
|
|
487
|
+
struct timeb timer;
|
|
488
|
+
struct tm *gmTimer;
|
|
489
|
+
char * _test = NULL;
|
|
490
|
+
uint32_t _ret = 0;
|
|
491
|
+
|
|
492
|
+
ftime (&timer);
|
|
493
|
+
gmTimer = gmtime( &timer.time );
|
|
494
|
+
_test = (char *) calloc( sizeof( char ), timer.millitm );
|
|
495
|
+
_ret = (uint32_t)(gmTimer->tm_year + 1900 + gmTimer->tm_mon + 1 + gmTimer->tm_mday +
|
|
496
|
+
gmTimer->tm_hour + gmTimer->tm_min + gmTimer->tm_sec + timer.millitm +
|
|
497
|
+
(uintptr_t) ( _test + timer.millitm ) + getpid());
|
|
498
|
+
|
|
499
|
+
if( _test )
|
|
500
|
+
free( _test );
|
|
501
|
+
|
|
502
|
+
return _ret;
|
|
503
|
+
}
|
|
504
|
+
#endif // OAES_HAVE_ISAAC
|
|
505
|
+
|
|
506
|
+
static OAES_RET oaes_key_destroy( oaes_key ** key )
|
|
507
|
+
{
|
|
508
|
+
if( NULL == *key )
|
|
509
|
+
return OAES_RET_SUCCESS;
|
|
510
|
+
|
|
511
|
+
if( (*key)->data )
|
|
512
|
+
{
|
|
513
|
+
free( (*key)->data );
|
|
514
|
+
(*key)->data = NULL;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if( (*key)->exp_data )
|
|
518
|
+
{
|
|
519
|
+
free( (*key)->exp_data );
|
|
520
|
+
(*key)->exp_data = NULL;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
(*key)->data_len = 0;
|
|
524
|
+
(*key)->exp_data_len = 0;
|
|
525
|
+
(*key)->num_keys = 0;
|
|
526
|
+
(*key)->key_base = 0;
|
|
527
|
+
free( *key );
|
|
528
|
+
*key = NULL;
|
|
529
|
+
|
|
530
|
+
return OAES_RET_SUCCESS;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
static OAES_RET oaes_key_expand( OAES_CTX * ctx )
|
|
534
|
+
{
|
|
535
|
+
size_t _i, _j;
|
|
536
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
537
|
+
|
|
538
|
+
if( NULL == _ctx )
|
|
539
|
+
return OAES_RET_ARG1;
|
|
540
|
+
|
|
541
|
+
if( NULL == _ctx->key )
|
|
542
|
+
return OAES_RET_NOKEY;
|
|
543
|
+
|
|
544
|
+
_ctx->key->key_base = _ctx->key->data_len / OAES_RKEY_LEN;
|
|
545
|
+
_ctx->key->num_keys = _ctx->key->key_base + OAES_ROUND_BASE;
|
|
546
|
+
|
|
547
|
+
_ctx->key->exp_data_len = _ctx->key->num_keys * OAES_RKEY_LEN * OAES_COL_LEN;
|
|
548
|
+
_ctx->key->exp_data = (uint8_t *)
|
|
549
|
+
calloc( _ctx->key->exp_data_len, sizeof( uint8_t ));
|
|
550
|
+
|
|
551
|
+
if( NULL == _ctx->key->exp_data )
|
|
552
|
+
return OAES_RET_MEM;
|
|
553
|
+
|
|
554
|
+
// the first _ctx->key->data_len are a direct copy
|
|
555
|
+
memcpy( _ctx->key->exp_data, _ctx->key->data, _ctx->key->data_len );
|
|
556
|
+
|
|
557
|
+
// apply ExpandKey algorithm for remainder
|
|
558
|
+
for( _i = _ctx->key->key_base; _i < _ctx->key->num_keys * OAES_RKEY_LEN; _i++ )
|
|
559
|
+
{
|
|
560
|
+
uint8_t _temp[OAES_COL_LEN];
|
|
561
|
+
|
|
562
|
+
memcpy( _temp,
|
|
563
|
+
_ctx->key->exp_data + ( _i - 1 ) * OAES_RKEY_LEN, OAES_COL_LEN );
|
|
564
|
+
|
|
565
|
+
// transform key column
|
|
566
|
+
if( 0 == _i % _ctx->key->key_base )
|
|
567
|
+
{
|
|
568
|
+
oaes_word_rot_left( _temp );
|
|
569
|
+
|
|
570
|
+
for( _j = 0; _j < OAES_COL_LEN; _j++ )
|
|
571
|
+
oaes_sub_byte( _temp + _j );
|
|
572
|
+
|
|
573
|
+
_temp[0] = _temp[0] ^ oaes_gf_8[ _i / _ctx->key->key_base - 1 ];
|
|
574
|
+
}
|
|
575
|
+
else if( _ctx->key->key_base > 6 && 4 == _i % _ctx->key->key_base )
|
|
576
|
+
{
|
|
577
|
+
for( _j = 0; _j < OAES_COL_LEN; _j++ )
|
|
578
|
+
oaes_sub_byte( _temp + _j );
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
for( _j = 0; _j < OAES_COL_LEN; _j++ )
|
|
582
|
+
{
|
|
583
|
+
_ctx->key->exp_data[ _i * OAES_RKEY_LEN + _j ] =
|
|
584
|
+
_ctx->key->exp_data[ ( _i - _ctx->key->key_base ) *
|
|
585
|
+
OAES_RKEY_LEN + _j ] ^ _temp[_j];
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
return OAES_RET_SUCCESS;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
static OAES_RET oaes_key_gen( OAES_CTX * ctx, size_t key_size )
|
|
593
|
+
{
|
|
594
|
+
size_t _i;
|
|
595
|
+
oaes_key * _key = NULL;
|
|
596
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
597
|
+
OAES_RET _rc = OAES_RET_SUCCESS;
|
|
598
|
+
|
|
599
|
+
if( NULL == _ctx )
|
|
600
|
+
return OAES_RET_ARG1;
|
|
601
|
+
|
|
602
|
+
_key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
|
|
603
|
+
|
|
604
|
+
if( NULL == _key )
|
|
605
|
+
return OAES_RET_MEM;
|
|
606
|
+
|
|
607
|
+
if( _ctx->key )
|
|
608
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
609
|
+
|
|
610
|
+
_key->data_len = key_size;
|
|
611
|
+
_key->data = (uint8_t *) calloc( key_size, sizeof( uint8_t ));
|
|
612
|
+
|
|
613
|
+
if( NULL == _key->data )
|
|
614
|
+
return OAES_RET_MEM;
|
|
615
|
+
|
|
616
|
+
for( _i = 0; _i < key_size; _i++ )
|
|
617
|
+
#ifdef OAES_HAVE_ISAAC
|
|
618
|
+
_key->data[_i] = (uint8_t) rand( _ctx->rctx );
|
|
619
|
+
#else
|
|
620
|
+
_key->data[_i] = (uint8_t) rand();
|
|
621
|
+
#endif // OAES_HAVE_ISAAC
|
|
622
|
+
|
|
623
|
+
_ctx->key = _key;
|
|
624
|
+
_rc = _rc || oaes_key_expand( ctx );
|
|
625
|
+
|
|
626
|
+
if( _rc != OAES_RET_SUCCESS )
|
|
627
|
+
{
|
|
628
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
629
|
+
return _rc;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return OAES_RET_SUCCESS;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
OAES_RET oaes_key_gen_128( OAES_CTX * ctx )
|
|
636
|
+
{
|
|
637
|
+
return oaes_key_gen( ctx, 16 );
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
OAES_RET oaes_key_gen_192( OAES_CTX * ctx )
|
|
641
|
+
{
|
|
642
|
+
return oaes_key_gen( ctx, 24 );
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
OAES_RET oaes_key_gen_256( OAES_CTX * ctx )
|
|
646
|
+
{
|
|
647
|
+
return oaes_key_gen( ctx, 32 );
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
OAES_RET oaes_key_export( OAES_CTX * ctx,
|
|
651
|
+
uint8_t * data, size_t * data_len )
|
|
652
|
+
{
|
|
653
|
+
size_t _data_len_in;
|
|
654
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
655
|
+
|
|
656
|
+
if( NULL == _ctx )
|
|
657
|
+
return OAES_RET_ARG1;
|
|
658
|
+
|
|
659
|
+
if( NULL == _ctx->key )
|
|
660
|
+
return OAES_RET_NOKEY;
|
|
661
|
+
|
|
662
|
+
if( NULL == data_len )
|
|
663
|
+
return OAES_RET_ARG3;
|
|
664
|
+
|
|
665
|
+
_data_len_in = *data_len;
|
|
666
|
+
// data + header
|
|
667
|
+
*data_len = _ctx->key->data_len + OAES_BLOCK_SIZE;
|
|
668
|
+
|
|
669
|
+
if( NULL == data )
|
|
670
|
+
return OAES_RET_SUCCESS;
|
|
671
|
+
|
|
672
|
+
if( _data_len_in < *data_len )
|
|
673
|
+
return OAES_RET_BUF;
|
|
674
|
+
|
|
675
|
+
// header
|
|
676
|
+
memcpy( data, oaes_header, OAES_BLOCK_SIZE );
|
|
677
|
+
data[5] = 0x01;
|
|
678
|
+
data[7] = (uint8_t)_ctx->key->data_len;
|
|
679
|
+
memcpy( data + OAES_BLOCK_SIZE, _ctx->key->data, _ctx->key->data_len );
|
|
680
|
+
|
|
681
|
+
return OAES_RET_SUCCESS;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
OAES_RET oaes_key_export_data( OAES_CTX * ctx,
|
|
685
|
+
uint8_t * data, size_t * data_len )
|
|
686
|
+
{
|
|
687
|
+
size_t _data_len_in;
|
|
688
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
689
|
+
|
|
690
|
+
if( NULL == _ctx )
|
|
691
|
+
return OAES_RET_ARG1;
|
|
692
|
+
|
|
693
|
+
if( NULL == _ctx->key )
|
|
694
|
+
return OAES_RET_NOKEY;
|
|
695
|
+
|
|
696
|
+
if( NULL == data_len )
|
|
697
|
+
return OAES_RET_ARG3;
|
|
698
|
+
|
|
699
|
+
_data_len_in = *data_len;
|
|
700
|
+
*data_len = _ctx->key->data_len;
|
|
701
|
+
|
|
702
|
+
if( NULL == data )
|
|
703
|
+
return OAES_RET_SUCCESS;
|
|
704
|
+
|
|
705
|
+
if( _data_len_in < *data_len )
|
|
706
|
+
return OAES_RET_BUF;
|
|
707
|
+
|
|
708
|
+
memcpy( data, _ctx->key->data, *data_len );
|
|
709
|
+
|
|
710
|
+
return OAES_RET_SUCCESS;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
OAES_RET oaes_key_import( OAES_CTX * ctx,
|
|
714
|
+
const uint8_t * data, size_t data_len )
|
|
715
|
+
{
|
|
716
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
717
|
+
OAES_RET _rc = OAES_RET_SUCCESS;
|
|
718
|
+
int _key_length;
|
|
719
|
+
|
|
720
|
+
if( NULL == _ctx )
|
|
721
|
+
return OAES_RET_ARG1;
|
|
722
|
+
|
|
723
|
+
if( NULL == data )
|
|
724
|
+
return OAES_RET_ARG2;
|
|
725
|
+
|
|
726
|
+
switch( data_len )
|
|
727
|
+
{
|
|
728
|
+
case 16 + OAES_BLOCK_SIZE:
|
|
729
|
+
case 24 + OAES_BLOCK_SIZE:
|
|
730
|
+
case 32 + OAES_BLOCK_SIZE:
|
|
731
|
+
break;
|
|
732
|
+
default:
|
|
733
|
+
return OAES_RET_ARG3;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// header
|
|
737
|
+
if( 0 != memcmp( data, oaes_header, 4 ) )
|
|
738
|
+
return OAES_RET_HEADER;
|
|
739
|
+
|
|
740
|
+
// header version
|
|
741
|
+
switch( data[4] )
|
|
742
|
+
{
|
|
743
|
+
case 0x01:
|
|
744
|
+
break;
|
|
745
|
+
default:
|
|
746
|
+
return OAES_RET_HEADER;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// header type
|
|
750
|
+
switch( data[5] )
|
|
751
|
+
{
|
|
752
|
+
case 0x01:
|
|
753
|
+
break;
|
|
754
|
+
default:
|
|
755
|
+
return OAES_RET_HEADER;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// options
|
|
759
|
+
_key_length = data[7];
|
|
760
|
+
switch( _key_length )
|
|
761
|
+
{
|
|
762
|
+
case 16:
|
|
763
|
+
case 24:
|
|
764
|
+
case 32:
|
|
765
|
+
break;
|
|
766
|
+
default:
|
|
767
|
+
return OAES_RET_HEADER;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
if( (int)data_len != _key_length + OAES_BLOCK_SIZE )
|
|
771
|
+
return OAES_RET_ARG3;
|
|
772
|
+
|
|
773
|
+
if( _ctx->key )
|
|
774
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
775
|
+
|
|
776
|
+
_ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
|
|
777
|
+
|
|
778
|
+
if( NULL == _ctx->key )
|
|
779
|
+
return OAES_RET_MEM;
|
|
780
|
+
|
|
781
|
+
_ctx->key->data_len = _key_length;
|
|
782
|
+
_ctx->key->data = (uint8_t *)
|
|
783
|
+
calloc( _key_length, sizeof( uint8_t ));
|
|
784
|
+
|
|
785
|
+
if( NULL == _ctx->key->data )
|
|
786
|
+
{
|
|
787
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
788
|
+
return OAES_RET_MEM;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
memcpy( _ctx->key->data, data + OAES_BLOCK_SIZE, _key_length );
|
|
792
|
+
_rc = _rc || oaes_key_expand( ctx );
|
|
793
|
+
|
|
794
|
+
if( _rc != OAES_RET_SUCCESS )
|
|
795
|
+
{
|
|
796
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
797
|
+
return _rc;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
return OAES_RET_SUCCESS;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
OAES_RET oaes_key_import_data( OAES_CTX * ctx,
|
|
804
|
+
const uint8_t * data, size_t data_len )
|
|
805
|
+
{
|
|
806
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
807
|
+
OAES_RET _rc = OAES_RET_SUCCESS;
|
|
808
|
+
|
|
809
|
+
if( NULL == _ctx )
|
|
810
|
+
return OAES_RET_ARG1;
|
|
811
|
+
|
|
812
|
+
if( NULL == data )
|
|
813
|
+
return OAES_RET_ARG2;
|
|
814
|
+
|
|
815
|
+
switch( data_len )
|
|
816
|
+
{
|
|
817
|
+
case 16:
|
|
818
|
+
case 24:
|
|
819
|
+
case 32:
|
|
820
|
+
break;
|
|
821
|
+
default:
|
|
822
|
+
return OAES_RET_ARG3;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
if( _ctx->key )
|
|
826
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
827
|
+
|
|
828
|
+
_ctx->key = (oaes_key *) calloc( sizeof( oaes_key ), 1 );
|
|
829
|
+
|
|
830
|
+
if( NULL == _ctx->key )
|
|
831
|
+
return OAES_RET_MEM;
|
|
832
|
+
|
|
833
|
+
_ctx->key->data_len = data_len;
|
|
834
|
+
_ctx->key->data = (uint8_t *)
|
|
835
|
+
calloc( data_len, sizeof( uint8_t ));
|
|
836
|
+
|
|
837
|
+
if( NULL == _ctx->key->data )
|
|
838
|
+
{
|
|
839
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
840
|
+
return OAES_RET_MEM;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
memcpy( _ctx->key->data, data, data_len );
|
|
844
|
+
_rc = _rc || oaes_key_expand( ctx );
|
|
845
|
+
|
|
846
|
+
if( _rc != OAES_RET_SUCCESS )
|
|
847
|
+
{
|
|
848
|
+
oaes_key_destroy( &(_ctx->key) );
|
|
849
|
+
return _rc;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
return OAES_RET_SUCCESS;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
OAES_CTX * oaes_alloc(void)
|
|
856
|
+
{
|
|
857
|
+
oaes_ctx * _ctx = (oaes_ctx *) calloc( sizeof( oaes_ctx ), 1 );
|
|
858
|
+
|
|
859
|
+
if( NULL == _ctx )
|
|
860
|
+
return NULL;
|
|
861
|
+
|
|
862
|
+
#ifdef OAES_HAVE_ISAAC
|
|
863
|
+
{
|
|
864
|
+
ub4 _i = 0;
|
|
865
|
+
char _seed[RANDSIZ + 1];
|
|
866
|
+
|
|
867
|
+
_ctx->rctx = (randctx *) calloc( sizeof( randctx ), 1 );
|
|
868
|
+
|
|
869
|
+
if( NULL == _ctx->rctx )
|
|
870
|
+
{
|
|
871
|
+
free( _ctx );
|
|
872
|
+
return NULL;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
oaes_get_seed( _seed );
|
|
876
|
+
memset( _ctx->rctx->randrsl, 0, RANDSIZ );
|
|
877
|
+
memcpy( _ctx->rctx->randrsl, _seed, RANDSIZ );
|
|
878
|
+
randinit( _ctx->rctx, TRUE);
|
|
879
|
+
}
|
|
880
|
+
#else
|
|
881
|
+
srand( oaes_get_seed() );
|
|
882
|
+
#endif // OAES_HAVE_ISAAC
|
|
883
|
+
|
|
884
|
+
_ctx->key = NULL;
|
|
885
|
+
oaes_set_option( _ctx, OAES_OPTION_CBC, NULL );
|
|
886
|
+
|
|
887
|
+
#ifdef OAES_DEBUG
|
|
888
|
+
_ctx->step_cb = NULL;
|
|
889
|
+
oaes_set_option( _ctx, OAES_OPTION_STEP_OFF, NULL );
|
|
890
|
+
#endif // OAES_DEBUG
|
|
891
|
+
|
|
892
|
+
return (OAES_CTX *) _ctx;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
OAES_RET oaes_free( OAES_CTX ** ctx )
|
|
896
|
+
{
|
|
897
|
+
oaes_ctx ** _ctx = (oaes_ctx **) ctx;
|
|
898
|
+
|
|
899
|
+
if( NULL == _ctx )
|
|
900
|
+
return OAES_RET_ARG1;
|
|
901
|
+
|
|
902
|
+
if( NULL == *_ctx )
|
|
903
|
+
return OAES_RET_SUCCESS;
|
|
904
|
+
|
|
905
|
+
if( (*_ctx)->key )
|
|
906
|
+
oaes_key_destroy( &((*_ctx)->key) );
|
|
907
|
+
|
|
908
|
+
#ifdef OAES_HAVE_ISAAC
|
|
909
|
+
if( (*_ctx)->rctx )
|
|
910
|
+
{
|
|
911
|
+
free( (*_ctx)->rctx );
|
|
912
|
+
(*_ctx)->rctx = NULL;
|
|
913
|
+
}
|
|
914
|
+
#endif // OAES_HAVE_ISAAC
|
|
915
|
+
|
|
916
|
+
free( *_ctx );
|
|
917
|
+
*_ctx = NULL;
|
|
918
|
+
|
|
919
|
+
return OAES_RET_SUCCESS;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
OAES_RET oaes_set_option( OAES_CTX * ctx,
|
|
923
|
+
OAES_OPTION option, const void * value )
|
|
924
|
+
{
|
|
925
|
+
size_t _i;
|
|
926
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
927
|
+
|
|
928
|
+
if( NULL == _ctx )
|
|
929
|
+
return OAES_RET_ARG1;
|
|
930
|
+
|
|
931
|
+
switch( option )
|
|
932
|
+
{
|
|
933
|
+
case OAES_OPTION_ECB:
|
|
934
|
+
_ctx->options &= ~OAES_OPTION_CBC;
|
|
935
|
+
memset( _ctx->iv, 0, OAES_BLOCK_SIZE );
|
|
936
|
+
break;
|
|
937
|
+
|
|
938
|
+
case OAES_OPTION_CBC:
|
|
939
|
+
_ctx->options &= ~OAES_OPTION_ECB;
|
|
940
|
+
if( value )
|
|
941
|
+
memcpy( _ctx->iv, value, OAES_BLOCK_SIZE );
|
|
942
|
+
else
|
|
943
|
+
{
|
|
944
|
+
for( _i = 0; _i < OAES_BLOCK_SIZE; _i++ )
|
|
945
|
+
#ifdef OAES_HAVE_ISAAC
|
|
946
|
+
_ctx->iv[_i] = (uint8_t) rand( _ctx->rctx );
|
|
947
|
+
#else
|
|
948
|
+
_ctx->iv[_i] = (uint8_t) rand();
|
|
949
|
+
#endif // OAES_HAVE_ISAAC
|
|
950
|
+
}
|
|
951
|
+
break;
|
|
952
|
+
|
|
953
|
+
#ifdef OAES_DEBUG
|
|
954
|
+
|
|
955
|
+
case OAES_OPTION_STEP_ON:
|
|
956
|
+
if( value )
|
|
957
|
+
{
|
|
958
|
+
_ctx->options &= ~OAES_OPTION_STEP_OFF;
|
|
959
|
+
_ctx->step_cb = value;
|
|
960
|
+
}
|
|
961
|
+
else
|
|
962
|
+
{
|
|
963
|
+
_ctx->options &= ~OAES_OPTION_STEP_ON;
|
|
964
|
+
_ctx->options |= OAES_OPTION_STEP_OFF;
|
|
965
|
+
_ctx->step_cb = NULL;
|
|
966
|
+
return OAES_RET_ARG3;
|
|
967
|
+
}
|
|
968
|
+
break;
|
|
969
|
+
|
|
970
|
+
case OAES_OPTION_STEP_OFF:
|
|
971
|
+
_ctx->options &= ~OAES_OPTION_STEP_ON;
|
|
972
|
+
_ctx->step_cb = NULL;
|
|
973
|
+
break;
|
|
974
|
+
|
|
975
|
+
#endif // OAES_DEBUG
|
|
976
|
+
|
|
977
|
+
default:
|
|
978
|
+
return OAES_RET_ARG2;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
_ctx->options |= option;
|
|
982
|
+
|
|
983
|
+
return OAES_RET_SUCCESS;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
static OAES_RET oaes_encrypt_block(
|
|
987
|
+
OAES_CTX * ctx, uint8_t * c, size_t c_len )
|
|
988
|
+
{
|
|
989
|
+
size_t _i, _j;
|
|
990
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
991
|
+
|
|
992
|
+
if( NULL == _ctx )
|
|
993
|
+
return OAES_RET_ARG1;
|
|
994
|
+
|
|
995
|
+
if( NULL == c )
|
|
996
|
+
return OAES_RET_ARG2;
|
|
997
|
+
|
|
998
|
+
if( c_len != OAES_BLOCK_SIZE )
|
|
999
|
+
return OAES_RET_ARG3;
|
|
1000
|
+
|
|
1001
|
+
if( NULL == _ctx->key )
|
|
1002
|
+
return OAES_RET_NOKEY;
|
|
1003
|
+
|
|
1004
|
+
#ifdef OAES_DEBUG
|
|
1005
|
+
if( _ctx->step_cb )
|
|
1006
|
+
_ctx->step_cb( c, "input", 1, NULL );
|
|
1007
|
+
#endif // OAES_DEBUG
|
|
1008
|
+
|
|
1009
|
+
// AddRoundKey(State, K0)
|
|
1010
|
+
for( _i = 0; _i < c_len; _i++ )
|
|
1011
|
+
c[_i] = c[_i] ^ _ctx->key->exp_data[_i];
|
|
1012
|
+
|
|
1013
|
+
#ifdef OAES_DEBUG
|
|
1014
|
+
if( _ctx->step_cb )
|
|
1015
|
+
{
|
|
1016
|
+
_ctx->step_cb( _ctx->key->exp_data, "k_sch", 1, NULL );
|
|
1017
|
+
_ctx->step_cb( c, "k_add", 1, NULL );
|
|
1018
|
+
}
|
|
1019
|
+
#endif // OAES_DEBUG
|
|
1020
|
+
|
|
1021
|
+
// for round = 1 step 1 to Nr–1
|
|
1022
|
+
for( _i = 1; _i < _ctx->key->num_keys - 1; _i++ )
|
|
1023
|
+
{
|
|
1024
|
+
// SubBytes(state)
|
|
1025
|
+
for( _j = 0; _j < c_len; _j++ )
|
|
1026
|
+
oaes_sub_byte( c + _j );
|
|
1027
|
+
|
|
1028
|
+
#ifdef OAES_DEBUG
|
|
1029
|
+
if( _ctx->step_cb )
|
|
1030
|
+
_ctx->step_cb( c, "s_box", _i, NULL );
|
|
1031
|
+
#endif // OAES_DEBUG
|
|
1032
|
+
|
|
1033
|
+
// ShiftRows(state)
|
|
1034
|
+
oaes_shift_rows( c );
|
|
1035
|
+
|
|
1036
|
+
#ifdef OAES_DEBUG
|
|
1037
|
+
if( _ctx->step_cb )
|
|
1038
|
+
_ctx->step_cb( c, "s_row", _i, NULL );
|
|
1039
|
+
#endif // OAES_DEBUG
|
|
1040
|
+
|
|
1041
|
+
// MixColumns(state)
|
|
1042
|
+
oaes_mix_cols( c );
|
|
1043
|
+
oaes_mix_cols( c + 4 );
|
|
1044
|
+
oaes_mix_cols( c + 8 );
|
|
1045
|
+
oaes_mix_cols( c + 12 );
|
|
1046
|
+
|
|
1047
|
+
#ifdef OAES_DEBUG
|
|
1048
|
+
if( _ctx->step_cb )
|
|
1049
|
+
_ctx->step_cb( c, "m_col", _i, NULL );
|
|
1050
|
+
#endif // OAES_DEBUG
|
|
1051
|
+
|
|
1052
|
+
// AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
|
|
1053
|
+
for( _j = 0; _j < c_len; _j++ )
|
|
1054
|
+
c[_j] = c[_j] ^
|
|
1055
|
+
_ctx->key->exp_data[_i * OAES_RKEY_LEN * OAES_COL_LEN + _j];
|
|
1056
|
+
|
|
1057
|
+
#ifdef OAES_DEBUG
|
|
1058
|
+
if( _ctx->step_cb )
|
|
1059
|
+
{
|
|
1060
|
+
_ctx->step_cb( _ctx->key->exp_data + _i * OAES_RKEY_LEN * OAES_COL_LEN,
|
|
1061
|
+
"k_sch", _i, NULL );
|
|
1062
|
+
_ctx->step_cb( c, "k_add", _i, NULL );
|
|
1063
|
+
}
|
|
1064
|
+
#endif // OAES_DEBUG
|
|
1065
|
+
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// SubBytes(state)
|
|
1069
|
+
for( _i = 0; _i < c_len; _i++ )
|
|
1070
|
+
oaes_sub_byte( c + _i );
|
|
1071
|
+
|
|
1072
|
+
#ifdef OAES_DEBUG
|
|
1073
|
+
if( _ctx->step_cb )
|
|
1074
|
+
_ctx->step_cb( c, "s_box", _ctx->key->num_keys - 1, NULL );
|
|
1075
|
+
#endif // OAES_DEBUG
|
|
1076
|
+
|
|
1077
|
+
// ShiftRows(state)
|
|
1078
|
+
oaes_shift_rows( c );
|
|
1079
|
+
|
|
1080
|
+
#ifdef OAES_DEBUG
|
|
1081
|
+
if( _ctx->step_cb )
|
|
1082
|
+
_ctx->step_cb( c, "s_row", _ctx->key->num_keys - 1, NULL );
|
|
1083
|
+
#endif // OAES_DEBUG
|
|
1084
|
+
|
|
1085
|
+
// AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
|
|
1086
|
+
for( _i = 0; _i < c_len; _i++ )
|
|
1087
|
+
c[_i] = c[_i] ^ _ctx->key->exp_data[
|
|
1088
|
+
( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN + _i ];
|
|
1089
|
+
|
|
1090
|
+
#ifdef OAES_DEBUG
|
|
1091
|
+
if( _ctx->step_cb )
|
|
1092
|
+
{
|
|
1093
|
+
_ctx->step_cb( _ctx->key->exp_data +
|
|
1094
|
+
( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN,
|
|
1095
|
+
"k_sch", _ctx->key->num_keys - 1, NULL );
|
|
1096
|
+
_ctx->step_cb( c, "output", _ctx->key->num_keys - 1, NULL );
|
|
1097
|
+
}
|
|
1098
|
+
#endif // OAES_DEBUG
|
|
1099
|
+
|
|
1100
|
+
return OAES_RET_SUCCESS;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
static OAES_RET oaes_decrypt_block(
|
|
1104
|
+
OAES_CTX * ctx, uint8_t * c, size_t c_len )
|
|
1105
|
+
{
|
|
1106
|
+
size_t _i, _j;
|
|
1107
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
1108
|
+
|
|
1109
|
+
if( NULL == _ctx )
|
|
1110
|
+
return OAES_RET_ARG1;
|
|
1111
|
+
|
|
1112
|
+
if( NULL == c )
|
|
1113
|
+
return OAES_RET_ARG2;
|
|
1114
|
+
|
|
1115
|
+
if( c_len != OAES_BLOCK_SIZE )
|
|
1116
|
+
return OAES_RET_ARG3;
|
|
1117
|
+
|
|
1118
|
+
if( NULL == _ctx->key )
|
|
1119
|
+
return OAES_RET_NOKEY;
|
|
1120
|
+
|
|
1121
|
+
#ifdef OAES_DEBUG
|
|
1122
|
+
if( _ctx->step_cb )
|
|
1123
|
+
_ctx->step_cb( c, "iinput", _ctx->key->num_keys - 1, NULL );
|
|
1124
|
+
#endif // OAES_DEBUG
|
|
1125
|
+
|
|
1126
|
+
// AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
|
|
1127
|
+
for( _i = 0; _i < c_len; _i++ )
|
|
1128
|
+
c[_i] = c[_i] ^ _ctx->key->exp_data[
|
|
1129
|
+
( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN + _i ];
|
|
1130
|
+
|
|
1131
|
+
#ifdef OAES_DEBUG
|
|
1132
|
+
if( _ctx->step_cb )
|
|
1133
|
+
{
|
|
1134
|
+
_ctx->step_cb( _ctx->key->exp_data +
|
|
1135
|
+
( _ctx->key->num_keys - 1 ) * OAES_RKEY_LEN * OAES_COL_LEN,
|
|
1136
|
+
"ik_sch", _ctx->key->num_keys - 1, NULL );
|
|
1137
|
+
_ctx->step_cb( c, "ik_add", _ctx->key->num_keys - 1, NULL );
|
|
1138
|
+
}
|
|
1139
|
+
#endif // OAES_DEBUG
|
|
1140
|
+
|
|
1141
|
+
for( _i = _ctx->key->num_keys - 2; _i > 0; _i-- )
|
|
1142
|
+
{
|
|
1143
|
+
// InvShiftRows(state)
|
|
1144
|
+
oaes_inv_shift_rows( c );
|
|
1145
|
+
|
|
1146
|
+
#ifdef OAES_DEBUG
|
|
1147
|
+
if( _ctx->step_cb )
|
|
1148
|
+
_ctx->step_cb( c, "is_row", _i, NULL );
|
|
1149
|
+
#endif // OAES_DEBUG
|
|
1150
|
+
|
|
1151
|
+
// InvSubBytes(state)
|
|
1152
|
+
for( _j = 0; _j < c_len; _j++ )
|
|
1153
|
+
oaes_inv_sub_byte( c + _j );
|
|
1154
|
+
|
|
1155
|
+
#ifdef OAES_DEBUG
|
|
1156
|
+
if( _ctx->step_cb )
|
|
1157
|
+
_ctx->step_cb( c, "is_box", _i, NULL );
|
|
1158
|
+
#endif // OAES_DEBUG
|
|
1159
|
+
|
|
1160
|
+
// AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
|
|
1161
|
+
for( _j = 0; _j < c_len; _j++ )
|
|
1162
|
+
c[_j] = c[_j] ^
|
|
1163
|
+
_ctx->key->exp_data[_i * OAES_RKEY_LEN * OAES_COL_LEN + _j];
|
|
1164
|
+
|
|
1165
|
+
#ifdef OAES_DEBUG
|
|
1166
|
+
if( _ctx->step_cb )
|
|
1167
|
+
{
|
|
1168
|
+
_ctx->step_cb( _ctx->key->exp_data + _i * OAES_RKEY_LEN * OAES_COL_LEN,
|
|
1169
|
+
"ik_sch", _i, NULL );
|
|
1170
|
+
_ctx->step_cb( c, "ik_add", _i, NULL );
|
|
1171
|
+
}
|
|
1172
|
+
#endif // OAES_DEBUG
|
|
1173
|
+
|
|
1174
|
+
// InvMixColums(state)
|
|
1175
|
+
oaes_inv_mix_cols( c );
|
|
1176
|
+
oaes_inv_mix_cols( c + 4 );
|
|
1177
|
+
oaes_inv_mix_cols( c + 8 );
|
|
1178
|
+
oaes_inv_mix_cols( c + 12 );
|
|
1179
|
+
|
|
1180
|
+
#ifdef OAES_DEBUG
|
|
1181
|
+
if( _ctx->step_cb )
|
|
1182
|
+
_ctx->step_cb( c, "im_col", _i, NULL );
|
|
1183
|
+
#endif // OAES_DEBUG
|
|
1184
|
+
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
// InvShiftRows(state)
|
|
1188
|
+
oaes_inv_shift_rows( c );
|
|
1189
|
+
|
|
1190
|
+
#ifdef OAES_DEBUG
|
|
1191
|
+
if( _ctx->step_cb )
|
|
1192
|
+
_ctx->step_cb( c, "is_row", 1, NULL );
|
|
1193
|
+
#endif // OAES_DEBUG
|
|
1194
|
+
|
|
1195
|
+
// InvSubBytes(state)
|
|
1196
|
+
for( _i = 0; _i < c_len; _i++ )
|
|
1197
|
+
oaes_inv_sub_byte( c + _i );
|
|
1198
|
+
|
|
1199
|
+
#ifdef OAES_DEBUG
|
|
1200
|
+
if( _ctx->step_cb )
|
|
1201
|
+
_ctx->step_cb( c, "is_box", 1, NULL );
|
|
1202
|
+
#endif // OAES_DEBUG
|
|
1203
|
+
|
|
1204
|
+
// AddRoundKey(state, w[0, Nb-1])
|
|
1205
|
+
for( _i = 0; _i < c_len; _i++ )
|
|
1206
|
+
c[_i] = c[_i] ^ _ctx->key->exp_data[_i];
|
|
1207
|
+
|
|
1208
|
+
#ifdef OAES_DEBUG
|
|
1209
|
+
if( _ctx->step_cb )
|
|
1210
|
+
{
|
|
1211
|
+
_ctx->step_cb( _ctx->key->exp_data, "ik_sch", 1, NULL );
|
|
1212
|
+
_ctx->step_cb( c, "ioutput", 1, NULL );
|
|
1213
|
+
}
|
|
1214
|
+
#endif // OAES_DEBUG
|
|
1215
|
+
|
|
1216
|
+
return OAES_RET_SUCCESS;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
OAES_RET oaes_encrypt( OAES_CTX * ctx,
|
|
1220
|
+
const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len )
|
|
1221
|
+
{
|
|
1222
|
+
size_t _i, _j, _c_len_in, _c_data_len;
|
|
1223
|
+
size_t _pad_len = m_len % OAES_BLOCK_SIZE == 0 ?
|
|
1224
|
+
0 : OAES_BLOCK_SIZE - m_len % OAES_BLOCK_SIZE;
|
|
1225
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
1226
|
+
OAES_RET _rc = OAES_RET_SUCCESS;
|
|
1227
|
+
uint8_t _flags = _pad_len ? OAES_FLAG_PAD : 0;
|
|
1228
|
+
|
|
1229
|
+
if( NULL == _ctx )
|
|
1230
|
+
return OAES_RET_ARG1;
|
|
1231
|
+
|
|
1232
|
+
if( NULL == m )
|
|
1233
|
+
return OAES_RET_ARG2;
|
|
1234
|
+
|
|
1235
|
+
if( NULL == c_len )
|
|
1236
|
+
return OAES_RET_ARG5;
|
|
1237
|
+
|
|
1238
|
+
_c_len_in = *c_len;
|
|
1239
|
+
// data + pad
|
|
1240
|
+
_c_data_len = m_len + _pad_len;
|
|
1241
|
+
// header + iv + data + pad
|
|
1242
|
+
*c_len = 2 * OAES_BLOCK_SIZE + m_len + _pad_len;
|
|
1243
|
+
|
|
1244
|
+
if( NULL == c )
|
|
1245
|
+
return OAES_RET_SUCCESS;
|
|
1246
|
+
|
|
1247
|
+
if( _c_len_in < *c_len )
|
|
1248
|
+
return OAES_RET_BUF;
|
|
1249
|
+
|
|
1250
|
+
if( NULL == _ctx->key )
|
|
1251
|
+
return OAES_RET_NOKEY;
|
|
1252
|
+
|
|
1253
|
+
// header
|
|
1254
|
+
memcpy(c, oaes_header, OAES_BLOCK_SIZE );
|
|
1255
|
+
memcpy(c + 6, &_ctx->options, sizeof(_ctx->options));
|
|
1256
|
+
memcpy(c + 8, &_flags, sizeof(_flags));
|
|
1257
|
+
// iv
|
|
1258
|
+
memcpy(c + OAES_BLOCK_SIZE, _ctx->iv, OAES_BLOCK_SIZE );
|
|
1259
|
+
// data
|
|
1260
|
+
memcpy(c + 2 * OAES_BLOCK_SIZE, m, m_len );
|
|
1261
|
+
|
|
1262
|
+
for( _i = 0; _i < _c_data_len; _i += OAES_BLOCK_SIZE )
|
|
1263
|
+
{
|
|
1264
|
+
uint8_t _block[OAES_BLOCK_SIZE];
|
|
1265
|
+
size_t _block_size = min( m_len - _i, OAES_BLOCK_SIZE );
|
|
1266
|
+
|
|
1267
|
+
memcpy( _block, c + 2 * OAES_BLOCK_SIZE + _i, _block_size );
|
|
1268
|
+
|
|
1269
|
+
// insert pad
|
|
1270
|
+
for( _j = 0; _j < OAES_BLOCK_SIZE - _block_size; _j++ )
|
|
1271
|
+
_block[ _block_size + _j ] = (uint8_t)(_j + 1);
|
|
1272
|
+
|
|
1273
|
+
// CBC
|
|
1274
|
+
if( _ctx->options & OAES_OPTION_CBC )
|
|
1275
|
+
{
|
|
1276
|
+
for( _j = 0; _j < OAES_BLOCK_SIZE; _j++ )
|
|
1277
|
+
_block[_j] = _block[_j] ^ _ctx->iv[_j];
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
_rc = _rc ||
|
|
1281
|
+
oaes_encrypt_block( ctx, _block, OAES_BLOCK_SIZE );
|
|
1282
|
+
memcpy( c + 2 * OAES_BLOCK_SIZE + _i, _block, OAES_BLOCK_SIZE );
|
|
1283
|
+
|
|
1284
|
+
if( _ctx->options & OAES_OPTION_CBC )
|
|
1285
|
+
memcpy( _ctx->iv, _block, OAES_BLOCK_SIZE );
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
return _rc;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
OAES_RET oaes_decrypt( OAES_CTX * ctx,
|
|
1292
|
+
const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len )
|
|
1293
|
+
{
|
|
1294
|
+
size_t _i, _j, _m_len_in;
|
|
1295
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
1296
|
+
OAES_RET _rc = OAES_RET_SUCCESS;
|
|
1297
|
+
uint8_t _iv[OAES_BLOCK_SIZE];
|
|
1298
|
+
uint8_t _flags;
|
|
1299
|
+
OAES_OPTION _options;
|
|
1300
|
+
|
|
1301
|
+
if( NULL == ctx )
|
|
1302
|
+
return OAES_RET_ARG1;
|
|
1303
|
+
|
|
1304
|
+
if( NULL == c )
|
|
1305
|
+
return OAES_RET_ARG2;
|
|
1306
|
+
|
|
1307
|
+
if( c_len % OAES_BLOCK_SIZE )
|
|
1308
|
+
return OAES_RET_ARG3;
|
|
1309
|
+
|
|
1310
|
+
if( NULL == m_len )
|
|
1311
|
+
return OAES_RET_ARG5;
|
|
1312
|
+
|
|
1313
|
+
_m_len_in = *m_len;
|
|
1314
|
+
*m_len = c_len - 2 * OAES_BLOCK_SIZE;
|
|
1315
|
+
|
|
1316
|
+
if( NULL == m )
|
|
1317
|
+
return OAES_RET_SUCCESS;
|
|
1318
|
+
|
|
1319
|
+
if( _m_len_in < *m_len )
|
|
1320
|
+
return OAES_RET_BUF;
|
|
1321
|
+
|
|
1322
|
+
if( NULL == _ctx->key )
|
|
1323
|
+
return OAES_RET_NOKEY;
|
|
1324
|
+
|
|
1325
|
+
// header
|
|
1326
|
+
if( 0 != memcmp( c, oaes_header, 4 ) )
|
|
1327
|
+
return OAES_RET_HEADER;
|
|
1328
|
+
|
|
1329
|
+
// header version
|
|
1330
|
+
switch( c[4] )
|
|
1331
|
+
{
|
|
1332
|
+
case 0x01:
|
|
1333
|
+
break;
|
|
1334
|
+
default:
|
|
1335
|
+
return OAES_RET_HEADER;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
// header type
|
|
1339
|
+
switch( c[5] )
|
|
1340
|
+
{
|
|
1341
|
+
case 0x02:
|
|
1342
|
+
break;
|
|
1343
|
+
default:
|
|
1344
|
+
return OAES_RET_HEADER;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
// options
|
|
1348
|
+
memcpy(&_options, c + 6, sizeof(_options));
|
|
1349
|
+
// validate that all options are valid
|
|
1350
|
+
if( _options & ~(
|
|
1351
|
+
OAES_OPTION_ECB
|
|
1352
|
+
| OAES_OPTION_CBC
|
|
1353
|
+
#ifdef OAES_DEBUG
|
|
1354
|
+
| OAES_OPTION_STEP_ON
|
|
1355
|
+
| OAES_OPTION_STEP_OFF
|
|
1356
|
+
#endif // OAES_DEBUG
|
|
1357
|
+
) )
|
|
1358
|
+
return OAES_RET_HEADER;
|
|
1359
|
+
if( ( _options & OAES_OPTION_ECB ) &&
|
|
1360
|
+
( _options & OAES_OPTION_CBC ) )
|
|
1361
|
+
return OAES_RET_HEADER;
|
|
1362
|
+
if( _options == OAES_OPTION_NONE )
|
|
1363
|
+
return OAES_RET_HEADER;
|
|
1364
|
+
|
|
1365
|
+
// flags
|
|
1366
|
+
memcpy(&_flags, c + 8, sizeof(_flags));
|
|
1367
|
+
// validate that all flags are valid
|
|
1368
|
+
if( _flags & ~(
|
|
1369
|
+
OAES_FLAG_PAD
|
|
1370
|
+
) )
|
|
1371
|
+
return OAES_RET_HEADER;
|
|
1372
|
+
|
|
1373
|
+
// iv
|
|
1374
|
+
memcpy( _iv, c + OAES_BLOCK_SIZE, OAES_BLOCK_SIZE);
|
|
1375
|
+
// data + pad
|
|
1376
|
+
memcpy( m, c + 2 * OAES_BLOCK_SIZE, *m_len );
|
|
1377
|
+
|
|
1378
|
+
for( _i = 0; _i < *m_len; _i += OAES_BLOCK_SIZE )
|
|
1379
|
+
{
|
|
1380
|
+
if( ( _options & OAES_OPTION_CBC ) && _i > 0 )
|
|
1381
|
+
memcpy( _iv, c + OAES_BLOCK_SIZE + _i, OAES_BLOCK_SIZE );
|
|
1382
|
+
|
|
1383
|
+
_rc = _rc ||
|
|
1384
|
+
oaes_decrypt_block( ctx, m + _i, min( *m_len - _i, OAES_BLOCK_SIZE ) );
|
|
1385
|
+
|
|
1386
|
+
// CBC
|
|
1387
|
+
if( _options & OAES_OPTION_CBC )
|
|
1388
|
+
{
|
|
1389
|
+
for( _j = 0; _j < OAES_BLOCK_SIZE; _j++ )
|
|
1390
|
+
m[ _i + _j ] = m[ _i + _j ] ^ _iv[_j];
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// remove pad
|
|
1395
|
+
if( _flags & OAES_FLAG_PAD )
|
|
1396
|
+
{
|
|
1397
|
+
int _is_pad = 1;
|
|
1398
|
+
size_t _temp = (size_t) m[*m_len - 1];
|
|
1399
|
+
|
|
1400
|
+
if( _temp <= 0x00 || _temp > 0x0f )
|
|
1401
|
+
return OAES_RET_HEADER;
|
|
1402
|
+
for( _i = 0; _i < _temp; _i++ )
|
|
1403
|
+
if( m[*m_len - 1 - _i] != _temp - _i )
|
|
1404
|
+
_is_pad = 0;
|
|
1405
|
+
if( _is_pad )
|
|
1406
|
+
{
|
|
1407
|
+
memset( m + *m_len - _temp, 0, _temp );
|
|
1408
|
+
*m_len -= _temp;
|
|
1409
|
+
}
|
|
1410
|
+
else
|
|
1411
|
+
return OAES_RET_HEADER;
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
return OAES_RET_SUCCESS;
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
OAES_API OAES_RET oaes_encryption_round( const uint8_t * key, uint8_t * c )
|
|
1419
|
+
{
|
|
1420
|
+
size_t _i;
|
|
1421
|
+
|
|
1422
|
+
if( NULL == key )
|
|
1423
|
+
return OAES_RET_ARG1;
|
|
1424
|
+
|
|
1425
|
+
if( NULL == c )
|
|
1426
|
+
return OAES_RET_ARG2;
|
|
1427
|
+
|
|
1428
|
+
// SubBytes(state)
|
|
1429
|
+
for( _i = 0; _i < OAES_BLOCK_SIZE; _i++ )
|
|
1430
|
+
oaes_sub_byte( c + _i );
|
|
1431
|
+
|
|
1432
|
+
// ShiftRows(state)
|
|
1433
|
+
oaes_shift_rows( c );
|
|
1434
|
+
|
|
1435
|
+
// MixColumns(state)
|
|
1436
|
+
oaes_mix_cols( c );
|
|
1437
|
+
oaes_mix_cols( c + 4 );
|
|
1438
|
+
oaes_mix_cols( c + 8 );
|
|
1439
|
+
oaes_mix_cols( c + 12 );
|
|
1440
|
+
|
|
1441
|
+
// AddRoundKey(State, key)
|
|
1442
|
+
for( _i = 0; _i < OAES_BLOCK_SIZE; _i++ )
|
|
1443
|
+
c[_i] ^= key[_i];
|
|
1444
|
+
|
|
1445
|
+
return OAES_RET_SUCCESS;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
OAES_API OAES_RET oaes_pseudo_encrypt_ecb( OAES_CTX * ctx, uint8_t * c )
|
|
1449
|
+
{
|
|
1450
|
+
size_t _i;
|
|
1451
|
+
oaes_ctx * _ctx = (oaes_ctx *) ctx;
|
|
1452
|
+
|
|
1453
|
+
if( NULL == _ctx )
|
|
1454
|
+
return OAES_RET_ARG1;
|
|
1455
|
+
|
|
1456
|
+
if( NULL == c )
|
|
1457
|
+
return OAES_RET_ARG2;
|
|
1458
|
+
|
|
1459
|
+
if( NULL == _ctx->key )
|
|
1460
|
+
return OAES_RET_NOKEY;
|
|
1461
|
+
|
|
1462
|
+
for ( _i = 0; _i < 10; ++_i )
|
|
1463
|
+
{
|
|
1464
|
+
oaes_encryption_round( &_ctx->key->exp_data[_i * OAES_RKEY_LEN * OAES_COL_LEN], c );
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
return OAES_RET_SUCCESS;
|
|
1468
|
+
}
|