Delete strength parameter from FIPS_drbg_generate. It isn't very useful
[openssl.git] / fips / rand / fips_drbg_lib.c
index 46e42e2947d521211925f70d0bfd51329ae7c3f6..98bd10bce374024434ac37d875a8f0374575c98a 100644 (file)
@@ -55,8 +55,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"
@@ -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)
                {
@@ -96,6 +98,7 @@ int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
                if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
                        {
                        FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
+                       dctx->status = DRBG_STATUS_ERROR;
                        return 0;
                        }
                }
@@ -112,8 +115,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)
                {
@@ -145,7 +154,7 @@ static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
        if (rv < (min_len + bl) || (rv % bl))
                return 0;
        /* Compare consecutive blocks for continuous PRNG test */
-       for (p = tout; p < tout + rv; p += bl)
+       for (p = tout; p < tout + rv - bl; p += bl)
                {
                if (!memcmp(p, p + bl, bl))
                        {
@@ -153,7 +162,10 @@ static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
                        return 0;
                        }
                }
-       return rv - bl;
+       rv -= bl;
+       if (rv > max_len)
+               return max_len;
+       return rv;
        }
 
 static void fips_cleanup_entropy(DRBG_CTX *dctx,
@@ -239,7 +251,8 @@ int FIPS_drbg_instantiate(DRBG_CTX *dctx,
 
 
        dctx->status = DRBG_STATUS_READY;
-       dctx->reseed_counter = 1;
+       if (!(dctx->flags & DRBG_CUSTOM_RESEED))
+               dctx->reseed_counter = 1;
 
        end:
 
@@ -303,7 +316,8 @@ int FIPS_drbg_reseed(DRBG_CTX *dctx,
                goto end;
 
        dctx->status = DRBG_STATUS_READY;
-       dctx->reseed_counter = 1;
+       if (!(dctx->flags & DRBG_CUSTOM_RESEED))
+               dctx->reseed_counter = 1;
        end:
 
        if (entropy && dctx->cleanup_entropy)
@@ -330,6 +344,7 @@ static int fips_drbg_check(DRBG_CTX *dctx)
                                                dctx->flags | DRBG_FLAG_TEST))
                        {
                        FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
+                       dctx->status = DRBG_STATUS_ERROR;
                        return 0;
                        }
                dctx->health_check_cnt = 0;
@@ -338,7 +353,7 @@ static int fips_drbg_check(DRBG_CTX *dctx)
        }
 
 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;
@@ -362,11 +377,10 @@ int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
                return 0;
                }
 
-       if (strength > dctx->strength)
-               {
-               r = FIPS_R_INSUFFICIENT_SECURITY_STRENGTH;
-               goto end;
-               }
+       if (dctx->flags & 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)
                {
@@ -385,10 +399,13 @@ 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->flags & DRBG_CUSTOM_RESEED))
+               {
+               if (dctx->reseed_counter >= dctx->reseed_interval)
+                       dctx->status = DRBG_STATUS_RESEED;
+               else
+                       dctx->reseed_counter++;
+               }
 
        end:
        if (r)