Remove superfluous PRNG self tests.
[openssl.git] / fips / rand / fips_drbg_selftest.c
index 31b7a0be58529f0a34ab3f9bbac7aa7e44c3e256..881bce041d5002f82af79f66a88e11e248d21ec1 100644 (file)
@@ -60,6 +60,7 @@
 #include <openssl/err.h>
 #include <openssl/fips_rand.h>
 #include "fips_rand_lcl.h"
+#include "fips_locl.h"
 
 typedef struct {
        int nid;
@@ -688,7 +689,7 @@ static const unsigned char sha512_additionalinput2[] =
        0x41,0xbe,0x14,0x87,0x81,0x08,0x0d,0xee
        };
 /* NB: not constant so we can corrupt it */
-static unsigned char sha512_entropyinputpr2[] =
+static const unsigned char sha512_entropyinputpr2[] =
        {
        0xed,0x22,0x42,0x61,0xa7,0x4c,0xed,0xc7,0x10,0x82,0x61,0x17,
        0xaa,0x7d,0xdb,0x4e,0x1c,0x96,0x61,0x23,0xcd,0x8f,0x84,0x77,
@@ -732,37 +733,33 @@ typedef struct
        int noncecnt;
        } TEST_ENT;
 
-static size_t test_entropy(DRBG_CTX *dctx, unsigned char *out,
+static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
                                 int entropy, size_t min_len, size_t max_len)
        {
        TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
-       memcpy(out, t->ent, t->entlen);
+       *pout = (unsigned char *)t->ent;
        t->entcnt++;
        return t->entlen;
        }
 
-static size_t test_nonce(DRBG_CTX *dctx, unsigned char *out,
+static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
                                 int entropy, size_t min_len, size_t max_len)
        {
        TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
-       memcpy(out, t->nonce, t->noncelen);
+       *pout = (unsigned char *)t->nonce;
        t->noncecnt++;
        return t->noncelen;
        }
 
-void FIPS_corrupt_drbg(void)
-       {
-       sha512_entropyinputpr2[0]++;
-       }
-
 static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        {
        TEST_ENT t;
        int rv = 0;
+       size_t adinlen;
        unsigned char randout[1024];
        if (!FIPS_drbg_init(dctx, td->nid, td->flags))
                return 0;
-       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, test_nonce))
+       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
                return 0;
 
        FIPS_drbg_set_app_data(dctx, &t);
@@ -780,8 +777,16 @@ static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        t.ent = td->entpr;
        t.entlen = td->entprlen;
 
+       /* Note for CTR without DF some additional input values
+        * ignore bytes after the keylength: so reduce adinlen
+        * to half to ensure invalid data is fed in.
+        */
+       if (!fips_post_corrupt(FIPS_TEST_DRBG, dctx->type, &dctx->flags))
+               adinlen = td->adinlen / 2;
+       else
+               adinlen = td->adinlen;
        if (!FIPS_drbg_generate(dctx, randout, td->katlen, 0, 1,
-                               td->adin, td->adinlen))
+                               td->adin, adinlen))
                goto err;
 
        t.ent = td->entg;
@@ -797,6 +802,8 @@ static int fips_drbg_single_kat(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        rv = 1;
 
        err:
+       if (rv == 0)
+               FIPSerr(FIPS_F_FIPS_DRBG_SINGLE_KAT, FIPS_R_SELFTEST_FAILED);
        FIPS_drbg_uninstantiate(dctx);
        
        return rv;
@@ -818,7 +825,7 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        if (!FIPS_drbg_init(dctx, td->nid, td->flags))
                goto err;
 
-       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, test_nonce))
+       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
                goto err;
 
        FIPS_drbg_set_app_data(dctx, &t);
@@ -857,10 +864,17 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
                goto err;
                }
 
+       dctx->flags &= ~DRBG_FLAG_NOERR;
+       if (!FIPS_drbg_uninstantiate(dctx))
+               {
+               FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
+               goto err;
+               }
+
        /* Instantiate with valid data. NB: errors now reported again */
        if (!FIPS_drbg_init(dctx, td->nid, td->flags))
                goto err;
-       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, test_nonce))
+       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
                goto err;
        FIPS_drbg_set_app_data(dctx, &t);
 
@@ -909,12 +923,20 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
                goto err;
                }
                
+       dctx->flags &= ~DRBG_FLAG_NOERR;
+
+       if (!FIPS_drbg_uninstantiate(dctx))
+               {
+               FIPSerr(FIPS_F_FIPS_DRBG_HEALTH_CHECK, FIPS_R_UNINSTANTIATE_ERROR);
+               goto err;
+               }
+
 
        /* Instantiate again with valid data */
 
        if (!FIPS_drbg_init(dctx, td->nid, td->flags))
                goto err;
-       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, test_nonce))
+       if (!FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0, test_nonce, 0))
                goto err;
        FIPS_drbg_set_app_data(dctx, &t);
 
@@ -952,11 +974,11 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
                }
 
        FIPS_drbg_uninstantiate(dctx);
-       p = (unsigned char *)dctx;
+       p = (unsigned char *)&dctx->d;
        /* Standard says we have to check uninstantiate really zeroes
         * the data...
         */
-       for (i = 0; i < sizeof(DRBG_CTX); i++)
+       for (i = 0; i < sizeof(dctx->d); i++)
                {
                if (*p != 0)
                        {
@@ -978,7 +1000,7 @@ static int fips_drbg_health_check(DRBG_CTX *dctx, DRBG_SELFTEST_DATA *td)
        return 0;
 
        }
-               
+
 
 int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags)
        {
@@ -1001,20 +1023,31 @@ int FIPS_selftest_drbg(void)
        {
        DRBG_CTX *dctx;
        DRBG_SELFTEST_DATA *td;
+       int rv = 1;
        dctx = FIPS_drbg_new(0, 0);
        if (!dctx)
                return 0;
        for (td = drbg_test; td->nid != 0; td++)
                {
+               if (!fips_post_started(FIPS_TEST_DRBG, td->nid, &td->flags))
+                       return 1;
                if (!fips_drbg_single_kat(dctx, td))
-                       break;
+                       {
+                       fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
+                       rv = 0;
+                       continue;
+                       }
                if (!fips_drbg_health_check(dctx, td))
-                       break;
+                       {
+                       fips_post_failed(FIPS_TEST_DRBG, td->nid, &td->flags);
+                       rv = 0;
+                       continue;
+                       }
+               if (!fips_post_success(FIPS_TEST_DRBG, td->nid, &td->flags))
+                       return 0;
                }
        FIPS_drbg_free(dctx);
-       if (td->nid == 0)
-               return 1;
-       return 0;
+       return rv;
        }