Check for uninitialised DRBG_CTX and don't free up default DRBG_CTX.
[openssl.git] / fips / rand / fips_drbg_lib.c
index 9873742c4535967abebd6947ac80c589d0469a56..32e4b83c5e9f7cdcb83fbc44436b3412dd7e0633 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,
@@ -197,6 +204,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 +215,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)
@@ -277,12 +291,12 @@ 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)
@@ -308,7 +322,7 @@ static int drbg_reseed(DRBG_CTX *dctx,
         */
        if (hcheck && !(dctx->xflags & DRBG_FLAG_TEST))
                {
-               if (!FIPS_drbg_test(dctx))
+               if (!FIPS_drbg_health_check(dctx))
                        {
                        r = FIPS_R_SELFTEST_FAILURE;
                        goto end;
@@ -339,7 +353,7 @@ static int 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;
        }
@@ -357,13 +371,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))
+               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;
        }