check for unset entropy and nonce callbacks
[openssl.git] / fips / rand / fips_drbg_lib.c
index 46f059d05806da31fbc69c2805ac14da100e00c8..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.
  */
@@ -55,8 +54,6 @@
 
 #include <string.h>
 #include <openssl/crypto.h>
-#include <openssl/evp.h>
-#include <openssl/aes.h>
 #include <openssl/err.h>
 #include <openssl/fips_rand.h>
 #include "fips_rand_lcl.h"
@@ -68,9 +65,10 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
        int rv;
        memset(dctx, 0, sizeof(DRBG_CTX));
        dctx->status = DRBG_STATUS_UNINITIALISED;
-       dctx->flags = flags;
+       dctx->xflags = flags;
        dctx->type = type;
 
+       dctx->iflags = 0;
        dctx->entropy_blocklen = 0;
        dctx->health_check_cnt = 0;
        dctx->health_check_interval = DRBG_HEALTH_INTERVAL;
@@ -79,6 +77,10 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
 
        if (rv == -2)
                rv = fips_drbg_ctr_init(dctx);
+       if (rv == -2)
+               rv = fips_drbg_hmac_init(dctx);
+       if (rv == -2)
+               rv = fips_drbg_ec_init(dctx);
 
        if (rv <= 0)
                {
@@ -90,10 +92,9 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
 
        /* If not in test mode run selftests on DRBG of the same type */
 
-       if (!(dctx->flags & DRBG_FLAG_TEST))
+       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);
                        return 0;
@@ -112,8 +113,14 @@ DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
                FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
                return NULL;
                }
+
        if (type == 0)
+               {
+               memset(dctx, 0, sizeof(DRBG_CTX));
+               dctx->type = 0;
+               dctx->status = DRBG_STATUS_UNINITIALISED;
                return dctx;
+               }
 
        if (FIPS_drbg_init(dctx, type, flags) <= 0)
                {
@@ -128,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,
@@ -137,7 +154,9 @@ 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->flags & DRBG_FLAG_TEST || !bl)
+       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,
                                min_len + bl, max_len + bl);
@@ -163,7 +182,7 @@ static void fips_cleanup_entropy(DRBG_CTX *dctx,
                                        unsigned char *out, size_t olen)
        {
        size_t bl;
-       if (dctx->flags & DRBG_FLAG_TEST)
+       if (dctx->xflags & DRBG_FLAG_TEST)
                bl = 0;
        else
                bl = dctx->entropy_blocklen;
@@ -187,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;
@@ -197,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)
@@ -217,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,
@@ -242,7 +268,8 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
 
 
        dctx->status = DRBG_STATUS_READY;
-       dctx->reseed_counter = 1;
+       if (!(dctx->iflags & DRBG_CUSTOM_RESEED))
+               dctx->reseed_counter = 1;
 
        end:
 
@@ -255,23 +282,23 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
        if (dctx->status == DRBG_STATUS_READY)
                return 1;
 
-       if (r && !(dctx->flags & DRBG_FLAG_NOERR))
+       if (r && !(dctx->iflags & DRBG_FLAG_NOERR))
                FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
 
        return 0;
 
        }
 
-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)
@@ -292,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);
@@ -306,7 +344,8 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx,
                goto end;
 
        dctx->status = DRBG_STATUS_READY;
-       dctx->reseed_counter = 1;
+       if (!(dctx->iflags & DRBG_CUSTOM_RESEED))
+               dctx->reseed_counter = 1;
        end:
 
        if (entropy && dctx->cleanup_entropy)
@@ -315,37 +354,46 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx,
        if (dctx->status == DRBG_STATUS_READY)
                return 1;
 
-       if (r && !(dctx->flags & DRBG_FLAG_NOERR))
-               FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
+       if (r && !(dctx->iflags & DRBG_FLAG_NOERR))
+               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->flags & DRBG_FLAG_TEST)
+       if (dctx->xflags & DRBG_FLAG_TEST)
                return 1;
        dctx->health_check_cnt++;
        if (dctx->health_check_cnt >= dctx->health_check_interval)
                {
-               DRBG_CTX tctx;
-               if (!fips_drbg_kat(&tctx, dctx->type,
-                                               dctx->flags | DRBG_FLAG_TEST))
+               if (!FIPS_drbg_health_check(dctx))
                        {
                        FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
                        return 0;
                        }
-               dctx->health_check_cnt = 0;
                }
        return 1;
        }
 
 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
-                       int strength, int prediction_resistance,
+                       int prediction_resistance,
                        const unsigned char *adin, size_t adinlen)
        {
        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;
 
@@ -365,15 +413,23 @@ int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
                return 0;
                }
 
-       if (strength > dctx->strength)
+       if (adinlen > dctx->max_adin)
                {
-               r = FIPS_R_INSUFFICIENT_SECURITY_STRENGTH;
+               r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
                goto end;
                }
 
+       if (dctx->iflags & DRBG_CUSTOM_RESEED)
+               dctx->generate(dctx, NULL, outlen, NULL, 0);
+       else if (dctx->reseed_counter >= dctx->reseed_interval)
+               dctx->status = DRBG_STATUS_RESEED;
+
        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;
@@ -388,15 +444,18 @@ int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
                dctx->status = DRBG_STATUS_ERROR;
                goto end;
                }
-       if (dctx->reseed_counter >= dctx->reseed_interval)
-               dctx->status = DRBG_STATUS_RESEED;
-       else
-               dctx->reseed_counter++;
+       if (!(dctx->iflags & DRBG_CUSTOM_RESEED))
+               {
+               if (dctx->reseed_counter >= dctx->reseed_interval)
+                       dctx->status = DRBG_STATUS_RESEED;
+               else
+                       dctx->reseed_counter++;
+               }
 
        end:
        if (r)
                {
-               if (!(dctx->flags & DRBG_FLAG_NOERR))
+               if (!(dctx->iflags & DRBG_FLAG_NOERR))
                        FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
                return 0;
                }
@@ -480,18 +539,23 @@ void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
        dctx->health_check_interval = interval;
        }
 
+void FIPS_drbg_set_reseed_interval(DRBG_CTX *dctx, int interval)
+       {
+       dctx->reseed_interval = 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 */
 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
        {
        /* No CPRNG in test mode */
-       if (dctx->flags & DRBG_FLAG_TEST)
+       if (dctx->xflags & DRBG_FLAG_TEST)
                return 1;
        /* Check block is valid: should never happen */
        if (dctx->lb_valid == 0)