]> err.no Git - linux-2.6/commitdiff
[CRYPTO] api: Add type-safe spawns
authorHerbert Xu <herbert@gondor.apana.org.au>
Sat, 16 Dec 2006 23:05:58 +0000 (10:05 +1100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Tue, 6 Feb 2007 22:21:01 +0000 (09:21 +1100)
This patch allows spawns of specific types (e.g., cipher) to be allocated.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/algapi.c
crypto/cbc.c
crypto/ecb.c
crypto/hmac.c
crypto/lrw.c
crypto/pcbc.c
crypto/xcbc.c
include/crypto/algapi.h

index 69eb504721a43dc3abd941a5be99dd0e4f0d825b..0f1abca1b98ce296f796fcb1d59998cb260bcae7 100644 (file)
@@ -377,7 +377,8 @@ void crypto_drop_spawn(struct crypto_spawn *spawn)
 }
 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
 
-struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn)
+struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
+                                   u32 mask)
 {
        struct crypto_alg *alg;
        struct crypto_alg *alg2;
@@ -396,10 +397,18 @@ struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn)
                return ERR_PTR(-EAGAIN);
        }
 
+       tfm = ERR_PTR(-EINVAL);
+       if (unlikely((alg->cra_flags ^ type) & mask))
+               goto out_put_alg;
+
        tfm = __crypto_alloc_tfm(alg);
        if (IS_ERR(tfm))
-               crypto_mod_put(alg);
+               goto out_put_alg;
+
+       return tfm;
 
+out_put_alg:
+       crypto_mod_put(alg);
        return tfm;
 }
 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
index f5542b4db387eea82b90e023da20e8e94dbe4ffb..136fea7e700095542c585cbb7cae9c16b64eb667 100644 (file)
@@ -243,6 +243,7 @@ static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
        struct crypto_instance *inst = (void *)tfm->__crt_alg;
        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
        struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
+       struct crypto_cipher *cipher;
 
        switch (crypto_tfm_alg_blocksize(tfm)) {
        case 8:
@@ -260,11 +261,11 @@ static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
                        ctx->xor = xor_quad;
        }
 
-       tfm = crypto_spawn_tfm(spawn);
-       if (IS_ERR(tfm))
-               return PTR_ERR(tfm);
+       cipher = crypto_spawn_cipher(spawn);
+       if (IS_ERR(cipher))
+               return PTR_ERR(cipher);
 
-       ctx->child = crypto_cipher_cast(tfm);
+       ctx->child = cipher;
        return 0;
 }
 
index f239aa9c4017834a7a193530386f0dc924a3fcbf..839a0aed8c229c26631ff56032165a19295a0531 100644 (file)
@@ -99,12 +99,13 @@ static int crypto_ecb_init_tfm(struct crypto_tfm *tfm)
        struct crypto_instance *inst = (void *)tfm->__crt_alg;
        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
        struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm);
+       struct crypto_cipher *cipher;
 
-       tfm = crypto_spawn_tfm(spawn);
-       if (IS_ERR(tfm))
-               return PTR_ERR(tfm);
+       cipher = crypto_spawn_cipher(spawn);
+       if (IS_ERR(cipher))
+               return PTR_ERR(cipher);
 
-       ctx->child = crypto_cipher_cast(tfm);
+       ctx->child = cipher;
        return 0;
 }
 
index b521bcd2b2c6036f155bc23641d41df877baca18..44187c5ee5933cbf9195ec799d337053ef0e0b8e 100644 (file)
@@ -172,15 +172,16 @@ static int hmac_digest(struct hash_desc *pdesc, struct scatterlist *sg,
 
 static int hmac_init_tfm(struct crypto_tfm *tfm)
 {
+       struct crypto_hash *hash;
        struct crypto_instance *inst = (void *)tfm->__crt_alg;
        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
        struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));
 
-       tfm = crypto_spawn_tfm(spawn);
-       if (IS_ERR(tfm))
-               return PTR_ERR(tfm);
+       hash = crypto_spawn_hash(spawn);
+       if (IS_ERR(hash))
+               return PTR_ERR(hash);
 
-       ctx->child = crypto_hash_cast(tfm);
+       ctx->child = hash;
        return 0;
 }
 
index 56642586d84f733b547e1a8ebef539de4a93e760..b4105080ac7aa9d5886ea28772cfea6101197d27 100644 (file)
@@ -201,21 +201,22 @@ static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
 
 static int init_tfm(struct crypto_tfm *tfm)
 {
+       struct crypto_cipher *cipher;
        struct crypto_instance *inst = (void *)tfm->__crt_alg;
        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
        struct priv *ctx = crypto_tfm_ctx(tfm);
        u32 *flags = &tfm->crt_flags;
 
-       tfm = crypto_spawn_tfm(spawn);
-       if (IS_ERR(tfm))
-               return PTR_ERR(tfm);
+       cipher = crypto_spawn_cipher(spawn);
+       if (IS_ERR(cipher))
+               return PTR_ERR(cipher);
 
-       if (crypto_tfm_alg_blocksize(tfm) != 16) {
+       if (crypto_cipher_blocksize(cipher) != 16) {
                *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
                return -EINVAL;
        }
 
-       ctx->child = crypto_cipher_cast(tfm);
+       ctx->child = cipher;
        return 0;
 }
 
index 0ffb46eb259feafc694e49941e1678394760b092..5174d7fdad6e20f2d3051d60d3c764dfeb8dbaf1 100644 (file)
@@ -247,6 +247,7 @@ static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
        struct crypto_instance *inst = (void *)tfm->__crt_alg;
        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
        struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
+       struct crypto_cipher *cipher;
 
        switch (crypto_tfm_alg_blocksize(tfm)) {
        case 8:
@@ -264,11 +265,11 @@ static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
                        ctx->xor = xor_quad;
        }
 
-       tfm = crypto_spawn_tfm(spawn);
-       if (IS_ERR(tfm))
-               return PTR_ERR(tfm);
+       cipher = crypto_spawn_cipher(spawn);
+       if (IS_ERR(cipher))
+               return PTR_ERR(cipher);
 
-       ctx->child = crypto_cipher_cast(tfm);
+       ctx->child = cipher;
        return 0;
 }
 
index 317e9f08fc045ca7cee745c7418b606b11020ad8..d7b4be0a057d8c96f29cfdbb797bdaeaddfbbc50 100644 (file)
@@ -254,14 +254,15 @@ static int crypto_xcbc_digest(struct hash_desc *pdesc,
 
 static int xcbc_init_tfm(struct crypto_tfm *tfm)
 {
+       struct crypto_cipher *cipher;
        struct crypto_instance *inst = (void *)tfm->__crt_alg;
        struct crypto_spawn *spawn = crypto_instance_ctx(inst);
        struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm));
        int bs = crypto_hash_blocksize(__crypto_hash_cast(tfm));
 
-       tfm = crypto_spawn_tfm(spawn);
-       if (IS_ERR(tfm))
-               return PTR_ERR(tfm);
+       cipher = crypto_spawn_cipher(spawn);
+       if (IS_ERR(cipher))
+               return PTR_ERR(cipher);
 
        switch(bs) {
        case 16:
@@ -271,7 +272,7 @@ static int xcbc_init_tfm(struct crypto_tfm *tfm)
                return -EINVAL;
        }
 
-       ctx->child = crypto_cipher_cast(tfm);
+       ctx->child = cipher;
        ctx->odds = (u8*)(ctx+1);
        ctx->prev = ctx->odds + bs;
        ctx->key = ctx->prev + bs;
index 5748aecdb414704c5baec50cac15fa5ee0d7b7a2..99c534d573d28222cf061569768380b302e7c6e1 100644 (file)
@@ -93,7 +93,8 @@ struct crypto_template *crypto_lookup_template(const char *name);
 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
                      struct crypto_instance *inst);
 void crypto_drop_spawn(struct crypto_spawn *spawn);
-struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn);
+struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
+                                   u32 mask);
 
 struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
                                       u32 type, u32 mask);
@@ -132,11 +133,28 @@ static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
        return crypto_tfm_ctx_aligned(&tfm->base);
 }
 
+static inline struct crypto_cipher *crypto_spawn_cipher(
+       struct crypto_spawn *spawn)
+{
+       u32 type = CRYPTO_ALG_TYPE_CIPHER;
+       u32 mask = CRYPTO_ALG_TYPE_MASK;
+
+       return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
+}
+
 static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
 {
        return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
 }
 
+static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn)
+{
+       u32 type = CRYPTO_ALG_TYPE_HASH;
+       u32 mask = CRYPTO_ALG_TYPE_HASH_MASK;
+
+       return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask));
+}
+
 static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
 {
        return crypto_tfm_ctx_aligned(&tfm->base);