From 08b69be797868cbb97846f3f67071adc4b77e26e Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 5 Aug 2022 16:54:21 +0200 Subject: [PATCH] minor changes for amalgamation * de-duplicate `struct oid_to_pbes` * add makefile target * add amalgamation to release * fix `small` demo * add header guards for `tomcrypt_private.h` * update docs * add CI job with amalgamated library Signed-off-by: Steffen Jaeckel --- .github/workflows/main.yml | 21 +++++++++++++++++++++ .gitignore | 1 + doc/crypt.tex | 22 ++++++++++++++++++++++ makefile | 13 +++++++++++++ makefile_include.mk | 7 +++++++ src/headers/tomcrypt_private.h | 10 ++++++++++ src/misc/pbes/pbes1.c | 5 ----- src/misc/pbes/pbes2.c | 5 ----- 8 files changed, 74 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 78b27f3a..1d5bba05 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -98,3 +98,24 @@ jobs: name: build-${{ github.run_id }}.tar.xz path: build-${{ github.run_id }}.tar.xz retention-days: 1 + + Amalgam: + runs-on: ${{ matrix.os }} + strategy: + matrix: + cc: [ gcc, clang ] + os: [ ubuntu-22.04, ubuntu-24.04 ] + steps: + - uses: actions/checkout@v4 + - name: install dependencies + run: | + sudo apt-get update -qq + sudo apt-get remove -y libtommath1 + curl -s https://packagecloud.io/install/repositories/libtom/packages/script.deb.sh | sudo bash + sudo apt-get install libtommath-git-dev + - name: run tests + env: + CC: "${{ matrix.cc }}" + run: | + make pre_gen + make CFLAGS="-DLTM_DESC -DUSE_LTM" EXTRALIBS="-ltommath" AMALGAM=1 -j$(nproc) check diff --git a/.gitignore b/.gitignore index f9db9dc7..76f6737e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # release files /libtomcrypt-* /crypt-* +pre_gen/ # suppress output of build process gcc_[12].txt diff --git a/doc/crypt.tex b/doc/crypt.tex index ea0b250c..98332af2 100644 --- a/doc/crypt.tex +++ b/doc/crypt.tex @@ -8896,6 +8896,28 @@ else() endif() \end{verbatim} + +\mysection{Amalgamated library} + +LibTomCrypt can also be built as an amalgamated library, i.e. as a single C source file + the header files. + +A release of the library contains the amalgamation in the path \texttt{pre\_gen/tomcrypt\_amalgam.c}. + +To create the amalgamation one can run: + +\begin{verbatim} +make pre_gen +\end{verbatim} + +The makefiles also support building the amalgamated library via: + +\begin{verbatim} +make CFLAGS="-DLTM_DESC" EXTRALIBS=-ltommath AMALGAM=1 +\end{verbatim} + +This will build the library and link against LibTomMath (which must be installed on your system). + + \mysection{Header Configuration} The file \textit{tomcrypt\_cfg.h} is what lets you control various high level macros which control the behaviour of the library. Build options are also stored in \textit{tomcrypt\_custom.h} which allow the enabling and disabling of various algorithms. diff --git a/makefile b/makefile index ddddb6cc..a80cbd21 100644 --- a/makefile +++ b/makefile @@ -143,3 +143,16 @@ coverage: $(call print-help,coverage,Create code-coverage of the library - but b # cleans everything - coverage output and standard 'clean' cleancov: cleancov-clean clean +ifndef AMALGAM +AMALGAM_FILTER_OUT = src/ciphers/aes/aes_tab.c src/ciphers/aes/aes_enc.c src/ciphers/aes/aes_enc_desc.c +SOURCES = $(filter-out $(AMALGAM_FILTER_OUT),$(OBJECTS:.o=.c)) +pre_gen/tomcrypt_amalgam.c: $(SOURCES) + mkdir -p pre_gen + printf "/*\n * This file has been auto-generated, do not edit!\n */\n\n" > $@ + cat $(SOURCES) >> $@ + +pre_gen: pre_gen/tomcrypt_amalgam.c + +.PHONY: pre_gen +endif + diff --git a/makefile_include.mk b/makefile_include.mk index ba01bf45..ebacc481 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -210,6 +210,7 @@ library: $(call print-help,library,Builds the library) $(LIBNAME) # List of objects to compile (all goes to libtomcrypt.a) +ifndef AMALGAM OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_desc.o src/ciphers/aes/aes_enc.o \ src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/ciphers/blowfish.o \ src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \ @@ -401,6 +402,11 @@ src/stream/salsa20/xsalsa20_setup.o src/stream/salsa20/xsalsa20_test.o \ src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_stream_memory.o \ src/stream/sober128/sober128_test.o src/stream/sosemanuk/sosemanuk.o \ src/stream/sosemanuk/sosemanuk_memory.o src/stream/sosemanuk/sosemanuk_test.o +else +OBJECTS=pre_gen/tomcrypt_amalgam.o + +LTC_CFLAGS := $(LTC_CFLAGS) -Wno-shadow -Isrc/ciphers/aes -Isrc/ciphers/safer -Isrc/ciphers/twofish -Isrc/hashes/whirl -Isrc/stream/sober128 +endif # List of test objects to compile (all goes to libtomcrypt_prof.a) TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcrypt_test.o \ @@ -537,6 +543,7 @@ zipup: $(call print-help,zipup,Prepare the archives for a release) doc/crypt.pdf -@(find libtomcrypt-$(VERSION)/ -type f | xargs grep 'FIXM[E]') && echo '############## BEWARE: the "fixme" marker was found !!! ##############' || true mkdir -p libtomcrypt-$(VERSION)/doc cp doc/crypt.pdf libtomcrypt-$(VERSION)/doc/crypt.pdf + $(MAKE) -C libtomcrypt-$(VERSION)/ pre_gen tar -c libtomcrypt-$(VERSION)/ | xz -6e -c - > crypt-$(VERSION).tar.xz zip -9rq crypt-$(VERSION).zip libtomcrypt-$(VERSION) rm -rf libtomcrypt-$(VERSION) diff --git a/src/headers/tomcrypt_private.h b/src/headers/tomcrypt_private.h index 0452117a..78f3d08f 100644 --- a/src/headers/tomcrypt_private.h +++ b/src/headers/tomcrypt_private.h @@ -4,6 +4,9 @@ #include "tomcrypt.h" #include +#ifndef TOMCRYPT_PRIVATE_H_ +#define TOMCRYPT_PRIVATE_H_ + /* * Internal Macros */ @@ -105,6 +108,11 @@ typedef struct unsigned long key_bits; } pbes_arg; +typedef struct { + const pbes_properties *data; + const char *oid; +} oid_to_pbes; + /* * Internal functions */ @@ -692,3 +700,5 @@ int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng #define LTC_WIN32_BCRYPT #endif #endif + +#endif /* TOMCRYPT_PRIVATE_H_ */ diff --git a/src/misc/pbes/pbes1.c b/src/misc/pbes/pbes1.c index 24c50961..86daa47c 100644 --- a/src/misc/pbes/pbes1.c +++ b/src/misc/pbes/pbes1.c @@ -50,11 +50,6 @@ static const pbes_properties s_pbes1_types[] = { { s_pkcs_12_wrap, "sha1", "3des", 24, 8 }, }; -typedef struct { - const pbes_properties *data; - const char *oid; -} oid_to_pbes; - static const oid_to_pbes s_pbes1_list[] = { { &s_pbes1_types[0], "1.2.840.113549.1.5.1" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.1 pbeWithMD2AndDES-CBC */ { &s_pbes1_types[1], "1.2.840.113549.1.5.4" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.4 pbeWithMD2AndRC2-CBC */ diff --git a/src/misc/pbes/pbes2.c b/src/misc/pbes/pbes2.c index 5e672fcd..cfb3426e 100644 --- a/src/misc/pbes/pbes2.c +++ b/src/misc/pbes/pbes2.c @@ -39,11 +39,6 @@ static const pbes_properties s_pbes2_default_types[] = { { s_pkcs_5_alg2_wrap, "sha1", "aes", 32, 0 }, }; -typedef struct { - const pbes_properties *data; - const char* oid; -} oid_to_pbes; - static const oid_to_pbes s_pbes2_list[] = { { &s_pbes2_default_types[0], "1.3.14.3.2.7" }, /* http://www.oid-info.com/get/1.3.14.3.2.7 desCBC */ { &s_pbes2_default_types[1], "1.2.840.113549.3.2" }, /* http://www.oid-info.com/get/1.2.840.113549.3.2 rc2CBC */