From 4cc2bbab6758b73e005d69cb454cb9ba269859df Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Sat, 1 Oct 2011 20:42:52 +0000 Subject: [PATCH] Make fips algorithm test utilities use RESP_EOL for end of line character(s). This should be CRLF even under *nix. --- fips/aes/fips_aesavs.c | 23 ++++++++++++----------- fips/aes/fips_gcmtest.c | 6 +++--- fips/cmac/fips_cmactest.c | 6 +++--- fips/des/fips_desmovs.c | 27 ++++++++++++++------------- fips/dsa/fips_dssvs.c | 33 ++++++++++++++++----------------- fips/ecdsa/fips_ecdsavs.c | 6 +++--- fips/fips_utl.h | 33 +++++++++++++++++++++++++++------ fips/hmac/fips_hmactest.c | 2 +- fips/rsa/fips_rsastest.c | 6 +++--- fips/rsa/fips_rsavtest.c | 4 ++-- fips/sha/fips_shatest.c | 8 ++++---- 11 files changed, 88 insertions(+), 66 deletions(-) diff --git a/fips/aes/fips_aesavs.c b/fips/aes/fips_aesavs.c index dae8023d0a..84bcbac32a 100644 --- a/fips/aes/fips_aesavs.c +++ b/fips/aes/fips_aesavs.c @@ -283,7 +283,7 @@ static int do_mct(char *amode, /* printf("Iteration %d\n", i); */ if (i > 0) { - fprintf(rfp,"COUNT = %d\n",i); + fprintf(rfp,"COUNT = %d" RESP_EOL ,i); OutputValue("KEY",key[i],nkeysz,rfp,0); if (imode != ECB) /* ECB */ OutputValue("IV",iv[i],AES_BLOCK_SIZE,rfp,0); @@ -419,7 +419,7 @@ static int do_mct(char *amode, /* Output Ciphertext | Plaintext */ OutputValue(t_tag[dir],dir ? ctext[j] : ptext[j],len,rfp, imode == CFB1); - fprintf(rfp, "\n"); /* add separator */ + fprintf(rfp, RESP_EOL); /* add separator */ /* Compute next KEY */ if (dir == XENCRYPT) @@ -622,7 +622,7 @@ static int proc_file(char *rqfile, char *rspfile) } else { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); ++ step; } } @@ -638,11 +638,12 @@ static int proc_file(char *rqfile, char *rspfile) if (akeysz) { /* insert current time & date */ time_t rtim = time(0); - fprintf(rfp, "# %s", ctime(&rtim)); + fputs("# ", rfp); + copy_line(ctime(&rtim), rfp); } else { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (strncmp(pp, "AESVS ", 6) == 0) { strcpy(algo, "AES"); @@ -674,7 +675,7 @@ static int proc_file(char *rqfile, char *rspfile) case 1: /* [ENCRYPT] | [DECRYPT] */ if (ibuf[0] == '[') { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); ++step; if (fips_strncasecmp(ibuf, "[ENCRYPT]", 9) == 0) dir = 1; @@ -697,7 +698,7 @@ static int proc_file(char *rqfile, char *rspfile) step = 2; case 2: /* KEY = xxxx */ - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if(*ibuf == '\n') break; if(!fips_strncasecmp(ibuf,"COUNT = ",8)) @@ -729,7 +730,7 @@ static int proc_file(char *rqfile, char *rspfile) break; case 3: /* IV = xxxx */ - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (fips_strncasecmp(ibuf, "IV = ", 5) != 0) { printf("Missing IV\n"); @@ -750,7 +751,7 @@ static int proc_file(char *rqfile, char *rspfile) break; case 4: /* PLAINTEXT = xxxx */ - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (fips_strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0) { printf("Missing PLAINTEXT\n"); @@ -794,7 +795,7 @@ static int proc_file(char *rqfile, char *rspfile) break; case 5: /* CIPHERTEXT = xxxx */ - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (fips_strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0) { printf("Missing KEY\n"); @@ -839,7 +840,7 @@ static int proc_file(char *rqfile, char *rspfile) } else if (strcmp(atest, "MCT") != 0) { /* MCT already added terminating nl */ - fputs(ibuf, rfp); + copy_line(ibuf, rfp); } step = 1; break; diff --git a/fips/aes/fips_gcmtest.c b/fips/aes/fips_gcmtest.c index bfb6c2700c..3839de8f8a 100644 --- a/fips/aes/fips_gcmtest.c +++ b/fips/aes/fips_gcmtest.c @@ -243,7 +243,7 @@ static void gcmtest(FILE *in, FILE *out, int encrypt) } rv = FIPS_cipher(&ctx, NULL, NULL, 0); if (rv < 0) - fprintf(out, "FAIL\n"); + fprintf(out, "FAIL" RESP_EOL); else OutputValue("PT", pt, ptlen, out, 0); if (iv) @@ -479,11 +479,11 @@ static void ccmtest(FILE *in, FILE *out) { if (rv == 0) Payload[0] = 0; - fputs("Result = Pass\n", out); + fputs("Result = Pass" RESP_EOL, out); OutputValue("Payload", Payload, len, out, 0); } else - fputs("Result = Fail\n", out); + fputs("Result = Fail" RESP_EOL, out); OPENSSL_free(CT); OPENSSL_free(Payload); CT = Payload = NULL; diff --git a/fips/cmac/fips_cmactest.c b/fips/cmac/fips_cmactest.c index bd002a22c2..6d799f2d5f 100644 --- a/fips/cmac/fips_cmactest.c +++ b/fips/cmac/fips_cmactest.c @@ -464,7 +464,7 @@ static int print_cmac_gen(const EVP_CIPHER *cipher, FILE *out, fputs("Mac = ", out); for (i = 0; i < Tlen; i++) fprintf(out, "%02x", res[i]); - fputs("\n", out); + fputs(RESP_EOL, out); rc = 1; } CMAC_CTX_free(cmac_ctx); @@ -502,9 +502,9 @@ static int print_cmac_ver(const EVP_CIPHER *cipher, FILE *out, else { if (!memcmp(Mac, res, Maclen)) - fputs("Result = P\n", out); + fputs("Result = P" RESP_EOL, out); else - fputs("Result = F\n", out); + fputs("Result = F" RESP_EOL, out); } CMAC_CTX_free(cmac_ctx); return rc; diff --git a/fips/des/fips_desmovs.c b/fips/des/fips_desmovs.c index 30fa01fa3c..e8766561ce 100644 --- a/fips/des/fips_desmovs.c +++ b/fips/des/fips_desmovs.c @@ -186,7 +186,7 @@ static void do_mct(char *amode, EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); - fprintf(rfp,"\nCOUNT = %d\n",i); + fprintf(rfp,RESP_EOL "COUNT = %d" RESP_EOL,i); if(kp == 1) OutputValue("KEY",akey,8,rfp,0); else @@ -341,7 +341,7 @@ static int proc_file(char *rqfile, char *rspfile) } else { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); ++ step; } } @@ -357,11 +357,12 @@ static int proc_file(char *rqfile, char *rspfile) if(*amode) { /* insert current time & date */ time_t rtim = time(0); - fprintf(rfp, "# %s", ctime(&rtim)); + fputs("# ", rfp); + copy_line(ctime(&rtim), rfp); } else { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if(!strncmp(pp,"INVERSE ",8) || !strncmp(pp,"DES ",4) || !strncmp(pp,"TDES ",5) || !strncmp(pp,"PERMUTATION ",12) @@ -397,7 +398,7 @@ static int proc_file(char *rqfile, char *rspfile) break; if (ibuf[0] == '[') { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); ++step; if (fips_strncasecmp(ibuf, "[ENCRYPT]", 9) == 0) dir = 1; @@ -422,17 +423,17 @@ static int proc_file(char *rqfile, char *rspfile) case 2: /* KEY = xxxx */ if(*ibuf == '\n') { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); break; } if(!fips_strncasecmp(ibuf,"COUNT = ",8)) { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); break; } if(!fips_strncasecmp(ibuf,"COUNT=",6)) { - fputs(ibuf, rfp); + copy_line(ibuf, rfp); break; } if(!fips_strncasecmp(ibuf,"NumKeys = ",10)) @@ -441,7 +442,7 @@ static int proc_file(char *rqfile, char *rspfile) break; } if (echo) - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if(!fips_strncasecmp(ibuf,"KEY = ",6)) { akeysz=64; @@ -497,7 +498,7 @@ static int proc_file(char *rqfile, char *rspfile) case 3: /* IV = xxxx */ if (echo) - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (fips_strncasecmp(ibuf, "IV = ", 5) != 0) { printf("Missing IV\n"); @@ -519,7 +520,7 @@ static int proc_file(char *rqfile, char *rspfile) case 4: /* PLAINTEXT = xxxx */ if (echo) - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (fips_strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0) { printf("Missing PLAINTEXT\n"); @@ -562,7 +563,7 @@ static int proc_file(char *rqfile, char *rspfile) case 5: /* CIPHERTEXT = xxxx */ if (echo) - fputs(ibuf, rfp); + copy_line(ibuf, rfp); if (fips_strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0) { printf("Missing KEY\n"); @@ -608,7 +609,7 @@ static int proc_file(char *rqfile, char *rspfile) } else if (strcmp(atest, "MCT") != 0) { /* MCT already added terminating nl */ - fputs(ibuf, rfp); + copy_line(ibuf, rfp); } step = 1; break; diff --git a/fips/dsa/fips_dssvs.c b/fips/dsa/fips_dssvs.c index d4487d7cfa..4cb05e296b 100644 --- a/fips/dsa/fips_dssvs.c +++ b/fips/dsa/fips_dssvs.c @@ -101,7 +101,7 @@ static void primes(FILE *in, FILE *out) pp=BN_new(); do_hex2bn(&pp,value); - fprintf(out, "result= %c\n", + fprintf(out, "result= %c" RESP_EOL, BN_is_prime_ex(pp,20,NULL,NULL) ? 'P' : 'F'); } } @@ -190,13 +190,12 @@ static void pqg(FILE *in, FILE *out) seed, M_EVP_MD_size(md), out, 0); if (!dsa2) { - fprintf(out, "c = %d\n",counter); - fprintf(out, "H = %lx\n\n",h); + fprintf(out, "c = %d" RESP_EOL, counter); + fprintf(out, "H = %lx" RESP_EOL RESP_EOL,h); } else { - fprintf(out, "counter = %d\n",counter); - fputs("\n", out); + fprintf(out, "counter = %d" RESP_EOL RESP_EOL, counter); } } } @@ -343,16 +342,16 @@ static void pqgver(FILE *in, FILE *out) if (idx >= 0) { if (BN_cmp(dsa->g, g)) - fprintf(out, "Result = F\n"); + fprintf(out, "Result = F" RESP_EOL); else - fprintf(out, "Result = P\n"); + fprintf(out, "Result = P" RESP_EOL); } else if (BN_cmp(dsa->p, p) || BN_cmp(dsa->q, q) || (!part_test && ((BN_cmp(dsa->g, g) || (counter != counter2) || (h != h2))))) - fprintf(out, "Result = F\n"); + fprintf(out, "Result = F" RESP_EOL); else - fprintf(out, "Result = P\n"); + fprintf(out, "Result = P" RESP_EOL); BN_free(p); BN_free(q); BN_free(g); @@ -470,13 +469,13 @@ static void keyver(FILE *in, FILE *out) paramcheck = -1; } if (paramcheck != 1) - fprintf(out, "Result = F\n"); + fprintf(out, "Result = F" RESP_EOL); else { if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y)) - fprintf(out, "Result = F\n"); + fprintf(out, "Result = F" RESP_EOL); else - fprintf(out, "Result = P\n"); + fprintf(out, "Result = P" RESP_EOL); } BN_free(X); BN_free(Y); @@ -537,7 +536,7 @@ static void keypair(FILE *in, FILE *out) do_bn_print_name(out, "P",dsa->p); do_bn_print_name(out, "Q",dsa->q); do_bn_print_name(out, "G",dsa->g); - fputs("\n", out); + fputs(RESP_EOL, out); while(n--) { @@ -546,7 +545,7 @@ static void keypair(FILE *in, FILE *out) do_bn_print_name(out, "X",dsa->priv_key); do_bn_print_name(out, "Y",dsa->pub_key); - fputs("\n", out); + fputs(RESP_EOL, out); } } } @@ -594,7 +593,7 @@ static void siggen(FILE *in, FILE *out) do_bn_print_name(out, "P",dsa->p); do_bn_print_name(out, "Q",dsa->q); do_bn_print_name(out, "G",dsa->g); - fputs("\n", out); + fputs(RESP_EOL, out); } else if(!strcmp(keyword,"Msg")) { @@ -616,7 +615,7 @@ static void siggen(FILE *in, FILE *out) do_bn_print_name(out, "R",sig->r); do_bn_print_name(out, "S",sig->s); - fputs("\n", out); + fputs(RESP_EOL, out); FIPS_dsa_sig_free(sig); FIPS_md_ctx_cleanup(&mctx); } @@ -685,7 +684,7 @@ static void sigver(FILE *in, FILE *out) no_err = 0; FIPS_md_ctx_cleanup(&mctx); - fprintf(out, "Result = %c\n\n", r == 1 ? 'P' : 'F'); + fprintf(out, "Result = %c" RESP_EOL RESP_EOL, r == 1 ? 'P' : 'F'); } } } diff --git a/fips/ecdsa/fips_ecdsavs.c b/fips/ecdsa/fips_ecdsavs.c index 4cf536cee8..898951a2c8 100644 --- a/fips/ecdsa/fips_ecdsavs.c +++ b/fips/ecdsa/fips_ecdsavs.c @@ -234,7 +234,7 @@ static int KeyPair(FILE *in, FILE *out) do_bn_print_name(out, "d", d); do_bn_print_name(out, "Qx", Qx); do_bn_print_name(out, "Qy", Qy); - fputs("\n", out); + fputs(RESP_EOL, out); EC_KEY_free(key); } @@ -287,7 +287,7 @@ static int PKV(FILE *in, FILE *out) no_err = 1; rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy); no_err = 0; - fprintf(out, "Result = %s\n", rv ? "P":"F"); + fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F"); } } @@ -453,7 +453,7 @@ static int SigVer(FILE *in, FILE *out) rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig); no_err = 0; - fprintf(out, "Result = %s\n", rv ? "P":"F"); + fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F"); } } diff --git a/fips/fips_utl.h b/fips/fips_utl.h index 899422f438..c0ce613f47 100644 --- a/fips/fips_utl.h +++ b/fips/fips_utl.h @@ -52,6 +52,12 @@ #include #include +#ifdef OPENSSL_SYS_WIN32 +#define RESP_EOL "\n" +#else +#define RESP_EOL "\r\n" +#endif + int hex2bin(const char *in, unsigned char *out); unsigned char *hex2bin_m(const char *in, long *plen); int do_hex2bn(BIGNUM **pr, const char *in); @@ -61,6 +67,7 @@ int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf); int parse_line2(char **pkw, char **pval, char *linebuf, char *olinebuf, int eol); BIGNUM *hex2bn(const char *in); int tidy_line(char *linebuf, char *olinebuf); +int copy_line(const char *in, FILE *ofp); int bint2bin(const char *in, int len, unsigned char *out); int bin2bint(const unsigned char *in,int len,char *out); void PrintValue(char *tag, unsigned char *val, int len); @@ -74,9 +81,9 @@ static int no_err; static void put_err_cb(int lib, int func,int reason,const char *file,int line) { - if (no_err) - return; - fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d" + if (no_err) + return; + fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d" ":file=%s:line=%d\n", ERR_PACK(lib, func, reason), lib, func, reason, file, line); @@ -257,7 +264,7 @@ int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn) r = do_bn_print(out, bn); if (!r) return 0; - fputs("\n", out); + fputs(RESP_EOL, out); return 1; } @@ -373,6 +380,20 @@ int tidy_line(char *linebuf, char *olinebuf) return 1; } +/* Copy supplied line to ofp replacing \n with \r\n */ +int copy_line(const char *in, FILE *ofp) + { + const char *p; + p = strchr(in, '\n'); + if (p) + { + fwrite(in, 1, (size_t)(p - in), ofp); + fputs(RESP_EOL, ofp); + } + else + fputs(in, ofp); + return 1; + } /* NB: this return the number of _bits_ read */ int bint2bin(const char *in, int len, unsigned char *out) @@ -412,7 +433,7 @@ void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode) if(bitmode) { olen=bin2bint(val,len,obuf); - fprintf(rfp, "%s = %.*s\n", tag, olen, obuf); + fprintf(rfp, "%s = %.*s" RESP_EOL, tag, olen, obuf); } else { @@ -420,7 +441,7 @@ void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode) fprintf(rfp, "%s = ", tag); for (i = 0; i < len; i++) fprintf(rfp, "%02x", val[i]); - fputs("\n", rfp); + fputs(RESP_EOL, rfp); } #if VERBOSE diff --git a/fips/hmac/fips_hmactest.c b/fips/hmac/fips_hmactest.c index 58e4a35a5b..07c18bfdfa 100644 --- a/fips/hmac/fips_hmactest.c +++ b/fips/hmac/fips_hmactest.c @@ -313,7 +313,7 @@ static int print_hmac(const EVP_MD *emd, FILE *out, fputs("Mac = ", out); for (i = 0; i < Tlen; i++) fprintf(out, "%02x", md[i]); - fputs("\n", out); + fputs(RESP_EOL, out); return 1; } diff --git a/fips/rsa/fips_rsastest.c b/fips/rsa/fips_rsastest.c index 7a3dd31cbb..e0dbe2a0d7 100644 --- a/fips/rsa/fips_rsastest.c +++ b/fips/rsa/fips_rsastest.c @@ -276,9 +276,9 @@ int rsa_stest(FILE *out, FILE *in, int Saltlen) BN_free(bn_e); fputs("n = ", out); do_bn_print(out, rsa->n); - fputs("\ne = ", out); + fputs(RESP_EOL "e = ", out); do_bn_print(out, rsa->e); - fputs("\n", out); + fputs(RESP_EOL, out); current_keylen = keylen; } @@ -352,7 +352,7 @@ static int rsa_printsig(FILE *out, RSA *rsa, const EVP_MD *dgst, for (i = 0; i < siglen; i++) fprintf(out, "%02X", sigbuf[i]); - fputs("\n", out); + fputs(RESP_EOL, out); ret = 1; diff --git a/fips/rsa/fips_rsavtest.c b/fips/rsa/fips_rsavtest.c index dbf73bee88..df33842691 100644 --- a/fips/rsa/fips_rsavtest.c +++ b/fips/rsa/fips_rsavtest.c @@ -355,9 +355,9 @@ static int rsa_printver(FILE *out, goto error; if (r == 0) - fputs("Result = F\n", out); + fputs("Result = F" RESP_EOL, out); else - fputs("Result = P\n", out); + fputs("Result = P" RESP_EOL, out); ret = 1; diff --git a/fips/sha/fips_shatest.c b/fips/sha/fips_shatest.c index cdbf7193b6..c14df16601 100644 --- a/fips/sha/fips_shatest.c +++ b/fips/sha/fips_shatest.c @@ -305,7 +305,7 @@ static int print_dgst(const EVP_MD *emd, FILE *out, fputs("MD = ", out); for (i = 0; i < mdlen; i++) fprintf(out, "%02x", md[i]); - fputs("\n", out); + fputs(RESP_EOL, out); return 1; } @@ -337,7 +337,7 @@ static int print_monte(const EVP_MD *md, FILE *out, memcpy(m2, Seed, SeedLen); memcpy(m3, Seed, SeedLen); - fputs("\n", out); + fputs(RESP_EOL, out); for (j = 0; j < 100; j++) { @@ -355,11 +355,11 @@ static int print_monte(const EVP_MD *md, FILE *out, m3 = p; FIPS_digestfinal(&ctx, m3, &m3len); } - fprintf(out, "COUNT = %d\n", j); + fprintf(out, "COUNT = %d" RESP_EOL, j); fputs("MD = ", out); for (k = 0; k < m3len; k++) fprintf(out, "%02x", m3[k]); - fputs("\n\n", out); + fputs(RESP_EOL RESP_EOL, out); memcpy(m1, m3, m3len); memcpy(m2, m3, m3len); m1len = m2len = m3len; -- 2.25.1