crypto: shash - Export/import hash state only
[safe/jmp/linux-2.6] / crypto / ansi_cprng.c
index 0fac8ff..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;
                        }
@@ -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)
@@ -262,7 +267,7 @@ empty_rbuf:
                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;
@@ -278,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;
@@ -302,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)
@@ -337,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)