Make algorithm test programs tolerate whitespace in input files.

This commit is contained in:
Dr. Stephen Henson 2007-01-14 17:01:31 +00:00
parent bdc778a795
commit 7c4dd3fefe
9 changed files with 294 additions and 64 deletions

View File

@ -4,6 +4,9 @@
Changes between 0.9.7l and 0.9.7m [xx XXX xxxx] 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 *) Have SSL/TLS server implementation tolerate "mismatched" record
protocol version while receiving ClientHello even if the protocol version while receiving ClientHello even if the
ClientHello is fragmented. (The server can't insist on the ClientHello is fragmented. (The server can't insist on the

View File

@ -62,6 +62,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <ctype.h>
#include <openssl/aes.h> #include <openssl/aes.h>
#include <openssl/evp.h> #include <openssl/evp.h>
@ -622,6 +623,61 @@ int do_mct(char *amode,
return ret; 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 # Config info for v-one
@ -636,6 +692,7 @@ int proc_file(char *rqfile)
char afn[256], rfn[256]; char afn[256], rfn[256];
FILE *afp = NULL, *rfp = NULL; FILE *afp = NULL, *rfp = NULL;
char ibuf[2048]; char ibuf[2048];
char tbuf[2048];
int ilen, len, ret = 0; int ilen, len, ret = 0;
char algo[8] = ""; char algo[8] = "";
char amode[8] = ""; char amode[8] = "";
@ -677,6 +734,7 @@ int proc_file(char *rqfile)
} }
while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
{ {
tidy_line(tbuf, ibuf);
ilen = strlen(ibuf); ilen = strlen(ibuf);
/* printf("step=%d ibuf=%s",step,ibuf); */ /* printf("step=%d ibuf=%s",step,ibuf); */
switch (step) switch (step)

View File

@ -63,6 +63,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <ctype.h>
#include <openssl/des.h> #include <openssl/des.h>
#include <openssl/evp.h> #include <openssl/evp.h>
@ -70,6 +71,61 @@
#include <openssl/err.h> #include <openssl/err.h>
#include "e_os.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 AES_BLOCK_SIZE 16*/
#define VERBOSE 0 #define VERBOSE 0
@ -414,7 +470,7 @@ int proc_file(char *rqfile)
{ {
char afn[256], rfn[256]; char afn[256], rfn[256];
FILE *afp = NULL, *rfp = NULL; FILE *afp = NULL, *rfp = NULL;
char ibuf[2048]; char ibuf[2048], tbuf[2048];
int ilen, len, ret = 0; int ilen, len, ret = 0;
char amode[8] = ""; char amode[8] = "";
char atest[100] = ""; char atest[100] = "";
@ -456,6 +512,7 @@ int proc_file(char *rqfile)
} }
while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
{ {
tidy_line(tbuf, ibuf);
ilen = strlen(ibuf); ilen = strlen(ibuf);
/* printf("step=%d ibuf=%s",step,ibuf);*/ /* printf("step=%d ibuf=%s",step,ibuf);*/
if(step == 3 && !strcmp(amode,"ECB")) if(step == 3 && !strcmp(amode,"ECB"))

View File

@ -16,6 +16,47 @@ int main()
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/fips_sha.h> #include <openssl/fips_sha.h>
#include <string.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) int hex2bin(const char *in, unsigned char *out)
{ {
@ -99,16 +140,20 @@ void pbn(const char *tag,const BIGNUM *val)
void primes() void primes()
{ {
char buf[10240]; char buf[10240];
char lbuf[10240];
char *keyword, *value;
while(fgets(buf,sizeof buf,stdin) != NULL) while(fgets(buf,sizeof buf,stdin) != NULL)
{ {
fputs(buf,stdout); fputs(buf,stdout);
if(!strncmp(buf,"Prime= ",7)) if (!parse_line(&keyword, &value, lbuf, buf))
continue;
if(!strcmp(keyword,"Prime"))
{ {
BIGNUM *pp; BIGNUM *pp;
pp=BN_new(); pp=BN_new();
BN_hex2bn(&pp,buf+7); BN_hex2bn(&pp,value);
printf("result= %c\n", printf("result= %c\n",
BN_is_prime(pp,20,NULL,NULL,NULL) ? 'P' : 'F'); BN_is_prime(pp,20,NULL,NULL,NULL) ? 'P' : 'F');
} }
@ -118,15 +163,22 @@ void primes()
void pqg() void pqg()
{ {
char buf[1024]; char buf[1024];
char lbuf[1024];
char *keyword, *value;
int nmod=0; int nmod=0;
while(fgets(buf,sizeof buf,stdin) != NULL) while(fgets(buf,sizeof buf,stdin) != NULL)
{ {
if(!strncmp(buf,"[mod = ",7)) if (!parse_line(&keyword, &value, lbuf, buf))
nmod=atoi(buf+7);
else if(!strncmp(buf,"N = ",4))
{ {
int n=atoi(buf+4); fputs(buf,stdout);
continue;
}
if(!strcmp(keyword,"[mod"))
nmod=atoi(value);
else if(!strcmp(keyword,"N"))
{
int n=atoi(value);
printf("[mod = %d]\n\n",nmod); printf("[mod = %d]\n\n",nmod);
@ -155,16 +207,23 @@ void pqg()
void keypair() void keypair()
{ {
char buf[1024]; char buf[1024];
char lbuf[1024];
char *keyword, *value;
int nmod=0; int nmod=0;
while(fgets(buf,sizeof buf,stdin) != NULL) while(fgets(buf,sizeof buf,stdin) != NULL)
{ {
if(!strncmp(buf,"[mod = ",7)) if (!parse_line(&keyword, &value, lbuf, buf))
nmod=atoi(buf+7); {
else if(!strncmp(buf,"N = ",4)) fputs(buf,stdout);
continue;
}
if(!strcmp(keyword,"[mod"))
nmod=atoi(value);
else if(!strcmp(keyword,"N"))
{ {
DSA *dsa; DSA *dsa;
int n=atoi(buf+4); int n=atoi(value);
printf("[mod = %d]\n\n",nmod); printf("[mod = %d]\n\n",nmod);
@ -189,14 +248,21 @@ void keypair()
void siggen() void siggen()
{ {
char buf[1024]; char buf[1024];
char lbuf[1024];
char *keyword, *value;
int nmod=0; int nmod=0;
DSA *dsa=NULL; DSA *dsa=NULL;
while(fgets(buf,sizeof buf,stdin) != NULL) while(fgets(buf,sizeof buf,stdin) != NULL)
{ {
if(!strncmp(buf,"[mod = ",7)) if (!parse_line(&keyword, &value, lbuf, buf))
{ {
nmod=atoi(buf+7); fputs(buf,stdout);
continue;
}
if(!strcmp(keyword,"[mod"))
{
nmod=atoi(value);
printf("[mod = %d]\n\n",nmod); printf("[mod = %d]\n\n",nmod);
dsa=DSA_generate_parameters(nmod,NULL,0,NULL,NULL,NULL,NULL); dsa=DSA_generate_parameters(nmod,NULL,0,NULL,NULL,NULL,NULL);
@ -205,14 +271,14 @@ void siggen()
pbn("G",dsa->g); pbn("G",dsa->g);
putc('\n',stdout); putc('\n',stdout);
} }
else if(!strncmp(buf,"Msg = ",6)) else if(!strcmp(keyword,"Msg"))
{ {
unsigned char msg[1024]; unsigned char msg[1024];
unsigned char hash[20]; unsigned char hash[20];
int n; int n;
DSA_SIG *sig; DSA_SIG *sig;
n=hex2bin(buf+6,msg); n=hex2bin(value,msg);
pv("Msg",msg,n); pv("Msg",msg,n);
DSA_generate_key(dsa); DSA_generate_key(dsa);
@ -231,26 +297,33 @@ void sigver()
{ {
DSA *dsa=NULL; DSA *dsa=NULL;
char buf[1024]; char buf[1024];
char lbuf[1024];
char *keyword, *value;
int nmod=0; int nmod=0;
unsigned char hash[20]; unsigned char hash[20];
DSA_SIG *sig=DSA_SIG_new(); DSA_SIG *sig=DSA_SIG_new();
while(fgets(buf,sizeof buf,stdin) != NULL) while(fgets(buf,sizeof buf,stdin) != NULL)
{ {
if(!strncmp(buf,"[mod = ",7)) if (!parse_line(&keyword, &value, lbuf, buf))
{ {
nmod=atoi(buf+7); fputs(buf,stdout);
continue;
}
if(!strcmp(keyword,"[mod"))
{
nmod=atoi(value);
if(dsa) if(dsa)
DSA_free(dsa); DSA_free(dsa);
dsa=DSA_new(); dsa=DSA_new();
} }
else if(!strncmp(buf,"P = ",4)) else if(!strcmp(keyword,"P"))
dsa->p=hex2bn(buf+4); dsa->p=hex2bn(value);
else if(!strncmp(buf,"Q = ",4)) else if(!strcmp(keyword,"Q"))
dsa->q=hex2bn(buf+4); dsa->q=hex2bn(value);
else if(!strncmp(buf,"G = ",4)) else if(!strcmp(keyword,"G"))
{ {
dsa->g=hex2bn(buf+4); dsa->g=hex2bn(value);
printf("[mod = %d]\n\n",nmod); printf("[mod = %d]\n\n",nmod);
pbn("P",dsa->p); pbn("P",dsa->p);
@ -258,22 +331,22 @@ void sigver()
pbn("G",dsa->g); pbn("G",dsa->g);
putc('\n',stdout); putc('\n',stdout);
} }
else if(!strncmp(buf,"Msg = ",6)) else if(!strcmp(keyword,"Msg"))
{ {
unsigned char msg[1024]; unsigned char msg[1024];
int n; int n;
n=hex2bin(buf+6,msg); n=hex2bin(value,msg);
pv("Msg",msg,n); pv("Msg",msg,n);
SHA1(msg,n,hash); SHA1(msg,n,hash);
} }
else if(!strncmp(buf,"Y = ",4)) else if(!strcmp(keyword,"Y"))
dsa->pub_key=hex2bn(buf+4); dsa->pub_key=hex2bn(value);
else if(!strncmp(buf,"R = ",4)) else if(!strcmp(keyword,"R"))
sig->r=hex2bn(buf+4); sig->r=hex2bn(value);
else if(!strncmp(buf,"S = ",4)) else if(!strcmp(keyword,"S"))
{ {
sig->s=hex2bn(buf+4); sig->s=hex2bn(value);
pbn("Y",dsa->pub_key); pbn("Y",dsa->pub_key);
pbn("R",sig->r); pbn("R",sig->r);
@ -316,4 +389,5 @@ int main(int argc,char **argv)
return 0; return 0;
} }
#endif #endif

View File

@ -25,6 +25,47 @@ int main()
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/fips_rand.h> #include <openssl/fips_rand.h>
#include <string.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) int hex2bin(const char *in, unsigned char *out)
{ {
@ -99,29 +140,30 @@ void vst()
unsigned char dt[8]; unsigned char dt[8];
unsigned char ret[8]; unsigned char ret[8];
char buf[1024]; char buf[1024];
char lbuf[1024];
char *keyword, *value;
int n; int n;
while(fgets(buf,sizeof buf,stdin) != NULL) 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); n=hex2bin(value,key1);
pv("Key1",key1,n);
} }
else if(!strncmp(buf,"Key2 = ",7)) else if(!strcmp(keyword,"Key2"))
{ {
n=hex2bin(buf+7,key2); n=hex2bin(value,key2);
pv("Key1",key2,n);
} }
else if(!strncmp(buf,"DT = ",5)) else if(!strcmp(keyword,"DT"))
{ {
n=hex2bin(buf+5,dt); n=hex2bin(value,dt);
pv("DT",dt,n);
} }
else if(!strncmp(buf,"V = ",4)) else if(!strcmp(keyword,"V"))
{ {
n=hex2bin(buf+4,v); n=hex2bin(value,v);
pv("V",v,n);
FIPS_rand_method()->cleanup(); FIPS_rand_method()->cleanup();
FIPS_set_prng_key(key1,key2); FIPS_set_prng_key(key1,key2);
@ -137,8 +179,6 @@ void vst()
pv("R",ret,8); pv("R",ret,8);
putc('\n',stdout); putc('\n',stdout);
} }
else
fputs(buf,stdout);
} }
} }
@ -151,6 +191,8 @@ void mct()
unsigned char dt[8]; unsigned char dt[8];
unsigned char ret[8]; unsigned char ret[8];
char buf[1024]; char buf[1024];
char lbuf[1024];
char *keyword, *value;
int n; int n;
BIGNUM *bn; BIGNUM *bn;
@ -159,26 +201,25 @@ void mct()
while(fgets(buf,sizeof buf,stdin) != NULL) 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); n=hex2bin(value,key1);
pv("Key1",key1,n);
} }
else if(!strncmp(buf,"Key2 = ",7)) else if(!strcmp(keyword,"Key2"))
{ {
n=hex2bin(buf+7,key2); n=hex2bin(value,key2);
pv("Key1",key2,n);
} }
else if(!strncmp(buf,"DT = ",5)) else if(!strcmp(keyword,"DT"))
{ {
n=hex2bin(buf+5,dt); n=hex2bin(value,dt);
pv("DT",dt,n);
} }
else if(!strncmp(buf,"V = ",4)) else if(!strcmp(keyword,"V"))
{ {
int iter; int iter;
n=hex2bin(buf+4,v); n=hex2bin(value,v);
pv("V",v,n);
FIPS_rand_method()->cleanup(); FIPS_rand_method()->cleanup();
FIPS_set_prng_key(key1,key2); FIPS_set_prng_key(key1,key2);
@ -200,8 +241,6 @@ void mct()
pv("R",ret,8); pv("R",ret,8);
putc('\n',stdout); putc('\n',stdout);
} }
else
fputs(buf,stdout);
} }
BN_free(bn); BN_free(bn);
} }

View File

@ -217,7 +217,7 @@ int rsa_test(BIO *err, BIO *out, BIO *in)
while (isspace((unsigned char)*q)) while (isspace((unsigned char)*q))
*q-- = 0; *q-- = 0;
*p = 0;
value = p + 1; value = p + 1;
/* Remove leading space from value */ /* Remove leading space from value */

View File

@ -210,7 +210,7 @@ int rsa_stest(BIO *err, BIO *out, BIO *in, int Saltlen)
while (isspace((unsigned char)*q)) while (isspace((unsigned char)*q))
*q-- = 0; *q-- = 0;
*p = 0;
value = p + 1; value = p + 1;
/* Remove leading space from value */ /* Remove leading space from value */

View File

@ -213,7 +213,7 @@ int rsa_test(BIO *err, BIO *out, BIO *in, int Saltlen)
while (isspace((unsigned char)*q)) while (isspace((unsigned char)*q))
*q-- = 0; *q-- = 0;
*p = 0;
value = p + 1; value = p + 1;
/* Remove leading space from value */ /* Remove leading space from value */

View File

@ -149,7 +149,7 @@ int main(int argc, char **argv)
} }
#define SHA_TEST_MAX_BITS 102400 #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) 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 */ /* Remove trailing space from value */
p = value + strlen(value) - 1; p = value + strlen(value) - 1;
while (*p == '\n' || isspace((unsigned char)*p)) while (*p == '\n' || isspace((unsigned char)*p))
*p-- = 0; *p-- = 0;