crypto: ansi_prng - alloc cipher just in init
[safe/jmp/linux-2.6] / crypto / ansi_cprng.c
index 72db0fd..5357ba7 100644 (file)
@@ -132,9 +132,15 @@ static int _get_more_prng_bytes(struct prng_context *ctx)
                         */
                        if (!memcmp(ctx->rand_data, ctx->last_rand_data,
                                        DEFAULT_BLK_SZ)) {
+                               if (fips_enabled) {
+                                       panic("cprng %p Failed repetition check!\n",
+                                               ctx);
+                               }
+
                                printk(KERN_ERR
                                        "ctx %p Failed repetition check!\n",
                                        ctx);
+
                                ctx->flags |= PRNG_NEED_RESET;
                                return -EINVAL;
                        }
@@ -161,7 +167,7 @@ static int _get_more_prng_bytes(struct prng_context *ctx)
        /*
         * Now update our DT value
         */
-       for (i = 0; i < DEFAULT_BLK_SZ; i++) {
+       for (i = DEFAULT_BLK_SZ - 1; i >= 0; i--) {
                ctx->DT[i] += 1;
                if (ctx->DT[i] != 0)
                        break;
@@ -181,7 +187,6 @@ static int _get_more_prng_bytes(struct prng_context *ctx)
 /* Our exported functions */
 static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx)
 {
-       unsigned long flags;
        unsigned char *ptr = buf;
        unsigned int byte_count = (unsigned int)nbytes;
        int err;
@@ -190,7 +195,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx)
        if (nbytes < 0)
                return -EINVAL;
 
-       spin_lock_irqsave(&ctx->prng_lock, flags);
+       spin_lock_bh(&ctx->prng_lock);
 
        err = -EINVAL;
        if (ctx->flags & PRNG_NEED_RESET)
@@ -223,9 +228,10 @@ remainder:
        }
 
        /*
-        * Copy up to the next whole block size
+        * Copy any data less than an entire block
         */
        if (byte_count < DEFAULT_BLK_SZ) {
+empty_rbuf:
                for (; ctx->rand_data_valid < DEFAULT_BLK_SZ;
                        ctx->rand_data_valid++) {
                        *ptr = ctx->rand_data[ctx->rand_data_valid];
@@ -240,24 +246,28 @@ remainder:
         * Now copy whole blocks
         */
        for (; byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
-               if (_get_more_prng_bytes(ctx) < 0) {
-                       memset(buf, 0, nbytes);
-                       err = -EINVAL;
-                       goto done;
+               if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
+                       if (_get_more_prng_bytes(ctx) < 0) {
+                               memset(buf, 0, nbytes);
+                               err = -EINVAL;
+                               goto done;
+                       }
                }
+               if (ctx->rand_data_valid > 0)
+                       goto empty_rbuf;
                memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
                ctx->rand_data_valid += DEFAULT_BLK_SZ;
                ptr += DEFAULT_BLK_SZ;
        }
 
        /*
-        * Now copy any extra partial data
+        * Now go back and get any remaining partial block
         */
        if (byte_count)
                goto remainder;
 
 done:
-       spin_unlock_irqrestore(&ctx->prng_lock, flags);
+       spin_unlock_bh(&ctx->prng_lock);
        dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",
                err, ctx);
        return err;
@@ -273,10 +283,9 @@ static int reset_prng_context(struct prng_context *ctx,
                              unsigned char *V, unsigned char *DT)
 {
        int ret;
-       int rc = -EINVAL;
        unsigned char *prng_key;
 
-       spin_lock(&ctx->prng_lock);
+       spin_lock_bh(&ctx->prng_lock);
        ctx->flags |= PRNG_NEED_RESET;
 
        prng_key = (key != NULL) ? key : (unsigned char *)DEFAULT_PRNG_KEY;
@@ -297,34 +306,20 @@ static int reset_prng_context(struct prng_context *ctx,
        memset(ctx->rand_data, 0, DEFAULT_BLK_SZ);
        memset(ctx->last_rand_data, 0, DEFAULT_BLK_SZ);
 
-       if (ctx->tfm)
-               crypto_free_cipher(ctx->tfm);
-
-       ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
-       if (IS_ERR(ctx->tfm)) {
-               dbgprint(KERN_CRIT "Failed to alloc tfm for context %p\n",
-                       ctx);
-               ctx->tfm = NULL;
-               goto out;
-       }
-
        ctx->rand_data_valid = DEFAULT_BLK_SZ;
 
        ret = crypto_cipher_setkey(ctx->tfm, prng_key, klen);
        if (ret) {
                dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
                        crypto_cipher_get_flags(ctx->tfm));
-               crypto_free_cipher(ctx->tfm);
                goto out;
        }
 
-       rc = 0;
+       ret = 0;
        ctx->flags &= ~PRNG_NEED_RESET;
 out:
-       spin_unlock(&ctx->prng_lock);
-
-       return rc;
-
+       spin_unlock_bh(&ctx->prng_lock);
+       return ret;
 }
 
 static int cprng_init(struct crypto_tfm *tfm)
@@ -332,8 +327,23 @@ static int cprng_init(struct crypto_tfm *tfm)
        struct prng_context *ctx = crypto_tfm_ctx(tfm);
 
        spin_lock_init(&ctx->prng_lock);
+       ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
+       if (IS_ERR(ctx->tfm)) {
+               dbgprint(KERN_CRIT "Failed to alloc tfm for context %p\n",
+                               ctx);
+               return PTR_ERR(ctx->tfm);
+       }
+
+       if (reset_prng_context(ctx, NULL, DEFAULT_PRNG_KSZ, NULL, NULL) < 0)
+               return -EINVAL;
 
-       return reset_prng_context(ctx, NULL, DEFAULT_PRNG_KSZ, NULL, NULL);
+       /*
+        * after allocation, we should always force the user to reset
+        * so they don't inadvertently use the insecure default values
+        * without specifying them intentially
+        */
+       ctx->flags |= PRNG_NEED_RESET;
+       return 0;
 }
 
 static void cprng_exit(struct crypto_tfm *tfm)
@@ -349,15 +359,25 @@ static int cprng_get_random(struct crypto_rng *tfm, u8 *rdata,
        return get_prng_bytes(rdata, dlen, prng);
 }
 
+/*
+ *  This is the cprng_registered reset method the seed value is
+ *  interpreted as the tuple { V KEY DT}
+ *  V and KEY are required during reset, and DT is optional, detected
+ *  as being present by testing the length of the seed
+ */
 static int cprng_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
 {
        struct prng_context *prng = crypto_rng_ctx(tfm);
-       u8 *key = seed + DEFAULT_PRNG_KSZ;
+       u8 *key = seed + DEFAULT_BLK_SZ;
+       u8 *dt = NULL;
 
        if (slen < DEFAULT_PRNG_KSZ + DEFAULT_BLK_SZ)
                return -EINVAL;
 
-       reset_prng_context(prng, key, DEFAULT_PRNG_KSZ, seed, NULL);
+       if (slen >= (2 * DEFAULT_BLK_SZ + DEFAULT_PRNG_KSZ))
+               dt = key + DEFAULT_PRNG_KSZ;
+
+       reset_prng_context(prng, key, DEFAULT_PRNG_KSZ, seed, dt);
 
        if (prng->flags & PRNG_NEED_RESET)
                return -EINVAL;
@@ -379,7 +399,7 @@ static struct crypto_alg rng_alg = {
                .rng = {
                        .rng_make_random        = cprng_get_random,
                        .rng_reset              = cprng_reset,
-                       .seedsize = DEFAULT_PRNG_KSZ + DEFAULT_BLK_SZ,
+                       .seedsize = DEFAULT_PRNG_KSZ + 2*DEFAULT_BLK_SZ,
                }
        }
 };