Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for VIA PadLock hardware crypto engine. |
| 5 | * |
| 6 | * Copyright (c) 2004 Michal Ludvig <michal@logix.cz> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 10 | #include <crypto/algapi.h> |
Sebastian Siewior | 89e1265 | 2007-10-17 23:18:57 +0800 | [diff] [blame] | 11 | #include <crypto/aes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 18 | #include <linux/percpu.h> |
| 19 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/byteorder.h> |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 21 | #include <asm/i387.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "padlock.h" |
| 23 | |
Michal Ludvig | ccc17c3 | 2006-07-15 10:23:49 +1000 | [diff] [blame] | 24 | /* Control word. */ |
| 25 | struct cword { |
| 26 | unsigned int __attribute__ ((__packed__)) |
| 27 | rounds:4, |
| 28 | algo:3, |
| 29 | keygen:1, |
| 30 | interm:1, |
| 31 | encdec:1, |
| 32 | ksize:2; |
| 33 | } __attribute__ ((__aligned__(PADLOCK_ALIGNMENT))); |
| 34 | |
Michal Ludvig | cc08632 | 2006-07-15 11:08:50 +1000 | [diff] [blame] | 35 | /* Whenever making any changes to the following |
| 36 | * structure *make sure* you keep E, d_data |
Sebastian Siewior | 7dc748e | 2008-04-01 21:24:50 +0800 | [diff] [blame] | 37 | * and cword aligned on 16 Bytes boundaries and |
| 38 | * the Hardware can access 16 * 16 bytes of E and d_data |
| 39 | * (only the first 15 * 16 bytes matter but the HW reads |
| 40 | * more). |
| 41 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct aes_ctx { |
Sebastian Siewior | 7dc748e | 2008-04-01 21:24:50 +0800 | [diff] [blame] | 43 | u32 E[AES_MAX_KEYLENGTH_U32] |
| 44 | __attribute__ ((__aligned__(PADLOCK_ALIGNMENT))); |
| 45 | u32 d_data[AES_MAX_KEYLENGTH_U32] |
| 46 | __attribute__ ((__aligned__(PADLOCK_ALIGNMENT))); |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 47 | struct { |
| 48 | struct cword encrypt; |
| 49 | struct cword decrypt; |
| 50 | } cword; |
Herbert Xu | 82062c7 | 2006-05-16 22:20:34 +1000 | [diff] [blame] | 51 | u32 *D; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 54 | static DEFINE_PER_CPU(struct cword *, last_cword); |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /* Tells whether the ACE is capable to generate |
| 57 | the extended key for a given key_len. */ |
| 58 | static inline int |
| 59 | aes_hw_extkey_available(uint8_t key_len) |
| 60 | { |
| 61 | /* TODO: We should check the actual CPU model/stepping |
| 62 | as it's possible that the capability will be |
| 63 | added in the next CPU revisions. */ |
| 64 | if (key_len == 16) |
| 65 | return 1; |
| 66 | return 0; |
| 67 | } |
| 68 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 69 | static inline struct aes_ctx *aes_ctx_common(void *ctx) |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 70 | { |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 71 | unsigned long addr = (unsigned long)ctx; |
Herbert Xu | f10b789 | 2006-01-25 22:34:01 +1100 | [diff] [blame] | 72 | unsigned long align = PADLOCK_ALIGNMENT; |
| 73 | |
| 74 | if (align <= crypto_tfm_ctx_alignment()) |
| 75 | align = 1; |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 76 | return (struct aes_ctx *)ALIGN(addr, align); |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 79 | static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm) |
| 80 | { |
| 81 | return aes_ctx_common(crypto_tfm_ctx(tfm)); |
| 82 | } |
| 83 | |
| 84 | static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm) |
| 85 | { |
| 86 | return aes_ctx_common(crypto_blkcipher_ctx(tfm)); |
| 87 | } |
| 88 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 89 | static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 90 | unsigned int key_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 92 | struct aes_ctx *ctx = aes_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 93 | const __le32 *key = (const __le32 *)in_key; |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 94 | u32 *flags = &tfm->crt_flags; |
Sebastian Siewior | 7dc748e | 2008-04-01 21:24:50 +0800 | [diff] [blame] | 95 | struct crypto_aes_ctx gen_aes; |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 96 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 98 | if (key_len % 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 103 | /* |
| 104 | * If the hardware is capable of generating the extended key |
| 105 | * itself we must supply the plain key for both encryption |
| 106 | * and decryption. |
| 107 | */ |
Herbert Xu | 82062c7 | 2006-05-16 22:20:34 +1000 | [diff] [blame] | 108 | ctx->D = ctx->E; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Sebastian Siewior | 7dc748e | 2008-04-01 21:24:50 +0800 | [diff] [blame] | 110 | ctx->E[0] = le32_to_cpu(key[0]); |
| 111 | ctx->E[1] = le32_to_cpu(key[1]); |
| 112 | ctx->E[2] = le32_to_cpu(key[2]); |
| 113 | ctx->E[3] = le32_to_cpu(key[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 115 | /* Prepare control words. */ |
| 116 | memset(&ctx->cword, 0, sizeof(ctx->cword)); |
| 117 | |
| 118 | ctx->cword.decrypt.encdec = 1; |
| 119 | ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4; |
| 120 | ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds; |
| 121 | ctx->cword.encrypt.ksize = (key_len - 16) / 8; |
| 122 | ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize; |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* Don't generate extended keys if the hardware can do it. */ |
| 125 | if (aes_hw_extkey_available(key_len)) |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 126 | goto ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 128 | ctx->D = ctx->d_data; |
| 129 | ctx->cword.encrypt.keygen = 1; |
| 130 | ctx->cword.decrypt.keygen = 1; |
| 131 | |
Sebastian Siewior | 7dc748e | 2008-04-01 21:24:50 +0800 | [diff] [blame] | 132 | if (crypto_aes_expand_key(&gen_aes, in_key, key_len)) { |
| 133 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 134 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Sebastian Siewior | 7dc748e | 2008-04-01 21:24:50 +0800 | [diff] [blame] | 137 | memcpy(ctx->E, gen_aes.key_enc, AES_MAX_KEYLENGTH); |
| 138 | memcpy(ctx->D, gen_aes.key_dec, AES_MAX_KEYLENGTH); |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 139 | |
| 140 | ok: |
| 141 | for_each_online_cpu(cpu) |
| 142 | if (&ctx->cword.encrypt == per_cpu(last_cword, cpu) || |
| 143 | &ctx->cword.decrypt == per_cpu(last_cword, cpu)) |
| 144 | per_cpu(last_cword, cpu) = NULL; |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* ====== Encryption/decryption routines ====== */ |
| 150 | |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 151 | /* These are the real call to PadLock. */ |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 152 | static inline void padlock_reset_key(struct cword *cword) |
Herbert Xu | 866cd90 | 2007-12-27 00:04:44 +1100 | [diff] [blame] | 153 | { |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 154 | int cpu = raw_smp_processor_id(); |
| 155 | |
| 156 | if (cword != per_cpu(last_cword, cpu)) |
| 157 | asm volatile ("pushfl; popfl"); |
| 158 | } |
| 159 | |
| 160 | static inline void padlock_store_cword(struct cword *cword) |
| 161 | { |
| 162 | per_cpu(last_cword, raw_smp_processor_id()) = cword; |
Herbert Xu | 866cd90 | 2007-12-27 00:04:44 +1100 | [diff] [blame] | 163 | } |
| 164 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 165 | /* |
| 166 | * While the padlock instructions don't use FP/SSE registers, they |
| 167 | * generate a spurious DNA fault when cr0.ts is '1'. These instructions |
| 168 | * should be used only inside the irq_ts_save/restore() context |
| 169 | */ |
| 170 | |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 171 | static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key, |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 172 | struct cword *control_word) |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 173 | { |
| 174 | asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */ |
| 175 | : "+S"(input), "+D"(output) |
| 176 | : "d"(control_word), "b"(key), "c"(1)); |
| 177 | } |
| 178 | |
| 179 | static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword) |
| 180 | { |
Herbert Xu | 490fe3f | 2008-01-11 08:09:35 +1100 | [diff] [blame] | 181 | u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1]; |
| 182 | u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT); |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 183 | |
| 184 | memcpy(tmp, in, AES_BLOCK_SIZE); |
| 185 | padlock_xcrypt(tmp, out, key, cword); |
| 186 | } |
| 187 | |
| 188 | static inline void aes_crypt(const u8 *in, u8 *out, u32 *key, |
| 189 | struct cword *cword) |
| 190 | { |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 191 | /* padlock_xcrypt requires at least two blocks of data. */ |
| 192 | if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) & |
| 193 | (PAGE_SIZE - 1)))) { |
| 194 | aes_crypt_copy(in, out, key, cword); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | padlock_xcrypt(in, out, key, cword); |
| 199 | } |
| 200 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 201 | static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key, |
| 202 | void *control_word, u32 count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 204 | if (count == 1) { |
| 205 | aes_crypt(input, output, key, control_word); |
| 206 | return; |
| 207 | } |
| 208 | |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 209 | asm volatile ("test $1, %%cl;" |
| 210 | "je 1f;" |
| 211 | "lea -1(%%ecx), %%eax;" |
| 212 | "mov $1, %%ecx;" |
| 213 | ".byte 0xf3,0x0f,0xa7,0xc8;" /* rep xcryptecb */ |
| 214 | "mov %%eax, %%ecx;" |
| 215 | "1:" |
| 216 | ".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | : "+S"(input), "+D"(output) |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 218 | : "d"(control_word), "b"(key), "c"(count) |
| 219 | : "ax"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Herbert Xu | 476df25 | 2005-07-06 13:54:09 -0700 | [diff] [blame] | 222 | static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key, |
| 223 | u8 *iv, void *control_word, u32 count) |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 224 | { |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 225 | /* rep xcryptcbc */ |
| 226 | asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" |
| 227 | : "+S" (input), "+D" (output), "+a" (iv) |
| 228 | : "d" (control_word), "b" (key), "c" (count)); |
Herbert Xu | 476df25 | 2005-07-06 13:54:09 -0700 | [diff] [blame] | 229 | return iv; |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 232 | static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 234 | struct aes_ctx *ctx = aes_ctx(tfm); |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 235 | int ts_state; |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 236 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 237 | padlock_reset_key(&ctx->cword.encrypt); |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 238 | ts_state = irq_ts_save(); |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 239 | aes_crypt(in, out, ctx->E, &ctx->cword.encrypt); |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 240 | irq_ts_restore(ts_state); |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 241 | padlock_store_cword(&ctx->cword.encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 244 | static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 246 | struct aes_ctx *ctx = aes_ctx(tfm); |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 247 | int ts_state; |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 248 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 249 | padlock_reset_key(&ctx->cword.encrypt); |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 250 | ts_state = irq_ts_save(); |
Herbert Xu | d4a7dd8 | 2007-12-28 11:05:46 +1100 | [diff] [blame] | 251 | aes_crypt(in, out, ctx->D, &ctx->cword.decrypt); |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 252 | irq_ts_restore(ts_state); |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 253 | padlock_store_cword(&ctx->cword.encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static struct crypto_alg aes_alg = { |
| 257 | .cra_name = "aes", |
Herbert Xu | c8a19c9 | 2005-11-05 18:06:26 +1100 | [diff] [blame] | 258 | .cra_driver_name = "aes-padlock", |
Michal Ludvig | ccc17c3 | 2006-07-15 10:23:49 +1000 | [diff] [blame] | 259 | .cra_priority = PADLOCK_CRA_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 261 | .cra_blocksize = AES_BLOCK_SIZE, |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 262 | .cra_ctxsize = sizeof(struct aes_ctx), |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 263 | .cra_alignmask = PADLOCK_ALIGNMENT - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | .cra_module = THIS_MODULE, |
| 265 | .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), |
| 266 | .cra_u = { |
| 267 | .cipher = { |
| 268 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 269 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
| 270 | .cia_setkey = aes_set_key, |
| 271 | .cia_encrypt = aes_encrypt, |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 272 | .cia_decrypt = aes_decrypt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | }; |
| 276 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 277 | static int ecb_aes_encrypt(struct blkcipher_desc *desc, |
| 278 | struct scatterlist *dst, struct scatterlist *src, |
| 279 | unsigned int nbytes) |
| 280 | { |
| 281 | struct aes_ctx *ctx = blk_aes_ctx(desc->tfm); |
| 282 | struct blkcipher_walk walk; |
| 283 | int err; |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 284 | int ts_state; |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 285 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 286 | padlock_reset_key(&ctx->cword.encrypt); |
Herbert Xu | 866cd90 | 2007-12-27 00:04:44 +1100 | [diff] [blame] | 287 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 288 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 289 | err = blkcipher_walk_virt(desc, &walk); |
| 290 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 291 | ts_state = irq_ts_save(); |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 292 | while ((nbytes = walk.nbytes)) { |
| 293 | padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr, |
| 294 | ctx->E, &ctx->cword.encrypt, |
| 295 | nbytes / AES_BLOCK_SIZE); |
| 296 | nbytes &= AES_BLOCK_SIZE - 1; |
| 297 | err = blkcipher_walk_done(desc, &walk, nbytes); |
| 298 | } |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 299 | irq_ts_restore(ts_state); |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 300 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 301 | padlock_store_cword(&ctx->cword.encrypt); |
| 302 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 303 | return err; |
| 304 | } |
| 305 | |
| 306 | static int ecb_aes_decrypt(struct blkcipher_desc *desc, |
| 307 | struct scatterlist *dst, struct scatterlist *src, |
| 308 | unsigned int nbytes) |
| 309 | { |
| 310 | struct aes_ctx *ctx = blk_aes_ctx(desc->tfm); |
| 311 | struct blkcipher_walk walk; |
| 312 | int err; |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 313 | int ts_state; |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 314 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 315 | padlock_reset_key(&ctx->cword.decrypt); |
Herbert Xu | 866cd90 | 2007-12-27 00:04:44 +1100 | [diff] [blame] | 316 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 317 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 318 | err = blkcipher_walk_virt(desc, &walk); |
| 319 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 320 | ts_state = irq_ts_save(); |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 321 | while ((nbytes = walk.nbytes)) { |
| 322 | padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr, |
| 323 | ctx->D, &ctx->cword.decrypt, |
| 324 | nbytes / AES_BLOCK_SIZE); |
| 325 | nbytes &= AES_BLOCK_SIZE - 1; |
| 326 | err = blkcipher_walk_done(desc, &walk, nbytes); |
| 327 | } |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 328 | irq_ts_restore(ts_state); |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 329 | |
| 330 | padlock_store_cword(&ctx->cword.encrypt); |
| 331 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 332 | return err; |
| 333 | } |
| 334 | |
| 335 | static struct crypto_alg ecb_aes_alg = { |
| 336 | .cra_name = "ecb(aes)", |
| 337 | .cra_driver_name = "ecb-aes-padlock", |
| 338 | .cra_priority = PADLOCK_COMPOSITE_PRIORITY, |
| 339 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 340 | .cra_blocksize = AES_BLOCK_SIZE, |
| 341 | .cra_ctxsize = sizeof(struct aes_ctx), |
| 342 | .cra_alignmask = PADLOCK_ALIGNMENT - 1, |
| 343 | .cra_type = &crypto_blkcipher_type, |
| 344 | .cra_module = THIS_MODULE, |
| 345 | .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list), |
| 346 | .cra_u = { |
| 347 | .blkcipher = { |
| 348 | .min_keysize = AES_MIN_KEY_SIZE, |
| 349 | .max_keysize = AES_MAX_KEY_SIZE, |
| 350 | .setkey = aes_set_key, |
| 351 | .encrypt = ecb_aes_encrypt, |
| 352 | .decrypt = ecb_aes_decrypt, |
| 353 | } |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | static int cbc_aes_encrypt(struct blkcipher_desc *desc, |
| 358 | struct scatterlist *dst, struct scatterlist *src, |
| 359 | unsigned int nbytes) |
| 360 | { |
| 361 | struct aes_ctx *ctx = blk_aes_ctx(desc->tfm); |
| 362 | struct blkcipher_walk walk; |
| 363 | int err; |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 364 | int ts_state; |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 365 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 366 | padlock_reset_key(&ctx->cword.encrypt); |
Herbert Xu | 866cd90 | 2007-12-27 00:04:44 +1100 | [diff] [blame] | 367 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 368 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 369 | err = blkcipher_walk_virt(desc, &walk); |
| 370 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 371 | ts_state = irq_ts_save(); |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 372 | while ((nbytes = walk.nbytes)) { |
| 373 | u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr, |
| 374 | walk.dst.virt.addr, ctx->E, |
| 375 | walk.iv, &ctx->cword.encrypt, |
| 376 | nbytes / AES_BLOCK_SIZE); |
| 377 | memcpy(walk.iv, iv, AES_BLOCK_SIZE); |
| 378 | nbytes &= AES_BLOCK_SIZE - 1; |
| 379 | err = blkcipher_walk_done(desc, &walk, nbytes); |
| 380 | } |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 381 | irq_ts_restore(ts_state); |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 382 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 383 | padlock_store_cword(&ctx->cword.decrypt); |
| 384 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 385 | return err; |
| 386 | } |
| 387 | |
| 388 | static int cbc_aes_decrypt(struct blkcipher_desc *desc, |
| 389 | struct scatterlist *dst, struct scatterlist *src, |
| 390 | unsigned int nbytes) |
| 391 | { |
| 392 | struct aes_ctx *ctx = blk_aes_ctx(desc->tfm); |
| 393 | struct blkcipher_walk walk; |
| 394 | int err; |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 395 | int ts_state; |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 396 | |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 397 | padlock_reset_key(&ctx->cword.encrypt); |
Herbert Xu | 866cd90 | 2007-12-27 00:04:44 +1100 | [diff] [blame] | 398 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 399 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 400 | err = blkcipher_walk_virt(desc, &walk); |
| 401 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 402 | ts_state = irq_ts_save(); |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 403 | while ((nbytes = walk.nbytes)) { |
| 404 | padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr, |
| 405 | ctx->D, walk.iv, &ctx->cword.decrypt, |
| 406 | nbytes / AES_BLOCK_SIZE); |
| 407 | nbytes &= AES_BLOCK_SIZE - 1; |
| 408 | err = blkcipher_walk_done(desc, &walk, nbytes); |
| 409 | } |
| 410 | |
Suresh Siddha | e491401 | 2008-08-13 22:02:26 +1000 | [diff] [blame] | 411 | irq_ts_restore(ts_state); |
Herbert Xu | 420a4b2 | 2008-08-31 15:58:45 +1000 | [diff] [blame] | 412 | |
| 413 | padlock_store_cword(&ctx->cword.encrypt); |
| 414 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 415 | return err; |
| 416 | } |
| 417 | |
| 418 | static struct crypto_alg cbc_aes_alg = { |
| 419 | .cra_name = "cbc(aes)", |
| 420 | .cra_driver_name = "cbc-aes-padlock", |
| 421 | .cra_priority = PADLOCK_COMPOSITE_PRIORITY, |
| 422 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 423 | .cra_blocksize = AES_BLOCK_SIZE, |
| 424 | .cra_ctxsize = sizeof(struct aes_ctx), |
| 425 | .cra_alignmask = PADLOCK_ALIGNMENT - 1, |
| 426 | .cra_type = &crypto_blkcipher_type, |
| 427 | .cra_module = THIS_MODULE, |
| 428 | .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list), |
| 429 | .cra_u = { |
| 430 | .blkcipher = { |
| 431 | .min_keysize = AES_MIN_KEY_SIZE, |
| 432 | .max_keysize = AES_MAX_KEY_SIZE, |
| 433 | .ivsize = AES_BLOCK_SIZE, |
| 434 | .setkey = aes_set_key, |
| 435 | .encrypt = cbc_aes_encrypt, |
| 436 | .decrypt = cbc_aes_decrypt, |
| 437 | } |
| 438 | } |
| 439 | }; |
| 440 | |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 441 | static int __init padlock_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 443 | int ret; |
| 444 | |
| 445 | if (!cpu_has_xcrypt) { |
Jeremy Katz | b43e726 | 2008-07-03 19:03:31 +0800 | [diff] [blame] | 446 | printk(KERN_NOTICE PFX "VIA PadLock not detected.\n"); |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 447 | return -ENODEV; |
| 448 | } |
| 449 | |
| 450 | if (!cpu_has_xcrypt_enabled) { |
Jeremy Katz | b43e726 | 2008-07-03 19:03:31 +0800 | [diff] [blame] | 451 | printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n"); |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 452 | return -ENODEV; |
| 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 455 | if ((ret = crypto_register_alg(&aes_alg))) |
| 456 | goto aes_err; |
| 457 | |
| 458 | if ((ret = crypto_register_alg(&ecb_aes_alg))) |
| 459 | goto ecb_aes_err; |
| 460 | |
| 461 | if ((ret = crypto_register_alg(&cbc_aes_alg))) |
| 462 | goto cbc_aes_err; |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 463 | |
| 464 | printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n"); |
| 465 | |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 466 | out: |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 467 | return ret; |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 468 | |
| 469 | cbc_aes_err: |
| 470 | crypto_unregister_alg(&ecb_aes_alg); |
| 471 | ecb_aes_err: |
| 472 | crypto_unregister_alg(&aes_alg); |
| 473 | aes_err: |
| 474 | printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n"); |
| 475 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 478 | static void __exit padlock_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Herbert Xu | 28ce728 | 2006-08-21 21:38:42 +1000 | [diff] [blame] | 480 | crypto_unregister_alg(&cbc_aes_alg); |
| 481 | crypto_unregister_alg(&ecb_aes_alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | crypto_unregister_alg(&aes_alg); |
| 483 | } |
Michal Ludvig | 1191f0a | 2006-08-06 22:46:20 +1000 | [diff] [blame] | 484 | |
| 485 | module_init(padlock_init); |
| 486 | module_exit(padlock_fini); |
| 487 | |
| 488 | MODULE_DESCRIPTION("VIA PadLock AES algorithm support"); |
| 489 | MODULE_LICENSE("GPL"); |
| 490 | MODULE_AUTHOR("Michal Ludvig"); |
| 491 | |
Sebastian Siewior | f8246af | 2007-10-05 16:52:01 +0800 | [diff] [blame] | 492 | MODULE_ALIAS("aes"); |