diff --git a/.github/qna.md b/.github/qna.md index c32e861..cec4f98 100644 --- a/.github/qna.md +++ b/.github/qna.md @@ -9,15 +9,23 @@ Since v0.2.0, *libpgfe* has been "self-sufficient" and does not depend on any th ## Q2: Interested in supporting Big Endian? I will think about it eventually, but not now! Because byte order compatibility will dramatically increase complexity of the code, -and I currently don't have enough time or effort to tackle it. Also, I do not have Big Endian machines or virtual machines, so the additional code would be left untested, which is what I don't want to happen. +and I currently don't have enough time or effort to tackle it. Also, I do not have Big Endian machines or virtual machines, so the additional code would be left untested, which is not what I expect. -## Q3: Why not MSVC? +## Q3: Why not [Assembly Language](https://en.wikipedia.org/wiki/Assembly_language)? + +1. I'm not familiar with Assembly. + +2. Assembly is machine-dependent. + +3. C is fast enough for *libpgfe*. It's no need to use Assembly to do those optimizations. + +## Q4: Why not MSVC? > ***PS:** MSVC = Microsoft Visual C++* -Firstly, this library prioritizes POSIX compatibility, instead of Windows compatibility. +1. This library prioritizes POSIX compatibility, instead of Windows compatibility. -Secondly, *libpgfe* need some necessary features that are not included in *MSVC*. For instance, like the C code below: +2. *libpgfe* needs some necessary features that are not included in *MSVC*. For instance, like the C code below. *MSVC* will refuse to compile that code, because a variable is used as the array's size, while *Clang* and *GCC* are OK with it. ```c int main() { @@ -27,5 +35,3 @@ int main() { return 0; } ``` - -*MSVC* will refuse to compile that code, because of a variable is used as the array's size, while *Clang* and *GCC* are OK with it. diff --git a/CMakeLists.txt b/CMakeLists.txt index 656c329..d02373d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.16.0) set(CMAKE_C_COMPILER clang) set(CMAKE_CXX_COMPILER clang++) -project(libpgfe VERSION 0.4.0 LANGUAGES C CXX) +project(libpgfe VERSION 0.5.0 LANGUAGES C CXX) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 14) @@ -12,42 +12,44 @@ include(CTest) enable_testing() 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_dir}/generic.c - ${src_dir}/generic-internal.c - ${src_dir}/templates.c - ${src_dir}/utils.c - ${src_dir}/sha-internal.c - ${src_dir}/sha2-backend.c - ${src_dir}/keccak-backend.c - ${src_dir}/md5-backend.c - ${src_dir}/sha1.c ${src_dir}/sha224.c ${src_dir}/sha256.c ${src_dir}/sha384.c ${src_dir}/sha512.c - ${src_dir}/sha3-224.c ${src_dir}/sha3-256.c ${src_dir}/sha3-384.c ${src_dir}/sha3-512.c - ${src_dir}/md5.c - ${src_dir}/shake.c - ${src_dir}/hmac.c - ${src_dir}/otp-generic.c - ${src_dir}/hotp.c ${src_dir}/totp.c - ${src_dir}/base-encoding-internal.c - ${src_dir}/base64.c ${src_dir}/base32.c ${src_dir}/base16.c - ${src_dir}/sequential_data.cpp - ${src_dir}/utils.cpp - ${src_dir}/algorithm_selectable.cpp - ${src_dir}/hash_encoder.cpp - ${src_dir}/hmac_encoder.cpp - ${src_dir}/abstract_base_encoding.cpp - ${src_dir}/base16.cpp - ${src_dir}/base32.cpp - ${src_dir}/base64.cpp - ${src_dir}/abstract_otp.cpp - ${src_dir}/hotp.cpp ${src_dir}/totp.cpp) + ${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_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) 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) -add_executable(pgfetestcpp ${test_dir}/test.cpp) target_include_directories(pgfe PRIVATE ${src_dir} @@ -55,8 +57,8 @@ target_include_directories(pgfe PRIVATE ) target_link_libraries(pgfetest pgfe) -# target_link_libraries(totptest pgfe) target_link_libraries(pgfetestcpp pgfe) +# target_link_libraries(totptest pgfe) # target_link_libraries(totptestcpp pgfe) include(${test_dir}/cmake/test_meta.cmake) diff --git a/Makefile b/Makefile index b9d3b3b..1d493f7 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -include meta.mak +include metadata.mak -.PHONY: all rebuild clean test install uninstall +.PHONY: all rebuild clean test install uninstall init-scripts all: @echo 'Building...' @@ -37,3 +37,6 @@ uninstall: @echo 'Removing shared library...' @rm -vrf $(LIB_DIR)/$(TARGET_FILE) @echo done + +init-scripts: + @chmod u+x ./scripts/{comment_header,update_meta,deploy} diff --git a/README.md b/README.md index 964c324..8cc6d5c 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ *libpgfe* currently supports hash encoding (e.g. SHA256, MD5), HMAC encoding, HOTP/TOTP and Base 16/32/64. -| Item | Content | +*libpgfe* is currently under heavy development, more features and optimization will be added in the future. + +| Entry | Info | | :----------- | :------------------ | | 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) | @@ -20,9 +22,7 @@ [*Any questions?*](.github/qna.md) -## Endianness (Byte order) - -> [*What is it anyway??*](https://en.wikipedia.org/wiki/Endianness) +## [Endianness (Byte order)](https://en.wikipedia.org/wiki/Endianness) The implementation philosophy of *libpgfe* assumes that the systems running this library are **Little Endian**, since it is widely used by architectures and OS. Therefore, this library should not run properly on Big Endian systems. diff --git a/include/algorithm_selectable.hpp b/include/algorithm_selectable.hpp index 98f1396..a7240e0 100644 --- a/include/algorithm_selectable.hpp +++ b/include/algorithm_selectable.hpp @@ -28,7 +28,7 @@ class AlgorithmSelectable void select_algorithm(pgfe_algorithm_choice choice); public: - pgfe_algorithm_choice algorithm(); + pgfe_algorithm_choice algorithm() const; }; } // namespace PGFE } // namespace chardon55 diff --git a/include/base-encoding-internal.h b/include/backend/base-encoding-internal.h similarity index 98% rename from include/base-encoding-internal.h rename to include/backend/base-encoding-internal.h index 6a7852c..ccee40d 100644 --- a/include/base-encoding-internal.h +++ b/include/backend/base-encoding-internal.h @@ -10,7 +10,7 @@ #include -#include "generic.h" +#include "../generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/generic-internal.h b/include/backend/generic-internal.h similarity index 99% rename from include/generic-internal.h rename to include/backend/generic-internal.h index f852841..018460a 100644 --- a/include/generic-internal.h +++ b/include/backend/generic-internal.h @@ -8,7 +8,7 @@ #ifndef LIBPGFE_GENERIC_INTERNAL_H #define LIBPGFE_GENERIC_INTERNAL_H -#include "generic.h" +#include "../generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/keccak-backend.h b/include/backend/keccak-backend.h similarity index 100% rename from include/keccak-backend.h rename to include/backend/keccak-backend.h diff --git a/include/md5-backend.h b/include/backend/md5-backend.h similarity index 100% rename from include/md5-backend.h rename to include/backend/md5-backend.h diff --git a/include/otp-generic.h b/include/backend/otp-generic.h similarity index 92% rename from include/otp-generic.h rename to include/backend/otp-generic.h index 75109c3..172deaf 100644 --- a/include/otp-generic.h +++ b/include/backend/otp-generic.h @@ -10,8 +10,8 @@ #include -#include "generic.h" -#include "hmac.h" +#include "../generic.h" +#include "../hmac.h" #ifdef __cplusplus extern "C" { diff --git a/include/sha-internal.h b/include/backend/sha-internal.h similarity index 100% rename from include/sha-internal.h rename to include/backend/sha-internal.h diff --git a/include/sha2-backend.h b/include/backend/sha2-backend.h similarity index 98% rename from include/sha2-backend.h rename to include/backend/sha2-backend.h index 453dc38..426ef63 100644 --- a/include/sha2-backend.h +++ b/include/backend/sha2-backend.h @@ -9,7 +9,8 @@ #define LIBPGFE_SHA2_INTERNAL_H #include "sha-internal.h" -#include "sha2.h" + +#include "../sha2.h" #ifdef __cpluslpus extern "C" { diff --git a/src/templates.c b/include/backend/templates.h similarity index 99% rename from src/templates.c rename to include/backend/templates.h index e5068d2..a440108 100644 --- a/src/templates.c +++ b/include/backend/templates.h @@ -1,12 +1,12 @@ /* libpgfe - templates.c + templates.h Copyright (c) 2022 Charles Dong */ -#ifndef LIBPGFE_TEMPLATES_C -#define LIBPGFE_TEMPLATES_C +#ifndef LIBPGFE_TEMPLATES_H +#define LIBPGFE_TEMPLATES_H #ifdef __cplusplus extern "C" { diff --git a/include/abstract_base_encoding.hpp b/include/backend_cpp/abstract_base_encoding.hpp similarity index 87% rename from include/abstract_base_encoding.hpp rename to include/backend_cpp/abstract_base_encoding.hpp index 42e705b..9f36b3d 100644 --- a/include/abstract_base_encoding.hpp +++ b/include/backend_cpp/abstract_base_encoding.hpp @@ -14,8 +14,8 @@ #include #include -#include "generic.h" -#include "sequential_data.hpp" +#include "../generic.h" +#include "../sequential_data.hpp" namespace chardon55 { namespace PGFE { @@ -63,10 +63,10 @@ class AbstractBaseEncoding SequentialData decode(const char *); SequentialData decode(std::string &); - base_short_size_t unit_size(); - base_short_size_t chunk_size(); - base_short_size_t bit_size(); - base_short_size_t alphabet_size(); + base_short_size_t unit_size() const; + base_short_size_t chunk_size() const; + base_short_size_t bit_size() const; + base_short_size_t alphabet_size() const; }; } // namespace PGFE diff --git a/include/abstract_encoder.hpp b/include/backend_cpp/abstract_encoder.hpp similarity index 87% rename from include/abstract_encoder.hpp rename to include/backend_cpp/abstract_encoder.hpp index 2084fa2..80b1334 100644 --- a/include/abstract_encoder.hpp +++ b/include/backend_cpp/abstract_encoder.hpp @@ -14,9 +14,9 @@ #include #include -#include "generic.h" -#include "sequential_data.hpp" -#include "utils.hpp" +#include "../generic.h" +#include "../sequential_data.hpp" +#include "../utils.hpp" namespace chardon55 { namespace PGFE { @@ -39,7 +39,7 @@ class AbstractEncoder update(sd.to_pgfe_seq(sz), sz); } - virtual SequentialData *get_digest() { + virtual const SequentialData *get_digest() { return nullptr; } }; // namespace AbstractEncoder diff --git a/include/abstract_hash_encoder.hpp b/include/backend_cpp/abstract_hash_encoder.hpp similarity index 62% rename from include/abstract_hash_encoder.hpp rename to include/backend_cpp/abstract_hash_encoder.hpp index 6728a0b..83bcfbf 100644 --- a/include/abstract_hash_encoder.hpp +++ b/include/backend_cpp/abstract_hash_encoder.hpp @@ -14,16 +14,28 @@ #include #include "abstract_encoder.hpp" -#include "algorithm-choice.h" -#include "algorithm_selectable.hpp" -#include "generic.h" -#include "generic.hpp" + +#include "../algorithm-choice.h" +#include "../algorithm_selectable.hpp" +#include "../generic.h" +#include "../generic.hpp" namespace chardon55 { namespace PGFE { class AbstractHashEncoder : public AbstractEncoder, public AlgorithmSelectable { +protected: + size_t digsz, blocksz; + +public: + size_t digest_size() const { + return digsz; + } + + size_t block_size() const { + return blocksz; + } }; } // namespace PGFE diff --git a/include/abstract_otp.hpp b/include/backend_cpp/abstract_otp.hpp similarity index 71% rename from include/abstract_otp.hpp rename to include/backend_cpp/abstract_otp.hpp index 05b6152..1720b87 100644 --- a/include/abstract_otp.hpp +++ b/include/backend_cpp/abstract_otp.hpp @@ -13,10 +13,10 @@ #include -#include "algorithm_selectable.hpp" -#include "generic.h" -#include "otp-generic.h" -#include "sequential_data.hpp" +#include "../algorithm_selectable.hpp" +#include "../backend/otp-generic.h" +#include "../generic.h" +#include "../sequential_data.hpp" namespace chardon55 { namespace PGFE { @@ -31,11 +31,11 @@ class AbstractOTP : public AlgorithmSelectable virtual void set_counter(pgfe_otp_counter_t){}; - virtual pgfe_otp_t generate(uint8_t digit_count = 6) { + virtual pgfe_otp_t generate(uint8_t digit_count = 6) const { return 0; } - virtual std::string generate_str(uint8_t digit_count = 6); + virtual std::string generate_str(uint8_t digit_count = 6) const; }; } // namespace PGFE diff --git a/include/base16.hpp b/include/base16.hpp index 915bbca..4038085 100644 --- a/include/base16.hpp +++ b/include/base16.hpp @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "abstract_base_encoding.hpp" +#include "backend_cpp/abstract_base_encoding.hpp" namespace chardon55 { namespace PGFE { diff --git a/include/base32.hpp b/include/base32.hpp index f8e06d8..c55ce70 100644 --- a/include/base32.hpp +++ b/include/base32.hpp @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "abstract_base_encoding.hpp" +#include "backend_cpp/abstract_base_encoding.hpp" namespace chardon55 { namespace PGFE { @@ -28,7 +28,7 @@ class Base32 : public AbstractBaseEncoding public: Base32(bool hex_mode = false); - bool is_hex_mode() { + bool is_hex_mode() const { return hexm; } }; diff --git a/include/base64.hpp b/include/base64.hpp index dbf7ae6..6f4b2cd 100644 --- a/include/base64.hpp +++ b/include/base64.hpp @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "abstract_base_encoding.hpp" +#include "backend_cpp/abstract_base_encoding.hpp" namespace chardon55 { namespace PGFE { @@ -28,7 +28,7 @@ class Base64 : public AbstractBaseEncoding public: Base64(bool url_safe_mode = false); - bool is_url_safe_mode() { + bool is_url_safe_mode() const { return _url_safe_mode; } }; diff --git a/include/generic.hpp b/include/generic.hpp index 09fbaa4..c3d5a5f 100644 --- a/include/generic.hpp +++ b/include/generic.hpp @@ -102,7 +102,7 @@ namespace chardon55 { namespace PGFE { -static std::unordered_map pgfe_option_map = { +static const std::unordered_map pgfe_option_map = { {"sha1", SHA1 }, {"SHA1", SHA1 }, {"sha224", SHA224 }, @@ -137,7 +137,7 @@ static std::unordered_map pgfe_option_map = {"MD5", MD5 }, }; -static std::unordered_map pgfe_digest_length = { +static const std::unordered_map pgfe_digest_length = { {SHA1, PGFE_SHA1_DIGEST_SIZE }, {SHA224, PGFE_SHA224_DIGEST_SIZE }, {SHA256, PGFE_SHA256_DIGEST_SIZE }, @@ -156,7 +156,7 @@ static std::unordered_map pgfe_digest_length = { {RawSHAKE256, PGFE_RawSHAKE256_DIGEST_SIZE}, }; -static std::unordered_map pgfe_block_length = { +static const std::unordered_map pgfe_block_length = { {SHA1, PGFE_SHA1_BLOCK_SIZE }, {SHA224, PGFE_SHA224_BLOCK_SIZE }, {SHA256, PGFE_SHA256_BLOCK_SIZE }, @@ -175,6 +175,11 @@ static std::unordered_map pgfe_block_length = { {RawSHAKE256, PGFE_RawSHAKE256_BLOCK_SIZE}, }; +// String to algorithm choice type + +pgfe_algorithm_choice string_to_algorithm_choice(const char *cs); +pgfe_algorithm_choice string_to_algorithm_choice(std::string cpp_s); + } // namespace PGFE } // namespace chardon55 diff --git a/include/hash_encoder.hpp b/include/hash_encoder.hpp index 7029fee..04b657e 100644 --- a/include/hash_encoder.hpp +++ b/include/hash_encoder.hpp @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "abstract_hash_encoder.hpp" +#include "backend_cpp/abstract_hash_encoder.hpp" #include "generic.hpp" @@ -25,12 +25,12 @@ class HashEncoder : public AbstractHashEncoder private: void *ctx = nullptr; - size_t digsz, blocksz; - pgfe_encode_t *seq = nullptr; + SequentialData *out = nullptr; void load_algorithm(); void destroy_context(); + void destroy_output(); void init(); @@ -49,15 +49,7 @@ class HashEncoder : public AbstractHashEncoder void update(std::string &cpp_s); void update(SequentialData &sd); - SequentialData *get_digest(uint64_t bitlength = 0); - - size_t digest_size() { - return digsz; - } - - size_t block_size() { - return blocksz; - } + const SequentialData *get_digest(uint64_t bitlength = 0); }; } // namespace PGFE diff --git a/include/hmac.h b/include/hmac.h index ce5cd4f..e5e9f17 100644 --- a/include/hmac.h +++ b/include/hmac.h @@ -8,7 +8,7 @@ #ifndef LIBPGFE_HMAC_H #define LIBPGFE_HMAC_H -#include "generic-internal.h" +#include "backend/generic-internal.h" #include "generic.h" #include "md5.h" #include "sha1.h" diff --git a/include/hmac_encoder.hpp b/include/hmac_encoder.hpp index c8d4c44..128023c 100644 --- a/include/hmac_encoder.hpp +++ b/include/hmac_encoder.hpp @@ -13,7 +13,7 @@ #include -#include "abstract_hash_encoder.hpp" +#include "backend_cpp/abstract_hash_encoder.hpp" #include "generic.h" #include "generic.hpp" #include "hmac.h" @@ -40,8 +40,6 @@ class HMACEncoder : public AbstractHashEncoder pgfe_hmac_sha3_512_ctx sha3_512; } ctx; - size_t digsz, blocksz; - SequentialData *output; void destroy_output(); @@ -66,15 +64,7 @@ class HMACEncoder : public AbstractHashEncoder void update(std::string &cpp_s); void update(SequentialData &sd); - SequentialData *get_digest(); - - size_t block_size() { - return blocksz; - } - - size_t digest_size() { - return digsz; - } + const SequentialData *get_digest(); }; } // namespace PGFE diff --git a/include/hotp.h b/include/hotp.h index 8546ac1..717dcc4 100644 --- a/include/hotp.h +++ b/include/hotp.h @@ -8,8 +8,8 @@ #ifndef LIBPGFE_HOTP_H #define LIBPGFE_HOTP_H -#include "generic-internal.h" -#include "otp-generic.h" +#include "backend/generic-internal.h" +#include "backend/otp-generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/hotp.hpp b/include/hotp.hpp index 37ee89b..85eb81b 100644 --- a/include/hotp.hpp +++ b/include/hotp.hpp @@ -11,7 +11,7 @@ #error libpgfe error: C++ headers are not compatible with C source #endif -#include "abstract_otp.hpp" +#include "backend_cpp/abstract_otp.hpp" #include "base32.hpp" namespace chardon55 { @@ -50,8 +50,8 @@ class HOTP : public AbstractOTP void set_counter(pgfe_otp_counter_t); - pgfe_otp_t generate(uint8_t digit_count = 6); - std::string generate_str(uint8_t digit_count = 6); + pgfe_otp_t generate(uint8_t digit_count = 6) const; + std::string generate_str(uint8_t digit_count = 6) const; }; } // namespace PGFE diff --git a/include/md5.h b/include/md5.h index a69cafa..68ac42f 100644 --- a/include/md5.h +++ b/include/md5.h @@ -10,7 +10,7 @@ #include -#include "generic-internal.h" +#include "backend/generic-internal.h" #include "generic.h" #ifdef __cplusplus diff --git a/include/meta.h b/include/meta.h deleted file mode 100644 index 9304ec4..0000000 --- a/include/meta.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - libpgfe - meta.h - - Copyright (c) 2022 Charles Dong -*/ - -#ifndef LIBPGFE_META_H -#define LIBPGFE_META_H - -#define LIBPGFE_NAME "libpgfe" - -#define LIBPGFE_MAJOR_VERSION 0 -#define LIBPGFE_MINOR_VERSION 4 -#define LIBPGFE_REVISION 0 - -#define LIBPGFE_VERSION "0.4.0" -#endif diff --git a/include/sequential_data.hpp b/include/sequential_data.hpp index a81484d..90829f5 100644 --- a/include/sequential_data.hpp +++ b/include/sequential_data.hpp @@ -40,27 +40,30 @@ class SequentialData SequentialData(const pgfe_encode_t *, size_t); SequentialData(const char *); SequentialData(std::string &); + SequentialData(const SequentialData *); + SequentialData(SequentialData *, bool delete_current); ~SequentialData(); - const char *to_cs(); - std::string to_str(); + const char *to_cs() const; + std::string to_str() const; const char *to_hex_cs(); - std::string to_hex_str(); + std::string to_hex_str() const; - const pgfe_encode_t *to_pgfe_seq(); - const pgfe_encode_t *to_pgfe_seq(size_t &); + const pgfe_encode_t *to_pgfe_seq() const; + const pgfe_encode_t *to_pgfe_seq(size_t &) const; - size_t length(); + size_t length() const; - bool is_str(); + bool is_str() const; void set_is_str(bool); - bool is_apparent_str(); + bool is_apparent_str() const; - SequentialData *truncate(size_t start, size_t length, bool inplace = false); + SequentialData *truncate(size_t start, size_t length, bool inplace); + SequentialData *truncate(size_t start, size_t length) const; - SequentialData *copy(); + SequentialData *copy() const; friend std::ostream &operator<<(std::ostream &os, const SequentialData &sd) { if (sd._is_str) { diff --git a/include/sha1.h b/include/sha1.h index 7a7a640..1e93ce8 100644 --- a/include/sha1.h +++ b/include/sha1.h @@ -10,7 +10,7 @@ #include -#include "generic-internal.h" +#include "backend/generic-internal.h" #ifdef __cplusplus extern "C" { diff --git a/include/sha2.h b/include/sha2.h index bcf245f..cc5a242 100644 --- a/include/sha2.h +++ b/include/sha2.h @@ -10,7 +10,7 @@ #include -#include "generic-internal.h" +#include "backend/generic-internal.h" #ifdef __cplusplus extern "C" { diff --git a/include/sha3.h b/include/sha3.h index 7c97271..23d24c7 100644 --- a/include/sha3.h +++ b/include/sha3.h @@ -10,8 +10,8 @@ #include +#include "backend/keccak-backend.h" #include "generic.h" -#include "keccak-backend.h" #ifdef __cplusplus extern "C" { diff --git a/include/totp.h b/include/totp.h index ab88a03..ae469dd 100644 --- a/include/totp.h +++ b/include/totp.h @@ -9,7 +9,7 @@ #ifndef LIBPGFE_TOTP_H #define LIBPGFE_TOTP_H -#include "otp-generic.h" +#include "backend/otp-generic.h" #ifdef __cplusplus extern "C" { diff --git a/include/totp.hpp b/include/totp.hpp index f958dc5..949d7a4 100644 --- a/include/totp.hpp +++ b/include/totp.hpp @@ -35,15 +35,15 @@ class TOTP : public HOTP void update_counter(); - pgfe_totp_interval_t get_interval(); + pgfe_totp_interval_t get_interval() const; void set_interval(pgfe_totp_interval_t); - pgfe_time_t get_initial_time(); + pgfe_time_t get_initial_time() const; void set_initial_time(pgfe_time_t); - pgfe_time_t get_update_time(); + pgfe_time_t get_update_time() const; - pgfe_time_t get_remain_time(); + pgfe_time_t get_remain_time() const; }; } // namespace PGFE diff --git a/include/utils.h b/include/utils.h index d461696..65dadf1 100644 --- a/include/utils.h +++ b/include/utils.h @@ -8,8 +8,8 @@ #ifndef LIBPGFE_UTILS_H #define LIBPGFE_UTILS_H +#include "backend/otp-generic.h" #include "generic.h" -#include "otp-generic.h" #include diff --git a/include/utils.hpp b/include/utils.hpp index aa54e55..5019061 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -28,9 +28,6 @@ SequentialData *to_sequential_data(const pgfe_encode_t *pgfe_c_seq, size_t); SequentialData *to_sequential_data(const char *cs); SequentialData *to_sequential_data(std::string &); -pgfe_algorithm_choice string_to_algorithm_choice(const char *cs); -pgfe_algorithm_choice string_to_algorithm_choice(std::string cpp_s); - namespace sequential_data { SequentialData *from_hex_string(const char *hex_cs); diff --git a/include/version.h b/include/version.h new file mode 100644 index 0000000..1d99022 --- /dev/null +++ b/include/version.h @@ -0,0 +1,18 @@ +/* + libpgfe + version.h + + Copyright (c) 2022 Charles Dong +*/ + +#ifndef LIBPGFE_VERSION_H +#define LIBPGFE_VERSION_H + +#define LIBPGFE_MAJOR_VERSION 0 +#define LIBPGFE_MINOR_VERSION 5 +#define LIBPGFE_REVISION 0 +#define LIBPGFE_VARIANT "" + +#define LIBPGFE_VERSION "0.5.0" + +#endif diff --git a/meta.mak b/metadata.mak similarity index 94% rename from meta.mak rename to metadata.mak index 802b863..0aa05e0 100644 --- a/meta.mak +++ b/metadata.mak @@ -1,5 +1,5 @@ PROJECT := libpgfe -VERSION := 0.4.0 +VERSION := 0.5.0 BUILD_DIR := build diff --git a/project.json b/project.json new file mode 100644 index 0000000..fca74ae --- /dev/null +++ b/project.json @@ -0,0 +1,4 @@ +{ + "name": "libpgfe", + "version": "0.5.0" +} \ No newline at end of file diff --git a/scripts/comment_header.py b/scripts/comment_header old mode 100644 new mode 100755 similarity index 77% rename from scripts/comment_header.py rename to scripts/comment_header index 19e658d..c0340d4 --- a/scripts/comment_header.py +++ b/scripts/comment_header @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + from pathlib import Path import sys @@ -24,7 +26,7 @@ def add_comment_header(file: Path): print(f"Updated {file}") -def iter_dir(path_item: Path): +def iter_dir(path_item: Path, inside_subdir=False): if not path_item.exists(): return @@ -32,10 +34,10 @@ def iter_dir(path_item: Path): add_comment_header(path_item) else: for item in path_item.iterdir(): - if item.is_dir() and item.name in INCLUDE_SUBDIR: - iter_dir(item) + if item.is_dir() and (item.name in INCLUDE_SUBDIR or inside_subdir): + iter_dir(item, inside_subdir=True) continue - + if item.name.split('.')[-1] in EXTS: add_comment_header(item) @@ -44,5 +46,6 @@ def main(args): if len(args) > 0: iter_dir(Path(args[0])) + if __name__ == "__main__": - main(sys.argv[1:]) \ No newline at end of file + main(sys.argv[1:]) diff --git a/scripts/deploy.py b/scripts/deploy old mode 100644 new mode 100755 similarity index 79% rename from scripts/deploy.py rename to scripts/deploy index 0dba1ac..60f1097 --- a/scripts/deploy.py +++ b/scripts/deploy @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + from argparse import ArgumentParser import os from glob import glob @@ -8,20 +10,21 @@ SHARED_LIB = "libpgfe.so" LIB_DIR = "./build" + def main(args): if args.uninstall: for ds in args.header_dir: _dir = Path(ds) / HEADER_ROOT if not _dir.exists(): continue - + shutil.rmtree(_dir) - + for ss in args.lib_dir: _dir = Path(ss) if not _dir.exists(): continue - + os.remove(_dir / SHARED_LIB) else: @@ -29,23 +32,26 @@ def main(args): _dir = Path(ds) / HEADER_ROOT if not _dir.exists(): _dir.mkdir(parents=True) - + for f in glob(r'./*.h'): shutil.copy(f, _dir) - + for f in glob(r'./*.hpp'): shutil.copy(f, _dir) - + for ss in args.lib_dir: _dir = Path(ss) if not _dir.exists(): _dir.mkdir(parents=True) - + shutil.copy(str(Path(LIB_DIR, SHARED_LIB)), _dir) + if __name__ == '__main__': ap = ArgumentParser() - ap.add_argument("-h", "--header-dir", required=True, nargs='*', action='extend', default=[]) - ap.add_argument("-l", "--lib-dir", required=True, nargs='*', action='extend', default=[]) + ap.add_argument("-h", "--header-dir", required=True, + nargs='*', action='extend', default=[]) + ap.add_argument("-l", "--lib-dir", required=True, + nargs='*', action='extend', default=[]) ap.add_argument("-u", "--uninstall", action='store_true') - main(ap.parse_args()) \ No newline at end of file + main(ap.parse_args()) diff --git a/scripts/metadata.mak.in b/scripts/metadata.mak.in new file mode 100644 index 0000000..8a9c71c --- /dev/null +++ b/scripts/metadata.mak.in @@ -0,0 +1,15 @@ +PROJECT := {name} +VERSION := {version} + +BUILD_DIR := build + +INCLUDE_DIR := include +SRC_DIR := src + +HEADER_DIR := /usr/local/include/libpgfe +LIB_DIR := /usr/lib +TARGET_DIR := $(BUILD_DIR) +TARGET_FILE := $(PROJECT).so + +TARGET := $(TARGET_DIR)/$(TARGET_FILE) +TEST_TARGET := $(TARGET_DIR)/$(TEST_BIN) \ No newline at end of file diff --git a/scripts/update_meta b/scripts/update_meta new file mode 100755 index 0000000..48c92d1 --- /dev/null +++ b/scripts/update_meta @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import json + + +FILES = { + "scripts/metadata.mak.in": "metadata.mak", + "scripts/version.h.template": "include/version.h", +} + + +def main(): + p = {} + with open("project.json") as fp: + p = json.load(fp) + + name = p['name'] + version = p['version'] + version_list = [int(i) for i in version.split('-')[0].split('.')] + + info = { + "name": name, + "version": version, + "major_version": version_list[0], + "minor_version": version_list[1], + "revision": version_list[2], + "variant": '-'.join(version.split('-')[1:]), + } + + for src, dest in FILES.items(): + with open(src, 'r') as s_fp: + with open(dest, 'w') as d_fp: + d_fp.write(s_fp.read().format(**info)) + + +if __name__ == '__main__': + main() diff --git a/scripts/update_meta.py b/scripts/update_meta.py deleted file mode 100644 index 68fff5f..0000000 --- a/scripts/update_meta.py +++ /dev/null @@ -1,43 +0,0 @@ -import sys - -import json - - -def main(args): - p = {} - with open(args[0]) as fp: - p = json.load(fp) - - name = p['name'] - version = p['version'] - version_list = [int(i) for i in version.split('.')] - - with open('meta.mak', 'w') as f: - f.write(f'''\ -PROJECT := {name} -VERSION := {version} - -BUILD_DIR := build -TARGET_DIR := $(BUILD_DIR)/bin -''') - - nameuc = name.upper() - - with open('meta.h', 'w') as f: - f.write(f'''\ -#ifndef {nameuc}_META_H -#define {nameuc}_META_H - -#define {nameuc}_NAME "{name}" - -#define {nameuc}_MAJOR_VERSION {version_list[0]} -#define {nameuc}_MINOR_VERSION {version_list[1]} -#define {nameuc}_REVISION {version_list[2]} - -#define {nameuc}_VERSION "{version}" -#endif - ''') - - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/scripts/version.h.template b/scripts/version.h.template new file mode 100644 index 0000000..f53ec14 --- /dev/null +++ b/scripts/version.h.template @@ -0,0 +1,11 @@ +#ifndef LIBPGFE_VERSION_H +#define LIBPGFE_VERSION_H + +#define LIBPGFE_MAJOR_VERSION {major_version} +#define LIBPGFE_MINOR_VERSION {minor_version} +#define LIBPGFE_REVISION {revision} +#define LIBPGFE_VARIANT "{variant}" + +#define LIBPGFE_VERSION "{version}" + +#endif diff --git a/src/base-encoding-internal.c b/src/c/base_encoding/base-encoding-internal.c similarity index 98% rename from src/base-encoding-internal.c rename to src/c/base_encoding/base-encoding-internal.c index c5e8bb9..12880ea 100644 --- a/src/base-encoding-internal.c +++ b/src/c/base_encoding/base-encoding-internal.c @@ -5,11 +5,11 @@ Copyright (c) 2022 Charles Dong */ -#include "base-encoding-internal.h" +#include "backend/base-encoding-internal.h" #include -#include "generic-internal.h" +#include "backend/generic-internal.h" uint8_t __pgfe_build_mask(uint8_t digit_c); diff --git a/src/base16.c b/src/c/base_encoding/base16.c similarity index 95% rename from src/base16.c rename to src/c/base_encoding/base16.c index c3460f5..46fba76 100644 --- a/src/base16.c +++ b/src/c/base_encoding/base16.c @@ -7,7 +7,7 @@ #include "base-encoding.h" -#include "base-encoding-internal.h" +#include "backend/base-encoding-internal.h" // Base 16 alphabet const char BASE16_ALPHABET[] = "0123456789ABCDEF" diff --git a/src/base32.c b/src/c/base_encoding/base32.c similarity index 96% rename from src/base32.c rename to src/c/base_encoding/base32.c index 25c7a18..197f8f3 100644 --- a/src/base32.c +++ b/src/c/base_encoding/base32.c @@ -9,8 +9,8 @@ #include -#include "base-encoding-internal.h" -#include "generic-internal.h" +#include "backend/base-encoding-internal.h" +#include "backend/generic-internal.h" // Base 32 alphabet // 0123456789ABCDEF diff --git a/src/base64.c b/src/c/base_encoding/base64.c similarity index 97% rename from src/base64.c rename to src/c/base_encoding/base64.c index 992b224..086332a 100644 --- a/src/base64.c +++ b/src/c/base_encoding/base64.c @@ -9,7 +9,7 @@ #include -#include "base-encoding-internal.h" +#include "backend/base-encoding-internal.h" // Base 64 alphabet // 0123456789ABCDEF diff --git a/src/generic-internal.c b/src/c/generic-internal.c similarity index 96% rename from src/generic-internal.c rename to src/c/generic-internal.c index aa5b261..c78cdab 100644 --- a/src/generic-internal.c +++ b/src/c/generic-internal.c @@ -5,7 +5,7 @@ Copyright (c) 2022 Charles Dong */ -#include "generic-internal.h" +#include "backend/generic-internal.h" inline void __pgfe_arrinit(pgfe_encode_t arr[], size_t size) { for (int i = 0; i < size; i++) { diff --git a/src/generic.c b/src/c/generic.c similarity index 100% rename from src/generic.c rename to src/c/generic.c diff --git a/src/keccak-backend.c b/src/c/hash/keccak-backend.c similarity index 99% rename from src/keccak-backend.c rename to src/c/hash/keccak-backend.c index 3c643cc..4134b3c 100644 --- a/src/keccak-backend.c +++ b/src/c/hash/keccak-backend.c @@ -5,7 +5,7 @@ Copyright (c) 2022 Charles Dong */ -#include "keccak-backend.h" +#include "backend/keccak-backend.h" #include #include diff --git a/src/md5-backend.c b/src/c/hash/md5-backend.c similarity index 99% rename from src/md5-backend.c rename to src/c/hash/md5-backend.c index f982a52..2e32304 100644 --- a/src/md5-backend.c +++ b/src/c/hash/md5-backend.c @@ -5,7 +5,7 @@ Copyright (c) 2022 Charles Dong */ -#include "md5-backend.h" +#include "backend/md5-backend.h" #include diff --git a/src/md5.c b/src/c/hash/md5.c similarity index 96% rename from src/md5.c rename to src/c/hash/md5.c index ba58c95..86c397a 100644 --- a/src/md5.c +++ b/src/c/hash/md5.c @@ -9,8 +9,8 @@ #include -#include "md5-backend.h" -#include "templates.c" +#include "backend/md5-backend.h" +#include "backend/templates.h" __PGFE_FRONTEND_GEN2(md5); __PGFE_FRONTEND_DEFAULT_GEN2(md5, MD5); diff --git a/src/sha-internal.c b/src/c/hash/sha-internal.c similarity index 66% rename from src/sha-internal.c rename to src/c/hash/sha-internal.c index e53ff61..f45f646 100644 --- a/src/sha-internal.c +++ b/src/c/hash/sha-internal.c @@ -5,4 +5,4 @@ Copyright (c) 2022 Charles Dong */ -#include "sha-internal.h" +#include "backend/sha-internal.h" diff --git a/src/sha1.c b/src/c/hash/sha1.c similarity index 96% rename from src/sha1.c rename to src/c/hash/sha1.c index f5617ce..fe13d7c 100644 --- a/src/sha1.c +++ b/src/c/hash/sha1.c @@ -7,9 +7,9 @@ #include "sha1.h" -#include "generic-internal.h" -#include "sha-internal.h" -#include "templates.c" +#include "backend/generic-internal.h" +#include "backend/sha-internal.h" +#include "backend/templates.h" const pgfe_word_t __pgfe_sha1_H0[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; diff --git a/src/sha2-backend.c b/src/c/hash/sha2-backend.c similarity index 98% rename from src/sha2-backend.c rename to src/c/hash/sha2-backend.c index 59e4179..8c8d42f 100644 --- a/src/sha2-backend.c +++ b/src/c/hash/sha2-backend.c @@ -5,9 +5,10 @@ Copyright (c) 2022 Charles Dong */ -#include "sha2-backend.h" +#include "backend/sha2-backend.h" + +#include "backend/templates.h" #include "sha2.h" -#include "templates.c" #include @@ -70,6 +71,10 @@ void __pgfe_sha256_process_block(struct pgfe_sha256_ctx *ctx) { ctx->state[7] += H; ctx->index = 0; + + // Wipe data from RAM + tmp1 = tmp2 = A = B = C = D = E = F = G = H = 0; + memset(ws, 0, PGFE_SHA256_BLOCK_SIZE); } void __pgfe_sha224n256_padding(struct pgfe_sha256_ctx *ctx) { diff --git a/src/sha224.c b/src/c/hash/sha224.c similarity index 77% rename from src/sha224.c rename to src/c/hash/sha224.c index 028ab85..e1d4cde 100644 --- a/src/sha224.c +++ b/src/c/hash/sha224.c @@ -7,14 +7,14 @@ #include "sha2.h" -#include "sha2-backend.h" -#include "templates.c" +#include "backend/sha2-backend.h" +#include "backend/templates.h" 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_FRONTEND_GEN2(sha224) +__PGFE_FRONTEND_DEFAULT_GEN2(sha224, SHA224) __PGFE_SHA_INIT(sha224) __PGFE_SHA_UPDATE(sha224, SHA224) diff --git a/src/sha256.c b/src/c/hash/sha256.c similarity index 77% rename from src/sha256.c rename to src/c/hash/sha256.c index 3bfa704..27f35af 100644 --- a/src/sha256.c +++ b/src/c/hash/sha256.c @@ -5,15 +5,16 @@ Copyright (c) 2022 Charles Dong */ -#include "sha2-backend.h" #include "sha2.h" -#include "templates.c" + +#include "backend/sha2-backend.h" +#include "backend/templates.h" 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_FRONTEND_GEN2(sha256) +__PGFE_FRONTEND_DEFAULT_GEN2(sha256, SHA256) __PGFE_SHA_INIT(sha256) __PGFE_SHA_UPDATE(sha256, SHA256) diff --git a/src/sha3-224.c b/src/c/hash/sha3-224.c similarity index 95% rename from src/sha3-224.c rename to src/c/hash/sha3-224.c index 8ce2d81..8e8ec44 100644 --- a/src/sha3-224.c +++ b/src/c/hash/sha3-224.c @@ -6,7 +6,8 @@ */ #include "sha3.h" -#include "templates.c" + +#include "backend/templates.h" __PGFE_FRONTEND_GEN2(sha3_224); __PGFE_FRONTEND_DEFAULT_GEN2(sha3_224, SHA3_224); diff --git a/src/sha3-256.c b/src/c/hash/sha3-256.c similarity index 95% rename from src/sha3-256.c rename to src/c/hash/sha3-256.c index 707449a..f34a1da 100644 --- a/src/sha3-256.c +++ b/src/c/hash/sha3-256.c @@ -6,7 +6,8 @@ */ #include "sha3.h" -#include "templates.c" + +#include "backend/templates.h" __PGFE_FRONTEND_GEN2(sha3_256); __PGFE_FRONTEND_DEFAULT_GEN2(sha3_256, SHA3_256); diff --git a/src/sha3-384.c b/src/c/hash/sha3-384.c similarity index 95% rename from src/sha3-384.c rename to src/c/hash/sha3-384.c index 2db7dea..9bb2358 100644 --- a/src/sha3-384.c +++ b/src/c/hash/sha3-384.c @@ -6,7 +6,8 @@ */ #include "sha3.h" -#include "templates.c" + +#include "backend/templates.h" __PGFE_FRONTEND_GEN2(sha3_384); __PGFE_FRONTEND_DEFAULT_GEN2(sha3_384, SHA3_384); diff --git a/src/sha3-512.c b/src/c/hash/sha3-512.c similarity index 95% rename from src/sha3-512.c rename to src/c/hash/sha3-512.c index d92aaac..77be6b3 100644 --- a/src/sha3-512.c +++ b/src/c/hash/sha3-512.c @@ -6,7 +6,8 @@ */ #include "sha3.h" -#include "templates.c" + +#include "backend/templates.h" __PGFE_FRONTEND_GEN2(sha3_512); __PGFE_FRONTEND_DEFAULT_GEN2(sha3_512, SHA3_512); diff --git a/src/sha384.c b/src/c/hash/sha384.c similarity index 93% rename from src/sha384.c rename to src/c/hash/sha384.c index 15328a3..41db829 100644 --- a/src/sha384.c +++ b/src/c/hash/sha384.c @@ -7,8 +7,8 @@ #include "sha2.h" -#include "sha2-backend.h" -#include "templates.c" +#include "backend/sha2-backend.h" +#include "backend/templates.h" const pgfe_word_t __pgfe_sha384_H0[] = { 0xC1059ED8, 0xCBBB9D5D, 0x367CD507, 0x629A292A, 0x3070DD17, 0x9159015A, 0xF70E5939, 0x152FECD8, diff --git a/src/sha512.c b/src/c/hash/sha512.c similarity index 97% rename from src/sha512.c rename to src/c/hash/sha512.c index 9cd0b60..2e80ddf 100644 --- a/src/sha512.c +++ b/src/c/hash/sha512.c @@ -7,8 +7,8 @@ #include "sha2.h" -#include "sha2-backend.h" -#include "templates.c" +#include "backend/sha2-backend.h" +#include "backend/templates.h" const pgfe_word_t __pgfe_sha512_H0[] = { 0xF3BCC908, 0x6A09E667, 0x84CAA73B, 0xBB67AE85, 0xFE94F82B, 0x3C6EF372, 0x5F1D36F1, 0xA54FF53A, diff --git a/src/shake.c b/src/c/hash/shake.c similarity index 98% rename from src/shake.c rename to src/c/hash/shake.c index 7e0ab8f..94147e0 100644 --- a/src/shake.c +++ b/src/c/hash/shake.c @@ -7,7 +7,7 @@ #include "sha3.h" -#include "templates.c" +#include "backend/templates.h" // RawSHAKE128 diff --git a/src/hmac.c b/src/c/hmac/hmac.c similarity index 99% rename from src/hmac.c rename to src/c/hmac/hmac.c index 1e3f5af..29fe917 100644 --- a/src/hmac.c +++ b/src/c/hmac/hmac.c @@ -7,7 +7,7 @@ #include "hmac.h" -#include "generic-internal.h" +#include "backend/generic-internal.h" #define __pgfe_hmac_tmpl(name, upper) \ void pgfe_hmac_##name##_init(struct pgfe_hmac_##name##_ctx *ctx) { \ diff --git a/src/hotp.c b/src/c/otp/hotp.c similarity index 98% rename from src/hotp.c rename to src/c/otp/hotp.c index 2babd04..55cb125 100644 --- a/src/hotp.c +++ b/src/c/otp/hotp.c @@ -9,7 +9,7 @@ #include -#include "generic-internal.h" +#include "backend/generic-internal.h" #include "sha1.h" #define __PGFE_SET_SIZE_CASE_C(upper, lower) \ diff --git a/src/otp-generic.c b/src/c/otp/otp-generic.c similarity index 92% rename from src/otp-generic.c rename to src/c/otp/otp-generic.c index c415e47..e7cc515 100644 --- a/src/otp-generic.c +++ b/src/c/otp/otp-generic.c @@ -5,7 +5,7 @@ Copyright (c) 2022 Charles Dong */ -#include "otp-generic.h" +#include "backend/otp-generic.h" pgfe_otp_t pgfe_dynamically_truncate(const pgfe_encode_t hash[], size_t length) { uint8_t offset = hash[length - 1] & 0xF; diff --git a/src/totp.c b/src/c/otp/totp.c similarity index 98% rename from src/totp.c rename to src/c/otp/totp.c index 9a31f09..31450f8 100644 --- a/src/totp.c +++ b/src/c/otp/totp.c @@ -7,7 +7,7 @@ #include "totp.h" -#include "generic-internal.h" +#include "backend/generic-internal.h" #include "hotp.h" #include "sha1.h" #include "utils.h" diff --git a/src/utils.c b/src/c/utils.c similarity index 98% rename from src/utils.c rename to src/c/utils.c index 74f648e..598a863 100644 --- a/src/utils.c +++ b/src/c/utils.c @@ -7,7 +7,7 @@ #include "utils.h" -#include "generic-internal.h" +#include "backend/generic-internal.h" size_t pgfe_hash_to_hex_string(const pgfe_encode_t hash[], size_t hash_length, char out[]) { char *op = out, dual_hex[3]; diff --git a/src/algorithm_selectable.cpp b/src/cpp/algorithm_selectable.cpp similarity index 66% rename from src/algorithm_selectable.cpp rename to src/cpp/algorithm_selectable.cpp index c2ca56a..2993c0f 100644 --- a/src/algorithm_selectable.cpp +++ b/src/cpp/algorithm_selectable.cpp @@ -1,3 +1,10 @@ +/* + libpgfe + algorithm_selectable.cpp + + Copyright (c) 2022 Charles Dong +*/ + #include "algorithm_selectable.hpp" using namespace chardon55::PGFE; @@ -10,6 +17,6 @@ void AlgorithmSelectable::select_algorithm(pgfe_algorithm_choice choice) { after_change_alg(); } -pgfe_algorithm_choice AlgorithmSelectable::algorithm() { +pgfe_algorithm_choice AlgorithmSelectable::algorithm() const { return cur_alg; } \ No newline at end of file diff --git a/src/abstract_base_encoding.cpp b/src/cpp/base_encoding/abstract_base_encoding.cpp similarity index 88% rename from src/abstract_base_encoding.cpp rename to src/cpp/base_encoding/abstract_base_encoding.cpp index 0d6f007..016d152 100644 --- a/src/abstract_base_encoding.cpp +++ b/src/cpp/base_encoding/abstract_base_encoding.cpp @@ -8,7 +8,7 @@ #include #include -#include "abstract_base_encoding.hpp" +#include "backend_cpp/abstract_base_encoding.hpp" #define PGFE_SPACE_MULTIPLE 1.2 @@ -94,18 +94,18 @@ SequentialData AbstractBaseEncoding::decode(std::string &cpp_s) { return decode(cpp_s.c_str()); } -base_short_size_t AbstractBaseEncoding::unit_size() { +base_short_size_t AbstractBaseEncoding::unit_size() const { return unitsz; } -base_short_size_t AbstractBaseEncoding::chunk_size() { +base_short_size_t AbstractBaseEncoding::chunk_size() const { return chunksz; } -base_short_size_t AbstractBaseEncoding::bit_size() { +base_short_size_t AbstractBaseEncoding::bit_size() const { return bitsz; } -base_short_size_t AbstractBaseEncoding::alphabet_size() { +base_short_size_t AbstractBaseEncoding::alphabet_size() const { return alphabetsz; } \ No newline at end of file diff --git a/src/base16.cpp b/src/cpp/base_encoding/base16.cpp similarity index 100% rename from src/base16.cpp rename to src/cpp/base_encoding/base16.cpp diff --git a/src/base32.cpp b/src/cpp/base_encoding/base32.cpp similarity index 100% rename from src/base32.cpp rename to src/cpp/base_encoding/base32.cpp diff --git a/src/base64.cpp b/src/cpp/base_encoding/base64.cpp similarity index 100% rename from src/base64.cpp rename to src/cpp/base_encoding/base64.cpp diff --git a/src/cpp/generic.cpp b/src/cpp/generic.cpp new file mode 100644 index 0000000..5df02ba --- /dev/null +++ b/src/cpp/generic.cpp @@ -0,0 +1,30 @@ +/* + libpgfe + generic.cpp + + Copyright (c) 2022 Charles Dong +*/ + +#ifndef LIBPGFE_GENERIC_CPP +#define LIBPGFE_GENERIC_CPP + +#include "generic.hpp" + +#include "generic.h" + +namespace chardon55 { +namespace PGFE { + +pgfe_algorithm_choice string_to_algorithm_choice(const char *cs) { + std::string s{cs}; + return string_to_algorithm_choice(s); +} + +pgfe_algorithm_choice string_to_algorithm_choice(std::string cpp_s) { + return pgfe_option_map.at(cpp_s); +} + +} // namespace PGFE +} // namespace chardon55 + +#endif \ No newline at end of file diff --git a/src/hash_encoder.cpp b/src/cpp/hash/hash_encoder.cpp similarity index 91% rename from src/hash_encoder.cpp rename to src/cpp/hash/hash_encoder.cpp index ad20f06..8271a33 100644 --- a/src/hash_encoder.cpp +++ b/src/cpp/hash/hash_encoder.cpp @@ -55,6 +55,13 @@ void HashEncoder::destroy_context() { __PGFE_BATCH_CASES(CTX_DELETE) } +void HashEncoder::destroy_output() { + if (!out) return; + + delete out; + out = nullptr; +} + void HashEncoder::before_change_alg() { destroy_context(); } @@ -67,11 +74,6 @@ void HashEncoder::load_algorithm() { __PGFE_BATCH_CASES(CTX_CREATE) __PGFE_BATCH_CASES(INIT_FUNC_CALL) __PGFE_BATCH_CASES(INIT_SIZE) - - if (seq) { - delete[] seq; - } - seq = new pgfe_encode_t[digsz + 1]; } void HashEncoder::reset() { @@ -90,9 +92,7 @@ HashEncoder::HashEncoder(pgfe_algorithm_choice choice) { HashEncoder::~HashEncoder() { destroy_context(); - if (seq) { - delete[] seq; - } + destroy_output(); } void HashEncoder::update(const pgfe_encode_t sequence[], size_t length) { @@ -111,7 +111,7 @@ inline void HashEncoder::update(SequentialData &sd) { return this->AbstractHashEncoder::update(sd); } -SequentialData *HashEncoder::get_digest(uint64_t bitlength) { +const SequentialData *HashEncoder::get_digest(uint64_t bitlength) { bool shake_flag = this->cur_alg == SHAKE128 || this->cur_alg == RawSHAKE128 || this->cur_alg == SHAKE256 || this->cur_alg == RawSHAKE256; uint64_t in_len = to_byte(bitlength) + bit_rem(bitlength); @@ -125,6 +125,9 @@ SequentialData *HashEncoder::get_digest(uint64_t bitlength) { } } + size_t length = shake_flag ? in_len : in_len < digsz && bitlength ? in_len : digsz; + pgfe_encode_t seq[length]; + if (shake_flag) { __PGFE_BATCH_SHAKE_CASES(DIGEST_FUNC_CALL_LIMIT) } @@ -132,5 +135,7 @@ SequentialData *HashEncoder::get_digest(uint64_t bitlength) { __PGFE_BATCH_CASES_SP(DIGEST_FUNC_CALL) } - return new SequentialData(seq, shake_flag ? in_len : in_len < digsz && bitlength ? in_len : digsz); + destroy_output(); + out = new SequentialData(seq, length); + return out; } \ No newline at end of file diff --git a/src/hmac_encoder.cpp b/src/cpp/hmac/hmac_encoder.cpp similarity index 95% rename from src/hmac_encoder.cpp rename to src/cpp/hmac/hmac_encoder.cpp index 5e69d9e..31467d9 100644 --- a/src/hmac_encoder.cpp +++ b/src/cpp/hmac/hmac_encoder.cpp @@ -36,6 +36,7 @@ using namespace chardon55::PGFE; void HMACEncoder::destroy_output() { if (output) { delete output; + output = nullptr; } } @@ -101,10 +102,8 @@ inline void HMACEncoder::update(SequentialData &sd) { this->AbstractHashEncoder::update(sd); } -SequentialData *HMACEncoder::get_digest() { - if (!output) { - __PGFE_BATCH_CASES_SP(HMAC_DIGEST) - } - - return output->copy(); +const SequentialData *HMACEncoder::get_digest() { + destroy_output(); + __PGFE_BATCH_CASES_SP(HMAC_DIGEST) + return output; } \ No newline at end of file diff --git a/src/abstract_otp.cpp b/src/cpp/otp/abstract_otp.cpp similarity index 85% rename from src/abstract_otp.cpp rename to src/cpp/otp/abstract_otp.cpp index ec7e832..55c02ec 100644 --- a/src/abstract_otp.cpp +++ b/src/cpp/otp/abstract_otp.cpp @@ -5,7 +5,7 @@ Copyright (c) 2022 Charles Dong */ -#include "abstract_otp.hpp" +#include "backend_cpp/abstract_otp.hpp" #include @@ -26,7 +26,7 @@ void AbstractOTP::set_secret(SequentialData &sd) { set_secret(sd.to_pgfe_seq(sz), sz); } -std::string AbstractOTP::generate_str(uint8_t digit_count) { +std::string AbstractOTP::generate_str(uint8_t digit_count) const { std::string str = std::to_string(generate(digit_count)); while (str.length() < digit_count) { str.insert(0, "0"); diff --git a/src/hotp.cpp b/src/cpp/otp/hotp.cpp similarity index 94% rename from src/hotp.cpp rename to src/cpp/otp/hotp.cpp index ea844d7..222237a 100644 --- a/src/hotp.cpp +++ b/src/cpp/otp/hotp.cpp @@ -81,7 +81,7 @@ void HOTP::set_counter(pgfe_otp_counter_t c) { this->co = c; } -pgfe_otp_t HOTP::generate(uint8_t digit_count) { +pgfe_otp_t HOTP::generate(uint8_t digit_count) const { if (!secret) { throw NotInitializedException(); } @@ -89,7 +89,7 @@ pgfe_otp_t HOTP::generate(uint8_t digit_count) { return pgfe_hotp_generic(cur_alg, secret->to_pgfe_seq(), secret->length(), co, digit_count); } -std::string HOTP::generate_str(uint8_t digit_count) { +std::string HOTP::generate_str(uint8_t digit_count) const { return this->AbstractOTP::generate_str(digit_count); } diff --git a/src/totp.cpp b/src/cpp/otp/totp.cpp similarity index 88% rename from src/totp.cpp rename to src/cpp/otp/totp.cpp index 05fa8bf..f791cc5 100644 --- a/src/totp.cpp +++ b/src/cpp/otp/totp.cpp @@ -15,11 +15,11 @@ void TOTP::set_interval(pgfe_totp_interval_t interval) { this->interval = interval; } -pgfe_totp_interval_t TOTP::get_interval() { +pgfe_totp_interval_t TOTP::get_interval() const { return interval; } -pgfe_time_t TOTP::get_initial_time() { +pgfe_time_t TOTP::get_initial_time() const { return initial_time; } @@ -27,7 +27,7 @@ void TOTP::set_initial_time(pgfe_time_t initial_time) { this->initial_time = initial_time; } -pgfe_time_t TOTP::get_update_time() { +pgfe_time_t TOTP::get_update_time() const { return update_time; } @@ -36,7 +36,7 @@ void TOTP::update_counter() { set_counter(__pgfe_calc_periodic_counter(update_time, interval, initial_time, &delta)); } -pgfe_time_t TOTP::get_remain_time() { +pgfe_time_t TOTP::get_remain_time() const { return delta - (pgfe_curtime() - update_time); } diff --git a/src/sequential_data.cpp b/src/cpp/sequential_data.cpp similarity index 66% rename from src/sequential_data.cpp rename to src/cpp/sequential_data.cpp index a1b95fc..b030668 100644 --- a/src/sequential_data.cpp +++ b/src/cpp/sequential_data.cpp @@ -12,9 +12,7 @@ using namespace chardon55::PGFE; SequentialData::~SequentialData() { - if (seq) { - delete[] seq; - } + delete[] seq; if (hex_str) { delete[] hex_str; @@ -34,16 +32,25 @@ SequentialData::SequentialData(const char *cs) : SequentialData((const pgfe_enco SequentialData::SequentialData(std::string &cpp_s) : SequentialData((const pgfe_encode_t *)cpp_s.c_str(), cpp_s.length()) {} -size_t SequentialData::length() { +SequentialData::SequentialData(const SequentialData *sd) : SequentialData((const pgfe_encode_t *)sd->seq, sd->sz) {} + +SequentialData::SequentialData(SequentialData *sd, bool delete_current) + : SequentialData((const pgfe_encode_t *)sd->seq, sd->sz) { + if (delete_current) { + delete sd; + } +} + +size_t SequentialData::length() const { return sz; } -const char *SequentialData::to_cs() { +const char *SequentialData::to_cs() const { return (const char *)seq; } -std::string SequentialData::to_str() { - std::string s((char *)seq); +std::string SequentialData::to_str() const { + std::string s{(char *)seq}; return s; } @@ -57,21 +64,23 @@ const char *SequentialData::to_hex_cs() { return hex_str; } -std::string SequentialData::to_hex_str() { - std::string s(to_hex_cs()); +std::string SequentialData::to_hex_str() const { + char hex_str[sz * 2 + 1]; + pgfe_hash_to_hex_string(seq, sz, hex_str); + std::string s(hex_str); return s; } -const pgfe_encode_t *SequentialData::to_pgfe_seq() { +const pgfe_encode_t *SequentialData::to_pgfe_seq() const { return seq; } -const pgfe_encode_t *SequentialData::to_pgfe_seq(size_t &length_out) { +const pgfe_encode_t *SequentialData::to_pgfe_seq(size_t &length_out) const { length_out = sz; return to_pgfe_seq(); } -bool SequentialData::is_str() { +bool SequentialData::is_str() const { return _is_str; } @@ -79,7 +88,7 @@ void SequentialData::set_is_str(bool str) { _is_str = str; } -bool SequentialData::is_apparent_str() { +bool SequentialData::is_apparent_str() const { return _apstr; } @@ -93,6 +102,10 @@ bool SequentialData::determine_ascii_str() { return true; } +SequentialData *SequentialData::truncate(size_t start, size_t length) const { + return new SequentialData(&seq[start], length); +} + SequentialData *SequentialData::truncate(size_t start, size_t length, bool inplace) { if (inplace) { if (start) { @@ -104,9 +117,9 @@ SequentialData *SequentialData::truncate(size_t start, size_t length, bool inpla return nullptr; } - return new SequentialData(&seq[start], length); + return truncate(start, length); } -SequentialData *SequentialData::copy() { +SequentialData *SequentialData::copy() const { return new SequentialData((const pgfe_encode_t *)seq, sz); } \ No newline at end of file diff --git a/src/utils.cpp b/src/cpp/utils.cpp similarity index 80% rename from src/utils.cpp rename to src/cpp/utils.cpp index 667fb66..c03e377 100644 --- a/src/utils.cpp +++ b/src/cpp/utils.cpp @@ -41,15 +41,6 @@ SequentialData *sequential_data::from_hex_string(std::string &hex_cpps) { return from_hex_string(hex_cpps.c_str()); } -pgfe_algorithm_choice string_to_algorithm_choice(const char *cs) { - std::string s{cs}; - return string_to_algorithm_choice(s); -} - -pgfe_algorithm_choice string_to_algorithm_choice(std::string cpp_s) { - return pgfe_option_map[cpp_s]; -} - } // namespace utils } // namespace PGFE } // namespace chardon55 \ No newline at end of file diff --git a/test/hash_encoder_test.hpp b/test/hash_encoder_test.hpp index 48c9aad..fdf6b1f 100644 --- a/test/hash_encoder_test.hpp +++ b/test/hash_encoder_test.hpp @@ -32,5 +32,4 @@ void hash_encoder_test(ARGS) { // puts(sd.to_hex_cs()); cout << *sd << endl; - delete sd; } \ No newline at end of file diff --git a/test/hmactest.hpp b/test/hmactest.hpp index e7ca2c3..4c57329 100644 --- a/test/hmactest.hpp +++ b/test/hmactest.hpp @@ -13,7 +13,6 @@ void hmac_encoder_test(ARGS) { USE_PGFE_CPP - USE_PGFE_CPP_UTILS HMACEncoder *encoder; @@ -28,8 +27,8 @@ void hmac_encoder_test(ARGS) { encoder->update(argv[4]); - auto sd = encoder->get_digest(); - puts(sd->to_hex_cs()); - delete sd; + SequentialData sd{encoder->get_digest()}; + puts(sd.to_hex_cs()); + delete encoder; }