2011-01-26 00:56:19 +00:00
|
|
|
/* ====================================================================
|
2011-05-01 16:18:52 +00:00
|
|
|
* Copyright (c) 2011 The OpenSSL Project. All rights reserved.
|
2011-01-26 00:56:19 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* openssl-core@openssl.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-01-27 17:23:43 +00:00
|
|
|
#define OPENSSL_FIPSAPI
|
|
|
|
|
2011-04-01 14:46:07 +00:00
|
|
|
#include <openssl/fips_rand.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
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);
|
2011-02-15 14:16:57 +00:00
|
|
|
int do_bn_print(FILE *out, const BIGNUM *bn);
|
|
|
|
int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
|
2011-01-26 00:56:19 +00:00
|
|
|
int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
|
|
|
|
BIGNUM *hex2bn(const char *in);
|
|
|
|
int tidy_line(char *linebuf, char *olinebuf);
|
|
|
|
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);
|
2011-03-25 16:36:46 +00:00
|
|
|
void fips_algtest_init(void);
|
2011-04-21 14:17:15 +00:00
|
|
|
void do_entropy_stick(void);
|
2011-05-01 16:18:52 +00:00
|
|
|
int fips_strncasecmp(const char *str1, const char *str2, size_t n);
|
|
|
|
int fips_strcasecmp(const char *str1, const char *str2);
|
2011-01-26 00:56:19 +00:00
|
|
|
|
|
|
|
static int no_err;
|
|
|
|
|
|
|
|
static void put_err_cb(int lib, int func,int reason,const char *file,int line)
|
|
|
|
{
|
|
|
|
if (no_err)
|
|
|
|
return;
|
2011-03-24 22:59:02 +00:00
|
|
|
fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
|
2011-01-26 00:56:19 +00:00
|
|
|
":file=%s:line=%d\n",
|
2011-03-24 22:59:02 +00:00
|
|
|
ERR_PACK(lib, func, reason),
|
2011-01-26 00:56:19 +00:00
|
|
|
lib, func, reason, file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_err_cb(int num, va_list args)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *str;
|
|
|
|
if (no_err)
|
|
|
|
return;
|
|
|
|
fputs("\t", stderr);
|
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
str = va_arg(args, char *);
|
|
|
|
if (str)
|
|
|
|
fputs(str, stderr);
|
|
|
|
}
|
|
|
|
fputs("\n", stderr);
|
|
|
|
}
|
|
|
|
|
2011-04-01 14:46:07 +00:00
|
|
|
/* Dummy Entropy to keep DRBG happy. WARNING: THIS IS TOTALLY BOGUS
|
|
|
|
* HAS ZERO SECURITY AND MUST NOT BE USED IN REAL APPLICATIONS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static unsigned char dummy_entropy[1024];
|
|
|
|
|
|
|
|
static size_t dummy_cb(DRBG_CTX *ctx, unsigned char **pout,
|
|
|
|
int entropy, size_t min_len, size_t max_len)
|
|
|
|
{
|
|
|
|
*pout = dummy_entropy;
|
|
|
|
return min_len;
|
|
|
|
}
|
|
|
|
|
2011-04-21 14:17:15 +00:00
|
|
|
static int entropy_stick = 0;
|
|
|
|
|
2011-04-01 14:46:07 +00:00
|
|
|
static void fips_algtest_init_nofips(void)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-04-01 14:46:07 +00:00
|
|
|
DRBG_CTX *ctx;
|
2011-04-21 14:17:15 +00:00
|
|
|
size_t i;
|
2011-01-26 00:56:19 +00:00
|
|
|
FIPS_set_error_callbacks(put_err_cb, add_err_cb);
|
2011-04-21 14:17:15 +00:00
|
|
|
for (i = 0; i < sizeof(dummy_entropy); i++)
|
|
|
|
dummy_entropy[i] = i & 0xff;
|
|
|
|
if (entropy_stick)
|
|
|
|
memcpy(dummy_entropy + 32, dummy_entropy + 16, 16);
|
2011-04-01 14:46:07 +00:00
|
|
|
ctx = FIPS_get_default_drbg();
|
|
|
|
FIPS_drbg_init(ctx, NID_aes_256_ctr, DRBG_FLAG_CTR_USE_DF);
|
2011-04-21 14:17:15 +00:00
|
|
|
FIPS_drbg_set_callbacks(ctx, dummy_cb, 0, 16, dummy_cb, 0);
|
2011-04-01 14:46:07 +00:00
|
|
|
FIPS_drbg_instantiate(ctx, dummy_entropy, 10);
|
2011-04-05 15:24:10 +00:00
|
|
|
FIPS_rand_set_method(FIPS_drbg_method());
|
2011-01-26 00:56:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-21 14:17:15 +00:00
|
|
|
void do_entropy_stick(void)
|
|
|
|
{
|
|
|
|
entropy_stick = 1;
|
|
|
|
}
|
|
|
|
|
2011-03-25 16:36:46 +00:00
|
|
|
void fips_algtest_init(void)
|
|
|
|
{
|
2011-04-01 14:46:07 +00:00
|
|
|
fips_algtest_init_nofips();
|
2011-05-11 14:43:38 +00:00
|
|
|
if (!FIPS_module_mode_set(1))
|
2011-03-25 16:36:46 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Error entering FIPS mode\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
int hex2bin(const char *in, unsigned char *out)
|
|
|
|
{
|
2011-02-15 15:56:13 +00:00
|
|
|
int n1, n2, isodd = 0;
|
2011-01-26 00:56:19 +00:00
|
|
|
unsigned char ch;
|
|
|
|
|
2011-02-15 15:56:13 +00:00
|
|
|
n1 = strlen(in);
|
|
|
|
if (in[n1 - 1] == '\n')
|
|
|
|
n1--;
|
|
|
|
|
|
|
|
if (n1 & 1)
|
|
|
|
isodd = 1;
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
|
|
|
|
{ /* first byte */
|
|
|
|
if ((in[n1] >= '0') && (in[n1] <= '9'))
|
|
|
|
ch = in[n1++] - '0';
|
|
|
|
else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
|
|
|
|
ch = in[n1++] - 'A' + 10;
|
|
|
|
else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
|
|
|
|
ch = in[n1++] - 'a' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
if(!in[n1])
|
|
|
|
{
|
|
|
|
out[n2++]=ch;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-14 19:42:49 +00:00
|
|
|
/* If input is odd length first digit is least significant: assumes
|
|
|
|
* all digits valid hex and null terminated which is true for the
|
|
|
|
* strings we pass.
|
|
|
|
*/
|
2011-02-15 15:56:13 +00:00
|
|
|
if (n1 == 1 && isodd)
|
2011-02-14 19:42:49 +00:00
|
|
|
{
|
|
|
|
out[n2++] = ch;
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
out[n2] = ch << 4;
|
|
|
|
/* second byte */
|
|
|
|
if ((in[n1] >= '0') && (in[n1] <= '9'))
|
|
|
|
ch = in[n1++] - '0';
|
|
|
|
else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
|
|
|
|
ch = in[n1++] - 'A' + 10;
|
|
|
|
else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
|
|
|
|
ch = in[n1++] - 'a' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
out[n2++] |= ch;
|
|
|
|
}
|
|
|
|
return n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *hex2bin_m(const char *in, long *plen)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
2011-02-08 18:15:59 +00:00
|
|
|
if (strlen(in) == 0)
|
|
|
|
{
|
|
|
|
*plen = 0;
|
|
|
|
return OPENSSL_malloc(1);
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
p = OPENSSL_malloc((strlen(in) + 1)/2);
|
|
|
|
*plen = hex2bin(in, p);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_hex2bn(BIGNUM **pr, const char *in)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
long plen;
|
|
|
|
int r = 0;
|
|
|
|
p = hex2bin_m(in, &plen);
|
|
|
|
if (!p)
|
|
|
|
return 0;
|
|
|
|
if (!*pr)
|
|
|
|
*pr = BN_new();
|
|
|
|
if (!*pr)
|
|
|
|
return 0;
|
|
|
|
if (BN_bin2bn(p, plen, *pr))
|
|
|
|
r = 1;
|
|
|
|
OPENSSL_free(p);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:16:57 +00:00
|
|
|
int do_bn_print(FILE *out, const BIGNUM *bn)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
unsigned char *tmp;
|
|
|
|
len = BN_num_bytes(bn);
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
fputs("00", out);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = OPENSSL_malloc(len);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Memory allocation error\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
BN_bn2bin(bn, tmp);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
fprintf(out, "%02x", tmp[i]);
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:16:57 +00:00
|
|
|
int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
fprintf(out, "%s = ", name);
|
|
|
|
r = do_bn_print(out, bn);
|
|
|
|
if (!r)
|
|
|
|
return 0;
|
|
|
|
fputs("\n", out);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-04-20 13:20:31 +00:00
|
|
|
if (*p != '\n')
|
|
|
|
fprintf(stderr, "Warning: missing EOL\n");
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
while (*p == '\n' || isspace((unsigned char)*p))
|
|
|
|
*p-- = 0;
|
|
|
|
|
|
|
|
*pkw = keyword;
|
|
|
|
*pval = value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIGNUM *hex2bn(const char *in)
|
|
|
|
{
|
|
|
|
BIGNUM *p=NULL;
|
|
|
|
|
|
|
|
if (!do_hex2bn(&p, in))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NB: this return the number of _bits_ read */
|
|
|
|
int bint2bin(const char *in, int len, unsigned char *out)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
memset(out,0,len);
|
|
|
|
for(n=0 ; n < len ; ++n)
|
|
|
|
if(in[n] == '1')
|
|
|
|
out[n/8]|=(0x80 >> (n%8));
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bin2bint(const unsigned char *in,int len,char *out)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for(n=0 ; n < len ; ++n)
|
|
|
|
out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------*/
|
|
|
|
|
|
|
|
void PrintValue(char *tag, unsigned char *val, int len)
|
|
|
|
{
|
2011-04-22 00:41:35 +00:00
|
|
|
#ifdef VERBOSE
|
|
|
|
OutputValue(tag, val, len, stdout, 0);
|
2011-01-26 00:56:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
|
|
|
|
{
|
|
|
|
char obuf[2048];
|
|
|
|
int olen;
|
|
|
|
|
|
|
|
if(bitmode)
|
2011-04-22 00:41:35 +00:00
|
|
|
{
|
2011-01-26 00:56:19 +00:00
|
|
|
olen=bin2bint(val,len,obuf);
|
2011-04-22 00:41:35 +00:00
|
|
|
fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
else
|
2011-04-22 00:41:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
fprintf(rfp, "%s = ", tag);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
fprintf(rfp, "%02x", val[i]);
|
|
|
|
fputs("\n", rfp);
|
|
|
|
}
|
2011-01-26 00:56:19 +00:00
|
|
|
|
|
|
|
#if VERBOSE
|
|
|
|
printf("%s = %.*s\n", tag, olen, obuf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-01 16:18:52 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|