Add periodic DRBG health checks as required by SP800-90.
[oweals/openssl.git] / fips / rand / fips_drbg_lib.c
index 7a0a1070e2b21dd740125a1e3256a1ee4bed2ff6..6d983aaf4bdede5653a19ec2840f3d483a2eebe4 100644 (file)
@@ -71,6 +71,9 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
        dctx->flags = flags;
        dctx->type = type;
 
+       dctx->health_check_cnt = 0;
+       dctx->health_check_interval = DRBG_HEALTH_INTERVAL;
+
        rv = fips_drbg_hash_init(dctx);
 
        if (rv == -2)
@@ -84,12 +87,23 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
                        FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_ERROR_INITIALISING_DRBG);
                }
 
+       /* If not in test mode run selftests on DRBG of the same type */
+
+       if (!(dctx->flags & DRBG_FLAG_TEST))
+               {
+               DRBG_CTX tctx;
+               if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
+                       {
+                       FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
+                       return 0;
+                       }
+               }
+
        return rv;
        }
 
 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
        {
-       int rv;
        DRBG_CTX *dctx;
        dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
        if (!dctx)
@@ -99,7 +113,6 @@ DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
                }
        if (type == 0)
                return dctx;
-       rv = FIPS_drbg_init(dctx, type, flags);
 
        if (FIPS_drbg_init(dctx, type, flags) <= 0)
                {
@@ -267,6 +280,24 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx,
        return 0;
        }
 
+static int fips_drbg_check(DRBG_CTX *dctx)
+       {
+       if (dctx->flags & 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))
+                       {
+                       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,
@@ -274,6 +305,9 @@ int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
        {
        int r = 0;
 
+       if (!fips_drbg_check(dctx))
+               return 0;
+
        if (dctx->status != DRBG_STATUS_READY
                && dctx->status != DRBG_STATUS_RESEED)
                {
@@ -398,6 +432,11 @@ int FIPS_drbg_get_strength(DRBG_CTX *dctx)
        return dctx->strength;
        }
 
+void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
+       {
+       dctx->health_check_interval = interval;
+       }
+
 static int drbg_stick = 0;
 
 void FIPS_drbg_stick(void)
@@ -414,7 +453,7 @@ int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
        /* Check block is valid: should never happen */
        if (dctx->lb_valid == 0)
                {
-               FIPSerr(FIPS_F_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
+               FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
                fips_set_selftest_fail();
                return 0;
                }
@@ -423,7 +462,7 @@ int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
        /* Check against last block: fail if match */
        if (!memcmp(dctx->lb, out, dctx->blocklength))
                {
-               FIPSerr(FIPS_F_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
+               FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
                fips_set_selftest_fail();
                return 0;
                }