Use 0 for tbslen to perform strlen.
[openssl.git] / crypto / rsa / rsa_gen.c
index 6f4b8db2c12197f159f275d4b88539395070bd0f..484468da05e333d5f0403a3aa42a5d70ac5d9214 100644 (file)
  * - Geoff
  */
 
+#define OPENSSL_FIPSAPI
+
 #include <stdio.h>
 #include <time.h>
 #include "cryptlib.h"
 #include <openssl/bn.h>
 #include <openssl/rsa.h>
 
+#ifdef OPENSSL_FIPS
+
+
+#include <openssl/fips.h>
+#include <openssl/evp.h>
+
+int fips_check_rsa(RSA *rsa)
+       {
+       const unsigned char tbs[] = "RSA Pairwise Check Data";
+       unsigned char *ctbuf = NULL, *ptbuf = NULL;
+       int len, ret = 0;
+       EVP_PKEY pk;
+       pk.type = EVP_PKEY_RSA;
+       pk.pkey.rsa = rsa;
+
+       /* Perform pairwise consistency signature test */
+       if (!fips_pkey_signature_test(FIPS_TEST_PAIRWISE, &pk, tbs, 0,
+                       NULL, 0, NULL, RSA_PKCS1_PADDING, NULL)
+               || !fips_pkey_signature_test(FIPS_TEST_PAIRWISE, &pk, tbs, 0,
+                       NULL, 0, NULL, RSA_X931_PADDING, NULL)
+               || !fips_pkey_signature_test(FIPS_TEST_PAIRWISE, &pk, tbs, 0,
+                       NULL, 0, NULL, RSA_PKCS1_PSS_PADDING, NULL))
+               goto err;
+       /* Now perform pairwise consistency encrypt/decrypt test */
+       ctbuf = OPENSSL_malloc(RSA_size(rsa));
+       if (!ctbuf)
+               goto err;
+
+       len = RSA_public_encrypt(sizeof(tbs) - 1, tbs, ctbuf, rsa, RSA_PKCS1_PADDING);
+       if (len <= 0)
+               goto err;
+       /* Check ciphertext doesn't match plaintext */
+       if ((len == (sizeof(tbs) - 1)) && !memcmp(tbs, ctbuf, len))
+               goto err;
+       ptbuf = OPENSSL_malloc(RSA_size(rsa));
+
+       if (!ptbuf)
+               goto err;
+       len = RSA_private_decrypt(len, ctbuf, ptbuf, rsa, RSA_PKCS1_PADDING);
+       if (len != (sizeof(tbs) - 1))
+               goto err;
+       if (memcmp(ptbuf, tbs, len))
+               goto err;
+
+       ret = 1;
+
+       if (!ptbuf)
+               goto err;
+       
+       err:
+       if (ret == 0)
+               {
+               fips_set_selftest_fail();
+               FIPSerr(FIPS_F_FIPS_CHECK_RSA,FIPS_R_PAIRWISE_TEST_FAILED);
+               }
+
+       if (ctbuf)
+               OPENSSL_free(ctbuf);
+       if (ptbuf)
+               OPENSSL_free(ptbuf);
+
+       return ret;
+       }
+#endif
+
 static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb);
 
 /* NB: this wrapper would normally be placed in rsa_lib.c and the static
@@ -85,13 +152,27 @@ int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
 static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
        {
        BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp;
+       BIGNUM local_r0,local_d,local_p;
+       BIGNUM *pr0,*d,*p;
        int bitsp,bitsq,ok= -1,n=0;
-       BN_CTX *ctx=NULL,*ctx2=NULL;
+       BN_CTX *ctx=NULL;
+
+#ifdef OPENSSL_FIPS
+       if(FIPS_selftest_failed())
+           {
+           FIPSerr(FIPS_F_RSA_BUILTIN_KEYGEN,FIPS_R_FIPS_SELFTEST_FAILED);
+           return 0;
+           }
+
+       if (FIPS_mode() && (bits < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
+           {
+           FIPSerr(FIPS_F_RSA_BUILTIN_KEYGEN,FIPS_R_KEY_TOO_SHORT);
+           return 0;
+           }
+#endif
 
        ctx=BN_CTX_new();
        if (ctx == NULL) goto err;
-       ctx2=BN_CTX_new();
-       if (ctx2 == NULL) goto err;
        BN_CTX_start(ctx);
        r0 = BN_CTX_get(ctx);
        r1 = BN_CTX_get(ctx);
@@ -141,7 +222,7 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
                if(degenerate == 3)
                        {
                        ok = 0; /* we set our own err */
-                       RSAerr(RSA_F_RSA_GENERATE_KEY,RSA_R_KEY_SIZE_TOO_SMALL);
+                       RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,RSA_R_KEY_SIZE_TOO_SMALL);
                        goto err;
                        }
                if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;
@@ -167,43 +248,57 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
        if (!BN_sub(r1,rsa->p,BN_value_one())) goto err;        /* p-1 */
        if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;        /* q-1 */
        if (!BN_mul(r0,r1,r2,ctx)) goto err;    /* (p-1)(q-1) */
-
-/* should not be needed, since gcd(p-1,e) == 1 and gcd(q-1,e) == 1 */
-/*     for (;;)
+       if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
                {
-               if (!BN_gcd(r3,r0,rsa->e,ctx)) goto err;
-               if (BN_is_one(r3)) break;
+                 pr0 = &local_r0;
+                 BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
+               }
+       else
+         pr0 = r0;
+       if (!BN_mod_inverse(rsa->d,rsa->e,pr0,ctx)) goto err;   /* d */
 
-               if (1)
-                       {
-                       if (!BN_add_word(rsa->e,2L)) goto err;
-                       continue;
-                       }
-               RSAerr(RSA_F_RSA_GENERATE_KEY,RSA_R_BAD_E_VALUE);
-               goto err;
+       /* set up d for correct BN_FLG_CONSTTIME flag */
+       if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
+               {
+               d = &local_d;
+               BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
                }
-*/
-       if (!BN_mod_inverse(rsa->d,rsa->e,r0,ctx2)) goto err;   /* d */
+       else
+               d = rsa->d;
 
        /* calculate d mod (p-1) */
-       if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx)) goto err;
+       if (!BN_mod(rsa->dmp1,d,r1,ctx)) goto err;
 
        /* calculate d mod (q-1) */
-       if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx)) goto err;
+       if (!BN_mod(rsa->dmq1,d,r2,ctx)) goto err;
 
        /* calculate inverse of q mod p */
-       if (!BN_mod_inverse(rsa->iqmp,rsa->q,rsa->p,ctx2)) goto err;
+       if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
+               {
+               p = &local_p;
+               BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
+               }
+       else
+               p = rsa->p;
+       if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err;
+
+#ifdef OPENSSL_FIPS
+       if(!fips_check_rsa(rsa))
+           goto err;
+#endif
 
        ok=1;
 err:
        if (ok == -1)
                {
-               RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN);
+               RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,ERR_LIB_BN);
                ok=0;
                }
-       BN_CTX_end(ctx);
-       BN_CTX_free(ctx);
-       BN_CTX_free(ctx2);
+       if (ctx != NULL)
+               {
+               BN_CTX_end(ctx);
+               BN_CTX_free(ctx);
+               }
 
        return ok;
        }