1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
|
/*
* glue.c
*
* Written by Jari Ruusu, December 14 2006
*
* Copyright 2001-2006 by Jari Ruusu.
* Redistribution of this file is permitted under the GNU Public License.
*/
#include <linux/version.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/errno.h>
#if LINUX_VERSION_CODE >= 0x20600
# include <linux/bio.h>
# include <linux/blkdev.h>
#endif
#if LINUX_VERSION_CODE >= 0x20200
# include <linux/slab.h>
# include <linux/loop.h>
# include <asm/uaccess.h>
#else
# include <linux/malloc.h>
# include <asm/segment.h>
# include "patched-loop.h"
#endif
#if LINUX_VERSION_CODE >= 0x20400
# include <linux/spinlock.h>
#endif
#include <asm/byteorder.h>
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
# include <asm/processor.h>
#endif
#include "aes.h"
#include "md5.h"
#if LINUX_VERSION_CODE >= 0x20600
typedef sector_t TransferSector_t;
# define LoopInfo_t struct loop_info64
#else
typedef int TransferSector_t;
# define LoopInfo_t struct loop_info
#endif
#if !defined(cpu_to_le32)
# if defined(__BIG_ENDIAN)
# define cpu_to_le32(x) ({u_int32_t __x=(x);((u_int32_t)((((u_int32_t)(__x)&(u_int32_t)0x000000ffUL)<<24)|(((u_int32_t)(__x)&(u_int32_t)0x0000ff00UL)<<8)|(((u_int32_t)(__x)&(u_int32_t)0x00ff0000UL)>>8)|(((u_int32_t)(__x)&(u_int32_t)0xff000000UL)>>24)));})
# else
# define cpu_to_le32(x) ((u_int32_t)(x))
# endif
#endif
#if LINUX_VERSION_CODE < 0x20200
# define copy_from_user(t,f,s) (verify_area(VERIFY_READ,f,s)?(s):(memcpy_fromfs(t,f,s),0))
#endif
#if !defined(LOOP_MULTI_KEY_SETUP)
# define LOOP_MULTI_KEY_SETUP 0x4C4D
#endif
#if !defined(LOOP_MULTI_KEY_SETUP_V3)
# define LOOP_MULTI_KEY_SETUP_V3 0x4C4E
#endif
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
# define KEY_ALLOC_COUNT 128
#else
# define KEY_ALLOC_COUNT 64
#endif
typedef struct {
aes_context *keyPtr[KEY_ALLOC_COUNT];
unsigned keyMask;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
u_int32_t *partialMD5;
u_int32_t partialMD5buf[8];
rwlock_t rwlock;
unsigned reversed;
unsigned blocked;
struct timer_list timer;
#else
u_int32_t partialMD5[4];
#endif
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
u_int32_t padlock_cw_e;
u_int32_t padlock_cw_d;
#endif
} AESmultiKey;
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
/* This function allocates AES context structures at special address such */
/* that returned address % 16 == 8 . That way expanded encryption and */
/* decryption keys in AES context structure are always 16 byte aligned */
static void *specialAligned_kmalloc(size_t size, unsigned int flags)
{
void *pn, **ps;
pn = kmalloc(size + (16 + 8), flags);
if(!pn) return (void *)0;
ps = (void **)((((unsigned long)pn + 15) & ~((unsigned long)15)) + 8);
*(ps - 1) = pn;
return (void *)ps;
}
static void specialAligned_kfree(void *ps)
{
if(ps) kfree(*((void **)ps - 1));
}
# define specialAligned_ctxSize ((sizeof(aes_context) + 15) & ~15)
#else
# define specialAligned_kmalloc kmalloc
# define specialAligned_kfree kfree
# define specialAligned_ctxSize sizeof(aes_context)
#endif
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
static void keyScrubWork(AESmultiKey *m)
{
aes_context *a0, *a1;
u_int32_t *p;
int x, y, z;
z = m->keyMask + 1;
for(x = 0; x < z; x++) {
a0 = m->keyPtr[x];
a1 = m->keyPtr[x + z];
memcpy(a1, a0, sizeof(aes_context));
m->keyPtr[x] = a1;
m->keyPtr[x + z] = a0;
p = (u_int32_t *) a0;
y = sizeof(aes_context) / sizeof(u_int32_t);
while(y > 0) {
*p ^= 0xFFFFFFFF;
p++;
y--;
}
}
x = m->reversed; /* x is 0 or 4 */
m->reversed ^= 4;
y = m->reversed; /* y is 4 or 0 */
p = &m->partialMD5buf[x];
memcpy(&m->partialMD5buf[y], p, 16);
m->partialMD5 = &m->partialMD5buf[y];
p[0] ^= 0xFFFFFFFF;
p[1] ^= 0xFFFFFFFF;
p[2] ^= 0xFFFFFFFF;
p[3] ^= 0xFFFFFFFF;
/* try to flush dirty cache data to RAM */
#if !defined(CONFIG_XEN) && (defined(CONFIG_X86_64) || (defined(CONFIG_X86) && !defined(CONFIG_M386) && !defined(CONFIG_CPU_386)))
__asm__ __volatile__ ("wbinvd": : :"memory");
#else
mb();
#endif
}
/* called only from loop thread process context */
static void keyScrubThreadFn(AESmultiKey *m)
{
write_lock(&m->rwlock);
if(!m->blocked) keyScrubWork(m);
write_unlock(&m->rwlock);
}
#if defined(NEW_TIMER_VOID_PTR_PARAM)
# define KeyScrubTimerFnParamType void *
#else
# define KeyScrubTimerFnParamType unsigned long
#endif
static void keyScrubTimerFn(KeyScrubTimerFnParamType);
static void keyScrubTimerInit(struct loop_device *lo)
{
AESmultiKey *m;
unsigned long expire;
m = (AESmultiKey *)lo->key_data;
expire = jiffies + HZ;
init_timer(&m->timer);
m->timer.expires = expire;
m->timer.data = (KeyScrubTimerFnParamType)lo;
m->timer.function = keyScrubTimerFn;
add_timer(&m->timer);
}
/* called only from timer handler context */
static void keyScrubTimerFn(KeyScrubTimerFnParamType d)
{
struct loop_device *lo = (struct loop_device *)d;
extern void loop_add_keyscrub_fn(struct loop_device *, void (*)(void *), void *);
/* rw lock needs process context, so make loop thread do scrubbing */
loop_add_keyscrub_fn(lo, (void (*)(void*))keyScrubThreadFn, lo->key_data);
/* start timer again */
keyScrubTimerInit(lo);
}
#endif
static AESmultiKey *allocMultiKey(void)
{
AESmultiKey *m;
aes_context *a;
int x = 0, n;
m = (AESmultiKey *) kmalloc(sizeof(AESmultiKey), GFP_KERNEL);
if(!m) return 0;
memset(m, 0, sizeof(AESmultiKey));
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
m->partialMD5 = &m->partialMD5buf[0];
rwlock_init(&m->rwlock);
init_timer(&m->timer);
again:
#endif
n = PAGE_SIZE / specialAligned_ctxSize;
if(!n) n = 1;
a = (aes_context *) specialAligned_kmalloc(specialAligned_ctxSize * n, GFP_KERNEL);
if(!a) {
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
if(x) specialAligned_kfree(m->keyPtr[0]);
#endif
kfree(m);
return 0;
}
while((x < KEY_ALLOC_COUNT) && n) {
m->keyPtr[x] = a;
a = (aes_context *)((unsigned char *)a + specialAligned_ctxSize);
x++;
n--;
}
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
if(x < 2) goto again;
#endif
return m;
}
static void clearAndFreeMultiKey(AESmultiKey *m)
{
aes_context *a;
int x, n;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
/* stop scrub timer. loop thread was killed earlier */
del_timer_sync(&m->timer);
/* make sure allocated keys are in original order */
if(m->reversed) keyScrubWork(m);
#endif
n = PAGE_SIZE / specialAligned_ctxSize;
if(!n) n = 1;
x = 0;
while(x < KEY_ALLOC_COUNT) {
a = m->keyPtr[x];
if(!a) break;
memset(a, 0, specialAligned_ctxSize * n);
specialAligned_kfree(a);
x += n;
}
memset(m, 0, sizeof(AESmultiKey));
kfree(m);
}
static int multiKeySetup(struct loop_device *lo, unsigned char *k, int version3)
{
AESmultiKey *m;
aes_context *a;
int x, y, n, err = 0;
union {
u_int32_t w[16];
unsigned char b[64];
} un;
#if LINUX_VERSION_CODE >= 0x20200
if(lo->lo_key_owner != current->uid && !capable(CAP_SYS_ADMIN))
return -EPERM;
#endif
m = (AESmultiKey *)lo->key_data;
if(!m) return -ENXIO;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
/* temporarily prevent loop thread from messing with keys */
write_lock(&m->rwlock);
m->blocked = 1;
/* make sure allocated keys are in original order */
if(m->reversed) keyScrubWork(m);
write_unlock(&m->rwlock);
#endif
n = PAGE_SIZE / specialAligned_ctxSize;
if(!n) n = 1;
x = 0;
while(x < KEY_ALLOC_COUNT) {
if(!m->keyPtr[x]) {
a = (aes_context *) specialAligned_kmalloc(specialAligned_ctxSize * n, GFP_KERNEL);
if(!a) {
err = -ENOMEM;
goto error_out;
}
y = x;
while((y < (x + n)) && (y < KEY_ALLOC_COUNT)) {
m->keyPtr[y] = a;
a = (aes_context *)((unsigned char *)a + specialAligned_ctxSize);
y++;
}
}
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
if(x >= 64) {
x++;
continue;
}
#endif
if(copy_from_user(&un.b[0], k, 32)) {
err = -EFAULT;
goto error_out;
}
aes_set_key(m->keyPtr[x], &un.b[0], lo->lo_encrypt_key_size, 0);
k += 32;
x++;
}
m->partialMD5[0] = 0x67452301;
m->partialMD5[1] = 0xefcdab89;
m->partialMD5[2] = 0x98badcfe;
m->partialMD5[3] = 0x10325476;
if(version3) {
/* only first 128 bits of iv-key is used */
if(copy_from_user(&un.b[0], k, 16)) {
err = -EFAULT;
goto error_out;
}
#if defined(__BIG_ENDIAN)
un.w[0] = cpu_to_le32(un.w[0]);
un.w[1] = cpu_to_le32(un.w[1]);
un.w[2] = cpu_to_le32(un.w[2]);
un.w[3] = cpu_to_le32(un.w[3]);
#endif
memset(&un.b[16], 0, 48);
md5_transform_CPUbyteorder(&m->partialMD5[0], &un.w[0]);
lo->lo_flags |= 0x080000; /* multi-key-v3 (info exported to user space) */
}
m->keyMask = 0x3F; /* range 0...63 */
lo->lo_flags |= 0x100000; /* multi-key (info exported to user space) */
memset(&un.b[0], 0, 32);
error_out:
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
/* re-enable loop thread key scrubbing */
write_lock(&m->rwlock);
m->blocked = 0;
write_unlock(&m->rwlock);
#endif
return err;
}
void loop_compute_sector_iv(TransferSector_t devSect, u_int32_t *ivout)
{
if(sizeof(TransferSector_t) == 8) {
ivout[0] = cpu_to_le32(devSect);
ivout[1] = cpu_to_le32((u_int64_t)devSect>>32);
ivout[3] = ivout[2] = 0;
} else {
ivout[0] = cpu_to_le32(devSect);
ivout[3] = ivout[2] = ivout[1] = 0;
}
}
void loop_compute_md5_iv_v3(TransferSector_t devSect, u_int32_t *ivout, u_int32_t *data)
{
int x;
#if defined(__BIG_ENDIAN)
int y, e;
#endif
u_int32_t buf[16];
#if defined(__BIG_ENDIAN)
y = 7;
e = 16;
do {
if (!y) {
e = 12;
/* md5_transform_CPUbyteorder wants data in CPU byte order */
/* devSect is already in CPU byte order -- no need to convert */
if(sizeof(TransferSector_t) == 8) {
/* use only 56 bits of sector number */
buf[12] = devSect;
buf[13] = (((u_int64_t)devSect >> 32) & 0xFFFFFF) | 0x80000000;
} else {
/* 32 bits of sector number + 24 zero bits */
buf[12] = devSect;
buf[13] = 0x80000000;
}
/* 4024 bits == 31 * 128 bit plaintext blocks + 56 bits of sector number */
/* For version 3 on-disk format this really should be 4536 bits, but can't be */
/* changed without breaking compatibility. V3 uses MD5-with-wrong-length IV */
buf[14] = 4024;
buf[15] = 0;
}
x = 0;
do {
buf[x ] = cpu_to_le32(data[0]);
buf[x + 1] = cpu_to_le32(data[1]);
buf[x + 2] = cpu_to_le32(data[2]);
buf[x + 3] = cpu_to_le32(data[3]);
x += 4;
data += 4;
} while (x < e);
md5_transform_CPUbyteorder(&ivout[0], &buf[0]);
} while (--y >= 0);
ivout[0] = cpu_to_le32(ivout[0]);
ivout[1] = cpu_to_le32(ivout[1]);
ivout[2] = cpu_to_le32(ivout[2]);
ivout[3] = cpu_to_le32(ivout[3]);
#else
x = 6;
do {
md5_transform_CPUbyteorder(&ivout[0], data);
data += 16;
} while (--x >= 0);
memcpy(buf, data, 48);
/* md5_transform_CPUbyteorder wants data in CPU byte order */
/* devSect is already in CPU byte order -- no need to convert */
if(sizeof(TransferSector_t) == 8) {
/* use only 56 bits of sector number */
buf[12] = devSect;
buf[13] = (((u_int64_t)devSect >> 32) & 0xFFFFFF) | 0x80000000;
} else {
/* 32 bits of sector number + 24 zero bits */
buf[12] = devSect;
buf[13] = 0x80000000;
}
/* 4024 bits == 31 * 128 bit plaintext blocks + 56 bits of sector number */
/* For version 3 on-disk format this really should be 4536 bits, but can't be */
/* changed without breaking compatibility. V3 uses MD5-with-wrong-length IV */
buf[14] = 4024;
buf[15] = 0;
md5_transform_CPUbyteorder(&ivout[0], &buf[0]);
#endif
}
/* this function exists for compatibility with old external cipher modules */
void loop_compute_md5_iv(TransferSector_t devSect, u_int32_t *ivout, u_int32_t *data)
{
ivout[0] = 0x67452301;
ivout[1] = 0xefcdab89;
ivout[2] = 0x98badcfe;
ivout[3] = 0x10325476;
loop_compute_md5_iv_v3(devSect, ivout, data);
}
/* Some external modules do not know if md5_transform_CPUbyteorder() */
/* is asmlinkage or not, so here is C language wrapper for them. */
void md5_transform_CPUbyteorder_C(u_int32_t *hash, u_int32_t const *in)
{
md5_transform_CPUbyteorder(hash, in);
}
int transfer_aes(struct loop_device *lo, int cmd, char *raw_buf,
char *loop_buf, int size, TransferSector_t devSect)
{
aes_context *a;
AESmultiKey *m;
int x;
unsigned y;
u_int32_t iv[8];
if(!size || (size & 511)) {
return -EINVAL;
}
m = (AESmultiKey *)lo->key_data;
y = m->keyMask;
if(cmd == READ) {
while(size) {
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_lock(&m->rwlock);
#endif
a = m->keyPtr[((unsigned)devSect) & y];
if(y) {
memcpy(&iv[0], raw_buf, 16);
raw_buf += 16;
loop_buf += 16;
} else {
loop_compute_sector_iv(devSect, &iv[0]);
}
x = 15;
do {
memcpy(&iv[4], raw_buf, 16);
aes_decrypt(a, raw_buf, loop_buf);
*((u_int32_t *)(&loop_buf[ 0])) ^= iv[0];
*((u_int32_t *)(&loop_buf[ 4])) ^= iv[1];
*((u_int32_t *)(&loop_buf[ 8])) ^= iv[2];
*((u_int32_t *)(&loop_buf[12])) ^= iv[3];
if(y && !x) {
raw_buf -= 496;
loop_buf -= 496;
memcpy(&iv[4], &m->partialMD5[0], 16);
loop_compute_md5_iv_v3(devSect, &iv[4], (u_int32_t *)(&loop_buf[16]));
} else {
raw_buf += 16;
loop_buf += 16;
memcpy(&iv[0], raw_buf, 16);
}
aes_decrypt(a, raw_buf, loop_buf);
*((u_int32_t *)(&loop_buf[ 0])) ^= iv[4];
*((u_int32_t *)(&loop_buf[ 4])) ^= iv[5];
*((u_int32_t *)(&loop_buf[ 8])) ^= iv[6];
*((u_int32_t *)(&loop_buf[12])) ^= iv[7];
if(y && !x) {
raw_buf += 512;
loop_buf += 512;
} else {
raw_buf += 16;
loop_buf += 16;
}
} while(--x >= 0);
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_unlock(&m->rwlock);
#endif
#if LINUX_VERSION_CODE >= 0x20600
cond_resched();
#elif LINUX_VERSION_CODE >= 0x20400
if(current->need_resched) {set_current_state(TASK_RUNNING);schedule();}
#elif LINUX_VERSION_CODE >= 0x20200
if(current->need_resched) {current->state=TASK_RUNNING;schedule();}
#else
if(need_resched) schedule();
#endif
size -= 512;
devSect++;
}
} else {
while(size) {
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_lock(&m->rwlock);
#endif
a = m->keyPtr[((unsigned)devSect) & y];
if(y) {
#if LINUX_VERSION_CODE < 0x20400
/* on 2.2 and older kernels, real raw_buf may be doing */
/* writes at any time, so this needs to be stack buffer */
u_int32_t tmp_raw_buf[128];
char *TMP_RAW_BUF = (char *)(&tmp_raw_buf[0]);
#else
/* on 2.4 and later kernels, real raw_buf is not doing */
/* any writes now so it can be used as temp buffer */
# define TMP_RAW_BUF raw_buf
#endif
memcpy(TMP_RAW_BUF, loop_buf, 512);
memcpy(&iv[0], &m->partialMD5[0], 16);
loop_compute_md5_iv_v3(devSect, &iv[0], (u_int32_t *)(&TMP_RAW_BUF[16]));
x = 15;
do {
iv[0] ^= *((u_int32_t *)(&TMP_RAW_BUF[ 0]));
iv[1] ^= *((u_int32_t *)(&TMP_RAW_BUF[ 4]));
iv[2] ^= *((u_int32_t *)(&TMP_RAW_BUF[ 8]));
iv[3] ^= *((u_int32_t *)(&TMP_RAW_BUF[12]));
aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
memcpy(&iv[0], raw_buf, 16);
raw_buf += 16;
#if LINUX_VERSION_CODE < 0x20400
TMP_RAW_BUF += 16;
#endif
iv[0] ^= *((u_int32_t *)(&TMP_RAW_BUF[ 0]));
iv[1] ^= *((u_int32_t *)(&TMP_RAW_BUF[ 4]));
iv[2] ^= *((u_int32_t *)(&TMP_RAW_BUF[ 8]));
iv[3] ^= *((u_int32_t *)(&TMP_RAW_BUF[12]));
aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
memcpy(&iv[0], raw_buf, 16);
raw_buf += 16;
#if LINUX_VERSION_CODE < 0x20400
TMP_RAW_BUF += 16;
#endif
} while(--x >= 0);
loop_buf += 512;
} else {
loop_compute_sector_iv(devSect, &iv[0]);
x = 15;
do {
iv[0] ^= *((u_int32_t *)(&loop_buf[ 0]));
iv[1] ^= *((u_int32_t *)(&loop_buf[ 4]));
iv[2] ^= *((u_int32_t *)(&loop_buf[ 8]));
iv[3] ^= *((u_int32_t *)(&loop_buf[12]));
aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
memcpy(&iv[0], raw_buf, 16);
loop_buf += 16;
raw_buf += 16;
iv[0] ^= *((u_int32_t *)(&loop_buf[ 0]));
iv[1] ^= *((u_int32_t *)(&loop_buf[ 4]));
iv[2] ^= *((u_int32_t *)(&loop_buf[ 8]));
iv[3] ^= *((u_int32_t *)(&loop_buf[12]));
aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
memcpy(&iv[0], raw_buf, 16);
loop_buf += 16;
raw_buf += 16;
} while(--x >= 0);
}
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_unlock(&m->rwlock);
#endif
#if LINUX_VERSION_CODE >= 0x20600
cond_resched();
#elif LINUX_VERSION_CODE >= 0x20400
if(current->need_resched) {set_current_state(TASK_RUNNING);schedule();}
#elif LINUX_VERSION_CODE >= 0x20200
if(current->need_resched) {current->state=TASK_RUNNING;schedule();}
#else
if(need_resched) schedule();
#endif
size -= 512;
devSect++;
}
}
return(0);
}
int keySetup_aes(struct loop_device *lo, LoopInfo_t *info)
{
AESmultiKey *m;
union {
u_int32_t w[8]; /* needed for 4 byte alignment for b[] */
unsigned char b[32];
} un;
lo->key_data = m = allocMultiKey();
if(!m) return(-ENOMEM);
memcpy(&un.b[0], &info->lo_encrypt_key[0], 32);
aes_set_key(m->keyPtr[0], &un.b[0], info->lo_encrypt_key_size, 0);
memset(&info->lo_encrypt_key[0], 0, sizeof(info->lo_encrypt_key));
memset(&un.b[0], 0, 32);
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
switch(info->lo_encrypt_key_size) {
case 256: /* bits */
case 32: /* bytes */
/* 14 rounds, AES, software key gen, normal oper, encrypt, 256-bit key */
m->padlock_cw_e = 14 | (1<<7) | (2<<10);
/* 14 rounds, AES, software key gen, normal oper, decrypt, 256-bit key */
m->padlock_cw_d = 14 | (1<<7) | (1<<9) | (2<<10);
break;
case 192: /* bits */
case 24: /* bytes */
/* 12 rounds, AES, software key gen, normal oper, encrypt, 192-bit key */
m->padlock_cw_e = 12 | (1<<7) | (1<<10);
/* 12 rounds, AES, software key gen, normal oper, decrypt, 192-bit key */
m->padlock_cw_d = 12 | (1<<7) | (1<<9) | (1<<10);
break;
default:
/* 10 rounds, AES, software key gen, normal oper, encrypt, 128-bit key */
m->padlock_cw_e = 10 | (1<<7);
/* 10 rounds, AES, software key gen, normal oper, decrypt, 128-bit key */
m->padlock_cw_d = 10 | (1<<7) | (1<<9);
break;
}
#endif
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
keyScrubTimerInit(lo);
#endif
return(0);
}
int keyClean_aes(struct loop_device *lo)
{
if(lo->key_data) {
clearAndFreeMultiKey((AESmultiKey *)lo->key_data);
lo->key_data = 0;
}
return(0);
}
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
#if LINUX_VERSION_CODE < 0x20400
#error "this code does not support padlock crypto instructions on 2.2 or older kernels"
#endif
static __inline__ void padlock_flush_key_context(void)
{
__asm__ __volatile__("pushf; popf" : : : "cc");
}
static __inline__ void padlock_rep_xcryptcbc(void *cw, void *k, void *s, void *d, void *iv, unsigned long cnt)
{
__asm__ __volatile__(".byte 0xF3,0x0F,0xA7,0xD0"
: "+a" (iv), "+c" (cnt), "+S" (s), "+D" (d) /*output*/
: "b" (k), "d" (cw) /*input*/
: "cc", "memory" /*modified*/ );
}
typedef struct {
u_int32_t iv[4];
u_int32_t cw[4];
u_int32_t dummy1[4];
} Padlock_IV_CW;
static int transfer_padlock_aes(struct loop_device *lo, int cmd, char *raw_buf,
char *loop_buf, int size, TransferSector_t devSect)
{
aes_context *a;
AESmultiKey *m;
unsigned y;
Padlock_IV_CW ivcwua;
Padlock_IV_CW *ivcw;
/* ivcw->iv and ivcw->cw must have 16 byte alignment */
ivcw = (Padlock_IV_CW *)(((unsigned long)&ivcwua + 15) & ~((unsigned long)15));
if(!size || (size & 511) || (((unsigned long)raw_buf | (unsigned long)loop_buf) & 15)) {
return -EINVAL;
}
m = (AESmultiKey *)lo->key_data;
y = m->keyMask;
if(cmd == READ) {
while(size) {
padlock_flush_key_context();
ivcw->cw[0] = m->padlock_cw_d;
ivcw->cw[3] = ivcw->cw[2] = ivcw->cw[1] = 0;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_lock(&m->rwlock);
#endif
a = m->keyPtr[((unsigned)devSect) & y];
if(y) {
memcpy(&ivcw->iv[0], raw_buf, 16);
padlock_rep_xcryptcbc(&ivcw->cw[0], &a->aes_d_key[0], raw_buf + 16, loop_buf + 16, &ivcw->iv[0], 31);
memcpy(&ivcw->iv[0], &m->partialMD5[0], 16);
loop_compute_md5_iv_v3(devSect, &ivcw->iv[0], (u_int32_t *)(&loop_buf[16]));
padlock_rep_xcryptcbc(&ivcw->cw[0], &a->aes_d_key[0], raw_buf, loop_buf, &ivcw->iv[0], 1);
} else {
loop_compute_sector_iv(devSect, &ivcw->iv[0]);
padlock_rep_xcryptcbc(&ivcw->cw[0], &a->aes_d_key[0], raw_buf, loop_buf, &ivcw->iv[0], 32);
}
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_unlock(&m->rwlock);
#endif
#if LINUX_VERSION_CODE >= 0x20600
cond_resched();
#else
if(current->need_resched) {set_current_state(TASK_RUNNING);schedule();}
#endif
size -= 512;
raw_buf += 512;
loop_buf += 512;
devSect++;
}
} else {
while(size) {
padlock_flush_key_context();
ivcw->cw[0] = m->padlock_cw_e;
ivcw->cw[3] = ivcw->cw[2] = ivcw->cw[1] = 0;
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_lock(&m->rwlock);
#endif
a = m->keyPtr[((unsigned)devSect) & y];
if(y) {
memcpy(raw_buf, loop_buf, 512);
memcpy(&ivcw->iv[0], &m->partialMD5[0], 16);
loop_compute_md5_iv_v3(devSect, &ivcw->iv[0], (u_int32_t *)(&raw_buf[16]));
padlock_rep_xcryptcbc(&ivcw->cw[0], &a->aes_e_key[0], raw_buf, raw_buf, &ivcw->iv[0], 32);
} else {
loop_compute_sector_iv(devSect, &ivcw->iv[0]);
padlock_rep_xcryptcbc(&ivcw->cw[0], &a->aes_e_key[0], loop_buf, raw_buf, &ivcw->iv[0], 32);
}
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
read_unlock(&m->rwlock);
#endif
#if LINUX_VERSION_CODE >= 0x20600
cond_resched();
#else
if(current->need_resched) {set_current_state(TASK_RUNNING);schedule();}
#endif
size -= 512;
raw_buf += 512;
loop_buf += 512;
devSect++;
}
}
return(0);
}
#endif
int handleIoctl_aes(struct loop_device *lo, int cmd, unsigned long arg)
{
int err;
switch (cmd) {
case LOOP_MULTI_KEY_SETUP:
err = multiKeySetup(lo, (unsigned char *)arg, 0);
break;
case LOOP_MULTI_KEY_SETUP_V3:
err = multiKeySetup(lo, (unsigned char *)arg, 1);
break;
default:
err = -EINVAL;
}
return err;
}
#if LINUX_VERSION_CODE >= 0x20200
static struct loop_func_table funcs_aes = {
number: 16, /* 16 == AES */
transfer: (void *) transfer_aes,
init: (void *) keySetup_aes,
release: keyClean_aes,
ioctl: (void *) handleIoctl_aes
};
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
static struct loop_func_table funcs_padlock_aes = {
number: 16, /* 16 == AES */
transfer: (void *) transfer_padlock_aes,
init: (void *) keySetup_aes,
release: keyClean_aes,
ioctl: (void *) handleIoctl_aes
};
#endif
int init_module_aes(void)
{
#if defined(CONFIG_BLK_DEV_LOOP_PADLOCK) && (defined(CONFIG_X86) || defined(CONFIG_X86_64))
if((boot_cpu_data.x86 >= 6) && (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR)
&& (cpuid_eax(0xC0000000) >= 0xC0000001) && ((cpuid_edx(0xC0000001) & 0xC0) == 0xC0)) {
if(loop_register_transfer(&funcs_padlock_aes)) {
printk("loop: unable to register padlock AES transfer\n");
return -EIO;
}
printk("loop: padlock hardware AES enabled\n");
} else
#endif
if(loop_register_transfer(&funcs_aes)) {
printk("loop: unable to register AES transfer\n");
return -EIO;
}
#ifdef CONFIG_BLK_DEV_LOOP_KEYSCRUB
printk("loop: AES key scrubbing enabled\n");
#endif
return 0;
}
void cleanup_module_aes(void)
{
if(loop_unregister_transfer(funcs_aes.number)) {
printk("loop: unable to unregister AES transfer\n");
}
}
#endif
|