RHEL4/crypto/cipher.c
<<
>>
Prefs
   1/*
   2 * Cryptographic API.
   3 *
   4 * Cipher operations.
   5 *
   6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License as published by the Free
  10 * Software Foundation; either version 2 of the License, or (at your option) 
  11 * any later version.
  12 *
  13 */
  14#include <linux/kernel.h>
  15#include <linux/crypto.h>
  16#include <linux/errno.h>
  17#include <linux/mm.h>
  18#include <linux/slab.h>
  19#include <asm/scatterlist.h>
  20#include "internal.h"
  21#include "scatterwalk.h"
  22
  23typedef void (cryptfn_t)(void *, u8 *, const u8 *);
  24typedef void (procfn_t)(struct crypto_tfm *, u8 *,
  25                        u8*, cryptfn_t, int enc, void *, int);
  26
  27static inline void xor_64(u8 *a, const u8 *b)
  28{
  29        ((u32 *)a)[0] ^= ((u32 *)b)[0];
  30        ((u32 *)a)[1] ^= ((u32 *)b)[1];
  31}
  32
  33static inline void xor_128(u8 *a, const u8 *b)
  34{
  35        ((u32 *)a)[0] ^= ((u32 *)b)[0];
  36        ((u32 *)a)[1] ^= ((u32 *)b)[1];
  37        ((u32 *)a)[2] ^= ((u32 *)b)[2];
  38        ((u32 *)a)[3] ^= ((u32 *)b)[3];
  39}
  40
  41
  42/* 
  43 * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  44 * multiple page boundaries by using temporary blocks.  In user context,
  45 * the kernel is given a chance to schedule us once per block.
  46 */
  47static int crypt(struct crypto_tfm *tfm,
  48                 struct scatterlist *dst,
  49                 struct scatterlist *src,
  50                 unsigned int nbytes, cryptfn_t crfn,
  51                 procfn_t prfn, int enc, void *info)
  52{
  53        struct scatter_walk walk_in, walk_out;
  54        const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
  55        u8 tmp_src[bsize];
  56        u8 tmp_dst[bsize];
  57
  58        if (!nbytes)
  59                return 0;
  60
  61        if (nbytes % bsize) {
  62                tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  63                return -EINVAL;
  64        }
  65
  66        scatterwalk_start(&walk_in, src);
  67        scatterwalk_start(&walk_out, dst);
  68
  69        for(;;) {
  70                u8 *src_p, *dst_p;
  71                int in_place;
  72
  73                scatterwalk_map(&walk_in, 0);
  74                scatterwalk_map(&walk_out, 1);
  75                src_p = scatterwalk_whichbuf(&walk_in, bsize, tmp_src);
  76                dst_p = scatterwalk_whichbuf(&walk_out, bsize, tmp_dst);
  77                in_place = scatterwalk_samebuf(&walk_in, &walk_out,
  78                                               src_p, dst_p);
  79
  80                nbytes -= bsize;
  81
  82                scatterwalk_copychunks(src_p, &walk_in, bsize, 0);
  83
  84                prfn(tfm, dst_p, src_p, crfn, enc, info, in_place);
  85
  86                scatterwalk_done(&walk_in, 0, nbytes);
  87
  88                scatterwalk_copychunks(dst_p, &walk_out, bsize, 1);
  89                scatterwalk_done(&walk_out, 1, nbytes);
  90
  91                if (!nbytes)
  92                        return 0;
  93
  94                crypto_yield(tfm);
  95        }
  96}
  97
  98static void cbc_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
  99                        cryptfn_t fn, int enc, void *info, int in_place)
 100{
 101        u8 *iv = info;
 102        
 103        /* Null encryption */
 104        if (!iv)
 105                return;
 106                
 107        if (enc) {
 108                tfm->crt_u.cipher.cit_xor_block(iv, src);
 109                fn(crypto_tfm_ctx(tfm), dst, iv);
 110                memcpy(iv, dst, crypto_tfm_alg_blocksize(tfm));
 111        } else {
 112                u8 stack[in_place ? crypto_tfm_alg_blocksize(tfm) : 0];
 113                u8 *buf = in_place ? stack : dst;
 114
 115                fn(crypto_tfm_ctx(tfm), buf, src);
 116                tfm->crt_u.cipher.cit_xor_block(buf, iv);
 117                memcpy(iv, src, crypto_tfm_alg_blocksize(tfm));
 118                if (buf != dst)
 119                        memcpy(dst, buf, crypto_tfm_alg_blocksize(tfm));
 120        }
 121}
 122
 123static void ecb_process(struct crypto_tfm *tfm, u8 *dst, u8 *src,
 124                        cryptfn_t fn, int enc, void *info, int in_place)
 125{
 126        fn(crypto_tfm_ctx(tfm), dst, src);
 127}
 128
 129static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
 130{
 131        struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
 132        
 133        if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
 134                tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
 135                return -EINVAL;
 136        } else
 137                return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
 138                                       &tfm->crt_flags);
 139}
 140
 141static int ecb_encrypt(struct crypto_tfm *tfm,
 142                       struct scatterlist *dst,
 143                       struct scatterlist *src, unsigned int nbytes)
 144{
 145        return crypt(tfm, dst, src, nbytes,
 146                     tfm->__crt_alg->cra_cipher.cia_encrypt,
 147                     ecb_process, 1, NULL);
 148}
 149
 150static int ecb_decrypt(struct crypto_tfm *tfm,
 151                       struct scatterlist *dst,
 152                       struct scatterlist *src,
 153                       unsigned int nbytes)
 154{
 155        return crypt(tfm, dst, src, nbytes,
 156                     tfm->__crt_alg->cra_cipher.cia_decrypt,
 157                     ecb_process, 1, NULL);
 158}
 159
 160static int cbc_encrypt(struct crypto_tfm *tfm,
 161                       struct scatterlist *dst,
 162                       struct scatterlist *src,
 163                       unsigned int nbytes)
 164{
 165        return crypt(tfm, dst, src, nbytes,
 166                     tfm->__crt_alg->cra_cipher.cia_encrypt,
 167                     cbc_process, 1, tfm->crt_cipher.cit_iv);
 168}
 169
 170static int cbc_encrypt_iv(struct crypto_tfm *tfm,
 171                          struct scatterlist *dst,
 172                          struct scatterlist *src,
 173                          unsigned int nbytes, u8 *iv)
 174{
 175        return crypt(tfm, dst, src, nbytes,
 176                     tfm->__crt_alg->cra_cipher.cia_encrypt,
 177                     cbc_process, 1, iv);
 178}
 179
 180static int cbc_decrypt(struct crypto_tfm *tfm,
 181                       struct scatterlist *dst,
 182                       struct scatterlist *src,
 183                       unsigned int nbytes)
 184{
 185        return crypt(tfm, dst, src, nbytes,
 186                     tfm->__crt_alg->cra_cipher.cia_decrypt,
 187                     cbc_process, 0, tfm->crt_cipher.cit_iv);
 188}
 189
 190static int cbc_decrypt_iv(struct crypto_tfm *tfm,
 191                          struct scatterlist *dst,
 192                          struct scatterlist *src,
 193                          unsigned int nbytes, u8 *iv)
 194{
 195        return crypt(tfm, dst, src, nbytes,
 196                     tfm->__crt_alg->cra_cipher.cia_decrypt,
 197                     cbc_process, 0, iv);
 198}
 199
 200static int nocrypt(struct crypto_tfm *tfm,
 201                   struct scatterlist *dst,
 202                   struct scatterlist *src,
 203                   unsigned int nbytes)
 204{
 205        return -ENOSYS;
 206}
 207
 208static int nocrypt_iv(struct crypto_tfm *tfm,
 209                      struct scatterlist *dst,
 210                      struct scatterlist *src,
 211                      unsigned int nbytes, u8 *iv)
 212{
 213        return -ENOSYS;
 214}
 215
 216int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
 217{
 218        u32 mode = flags & CRYPTO_TFM_MODE_MASK;
 219        
 220        tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
 221        if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
 222                tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
 223        
 224        return 0;
 225}
 226
 227int crypto_init_cipher_ops(struct crypto_tfm *tfm)
 228{
 229        int ret = 0;
 230        struct cipher_tfm *ops = &tfm->crt_cipher;
 231
 232        ops->cit_setkey = setkey;
 233
 234        switch (tfm->crt_cipher.cit_mode) {
 235        case CRYPTO_TFM_MODE_ECB:
 236                ops->cit_encrypt = ecb_encrypt;
 237                ops->cit_decrypt = ecb_decrypt;
 238                break;
 239                
 240        case CRYPTO_TFM_MODE_CBC:
 241                ops->cit_encrypt = cbc_encrypt;
 242                ops->cit_decrypt = cbc_decrypt;
 243                ops->cit_encrypt_iv = cbc_encrypt_iv;
 244                ops->cit_decrypt_iv = cbc_decrypt_iv;
 245                break;
 246                
 247        case CRYPTO_TFM_MODE_CFB:
 248                ops->cit_encrypt = nocrypt;
 249                ops->cit_decrypt = nocrypt;
 250                ops->cit_encrypt_iv = nocrypt_iv;
 251                ops->cit_decrypt_iv = nocrypt_iv;
 252                break;
 253        
 254        case CRYPTO_TFM_MODE_CTR:
 255                ops->cit_encrypt = nocrypt;
 256                ops->cit_decrypt = nocrypt;
 257                ops->cit_encrypt_iv = nocrypt_iv;
 258                ops->cit_decrypt_iv = nocrypt_iv;
 259                break;
 260
 261        default:
 262                BUG();
 263        }
 264        
 265        if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
 266                
 267                switch (crypto_tfm_alg_blocksize(tfm)) {
 268                case 8:
 269                        ops->cit_xor_block = xor_64;
 270                        break;
 271                        
 272                case 16:
 273                        ops->cit_xor_block = xor_128;
 274                        break;
 275                        
 276                default:
 277                        printk(KERN_WARNING "%s: block size %u not supported\n",
 278                               crypto_tfm_alg_name(tfm),
 279                               crypto_tfm_alg_blocksize(tfm));
 280                        ret = -EINVAL;
 281                        goto out;
 282                }
 283                
 284                ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
 285                ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL);
 286                if (ops->cit_iv == NULL)
 287                        ret = -ENOMEM;
 288        }
 289
 290out:    
 291        return ret;
 292}
 293
 294void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
 295{
 296        if (tfm->crt_cipher.cit_iv)
 297                kfree(tfm->crt_cipher.cit_iv);
 298}
 299