Skip to content

Commit 5edb54e

Browse files
authored
Merge pull request #696 from libtom/some-improvements
Some improvements
2 parents 563f0fb + ae50082 commit 5edb54e

File tree

113 files changed

+590
-424
lines changed

Some content is hidden

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

113 files changed

+590
-424
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ on:
55
branches:
66
- master
77
- develop
8-
- /^release\/.*$/
8+
- 'release/**'
9+
- 'support/**'
10+
- 'ci/**'
911
pull_request:
1012
branches:
1113
- master
1214
- develop
13-
- /^release\/.*$/
15+
- 'release/**'
16+
- 'support/**'
17+
- 'ci/**'
1418

1519
concurrency:
1620
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}

demos/gcm-file/gcm_filehandle.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int gcm_filehandle( int cipher,
116116
if (direction == GCM_DECRYPT) {
117117
tot_data -= taglen;
118118
}
119-
rewind(in);
119+
fseek(in, 0, SEEK_SET);
120120
do {
121121
x = MIN(tot_data, LTC_FILE_READ_BUFSIZE);
122122
x = fread(buf, 1, x, in);
@@ -140,6 +140,10 @@ int gcm_filehandle( int cipher,
140140
}
141141

142142
if (direction == GCM_DECRYPT) {
143+
if (feof(in) || ferror(in)) {
144+
err = CRYPT_ERROR;
145+
goto LBL_CLEANBUF;
146+
}
143147
x = fread(buf, 1, taglen, in);
144148
if (x != taglen) {
145149
err = CRYPT_ERROR;
@@ -170,7 +174,7 @@ int gcm_filehandle( int cipher,
170174
#endif
171175
if(*res == 0) {
172176
x = ftell(out);
173-
rewind(out);
177+
fseek(in, 0, SEEK_SET);
174178
while((size_t)ftell(out) < x) {
175179
fwrite(buf, 1, LTC_FILE_READ_BUFSIZE, out);
176180
}

demos/hashsum.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
'\255')
3131
#define HEXOF(x) (x - s_base(x))
3232

33+
#ifndef LTC_ARRAY_SIZE
34+
#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
35+
#endif
36+
3337
static char* hashsum;
3438

3539
static void cleanup(void)
@@ -190,7 +194,7 @@ int main(int argc, char **argv)
190194
die(EXIT_FAILURE);
191195
}
192196

193-
for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
197+
for (x = 0; x < LTC_ARRAY_SIZE(idxs); ++x) {
194198
idxs[x] = -2;
195199
}
196200
argn = 1;
@@ -243,7 +247,7 @@ int main(int argc, char **argv)
243247
die(EXIT_FAILURE);
244248
}
245249
idx++;
246-
if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
250+
if ((size_t)idx >= LTC_ARRAY_SIZE(idxs)) {
247251
fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
248252
die(EXIT_FAILURE);
249253
}

demos/pem-info.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static const struct {
3434
static const char *s_map_cipher(const char *name)
3535
{
3636
unsigned long n;
37-
for (n = 0; n < sizeof(cipher_name_map)/sizeof(cipher_name_map[0]); ++n) {
37+
for (n = 0; n < LTC_ARRAY_SIZE(cipher_name_map); ++n) {
3838
if (strcmp(name, cipher_name_map[n].is) == 0)
3939
return cipher_name_map[n].should;
4040
}
@@ -61,7 +61,7 @@ static const char *s_map_mode(enum cipher_mode mode)
6161
{
6262
size_t n;
6363
mode &= cm_modes | cm_1bit | cm_8bit;
64-
for (n = 0; n < sizeof(cipher_mode_map)/sizeof(cipher_mode_map[0]); ++n) {
64+
for (n = 0; n < LTC_ARRAY_SIZE(cipher_mode_map); ++n) {
6565
if (cipher_mode_map[n].mode == mode)
6666
return cipher_mode_map[n].name;
6767
}

demos/timing.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static const struct {
658658

659659
if (ltc_mp.name == NULL) return;
660660

661-
for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
661+
for (x = 0; x < LTC_ARRAY_SIZE(groups); x++) {
662662
t2 = 0;
663663
for (y = 0; y < 4; y++) {
664664
t_start();
@@ -1408,7 +1408,7 @@ if (argc > 1) {
14081408
if (strstr(argv[1], "-h")) {
14091409
die(EXIT_SUCCESS);
14101410
} else if (strstr(argv[1], "-l")) {
1411-
for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
1411+
for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
14121412
printf("%s\n", test_functions[i].name);
14131413
}
14141414
exit(0);
@@ -1446,7 +1446,7 @@ if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT
14461446
/* single test name from commandline */
14471447
if (argc > 1) single_test = argv[1];
14481448

1449-
for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
1449+
for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
14501450
if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
14511451
continue;
14521452
}

demos/tv_gen.c

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

5+
#define OPEN_FILE(f, o) do { \
6+
o = fopen(f, "w"); \
7+
if (o == NULL) { \
8+
perror("can't open " f); \
9+
return; \
10+
} \
11+
} while(0)
12+
513
static void hash_gen(void)
614
{
715
unsigned char md[MAXBLOCKSIZE], *buf;
816
unsigned long outlen, x, y, z;
917
FILE *out;
1018
int err;
1119

12-
out = fopen("hash_tv.txt", "w");
13-
if (out == NULL) {
14-
perror("can't open hash_tv.txt");
15-
return;
16-
}
20+
OPEN_FILE("hash_tv.txt", out);
1721

1822
fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
1923
for (x = 0; hash_descriptor[x].name != NULL; x++) {
@@ -52,7 +56,7 @@ static void cipher_gen(void)
5256
FILE *out;
5357
symmetric_key skey;
5458

55-
out = fopen("cipher_tv.txt", "w");
59+
OPEN_FILE("cipher_tv.txt", out);
5660

5761
fprintf(out,
5862
"Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n"
@@ -126,7 +130,7 @@ static void hmac_gen(void)
126130
FILE *out;
127131
unsigned long len;
128132

129-
out = fopen("hmac_tv.txt", "w");
133+
OPEN_FILE("hmac_tv.txt", out);
130134

131135
fprintf(out,
132136
"HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
@@ -179,7 +183,7 @@ static void omac_gen(void)
179183
FILE *out;
180184
unsigned long len;
181185

182-
out = fopen("omac_tv.txt", "w");
186+
OPEN_FILE("omac_tv.txt", out);
183187

184188
fprintf(out,
185189
"OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
@@ -240,7 +244,7 @@ static void pmac_gen(void)
240244
FILE *out;
241245
unsigned long len;
242246

243-
out = fopen("pmac_tv.txt", "w");
247+
OPEN_FILE("pmac_tv.txt", out);
244248

245249
fprintf(out,
246250
"PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed. The initial key is\n"
@@ -302,7 +306,7 @@ static void eax_gen(void)
302306
plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
303307
unsigned long len;
304308

305-
out = fopen("eax_tv.txt", "w");
309+
OPEN_FILE("eax_tv.txt", out);
306310
fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
307311
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
308312
"step repeated sufficiently.\n\n");
@@ -368,7 +372,7 @@ static void ocb_gen(void)
368372
plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
369373
unsigned long len;
370374

371-
out = fopen("ocb_tv.txt", "w");
375+
OPEN_FILE("ocb_tv.txt", out);
372376
fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
373377
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
374378
"step repeated sufficiently. The nonce is fixed throughout.\n\n");
@@ -437,7 +441,7 @@ static void ocb3_gen(void)
437441
plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
438442
unsigned long len;
439443

440-
out = fopen("ocb3_tv.txt", "w");
444+
OPEN_FILE("ocb3_tv.txt", out);
441445
fprintf(out, "OCB3 Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
442446
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
443447
"step repeated sufficiently. The nonce is fixed throughout. AAD is fixed to 3 bytes (ASCII) 'AAD'.\n\n");
@@ -509,7 +513,7 @@ static void ccm_gen(void)
509513
unsigned long len;
510514
const unsigned int taglen[] = {4, 6, 8, 10, 12, 14, 16};
511515

512-
out = fopen("ccm_tv.txt", "w");
516+
OPEN_FILE("ccm_tv.txt", out);
513517
fprintf(out, "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
514518
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
515519
"step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
@@ -535,7 +539,7 @@ static void ccm_gen(void)
535539
nonce[z] = z;
536540
}
537541

538-
for (t = 0; t < sizeof(taglen)/sizeof(taglen[0]); ++t) {
542+
for (t = 0; t < LTC_ARRAY_SIZE(taglen); ++t) {
539543
for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
540544
for (z = 0; z < y1; z++) {
541545
plaintext[z] = (unsigned char)(z & 255);
@@ -579,7 +583,7 @@ static void gcm_gen(void)
579583
unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
580584
unsigned long len;
581585

582-
out = fopen("gcm_tv.txt", "w");
586+
OPEN_FILE("gcm_tv.txt", out);
583587
fprintf(out, "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
584588
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
585589
"step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n");
@@ -641,7 +645,7 @@ static void base64_gen(void)
641645
char dst[256];
642646
unsigned long x, len;
643647

644-
out = fopen("base64_tv.txt", "w");
648+
OPEN_FILE("base64_tv.txt", out);
645649
fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
646650
for (x = 0; x <= 32; x++) {
647651
for (ch = 0; ch < x; ch++) {
@@ -666,7 +670,7 @@ static void ecc_gen(void)
666670
ecc_point *G, *R;
667671
int x;
668672

669-
out = fopen("ecc_tv.txt", "w");
673+
OPEN_FILE("ecc_tv.txt", out);
670674
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");
671675
G = ltc_ecc_new_point();
672676
R = ltc_ecc_new_point();
@@ -713,7 +717,7 @@ static void lrw_gen(void)
713717
tweak[x] = key[x] = iv[x] = x;
714718
}
715719

716-
out = fopen("lrw_tv.txt", "w");
720+
OPEN_FILE("lrw_tv.txt", out);
717721
for (x = 16; x < (int)(sizeof(buf)); x += 16) {
718722
if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
719723
fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));

makefile.mingw

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,10 @@ TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcryp
236236
tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
237237
tests/ecc_test.o tests/ed25519_test.o tests/file_test.o tests/mac_test.o tests/misc_test.o \
238238
tests/modes_test.o tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o \
239-
tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \
240-
tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \
241-
tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o
239+
tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \
240+
tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \
241+
tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o \
242+
tests/x25519_test.o
242243

243244
#The following headers will be installed by "make install"
244245
HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \

makefile.msvc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ TOBJECTS=tests/base16_test.obj tests/base32_test.obj tests/base64_test.obj tests
229229
tests/cipher_hash_test.obj tests/common.obj tests/der_test.obj tests/dh_test.obj tests/dsa_test.obj \
230230
tests/ecc_test.obj tests/ed25519_test.obj tests/file_test.obj tests/mac_test.obj tests/misc_test.obj \
231231
tests/modes_test.obj tests/mpi_test.obj tests/multi_test.obj tests/no_null_termination_check_test.obj \
232-
tests/no_prng.obj tests/padding_test.obj tests/pem_test.obj tests/pkcs_1_eme_test.obj tests/pkcs_1_emsa_test.obj \
233-
tests/pkcs_1_oaep_test.obj tests/pkcs_1_pss_test.obj tests/pkcs_1_test.obj tests/prng_test.obj \
234-
tests/rotate_test.obj tests/rsa_test.obj tests/ssh_test.obj tests/store_test.obj tests/test.obj tests/x25519_test.obj
232+
tests/no_prng.obj tests/padding_test.obj tests/pem_test.obj tests/pk_oid_test.obj tests/pkcs_1_eme_test.obj \
233+
tests/pkcs_1_emsa_test.obj tests/pkcs_1_oaep_test.obj tests/pkcs_1_pss_test.obj tests/pkcs_1_test.obj \
234+
tests/prng_test.obj tests/rotate_test.obj tests/rsa_test.obj tests/ssh_test.obj tests/store_test.obj tests/test.obj \
235+
tests/x25519_test.obj
235236

236237
#The following headers will be installed by "make install"
237238
HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \

makefile.unix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcryp
250250
tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
251251
tests/ecc_test.o tests/ed25519_test.o tests/file_test.o tests/mac_test.o tests/misc_test.o \
252252
tests/modes_test.o tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o \
253-
tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \
254-
tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \
255-
tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o
253+
tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \
254+
tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \
255+
tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o \
256+
tests/x25519_test.o
256257

257258
#The following headers will be installed by "make install"
258259
HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \

makefile_include.mk

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,10 @@ TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcryp
420420
tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
421421
tests/ecc_test.o tests/ed25519_test.o tests/file_test.o tests/mac_test.o tests/misc_test.o \
422422
tests/modes_test.o tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o \
423-
tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \
424-
tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \
425-
tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o
423+
tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \
424+
tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \
425+
tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o \
426+
tests/x25519_test.o
426427

427428
# The following headers will be installed by "make install"
428429
HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \
@@ -494,22 +495,23 @@ $(DESTDIR)$(BINPATH):
494495
install -p -d $(DESTDIR)$(BINPATH)
495496

496497
.common_install_bins: $(USEFUL_DEMOS) $(DESTDIR)$(BINPATH)
497-
for d in $(USEFUL_DEMOS); do $(INSTALL_CMD) -p -m 775 $$d $(DESTDIR)$(BINPATH)/ltc-$$d
498+
for d in $(USEFUL_DEMOS); do $(INSTALL_CMD) -p -m 775 $$d $(DESTDIR)$(BINPATH)/ltc-$$d; done
498499
$(INSTALL_CMD) -p -m 775 demos/ltc $(DESTDIR)$(BINPATH)
499500

500501
install_docs: $(call print-help,install_docs,Installs the Developer Manual) doc/crypt.pdf
501502
install -p -d $(DESTDIR)$(DATAPATH)
502503
install -p -m 644 doc/crypt.pdf $(DESTDIR)$(DATAPATH)
503504

504505
install_test: $(call print-help,install_test,Installs the self-test binary) test $(DESTDIR)$(BINPATH)
505-
$(INSTALL_CMD) -p -m 775 $< $(DESTDIR)$(BINPATH)
506+
$(INSTALL_CMD) -p -m 775 $< $(DESTDIR)$(BINPATH)/ltc-$<
506507

507508
install_hooks: $(call print-help,install_hooks,Installs the git hooks)
508509
for s in `ls hooks/`; do ln -s ../../hooks/$$s .git/hooks/$$s; done
509510

510511
HEADER_FILES=$(notdir $(HEADERS_PUB))
511512
.common_uninstall:
512513
$(UNINSTALL_CMD) $(DESTDIR)$(LIBPATH)/$(LIBNAME)
514+
for d in $(USEFUL_DEMOS) test; do rm -f $(DESTDIR)$(BINPATH)/ltc-$$d; done
513515
$(UNINSTALL_CMD) $(HEADER_FILES:%=$(DESTDIR)$(INCPATH)/%)
514516

515517
#This rule cleans the source tree of all compiled code, not including the pdf

0 commit comments

Comments
 (0)