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