[CRYPTO] api: Added crypto_type support
[powerpc.git] / include / linux / crypto.h
index 5a0470e..8e9c407 100644 (file)
 #ifndef _LINUX_CRYPTO_H
 #define _LINUX_CRYPTO_H
 
+#include <asm/atomic.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/list.h>
+#include <linux/slab.h>
 #include <linux/string.h>
-#include <asm/page.h>
+#include <linux/uaccess.h>
 
 /*
  * Algorithm masks and types.
  */
-#define CRYPTO_ALG_TYPE_MASK           0x000000ff
+#define CRYPTO_ALG_TYPE_MASK           0x0000000f
 #define CRYPTO_ALG_TYPE_CIPHER         0x00000001
 #define CRYPTO_ALG_TYPE_DIGEST         0x00000002
 #define CRYPTO_ALG_TYPE_COMPRESS       0x00000004
 
+#define CRYPTO_ALG_LARVAL              0x00000010
+#define CRYPTO_ALG_DEAD                        0x00000020
+#define CRYPTO_ALG_DYING               0x00000040
+#define CRYPTO_ALG_ASYNC               0x00000080
+
 /*
  * Transform masks and values (for crt_flags).
  */
 #define CRYPTO_DIR_ENCRYPT             1
 #define CRYPTO_DIR_DECRYPT             0
 
+/*
+ * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
+ * declaration) is used to ensure that the crypto_tfm context structure is
+ * aligned correctly for the given architecture so that there are no alignment
+ * faults for C data types.  In particular, this is required on platforms such
+ * as arm where pointers are 32-bit aligned but there are data types such as
+ * u64 which require 64-bit alignment.
+ */
+#if defined(ARCH_KMALLOC_MINALIGN)
+#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
+#elif defined(ARCH_SLAB_MINALIGN)
+#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
+#endif
+
+#ifdef CRYPTO_MINALIGN
+#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
+#else
+#define CRYPTO_MINALIGN_ATTR
+#endif
+
 struct scatterlist;
 struct crypto_tfm;
+struct crypto_type;
 
 struct cipher_desc {
        struct crypto_tfm *tfm;
-       void (*crfn)(void *ctx, u8 *dst, const u8 *src);
+       void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
        unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
                             const u8 *src, unsigned int nbytes);
        void *info;
@@ -79,10 +107,10 @@ struct cipher_desc {
 struct cipher_alg {
        unsigned int cia_min_keysize;
        unsigned int cia_max_keysize;
-       int (*cia_setkey)(void *ctx, const u8 *key,
-                         unsigned int keylen, u32 *flags);
-       void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src);
-       void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src);
+       int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
+                         unsigned int keylen);
+       void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
+       void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
 
        unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
                                        u8 *dst, const u8 *src,
@@ -100,20 +128,19 @@ struct cipher_alg {
 
 struct digest_alg {
        unsigned int dia_digestsize;
-       void (*dia_init)(void *ctx);
-       void (*dia_update)(void *ctx, const u8 *data, unsigned int len);
-       void (*dia_final)(void *ctx, u8 *out);
-       int (*dia_setkey)(void *ctx, const u8 *key,
-                         unsigned int keylen, u32 *flags);
+       void (*dia_init)(struct crypto_tfm *tfm);
+       void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
+                          unsigned int len);
+       void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
+       int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
+                         unsigned int keylen);
 };
 
 struct compress_alg {
-       int (*coa_init)(void *ctx);
-       void (*coa_exit)(void *ctx);
-       int (*coa_compress)(void *ctx, const u8 *src, unsigned int slen,
-                           u8 *dst, unsigned int *dlen);
-       int (*coa_decompress)(void *ctx, const u8 *src, unsigned int slen,
-                             u8 *dst, unsigned int *dlen);
+       int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
+                           unsigned int slen, u8 *dst, unsigned int *dlen);
+       int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
+                             unsigned int slen, u8 *dst, unsigned int *dlen);
 };
 
 #define cra_cipher     cra_u.cipher
@@ -122,21 +149,30 @@ struct compress_alg {
 
 struct crypto_alg {
        struct list_head cra_list;
+       struct list_head cra_users;
+
        u32 cra_flags;
        unsigned int cra_blocksize;
        unsigned int cra_ctxsize;
        unsigned int cra_alignmask;
 
        int cra_priority;
+       atomic_t cra_refcnt;
+
+       char cra_name[CRYPTO_MAX_ALG_NAME];
+       char cra_driver_name[CRYPTO_MAX_ALG_NAME];
 
-       const char cra_name[CRYPTO_MAX_ALG_NAME];
-       const char cra_driver_name[CRYPTO_MAX_ALG_NAME];
+       const struct crypto_type *cra_type;
 
        union {
                struct cipher_alg cipher;
                struct digest_alg digest;
                struct compress_alg compress;
        } cra_u;
+
+       int (*cra_init)(struct crypto_tfm *tfm);
+       void (*cra_exit)(struct crypto_tfm *tfm);
+       void (*cra_destroy)(struct crypto_alg *alg);
        
        struct module *cra_module;
 };
@@ -161,8 +197,8 @@ static inline int crypto_alg_available(const char *name, u32 flags)
 
 /*
  * Transforms: user-instantiated objects which encapsulate algorithms
- * and core processing logic.  Managed via crypto_alloc_tfm() and
- * crypto_free_tfm(), as well as the various helpers below.
+ * and core processing logic.  Managed via crypto_alloc_*() and
+ * crypto_free_*(), as well as the various helpers below.
  */
 
 struct cipher_tfm {
@@ -229,23 +265,24 @@ struct crypto_tfm {
        
        struct crypto_alg *__crt_alg;
 
-       char __crt_ctx[] __attribute__ ((__aligned__));
+       void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
+};
+
+enum {
+       CRYPTOA_UNSPEC,
+       CRYPTOA_ALG,
+};
+
+struct crypto_attr_alg {
+       char name[CRYPTO_MAX_ALG_NAME];
 };
 
 /* 
  * Transform user interface.
  */
  
-/*
- * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
- * If that fails and the kernel supports dynamically loadable modules, it
- * will then attempt to load a module of the same name or alias.  A refcount
- * is grabbed on the algorithm which is then associated with the new transform.
- *
- * crypto_free_tfm() frees up the transform and any associated resources,
- * then drops the refcount on the associated algorithm.
- */
 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
+struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
 void crypto_free_tfm(struct crypto_tfm *tfm);
 
 /*
@@ -256,6 +293,16 @@ static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
        return tfm->__crt_alg->cra_name;
 }
 
+static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
+{
+       return tfm->__crt_alg->cra_driver_name;
+}
+
+static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
+{
+       return tfm->__crt_alg->cra_priority;
+}
+
 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
 {
        return module_name(tfm->__crt_alg->cra_module);
@@ -346,8 +393,6 @@ static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
                                        const u8 *key, unsigned int keylen)
 {
        BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
-       if (tfm->crt_digest.dit_setkey == NULL)
-               return -ENOSYS;
        return tfm->crt_digest.dit_setkey(tfm, key, keylen);
 }
 
@@ -373,7 +418,6 @@ static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
                                            unsigned int nbytes, u8 *iv)
 {
        BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
-       BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
        return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
 }                                        
 
@@ -392,7 +436,6 @@ static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
                                            unsigned int nbytes, u8 *iv)
 {
        BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
-       BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
        return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
 }