Skip to content

Commit d34c440

Browse files
authored
Merge pull request #560 from libtom/cleanup-and-fixes
Cleanup and fixes
2 parents ddf1b63 + c117cd8 commit d34c440

File tree

16 files changed

+82
-108
lines changed

16 files changed

+82
-108
lines changed

doc/crypt.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7462,7 +7462,7 @@ \subsection{Padding mode argument}
74627462
\begin{center}
74637463
\begin{tabular}{|l|l|}
74647464
\hline \textbf{mode} & \textbf{Standard} \\
7465-
\hline LTC\_PAD\_PKCS7 & RFC-5652 / PKCS \#7 \\
7465+
\hline LTC\_PAD\_PKCS7 & a.k.a CMS; best described in RFC-5652 / PKCS \#7; also mentioned in PKCS \#5 which refers to RFC-1423 \\
74667466
\hline LTC\_PAD\_ISO\_10126 & ISO/IEC 10126 \footnote{\textit{ISO/IEC 10126} support is only available when the library is built with \textit{rng\_get\_bytes()} support} \\
74677467
\hline LTC\_PAD\_ANSI\_X923 & ANSI X.923 \\
74687468
\hline LTC\_PAD\_ONE\_AND\_ZERO & ISO/IEC 7816-4 \\

makefile.shared

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
#
1616

1717
PLATFORM := $(shell uname | sed -e 's/_.*//')
18+
### Observed uname outputs:
19+
# MINGW32_NT-6.2 (on MSYS/MINGW old)
20+
# MINGW64_NT-10.0-14393 (on MSYS new)
21+
# MSYS_NT-10.0-19042 (on MSYS2)
22+
# CYGWIN_NT-10.0 (on Cygwin 64bit)
23+
# CYGWIN_NT-6.2-WOW64 (on Cygwin 32bit)
24+
# Linux (on all Linux distros)
25+
# Darwin (on macOS, OS X)
1826

1927
ifeq ($(LIBTOOL),rlibtool)
2028
TGTLIBTOOL:=slibtool-shared

src/encauth/ccm/ccm_add_nonce.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ int ccm_add_nonce(ccm_state *ccm,
2525
if ((15 - ccm->noncelen) > ccm->L) {
2626
ccm->L = 15 - ccm->noncelen;
2727
}
28+
if (ccm->L > 8) {
29+
return CRYPT_INVALID_ARG;
30+
}
2831

2932
/* decrease noncelen to match L */
3033
if ((ccm->noncelen + ccm->L) > 15) {
@@ -38,7 +41,7 @@ int ccm_add_nonce(ccm_state *ccm,
3841
(ccm->L-1));
3942

4043
/* nonce */
41-
for (y = 0; y < (16 - (ccm->L + 1)); y++) {
44+
for (y = 0; y < 15 - ccm->L; y++) {
4245
ccm->PAD[x++] = nonce[y];
4346
}
4447

src/encauth/ccm/ccm_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int ccm_init(ccm_state *ccm, int cipher,
3535
}
3636

3737
/* make sure the taglen is valid */
38-
if (taglen < 4 || taglen > 16 || (taglen % 2) == 1) {
38+
if (taglen < 4 || taglen > 16 || (taglen % 2) == 1 || aadlen < 0 || ptlen < 0) {
3939
return CRYPT_INVALID_ARG;
4040
}
4141
ccm->taglen = taglen;

src/encauth/ccm/ccm_memory.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int ccm_memory(int cipher,
7575
}
7676

7777
/* make sure the taglen is valid */
78-
if (*taglen < 4 || *taglen > 16 || (*taglen % 2) == 1) {
78+
if (*taglen < 4 || *taglen > 16 || (*taglen % 2) == 1 || headerlen > 0x7fffffffu) {
7979
return CRYPT_INVALID_ARG;
8080
}
8181

@@ -108,6 +108,9 @@ int ccm_memory(int cipher,
108108
if ((15 - noncelen) > L) {
109109
L = 15 - noncelen;
110110
}
111+
if (L > 8) {
112+
return CRYPT_INVALID_ARG;
113+
}
111114

112115
/* allocate mem for the symmetric key */
113116
if (uskey == NULL) {
@@ -141,7 +144,7 @@ int ccm_memory(int cipher,
141144
(L-1));
142145

143146
/* nonce */
144-
for (y = 0; y < (16 - (L + 1)); y++) {
147+
for (y = 0; y < 15 - L; y++) {
145148
PAD[x++] = nonce[y];
146149
}
147150

src/headers/tomcrypt_cfg.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
105105
#define ENDIAN_64BITWORD
106106
#if defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
107107
#define ENDIAN_BIG
108-
#endif
108+
#else
109109
#define ENDIAN_LITTLE
110110
#endif
111111
#endif
@@ -288,6 +288,12 @@ typedef unsigned long ltc_mp_digit;
288288
#define LTC_HAVE_ROTATE_BUILTIN
289289
#endif
290290

291+
#if defined(__GNUC__)
292+
#define LTC_ALIGN(n) __attribute__((aligned(n)))
293+
#else
294+
#define LTC_ALIGN(n)
295+
#endif
296+
291297
#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405)
292298
# define LTC_DEPRECATED(s) __attribute__((deprecated("replaced by " #s)))
293299
# define PRIVATE_LTC_DEPRECATED_PRAGMA(s) _Pragma(#s)
@@ -303,3 +309,5 @@ typedef unsigned long ltc_mp_digit;
303309
# define LTC_DEPRECATED(s)
304310
# define LTC_DEPRECATED_PRAGMA(s)
305311
#endif
312+
313+
#endif /* TOMCRYPT_CFG_H */

src/headers/tomcrypt_cipher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ typedef struct {
318318
ctrlen;
319319

320320
/** The counter */
321-
unsigned char ctr[MAXBLOCKSIZE],
321+
unsigned char ctr[MAXBLOCKSIZE];
322322
/** The pad used to encrypt/decrypt */
323-
pad[MAXBLOCKSIZE];
323+
unsigned char pad[MAXBLOCKSIZE] LTC_ALIGN(16);
324324
/** The scheduled key */
325325
symmetric_key key;
326326
} symmetric_CTR;

src/headers/tomcrypt_mac.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ int ocb3_test(void);
395395
typedef struct {
396396
symmetric_key K;
397397
int cipher, /* which cipher */
398-
taglen, /* length of the tag */
398+
taglen, /* length of the tag (encoded in M value) */
399399
x; /* index in PAD */
400400

401401
unsigned long L, /* L value */
@@ -405,7 +405,7 @@ typedef struct {
405405
current_aadlen, /* length of the currently provided add */
406406
noncelen; /* length of the nonce */
407407

408-
unsigned char PAD[16],
408+
unsigned char PAD[16], /* flags | Nonce N | l(m) */
409409
ctr[16],
410410
CTRPAD[16],
411411
CTRlen;
@@ -482,7 +482,7 @@ typedef struct {
482482
#ifdef LTC_GCM_TABLES
483483
unsigned char PC[16][256][16] /* 16 tables of 8x128 */
484484
#ifdef LTC_GCM_TABLES_SSE2
485-
__attribute__ ((aligned (16)))
485+
LTC_ALIGN(16)
486486
#endif
487487
;
488488
#endif

src/misc/compare_testvector.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ int compare_testvector(const void* is, const unsigned long is_len, const void* s
6464
}
6565
#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
6666
if (res != 0) {
67-
fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
67+
fprintf(stderr, "Testvector #%i(0x%x) of %s failed:\n", which, which, what);
6868
s_print_hex("SHOULD", should, should_len);
6969
s_print_hex("IS ", is, is_len);
7070
#if LTC_TEST_DBG > 1
7171
} else {
72-
fprintf(stderr, "Testvector #%i of %s passed!\n", which, what);
72+
fprintf(stderr, "Testvector #%i(0x%x) of %s passed!\n", which, which, what);
7373
#endif
7474
}
7575
#else

tests/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ extern prng_state yarrow_prng;
1313
#define SHOULD_FAIL(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
1414
#define SHOULD_FAIL_WITH(x, e) do { fprintf(stderr, "%s:\n", #x); run_cmd((x) == (e) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
1515
#define ENSURE(x) do { fprintf(stderr, "%s:\n", #x); run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
16+
#define ENSUREX(x, str) do { fprintf(stderr, "%s:\n", #x); run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, (str)); } while (0)
1617
#else
1718
#define DO(x) do { run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0)
1819
#define DOX(x, str) do { run_cmd((x), __LINE__, __FILE__, #x, (str)); } while (0)
1920
#define SHOULD_FAIL(x) do { run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
2021
#define SHOULD_FAIL_WITH(x, e) do { run_cmd((x) == (e) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
2122
#define ENSURE(x) do { run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
23+
#define ENSUREX(x, str) do { run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, (str)); } while (0)
2224
#endif
2325

2426
#define COMPARE_TESTVECTOR(i, il, s, sl, wa, wi) do { DO(do_compare_testvector((i), (il), (s), (sl), (wa), (wi))); } while(0)

0 commit comments

Comments
 (0)