ia64/linux-2.6.18-xen.hg

annotate crypto/tea.c @ 854:950b9eb27661

usbback: fix urb interval value for interrupt urbs.

Signed-off-by: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 06 13:51:20 2009 +0100 (2009-04-06)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * Cryptographic API.
ian@0 3 *
ian@0 4 * TEA, XTEA, and XETA crypto alogrithms
ian@0 5 *
ian@0 6 * The TEA and Xtended TEA algorithms were developed by David Wheeler
ian@0 7 * and Roger Needham at the Computer Laboratory of Cambridge University.
ian@0 8 *
ian@0 9 * Due to the order of evaluation in XTEA many people have incorrectly
ian@0 10 * implemented it. XETA (XTEA in the wrong order), exists for
ian@0 11 * compatibility with these implementations.
ian@0 12 *
ian@0 13 * Copyright (c) 2004 Aaron Grothe ajgrothe@yahoo.com
ian@0 14 *
ian@0 15 * This program is free software; you can redistribute it and/or modify
ian@0 16 * it under the terms of the GNU General Public License as published by
ian@0 17 * the Free Software Foundation; either version 2 of the License, or
ian@0 18 * (at your option) any later version.
ian@0 19 *
ian@0 20 */
ian@0 21
ian@0 22 #include <linux/init.h>
ian@0 23 #include <linux/module.h>
ian@0 24 #include <linux/mm.h>
ian@0 25 #include <asm/byteorder.h>
ian@0 26 #include <asm/scatterlist.h>
ian@0 27 #include <linux/crypto.h>
ian@0 28 #include <linux/types.h>
ian@0 29
ian@0 30 #define TEA_KEY_SIZE 16
ian@0 31 #define TEA_BLOCK_SIZE 8
ian@0 32 #define TEA_ROUNDS 32
ian@0 33 #define TEA_DELTA 0x9e3779b9
ian@0 34
ian@0 35 #define XTEA_KEY_SIZE 16
ian@0 36 #define XTEA_BLOCK_SIZE 8
ian@0 37 #define XTEA_ROUNDS 32
ian@0 38 #define XTEA_DELTA 0x9e3779b9
ian@0 39
ian@0 40 struct tea_ctx {
ian@0 41 u32 KEY[4];
ian@0 42 };
ian@0 43
ian@0 44 struct xtea_ctx {
ian@0 45 u32 KEY[4];
ian@0 46 };
ian@0 47
ian@0 48 static int tea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
ian@0 49 unsigned int key_len, u32 *flags)
ian@0 50 {
ian@0 51 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 52 const __le32 *key = (const __le32 *)in_key;
ian@0 53
ian@0 54 if (key_len != 16)
ian@0 55 {
ian@0 56 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
ian@0 57 return -EINVAL;
ian@0 58 }
ian@0 59
ian@0 60 ctx->KEY[0] = le32_to_cpu(key[0]);
ian@0 61 ctx->KEY[1] = le32_to_cpu(key[1]);
ian@0 62 ctx->KEY[2] = le32_to_cpu(key[2]);
ian@0 63 ctx->KEY[3] = le32_to_cpu(key[3]);
ian@0 64
ian@0 65 return 0;
ian@0 66
ian@0 67 }
ian@0 68
ian@0 69 static void tea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ian@0 70 {
ian@0 71 u32 y, z, n, sum = 0;
ian@0 72 u32 k0, k1, k2, k3;
ian@0 73 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 74 const __le32 *in = (const __le32 *)src;
ian@0 75 __le32 *out = (__le32 *)dst;
ian@0 76
ian@0 77 y = le32_to_cpu(in[0]);
ian@0 78 z = le32_to_cpu(in[1]);
ian@0 79
ian@0 80 k0 = ctx->KEY[0];
ian@0 81 k1 = ctx->KEY[1];
ian@0 82 k2 = ctx->KEY[2];
ian@0 83 k3 = ctx->KEY[3];
ian@0 84
ian@0 85 n = TEA_ROUNDS;
ian@0 86
ian@0 87 while (n-- > 0) {
ian@0 88 sum += TEA_DELTA;
ian@0 89 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
ian@0 90 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
ian@0 91 }
ian@0 92
ian@0 93 out[0] = cpu_to_le32(y);
ian@0 94 out[1] = cpu_to_le32(z);
ian@0 95 }
ian@0 96
ian@0 97 static void tea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ian@0 98 {
ian@0 99 u32 y, z, n, sum;
ian@0 100 u32 k0, k1, k2, k3;
ian@0 101 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 102 const __le32 *in = (const __le32 *)src;
ian@0 103 __le32 *out = (__le32 *)dst;
ian@0 104
ian@0 105 y = le32_to_cpu(in[0]);
ian@0 106 z = le32_to_cpu(in[1]);
ian@0 107
ian@0 108 k0 = ctx->KEY[0];
ian@0 109 k1 = ctx->KEY[1];
ian@0 110 k2 = ctx->KEY[2];
ian@0 111 k3 = ctx->KEY[3];
ian@0 112
ian@0 113 sum = TEA_DELTA << 5;
ian@0 114
ian@0 115 n = TEA_ROUNDS;
ian@0 116
ian@0 117 while (n-- > 0) {
ian@0 118 z -= ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3);
ian@0 119 y -= ((z << 4) + k0) ^ (z + sum) ^ ((z >> 5) + k1);
ian@0 120 sum -= TEA_DELTA;
ian@0 121 }
ian@0 122
ian@0 123 out[0] = cpu_to_le32(y);
ian@0 124 out[1] = cpu_to_le32(z);
ian@0 125 }
ian@0 126
ian@0 127 static int xtea_setkey(struct crypto_tfm *tfm, const u8 *in_key,
ian@0 128 unsigned int key_len, u32 *flags)
ian@0 129 {
ian@0 130 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 131 const __le32 *key = (const __le32 *)in_key;
ian@0 132
ian@0 133 if (key_len != 16)
ian@0 134 {
ian@0 135 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
ian@0 136 return -EINVAL;
ian@0 137 }
ian@0 138
ian@0 139 ctx->KEY[0] = le32_to_cpu(key[0]);
ian@0 140 ctx->KEY[1] = le32_to_cpu(key[1]);
ian@0 141 ctx->KEY[2] = le32_to_cpu(key[2]);
ian@0 142 ctx->KEY[3] = le32_to_cpu(key[3]);
ian@0 143
ian@0 144 return 0;
ian@0 145
ian@0 146 }
ian@0 147
ian@0 148 static void xtea_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ian@0 149 {
ian@0 150 u32 y, z, sum = 0;
ian@0 151 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
ian@0 152 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 153 const __le32 *in = (const __le32 *)src;
ian@0 154 __le32 *out = (__le32 *)dst;
ian@0 155
ian@0 156 y = le32_to_cpu(in[0]);
ian@0 157 z = le32_to_cpu(in[1]);
ian@0 158
ian@0 159 while (sum != limit) {
ian@0 160 y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]);
ian@0 161 sum += XTEA_DELTA;
ian@0 162 z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]);
ian@0 163 }
ian@0 164
ian@0 165 out[0] = cpu_to_le32(y);
ian@0 166 out[1] = cpu_to_le32(z);
ian@0 167 }
ian@0 168
ian@0 169 static void xtea_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ian@0 170 {
ian@0 171 u32 y, z, sum;
ian@0 172 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 173 const __le32 *in = (const __le32 *)src;
ian@0 174 __le32 *out = (__le32 *)dst;
ian@0 175
ian@0 176 y = le32_to_cpu(in[0]);
ian@0 177 z = le32_to_cpu(in[1]);
ian@0 178
ian@0 179 sum = XTEA_DELTA * XTEA_ROUNDS;
ian@0 180
ian@0 181 while (sum) {
ian@0 182 z -= ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 & 3]);
ian@0 183 sum -= XTEA_DELTA;
ian@0 184 y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]);
ian@0 185 }
ian@0 186
ian@0 187 out[0] = cpu_to_le32(y);
ian@0 188 out[1] = cpu_to_le32(z);
ian@0 189 }
ian@0 190
ian@0 191
ian@0 192 static void xeta_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ian@0 193 {
ian@0 194 u32 y, z, sum = 0;
ian@0 195 u32 limit = XTEA_DELTA * XTEA_ROUNDS;
ian@0 196 struct xtea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 197 const __le32 *in = (const __le32 *)src;
ian@0 198 __le32 *out = (__le32 *)dst;
ian@0 199
ian@0 200 y = le32_to_cpu(in[0]);
ian@0 201 z = le32_to_cpu(in[1]);
ian@0 202
ian@0 203 while (sum != limit) {
ian@0 204 y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3];
ian@0 205 sum += XTEA_DELTA;
ian@0 206 z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3];
ian@0 207 }
ian@0 208
ian@0 209 out[0] = cpu_to_le32(y);
ian@0 210 out[1] = cpu_to_le32(z);
ian@0 211 }
ian@0 212
ian@0 213 static void xeta_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
ian@0 214 {
ian@0 215 u32 y, z, sum;
ian@0 216 struct tea_ctx *ctx = crypto_tfm_ctx(tfm);
ian@0 217 const __le32 *in = (const __le32 *)src;
ian@0 218 __le32 *out = (__le32 *)dst;
ian@0 219
ian@0 220 y = le32_to_cpu(in[0]);
ian@0 221 z = le32_to_cpu(in[1]);
ian@0 222
ian@0 223 sum = XTEA_DELTA * XTEA_ROUNDS;
ian@0 224
ian@0 225 while (sum) {
ian@0 226 z -= (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 & 3];
ian@0 227 sum -= XTEA_DELTA;
ian@0 228 y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3];
ian@0 229 }
ian@0 230
ian@0 231 out[0] = cpu_to_le32(y);
ian@0 232 out[1] = cpu_to_le32(z);
ian@0 233 }
ian@0 234
ian@0 235 static struct crypto_alg tea_alg = {
ian@0 236 .cra_name = "tea",
ian@0 237 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
ian@0 238 .cra_blocksize = TEA_BLOCK_SIZE,
ian@0 239 .cra_ctxsize = sizeof (struct tea_ctx),
ian@0 240 .cra_alignmask = 3,
ian@0 241 .cra_module = THIS_MODULE,
ian@0 242 .cra_list = LIST_HEAD_INIT(tea_alg.cra_list),
ian@0 243 .cra_u = { .cipher = {
ian@0 244 .cia_min_keysize = TEA_KEY_SIZE,
ian@0 245 .cia_max_keysize = TEA_KEY_SIZE,
ian@0 246 .cia_setkey = tea_setkey,
ian@0 247 .cia_encrypt = tea_encrypt,
ian@0 248 .cia_decrypt = tea_decrypt } }
ian@0 249 };
ian@0 250
ian@0 251 static struct crypto_alg xtea_alg = {
ian@0 252 .cra_name = "xtea",
ian@0 253 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
ian@0 254 .cra_blocksize = XTEA_BLOCK_SIZE,
ian@0 255 .cra_ctxsize = sizeof (struct xtea_ctx),
ian@0 256 .cra_alignmask = 3,
ian@0 257 .cra_module = THIS_MODULE,
ian@0 258 .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
ian@0 259 .cra_u = { .cipher = {
ian@0 260 .cia_min_keysize = XTEA_KEY_SIZE,
ian@0 261 .cia_max_keysize = XTEA_KEY_SIZE,
ian@0 262 .cia_setkey = xtea_setkey,
ian@0 263 .cia_encrypt = xtea_encrypt,
ian@0 264 .cia_decrypt = xtea_decrypt } }
ian@0 265 };
ian@0 266
ian@0 267 static struct crypto_alg xeta_alg = {
ian@0 268 .cra_name = "xeta",
ian@0 269 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
ian@0 270 .cra_blocksize = XTEA_BLOCK_SIZE,
ian@0 271 .cra_ctxsize = sizeof (struct xtea_ctx),
ian@0 272 .cra_alignmask = 3,
ian@0 273 .cra_module = THIS_MODULE,
ian@0 274 .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list),
ian@0 275 .cra_u = { .cipher = {
ian@0 276 .cia_min_keysize = XTEA_KEY_SIZE,
ian@0 277 .cia_max_keysize = XTEA_KEY_SIZE,
ian@0 278 .cia_setkey = xtea_setkey,
ian@0 279 .cia_encrypt = xeta_encrypt,
ian@0 280 .cia_decrypt = xeta_decrypt } }
ian@0 281 };
ian@0 282
ian@0 283 static int __init init(void)
ian@0 284 {
ian@0 285 int ret = 0;
ian@0 286
ian@0 287 ret = crypto_register_alg(&tea_alg);
ian@0 288 if (ret < 0)
ian@0 289 goto out;
ian@0 290
ian@0 291 ret = crypto_register_alg(&xtea_alg);
ian@0 292 if (ret < 0) {
ian@0 293 crypto_unregister_alg(&tea_alg);
ian@0 294 goto out;
ian@0 295 }
ian@0 296
ian@0 297 ret = crypto_register_alg(&xeta_alg);
ian@0 298 if (ret < 0) {
ian@0 299 crypto_unregister_alg(&tea_alg);
ian@0 300 crypto_unregister_alg(&xtea_alg);
ian@0 301 goto out;
ian@0 302 }
ian@0 303
ian@0 304 out:
ian@0 305 return ret;
ian@0 306 }
ian@0 307
ian@0 308 static void __exit fini(void)
ian@0 309 {
ian@0 310 crypto_unregister_alg(&tea_alg);
ian@0 311 crypto_unregister_alg(&xtea_alg);
ian@0 312 crypto_unregister_alg(&xeta_alg);
ian@0 313 }
ian@0 314
ian@0 315 MODULE_ALIAS("xtea");
ian@0 316 MODULE_ALIAS("xeta");
ian@0 317
ian@0 318 module_init(init);
ian@0 319 module_exit(fini);
ian@0 320
ian@0 321 MODULE_LICENSE("GPL");
ian@0 322 MODULE_DESCRIPTION("TEA, XTEA & XETA Cryptographic Algorithms");