Skip to content

Commit 238eb73

Browse files
authored
Merge pull request #504 from libtom/minor-fixes
Minor fixes and improvements
2 parents 6595403 + c0d1cbd commit 238eb73

File tree

14 files changed

+141
-77
lines changed

14 files changed

+141
-77
lines changed

.ci/meta_builds.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function run_gcc() {
3434

3535
ASAN_OPTIONS=verbosity=1 ./test t ltm 1>test_std.txt 2> test_err.txt || exit 1
3636

37-
if echo $2 | grep -q GMP ; then
37+
if echo $* | grep -q GMP ; then
3838
echo
3939
echo "Run ASAN tests with GMP..."
4040

@@ -68,7 +68,7 @@ function run_clang() {
6868
echo "Run UBSAN tests with LTM..."
6969
UBSAN_OPTIONS=verbosity=1 ./test t ltm 1>test_std.txt 2> test_err.txt || exit 1
7070

71-
if echo $2 | grep -q GMP ; then
71+
if echo $* | grep -q GMP ; then
7272
echo
7373
echo "Run UBSAN tests with GMP..."
7474

.travis.yml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,97 +30,97 @@ script:
3030
bash "${BUILDSCRIPT}" "${BUILDNAME}" "-DUSE_TFM -DTFM_DESC" "makefile.shared V=1" "${BUILDOPTIONS}" "-ltfm"
3131
env:
3232
- |
33-
BUILDSCRIPT=".ci/meta_builds.sh"
3433
BUILDNAME="META_BUILDS"
3534
BUILDOPTIONS="-DGMP_DESC"
35+
BUILDSCRIPT=".ci/meta_builds.sh"
3636
- |
37-
BUILDSCRIPT=".ci/valgrind.sh"
3837
BUILDNAME="VALGRIND"
3938
BUILDOPTIONS=" "
39+
BUILDSCRIPT=".ci/valgrind.sh"
4040
- |
41-
BUILDSCRIPT=".ci/run.sh"
4241
BUILDNAME="STOCK"
4342
BUILDOPTIONS=" "
44-
- |
4543
BUILDSCRIPT=".ci/run.sh"
44+
- |
4645
BUILDNAME="STOCK-MPI"
4746
BUILDOPTIONS="-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM"
48-
- |
4947
BUILDSCRIPT=".ci/run.sh"
48+
- |
5049
BUILDNAME="EASY"
5150
BUILDOPTIONS="-DLTC_EASY"
52-
- |
5351
BUILDSCRIPT=".ci/run.sh"
52+
- |
5453
BUILDNAME="SMALL"
5554
BUILDOPTIONS="-DLTC_SMALL_CODE"
56-
- |
5755
BUILDSCRIPT=".ci/run.sh"
56+
- |
5857
BUILDNAME="NOTABLES"
5958
BUILDOPTIONS="-DLTC_NO_TABLES"
60-
- |
6159
BUILDSCRIPT=".ci/run.sh"
60+
- |
6261
BUILDNAME="SMALL+NOTABLES"
6362
BUILDOPTIONS="-DLTC_SMALL_CODE -DLTC_NO_TABLES"
64-
- |
6563
BUILDSCRIPT=".ci/run.sh"
64+
- |
6665
BUILDNAME="CLEANSTACK"
6766
BUILDOPTIONS="-DLTC_CLEAN_STACK"
68-
- |
6967
BUILDSCRIPT=".ci/run.sh"
68+
- |
7069
BUILDNAME="CLEANSTACK+SMALL"
7170
BUILDOPTIONS="-DLTC_SMALL_CODE -DLTC_CLEAN_STACK"
72-
- |
7371
BUILDSCRIPT=".ci/run.sh"
72+
- |
7473
BUILDNAME="CLEANSTACK+NOTABLES"
7574
BUILDOPTIONS="-DLTC_NO_TABLES -DLTC_CLEAN_STACK"
76-
- |
7775
BUILDSCRIPT=".ci/run.sh"
76+
- |
7877
BUILDNAME="CLEANSTACK+NOTABLES+SMALL"
7978
BUILDOPTIONS="-DLTC_NO_TABLES -DLTC_CLEAN_STACK -DLTC_SMALL_CODE"
80-
- |
8179
BUILDSCRIPT=".ci/run.sh"
80+
- |
8281
BUILDNAME="NO_FAST"
8382
BUILDOPTIONS="-DLTC_NO_FAST"
84-
- |
8583
BUILDSCRIPT=".ci/run.sh"
84+
- |
8685
BUILDNAME="NO_FAST+NOTABLES"
8786
BUILDOPTIONS="-DLTC_NO_FAST -DLTC_NO_TABLES"
88-
- |
8987
BUILDSCRIPT=".ci/run.sh"
88+
- |
9089
BUILDNAME="NO_ASM"
9190
BUILDOPTIONS="-DLTC_NO_ASM"
92-
- |
9391
BUILDSCRIPT=".ci/run.sh"
92+
- |
9493
BUILDNAME="NO_TIMING_RESISTANCE"
9594
BUILDOPTIONS="-DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING"
96-
- |
9795
BUILDSCRIPT=".ci/run.sh"
96+
- |
9897
BUILDNAME="CLEANSTACK+NOTABLES+SMALL+NO_ASM+NO_TIMING_RESISTANCE+LTC_FORTUNA_RESEED_RATELIMIT_STATIC"
9998
BUILDOPTIONS="-DLTC_CLEAN_STACK -DLTC_NO_TABLES -DLTC_SMALL_CODE -DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC"
100-
- |
10199
BUILDSCRIPT=".ci/run.sh"
100+
- |
102101
BUILDNAME="PTHREAD"
103102
BUILDOPTIONS="-DLTC_PTHREAD"
104-
- |
105103
BUILDSCRIPT=".ci/run.sh"
106-
BUILDNAME="CLEANSTACK+NOTABLES+SMALL+NO_ASM+NO_TIMING_RESISTANCE+LTC_FORTUNA_RESEED_RATELIMIT_STATIC+PTHREAD"
107-
BUILDOPTIONS="-DLTC_CLEAN_STACK -DLTC_NO_TABLES -DLTC_SMALL_CODE -DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC -DLTC_PTHREAD"
108104
- |
105+
BUILDNAME="PTHREAD+CLEANSTACK+NOTABLES+SMALL+NO_ASM+NO_TIMING_RESISTANCE+LTC_FORTUNA_RESEED_RATELIMIT_STATIC"
106+
BUILDOPTIONS="-DLTC_CLEAN_STACK -DLTC_NO_TABLES -DLTC_SMALL_CODE -DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC -DLTC_PTHREAD"
109107
BUILDSCRIPT=".ci/run.sh"
108+
- |
110109
BUILDNAME="STOCK+ARGTYPE=1"
111110
BUILDOPTIONS="-DARGTYPE=1"
112-
- |
113111
BUILDSCRIPT=".ci/run.sh"
112+
- |
114113
BUILDNAME="STOCK+ARGTYPE=2"
115114
BUILDOPTIONS="-DARGTYPE=2"
116-
- |
117115
BUILDSCRIPT=".ci/run.sh"
116+
- |
118117
BUILDNAME="STOCK+ARGTYPE=3"
119118
BUILDOPTIONS="-DARGTYPE=3"
120-
- |
121119
BUILDSCRIPT=".ci/run.sh"
120+
- |
122121
BUILDNAME="STOCK+ARGTYPE=4"
123122
BUILDOPTIONS="-DARGTYPE=4"
123+
BUILDSCRIPT=".ci/run.sh"
124124
125125
after_failure:
126126
- cat test_std.txt

demos/demo_dynamic.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,29 @@ def digest(self):
245245
LTC.sha256_done(byref(self.state), byref(md))
246246
return md.raw
247247

248+
# - - - - - - - - - - - - -
249+
# a SHA256 app fragment
250+
251+
if SHOW_SHA256_EXAMPLE:
252+
print('-'*60)
253+
data = b'hello world' # we want bytes, not Unicode
254+
255+
sha256 = SHA256()
256+
sha256.update(data)
257+
md = sha256.digest()
258+
259+
template = '\n the SHA256 digest for "%s" is %s \n'
260+
print(template % (data, hexlify(md)))
261+
248262
class ChaCha(object):
249263
def __init__(self, key, rounds):
250264
self.state = c_buffer(_get_size(b'chacha_state'))
251-
self.counter = c_int(1)
265+
self.counter = c_uint(1)
252266
err = LTC.chacha_setup(byref(self.state), key, len(key), rounds)
253267
if err != CRYPT_OK:
254268
raise Exception('LTC.chacha_setup(), err = %d, "%s"' % (err, _err2str(err)))
255269
def set_iv32(self, iv):
256-
err = LTC.chacha_ivctr32(byref(self.state), iv, len(iv), byref(self.counter))
270+
err = LTC.chacha_ivctr32(byref(self.state), iv, len(iv), self.counter)
257271
if err != CRYPT_OK:
258272
raise Exception('LTC.chacha_ivctr32(), err = %d, "%s"' % (err, _err2str(err)))
259273
def crypt(self, datain):
@@ -263,20 +277,6 @@ def crypt(self, datain):
263277
raise Exception('LTC.chacha_crypt(), err = %d, "%s"' % (err, _err2str(err)))
264278
return dataout.raw
265279

266-
# - - - - - - - - - - - - -
267-
# a SHA256 app fragment
268-
269-
if SHOW_SHA256_EXAMPLE:
270-
print('-'*60)
271-
data = b'hello world' # we want bytes, not Unicode
272-
273-
sha256 = SHA256()
274-
sha256.update(data)
275-
md = sha256.digest()
276-
277-
template = '\n the SHA256 digest for "%s" is %s \n'
278-
print(template % (data, hexlify(md)))
279-
280280
# - - - - - - - - - - - - -
281281
# a ChaCha app fragment
282282

helper.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ sub patch_file {
280280

281281
sub version_from_tomcrypt_h {
282282
my $h = read_file(shift);
283-
if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(.*)"/s) {
283+
if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(\S*)"/s) {
284284
return "VERSION_PC=$1.$2.$3", "VERSION_LT=1:1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
285285
}
286286
else {

makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#
66
# (GNU make only)
77

8+
ifeq ($V,0)
9+
silent_echo= > /dev/null
10+
else
11+
silent_echo=
12+
endif
813
ifeq ($V,1)
914
silent=
1015
silent_stdout=
@@ -42,37 +47,37 @@ LTC_EXTRALIBS += $(EXTRALIBS)
4247
#AES comes in two flavours... enc+dec and enc
4348
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
4449
ifneq ($V,1)
45-
@echo " * ${CC} $@"
50+
@echo " * ${CC} $@" ${silent_echo}
4651
endif
4752
${silent} ${CC} ${LTC_CFLAGS} -DENCRYPT_ONLY -c $< -o $@
4853

4954
.c.o:
5055
ifneq ($V,1)
51-
@echo " * ${CC} $@"
56+
@echo " * ${CC} $@" ${silent_echo}
5257
endif
5358
${silent} ${CC} ${LTC_CFLAGS} -c $< -o $@
5459

5560
$(LIBNAME): $(OBJECTS)
5661
ifneq ($V,1)
57-
@echo " * ${AR} $@"
62+
@echo " * ${AR} $@" ${silent_echo}
5863
endif
5964
${silent} $(AR) $(ARFLAGS) $@ $(OBJECTS)
6065
ifneq ($V,1)
61-
@echo " * ${RANLIB} $@"
66+
@echo " * ${RANLIB} $@" ${silent_echo}
6267
endif
6368
${silent} $(RANLIB) $@
6469

6570
test: $(call print-help,test,Builds the library and the 'test' application to run all self-tests) $(LIBNAME) $(TOBJECTS)
6671
ifneq ($V,1)
67-
@echo " * ${CC} $@"
72+
@echo " * ${CC} $@" ${silent_echo}
6873
endif
6974
${silent} $(CC) $(LTC_LDFLAGS) $(TOBJECTS) $(LIB_PRE) $(LIBNAME) $(LIB_POST) $(LTC_EXTRALIBS) -o $(TEST)
7075

7176
# build the demos from a template
7277
define DEMO_template
7378
$(1): $(call print-help,$(1),Builds the library and the '$(1)' demo) demos/$(1).o $$(LIBNAME)
7479
ifneq ($V,1)
75-
@echo " * $${CC} $$@"
80+
@echo " * $${CC} $$@" ${silent_echo}
7681
endif
7782
$${silent} $$(CC) $$(LTC_LDFLAGS) $$< $$(LIB_PRE) $$(LIBNAME) $$(LIB_POST) $$(LTC_EXTRALIBS) -o $(1)
7883
endef

src/ciphers/anubis.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,11 @@ int anubis_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetr
11311131
LTC_ARGCHK(pt != NULL);
11321132
LTC_ARGCHK(ct != NULL);
11331133
LTC_ARGCHK(skey != NULL);
1134+
1135+
if (skey->anubis.R < 12 || skey->anubis.R > 18) {
1136+
return CRYPT_INVALID_ROUNDS;
1137+
}
1138+
11341139
anubis_crypt(pt, ct, skey->anubis.roundKeyEnc, skey->anubis.R);
11351140
return CRYPT_OK;
11361141
}
@@ -1147,6 +1152,11 @@ int anubis_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetr
11471152
LTC_ARGCHK(pt != NULL);
11481153
LTC_ARGCHK(ct != NULL);
11491154
LTC_ARGCHK(skey != NULL);
1155+
1156+
if (skey->anubis.R < 12 || skey->anubis.R > 18) {
1157+
return CRYPT_INVALID_ROUNDS;
1158+
}
1159+
11501160
anubis_crypt(ct, pt, skey->anubis.roundKeyDec, skey->anubis.R);
11511161
return CRYPT_OK;
11521162
}

src/ciphers/rc5.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_
136136
LTC_ARGCHK(pt != NULL);
137137
LTC_ARGCHK(ct != NULL);
138138

139+
if (skey->rc5.rounds < 12 || skey->rc5.rounds > 24) {
140+
return CRYPT_INVALID_ROUNDS;
141+
}
142+
139143
LOAD32L(A, &pt[0]);
140144
LOAD32L(B, &pt[4]);
141145
A += skey->rc5.K[0];
@@ -192,6 +196,10 @@ int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_
192196
LTC_ARGCHK(pt != NULL);
193197
LTC_ARGCHK(ct != NULL);
194198

199+
if (skey->rc5.rounds < 12 || skey->rc5.rounds > 24) {
200+
return CRYPT_INVALID_ROUNDS;
201+
}
202+
195203
LOAD32L(A, &ct[0]);
196204
LOAD32L(B, &ct[4]);
197205
K = skey->rc5.K + (skey->rc5.rounds << 1);

src/ciphers/safer/saferp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ int saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetr
347347
LTC_ARGCHK(ct != NULL);
348348
LTC_ARGCHK(skey != NULL);
349349

350+
if (skey->saferp.rounds < 8 || skey->saferp.rounds > 16) {
351+
return CRYPT_INVALID_ROUNDS;
352+
}
353+
350354
/* do eight rounds */
351355
for (x = 0; x < 16; x++) {
352356
b[x] = pt[x];
@@ -411,6 +415,10 @@ int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetr
411415
LTC_ARGCHK(ct != NULL);
412416
LTC_ARGCHK(skey != NULL);
413417

418+
if (skey->saferp.rounds < 8 || skey->saferp.rounds > 16) {
419+
return CRYPT_INVALID_ROUNDS;
420+
}
421+
414422
/* do eight rounds */
415423
b[0] = ct[0] ^ skey->saferp.K[skey->saferp.rounds*2][0];
416424
b[1] = (ct[1] - skey->saferp.K[skey->saferp.rounds*2][1]) & 255;

src/encauth/gcm/gcm_gf_mult.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
#include "tomcrypt_private.h"
1515

16-
#if defined(LTC_GCM_TABLES) || defined(LTC_LRW_TABLES) || ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST))
16+
#if defined(LTC_GCM_TABLES) || defined(LTC_LRW_TABLES) || (defined(LTC_GCM_MODE) && defined(LTC_FAST))
1717

1818
/* this is x*2^128 mod p(x) ... the results are 16 bytes each stored in a packed format. Since only the
1919
* lower 16 bits are not zero'ed I removed the upper 14 bytes */

src/headers/tomcrypt.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <limits.h>
2020

2121
/* use configuration data */
22-
#include <tomcrypt_custom.h>
22+
#include "tomcrypt_custom.h"
2323

2424
#ifdef __cplusplus
2525
extern "C" {
@@ -81,17 +81,17 @@ enum {
8181
CRYPT_HASH_OVERFLOW /* Hash applied to too many bits */
8282
};
8383

84-
#include <tomcrypt_cfg.h>
85-
#include <tomcrypt_macros.h>
86-
#include <tomcrypt_cipher.h>
87-
#include <tomcrypt_hash.h>
88-
#include <tomcrypt_mac.h>
89-
#include <tomcrypt_prng.h>
90-
#include <tomcrypt_pk.h>
91-
#include <tomcrypt_math.h>
92-
#include <tomcrypt_misc.h>
93-
#include <tomcrypt_argchk.h>
94-
#include <tomcrypt_pkcs.h>
84+
#include "tomcrypt_cfg.h"
85+
#include "tomcrypt_macros.h"
86+
#include "tomcrypt_cipher.h"
87+
#include "tomcrypt_hash.h"
88+
#include "tomcrypt_mac.h"
89+
#include "tomcrypt_prng.h"
90+
#include "tomcrypt_pk.h"
91+
#include "tomcrypt_math.h"
92+
#include "tomcrypt_misc.h"
93+
#include "tomcrypt_argchk.h"
94+
#include "tomcrypt_pkcs.h"
9595

9696
#ifdef __cplusplus
9797
}

0 commit comments

Comments
 (0)