From c8ac1120ad832f560defb6920a42b547ed45d5b4 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 23 Jun 2020 13:55:48 +0100 Subject: [PATCH] cli --- .gitignore | 3 +- Cargo.toml | 2 +- Makefile | 7 +- {test => cli}/Makefile | 15 +++- cli/build/.gitkeep | 0 {test => cli}/include | 0 {test => cli}/lib/libkhash.so | 0 cli/src/main.c | 133 ++++++++++++++++++++++++++++++++++ include/khash.h | 2 + src/ctx.rs | 30 ++++++++ src/lib.rs | 21 ++++++ test/src/main.c | 25 ------- 12 files changed, 206 insertions(+), 32 deletions(-) rename {test => cli}/Makefile (51%) create mode 100644 cli/build/.gitkeep rename {test => cli}/include (100%) rename {test => cli}/lib/libkhash.so (100%) create mode 100644 cli/src/main.c create mode 100644 src/ctx.rs delete mode 100644 test/src/main.c diff --git a/.gitignore b/.gitignore index d449bd3..4eb686f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ Cargo.lock *~ node_modules/ -test/build/test +cli/build/test +cli/build/kana-hash diff --git a/Cargo.toml b/Cargo.toml index b3efc07..990015e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["cdylib"] opt-level = 3 lto = "fat" codegen-units = 1 -panic = "abort" +panic = "unwind" [dependencies] sha2 = "0.9" diff --git a/Makefile b/Makefile index bb9cb3e..fc90a28 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,20 @@ INSTALL:= /usr/local/lib +INSTALL-BIN:= /usr/local/bin +CLI:= cli khash: RUSTFLAGS="-C target-cpu=native" cargo build --release strip ./target/release/libkhash.so + cd $(CLI) && $(MAKE) kana-hash test: cargo test - cd test && $(MAKE) + cd $(CLI) && $(MAKE) install: cp -f ./target/release/libkhash.so $(INSTALL)/libkhash.so + cp -f $(CLI)/build/kana-hash $(INSTALL-BIN)/kana-hash uninstall: rm -f $(INSTALL)/libkana_hash.so + rm -f $(INSTALL-BIN)/kana-hash diff --git a/test/Makefile b/cli/Makefile similarity index 51% rename from test/Makefile rename to cli/Makefile index a7fdbba..d6bf92f 100644 --- a/test/Makefile +++ b/cli/Makefile @@ -1,15 +1,22 @@ SRC:= src/*.c INCLUDE:=include/ -LIB:=../target/release -BUILD:=build/ +LIB:=lib +BUILD:=build CFLAGS:= -g -Wall -pedantic LFLAGS:= -L$(LIB) -lkhash -all: clean test +all: clean test kana-hash clean: + rm -f $(BUILD)/* -test: +kana-hash: gcc $(SRC) -I$(INCLUDE) $(CFLAGS) -o $(BUILD)/$@ $(LFLAGS) + $(BUILD)/$@ test + +test: + gcc $(SRC) -I$(INCLUDE) -DTEST $(CFLAGS) -o $(BUILD)/$@ $(LFLAGS) $(BUILD)/$@ + + diff --git a/cli/build/.gitkeep b/cli/build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/include b/cli/include similarity index 100% rename from test/include rename to cli/include diff --git a/test/lib/libkhash.so b/cli/lib/libkhash.so similarity index 100% rename from test/lib/libkhash.so rename to cli/lib/libkhash.so diff --git a/cli/src/main.c b/cli/src/main.c new file mode 100644 index 0000000..9659908 --- /dev/null +++ b/cli/src/main.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include + +#include + +#ifdef TEST +int main(void) +{ + setlocale(LC_ALL, ""); + const char* string = "hello world!"; + + printf("input: %s\n", string); + khash_salt salt; + assert(khash_new_salt(KHASH_SALT_TYPE_RANDOM, NULL, 0, &salt) == KHASH_SUCCESS); + printf("salt: %d\n", (int)salt.size); + size_t length; + assert(khash_length(string, strlen(string), &salt, &length) == KHASH_SUCCESS); + printf("length: %d\n", (int)length); + char* output = alloca(length+1); + assert(khash_do(string, strlen(string), &salt, output,length) == KHASH_SUCCESS); + + printf("output: %s\n", output); + return 0; +} +#else + +#define KTRY(expr, msg) (assert(((expr) && (msg))== KHASH_SUCCESS)) + +void k_do(const char* input, const khash_salt* salt) +{ + khash_salt clone; + KTRY(khash_clone_salt(salt, &clone), "khash: saltclone failed"); + + size_t length; + KTRY(khash_length(input, strlen(input), &clone, &length), "khash: hashlength failed"); + + char* output =alloca(length+1); + KTRY(khash_do(input,strlen(input), &clone, output, length), "khash: hashstring failed"); + output[length] = 0; //ensure no overflow. + printf("%s\n", output); +} + +void reseed_salt(khash_salt* salt, uint8_t type, const void* in_ptr, size_t ptr_sz) +{ + KTRY(khash_free_salt(salt), "khash: saltrefree failed"); + KTRY(khash_new_salt(type, in_ptr, ptr_sz, salt), "khash: saltreseed failed"); +} + +static int _main(int argc, char** argv, khash_salt salt) +{ + int look = 1; + if (argc <= 1) + { + printf("try `%s --help`\n", argv[0]); + return 1; + } + + for(argv++;*argv;argv++) + { + if (!look) goto work; + if (strcmp(*argv, "--help") == 0) + { + printf("kana-hash cli\n"); + printf("Usage: khash [--salt SALT-TYPE []] [--] \n"); + printf(" --salt: Specify the salt.\n"); + printf(" SALT_TYPE: D: default embedded.\n"); + printf(" : N: no salt.\n"); + printf(" : S : specific salt.\n"); + printf(" --: Stop reading args here.\n"); + return 1; + } + else if (strcmp(*argv, "--") == 0) + look = 0; + else if (strcmp(*argv, "--salt")==0) + { + if (argv[1]) + { + switch (argv[1][0]) + { + case 'd': + case 'D': + reseed_salt(&salt, KHASH_SALT_TYPE_DEFAULT, NULL, 0); + break; + case 'N': + case 'n': + reseed_salt(&salt, KHASH_SALT_TYPE_NONE, NULL, 0); + break; + case 'S': + case 's': + if(argv[2]) + reseed_salt(&salt, KHASH_SALT_TYPE_SPECIFIC, argv[2], strlen(argv[2])); + else { + fprintf(stderr, "SALT_TYPE `%c' expects a value.\n", *argv[1]); + return 1; + } + argv++; + break; + default: + fprintf(stderr, "Unknown SALT_TYPE `%c'\n", *argv[1]); + return 1; + } + argv++; + } + else { + fprintf(stderr, "--salt expects at least SALT_TYPE.\n"); + return 1; + } + } + else { + work: + k_do(*argv, &salt); + } + } + + return 0; +} + +int main(int argc, char** argv) +{ + + khash_salt salt; + KTRY(khash_new_salt(KHASH_SALT_TYPE_DEFAULT, NULL, 0, &salt), "khash: saltgen failed"); + + int res = _main(argc, argv, salt); + + KTRY(khash_free_salt(&salt), "khash: saltfree failed"); + + return res; +} +#endif diff --git a/include/khash.h b/include/khash.h index 8b46771..06ea57c 100644 --- a/include/khash.h +++ b/include/khash.h @@ -42,6 +42,8 @@ extern "C" { extern int32_t khash_new_salt(uint8_t salt_type, const void* data, size_t size, khash_salt* output); /// Free a salt allocated with `khash_new_salt`. It is okay to call this multiple times. extern int32_t khash_free_salt(khash_salt* salt); + /// Clone a salt allocated with `khash_new_salt`. + extern int32_t khash_clone_salt(const khash_salt* src, khash_salt* dst); /// Compute the length of hash required for the specified input. extern int32_t khash_length(const void* data, size_t size, const khash_salt* salt, size_t* length); /// Compute the hash and store it in `string`. Will write no more than `strlen` bytes into `string`. diff --git a/src/ctx.rs b/src/ctx.rs new file mode 100644 index 0000000..05d30f1 --- /dev/null +++ b/src/ctx.rs @@ -0,0 +1,30 @@ +use crate::*; + +pub const ALGO_CRC32: u8 = 0; +pub const ALGO_CRC64: u8 = 1; +pub const ALGO_SHA256: u8 = 2; + +#[derive(Debug,PartialEq,Eq,Hash)] +pub enum Algorithm +{ + Crc32, + Crc64, + Sha256, +} + +pub struct Context +{ + algo: Algorithm, + salt: salt::Salt, + +} + +/// FFI context +#[derive(Debug)] +#[repr(C)] +pub struct CContext +{ + algo: u8, + salt: *mut salt::FFI, + +} diff --git a/src/lib.rs b/src/lib.rs index 0535b02..7377b4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,3 +161,24 @@ pub unsafe extern "C" fn khash_new_salt(salt_type: u8, bin: *const c_void, sz: s GENERIC_SUCCESS } } + +#[no_mangle] +pub unsafe extern "C" fn khash_clone_salt(salt: *const salt::FFI, out: *mut salt::FFI) -> i32 +{ + no_unwind!{ + *out = salt::into_raw(salt::clone_from_raw(salt)); + GENERIC_SUCCESS + } +} + + +//TODO: +/* +mod ctx; + +#[no_mangle] +pub unsafe extern "C" fn khash_new_context(salt: *mut salt::FFI, ctx: *mut ctx::CContext) -> i32 +{ + +} +*/ diff --git a/test/src/main.c b/test/src/main.c deleted file mode 100644 index 23e3782..0000000 --- a/test/src/main.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -#include -#include -#include - -int main(void) -{ - setlocale(LC_ALL, ""); - const char* string = "hello world!"; - - printf("input: %s\n", string); - khash_salt salt; - assert(khash_new_salt(KHASH_SALT_TYPE_RANDOM, NULL, 0, &salt) == KHASH_SUCCESS); - printf("salt: %d\n", (int)salt.size); - size_t length; - assert(khash_length(string, strlen(string), &salt, &length) == KHASH_SUCCESS); - printf("length: %d\n", (int)length); - char* output = alloca(length+1); - assert(khash_do(string, strlen(string), &salt, output,length) == KHASH_SUCCESS); - - printf("output: %s\n", output); - return 0; -}