Skip to content

Commit d032686

Browse files
authored
Merge pull request #596 from libtom/amalgamation
Amalgamation
2 parents ac2c11a + 92aef0a commit d032686

File tree

152 files changed

+1591
-1209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+1591
-1209
lines changed

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,24 @@ jobs:
9898
name: build-${{ github.run_id }}.tar.xz
9999
path: build-${{ github.run_id }}.tar.xz
100100
retention-days: 1
101+
102+
Amalgam:
103+
runs-on: ${{ matrix.os }}
104+
strategy:
105+
matrix:
106+
cc: [ gcc, clang ]
107+
os: [ ubuntu-22.04, ubuntu-24.04 ]
108+
steps:
109+
- uses: actions/checkout@v4
110+
- name: install dependencies
111+
run: |
112+
sudo apt-get update -qq
113+
sudo apt-get remove -y libtommath1
114+
curl -s https://packagecloud.io/install/repositories/libtom/packages/script.deb.sh | sudo bash
115+
sudo apt-get install libtommath-git-dev
116+
- name: run tests
117+
env:
118+
CC: "${{ matrix.cc }}"
119+
run: |
120+
make pre_gen
121+
make CFLAGS="-DLTM_DESC -DUSE_LTM" EXTRALIBS="-ltommath" AMALGAM=1 -j$(nproc) check

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# release files
1111
/libtomcrypt-*
1212
/crypt-*
13+
pre_gen/
1314

1415
# suppress output of build process
1516
gcc_[12].txt

demos/timing.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,12 @@ static void time_mult(void)
528528
if (ltc_mp.name == NULL) return;
529529

530530
fprintf(stderr, "Timing Multiplying:\n");
531-
mp_init_multi(&a,&b,&c,NULL);
532-
for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
533-
mp_rand(a, x);
534-
mp_rand(b, x);
531+
ltc_mp_init_multi(&a,&b,&c,NULL);
532+
for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) {
533+
ltc_mp_rand(a, x);
534+
ltc_mp_rand(b, x);
535535

536-
#define DO1 mp_mul(a, b, c);
536+
#define DO1 ltc_mp_mul(a, b, c);
537537
#define DO2 DO1; DO1;
538538

539539
t2 = -1;
@@ -544,9 +544,9 @@ static void time_mult(void)
544544
t1 = (t_read() - t1)>>1;
545545
if (t1 < t2) t2 = t1;
546546
}
547-
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
547+
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*LTC_MP_DIGIT_BIT, t2);
548548
}
549-
mp_clear_multi(a,b,c,NULL);
549+
ltc_mp_deinit_multi(a,b,c,NULL);
550550

551551
#undef DO1
552552
#undef DO2
@@ -561,11 +561,11 @@ static void time_sqr(void)
561561
if (ltc_mp.name == NULL) return;
562562

563563
fprintf(stderr, "Timing Squaring:\n");
564-
mp_init_multi(&a,&b,NULL);
565-
for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
566-
mp_rand(a, x);
564+
ltc_mp_init_multi(&a,&b,NULL);
565+
for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) {
566+
ltc_mp_rand(a, x);
567567

568-
#define DO1 mp_sqr(a, b);
568+
#define DO1 ltc_mp_sqr(a, b);
569569
#define DO2 DO1; DO1;
570570

571571
t2 = -1;
@@ -576,9 +576,9 @@ static void time_sqr(void)
576576
t1 = (t_read() - t1)>>1;
577577
if (t1 < t2) t2 = t1;
578578
}
579-
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
579+
fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*LTC_MP_DIGIT_BIT, t2);
580580
}
581-
mp_clear_multi(a,b,NULL);
581+
ltc_mp_deinit_multi(a,b,NULL);
582582

583583
#undef DO1
584584
#undef DO2

demos/tv_gen.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -670,31 +670,31 @@ static void ecc_gen(void)
670670
fprintf(out, "ecc vectors. These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
671671
G = ltc_ecc_new_point();
672672
R = ltc_ecc_new_point();
673-
mp_init(&k);
674-
mp_init(&order);
675-
mp_init(&modulus);
676-
mp_init(&a);
673+
ltc_mp_init(&k);
674+
ltc_mp_init(&order);
675+
ltc_mp_init(&modulus);
676+
ltc_mp_init(&a);
677677

678678
for (x = 0; ltc_ecc_curves[x].prime != NULL; x++) {
679679
fprintf(out, "%s\n", ltc_ecc_curves[x].OID);
680-
mp_set(k, 1);
680+
ltc_mp_set(k, 1);
681681

682-
mp_read_radix(order, ltc_ecc_curves[x].order, 16);
683-
mp_read_radix(modulus, ltc_ecc_curves[x].prime, 16);
684-
mp_read_radix(a, ltc_ecc_curves[x].A, 16);
685-
mp_read_radix(G->x, ltc_ecc_curves[x].Gx, 16);
686-
mp_read_radix(G->y, ltc_ecc_curves[x].Gy, 16);
687-
mp_set(G->z, 1);
682+
ltc_mp_read_radix(order, ltc_ecc_curves[x].order, 16);
683+
ltc_mp_read_radix(modulus, ltc_ecc_curves[x].prime, 16);
684+
ltc_mp_read_radix(a, ltc_ecc_curves[x].A, 16);
685+
ltc_mp_read_radix(G->x, ltc_ecc_curves[x].Gx, 16);
686+
ltc_mp_read_radix(G->y, ltc_ecc_curves[x].Gy, 16);
687+
ltc_mp_set(G->z, 1);
688688

689-
while (mp_cmp(k, order) == LTC_MP_LT) {
689+
while (ltc_mp_cmp(k, order) == LTC_MP_LT) {
690690
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
691-
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
692-
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
693-
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
694-
mp_mul_d(k, 3, k);
691+
ltc_mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
692+
ltc_mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
693+
ltc_mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
694+
ltc_mp_mul_d(k, 3, k);
695695
}
696696
}
697-
mp_clear_multi(k, order, modulus, a, LTC_NULL);
697+
ltc_mp_deinit_multi(k, order, modulus, a, LTC_NULL);
698698
ltc_ecc_del_point(G);
699699
ltc_ecc_del_point(R);
700700
fclose(out);

doc/crypt.tex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8896,6 +8896,28 @@ \subsection{Installation Directories}
88968896
endif()
88978897
\end{verbatim}
88988898

8899+
8900+
\mysection{Amalgamated library}
8901+
8902+
LibTomCrypt can also be built as an amalgamated library, i.e. as a single C source file + the header files.
8903+
8904+
A release of the library contains the amalgamation in the path \texttt{pre\_gen/tomcrypt\_amalgam.c}.
8905+
8906+
To create the amalgamation one can run:
8907+
8908+
\begin{verbatim}
8909+
make pre_gen
8910+
\end{verbatim}
8911+
8912+
The makefiles also support building the amalgamated library via:
8913+
8914+
\begin{verbatim}
8915+
make CFLAGS="-DLTM_DESC" EXTRALIBS=-ltommath AMALGAM=1
8916+
\end{verbatim}
8917+
8918+
This will build the library and link against LibTomMath (which must be installed on your system).
8919+
8920+
88998921
\mysection{Header Configuration}
89008922
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
89018923
stored in \textit{tomcrypt\_custom.h} which allow the enabling and disabling of various algorithms.

makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,22 @@ coverage: $(call print-help,coverage,Create code-coverage of the library - but b
143143

144144
# cleans everything - coverage output and standard 'clean'
145145
cleancov: cleancov-clean clean
146+
ifndef AMALGAM
147+
AMALGAM_FILTER_OUT = src/ciphers/aes/aes_enc.c src/ciphers/aes/aes_enc_desc.c
148+
TAB_SOURCES = src/ciphers/aes/aes_tab.c src/ciphers/safer/safer_tab.c src/hashes/whirl/whirltab.c src/stream/sober128/sober128tab.c
149+
SOURCES = $(filter-out $(AMALGAM_FILTER_OUT),$(OBJECTS:.o=.c))
150+
pre_gen/tomcrypt_amalgam.c: $(TAB_SOURCES) $(SOURCES)
151+
mkdir -p pre_gen
152+
printf "/*\n * This file has been auto-generated, do not edit!\n */\n\n" > $@
153+
printf "#define LTC_AES_TAB_C\n" >> $@
154+
printf "#define LTC_SAFER_TAB_C\n" >> $@
155+
printf "#define LTC_SOBER128TAB_C\n" >> $@
156+
printf "#define LTC_WHIRLTAB_C\n\n" >> $@
157+
printf "#include \"tomcrypt_private.h\"\n\n" >> $@
158+
cat $^ >> $@
159+
160+
pre_gen: pre_gen/tomcrypt_amalgam.c
161+
162+
.PHONY: pre_gen/tomcrypt_amalgam.c
163+
endif
164+

makefile_include.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ library: $(call print-help,library,Builds the library) $(LIBNAME)
210210

211211

212212
# List of objects to compile (all goes to libtomcrypt.a)
213+
ifndef AMALGAM
213214
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_desc.o src/ciphers/aes/aes_enc.o \
214215
src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/ciphers/blowfish.o \
215216
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 \
401402
src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_stream_memory.o \
402403
src/stream/sober128/sober128_test.o src/stream/sosemanuk/sosemanuk.o \
403404
src/stream/sosemanuk/sosemanuk_memory.o src/stream/sosemanuk/sosemanuk_test.o
405+
else
406+
OBJECTS=pre_gen/tomcrypt_amalgam.o
407+
408+
LTC_CFLAGS := $(LTC_CFLAGS) -Wno-shadow -Isrc/ciphers/aes -Isrc/ciphers/safer -Isrc/ciphers/twofish -Isrc/hashes/whirl -Isrc/stream/sober128
409+
endif
404410

405411
# List of test objects to compile (all goes to libtomcrypt_prof.a)
406412
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
537543
-@(find libtomcrypt-$(VERSION)/ -type f | xargs grep 'FIXM[E]') && echo '############## BEWARE: the "fixme" marker was found !!! ##############' || true
538544
mkdir -p libtomcrypt-$(VERSION)/doc
539545
cp doc/crypt.pdf libtomcrypt-$(VERSION)/doc/crypt.pdf
546+
$(MAKE) -C libtomcrypt-$(VERSION)/ pre_gen
540547
tar -c libtomcrypt-$(VERSION)/ | xz -6e -c - > crypt-$(VERSION).tar.xz
541548
zip -9rq crypt-$(VERSION).zip libtomcrypt-$(VERSION)
542549
rm -rf libtomcrypt-$(VERSION)

src/ciphers/aes/aes.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ const struct ltc_cipher_descriptor rijndael_enc_desc =
6262

6363
#endif
6464

65+
#ifndef LTC_AES_TAB_C
6566
#define LTC_AES_TAB_C
6667
#include "aes_tab.c"
68+
#endif
6769

6870
static ulong32 setup_mix(ulong32 temp)
6971
{
@@ -726,5 +728,11 @@ int ECB_KS(int *keysize)
726728
return CRYPT_OK;
727729
}
728730

729-
#endif
731+
#undef SETUP
732+
#undef ECB_ENC
733+
#undef ECB_DEC
734+
#undef ECB_DONE
735+
#undef ECB_TEST
736+
#undef ECB_KS
730737

738+
#endif

src/ciphers/anubis.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ const struct ltc_cipher_descriptor anubis_desc = {
2626

2727
#define MAX_N 10
2828

29+
30+
#define T0 anubis_T0
31+
#define T1 anubis_T1
32+
#define T2 anubis_T2
33+
#define T3 anubis_T3
34+
#define T4 anubis_T4
35+
#define T5 anubis_T5
36+
#define rc anubis_rc
2937
/*
3038
* Though Anubis is endianness-neutral, the encryption tables are listed
3139
* in BIG-ENDIAN format, which is adopted throughout this implementation
@@ -1546,5 +1554,14 @@ int anubis_keysize(int *keysize)
15461554
return CRYPT_OK;
15471555
}
15481556

1557+
#undef MAX_N
1558+
#undef T0
1559+
#undef T1
1560+
#undef T2
1561+
#undef T3
1562+
#undef T4
1563+
#undef T5
1564+
#undef rc
1565+
15491566
#endif
15501567

src/ciphers/blowfish.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,5 +654,7 @@ int blowfish_keysize(int *keysize)
654654
return CRYPT_OK;
655655
}
656656

657+
#undef F
658+
657659
#endif
658660

0 commit comments

Comments
 (0)