Make fips algorithm test utilities use RESP_EOL for end of line character(s).
authorDr. Stephen Henson <steve@openssl.org>
Sat, 1 Oct 2011 20:42:52 +0000 (20:42 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sat, 1 Oct 2011 20:42:52 +0000 (20:42 +0000)
This should be CRLF even under *nix.

fips/aes/fips_aesavs.c
fips/aes/fips_gcmtest.c
fips/cmac/fips_cmactest.c
fips/des/fips_desmovs.c
fips/dsa/fips_dssvs.c
fips/ecdsa/fips_ecdsavs.c
fips/fips_utl.h
fips/hmac/fips_hmactest.c
fips/rsa/fips_rsastest.c
fips/rsa/fips_rsavtest.c
fips/sha/fips_shatest.c

index dae8023..84bcbac 100644 (file)
@@ -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;
index bfb6c27..3839de8 100644 (file)
@@ -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;
index bd002a2..6d799f2 100644 (file)
@@ -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;
index 30fa01f..e876656 100644 (file)
@@ -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;
index d4487d7..4cb05e2 100644 (file)
@@ -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');
            }
        }
     }
index 4cf536c..898951a 100644 (file)
@@ -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");
                        }
 
                }
index 899422f..c0ce613 100644 (file)
 #include <openssl/fips_rand.h>
 #include <openssl/objects.h>
 
+#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
index 58e4a35..07c18bf 100644 (file)
@@ -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;
        }
 
index 7a3dd31..e0dbe2a 100644 (file)
@@ -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;
 
index dbf73be..df33842 100644 (file)
@@ -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;
 
index cdbf719..c14df16 100644 (file)
@@ -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;