From 23430786606264f4f4e854fb214b0c5f6185ff95 Mon Sep 17 00:00:00 2001 From: Ben Laurie Date: Sat, 27 Sep 2003 15:54:43 +0000 Subject: [PATCH] Selftest RSA and some fixes. --- Makefile.org | 2 +- fips/des/fingerprint.sha1 | 2 +- fips/des/fips_des_selftest.c | 2 +- fips/fingerprint.sha1 | 6 +- fips/fips.c | 3 +- fips/fips.h | 3 + fips/fips_err.h | 4 +- fips/fips_make_sha1 | 2 +- fips/rsa/Makefile.ssl | 4 +- fips/rsa/fingerprint.sha1 | 1 + fips/rsa/fips_rsa_selftest.c | 189 +++++++++++++++++++++++++++++++++++ test/Makefile.ssl | 2 +- 12 files changed, 208 insertions(+), 12 deletions(-) create mode 100644 fips/rsa/fips_rsa_selftest.c diff --git a/Makefile.org b/Makefile.org index 9eafb68a96..8191f99e06 100644 --- a/Makefile.org +++ b/Makefile.org @@ -226,7 +226,7 @@ all: Makefile.ssl sub_all openssl.pc sigs: $(SIGS) libcrypto.sha1: libcrypto.a - if echo "$(CFLAG)" | grep " -DFIPS" > /dev/null 2> /dev/null; then \ + if egrep 'define OPENSSL_FIPS' $(TOP)/include/openssl/opensslconf.h > /dev/null; then \ fips/sha1/fips_standalone_sha1 libcrypto.a > libcrypto.sha1; \ fi diff --git a/fips/des/fingerprint.sha1 b/fips/des/fingerprint.sha1 index 742465a5f0..cc0eea38a9 100644 --- a/fips/des/fingerprint.sha1 +++ b/fips/des/fingerprint.sha1 @@ -1,3 +1,3 @@ SHA1(fips_des_enc.c)= 75389f527cc456178e6a2e35f82bf49f98fe3e90 -SHA1(fips_des_selftest.c)= 4165967e54097aab14e6737525c7f34e052d7b16 +SHA1(fips_des_selftest.c)= d81ee4db762d89cca749138a99100d342f195665 SHA1(fips_des_locl.h)= a4cf60ca32476a2483b3e4460ec9a19c0444fd20 diff --git a/fips/des/fips_des_selftest.c b/fips/des/fips_des_selftest.c index 811581f032..bb1966c8fb 100644 --- a/fips/des/fips_des_selftest.c +++ b/fips/des/fips_des_selftest.c @@ -86,7 +86,7 @@ int FIPS_selftest_des() DES_ecb_encrypt(&tests[n].plaintext,&buf,&key,1); if(memcmp(buf,tests[n].ciphertext,sizeof buf)) { - FIPSerr(FIPS_F_FIPS_SELFTEST_AES,FIPS_R_SELFTEST_FAILED); + FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED); return 0; } } diff --git a/fips/fingerprint.sha1 b/fips/fingerprint.sha1 index 44277a95c3..a930af3e2e 100644 --- a/fips/fingerprint.sha1 +++ b/fips/fingerprint.sha1 @@ -1,4 +1,4 @@ -SHA1(fips.c)= 3224544013a0115b0e1516f2b4c248b0b7f5e820 +SHA1(fips.c)= 8b4edd549ed66fff15c26ef9977c2d47a67f31e8 SHA1(fips_err_wrapper.c)= ad4a2ffa18743c83827de398c811eb6124ba0b27 -SHA1(fips.h)= 638e5f4d564fd503a6060bc49567c52fc21d8d10 -SHA1(fips_err.h)= 8d9fd3ab3e6ca5297c5714e7f6cd9834e22b4cba +SHA1(fips.h)= 1a8bf37b0fb3131640b8dfbf3dcfa6d5fd5ce4a4 +SHA1(fips_err.h)= 90926739cc5aa4fa4a3e4746b83783bb079ea248 diff --git a/fips/fips.c b/fips/fips.c index 0db04e098a..16280fbc95 100644 --- a/fips/fips.c +++ b/fips/fips.c @@ -65,7 +65,8 @@ int FIPS_selftest() { return FIPS_selftest_sha1() && FIPS_selftest_aes() - && FIPS_selftest_des(); + && FIPS_selftest_des() + && FIPS_selftest_rsa(); } static int FIPS_check_exe(const char *path) diff --git a/fips/fips.h b/fips/fips.h index 2e7dc3c1a4..278678f33c 100644 --- a/fips/fips.h +++ b/fips/fips.h @@ -62,6 +62,7 @@ int FIPS_dsa_check(struct dsa_st *dsa); int FIPS_selftest_sha1(void); int FIPS_selftest_aes(void); int FIPS_selftest_des(void); +int FIPS_selftest_rsa(void); /* The following lines are auto generated by the script mkerr.pl. Any changes * made after this point may be overwritten when the script is next run. @@ -81,6 +82,8 @@ void ERR_load_FIPS_strings(void); #define FIPS_F_FIPS_DSA_CHECK 102 #define FIPS_F_FIPS_MODE_SET 105 #define FIPS_F_FIPS_SELFTEST_AES 104 +#define FIPS_F_FIPS_SELFTEST_DES 107 +#define FIPS_F_FIPS_SELFTEST_RSA 108 #define FIPS_F_FIPS_SELFTEST_SHA1 103 #define FIPS_F_HASH_FINAL 100 #define FIPS_F_SSLEAY_RAND_BYTES 101 diff --git a/fips/fips_err.h b/fips/fips_err.h index 90c30320f2..a41f1f5505 100644 --- a/fips/fips_err.h +++ b/fips/fips_err.h @@ -1,4 +1,4 @@ -/* fips/fips_err.c */ +/* fips/fips_err.h */ /* ==================================================================== * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved. * @@ -70,6 +70,8 @@ static ERR_STRING_DATA FIPS_str_functs[]= {ERR_PACK(0,FIPS_F_FIPS_DSA_CHECK,0), "FIPS_dsa_check"}, {ERR_PACK(0,FIPS_F_FIPS_MODE_SET,0), "FIPS_mode_set"}, {ERR_PACK(0,FIPS_F_FIPS_SELFTEST_AES,0), "FIPS_selftest_aes"}, +{ERR_PACK(0,FIPS_F_FIPS_SELFTEST_DES,0), "FIPS_selftest_des"}, +{ERR_PACK(0,FIPS_F_FIPS_SELFTEST_RSA,0), "FIPS_SELFTEST_RSA"}, {ERR_PACK(0,FIPS_F_FIPS_SELFTEST_SHA1,0), "FIPS_selftest_sha1"}, {ERR_PACK(0,FIPS_F_HASH_FINAL,0), "HASH_FINAL"}, {ERR_PACK(0,FIPS_F_SSLEAY_RAND_BYTES,0), "SSLEAY_RAND_BYTES"}, diff --git a/fips/fips_make_sha1 b/fips/fips_make_sha1 index c669b2435a..7ff94fc807 100755 --- a/fips/fips_make_sha1 +++ b/fips/fips_make_sha1 @@ -24,4 +24,4 @@ cd ../des $S fips_des_enc.c fips_des_selftest.c fips_des_locl.h > fingerprint.sha1 cd ../rsa -$S fips_rsa_eay.c fips_rsa_gen.c > fingerprint.sha1 +$S fips_rsa_eay.c fips_rsa_gen.c fips_rsa_selftest.c > fingerprint.sha1 diff --git a/fips/rsa/Makefile.ssl b/fips/rsa/Makefile.ssl index 3b99ff17e4..29ee6eb1ff 100644 --- a/fips/rsa/Makefile.ssl +++ b/fips/rsa/Makefile.ssl @@ -23,8 +23,8 @@ TEST=fips_dsatest.c APPS= LIB=$(TOP)/libcrypto.a -LIBSRC=fips_rsa_eay.c fips_rsa_gen.c -LIBOBJ=fips_rsa_eay.o fips_rsa_gen.o +LIBSRC=fips_rsa_eay.c fips_rsa_gen.c fips_rsa_selftest.c +LIBOBJ=fips_rsa_eay.o fips_rsa_gen.o fips_rsa_selftest.o SRC= $(LIBSRC) diff --git a/fips/rsa/fingerprint.sha1 b/fips/rsa/fingerprint.sha1 index 47b7ca6e9f..8b361278ca 100644 --- a/fips/rsa/fingerprint.sha1 +++ b/fips/rsa/fingerprint.sha1 @@ -1,2 +1,3 @@ SHA1(fips_rsa_eay.c)= 945cac757aecfad5a3c6bfcd4db7c384e51342f5 SHA1(fips_rsa_gen.c)= 4367cb3840db0df5b50846a198c33911c28ab2f4 +SHA1(fips_rsa_selftest.c)= 1b7884479dd0d86cff13cb3467b5aa7b9d0c91e3 diff --git a/fips/rsa/fips_rsa_selftest.c b/fips/rsa/fips_rsa_selftest.c new file mode 100644 index 0000000000..f883c0152b --- /dev/null +++ b/fips/rsa/fips_rsa_selftest.c @@ -0,0 +1,189 @@ +/* ==================================================================== + * Copyright (c) 2003 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 + * 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. + * + */ + +#include +#include +#include +#include +#include + +#ifdef OPENSSL_FIPS +#define SetKey \ + key->n = BN_bin2bn(n, sizeof(n)-1, key->n); \ + key->e = BN_bin2bn(e, sizeof(e)-1, key->e); \ + key->d = BN_bin2bn(d, sizeof(d)-1, key->d); \ + key->p = BN_bin2bn(p, sizeof(p)-1, key->p); \ + key->q = BN_bin2bn(q, sizeof(q)-1, key->q); \ + key->dmp1 = BN_bin2bn(dmp1, sizeof(dmp1)-1, key->dmp1); \ + key->dmq1 = BN_bin2bn(dmq1, sizeof(dmq1)-1, key->dmq1); \ + key->iqmp = BN_bin2bn(iqmp, sizeof(iqmp)-1, key->iqmp); \ + memcpy(c, ctext_ex, sizeof(ctext_ex) - 1); \ + return (sizeof(ctext_ex) - 1); + +static int setkey(RSA *key, unsigned char *c) + { + static unsigned char n[] = +"\x00\xBB\xF8\x2F\x09\x06\x82\xCE\x9C\x23\x38\xAC\x2B\x9D\xA8\x71" +"\xF7\x36\x8D\x07\xEE\xD4\x10\x43\xA4\x40\xD6\xB6\xF0\x74\x54\xF5" +"\x1F\xB8\xDF\xBA\xAF\x03\x5C\x02\xAB\x61\xEA\x48\xCE\xEB\x6F\xCD" +"\x48\x76\xED\x52\x0D\x60\xE1\xEC\x46\x19\x71\x9D\x8A\x5B\x8B\x80" +"\x7F\xAF\xB8\xE0\xA3\xDF\xC7\x37\x72\x3E\xE6\xB4\xB7\xD9\x3A\x25" +"\x84\xEE\x6A\x64\x9D\x06\x09\x53\x74\x88\x34\xB2\x45\x45\x98\x39" +"\x4E\xE0\xAA\xB1\x2D\x7B\x61\xA5\x1F\x52\x7A\x9A\x41\xF6\xC1\x68" +"\x7F\xE2\x53\x72\x98\xCA\x2A\x8F\x59\x46\xF8\xE5\xFD\x09\x1D\xBD" +"\xCB"; + + static unsigned char e[] = "\x11"; + + static unsigned char d[] = +"\x00\xA5\xDA\xFC\x53\x41\xFA\xF2\x89\xC4\xB9\x88\xDB\x30\xC1\xCD" +"\xF8\x3F\x31\x25\x1E\x06\x68\xB4\x27\x84\x81\x38\x01\x57\x96\x41" +"\xB2\x94\x10\xB3\xC7\x99\x8D\x6B\xC4\x65\x74\x5E\x5C\x39\x26\x69" +"\xD6\x87\x0D\xA2\xC0\x82\xA9\x39\xE3\x7F\xDC\xB8\x2E\xC9\x3E\xDA" +"\xC9\x7F\xF3\xAD\x59\x50\xAC\xCF\xBC\x11\x1C\x76\xF1\xA9\x52\x94" +"\x44\xE5\x6A\xAF\x68\xC5\x6C\x09\x2C\xD3\x8D\xC3\xBE\xF5\xD2\x0A" +"\x93\x99\x26\xED\x4F\x74\xA1\x3E\xDD\xFB\xE1\xA1\xCE\xCC\x48\x94" +"\xAF\x94\x28\xC2\xB7\xB8\x88\x3F\xE4\x46\x3A\x4B\xC8\x5B\x1C\xB3" +"\xC1"; + + static unsigned char p[] = +"\x00\xEE\xCF\xAE\x81\xB1\xB9\xB3\xC9\x08\x81\x0B\x10\xA1\xB5\x60" +"\x01\x99\xEB\x9F\x44\xAE\xF4\xFD\xA4\x93\xB8\x1A\x9E\x3D\x84\xF6" +"\x32\x12\x4E\xF0\x23\x6E\x5D\x1E\x3B\x7E\x28\xFA\xE7\xAA\x04\x0A" +"\x2D\x5B\x25\x21\x76\x45\x9D\x1F\x39\x75\x41\xBA\x2A\x58\xFB\x65" +"\x99"; + + static unsigned char q[] = +"\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9" +"\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D" +"\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5" +"\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x15" +"\x03"; + + static unsigned char dmp1[] = +"\x54\x49\x4C\xA6\x3E\xBA\x03\x37\xE4\xE2\x40\x23\xFC\xD6\x9A\x5A" +"\xEB\x07\xDD\xDC\x01\x83\xA4\xD0\xAC\x9B\x54\xB0\x51\xF2\xB1\x3E" +"\xD9\x49\x09\x75\xEA\xB7\x74\x14\xFF\x59\xC1\xF7\x69\x2E\x9A\x2E" +"\x20\x2B\x38\xFC\x91\x0A\x47\x41\x74\xAD\xC9\x3C\x1F\x67\xC9\x81"; + + static unsigned char dmq1[] = +"\x47\x1E\x02\x90\xFF\x0A\xF0\x75\x03\x51\xB7\xF8\x78\x86\x4C\xA9" +"\x61\xAD\xBD\x3A\x8A\x7E\x99\x1C\x5C\x05\x56\xA9\x4C\x31\x46\xA7" +"\xF9\x80\x3F\x8F\x6F\x8A\xE3\x42\xE9\x31\xFD\x8A\xE4\x7A\x22\x0D" +"\x1B\x99\xA4\x95\x84\x98\x07\xFE\x39\xF9\x24\x5A\x98\x36\xDA\x3D"; + + static unsigned char iqmp[] = +"\x00\xB0\x6C\x4F\xDA\xBB\x63\x01\x19\x8D\x26\x5B\xDB\xAE\x94\x23" +"\xB3\x80\xF2\x71\xF7\x34\x53\x88\x50\x93\x07\x7F\xCD\x39\xE2\x11" +"\x9F\xC9\x86\x32\x15\x4F\x58\x83\xB1\x67\xA9\x67\xBF\x40\x2B\x4E" +"\x9E\x2E\x0F\x96\x56\xE6\x98\xEA\x36\x66\xED\xFB\x25\x79\x80\x39" +"\xF7"; + + static unsigned char ctext_ex[] = +"\x42\x4b\xc9\x51\x61\xd4\xca\xa0\x18\x6c\x4d\xca\x61\x8f\x2d\x07" +"\x8c\x63\xc5\x6b\xa2\x4c\x32\xb1\xda\xb7\xdd\x32\xb6\x51\x68\xc3" +"\x6e\x98\x46\xd6\xbb\x1a\xd5\x99\x05\x92\x7c\xd7\xbc\x08\x9e\xe4" +"\xc3\x70\x4d\xe6\x99\x7e\x61\x31\x07\x7a\x19\xdb\x3e\x11\xfa\x3d" +"\x7c\x61\xd7\x78\x14\x3f\x05\x16\xa0\xc4\xbf\xcd\xee\xca\x67\x4c" +"\x80\x4e\xca\x43\x2f\x35\x43\x58\xa7\x50\x7e\x3e\x52\x82\xab\xac" +"\xa6\x50\xe8\x39\x9f\xe0\x7f\x58\x1d\x1b\x90\x93\x04\xec\xb3\xf9" +"\x24\xd3\x75\x3e\x39\xd1\x14\xc6\x33\xce\xd6\xee\x20\x47\xec\xe4"; + + SetKey; + } + +int FIPS_selftest_rsa() + { + int clen; + RSA *key; + unsigned char expected_ctext[256]; + unsigned char ctext[256]; + unsigned char ptext[256]; + static unsigned char original_ptext[] = + "\x01\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0" + "\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12" + "\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34" + "\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34\x56" + "\x89\xab\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34\x56\x78" + "\xab\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34\x56\x78\x9a" + "\xcd\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34\x56\x78\x9a\xbc" + "\xef\x12\x34\x56\x78\x9a\xbc\xde\xf0\x12\x34\x56\x78\x9a\xbc\xde"; + int n; + + ERR_load_crypto_strings(); + key=RSA_new(); + clen=setkey(key,expected_ctext); + n=RSA_public_encrypt(sizeof(original_ptext)-1,original_ptext,ctext,key, + RSA_NO_PADDING); + if(n < 0) + { + ERR_print_errors_fp(stderr); + exit(1); + } + if(n != clen || memcmp(ctext,expected_ctext,n)) + { + FIPSerr(FIPS_F_FIPS_SELFTEST_RSA,FIPS_R_SELFTEST_FAILED); + return 0; + } + n=RSA_private_decrypt(n,ctext,ptext,key,RSA_NO_PADDING); + if(n < 0) + { + ERR_print_errors_fp(stderr); + exit(1); + } + if(n != sizeof(original_ptext)-1 || memcmp(ptext,original_ptext,n)) + { + FIPSerr(FIPS_F_FIPS_SELFTEST_RSA,FIPS_R_SELFTEST_FAILED); + return 0; + } + RSA_free(key); + return 1; + } + +#endif /* def OPENSSL_FIPS */ diff --git a/test/Makefile.ssl b/test/Makefile.ssl index 6547fa6025..678d489efc 100644 --- a/test/Makefile.ssl +++ b/test/Makefile.ssl @@ -555,7 +555,7 @@ $(SSLTEST): $(SSLTEST).o $(DLIBSSL) $(DLIBCRYPTO) LD_LIBRARY_PATH=..:$$LD_LIBRARY_PATH \ $(CC) -o $(SSLTEST) $(CFLAGS) $(SSLTEST).o $(PEX_LIBS) $(LIBSSL) $(LIBKRB5) $(LIBCRYPTO) $(EX_LIBS) ; \ fi - if echo "$(CFLAG)" | grep " -DFIPS" > /dev/null 2> /dev/null; then \ + if egrep 'define OPENSSL_FIPS' $(TOP)/include/openssl/opensslconf.h > /dev/null; then \ $(TOP)/fips/openssl_fips_fingerprint $(TOP)/libcrypto.a $(SSLTEST); \ fi -- 2.25.1