From 7c4dd3fefea72ea96fefd40d791dac6070147cc1 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Sun, 14 Jan 2007 17:01:31 +0000 Subject: [PATCH] Make algorithm test programs tolerate whitespace in input files. --- CHANGES | 3 + fips-1.0/aes/fips_aesavs.c | 58 +++++++++++++++ fips-1.0/des/fips_desmovs.c | 59 ++++++++++++++- fips-1.0/dsa/fips_dssvs.c | 134 +++++++++++++++++++++++++++-------- fips-1.0/rand/fips_rngvs.c | 95 +++++++++++++++++-------- fips-1.0/rsa/fips_rsagtest.c | 2 +- fips-1.0/rsa/fips_rsastest.c | 2 +- fips-1.0/rsa/fips_rsavtest.c | 2 +- fips-1.0/sha/fips_shatest.c | 3 +- 9 files changed, 294 insertions(+), 64 deletions(-) diff --git a/CHANGES b/CHANGES index 3708edcae0..3cd2a3fa7b 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,9 @@ Changes between 0.9.7l and 0.9.7m [xx XXX xxxx] + *) Make algorithm test programs more tolerant of whitespace. + [Steve Henson] + *) Have SSL/TLS server implementation tolerate "mismatched" record protocol version while receiving ClientHello even if the ClientHello is fragmented. (The server can't insist on the diff --git a/fips-1.0/aes/fips_aesavs.c b/fips-1.0/aes/fips_aesavs.c index 6bb9b899c8..52211265d6 100644 --- a/fips-1.0/aes/fips_aesavs.c +++ b/fips-1.0/aes/fips_aesavs.c @@ -62,6 +62,7 @@ #include #include #include +#include #include #include @@ -622,6 +623,61 @@ int do_mct(char *amode, return ret; } +/* To avoid extensive changes to test program at this stage just convert + * the input line into an acceptable form. Keyword lines converted to form + * "keyword = value\n" no matter what white space present, all other lines + * just have leading and trailing space removed. + */ + +static int tidy_line(char *linebuf, char *olinebuf) + { + char *keyword, *value, *p, *q; + strcpy(linebuf, olinebuf); + keyword = linebuf; + /* Skip leading space */ + while (isspace((unsigned char)*keyword)) + keyword++; + /* Look for = sign */ + p = strchr(linebuf, '='); + + /* If no '=' just chop leading, trailing ws */ + if (!p) + { + p = keyword + strlen(keyword) - 1; + while (*p == '\n' || isspace((unsigned char)*p)) + *p-- = 0; + strcpy(olinebuf, keyword); + strcat(olinebuf, "\n"); + return 1; + } + + q = p - 1; + + /* Remove trailing space */ + while (isspace((unsigned char)*q)) + *q-- = 0; + + *p = 0; + value = p + 1; + + /* Remove leading space from value */ + while (isspace((unsigned char)*value)) + value++; + + /* Remove trailing space from value */ + p = value + strlen(value) - 1; + + while (*p == '\n' || isspace((unsigned char)*p)) + *p-- = 0; + + strcpy(olinebuf, keyword); + strcat(olinebuf, " = "); + strcat(olinebuf, value); + strcat(olinebuf, "\n"); + + return 1; + } + /*================================================*/ /*---------------------------- # Config info for v-one @@ -636,6 +692,7 @@ int proc_file(char *rqfile) char afn[256], rfn[256]; FILE *afp = NULL, *rfp = NULL; char ibuf[2048]; + char tbuf[2048]; int ilen, len, ret = 0; char algo[8] = ""; char amode[8] = ""; @@ -677,6 +734,7 @@ int proc_file(char *rqfile) } while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) { + tidy_line(tbuf, ibuf); ilen = strlen(ibuf); /* printf("step=%d ibuf=%s",step,ibuf); */ switch (step) diff --git a/fips-1.0/des/fips_desmovs.c b/fips-1.0/des/fips_desmovs.c index 5eb55726e3..4816e338ad 100644 --- a/fips-1.0/des/fips_desmovs.c +++ b/fips-1.0/des/fips_desmovs.c @@ -63,6 +63,7 @@ #include #include #include +#include #include #include @@ -70,6 +71,61 @@ #include #include "e_os.h" +/* To avoid extensive changes to test program at this stage just convert + * the input line into an acceptable form. Keyword lines converted to form + * "keyword = value\n" no matter what white space present, all other lines + * just have leading and trailing space removed. + */ + +static int tidy_line(char *linebuf, char *olinebuf) + { + char *keyword, *value, *p, *q; + strcpy(linebuf, olinebuf); + keyword = linebuf; + /* Skip leading space */ + while (isspace((unsigned char)*keyword)) + keyword++; + /* Look for = sign */ + p = strchr(linebuf, '='); + + /* If no '=' just chop leading, trailing ws */ + if (!p) + { + p = keyword + strlen(keyword) - 1; + while (*p == '\n' || isspace((unsigned char)*p)) + *p-- = 0; + strcpy(olinebuf, keyword); + strcat(olinebuf, "\n"); + return 1; + } + + q = p - 1; + + /* Remove trailing space */ + while (isspace((unsigned char)*q)) + *q-- = 0; + + *p = 0; + value = p + 1; + + /* Remove leading space from value */ + while (isspace((unsigned char)*value)) + value++; + + /* Remove trailing space from value */ + p = value + strlen(value) - 1; + + while (*p == '\n' || isspace((unsigned char)*p)) + *p-- = 0; + + strcpy(olinebuf, keyword); + strcat(olinebuf, " = "); + strcat(olinebuf, value); + strcat(olinebuf, "\n"); + + return 1; + } + /*#define AES_BLOCK_SIZE 16*/ #define VERBOSE 0 @@ -414,7 +470,7 @@ int proc_file(char *rqfile) { char afn[256], rfn[256]; FILE *afp = NULL, *rfp = NULL; - char ibuf[2048]; + char ibuf[2048], tbuf[2048]; int ilen, len, ret = 0; char amode[8] = ""; char atest[100] = ""; @@ -456,6 +512,7 @@ int proc_file(char *rqfile) } while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) { + tidy_line(tbuf, ibuf); ilen = strlen(ibuf); /* printf("step=%d ibuf=%s",step,ibuf);*/ if(step == 3 && !strcmp(amode,"ECB")) diff --git a/fips-1.0/dsa/fips_dssvs.c b/fips-1.0/dsa/fips_dssvs.c index 560d635981..fe4e54f287 100644 --- a/fips-1.0/dsa/fips_dssvs.c +++ b/fips-1.0/dsa/fips_dssvs.c @@ -16,6 +16,47 @@ int main() #include #include #include +#include + +static int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf) + { + char *keyword, *value, *p, *q; + strcpy(linebuf, olinebuf); + keyword = linebuf; + /* Skip leading space */ + while (isspace((unsigned char)*keyword)) + keyword++; + + /* Look for = sign */ + p = strchr(linebuf, '='); + + /* If no '=' exit */ + if (!p) + return 0; + + q = p - 1; + + /* Remove trailing space */ + while (isspace((unsigned char)*q)) + *q-- = 0; + + *p = 0; + value = p + 1; + + /* Remove leading space from value */ + while (isspace((unsigned char)*value)) + value++; + + /* Remove trailing space from value */ + p = value + strlen(value) - 1; + + while (*p == '\n' || isspace((unsigned char)*p)) + *p-- = 0; + + *pkw = keyword; + *pval = value; + return 1; + } int hex2bin(const char *in, unsigned char *out) { @@ -99,16 +140,20 @@ void pbn(const char *tag,const BIGNUM *val) void primes() { char buf[10240]; + char lbuf[10240]; + char *keyword, *value; while(fgets(buf,sizeof buf,stdin) != NULL) { fputs(buf,stdout); - if(!strncmp(buf,"Prime= ",7)) + if (!parse_line(&keyword, &value, lbuf, buf)) + continue; + if(!strcmp(keyword,"Prime")) { BIGNUM *pp; pp=BN_new(); - BN_hex2bn(&pp,buf+7); + BN_hex2bn(&pp,value); printf("result= %c\n", BN_is_prime(pp,20,NULL,NULL,NULL) ? 'P' : 'F'); } @@ -118,15 +163,22 @@ void primes() void pqg() { char buf[1024]; + char lbuf[1024]; + char *keyword, *value; int nmod=0; while(fgets(buf,sizeof buf,stdin) != NULL) { - if(!strncmp(buf,"[mod = ",7)) - nmod=atoi(buf+7); - else if(!strncmp(buf,"N = ",4)) + if (!parse_line(&keyword, &value, lbuf, buf)) + { + fputs(buf,stdout); + continue; + } + if(!strcmp(keyword,"[mod")) + nmod=atoi(value); + else if(!strcmp(keyword,"N")) { - int n=atoi(buf+4); + int n=atoi(value); printf("[mod = %d]\n\n",nmod); @@ -155,16 +207,23 @@ void pqg() void keypair() { char buf[1024]; + char lbuf[1024]; + char *keyword, *value; int nmod=0; while(fgets(buf,sizeof buf,stdin) != NULL) { - if(!strncmp(buf,"[mod = ",7)) - nmod=atoi(buf+7); - else if(!strncmp(buf,"N = ",4)) + if (!parse_line(&keyword, &value, lbuf, buf)) + { + fputs(buf,stdout); + continue; + } + if(!strcmp(keyword,"[mod")) + nmod=atoi(value); + else if(!strcmp(keyword,"N")) { DSA *dsa; - int n=atoi(buf+4); + int n=atoi(value); printf("[mod = %d]\n\n",nmod); @@ -189,14 +248,21 @@ void keypair() void siggen() { char buf[1024]; + char lbuf[1024]; + char *keyword, *value; int nmod=0; DSA *dsa=NULL; while(fgets(buf,sizeof buf,stdin) != NULL) { - if(!strncmp(buf,"[mod = ",7)) + if (!parse_line(&keyword, &value, lbuf, buf)) + { + fputs(buf,stdout); + continue; + } + if(!strcmp(keyword,"[mod")) { - nmod=atoi(buf+7); + nmod=atoi(value); printf("[mod = %d]\n\n",nmod); dsa=DSA_generate_parameters(nmod,NULL,0,NULL,NULL,NULL,NULL); @@ -205,14 +271,14 @@ void siggen() pbn("G",dsa->g); putc('\n',stdout); } - else if(!strncmp(buf,"Msg = ",6)) + else if(!strcmp(keyword,"Msg")) { unsigned char msg[1024]; unsigned char hash[20]; int n; DSA_SIG *sig; - n=hex2bin(buf+6,msg); + n=hex2bin(value,msg); pv("Msg",msg,n); DSA_generate_key(dsa); @@ -231,26 +297,33 @@ void sigver() { DSA *dsa=NULL; char buf[1024]; + char lbuf[1024]; + char *keyword, *value; int nmod=0; unsigned char hash[20]; DSA_SIG *sig=DSA_SIG_new(); while(fgets(buf,sizeof buf,stdin) != NULL) { - if(!strncmp(buf,"[mod = ",7)) + if (!parse_line(&keyword, &value, lbuf, buf)) + { + fputs(buf,stdout); + continue; + } + if(!strcmp(keyword,"[mod")) { - nmod=atoi(buf+7); + nmod=atoi(value); if(dsa) DSA_free(dsa); dsa=DSA_new(); } - else if(!strncmp(buf,"P = ",4)) - dsa->p=hex2bn(buf+4); - else if(!strncmp(buf,"Q = ",4)) - dsa->q=hex2bn(buf+4); - else if(!strncmp(buf,"G = ",4)) + else if(!strcmp(keyword,"P")) + dsa->p=hex2bn(value); + else if(!strcmp(keyword,"Q")) + dsa->q=hex2bn(value); + else if(!strcmp(keyword,"G")) { - dsa->g=hex2bn(buf+4); + dsa->g=hex2bn(value); printf("[mod = %d]\n\n",nmod); pbn("P",dsa->p); @@ -258,22 +331,22 @@ void sigver() pbn("G",dsa->g); putc('\n',stdout); } - else if(!strncmp(buf,"Msg = ",6)) + else if(!strcmp(keyword,"Msg")) { unsigned char msg[1024]; int n; - n=hex2bin(buf+6,msg); + n=hex2bin(value,msg); pv("Msg",msg,n); SHA1(msg,n,hash); } - else if(!strncmp(buf,"Y = ",4)) - dsa->pub_key=hex2bn(buf+4); - else if(!strncmp(buf,"R = ",4)) - sig->r=hex2bn(buf+4); - else if(!strncmp(buf,"S = ",4)) + else if(!strcmp(keyword,"Y")) + dsa->pub_key=hex2bn(value); + else if(!strcmp(keyword,"R")) + sig->r=hex2bn(value); + else if(!strcmp(keyword,"S")) { - sig->s=hex2bn(buf+4); + sig->s=hex2bn(value); pbn("Y",dsa->pub_key); pbn("R",sig->r); @@ -316,4 +389,5 @@ int main(int argc,char **argv) return 0; } + #endif diff --git a/fips-1.0/rand/fips_rngvs.c b/fips-1.0/rand/fips_rngvs.c index 2c3fdbcca7..fdb38a5c82 100644 --- a/fips-1.0/rand/fips_rngvs.c +++ b/fips-1.0/rand/fips_rngvs.c @@ -25,6 +25,47 @@ int main() #include #include #include +#include + +static int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf) + { + char *keyword, *value, *p, *q; + strcpy(linebuf, olinebuf); + keyword = linebuf; + /* Skip leading space */ + while (isspace((unsigned char)*keyword)) + keyword++; + + /* Look for = sign */ + p = strchr(linebuf, '='); + + /* If no '=' exit */ + if (!p) + return 0; + + q = p - 1; + + /* Remove trailing space */ + while (isspace((unsigned char)*q)) + *q-- = 0; + + *p = 0; + value = p + 1; + + /* Remove leading space from value */ + while (isspace((unsigned char)*value)) + value++; + + /* Remove trailing space from value */ + p = value + strlen(value) - 1; + + while (*p == '\n' || isspace((unsigned char)*p)) + *p-- = 0; + + *pkw = keyword; + *pval = value; + return 1; + } int hex2bin(const char *in, unsigned char *out) { @@ -99,29 +140,30 @@ void vst() unsigned char dt[8]; unsigned char ret[8]; char buf[1024]; + char lbuf[1024]; + char *keyword, *value; int n; while(fgets(buf,sizeof buf,stdin) != NULL) { - if(!strncmp(buf,"Key1 = ",7)) + fputs(buf,stdout); + if (!parse_line(&keyword, &value, lbuf, buf)) + continue; + if(!strcmp(keyword,"Key1")) { - n=hex2bin(buf+7,key1); - pv("Key1",key1,n); + n=hex2bin(value,key1); } - else if(!strncmp(buf,"Key2 = ",7)) + else if(!strcmp(keyword,"Key2")) { - n=hex2bin(buf+7,key2); - pv("Key1",key2,n); + n=hex2bin(value,key2); } - else if(!strncmp(buf,"DT = ",5)) + else if(!strcmp(keyword,"DT")) { - n=hex2bin(buf+5,dt); - pv("DT",dt,n); + n=hex2bin(value,dt); } - else if(!strncmp(buf,"V = ",4)) + else if(!strcmp(keyword,"V")) { - n=hex2bin(buf+4,v); - pv("V",v,n); + n=hex2bin(value,v); FIPS_rand_method()->cleanup(); FIPS_set_prng_key(key1,key2); @@ -137,8 +179,6 @@ void vst() pv("R",ret,8); putc('\n',stdout); } - else - fputs(buf,stdout); } } @@ -151,6 +191,8 @@ void mct() unsigned char dt[8]; unsigned char ret[8]; char buf[1024]; + char lbuf[1024]; + char *keyword, *value; int n; BIGNUM *bn; @@ -159,26 +201,25 @@ void mct() while(fgets(buf,sizeof buf,stdin) != NULL) { - if(!strncmp(buf,"Key1 = ",7)) + fputs(buf,stdout); + if (!parse_line(&keyword, &value, lbuf, buf)) + continue; + if(!strcmp(keyword,"Key1")) { - n=hex2bin(buf+7,key1); - pv("Key1",key1,n); + n=hex2bin(value,key1); } - else if(!strncmp(buf,"Key2 = ",7)) + else if(!strcmp(keyword,"Key2")) { - n=hex2bin(buf+7,key2); - pv("Key1",key2,n); + n=hex2bin(value,key2); } - else if(!strncmp(buf,"DT = ",5)) + else if(!strcmp(keyword,"DT")) { - n=hex2bin(buf+5,dt); - pv("DT",dt,n); + n=hex2bin(value,dt); } - else if(!strncmp(buf,"V = ",4)) + else if(!strcmp(keyword,"V")) { int iter; - n=hex2bin(buf+4,v); - pv("V",v,n); + n=hex2bin(value,v); FIPS_rand_method()->cleanup(); FIPS_set_prng_key(key1,key2); @@ -200,8 +241,6 @@ void mct() pv("R",ret,8); putc('\n',stdout); } - else - fputs(buf,stdout); } BN_free(bn); } diff --git a/fips-1.0/rsa/fips_rsagtest.c b/fips-1.0/rsa/fips_rsagtest.c index 15d3225d53..fdeb05c98b 100644 --- a/fips-1.0/rsa/fips_rsagtest.c +++ b/fips-1.0/rsa/fips_rsagtest.c @@ -217,7 +217,7 @@ int rsa_test(BIO *err, BIO *out, BIO *in) while (isspace((unsigned char)*q)) *q-- = 0; - + *p = 0; value = p + 1; /* Remove leading space from value */ diff --git a/fips-1.0/rsa/fips_rsastest.c b/fips-1.0/rsa/fips_rsastest.c index 880dd636a7..c002065b11 100644 --- a/fips-1.0/rsa/fips_rsastest.c +++ b/fips-1.0/rsa/fips_rsastest.c @@ -210,7 +210,7 @@ int rsa_stest(BIO *err, BIO *out, BIO *in, int Saltlen) while (isspace((unsigned char)*q)) *q-- = 0; - + *p = 0; value = p + 1; /* Remove leading space from value */ diff --git a/fips-1.0/rsa/fips_rsavtest.c b/fips-1.0/rsa/fips_rsavtest.c index 7e2c40424d..8be7e989d7 100644 --- a/fips-1.0/rsa/fips_rsavtest.c +++ b/fips-1.0/rsa/fips_rsavtest.c @@ -213,7 +213,7 @@ int rsa_test(BIO *err, BIO *out, BIO *in, int Saltlen) while (isspace((unsigned char)*q)) *q-- = 0; - + *p = 0; value = p + 1; /* Remove leading space from value */ diff --git a/fips-1.0/sha/fips_shatest.c b/fips-1.0/sha/fips_shatest.c index 4896b467e4..314a4b0da7 100644 --- a/fips-1.0/sha/fips_shatest.c +++ b/fips-1.0/sha/fips_shatest.c @@ -149,7 +149,7 @@ int main(int argc, char **argv) } #define SHA_TEST_MAX_BITS 102400 -#define SHA_TEST_MAXLINELEN (((SHA_TEST_MAX_BITS >> 3) * 2) + 10) +#define SHA_TEST_MAXLINELEN (((SHA_TEST_MAX_BITS >> 3) * 2) + 100) int dgst_test(BIO *err, BIO *out, BIO *in) { @@ -203,7 +203,6 @@ int dgst_test(BIO *err, BIO *out, BIO *in) /* Remove trailing space from value */ p = value + strlen(value) - 1; - while (*p == '\n' || isspace((unsigned char)*p)) *p-- = 0; -- 2.25.1