Make algorithm test programs tolerate whitespace in input files.
authorDr. Stephen Henson <steve@openssl.org>
Sun, 14 Jan 2007 17:01:31 +0000 (17:01 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sun, 14 Jan 2007 17:01:31 +0000 (17:01 +0000)
CHANGES
fips-1.0/aes/fips_aesavs.c
fips-1.0/des/fips_desmovs.c
fips-1.0/dsa/fips_dssvs.c
fips-1.0/rand/fips_rngvs.c
fips-1.0/rsa/fips_rsagtest.c
fips-1.0/rsa/fips_rsastest.c
fips-1.0/rsa/fips_rsavtest.c
fips-1.0/sha/fips_shatest.c

diff --git a/CHANGES b/CHANGES
index 3708edcae04fd036a51e858710c97b8e043945f1..3cd2a3fa7bc653e3f7757c2b53e7ae8315c5a42a 100644 (file)
--- 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
index 6bb9b899c868f1a7317d1064de239a4b77de30c5..52211265d64819fd2041ae666a462384e42302a6 100644 (file)
@@ -62,6 +62,7 @@
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
+#include <ctype.h>
 
 #include <openssl/aes.h>
 #include <openssl/evp.h>
@@ -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)
index 5eb55726e3b86b0db5cbdbbb22310ad8b41070e3..4816e338adac924ce01405d489d8239b5562b8fa 100644 (file)
@@ -63,6 +63,7 @@
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
+#include <ctype.h>
 
 #include <openssl/des.h>
 #include <openssl/evp.h>
 #include <openssl/err.h>
 #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"))
index 560d6359810fc8d276994a2a7fb1c5c80d445c71..fe4e54f28700cb343993e7a3655073b1a10a5dca 100644 (file)
@@ -16,6 +16,47 @@ int main()
 #include <openssl/err.h>
 #include <openssl/fips_sha.h>
 #include <string.h>
+#include <ctype.h>
+
+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
index 2c3fdbcca7c1f1ed47dc566312d5adc95ed7d483..fdb38a5c82d6e1cf7d7e3caa04f6cfb325499296 100644 (file)
@@ -25,6 +25,47 @@ int main()
 #include <openssl/rand.h>
 #include <openssl/fips_rand.h>
 #include <string.h>
+#include <ctype.h>
+
+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);
     }
index 15d3225d53815c00849f98c5df357e1a48812f27..fdeb05c98ba79f0e9ddf6b6604bca75b0fe02d1d 100644 (file)
@@ -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 */
index 880dd636a7d87aa33e794ebf0c0b45e3abf177ce..c002065b11584399739e9035abf0400a19f3bc3e 100644 (file)
@@ -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 */
index 7e2c40424d2ab3259fb667059c67deaf4bb64acb..8be7e989d7ae89380bcfe63f16ded3a7936b33f3 100644 (file)
@@ -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 */
index 4896b467e42d9f3551b769486ce9773c3604d1bf..314a4b0da736da37555f518a4f765c3c94a8853d 100644 (file)
@@ -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;