use different names for asm temp files to avoid problems on some platforms
[openssl.git] / fips / fips_utl.h
index f64a35d8943e1a7409e5170cb5bd1a48254662af..491bc2ace9ab3897982a3c043f4ebd7b699ff2de 100644 (file)
@@ -1,5 +1,5 @@
 /* ====================================================================
- * Copyright (c) 2007 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  *
  */
 
+#ifndef FIPS_UTL_H
+#define FIPS_UTL_H
+
 #define OPENSSL_FIPSAPI
 
 #include <openssl/fips_rand.h>
 #include <openssl/objects.h>
 
+#ifdef OPENSSL_SYS_WIN32
+#define RESP_EOL       "\n"
+#else
+#define RESP_EOL       "\r\n"
+#endif
+
+#ifndef FIPS_AUTH_OFFICER_PASS
+#define FIPS_AUTH_OFFICER_PASS "Default FIPS Crypto Officer Password"
+#endif
+
+#ifndef FIPS_AUTH_USER_PASS
+#define FIPS_AUTH_USER_PASS    "Default FIPS Crypto User Password"
+#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);
 int do_bn_print(FILE *out, const BIGNUM *bn);
 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
 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);
 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode);
 void fips_algtest_init(void);
 void do_entropy_stick(void);
+int fips_strncasecmp(const char *str1, const char *str2, size_t n);
+int fips_strcasecmp(const char *str1, const char *str2);
 
 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);
@@ -134,7 +156,7 @@ void do_entropy_stick(void)
 void fips_algtest_init(void)
        {
        fips_algtest_init_nofips();
-       if (!FIPS_mode_set(1))
+       if (!FIPS_module_mode_set(1, FIPS_AUTH_USER_PASS))
                {
                fprintf(stderr, "Error entering FIPS mode\n");
                exit(1);
@@ -254,11 +276,16 @@ 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;
        }
 
 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
+       {
+       return parse_line2(pkw, pval, linebuf, olinebuf, 1);
+       }
+
+int parse_line2(char **pkw, char **pval, char *linebuf, char *olinebuf, int eol)
        {
        char *keyword, *value, *p, *q;
        strcpy(linebuf, olinebuf);
@@ -290,7 +317,7 @@ int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
        /* Remove trailing space from value */
        p = value + strlen(value) - 1;
 
-       if (*p != '\n')
+       if (eol && *p != '\n')
                fprintf(stderr, "Warning: missing EOL\n");
 
        while (*p == '\n' || isspace((unsigned char)*p))
@@ -365,6 +392,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)
@@ -404,7 +445,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
        {
@@ -412,7 +453,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
@@ -420,3 +461,34 @@ void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
 #endif
     }
 
+/* Not all platforms support strcasecmp and strncasecmp: implement versions
+ * in here to avoid need to include them in the validated module. Taken
+ * from crypto/o_str.c written by Richard Levitte (richard@levitte.org)
+ */
+
+int fips_strncasecmp(const char *str1, const char *str2, size_t n)
+       {
+       while (*str1 && *str2 && n)
+               {
+               int res = toupper(*str1) - toupper(*str2);
+               if (res) return res < 0 ? -1 : 1;
+               str1++;
+               str2++;
+               n--;
+               }
+       if (n == 0)
+               return 0;
+       if (*str1)
+               return 1;
+       if (*str2)
+               return -1;
+       return 0;
+       }
+
+int fips_strcasecmp(const char *str1, const char *str2)
+       {
+       return fips_strncasecmp(str1, str2, (size_t)-1);
+       }
+
+
+#endif