Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# release files
/libtomcrypt-*
/crypt-*
pre_gen/

# suppress output of build process
gcc_[12].txt
Expand Down
26 changes: 13 additions & 13 deletions demos/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,12 @@ static void time_mult(void)
if (ltc_mp.name == NULL) return;

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

#define DO1 mp_mul(a, b, c);
#define DO1 ltc_mp_mul(a, b, c);
#define DO2 DO1; DO1;

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

#undef DO1
#undef DO2
Expand All @@ -561,11 +561,11 @@ static void time_sqr(void)
if (ltc_mp.name == NULL) return;

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

#define DO1 mp_sqr(a, b);
#define DO1 ltc_mp_sqr(a, b);
#define DO2 DO1; DO1;

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

#undef DO1
#undef DO2
Expand Down
34 changes: 17 additions & 17 deletions demos/tv_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,31 +670,31 @@ static void ecc_gen(void)
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");
G = ltc_ecc_new_point();
R = ltc_ecc_new_point();
mp_init(&k);
mp_init(&order);
mp_init(&modulus);
mp_init(&a);
ltc_mp_init(&k);
ltc_mp_init(&order);
ltc_mp_init(&modulus);
ltc_mp_init(&a);

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

mp_read_radix(order, ltc_ecc_curves[x].order, 16);
mp_read_radix(modulus, ltc_ecc_curves[x].prime, 16);
mp_read_radix(a, ltc_ecc_curves[x].A, 16);
mp_read_radix(G->x, ltc_ecc_curves[x].Gx, 16);
mp_read_radix(G->y, ltc_ecc_curves[x].Gy, 16);
mp_set(G->z, 1);
ltc_mp_read_radix(order, ltc_ecc_curves[x].order, 16);
ltc_mp_read_radix(modulus, ltc_ecc_curves[x].prime, 16);
ltc_mp_read_radix(a, ltc_ecc_curves[x].A, 16);
ltc_mp_read_radix(G->x, ltc_ecc_curves[x].Gx, 16);
ltc_mp_read_radix(G->y, ltc_ecc_curves[x].Gy, 16);
ltc_mp_set(G->z, 1);

while (mp_cmp(k, order) == LTC_MP_LT) {
while (ltc_mp_cmp(k, order) == LTC_MP_LT) {
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
mp_mul_d(k, 3, k);
ltc_mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
ltc_mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
ltc_mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
ltc_mp_mul_d(k, 3, k);
}
}
mp_clear_multi(k, order, modulus, a, LTC_NULL);
ltc_mp_deinit_multi(k, order, modulus, a, LTC_NULL);
ltc_ecc_del_point(G);
ltc_ecc_del_point(R);
fclose(out);
Expand Down
22 changes: 22 additions & 0 deletions doc/crypt.tex
Original file line number Diff line number Diff line change
Expand Up @@ -8896,6 +8896,28 @@ \subsection{Installation Directories}
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.
Expand Down
19 changes: 19 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,22 @@ 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_enc.c src/ciphers/aes/aes_enc_desc.c
TAB_SOURCES = src/ciphers/aes/aes_tab.c src/ciphers/safer/safer_tab.c src/hashes/whirl/whirltab.c src/stream/sober128/sober128tab.c
SOURCES = $(filter-out $(AMALGAM_FILTER_OUT),$(OBJECTS:.o=.c))
pre_gen/tomcrypt_amalgam.c: $(TAB_SOURCES) $(SOURCES)
mkdir -p pre_gen
printf "/*\n * This file has been auto-generated, do not edit!\n */\n\n" > $@
printf "#define LTC_AES_TAB_C\n" >> $@
printf "#define LTC_SAFER_TAB_C\n" >> $@
printf "#define LTC_SOBER128TAB_C\n" >> $@
printf "#define LTC_WHIRLTAB_C\n\n" >> $@
printf "#include \"tomcrypt_private.h\"\n\n" >> $@
cat $^ >> $@

pre_gen: pre_gen/tomcrypt_amalgam.c

.PHONY: pre_gen/tomcrypt_amalgam.c
endif

7 changes: 7 additions & 0 deletions makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/ciphers/aes/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ const struct ltc_cipher_descriptor rijndael_enc_desc =

#endif

#ifndef LTC_AES_TAB_C
#define LTC_AES_TAB_C
#include "aes_tab.c"
#endif

static ulong32 setup_mix(ulong32 temp)
{
Expand Down Expand Up @@ -726,5 +728,11 @@ int ECB_KS(int *keysize)
return CRYPT_OK;
}

#endif
#undef SETUP
#undef ECB_ENC
#undef ECB_DEC
#undef ECB_DONE
#undef ECB_TEST
#undef ECB_KS

#endif
17 changes: 17 additions & 0 deletions src/ciphers/anubis.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const struct ltc_cipher_descriptor anubis_desc = {

#define MAX_N 10


#define T0 anubis_T0
#define T1 anubis_T1
#define T2 anubis_T2
#define T3 anubis_T3
#define T4 anubis_T4
#define T5 anubis_T5
#define rc anubis_rc
/*
* Though Anubis is endianness-neutral, the encryption tables are listed
* in BIG-ENDIAN format, which is adopted throughout this implementation
Expand Down Expand Up @@ -1546,5 +1554,14 @@ int anubis_keysize(int *keysize)
return CRYPT_OK;
}

#undef MAX_N
#undef T0
#undef T1
#undef T2
#undef T3
#undef T4
#undef T5
#undef rc

#endif

2 changes: 2 additions & 0 deletions src/ciphers/blowfish.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,5 +654,7 @@ int blowfish_keysize(int *keysize)
return CRYPT_OK;
}

#undef F

#endif

2 changes: 2 additions & 0 deletions src/ciphers/camellia.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,6 @@ int camellia_keysize(int *keysize)
return CRYPT_OK;
}

#undef loc

#endif
4 changes: 4 additions & 0 deletions src/ciphers/cast5.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
}
#endif

#define FI cast5_FI
LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
{
ulong32 I;
Expand Down Expand Up @@ -702,4 +703,7 @@ int cast5_keysize(int *keysize)
return CRYPT_OK;
}

#undef GB
#undef FI

#endif
3 changes: 3 additions & 0 deletions src/ciphers/des.c
Original file line number Diff line number Diff line change
Expand Up @@ -2238,5 +2238,8 @@ int des3_keysize(int *keysize)
return CRYPT_OK;
}

#undef EN0
#undef DE1

#endif

8 changes: 8 additions & 0 deletions src/ciphers/idea.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static ushort16 s_add_inv(ushort16 x)
return LOW16(0 - x);
}

#define s_setup_key s_idea_setup_key
static int s_setup_key(const unsigned char *key, symmetric_key *skey)
{
int i, j;
Expand Down Expand Up @@ -247,4 +248,11 @@ int idea_test(void)
#endif
}

#undef LOW16
#undef HIGH16
#undef MUL
#undef STORE16
#undef LOAD16
#undef s_setup_key

#endif
4 changes: 4 additions & 0 deletions src/ciphers/kasumi.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const struct ltc_cipher_descriptor kasumi_desc = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};

#define FI kasumi_FI
static u16 FI( u16 in, u16 subkey )
{
u16 nine, seven;
Expand Down Expand Up @@ -304,4 +305,7 @@ int kasumi_test(void)
#endif
}

#undef ROL16
#undef FI

#endif
2 changes: 2 additions & 0 deletions src/ciphers/khazad.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,4 +837,6 @@ int khazad_keysize(int *keysize)
return CRYPT_INVALID_KEYSIZE;
}

#undef R

#endif
3 changes: 3 additions & 0 deletions src/ciphers/kseed.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,7 @@ int kseed_keysize(int *keysize)
return CRYPT_OK;
}

#undef G
#undef F

#endif
7 changes: 7 additions & 0 deletions src/ciphers/noekeon.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,5 +313,12 @@ int noekeon_keysize(int *keysize)
return CRYPT_OK;
}

#undef kTHETA
#undef THETA
#undef GAMMA
#undef PI1
#undef PI2
#undef ROUND

#endif

3 changes: 3 additions & 0 deletions src/ciphers/rc5.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const struct ltc_cipher_descriptor rc5_desc =
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};

#define stab rc5_stab
static const ulong32 stab[50] = {
0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL,
0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL,
Expand Down Expand Up @@ -317,6 +318,8 @@ int rc5_keysize(int *keysize)
return CRYPT_OK;
}

#undef stab

#endif


Expand Down
Loading