diff options
Diffstat (limited to 'crypto/tea.c')
| -rw-r--r-- | crypto/tea.c | 98 |
1 files changed, 51 insertions, 47 deletions
diff --git a/crypto/tea.c b/crypto/tea.c index 5924efdd3a16..a6a02b30e470 100644 --- a/crypto/tea.c +++ b/crypto/tea.c | |||
| @@ -22,8 +22,10 @@ | |||
| 22 | #include <linux/init.h> | 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> | 23 | #include <linux/module.h> |
| 24 | #include <linux/mm.h> | 24 | #include <linux/mm.h> |
| 25 | #include <asm/byteorder.h> | ||
| 25 | #include <asm/scatterlist.h> | 26 | #include <asm/scatterlist.h> |
| 26 | #include <linux/crypto.h> | 27 | #include <linux/crypto.h> |
| 28 | #include <linux/types.h> | ||
| 27 | 29 | ||
| 28 | #define TEA_KEY_SIZE 16 | 30 | #define TEA_KEY_SIZE 16 |
| 29 | #define TEA_BLOCK_SIZE 8 | 31 | #define TEA_BLOCK_SIZE 8 |
| @@ -35,9 +37,6 @@ | |||
| 35 | #define XTEA_ROUNDS 32 | 37 | #define XTEA_ROUNDS 32 |
| 36 | #define XTEA_DELTA 0x9e3779b9 | 38 | #define XTEA_DELTA 0x9e3779b9 |
| 37 | 39 | ||
| 38 | #define u32_in(x) le32_to_cpu(*(const __le32 *)(x)) | ||
| 39 | #define u32_out(to, from) (*(__le32 *)(to) = cpu_to_le32(from)) | ||
| 40 | |||
| 41 | struct tea_ctx { | 40 | struct tea_ctx { |
| 42 | u32 KEY[4]; | 41 | u32 KEY[4]; |
| 43 | }; | 42 | }; |
| @@ -49,8 +48,8 @@ struct xtea_ctx { | |||
| 49 | static int tea_setkey(void *ctx_arg, const u8 *in_key, | 48 | static int tea_setkey(void *ctx_arg, const u8 *in_key, |
| 50 | unsigned int key_len, u32 *flags) | 49 | unsigned int key_len, u32 *flags) |
| 51 | { | 50 | { |
| 52 | |||
| 53 | struct tea_ctx *ctx = ctx_arg; | 51 | struct tea_ctx *ctx = ctx_arg; |
| 52 | const __le32 *key = (const __le32 *)in_key; | ||
| 54 | 53 | ||
| 55 | if (key_len != 16) | 54 | if (key_len != 16) |
| 56 | { | 55 | { |
| @@ -58,10 +57,10 @@ static int tea_setkey(void *ctx_arg, const u8 *in_key, | |||
| 58 | return -EINVAL; | 57 | return -EINVAL; |
| 59 | } | 58 | } |
| 60 | 59 | ||
| 61 | ctx->KEY[0] = u32_in (in_key); | 60 | ctx->KEY[0] = le32_to_cpu(key[0]); |
| 62 | ctx->KEY[1] = u32_in (in_key + 4); | 61 | ctx->KEY[1] = le32_to_cpu(key[1]); |
| 63 | ctx->KEY[2] = u32_in (in_key + 8); | 62 | ctx->KEY[2] = le32_to_cpu(key[2]); |
| 64 | ctx->KEY[3] = u32_in (in_key + 12); | 63 | ctx->KEY[3] = le32_to_cpu(key[3]); |
| 65 | 64 | ||
| 66 | return 0; | 65 | return 0; |
| 67 | 66 | ||
| @@ -73,9 +72,11 @@ static void tea_encrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 73 | u32 k0, k1, k2, k3; | 72 | u32 k0, k1, k2, k3; |
| 74 | 73 | ||
| 75 | struct tea_ctx *ctx = ctx_arg; | 74 | struct tea_ctx *ctx = ctx_arg; |
| 75 | const __le32 *in = (const __le32 *)src; | ||
| 76 | __le32 *out = (__le32 *)dst; | ||
| 76 | 77 | ||
| 77 | y = u32_in (src); | 78 | y = le32_to_cpu(in[0]); |
| 78 | z = u32_in (src + 4); | 79 | z = le32_to_cpu(in[1]); |
| 79 | 80 | ||
| 80 | k0 = ctx->KEY[0]; | 81 | k0 = ctx->KEY[0]; |
| 81 | k1 = ctx->KEY[1]; | 82 | k1 = ctx->KEY[1]; |
| @@ -90,19 +91,20 @@ static void tea_encrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 90 | z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3); | 91 | z += ((y << 4) + k2) ^ (y + sum) ^ ((y >> 5) + k3); |
| 91 | } | 92 | } |
| 92 | 93 | ||
| 93 | u32_out (dst, y); | 94 | out[0] = cpu_to_le32(y); |
| 94 | u32_out (dst + 4, z); | 95 | out[1] = cpu_to_le32(z); |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | static void tea_decrypt(void *ctx_arg, u8 *dst, const u8 *src) | 98 | static void tea_decrypt(void *ctx_arg, u8 *dst, const u8 *src) |
| 98 | { | 99 | { |
| 99 | u32 y, z, n, sum; | 100 | u32 y, z, n, sum; |
| 100 | u32 k0, k1, k2, k3; | 101 | u32 k0, k1, k2, k3; |
| 101 | |||
| 102 | struct tea_ctx *ctx = ctx_arg; | 102 | struct tea_ctx *ctx = ctx_arg; |
| 103 | const __le32 *in = (const __le32 *)src; | ||
| 104 | __le32 *out = (__le32 *)dst; | ||
| 103 | 105 | ||
| 104 | y = u32_in (src); | 106 | y = le32_to_cpu(in[0]); |
| 105 | z = u32_in (src + 4); | 107 | z = le32_to_cpu(in[1]); |
| 106 | 108 | ||
| 107 | k0 = ctx->KEY[0]; | 109 | k0 = ctx->KEY[0]; |
| 108 | k1 = ctx->KEY[1]; | 110 | k1 = ctx->KEY[1]; |
| @@ -119,16 +121,15 @@ static void tea_decrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 119 | sum -= TEA_DELTA; | 121 | sum -= TEA_DELTA; |
| 120 | } | 122 | } |
| 121 | 123 | ||
| 122 | u32_out (dst, y); | 124 | out[0] = cpu_to_le32(y); |
| 123 | u32_out (dst + 4, z); | 125 | out[1] = cpu_to_le32(z); |
| 124 | |||
| 125 | } | 126 | } |
| 126 | 127 | ||
| 127 | static int xtea_setkey(void *ctx_arg, const u8 *in_key, | 128 | static int xtea_setkey(void *ctx_arg, const u8 *in_key, |
| 128 | unsigned int key_len, u32 *flags) | 129 | unsigned int key_len, u32 *flags) |
| 129 | { | 130 | { |
| 130 | |||
| 131 | struct xtea_ctx *ctx = ctx_arg; | 131 | struct xtea_ctx *ctx = ctx_arg; |
| 132 | const __le32 *key = (const __le32 *)in_key; | ||
| 132 | 133 | ||
| 133 | if (key_len != 16) | 134 | if (key_len != 16) |
| 134 | { | 135 | { |
| @@ -136,10 +137,10 @@ static int xtea_setkey(void *ctx_arg, const u8 *in_key, | |||
| 136 | return -EINVAL; | 137 | return -EINVAL; |
| 137 | } | 138 | } |
| 138 | 139 | ||
| 139 | ctx->KEY[0] = u32_in (in_key); | 140 | ctx->KEY[0] = le32_to_cpu(key[0]); |
| 140 | ctx->KEY[1] = u32_in (in_key + 4); | 141 | ctx->KEY[1] = le32_to_cpu(key[1]); |
| 141 | ctx->KEY[2] = u32_in (in_key + 8); | 142 | ctx->KEY[2] = le32_to_cpu(key[2]); |
| 142 | ctx->KEY[3] = u32_in (in_key + 12); | 143 | ctx->KEY[3] = le32_to_cpu(key[3]); |
| 143 | 144 | ||
| 144 | return 0; | 145 | return 0; |
| 145 | 146 | ||
| @@ -147,14 +148,15 @@ static int xtea_setkey(void *ctx_arg, const u8 *in_key, | |||
| 147 | 148 | ||
| 148 | static void xtea_encrypt(void *ctx_arg, u8 *dst, const u8 *src) | 149 | static void xtea_encrypt(void *ctx_arg, u8 *dst, const u8 *src) |
| 149 | { | 150 | { |
| 150 | |||
| 151 | u32 y, z, sum = 0; | 151 | u32 y, z, sum = 0; |
| 152 | u32 limit = XTEA_DELTA * XTEA_ROUNDS; | 152 | u32 limit = XTEA_DELTA * XTEA_ROUNDS; |
| 153 | 153 | ||
| 154 | struct xtea_ctx *ctx = ctx_arg; | 154 | struct xtea_ctx *ctx = ctx_arg; |
| 155 | const __le32 *in = (const __le32 *)src; | ||
| 156 | __le32 *out = (__le32 *)dst; | ||
| 155 | 157 | ||
| 156 | y = u32_in (src); | 158 | y = le32_to_cpu(in[0]); |
| 157 | z = u32_in (src + 4); | 159 | z = le32_to_cpu(in[1]); |
| 158 | 160 | ||
| 159 | while (sum != limit) { | 161 | while (sum != limit) { |
| 160 | y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); | 162 | y += ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum&3]); |
| @@ -162,19 +164,19 @@ static void xtea_encrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 162 | z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]); | 164 | z += ((y << 4 ^ y >> 5) + y) ^ (sum + ctx->KEY[sum>>11 &3]); |
| 163 | } | 165 | } |
| 164 | 166 | ||
| 165 | u32_out (dst, y); | 167 | out[0] = cpu_to_le32(y); |
| 166 | u32_out (dst + 4, z); | 168 | out[1] = cpu_to_le32(z); |
| 167 | |||
| 168 | } | 169 | } |
| 169 | 170 | ||
| 170 | static void xtea_decrypt(void *ctx_arg, u8 *dst, const u8 *src) | 171 | static void xtea_decrypt(void *ctx_arg, u8 *dst, const u8 *src) |
| 171 | { | 172 | { |
| 172 | |||
| 173 | u32 y, z, sum; | 173 | u32 y, z, sum; |
| 174 | struct tea_ctx *ctx = ctx_arg; | 174 | struct tea_ctx *ctx = ctx_arg; |
| 175 | const __le32 *in = (const __le32 *)src; | ||
| 176 | __le32 *out = (__le32 *)dst; | ||
| 175 | 177 | ||
| 176 | y = u32_in (src); | 178 | y = le32_to_cpu(in[0]); |
| 177 | z = u32_in (src + 4); | 179 | z = le32_to_cpu(in[1]); |
| 178 | 180 | ||
| 179 | sum = XTEA_DELTA * XTEA_ROUNDS; | 181 | sum = XTEA_DELTA * XTEA_ROUNDS; |
| 180 | 182 | ||
| @@ -184,22 +186,22 @@ static void xtea_decrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 184 | y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]); | 186 | y -= ((z << 4 ^ z >> 5) + z) ^ (sum + ctx->KEY[sum & 3]); |
| 185 | } | 187 | } |
| 186 | 188 | ||
| 187 | u32_out (dst, y); | 189 | out[0] = cpu_to_le32(y); |
| 188 | u32_out (dst + 4, z); | 190 | out[1] = cpu_to_le32(z); |
| 189 | |||
| 190 | } | 191 | } |
| 191 | 192 | ||
| 192 | 193 | ||
| 193 | static void xeta_encrypt(void *ctx_arg, u8 *dst, const u8 *src) | 194 | static void xeta_encrypt(void *ctx_arg, u8 *dst, const u8 *src) |
| 194 | { | 195 | { |
| 195 | |||
| 196 | u32 y, z, sum = 0; | 196 | u32 y, z, sum = 0; |
| 197 | u32 limit = XTEA_DELTA * XTEA_ROUNDS; | 197 | u32 limit = XTEA_DELTA * XTEA_ROUNDS; |
| 198 | 198 | ||
| 199 | struct xtea_ctx *ctx = ctx_arg; | 199 | struct xtea_ctx *ctx = ctx_arg; |
| 200 | const __le32 *in = (const __le32 *)src; | ||
| 201 | __le32 *out = (__le32 *)dst; | ||
| 200 | 202 | ||
| 201 | y = u32_in (src); | 203 | y = le32_to_cpu(in[0]); |
| 202 | z = u32_in (src + 4); | 204 | z = le32_to_cpu(in[1]); |
| 203 | 205 | ||
| 204 | while (sum != limit) { | 206 | while (sum != limit) { |
| 205 | y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3]; | 207 | y += (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum&3]; |
| @@ -207,19 +209,19 @@ static void xeta_encrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 207 | z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3]; | 209 | z += (y << 4 ^ y >> 5) + (y ^ sum) + ctx->KEY[sum>>11 &3]; |
| 208 | } | 210 | } |
| 209 | 211 | ||
| 210 | u32_out (dst, y); | 212 | out[0] = cpu_to_le32(y); |
| 211 | u32_out (dst + 4, z); | 213 | out[1] = cpu_to_le32(z); |
| 212 | |||
| 213 | } | 214 | } |
| 214 | 215 | ||
| 215 | static void xeta_decrypt(void *ctx_arg, u8 *dst, const u8 *src) | 216 | static void xeta_decrypt(void *ctx_arg, u8 *dst, const u8 *src) |
| 216 | { | 217 | { |
| 217 | |||
| 218 | u32 y, z, sum; | 218 | u32 y, z, sum; |
| 219 | struct tea_ctx *ctx = ctx_arg; | 219 | struct tea_ctx *ctx = ctx_arg; |
| 220 | const __le32 *in = (const __le32 *)src; | ||
| 221 | __le32 *out = (__le32 *)dst; | ||
| 220 | 222 | ||
| 221 | y = u32_in (src); | 223 | y = le32_to_cpu(in[0]); |
| 222 | z = u32_in (src + 4); | 224 | z = le32_to_cpu(in[1]); |
| 223 | 225 | ||
| 224 | sum = XTEA_DELTA * XTEA_ROUNDS; | 226 | sum = XTEA_DELTA * XTEA_ROUNDS; |
| 225 | 227 | ||
| @@ -229,9 +231,8 @@ static void xeta_decrypt(void *ctx_arg, u8 *dst, const u8 *src) | |||
| 229 | y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3]; | 231 | y -= (z << 4 ^ z >> 5) + (z ^ sum) + ctx->KEY[sum & 3]; |
| 230 | } | 232 | } |
| 231 | 233 | ||
| 232 | u32_out (dst, y); | 234 | out[0] = cpu_to_le32(y); |
| 233 | u32_out (dst + 4, z); | 235 | out[1] = cpu_to_le32(z); |
| 234 | |||
| 235 | } | 236 | } |
| 236 | 237 | ||
| 237 | static struct crypto_alg tea_alg = { | 238 | static struct crypto_alg tea_alg = { |
| @@ -239,6 +240,7 @@ static struct crypto_alg tea_alg = { | |||
| 239 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, | 240 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 240 | .cra_blocksize = TEA_BLOCK_SIZE, | 241 | .cra_blocksize = TEA_BLOCK_SIZE, |
| 241 | .cra_ctxsize = sizeof (struct tea_ctx), | 242 | .cra_ctxsize = sizeof (struct tea_ctx), |
| 243 | .cra_alignmask = 3, | ||
| 242 | .cra_module = THIS_MODULE, | 244 | .cra_module = THIS_MODULE, |
| 243 | .cra_list = LIST_HEAD_INIT(tea_alg.cra_list), | 245 | .cra_list = LIST_HEAD_INIT(tea_alg.cra_list), |
| 244 | .cra_u = { .cipher = { | 246 | .cra_u = { .cipher = { |
| @@ -254,6 +256,7 @@ static struct crypto_alg xtea_alg = { | |||
| 254 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, | 256 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 255 | .cra_blocksize = XTEA_BLOCK_SIZE, | 257 | .cra_blocksize = XTEA_BLOCK_SIZE, |
| 256 | .cra_ctxsize = sizeof (struct xtea_ctx), | 258 | .cra_ctxsize = sizeof (struct xtea_ctx), |
| 259 | .cra_alignmask = 3, | ||
| 257 | .cra_module = THIS_MODULE, | 260 | .cra_module = THIS_MODULE, |
| 258 | .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list), | 261 | .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list), |
| 259 | .cra_u = { .cipher = { | 262 | .cra_u = { .cipher = { |
| @@ -269,6 +272,7 @@ static struct crypto_alg xeta_alg = { | |||
| 269 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, | 272 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 270 | .cra_blocksize = XTEA_BLOCK_SIZE, | 273 | .cra_blocksize = XTEA_BLOCK_SIZE, |
| 271 | .cra_ctxsize = sizeof (struct xtea_ctx), | 274 | .cra_ctxsize = sizeof (struct xtea_ctx), |
| 275 | .cra_alignmask = 3, | ||
| 272 | .cra_module = THIS_MODULE, | 276 | .cra_module = THIS_MODULE, |
| 273 | .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list), | 277 | .cra_list = LIST_HEAD_INIT(xtea_alg.cra_list), |
| 274 | .cra_u = { .cipher = { | 278 | .cra_u = { .cipher = { |
