Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Cipher operations. |
| 5 | * |
| 6 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 7 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/compiler.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/crypto.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <asm/scatterlist.h> |
| 23 | #include "internal.h" |
| 24 | #include "scatterwalk.h" |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static inline void xor_64(u8 *a, const u8 *b) |
| 27 | { |
| 28 | ((u32 *)a)[0] ^= ((u32 *)b)[0]; |
| 29 | ((u32 *)a)[1] ^= ((u32 *)b)[1]; |
| 30 | } |
| 31 | |
| 32 | static inline void xor_128(u8 *a, const u8 *b) |
| 33 | { |
| 34 | ((u32 *)a)[0] ^= ((u32 *)b)[0]; |
| 35 | ((u32 *)a)[1] ^= ((u32 *)b)[1]; |
| 36 | ((u32 *)a)[2] ^= ((u32 *)b)[2]; |
| 37 | ((u32 *)a)[3] ^= ((u32 *)b)[3]; |
| 38 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 40 | static unsigned int crypt_slow(const struct cipher_desc *desc, |
| 41 | struct scatter_walk *in, |
| 42 | struct scatter_walk *out, unsigned int bsize) |
| 43 | { |
Herbert Xu | 9d853c3 | 2005-07-15 07:41:31 -0700 | [diff] [blame] | 44 | unsigned long alignmask = crypto_tfm_alg_alignmask(desc->tfm); |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 45 | u8 buffer[bsize * 2 + alignmask]; |
| 46 | u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 47 | u8 *dst = src + bsize; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 48 | unsigned int n; |
| 49 | |
| 50 | n = scatterwalk_copychunks(src, in, bsize, 0); |
| 51 | scatterwalk_advance(in, n); |
| 52 | |
| 53 | desc->prfn(desc, dst, src, bsize); |
| 54 | |
| 55 | n = scatterwalk_copychunks(dst, out, bsize, 1); |
| 56 | scatterwalk_advance(out, n); |
| 57 | |
| 58 | return bsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 61 | static inline unsigned int crypt_fast(const struct cipher_desc *desc, |
| 62 | struct scatter_walk *in, |
| 63 | struct scatter_walk *out, |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 64 | unsigned int nbytes, u8 *tmp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 66 | u8 *src, *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 68 | src = in->data; |
| 69 | dst = scatterwalk_samebuf(in, out) ? src : out->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 71 | if (tmp) { |
| 72 | memcpy(tmp, in->data, nbytes); |
| 73 | src = tmp; |
| 74 | dst = tmp; |
| 75 | } |
| 76 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 77 | nbytes = desc->prfn(desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 79 | if (tmp) |
| 80 | memcpy(out->data, tmp, nbytes); |
| 81 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 82 | scatterwalk_advance(in, nbytes); |
| 83 | scatterwalk_advance(out, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 85 | return nbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Generic encrypt/decrypt wrapper for ciphers, handles operations across |
| 90 | * multiple page boundaries by using temporary blocks. In user context, |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 91 | * the kernel is given a chance to schedule us once per page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 93 | static int crypt(const struct cipher_desc *desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | struct scatterlist *dst, |
| 95 | struct scatterlist *src, |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 96 | unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
| 98 | struct scatter_walk walk_in, walk_out; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 99 | struct crypto_tfm *tfm = desc->tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | const unsigned int bsize = crypto_tfm_alg_blocksize(tfm); |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 101 | unsigned int alignmask = crypto_tfm_alg_alignmask(tfm); |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 102 | unsigned long buffer = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | if (!nbytes) |
| 105 | return 0; |
| 106 | |
| 107 | if (nbytes % bsize) { |
| 108 | tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN; |
| 109 | return -EINVAL; |
| 110 | } |
| 111 | |
| 112 | scatterwalk_start(&walk_in, src); |
| 113 | scatterwalk_start(&walk_out, dst); |
| 114 | |
| 115 | for(;;) { |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 116 | unsigned int n = nbytes; |
| 117 | u8 *tmp = NULL; |
| 118 | |
| 119 | if (!scatterwalk_aligned(&walk_in, alignmask) || |
| 120 | !scatterwalk_aligned(&walk_out, alignmask)) { |
| 121 | if (!buffer) { |
| 122 | buffer = __get_free_page(GFP_ATOMIC); |
| 123 | if (!buffer) |
| 124 | n = 0; |
| 125 | } |
| 126 | tmp = (u8 *)buffer; |
| 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | scatterwalk_map(&walk_in, 0); |
| 130 | scatterwalk_map(&walk_out, 1); |
| 131 | |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 132 | n = scatterwalk_clamp(&walk_in, n); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 133 | n = scatterwalk_clamp(&walk_out, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 135 | if (likely(n >= bsize)) |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 136 | n = crypt_fast(desc, &walk_in, &walk_out, n, tmp); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 137 | else |
| 138 | n = crypt_slow(desc, &walk_in, &walk_out, bsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 140 | nbytes -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | scatterwalk_done(&walk_in, 0, nbytes); |
| 143 | scatterwalk_done(&walk_out, 1, nbytes); |
| 144 | |
| 145 | if (!nbytes) |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 146 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | crypto_yield(tfm); |
| 149 | } |
Herbert Xu | 9547737 | 2005-07-06 13:52:09 -0700 | [diff] [blame] | 150 | |
| 151 | if (buffer) |
| 152 | free_page(buffer); |
| 153 | |
| 154 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Herbert Xu | 915e856 | 2005-07-06 13:53:47 -0700 | [diff] [blame] | 157 | static int crypt_iv_unaligned(struct cipher_desc *desc, |
| 158 | struct scatterlist *dst, |
| 159 | struct scatterlist *src, |
| 160 | unsigned int nbytes) |
| 161 | { |
| 162 | struct crypto_tfm *tfm = desc->tfm; |
Herbert Xu | 9d853c3 | 2005-07-15 07:41:31 -0700 | [diff] [blame] | 163 | unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
Herbert Xu | 915e856 | 2005-07-06 13:53:47 -0700 | [diff] [blame] | 164 | u8 *iv = desc->info; |
| 165 | |
| 166 | if (unlikely(((unsigned long)iv & alignmask))) { |
| 167 | unsigned int ivsize = tfm->crt_cipher.cit_ivsize; |
| 168 | u8 buffer[ivsize + alignmask]; |
| 169 | u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 170 | int err; |
| 171 | |
| 172 | desc->info = memcpy(tmp, iv, ivsize); |
| 173 | err = crypt(desc, dst, src, nbytes); |
| 174 | memcpy(iv, tmp, ivsize); |
| 175 | |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | return crypt(desc, dst, src, nbytes); |
| 180 | } |
| 181 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 182 | static unsigned int cbc_process_encrypt(const struct cipher_desc *desc, |
| 183 | u8 *dst, const u8 *src, |
| 184 | unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 186 | struct crypto_tfm *tfm = desc->tfm; |
| 187 | void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block; |
| 188 | int bsize = crypto_tfm_alg_blocksize(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 190 | void (*fn)(void *, u8 *, const u8 *) = desc->crfn; |
| 191 | u8 *iv = desc->info; |
| 192 | unsigned int done = 0; |
| 193 | |
Herbert Xu | fe2d529 | 2005-09-06 14:49:44 -0700 | [diff] [blame] | 194 | nbytes -= bsize; |
| 195 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 196 | do { |
| 197 | xor(iv, src); |
| 198 | fn(crypto_tfm_ctx(tfm), dst, iv); |
| 199 | memcpy(iv, dst, bsize); |
| 200 | |
| 201 | src += bsize; |
| 202 | dst += bsize; |
Herbert Xu | fe2d529 | 2005-09-06 14:49:44 -0700 | [diff] [blame] | 203 | } while ((done += bsize) <= nbytes); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 204 | |
| 205 | return done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 208 | static unsigned int cbc_process_decrypt(const struct cipher_desc *desc, |
| 209 | u8 *dst, const u8 *src, |
| 210 | unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 212 | struct crypto_tfm *tfm = desc->tfm; |
| 213 | void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block; |
| 214 | int bsize = crypto_tfm_alg_blocksize(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 216 | u8 stack[src == dst ? bsize : 0]; |
| 217 | u8 *buf = stack; |
| 218 | u8 **dst_p = src == dst ? &buf : &dst; |
| 219 | |
| 220 | void (*fn)(void *, u8 *, const u8 *) = desc->crfn; |
| 221 | u8 *iv = desc->info; |
| 222 | unsigned int done = 0; |
| 223 | |
Herbert Xu | fe2d529 | 2005-09-06 14:49:44 -0700 | [diff] [blame] | 224 | nbytes -= bsize; |
| 225 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 226 | do { |
| 227 | u8 *tmp_dst = *dst_p; |
| 228 | |
| 229 | fn(crypto_tfm_ctx(tfm), tmp_dst, src); |
| 230 | xor(tmp_dst, iv); |
| 231 | memcpy(iv, src, bsize); |
| 232 | if (tmp_dst != dst) |
| 233 | memcpy(dst, tmp_dst, bsize); |
| 234 | |
| 235 | src += bsize; |
| 236 | dst += bsize; |
Herbert Xu | fe2d529 | 2005-09-06 14:49:44 -0700 | [diff] [blame] | 237 | } while ((done += bsize) <= nbytes); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 238 | |
| 239 | return done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 242 | static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst, |
| 243 | const u8 *src, unsigned int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 245 | struct crypto_tfm *tfm = desc->tfm; |
| 246 | int bsize = crypto_tfm_alg_blocksize(tfm); |
| 247 | void (*fn)(void *, u8 *, const u8 *) = desc->crfn; |
| 248 | unsigned int done = 0; |
| 249 | |
Herbert Xu | fe2d529 | 2005-09-06 14:49:44 -0700 | [diff] [blame] | 250 | nbytes -= bsize; |
| 251 | |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 252 | do { |
| 253 | fn(crypto_tfm_ctx(tfm), dst, src); |
| 254 | |
| 255 | src += bsize; |
| 256 | dst += bsize; |
Herbert Xu | fe2d529 | 2005-09-06 14:49:44 -0700 | [diff] [blame] | 257 | } while ((done += bsize) <= nbytes); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 258 | |
| 259 | return done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
| 263 | { |
| 264 | struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; |
| 265 | |
| 266 | if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) { |
| 267 | tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 268 | return -EINVAL; |
| 269 | } else |
| 270 | return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen, |
| 271 | &tfm->crt_flags); |
| 272 | } |
| 273 | |
| 274 | static int ecb_encrypt(struct crypto_tfm *tfm, |
| 275 | struct scatterlist *dst, |
| 276 | struct scatterlist *src, unsigned int nbytes) |
| 277 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 278 | struct cipher_desc desc; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 279 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 280 | |
| 281 | desc.tfm = tfm; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 282 | desc.crfn = cipher->cia_encrypt; |
| 283 | desc.prfn = cipher->cia_encrypt_ecb ?: ecb_process; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 284 | |
| 285 | return crypt(&desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static int ecb_decrypt(struct crypto_tfm *tfm, |
| 289 | struct scatterlist *dst, |
| 290 | struct scatterlist *src, |
| 291 | unsigned int nbytes) |
| 292 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 293 | struct cipher_desc desc; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 294 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 295 | |
| 296 | desc.tfm = tfm; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 297 | desc.crfn = cipher->cia_decrypt; |
| 298 | desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 299 | |
| 300 | return crypt(&desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int cbc_encrypt(struct crypto_tfm *tfm, |
| 304 | struct scatterlist *dst, |
| 305 | struct scatterlist *src, |
| 306 | unsigned int nbytes) |
| 307 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 308 | struct cipher_desc desc; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 309 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 310 | |
| 311 | desc.tfm = tfm; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 312 | desc.crfn = cipher->cia_encrypt; |
| 313 | desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 314 | desc.info = tfm->crt_cipher.cit_iv; |
| 315 | |
| 316 | return crypt(&desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static int cbc_encrypt_iv(struct crypto_tfm *tfm, |
| 320 | struct scatterlist *dst, |
| 321 | struct scatterlist *src, |
| 322 | unsigned int nbytes, u8 *iv) |
| 323 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 324 | struct cipher_desc desc; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 325 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 326 | |
| 327 | desc.tfm = tfm; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 328 | desc.crfn = cipher->cia_encrypt; |
| 329 | desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 330 | desc.info = iv; |
| 331 | |
Herbert Xu | 915e856 | 2005-07-06 13:53:47 -0700 | [diff] [blame] | 332 | return crypt_iv_unaligned(&desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int cbc_decrypt(struct crypto_tfm *tfm, |
| 336 | struct scatterlist *dst, |
| 337 | struct scatterlist *src, |
| 338 | unsigned int nbytes) |
| 339 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 340 | struct cipher_desc desc; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 341 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 342 | |
| 343 | desc.tfm = tfm; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 344 | desc.crfn = cipher->cia_decrypt; |
| 345 | desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 346 | desc.info = tfm->crt_cipher.cit_iv; |
| 347 | |
| 348 | return crypt(&desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int cbc_decrypt_iv(struct crypto_tfm *tfm, |
| 352 | struct scatterlist *dst, |
| 353 | struct scatterlist *src, |
| 354 | unsigned int nbytes, u8 *iv) |
| 355 | { |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 356 | struct cipher_desc desc; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 357 | struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 358 | |
| 359 | desc.tfm = tfm; |
Herbert Xu | 4072518 | 2005-07-06 13:51:52 -0700 | [diff] [blame] | 360 | desc.crfn = cipher->cia_decrypt; |
| 361 | desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt; |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 362 | desc.info = iv; |
| 363 | |
Herbert Xu | 915e856 | 2005-07-06 13:53:47 -0700 | [diff] [blame] | 364 | return crypt_iv_unaligned(&desc, dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static int nocrypt(struct crypto_tfm *tfm, |
| 368 | struct scatterlist *dst, |
| 369 | struct scatterlist *src, |
| 370 | unsigned int nbytes) |
| 371 | { |
| 372 | return -ENOSYS; |
| 373 | } |
| 374 | |
| 375 | static int nocrypt_iv(struct crypto_tfm *tfm, |
| 376 | struct scatterlist *dst, |
| 377 | struct scatterlist *src, |
| 378 | unsigned int nbytes, u8 *iv) |
| 379 | { |
| 380 | return -ENOSYS; |
| 381 | } |
| 382 | |
| 383 | int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags) |
| 384 | { |
| 385 | u32 mode = flags & CRYPTO_TFM_MODE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | int crypto_init_cipher_ops(struct crypto_tfm *tfm) |
| 391 | { |
| 392 | int ret = 0; |
| 393 | struct cipher_tfm *ops = &tfm->crt_cipher; |
| 394 | |
| 395 | ops->cit_setkey = setkey; |
| 396 | |
| 397 | switch (tfm->crt_cipher.cit_mode) { |
| 398 | case CRYPTO_TFM_MODE_ECB: |
| 399 | ops->cit_encrypt = ecb_encrypt; |
| 400 | ops->cit_decrypt = ecb_decrypt; |
| 401 | break; |
| 402 | |
| 403 | case CRYPTO_TFM_MODE_CBC: |
| 404 | ops->cit_encrypt = cbc_encrypt; |
| 405 | ops->cit_decrypt = cbc_decrypt; |
| 406 | ops->cit_encrypt_iv = cbc_encrypt_iv; |
| 407 | ops->cit_decrypt_iv = cbc_decrypt_iv; |
| 408 | break; |
| 409 | |
| 410 | case CRYPTO_TFM_MODE_CFB: |
| 411 | ops->cit_encrypt = nocrypt; |
| 412 | ops->cit_decrypt = nocrypt; |
| 413 | ops->cit_encrypt_iv = nocrypt_iv; |
| 414 | ops->cit_decrypt_iv = nocrypt_iv; |
| 415 | break; |
| 416 | |
| 417 | case CRYPTO_TFM_MODE_CTR: |
| 418 | ops->cit_encrypt = nocrypt; |
| 419 | ops->cit_decrypt = nocrypt; |
| 420 | ops->cit_encrypt_iv = nocrypt_iv; |
| 421 | ops->cit_decrypt_iv = nocrypt_iv; |
| 422 | break; |
| 423 | |
| 424 | default: |
| 425 | BUG(); |
| 426 | } |
| 427 | |
| 428 | if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) { |
Herbert Xu | 9d853c3 | 2005-07-15 07:41:31 -0700 | [diff] [blame] | 429 | unsigned long align; |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 430 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | switch (crypto_tfm_alg_blocksize(tfm)) { |
| 433 | case 8: |
| 434 | ops->cit_xor_block = xor_64; |
| 435 | break; |
| 436 | |
| 437 | case 16: |
| 438 | ops->cit_xor_block = xor_128; |
| 439 | break; |
| 440 | |
| 441 | default: |
| 442 | printk(KERN_WARNING "%s: block size %u not supported\n", |
| 443 | crypto_tfm_alg_name(tfm), |
| 444 | crypto_tfm_alg_blocksize(tfm)); |
| 445 | ret = -EINVAL; |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm); |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 450 | align = crypto_tfm_alg_alignmask(tfm) + 1; |
| 451 | addr = (unsigned long)crypto_tfm_ctx(tfm); |
| 452 | addr = ALIGN(addr, align); |
| 453 | addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align); |
| 454 | ops->cit_iv = (void *)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | out: |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | void crypto_exit_cipher_ops(struct crypto_tfm *tfm) |
| 462 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |