RHEL4/crypto/api.c
<<
>>
Prefs
   1/*
   2 * Scatterlist Cryptographic API.
   3 *
   4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
   5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
   6 *
   7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
   8 * and Nettle, by Niels M\xF6ller.
   9 *
  10 * This program is free software; you can redistribute it and/or modify it
  11 * under the terms of the GNU General Public License as published by the Free
  12 * Software Foundation; either version 2 of the License, or (at your option) 
  13 * any later version.
  14 *
  15 */
  16#include <linux/init.h>
  17#include <linux/crypto.h>
  18#include <linux/errno.h>
  19#include <linux/rwsem.h>
  20#include <linux/slab.h>
  21#include "internal.h"
  22
  23LIST_HEAD(crypto_alg_list);
  24DECLARE_RWSEM(crypto_alg_sem);
  25
  26static inline int crypto_alg_get(struct crypto_alg *alg)
  27{
  28        return try_module_get(alg->cra_module);
  29}
  30
  31static inline void crypto_alg_put(struct crypto_alg *alg)
  32{
  33        module_put(alg->cra_module);
  34}
  35
  36struct crypto_alg *crypto_alg_lookup(const char *name)
  37{
  38        struct crypto_alg *q, *alg = NULL;
  39
  40        if (!name)
  41                return NULL;
  42        
  43        down_read(&crypto_alg_sem);
  44        
  45        list_for_each_entry(q, &crypto_alg_list, cra_list) {
  46                if (!(strcmp(q->cra_name, name))) {
  47                        if (crypto_alg_get(q))
  48                                alg = q;
  49                        break;
  50                }
  51        }
  52        
  53        up_read(&crypto_alg_sem);
  54        return alg;
  55}
  56
  57static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
  58{
  59        tfm->crt_flags = 0;
  60        
  61        switch (crypto_tfm_alg_type(tfm)) {
  62        case CRYPTO_ALG_TYPE_CIPHER:
  63                return crypto_init_cipher_flags(tfm, flags);
  64                
  65        case CRYPTO_ALG_TYPE_DIGEST:
  66                return crypto_init_digest_flags(tfm, flags);
  67                
  68        case CRYPTO_ALG_TYPE_COMPRESS:
  69                return crypto_init_compress_flags(tfm, flags);
  70        
  71        default:
  72                break;
  73        }
  74        
  75        BUG();
  76        return -EINVAL;
  77}
  78
  79static int crypto_init_ops(struct crypto_tfm *tfm)
  80{
  81        switch (crypto_tfm_alg_type(tfm)) {
  82        case CRYPTO_ALG_TYPE_CIPHER:
  83                return crypto_init_cipher_ops(tfm);
  84                
  85        case CRYPTO_ALG_TYPE_DIGEST:
  86                return crypto_init_digest_ops(tfm);
  87                
  88        case CRYPTO_ALG_TYPE_COMPRESS:
  89                return crypto_init_compress_ops(tfm);
  90        
  91        default:
  92                break;
  93        }
  94        
  95        BUG();
  96        return -EINVAL;
  97}
  98
  99static void crypto_exit_ops(struct crypto_tfm *tfm)
 100{
 101        switch (crypto_tfm_alg_type(tfm)) {
 102        case CRYPTO_ALG_TYPE_CIPHER:
 103                crypto_exit_cipher_ops(tfm);
 104                break;
 105                
 106        case CRYPTO_ALG_TYPE_DIGEST:
 107                crypto_exit_digest_ops(tfm);
 108                break;
 109                
 110        case CRYPTO_ALG_TYPE_COMPRESS:
 111                crypto_exit_compress_ops(tfm);
 112                break;
 113        
 114        default:
 115                BUG();
 116                
 117        }
 118}
 119
 120struct crypto_tfm *crypto_alloc_tfm2(const char *name, u32 flags,
 121                                     int nomodload)
 122{
 123        struct crypto_tfm *tfm = NULL;
 124        struct crypto_alg *alg;
 125
 126        if (!nomodload) {
 127                alg = crypto_alg_mod_lookup(name);
 128        }
 129        else {
 130                alg = crypto_alg_lookup(name);
 131        }
 132
 133        if (alg == NULL)
 134                goto out;
 135        
 136        tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
 137        if (tfm == NULL)
 138                goto out_put;
 139
 140        memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
 141        
 142        tfm->__crt_alg = alg;
 143        
 144        if (crypto_init_flags(tfm, flags))
 145                goto out_free_tfm;
 146                
 147        if (crypto_init_ops(tfm)) {
 148                crypto_exit_ops(tfm);
 149                goto out_free_tfm;
 150        }
 151
 152        goto out;
 153
 154out_free_tfm:
 155        kfree(tfm);
 156        tfm = NULL;
 157out_put:
 158        crypto_alg_put(alg);
 159out:
 160        return tfm;
 161}
 162
 163struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
 164{
 165        return crypto_alloc_tfm2(name, flags, 0);
 166}
 167
 168void crypto_free_tfm(struct crypto_tfm *tfm)
 169{
 170        struct crypto_alg *alg = tfm->__crt_alg;
 171        int size = sizeof(*tfm) + alg->cra_ctxsize;
 172
 173        crypto_exit_ops(tfm);
 174        crypto_alg_put(alg);
 175        memset(tfm, 0, size);
 176        kfree(tfm);
 177}
 178
 179int crypto_register_alg(struct crypto_alg *alg)
 180{
 181        int ret = 0;
 182        struct crypto_alg *q;
 183        
 184        down_write(&crypto_alg_sem);
 185        
 186        list_for_each_entry(q, &crypto_alg_list, cra_list) {
 187                if (!(strcmp(q->cra_name, alg->cra_name))) {
 188                        ret = -EEXIST;
 189                        goto out;
 190                }
 191        }
 192        
 193        list_add_tail(&alg->cra_list, &crypto_alg_list);
 194out:    
 195        up_write(&crypto_alg_sem);
 196        return ret;
 197}
 198
 199int crypto_unregister_alg(struct crypto_alg *alg)
 200{
 201        int ret = -ENOENT;
 202        struct crypto_alg *q;
 203        
 204        BUG_ON(!alg->cra_module);
 205        
 206        down_write(&crypto_alg_sem);
 207        list_for_each_entry(q, &crypto_alg_list, cra_list) {
 208                if (alg == q) {
 209                        list_del(&alg->cra_list);
 210                        ret = 0;
 211                        goto out;
 212                }
 213        }
 214out:    
 215        up_write(&crypto_alg_sem);
 216        return ret;
 217}
 218
 219int crypto_alg_available(const char *name, u32 flags)
 220{
 221        int ret = 0;
 222        struct crypto_alg *alg = crypto_alg_mod_lookup(name);
 223        
 224        if (alg) {
 225                crypto_alg_put(alg);
 226                ret = 1;
 227        }
 228        
 229        return ret;
 230}
 231
 232static int __init init_crypto(void)
 233{
 234        printk(KERN_INFO "Initializing Cryptographic API\n");
 235        crypto_init_proc();
 236        return 0;
 237}
 238
 239__initcall(init_crypto);
 240
 241EXPORT_SYMBOL_GPL(crypto_register_alg);
 242EXPORT_SYMBOL_GPL(crypto_unregister_alg);
 243EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
 244EXPORT_SYMBOL_GPL(crypto_free_tfm);
 245EXPORT_SYMBOL_GPL(crypto_alg_available);
 246