Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2006 USAGI/WIDE Project |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: |
| 19 | * Kazunori Miyazawa <miyazawa@linux-ipv6.org> |
| 20 | */ |
| 21 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 22 | #include <crypto/internal/hash.h> |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 23 | #include <linux/err.h> |
| 24 | #include <linux/kernel.h> |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 25 | |
Adrian Bunk | 5b37538 | 2006-11-17 13:43:04 +1100 | [diff] [blame] | 26 | static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101, |
| 27 | 0x02020202, 0x02020202, 0x02020202, 0x02020202, |
| 28 | 0x03030303, 0x03030303, 0x03030303, 0x03030303}; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 29 | /* |
| 30 | * +------------------------ |
| 31 | * | <parent tfm> |
| 32 | * +------------------------ |
| 33 | * | crypto_xcbc_ctx |
| 34 | * +------------------------ |
| 35 | * | odds (block size) |
| 36 | * +------------------------ |
| 37 | * | prev (block size) |
| 38 | * +------------------------ |
| 39 | * | key (block size) |
| 40 | * +------------------------ |
| 41 | * | consts (block size * 3) |
| 42 | * +------------------------ |
| 43 | */ |
| 44 | struct crypto_xcbc_ctx { |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 45 | struct crypto_cipher *child; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 46 | u8 *odds; |
| 47 | u8 *prev; |
| 48 | u8 *key; |
| 49 | u8 *consts; |
| 50 | void (*xor)(u8 *a, const u8 *b, unsigned int bs); |
| 51 | unsigned int keylen; |
| 52 | unsigned int len; |
| 53 | }; |
| 54 | |
| 55 | static void xor_128(u8 *a, const u8 *b, unsigned int bs) |
| 56 | { |
| 57 | ((u32 *)a)[0] ^= ((u32 *)b)[0]; |
| 58 | ((u32 *)a)[1] ^= ((u32 *)b)[1]; |
| 59 | ((u32 *)a)[2] ^= ((u32 *)b)[2]; |
| 60 | ((u32 *)a)[3] ^= ((u32 *)b)[3]; |
| 61 | } |
| 62 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 63 | static int _crypto_xcbc_digest_setkey(struct crypto_shash *parent, |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 64 | struct crypto_xcbc_ctx *ctx) |
| 65 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 66 | int bs = crypto_shash_blocksize(parent); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 67 | int err = 0; |
| 68 | u8 key1[bs]; |
| 69 | |
| 70 | if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen))) |
| 71 | return err; |
| 72 | |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 73 | crypto_cipher_encrypt_one(ctx->child, key1, ctx->consts); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 74 | |
| 75 | return crypto_cipher_setkey(ctx->child, key1, bs); |
| 76 | } |
| 77 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 78 | static int crypto_xcbc_digest_setkey(struct crypto_shash *parent, |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 79 | const u8 *inkey, unsigned int keylen) |
| 80 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 81 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 82 | |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 83 | if (keylen != crypto_cipher_blocksize(ctx->child)) |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 84 | return -EINVAL; |
| 85 | |
| 86 | ctx->keylen = keylen; |
| 87 | memcpy(ctx->key, inkey, keylen); |
| 88 | ctx->consts = (u8*)ks; |
| 89 | |
| 90 | return _crypto_xcbc_digest_setkey(parent, ctx); |
| 91 | } |
| 92 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 93 | static int crypto_xcbc_digest_init(struct shash_desc *pdesc) |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 94 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 95 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(pdesc->tfm); |
| 96 | int bs = crypto_shash_blocksize(pdesc->tfm); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 97 | |
| 98 | ctx->len = 0; |
| 99 | memset(ctx->odds, 0, bs); |
| 100 | memset(ctx->prev, 0, bs); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 105 | static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p, |
| 106 | unsigned int len) |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 107 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 108 | struct crypto_shash *parent = pdesc->tfm; |
| 109 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent); |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 110 | struct crypto_cipher *tfm = ctx->child; |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 111 | int bs = crypto_shash_blocksize(parent); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 112 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 113 | /* checking the data can fill the block */ |
| 114 | if ((ctx->len + len) <= bs) { |
| 115 | memcpy(ctx->odds + ctx->len, p, len); |
| 116 | ctx->len += len; |
| 117 | return 0; |
| 118 | } |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 119 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 120 | /* filling odds with new data and encrypting it */ |
| 121 | memcpy(ctx->odds + ctx->len, p, bs - ctx->len); |
| 122 | len -= bs - ctx->len; |
| 123 | p += bs - ctx->len; |
Joy Latten | 2f40a17 | 2008-03-06 19:28:44 +0800 | [diff] [blame] | 124 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 125 | ctx->xor(ctx->prev, ctx->odds, bs); |
| 126 | crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev); |
Joy Latten | 2f40a17 | 2008-03-06 19:28:44 +0800 | [diff] [blame] | 127 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 128 | /* clearing the length */ |
| 129 | ctx->len = 0; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 130 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 131 | /* encrypting the rest of data */ |
| 132 | while (len > bs) { |
| 133 | ctx->xor(ctx->prev, p, bs); |
| 134 | crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev); |
| 135 | p += bs; |
| 136 | len -= bs; |
| 137 | } |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 138 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 139 | /* keeping the surplus of blocksize */ |
| 140 | if (len) { |
| 141 | memcpy(ctx->odds, p, len); |
| 142 | ctx->len = len; |
Joy Latten | 1edcf2e | 2008-04-02 14:36:09 +0800 | [diff] [blame] | 143 | } |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 148 | static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out) |
Herbert Xu | fb46984 | 2006-12-10 10:45:28 +1100 | [diff] [blame] | 149 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 150 | struct crypto_shash *parent = pdesc->tfm; |
| 151 | struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent); |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 152 | struct crypto_cipher *tfm = ctx->child; |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 153 | int bs = crypto_shash_blocksize(parent); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 154 | int err = 0; |
| 155 | |
| 156 | if (ctx->len == bs) { |
| 157 | u8 key2[bs]; |
| 158 | |
| 159 | if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0) |
| 160 | return err; |
| 161 | |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 162 | crypto_cipher_encrypt_one(tfm, key2, |
| 163 | (u8 *)(ctx->consts + bs)); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 164 | |
| 165 | ctx->xor(ctx->prev, ctx->odds, bs); |
| 166 | ctx->xor(ctx->prev, key2, bs); |
| 167 | _crypto_xcbc_digest_setkey(parent, ctx); |
| 168 | |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 169 | crypto_cipher_encrypt_one(tfm, out, ctx->prev); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 170 | } else { |
| 171 | u8 key3[bs]; |
| 172 | unsigned int rlen; |
| 173 | u8 *p = ctx->odds + ctx->len; |
| 174 | *p = 0x80; |
| 175 | p++; |
| 176 | |
| 177 | rlen = bs - ctx->len -1; |
| 178 | if (rlen) |
| 179 | memset(p, 0, rlen); |
| 180 | |
| 181 | if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0) |
| 182 | return err; |
| 183 | |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 184 | crypto_cipher_encrypt_one(tfm, key3, |
| 185 | (u8 *)(ctx->consts + bs * 2)); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 186 | |
| 187 | ctx->xor(ctx->prev, ctx->odds, bs); |
| 188 | ctx->xor(ctx->prev, key3, bs); |
| 189 | |
| 190 | _crypto_xcbc_digest_setkey(parent, ctx); |
| 191 | |
Herbert Xu | 6b701dd | 2006-12-24 09:59:42 +1100 | [diff] [blame] | 192 | crypto_cipher_encrypt_one(tfm, out, ctx->prev); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 198 | static int xcbc_init_tfm(struct crypto_tfm *tfm) |
| 199 | { |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 200 | struct crypto_cipher *cipher; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 201 | struct crypto_instance *inst = (void *)tfm->__crt_alg; |
| 202 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 203 | struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 204 | int bs = crypto_tfm_alg_blocksize(tfm); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 205 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 206 | cipher = crypto_spawn_cipher(spawn); |
| 207 | if (IS_ERR(cipher)) |
| 208 | return PTR_ERR(cipher); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 209 | |
| 210 | switch(bs) { |
| 211 | case 16: |
| 212 | ctx->xor = xor_128; |
| 213 | break; |
| 214 | default: |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 218 | ctx->child = cipher; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 219 | ctx->odds = (u8*)(ctx+1); |
| 220 | ctx->prev = ctx->odds + bs; |
| 221 | ctx->key = ctx->prev + bs; |
| 222 | |
| 223 | return 0; |
| 224 | }; |
| 225 | |
| 226 | static void xcbc_exit_tfm(struct crypto_tfm *tfm) |
| 227 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 228 | struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 229 | crypto_free_cipher(ctx->child); |
| 230 | } |
| 231 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 232 | static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 233 | { |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 234 | struct shash_instance *inst; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 235 | struct crypto_alg *alg; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 236 | int err; |
| 237 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 238 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 239 | if (err) |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 240 | return err; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 241 | |
| 242 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, |
| 243 | CRYPTO_ALG_TYPE_MASK); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 244 | if (IS_ERR(alg)) |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 245 | return PTR_ERR(alg); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 246 | |
| 247 | switch(alg->cra_blocksize) { |
| 248 | case 16: |
| 249 | break; |
| 250 | default: |
Herbert Xu | 1b87887 | 2008-01-01 15:44:50 +1100 | [diff] [blame] | 251 | goto out_put_alg; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 252 | } |
| 253 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 254 | inst = shash_alloc_instance("xcbc", alg); |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 255 | if (IS_ERR(inst)) |
| 256 | goto out_put_alg; |
| 257 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 258 | err = crypto_init_spawn(shash_instance_ctx(inst), alg, |
| 259 | shash_crypto_instance(inst), |
| 260 | CRYPTO_ALG_TYPE_MASK); |
| 261 | if (err) |
| 262 | goto out_free_inst; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 263 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 264 | inst->alg.base.cra_priority = alg->cra_priority; |
| 265 | inst->alg.base.cra_blocksize = alg->cra_blocksize; |
| 266 | inst->alg.base.cra_alignmask = alg->cra_alignmask; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 267 | |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 268 | inst->alg.digestsize = alg->cra_blocksize; |
| 269 | inst->alg.base.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) + |
| 270 | ALIGN(alg->cra_blocksize * 3, |
| 271 | sizeof(void *)); |
| 272 | inst->alg.base.cra_init = xcbc_init_tfm; |
| 273 | inst->alg.base.cra_exit = xcbc_exit_tfm; |
| 274 | |
| 275 | inst->alg.init = crypto_xcbc_digest_init; |
| 276 | inst->alg.update = crypto_xcbc_digest_update; |
| 277 | inst->alg.final = crypto_xcbc_digest_final; |
| 278 | inst->alg.setkey = crypto_xcbc_digest_setkey; |
| 279 | |
| 280 | err = shash_register_instance(tmpl, inst); |
| 281 | if (err) { |
| 282 | out_free_inst: |
| 283 | shash_free_instance(shash_crypto_instance(inst)); |
| 284 | } |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 285 | |
| 286 | out_put_alg: |
| 287 | crypto_mod_put(alg); |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 288 | return err; |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static struct crypto_template crypto_xcbc_tmpl = { |
| 292 | .name = "xcbc", |
Herbert Xu | 3106caa | 2009-07-12 12:48:32 +0800 | [diff] [blame^] | 293 | .create = xcbc_create, |
| 294 | .free = shash_free_instance, |
Kazunori MIYAZAWA | 333b0d7 | 2006-10-28 13:15:24 +1000 | [diff] [blame] | 295 | .module = THIS_MODULE, |
| 296 | }; |
| 297 | |
| 298 | static int __init crypto_xcbc_module_init(void) |
| 299 | { |
| 300 | return crypto_register_template(&crypto_xcbc_tmpl); |
| 301 | } |
| 302 | |
| 303 | static void __exit crypto_xcbc_module_exit(void) |
| 304 | { |
| 305 | crypto_unregister_template(&crypto_xcbc_tmpl); |
| 306 | } |
| 307 | |
| 308 | module_init(crypto_xcbc_module_init); |
| 309 | module_exit(crypto_xcbc_module_exit); |
| 310 | |
| 311 | MODULE_LICENSE("GPL"); |
| 312 | MODULE_DESCRIPTION("XCBC keyed hash algorithm"); |