Merge pull request #390 from libtom/fixes/and/improvements

Fixes and improvements
This commit is contained in:
karel-m 2018-05-24 17:08:28 +02:00 committed by GitHub
commit a94b02be94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 184 additions and 148 deletions

View File

@ -78,7 +78,7 @@ void cipher_gen(void)
printf("keysize error: %s\n", error_to_string(err));
exit(EXIT_FAILURE);
}
if (kl == lastkl) break;
if (kl == lastkl) continue;
lastkl = kl;
fprintf(out, "Key Size: %d bytes\n", kl);

View File

@ -1434,6 +1434,58 @@ Key Size: 8 bytes
Cipher: 3des
Key Size: 16 bytes
0: DF0B6C9C31CD0CE4
1: 9B3503FDF249920B
2: 653924639C39E7FF
3: 6A29E0A7F42025BB
4: 1628B719BC875D20
5: 7D77004A18D0C0B2
6: 4D21684EFE962DC1
7: B6BD7F82B648A364
8: 1F87ABAD83D19E96
9: 3DF3533220C3CDED
10: D0E7D0ABFBA68747
11: 109FE5B38D74E6C9
12: AE12C4B4D523784F
13: 953CD7F264166764
14: 70B3A87D72FA0A22
15: 9C9D09AC66AB8F6D
16: 4A15AEACB35B76F0
17: EFA32F95623BCF1A
18: 679901F7737E195C
19: 221BB06209DDFCF4
20: 0889A953C60BB1BF
21: 88F2249380E2D5D9
22: 5AB26168B7FA24D5
23: 934229150997D390
24: 535E4F4C4DA97062
25: 03E8D711AC2B8154
26: CB5EF6E72EA3EC49
27: 9278A864F488C94A
28: CB91B77401DAF004
29: 4D0BA1C9794E0099
30: 9CFA24A21F48043F
31: BB6B3A33AEEC01F4
32: F2A8566E0FF6033D
33: E6AC213000E955E6
34: 91F5FF42BBE0B81B
35: 6506D72ADEA70E12
36: F9BD8C0506C7CC4E
37: 89CD85D1C98439ED
38: 409410E3E7D66B10
39: 4CA64F96F4F3D216
40: 383D18FBF8C006BC
41: 3806A8CB006EC243
42: EE73C06D903D2FCF
43: 624BFD3FAD7ED9EB
44: 1B5457F2731FB5D1
45: 4EC4632DFAC9D5D6
46: 8F0B3100FAD612C5
47: F955FCAD55AC6C90
48: BEB5F023BD413960
49: BDC369F3288ED754
Key Size: 24 bytes
0: 58ED248F77F6B19E
1: DA5C39983FD34F30

View File

@ -36,7 +36,7 @@ const struct ltc_cipher_descriptor des3_desc =
{
"3des",
14,
24, 24, 8, 16,
16, 24, 8, 16,
&des3_setup,
&des3_ecb_encrypt,
&des3_ecb_decrypt,
@ -2068,8 +2068,11 @@ int des_keysize(int *keysize)
int des3_keysize(int *keysize)
{
LTC_ARGCHK(keysize != NULL);
if(*keysize < 24) {
return CRYPT_INVALID_KEYSIZE;
if (*keysize < 16)
return CRYPT_INVALID_KEYSIZE;
if (*keysize < 24) {
*keysize = 16;
return CRYPT_OK;
}
*keysize = 24;
return CRYPT_OK;

View File

@ -828,16 +828,16 @@ int der_printable_value_decode(int v);
/* UTF-8 */
#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
#include <wchar.h>
#if defined(__WCHAR_MAX__)
#define LTC_WCHAR_MAX __WCHAR_MAX__
#elif defined(WCHAR_MAX)
#define LTC_WCHAR_MAX WCHAR_MAX
#endif
#if defined(__WCHAR_MAX__)
#define LTC_WCHAR_MAX __WCHAR_MAX__
#else
#include <wchar.h>
#define LTC_WCHAR_MAX WCHAR_MAX
#endif
/* please note that it might happen that LTC_WCHAR_MAX is undefined */
#else
typedef ulong32 wchar_t;
#define LTC_WCHAR_MAX 0xFFFFFFFF
typedef ulong32 wchar_t;
#define LTC_WCHAR_MAX 0xFFFFFFFF
#endif
int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,

View File

@ -70,6 +70,88 @@ int do_compare_testvector(const void* is, const unsigned long is_len, const void
}
}
#ifdef LTC_TEST_READDIR
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
static off_t fsize(const char *filename)
{
struct stat st;
if (stat(filename, &st) == 0) return st.st_size;
return -1;
}
int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_cleanup_cb cleanup, const char *test)
{
DIR *d = opendir(path);
struct dirent *de;
char fname[PATH_MAX];
void* buf = NULL;
FILE *f = NULL;
off_t fsz;
unsigned long sz;
int err = CRYPT_FILE_NOTFOUND;
if (d == NULL)
return CRYPT_FILE_NOTFOUND;
while((de = readdir(d)) != NULL) {
fname[0] = '\0';
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
strcat(fname, path);
strcat(fname, "/");
strcat(fname, de->d_name);
fsz = fsize(fname);
if (fsz == -1) {
err = CRYPT_FILE_NOTFOUND;
break;
}
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
fprintf(stderr, "%s: Try to process %s\n", test, fname);
#endif
f = fopen(fname, "rb");
sz = fsz;
buf = XMALLOC(fsz);
if (fread(buf, 1, sz, f) != sz) {
err = CRYPT_ERROR;
break;
}
err = process(buf, sz, ctx);
if (err == CRYPT_NOP) {
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
fprintf(stderr, "%s: Skip: %s\n", test, fname);
#endif
break;
} else if (err != CRYPT_OK) {
#if defined(LTC_TEST_DBG)
fprintf(stderr, "%s: Test %s failed (cause: %s).\n\n", test, fname, error_to_string(err));
#else
LTC_UNUSED_PARAM(test);
#endif
break;
}
if ((err != CRYPT_NOP) && (cleanup != NULL)) {
cleanup(ctx);
}
XFREE(buf);
buf = NULL;
fclose(f);
f = NULL;
}
if (buf != NULL) XFREE(buf);
if (f != NULL) fclose(f);
closedir(d);
return err;
}
#endif
prng_state yarrow_prng;
/* ref: $Format:%D$ */

View File

@ -23,6 +23,16 @@ extern prng_state yarrow_prng;
#define SHOULD_FAIL(x) do { run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
#endif
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
#define LTC_TEST_READDIR
typedef int (*dir_iter_cb)(const void *d, unsigned long l, void* ctx);
typedef void (*dir_cleanup_cb)(void* ctx);
int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_cleanup_cb cleanup, const char *test);
#endif
void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm);
void print_hex(const char* what, const void* v, const unsigned long l);

View File

@ -1300,72 +1300,19 @@ static void der_Xcode_test(void)
mp_clear(mpinteger);
}
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
static off_t fsize(const char *filename)
#ifdef LTC_TEST_READDIR
int _der_decode_sequence_flexi(const void *in, unsigned long inlen, void* ctx)
{
struct stat st;
if (stat(filename, &st) == 0) return st.st_size;
return -1;
}
static void der_asn1_test(void)
{
DIR *d = opendir("tests/asn1");
struct dirent *de;
char fname[PATH_MAX];
void* buf = NULL;
FILE *f = NULL;
off_t fsz;
unsigned long sz;
ltc_asn1_list *list;
int err;
if (d == NULL)
return;
while((de = readdir(d)) != NULL) {
fname[0] = '\0';
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
strcat(fname, "tests/asn1/");
strcat(fname, de->d_name);
fsz = fsize(fname);
if (fsz == -1)
break;
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
fprintf(stderr, "Try to decode %s\n", fname);
#endif
f = fopen(fname, "rb");
sz = fsz;
buf = XMALLOC(fsz);
if (fread(buf, 1, sz, f) != sz)
break;
if ((err = der_decode_sequence_flexi(buf, &sz, &list)) == CRYPT_OK) {
ltc_asn1_list** list = ctx;
if (der_decode_sequence_flexi(in, &inlen, list) == CRYPT_OK) {
#ifdef LTC_DER_TESTS_PRINT_FLEXI
fprintf(stderr, "\n\n");
_der_tests_print_flexi(list, 0);
fprintf(stderr, "\n\n");
fprintf(stderr, "\n\n");
_der_tests_print_flexi(*list, 0);
fprintf(stderr, "\n\n");
#endif
der_sequence_free(list);
} else {
#if defined(LTC_TEST_DBG)
fprintf(stderr, "Could not decode %s: %s\n\n", fname, error_to_string(err));
#endif
}
XFREE(buf);
buf = NULL;
fclose(f);
f = NULL;
der_sequence_free(*list);
}
if (buf != NULL) XFREE(buf);
if (f != NULL) fclose(f);
closedir(d);
return CRYPT_OK;
}
#endif
@ -1620,6 +1567,9 @@ int der_test(void)
unsigned char utf8_buf[32];
wchar_t utf8_out[32];
#ifdef LTC_TEST_READDIR
ltc_asn1_list *list;
#endif
if (ltc_mp.name == NULL) return CRYPT_NOP;
@ -1627,8 +1577,8 @@ int der_test(void)
der_Xcode_test();
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
der_asn1_test();
#ifdef LTC_TEST_READDIR
DO(test_process_dir("tests/asn1", &list, _der_decode_sequence_flexi, NULL, "DER ASN.1 special cases"));
#endif
der_custom_test();

View File

@ -329,80 +329,19 @@ static int _rsa_issue_301(int prng_idx)
return CRYPT_OK;
}
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
static off_t fsize(const char *filename)
#ifdef LTC_TEST_READDIR
static int _rsa_import_x509(const void *in, unsigned long inlen, void *key)
{
struct stat st;
if (stat(filename, &st) == 0) return st.st_size;
return -1;
}
static int _rsa_size_test(void)
{
DIR *d = opendir("tests/rsa");
struct dirent *de;
char fname[PATH_MAX];
void* buf = NULL;
FILE *f = NULL;
off_t fsz;
unsigned long sz;
int err = CRYPT_FILE_NOTFOUND;
rsa_key k;
if (d == NULL)
return CRYPT_FILE_NOTFOUND;
while((de = readdir(d)) != NULL) {
fname[0] = '\0';
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
strcat(fname, "tests/rsa/");
strcat(fname, de->d_name);
fsz = fsize(fname);
if (fsz == -1)
break;
/* here we use the filesize as indicator for the rsa size
* that would fail to import for tfm because it's fixed-size
*/
if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (fsz > 2048)) {
/* here we use the filesize as indicator for the rsa size
* that would fail to import for tfm because it's fixed-size
*/
if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (inlen > 2048)) {
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
fprintf(stderr, "TomsFastMath skip: %s\n", fname);
fprintf(stderr, "Skipping testcase because of TomsFastMath\n");
#endif
continue;
}
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
fprintf(stderr, "Try to import %s\n", fname);
#endif
f = fopen(fname, "rb");
sz = fsz;
buf = XMALLOC(fsz);
if (fread(buf, 1, sz, f) != sz) {
err = CRYPT_ERROR;
break;
}
if ((err = rsa_import_x509(buf, sz, &k)) == CRYPT_OK) {
rsa_free(&k);
} else {
#if defined(LTC_TEST_DBG)
fprintf(stderr, "Could not import RSA key of %s: %s\n\n", fname, error_to_string(err));
#endif
break;
}
XFREE(buf);
buf = NULL;
fclose(f);
f = NULL;
return CRYPT_NOP;
}
if (buf != NULL) XFREE(buf);
if (f != NULL) fclose(f);
closedir(d);
return err;
return rsa_import_x509(in, inlen, key);
}
#endif
@ -431,8 +370,8 @@ int rsa_test(void)
return 1;
}
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
DO(_rsa_size_test());
#ifdef LTC_TEST_READDIR
DO(test_process_dir("tests/rsa", &key, _rsa_import_x509, (dir_cleanup_cb)rsa_free, "rsa_test"));
#endif
DO(_rsa_issue_301(prng_idx));