From 4eebef1961a1c01890fe11dc5f1b9f3a1ea705e0 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 6 Sep 2023 23:23:31 -0400 Subject: add examples/ --- examples/00-sha3-256/Makefile | 16 ++++++++ examples/00-sha3-256/hex.h | 1 + examples/00-sha3-256/main.c | 23 ++++++++++++ examples/00-sha3-256/sha3.c | 1 + examples/00-sha3-256/sha3.h | 1 + examples/01-shake128/Makefile | 16 ++++++++ examples/01-shake128/hex.h | 1 + examples/01-shake128/main.c | 24 ++++++++++++ examples/01-shake128/sha3.c | 1 + examples/01-shake128/sha3.h | 1 + examples/02-kmac128/Makefile | 16 ++++++++ examples/02-kmac128/hex.h | 1 + examples/02-kmac128/main.c | 53 +++++++++++++++++++++++++++ examples/02-kmac128/sha3.c | 1 + examples/02-kmac128/sha3.h | 1 + examples/03-tuplehash128/Makefile | 16 ++++++++ examples/03-tuplehash128/hex.h | 1 + examples/03-tuplehash128/main.c | 75 ++++++++++++++++++++++++++++++++++++++ examples/03-tuplehash128/sha3.c | 1 + examples/03-tuplehash128/sha3.h | 1 + examples/04-turboshake128/Makefile | 16 ++++++++ examples/04-turboshake128/hex.h | 1 + examples/04-turboshake128/main.c | 56 ++++++++++++++++++++++++++++ examples/04-turboshake128/sha3.c | 1 + examples/04-turboshake128/sha3.h | 1 + examples/README.md | 1 + 26 files changed, 327 insertions(+) create mode 100644 examples/00-sha3-256/Makefile create mode 120000 examples/00-sha3-256/hex.h create mode 100644 examples/00-sha3-256/main.c create mode 120000 examples/00-sha3-256/sha3.c create mode 120000 examples/00-sha3-256/sha3.h create mode 100644 examples/01-shake128/Makefile create mode 120000 examples/01-shake128/hex.h create mode 100644 examples/01-shake128/main.c create mode 120000 examples/01-shake128/sha3.c create mode 120000 examples/01-shake128/sha3.h create mode 100644 examples/02-kmac128/Makefile create mode 120000 examples/02-kmac128/hex.h create mode 100644 examples/02-kmac128/main.c create mode 120000 examples/02-kmac128/sha3.c create mode 120000 examples/02-kmac128/sha3.h create mode 100644 examples/03-tuplehash128/Makefile create mode 120000 examples/03-tuplehash128/hex.h create mode 100644 examples/03-tuplehash128/main.c create mode 120000 examples/03-tuplehash128/sha3.c create mode 120000 examples/03-tuplehash128/sha3.h create mode 100644 examples/04-turboshake128/Makefile create mode 120000 examples/04-turboshake128/hex.h create mode 100644 examples/04-turboshake128/main.c create mode 120000 examples/04-turboshake128/sha3.c create mode 120000 examples/04-turboshake128/sha3.h create mode 100644 examples/README.md (limited to 'examples') diff --git a/examples/00-sha3-256/Makefile b/examples/00-sha3-256/Makefile new file mode 100644 index 0000000..198831b --- /dev/null +++ b/examples/00-sha3-256/Makefile @@ -0,0 +1,16 @@ +CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -O3 -march=native -mtune=native +APP=./sha3-256-example +OBJS=sha3.o main.o + +.PHONY=all + +all: $(APP) + +$(APP): $(OBJS) + $(CC) -o $(APP) $(CFLAGS) $(OBJS) + +%.o: %.c + $(CC) -c $(CFLAGS) $< + +clean: + $(RM) -f $(APP) $(OBJS) diff --git a/examples/00-sha3-256/hex.h b/examples/00-sha3-256/hex.h new file mode 120000 index 0000000..2adfa3e --- /dev/null +++ b/examples/00-sha3-256/hex.h @@ -0,0 +1 @@ +../../hex.h \ No newline at end of file diff --git a/examples/00-sha3-256/main.c b/examples/00-sha3-256/main.c new file mode 100644 index 0000000..26cdec3 --- /dev/null +++ b/examples/00-sha3-256/main.c @@ -0,0 +1,23 @@ +// +// sha3-256-example: print sha3-256 hash of data to standard output. +// +#include +#include +#include "hex.h" +#include "sha3.h" + +// test data +static const uint8_t DATA[] = "this is some test data"; + +int main() { + // hash data + uint8_t buf[32] = { 0 }; + sha3_256(DATA, sizeof(DATA), buf); + + // print result to stdout + printf("SHA3-256: "); + hex_write(stdout, buf, sizeof(buf)); + printf("\n"); + + return 0; +} diff --git a/examples/00-sha3-256/sha3.c b/examples/00-sha3-256/sha3.c new file mode 120000 index 0000000..4748193 --- /dev/null +++ b/examples/00-sha3-256/sha3.c @@ -0,0 +1 @@ +../../sha3.c \ No newline at end of file diff --git a/examples/00-sha3-256/sha3.h b/examples/00-sha3-256/sha3.h new file mode 120000 index 0000000..b7c53d4 --- /dev/null +++ b/examples/00-sha3-256/sha3.h @@ -0,0 +1 @@ +../../sha3.h \ No newline at end of file diff --git a/examples/01-shake128/Makefile b/examples/01-shake128/Makefile new file mode 100644 index 0000000..f5080cd --- /dev/null +++ b/examples/01-shake128/Makefile @@ -0,0 +1,16 @@ +CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -O3 -march=native -mtune=native +APP=./shake128-example +OBJS=sha3.o main.o + +.PHONY=all + +all: $(APP) + +$(APP): $(OBJS) + $(CC) -o $(APP) $(CFLAGS) $(OBJS) + +%.o: %.c + $(CC) -c $(CFLAGS) $< + +clean: + $(RM) -f $(APP) $(OBJS) diff --git a/examples/01-shake128/hex.h b/examples/01-shake128/hex.h new file mode 120000 index 0000000..2adfa3e --- /dev/null +++ b/examples/01-shake128/hex.h @@ -0,0 +1 @@ +../../hex.h \ No newline at end of file diff --git a/examples/01-shake128/main.c b/examples/01-shake128/main.c new file mode 100644 index 0000000..f2a2347 --- /dev/null +++ b/examples/01-shake128/main.c @@ -0,0 +1,24 @@ +// +// shake128-example: hash contents of DATA with SHAKE128 and print first +// 200 bytes of SHAKE128 hash of data to standard output. +// +#include +#include +#include "hex.h" +#include "sha3.h" + +// test data +static const uint8_t DATA[] = "this is some test data"; + +int main() { + // hash data + uint8_t buf[200] = { 0 }; + shake128_xof_once(DATA, sizeof(DATA), buf, sizeof(buf)); + + // print result to stdout + printf("SHAKE128 (200 bytes): "); + hex_write(stdout, buf, sizeof(buf)); + printf("\n"); + + return 0; +} diff --git a/examples/01-shake128/sha3.c b/examples/01-shake128/sha3.c new file mode 120000 index 0000000..4748193 --- /dev/null +++ b/examples/01-shake128/sha3.c @@ -0,0 +1 @@ +../../sha3.c \ No newline at end of file diff --git a/examples/01-shake128/sha3.h b/examples/01-shake128/sha3.h new file mode 120000 index 0000000..b7c53d4 --- /dev/null +++ b/examples/01-shake128/sha3.h @@ -0,0 +1 @@ +../../sha3.h \ No newline at end of file diff --git a/examples/02-kmac128/Makefile b/examples/02-kmac128/Makefile new file mode 100644 index 0000000..ae267f9 --- /dev/null +++ b/examples/02-kmac128/Makefile @@ -0,0 +1,16 @@ +CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -O3 -march=native -mtune=native +APP=./kmac128-example +OBJS=sha3.o main.o + +.PHONY=all + +all: $(APP) + +$(APP): $(OBJS) + $(CC) -o $(APP) $(CFLAGS) $(OBJS) + +%.o: %.c + $(CC) -c $(CFLAGS) $< + +clean: + $(RM) -f $(APP) $(OBJS) diff --git a/examples/02-kmac128/hex.h b/examples/02-kmac128/hex.h new file mode 120000 index 0000000..2adfa3e --- /dev/null +++ b/examples/02-kmac128/hex.h @@ -0,0 +1 @@ +../../hex.h \ No newline at end of file diff --git a/examples/02-kmac128/main.c b/examples/02-kmac128/main.c new file mode 100644 index 0000000..d1965a9 --- /dev/null +++ b/examples/02-kmac128/main.c @@ -0,0 +1,53 @@ +// +// kmac128-example: Hash contents of DATA with KMAC128 and key and print first +// 32 bytes of result to standard output. +// +#include +#include +#include "hex.h" +#include "sha3.h" + +// test data +static const uint8_t DATA[] = "this is some test data"; + +static const struct { + const char *name; + kmac_params_t params; +} ROWS[] = {{ + .name = "KMAC128 (key = \"\", custom = \"\")", +}, { + .name = "KMAC128 (key = \"foo\", custom = \"\")", + .params = { + .key = (uint8_t*) "foo", + .key_len = 3, + }, +}, { + .name = "KMAC128 (key = \"bar\", custom = \"\")", + .params = { + .key = (uint8_t*) "bar", + .key_len = 3, + }, +}, { + .name = "KMAC128 (key = \"bar\", custom = \"blum\")", + .params = { + .key = (uint8_t*) "bar", + .key_len = 3, + .custom = (uint8_t*) "blum", + .custom_len = 4, + }, +}}; + +int main() { + for (size_t i = 0; i < sizeof(ROWS)/sizeof(ROWS[0]); i++) { + // hash data + uint8_t buf[32] = { 0 }; + kmac128_xof_once(ROWS[i].params, DATA, sizeof(DATA), buf, sizeof(buf)); + + // print to stdout + printf("%s:\n", ROWS[i].name); + hex_write(stdout, buf, sizeof(buf)); + printf("\n"); + } + + return 0; +} diff --git a/examples/02-kmac128/sha3.c b/examples/02-kmac128/sha3.c new file mode 120000 index 0000000..4748193 --- /dev/null +++ b/examples/02-kmac128/sha3.c @@ -0,0 +1 @@ +../../sha3.c \ No newline at end of file diff --git a/examples/02-kmac128/sha3.h b/examples/02-kmac128/sha3.h new file mode 120000 index 0000000..b7c53d4 --- /dev/null +++ b/examples/02-kmac128/sha3.h @@ -0,0 +1 @@ +../../sha3.h \ No newline at end of file diff --git a/examples/03-tuplehash128/Makefile b/examples/03-tuplehash128/Makefile new file mode 100644 index 0000000..cbbd3a9 --- /dev/null +++ b/examples/03-tuplehash128/Makefile @@ -0,0 +1,16 @@ +CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -O3 -march=native -mtune=native +APP=./tuplehash128-example +OBJS=sha3.o main.o + +.PHONY=all + +all: $(APP) + +$(APP): $(OBJS) + $(CC) -o $(APP) $(CFLAGS) $(OBJS) + +%.o: %.c + $(CC) -c $(CFLAGS) $< + +clean: + $(RM) -f $(APP) $(OBJS) diff --git a/examples/03-tuplehash128/hex.h b/examples/03-tuplehash128/hex.h new file mode 120000 index 0000000..2adfa3e --- /dev/null +++ b/examples/03-tuplehash128/hex.h @@ -0,0 +1 @@ +../../hex.h \ No newline at end of file diff --git a/examples/03-tuplehash128/main.c b/examples/03-tuplehash128/main.c new file mode 100644 index 0000000..c9e1952 --- /dev/null +++ b/examples/03-tuplehash128/main.c @@ -0,0 +1,75 @@ +// +// tuplehash128-example: Hash tuples with TupleHash128 and key +// and print first 20 bytes of result to standard output. +// +#include +#include +#include "hex.h" +#include "sha3.h" + +static const struct { + const char *name; + tuplehash_str_t strs[10]; + size_t num_strs; +} ROWS[] = {{ + .name = "TupleHash128: \"abc\"", + .strs = {{ + .ptr = (uint8_t*) "abc", + .len = 3, + }}, + .num_strs = 1, +}, { + .name = "TupleHash128: \"ab\", \"c\"", + .strs = {{ + .ptr = (uint8_t*) "ab", + .len = 2, + }, { + .ptr = (uint8_t*) "c", + .len = 1, + }}, + .num_strs = 2, +}, { + .name = "TupleHash128: \"a\", \"bc\"", + .strs = {{ + .ptr = (uint8_t*) "a", + .len = 1, + }, { + .ptr = (uint8_t*) "bc", + .len = 2, + }}, + .num_strs = 2, +}, { + .name = "TupleHash128: \"a\", \"b\", \"c\"", + .strs = {{ + .ptr = (uint8_t*) "a", + .len = 1, + }, { + .ptr = (uint8_t*) "b", + .len = 1, + }, { + .ptr = (uint8_t*) "c", + .len = 1, + }}, + .num_strs = 3, +}}; + +int main() { + for (size_t i = 0; i < sizeof(ROWS)/sizeof(ROWS[0]); i++) { + // build params + tuplehash_params_t params = { + .strs = ROWS[i].strs, + .num_strs = ROWS[i].num_strs, + }; + + // hash data + uint8_t buf[32]; + tuplehash128(params, buf, sizeof(buf)); + + // print to stdout + printf("%s:\n", ROWS[i].name); + hex_write(stdout, buf, sizeof(buf)); + printf("\n"); + } + + return 0; +} diff --git a/examples/03-tuplehash128/sha3.c b/examples/03-tuplehash128/sha3.c new file mode 120000 index 0000000..4748193 --- /dev/null +++ b/examples/03-tuplehash128/sha3.c @@ -0,0 +1 @@ +../../sha3.c \ No newline at end of file diff --git a/examples/03-tuplehash128/sha3.h b/examples/03-tuplehash128/sha3.h new file mode 120000 index 0000000..b7c53d4 --- /dev/null +++ b/examples/03-tuplehash128/sha3.h @@ -0,0 +1 @@ +../../sha3.h \ No newline at end of file diff --git a/examples/04-turboshake128/Makefile b/examples/04-turboshake128/Makefile new file mode 100644 index 0000000..020ca7c --- /dev/null +++ b/examples/04-turboshake128/Makefile @@ -0,0 +1,16 @@ +CFLAGS=-W -Wall -Wextra -Werror -pedantic -std=c11 -O3 -march=native -mtune=native +APP=./turbohash128-example +OBJS=sha3.o main.o + +.PHONY=all + +all: $(APP) + +$(APP): $(OBJS) + $(CC) -o $(APP) $(CFLAGS) $(OBJS) + +%.o: %.c + $(CC) -c $(CFLAGS) $< + +clean: + $(RM) -f $(APP) $(OBJS) diff --git a/examples/04-turboshake128/hex.h b/examples/04-turboshake128/hex.h new file mode 120000 index 0000000..2adfa3e --- /dev/null +++ b/examples/04-turboshake128/hex.h @@ -0,0 +1 @@ +../../hex.h \ No newline at end of file diff --git a/examples/04-turboshake128/main.c b/examples/04-turboshake128/main.c new file mode 100644 index 0000000..a20a323 --- /dev/null +++ b/examples/04-turboshake128/main.c @@ -0,0 +1,56 @@ +// +// turboshake128-example: Absorb 4096 bytes of data from /dev/urandom in +// 1024 byte chunks, hash it with TurboShake128, then print 128 bytes of +// result in 32 byte chunks (hex-encoded) to standard output. +// output. +// +#include +#include +#include +#include "hex.h" +#include "sha3.h" + +int main() { + // init turboshake + turboshake_t ts; + turboshake128_init(&ts); + + // open source + FILE *fh = fopen("/dev/urandom", "rb"); + if (!fh) { + err(-1, "fopen()"); + } + + { + // read and absorb + uint8_t buf[1024] = { 0 }; + for (size_t i = 0; i < 4096/sizeof(buf); i++) { + // read data + if (!fread(buf, sizeof(buf), 1, fh)) { + err(-1, "fread()"); + } + + // absorb + turboshake128_absorb(&ts, buf, sizeof(buf)); + } + } + + // close source + if (!fclose(fh)) { + warn("fclose()"); + } + + + printf("TurboShake128 output:\n"); + { + // squeeze in 32 byte chunks, write to stdout + uint8_t buf[32] = { 0 }; + for (size_t ofs = 0; ofs < 128; ofs += sizeof(buf)) { + turboshake128_squeeze(&ts, buf, sizeof(buf)); + hex_write(stdout, buf, sizeof(buf)); + } + } + printf("\n"); + + return 0; +} diff --git a/examples/04-turboshake128/sha3.c b/examples/04-turboshake128/sha3.c new file mode 120000 index 0000000..4748193 --- /dev/null +++ b/examples/04-turboshake128/sha3.c @@ -0,0 +1 @@ +../../sha3.c \ No newline at end of file diff --git a/examples/04-turboshake128/sha3.h b/examples/04-turboshake128/sha3.h new file mode 120000 index 0000000..b7c53d4 --- /dev/null +++ b/examples/04-turboshake128/sha3.h @@ -0,0 +1 @@ +../../sha3.h \ No newline at end of file diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..4695d32 --- /dev/null +++ b/examples/README.md @@ -0,0 +1 @@ +Miscellaneous simple examples. -- cgit v1.2.3