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