Skip to content

Commit efad039

Browse files
authored
Merge pull request #751 from libtom/more-fixes-and-improvements
More fixes and improvements
2 parents 45f9576 + a724483 commit efad039

56 files changed

Lines changed: 394 additions & 324 deletions

Some content is hidden

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

.ci/build_options.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
if [ "$#" != "5" ]; then
4+
echo "Usage is: ${0} \"build_options\" \"<prepend CFLAGS>\" \"<makefile>\" \"<append CFLAGS>\" <math library to link to>"
5+
echo "CC=gcc ${0} \"build_options\" \" \" \"makefile\" \"-DUSE_LTM -DLTM_DESC -I../libtommath\" ../libtommath/libtommath.a"
6+
exit -1
7+
fi
8+
9+
# output version
10+
bash .ci/printinfo.sh
11+
12+
set -e
13+
14+
options=(
15+
-DLTC_EASY
16+
-DLTC_FORTUNA_RESEED_RATELIMIT_STATIC
17+
-DLTC_FORTUNA_USE_ENCRYPT_ONLY
18+
-DLTC_MECC_FP
19+
-DLTC_NO_TABLES
20+
-DLTC_NO_FAST
21+
-DLTC_NO_ASM
22+
-DLTC_NO_DEPRECATED_APIS
23+
-DLTC_NO_ECC_TIMING_RESISTANT
24+
-DLTC_NO_RSA_BLINDING
25+
-DLTC_PTHREAD
26+
-DLTC_SMALL_CODE
27+
-DLTC_SMALL_STACK
28+
)
29+
30+
make clean V=0
31+
make pre_gen
32+
for opt in ${options[@]}; do
33+
echo "Build: $opt"
34+
CFLAGS="$2 $CFLAGS $4 $opt" EXTRALIBS="$5" make -j$(nproc) -f $3 AMALGAM=1 all 1>>gcc_1.txt 2>>gcc_2.txt
35+
./small
36+
make clean V=0
37+
done
38+
39+
# we don't want LTC_EASY when running the tests now
40+
unset 'options[0]'
41+
42+
echo "All: ${options[@]}"
43+
CFLAGS="$2 $CFLAGS $4 ${options[@]}" EXTRALIBS="$5" make -j$(nproc) -f $3 AMALGAM=1 all 1>>gcc_1.txt 2>>gcc_2.txt
44+
./test >test_std.txt 2>test_err.txt
45+
46+
exit 0

.ci/coverage_more.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,17 @@ else
3232
echo "hashsum okay"
3333
fi
3434

35+
pdiv "aesgcm"
36+
./hashsum -a sha256 aesgcm > aesgcm.sha256sum
37+
./aesgcm -e aesgcm aesgcm.enc0 "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF"
38+
./aesgcm -d aesgcm.enc0 aesgcm.dec0 "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF"
39+
./aesgcm -e aesgcm aesgcm.enc1 "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABB"
40+
./aesgcm -d aesgcm.enc1 aesgcm.dec1 "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABB"
41+
for i in {0..1}; do
42+
rm aesgcm
43+
mv aesgcm.dec${i} aesgcm
44+
./hashsum -c aesgcm.sha256sum
45+
done
46+
chmod +x aesgcm
3547

3648
exit 0

.github/workflows/main.yml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,8 @@ jobs:
4545
- { BUILDNAME: 'META_BUILDS', BUILDOPTIONS: '-DGMP_DESC', BUILDSCRIPT: '.ci/meta_builds.sh' }
4646
- { BUILDNAME: 'VALGRIND', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/valgrind.sh' }
4747
- { BUILDNAME: 'STOCK', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/run.sh' }
48+
- { BUILDNAME: 'BUILD_OPTIONS', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/build_options.sh' }
4849
- { BUILDNAME: 'STOCK-MPI', BUILDOPTIONS: '-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM', BUILDSCRIPT: '.ci/run.sh' }
49-
- { BUILDNAME: 'EASY', BUILDOPTIONS: '-DLTC_EASY', BUILDSCRIPT: '.ci/run.sh' }
50-
- { BUILDNAME: 'SMALL_CODE', BUILDOPTIONS: '-DLTC_SMALL_CODE', BUILDSCRIPT: '.ci/run.sh' }
51-
- { BUILDNAME: 'SMALL_STACK', BUILDOPTIONS: '-DLTC_SMALL_STACK', BUILDSCRIPT: '.ci/run.sh' }
52-
- { BUILDNAME: 'SMALL', BUILDOPTIONS: '-DLTC_SMALL_CODE -DLTC_SMALL_STACK', BUILDSCRIPT: '.ci/run.sh' }
53-
- { BUILDNAME: 'NO_TABLES', BUILDOPTIONS: '-DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
54-
- { BUILDNAME: 'NO_FAST', BUILDOPTIONS: '-DLTC_NO_FAST', BUILDSCRIPT: '.ci/run.sh' }
55-
- { BUILDNAME: 'NO_FAST+SMALL+NO_TABLES', BUILDOPTIONS: '-DLTC_NO_FAST -DLTC_SMALL_CODE -DLTC_SMALL_STACK -DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
56-
- { BUILDNAME: 'NO_ASM', BUILDOPTIONS: '-DLTC_NO_ASM', BUILDSCRIPT: '.ci/run.sh' }
57-
- { BUILDNAME: 'NO_DEPRECATED_APIS', BUILDOPTIONS: '-DLTC_NO_DEPRECATED_APIS', BUILDSCRIPT: '.ci/run.sh' }
58-
- { BUILDNAME: 'NO_TIMING_RESISTANCE', BUILDOPTIONS: '-DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING', BUILDSCRIPT: '.ci/run.sh' }
59-
- { BUILDNAME: 'FORTUNA_CUSTOM_OPTIONS', BUILDOPTIONS: '-DLTC_FORTUNA_USE_ENCRYPT_ONLY -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC', BUILDSCRIPT: '.ci/run.sh' }
60-
- { BUILDNAME: 'ECC_FP', BUILDOPTIONS: '-DLTC_MECC_FP', BUILDSCRIPT: '.ci/run.sh' }
61-
- { BUILDNAME: 'ECC_FP+PTHREAD', BUILDOPTIONS: '-DLTC_MECC_FP -DLTC_PTHREAD', BUILDSCRIPT: '.ci/run.sh' }
6250
- { BUILDNAME: 'STOCK+ARGTYPE=1', BUILDOPTIONS: '-DARGTYPE=1', BUILDSCRIPT: '.ci/run.sh' }
6351
- { BUILDNAME: 'STOCK+ARGTYPE=2', BUILDOPTIONS: '-DARGTYPE=2', BUILDSCRIPT: '.ci/run.sh' }
6452
- { BUILDNAME: 'STOCK+ARGTYPE=3', BUILDOPTIONS: '-DARGTYPE=3', BUILDSCRIPT: '.ci/run.sh' }

demos/constants.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
/* SPDX-License-Identifier: Unlicense */
33
#include "tomcrypt.h"
44

5-
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
6-
#include <libgen.h>
7-
#else
8-
#define basename(x) x
9-
#endif
5+
#include <string.h>
6+
#define basename(path) ( strrchr((path), '/') ? strrchr((path), '/') + 1 : strrchr((path), '\\') ? strrchr((path), '\\') + 1 : (path) )
107

118
/**
129
@file demo_crypt_constants.c

demos/hashsum.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212

1313
#include <tomcrypt.h>
1414

15-
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
16-
#include <libgen.h>
17-
#else
18-
#define basename(x) x
19-
#endif
15+
#include <string.h>
16+
#define basename(path) ( strrchr((path), '/') ? strrchr((path), '/') + 1 : strrchr((path), '\\') ? strrchr((path), '\\') + 1 : (path) )
2017

2118
#if !defined(PATH_MAX) && defined(_MSC_VER)
2219
#include <windows.h>

demos/sizes.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
#include "tomcrypt.h"
55

6-
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
7-
#include <libgen.h>
8-
#else
9-
#define basename(x) x
10-
#endif
6+
#include <string.h>
7+
#define basename(path) ( strrchr((path), '/') ? strrchr((path), '/') + 1 : strrchr((path), '\\') ? strrchr((path), '\\') + 1 : (path) )
8+
119
/**
1210
@file demo_crypt_sizes.c
1311

demos/timing.c

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#define PRI64 "ll"
99
#endif
1010

11-
static prng_state yarrow_prng;
11+
static prng_state timing_prng;
12+
static const char *timing_prng_name;
13+
static int timing_prng_id;
1214

1315
/* timing */
1416
#define KTIMES 25
@@ -166,7 +168,7 @@ static void time_keysched(void)
166168
kl = cipher_descriptor[x].min_key_length;
167169
c1 = (ulong64)-1;
168170
for (y1 = 0; y1 < KTIMES; y1++) {
169-
yarrow_read(key, kl, &yarrow_prng);
171+
prng_descriptor[timing_prng_id].read(key, kl, &timing_prng);
170172
t_start();
171173
DO1(key);
172174
t1 = t_read();
@@ -692,11 +694,11 @@ static const struct {
692694
for (y = 0; y < 4; y++) {
693695
t_start();
694696
t1 = t_read();
695-
if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
697+
if ((err = dsa_generate_pqg(&timing_prng, timing_prng_id, groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
696698
fprintf(stderr, "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
697699
exit(EXIT_FAILURE);
698700
}
699-
if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
701+
if ((err = dsa_generate_key(&timing_prng, timing_prng_id, &key)) != CRYPT_OK) {
700702
fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
701703
exit(EXIT_FAILURE);
702704
}
@@ -734,8 +736,8 @@ static void time_rsa(void)
734736
ltc_rsa_op_parameters rsa_params = {
735737
.u.crypt.lparam = (const unsigned char *)"testprog",
736738
.u.crypt.lparamlen = 8,
737-
.prng = &yarrow_prng,
738-
.wprng = find_prng("yarrow"),
739+
.prng = &timing_prng,
740+
.wprng = timing_prng_id,
739741
.params.hash_idx = find_hash("sha1"),
740742
.params.mgf1_hash_idx = find_hash("sha1"),
741743
.params.saltlen = 8,
@@ -749,7 +751,7 @@ static void time_rsa(void)
749751
for (y = 0; y < 4; y++) {
750752
t_start();
751753
t1 = t_read();
752-
if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
754+
if ((err = rsa_make_key(&timing_prng, timing_prng_id, x/8, 65537, &key)) != CRYPT_OK) {
753755
fprintf(stderr, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
754756
exit(EXIT_FAILURE);
755757
}
@@ -882,7 +884,7 @@ static void time_dh(void)
882884

883885
t_start();
884886
t1 = t_read();
885-
if ((err = dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
887+
if ((err = dh_generate_key(&timing_prng, timing_prng_id, &key)) != CRYPT_OK) {
886888
fprintf(stderr, "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
887889
exit(EXIT_FAILURE);
888890
}
@@ -907,7 +909,7 @@ static void time_ecc(void)
907909
ulong64 t1, t2;
908910
unsigned char buf[2][256] = { 0 };
909911
unsigned long i, w, x, y, z;
910-
int err, stat;
912+
int err, stat, hashidx;
911913
const unsigned long sizes[] = {
912914
#ifdef LTC_ECC_SECP112R1
913915
112/8,
@@ -934,29 +936,48 @@ static void time_ecc(void)
934936
521/8,
935937
#endif
936938
100000};
939+
prng_state ecc_prng;
937940
ltc_ecc_sig_opts sig_opts = {
938941
.type = LTC_ECCSIG_RFC7518,
939-
.prng = &yarrow_prng,
940-
.wprng = find_prng ("yarrow")
942+
.prng = &ecc_prng,
943+
.wprng = timing_prng_id
941944
};
945+
const unsigned char prng_entropy[] = {
946+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
947+
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
948+
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
949+
0x1f, 0x20, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
950+
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
951+
0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
952+
0x1d, 0x1e, 0x1f, 0x20
953+
};
954+
if ((err = prng_descriptor[timing_prng_id].pimport(prng_entropy, sizeof(prng_entropy), &ecc_prng)) != CRYPT_OK) {
955+
fprintf(stderr, "\n\nprng.import() says %s!\n", error_to_string(err));
956+
exit(EXIT_FAILURE);
957+
}
958+
if ((err = prng_descriptor[timing_prng_id].ready(&ecc_prng)) != CRYPT_OK) {
959+
fprintf(stderr, "\n\nprng.ready() says %s!\n", error_to_string(err));
960+
exit(EXIT_FAILURE);
961+
}
942962

943963
if (ltc_mp.name == NULL) return;
964+
hashidx = find_hash("sha1");
944965

945966
for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
946967
t2 = 0;
947968
for (y = 0; y < 256; y++) {
948969
t_start();
949970
t1 = t_read();
950-
if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
971+
if ((err = ecc_make_key(sig_opts.prng, sig_opts.wprng, x, &key)) != CRYPT_OK) {
951972
fprintf(stderr, "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
952973
exit(EXIT_FAILURE);
953974
}
954975
t1 = t_read() - t1;
955976
t2 += t1;
956977

957978
#ifdef LTC_PROFILE
958-
t2 <<= 8;
959-
break;
979+
t2 <<= 8;
980+
break;
960981
#endif
961982

962983
if (y < 255) {
@@ -971,21 +992,22 @@ static void time_ecc(void)
971992
t_start();
972993
t1 = t_read();
973994
z = sizeof(buf[1]);
974-
if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
995+
if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, sig_opts.prng, sig_opts.wprng, hashidx,
975996
&key)) != CRYPT_OK) {
976997
fprintf(stderr, "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
977998
exit(EXIT_FAILURE);
978999
}
9791000
t1 = t_read() - t1;
9801001
t2 += t1;
9811002
#ifdef LTC_PROFILE
982-
t2 <<= 8;
983-
break;
1003+
t2 <<= 8;
1004+
break;
9841005
#endif
9851006
}
9861007
t2 >>= 8;
9871008
fprintf(stderr, "ECC-%lu encrypt_key took %15"PRI64"u cycles\n", x*8, t2);
9881009

1010+
9891011
t2 = 0;
9901012
for (y = 0; y < 256; y++) {
9911013
t_start();
@@ -998,8 +1020,8 @@ static void time_ecc(void)
9981020
t1 = t_read() - t1;
9991021
t2 += t1;
10001022
#ifdef LTC_PROFILE
1001-
t2 <<= 8;
1002-
break;
1023+
t2 <<= 8;
1024+
break;
10031025
#endif
10041026
}
10051027
t2 >>= 8;
@@ -1017,8 +1039,8 @@ static void time_ecc(void)
10171039
t1 = t_read() - t1;
10181040
t2 += t1;
10191041
#ifdef LTC_PROFILE
1020-
t2 <<= 8;
1021-
break;
1042+
t2 <<= 8;
1043+
break;
10221044
#endif
10231045
}
10241046
t2 >>= 8;
@@ -1039,8 +1061,8 @@ static void time_ecc(void)
10391061
t1 = t_read() - t1;
10401062
t2 += t1;
10411063
#ifdef LTC_PROFILE
1042-
t2 <<= 8;
1043-
break;
1064+
t2 <<= 8;
1065+
break;
10441066
#endif
10451067
}
10461068
t2 >>= 8;
@@ -1245,8 +1267,8 @@ static void time_macs_(unsigned long MAC_SIZE)
12451267
exit(EXIT_FAILURE);
12461268
}
12471269

1248-
yarrow_read(ctx.buf, ctx.size, &yarrow_prng);
1249-
yarrow_read(ctx.key, 16, &yarrow_prng);
1270+
prng_descriptor[timing_prng_id].read(ctx.buf, ctx.size, &timing_prng);
1271+
prng_descriptor[timing_prng_id].read(ctx.key, sizeof(ctx.key), &timing_prng);
12501272

12511273
for (n = 0; n < LTC_ARRAY_SIZE(time_funs); ++n) {
12521274
if (!should_skip(time_funs[n].name))
@@ -1498,9 +1520,9 @@ static void time_eacs_(unsigned long MAC_SIZE)
14981520

14991521
ctx.cipher_idx = find_cipher("aes");
15001522

1501-
yarrow_read(ctx.buf, ctx.size, &yarrow_prng);
1502-
yarrow_read(ctx.key, sizeof(ctx.key), &yarrow_prng);
1503-
yarrow_read(ctx.IV, sizeof(ctx.IV), &yarrow_prng);
1523+
prng_descriptor[timing_prng_id].read(ctx.buf, ctx.size, &timing_prng);
1524+
prng_descriptor[timing_prng_id].read(ctx.key, sizeof(ctx.key), &timing_prng);
1525+
prng_descriptor[timing_prng_id].read(ctx.IV, sizeof(ctx.IV), &timing_prng);
15041526

15051527
for (n = 0; n < LTC_ARRAY_SIZE(time_funs); ++n) {
15061528
if (!should_skip(time_funs[n].name))
@@ -1605,8 +1627,12 @@ register_all_prngs();
16051627
} else if (argc > 3){
16061628
filter_arg = argv[3];
16071629
}
1608-
1609-
if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
1630+
if (find_prng("sober128") != -1)
1631+
timing_prng_name = "sober128";
1632+
else
1633+
timing_prng_name = "yarrow";
1634+
timing_prng_id = find_prng(timing_prng_name);
1635+
if ((err = rng_make_prng(128, timing_prng_id, &timing_prng, NULL)) != CRYPT_OK) {
16101636
fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
16111637
exit(EXIT_FAILURE);
16121638
}

doc/makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ termdoc: docdvi
5959
dvi2tty crypt.dvi -w120
6060

6161
clean:
62-
rm -f $(LEFTOVERS)
63-
rm -rf doxygen/
62+
${silent} rm -f $(LEFTOVERS)
63+
${silent} rm -rf doxygen/

helper.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ sub check_source {
7171
my $n = $1;
7272
push @{$troubles->{invalid_macro_name}}, "$lineno($n)"
7373
unless ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '__has_builtin') ||
74+
($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '__has_attribute') ||
7475
($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '_WIN32_WINNT') ||
7576
($file eq 'src/prngs/rng_get_bytes.c' && $n eq '_WIN32_WINNT');
7677
}

makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ pre_gen/tomcrypt_amalgam.c: $(TAB_SOURCES) $(SOURCES)
156156
printf "#define LTC_WHIRLTAB_C\n\n" >> $@
157157
printf "#include \"tomcrypt_private.h\"\n\n" >> $@
158158
cat $^ >> $@
159+
printf "#define ENCRYPT_ONLY\n\n" >> $@
160+
cat src/ciphers/aes/aes.c >> $@
161+
cat src/ciphers/aes/aes_desc.c >> $@
162+
printf "#undef ENCRYPT_ONLY\n\n" >> $@
159163

160164
pre_gen: pre_gen/tomcrypt_amalgam.c
161165

0 commit comments

Comments
 (0)