diff --git a/.gitignore b/.gitignore index d747306..a3d2245 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,6 @@ build/ lib/ .vscode/ -test/totptest.* \ No newline at end of file +test/totptest.* + +Testing/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 295abf5..a936f5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,81 +1,33 @@ -cmake_minimum_required(VERSION 3.16.0) - -set(CMAKE_C_COMPILER clang) -set(CMAKE_CXX_COMPILER clang++) +cmake_minimum_required(VERSION 3.26.0) project(libpgfe - VERSION 0.6.1 + VERSION 0.7.0 DESCRIPTION "Cryptographic library" HOMEPAGE_URL "https://github.com/chardon55/libpgfe" LANGUAGES C CXX ) +if(CMAKE_TOOLCHAIN_FILE) + include(${CMAKE_TOOLCHAIN_FILE}) +endif() + set(CMAKE_C_STANDARD 11) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_C_STANDARD_REQUIRED ON) -include(CTest) -enable_testing() +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) set(src_dir "src") -set(src_c_dir "${src_dir}/c") -set(src_cpp_dir "${src_dir}/cpp") set(include_dir "include") -set(test_dir "test") - -add_library(pgfe SHARED - ${src_c_dir}/generic.c - ${src_c_dir}/generic-internal.c - ${src_c_dir}/utils.c - ${src_c_dir}/hash/sha-internal.c - ${src_c_dir}/hash/sha2-backend.c - ${src_c_dir}/hash/keccak-backend.c - ${src_c_dir}/hash/md5-backend.c - ${src_c_dir}/hash/sha1.c ${src_c_dir}/hash/sha224.c ${src_c_dir}/hash/sha256.c ${src_c_dir}/hash/sha384.c ${src_c_dir}/hash/sha512.c - ${src_c_dir}/hash/sha3-224.c ${src_c_dir}/hash/sha3-256.c ${src_c_dir}/hash/sha3-384.c ${src_c_dir}/hash/sha3-512.c - ${src_c_dir}/hash/md5.c - ${src_c_dir}/hash/shake.c - ${src_c_dir}/hmac/hmac.c - ${src_c_dir}/otp/otp-generic.c - ${src_c_dir}/otp/hotp.c ${src_c_dir}/otp/totp.c - ${src_c_dir}/base_encoding/base-encoding-internal.c - ${src_c_dir}/base_encoding/base64.c ${src_c_dir}/base_encoding/base32.c ${src_c_dir}/base_encoding/base16.c - ${src_c_dir}/blowfish/blowfish.c - ${src_cpp_dir}/generic.cpp - ${src_cpp_dir}/sequential_data.cpp - ${src_cpp_dir}/utils.cpp - ${src_cpp_dir}/algorithm_selectable.cpp - ${src_cpp_dir}/hash/hash_encoder.cpp - ${src_cpp_dir}/hmac/hmac_encoder.cpp - ${src_cpp_dir}/base_encoding/abstract_base_encoding.cpp - ${src_cpp_dir}/base_encoding/base16.cpp - ${src_cpp_dir}/base_encoding/base32.cpp - ${src_cpp_dir}/base_encoding/base64.cpp - ${src_cpp_dir}/otp/abstract_otp.cpp - ${src_cpp_dir}/otp/hotp.cpp ${src_cpp_dir}/otp/totp.cpp - ${src_cpp_dir}/misc/abstract_symmetric_crypter.cpp - ${src_cpp_dir}/blowfish/blowfish.cpp) -add_executable(pgfetest ${test_dir}/test.c) -add_executable(pgfetestcpp ${test_dir}/test.cpp) -# add_executable(totptest ${test_dir}/totptest.c) -# add_executable(totptestcpp ${test_dir}/totptest.cpp) +set(cmake_module_dir "cmake") -target_include_directories(pgfe PRIVATE - ${include_dir} -) +list(APPEND cmake_modules "libpgfe") -target_link_libraries(pgfetest pgfe) -target_link_libraries(pgfetestcpp pgfe) -# target_link_libraries(totptest pgfe) -# target_link_libraries(totptestcpp pgfe) +foreach(mod IN LISTS cmake_modules) + include(${cmake_module_dir}/targets/${mod}.cmake) +endforeach() -include(${test_dir}/cmake/test_meta.cmake) -include(${test_dir}/cmake/hash_tests.cmake) -include(${test_dir}/cmake/hmac_tests.cmake) -include(${test_dir}/cmake/util_tests.cmake) -include(${test_dir}/cmake/base_tests.cmake) -include(${test_dir}/cmake/otp_tests.cmake) -include(${test_dir}/cmake/shake_tests.cmake) -include(${test_dir}/cmake/blowfish_tests.cmake) +include(${cmake_module_dir}/tests.cmake) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 217b7c9..0000000 --- a/LICENSE +++ /dev/null @@ -1,28 +0,0 @@ -BSD 3-Clause License - -Copyright (c) 2022 Charles Dong - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..a0087cd --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Charles Dong + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile index 1d493f7..c8bd181 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ include metadata.mak -.PHONY: all rebuild clean test install uninstall init-scripts +.PHONY: all build rebuild clean test install uninstall init-scripts all: @echo 'Building...' - @cmake . -B $(BUILD_DIR) - @cmake --build $(BUILD_DIR) --config Release -j18 -- + @cmake . -B $(BUILD_DIR) -G Ninja -DCMAKE_TOOLCHAIN_FILE=$(DEFAULT_CMAKE_TOOLCHAIN_FILE) + @cmake --build $(BUILD_DIR) --config Release -j20 --target all -- @echo 'Building done' rebuild: clean all @@ -14,9 +14,9 @@ $(BUILD_DIR): @mkdir -vp $(BUILD_DIR) clean: - @echo 'Cleaning...' - @rm -rfv ./$(BUILD_DIR) - @echo 'Cleaning done' + @echo -ne 'Cleaning... ' + @rm -rf ./$(BUILD_DIR) + @echo 'done' test: all @ctest --test-dir $(BUILD_DIR) -C Release @@ -39,4 +39,48 @@ uninstall: @echo done init-scripts: - @chmod u+x ./scripts/{comment_header,update_meta,deploy} + @chmod u+x ./scripts/{comment_header.py,update_meta.py,deploy.py} + + +# Cross compilation + +build-x86_64: clean + @echo 'Building...' + @cmake . -B $(BUILD_DIR) -G Ninja -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/x86_64-linux-gnu-clang.cmake + @cmake --build $(BUILD_DIR) --config Release -j20 --target pgfe -- + @echo 'Building done' + +build-x86_64-mold: clean + @echo 'Building...' + @cmake . -B $(BUILD_DIR) -G Ninja -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/x86_64-linux-gnu-clang-mold.cmake + @cmake --build $(BUILD_DIR) --config Release -j20 --target pgfe -- + @echo 'Building done' + +build-aarch64-android: clean + @echo 'Building...' + @cmake . -B $(BUILD_DIR) -G Ninja \ + -DNDK_ROOT=$(NDK_ROOT) \ + -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/aarch64-linux-android.cmake + @cmake --build $(BUILD_DIR) --config Release -j20 --target pgfe -- + @echo 'Building done' + +build-arm-android: clean + @echo 'Building...' + @cmake . -B $(BUILD_DIR) -G Ninja -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/arm-linux-android.cmake \ + -DNDK_ROOT=$(NDK_ROOT) + @cmake --build $(BUILD_DIR) --config Release -j20 --target pgfe -- + @echo 'Building done' + +build-x86_64-android: clean + @echo 'Building...' + @cmake . -B $(BUILD_DIR) -G Ninja -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/x86_64-linux-android.cmake \ + -DNDK_ROOT=$(NDK_ROOT) + @cmake --build $(BUILD_DIR) --config Release -j20 --target pgfe -- + @echo 'Building done' + +build-x86-android: clean + @echo 'Building...' + @cmake . -B $(BUILD_DIR) -G Ninja -DCMAKE_TOOLCHAIN_FILE=./cmake/toolchains/x86-linux-android.cmake \ + -DNDK_ROOT=$(NDK_ROOT) + @cmake --build $(BUILD_DIR) --config Release -j20 --target pgfe -- + @echo 'Building done' diff --git a/README.md b/README.md index be3b18e..0f5a69b 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,10 @@ ![Version](https://img.shields.io/github/v/tag/chardon55/libpgfe?color=brightgreen&label=version) ![License](https://img.shields.io/github/license/chardon55/libpgfe) -![Files](https://img.shields.io/github/directory-file-count/chardon55/libpgfe) -![File size](https://img.shields.io/github/repo-size/chardon55/libpgfe) ## Introduction -**libpgfe** (IPA: /ˌlɪbˈpɪɡfiː/, like "lib-pig-fee") is a free and open-source cryptographic library focusing on flexibility and easy-to-access interfaces, which is written in [C](https://en.wikipedia.org/wiki/C_(programming_language)) and [C++](https://en.wikipedia.org/wiki/C%2B%2B). +**libpgfe** (IPA: /[ˌlɪbˈpɪɡfiː](http://ipa-reader.xyz/?text=%CB%8Cl%C9%AAb%CB%88p%C9%AA%C9%A1fi%CB%90)/, respelling: "lib-PIG-fee") is a free and open-source cryptographic library focusing on flexibility and easy-to-access interfaces, which is written in [C](https://en.wikipedia.org/wiki/C_(programming_language)) and [C++](https://en.wikipedia.org/wiki/C%2B%2B). *libpgfe* currently supports hash encoding (e.g. SHA256, MD5), HMAC encoding, HOTP/TOTP, Base 16/32/64 and Blowfish. @@ -18,7 +16,17 @@ | C Standard | [C11 (ISO/IEC 9899:2011)](https://en.wikipedia.org/wiki/C11_(C_standard_revision)) | | C++ Standard | [C++14 (ISO/IEC 14882:2014)](https://en.wikipedia.org/wiki/C++14) | | Compiler | LLVM Clang | -| License | BSD 3-Clause | +| License | Dual (MIT / Apache 2.0) | + +> +> NOTE +> +> Since 0.7.0, I have started to rewrite *libpgfe* in [Rust](https://rustlang.org), in order to prevent from potential memory leaks and +> other unsafe things. +> +> The Rust edition libpgfe is planned to be released as v1. Before that, libpgfe v0 might still get some new features, but mostly will be +> daily sustaining. +> [*Any questions?*](.github/qna.md) diff --git a/cmake/config/ndk_root.cmake b/cmake/config/ndk_root.cmake new file mode 100644 index 0000000..01db091 --- /dev/null +++ b/cmake/config/ndk_root.cmake @@ -0,0 +1,2 @@ +set(NDK_ROOT /otp/android-sdk/ndk/default) +set(NDK_ROOT ~/Android/Sdk/ndk/23.1.7779620/) \ No newline at end of file diff --git a/cmake/targets/libpgfe.cmake b/cmake/targets/libpgfe.cmake new file mode 100644 index 0000000..d0c938d --- /dev/null +++ b/cmake/targets/libpgfe.cmake @@ -0,0 +1,56 @@ +add_library(pgfe SHARED + # Core + ${src_dir}/core/generic.c + ${src_dir}/core/generic-internal.c + ${src_dir}/core/utils.c + ${src_dir}/core/abstract_symmetric_crypter.cpp + ${src_dir}/core/algorithm_selectable.cpp + ${src_dir}/core/generic.cpp + ${src_dir}/core/sequential_data.cpp + ${src_dir}/core/utils.cpp + # Hashes + ${src_dir}/hashes/sha-internal.c + ${src_dir}/hashes/sha2-backend.c + ${src_dir}/hashes/keccak-backend.c + ${src_dir}/hashes/md5-backend.c + ${src_dir}/hashes/sha1.c + ${src_dir}/hashes/sha224.c + ${src_dir}/hashes/sha256.c + ${src_dir}/hashes/sha384.c + ${src_dir}/hashes/sha512.c + ${src_dir}/hashes/sha3-224.c + ${src_dir}/hashes/sha3-256.c + ${src_dir}/hashes/sha3-384.c + ${src_dir}/hashes/sha3-512.c + ${src_dir}/hashes/md5.c + ${src_dir}/hashes/shake.c + ${src_dir}/hashes/hash_encoder.cpp + # HMAC + ${src_dir}/hmac/hmac.c + ${src_dir}/hmac/hmac_encoder.cpp + # Base encoding + ${src_dir}/base_encoding/base-encoding-internal.c + ${src_dir}/base_encoding/base64.c + ${src_dir}/base_encoding/base32.c + ${src_dir}/base_encoding/base16.c + ${src_dir}/base_encoding/abstract_base_encoding.cpp + ${src_dir}/base_encoding/base16.cpp + ${src_dir}/base_encoding/base32.cpp + ${src_dir}/base_encoding/base64.cpp + # OTP + ${src_dir}/otp/otp-generic.c + ${src_dir}/otp/hotp.c + ${src_dir}/otp/totp-internal.c + ${src_dir}/otp/totp.c + ${src_dir}/otp/abstract_otp.cpp + ${src_dir}/otp/hotp.cpp + ${src_dir}/otp/totp.cpp + # Fishes (Blowfish, Twofish, Threefish etc.) + ${src_dir}/fishes/blowfish.c + ${src_dir}/fishes/blowfish.cpp +) + +target_include_directories(pgfe + PUBLIC ${include_dir} + PRIVATE ${src_dir} +) diff --git a/cmake/tests.cmake b/cmake/tests.cmake new file mode 100644 index 0000000..549fc55 --- /dev/null +++ b/cmake/tests.cmake @@ -0,0 +1,36 @@ +set(test_dir "tests") + +# CTest +include(CTest) +enable_testing() + +set(test_c_exe pgfe_test_c) +set(test_cpp_exe pgfe_test_cpp) + +## C lib test +add_executable(${test_c_exe} ${test_dir}/c/test.c) + +target_include_directories(${test_c_exe} + PUBLIC ${include_dir} + PRIVATE ${test_dir}/core ${test_dir}/c/ +) + +target_link_libraries(${test_c_exe} pgfe) + +## C++ port test +add_executable(${test_cpp_exe} ${test_dir}/cpp/test.cpp) + +target_include_directories(${test_cpp_exe} + PUBLIC ${include_dir} + PRIVATE ${test_dir}/core ${test_dir}/cpp/ +) + +target_link_libraries(${test_cpp_exe} pgfe) + +list(APPEND cmake_test_modules "base" "blowfish" "hash" "hmac" "otp" "shake" "util") + +include(${test_dir}/${cmake_module_dir}/test_meta.cmake) + +foreach(mod IN LISTS cmake_test_modules) + include(${test_dir}/${cmake_module_dir}/${mod}_tests.cmake) +endforeach() \ No newline at end of file diff --git a/cmake/toolchains/aarch64-linux-android.cmake b/cmake/toolchains/aarch64-linux-android.cmake new file mode 100644 index 0000000..f248a15 --- /dev/null +++ b/cmake/toolchains/aarch64-linux-android.cmake @@ -0,0 +1,23 @@ +set(CMAKE_SYSTEM_NAME Linux) + + +set(arch aarch64) +set(arch_alt arm64) +set(subarch v8a) +set(triple ${arch}-linux-android) + +include(${CMAKE_CURRENT_LIST_DIR}/../config/ndk_root.cmake) + +if(NOT HOST_ID) + set(HOST_ID linux-x86_64) +endif() + +if(NOT ANDROID_PLATFORM_VERSION) + set(ANDROID_PLATFORM_VERSION 23) +endif() + +set(ANDROID_ABI ${arch_alt}-${subarch}) +set(ANDROID_PLATFORM android-${ANDROID_PLATFORM_VERSION}) +set(ANDROID_STL c++_shared) + +include(${NDK_ROOT}/build/cmake/android.toolchain.cmake) \ No newline at end of file diff --git a/cmake/toolchains/arm-linux-android.cmake b/cmake/toolchains/arm-linux-android.cmake new file mode 100644 index 0000000..e5609d6 --- /dev/null +++ b/cmake/toolchains/arm-linux-android.cmake @@ -0,0 +1,23 @@ +set(CMAKE_SYSTEM_NAME Linux) + + +set(arch arm) +set(arch_alt armeabi) +set(subarch v7a) +set(triple ${arch}${subarch}-linux-android) + +include(${CMAKE_CURRENT_LIST_DIR}/../config/ndk_root.cmake) + +if(NOT HOST_ID) + set(HOST_ID linux-x86_64) +endif() + +if(NOT ANDROID_PLATFORM_VERSION) + set(ANDROID_PLATFORM_VERSION 23) +endif() + +set(ANDROID_ABI ${arch_alt}-${subarch}) +set(ANDROID_PLATFORM android-${ANDROID_PLATFORM_VERSION}) +set(ANDROID_STL c++_shared) + +include(${NDK_ROOT}/build/cmake/android.toolchain.cmake) diff --git a/cmake/toolchains/x86-linux-android.cmake b/cmake/toolchains/x86-linux-android.cmake new file mode 100644 index 0000000..bff9501 --- /dev/null +++ b/cmake/toolchains/x86-linux-android.cmake @@ -0,0 +1,23 @@ +set(CMAKE_SYSTEM_NAME Linux) + + +set(arch x86) +set(arch_alt i686) +set(subarch "") +set(triple ${arch}${subarch}-linux-android) + +include(${CMAKE_CURRENT_LIST_DIR}/../config/ndk_root.cmake) + +if(NOT HOST_ID) + set(HOST_ID linux-x86_64) +endif() + +if(NOT ANDROID_PLATFORM_VERSION) + set(ANDROID_PLATFORM_VERSION 23) +endif() + +set(ANDROID_ABI ${arch}) +set(ANDROID_PLATFORM android-${ANDROID_PLATFORM_VERSION}) +set(ANDROID_STL c++_shared) + +include(${NDK_ROOT}/build/cmake/android.toolchain.cmake) diff --git a/cmake/toolchains/x86_64-linux-android.cmake b/cmake/toolchains/x86_64-linux-android.cmake new file mode 100644 index 0000000..c6e8bbd --- /dev/null +++ b/cmake/toolchains/x86_64-linux-android.cmake @@ -0,0 +1,22 @@ +set(CMAKE_SYSTEM_NAME Linux) + + +set(arch x86_64) +set(subarch "") +set(triple ${arch}${subarch}-linux-android) + +include(${CMAKE_CURRENT_LIST_DIR}/../config/ndk_root.cmake) + +if(NOT HOST_ID) + set(HOST_ID linux-x86_64) +endif() + +if(NOT ANDROID_PLATFORM_VERSION) + set(ANDROID_PLATFORM_VERSION 23) +endif() + +set(ANDROID_ABI ${arch}) +set(ANDROID_PLATFORM android-${ANDROID_PLATFORM_VERSION}) +set(ANDROID_STL c++_shared) + +include(${NDK_ROOT}/build/cmake/android.toolchain.cmake) diff --git a/cmake/toolchains/x86_64-linux-gnu-clang-mold.cmake b/cmake/toolchains/x86_64-linux-gnu-clang-mold.cmake new file mode 100644 index 0000000..a0e8fe4 --- /dev/null +++ b/cmake/toolchains/x86_64-linux-gnu-clang-mold.cmake @@ -0,0 +1,32 @@ +set(CMAKE_SYSTEM_NAME Linux) + + +set(triple x86_64-linux-gnu-mold) + +set(CMAKE_C_COMPILER clang) +set(CMAKE_C_COMPILER_TARGET ${triple}) + +set(CMAKE_CXX_COMPILER clang++) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) + +SET(CMAKE_AR llvm-ar) +SET(CMAKE_RANLIB llvm-ranlib) +set(CMAKE_ADDR2LINE llvm-addr2line) + +set(CMAKE_C_COMPILER_AR llvm-ar) +set(CMAKE_C_COMPILER_RANLIB llvm-ranlib) + +set(CMAKE_CXX_COMPILER_AR llvm-ar) +set(CMAKE_CXX_COMPILER_RANLIB llvm-ranlib) + +# Use mold +set(CMAKE_LINKER ld.mold) +add_link_options("-fuse-ld=mold") + +set(CMAKE_NM llvm-nm) +set(CMAKE_OBJCOPY llvm-objcopy) +set(CMAKE_OBJDUMP llvm-objdump) + +set(CMAKE_READELF llvm-readelf) +set(CMAKE_READOBJ llvm-readobj) +set(CMAKE_STRIP llvm-strip) \ No newline at end of file diff --git a/cmake/toolchains/x86_64-linux-gnu-clang.cmake b/cmake/toolchains/x86_64-linux-gnu-clang.cmake new file mode 100644 index 0000000..f1ee56f --- /dev/null +++ b/cmake/toolchains/x86_64-linux-gnu-clang.cmake @@ -0,0 +1,31 @@ +set(CMAKE_SYSTEM_NAME Linux) + + +set(triple x86_64-linux-gnu) + +set(CMAKE_C_COMPILER clang) +set(CMAKE_C_COMPILER_TARGET ${triple}) + +set(CMAKE_CXX_COMPILER clang++) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) + +SET(CMAKE_AR llvm-ar) +SET(CMAKE_RANLIB llvm-ranlib) +set(CMAKE_ADDR2LINE llvm-addr2line) + +set(CMAKE_C_COMPILER_AR llvm-ar) +set(CMAKE_C_COMPILER_RANLIB llvm-ranlib) + +set(CMAKE_CXX_COMPILER_AR llvm-ar) +set(CMAKE_CXX_COMPILER_RANLIB llvm-ranlib) + +set(CMAKE_LINKER ld.lld) +add_link_options("-fuse-ld=lld") + +set(CMAKE_NM llvm-nm) +set(CMAKE_OBJCOPY llvm-objcopy) +set(CMAKE_OBJDUMP llvm-objdump) + +set(CMAKE_READELF llvm-readelf) +set(CMAKE_READOBJ llvm-readobj) +set(CMAKE_STRIP llvm-strip) \ No newline at end of file diff --git a/include/backend/generic-internal.h b/include/backend/generic-internal.h deleted file mode 100644 index 00cfbc4..0000000 --- a/include/backend/generic-internal.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - libpgfe - generic-internal.h - - Generic configuration backend - - Copyright (c) 2022 Charles Dong -*/ - -#ifndef LIBPGFE_GENERIC_INTERNAL_H -#define LIBPGFE_GENERIC_INTERNAL_H - -#include "../generic.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define PGFE_HEX_UNKNOWN_CHAR 0xF1 - -#define PGFE_ENCODER_DEF_SIG pgfe_encode_multi_func *func, size_t block_size, size_t digest_size -#define PGFE_ENCODER_CALL_SIG func, block_size, digest_size - -// Circular left shift -#define clshift(n, c) (((n) << (c)) | ((n) >> (to_bit(sizeof(n)) - (c)))) -// Circular right shift -#define crshift(n, c) (((n) >> (c)) | ((n) << (to_bit(sizeof(n)) - (c)))) - -#define __pgfe_loop for (;;) - -void __pgfe_arrinit(pgfe_encode_t arr[], size_t size); - -void __pgfe_arrcpy(pgfe_encode_t dest[], size_t dest_s, const pgfe_encode_t src[], size_t src_s); - -void __pgfe_ch2hex(char ch, pgfe_encode_t *hex); - -void __pgfe_reverse_elements(pgfe_encode_t *low, pgfe_encode_t *high); - -#define __PGFE_BATCH_CASES_C(target, name) \ - switch (target) { \ - __PGFE_##name##_CASE_C(MD5, md5); \ - __PGFE_##name##_CASE_C(SHA1, sha1); \ - __PGFE_##name##_CASE_C(SHA224, sha224); \ - __PGFE_##name##_CASE_C(SHA256, sha256); \ - __PGFE_##name##_CASE_C(SHA384, sha384); \ - __PGFE_##name##_CASE_C(SHA512, sha512); \ - __PGFE_##name##_CASE_C(SHA512_224, sha512_224); \ - __PGFE_##name##_CASE_C(SHA512_256, sha512_256); \ - __PGFE_##name##_CASE_C(SHA3_224, sha3_224); \ - __PGFE_##name##_CASE_C(SHA3_256, sha3_256); \ - __PGFE_##name##_CASE_C(SHA3_384, sha3_384); \ - __PGFE_##name##_CASE_C(SHA3_512, sha3_512); \ - __PGFE_##name##_CASE_C(RawSHAKE128, rawshake128); \ - __PGFE_##name##_CASE_C(SHAKE128, shake128); \ - __PGFE_##name##_CASE_C(RawSHAKE256, rawshake256); \ - __PGFE_##name##_CASE_C(SHAKE256, shake256); \ - default: \ - break; \ - } - -#define __PGFE_BATCH_CASES_SP_C(target, name) \ - switch (target) { \ - __PGFE_##name##_CASE_C(MD5, md5); \ - __PGFE_##name##_CASE_C(SHA1, sha1); \ - __PGFE_##name##_CASE_C(SHA224, sha224); \ - __PGFE_##name##_CASE_C(SHA256, sha256); \ - __PGFE_##name##_CASE_C(SHA384, sha384); \ - __PGFE_##name##_CASE_C(SHA512, sha512); \ - __PGFE_##name##_CASE_C(SHA512_224, sha512_224); \ - __PGFE_##name##_CASE_C(SHA512_256, sha512_256); \ - __PGFE_##name##_CASE_C(SHA3_224, sha3_224); \ - __PGFE_##name##_CASE_C(SHA3_256, sha3_256); \ - __PGFE_##name##_CASE_C(SHA3_384, sha3_384); \ - __PGFE_##name##_CASE_C(SHA3_512, sha3_512); \ - default: \ - break; \ - } - -#define __PGFE_BATCH_CASES_SHAKE_C(target, name) \ - switch (target) { \ - __PGFE_##name##_CASE_C(RawSHAKE128, rawshake128); \ - __PGFE_##name##_CASE_C(SHAKE128, shake128); \ - __PGFE_##name##_CASE_C(RawSHAKE256, rawshake256); \ - __PGFE_##name##_CASE_C(SHAKE256, shake256); \ - default: \ - break; \ - } - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/include/backend/keccak-backend.h b/include/backend/keccak-backend.h deleted file mode 100644 index c294ed2..0000000 --- a/include/backend/keccak-backend.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - libpgfe - keccak-backend.h - - Backend implementation of SHA3/Keccak algorithm - - Copyright (c) 2022 Charles Dong -*/ - -#ifndef LIBPGFE_KECCAK_BACKEND_H -#define LIBPGFE_KECCAK_BACKEND_H - -#include - -#include "generic-internal.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define __PGFE_KECCAK_b 1600 -#define __PGFE_KECCAK_MAX_RATE 1536 - -typedef uint64_t pgfe_keccak_lane_t; - -typedef pgfe_keccak_lane_t pgfe_keccak_bitcube_t[5][5]; - -// Generic Keccak Sponge Context -struct pgfe_keccak_sponge_ctx -{ - pgfe_keccak_bitcube_t state; // Keccak sponge state array - pgfe_encode_t data_queue[to_byte(__PGFE_KECCAK_MAX_RATE)]; // Data queue - uint32_t rate, capacity, inqueue_bits, out_length; // Rate, capacity, bits in queue, output bit length - uint16_t nr; // Number of rounds (Usually 24) - int squeezing; // Is squeezing - unsigned int squeezable; // Size of squeezable bits - pgfe_encode_t ap; // Reserved appendix for adapting SHA-3 - uint8_t ap_len; // Bit length of appendix - int ap_added; // Is appendix added -}; - -int __pgfe_keccak_init(struct pgfe_keccak_sponge_ctx *ctx, uint32_t capacity); - -int __pgfe_keccak_absorb_b1600(struct pgfe_keccak_sponge_ctx *ctx, const pgfe_encode_t input[], uint64_t bit_len); - -int __pgfe_keccak_squeeze_b1600(struct pgfe_keccak_sponge_ctx *ctx, pgfe_encode_t output[]); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/include/backend/md5-backend.h b/include/backend/md5-backend.h deleted file mode 100644 index 649d1ab..0000000 --- a/include/backend/md5-backend.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - libpgfe - md5-backend.h - - Backend implementation of MD5 algorithm - - Copyright (c) 2022 Charles Dong -*/ - -#ifndef LIBPGFE_MD5_BACKEND_H -#define LIBPGFE_MD5_BACKEND_H - -#include "generic-internal.h" - -#ifdef __cplusplus -extern "C" { -#endif - -void __pgfe_md5_transform(pgfe_word_t state[4], const pgfe_encode_t block[64]); - -void __pgfe_md5_encode(pgfe_word_t input[], size_t length, pgfe_encode_t output[]); - -void __pgfe_md5_decode(const pgfe_encode_t input[], size_t length, pgfe_word_t output[]); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/include/base-encoding.h b/include/base-encoding.h deleted file mode 100644 index 19cced8..0000000 --- a/include/base-encoding.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - libpgfe - base-encoding.h - - Copyright (c) 2022 Charles Dong -*/ - -#ifndef LIBPGFE_BASE_ENCODING_H -#define LIBPGFE_BASE_ENCODING_H - -#include "generic.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define PGFE_BASE64_UNIT_SIZE 3 -#define PGFE_BASE64_CHUNK_COUNT 4 -#define PGFE_BASE64_BIT_SIZE 6 -#define PGFE_BASE64_ALPHABET_SIZE 64 - -#define PGFE_BASE32_UNIT_SIZE 5 -#define PGFE_BASE32_CHUNK_COUNT 8 -#define PGFE_BASE32_BIT_SIZE 5 -#define PGFE_BASE32_ALPHABET_SIZE 32 - -#define PGFE_BASE16_UNIT_SIZE 1 -#define PGFE_BASE16_CHUNK_COUNT 2 -#define PGFE_BASE16_BIT_SIZE 4 -#define PGFE_BASE16_ALPHABET_SIZE 16 - -// Base 64 - -// Encode input to standard Base 64 -size_t pgfe_encode_base64(const pgfe_encode_t input[], size_t input_length, char cs_out[]); - -// Encode input to the URL and Filename safe variant of Base 64 -size_t pgfe_encode_base64_url(const pgfe_encode_t input[], size_t input_length, char cs_out[]); - -// Decode Base 64 (for both standard and name-safe) -size_t pgfe_decode_base64(const char base64_cs[], pgfe_encode_t output[]); - -// Base 32 - -// Encode input to standard Base 32 -size_t pgfe_encode_base32(const pgfe_encode_t input[], size_t input_length, char cs_out[]); - -// Encode input to Base 32-Hex -size_t pgfe_encode_base32hex(const pgfe_encode_t input[], size_t input_length, char cs_out[]); - -// Decode standard Base 32 -size_t pgfe_decode_base32(const char base32_cs[], pgfe_encode_t output[]); - -// Decode Base 32-Hex -size_t pgfe_decode_base32hex(const char base32_cs[], pgfe_encode_t output[]); - -// Base 16 - -size_t pgfe_encode_base16(const pgfe_encode_t input[], size_t input_length, char cs_out[]); - -size_t pgfe_decode_base16(const char base16_cs[], pgfe_encode_t output[]); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/include/backend_cpp/abstract_encoder.hpp b/include/libpgfe/abstract_encoder.hpp similarity index 86% rename from include/backend_cpp/abstract_encoder.hpp rename to include/libpgfe/abstract_encoder.hpp index 3f16248..766b137 100644 --- a/include/backend_cpp/abstract_encoder.hpp +++ b/include/libpgfe/abstract_encoder.hpp @@ -2,7 +2,7 @@ libpgfe abstract_encoder.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_ENCODER_HPP @@ -14,9 +14,9 @@ #include #include -#include "../generic.h" -#include "../sequential_data.hpp" -#include "../utils.hpp" +#include "libpgfe/generic.h" +#include "libpgfe/sequential_data.hpp" +#include "libpgfe/utils.hpp" namespace libpgfe { diff --git a/include/backend_cpp/abstract_symmetric_crypter.hpp b/include/libpgfe/abstract_symmetric_crypter.hpp similarity index 91% rename from include/backend_cpp/abstract_symmetric_crypter.hpp rename to include/libpgfe/abstract_symmetric_crypter.hpp index 7f6f90b..7de5e7d 100644 --- a/include/backend_cpp/abstract_symmetric_crypter.hpp +++ b/include/libpgfe/abstract_symmetric_crypter.hpp @@ -2,7 +2,7 @@ libpgfe abstract_symmetric_crypter.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_ABSTRACT_SYMMETRIC_CRYPTER_HPP @@ -13,7 +13,7 @@ #include -#include "../sequential_data.hpp" +#include "libpgfe/sequential_data.hpp" namespace libpgfe { diff --git a/include/algorithm-choice.h b/include/libpgfe/algorithm-choice.h similarity index 94% rename from include/algorithm-choice.h rename to include/libpgfe/algorithm-choice.h index ae6d2a1..9248f83 100644 --- a/include/algorithm-choice.h +++ b/include/libpgfe/algorithm-choice.h @@ -2,7 +2,7 @@ libpgfe algorithm-choice.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_ALGORITHM_CHOICE_H diff --git a/include/algorithm_selectable.hpp b/include/libpgfe/algorithm_selectable.hpp similarity index 86% rename from include/algorithm_selectable.hpp rename to include/libpgfe/algorithm_selectable.hpp index 73aecaa..ad450c0 100644 --- a/include/algorithm_selectable.hpp +++ b/include/libpgfe/algorithm_selectable.hpp @@ -2,7 +2,7 @@ libpgfe algorithm_selectable.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_ALGORITHM_SELECTABLE_HPP @@ -12,7 +12,7 @@ #include #include -#include "algorithm-choice.h" +#include "libpgfe/algorithm-choice.h" namespace libpgfe { diff --git a/include/backend_cpp/abstract_base_encoding.hpp b/include/libpgfe/base_encoding/abstract_base_encoding.hpp similarity index 93% rename from include/backend_cpp/abstract_base_encoding.hpp rename to include/libpgfe/base_encoding/abstract_base_encoding.hpp index 81af2d1..4aef6a6 100644 --- a/include/backend_cpp/abstract_base_encoding.hpp +++ b/include/libpgfe/base_encoding/abstract_base_encoding.hpp @@ -2,7 +2,7 @@ libpgfe abstract_base_encoding.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_ABSTRACT_BASE_ENCODING @@ -15,8 +15,8 @@ #include #include -#include "../generic.h" -#include "../sequential_data.hpp" +#include "libpgfe/generic.h" +#include "libpgfe/sequential_data.hpp" namespace libpgfe { diff --git a/include/libpgfe/base_encoding/base16.h b/include/libpgfe/base_encoding/base16.h new file mode 100644 index 0000000..52e1aca --- /dev/null +++ b/include/libpgfe/base_encoding/base16.h @@ -0,0 +1,25 @@ +#ifndef LIBPGFE_BASE16_H +#define LIBPGFE_BASE16_H + +#include "libpgfe/generic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define PGFE_BASE16_UNIT_SIZE 1 +#define PGFE_BASE16_CHUNK_COUNT 2 +#define PGFE_BASE16_BIT_SIZE 4 +#define PGFE_BASE16_ALPHABET_SIZE 16 + +// Base 16 + +size_t pgfe_encode_base16(const pgfe_encode_t input[], size_t input_length, char cs_out[]); + +size_t pgfe_decode_base16(const char base16_cs[], pgfe_encode_t output[]); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/include/base16.hpp b/include/libpgfe/base_encoding/base16.hpp similarity index 83% rename from include/base16.hpp rename to include/libpgfe/base_encoding/base16.hpp index 1bc46a1..422888b 100644 --- a/include/base16.hpp +++ b/include/libpgfe/base_encoding/base16.hpp @@ -2,7 +2,7 @@ libpgfe base16.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_BASE16_HPP @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "backend_cpp/abstract_base_encoding.hpp" +#include "libpgfe/base_encoding/abstract_base_encoding.hpp" namespace libpgfe { diff --git a/include/libpgfe/base_encoding/base32.h b/include/libpgfe/base_encoding/base32.h new file mode 100644 index 0000000..1d26b03 --- /dev/null +++ b/include/libpgfe/base_encoding/base32.h @@ -0,0 +1,33 @@ +#ifndef LIBPGFE_BASE32_H +#define LIBPGFE_BASE32_H + +#include "libpgfe/generic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define PGFE_BASE32_UNIT_SIZE 5 +#define PGFE_BASE32_CHUNK_COUNT 8 +#define PGFE_BASE32_BIT_SIZE 5 +#define PGFE_BASE32_ALPHABET_SIZE 32 + +// Base 32 + +// Encode input to standard Base 32 +size_t pgfe_encode_base32(const pgfe_encode_t input[], size_t input_length, char cs_out[]); + +// Encode input to Base 32-Hex +size_t pgfe_encode_base32hex(const pgfe_encode_t input[], size_t input_length, char cs_out[]); + +// Decode standard Base 32 +size_t pgfe_decode_base32(const char base32_cs[], pgfe_encode_t output[]); + +// Decode Base 32-Hex +size_t pgfe_decode_base32hex(const char base32_cs[], pgfe_encode_t output[]); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/include/base32.hpp b/include/libpgfe/base_encoding/base32.hpp similarity index 85% rename from include/base32.hpp rename to include/libpgfe/base_encoding/base32.hpp index 8cfcfd6..7e4420c 100644 --- a/include/base32.hpp +++ b/include/libpgfe/base_encoding/base32.hpp @@ -2,7 +2,7 @@ libpgfe base32.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_BASE32_HPP @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "backend_cpp/abstract_base_encoding.hpp" +#include "libpgfe/base_encoding/abstract_base_encoding.hpp" namespace libpgfe { diff --git a/include/libpgfe/base_encoding/base64.h b/include/libpgfe/base_encoding/base64.h new file mode 100644 index 0000000..61ebcce --- /dev/null +++ b/include/libpgfe/base_encoding/base64.h @@ -0,0 +1,30 @@ +#ifndef LIBPGFE_BASE64_H +#define LIBPGFE_BASE64_H + +#include "libpgfe/generic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define PGFE_BASE64_UNIT_SIZE 3 +#define PGFE_BASE64_CHUNK_COUNT 4 +#define PGFE_BASE64_BIT_SIZE 6 +#define PGFE_BASE64_ALPHABET_SIZE 64 + +// Base 64 + +// Encode input to standard Base 64 +size_t pgfe_encode_base64(const pgfe_encode_t input[], size_t input_length, char cs_out[]); + +// Encode input to the URL and Filename safe variant of Base 64 +size_t pgfe_encode_base64_url(const pgfe_encode_t input[], size_t input_length, char cs_out[]); + +// Decode Base 64 (for both standard and name-safe) +size_t pgfe_decode_base64(const char base64_cs[], pgfe_encode_t output[]); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/include/base64.hpp b/include/libpgfe/base_encoding/base64.hpp similarity index 86% rename from include/base64.hpp rename to include/libpgfe/base_encoding/base64.hpp index 2c88a29..dab3fc1 100644 --- a/include/base64.hpp +++ b/include/libpgfe/base_encoding/base64.hpp @@ -2,7 +2,7 @@ libpgfe base64.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_BASE64_HPP @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "backend_cpp/abstract_base_encoding.hpp" +#include "libpgfe/base_encoding/abstract_base_encoding.hpp" namespace libpgfe { diff --git a/include/exceptions.hpp b/include/libpgfe/exceptions.hpp similarity index 90% rename from include/exceptions.hpp rename to include/libpgfe/exceptions.hpp index da1557d..9c06a09 100644 --- a/include/exceptions.hpp +++ b/include/libpgfe/exceptions.hpp @@ -2,7 +2,7 @@ libpgfe exceptions.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ diff --git a/include/blowfish.h b/include/libpgfe/fishes/blowfish.h similarity index 93% rename from include/blowfish.h rename to include/libpgfe/fishes/blowfish.h index 04a1474..dd4daaf 100644 --- a/include/blowfish.h +++ b/include/libpgfe/fishes/blowfish.h @@ -4,7 +4,7 @@ Blowfish - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_BLOWFISH_H @@ -13,7 +13,7 @@ extern "C" { #endif -#include "backend/generic-internal.h" +#include "libpgfe/generic.h" #define __PGFE_BF_N 16 #define __PGFE_BF_Np2 18 diff --git a/include/blowfish.hpp b/include/libpgfe/fishes/blowfish.hpp similarity index 88% rename from include/blowfish.hpp rename to include/libpgfe/fishes/blowfish.hpp index b320467..3477bd1 100644 --- a/include/blowfish.hpp +++ b/include/libpgfe/fishes/blowfish.hpp @@ -2,7 +2,7 @@ libpgfe blowfish.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_BLOWFISH_HPP @@ -13,8 +13,8 @@ #include -#include "backend_cpp/abstract_symmetric_crypter.hpp" -#include "blowfish.h" +#include "libpgfe/abstract_symmetric_crypter.hpp" +#include "libpgfe/fishes/blowfish.h" namespace libpgfe { diff --git a/include/generic.h b/include/libpgfe/generic.h similarity index 77% rename from include/generic.h rename to include/libpgfe/generic.h index 3b0e5b7..acacfa0 100644 --- a/include/generic.h +++ b/include/libpgfe/generic.h @@ -2,7 +2,7 @@ libpgfe generic.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_H @@ -11,21 +11,21 @@ #include #include -#include "algorithm-choice.h" +#include "libpgfe/algorithm-choice.h" #ifdef __cplusplus extern "C" { #endif // Default reading buffer size -#define PGFE_BUFFER_SIZE 1024 +#define PGFE_BUFFER_SIZE (1024) // byte -> bit (e.g. to_bit(4) = 32) -#define to_bit(value) ((value)*8) +#define pgfe_to_bit(value) ((value)*8) // bit -> byte (e.g. to_byte(32) = 4) -#define to_byte(value) ((value) / 8) +#define pgfe_to_byte(value) ((value) / 8) // Remainder of bit -#define bit_rem(value) ((value) % 8) +#define pgfe_bit_rem(value) ((value) % 8) typedef char pgfe_ascii_t; diff --git a/include/libpgfe/generic.hpp b/include/libpgfe/generic.hpp new file mode 100644 index 0000000..a85ed74 --- /dev/null +++ b/include/libpgfe/generic.hpp @@ -0,0 +1,107 @@ +/* + libpgfe + generic.hpp + + Copyright (c) 2022-2023 Charles Dong +*/ + +#ifndef LIBPGFE_GENERIC_HPP +#define LIBPGFE_GENERIC_HPP +#ifndef __cplusplus +#error libpgfe error: C++ headers are not compatible with C source +#endif + +#include +#include + +#include "libpgfe/algorithm-choice.h" +#include "libpgfe/exceptions.hpp" +#include "libpgfe/hashes.h" + +namespace libpgfe { + +static const std::unordered_map pgfe_option_map = { + {"sha1", SHA1 }, + {"SHA1", SHA1 }, + {"sha224", SHA224 }, + {"SHA224", SHA224 }, + {"sha256", SHA256 }, + {"SHA256", SHA256 }, + {"sha384", SHA384 }, + {"SHA384", SHA384 }, + {"sha512", SHA512 }, + {"SHA512", SHA512 }, + {"sha512_224", SHA512_224 }, + {"SHA512_224", SHA512_224 }, + {"sha512_256", SHA512_256 }, + {"SHA512_256", SHA512_256 }, + {"sha3_224", SHA3_224 }, + {"SHA3_224", SHA3_224 }, + {"sha3_256", SHA3_256 }, + {"SHA3_256", SHA3_256 }, + {"sha3_384", SHA3_384 }, + {"SHA3_384", SHA3_384 }, + {"sha3_512", SHA3_512 }, + {"SHA3_512", SHA3_512 }, + {"shake128", SHAKE128 }, + {"SHAKE128", SHAKE128 }, + {"rawshake128", RawSHAKE128}, + {"RawSHAKE128", RawSHAKE128}, + {"shake256", SHAKE256 }, + {"SHAKE256", SHAKE256 }, + {"rawshake256", RawSHAKE256}, + {"RawSHAKE256", RawSHAKE256}, + {"md5", MD5 }, + {"MD5", MD5 }, +}; + +static const std::unordered_map pgfe_digest_length = { + {SHA1, PGFE_SHA1_DIGEST_SIZE }, + {SHA224, PGFE_SHA224_DIGEST_SIZE }, + {SHA256, PGFE_SHA256_DIGEST_SIZE }, + {SHA384, PGFE_SHA384_DIGEST_SIZE }, + {SHA512, PGFE_SHA512_DIGEST_SIZE }, + {SHA512_224, PGFE_SHA512_224_DIGEST_SIZE }, + {SHA512_256, PGFE_SHA512_256_DIGEST_SIZE }, + {SHA3_224, PGFE_SHA3_224_DIGEST_SIZE }, + {SHA3_256, PGFE_SHA3_256_DIGEST_SIZE }, + {SHA3_384, PGFE_SHA3_384_DIGEST_SIZE }, + {SHA3_512, PGFE_SHA3_512_DIGEST_SIZE }, + {MD5, PGFE_MD5_DIGEST_SIZE }, + {SHAKE128, PGFE_SHAKE128_DIGEST_SIZE }, + {RawSHAKE128, PGFE_RawSHAKE128_DIGEST_SIZE}, + {SHAKE256, PGFE_SHAKE256_DIGEST_SIZE }, + {RawSHAKE256, PGFE_RawSHAKE256_DIGEST_SIZE}, +}; + +static const std::unordered_map pgfe_block_length = { + {SHA1, PGFE_SHA1_BLOCK_SIZE }, + {SHA224, PGFE_SHA224_BLOCK_SIZE }, + {SHA256, PGFE_SHA256_BLOCK_SIZE }, + {SHA384, PGFE_SHA384_BLOCK_SIZE }, + {SHA512, PGFE_SHA512_BLOCK_SIZE }, + {SHA512_224, PGFE_SHA512_224_BLOCK_SIZE }, + {SHA512_256, PGFE_SHA512_256_BLOCK_SIZE }, + {SHA3_224, PGFE_SHA3_224_BLOCK_SIZE }, + {SHA3_256, PGFE_SHA3_256_BLOCK_SIZE }, + {SHA3_384, PGFE_SHA3_384_BLOCK_SIZE }, + {SHA3_512, PGFE_SHA3_512_BLOCK_SIZE }, + {MD5, PGFE_MD5_BLOCK_SIZE }, + {SHAKE128, PGFE_SHAKE128_BLOCK_SIZE }, + {RawSHAKE128, PGFE_RawSHAKE128_BLOCK_SIZE}, + {SHAKE256, PGFE_SHAKE256_BLOCK_SIZE }, + {RawSHAKE256, PGFE_RawSHAKE256_BLOCK_SIZE}, +}; + +// String to algorithm choice type + +pgfe_algorithm_choice _algstr(const char *cs); +pgfe_algorithm_choice _algstr(std::string cpp_s); + +inline pgfe_algorithm_choice operator"" _pgfe_alg(const char *str, size_t size) { + return _algstr(str); +} + +} // namespace libpgfe + +#endif \ No newline at end of file diff --git a/include/libpgfe/hashes.h b/include/libpgfe/hashes.h new file mode 100644 index 0000000..102bc22 --- /dev/null +++ b/include/libpgfe/hashes.h @@ -0,0 +1,9 @@ +#ifndef LIBPGFE_HASHES_H +#define LIBPGFE_HASHES_H + +#include "libpgfe/hashes/md5.h" +#include "libpgfe/hashes/sha1.h" +#include "libpgfe/hashes/sha2.h" +#include "libpgfe/hashes/sha3.h" + +#endif \ No newline at end of file diff --git a/include/backend_cpp/abstract_hash_encoder.hpp b/include/libpgfe/hashes/abstract_hash_encoder.hpp similarity index 71% rename from include/backend_cpp/abstract_hash_encoder.hpp rename to include/libpgfe/hashes/abstract_hash_encoder.hpp index 348d9c9..cc14795 100644 --- a/include/backend_cpp/abstract_hash_encoder.hpp +++ b/include/libpgfe/hashes/abstract_hash_encoder.hpp @@ -2,7 +2,7 @@ libpgfe abstract_hash_encoder.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_HASH_ENCODER_HPP @@ -13,12 +13,12 @@ #include -#include "abstract_encoder.hpp" +#include "libpgfe/abstract_encoder.hpp" -#include "../algorithm-choice.h" -#include "../algorithm_selectable.hpp" -#include "../generic.h" -#include "../generic.hpp" +#include "libpgfe/algorithm-choice.h" +#include "libpgfe/algorithm_selectable.hpp" +#include "libpgfe/generic.h" +#include "libpgfe/generic.hpp" namespace libpgfe { diff --git a/include/hash_encoder.hpp b/include/libpgfe/hashes/hash_encoder.hpp similarity index 87% rename from include/hash_encoder.hpp rename to include/libpgfe/hashes/hash_encoder.hpp index 9a5b76c..da9c6a4 100644 --- a/include/hash_encoder.hpp +++ b/include/libpgfe/hashes/hash_encoder.hpp @@ -4,7 +4,7 @@ Hash encoder - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_HASH_ENCODER_HPP @@ -13,11 +13,11 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "backend_cpp/abstract_hash_encoder.hpp" +#include "libpgfe/hashes/abstract_hash_encoder.hpp" #include -#include "generic.hpp" +#include "libpgfe/generic.hpp" namespace libpgfe { diff --git a/include/md5.h b/include/libpgfe/hashes/md5.h similarity index 90% rename from include/md5.h rename to include/libpgfe/hashes/md5.h index 68ac42f..aef4db3 100644 --- a/include/md5.h +++ b/include/libpgfe/hashes/md5.h @@ -2,7 +2,7 @@ libpgfe md5.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_MD5_H @@ -10,8 +10,7 @@ #include -#include "backend/generic-internal.h" -#include "generic.h" +#include "libpgfe/generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/sha1.h b/include/libpgfe/hashes/sha1.h similarity index 93% rename from include/sha1.h rename to include/libpgfe/hashes/sha1.h index 2168956..40870d6 100644 --- a/include/sha1.h +++ b/include/libpgfe/hashes/sha1.h @@ -2,7 +2,7 @@ libpgfe sha1.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SHA1_H @@ -10,7 +10,7 @@ #include -#include "backend/generic-internal.h" +#include "libpgfe/generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/sha2.h b/include/libpgfe/hashes/sha2.h similarity index 98% rename from include/sha2.h rename to include/libpgfe/hashes/sha2.h index cc5a242..e997f54 100644 --- a/include/sha2.h +++ b/include/libpgfe/hashes/sha2.h @@ -2,7 +2,7 @@ libpgfe sha2.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SHA2_H @@ -10,7 +10,7 @@ #include -#include "backend/generic-internal.h" +#include "libpgfe/generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/sha3.h b/include/libpgfe/hashes/sha3.h similarity index 85% rename from include/sha3.h rename to include/libpgfe/hashes/sha3.h index 23d24c7..d35468a 100644 --- a/include/sha3.h +++ b/include/libpgfe/hashes/sha3.h @@ -2,7 +2,7 @@ libpgfe sha3.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SHA3_H @@ -10,13 +10,32 @@ #include -#include "backend/keccak-backend.h" -#include "generic.h" +#include "libpgfe/generic.h" #ifdef __cplusplus extern "C" { #endif +#define __PGFE_KECCAK_MAX_RATE 1536 + +typedef uint64_t pgfe_keccak_lane_t; + +typedef pgfe_keccak_lane_t pgfe_keccak_bitcube_t[5][5]; + +// Generic Keccak Sponge Context +struct pgfe_keccak_sponge_ctx +{ + pgfe_keccak_bitcube_t state; // Keccak sponge state array + pgfe_encode_t data_queue[pgfe_to_byte(__PGFE_KECCAK_MAX_RATE)]; // Data queue + uint32_t rate, capacity, inqueue_bits, out_length; // Rate, capacity, bits in queue, output bit length + uint16_t nr; // Number of rounds (Usually 24) + int squeezing; // Is squeezing + unsigned int squeezable; // Size of squeezable bits + pgfe_encode_t ap; // Reserved appendix for adapting SHA-3 + uint8_t ap_len; // Bit length of appendix + int ap_added; // Is appendix added +}; + #define PGFE_SHA3_224_DIGEST_SIZE 28 #define PGFE_SHA3_256_DIGEST_SIZE 32 #define PGFE_SHA3_384_DIGEST_SIZE 48 diff --git a/include/hmac.h b/include/libpgfe/hmac/hmac.h similarity index 98% rename from include/hmac.h rename to include/libpgfe/hmac/hmac.h index e5e9f17..3420605 100644 --- a/include/hmac.h +++ b/include/libpgfe/hmac/hmac.h @@ -2,18 +2,14 @@ libpgfe hmac.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_HMAC_H #define LIBPGFE_HMAC_H -#include "backend/generic-internal.h" -#include "generic.h" -#include "md5.h" -#include "sha1.h" -#include "sha2.h" -#include "sha3.h" +#include "libpgfe/generic.h" +#include "libpgfe/hashes.h" #ifdef __cplusplus extern "C" { diff --git a/include/hmac_encoder.hpp b/include/libpgfe/hmac/hmac_encoder.hpp similarity index 89% rename from include/hmac_encoder.hpp rename to include/libpgfe/hmac/hmac_encoder.hpp index f8cb60c..40ceaf1 100644 --- a/include/hmac_encoder.hpp +++ b/include/libpgfe/hmac/hmac_encoder.hpp @@ -2,7 +2,7 @@ libpgfe hmac_encoder.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_HMAC_ENCODER_HPP @@ -14,10 +14,10 @@ #include #include -#include "backend_cpp/abstract_hash_encoder.hpp" -#include "generic.h" -#include "generic.hpp" -#include "hmac.h" +#include "libpgfe/generic.h" +#include "libpgfe/generic.hpp" +#include "libpgfe/hashes/abstract_hash_encoder.hpp" +#include "libpgfe/hmac/hmac.h" namespace libpgfe { diff --git a/include/backend_cpp/abstract_otp.hpp b/include/libpgfe/otp/abstract_otp.hpp similarity index 73% rename from include/backend_cpp/abstract_otp.hpp rename to include/libpgfe/otp/abstract_otp.hpp index 94023aa..ff8cfce 100644 --- a/include/backend_cpp/abstract_otp.hpp +++ b/include/libpgfe/otp/abstract_otp.hpp @@ -2,21 +2,21 @@ libpgfe abstract_otp.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#ifndef LIBPGFE_GENERIC_OTP_HPP -#define LIBPGFE_GENERIC_OTP_HPP +#ifndef LIBPGFE_OTP_GENERIC_OTP_HPP +#define LIBPGFE_OTP_GENERIC_OTP_HPP #ifndef __cplusplus #error libpgfe error: C++ headers are not compatible with C source #endif #include -#include "../algorithm_selectable.hpp" -#include "../backend/otp-generic.h" -#include "../generic.h" -#include "../sequential_data.hpp" +#include "libpgfe/algorithm_selectable.hpp" +#include "libpgfe/generic.h" +#include "libpgfe/sequential_data.hpp" +#include "libpgfe/otp/types.h" namespace libpgfe { diff --git a/include/hotp.h b/include/libpgfe/otp/hotp.h similarity index 86% rename from include/hotp.h rename to include/libpgfe/otp/hotp.h index 8113fd3..41513f9 100644 --- a/include/hotp.h +++ b/include/libpgfe/otp/hotp.h @@ -2,14 +2,14 @@ libpgfe hotp.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#ifndef LIBPGFE_HOTP_H -#define LIBPGFE_HOTP_H +#ifndef LIBPGFE_OTP_HOTP_H +#define LIBPGFE_OTP_HOTP_H -#include "backend/generic-internal.h" -#include "backend/otp-generic.h" +#include "libpgfe/generic.h" +#include "libpgfe/otp/types.h" #ifdef __cplusplus extern "C" { diff --git a/include/hotp.hpp b/include/libpgfe/otp/hotp.hpp similarity index 63% rename from include/hotp.hpp rename to include/libpgfe/otp/hotp.hpp index 39674d9..b27b1f1 100644 --- a/include/hotp.hpp +++ b/include/libpgfe/otp/hotp.hpp @@ -2,19 +2,19 @@ libpgfe hotp.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#ifndef LIBPGFE_HOTP_HPP -#define LIBPGFE_HOTP_HPP +#ifndef LIBPGFE_OTP_HOTP_HPP +#define LIBPGFE_OTP_HOTP_HPP #ifndef __cplusplus #error libpgfe error: C++ headers are not compatible with C source #endif #include -#include "backend_cpp/abstract_otp.hpp" -#include "base32.hpp" +#include "libpgfe/base_encoding/base32.hpp" +#include "libpgfe/otp/abstract_otp.hpp" namespace libpgfe { @@ -29,7 +29,7 @@ class HOTP : public AbstractOTP Base32 base32; protected: - void after_change_alg(); + void after_change_alg() override; public: HOTP(pgfe_algorithm_choice algorithm = SHA1); @@ -38,18 +38,18 @@ class HOTP : public AbstractOTP HOTP(std::string &, pgfe_algorithm_choice = SHA1); HOTP(SequentialData &, pgfe_algorithm_choice = SHA1); - void set_secret(const pgfe_encode_t *, size_t); + void set_secret(const pgfe_encode_t *, size_t) override; void set_secret(const char *cs, bool is_base32 = false); void set_secret(const std::string &, bool is_base32 = false); - void set_secret(const SequentialData &); + void set_secret(const SequentialData &) override; void set_secret_from_base32(const char *); void set_secret_from_base32(const std::string &); - void set_counter(pgfe_otp_counter_t); + void set_counter(pgfe_otp_counter_t) override; - pgfe_otp_t generate(uint8_t digit_count = 6) const; - std::string generate_str(uint8_t digit_count = 6) const; + pgfe_otp_t generate(uint8_t digit_count = 6) const override; + std::string generate_str(uint8_t digit_count = 6) const override; }; } // namespace libpgfe diff --git a/include/totp.h b/include/libpgfe/otp/totp.h similarity index 82% rename from include/totp.h rename to include/libpgfe/otp/totp.h index 7803a74..7652ff3 100644 --- a/include/totp.h +++ b/include/libpgfe/otp/totp.h @@ -2,16 +2,17 @@ libpgfe totp.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#ifndef LIBPGFE_TOTP_H -#define LIBPGFE_TOTP_H +#ifndef LIBPGFE_OTP_TOTP_H +#define LIBPGFE_OTP_TOTP_H #include -#include "backend/otp-generic.h" +#include "libpgfe/generic.h" +#include "libpgfe/otp/types.h" #ifdef __cplusplus extern "C" { @@ -21,10 +22,6 @@ typedef uint32_t pgfe_totp_interval_t; typedef int32_t pgfe_totp_delta_t; -pgfe_otp_counter_t __pgfe_calc_periodic_counter( - time_t realtime, pgfe_totp_interval_t interval, time_t initial_time, pgfe_totp_delta_t *delta_out -); - // Generic TOTP function suitable for various hash algorithm pgfe_otp_t pgfe_totp_generic( enum pgfe_algorithm_choice alg, const pgfe_encode_t secret[], size_t secret_length, uint8_t digit_c, diff --git a/include/totp.hpp b/include/libpgfe/otp/totp.hpp similarity index 70% rename from include/totp.hpp rename to include/libpgfe/otp/totp.hpp index 906d66c..93d6c31 100644 --- a/include/totp.hpp +++ b/include/libpgfe/otp/totp.hpp @@ -2,28 +2,30 @@ libpgfe totp.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#ifndef LIBPGFE_TOTP_HPP -#define LIBPGFE_TOTP_HPP +#ifndef LIBPGFE_OTP_TOTP_HPP +#define LIBPGFE_OTP_TOTP_HPP #ifndef __cplusplus #error libpgfe error: C++ headers are not compatible with C source #endif -#include "hotp.hpp" -#include "totp.h" +#include "libpgfe/otp/hotp.hpp" +#include "libpgfe/otp/totp.h" namespace libpgfe { class TOTP : public HOTP { private: - pgfe_totp_interval_t interval; - time_t initial_time = 0, update_time = 0; + pgfe_totp_interval_t _interval; + time_t _initial_time = 0, _update_time = 0; pgfe_totp_delta_t delta = 0; + void update_counter(); + public: TOTP(pgfe_totp_interval_t = 30, pgfe_algorithm_choice = SHA1); TOTP(const pgfe_encode_t *, size_t, pgfe_totp_interval_t = 30, pgfe_algorithm_choice = SHA1); @@ -32,17 +34,15 @@ class TOTP : public HOTP TOTP(SequentialData &, pgfe_totp_interval_t = 30, pgfe_algorithm_choice = SHA1); ~TOTP(); - void update_counter(); - - pgfe_totp_interval_t get_interval() const; + pgfe_totp_interval_t interval() const; void set_interval(pgfe_totp_interval_t); - time_t get_initial_time() const; + time_t initial_time() const; void set_initial_time(time_t); - time_t get_update_time() const; + time_t update_time() const; - time_t get_remain_time() const; + time_t remaining_time() const; pgfe_otp_t generate(uint8_t digit_count = 6); diff --git a/include/libpgfe/otp/types.h b/include/libpgfe/otp/types.h new file mode 100644 index 0000000..8c9dabf --- /dev/null +++ b/include/libpgfe/otp/types.h @@ -0,0 +1,17 @@ +#ifndef LIBPGFE_OTP_TYPES_H +#define LIBPGFE_OTP_TYPES_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef uint32_t pgfe_otp_t; + +typedef uint64_t pgfe_otp_counter_t; + +typedef pgfe_encode_t pgfe_otp_secret_t; + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/include/sequential_data.hpp b/include/libpgfe/sequential_data.hpp similarity index 93% rename from include/sequential_data.hpp rename to include/libpgfe/sequential_data.hpp index 5adc6a7..4daa971 100644 --- a/include/sequential_data.hpp +++ b/include/libpgfe/sequential_data.hpp @@ -4,7 +4,7 @@ Header of Sequential Data class - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SEQUENTIAL_DATA_HPP @@ -16,8 +16,8 @@ #include #include -#include "generic.h" -#include "utils.h" +#include "libpgfe/generic.h" +#include "libpgfe/utils.h" namespace libpgfe { diff --git a/include/utils.h b/include/libpgfe/utils.h similarity index 78% rename from include/utils.h rename to include/libpgfe/utils.h index 4e3f389..cb405c4 100644 --- a/include/utils.h +++ b/include/libpgfe/utils.h @@ -2,16 +2,13 @@ libpgfe utils.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_UTILS_H #define LIBPGFE_UTILS_H -#include "backend/otp-generic.h" -#include "generic.h" - -#include +#include "libpgfe/generic.h" #ifdef __cplusplus extern "C" { @@ -34,11 +31,6 @@ size_t pgfe_hash_string_clean_(char hash_s[]); // Calculate length of a hash represented by a string size_t pgfe_hash_length(const char hash_s[]); -// Get current UNIX time -time_t pgfe_curtime(); - -void pgfe_otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c); - #ifdef __cplusplus } #endif diff --git a/include/utils.hpp b/include/libpgfe/utils.hpp similarity index 85% rename from include/utils.hpp rename to include/libpgfe/utils.hpp index cd85b9b..29ae848 100644 --- a/include/utils.hpp +++ b/include/libpgfe/utils.hpp @@ -2,7 +2,7 @@ libpgfe utils.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_UTILS_HPP @@ -13,8 +13,8 @@ #include -#include "generic.hpp" -#include "sequential_data.hpp" +#include "libpgfe/generic.hpp" +#include "libpgfe/sequential_data.hpp" namespace libpgfe { namespace utils { diff --git a/include/version.h b/include/libpgfe/version.h similarity index 53% rename from include/version.h rename to include/libpgfe/version.h index 60e6a93..79443d4 100644 --- a/include/version.h +++ b/include/libpgfe/version.h @@ -2,17 +2,17 @@ libpgfe version.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_VERSION_H #define LIBPGFE_VERSION_H #define LIBPGFE_MAJOR_VERSION 0 -#define LIBPGFE_MINOR_VERSION 6 -#define LIBPGFE_REVISION 1 +#define LIBPGFE_MINOR_VERSION 7 +#define LIBPGFE_REVISION 0 #define LIBPGFE_VARIANT "" -#define LIBPGFE_VERSION "0.6.1" +#define LIBPGFE_VERSION "0.7.0" #endif diff --git a/metadata.mak b/metadata.mak index f1418d6..1cfa3fe 100644 --- a/metadata.mak +++ b/metadata.mak @@ -1,5 +1,5 @@ PROJECT := libpgfe -VERSION := 0.6.1 +VERSION := 0.7.0 BUILD_DIR := build @@ -12,4 +12,8 @@ TARGET_DIR := $(BUILD_DIR) TARGET_FILE := $(PROJECT).so TARGET := $(TARGET_DIR)/$(TARGET_FILE) -TEST_TARGET := $(TARGET_DIR)/$(TEST_BIN) \ No newline at end of file +TEST_TARGET := $(TARGET_DIR)/$(TEST_BIN) + +DEFAULT_CMAKE_TOOLCHAIN_FILE := ./cmake/toolchains/x86_64-linux-gnu-clang.cmake + +NDK_ROOT := ~/Android/Sdk/ndk/23.1.7779620/ \ No newline at end of file diff --git a/project.json b/project.json index 22e2cec..9c80d24 100644 --- a/project.json +++ b/project.json @@ -1,4 +1,4 @@ { "name": "libpgfe", - "version": "0.6.1" + "version": "0.7.0" } \ No newline at end of file diff --git a/scripts/comment_header b/scripts/comment_header.py similarity index 96% rename from scripts/comment_header rename to scripts/comment_header.py index c0340d4..898f3f5 100755 --- a/scripts/comment_header +++ b/scripts/comment_header.py @@ -9,7 +9,7 @@ libpgfe {0} - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ ''' diff --git a/scripts/deploy b/scripts/deploy.py similarity index 100% rename from scripts/deploy rename to scripts/deploy.py diff --git a/scripts/update_meta b/scripts/update_meta.py similarity index 100% rename from scripts/update_meta rename to scripts/update_meta.py diff --git a/src/cpp/base_encoding/abstract_base_encoding.cpp b/src/base_encoding/abstract_base_encoding.cpp similarity index 93% rename from src/cpp/base_encoding/abstract_base_encoding.cpp rename to src/base_encoding/abstract_base_encoding.cpp index 78ebe9d..a03a2dc 100644 --- a/src/cpp/base_encoding/abstract_base_encoding.cpp +++ b/src/base_encoding/abstract_base_encoding.cpp @@ -2,13 +2,14 @@ libpgfe abstract_base_encoding.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ +#include "libpgfe/base_encoding/abstract_base_encoding.hpp" #include #include -#include "backend_cpp/abstract_base_encoding.hpp" +#include "../core/generic-internal.h" #define PGFE_SPACE_MULTIPLE 1.2 diff --git a/src/c/base_encoding/base-encoding-internal.c b/src/base_encoding/base-encoding-internal.c similarity index 80% rename from src/c/base_encoding/base-encoding-internal.c rename to src/base_encoding/base-encoding-internal.c index d1fcbfb..9d21d79 100644 --- a/src/c/base_encoding/base-encoding-internal.c +++ b/src/base_encoding/base-encoding-internal.c @@ -2,21 +2,21 @@ libpgfe base-encoding-internal.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend/base-encoding-internal.h" +#include "./base-encoding-internal.h" #include #include -#include "backend/generic-internal.h" +#include "../core/generic-internal.h" -#define __bitsz 8 +#define __bitsz (8) #define __mkmask(digit) (UINT8_MAX >> (__bitsz - (digit))) -size_t __pgfe_transform_codes(const pgfe_encode_t input[], size_t length, uint8_t chunk_size, pgfe_encode_t out[]) { +size_t transform(const pgfe_encode_t input[], size_t length, uint8_t chunk_size, pgfe_encode_t out[]) { const pgfe_encode_t *inp = input; pgfe_encode_t *op = out; size_t low, high, mv_sz, sz_diff; @@ -47,7 +47,7 @@ size_t __pgfe_transform_codes(const pgfe_encode_t input[], size_t length, uint8_ return op - out; } -size_t __pgfe_unittostr( +size_t unittostr( size_t chunk_count, size_t unit_size, size_t bit_size, size_t alphabet_size, const char alphabet[], const pgfe_encode_t unit[], char out[], bool padding ) { @@ -55,7 +55,7 @@ size_t __pgfe_unittostr( pgfe_encode_t unit_split[chunk_count]; int i; - __pgfe_transform_codes(unit, unit_size, bit_size, unit_split); + transform(unit, unit_size, bit_size, unit_split); for (i = 0; i < chunk_count; i++) { unit_s[i] = alphabet[unit_split[i]]; } @@ -74,7 +74,7 @@ size_t __pgfe_unittostr( return chunk_count; } -size_t __pgfe_encode_base_generic( +size_t encode_base_generic( PGFE_BASE_PARAMS_DEF, const char alphabet[], const pgfe_encode_t input[], size_t input_length, char cs_out[] ) { pgfe_encode_t input_unit[unit_size]; @@ -85,7 +85,7 @@ size_t __pgfe_encode_base_generic( for (i = 0; i < input_length; i += unit_size) { remain = input_length - i; if (remain < unit_size) { - __pgfe_arrinit(input_unit, unit_size); + arrinit(input_unit, unit_size); memcpy(input_unit, inp, remain); } else { @@ -93,9 +93,7 @@ size_t __pgfe_encode_base_generic( remain = unit_size; } - sp += __pgfe_unittostr( - chunk_count, unit_size, bit_size, alphabet_size, alphabet, input_unit, sp, unit_size - remain - ); + sp += unittostr(chunk_count, unit_size, bit_size, alphabet_size, alphabet, input_unit, sp, unit_size - remain); inp += unit_size; } @@ -103,9 +101,8 @@ size_t __pgfe_encode_base_generic( return sp - cs_out; } -size_t __pgfe_decode_base_generic( - PGFE_BASE_PARAMS_DEF, pgfe_encode_t (*func)(char), const char basexx_cs[], pgfe_encode_t output[] -) { +size_t +decode_base_generic(PGFE_BASE_PARAMS_DEF, pgfe_encode_t (*func)(char), const char basexx_cs[], pgfe_encode_t output[]) { pgfe_encode_t *op, ch, sig, o_unit[unit_size]; const char *sp = basexx_cs; size_t i, j; @@ -119,7 +116,7 @@ size_t __pgfe_decode_base_generic( ch = func(*sp); sig = ch & 0xC0; - if (sig == PGFE_BASE_UNKNOWN_CODE) { + if (sig == UNKNOWN_CODE) { continue; } diff --git a/include/backend/base-encoding-internal.h b/src/base_encoding/base-encoding-internal.h similarity index 52% rename from include/backend/base-encoding-internal.h rename to src/base_encoding/base-encoding-internal.h index 9e2b46e..2f9ff7d 100644 --- a/include/backend/base-encoding-internal.h +++ b/src/base_encoding/base-encoding-internal.h @@ -4,7 +4,7 @@ Base encoding backend - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_BASE_ENCODING_INTERNAL_H @@ -12,40 +12,38 @@ #include -#include "../generic.h" +#include "libpgfe/generic.h" #ifdef __cplusplus extern "C" { #endif -#define PGFE_BASE_PADDING_CODE 0x00 -#define PGFE_BASE_UNKNOWN_CODE 0xC0 +#define PADDING_CODE (0x00) +#define UNKNOWN_CODE (0xC0) #define PGFE_BASE_PARAMS(upper) \ PGFE_##upper##_CHUNK_COUNT, PGFE_##upper##_UNIT_SIZE, PGFE_##upper##_BIT_SIZE, PGFE_##upper##_ALPHABET_SIZE #define PGFE_BASE_PARAMS_DEF size_t chunk_count, size_t unit_size, size_t bit_size, size_t alphabet_size -pgfe_encode_t pgfe_decode_base64_char(char base64_c); +pgfe_encode_t decode_base64_char(char base64_c); -pgfe_encode_t pgfe_decode_base32_char(char base32_c); +pgfe_encode_t decode_base32_char(char base32_c); -pgfe_encode_t pgfe_decode_base32hex_char(char base32_c); +pgfe_encode_t decode_base32hex_char(char base32_c); -pgfe_encode_t pgfe_decode_base16_char(char base16_c); +pgfe_encode_t decode_base16_char(char base16_c); -size_t __pgfe_transform_codes(const pgfe_encode_t input[], size_t length, uint8_t chunk_size, pgfe_encode_t out[]); +size_t transform(const pgfe_encode_t input[], size_t length, uint8_t chunk_size, pgfe_encode_t out[]); -size_t -__pgfe_unittostr(PGFE_BASE_PARAMS_DEF, const char alphabet[], const pgfe_encode_t unit[], char out[], bool padding); +size_t unittostr(PGFE_BASE_PARAMS_DEF, const char alphabet[], const pgfe_encode_t unit[], char out[], bool padding); -size_t __pgfe_encode_base_generic( +size_t encode_base_generic( PGFE_BASE_PARAMS_DEF, const char alphabet[], const pgfe_encode_t input[], size_t input_length, char cs_out[] ); -size_t __pgfe_decode_base_generic( - PGFE_BASE_PARAMS_DEF, pgfe_encode_t (*func)(char), const char basexx_cs[], pgfe_encode_t output[] -); +size_t +decode_base_generic(PGFE_BASE_PARAMS_DEF, pgfe_encode_t (*func)(char), const char basexx_cs[], pgfe_encode_t output[]); #ifdef __cplusplus } diff --git a/src/c/base_encoding/base16.c b/src/base_encoding/base16.c similarity index 60% rename from src/c/base_encoding/base16.c rename to src/base_encoding/base16.c index 66d2d60..6519621 100644 --- a/src/c/base_encoding/base16.c +++ b/src/base_encoding/base16.c @@ -2,18 +2,18 @@ libpgfe base16.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "base-encoding.h" +#include "libpgfe/base_encoding/base16.h" -#include "backend/base-encoding-internal.h" +#include "./base-encoding-internal.h" // Base 16 alphabet const char BASE16_ALPHABET[] = "0123456789ABCDEF" "0"; // Compatibility addition -inline pgfe_encode_t pgfe_decode_base16_char(char base16_c) { +inline pgfe_encode_t decode_base16_char(char base16_c) { pgfe_encode_t o; if (base16_c >= '0' && base16_c <= '9') { @@ -26,16 +26,16 @@ inline pgfe_encode_t pgfe_decode_base16_char(char base16_c) { o = base16_c - 'a' + 10; } else { - o = PGFE_BASE_UNKNOWN_CODE; + o = UNKNOWN_CODE; } return o; } inline size_t pgfe_encode_base16(const pgfe_encode_t input[], size_t input_length, char cs_out[]) { - return __pgfe_encode_base_generic(PGFE_BASE_PARAMS(BASE16), BASE16_ALPHABET, input, input_length, cs_out); + return encode_base_generic(PGFE_BASE_PARAMS(BASE16), BASE16_ALPHABET, input, input_length, cs_out); } size_t pgfe_decode_base16(const char base16_cs[], pgfe_encode_t output[]) { - return __pgfe_decode_base_generic(PGFE_BASE_PARAMS(BASE16), pgfe_decode_base16_char, base16_cs, output); + return decode_base_generic(PGFE_BASE_PARAMS(BASE16), decode_base16_char, base16_cs, output); } \ No newline at end of file diff --git a/src/cpp/base_encoding/base16.cpp b/src/base_encoding/base16.cpp similarity index 82% rename from src/cpp/base_encoding/base16.cpp rename to src/base_encoding/base16.cpp index 7c657ca..7fc4b6a 100644 --- a/src/cpp/base_encoding/base16.cpp +++ b/src/base_encoding/base16.cpp @@ -2,12 +2,12 @@ libpgfe base16.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "base16.hpp" +#include "libpgfe/base_encoding/base16.hpp" -#include "base-encoding.h" +#include "libpgfe/base_encoding/base16.h" using namespace libpgfe; diff --git a/src/c/base_encoding/base32.c b/src/base_encoding/base32.c similarity index 65% rename from src/c/base_encoding/base32.c rename to src/base_encoding/base32.c index 69111bd..5d52b46 100644 --- a/src/c/base_encoding/base32.c +++ b/src/base_encoding/base32.c @@ -2,15 +2,15 @@ libpgfe base32.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "base-encoding.h" +#include "libpgfe/base_encoding/base32.h" #include -#include "backend/base-encoding-internal.h" -#include "backend/generic-internal.h" +#include "../core/generic-internal.h" +#include "./base-encoding-internal.h" // Base 32 alphabet // 0123456789ABCDEF @@ -24,7 +24,7 @@ const char BASE32_ALPHABET_EXTHEX[] = "0123456789ABCDEF" // 0X "GHIJKLMNOPQRSTUV" // 1X "="; // Padding -inline pgfe_encode_t pgfe_decode_base32_char(char base32_c) { +inline pgfe_encode_t decode_base32_char(char base32_c) { pgfe_encode_t o; if (base32_c >= '2' && base32_c <= '7') { @@ -37,16 +37,16 @@ inline pgfe_encode_t pgfe_decode_base32_char(char base32_c) { o = base32_c - 'a'; } else if (base32_c == '=') { - o = PGFE_BASE_PADDING_CODE; + o = PADDING_CODE; } else { - o = PGFE_BASE_UNKNOWN_CODE; + o = UNKNOWN_CODE; } return o; } -inline pgfe_encode_t pgfe_decode_base32hex_char(char base32_c) { +inline pgfe_encode_t decode_base32hex_char(char base32_c) { pgfe_encode_t o; if (base32_c >= '0' && base32_c <= '9') { @@ -59,27 +59,27 @@ inline pgfe_encode_t pgfe_decode_base32hex_char(char base32_c) { o = base32_c - 'a' + 10; } else if (base32_c == '=') { - o = PGFE_BASE_PADDING_CODE; + o = PADDING_CODE; } else { - o = PGFE_BASE_UNKNOWN_CODE; + o = UNKNOWN_CODE; } return o; } inline size_t pgfe_encode_base32(const pgfe_encode_t input[], size_t input_length, char cs_out[]) { - return __pgfe_encode_base_generic(PGFE_BASE_PARAMS(BASE32), BASE32_ALPHABET, input, input_length, cs_out); + return encode_base_generic(PGFE_BASE_PARAMS(BASE32), BASE32_ALPHABET, input, input_length, cs_out); } inline size_t pgfe_encode_base32hex(const pgfe_encode_t input[], size_t input_length, char cs_out[]) { - return __pgfe_encode_base_generic(PGFE_BASE_PARAMS(BASE32), BASE32_ALPHABET_EXTHEX, input, input_length, cs_out); + return encode_base_generic(PGFE_BASE_PARAMS(BASE32), BASE32_ALPHABET_EXTHEX, input, input_length, cs_out); } inline size_t pgfe_decode_base32(const char base32_cs[], pgfe_encode_t output[]) { - return __pgfe_decode_base_generic(PGFE_BASE_PARAMS(BASE32), pgfe_decode_base32_char, base32_cs, output); + return decode_base_generic(PGFE_BASE_PARAMS(BASE32), decode_base32_char, base32_cs, output); } inline size_t pgfe_decode_base32hex(const char base32_cs[], pgfe_encode_t output[]) { - return __pgfe_decode_base_generic(PGFE_BASE_PARAMS(BASE32), pgfe_decode_base32hex_char, base32_cs, output); + return decode_base_generic(PGFE_BASE_PARAMS(BASE32), decode_base32hex_char, base32_cs, output); } \ No newline at end of file diff --git a/src/cpp/base_encoding/base32.cpp b/src/base_encoding/base32.cpp similarity index 85% rename from src/cpp/base_encoding/base32.cpp rename to src/base_encoding/base32.cpp index 6c0fc54..19644c5 100644 --- a/src/cpp/base_encoding/base32.cpp +++ b/src/base_encoding/base32.cpp @@ -2,12 +2,12 @@ libpgfe base32.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "base32.hpp" +#include "libpgfe/base_encoding/base32.hpp" -#include "base-encoding.h" +#include "libpgfe/base_encoding/base32.h" using namespace libpgfe; diff --git a/src/c/base_encoding/base64.c b/src/base_encoding/base64.c similarity index 73% rename from src/c/base_encoding/base64.c rename to src/base_encoding/base64.c index 1289e5b..cd83c32 100644 --- a/src/c/base_encoding/base64.c +++ b/src/base_encoding/base64.c @@ -2,14 +2,14 @@ libpgfe base64.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "base-encoding.h" +#include "libpgfe/base_encoding/base64.h" #include -#include "backend/base-encoding-internal.h" +#include "./base-encoding-internal.h" // Base 64 alphabet // 0123456789ABCDEF @@ -27,7 +27,7 @@ const char BASE64_ALPHABET_FS[] = "ABCDEFGHIJKLMNOP" // 0X "wxyz0123456789-_" // 3X "="; // Padding -inline pgfe_encode_t pgfe_decode_base64_char(char base64_c) { +inline pgfe_encode_t decode_base64_char(char base64_c) { pgfe_encode_t o; if (base64_c >= 'a' && base64_c <= 'z') { @@ -46,23 +46,23 @@ inline pgfe_encode_t pgfe_decode_base64_char(char base64_c) { o = 63; } else if (base64_c == '=') { - o = PGFE_BASE_PADDING_CODE; + o = PADDING_CODE; } else { - o = PGFE_BASE_UNKNOWN_CODE; + o = UNKNOWN_CODE; } return o; } inline size_t pgfe_encode_base64(const pgfe_encode_t input[], size_t input_length, char cs_out[]) { - return __pgfe_encode_base_generic(PGFE_BASE_PARAMS(BASE64), BASE64_ALPHABET, input, input_length, cs_out); + return encode_base_generic(PGFE_BASE_PARAMS(BASE64), BASE64_ALPHABET, input, input_length, cs_out); } inline size_t pgfe_encode_base64_url(const pgfe_encode_t input[], size_t input_length, char cs_out[]) { - return __pgfe_encode_base_generic(PGFE_BASE_PARAMS(BASE64), BASE64_ALPHABET_FS, input, input_length, cs_out); + return encode_base_generic(PGFE_BASE_PARAMS(BASE64), BASE64_ALPHABET_FS, input, input_length, cs_out); } inline size_t pgfe_decode_base64(const char base64_cs[], pgfe_encode_t output[]) { - return __pgfe_decode_base_generic(PGFE_BASE_PARAMS(BASE64), pgfe_decode_base64_char, base64_cs, output); + return decode_base_generic(PGFE_BASE_PARAMS(BASE64), decode_base64_char, base64_cs, output); } \ No newline at end of file diff --git a/src/cpp/base_encoding/base64.cpp b/src/base_encoding/base64.cpp similarity index 85% rename from src/cpp/base_encoding/base64.cpp rename to src/base_encoding/base64.cpp index 95d25be..27a34d1 100644 --- a/src/cpp/base_encoding/base64.cpp +++ b/src/base_encoding/base64.cpp @@ -2,12 +2,12 @@ libpgfe base64.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "base64.hpp" +#include "libpgfe/base_encoding/base64.hpp" -#include "base-encoding.h" +#include "libpgfe/base_encoding/base64.h" using namespace libpgfe; diff --git a/src/c/generic.c b/src/c/generic.c deleted file mode 100644 index ae57947..0000000 --- a/src/c/generic.c +++ /dev/null @@ -1,8 +0,0 @@ -/* - libpgfe - generic.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "generic.h" diff --git a/src/c/hash/sha-internal.c b/src/c/hash/sha-internal.c deleted file mode 100644 index f45f646..0000000 --- a/src/c/hash/sha-internal.c +++ /dev/null @@ -1,8 +0,0 @@ -/* - libpgfe - sha-internal.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "backend/sha-internal.h" diff --git a/src/c/hash/sha224.c b/src/c/hash/sha224.c deleted file mode 100644 index f813860..0000000 --- a/src/c/hash/sha224.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - libpgfe - sha224.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "sha2.h" - -#include - -#include "backend/sha2-backend.h" -#include "backend/templates.h" - -static const pgfe_word_t __pgfe_sha224_H0[] = { - 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939, 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4, -}; - -__PGFE_FRONTEND_GEN2(sha224) -__PGFE_FRONTEND_DEFAULT_GEN2(sha224, SHA224) - -__PGFE_SHA_INIT(sha224) -__PGFE_SHA_UPDATE(sha224, SHA224) - -inline void pgfe_sha224_digest(struct pgfe_sha224_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_sha224n256_digest(ctx, output, PGFE_SHA224_DIGEST_SIZE); -} diff --git a/src/c/hash/sha256.c b/src/c/hash/sha256.c deleted file mode 100644 index 790aa26..0000000 --- a/src/c/hash/sha256.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - libpgfe - sha256.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "sha2.h" - -#include - -#include "backend/sha2-backend.h" -#include "backend/templates.h" - -static const pgfe_word_t __pgfe_sha256_H0[] = { - 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, -}; - -__PGFE_FRONTEND_GEN2(sha256) -__PGFE_FRONTEND_DEFAULT_GEN2(sha256, SHA256) - -__PGFE_SHA_INIT(sha256) -__PGFE_SHA_UPDATE(sha256, SHA256) - -inline void pgfe_sha256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_sha224n256_digest(ctx, output, PGFE_SHA256_DIGEST_SIZE); -} \ No newline at end of file diff --git a/src/c/otp/otp-generic.c b/src/c/otp/otp-generic.c deleted file mode 100644 index 6a9460e..0000000 --- a/src/c/otp/otp-generic.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - libpgfe - otp-generic.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "backend/otp-generic.h" - -#define __ENS_BIT 8 -#define __SIZE 4 - -pgfe_otp_t pgfe_dynamically_truncate(const pgfe_encode_t hash[], size_t length) { - uint8_t offset = hash[length - 1] & 0xF; - pgfe_otp_t code = 0; - - for (size_t i = 0; i < __SIZE; i++) { - code <<= __ENS_BIT; - code |= hash[offset + i]; - } - - return code << 1 >> 1; -} \ No newline at end of file diff --git a/src/cpp/misc/abstract_symmetric_crypter.cpp b/src/core/abstract_symmetric_crypter.cpp similarity index 91% rename from src/cpp/misc/abstract_symmetric_crypter.cpp rename to src/core/abstract_symmetric_crypter.cpp index 81efab8..5ad2a43 100644 --- a/src/cpp/misc/abstract_symmetric_crypter.cpp +++ b/src/core/abstract_symmetric_crypter.cpp @@ -2,10 +2,10 @@ libpgfe abstract_symmetric_crypter.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend_cpp/abstract_symmetric_crypter.hpp" +#include "libpgfe/abstract_symmetric_crypter.hpp" #include diff --git a/src/cpp/algorithm_selectable.cpp b/src/core/algorithm_selectable.cpp similarity index 80% rename from src/cpp/algorithm_selectable.cpp rename to src/core/algorithm_selectable.cpp index dd151fd..e0783d8 100644 --- a/src/cpp/algorithm_selectable.cpp +++ b/src/core/algorithm_selectable.cpp @@ -2,10 +2,10 @@ libpgfe algorithm_selectable.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "algorithm_selectable.hpp" +#include "libpgfe/algorithm_selectable.hpp" using namespace libpgfe; diff --git a/src/c/generic-internal.c b/src/core/generic-internal.c similarity index 60% rename from src/c/generic-internal.c rename to src/core/generic-internal.c index d7c4029..bf24317 100644 --- a/src/c/generic-internal.c +++ b/src/core/generic-internal.c @@ -2,24 +2,24 @@ libpgfe generic-internal.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend/generic-internal.h" +#include "./generic-internal.h" #include -inline void __pgfe_arrinit(pgfe_encode_t arr[], size_t size) { +inline void arrinit(pgfe_encode_t arr[], size_t size) { memset(arr, 0, size); } -inline void __pgfe_arrcpy(pgfe_encode_t dest[], size_t dest_s, const pgfe_encode_t src[], size_t src_s) { +inline void arrcpy(pgfe_encode_t dest[], size_t dest_s, const pgfe_encode_t src[], size_t src_s) { size_t cpy_s = dest_s > src_s ? src_s : dest_s; memcpy(dest, src, cpy_s); - __pgfe_arrinit(dest + cpy_s, dest_s - cpy_s); + arrinit(dest + cpy_s, dest_s - cpy_s); } -inline void __pgfe_ch2hex(char ch, pgfe_encode_t *hex) { +inline void ch2hex(char ch, pgfe_encode_t *hex) { pgfe_encode_t tmp_o = 0; if (ch >= '0' && ch <= '9') { tmp_o = ch - '0'; @@ -37,7 +37,7 @@ inline void __pgfe_ch2hex(char ch, pgfe_encode_t *hex) { *hex = tmp_o; } -void __pgfe_reverse_elements(pgfe_encode_t *low, pgfe_encode_t *high) { +void reverse(pgfe_encode_t *low, pgfe_encode_t *high) { pgfe_encode_t tmp; for (; low < high; low++, high--) { diff --git a/include/generic.hpp b/src/core/generic-internal.h similarity index 65% rename from include/generic.hpp rename to src/core/generic-internal.h index 21ddbc7..7d60f02 100644 --- a/include/generic.hpp +++ b/src/core/generic-internal.h @@ -1,28 +1,48 @@ /* libpgfe - generic.hpp + generic-internal.h - Copyright (c) 2022 Charles Dong + Generic configuration backend + + Copyright (c) 2022-2023 Charles Dong */ -#ifndef LIBPGFE_GENERIC_HPP -#define LIBPGFE_GENERIC_HPP -#ifndef __cplusplus -#error libpgfe error: C++ headers are not compatible with C source +#ifndef LIBPGFE_GENERIC_INTERNAL_H +#define LIBPGFE_GENERIC_INTERNAL_H + +#include "libpgfe/generic.h" + +#ifdef __cplusplus +extern "C" { #endif -#include -#include +#define PGFE_HEX_UNKNOWN_CHAR 0xF1 -#include "algorithm-choice.h" -#include "exceptions.hpp" -#include "md5.h" -#include "sha1.h" -#include "sha2.h" -#include "sha3.h" +#define PGFE_ENCODER_DEF_SIG pgfe_encode_multi_func *func, size_t block_size, size_t digest_size +#define PGFE_ENCODER_CALL_SIG func, block_size, digest_size -#define __PGFE_BATCH_CASES(name) \ - switch (cur_alg) { \ +// Conversion mapping (to bit) +#define to_bit(value) pgfe_to_bit(value) +// Conversion mapping (to byte) +#define to_byte(value) pgfe_to_byte(value) +// Conversion mapping (bit remainder) +#define bit_rem(value) pgfe_bit_rem(value) + +// Circular left shift +#define clshift(n, c) (((n) << (c)) | ((n) >> (to_bit(sizeof(n)) - (c)))) +// Circular right shift +#define crshift(n, c) (((n) >> (c)) | ((n) << (to_bit(sizeof(n)) - (c)))) + +void arrinit(pgfe_encode_t arr[], size_t size); + +void arrcpy(pgfe_encode_t dest[], size_t dest_s, const pgfe_encode_t src[], size_t src_s); + +void ch2hex(char ch, pgfe_encode_t *hex); + +void reverse(pgfe_encode_t *low, pgfe_encode_t *high); + +#define __PGFE_BATCH_CASES(target, name) \ + switch (target) { \ __PGFE_##name##_CASE(MD5, md5); \ __PGFE_##name##_CASE(SHA1, sha1); \ __PGFE_##name##_CASE(SHA224, sha224); \ @@ -43,8 +63,8 @@ break; \ } -#define __PGFE_BATCH_CASES_SP(name) \ - switch (cur_alg) { \ +#define __PGFE_BATCH_CASES_SP(target, name) \ + switch (target) { \ __PGFE_##name##_CASE(MD5, md5); \ __PGFE_##name##_CASE(SHA1, sha1); \ __PGFE_##name##_CASE(SHA224, sha224); \ @@ -61,6 +81,18 @@ break; \ } +#define __PGFE_BATCH_CASES_SHAKE(target, name) \ + switch (target) { \ + __PGFE_##name##_CASE(RawSHAKE128, rawshake128); \ + __PGFE_##name##_CASE(SHAKE128, shake128); \ + __PGFE_##name##_CASE(RawSHAKE256, rawshake256); \ + __PGFE_##name##_CASE(SHAKE256, shake256); \ + default: \ + break; \ + } + +// CXX compatibility + #define __PGFE_BATCH_SHAKE_CASES(name) \ switch (cur_alg) { \ __PGFE_##name##_CASE(RawSHAKE128, rawshake128); \ @@ -97,90 +129,8 @@ delete (pgfe_##name##_ctx *)ctx; \ break -namespace libpgfe { - -static const std::unordered_map pgfe_option_map = { - {"sha1", SHA1 }, - {"SHA1", SHA1 }, - {"sha224", SHA224 }, - {"SHA224", SHA224 }, - {"sha256", SHA256 }, - {"SHA256", SHA256 }, - {"sha384", SHA384 }, - {"SHA384", SHA384 }, - {"sha512", SHA512 }, - {"SHA512", SHA512 }, - {"sha512_224", SHA512_224 }, - {"SHA512_224", SHA512_224 }, - {"sha512_256", SHA512_256 }, - {"SHA512_256", SHA512_256 }, - {"sha3_224", SHA3_224 }, - {"SHA3_224", SHA3_224 }, - {"sha3_256", SHA3_256 }, - {"SHA3_256", SHA3_256 }, - {"sha3_384", SHA3_384 }, - {"SHA3_384", SHA3_384 }, - {"sha3_512", SHA3_512 }, - {"SHA3_512", SHA3_512 }, - {"shake128", SHAKE128 }, - {"SHAKE128", SHAKE128 }, - {"rawshake128", RawSHAKE128}, - {"RawSHAKE128", RawSHAKE128}, - {"shake256", SHAKE256 }, - {"SHAKE256", SHAKE256 }, - {"rawshake256", RawSHAKE256}, - {"RawSHAKE256", RawSHAKE256}, - {"md5", MD5 }, - {"MD5", MD5 }, -}; - -static const std::unordered_map pgfe_digest_length = { - {SHA1, PGFE_SHA1_DIGEST_SIZE }, - {SHA224, PGFE_SHA224_DIGEST_SIZE }, - {SHA256, PGFE_SHA256_DIGEST_SIZE }, - {SHA384, PGFE_SHA384_DIGEST_SIZE }, - {SHA512, PGFE_SHA512_DIGEST_SIZE }, - {SHA512_224, PGFE_SHA512_224_DIGEST_SIZE }, - {SHA512_256, PGFE_SHA512_256_DIGEST_SIZE }, - {SHA3_224, PGFE_SHA3_224_DIGEST_SIZE }, - {SHA3_256, PGFE_SHA3_256_DIGEST_SIZE }, - {SHA3_384, PGFE_SHA3_384_DIGEST_SIZE }, - {SHA3_512, PGFE_SHA3_512_DIGEST_SIZE }, - {MD5, PGFE_MD5_DIGEST_SIZE }, - {SHAKE128, PGFE_SHAKE128_DIGEST_SIZE }, - {RawSHAKE128, PGFE_RawSHAKE128_DIGEST_SIZE}, - {SHAKE256, PGFE_SHAKE256_DIGEST_SIZE }, - {RawSHAKE256, PGFE_RawSHAKE256_DIGEST_SIZE}, -}; - -static const std::unordered_map pgfe_block_length = { - {SHA1, PGFE_SHA1_BLOCK_SIZE }, - {SHA224, PGFE_SHA224_BLOCK_SIZE }, - {SHA256, PGFE_SHA256_BLOCK_SIZE }, - {SHA384, PGFE_SHA384_BLOCK_SIZE }, - {SHA512, PGFE_SHA512_BLOCK_SIZE }, - {SHA512_224, PGFE_SHA512_224_BLOCK_SIZE }, - {SHA512_256, PGFE_SHA512_256_BLOCK_SIZE }, - {SHA3_224, PGFE_SHA3_224_BLOCK_SIZE }, - {SHA3_256, PGFE_SHA3_256_BLOCK_SIZE }, - {SHA3_384, PGFE_SHA3_384_BLOCK_SIZE }, - {SHA3_512, PGFE_SHA3_512_BLOCK_SIZE }, - {MD5, PGFE_MD5_BLOCK_SIZE }, - {SHAKE128, PGFE_SHAKE128_BLOCK_SIZE }, - {RawSHAKE128, PGFE_RawSHAKE128_BLOCK_SIZE}, - {SHAKE256, PGFE_SHAKE256_BLOCK_SIZE }, - {RawSHAKE256, PGFE_RawSHAKE256_BLOCK_SIZE}, -}; - -// String to algorithm choice type - -pgfe_algorithm_choice _algstr(const char *cs); -pgfe_algorithm_choice _algstr(std::string cpp_s); - -inline pgfe_algorithm_choice operator"" _pgfe_alg(const char *str, size_t size) { - return _algstr(str); +#ifdef __cplusplus } - -} // namespace libpgfe +#endif #endif \ No newline at end of file diff --git a/src/core/generic.c b/src/core/generic.c new file mode 100644 index 0000000..f9e7801 --- /dev/null +++ b/src/core/generic.c @@ -0,0 +1,8 @@ +/* + libpgfe + generic.c + + Copyright (c) 2022-2023 Charles Dong +*/ + +#include "libpgfe/generic.h" diff --git a/src/cpp/generic.cpp b/src/core/generic.cpp similarity index 76% rename from src/cpp/generic.cpp rename to src/core/generic.cpp index b4ef412..5a252f9 100644 --- a/src/cpp/generic.cpp +++ b/src/core/generic.cpp @@ -2,15 +2,15 @@ libpgfe generic.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_CPP #define LIBPGFE_GENERIC_CPP -#include "generic.hpp" +#include "libpgfe/generic.hpp" -#include "generic.h" +#include "libpgfe/generic.h" namespace libpgfe { diff --git a/src/cpp/sequential_data.cpp b/src/core/sequential_data.cpp similarity index 96% rename from src/cpp/sequential_data.cpp rename to src/core/sequential_data.cpp index 808117d..69d8c60 100644 --- a/src/cpp/sequential_data.cpp +++ b/src/core/sequential_data.cpp @@ -2,10 +2,10 @@ libpgfe sequential_data.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sequential_data.hpp" +#include "libpgfe/sequential_data.hpp" #include #include diff --git a/src/c/utils.c b/src/core/utils.c similarity index 82% rename from src/c/utils.c rename to src/core/utils.c index 8653e29..7fa55f6 100644 --- a/src/c/utils.c +++ b/src/core/utils.c @@ -2,14 +2,15 @@ libpgfe utils.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "utils.h" +#include "libpgfe/utils.h" +#include #include -#include "backend/generic-internal.h" +#include "./generic-internal.h" size_t pgfe_hash_to_hex_string(const pgfe_encode_t hash[], size_t hash_length, char out[]) { char *op = out, dual_hex[3]; @@ -43,7 +44,7 @@ size_t pgfe_hex_string_to_hash(const char hash_s[], pgfe_encode_t hash_out[]) { } for (op = hash_out, pos = hash_str_len % 2; *sp; sp++) { - __pgfe_ch2hex(*sp, &tmp_o); + ch2hex(*sp, &tmp_o); if (tmp_o == PGFE_HEX_UNKNOWN_CHAR) { continue; @@ -83,7 +84,7 @@ size_t pgfe_hash_string_clean(const char hash_s[], char output[]) { } } - __pgfe_ch2hex(*sp, &h); + ch2hex(*sp, &h); if (h == PGFE_HEX_UNKNOWN_CHAR) { continue; } @@ -108,18 +109,3 @@ size_t pgfe_hash_length(const char hash_s[]) { return len / 2 + len % 2; } - -inline time_t pgfe_curtime() { - return time(NULL); -} - -void pgfe_otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c) { - char *sp; - - for (sp = cs_out + digit_c - 1; sp >= cs_out; sp--) { - *sp = (char)(otp % 10 + '0'); - otp /= 10; - } - - cs_out[digit_c] = 0; -} \ No newline at end of file diff --git a/src/cpp/utils.cpp b/src/core/utils.cpp similarity index 89% rename from src/cpp/utils.cpp rename to src/core/utils.cpp index 28b631c..9d24d9e 100644 --- a/src/cpp/utils.cpp +++ b/src/core/utils.cpp @@ -2,14 +2,14 @@ libpgfe utils.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "utils.hpp" +#include "libpgfe/utils.hpp" #include -#include "utils.h" +#include "libpgfe/utils.h" namespace libpgfe { namespace utils { diff --git a/src/c/blowfish/blowfish.c b/src/fishes/blowfish.c similarity index 99% rename from src/c/blowfish/blowfish.c rename to src/fishes/blowfish.c index a2610c1..c04eb2a 100644 --- a/src/c/blowfish/blowfish.c +++ b/src/fishes/blowfish.c @@ -2,10 +2,10 @@ libpgfe blowfish.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "blowfish.h" +#include "libpgfe/fishes/blowfish.h" #include diff --git a/src/cpp/blowfish/blowfish.cpp b/src/fishes/blowfish.cpp similarity index 95% rename from src/cpp/blowfish/blowfish.cpp rename to src/fishes/blowfish.cpp index 51cf3d6..ee52a51 100644 --- a/src/cpp/blowfish/blowfish.cpp +++ b/src/fishes/blowfish.cpp @@ -2,10 +2,10 @@ libpgfe blowfish.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "blowfish.hpp" +#include "libpgfe/fishes/blowfish.hpp" #include #include diff --git a/src/cpp/hash/hash_encoder.cpp b/src/hashes/hash_encoder.cpp similarity index 89% rename from src/cpp/hash/hash_encoder.cpp rename to src/hashes/hash_encoder.cpp index 6c7bbab..e4c64a4 100644 --- a/src/cpp/hash/hash_encoder.cpp +++ b/src/hashes/hash_encoder.cpp @@ -2,20 +2,18 @@ libpgfe hash_encoder.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "hash_encoder.hpp" +#include "libpgfe/hashes/hash_encoder.hpp" #include #include -#include "md5.h" -#include "sha1.h" -#include "sha2.h" -#include "sha3.h" +#include "libpgfe/hashes.h" +#include "libpgfe/utils.h" -#include "utils.h" +#include "../core/generic-internal.h" #define __PGFE_CTX_CREATE_CASE(alg, name) \ case alg: \ @@ -52,7 +50,7 @@ using namespace libpgfe; void HashEncoder::destroy_context() { if (!ctx) return; - __PGFE_BATCH_CASES(CTX_DELETE) + __PGFE_BATCH_CASES(cur_alg, CTX_DELETE) } void HashEncoder::before_change_alg() { @@ -60,9 +58,9 @@ void HashEncoder::before_change_alg() { } void HashEncoder::load_algorithm() { - __PGFE_BATCH_CASES(CTX_CREATE) - __PGFE_BATCH_CASES(INIT_FUNC_CALL) - __PGFE_BATCH_CASES(INIT_SIZE) + __PGFE_BATCH_CASES(cur_alg, CTX_CREATE) + __PGFE_BATCH_CASES(cur_alg, INIT_FUNC_CALL) + __PGFE_BATCH_CASES(cur_alg, INIT_SIZE) } void HashEncoder::reset() { @@ -84,7 +82,7 @@ HashEncoder::~HashEncoder() { } void HashEncoder::update(const pgfe_encode_t sequence[], size_t length) { - __PGFE_BATCH_CASES(UPDATE_FUNC_CALL3) + __PGFE_BATCH_CASES(cur_alg, UPDATE_FUNC_CALL3) } inline void HashEncoder::update(const char cs[]) { @@ -120,7 +118,7 @@ const SequentialData *HashEncoder::get_digest(uint64_t bitlength) { __PGFE_BATCH_SHAKE_CASES(DIGEST_FUNC_CALL_LIMIT) } else { - __PGFE_BATCH_CASES_SP(DIGEST_FUNC_CALL) + __PGFE_BATCH_CASES_SP(cur_alg, DIGEST_FUNC_CALL) } out = std::make_unique((const pgfe_encode_t *)seq, length); diff --git a/src/c/hash/keccak-backend.c b/src/hashes/keccak-backend.c similarity index 90% rename from src/c/hash/keccak-backend.c rename to src/hashes/keccak-backend.c index 2efd3a2..577a6d0 100644 --- a/src/c/hash/keccak-backend.c +++ b/src/hashes/keccak-backend.c @@ -2,19 +2,19 @@ libpgfe keccak-backend.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend/keccak-backend.h" +#include "./keccak-backend.h" #include #include -static const uint16_t _r[24] = { +static const uint16_t r[24] = { 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44, }; -static const uint64_t _RC[24] = { +static const uint64_t RC[24] = { 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, @@ -22,7 +22,7 @@ static const uint64_t _RC[24] = { 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008, }; -static const uint16_t _piln[24] = { +static const uint16_t piln[24] = { 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1, }; @@ -47,9 +47,9 @@ static void transform(pgfe_keccak_bitcube_t A, uint64_t RC) { // Rho & Pi t = A[1][0]; for (x = 0; x < 24; x++) { - j = _piln[x]; + j = piln[x]; C[0] = A[j % 5][j / 5]; - A[j % 5][j / 5] = clshift(t, _r[x]); + A[j % 5][j / 5] = clshift(t, r[x]); t = C[0]; } @@ -70,10 +70,10 @@ static void transform(pgfe_keccak_bitcube_t A, uint64_t RC) { A[0][0] ^= RC; } -int __pgfe_keccak_init(struct pgfe_keccak_sponge_ctx *ctx, uint32_t capacity) { - uint32_t r = __PGFE_KECCAK_b - capacity; +int keccak_init(struct pgfe_keccak_sponge_ctx *ctx, uint32_t capacity) { + uint32_t r = KECCAK_b - capacity; - if (r >= __PGFE_KECCAK_b || r % 64) { + if (r >= KECCAK_b || r % 64) { return EXIT_FAILURE; } @@ -109,7 +109,7 @@ seq_to_state_jump_end:; static void permutation(pgfe_keccak_bitcube_t A, uint16_t nr) { for (uint16_t i = 0; i < nr; i++) { - transform(A, _RC[i]); + transform(A, RC[i]); } } @@ -164,7 +164,7 @@ static void padding(struct pgfe_keccak_sponge_ctx *ctx) { ctx->squeezing = 1; } -int __pgfe_keccak_absorb_b1600(struct pgfe_keccak_sponge_ctx *ctx, const pgfe_encode_t input[], uint64_t bit_len) { +int keccak_absorb_b1600(struct pgfe_keccak_sponge_ctx *ctx, const pgfe_encode_t input[], uint64_t bit_len) { uint64_t i, j, whole_blocks; uint32_t partial_block, partial_byte; uint8_t mask; @@ -211,12 +211,12 @@ int __pgfe_keccak_absorb_b1600(struct pgfe_keccak_sponge_ctx *ctx, const pgfe_en return EXIT_SUCCESS; } -int __pgfe_keccak_squeeze_b1600(struct pgfe_keccak_sponge_ctx *ctx, pgfe_encode_t output[]) { +int keccak_squeeze_b1600(struct pgfe_keccak_sponge_ctx *ctx, pgfe_encode_t output[]) { uint64_t i, bit_len = ctx->out_length; uint32_t partial_block; if (ctx->ap_len && !ctx->ap_added) { - __pgfe_keccak_absorb_b1600(ctx, &ctx->ap, ctx->ap_len); + keccak_absorb_b1600(ctx, &ctx->ap, ctx->ap_len); ctx->ap_added = 1; } diff --git a/src/hashes/keccak-backend.h b/src/hashes/keccak-backend.h new file mode 100644 index 0000000..4bfbdbd --- /dev/null +++ b/src/hashes/keccak-backend.h @@ -0,0 +1,35 @@ +/* + libpgfe + keccak-backend.h + + Backend implementation of SHA3/Keccak algorithm + + Copyright (c) 2022-2023 Charles Dong +*/ + +#ifndef LIBPGFE_KECCAK_BACKEND_H +#define LIBPGFE_KECCAK_BACKEND_H + +#include + +#include "libpgfe/hashes/sha3.h" + +#include "../core/generic-internal.h" + +#define KECCAK_b 1600 + +#ifdef __cplusplus +extern "C" { +#endif + +int keccak_init(struct pgfe_keccak_sponge_ctx *ctx, uint32_t capacity); + +int keccak_absorb_b1600(struct pgfe_keccak_sponge_ctx *ctx, const pgfe_encode_t input[], uint64_t bit_len); + +int keccak_squeeze_b1600(struct pgfe_keccak_sponge_ctx *ctx, pgfe_encode_t output[]); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/c/hash/md5-backend.c b/src/hashes/md5-backend.c similarity index 87% rename from src/c/hash/md5-backend.c rename to src/hashes/md5-backend.c index 27abeb6..444743c 100644 --- a/src/c/hash/md5-backend.c +++ b/src/hashes/md5-backend.c @@ -2,10 +2,10 @@ libpgfe md5-backend.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend/md5-backend.h" +#include "./md5-backend.h" #include @@ -18,21 +18,21 @@ #define _O(i, offset, limit) (((i) + (offset)) % (limit)) -static const uint8_t __S[4][4] = { +static const uint8_t S[4][4] = { {7, 12, 17, 22}, {5, 9, 14, 20}, {4, 11, 16, 23}, {6, 10, 15, 21}, }; -static const uint8_t __idx[4][16] = { +static const uint8_t idx[4][16] = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12}, {5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2 }, {0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 }, }; -static const uint32_t __ac[4][16] = { +static const uint32_t ac[4][16] = { {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821}, {0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, @@ -43,7 +43,7 @@ static const uint32_t __ac[4][16] = { 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}, }; -#define __F(r, a, b, c, d, x, s, ac) \ +#define _F(r, a, b, c, d, x, s, ac) \ switch (r) { \ case 0: \ _ROUND_TRAN((a), F((b), (c), (d)), (b), (x), (s), (ac)); \ @@ -61,7 +61,7 @@ static const uint32_t __ac[4][16] = { break; \ } -void __pgfe_md5_decode(const pgfe_encode_t input[], size_t length, pgfe_word_t output[]) { +void md5_decode(const pgfe_encode_t input[], size_t length, pgfe_word_t output[]) { size_t i, ix4; for (i = ix4 = 0; ix4 < length; i++, ix4 += 4) { @@ -72,7 +72,7 @@ void __pgfe_md5_decode(const pgfe_encode_t input[], size_t length, pgfe_word_t o } } -void __pgfe_md5_encode(pgfe_word_t input[], size_t length, pgfe_encode_t output[]) { +void md5_encode(pgfe_word_t input[], size_t length, pgfe_encode_t output[]) { size_t i, ix4; for (i = ix4 = 0; ix4 < length; i++, ix4 += 4) { @@ -83,7 +83,7 @@ void __pgfe_md5_encode(pgfe_word_t input[], size_t length, pgfe_encode_t output[ } } -void __pgfe_md5_transform(pgfe_word_t state[4], const pgfe_encode_t block[64]) { +void md5_transform(pgfe_word_t state[4], const pgfe_encode_t block[64]) { pgfe_word_t x[16], buf[4]; uint16_t r, i; @@ -93,13 +93,13 @@ void __pgfe_md5_transform(pgfe_word_t state[4], const pgfe_encode_t block[64]) { buf[1] = state[2]; // c buf[0] = state[3]; // d - __pgfe_md5_decode(block, 64, x); + md5_decode(block, 64, x); // Round 1 - 4 for (r = 0; r < 4; r++) { for (i = 0; i < 16; i++) { - __F(r, buf[_O(i, 3, 4)], buf[_O(i, 2, 4)], buf[_O(i, 1, 4)], buf[_O(i, 0, 4)], x[__idx[r][i]], - __S[r][i % 4], __ac[r][i]); + _F(r, buf[_O(i, 3, 4)], buf[_O(i, 2, 4)], buf[_O(i, 1, 4)], buf[_O(i, 0, 4)], x[idx[r][i]], S[r][i % 4], + ac[r][i]); } } diff --git a/src/hashes/md5-backend.h b/src/hashes/md5-backend.h new file mode 100644 index 0000000..a7c0440 --- /dev/null +++ b/src/hashes/md5-backend.h @@ -0,0 +1,29 @@ +/* + libpgfe + md5-backend.h + + Backend implementation of MD5 algorithm + + Copyright (c) 2022-2023 Charles Dong +*/ + +#ifndef LIBPGFE_MD5_BACKEND_H +#define LIBPGFE_MD5_BACKEND_H + +#include "../core/generic-internal.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void md5_transform(pgfe_word_t state[4], const pgfe_encode_t block[64]); + +void md5_encode(pgfe_word_t input[], size_t length, pgfe_encode_t output[]); + +void md5_decode(const pgfe_encode_t input[], size_t length, pgfe_word_t output[]); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/c/hash/md5.c b/src/hashes/md5.c similarity index 79% rename from src/c/hash/md5.c rename to src/hashes/md5.c index 7c2d610..551e9f0 100644 --- a/src/c/hash/md5.c +++ b/src/hashes/md5.c @@ -2,18 +2,18 @@ libpgfe md5.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "md5.h" +#include "libpgfe/hashes/md5.h" #include -#include "backend/md5-backend.h" -#include "backend/templates.h" +#include "./md5-backend.h" +#include "./templates.h" -__PGFE_FRONTEND_GEN2(md5); -__PGFE_FRONTEND_DEFAULT_GEN2(md5, MD5); +FRONTEND_GEN2(md5); +FRONTEND_GEN2_DEFAULT(md5, MD5); static const pgfe_word_t H0[] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; @@ -42,10 +42,10 @@ void pgfe_md5_update(struct pgfe_md5_ctx *ctx, const pgfe_encode_t input[], size if (length >= part_len) { memcpy(ctx->block + index, input, part_len); - __pgfe_md5_transform(ctx->state, ctx->block); + md5_transform(ctx->state, ctx->block); for (i = part_len; i + 63 < length; i += 64) { - __pgfe_md5_transform(ctx->state, input + i); + md5_transform(ctx->state, input + i); } index = 0; @@ -61,7 +61,7 @@ void pgfe_md5_digest(struct pgfe_md5_ctx *ctx, pgfe_encode_t output[]) { pgfe_encode_t chunk[8]; size_t i, padding_len; - __pgfe_md5_encode(ctx->count, 8, chunk); + md5_encode(ctx->count, 8, chunk); i = (size_t)((ctx->count[0] >> 3) & 0x3F); padding_len = (i < 56) ? (56 - i) : (120 - i); @@ -73,5 +73,5 @@ void pgfe_md5_digest(struct pgfe_md5_ctx *ctx, pgfe_encode_t output[]) { memset(ctx->block, 0, sizeof(ctx->block)); ctx->count[0] = ctx->count[1] = 0; - __pgfe_md5_encode(ctx->state, PGFE_MD5_DIGEST_SIZE, output); + md5_encode(ctx->state, PGFE_MD5_DIGEST_SIZE, output); } \ No newline at end of file diff --git a/src/hashes/sha-internal.c b/src/hashes/sha-internal.c new file mode 100644 index 0000000..62773f7 --- /dev/null +++ b/src/hashes/sha-internal.c @@ -0,0 +1,8 @@ +/* + libpgfe + sha-internal.c + + Copyright (c) 2022-2023 Charles Dong +*/ + +#include "./sha-internal.h" diff --git a/include/backend/sha-internal.h b/src/hashes/sha-internal.h similarity index 78% rename from include/backend/sha-internal.h rename to src/hashes/sha-internal.h index d5a2f0a..b2177c6 100644 --- a/include/backend/sha-internal.h +++ b/src/hashes/sha-internal.h @@ -4,13 +4,13 @@ Generic implementation of SHA family - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SHA_INTERNAL_H #define LIBPGFE_SHA_INTERNAL_H -#include "generic-internal.h" +#include "../core/generic-internal.h" #ifdef __cplusplus extern "C" { @@ -21,7 +21,7 @@ extern "C" { #define sha_parity(x, y, z) ((x) ^ (y) ^ (z)) -#define __PGFE_PADDING_HEADER 0x80 +#define PADDING_HEADER 0x80 #ifdef __cplusplus } diff --git a/src/c/hash/sha1.c b/src/hashes/sha1.c similarity index 78% rename from src/c/hash/sha1.c rename to src/hashes/sha1.c index d992aec..f044eb9 100644 --- a/src/c/hash/sha1.c +++ b/src/hashes/sha1.c @@ -2,23 +2,23 @@ libpgfe sha1.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha1.h" +#include "libpgfe/hashes/sha1.h" #include -#include "backend/generic-internal.h" -#include "backend/sha-internal.h" -#include "backend/templates.h" +#include "../core/generic-internal.h" +#include "./sha-internal.h" +#include "./templates.h" -static const pgfe_word_t __pgfe_sha1_H0[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; +static const pgfe_word_t H0[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; -__PGFE_FRONTEND_GEN2(sha1) -__PGFE_FRONTEND_DEFAULT_GEN2(sha1, SHA1) +FRONTEND_GEN2(sha1) +FRONTEND_GEN2_DEFAULT(sha1, SHA1) -static void __pgfe_sha1_process_block(struct pgfe_sha1_ctx *ctx) { +static void sha1_process_block(struct pgfe_sha1_ctx *ctx) { // Constants in SHA1 static const pgfe_word_t K[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6}; @@ -91,14 +91,14 @@ static void __pgfe_sha1_process_block(struct pgfe_sha1_ctx *ctx) { memset(ws, 0, sizeof(ws)); } -static void __pgfe_sha1_padding(struct pgfe_sha1_ctx *ctx) { - ctx->block[ctx->index++] = __PGFE_PADDING_HEADER; +static void sha1_padding(struct pgfe_sha1_ctx *ctx) { + ctx->block[ctx->index++] = PADDING_HEADER; // 56 = PGFE_SHA1_BLOCK_SIZE - 8 if (ctx->index > 56) { memset(ctx->block + ctx->index, 0, PGFE_SHA1_BLOCK_SIZE - ctx->index); ctx->index = PGFE_SHA1_BLOCK_SIZE; - __pgfe_sha1_process_block(ctx); + sha1_process_block(ctx); } else if (ctx->index < 56) { memset(ctx->block + ctx->index, 0, 56 - ctx->index); @@ -115,9 +115,9 @@ static void __pgfe_sha1_padding(struct pgfe_sha1_ctx *ctx) { ctx->block[62] = (uint8_t)(ctx->len_low >> 8); ctx->block[63] = (uint8_t)ctx->len_low; - __pgfe_sha1_process_block(ctx); + sha1_process_block(ctx); } -__PGFE_SHA_INIT(sha1) -__PGFE_SHA_UPDATE(sha1, SHA1) -__PGFE_SHA_DIGEST(sha1, SHA1) +SHA_INIT(sha1) +SHA_UPDATE(sha1, SHA1) +SHA_DIGEST(sha1, SHA1) diff --git a/src/c/hash/sha2-backend.c b/src/hashes/sha2-backend.c similarity index 88% rename from src/c/hash/sha2-backend.c rename to src/hashes/sha2-backend.c index 9d0f0e0..b9bf17a 100644 --- a/src/c/hash/sha2-backend.c +++ b/src/hashes/sha2-backend.c @@ -2,19 +2,19 @@ libpgfe sha2-backend.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend/sha2-backend.h" +#include "./sha2-backend.h" -#include "backend/templates.h" -#include "sha2.h" +#include "./templates.h" +#include "libpgfe/hashes/sha2.h" #include // SHA-224/256 -void __pgfe_sha256_process_block(struct pgfe_sha256_ctx *ctx) { +void sha256_process_block(struct pgfe_sha256_ctx *ctx) { static const pgfe_word_t K[] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, @@ -78,14 +78,14 @@ void __pgfe_sha256_process_block(struct pgfe_sha256_ctx *ctx) { memset(ws, 0, PGFE_SHA256_BLOCK_SIZE); } -void __pgfe_sha224n256_padding(struct pgfe_sha256_ctx *ctx) { - ctx->block[ctx->index++] = __PGFE_PADDING_HEADER; +void sha224n256_padding(struct pgfe_sha256_ctx *ctx) { + ctx->block[ctx->index++] = PADDING_HEADER; // 56 = PGFE_SHA256_BLOCK_SIZE - 8 if (ctx->index > 56) { memset(ctx->block + ctx->index, 0, PGFE_SHA256_BLOCK_SIZE - ctx->index); ctx->index = PGFE_SHA256_BLOCK_SIZE; - __pgfe_sha256_process_block(ctx); + sha256_process_block(ctx); } else if (ctx->index < 56) { memset(ctx->block + ctx->index, 0, 56 - ctx->index); @@ -102,15 +102,15 @@ void __pgfe_sha224n256_padding(struct pgfe_sha256_ctx *ctx) { ctx->block[62] = (uint8_t)(ctx->len_low >> 8); ctx->block[63] = (uint8_t)(ctx->len_low); - __pgfe_sha256_process_block(ctx); + sha256_process_block(ctx); } -void __pgfe_sha224n256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size) { +void sha224n256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size) { if (!ctx || !output) return; size_t i; - __pgfe_sha224n256_padding(ctx); + sha224n256_padding(ctx); // Wipe sensitive data from the RAM memset(ctx->block, 0, sizeof(ctx->block)); @@ -124,7 +124,7 @@ void __pgfe_sha224n256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[ // SHA-384/512 -void __pgfe_sha384n512_process_block(struct pgfe_sha512_ctx *ctx) { +void sha384n512_process_block(struct pgfe_sha512_ctx *ctx) { static const pgfe_word_t K[] = { 0xD728AE22, 0x428A2F98, 0x23EF65CD, 0x71374491, 0xEC4D3B2F, 0xB5C0FBCF, 0x8189DBBC, 0xE9B5DBA5, 0xF348B538, 0x3956C25B, 0xB605D019, 0x59F111F1, 0xAF194F9B, 0x923F82A4, 0xDA6D8118, 0xAB1C5ED5, 0xA3030242, 0xD807AA98, @@ -201,7 +201,7 @@ void __pgfe_sha384n512_process_block(struct pgfe_sha512_ctx *ctx) { memset(ws, 0, 80); } -void __pgfe_sha384n512_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length) { +void sha384n512_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length) { if (!ctx || !input) return; const pgfe_encode_t *inp = input; @@ -219,21 +219,21 @@ void __pgfe_sha384n512_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t i } if (!corrupt_flag && ctx->index == PGFE_SHA512_BLOCK_SIZE) { - __pgfe_sha384n512_process_block(ctx); + sha384n512_process_block(ctx); } inp++; } } -void __pgfe_sha384n512_padding(struct pgfe_sha512_ctx *ctx, pgfe_encode_t padding_byte) { +void sha384n512_padding(struct pgfe_sha512_ctx *ctx, pgfe_encode_t padding_byte) { ctx->block[ctx->index++] = padding_byte; // 112 = PGFE_SHA512_BLOCK_SIZE - 16 if (ctx->index > 112) { memset(&ctx->block[ctx->index], 0, PGFE_SHA512_BLOCK_SIZE - ctx->index); ctx->index = PGFE_SHA512_BLOCK_SIZE; - __pgfe_sha384n512_process_block(ctx); + sha384n512_process_block(ctx); } else if (ctx->index < 112) { memset(&ctx->block[ctx->index], 0, 112 - ctx->index); @@ -257,15 +257,15 @@ void __pgfe_sha384n512_padding(struct pgfe_sha512_ctx *ctx, pgfe_encode_t paddin ctx->block[126] = (uint8_t)(ctx->len_low >> 8); ctx->block[127] = (uint8_t)ctx->len_low; - __pgfe_sha384n512_process_block(ctx); + sha384n512_process_block(ctx); } -void __pgfe_sha384n512_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size) { +void sha384n512_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size) { if (!ctx || !output) return; size_t i; - __pgfe_sha384n512_padding(ctx, __PGFE_PADDING_HEADER); + sha384n512_padding(ctx, PADDING_HEADER); // Wipe data from RAM memset(ctx->block, 0, sizeof(ctx->block)); diff --git a/include/backend/sha2-backend.h b/src/hashes/sha2-backend.h similarity index 51% rename from include/backend/sha2-backend.h rename to src/hashes/sha2-backend.h index 413cf9b..055733b 100644 --- a/include/backend/sha2-backend.h +++ b/src/hashes/sha2-backend.h @@ -4,15 +4,15 @@ Generic implementation of SHA2 family - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SHA2_INTERNAL_H #define LIBPGFE_SHA2_INTERNAL_H -#include "sha-internal.h" +#include "./sha-internal.h" -#include "../sha2.h" +#include "libpgfe/hashes/sha2.h" #ifdef __cpluslpus extern "C" { @@ -28,25 +28,25 @@ extern "C" { #define sha512_sigma0(word) (crshift((word), 1) ^ crshift((word), 8) ^ ((word) >> 7)) #define sha512_sigma1(word) (crshift((word), 19) ^ crshift((word), 61) ^ ((word) >> 6)) -void __pgfe_sha224n256_init(struct pgfe_sha256_ctx *ctx, const pgfe_word_t H0[]); +void sha224n256_init(struct pgfe_sha256_ctx *ctx, const pgfe_word_t H0[]); -void __pgfe_sha256_process_block(struct pgfe_sha256_ctx *ctx); +void sha256_process_block(struct pgfe_sha256_ctx *ctx); -#define __pgfe_sha224_process_block __pgfe_sha256_process_block +#define sha224_process_block sha256_process_block -void __pgfe_sha224n256_padding(struct pgfe_sha256_ctx *ctx); +void sha224n256_padding(struct pgfe_sha256_ctx *ctx); -void __pgfe_sha224n256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size); +void sha224n256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size); -void __pgfe_sha384n512_init(struct pgfe_sha512_ctx *ctx, const pgfe_word_t H0[]); +void sha384n512_init(struct pgfe_sha512_ctx *ctx, const pgfe_word_t H0[]); -void __pgfe_sha384n512_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length); +void sha384n512_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length); -void __pgfe_sha384n512_process_block(struct pgfe_sha512_ctx *ctx); +void sha384n512_process_block(struct pgfe_sha512_ctx *ctx); -void __pgfe_sha384n512_padding(struct pgfe_sha512_ctx *ctx, pgfe_encode_t padding_byte); +void sha384n512_padding(struct pgfe_sha512_ctx *ctx, pgfe_encode_t padding_byte); -void __pgfe_sha384n512_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size); +void sha384n512_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[], uint8_t digest_size); #ifdef __cplusplus } diff --git a/src/hashes/sha224.c b/src/hashes/sha224.c new file mode 100644 index 0000000..0ec3a8f --- /dev/null +++ b/src/hashes/sha224.c @@ -0,0 +1,27 @@ +/* + libpgfe + sha224.c + + Copyright (c) 2022-2023 Charles Dong +*/ + +#include "libpgfe/hashes/sha2.h" + +#include + +#include "./sha2-backend.h" +#include "./templates.h" + +static const pgfe_word_t H0[] = { + 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939, 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4, +}; + +FRONTEND_GEN2(sha224) +FRONTEND_GEN2_DEFAULT(sha224, SHA224) + +SHA_INIT(sha224) +SHA_UPDATE(sha224, SHA224) + +inline void pgfe_sha224_digest(struct pgfe_sha224_ctx *ctx, pgfe_encode_t output[]) { + sha224n256_digest(ctx, output, PGFE_SHA224_DIGEST_SIZE); +} diff --git a/src/hashes/sha256.c b/src/hashes/sha256.c new file mode 100644 index 0000000..b7b835a --- /dev/null +++ b/src/hashes/sha256.c @@ -0,0 +1,27 @@ +/* + libpgfe + sha256.c + + Copyright (c) 2022-2023 Charles Dong +*/ + +#include "libpgfe/hashes/sha2.h" + +#include + +#include "./sha2-backend.h" +#include "./templates.h" + +static const pgfe_word_t H0[] = { + 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, +}; + +FRONTEND_GEN2(sha256) +FRONTEND_GEN2_DEFAULT(sha256, SHA256) + +SHA_INIT(sha256) +SHA_UPDATE(sha256, SHA256) + +inline void pgfe_sha256_digest(struct pgfe_sha256_ctx *ctx, pgfe_encode_t output[]) { + sha224n256_digest(ctx, output, PGFE_SHA256_DIGEST_SIZE); +} \ No newline at end of file diff --git a/src/c/hash/sha3-224.c b/src/hashes/sha3-224.c similarity index 57% rename from src/c/hash/sha3-224.c rename to src/hashes/sha3-224.c index f37437a..a0d7383 100644 --- a/src/c/hash/sha3-224.c +++ b/src/hashes/sha3-224.c @@ -2,29 +2,30 @@ libpgfe sha3-224.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha3.h" +#include "libpgfe/hashes/sha3.h" #include -#include "backend/templates.h" +#include "./keccak-backend.h" +#include "./templates.h" -__PGFE_FRONTEND_GEN2(sha3_224); -__PGFE_FRONTEND_DEFAULT_GEN2(sha3_224, SHA3_224); +FRONTEND_GEN2(sha3_224); +FRONTEND_GEN2_DEFAULT(sha3_224, SHA3_224); inline void pgfe_sha3_224_init(struct pgfe_sha3_224_ctx *ctx) { - __pgfe_keccak_init(ctx, 448); + keccak_init(ctx, 448); ctx->out_length = 224; ctx->ap = PGFE_SHA3_APPENDIX; ctx->ap_len = PGFE_SHA3_APPENDIX_SIZE; } inline void pgfe_sha3_224_update(struct pgfe_sha3_224_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); + keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); } inline void pgfe_sha3_224_digest(struct pgfe_sha3_224_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_keccak_squeeze_b1600(ctx, output); + keccak_squeeze_b1600(ctx, output); } \ No newline at end of file diff --git a/src/c/hash/sha3-256.c b/src/hashes/sha3-256.c similarity index 57% rename from src/c/hash/sha3-256.c rename to src/hashes/sha3-256.c index 637fda6..57e6af2 100644 --- a/src/c/hash/sha3-256.c +++ b/src/hashes/sha3-256.c @@ -2,29 +2,30 @@ libpgfe sha3-256.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha3.h" +#include "libpgfe/hashes/sha3.h" #include -#include "backend/templates.h" +#include "./keccak-backend.h" +#include "./templates.h" -__PGFE_FRONTEND_GEN2(sha3_256); -__PGFE_FRONTEND_DEFAULT_GEN2(sha3_256, SHA3_256); +FRONTEND_GEN2(sha3_256); +FRONTEND_GEN2_DEFAULT(sha3_256, SHA3_256); inline void pgfe_sha3_256_init(struct pgfe_sha3_256_ctx *ctx) { - __pgfe_keccak_init(ctx, 512); + keccak_init(ctx, 512); ctx->out_length = 256; ctx->ap = PGFE_SHA3_APPENDIX; ctx->ap_len = PGFE_SHA3_APPENDIX_SIZE; } inline void pgfe_sha3_256_update(struct pgfe_sha3_256_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); + keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); } inline void pgfe_sha3_256_digest(struct pgfe_sha3_256_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_keccak_squeeze_b1600(ctx, output); + keccak_squeeze_b1600(ctx, output); } \ No newline at end of file diff --git a/src/c/hash/sha3-384.c b/src/hashes/sha3-384.c similarity index 57% rename from src/c/hash/sha3-384.c rename to src/hashes/sha3-384.c index c5424e6..88b2e27 100644 --- a/src/c/hash/sha3-384.c +++ b/src/hashes/sha3-384.c @@ -2,29 +2,30 @@ libpgfe sha3-384.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha3.h" +#include "libpgfe/hashes/sha3.h" #include -#include "backend/templates.h" +#include "./keccak-backend.h" +#include "./templates.h" -__PGFE_FRONTEND_GEN2(sha3_384); -__PGFE_FRONTEND_DEFAULT_GEN2(sha3_384, SHA3_384); +FRONTEND_GEN2(sha3_384); +FRONTEND_GEN2_DEFAULT(sha3_384, SHA3_384); inline void pgfe_sha3_384_init(struct pgfe_sha3_384_ctx *ctx) { - __pgfe_keccak_init(ctx, 768); + keccak_init(ctx, 768); ctx->out_length = 384; ctx->ap = PGFE_SHA3_APPENDIX; ctx->ap_len = PGFE_SHA3_APPENDIX_SIZE; } inline void pgfe_sha3_384_update(struct pgfe_sha3_384_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); + keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); } inline void pgfe_sha3_384_digest(struct pgfe_sha3_384_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_keccak_squeeze_b1600(ctx, output); + keccak_squeeze_b1600(ctx, output); } \ No newline at end of file diff --git a/src/c/hash/sha3-512.c b/src/hashes/sha3-512.c similarity index 56% rename from src/c/hash/sha3-512.c rename to src/hashes/sha3-512.c index 39142f1..2eccb93 100644 --- a/src/c/hash/sha3-512.c +++ b/src/hashes/sha3-512.c @@ -2,29 +2,30 @@ libpgfe sha3-512.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha3.h" +#include "libpgfe/hashes/sha3.h" #include -#include "backend/templates.h" +#include "./keccak-backend.h" +#include "./templates.h" -__PGFE_FRONTEND_GEN2(sha3_512); -__PGFE_FRONTEND_DEFAULT_GEN2(sha3_512, SHA3_512); +FRONTEND_GEN2(sha3_512); +FRONTEND_GEN2_DEFAULT(sha3_512, SHA3_512); inline void pgfe_sha3_512_init(struct pgfe_sha3_512_ctx *ctx) { - __pgfe_keccak_init(ctx, 1024); + keccak_init(ctx, 1024); ctx->out_length = 512; ctx->ap = PGFE_SHA3_APPENDIX; ctx->ap_len = PGFE_SHA3_APPENDIX_SIZE; } inline void pgfe_sha3_512_update(struct pgfe_sha3_512_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); + keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); } inline void pgfe_sha3_512_digest(struct pgfe_sha3_512_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_keccak_squeeze_b1600(ctx, output); + keccak_squeeze_b1600(ctx, output); } \ No newline at end of file diff --git a/src/c/hash/sha384.c b/src/hashes/sha384.c similarity index 58% rename from src/c/hash/sha384.c rename to src/hashes/sha384.c index 95cad0a..ce5c1f6 100644 --- a/src/c/hash/sha384.c +++ b/src/hashes/sha384.c @@ -2,23 +2,23 @@ libpgfe sha384.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha2.h" +#include "libpgfe/hashes/sha2.h" #include -#include "backend/sha2-backend.h" -#include "backend/templates.h" +#include "./sha2-backend.h" +#include "./templates.h" -static const pgfe_word_t __pgfe_sha384_H0[] = { +static const pgfe_word_t H0[] = { 0xC1059ED8, 0xCBBB9D5D, 0x367CD507, 0x629A292A, 0x3070DD17, 0x9159015A, 0xF70E5939, 0x152FECD8, 0xFFC00B31, 0x67332667, 0x68581511, 0x8EB44A87, 0x64F98FA7, 0xDB0C2E0D, 0xBEFA4FA4, 0x47B5481D, }; -__PGFE_FRONTEND_GEN2(sha384) -__PGFE_FRONTEND_DEFAULT_GEN2(sha384, SHA384) +FRONTEND_GEN2(sha384) +FRONTEND_GEN2_DEFAULT(sha384, SHA384) void pgfe_sha384_init(struct pgfe_sha384_ctx *ctx) { if (!ctx) return; @@ -26,13 +26,13 @@ void pgfe_sha384_init(struct pgfe_sha384_ctx *ctx) { ctx->len_low = ctx->len_high = 0; ctx->index = 0; - memcpy(ctx->state, __pgfe_sha384_H0, sizeof(ctx->state)); + memcpy(ctx->state, H0, sizeof(ctx->state)); } inline void pgfe_sha384_update(struct pgfe_sha384_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_sha384n512_update(ctx, input, length); + sha384n512_update(ctx, input, length); } inline void pgfe_sha384_digest(struct pgfe_sha384_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_sha384n512_digest(ctx, output, PGFE_SHA384_DIGEST_SIZE); + sha384n512_digest(ctx, output, PGFE_SHA384_DIGEST_SIZE); } \ No newline at end of file diff --git a/src/c/hash/sha512.c b/src/hashes/sha512.c similarity index 61% rename from src/c/hash/sha512.c rename to src/hashes/sha512.c index 22327f2..70b6d92 100644 --- a/src/c/hash/sha512.c +++ b/src/hashes/sha512.c @@ -2,27 +2,27 @@ libpgfe sha512.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha2.h" +#include "libpgfe/hashes/sha2.h" #include -#include "backend/sha2-backend.h" -#include "backend/templates.h" +#include "./sha2-backend.h" +#include "./templates.h" -static const pgfe_word_t __pgfe_sha512_H0[] = { +static const pgfe_word_t H0[] = { 0xF3BCC908, 0x6A09E667, 0x84CAA73B, 0xBB67AE85, 0xFE94F82B, 0x3C6EF372, 0x5F1D36F1, 0xA54FF53A, 0xADE682D1, 0x510E527F, 0x2B3E6C1F, 0x9B05688C, 0xFB41BD6B, 0x1F83D9AB, 0x137E2179, 0x5BE0CD19, }; -static const pgfe_word_t __pgfe_sha512_224_H0[] = { +static const pgfe_word_t H0_224[] = { 0x19544DA2, 0x8C3D37C8, 0x89DCD4D6, 0x73E19966, 0x32FF9C82, 0x1DFAB7AE, 0x582F9FCF, 0x679DD514, 0x7BD44DA8, 0x0F6D2B69, 0x04C48942, 0x77E36F73, 0x6A1D36C8, 0x3F9D85A8, 0x91D692A1, 0x1112E6AD, }; -static const pgfe_word_t __pgfe_sha512_256_H0[] = { +static const pgfe_word_t H0_256[] = { 0xFC2BF72C, 0x22312194, 0xC84C64C2, 0x9F555FA3, 0x6F53B151, 0x2393B86B, 0x5940EABD, 0x96387719, 0xA88EFFE3, 0x96283EE2, 0x53863992, 0xBE5E1E25, 0x2C85B8AA, 0x2B0199FC, 0x81C52CA2, 0x0EB72DDC, }; @@ -36,47 +36,47 @@ static void pgfe_sha512_generic_init(struct pgfe_sha512_ctx *ctx, const pgfe_wor memcpy(ctx->state, H0, sizeof(ctx->state)); } -__PGFE_FRONTEND_GEN2(sha512) -__PGFE_FRONTEND_DEFAULT_GEN2(sha512, SHA512); +FRONTEND_GEN2(sha512) +FRONTEND_GEN2_DEFAULT(sha512, SHA512); inline void pgfe_sha512_init(struct pgfe_sha512_ctx *ctx) { - pgfe_sha512_generic_init(ctx, __pgfe_sha512_H0); + pgfe_sha512_generic_init(ctx, H0); } inline void pgfe_sha512_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_sha384n512_update(ctx, input, length); + sha384n512_update(ctx, input, length); } inline void pgfe_sha512_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_sha384n512_digest(ctx, output, PGFE_SHA512_DIGEST_SIZE); + sha384n512_digest(ctx, output, PGFE_SHA512_DIGEST_SIZE); } -__PGFE_FRONTEND_GEN2(sha512_224); -__PGFE_FRONTEND_DEFAULT_GEN2(sha512_224, SHA512_224); +FRONTEND_GEN2(sha512_224); +FRONTEND_GEN2_DEFAULT(sha512_224, SHA512_224); inline void pgfe_sha512_224_init(struct pgfe_sha512_ctx *ctx) { - pgfe_sha512_generic_init(ctx, __pgfe_sha512_224_H0); + pgfe_sha512_generic_init(ctx, H0_224); } inline void pgfe_sha512_224_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_sha384n512_update(ctx, input, length); + sha384n512_update(ctx, input, length); } inline void pgfe_sha512_224_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_sha384n512_digest(ctx, output, PGFE_SHA512_224_DIGEST_SIZE); + sha384n512_digest(ctx, output, PGFE_SHA512_224_DIGEST_SIZE); } -__PGFE_FRONTEND_GEN2(sha512_256); -__PGFE_FRONTEND_DEFAULT_GEN2(sha512_256, SHA512_256); +FRONTEND_GEN2(sha512_256); +FRONTEND_GEN2_DEFAULT(sha512_256, SHA512_256); inline void pgfe_sha512_256_init(struct pgfe_sha512_ctx *ctx) { - pgfe_sha512_generic_init(ctx, __pgfe_sha512_256_H0); + pgfe_sha512_generic_init(ctx, H0_256); } inline void pgfe_sha512_256_update(struct pgfe_sha512_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_sha384n512_update(ctx, input, length); + sha384n512_update(ctx, input, length); } inline void pgfe_sha512_256_digest(struct pgfe_sha512_ctx *ctx, pgfe_encode_t output[]) { - __pgfe_sha384n512_digest(ctx, output, PGFE_SHA512_256_DIGEST_SIZE); + sha384n512_digest(ctx, output, PGFE_SHA512_256_DIGEST_SIZE); } diff --git a/src/c/hash/shake.c b/src/hashes/shake.c similarity index 76% rename from src/c/hash/shake.c rename to src/hashes/shake.c index 58df97e..3d1ab12 100644 --- a/src/c/hash/shake.c +++ b/src/hashes/shake.c @@ -2,44 +2,45 @@ libpgfe shake.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "sha3.h" +#include "libpgfe/hashes/sha3.h" #include -#include "backend/templates.h" +#include "./keccak-backend.h" +#include "./templates.h" // RawSHAKE128 -__PGFE_FRONTEND_GEN3(rawshake128, RAWSHAKE128) +FRONTEND_GEN3(rawshake128, RAWSHAKE128) // -- Context-based functions void pgfe_rawshake128_init(struct pgfe_shake128_ctx *ctx) { - __pgfe_keccak_init(ctx, 256); + keccak_init(ctx, 256); ctx->ap = PGFE_RAWSHAKE_APPENDIX; ctx->ap_len = PGFE_RAWSHAKE_APPENDIX_SIZE; } void pgfe_rawshake128_update(struct pgfe_shake128_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); + keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); } void pgfe_rawshake128_digest(struct pgfe_shake128_ctx *ctx, pgfe_encode_t output[], size_t output_bitlength) { ctx->out_length = output_bitlength; - __pgfe_keccak_squeeze_b1600(ctx, output); + keccak_squeeze_b1600(ctx, output); } // SHAKE128 -__PGFE_FRONTEND_GEN3(shake128, SHAKE128) +FRONTEND_GEN3(shake128, SHAKE128) // -- Context-based functions void pgfe_shake128_init(struct pgfe_shake128_ctx *ctx) { - __pgfe_keccak_init(ctx, 256); + keccak_init(ctx, 256); ctx->ap = PGFE_SHAKE_APPENDIX; ctx->ap_len = PGFE_SHAKE_APPENDIX_SIZE; } @@ -54,33 +55,33 @@ inline void pgfe_shake128_digest(struct pgfe_shake128_ctx *ctx, pgfe_encode_t ou // RawSHAKE256 -__PGFE_FRONTEND_GEN3(rawshake256, RAWSHAKE256) +FRONTEND_GEN3(rawshake256, RAWSHAKE256) // -- Context-based functions void pgfe_rawshake256_init(struct pgfe_shake256_ctx *ctx) { - __pgfe_keccak_init(ctx, 512); + keccak_init(ctx, 512); ctx->ap = PGFE_RAWSHAKE_APPENDIX; ctx->ap_len = PGFE_RAWSHAKE_APPENDIX_SIZE; } void pgfe_rawshake256_update(struct pgfe_shake256_ctx *ctx, const pgfe_encode_t input[], size_t length) { - __pgfe_keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); + keccak_absorb_b1600(ctx, input, to_bit((uint64_t)length)); } void pgfe_rawshake256_digest(struct pgfe_shake256_ctx *ctx, pgfe_encode_t output[], size_t output_bitlength) { ctx->out_length = output_bitlength; - __pgfe_keccak_squeeze_b1600(ctx, output); + keccak_squeeze_b1600(ctx, output); } // SHAKE256 -__PGFE_FRONTEND_GEN3(shake256, SHAKE256) +FRONTEND_GEN3(shake256, SHAKE256) // -- Context-based functions void pgfe_shake256_init(struct pgfe_shake256_ctx *ctx) { - __pgfe_keccak_init(ctx, 512); + keccak_init(ctx, 512); ctx->ap = PGFE_SHAKE_APPENDIX; ctx->ap_len = PGFE_SHAKE_APPENDIX_SIZE; } diff --git a/include/backend/templates.h b/src/hashes/templates.h similarity index 96% rename from include/backend/templates.h rename to src/hashes/templates.h index 9aa1de2..88a9a2c 100644 --- a/include/backend/templates.h +++ b/src/hashes/templates.h @@ -4,7 +4,7 @@ Template macros - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_TEMPLATES_H @@ -16,7 +16,7 @@ extern "C" { // NATIVE HASH BACKEND -#define __PGFE_FRONTEND_GEN2(name) \ +#define FRONTEND_GEN2(name) \ void pgfe_##name##_encode(const pgfe_encode_t data[], size_t length, pgfe_encode_t output[]) { \ struct pgfe_##name##_ctx ctx; \ \ @@ -63,12 +63,12 @@ extern "C" { pgfe_##name##_digest(&ctx, output); \ } -#define __PGFE_FRONTEND_DEFAULT_GEN2(name, upper) \ +#define FRONTEND_GEN2_DEFAULT(name, upper) \ inline void pgfe_##name##_encode_default(const pgfe_encode_t data_str[], pgfe_encode_t output[]) { \ pgfe_##name##_encode(data_str, strlen((char *)data_str), output); \ } -#define __PGFE_FRONTEND_GEN3(name, upper) \ +#define FRONTEND_GEN3(name, upper) \ void pgfe_##name##_encode( \ const pgfe_encode_t data[], size_t length, pgfe_encode_t output[], size_t out_bitlength \ ) { \ @@ -122,17 +122,17 @@ extern "C" { pgfe_##name##_encode(data_str, strlen((char *)data_str), output, out_bitlength); \ } -#define __PGFE_SHA_INIT(name) \ +#define SHA_INIT(name) \ void pgfe_##name##_init(struct pgfe_##name##_ctx *ctx) { \ if (!ctx) return; \ \ ctx->len_low = ctx->len_high = 0; \ ctx->index = 0; \ \ - memcpy(ctx->state, __pgfe_##name##_H0, sizeof(__pgfe_##name##_H0)); \ + memcpy(ctx->state, H0, sizeof(H0)); \ } -#define __PGFE_SHA_UPDATE(name, upper) \ +#define SHA_UPDATE(name, upper) \ void pgfe_##name##_update(struct pgfe_##name##_ctx *ctx, const pgfe_encode_t input[], size_t length) { \ if (!ctx || !input) return; \ \ @@ -147,20 +147,20 @@ extern "C" { corrupt_flag = ctx->len_low < tmp_low && !(++ctx->len_high); \ \ if (!corrupt_flag && ctx->index == PGFE_##upper##_BLOCK_SIZE) { \ - __pgfe_##name##_process_block(ctx); \ + name##_process_block(ctx); \ } \ \ inp++; \ } \ } -#define __PGFE_SHA_DIGEST(name, upper) \ +#define SHA_DIGEST(name, upper) \ void pgfe_##name##_digest(struct pgfe_##name##_ctx *ctx, pgfe_encode_t output[]) { \ if (!ctx || !output) return; \ \ size_t i; \ \ - __pgfe_##name##_padding(ctx); \ + name##_padding(ctx); \ \ /* Wipe stored data */ \ memset(ctx->block, 0, sizeof(ctx->block)); \ diff --git a/src/c/hmac/hmac.c b/src/hmac/hmac.c similarity index 85% rename from src/c/hmac/hmac.c rename to src/hmac/hmac.c index 8683fe7..4d14ab9 100644 --- a/src/c/hmac/hmac.c +++ b/src/hmac/hmac.c @@ -2,16 +2,16 @@ libpgfe hmac.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "hmac.h" +#include "libpgfe/hmac/hmac.h" #include -#include "backend/generic-internal.h" +#include "../core/generic-internal.h" -#define __pgfe_hmac_tmpl(name, upper) \ +#define hmac_gen(name, upper) \ void pgfe_hmac_##name##_init(struct pgfe_hmac_##name##_ctx *ctx) { \ pgfe_##name##_init(&ctx->hash_ctx); \ memset(ctx->k_i_pad, I_UNIT, PGFE_##upper##_BLOCK_SIZE); \ @@ -21,8 +21,8 @@ void pgfe_hmac_##name##_set_key( \ struct pgfe_hmac_##name##_ctx *ctx, const pgfe_encode_t key[], size_t key_length \ ) { \ - __pgfe_arrcpy(ctx->k_i_pad, PGFE_##upper##_BLOCK_SIZE, key, key_length); \ - __pgfe_arrcpy(ctx->k_o_pad, PGFE_##upper##_BLOCK_SIZE, key, key_length); \ + arrcpy(ctx->k_i_pad, PGFE_##upper##_BLOCK_SIZE, key, key_length); \ + arrcpy(ctx->k_o_pad, PGFE_##upper##_BLOCK_SIZE, key, key_length); \ for (size_t i = 0; i < PGFE_##upper##_BLOCK_SIZE; i++) { \ ctx->k_i_pad[i] ^= I_UNIT; \ ctx->k_o_pad[i] ^= O_UNIT; \ @@ -58,20 +58,20 @@ pgfe_hmac_##name##_digest(&ctx, output); \ } -__pgfe_hmac_tmpl(md5, MD5); -__pgfe_hmac_tmpl(sha1, SHA1); -__pgfe_hmac_tmpl(sha224, SHA224); -__pgfe_hmac_tmpl(sha256, SHA256); -__pgfe_hmac_tmpl(sha384, SHA384); -__pgfe_hmac_tmpl(sha512, SHA512); -__pgfe_hmac_tmpl(sha512_224, SHA512_224); -__pgfe_hmac_tmpl(sha512_256, SHA512_256); -__pgfe_hmac_tmpl(sha3_224, SHA3_224); -__pgfe_hmac_tmpl(sha3_256, SHA3_256); -__pgfe_hmac_tmpl(sha3_384, SHA3_384); -__pgfe_hmac_tmpl(sha3_512, SHA3_512); +hmac_gen(md5, MD5); +hmac_gen(sha1, SHA1); +hmac_gen(sha224, SHA224); +hmac_gen(sha256, SHA256); +hmac_gen(sha384, SHA384); +hmac_gen(sha512, SHA512); +hmac_gen(sha512_224, SHA512_224); +hmac_gen(sha512_256, SHA512_256); +hmac_gen(sha3_224, SHA3_224); +hmac_gen(sha3_256, SHA3_256); +hmac_gen(sha3_384, SHA3_384); +hmac_gen(sha3_512, SHA3_512); -#define __PGFE_ALG_SELECT_CASE_C(upper, lower) \ +#define __PGFE_ALG_SELECT_CASE(upper, lower) \ case upper: \ pgfe_hmac_##lower(key, key_length, data, length, output); \ break @@ -80,5 +80,5 @@ void pgfe_hmac( enum pgfe_algorithm_choice alg, const pgfe_encode_t key[], size_t key_length, const pgfe_encode_t data[], size_t length, pgfe_encode_t output[] ) { - __PGFE_BATCH_CASES_SP_C(alg, ALG_SELECT) + __PGFE_BATCH_CASES_SP(alg, ALG_SELECT) } \ No newline at end of file diff --git a/src/cpp/hmac/hmac_encoder.cpp b/src/hmac/hmac_encoder.cpp similarity index 90% rename from src/cpp/hmac/hmac_encoder.cpp rename to src/hmac/hmac_encoder.cpp index 3005b2a..5276ecd 100644 --- a/src/cpp/hmac/hmac_encoder.cpp +++ b/src/hmac/hmac_encoder.cpp @@ -2,10 +2,12 @@ libpgfe hmac_encoder.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "hmac_encoder.hpp" +#include "libpgfe/hmac/hmac_encoder.hpp" + +#include "../core/generic-internal.h" #include @@ -54,12 +56,12 @@ HMACEncoder::HMACEncoder(pgfe_algorithm_choice algorithm, SequentialData &sd) { } void HMACEncoder::after_change_alg() { - __PGFE_BATCH_CASES_SP(HMAC_INIT_CTX) - __PGFE_BATCH_CASES_SP(INIT_SIZE) + __PGFE_BATCH_CASES_SP(cur_alg, HMAC_INIT_CTX) + __PGFE_BATCH_CASES_SP(cur_alg, INIT_SIZE) } void HMACEncoder::set_key(const pgfe_encode_t sequence[], size_t length) { - __PGFE_BATCH_CASES_SP(HMAC_SET_KEY) + __PGFE_BATCH_CASES_SP(cur_alg, HMAC_SET_KEY) } void HMACEncoder::set_key(const char cs[]) { @@ -76,7 +78,7 @@ void HMACEncoder::set_key(SequentialData &sd) { } void HMACEncoder::update(const pgfe_encode_t sequence[], size_t length) { - __PGFE_BATCH_CASES_SP(HMAC_ADD_DATA) + __PGFE_BATCH_CASES_SP(cur_alg, HMAC_ADD_DATA) } inline void HMACEncoder::update(const char cs[]) { @@ -92,6 +94,6 @@ inline void HMACEncoder::update(SequentialData &sd) { } const SequentialData *HMACEncoder::get_digest() { - __PGFE_BATCH_CASES_SP(HMAC_DIGEST) + __PGFE_BATCH_CASES_SP(cur_alg, HMAC_DIGEST) return (const SequentialData *)output.get(); } \ No newline at end of file diff --git a/src/cpp/otp/abstract_otp.cpp b/src/otp/abstract_otp.cpp similarity index 86% rename from src/cpp/otp/abstract_otp.cpp rename to src/otp/abstract_otp.cpp index 9916f52..036d298 100644 --- a/src/cpp/otp/abstract_otp.cpp +++ b/src/otp/abstract_otp.cpp @@ -2,15 +2,13 @@ libpgfe abstract_otp.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "backend_cpp/abstract_otp.hpp" +#include "libpgfe/otp/abstract_otp.hpp" #include -#include "base-encoding.h" - using namespace libpgfe; void AbstractOTP::set_secret(const char *cs) { diff --git a/src/c/otp/hotp.c b/src/otp/hotp.c similarity index 79% rename from src/c/otp/hotp.c rename to src/otp/hotp.c index 20ec692..cc89fcd 100644 --- a/src/c/otp/hotp.c +++ b/src/otp/hotp.c @@ -2,17 +2,19 @@ libpgfe hotp.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "hotp.h" +#include "libpgfe/otp/hotp.h" #include -#include "backend/generic-internal.h" -#include "sha1.h" +#include "../core/generic-internal.h" +#include "./otp-generic.h" -#define __PGFE_SET_SIZE_CASE_C(upper, lower) \ +#include "libpgfe/hashes.h" + +#define __PGFE_SET_SIZE_CASE(upper, lower) \ case upper: \ digest_size = PGFE_##upper##_DIGEST_SIZE; \ break @@ -20,9 +22,9 @@ // 10 ^ 0 1 2 3 4 5 6 7 8 9 static const pgfe_otp_t DIGITS_POW[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; -pgfe_otp_t __pgfe_10pow(uint8_t p); +pgfe_otp_t _10pow(uint8_t p); -inline pgfe_otp_t __pgfe_10pow(uint8_t p) { +inline pgfe_otp_t _10pow(uint8_t p) { return DIGITS_POW[p]; } @@ -32,19 +34,19 @@ pgfe_otp_t pgfe_hotp_generic( ) { static const size_t enc_s = 1, counter_len = 8; int digest_size; - __PGFE_BATCH_CASES_C(alg, SET_SIZE) + __PGFE_BATCH_CASES(alg, SET_SIZE) pgfe_encode_t counter_h[counter_len], out_hash[digest_size]; pgfe_otp_t result; memcpy(counter_h, &counter, counter_len); - __pgfe_reverse_elements(counter_h, counter_h + counter_len - 1); + reverse(counter_h, counter_h + counter_len - 1); pgfe_hmac(alg, secret, secret_length, counter_h, counter_len, out_hash); - result = pgfe_dynamically_truncate(out_hash, digest_size); + result = dynamically_truncate(out_hash, digest_size); if (digit_c < PGFE_OTP_DIGIT_MAX_LIMIT) { - result %= __pgfe_10pow(digit_c); + result %= _10pow(digit_c); } return result; diff --git a/src/cpp/otp/hotp.cpp b/src/otp/hotp.cpp similarity index 90% rename from src/cpp/otp/hotp.cpp rename to src/otp/hotp.cpp index 9f59c7c..e3c17f6 100644 --- a/src/cpp/otp/hotp.cpp +++ b/src/otp/hotp.cpp @@ -2,15 +2,17 @@ libpgfe hotp.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "hotp.hpp" +#include "libpgfe/otp/hotp.hpp" #include -#include "generic.hpp" -#include "hotp.h" +#include "libpgfe/generic.hpp" +#include "libpgfe/otp/hotp.h" + +#include "core/generic-internal.h" using namespace libpgfe; @@ -35,7 +37,7 @@ HOTP::HOTP(SequentialData &secret, pgfe_algorithm_choice algorithm) : HOTP(algor } void HOTP::after_change_alg() { - __PGFE_BATCH_CASES(INIT_SIZE) + __PGFE_BATCH_CASES(cur_alg, INIT_SIZE) } void HOTP::set_secret(const pgfe_encode_t *seq, size_t length) { diff --git a/src/otp/otp-generic.c b/src/otp/otp-generic.c new file mode 100644 index 0000000..f8674a2 --- /dev/null +++ b/src/otp/otp-generic.c @@ -0,0 +1,34 @@ +/* + libpgfe + otp-generic.c + + Copyright (c) 2022-2023 Charles Dong +*/ + +#include "./otp-generic.h" + +#define ENS_BIT 8 +#define SIZE 4 + +pgfe_otp_t dynamically_truncate(const pgfe_encode_t hash[], size_t length) { + uint8_t offset = hash[length - 1] & 0xF; + pgfe_otp_t code = 0; + + for (size_t i = 0; i < SIZE; i++) { + code <<= ENS_BIT; + code |= hash[offset + i]; + } + + return code << 1 >> 1; +} + +void otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c) { + char *sp; + + for (sp = cs_out + digit_c - 1; sp >= cs_out; sp--) { + *sp = (char)(otp % 10 + '0'); + otp /= 10; + } + + cs_out[digit_c] = 0; +} diff --git a/include/backend/otp-generic.h b/src/otp/otp-generic.h similarity index 55% rename from include/backend/otp-generic.h rename to src/otp/otp-generic.h index 4b09d0e..c69896f 100644 --- a/include/backend/otp-generic.h +++ b/src/otp/otp-generic.h @@ -4,7 +4,7 @@ Generic implementation of HOTP/TOTP - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_OTP_GENERIC_H @@ -12,8 +12,9 @@ #include -#include "../generic.h" -#include "../hmac.h" +#include "libpgfe/generic.h" +#include "libpgfe/hmac/hmac.h" +#include "libpgfe/otp/types.h" #ifdef __cplusplus extern "C" { @@ -23,14 +24,10 @@ extern "C" { #define PGFE_OTP_DIGIT_MAX_LIMIT 10 -typedef uint32_t pgfe_otp_t; - -typedef uint64_t pgfe_otp_counter_t; - -typedef pgfe_encode_t pgfe_otp_secret_t; - // Performing dynamic truncation -pgfe_otp_t pgfe_dynamically_truncate(const pgfe_encode_t hash[], size_t length); +pgfe_otp_t dynamically_truncate(const pgfe_encode_t hash[], size_t length); + +void otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c); #ifdef __cplusplus } diff --git a/src/otp/totp-internal.c b/src/otp/totp-internal.c new file mode 100644 index 0000000..8140541 --- /dev/null +++ b/src/otp/totp-internal.c @@ -0,0 +1,15 @@ +#include "totp-internal.h" + +pgfe_otp_counter_t calc_periodic_counter( + time_t realtime, pgfe_totp_interval_t interval, time_t initial_time, pgfe_totp_delta_t *delta_out +) { + time_t time_offset = realtime - initial_time; + if (delta_out) { + *delta_out = interval - time_offset % interval; + } + return time_offset / interval; +} + +inline time_t curtime() { + return time(NULL); +} \ No newline at end of file diff --git a/src/otp/totp-internal.h b/src/otp/totp-internal.h new file mode 100644 index 0000000..c2761d6 --- /dev/null +++ b/src/otp/totp-internal.h @@ -0,0 +1,23 @@ +#ifndef LIBPGFE_OTP_TOTP_INTERNAL_H +#define LIBPGFE_OTP_TOTP_INTERNAL_H + +#include + +#include "libpgfe/otp/totp.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Get current UNIX time +time_t curtime(); + +pgfe_otp_counter_t calc_periodic_counter( + time_t realtime, pgfe_totp_interval_t interval, time_t initial_time, pgfe_totp_delta_t *delta_out +); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/c/otp/totp.c b/src/otp/totp.c similarity index 68% rename from src/c/otp/totp.c rename to src/otp/totp.c index 2f59114..3fdfa57 100644 --- a/src/c/otp/totp.c +++ b/src/otp/totp.c @@ -2,31 +2,23 @@ libpgfe totp.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "totp.h" +#include "libpgfe/otp/totp.h" -#include "backend/generic-internal.h" -#include "hotp.h" -#include "sha1.h" -#include "utils.h" +#include "../core/generic-internal.h" +#include "./otp-generic.h" +#include "./totp-internal.h" -pgfe_otp_counter_t __pgfe_calc_periodic_counter( - time_t realtime, pgfe_totp_interval_t interval, time_t initial_time, pgfe_totp_delta_t *delta_out -) { - time_t time_offset = realtime - initial_time; - if (delta_out) { - *delta_out = interval - time_offset % interval; - } - return time_offset / interval; -} +#include "libpgfe/hashes/sha1.h" +#include "libpgfe/otp/hotp.h" pgfe_otp_t pgfe_totp_generic( enum pgfe_algorithm_choice alg, const pgfe_encode_t secret[], size_t secret_length, uint8_t digit_c, pgfe_totp_interval_t interval, time_t initial_time, pgfe_totp_delta_t *delta_out ) { - pgfe_otp_counter_t counter = __pgfe_calc_periodic_counter(pgfe_curtime(), interval, initial_time, delta_out); + pgfe_otp_counter_t counter = calc_periodic_counter(curtime(), interval, initial_time, delta_out); return pgfe_hotp_generic(alg, secret, secret_length, counter, digit_c); } diff --git a/src/cpp/otp/totp.cpp b/src/otp/totp.cpp similarity index 60% rename from src/cpp/otp/totp.cpp rename to src/otp/totp.cpp index 9dab637..3d23e64 100644 --- a/src/cpp/otp/totp.cpp +++ b/src/otp/totp.cpp @@ -2,12 +2,15 @@ libpgfe totp.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "totp.hpp" +#include "libpgfe/otp/totp.hpp" -#include "utils.h" +#include "libpgfe/utils.h" + +#include "./otp-generic.h" +#include "./totp-internal.h" using namespace libpgfe; @@ -38,40 +41,40 @@ TOTP::TOTP(SequentialData &sd, pgfe_totp_interval_t interval, pgfe_algorithm_cho TOTP::~TOTP() {} void TOTP::set_interval(pgfe_totp_interval_t interval) { - this->interval = interval; + this->_interval = interval; } -pgfe_totp_interval_t TOTP::get_interval() const { - return interval; +pgfe_totp_interval_t TOTP::interval() const { + return _interval; } -time_t TOTP::get_initial_time() const { - return initial_time; +time_t TOTP::initial_time() const { + return _initial_time; } -void TOTP::set_initial_time(time_t initial_time) { - this->initial_time = initial_time; +void TOTP::set_initial_time(time_t _initial_time) { + this->_initial_time = _initial_time; } -time_t TOTP::get_update_time() const { - return update_time; +time_t TOTP::update_time() const { + return _update_time; } void TOTP::update_counter() { - update_time = pgfe_curtime(); - set_counter(__pgfe_calc_periodic_counter(update_time, interval, initial_time, &delta)); + _update_time = curtime(); + set_counter(calc_periodic_counter(_update_time, _interval, _initial_time, &delta)); } -time_t TOTP::get_remain_time() const { - return delta - (pgfe_curtime() - update_time); +time_t TOTP::remaining_time() const { + return delta - (curtime() - _update_time); } pgfe_otp_t TOTP::generate(uint8_t digit_count) { - if (get_remain_time() <= 0) update_counter(); + if (remaining_time() <= 0) update_counter(); return this->HOTP::generate(digit_count); } std::string TOTP::generate_str(uint8_t digit_count) { - if (get_remain_time() <= 0) update_counter(); + if (remaining_time() <= 0) update_counter(); return this->HOTP::generate_str(digit_count); } diff --git a/test/baseencodetest.h b/tests/c/baseencodetest.h similarity index 89% rename from test/baseencodetest.h rename to tests/c/baseencodetest.h index 93a0126..c01cbf8 100644 --- a/test/baseencodetest.h +++ b/tests/c/baseencodetest.h @@ -2,14 +2,16 @@ libpgfe baseencodetest.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" #include -#include "../include/base-encoding.h" +#include "libpgfe/base_encoding/base16.h" +#include "libpgfe/base_encoding/base32.h" +#include "libpgfe/base_encoding/base64.h" void base16_encode_test(ARGS) { char cs_out[700]; diff --git a/test/blowfish_test.h b/tests/c/blowfish_test.h similarity index 93% rename from test/blowfish_test.h rename to tests/c/blowfish_test.h index 230f928..7a74c66 100644 --- a/test/blowfish_test.h +++ b/tests/c/blowfish_test.h @@ -2,13 +2,13 @@ libpgfe blowfish_test.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/blowfish.h" -#include "../include/utils.h" +#include "libpgfe/fishes/blowfish.h" +#include "libpgfe/utils.h" void blowfish_encrypt_unit_test(ARGS) { struct pgfe_blowfish_ctx ctx; diff --git a/test/hmactest.h b/tests/c/hmactest.h similarity index 93% rename from test/hmactest.h rename to tests/c/hmactest.h index ebff302..58ad4d8 100644 --- a/test/hmactest.h +++ b/tests/c/hmactest.h @@ -2,14 +2,14 @@ libpgfe hmactest.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/hmac.h" -#include "../include/md5.h" -#include "../include/utils.h" +#include "libpgfe/hmac/hmac.h" +#include "libpgfe/hashes/md5.h" +#include "libpgfe/utils.h" enum pgfe_algorithm_choice get_alg_choice(char option[]) { enum pgfe_algorithm_choice c; diff --git a/test/mdtest.h b/tests/c/mdtest.h similarity index 70% rename from test/mdtest.h rename to tests/c/mdtest.h index e06672a..d71313c 100644 --- a/test/mdtest.h +++ b/tests/c/mdtest.h @@ -2,13 +2,13 @@ libpgfe mdtest.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/md5.h" -#include "../include/utils.h" +#include "libpgfe/hashes/md5.h" +#include "libpgfe/utils.h" void md5test(ARGS) { pgfe_encode_t output[PGFE_MD5_DIGEST_SIZE]; diff --git a/test/otptest.h b/tests/c/otptest.h similarity index 86% rename from test/otptest.h rename to tests/c/otptest.h index 1a00cf1..725bf32 100644 --- a/test/otptest.h +++ b/tests/c/otptest.h @@ -2,14 +2,14 @@ libpgfe otptest.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/hotp.h" -#include "../include/sha2.h" -#include "../include/utils.h" +#include "libpgfe/hashes/sha2.h" +#include "libpgfe/otp/hotp.h" +#include "libpgfe/utils.h" void hotp_hex(ARGS) { pgfe_encode_t key[100]; diff --git a/test/shake_test.h b/tests/c/shake_test.h similarity index 80% rename from test/shake_test.h rename to tests/c/shake_test.h index 8774eb0..b5692af 100644 --- a/test/shake_test.h +++ b/tests/c/shake_test.h @@ -2,16 +2,18 @@ libpgfe shake_test.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" +#define TEST_FILE1_PATH "../tests/test.txt" + #include #include -#include "../include/sha3.h" -#include "../include/utils.h" +#include "libpgfe/hashes/sha3.h" +#include "libpgfe/utils.h" void shake_test(ARGS) { size_t bitsize; @@ -22,9 +24,9 @@ void shake_test(ARGS) { sscanf(argv[3], "%u", &hash_type); sscanf(argv[4], "%lu", &bitsize); - size_t byte_size = to_byte(bitsize); + size_t byte_size = pgfe_to_byte(bitsize); - pgfe_encode_t out[byte_size + bit_rem(bitsize)]; + pgfe_encode_t out[byte_size + pgfe_bit_rem(bitsize)]; void (*func)(const pgfe_encode_t data_str[], pgfe_encode_t output[], size_t out_bitlength); alg[3] = 0; @@ -70,13 +72,13 @@ void shake_test_file(ARGS) { sscanf(argv[3], "%u", &hash_type); sscanf(argv[4], "%lu", &bitsize); - size_t byte_size = to_byte(bitsize); + size_t byte_size = pgfe_to_byte(bitsize); - pgfe_encode_t out[byte_size + bit_rem(bitsize)]; - void (*func)(FILE * fp, pgfe_encode_t output[], size_t out_bitlength); + pgfe_encode_t out[byte_size + pgfe_bit_rem(bitsize)]; + void (*func)(FILE *fp, pgfe_encode_t output[], size_t out_bitlength); func = NULL; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); alg[3] = 0; if (!strcasecmp(alg, "raw")) { diff --git a/test/shatest.h b/tests/c/shatest.h similarity index 88% rename from test/shatest.h rename to tests/c/shatest.h index 74900c6..7e68e05 100644 --- a/test/shatest.h +++ b/tests/c/shatest.h @@ -2,15 +2,15 @@ libpgfe shatest.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/sha1.h" -#include "../include/sha2.h" -#include "../include/sha3.h" -#include "../include/utils.h" +#define TEST_FILE1_PATH "../tests/test.txt" + +#include "libpgfe/hashes.h" +#include "libpgfe/utils.h" void sha1_test(ARGS) { pgfe_encode_t output[PGFE_SHA1_DIGEST_SIZE]; @@ -22,7 +22,7 @@ void sha1_test(ARGS) { void sha1_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA1_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha1_encode_f(fp, output); fclose(fp); @@ -46,7 +46,7 @@ void sha256_test(ARGS) { void sha256_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA256_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha256_encode_f(fp, output); fclose(fp); @@ -70,7 +70,7 @@ void sha512_test(ARGS) { void sha512_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA512_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha512_encode_f(fp, output); fclose(fp); @@ -101,7 +101,7 @@ void sha3_224_test(ARGS) { void sha3_224_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA3_224_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha3_224_encode_f(fp, output); fclose(fp); @@ -118,7 +118,7 @@ void sha3_256_test(ARGS) { void sha3_256_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA3_256_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha3_256_encode_f(fp, output); fclose(fp); @@ -135,7 +135,7 @@ void sha3_384_test(ARGS) { void sha3_384_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA3_384_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha3_384_encode_f(fp, output); fclose(fp); @@ -152,7 +152,7 @@ void sha3_512_test(ARGS) { void sha3_512_file_test(ARGS) { pgfe_encode_t output[PGFE_SHA3_512_DIGEST_SIZE]; - FILE *fp = fopen("../test/test.txt", "r"); + FILE *fp = fopen(TEST_FILE1_PATH, "r"); pgfe_sha3_512_encode_f(fp, output); fclose(fp); diff --git a/test/test.c b/tests/c/test.c similarity index 98% rename from test/test.c rename to tests/c/test.c index 7ce6b55..1aaeff4 100644 --- a/test/test.c +++ b/tests/c/test.c @@ -2,7 +2,7 @@ libpgfe test.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include diff --git a/test/utilstest.h b/tests/c/utilstest.h similarity index 88% rename from test/utilstest.h rename to tests/c/utilstest.h index b476f3f..717e25c 100644 --- a/test/utilstest.h +++ b/tests/c/utilstest.h @@ -2,12 +2,12 @@ libpgfe utilstest.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/utils.h" +#include "libpgfe/utils.h" #include void string2hash(ARGS) { diff --git a/test/cmake/base_tests.cmake b/tests/cmake/base_tests.cmake similarity index 82% rename from test/cmake/base_tests.cmake rename to tests/cmake/base_tests.cmake index 025c20a..5755fd8 100644 --- a/test/cmake/base_tests.cmake +++ b/tests/cmake/base_tests.cmake @@ -22,25 +22,25 @@ foreach(alg IN LISTS Base_algorithms) add_test( NAME ${test_name1} - COMMAND pgfetest "${alg}-encode" ${test_item} + COMMAND ${test_c_exe} "${alg}-encode" ${test_item} ) set_property(TEST ${test_name1} PROPERTY PASS_REGULAR_EXPRESSION ${cipher}) add_test( NAME ${test_name2} - COMMAND pgfetest "${alg}-decode" ${cipher} + COMMAND ${test_c_exe} "${alg}-decode" ${cipher} ) set_property(TEST ${test_name2} PROPERTY PASS_REGULAR_EXPRESSION ${test_item}) add_test( NAME ${test_name1_cpp} - COMMAND pgfetestcpp "${alg}_encode" ${test_item} + COMMAND ${test_cpp_exe} "${alg}_encode" ${test_item} ) set_property(TEST ${test_name1_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${cipher}) add_test( NAME ${test_name2_cpp} - COMMAND pgfetestcpp "${alg}_decode" ${cipher} + COMMAND ${test_cpp_exe} "${alg}_decode" ${cipher} ) set_property(TEST ${test_name2_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${test_item}) @@ -51,5 +51,5 @@ endforeach() add_test( NAME base64_multithread - COMMAND pgfetestcpp base_multithread1 + COMMAND ${test_cpp_exe} base_multithread1 ) diff --git a/test/cmake/blowfish_tests.cmake b/tests/cmake/blowfish_tests.cmake similarity index 80% rename from test/cmake/blowfish_tests.cmake rename to tests/cmake/blowfish_tests.cmake index fd3d68a..a6a5c37 100644 --- a/test/cmake/blowfish_tests.cmake +++ b/tests/cmake/blowfish_tests.cmake @@ -13,13 +13,13 @@ foreach(item IN ZIP_LISTS Blowfish_keys Blowfish_plains Blowfish_crypts) add_test( NAME ${test_name_enc} - COMMAND pgfetest blowfish_enc_unit ${item_0} ${item_1} + COMMAND ${test_c_exe} blowfish_enc_unit ${item_0} ${item_1} ) set_property(TEST ${test_name_enc} PROPERTY PASS_REGULAR_EXPRESSION ${item_2}) add_test( NAME ${test_name_dec} - COMMAND pgfetest blowfish_dec_unit ${item_0} ${item_2} + COMMAND ${test_c_exe} blowfish_dec_unit ${item_0} ${item_2} ) set_property(TEST ${test_name_dec} PROPERTY PASS_REGULAR_EXPRESSION ${item_1}) @@ -35,25 +35,25 @@ foreach(item IN ZIP_LISTS Blowfish_keys_str Blowfish_plains_str Blowfish_crypts_ add_test( NAME ${test_name_enc} - COMMAND pgfetest blowfish_enc_str ${item_0} ${item_1} + COMMAND ${test_c_exe} blowfish_enc_str ${item_0} ${item_1} ) set_property(TEST ${test_name_enc} PROPERTY PASS_REGULAR_EXPRESSION ${item_2}) add_test( NAME ${test_name_dec} - COMMAND pgfetest blowfish_dec_str ${item_0} ${item_2} + COMMAND ${test_c_exe} blowfish_dec_str ${item_0} ${item_2} ) set_property(TEST ${test_name_dec} PROPERTY PASS_REGULAR_EXPRESSION ${item_1}) add_test( NAME ${test_name_enc_cpp} - COMMAND pgfetestcpp blowfish_encrypt ${item_0} ${item_1} + COMMAND ${test_cpp_exe} blowfish_encrypt ${item_0} ${item_1} ) set_property(TEST ${test_name_enc_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${item_2}) add_test( NAME ${test_name_dec_cpp} - COMMAND pgfetestcpp blowfish_decrypt ${item_0} ${item_2} + COMMAND ${test_cpp_exe} blowfish_decrypt ${item_0} ${item_2} ) set_property(TEST ${test_name_dec_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${item_1}) diff --git a/test/cmake/hash_tests.cmake b/tests/cmake/hash_tests.cmake similarity index 94% rename from test/cmake/hash_tests.cmake rename to tests/cmake/hash_tests.cmake index 9b0811f..b093adf 100644 --- a/test/cmake/hash_tests.cmake +++ b/tests/cmake/hash_tests.cmake @@ -47,13 +47,13 @@ foreach(item IN ZIP_LISTS AlgList HashResults_string1) add_test( NAME ${test_name_c} - COMMAND pgfetest ${item_0} ${test_text} + COMMAND ${test_c_exe} ${item_0} ${test_text} ) set_property(TEST ${test_name_c} PROPERTY PASS_REGULAR_EXPRESSION ${item_1}) add_test( NAME ${test_name_cpp} - COMMAND pgfetestcpp hash_encoder ${item_0} ${test_text} + COMMAND ${test_cpp_exe} hash_encoder ${item_0} ${test_text} ) set_property(TEST ${test_name_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${item_1}) endforeach() @@ -63,7 +63,7 @@ foreach(item IN ZIP_LISTS AlgList_file HashResults_file1) add_test( NAME ${test_name_c} - COMMAND pgfetest "${item_0}_file" test/test.txt + COMMAND ${test_c_exe} "${item_0}_file" test/test.txt ) set_property(TEST ${test_name_c} PROPERTY PASS_REGULAR_EXPRESSION ${item_1}) endforeach() \ No newline at end of file diff --git a/test/cmake/hmac_tests.cmake b/tests/cmake/hmac_tests.cmake similarity index 93% rename from test/cmake/hmac_tests.cmake rename to tests/cmake/hmac_tests.cmake index c6a212c..4c907e9 100644 --- a/test/cmake/hmac_tests.cmake +++ b/tests/cmake/hmac_tests.cmake @@ -30,13 +30,13 @@ foreach(alg IN LISTS HMAC_alglist) add_test( NAME ${test_name_c} - COMMAND pgfetest hmac ${alg} ${tup_0} ${tup_1} + COMMAND ${test_c_exe} hmac ${alg} ${tup_0} ${tup_1} ) set_property(TEST ${test_name_c} PROPERTY PASS_REGULAR_EXPRESSION ${result}) add_test( NAME ${test_name_cpp} - COMMAND pgfetestcpp hmac_encoder ${alg} ${tup_0} ${tup_1} + COMMAND ${test_cpp_exe} hmac_encoder ${alg} ${tup_0} ${tup_1} ) set_property(TEST ${test_name_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${result}) diff --git a/test/cmake/otp_tests.cmake b/tests/cmake/otp_tests.cmake similarity index 86% rename from test/cmake/otp_tests.cmake rename to tests/cmake/otp_tests.cmake index a82580a..373160e 100644 --- a/test/cmake/otp_tests.cmake +++ b/tests/cmake/otp_tests.cmake @@ -14,13 +14,13 @@ foreach(item IN ZIP_LISTS HOTP_alglist HOTP_inputs HOTP_counters HOTP_results) add_test( NAME ${test_name_c} - COMMAND pgfetest "hotp_hex_${item_0}" ${item_1} ${item_2} + COMMAND ${test_c_exe} "hotp_hex_${item_0}" ${item_1} ${item_2} ) set_property(TEST ${test_name_c} PROPERTY PASS_REGULAR_EXPRESSION ${item_3}) add_test( NAME ${test_name_cpp} - COMMAND pgfetestcpp hotp_hex ${item_0} ${item_1} ${item_2} + COMMAND ${test_cpp_exe} hotp_hex ${item_0} ${item_1} ${item_2} ) set_property(TEST ${test_name_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${item_3}) endforeach() diff --git a/test/cmake/shake_tests.cmake b/tests/cmake/shake_tests.cmake similarity index 92% rename from test/cmake/shake_tests.cmake rename to tests/cmake/shake_tests.cmake index 6dc8540..5285b2b 100644 --- a/test/cmake/shake_tests.cmake +++ b/tests/cmake/shake_tests.cmake @@ -39,7 +39,7 @@ while(${i} LESS ${SHAKE_lengthSize_len}) add_test( NAME ${test_name} - COMMAND pgfetest shake-test ${alg} ${alg_tup_0} ${alg_tup_1} ${text} + COMMAND ${test_c_exe} shake-test ${alg} ${alg_tup_0} ${alg_tup_1} ${text} ) list(GET SHAKE_results ${k} SHAKE_result) @@ -49,7 +49,7 @@ while(${i} LESS ${SHAKE_lengthSize_len}) add_test( NAME ${test_name_cpp} - COMMAND pgfetestcpp hash_encoder "${alg}${alg_tup_0}" ${alg_tup_1} ${text} + COMMAND ${test_cpp_exe} hash_encoder "${alg}${alg_tup_0}" ${alg_tup_1} ${text} ) set_property(TEST ${test_name_cpp} PROPERTY PASS_REGULAR_EXPRESSION ${SHAKE_result}) @@ -62,7 +62,7 @@ while(${i} LESS ${SHAKE_lengthSize_len}) add_test( NAME ${test_name} - COMMAND pgfetest shake-test_file ${alg} ${alg_tup_0} ${alg_tup_1} + COMMAND ${test_c_exe} shake-test_file ${alg} ${alg_tup_0} ${alg_tup_1} ) list(GET SHAKE_results_file ${kf} SHAKE_result_file) diff --git a/test/cmake/test_meta.cmake b/tests/cmake/test_meta.cmake similarity index 77% rename from test/cmake/test_meta.cmake rename to tests/cmake/test_meta.cmake index cb2bad2..e2c527a 100644 --- a/test/cmake/test_meta.cmake +++ b/tests/cmake/test_meta.cmake @@ -2,4 +2,4 @@ list(APPEND AlgList md5 sha1 sha224 sha256 sha384 sha512 sha512_224 sha512_256 s set(test_text "hello") set(test_text2 "how are you?") -list(APPEND test_texts ${test_text} ${test_text2}) \ No newline at end of file +list(APPEND test_texts ${test_text} ${test_text2}) diff --git a/test/cmake/util_tests.cmake b/tests/cmake/util_tests.cmake similarity index 69% rename from test/cmake/util_tests.cmake rename to tests/cmake/util_tests.cmake index 32ffce5..6fa7d0e 100644 --- a/test/cmake/util_tests.cmake +++ b/tests/cmake/util_tests.cmake @@ -3,40 +3,40 @@ set(test_noisy_hash "0Xaaf4c61ddc+`ZZHHgc5[e8a2dab;:edeKKKKKK0f3b48??//2cdw9aea9 add_test( NAME string_to_hash_normal - COMMAND pgfetest string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" + COMMAND ${test_c_exe} string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" ) add_test( NAME string_to_hash_odd_length - COMMAND pgfetest string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434" + COMMAND ${test_c_exe} string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434" ) add_test( NAME string_to_hash_with_prefix - COMMAND pgfetest string2hash "0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" + COMMAND ${test_c_exe} string2hash "0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" ) add_test( NAME string_to_hash_prefix_odd - COMMAND pgfetest string2hash "0Xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434" + COMMAND ${test_c_exe} string2hash "0Xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434" ) add_test( NAME string_to_hash_with_suffix - COMMAND pgfetest string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434dH" + COMMAND ${test_c_exe} string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434dH" ) add_test( NAME string_to_hash_suffix_odd - COMMAND pgfetest string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434h" + COMMAND ${test_c_exe} string2hash "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434h" ) add_test( NAME string_to_hash_both_odd - COMMAND pgfetest string2hash "0Xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434h" + COMMAND ${test_c_exe} string2hash "0Xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434h" ) add_test( NAME hash_string_clean - COMMAND pgfetest hash_string_clean ${test_noisy_hash} + COMMAND ${test_c_exe} hash_string_clean ${test_noisy_hash} ) add_test( NAME hash_string_clean_inplace - COMMAND pgfetest hash_string_clean_inplace ${test_noisy_hash} + COMMAND ${test_c_exe} hash_string_clean_inplace ${test_noisy_hash} ) set_property(TEST string_to_hash_normal PROPERTY PASS_REGULAR_EXPRESSION ${test_text_sha1}) diff --git a/test/test.h b/tests/core/test.h similarity index 96% rename from test/test.h rename to tests/core/test.h index 1e385a7..57f05eb 100644 --- a/test/test.h +++ b/tests/core/test.h @@ -2,7 +2,7 @@ libpgfe test.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_TEST_TEST_H diff --git a/test/base_multithread.hpp b/tests/cpp/base_multithread.hpp similarity index 93% rename from test/base_multithread.hpp rename to tests/cpp/base_multithread.hpp index 7614e22..ca173d7 100644 --- a/test/base_multithread.hpp +++ b/tests/cpp/base_multithread.hpp @@ -2,9 +2,8 @@ libpgfe base_multithread.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ - #include "test.h" #include @@ -12,8 +11,8 @@ #include #include -#include "../include/base-encoding.h" -#include "../include/base64.hpp" +#include "libpgfe/base_encoding/base64.h" +#include "libpgfe/base_encoding/base64.hpp" using namespace libpgfe; diff --git a/test/base_test.hpp b/tests/cpp/base_test.hpp similarity index 88% rename from test/base_test.hpp rename to tests/cpp/base_test.hpp index b00b3ea..5c8281d 100644 --- a/test/base_test.hpp +++ b/tests/cpp/base_test.hpp @@ -2,14 +2,14 @@ libpgfe base_test.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" -#include "../include/base16.hpp" -#include "../include/base32.hpp" -#include "../include/base64.hpp" +#include "libpgfe/base_encoding/base16.hpp" +#include "libpgfe/base_encoding/base32.hpp" +#include "libpgfe/base_encoding/base64.hpp" using namespace std; using namespace libpgfe; diff --git a/test/blowfish_test.hpp b/tests/cpp/blowfish_test.hpp similarity index 84% rename from test/blowfish_test.hpp rename to tests/cpp/blowfish_test.hpp index 0d95e4c..b89c029 100644 --- a/test/blowfish_test.hpp +++ b/tests/cpp/blowfish_test.hpp @@ -2,7 +2,7 @@ libpgfe blowfish_test.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" @@ -11,8 +11,8 @@ #include #include -#include "../include/blowfish.hpp" -#include "../include/utils.hpp" +#include "libpgfe/fishes/blowfish.hpp" +#include "libpgfe/utils.hpp" void blowfish_encrypt_test(ARGS) { using namespace libpgfe; diff --git a/test/hash_encoder_test.hpp b/tests/cpp/hash_encoder_test.hpp similarity index 77% rename from test/hash_encoder_test.hpp rename to tests/cpp/hash_encoder_test.hpp index a29f46a..a214703 100644 --- a/test/hash_encoder_test.hpp +++ b/tests/cpp/hash_encoder_test.hpp @@ -2,7 +2,7 @@ libpgfe hash_encoder_test.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" @@ -10,9 +10,9 @@ #include #include -#include "../include/hash_encoder.hpp" -#include "../include/utils.h" -#include "../include/utils.hpp" +#include "libpgfe/hashes/hash_encoder.hpp" +#include "libpgfe/utils.h" +#include "libpgfe/utils.hpp" void hash_encoder_test(ARGS) { using namespace std; diff --git a/test/hmactest.hpp b/tests/cpp/hmactest.hpp similarity index 83% rename from test/hmactest.hpp rename to tests/cpp/hmactest.hpp index 00c80db..e2d8294 100644 --- a/test/hmactest.hpp +++ b/tests/cpp/hmactest.hpp @@ -2,16 +2,16 @@ libpgfe hmactest.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" #include -#include "../include/hmac_encoder.hpp" -#include "../include/utils.h" -#include "../include/utils.hpp" +#include "libpgfe/hmac/hmac_encoder.hpp" +#include "libpgfe/utils.h" +#include "libpgfe/utils.hpp" void hmac_encoder_test(ARGS) { using namespace libpgfe; diff --git a/test/otptest.hpp b/tests/cpp/otptest.hpp similarity index 80% rename from test/otptest.hpp rename to tests/cpp/otptest.hpp index 83d42c3..ab245a0 100644 --- a/test/otptest.hpp +++ b/tests/cpp/otptest.hpp @@ -2,7 +2,7 @@ libpgfe otptest.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "test.h" @@ -10,8 +10,8 @@ #include #include -#include "../include/hotp.hpp" -#include "../include/utils.hpp" +#include "libpgfe/otp/hotp.hpp" +#include "libpgfe/utils.hpp" void hotp_hex_test(ARGS) { HOTP hotp{_algstr(argv[2])}; diff --git a/test/test.cpp b/tests/cpp/test.cpp similarity index 97% rename from test/test.cpp rename to tests/cpp/test.cpp index fd9df00..b6c042d 100644 --- a/test/test.cpp +++ b/tests/cpp/test.cpp @@ -2,7 +2,7 @@ libpgfe test.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include diff --git a/test/test.txt b/tests/test.txt similarity index 100% rename from test/test.txt rename to tests/test.txt