Add loaded dynamic ENGINEs to list.
[openssl.git] / fips / rand / fips_drbg_lib.c
index c2e42896d1abc6d8b4785b5e434eed97d5ecf6f0..ee162d05eb0281d42548ec0365262ae79631943b 100644 (file)
@@ -1,4 +1,3 @@
-/* fips/rand/fips_drbg_lib.c */
 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project.
  */
@@ -95,11 +94,9 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
 
        if (!(dctx->xflags & DRBG_FLAG_TEST))
                {
-               DRBG_CTX tctx;
-               if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
+               if (!FIPS_drbg_health_check(dctx))
                        {
                        FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
-                       dctx->status = DRBG_STATUS_ERROR;
                        return 0;
                        }
                }
@@ -138,8 +135,18 @@ void FIPS_drbg_free(DRBG_CTX *dctx)
        {
        if (dctx->uninstantiate)
                dctx->uninstantiate(dctx);
-       OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
-       OPENSSL_free(dctx);
+       /* Don't free up default DRBG */
+       if (dctx == FIPS_get_default_drbg())
+               {
+               memset(dctx, 0, sizeof(DRBG_CTX));
+               dctx->type = 0;
+               dctx->status = DRBG_STATUS_UNINITIALISED;
+               }
+       else
+               {
+               OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
+               OPENSSL_free(dctx);
+               }
        }
 
 static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
@@ -147,6 +154,8 @@ static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
        {
        unsigned char *tout, *p;
        size_t bl = dctx->entropy_blocklen, rv;
+       if (!dctx->get_entropy)
+               return 0;
        if (dctx->xflags & DRBG_FLAG_TEST || !bl)
                return dctx->get_entropy(dctx, pout, entropy, min_len, max_len);
        rv = dctx->get_entropy(dctx, &tout, entropy + bl,
@@ -197,6 +206,7 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
        FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
        FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
        FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
+       FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_DRBG_NOT_INITIALISED);
 #endif
 
        int r = 0;
@@ -207,6 +217,12 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
                goto end;
                }
 
+       if (!dctx->instantiate)
+               {
+               r = FIPS_R_DRBG_NOT_INITIALISED;
+               goto end;
+               }
+
        if (dctx->status != DRBG_STATUS_UNINITIALISED)
                {
                if (dctx->status == DRBG_STATUS_ERROR)
@@ -227,7 +243,7 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
                goto end;
                }
 
-       if (dctx->max_nonce > 0)
+       if (dctx->max_nonce > 0 && dctx->get_nonce)
                {
                noncelen = dctx->get_nonce(dctx, &nonce,
                                        dctx->strength / 2,
@@ -273,16 +289,16 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
 
        }
 
-int FIPS_drbg_reseed(DRBG_CTX *dctx,
-                       const unsigned char *adin, size_t adinlen)
+static int drbg_reseed(DRBG_CTX *dctx,
+                       const unsigned char *adin, size_t adinlen, int hcheck)
        {
        unsigned char *entropy = NULL;
-       size_t entlen;
+       size_t entlen = 0;
        int r = 0;
 
 #if 0
-       FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
-       FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
+       FIPSerr(FIPS_F_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
+       FIPSerr(FIPS_F_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
 #endif
        if (dctx->status != DRBG_STATUS_READY
                && dctx->status != DRBG_STATUS_RESEED)
@@ -303,6 +319,17 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx,
                }
 
        dctx->status = DRBG_STATUS_ERROR;
+       /* Peform health check on all reseed operations if not a prediction
+        * resistance request and not in test mode.
+        */
+       if (hcheck && !(dctx->xflags & DRBG_FLAG_TEST))
+               {
+               if (!FIPS_drbg_health_check(dctx))
+                       {
+                       r = FIPS_R_SELFTEST_FAILURE;
+                       goto end;
+                       }
+               }
 
        entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
                                dctx->min_entropy, dctx->max_entropy);
@@ -328,11 +355,17 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx,
                return 1;
 
        if (r && !(dctx->iflags & DRBG_FLAG_NOERR))
-               FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
+               FIPSerr(FIPS_F_DRBG_RESEED, r);
 
        return 0;
        }
 
+int FIPS_drbg_reseed(DRBG_CTX *dctx,
+                       const unsigned char *adin, size_t adinlen)
+       {
+       return drbg_reseed(dctx, adin, adinlen, 1);
+       }
+
 static int fips_drbg_check(DRBG_CTX *dctx)
        {
        if (dctx->xflags & DRBG_FLAG_TEST)
@@ -340,13 +373,11 @@ static int fips_drbg_check(DRBG_CTX *dctx)
        dctx->health_check_cnt++;
        if (dctx->health_check_cnt >= dctx->health_check_interval)
                {
-               if (FIPS_drbg_test(dctx) <= 0)
+               if (!FIPS_drbg_health_check(dctx))
                        {
                        FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
-                       dctx->status = DRBG_STATUS_ERROR;
                        return 0;
                        }
-               dctx->health_check_cnt = 0;
                }
        return 1;
        }
@@ -357,6 +388,12 @@ int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
        {
        int r = 0;
 
+       if (FIPS_selftest_failed())
+               {
+               FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, FIPS_R_SELFTEST_FAILED);
+               return 0;
+               }
+
        if (!fips_drbg_check(dctx))
                return 0;
 
@@ -389,7 +426,10 @@ int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
 
        if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
                {
-               if (!FIPS_drbg_reseed(dctx, adin, adinlen))
+               /* If prediction resistance request don't do health check */
+               int hcheck = prediction_resistance ? 0 : 1;
+               
+               if (!drbg_reseed(dctx, adin, adinlen, hcheck))
                        {
                        r = FIPS_R_RESEED_ERROR;
                        goto end;
@@ -506,9 +546,9 @@ void FIPS_drbg_set_reseed_interval(DRBG_CTX *dctx, int interval)
 
 static int drbg_stick = 0;
 
-void FIPS_drbg_stick(void)
+void FIPS_drbg_stick(int onoff)
        {
-       drbg_stick = 1;
+       drbg_stick = onoff;
        }
 
 /* Continuous DRBG utility function */