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 f0f0360..21aa484 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16.0) +cmake_minimum_required(VERSION 3.26.0) set(CMAKE_C_COMPILER clang) set(CMAKE_CXX_COMPILER clang++) @@ -11,75 +11,22 @@ project(libpgfe ) 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 PUBLIC - ${include_dir} -) - -target_include_directories(pgfe PRIVATE - ${src_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 efd54ad..0000000 --- a/LICENSE +++ /dev/null @@ -1,28 +0,0 @@ -BSD 3-Clause License - -Copyright (c) 2023 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. 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..290e60d 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ include metadata.mak all: @echo 'Building...' - @cmake . -B $(BUILD_DIR) - @cmake --build $(BUILD_DIR) --config Release -j18 -- + @cmake . -B $(BUILD_DIR) -G Ninja + @cmake --build $(BUILD_DIR) --config Release -j20 --target all -- @echo 'Building done' rebuild: clean all @@ -39,4 +39,4 @@ 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} diff --git a/README.md b/README.md index d1750f6..e75f2c3 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Introduction -**libpgfe** (IPA: /[ˌlɪbˈpɪɡfiː](http://ipa-reader.xyz/?text=%CB%8Cl%C9%AAb%CB%88p%C9%AA%C9%A1fi%CB%90)/, 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 and Base 16/32/64. @@ -16,7 +16,7 @@ | 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) | [*Any questions?*](.github/qna.md) diff --git a/cmake/targets/libpgfe.cmake b/cmake/targets/libpgfe.cmake new file mode 100644 index 0000000..b3c2c08 --- /dev/null +++ b/cmake/targets/libpgfe.cmake @@ -0,0 +1,55 @@ +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.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/include/libpgfe/abstract_encoder.hpp b/include/libpgfe/abstract_encoder.hpp index 640be80..766b137 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/abstract_symmetric_crypter.hpp b/include/libpgfe/abstract_symmetric_crypter.hpp index ba9cb0e..7de5e7d 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/algorithm-choice.h b/include/libpgfe/algorithm-choice.h index ae6d2a1..9248f83 100644 --- a/include/libpgfe/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/libpgfe/algorithm_selectable.hpp b/include/libpgfe/algorithm_selectable.hpp index c1e9f0b..ad450c0 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/base-encoding.h b/include/libpgfe/base-encoding.h deleted file mode 100644 index 0f36db7..0000000 --- a/include/libpgfe/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 "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 - -#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/libpgfe/abstract_base_encoding.hpp b/include/libpgfe/base_encoding/abstract_base_encoding.hpp similarity index 97% rename from include/libpgfe/abstract_base_encoding.hpp rename to include/libpgfe/base_encoding/abstract_base_encoding.hpp index 99d6a4b..4aef6a6 100644 --- a/include/libpgfe/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 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/libpgfe/base16.hpp b/include/libpgfe/base_encoding/base16.hpp similarity index 83% rename from include/libpgfe/base16.hpp rename to include/libpgfe/base_encoding/base16.hpp index ba2ae2d..422888b 100644 --- a/include/libpgfe/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 "libpgfe/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/libpgfe/base32.hpp b/include/libpgfe/base_encoding/base32.hpp similarity index 85% rename from include/libpgfe/base32.hpp rename to include/libpgfe/base_encoding/base32.hpp index 4eb7dd6..7e4420c 100644 --- a/include/libpgfe/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 "libpgfe/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/libpgfe/base64.hpp b/include/libpgfe/base_encoding/base64.hpp similarity index 86% rename from include/libpgfe/base64.hpp rename to include/libpgfe/base_encoding/base64.hpp index c87b884..dab3fc1 100644 --- a/include/libpgfe/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 "libpgfe/abstract_base_encoding.hpp" +#include "libpgfe/base_encoding/abstract_base_encoding.hpp" namespace libpgfe { diff --git a/include/libpgfe/exceptions.hpp b/include/libpgfe/exceptions.hpp index da1557d..9c06a09 100644 --- a/include/libpgfe/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/libpgfe/blowfish.h b/include/libpgfe/fishes/blowfish.h similarity index 96% rename from include/libpgfe/blowfish.h rename to include/libpgfe/fishes/blowfish.h index 3aaf488..dd4daaf 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/blowfish.hpp b/include/libpgfe/fishes/blowfish.hpp similarity index 92% rename from include/libpgfe/blowfish.hpp rename to include/libpgfe/fishes/blowfish.hpp index a89410c..3477bd1 100644 --- a/include/libpgfe/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 @@ -14,7 +14,7 @@ #include #include "libpgfe/abstract_symmetric_crypter.hpp" -#include "libpgfe/blowfish.h" +#include "libpgfe/fishes/blowfish.h" namespace libpgfe { diff --git a/include/libpgfe/generic.h b/include/libpgfe/generic.h index 0c11a9b..acacfa0 100644 --- a/include/libpgfe/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 @@ -18,14 +18,14 @@ 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 index b62d641..a85ed74 100644 --- a/include/libpgfe/generic.hpp +++ b/include/libpgfe/generic.hpp @@ -2,7 +2,7 @@ libpgfe generic.hpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_HPP @@ -16,46 +16,7 @@ #include "libpgfe/algorithm-choice.h" #include "libpgfe/exceptions.hpp" -#include "libpgfe/md5.h" -#include "libpgfe/sha1.h" -#include "libpgfe/sha2.h" -#include "libpgfe/sha3.h" - -#define __PGFE_BATCH_SHAKE_CASES(name) \ - switch (cur_alg) { \ - __PGFE_##name##_CASE(RawSHAKE128, rawshake128); \ - __PGFE_##name##_CASE(SHAKE128, shake128); \ - __PGFE_##name##_CASE(RawSHAKE256, rawshake256); \ - __PGFE_##name##_CASE(SHAKE256, shake256); \ - default: \ - break; \ - } - -#define __PGFE_INIT_SIZE_CASE(alg, name) \ - case alg: \ - digsz = PGFE_##alg##_DIGEST_SIZE; \ - blocksz = PGFE_##alg##_BLOCK_SIZE; \ - break - -#define __PGFE_INIT_CTXP_CASE(alg, name) \ - case alg: \ - pgfe_##name##_init((pgfe_##name##_ctx *)ctx); \ - break - -#define __PGFE_INIT_CTX_CASE(alg, name) \ - case alg: \ - pgfe_##name##_init(&ctx); \ - break - -#define __PGFE_SET_CTXP_CASE(alg, name) \ - case alg: \ - ctx = new pgfe_##name##_ctx; \ - break - -#define __PGFE_FREE_CTXP_CASE(alg, name) \ - case alg: \ - delete (pgfe_##name##_ctx *)ctx; \ - break +#include "libpgfe/hashes.h" namespace libpgfe { diff --git a/include/libpgfe/hash.h b/include/libpgfe/hash.h deleted file mode 100644 index aaa6338..0000000 --- a/include/libpgfe/hash.h +++ /dev/null @@ -1,4 +0,0 @@ -#include "libpgfe/md5.h" -#include "libpgfe/sha1.h" -#include "libpgfe/sha2.h" -#include "libpgfe/sha3.h" \ 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/libpgfe/abstract_hash_encoder.hpp b/include/libpgfe/hashes/abstract_hash_encoder.hpp similarity index 94% rename from include/libpgfe/abstract_hash_encoder.hpp rename to include/libpgfe/hashes/abstract_hash_encoder.hpp index 060abaa..cc14795 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/hash_encoder.hpp b/include/libpgfe/hashes/hash_encoder.hpp similarity index 87% rename from include/libpgfe/hash_encoder.hpp rename to include/libpgfe/hashes/hash_encoder.hpp index 821f886..da9c6a4 100644 --- a/include/libpgfe/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 "libpgfe/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/libpgfe/md5.h b/include/libpgfe/hashes/md5.h similarity index 95% rename from include/libpgfe/md5.h rename to include/libpgfe/hashes/md5.h index 9b43ecc..aef4db3 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/sha1.h b/include/libpgfe/hashes/sha1.h similarity index 96% rename from include/libpgfe/sha1.h rename to include/libpgfe/hashes/sha1.h index 3a4ebb9..40870d6 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/sha2.h b/include/libpgfe/hashes/sha2.h similarity index 99% rename from include/libpgfe/sha2.h rename to include/libpgfe/hashes/sha2.h index a184844..e997f54 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/sha3.h b/include/libpgfe/hashes/sha3.h similarity index 88% rename from include/libpgfe/sha3.h rename to include/libpgfe/hashes/sha3.h index 879a5d2..d35468a 100644 --- a/include/libpgfe/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 @@ -25,15 +25,15 @@ 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 + 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 diff --git a/include/libpgfe/hmac.h b/include/libpgfe/hmac/hmac.h similarity index 98% rename from include/libpgfe/hmac.h rename to include/libpgfe/hmac/hmac.h index 7de3a03..3420605 100644 --- a/include/libpgfe/hmac.h +++ b/include/libpgfe/hmac/hmac.h @@ -2,17 +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 "libpgfe/generic.h" -#include "libpgfe/md5.h" -#include "libpgfe/sha1.h" -#include "libpgfe/sha2.h" -#include "libpgfe/sha3.h" +#include "libpgfe/hashes.h" #ifdef __cplusplus extern "C" { diff --git a/include/libpgfe/hmac_encoder.hpp b/include/libpgfe/hmac/hmac_encoder.hpp similarity index 93% rename from include/libpgfe/hmac_encoder.hpp rename to include/libpgfe/hmac/hmac_encoder.hpp index 6915d88..40ceaf1 100644 --- a/include/libpgfe/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 "libpgfe/abstract_hash_encoder.hpp" #include "libpgfe/generic.h" #include "libpgfe/generic.hpp" -#include "libpgfe/hmac.h" +#include "libpgfe/hashes/abstract_hash_encoder.hpp" +#include "libpgfe/hmac/hmac.h" namespace libpgfe { diff --git a/include/libpgfe/abstract_otp.hpp b/include/libpgfe/otp/abstract_otp.hpp similarity index 84% rename from include/libpgfe/abstract_otp.hpp rename to include/libpgfe/otp/abstract_otp.hpp index 38ba3a8..ff8cfce 100644 --- a/include/libpgfe/abstract_otp.hpp +++ b/include/libpgfe/otp/abstract_otp.hpp @@ -2,11 +2,11 @@ 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 @@ -15,8 +15,8 @@ #include "libpgfe/algorithm_selectable.hpp" #include "libpgfe/generic.h" -#include "libpgfe/otp_types.h" #include "libpgfe/sequential_data.hpp" +#include "libpgfe/otp/types.h" namespace libpgfe { diff --git a/include/libpgfe/hotp.h b/include/libpgfe/otp/hotp.h similarity index 88% rename from include/libpgfe/hotp.h rename to include/libpgfe/otp/hotp.h index 2a5a4e8..41513f9 100644 --- a/include/libpgfe/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 "libpgfe/generic.h" -#include "libpgfe/otp_types.h" +#include "libpgfe/otp/types.h" #ifdef __cplusplus extern "C" { diff --git a/include/libpgfe/hotp.hpp b/include/libpgfe/otp/hotp.hpp similarity index 87% rename from include/libpgfe/hotp.hpp rename to include/libpgfe/otp/hotp.hpp index c31781a..b27b1f1 100644 --- a/include/libpgfe/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 "libpgfe/abstract_otp.hpp" -#include "libpgfe/base32.hpp" +#include "libpgfe/base_encoding/base32.hpp" +#include "libpgfe/otp/abstract_otp.hpp" namespace libpgfe { diff --git a/include/libpgfe/totp.h b/include/libpgfe/otp/totp.h similarity index 92% rename from include/libpgfe/totp.h rename to include/libpgfe/otp/totp.h index 20cbc80..826f0e0 100644 --- a/include/libpgfe/totp.h +++ b/include/libpgfe/otp/totp.h @@ -2,17 +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 "libpgfe/generic.h" -#include "libpgfe/otp_types.h" +#include "libpgfe/otp/types.h" #ifdef __cplusplus extern "C" { diff --git a/include/libpgfe/totp.hpp b/include/libpgfe/otp/totp.hpp similarity index 87% rename from include/libpgfe/totp.hpp rename to include/libpgfe/otp/totp.hpp index dddce05..44c4889 100644 --- a/include/libpgfe/totp.hpp +++ b/include/libpgfe/otp/totp.hpp @@ -2,17 +2,17 @@ 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 "libpgfe/hotp.hpp" -#include "libpgfe/totp.h" +#include "libpgfe/otp/hotp.hpp" +#include "libpgfe/otp/totp.h" namespace libpgfe { diff --git a/include/libpgfe/otp_types.h b/include/libpgfe/otp/types.h similarity index 100% rename from include/libpgfe/otp_types.h rename to include/libpgfe/otp/types.h diff --git a/include/libpgfe/sequential_data.hpp b/include/libpgfe/sequential_data.hpp index 7eee5a1..4daa971 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/utils.h b/include/libpgfe/utils.h index de089de..cb405c4 100644 --- a/include/libpgfe/utils.h +++ b/include/libpgfe/utils.h @@ -2,7 +2,7 @@ libpgfe utils.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_UTILS_H diff --git a/include/libpgfe/utils.hpp b/include/libpgfe/utils.hpp index 53ac8b9..29ae848 100644 --- a/include/libpgfe/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 diff --git a/include/libpgfe/version.h b/include/libpgfe/version.h index 7f7bb24..c23f355 100644 --- a/include/libpgfe/version.h +++ b/include/libpgfe/version.h @@ -2,7 +2,7 @@ libpgfe version.h - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_VERSION_H 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 4803743..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 "libpgfe/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 81% rename from src/c/base_encoding/base-encoding-internal.c rename to src/base_encoding/base-encoding-internal.c index 1160a9d..9d21d79 100644 --- a/src/c/base_encoding/base-encoding-internal.c +++ b/src/base_encoding/base-encoding-internal.c @@ -2,7 +2,7 @@ libpgfe base-encoding-internal.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "./base-encoding-internal.h" @@ -10,13 +10,13 @@ #include #include -#include "../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/src/c/base_encoding/base-encoding-internal.h b/src/base_encoding/base-encoding-internal.h similarity index 54% rename from src/c/base_encoding/base-encoding-internal.h rename to src/base_encoding/base-encoding-internal.h index e7b4b79..2f9ff7d 100644 --- a/src/c/base_encoding/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 @@ -18,34 +18,32 @@ 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 64% rename from src/c/base_encoding/base16.c rename to src/base_encoding/base16.c index 528366e..6519621 100644 --- a/src/c/base_encoding/base16.c +++ b/src/base_encoding/base16.c @@ -2,10 +2,10 @@ libpgfe base16.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/base-encoding.h" +#include "libpgfe/base_encoding/base16.h" #include "./base-encoding-internal.h" @@ -13,7 +13,7 @@ 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 4b77fec..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 "libpgfe/base16.hpp" +#include "libpgfe/base_encoding/base16.hpp" -#include "libpgfe/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 67% rename from src/c/base_encoding/base32.c rename to src/base_encoding/base32.c index f31c3fe..5d52b46 100644 --- a/src/c/base_encoding/base32.c +++ b/src/base_encoding/base32.c @@ -2,14 +2,14 @@ libpgfe base32.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/base-encoding.h" +#include "libpgfe/base_encoding/base32.h" #include -#include "../generic-internal.h" +#include "../core/generic-internal.h" #include "./base-encoding-internal.h" // Base 32 alphabet @@ -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 91fd027..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 "libpgfe/base32.hpp" +#include "libpgfe/base_encoding/base32.hpp" -#include "libpgfe/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 75% rename from src/c/base_encoding/base64.c rename to src/base_encoding/base64.c index d67a68d..cd83c32 100644 --- a/src/c/base_encoding/base64.c +++ b/src/base_encoding/base64.c @@ -2,10 +2,10 @@ libpgfe base64.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/base-encoding.h" +#include "libpgfe/base_encoding/base64.h" #include @@ -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 a3b8dc2..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 "libpgfe/base64.hpp" +#include "libpgfe/base_encoding/base64.hpp" -#include "libpgfe/base-encoding.h" +#include "libpgfe/base_encoding/base64.h" using namespace libpgfe; diff --git a/src/c/hash/keccak-backend.h b/src/c/hash/keccak-backend.h deleted file mode 100644 index acae1bb..0000000 --- a/src/c/hash/keccak-backend.h +++ /dev/null @@ -1,35 +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 "libpgfe/sha3.h" - -#include "../generic-internal.h" - -#define __PGFE_KECCAK_b 1600 - -#ifdef __cplusplus -extern "C" { -#endif - -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/src/c/hash/md5-backend.h b/src/c/hash/md5-backend.h deleted file mode 100644 index ab7eb1d..0000000 --- a/src/c/hash/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/src/c/hash/sha224.c b/src/c/hash/sha224.c deleted file mode 100644 index 2713fe3..0000000 --- a/src/c/hash/sha224.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - libpgfe - sha224.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "libpgfe/sha2.h" - -#include - -#include "./sha2-backend.h" -#include "./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 2f616ba..0000000 --- a/src/c/hash/sha256.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - libpgfe - sha256.c - - Copyright (c) 2022 Charles Dong -*/ - -#include "libpgfe/sha2.h" - -#include - -#include "./sha2-backend.h" -#include "./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/cpp/misc/abstract_symmetric_crypter.cpp b/src/core/abstract_symmetric_crypter.cpp similarity index 96% rename from src/cpp/misc/abstract_symmetric_crypter.cpp rename to src/core/abstract_symmetric_crypter.cpp index 6acc2df..5ad2a43 100644 --- a/src/cpp/misc/abstract_symmetric_crypter.cpp +++ b/src/core/abstract_symmetric_crypter.cpp @@ -2,7 +2,7 @@ libpgfe abstract_symmetric_crypter.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "libpgfe/abstract_symmetric_crypter.hpp" diff --git a/src/cpp/algorithm_selectable.cpp b/src/core/algorithm_selectable.cpp similarity index 90% rename from src/cpp/algorithm_selectable.cpp rename to src/core/algorithm_selectable.cpp index e9a16b9..e0783d8 100644 --- a/src/cpp/algorithm_selectable.cpp +++ b/src/core/algorithm_selectable.cpp @@ -2,7 +2,7 @@ libpgfe algorithm_selectable.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "libpgfe/algorithm_selectable.hpp" diff --git a/src/c/generic-internal.c b/src/core/generic-internal.c similarity index 63% rename from src/c/generic-internal.c rename to src/core/generic-internal.c index bf01583..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 "./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/src/c/generic-internal.h b/src/core/generic-internal.h similarity index 63% rename from src/c/generic-internal.h rename to src/core/generic-internal.h index 706a7ec..7d60f02 100644 --- a/src/c/generic-internal.h +++ b/src/core/generic-internal.h @@ -4,7 +4,7 @@ Generic configuration backend - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_INTERNAL_H @@ -21,20 +21,25 @@ extern "C" { #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 +// 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)))) -#define __pgfe_loop for (;;) - -void __pgfe_arrinit(pgfe_encode_t arr[], size_t size); +void 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 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 ch2hex(char ch, pgfe_encode_t *hex); -void __pgfe_reverse_elements(pgfe_encode_t *low, pgfe_encode_t *high); +void reverse(pgfe_encode_t *low, pgfe_encode_t *high); #define __PGFE_BATCH_CASES(target, name) \ switch (target) { \ @@ -86,6 +91,44 @@ void __pgfe_reverse_elements(pgfe_encode_t *low, pgfe_encode_t *high); break; \ } +// CXX compatibility + +#define __PGFE_BATCH_SHAKE_CASES(name) \ + switch (cur_alg) { \ + __PGFE_##name##_CASE(RawSHAKE128, rawshake128); \ + __PGFE_##name##_CASE(SHAKE128, shake128); \ + __PGFE_##name##_CASE(RawSHAKE256, rawshake256); \ + __PGFE_##name##_CASE(SHAKE256, shake256); \ + default: \ + break; \ + } + +#define __PGFE_INIT_SIZE_CASE(alg, name) \ + case alg: \ + digsz = PGFE_##alg##_DIGEST_SIZE; \ + blocksz = PGFE_##alg##_BLOCK_SIZE; \ + break + +#define __PGFE_INIT_CTXP_CASE(alg, name) \ + case alg: \ + pgfe_##name##_init((pgfe_##name##_ctx *)ctx); \ + break + +#define __PGFE_INIT_CTX_CASE(alg, name) \ + case alg: \ + pgfe_##name##_init(&ctx); \ + break + +#define __PGFE_SET_CTXP_CASE(alg, name) \ + case alg: \ + ctx = new pgfe_##name##_ctx; \ + break + +#define __PGFE_FREE_CTXP_CASE(alg, name) \ + case alg: \ + delete (pgfe_##name##_ctx *)ctx; \ + break + #ifdef __cplusplus } #endif diff --git a/src/c/generic.c b/src/core/generic.c similarity index 60% rename from src/c/generic.c rename to src/core/generic.c index 2852ac0..f9e7801 100644 --- a/src/c/generic.c +++ b/src/core/generic.c @@ -2,7 +2,7 @@ libpgfe generic.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "libpgfe/generic.h" diff --git a/src/cpp/generic.cpp b/src/core/generic.cpp similarity index 90% rename from src/cpp/generic.cpp rename to src/core/generic.cpp index 308a565..5a252f9 100644 --- a/src/cpp/generic.cpp +++ b/src/core/generic.cpp @@ -2,7 +2,7 @@ libpgfe generic.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_GENERIC_CPP diff --git a/src/cpp/sequential_data.cpp b/src/core/sequential_data.cpp similarity index 98% rename from src/cpp/sequential_data.cpp rename to src/core/sequential_data.cpp index 881a2fb..69d8c60 100644 --- a/src/cpp/sequential_data.cpp +++ b/src/core/sequential_data.cpp @@ -2,7 +2,7 @@ libpgfe sequential_data.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "libpgfe/sequential_data.hpp" diff --git a/src/c/utils.c b/src/core/utils.c similarity index 95% rename from src/c/utils.c rename to src/core/utils.c index f87d86d..7fa55f6 100644 --- a/src/c/utils.c +++ b/src/core/utils.c @@ -2,11 +2,12 @@ libpgfe utils.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "libpgfe/utils.h" +#include #include #include "./generic-internal.h" @@ -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; } diff --git a/src/cpp/utils.cpp b/src/core/utils.cpp similarity index 95% rename from src/cpp/utils.cpp rename to src/core/utils.cpp index ae04aef..9d24d9e 100644 --- a/src/cpp/utils.cpp +++ b/src/core/utils.cpp @@ -2,7 +2,7 @@ libpgfe utils.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "libpgfe/utils.hpp" 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 5f22b98..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 "libpgfe/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 6a03c4c..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 "libpgfe/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 96% rename from src/cpp/hash/hash_encoder.cpp rename to src/hashes/hash_encoder.cpp index 1cd812b..e4c64a4 100644 --- a/src/cpp/hash/hash_encoder.cpp +++ b/src/hashes/hash_encoder.cpp @@ -2,18 +2,18 @@ libpgfe hash_encoder.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/hash_encoder.hpp" +#include "libpgfe/hashes/hash_encoder.hpp" #include #include -#include "libpgfe/hash.h" +#include "libpgfe/hashes.h" #include "libpgfe/utils.h" -#include "../../c/generic-internal.h" +#include "../core/generic-internal.h" #define __PGFE_CTX_CREATE_CASE(alg, name) \ case alg: \ 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 04b3222..577a6d0 100644 --- a/src/c/hash/keccak-backend.c +++ b/src/hashes/keccak-backend.c @@ -2,7 +2,7 @@ libpgfe keccak-backend.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "./keccak-backend.h" @@ -10,11 +10,11 @@ #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 954e350..444743c 100644 --- a/src/c/hash/md5-backend.c +++ b/src/hashes/md5-backend.c @@ -2,7 +2,7 @@ libpgfe md5-backend.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "./md5-backend.h" @@ -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 82% rename from src/c/hash/md5.c rename to src/hashes/md5.c index 04373fa..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 "libpgfe/md5.h" +#include "libpgfe/hashes/md5.h" #include #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/c/hash/sha-internal.c b/src/hashes/sha-internal.c similarity index 61% rename from src/c/hash/sha-internal.c rename to src/hashes/sha-internal.c index afd4fab..62773f7 100644 --- a/src/c/hash/sha-internal.c +++ b/src/hashes/sha-internal.c @@ -2,7 +2,7 @@ libpgfe sha-internal.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "./sha-internal.h" diff --git a/src/c/hash/sha-internal.h b/src/hashes/sha-internal.h similarity index 78% rename from src/c/hash/sha-internal.h rename to src/hashes/sha-internal.h index 7ea750e..b2177c6 100644 --- a/src/c/hash/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 80% rename from src/c/hash/sha1.c rename to src/hashes/sha1.c index 0a5a1c4..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 "libpgfe/sha1.h" +#include "libpgfe/hashes/sha1.h" #include -#include "../generic-internal.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 89% rename from src/c/hash/sha2-backend.c rename to src/hashes/sha2-backend.c index 9a55cdf..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 "./sha2-backend.h" #include "./templates.h" -#include "libpgfe/sha2.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/src/c/hash/sha2-backend.h b/src/hashes/sha2-backend.h similarity index 52% rename from src/c/hash/sha2-backend.h rename to src/hashes/sha2-backend.h index 7610316..055733b 100644 --- a/src/c/hash/sha2-backend.h +++ b/src/hashes/sha2-backend.h @@ -4,7 +4,7 @@ Generic implementation of SHA2 family - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #ifndef LIBPGFE_SHA2_INTERNAL_H @@ -12,7 +12,7 @@ #include "./sha-internal.h" -#include "libpgfe/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 60% rename from src/c/hash/sha3-224.c rename to src/hashes/sha3-224.c index b900c3c..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 "libpgfe/sha3.h" +#include "libpgfe/hashes/sha3.h" #include +#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 60% rename from src/c/hash/sha3-256.c rename to src/hashes/sha3-256.c index 9155dee..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 "libpgfe/sha3.h" +#include "libpgfe/hashes/sha3.h" #include +#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 60% rename from src/c/hash/sha3-384.c rename to src/hashes/sha3-384.c index 6bbfe3b..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 "libpgfe/sha3.h" +#include "libpgfe/hashes/sha3.h" #include +#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 60% rename from src/c/hash/sha3-512.c rename to src/hashes/sha3-512.c index 9d63e8d..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 "libpgfe/sha3.h" +#include "libpgfe/hashes/sha3.h" #include +#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 64% rename from src/c/hash/sha384.c rename to src/hashes/sha384.c index 675bd00..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 "libpgfe/sha2.h" +#include "libpgfe/hashes/sha2.h" #include #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 63% rename from src/c/hash/sha512.c rename to src/hashes/sha512.c index bb83a17..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 "libpgfe/sha2.h" +#include "libpgfe/hashes/sha2.h" #include #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 77% rename from src/c/hash/shake.c rename to src/hashes/shake.c index 6347ac3..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 "libpgfe/sha3.h" +#include "libpgfe/hashes/sha3.h" #include +#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/src/c/hash/templates.h b/src/hashes/templates.h similarity index 96% rename from src/c/hash/templates.h rename to src/hashes/templates.h index 9aa1de2..88a9a2c 100644 --- a/src/c/hash/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 87% rename from src/c/hmac/hmac.c rename to src/hmac/hmac.c index 46a5ee3..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 "libpgfe/hmac.h" +#include "libpgfe/hmac/hmac.h" #include -#include "../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,18 +58,18 @@ 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(upper, lower) \ case upper: \ diff --git a/src/cpp/hmac/hmac_encoder.cpp b/src/hmac/hmac_encoder.cpp similarity index 96% rename from src/cpp/hmac/hmac_encoder.cpp rename to src/hmac/hmac_encoder.cpp index 9a02509..5276ecd 100644 --- a/src/cpp/hmac/hmac_encoder.cpp +++ b/src/hmac/hmac_encoder.cpp @@ -2,12 +2,12 @@ libpgfe hmac_encoder.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/hmac_encoder.hpp" +#include "libpgfe/hmac/hmac_encoder.hpp" -#include "../../c/generic-internal.h" +#include "../core/generic-internal.h" #include diff --git a/src/cpp/otp/abstract_otp.cpp b/src/otp/abstract_otp.cpp similarity index 85% rename from src/cpp/otp/abstract_otp.cpp rename to src/otp/abstract_otp.cpp index 5c71229..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 "libpgfe/abstract_otp.hpp" +#include "libpgfe/otp/abstract_otp.hpp" #include -#include "libpgfe/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 84% rename from src/c/otp/hotp.c rename to src/otp/hotp.c index 0c115f8..cc89fcd 100644 --- a/src/c/otp/hotp.c +++ b/src/otp/hotp.c @@ -2,17 +2,17 @@ libpgfe hotp.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/hotp.h" +#include "libpgfe/otp/hotp.h" #include -#include "../generic-internal.h" +#include "../core/generic-internal.h" #include "./otp-generic.h" -#include "libpgfe/hash.h" +#include "libpgfe/hashes.h" #define __PGFE_SET_SIZE_CASE(upper, lower) \ case upper: \ @@ -22,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]; } @@ -40,13 +40,13 @@ pgfe_otp_t pgfe_hotp_generic( 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 93% rename from src/cpp/otp/hotp.cpp rename to src/otp/hotp.cpp index 44a535d..e3c17f6 100644 --- a/src/cpp/otp/hotp.cpp +++ b/src/otp/hotp.cpp @@ -2,17 +2,17 @@ libpgfe hotp.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/hotp.hpp" +#include "libpgfe/otp/hotp.hpp" #include #include "libpgfe/generic.hpp" -#include "libpgfe/hotp.h" +#include "libpgfe/otp/hotp.h" -#include "../../c/generic-internal.h" +#include "core/generic-internal.h" using namespace libpgfe; diff --git a/src/c/otp/otp-generic.c b/src/otp/otp-generic.c similarity index 54% rename from src/c/otp/otp-generic.c rename to src/otp/otp-generic.c index 7b7c6fe..a04813a 100644 --- a/src/c/otp/otp-generic.c +++ b/src/otp/otp-generic.c @@ -2,31 +2,31 @@ libpgfe otp-generic.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ #include "./otp-generic.h" -#define __ENS_BIT 8 -#define __SIZE 4 +#define ENS_BIT 8 +#define SIZE 4 -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) { uint8_t offset = hash[length - 1] & 0xF; pgfe_otp_t code = 0; - for (size_t i = 0; i < __SIZE; i++) { - code <<= __ENS_BIT; + for (size_t i = 0; i < SIZE; i++) { + code <<= ENS_BIT; code |= hash[offset + i]; } return code << 1 >> 1; } -inline time_t pgfe_curtime() { +inline time_t curtime() { return time(NULL); } -void pgfe_otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c) { +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--) { diff --git a/src/c/otp/otp-generic.h b/src/otp/otp-generic.h similarity index 60% rename from src/c/otp/otp-generic.h rename to src/otp/otp-generic.h index fc2904f..67a5cbf 100644 --- a/src/c/otp/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 @@ -14,8 +14,8 @@ #include #include "libpgfe/generic.h" -#include "libpgfe/hmac.h" -#include "libpgfe/otp_types.h" +#include "libpgfe/hmac/hmac.h" +#include "libpgfe/otp/types.h" #ifdef __cplusplus extern "C" { @@ -26,12 +26,12 @@ extern "C" { #define PGFE_OTP_DIGIT_MAX_LIMIT 10 // 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); // Get current UNIX time -time_t pgfe_curtime(); +time_t curtime(); -void pgfe_otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c); +void otp_to_string(pgfe_otp_t otp, char *cs_out, size_t digit_c); #ifdef __cplusplus } diff --git a/src/c/otp/totp.c b/src/otp/totp.c similarity index 83% rename from src/c/otp/totp.c rename to src/otp/totp.c index efa6113..7d0644e 100644 --- a/src/c/otp/totp.c +++ b/src/otp/totp.c @@ -2,14 +2,16 @@ libpgfe totp.c - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/totp.h" +#include "libpgfe/otp/totp.h" -#include "../generic-internal.h" -#include "libpgfe/hotp.h" -#include "libpgfe/sha1.h" +#include "../core/generic-internal.h" +#include "./otp-generic.h" + +#include "libpgfe/hashes/sha1.h" +#include "libpgfe/otp/hotp.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 @@ -25,7 +27,7 @@ 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 = __pgfe_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 90% rename from src/cpp/otp/totp.cpp rename to src/otp/totp.cpp index eb9e389..a46c8a7 100644 --- a/src/cpp/otp/totp.cpp +++ b/src/otp/totp.cpp @@ -2,13 +2,14 @@ libpgfe totp.cpp - Copyright (c) 2022 Charles Dong + Copyright (c) 2022-2023 Charles Dong */ -#include "libpgfe/totp.hpp" +#include "libpgfe/otp/totp.hpp" + #include "libpgfe/utils.h" -#include "../../c/otp/otp-generic.h" +#include "./otp-generic.h" using namespace libpgfe; @@ -59,12 +60,12 @@ time_t TOTP::update_time() const { } void TOTP::update_counter() { - _update_time = pgfe_curtime(); + _update_time = curtime(); set_counter(__pgfe_calc_periodic_counter(_update_time, _interval, _initial_time, &delta)); } time_t TOTP::remaining_time() const { - return delta - (pgfe_curtime() - _update_time); + return delta - (curtime() - _update_time); } pgfe_otp_t TOTP::generate(uint8_t 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 c1621e3..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/libpgfe/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 92% rename from test/blowfish_test.h rename to tests/c/blowfish_test.h index c5fb17c..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/libpgfe/blowfish.h" -#include "../include/libpgfe/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 92% rename from test/hmactest.h rename to tests/c/hmactest.h index 8e715b2..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/libpgfe/hmac.h" -#include "../include/libpgfe/md5.h" -#include "../include/libpgfe/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 68% rename from test/mdtest.h rename to tests/c/mdtest.h index 9ab8e72..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/libpgfe/md5.h" -#include "../include/libpgfe/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 85% rename from test/otptest.h rename to tests/c/otptest.h index 7a9cc42..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/libpgfe/hotp.h" -#include "../include/libpgfe/sha2.h" -#include "../include/libpgfe/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 4a75b3c..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/libpgfe/sha3.h" -#include "../include/libpgfe/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 87% rename from test/shatest.h rename to tests/c/shatest.h index af6317a..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/libpgfe/sha1.h" -#include "../include/libpgfe/sha2.h" -#include "../include/libpgfe/sha3.h" -#include "../include/libpgfe/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 993cc82..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/libpgfe/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 72bb713..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/libpgfe/base-encoding.h" -#include "../include/libpgfe/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 206df57..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/libpgfe/base16.hpp" -#include "../include/libpgfe/base32.hpp" -#include "../include/libpgfe/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 83% rename from test/blowfish_test.hpp rename to tests/cpp/blowfish_test.hpp index 976ec7f..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/libpgfe/blowfish.hpp" -#include "../include/libpgfe/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 74% rename from test/hash_encoder_test.hpp rename to tests/cpp/hash_encoder_test.hpp index b33c062..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/libpgfe/hash_encoder.hpp" -#include "../include/libpgfe/utils.h" -#include "../include/libpgfe/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 80% rename from test/hmactest.hpp rename to tests/cpp/hmactest.hpp index 98b8053..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/libpgfe/hmac_encoder.hpp" -#include "../include/libpgfe/utils.h" -#include "../include/libpgfe/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 78% rename from test/otptest.hpp rename to tests/cpp/otptest.hpp index 32263c8..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/libpgfe/hotp.hpp" -#include "../include/libpgfe/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