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 | * |
| 8 | * Key expansion routine taken from crypto/aes.c |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * --------------------------------------------------------------------------- |
| 16 | * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. |
| 17 | * All rights reserved. |
| 18 | * |
| 19 | * LICENSE TERMS |
| 20 | * |
| 21 | * The free distribution and use of this software in both source and binary |
| 22 | * form is allowed (with or without changes) provided that: |
| 23 | * |
| 24 | * 1. distributions of this source code include the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer; |
| 26 | * |
| 27 | * 2. distributions in binary form include the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer |
| 29 | * in the documentation and/or other associated materials; |
| 30 | * |
| 31 | * 3. the copyright holder's name is not used to endorse products |
| 32 | * built using this software without specific written permission. |
| 33 | * |
| 34 | * ALTERNATIVELY, provided that this notice is retained in full, this product |
| 35 | * may be distributed under the terms of the GNU General Public License (GPL), |
| 36 | * in which case the provisions of the GPL apply INSTEAD OF those given above. |
| 37 | * |
| 38 | * DISCLAIMER |
| 39 | * |
| 40 | * This software is provided 'as is' with no explicit or implied warranties |
| 41 | * in respect of its properties, including, but not limited to, correctness |
| 42 | * and/or fitness for purpose. |
| 43 | * --------------------------------------------------------------------------- |
| 44 | */ |
| 45 | |
| 46 | #include <linux/module.h> |
| 47 | #include <linux/init.h> |
| 48 | #include <linux/types.h> |
| 49 | #include <linux/errno.h> |
| 50 | #include <linux/crypto.h> |
| 51 | #include <linux/interrupt.h> |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 52 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <asm/byteorder.h> |
| 54 | #include "padlock.h" |
| 55 | |
| 56 | #define AES_MIN_KEY_SIZE 16 /* in uint8_t units */ |
| 57 | #define AES_MAX_KEY_SIZE 32 /* ditto */ |
| 58 | #define AES_BLOCK_SIZE 16 /* ditto */ |
| 59 | #define AES_EXTENDED_KEY_SIZE 64 /* in uint32_t units */ |
| 60 | #define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t)) |
| 61 | |
| 62 | struct aes_ctx { |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 63 | uint32_t e_data[AES_EXTENDED_KEY_SIZE]; |
| 64 | uint32_t d_data[AES_EXTENDED_KEY_SIZE]; |
| 65 | struct { |
| 66 | struct cword encrypt; |
| 67 | struct cword decrypt; |
| 68 | } cword; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | uint32_t *E; |
| 70 | uint32_t *D; |
| 71 | int key_length; |
| 72 | }; |
| 73 | |
| 74 | /* ====== Key management routines ====== */ |
| 75 | |
| 76 | static inline uint32_t |
| 77 | generic_rotr32 (const uint32_t x, const unsigned bits) |
| 78 | { |
| 79 | const unsigned n = bits % 32; |
| 80 | return (x >> n) | (x << (32 - n)); |
| 81 | } |
| 82 | |
| 83 | static inline uint32_t |
| 84 | generic_rotl32 (const uint32_t x, const unsigned bits) |
| 85 | { |
| 86 | const unsigned n = bits % 32; |
| 87 | return (x << n) | (x >> (32 - n)); |
| 88 | } |
| 89 | |
| 90 | #define rotl generic_rotl32 |
| 91 | #define rotr generic_rotr32 |
| 92 | |
| 93 | /* |
| 94 | * #define byte(x, nr) ((unsigned char)((x) >> (nr*8))) |
| 95 | */ |
| 96 | static inline uint8_t |
| 97 | byte(const uint32_t x, const unsigned n) |
| 98 | { |
| 99 | return x >> (n << 3); |
| 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #define E_KEY ctx->E |
| 103 | #define D_KEY ctx->D |
| 104 | |
| 105 | static uint8_t pow_tab[256]; |
| 106 | static uint8_t log_tab[256]; |
| 107 | static uint8_t sbx_tab[256]; |
| 108 | static uint8_t isb_tab[256]; |
| 109 | static uint32_t rco_tab[10]; |
| 110 | static uint32_t ft_tab[4][256]; |
| 111 | static uint32_t it_tab[4][256]; |
| 112 | |
| 113 | static uint32_t fl_tab[4][256]; |
| 114 | static uint32_t il_tab[4][256]; |
| 115 | |
| 116 | static inline uint8_t |
| 117 | f_mult (uint8_t a, uint8_t b) |
| 118 | { |
| 119 | uint8_t aa = log_tab[a], cc = aa + log_tab[b]; |
| 120 | |
| 121 | return pow_tab[cc + (cc < aa ? 1 : 0)]; |
| 122 | } |
| 123 | |
| 124 | #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0) |
| 125 | |
| 126 | #define f_rn(bo, bi, n, k) \ |
| 127 | bo[n] = ft_tab[0][byte(bi[n],0)] ^ \ |
| 128 | ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \ |
| 129 | ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ |
| 130 | ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n) |
| 131 | |
| 132 | #define i_rn(bo, bi, n, k) \ |
| 133 | bo[n] = it_tab[0][byte(bi[n],0)] ^ \ |
| 134 | it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \ |
| 135 | it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ |
| 136 | it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n) |
| 137 | |
| 138 | #define ls_box(x) \ |
| 139 | ( fl_tab[0][byte(x, 0)] ^ \ |
| 140 | fl_tab[1][byte(x, 1)] ^ \ |
| 141 | fl_tab[2][byte(x, 2)] ^ \ |
| 142 | fl_tab[3][byte(x, 3)] ) |
| 143 | |
| 144 | #define f_rl(bo, bi, n, k) \ |
| 145 | bo[n] = fl_tab[0][byte(bi[n],0)] ^ \ |
| 146 | fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \ |
| 147 | fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ |
| 148 | fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n) |
| 149 | |
| 150 | #define i_rl(bo, bi, n, k) \ |
| 151 | bo[n] = il_tab[0][byte(bi[n],0)] ^ \ |
| 152 | il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \ |
| 153 | il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \ |
| 154 | il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n) |
| 155 | |
| 156 | static void |
| 157 | gen_tabs (void) |
| 158 | { |
| 159 | uint32_t i, t; |
| 160 | uint8_t p, q; |
| 161 | |
| 162 | /* log and power tables for GF(2**8) finite field with |
| 163 | 0x011b as modular polynomial - the simplest prmitive |
| 164 | root is 0x03, used here to generate the tables */ |
| 165 | |
| 166 | for (i = 0, p = 1; i < 256; ++i) { |
| 167 | pow_tab[i] = (uint8_t) p; |
| 168 | log_tab[p] = (uint8_t) i; |
| 169 | |
| 170 | p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0); |
| 171 | } |
| 172 | |
| 173 | log_tab[1] = 0; |
| 174 | |
| 175 | for (i = 0, p = 1; i < 10; ++i) { |
| 176 | rco_tab[i] = p; |
| 177 | |
| 178 | p = (p << 1) ^ (p & 0x80 ? 0x01b : 0); |
| 179 | } |
| 180 | |
| 181 | for (i = 0; i < 256; ++i) { |
| 182 | p = (i ? pow_tab[255 - log_tab[i]] : 0); |
| 183 | q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2)); |
| 184 | p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2)); |
| 185 | sbx_tab[i] = p; |
| 186 | isb_tab[p] = (uint8_t) i; |
| 187 | } |
| 188 | |
| 189 | for (i = 0; i < 256; ++i) { |
| 190 | p = sbx_tab[i]; |
| 191 | |
| 192 | t = p; |
| 193 | fl_tab[0][i] = t; |
| 194 | fl_tab[1][i] = rotl (t, 8); |
| 195 | fl_tab[2][i] = rotl (t, 16); |
| 196 | fl_tab[3][i] = rotl (t, 24); |
| 197 | |
| 198 | t = ((uint32_t) ff_mult (2, p)) | |
| 199 | ((uint32_t) p << 8) | |
| 200 | ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24); |
| 201 | |
| 202 | ft_tab[0][i] = t; |
| 203 | ft_tab[1][i] = rotl (t, 8); |
| 204 | ft_tab[2][i] = rotl (t, 16); |
| 205 | ft_tab[3][i] = rotl (t, 24); |
| 206 | |
| 207 | p = isb_tab[i]; |
| 208 | |
| 209 | t = p; |
| 210 | il_tab[0][i] = t; |
| 211 | il_tab[1][i] = rotl (t, 8); |
| 212 | il_tab[2][i] = rotl (t, 16); |
| 213 | il_tab[3][i] = rotl (t, 24); |
| 214 | |
| 215 | t = ((uint32_t) ff_mult (14, p)) | |
| 216 | ((uint32_t) ff_mult (9, p) << 8) | |
| 217 | ((uint32_t) ff_mult (13, p) << 16) | |
| 218 | ((uint32_t) ff_mult (11, p) << 24); |
| 219 | |
| 220 | it_tab[0][i] = t; |
| 221 | it_tab[1][i] = rotl (t, 8); |
| 222 | it_tab[2][i] = rotl (t, 16); |
| 223 | it_tab[3][i] = rotl (t, 24); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b) |
| 228 | |
| 229 | #define imix_col(y,x) \ |
| 230 | u = star_x(x); \ |
| 231 | v = star_x(u); \ |
| 232 | w = star_x(v); \ |
| 233 | t = w ^ (x); \ |
| 234 | (y) = u ^ v ^ w; \ |
| 235 | (y) ^= rotr(u ^ t, 8) ^ \ |
| 236 | rotr(v ^ t, 16) ^ \ |
| 237 | rotr(t,24) |
| 238 | |
| 239 | /* initialise the key schedule from the user supplied key */ |
| 240 | |
| 241 | #define loop4(i) \ |
| 242 | { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \ |
| 243 | t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \ |
| 244 | t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \ |
| 245 | t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \ |
| 246 | t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \ |
| 247 | } |
| 248 | |
| 249 | #define loop6(i) \ |
| 250 | { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \ |
| 251 | t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \ |
| 252 | t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \ |
| 253 | t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \ |
| 254 | t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \ |
| 255 | t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \ |
| 256 | t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \ |
| 257 | } |
| 258 | |
| 259 | #define loop8(i) \ |
| 260 | { t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \ |
| 261 | t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \ |
| 262 | t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \ |
| 263 | t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \ |
| 264 | t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \ |
| 265 | t = E_KEY[8 * i + 4] ^ ls_box(t); \ |
| 266 | E_KEY[8 * i + 12] = t; \ |
| 267 | t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \ |
| 268 | t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \ |
| 269 | t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \ |
| 270 | } |
| 271 | |
| 272 | /* Tells whether the ACE is capable to generate |
| 273 | the extended key for a given key_len. */ |
| 274 | static inline int |
| 275 | aes_hw_extkey_available(uint8_t key_len) |
| 276 | { |
| 277 | /* TODO: We should check the actual CPU model/stepping |
| 278 | as it's possible that the capability will be |
| 279 | added in the next CPU revisions. */ |
| 280 | if (key_len == 16) |
| 281 | return 1; |
| 282 | return 0; |
| 283 | } |
| 284 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 285 | static inline struct aes_ctx *aes_ctx(void *ctx) |
| 286 | { |
| 287 | return (struct aes_ctx *)ALIGN((unsigned long)ctx, PADLOCK_ALIGNMENT); |
| 288 | } |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | static int |
| 291 | aes_set_key(void *ctx_arg, const uint8_t *in_key, unsigned int key_len, uint32_t *flags) |
| 292 | { |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 293 | struct aes_ctx *ctx = aes_ctx(ctx_arg); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 294 | const __le32 *key = (const __le32 *)in_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | uint32_t i, t, u, v, w; |
| 296 | uint32_t P[AES_EXTENDED_KEY_SIZE]; |
| 297 | uint32_t rounds; |
| 298 | |
| 299 | if (key_len != 16 && key_len != 24 && key_len != 32) { |
| 300 | *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | ctx->key_length = key_len; |
| 305 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 306 | /* |
| 307 | * If the hardware is capable of generating the extended key |
| 308 | * itself we must supply the plain key for both encryption |
| 309 | * and decryption. |
| 310 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | ctx->E = ctx->e_data; |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 312 | ctx->D = ctx->e_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 314 | E_KEY[0] = le32_to_cpu(key[0]); |
| 315 | E_KEY[1] = le32_to_cpu(key[1]); |
| 316 | E_KEY[2] = le32_to_cpu(key[2]); |
| 317 | E_KEY[3] = le32_to_cpu(key[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 319 | /* Prepare control words. */ |
| 320 | memset(&ctx->cword, 0, sizeof(ctx->cword)); |
| 321 | |
| 322 | ctx->cword.decrypt.encdec = 1; |
| 323 | ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4; |
| 324 | ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds; |
| 325 | ctx->cword.encrypt.ksize = (key_len - 16) / 8; |
| 326 | ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize; |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | /* Don't generate extended keys if the hardware can do it. */ |
| 329 | if (aes_hw_extkey_available(key_len)) |
| 330 | return 0; |
| 331 | |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 332 | ctx->D = ctx->d_data; |
| 333 | ctx->cword.encrypt.keygen = 1; |
| 334 | ctx->cword.decrypt.keygen = 1; |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | switch (key_len) { |
| 337 | case 16: |
| 338 | t = E_KEY[3]; |
| 339 | for (i = 0; i < 10; ++i) |
| 340 | loop4 (i); |
| 341 | break; |
| 342 | |
| 343 | case 24: |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 344 | E_KEY[4] = le32_to_cpu(key[4]); |
| 345 | t = E_KEY[5] = le32_to_cpu(key[5]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | for (i = 0; i < 8; ++i) |
| 347 | loop6 (i); |
| 348 | break; |
| 349 | |
| 350 | case 32: |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 351 | E_KEY[4] = le32_to_cpu(in_key[4]); |
| 352 | E_KEY[5] = le32_to_cpu(in_key[5]); |
| 353 | E_KEY[6] = le32_to_cpu(in_key[6]); |
| 354 | t = E_KEY[7] = le32_to_cpu(in_key[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | for (i = 0; i < 7; ++i) |
| 356 | loop8 (i); |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | D_KEY[0] = E_KEY[0]; |
| 361 | D_KEY[1] = E_KEY[1]; |
| 362 | D_KEY[2] = E_KEY[2]; |
| 363 | D_KEY[3] = E_KEY[3]; |
| 364 | |
| 365 | for (i = 4; i < key_len + 24; ++i) { |
| 366 | imix_col (D_KEY[i], E_KEY[i]); |
| 367 | } |
| 368 | |
| 369 | /* PadLock needs a different format of the decryption key. */ |
| 370 | rounds = 10 + (key_len - 16) / 4; |
| 371 | |
| 372 | for (i = 0; i < rounds; i++) { |
| 373 | P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0]; |
| 374 | P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1]; |
| 375 | P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2]; |
| 376 | P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3]; |
| 377 | } |
| 378 | |
| 379 | P[0] = E_KEY[(rounds * 4) + 0]; |
| 380 | P[1] = E_KEY[(rounds * 4) + 1]; |
| 381 | P[2] = E_KEY[(rounds * 4) + 2]; |
| 382 | P[3] = E_KEY[(rounds * 4) + 3]; |
| 383 | |
| 384 | memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /* ====== Encryption/decryption routines ====== */ |
| 390 | |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 391 | /* These are the real call to PadLock. */ |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 392 | static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key, |
| 393 | void *control_word, u32 count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | asm volatile ("pushfl; popfl"); /* enforce key reload. */ |
| 396 | asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */ |
| 397 | : "+S"(input), "+D"(output) |
| 398 | : "d"(control_word), "b"(key), "c"(count)); |
| 399 | } |
| 400 | |
Herbert Xu | 476df25 | 2005-07-06 13:54:09 -0700 | [diff] [blame] | 401 | static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key, |
| 402 | u8 *iv, void *control_word, u32 count) |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 403 | { |
| 404 | /* Enforce key reload. */ |
| 405 | asm volatile ("pushfl; popfl"); |
| 406 | /* rep xcryptcbc */ |
| 407 | asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" |
| 408 | : "+S" (input), "+D" (output), "+a" (iv) |
| 409 | : "d" (control_word), "b" (key), "c" (count)); |
Herbert Xu | 476df25 | 2005-07-06 13:54:09 -0700 | [diff] [blame] | 410 | return iv; |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | aes_encrypt(void *ctx_arg, uint8_t *out, const uint8_t *in) |
| 415 | { |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 416 | struct aes_ctx *ctx = aes_ctx(ctx_arg); |
| 417 | padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static void |
| 421 | aes_decrypt(void *ctx_arg, uint8_t *out, const uint8_t *in) |
| 422 | { |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 423 | struct aes_ctx *ctx = aes_ctx(ctx_arg); |
| 424 | padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 427 | static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out, |
| 428 | const u8 *in, unsigned int nbytes) |
| 429 | { |
| 430 | struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm)); |
| 431 | padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, |
| 432 | nbytes / AES_BLOCK_SIZE); |
| 433 | return nbytes & ~(AES_BLOCK_SIZE - 1); |
| 434 | } |
| 435 | |
| 436 | static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out, |
| 437 | const u8 *in, unsigned int nbytes) |
| 438 | { |
| 439 | struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm)); |
| 440 | padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, |
| 441 | nbytes / AES_BLOCK_SIZE); |
| 442 | return nbytes & ~(AES_BLOCK_SIZE - 1); |
| 443 | } |
| 444 | |
| 445 | static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out, |
| 446 | const u8 *in, unsigned int nbytes) |
| 447 | { |
| 448 | struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm)); |
Herbert Xu | 476df25 | 2005-07-06 13:54:09 -0700 | [diff] [blame] | 449 | u8 *iv; |
| 450 | |
| 451 | iv = padlock_xcrypt_cbc(in, out, ctx->E, desc->info, |
| 452 | &ctx->cword.encrypt, nbytes / AES_BLOCK_SIZE); |
| 453 | memcpy(desc->info, iv, AES_BLOCK_SIZE); |
| 454 | |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 455 | return nbytes & ~(AES_BLOCK_SIZE - 1); |
| 456 | } |
| 457 | |
| 458 | static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out, |
| 459 | const u8 *in, unsigned int nbytes) |
| 460 | { |
| 461 | struct aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(desc->tfm)); |
| 462 | padlock_xcrypt_cbc(in, out, ctx->D, desc->info, &ctx->cword.decrypt, |
| 463 | nbytes / AES_BLOCK_SIZE); |
| 464 | return nbytes & ~(AES_BLOCK_SIZE - 1); |
| 465 | } |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | static struct crypto_alg aes_alg = { |
| 468 | .cra_name = "aes", |
Herbert Xu | c8a19c9 | 2005-11-05 18:06:26 +1100 | [diff] [blame^] | 469 | .cra_driver_name = "aes-padlock", |
| 470 | .cra_priority = 300, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 472 | .cra_blocksize = AES_BLOCK_SIZE, |
Herbert Xu | fbdae9f | 2005-07-06 13:53:29 -0700 | [diff] [blame] | 473 | .cra_ctxsize = sizeof(struct aes_ctx), |
Herbert Xu | 6789b2d | 2005-07-06 13:52:27 -0700 | [diff] [blame] | 474 | .cra_alignmask = PADLOCK_ALIGNMENT - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | .cra_module = THIS_MODULE, |
| 476 | .cra_list = LIST_HEAD_INIT(aes_alg.cra_list), |
| 477 | .cra_u = { |
| 478 | .cipher = { |
| 479 | .cia_min_keysize = AES_MIN_KEY_SIZE, |
| 480 | .cia_max_keysize = AES_MAX_KEY_SIZE, |
| 481 | .cia_setkey = aes_set_key, |
| 482 | .cia_encrypt = aes_encrypt, |
Herbert Xu | 28e8c3a | 2005-07-06 13:52:43 -0700 | [diff] [blame] | 483 | .cia_decrypt = aes_decrypt, |
| 484 | .cia_encrypt_ecb = aes_encrypt_ecb, |
| 485 | .cia_decrypt_ecb = aes_decrypt_ecb, |
| 486 | .cia_encrypt_cbc = aes_encrypt_cbc, |
| 487 | .cia_decrypt_cbc = aes_decrypt_cbc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | }; |
| 491 | |
| 492 | int __init padlock_init_aes(void) |
| 493 | { |
| 494 | printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n"); |
| 495 | |
| 496 | gen_tabs(); |
| 497 | return crypto_register_alg(&aes_alg); |
| 498 | } |
| 499 | |
| 500 | void __exit padlock_fini_aes(void) |
| 501 | { |
| 502 | crypto_unregister_alg(&aes_alg); |
| 503 | } |