mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5168 lines
164 KiB

  1. diff -pruN linux-2.4.26/Documentation/Configure.help linux-2.4.26_crypto/Documentation/Configure.help
  2. --- linux-2.4.26/Documentation/Configure.help 2004-04-14 15:05:24.000000000 +0200
  3. +++ linux-2.4.26_crypto/Documentation/Configure.help 2004-05-16 14:57:30.000000000 +0200
  4. @@ -607,6 +607,11 @@ CONFIG_BLK_STATS
  5. If unsure, say N.
  6. +AES encrypted loop device support
  7. +CONFIG_BLK_DEV_LOOP_AES
  8. + If you want to use AES encryption algorithm to encrypt loop devices,
  9. + say Y here. If you don't know what to do here, say N.
  10. +
  11. ATA/IDE/MFM/RLL support
  12. CONFIG_IDE
  13. If you say Y here, your kernel will be able to manage low cost mass
  14. diff -pruN linux-2.4.26/drivers/block/Config.in linux-2.4.26_crypto/drivers/block/Config.in
  15. --- linux-2.4.26/drivers/block/Config.in 2003-11-28 19:26:19.000000000 +0100
  16. +++ linux-2.4.26_crypto/drivers/block/Config.in 2004-05-16 14:57:30.000000000 +0200
  17. @@ -41,6 +41,9 @@ dep_tristate 'Mylex DAC960/DAC1100 PCI R
  18. dep_tristate 'Micro Memory MM5415 Battery Backed RAM support (EXPERIMENTAL)' CONFIG_BLK_DEV_UMEM $CONFIG_PCI $CONFIG_EXPERIMENTAL
  19. tristate 'Loopback device support' CONFIG_BLK_DEV_LOOP
  20. +if [ "$CONFIG_BLK_DEV_LOOP" != "n" ]; then
  21. + bool ' AES encrypted loop device support' CONFIG_BLK_DEV_LOOP_AES
  22. +fi
  23. dep_tristate 'Network block device support' CONFIG_BLK_DEV_NBD $CONFIG_NET
  24. tristate 'RAM disk support' CONFIG_BLK_DEV_RAM
  25. diff -pruN linux-2.4.26/drivers/block/loop.c linux-2.4.26_crypto/drivers/block/loop.c
  26. --- linux-2.4.26/drivers/block/loop.c 2003-08-25 13:44:41.000000000 +0200
  27. +++ linux-2.4.26_crypto/drivers/block/loop.c 2004-05-16 14:57:30.000000000 +0200
  28. @@ -2,7 +2,7 @@
  29. * linux/drivers/block/loop.c
  30. *
  31. * Written by Theodore Ts'o, 3/29/93
  32. - *
  33. + *
  34. * Copyright 1993 by Theodore Ts'o. Redistribution of this file is
  35. * permitted under the GNU General Public License.
  36. *
  37. @@ -21,12 +21,12 @@
  38. * Loadable modules and other fixes by AK, 1998
  39. *
  40. * Make real block number available to downstream transfer functions, enables
  41. - * CBC (and relatives) mode encryption requiring unique IVs per data block.
  42. + * CBC (and relatives) mode encryption requiring unique IVs per data block.
  43. * Reed H. Petty, rhp@draper.net
  44. *
  45. * Maximum number of loop devices now dynamic via max_loop module parameter.
  46. * Russell Kroll <rkroll@exploits.org> 19990701
  47. - *
  48. + *
  49. * Maximum number of loop devices when compiled-in now selectable by passing
  50. * max_loop=<1-255> to the kernel on boot.
  51. * Erik I. Bols�, <eriki@himolde.no>, Oct 31, 1999
  52. @@ -39,20 +39,30 @@
  53. * Support up to 256 loop devices
  54. * Heinz Mauelshagen <mge@sistina.com>, Feb 2002
  55. *
  56. - * Still To Fix:
  57. - * - Advisory locking is ignored here.
  58. - * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  59. + * AES transfer added. IV is now passed as (512 byte) sector number.
  60. + * Jari Ruusu, May 18 2001
  61. + *
  62. + * External encryption module locking bug fixed.
  63. + * Ingo Rohloff <rohloff@in.tum.de>, June 21 2001
  64. + *
  65. + * Make device backed loop work with swap (pre-allocated buffers + queue rewrite).
  66. + * Jari Ruusu, September 2 2001
  67. *
  68. - * WARNING/FIXME:
  69. - * - The block number as IV passing to low level transfer functions is broken:
  70. - * it passes the underlying device's block number instead of the
  71. - * offset. This makes it change for a given block when the file is
  72. - * moved/restored/copied and also doesn't work over NFS.
  73. - * AV, Feb 12, 2000: we pass the logical block number now. It fixes the
  74. - * problem above. Encryption modules that used to rely on the old scheme
  75. - * should just call ->i_mapping->bmap() to calculate the physical block
  76. - * number.
  77. - */
  78. + * File backed code now uses file->f_op->read/write. Based on Andrew Morton's idea.
  79. + * Jari Ruusu, May 23 2002
  80. + *
  81. + * Backported struct loop_info64 ioctls from 2.6 kernels (64 bit offsets and
  82. + * 64 bit sizelimits). Added support for removing offset from IV computations.
  83. + * Jari Ruusu, September 21 2003
  84. + *
  85. + * Added support for MD5 IV computation and multi-key operation.
  86. + * Jari Ruusu, October 8 2003
  87. + *
  88. + *
  89. + * Still To Fix:
  90. + * - Advisory locking is ignored here.
  91. + * - Should use an own CAP_* category instead of CAP_SYS_ADMIN
  92. + */
  93. #include <linux/config.h>
  94. #include <linux/module.h>
  95. @@ -73,8 +83,11 @@
  96. #include <linux/slab.h>
  97. #include <asm/uaccess.h>
  98. +#include <asm/byteorder.h>
  99. #include <linux/loop.h>
  100. +#include "../misc/aes.h"
  101. +#include "../misc/md5.h"
  102. #define MAJOR_NR LOOP_MAJOR
  103. @@ -82,21 +95,25 @@ static int max_loop = 8;
  104. static struct loop_device *loop_dev;
  105. static int *loop_sizes;
  106. static int *loop_blksizes;
  107. +static int *loop_hardsizes;
  108. static devfs_handle_t devfs_handle; /* For the directory */
  109. +#if defined(__x86_64__) && defined(CONFIG_IA32_EMULATION)
  110. +# include <asm/ioctl32.h>
  111. +# define IOCTL32_COMPATIBLE_PTR ((void*)sys_ioctl)
  112. +#endif
  113. +
  114. /*
  115. * Transfer functions
  116. */
  117. static int transfer_none(struct loop_device *lo, int cmd, char *raw_buf,
  118. char *loop_buf, int size, int real_block)
  119. {
  120. - if (raw_buf != loop_buf) {
  121. - if (cmd == READ)
  122. - memcpy(loop_buf, raw_buf, size);
  123. - else
  124. - memcpy(raw_buf, loop_buf, size);
  125. - }
  126. + /* this code is only called from file backed loop */
  127. + /* and that code expects this function to be no-op */
  128. + if (current->need_resched)
  129. + {set_current_state(TASK_RUNNING);schedule();}
  130. return 0;
  131. }
  132. @@ -118,12 +135,13 @@ static int transfer_xor(struct loop_devi
  133. keysize = lo->lo_encrypt_key_size;
  134. for (i = 0; i < size; i++)
  135. *out++ = *in++ ^ key[(i & 511) % keysize];
  136. + if (current->need_resched)
  137. + {set_current_state(TASK_RUNNING);schedule();}
  138. return 0;
  139. }
  140. static int none_status(struct loop_device *lo, struct loop_info *info)
  141. {
  142. - lo->lo_flags |= LO_FLAGS_BH_REMAP;
  143. return 0;
  144. }
  145. @@ -134,336 +152,751 @@ static int xor_status(struct loop_device
  146. return 0;
  147. }
  148. -struct loop_func_table none_funcs = {
  149. +struct loop_func_table none_funcs = {
  150. number: LO_CRYPT_NONE,
  151. transfer: transfer_none,
  152. init: none_status,
  153. };
  154. -struct loop_func_table xor_funcs = {
  155. +struct loop_func_table xor_funcs = {
  156. number: LO_CRYPT_XOR,
  157. transfer: transfer_xor,
  158. - init: xor_status
  159. + init: xor_status,
  160. };
  161. -/* xfer_funcs[0] is special - its release function is never called */
  162. -struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  163. - &none_funcs,
  164. - &xor_funcs
  165. -};
  166. -
  167. -#define MAX_DISK_SIZE 1024*1024*1024
  168. -
  169. -static int compute_loop_size(struct loop_device *lo, struct dentry * lo_dentry, kdev_t lodev)
  170. -{
  171. - if (S_ISREG(lo_dentry->d_inode->i_mode))
  172. - return (lo_dentry->d_inode->i_size - lo->lo_offset) >> BLOCK_SIZE_BITS;
  173. - if (blk_size[MAJOR(lodev)])
  174. - return blk_size[MAJOR(lodev)][MINOR(lodev)] -
  175. - (lo->lo_offset >> BLOCK_SIZE_BITS);
  176. - return MAX_DISK_SIZE;
  177. -}
  178. +#if CONFIG_BLK_DEV_LOOP_AES
  179. +typedef struct {
  180. + aes_context *keyPtr[64];
  181. + unsigned keyMask;
  182. +} AESmultiKey;
  183. +
  184. +static AESmultiKey *allocMultiKey(void)
  185. +{
  186. + AESmultiKey *m;
  187. + aes_context *a;
  188. + int x, n;
  189. +
  190. + m = (AESmultiKey *) kmalloc(sizeof(AESmultiKey), GFP_KERNEL);
  191. + if(!m) return 0;
  192. + memset(m, 0, sizeof(AESmultiKey));
  193. +
  194. + n = PAGE_SIZE / sizeof(aes_context);
  195. + if(!n) n = 1;
  196. +
  197. + a = (aes_context *) kmalloc(sizeof(aes_context) * n, GFP_KERNEL);
  198. + if(!a) {
  199. + kfree(m);
  200. + return 0;
  201. + }
  202. +
  203. + x = 0;
  204. + while((x < 64) && n) {
  205. + m->keyPtr[x] = a;
  206. + a++;
  207. + x++;
  208. + n--;
  209. + }
  210. + return m;
  211. +}
  212. +
  213. +static void clearAndFreeMultiKey(AESmultiKey *m)
  214. +{
  215. + aes_context *a;
  216. + int x, n;
  217. +
  218. + n = PAGE_SIZE / sizeof(aes_context);
  219. + if(!n) n = 1;
  220. +
  221. + x = 0;
  222. + while(x < 64) {
  223. + a = m->keyPtr[x];
  224. + if(!a) break;
  225. + memset(a, 0, sizeof(aes_context) * n);
  226. + kfree(a);
  227. + x += n;
  228. + }
  229. +
  230. + kfree(m);
  231. +}
  232. +
  233. +static int multiKeySetup(struct loop_device *lo, unsigned char *k)
  234. +{
  235. + AESmultiKey *m;
  236. + aes_context *a;
  237. + int x, y, n;
  238. + union {
  239. + u_int32_t w[8]; /* needed for 4 byte alignment for b[] */
  240. + unsigned char b[32];
  241. + } un;
  242. +
  243. + if(lo->lo_key_owner != current->uid && !capable(CAP_SYS_ADMIN))
  244. + return -EPERM;
  245. +
  246. + m = (AESmultiKey *)lo->key_data;
  247. + if(!m) return -ENXIO;
  248. +
  249. + n = PAGE_SIZE / sizeof(aes_context);
  250. + if(!n) n = 1;
  251. +
  252. + x = 0;
  253. + while(x < 64) {
  254. + if(!m->keyPtr[x]) {
  255. + a = (aes_context *) kmalloc(sizeof(aes_context) * n, GFP_KERNEL);
  256. + if(!a) return -ENOMEM;
  257. + y = x;
  258. + while((y < (x + n)) && (y < 64)) {
  259. + m->keyPtr[y] = a;
  260. + a++;
  261. + y++;
  262. + }
  263. + }
  264. + if(copy_from_user(&un.b[0], k, 32)) return -EFAULT;
  265. + aes_set_key(m->keyPtr[x], &un.b[0], lo->lo_encrypt_key_size, 0);
  266. + k += 32;
  267. + x++;
  268. + }
  269. + m->keyMask = 0x3F; /* range 0...63 */
  270. + lo->lo_flags |= 0x100000; /* multi-key (info exported to user space) */
  271. + memset(&un.b[0], 0, 32);
  272. + return 0;
  273. +}
  274. +
  275. +void loop_compute_sector_iv(int devSect, u_int32_t *ivout)
  276. +{
  277. + ivout[0] = cpu_to_le32(devSect);
  278. + ivout[3] = ivout[2] = ivout[1] = 0;
  279. +}
  280. +
  281. +void loop_compute_md5_iv(int devSect, u_int32_t *ivout, u_int32_t *data)
  282. +{
  283. + int x;
  284. +#if defined(__BIG_ENDIAN)
  285. + int y, e;
  286. +#endif
  287. + u_int32_t buf[16];
  288. -static void figure_loop_size(struct loop_device *lo)
  289. -{
  290. - loop_sizes[lo->lo_number] = compute_loop_size(lo,
  291. - lo->lo_backing_file->f_dentry,
  292. - lo->lo_device);
  293. + ivout[0] = 0x67452301;
  294. + ivout[1] = 0xefcdab89;
  295. + ivout[2] = 0x98badcfe;
  296. + ivout[3] = 0x10325476;
  297. +
  298. +#if defined(__BIG_ENDIAN)
  299. + y = 7;
  300. + e = 16;
  301. + do {
  302. + if (!y) {
  303. + e = 12;
  304. + /* md5_transform_CPUbyteorder wants data in CPU byte order */
  305. + /* devSect is already in CPU byte order -- no need to convert */
  306. + /* 32 bits of sector number + 24 zero bits */
  307. + buf[12] = devSect;
  308. + buf[13] = 0x80000000;
  309. + /* 4024 bits == 31 * 128 bit plaintext blocks + 56 bits of sector number */
  310. + buf[14] = 4024;
  311. + buf[15] = 0;
  312. + }
  313. + x = 0;
  314. + do {
  315. + buf[x ] = cpu_to_le32(data[0]);
  316. + buf[x + 1] = cpu_to_le32(data[1]);
  317. + buf[x + 2] = cpu_to_le32(data[2]);
  318. + buf[x + 3] = cpu_to_le32(data[3]);
  319. + x += 4;
  320. + data += 4;
  321. + } while (x < e);
  322. + md5_transform_CPUbyteorder(&ivout[0], &buf[0]);
  323. + } while (--y >= 0);
  324. + ivout[0] = cpu_to_le32(ivout[0]);
  325. + ivout[1] = cpu_to_le32(ivout[1]);
  326. + ivout[2] = cpu_to_le32(ivout[2]);
  327. + ivout[3] = cpu_to_le32(ivout[3]);
  328. +#else
  329. + x = 6;
  330. + do {
  331. + md5_transform_CPUbyteorder(&ivout[0], data);
  332. + data += 16;
  333. + } while (--x >= 0);
  334. + memcpy(buf, data, 48);
  335. + /* md5_transform_CPUbyteorder wants data in CPU byte order */
  336. + /* devSect is already in CPU byte order -- no need to convert */
  337. + /* 32 bits of sector number + 24 zero bits */
  338. + buf[12] = devSect;
  339. + buf[13] = 0x80000000;
  340. + /* 4024 bits == 31 * 128 bit plaintext blocks + 56 bits of sector number */
  341. + buf[14] = 4024;
  342. + buf[15] = 0;
  343. + md5_transform_CPUbyteorder(&ivout[0], &buf[0]);
  344. +#endif
  345. }
  346. -static int lo_send(struct loop_device *lo, struct buffer_head *bh, int bsize,
  347. - loff_t pos)
  348. +static int transfer_aes(struct loop_device *lo, int cmd, char *raw_buf,
  349. + char *loop_buf, int size, int devSect)
  350. {
  351. - struct file *file = lo->lo_backing_file; /* kudos to NFsckingS */
  352. - struct address_space *mapping = file->f_dentry->d_inode->i_mapping;
  353. - struct address_space_operations *aops = mapping->a_ops;
  354. - struct page *page;
  355. - char *kaddr, *data;
  356. - unsigned long index;
  357. - unsigned size, offset;
  358. - int len;
  359. + aes_context *a;
  360. + AESmultiKey *m;
  361. + int x;
  362. + unsigned y;
  363. + u_int32_t iv[8];
  364. +
  365. + if(!size || (size & 511)) {
  366. + return -EINVAL;
  367. + }
  368. + m = (AESmultiKey *)lo->key_data;
  369. + y = m->keyMask;
  370. + if(cmd == READ) {
  371. + while(size) {
  372. + a = m->keyPtr[((unsigned)devSect) & y];
  373. + if(y) {
  374. + memcpy(&iv[0], raw_buf, 16);
  375. + raw_buf += 16;
  376. + loop_buf += 16;
  377. + } else {
  378. + loop_compute_sector_iv(devSect, &iv[0]);
  379. + }
  380. + x = 15;
  381. + do {
  382. + memcpy(&iv[4], raw_buf, 16);
  383. + aes_decrypt(a, raw_buf, loop_buf);
  384. + *((u_int32_t *)(&loop_buf[ 0])) ^= iv[0];
  385. + *((u_int32_t *)(&loop_buf[ 4])) ^= iv[1];
  386. + *((u_int32_t *)(&loop_buf[ 8])) ^= iv[2];
  387. + *((u_int32_t *)(&loop_buf[12])) ^= iv[3];
  388. + if(y && !x) {
  389. + raw_buf -= 496;
  390. + loop_buf -= 496;
  391. + loop_compute_md5_iv(devSect, &iv[4], (u_int32_t *)(&loop_buf[16]));
  392. + } else {
  393. + raw_buf += 16;
  394. + loop_buf += 16;
  395. + memcpy(&iv[0], raw_buf, 16);
  396. + }
  397. + aes_decrypt(a, raw_buf, loop_buf);
  398. + *((u_int32_t *)(&loop_buf[ 0])) ^= iv[4];
  399. + *((u_int32_t *)(&loop_buf[ 4])) ^= iv[5];
  400. + *((u_int32_t *)(&loop_buf[ 8])) ^= iv[6];
  401. + *((u_int32_t *)(&loop_buf[12])) ^= iv[7];
  402. + if(y && !x) {
  403. + raw_buf += 512;
  404. + loop_buf += 512;
  405. + } else {
  406. + raw_buf += 16;
  407. + loop_buf += 16;
  408. + }
  409. + } while(--x >= 0);
  410. + if(current->need_resched) {set_current_state(TASK_RUNNING);schedule();}
  411. + size -= 512;
  412. + devSect++;
  413. + }
  414. + } else {
  415. + while(size) {
  416. + a = m->keyPtr[((unsigned)devSect) & y];
  417. + if(y) {
  418. + memcpy(raw_buf, loop_buf, 512);
  419. + loop_compute_md5_iv(devSect, &iv[0], (u_int32_t *)(&raw_buf[16]));
  420. + x = 15;
  421. + do {
  422. + iv[0] ^= *((u_int32_t *)(&raw_buf[ 0]));
  423. + iv[1] ^= *((u_int32_t *)(&raw_buf[ 4]));
  424. + iv[2] ^= *((u_int32_t *)(&raw_buf[ 8]));
  425. + iv[3] ^= *((u_int32_t *)(&raw_buf[12]));
  426. + aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
  427. + memcpy(&iv[0], raw_buf, 16);
  428. + raw_buf += 16;
  429. + iv[0] ^= *((u_int32_t *)(&raw_buf[ 0]));
  430. + iv[1] ^= *((u_int32_t *)(&raw_buf[ 4]));
  431. + iv[2] ^= *((u_int32_t *)(&raw_buf[ 8]));
  432. + iv[3] ^= *((u_int32_t *)(&raw_buf[12]));
  433. + aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
  434. + memcpy(&iv[0], raw_buf, 16);
  435. + raw_buf += 16;
  436. + } while(--x >= 0);
  437. + loop_buf += 512;
  438. + } else {
  439. + loop_compute_sector_iv(devSect, &iv[0]);
  440. + x = 15;
  441. + do {
  442. + iv[0] ^= *((u_int32_t *)(&loop_buf[ 0]));
  443. + iv[1] ^= *((u_int32_t *)(&loop_buf[ 4]));
  444. + iv[2] ^= *((u_int32_t *)(&loop_buf[ 8]));
  445. + iv[3] ^= *((u_int32_t *)(&loop_buf[12]));
  446. + aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
  447. + memcpy(&iv[0], raw_buf, 16);
  448. + loop_buf += 16;
  449. + raw_buf += 16;
  450. + iv[0] ^= *((u_int32_t *)(&loop_buf[ 0]));
  451. + iv[1] ^= *((u_int32_t *)(&loop_buf[ 4]));
  452. + iv[2] ^= *((u_int32_t *)(&loop_buf[ 8]));
  453. + iv[3] ^= *((u_int32_t *)(&loop_buf[12]));
  454. + aes_encrypt(a, (unsigned char *)(&iv[0]), raw_buf);
  455. + memcpy(&iv[0], raw_buf, 16);
  456. + loop_buf += 16;
  457. + raw_buf += 16;
  458. + } while(--x >= 0);
  459. + }
  460. + if(current->need_resched) {set_current_state(TASK_RUNNING);schedule();}
  461. + size -= 512;
  462. + devSect++;
  463. + }
  464. + }
  465. + return(0);
  466. +}
  467. +
  468. +static int keySetup_aes(struct loop_device *lo, struct loop_info *info)
  469. +{
  470. + AESmultiKey *m;
  471. + union {
  472. + u_int32_t w[8]; /* needed for 4 byte alignment for b[] */
  473. + unsigned char b[32];
  474. + } un;
  475. +
  476. + lo->key_data = m = allocMultiKey();
  477. + if(!m) return(-ENOMEM);
  478. + memcpy(&un.b[0], &info->lo_encrypt_key[0], 32);
  479. + aes_set_key(m->keyPtr[0], &un.b[0], info->lo_encrypt_key_size, 0);
  480. + memset(&info->lo_encrypt_key[0], 0, sizeof(info->lo_encrypt_key));
  481. + memset(&un.b[0], 0, 32);
  482. + return(0);
  483. +}
  484. +
  485. +static int keyClean_aes(struct loop_device *lo)
  486. +{
  487. + if(lo->key_data) {
  488. + clearAndFreeMultiKey((AESmultiKey *)lo->key_data);
  489. + lo->key_data = 0;
  490. + }
  491. + return(0);
  492. +}
  493. +
  494. +static int handleIoctl_aes(struct loop_device *lo, int cmd, unsigned long arg)
  495. +{
  496. + int err;
  497. +
  498. + switch (cmd) {
  499. + case LOOP_MULTI_KEY_SETUP:
  500. + err = multiKeySetup(lo, (unsigned char *)arg);
  501. + break;
  502. + default:
  503. + err = -EINVAL;
  504. + }
  505. + return err;
  506. +}
  507. +
  508. +static struct loop_func_table funcs_aes = {
  509. + number: 16, /* 16 == AES */
  510. + transfer: transfer_aes,
  511. + init: keySetup_aes,
  512. + release: keyClean_aes,
  513. + ioctl: handleIoctl_aes
  514. +};
  515. - down(&mapping->host->i_sem);
  516. - index = pos >> PAGE_CACHE_SHIFT;
  517. - offset = pos & (PAGE_CACHE_SIZE - 1);
  518. - len = bh->b_size;
  519. - data = bh->b_data;
  520. - while (len > 0) {
  521. - int IV = index * (PAGE_CACHE_SIZE/bsize) + offset/bsize;
  522. - int transfer_result;
  523. +EXPORT_SYMBOL(loop_compute_sector_iv);
  524. +EXPORT_SYMBOL(loop_compute_md5_iv);
  525. +#endif /* CONFIG_BLK_DEV_LOOP_AES */
  526. - size = PAGE_CACHE_SIZE - offset;
  527. - if (size > len)
  528. - size = len;
  529. +/* xfer_funcs[0] is special - its release function is never called */
  530. +struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
  531. + &none_funcs,
  532. + &xor_funcs,
  533. +#if CONFIG_BLK_DEV_LOOP_AES
  534. + [LO_CRYPT_AES] = &funcs_aes,
  535. +#endif
  536. +};
  537. - page = grab_cache_page(mapping, index);
  538. - if (!page)
  539. - goto fail;
  540. - kaddr = kmap(page);
  541. - if (aops->prepare_write(file, page, offset, offset+size))
  542. - goto unlock;
  543. - flush_dcache_page(page);
  544. - transfer_result = lo_do_transfer(lo, WRITE, kaddr + offset, data, size, IV);
  545. - if (transfer_result) {
  546. - /*
  547. - * The transfer failed, but we still write the data to
  548. - * keep prepare/commit calls balanced.
  549. - */
  550. - printk(KERN_ERR "loop: transfer error block %ld\n", index);
  551. - memset(kaddr + offset, 0, size);
  552. - }
  553. - if (aops->commit_write(file, page, offset, offset+size))
  554. - goto unlock;
  555. - if (transfer_result)
  556. - goto unlock;
  557. - kunmap(page);
  558. - data += size;
  559. - len -= size;
  560. - offset = 0;
  561. - index++;
  562. - pos += size;
  563. - UnlockPage(page);
  564. - page_cache_release(page);
  565. +/*
  566. + * First number of 'lo_prealloc' is the default number of RAM pages
  567. + * to pre-allocate for each device backed loop. Every (configured)
  568. + * device backed loop pre-allocates this amount of RAM pages unless
  569. + * later 'lo_prealloc' numbers provide an override. 'lo_prealloc'
  570. + * overrides are defined in pairs: loop_index,number_of_pages
  571. + */
  572. +static int lo_prealloc[9] = { 125, 999, 0, 999, 0, 999, 0, 999, 0 };
  573. +#define LO_PREALLOC_MIN 4 /* minimum user defined pre-allocated RAM pages */
  574. +#define LO_PREALLOC_MAX 512 /* maximum user defined pre-allocated RAM pages */
  575. +
  576. +#ifdef MODULE
  577. +MODULE_PARM(lo_prealloc, "1-9i");
  578. +MODULE_PARM_DESC(lo_prealloc, "Number of pre-allocated pages [,index,pages]...");
  579. +#else
  580. +static int __init lo_prealloc_setup(char *str)
  581. +{
  582. + int x, y, z;
  583. +
  584. + for (x = 0; x < (sizeof(lo_prealloc) / sizeof(int)); x++) {
  585. + z = get_option(&str, &y);
  586. + if (z > 0)
  587. + lo_prealloc[x] = y;
  588. + if (z < 2)
  589. + break;
  590. }
  591. - up(&mapping->host->i_sem);
  592. - return 0;
  593. -
  594. -unlock:
  595. - kunmap(page);
  596. - UnlockPage(page);
  597. - page_cache_release(page);
  598. -fail:
  599. - up(&mapping->host->i_sem);
  600. - return -1;
  601. + return 1;
  602. }
  603. +__setup("lo_prealloc=", lo_prealloc_setup);
  604. +#endif
  605. -struct lo_read_data {
  606. - struct loop_device *lo;
  607. - char *data;
  608. - int bsize;
  609. -};
  610. +/*
  611. + * This is loop helper thread nice value in range
  612. + * from 0 (low priority) to -20 (high priority).
  613. + */
  614. +#if defined(DEF_NICE) && defined(DEF_COUNTER)
  615. +static int lo_nice = -20; /* old scheduler default */
  616. +#else
  617. +static int lo_nice = -1; /* O(1) scheduler default */
  618. +#endif
  619. -static int lo_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
  620. +#ifdef MODULE
  621. +MODULE_PARM(lo_nice, "1i");
  622. +MODULE_PARM_DESC(lo_nice, "Loop thread scheduler nice (0 ... -20)");
  623. +#else
  624. +static int __init lo_nice_setup(char *str)
  625. {
  626. - char *kaddr;
  627. - unsigned long count = desc->count;
  628. - struct lo_read_data *p = (struct lo_read_data*)desc->buf;
  629. - struct loop_device *lo = p->lo;
  630. - int IV = page->index * (PAGE_CACHE_SIZE/p->bsize) + offset/p->bsize;
  631. + int y;
  632. - if (size > count)
  633. - size = count;
  634. -
  635. - kaddr = kmap(page);
  636. - if (lo_do_transfer(lo, READ, kaddr + offset, p->data, size, IV)) {
  637. - size = 0;
  638. - printk(KERN_ERR "loop: transfer error block %ld\n",page->index);
  639. - desc->error = -EINVAL;
  640. - }
  641. - kunmap(page);
  642. -
  643. - desc->count = count - size;
  644. - desc->written += size;
  645. - p->data += size;
  646. - return size;
  647. -}
  648. -
  649. -static int lo_receive(struct loop_device *lo, struct buffer_head *bh, int bsize,
  650. - loff_t pos)
  651. -{
  652. - struct lo_read_data cookie;
  653. - read_descriptor_t desc;
  654. - struct file *file;
  655. -
  656. - cookie.lo = lo;
  657. - cookie.data = bh->b_data;
  658. - cookie.bsize = bsize;
  659. - desc.written = 0;
  660. - desc.count = bh->b_size;
  661. - desc.buf = (char*)&cookie;
  662. - desc.error = 0;
  663. - spin_lock_irq(&lo->lo_lock);
  664. - file = lo->lo_backing_file;
  665. - spin_unlock_irq(&lo->lo_lock);
  666. - do_generic_file_read(file, &pos, &desc, lo_read_actor);
  667. - return desc.error;
  668. + if (get_option(&str, &y) == 1)
  669. + lo_nice = y;
  670. + return 1;
  671. }
  672. +__setup("lo_nice=", lo_nice_setup);
  673. +#endif
  674. -static inline int loop_get_bs(struct loop_device *lo)
  675. -{
  676. - int bs = 0;
  677. +typedef struct {
  678. + struct buffer_head **q0;
  679. + struct buffer_head **q1;
  680. + struct buffer_head **q2;
  681. + int x0;
  682. + int x1;
  683. + int x2;
  684. +} que_look_up_table;
  685. - if (blksize_size[MAJOR(lo->lo_device)])
  686. - bs = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
  687. - if (!bs)
  688. - bs = BLOCK_SIZE;
  689. +static void loop_prealloc_cleanup(struct loop_device *lo)
  690. +{
  691. + struct buffer_head *bh;
  692. - return bs;
  693. + while ((bh = lo->lo_bh_free)) {
  694. + __free_page(bh->b_page);
  695. + lo->lo_bh_free = bh->b_reqnext;
  696. + bh->b_reqnext = NULL;
  697. + kmem_cache_free(bh_cachep, bh);
  698. + }
  699. }
  700. -static inline unsigned long loop_get_iv(struct loop_device *lo,
  701. - unsigned long sector)
  702. +static int loop_prealloc_init(struct loop_device *lo, int y)
  703. {
  704. - int bs = loop_get_bs(lo);
  705. - unsigned long offset, IV;
  706. + struct buffer_head *bh;
  707. + int x;
  708. - IV = sector / (bs >> 9) + lo->lo_offset / bs;
  709. - offset = ((sector % (bs >> 9)) << 9) + lo->lo_offset % bs;
  710. - if (offset >= bs)
  711. - IV++;
  712. + if(!y) {
  713. + y = lo_prealloc[0];
  714. + for (x = 1; x < (sizeof(lo_prealloc) / sizeof(int)); x += 2) {
  715. + if (lo_prealloc[x + 1] && (lo->lo_number == lo_prealloc[x])) {
  716. + y = lo_prealloc[x + 1];
  717. + break;
  718. + }
  719. + }
  720. + }
  721. + lo->lo_bh_flsh = (y * 3) / 4;
  722. - return IV;
  723. + for (x = 0; x < y; x++) {
  724. + bh = kmem_cache_alloc(bh_cachep, SLAB_KERNEL);
  725. + if (!bh) {
  726. + loop_prealloc_cleanup(lo);
  727. + return 1;
  728. + }
  729. + bh->b_page = alloc_page(GFP_KERNEL);
  730. + if (!bh->b_page) {
  731. + bh->b_reqnext = NULL;
  732. + kmem_cache_free(bh_cachep, bh);
  733. + loop_prealloc_cleanup(lo);
  734. + return 1;
  735. + }
  736. + bh->b_reqnext = lo->lo_bh_free;
  737. + lo->lo_bh_free = bh;
  738. + }
  739. + return 0;
  740. }
  741. -static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw)
  742. +static void loop_add_queue_last(struct loop_device *lo, struct buffer_head *bh, struct buffer_head **q)
  743. {
  744. - loff_t pos;
  745. - int ret;
  746. + unsigned long flags;
  747. - pos = ((loff_t) bh->b_rsector << 9) + lo->lo_offset;
  748. + spin_lock_irqsave(&lo->lo_lock, flags);
  749. + if (*q) {
  750. + bh->b_reqnext = (*q)->b_reqnext;
  751. + (*q)->b_reqnext = bh;
  752. + } else {
  753. + bh->b_reqnext = bh;
  754. + }
  755. + *q = bh;
  756. + spin_unlock_irqrestore(&lo->lo_lock, flags);
  757. - if (rw == WRITE)
  758. - ret = lo_send(lo, bh, loop_get_bs(lo), pos);
  759. - else
  760. - ret = lo_receive(lo, bh, loop_get_bs(lo), pos);
  761. + if (waitqueue_active(&lo->lo_bh_wait))
  762. + wake_up_interruptible(&lo->lo_bh_wait);
  763. +}
  764. - return ret;
  765. +static void loop_add_queue_first(struct loop_device *lo, struct buffer_head *bh, struct buffer_head **q)
  766. +{
  767. + spin_lock_irq(&lo->lo_lock);
  768. + if (*q) {
  769. + bh->b_reqnext = (*q)->b_reqnext;
  770. + (*q)->b_reqnext = bh;
  771. + } else {
  772. + bh->b_reqnext = bh;
  773. + *q = bh;
  774. + }
  775. + spin_unlock_irq(&lo->lo_lock);
  776. }
  777. -static void loop_end_io_transfer(struct buffer_head *bh, int uptodate);
  778. -static void loop_put_buffer(struct buffer_head *bh)
  779. +static struct buffer_head *loop_get_bh(struct loop_device *lo, int *list_nr,
  780. + que_look_up_table *qt)
  781. {
  782. - /*
  783. - * check b_end_io, may just be a remapped bh and not an allocated one
  784. - */
  785. - if (bh && bh->b_end_io == loop_end_io_transfer) {
  786. - __free_page(bh->b_page);
  787. - kmem_cache_free(bh_cachep, bh);
  788. + struct buffer_head *bh = NULL, *last;
  789. +
  790. + spin_lock_irq(&lo->lo_lock);
  791. + if ((last = *qt->q0)) {
  792. + bh = last->b_reqnext;
  793. + if (bh == last)
  794. + *qt->q0 = NULL;
  795. + else
  796. + last->b_reqnext = bh->b_reqnext;
  797. + bh->b_reqnext = NULL;
  798. + *list_nr = qt->x0;
  799. + } else if ((last = *qt->q1)) {
  800. + bh = last->b_reqnext;
  801. + if (bh == last)
  802. + *qt->q1 = NULL;
  803. + else
  804. + last->b_reqnext = bh->b_reqnext;
  805. + bh->b_reqnext = NULL;
  806. + *list_nr = qt->x1;
  807. + } else if ((last = *qt->q2)) {
  808. + bh = last->b_reqnext;
  809. + if (bh == last)
  810. + *qt->q2 = NULL;
  811. + else
  812. + last->b_reqnext = bh->b_reqnext;
  813. + bh->b_reqnext = NULL;
  814. + *list_nr = qt->x2;
  815. }
  816. + spin_unlock_irq(&lo->lo_lock);
  817. + return bh;
  818. }
  819. -/*
  820. - * Add buffer_head to back of pending list
  821. - */
  822. -static void loop_add_bh(struct loop_device *lo, struct buffer_head *bh)
  823. +static void loop_put_buffer(struct loop_device *lo, struct buffer_head *b)
  824. {
  825. unsigned long flags;
  826. + int wk;
  827. spin_lock_irqsave(&lo->lo_lock, flags);
  828. - if (lo->lo_bhtail) {
  829. - lo->lo_bhtail->b_reqnext = bh;
  830. - lo->lo_bhtail = bh;
  831. - } else
  832. - lo->lo_bh = lo->lo_bhtail = bh;
  833. + b->b_reqnext = lo->lo_bh_free;
  834. + lo->lo_bh_free = b;
  835. + wk = lo->lo_bh_need;
  836. spin_unlock_irqrestore(&lo->lo_lock, flags);
  837. - up(&lo->lo_bh_mutex);
  838. + if (wk && waitqueue_active(&lo->lo_bh_wait))
  839. + wake_up_interruptible(&lo->lo_bh_wait);
  840. }
  841. -/*
  842. - * Grab first pending buffer
  843. - */
  844. -static struct buffer_head *loop_get_bh(struct loop_device *lo)
  845. +static void loop_end_io_transfer_wr(struct buffer_head *bh, int uptodate)
  846. {
  847. - struct buffer_head *bh;
  848. -
  849. - spin_lock_irq(&lo->lo_lock);
  850. - if ((bh = lo->lo_bh)) {
  851. - if (bh == lo->lo_bhtail)
  852. - lo->lo_bhtail = NULL;
  853. - lo->lo_bh = bh->b_reqnext;
  854. - bh->b_reqnext = NULL;
  855. - }
  856. - spin_unlock_irq(&lo->lo_lock);
  857. + struct loop_device *lo = &loop_dev[MINOR(bh->b_dev)];
  858. + struct buffer_head *rbh = bh->b_private;
  859. - return bh;
  860. + rbh->b_reqnext = NULL;
  861. + rbh->b_end_io(rbh, uptodate);
  862. + loop_put_buffer(lo, bh);
  863. + if (atomic_dec_and_test(&lo->lo_pending))
  864. + wake_up_interruptible(&lo->lo_bh_wait);
  865. }
  866. -/*
  867. - * when buffer i/o has completed. if BH_Dirty is set, this was a WRITE
  868. - * and lo->transfer stuff has already been done. if not, it was a READ
  869. - * so queue it for the loop thread and let it do the transfer out of
  870. - * b_end_io context (we don't want to do decrypt of a page with irqs
  871. - * disabled)
  872. - */
  873. -static void loop_end_io_transfer(struct buffer_head *bh, int uptodate)
  874. +static void loop_end_io_transfer_rd(struct buffer_head *bh, int uptodate)
  875. {
  876. struct loop_device *lo = &loop_dev[MINOR(bh->b_dev)];
  877. - if (!uptodate || test_bit(BH_Dirty, &bh->b_state)) {
  878. - struct buffer_head *rbh = bh->b_private;
  879. -
  880. - rbh->b_end_io(rbh, uptodate);
  881. - if (atomic_dec_and_test(&lo->lo_pending))
  882. - up(&lo->lo_bh_mutex);
  883. - loop_put_buffer(bh);
  884. - } else
  885. - loop_add_bh(lo, bh);
  886. + if (!uptodate)
  887. + loop_end_io_transfer_wr(bh, uptodate);
  888. + else
  889. + loop_add_queue_last(lo, bh, &lo->lo_bh_que0);
  890. }
  891. static struct buffer_head *loop_get_buffer(struct loop_device *lo,
  892. - struct buffer_head *rbh)
  893. + struct buffer_head *rbh, int from_thread, int rw)
  894. {
  895. struct buffer_head *bh;
  896. + struct page *p;
  897. + unsigned long flags;
  898. - /*
  899. - * for xfer_funcs that can operate on the same bh, do that
  900. - */
  901. - if (lo->lo_flags & LO_FLAGS_BH_REMAP) {
  902. - bh = rbh;
  903. - goto out_bh;
  904. + spin_lock_irqsave(&lo->lo_lock, flags);
  905. + bh = lo->lo_bh_free;
  906. + if (bh) {
  907. + lo->lo_bh_free = bh->b_reqnext;
  908. + if (from_thread)
  909. + lo->lo_bh_need = 0;
  910. + } else {
  911. + if (from_thread)
  912. + lo->lo_bh_need = 1;
  913. }
  914. + spin_unlock_irqrestore(&lo->lo_lock, flags);
  915. + if (!bh)
  916. + return (struct buffer_head *)0;
  917. - do {
  918. - bh = kmem_cache_alloc(bh_cachep, SLAB_NOIO);
  919. - if (bh)
  920. - break;
  921. -
  922. - run_task_queue(&tq_disk);
  923. - set_current_state(TASK_INTERRUPTIBLE);
  924. - schedule_timeout(HZ);
  925. - } while (1);
  926. - memset(bh, 0, sizeof(*bh));
  927. + p = bh->b_page;
  928. + memset(bh, 0, sizeof(struct buffer_head));
  929. + bh->b_page = p;
  930. + bh->b_private = rbh;
  931. bh->b_size = rbh->b_size;
  932. bh->b_dev = rbh->b_rdev;
  933. + bh->b_rdev = lo->lo_device;
  934. bh->b_state = (1 << BH_Req) | (1 << BH_Mapped) | (1 << BH_Lock);
  935. + bh->b_data = page_address(bh->b_page);
  936. + bh->b_end_io = (rw == WRITE) ? loop_end_io_transfer_wr : loop_end_io_transfer_rd;
  937. + bh->b_rsector = rbh->b_rsector + lo->lo_offs_sec;
  938. + init_waitqueue_head(&bh->b_wait);
  939. +
  940. + return bh;
  941. +}
  942. +
  943. +static int figure_loop_size(struct loop_device *lo)
  944. +{
  945. + loff_t size, offs;
  946. + unsigned int x;
  947. + int err = 0;
  948. + kdev_t lodev = lo->lo_device;
  949. +
  950. + offs = lo->lo_offset;
  951. + if (S_ISREG(lo->lo_backing_file->f_dentry->d_inode->i_mode)) {
  952. + size = lo->lo_backing_file->f_dentry->d_inode->i_size;
  953. + } else {
  954. + offs &= ~((loff_t)511);
  955. + if (blk_size[MAJOR(lodev)])
  956. + size = (loff_t)(blk_size[MAJOR(lodev)][MINOR(lodev)]) << BLOCK_SIZE_BITS;
  957. + else
  958. + size = 1024*1024*1024; /* unknown size */
  959. + }
  960. + if ((offs > 0) && (offs < size)) {
  961. + size -= offs;
  962. + } else {
  963. + if (offs)
  964. + err = -EINVAL;
  965. + lo->lo_offset = 0;
  966. + lo->lo_offs_sec = lo->lo_iv_remove = 0;
  967. + }
  968. + if ((lo->lo_sizelimit > 0) && (lo->lo_sizelimit <= size)) {
  969. + size = lo->lo_sizelimit;
  970. + } else {
  971. + if (lo->lo_sizelimit)
  972. + err = -EINVAL;
  973. + lo->lo_sizelimit = 0;
  974. + }
  975. + size >>= BLOCK_SIZE_BITS;
  976. /*
  977. - * easy way out, although it does waste some memory for < PAGE_SIZE
  978. - * blocks... if highmem bounce buffering can get away with it,
  979. - * so can we :-)
  980. + * Unfortunately, if we want to do I/O on the device,
  981. + * the number of 1024-byte blocks has to fit into unsigned int
  982. */
  983. - do {
  984. - bh->b_page = alloc_page(GFP_NOIO);
  985. - if (bh->b_page)
  986. - break;
  987. + x = (unsigned int)size;
  988. + if ((loff_t)x != size) {
  989. + err = -EFBIG;
  990. + size = 0;
  991. + }
  992. - run_task_queue(&tq_disk);
  993. - set_current_state(TASK_INTERRUPTIBLE);
  994. - schedule_timeout(HZ);
  995. - } while (1);
  996. + loop_sizes[lo->lo_number] = size;
  997. + return err;
  998. +}
  999. - bh->b_data = page_address(bh->b_page);
  1000. - bh->b_end_io = loop_end_io_transfer;
  1001. - bh->b_private = rbh;
  1002. - init_waitqueue_head(&bh->b_wait);
  1003. +static int loop_file_io(struct file *file, char *buf, int size, loff_t *ppos, int w)
  1004. +{
  1005. + mm_segment_t fs;
  1006. + int x, y, z;
  1007. -out_bh:
  1008. - bh->b_rsector = rbh->b_rsector + (lo->lo_offset >> 9);
  1009. - spin_lock_irq(&lo->lo_lock);
  1010. - bh->b_rdev = lo->lo_device;
  1011. - spin_unlock_irq(&lo->lo_lock);
  1012. + y = 0;
  1013. + do {
  1014. + z = size - y;
  1015. + fs = get_fs();
  1016. + set_fs(get_ds());
  1017. + if (w) {
  1018. + x = file->f_op->write(file, buf + y, z, ppos);
  1019. + set_fs(fs);
  1020. + } else {
  1021. + x = file->f_op->read(file, buf + y, z, ppos);
  1022. + set_fs(fs);
  1023. + if (!x)
  1024. + return 1;
  1025. + }
  1026. + if (x < 0) {
  1027. + if ((x == -EAGAIN) || (x == -ENOMEM) || (x == -ERESTART) || (x == -EINTR)) {
  1028. + run_task_queue(&tq_disk);
  1029. + set_current_state(TASK_INTERRUPTIBLE);
  1030. + schedule_timeout(HZ / 2);
  1031. + continue;
  1032. + }
  1033. + return 1;
  1034. + }
  1035. + y += x;
  1036. + } while (y < size);
  1037. + return 0;
  1038. +}
  1039. - return bh;
  1040. +static int do_bh_filebacked(struct loop_device *lo, struct buffer_head *bh, int rw)
  1041. +{
  1042. + loff_t pos;
  1043. + struct file *file = lo->lo_backing_file;
  1044. + char *data, *buf;
  1045. + unsigned int size, len;
  1046. + unsigned long IV;
  1047. +
  1048. + pos = ((loff_t) bh->b_rsector << 9) + lo->lo_offset;
  1049. + buf = page_address(lo->lo_bh_free->b_page);
  1050. + len = bh->b_size;
  1051. + data = bh_kmap(bh);
  1052. + IV = bh->b_rsector;
  1053. + if (!lo->lo_iv_remove)
  1054. + IV += lo->lo_offs_sec;
  1055. + while (len > 0) {
  1056. + if (lo->lo_encrypt_type == LO_CRYPT_NONE) {
  1057. + /* this code relies that NONE transfer is a no-op */
  1058. + buf = data;
  1059. + }
  1060. + size = PAGE_SIZE;
  1061. + if (size > len)
  1062. + size = len;
  1063. + if (rw == WRITE) {
  1064. + if (lo_do_transfer(lo, WRITE, buf, data, size, IV)) {
  1065. + printk(KERN_ERR "loop%d: write transfer error, sector %lu\n", lo->lo_number, IV);
  1066. + goto kunmap_and_out;
  1067. + }
  1068. + if (loop_file_io(file, buf, size, &pos, 1)) {
  1069. + printk(KERN_ERR "loop%d: write i/o error, sector %lu\n", lo->lo_number, IV);
  1070. + goto kunmap_and_out;
  1071. + }
  1072. + } else {
  1073. + if (loop_file_io(file, buf, size, &pos, 0)) {
  1074. + printk(KERN_ERR "loop%d: read i/o error, sector %lu\n", lo->lo_number, IV);
  1075. + goto kunmap_and_out;
  1076. + }
  1077. + if (lo_do_transfer(lo, READ, buf, data, size, IV)) {
  1078. + printk(KERN_ERR "loop%d: read transfer error, sector %lu\n", lo->lo_number, IV);
  1079. + goto kunmap_and_out;
  1080. + }
  1081. + }
  1082. + data += size;
  1083. + len -= size;
  1084. + IV += size >> 9;
  1085. + }
  1086. + bh_kunmap(bh);
  1087. + return 0;
  1088. +
  1089. +kunmap_and_out:
  1090. + bh_kunmap(bh);
  1091. + return 1;
  1092. }
  1093. static int loop_make_request(request_queue_t *q, int rw, struct buffer_head *rbh)
  1094. {
  1095. - struct buffer_head *bh = NULL;
  1096. + struct buffer_head *bh;
  1097. struct loop_device *lo;
  1098. - unsigned long IV;
  1099. + char *md;
  1100. + set_current_state(TASK_RUNNING);
  1101. if (!buffer_locked(rbh))
  1102. BUG();
  1103. @@ -483,45 +916,55 @@ static int loop_make_request(request_que
  1104. } else if (rw == READA) {
  1105. rw = READ;
  1106. } else if (rw != READ) {
  1107. - printk(KERN_ERR "loop: unknown command (%d)\n", rw);
  1108. + printk(KERN_ERR "loop%d: unknown command (%d)\n", lo->lo_number, rw);
  1109. goto err;
  1110. }
  1111. - rbh = blk_queue_bounce(q, rw, rbh);
  1112. -
  1113. /*
  1114. * file backed, queue for loop_thread to handle
  1115. */
  1116. if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  1117. - /*
  1118. - * rbh locked at this point, noone else should clear
  1119. - * the dirty flag
  1120. - */
  1121. - if (rw == WRITE)
  1122. - set_bit(BH_Dirty, &rbh->b_state);
  1123. - loop_add_bh(lo, rbh);
  1124. + loop_add_queue_last(lo, rbh, (rw == WRITE) ? &lo->lo_bh_que1 : &lo->lo_bh_que0);
  1125. + return 0;
  1126. + }
  1127. +
  1128. + /*
  1129. + * device backed, just remap rdev & rsector for NONE transfer
  1130. + */
  1131. + if (lo->lo_encrypt_type == LO_CRYPT_NONE) {
  1132. + rbh->b_rsector += lo->lo_offs_sec;
  1133. + rbh->b_rdev = lo->lo_device;
  1134. + generic_make_request(rw, rbh);
  1135. + if (atomic_dec_and_test(&lo->lo_pending))
  1136. + wake_up_interruptible(&lo->lo_bh_wait);
  1137. return 0;
  1138. }
  1139. /*
  1140. - * piggy old buffer on original, and submit for I/O
  1141. + * device backed, start reads and writes now if buffer available
  1142. */
  1143. - bh = loop_get_buffer(lo, rbh);
  1144. - IV = loop_get_iv(lo, rbh->b_rsector);
  1145. + bh = loop_get_buffer(lo, rbh, 0, rw);
  1146. + if (!bh) {
  1147. + /* just queue request and let thread handle alloc later */
  1148. + loop_add_queue_last(lo, rbh, (rw == WRITE) ? &lo->lo_bh_que1 : &lo->lo_bh_que2);
  1149. + return 0;
  1150. + }
  1151. if (rw == WRITE) {
  1152. - set_bit(BH_Dirty, &bh->b_state);
  1153. - if (lo_do_transfer(lo, WRITE, bh->b_data, rbh->b_data,
  1154. - bh->b_size, IV))
  1155. + int trv;
  1156. + md = bh_kmap(rbh);
  1157. + trv = lo_do_transfer(lo, WRITE, bh->b_data, md, bh->b_size, bh->b_rsector - lo->lo_iv_remove);
  1158. + bh_kunmap(rbh);
  1159. + if (trv) {
  1160. + loop_put_buffer(lo, bh);
  1161. goto err;
  1162. + }
  1163. }
  1164. -
  1165. generic_make_request(rw, bh);
  1166. return 0;
  1167. err:
  1168. if (atomic_dec_and_test(&lo->lo_pending))
  1169. - up(&lo->lo_bh_mutex);
  1170. - loop_put_buffer(bh);
  1171. + wake_up_interruptible(&lo->lo_bh_wait);
  1172. out:
  1173. buffer_IO_error(rbh);
  1174. return 0;
  1175. @@ -530,30 +973,6 @@ inactive:
  1176. goto out;
  1177. }
  1178. -static inline void loop_handle_bh(struct loop_device *lo,struct buffer_head *bh)
  1179. -{
  1180. - int ret;
  1181. -
  1182. - /*
  1183. - * For block backed loop, we know this is a READ
  1184. - */
  1185. - if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  1186. - int rw = !!test_and_clear_bit(BH_Dirty, &bh->b_state);
  1187. -
  1188. - ret = do_bh_filebacked(lo, bh, rw);
  1189. - bh->b_end_io(bh, !ret);
  1190. - } else {
  1191. - struct buffer_head *rbh = bh->b_private;
  1192. - unsigned long IV = loop_get_iv(lo, rbh->b_rsector);
  1193. -
  1194. - ret = lo_do_transfer(lo, READ, bh->b_data, rbh->b_data,
  1195. - bh->b_size, IV);
  1196. -
  1197. - rbh->b_end_io(rbh, !ret);
  1198. - loop_put_buffer(bh);
  1199. - }
  1200. -}
  1201. -
  1202. /*
  1203. * worker thread that handles reads/writes to file backed loop devices,
  1204. * to avoid blocking in our make_request_fn. it also does loop decrypting
  1205. @@ -563,8 +982,20 @@ static inline void loop_handle_bh(struct
  1206. static int loop_thread(void *data)
  1207. {
  1208. struct loop_device *lo = data;
  1209. - struct buffer_head *bh;
  1210. + struct buffer_head *bh, *xbh;
  1211. + int x, rw, qi = 0, flushcnt = 0;
  1212. + wait_queue_t waitq;
  1213. + que_look_up_table qt[4] = {
  1214. + { &lo->lo_bh_que0, &lo->lo_bh_que1, &lo->lo_bh_que2, 0, 1, 2 },
  1215. + { &lo->lo_bh_que2, &lo->lo_bh_que0, &lo->lo_bh_que1, 2, 0, 1 },
  1216. + { &lo->lo_bh_que0, &lo->lo_bh_que2, &lo->lo_bh_que1, 0, 2, 1 },
  1217. + { &lo->lo_bh_que1, &lo->lo_bh_que0, &lo->lo_bh_que2, 1, 0, 2 }
  1218. + };
  1219. + char *md;
  1220. + static const struct rlimit loop_rlim_defaults[RLIM_NLIMITS] = INIT_RLIMITS;
  1221. + init_waitqueue_entry(&waitq, current);
  1222. + memcpy(&current->rlim[0], &loop_rlim_defaults[0], sizeof(current->rlim));
  1223. daemonize();
  1224. exit_files(current);
  1225. reparent_to_init();
  1226. @@ -576,12 +1007,30 @@ static int loop_thread(void *data)
  1227. flush_signals(current);
  1228. spin_unlock_irq(&current->sigmask_lock);
  1229. + if (lo_nice > 0)
  1230. + lo_nice = 0;
  1231. + if (lo_nice < -20)
  1232. + lo_nice = -20;
  1233. +#if defined(DEF_NICE) && defined(DEF_COUNTER)
  1234. + /* old scheduler syntax */
  1235. + current->policy = SCHED_OTHER;
  1236. + current->nice = lo_nice;
  1237. +#else
  1238. + /* O(1) scheduler syntax */
  1239. + set_user_nice(current, lo_nice);
  1240. +#endif
  1241. +
  1242. spin_lock_irq(&lo->lo_lock);
  1243. lo->lo_state = Lo_bound;
  1244. atomic_inc(&lo->lo_pending);
  1245. spin_unlock_irq(&lo->lo_lock);
  1246. current->flags |= PF_NOIO;
  1247. +#if defined(PF_NOFREEZE)
  1248. + current->flags |= PF_NOFREEZE;
  1249. +#elif defined(PF_IOTHREAD)
  1250. + current->flags |= PF_IOTHREAD;
  1251. +#endif
  1252. /*
  1253. * up sem, we are running
  1254. @@ -589,23 +1038,110 @@ static int loop_thread(void *data)
  1255. up(&lo->lo_sem);
  1256. for (;;) {
  1257. - down_interruptible(&lo->lo_bh_mutex);
  1258. + add_wait_queue(&lo->lo_bh_wait, &waitq);
  1259. + for (;;) {
  1260. + set_current_state(TASK_INTERRUPTIBLE);
  1261. + if (!atomic_read(&lo->lo_pending))
  1262. + break;
  1263. +
  1264. + x = 0;
  1265. + spin_lock_irq(&lo->lo_lock);
  1266. + if (lo->lo_bh_que0) {
  1267. + x = 1;
  1268. + } else if (lo->lo_bh_que1 || lo->lo_bh_que2) {
  1269. + /* file backed works too because lo->lo_bh_need == 0 */
  1270. + if (lo->lo_bh_free || !lo->lo_bh_need)
  1271. + x = 1;
  1272. + }
  1273. + spin_unlock_irq(&lo->lo_lock);
  1274. + if (x)
  1275. + break;
  1276. +
  1277. + schedule();
  1278. + }
  1279. + set_current_state(TASK_RUNNING);
  1280. + remove_wait_queue(&lo->lo_bh_wait, &waitq);
  1281. +
  1282. /*
  1283. - * could be upped because of tear-down, not because of
  1284. + * could be woken because of tear-down, not because of
  1285. * pending work
  1286. */
  1287. if (!atomic_read(&lo->lo_pending))
  1288. break;
  1289. - bh = loop_get_bh(lo);
  1290. - if (!bh) {
  1291. - printk("loop: missing bh\n");
  1292. + /*
  1293. + * read queues using alternating order to prevent starvation
  1294. + */
  1295. + bh = loop_get_bh(lo, &x, &qt[++qi & 3]);
  1296. + if (!bh)
  1297. + continue;
  1298. +
  1299. + /*
  1300. + * x list tag usage(buffer-allocated)
  1301. + * --- -------------- -----------------------
  1302. + * 0 lo->lo_bh_que0 dev-read(y) / file-read
  1303. + * 1 lo->lo_bh_que1 dev-write(n) / file-write
  1304. + * 2 lo->lo_bh_que2 dev-read(n)
  1305. + */
  1306. + rw = (x == 1) ? WRITE : READ;
  1307. + if ((x >= 1) && !(lo->lo_flags & LO_FLAGS_DO_BMAP)) {
  1308. + /* loop_make_request didn't allocate a buffer, do that now */
  1309. + xbh = loop_get_buffer(lo, bh, 1, rw);
  1310. + if (!xbh) {
  1311. + run_task_queue(&tq_disk);
  1312. + flushcnt = 0;
  1313. + loop_add_queue_first(lo, bh, (rw == WRITE) ? &lo->lo_bh_que1 : &lo->lo_bh_que2);
  1314. + /* lo->lo_bh_need should be 1 now, go back to sleep */
  1315. + continue;
  1316. + }
  1317. + if (rw == WRITE) {
  1318. + int trv;
  1319. + md = bh_kmap(bh);
  1320. + trv = lo_do_transfer(lo, WRITE, xbh->b_data, md, xbh->b_size, xbh->b_rsector - lo->lo_iv_remove);
  1321. + bh_kunmap(bh);
  1322. + if (trv) {
  1323. + loop_put_buffer(lo, xbh);
  1324. + buffer_IO_error(bh);
  1325. + atomic_dec(&lo->lo_pending);
  1326. + continue;
  1327. + }
  1328. + }
  1329. + generic_make_request(rw, xbh);
  1330. +
  1331. + /* start I/O if there are no more requests lacking buffers */
  1332. + x = 0;
  1333. + spin_lock_irq(&lo->lo_lock);
  1334. + if (!lo->lo_bh_que1 && !lo->lo_bh_que2)
  1335. + x = 1;
  1336. + spin_unlock_irq(&lo->lo_lock);
  1337. + if (x || (++flushcnt >= lo->lo_bh_flsh)) {
  1338. + run_task_queue(&tq_disk);
  1339. + flushcnt = 0;
  1340. + }
  1341. +
  1342. + /* request not completely processed yet */
  1343. continue;
  1344. }
  1345. - loop_handle_bh(lo, bh);
  1346. + if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  1347. + /* request is for file backed device */
  1348. + x = do_bh_filebacked(lo, bh, rw);
  1349. + bh->b_reqnext = NULL;
  1350. + bh->b_end_io(bh, !x);
  1351. + } else {
  1352. + /* device backed read has completed, do decrypt now */
  1353. + xbh = bh->b_private;
  1354. + /* must not use bh->b_rsector as IV, as it may be modified by LVM at this point */
  1355. + /* instead, recompute IV from original request */
  1356. + md = bh_kmap(xbh);
  1357. + x = lo_do_transfer(lo, READ, bh->b_data, md, bh->b_size, xbh->b_rsector + lo->lo_offs_sec - lo->lo_iv_remove);
  1358. + bh_kunmap(xbh);
  1359. + xbh->b_reqnext = NULL;
  1360. + xbh->b_end_io(xbh, !x);
  1361. + loop_put_buffer(lo, bh);
  1362. + }
  1363. /*
  1364. - * upped both for pending work and tear-down, lo_pending
  1365. + * woken both for pending work and tear-down, lo_pending
  1366. * will hit zero then
  1367. */
  1368. if (atomic_dec_and_test(&lo->lo_pending))
  1369. @@ -616,15 +1152,34 @@ static int loop_thread(void *data)
  1370. return 0;
  1371. }
  1372. +static void loop_set_softblksz(struct loop_device *lo, kdev_t dev)
  1373. +{
  1374. + int bs = 0, x;
  1375. +
  1376. + if (blksize_size[MAJOR(lo->lo_device)])
  1377. + bs = blksize_size[MAJOR(lo->lo_device)][MINOR(lo->lo_device)];
  1378. + if (!bs)
  1379. + bs = BLOCK_SIZE;
  1380. + if (lo->lo_flags & LO_FLAGS_DO_BMAP) {
  1381. + x = loop_sizes[lo->lo_number];
  1382. + if ((bs == 8192) && (x & 7))
  1383. + bs = 4096;
  1384. + if ((bs == 4096) && (x & 3))
  1385. + bs = 2048;
  1386. + if ((bs == 2048) && (x & 1))
  1387. + bs = 1024;
  1388. + }
  1389. + set_blocksize(dev, bs);
  1390. +}
  1391. +
  1392. static int loop_set_fd(struct loop_device *lo, struct file *lo_file, kdev_t dev,
  1393. unsigned int arg)
  1394. {
  1395. struct file *file;
  1396. struct inode *inode;
  1397. kdev_t lo_device;
  1398. - int lo_flags = 0;
  1399. + int lo_flags = 0, hardsz = 512;
  1400. int error;
  1401. - int bs;
  1402. MOD_INC_USE_COUNT;
  1403. @@ -643,33 +1198,46 @@ static int loop_set_fd(struct loop_devic
  1404. if (!(file->f_mode & FMODE_WRITE))
  1405. lo_flags |= LO_FLAGS_READ_ONLY;
  1406. + lo->lo_offset = lo->lo_sizelimit = 0;
  1407. + lo->lo_offs_sec = lo->lo_iv_remove = 0;
  1408. + lo->lo_bh_free = lo->lo_bh_que2 = lo->lo_bh_que1 = lo->lo_bh_que0 = NULL;
  1409. + lo->lo_bh_need = lo->lo_bh_flsh = 0;
  1410. + init_waitqueue_head(&lo->lo_bh_wait);
  1411. if (S_ISBLK(inode->i_mode)) {
  1412. lo_device = inode->i_rdev;
  1413. if (lo_device == dev) {
  1414. error = -EBUSY;
  1415. goto out_putf;
  1416. }
  1417. + if (loop_prealloc_init(lo, 0)) {
  1418. + error = -ENOMEM;
  1419. + goto out_putf;
  1420. + }
  1421. + hardsz = get_hardsect_size(lo_device);
  1422. } else if (S_ISREG(inode->i_mode)) {
  1423. - struct address_space_operations *aops = inode->i_mapping->a_ops;
  1424. /*
  1425. * If we can't read - sorry. If we only can't write - well,
  1426. * it's going to be read-only.
  1427. */
  1428. - if (!aops->readpage)
  1429. + if (!file->f_op || !file->f_op->read)
  1430. goto out_putf;
  1431. - if (!aops->prepare_write || !aops->commit_write)
  1432. + if (!file->f_op->write)
  1433. lo_flags |= LO_FLAGS_READ_ONLY;
  1434. lo_device = inode->i_dev;
  1435. lo_flags |= LO_FLAGS_DO_BMAP;
  1436. + if (loop_prealloc_init(lo, 1)) {
  1437. + error = -ENOMEM;
  1438. + goto out_putf;
  1439. + }
  1440. error = 0;
  1441. } else
  1442. goto out_putf;
  1443. get_file(file);
  1444. - if (IS_RDONLY (inode) || is_read_only(lo_device)
  1445. + if ((S_ISREG(inode->i_mode) && IS_RDONLY(inode)) || is_read_only(lo_device)
  1446. || !(lo_file->f_mode & FMODE_WRITE))
  1447. lo_flags |= LO_FLAGS_READ_ONLY;
  1448. @@ -677,28 +1245,40 @@ static int loop_set_fd(struct loop_devic
  1449. lo->lo_device = lo_device;
  1450. lo->lo_flags = lo_flags;
  1451. + if(lo_flags & LO_FLAGS_READ_ONLY)
  1452. + lo->lo_flags |= 0x200000; /* export to user space */
  1453. lo->lo_backing_file = file;
  1454. lo->transfer = NULL;
  1455. lo->ioctl = NULL;
  1456. - figure_loop_size(lo);
  1457. - lo->old_gfp_mask = inode->i_mapping->gfp_mask;
  1458. - inode->i_mapping->gfp_mask &= ~(__GFP_IO|__GFP_FS);
  1459. -
  1460. - bs = 0;
  1461. - if (blksize_size[MAJOR(lo_device)])
  1462. - bs = blksize_size[MAJOR(lo_device)][MINOR(lo_device)];
  1463. - if (!bs)
  1464. - bs = BLOCK_SIZE;
  1465. + if (figure_loop_size(lo)) {
  1466. + error = -EFBIG;
  1467. + goto out_cleanup;
  1468. + }
  1469. - set_blocksize(dev, bs);
  1470. + if (lo_flags & LO_FLAGS_DO_BMAP) {
  1471. + lo->old_gfp_mask = inode->i_mapping->gfp_mask;
  1472. + inode->i_mapping->gfp_mask &= ~(__GFP_IO|__GFP_FS);
  1473. + inode->i_mapping->gfp_mask |= __GFP_HIGH;
  1474. + } else {
  1475. + lo->old_gfp_mask = -1;
  1476. + }
  1477. - lo->lo_bh = lo->lo_bhtail = NULL;
  1478. - kernel_thread(loop_thread, lo, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  1479. - down(&lo->lo_sem);
  1480. + loop_hardsizes[MINOR(dev)] = hardsz;
  1481. + loop_set_softblksz(lo, dev);
  1482. + error = kernel_thread(loop_thread, lo, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  1483. + if(error < 0)
  1484. + goto out_mapping;
  1485. + down(&lo->lo_sem);
  1486. fput(file);
  1487. return 0;
  1488. + out_mapping:
  1489. + if(lo->old_gfp_mask != -1)
  1490. + inode->i_mapping->gfp_mask = lo->old_gfp_mask;
  1491. + out_cleanup:
  1492. + loop_prealloc_cleanup(lo);
  1493. + fput(file);
  1494. out_putf:
  1495. fput(file);
  1496. out:
  1497. @@ -708,13 +1288,14 @@ static int loop_set_fd(struct loop_devic
  1498. static int loop_release_xfer(struct loop_device *lo)
  1499. {
  1500. - int err = 0;
  1501. + int err = 0;
  1502. if (lo->lo_encrypt_type) {
  1503. - struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type];
  1504. + struct loop_func_table *xfer= xfer_funcs[lo->lo_encrypt_type];
  1505. + lo->transfer = NULL;
  1506. if (xfer && xfer->release)
  1507. - err = xfer->release(lo);
  1508. + err = xfer->release(lo);
  1509. if (xfer && xfer->unlock)
  1510. - xfer->unlock(lo);
  1511. + xfer->unlock(lo);
  1512. lo->lo_encrypt_type = 0;
  1513. }
  1514. return err;
  1515. @@ -722,19 +1303,19 @@ static int loop_release_xfer(struct loop
  1516. static int loop_init_xfer(struct loop_device *lo, int type,struct loop_info *i)
  1517. {
  1518. - int err = 0;
  1519. + int err = 0;
  1520. if (type) {
  1521. - struct loop_func_table *xfer = xfer_funcs[type];
  1522. + struct loop_func_table *xfer = xfer_funcs[type];
  1523. if (xfer->init)
  1524. err = xfer->init(lo, i);
  1525. - if (!err) {
  1526. + if (!err) {
  1527. lo->lo_encrypt_type = type;
  1528. if (xfer->lock)
  1529. xfer->lock(lo);
  1530. }
  1531. }
  1532. return err;
  1533. -}
  1534. +}
  1535. static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
  1536. {
  1537. @@ -751,11 +1332,12 @@ static int loop_clr_fd(struct loop_devic
  1538. spin_lock_irq(&lo->lo_lock);
  1539. lo->lo_state = Lo_rundown;
  1540. if (atomic_dec_and_test(&lo->lo_pending))
  1541. - up(&lo->lo_bh_mutex);
  1542. + wake_up_interruptible(&lo->lo_bh_wait);
  1543. spin_unlock_irq(&lo->lo_lock);
  1544. down(&lo->lo_sem);
  1545. + loop_prealloc_cleanup(lo);
  1546. lo->lo_backing_file = NULL;
  1547. loop_release_xfer(lo);
  1548. @@ -763,87 +1345,216 @@ static int loop_clr_fd(struct loop_devic
  1549. lo->ioctl = NULL;
  1550. lo->lo_device = 0;
  1551. lo->lo_encrypt_type = 0;
  1552. - lo->lo_offset = 0;
  1553. + lo->lo_offset = lo->lo_sizelimit = 0;
  1554. + lo->lo_offs_sec = lo->lo_iv_remove = 0;
  1555. lo->lo_encrypt_key_size = 0;
  1556. lo->lo_flags = 0;
  1557. memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
  1558. memset(lo->lo_name, 0, LO_NAME_SIZE);
  1559. + memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
  1560. loop_sizes[lo->lo_number] = 0;
  1561. invalidate_bdev(bdev, 0);
  1562. - filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp;
  1563. + if (gfp != -1)
  1564. + filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp;
  1565. lo->lo_state = Lo_unbound;
  1566. fput(filp);
  1567. MOD_DEC_USE_COUNT;
  1568. return 0;
  1569. }
  1570. -static int loop_set_status(struct loop_device *lo, struct loop_info *arg)
  1571. +static void
  1572. +loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
  1573. +{
  1574. + memset(info64, 0, sizeof(*info64));
  1575. + info64->lo_number = info->lo_number;
  1576. + info64->lo_device = info->lo_device;
  1577. + info64->lo_inode = info->lo_inode;
  1578. + info64->lo_rdevice = info->lo_rdevice;
  1579. + info64->lo_offset = info->lo_offset;
  1580. + info64->lo_encrypt_type = info->lo_encrypt_type;
  1581. + info64->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1582. + info64->lo_flags = info->lo_flags;
  1583. + info64->lo_init[0] = info->lo_init[0];
  1584. + info64->lo_init[1] = info->lo_init[1];
  1585. + if (info->lo_encrypt_type == 18) /* LO_CRYPT_CRYPTOAPI */
  1586. + memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
  1587. + else
  1588. + memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
  1589. + memcpy(info64->lo_encrypt_key, info->lo_encrypt_key, LO_KEY_SIZE);
  1590. +}
  1591. +
  1592. +static int
  1593. +loop_info64_to_old(struct loop_info64 *info64, struct loop_info *info)
  1594. +{
  1595. + memset(info, 0, sizeof(*info));
  1596. + info->lo_number = info64->lo_number;
  1597. + info->lo_device = info64->lo_device;
  1598. + info->lo_inode = info64->lo_inode;
  1599. + info->lo_rdevice = info64->lo_rdevice;
  1600. + info->lo_offset = info64->lo_offset;
  1601. + info->lo_encrypt_type = info64->lo_encrypt_type;
  1602. + info->lo_encrypt_key_size = info64->lo_encrypt_key_size;
  1603. + info->lo_flags = info64->lo_flags;
  1604. + info->lo_init[0] = info64->lo_init[0];
  1605. + info->lo_init[1] = info64->lo_init[1];
  1606. + if (info->lo_encrypt_type == 18) /* LO_CRYPT_CRYPTOAPI */
  1607. + memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
  1608. + else
  1609. + memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
  1610. + memcpy(info->lo_encrypt_key, info64->lo_encrypt_key, LO_KEY_SIZE);
  1611. +
  1612. + /* error in case values were truncated */
  1613. + if (info->lo_device != info64->lo_device ||
  1614. + info->lo_rdevice != info64->lo_rdevice ||
  1615. + info->lo_inode != info64->lo_inode ||
  1616. + info->lo_offset != info64->lo_offset ||
  1617. + info64->lo_sizelimit)
  1618. + return -EOVERFLOW;
  1619. +
  1620. + return 0;
  1621. +}
  1622. +
  1623. +static int loop_set_status(struct loop_device *lo, kdev_t dev, struct loop_info64 *info, struct loop_info *oldinfo)
  1624. {
  1625. - struct loop_info info;
  1626. int err;
  1627. unsigned int type;
  1628. - if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
  1629. + if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
  1630. !capable(CAP_SYS_ADMIN))
  1631. return -EPERM;
  1632. if (lo->lo_state != Lo_bound)
  1633. return -ENXIO;
  1634. - if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1635. - return -EFAULT;
  1636. - if ((unsigned int) info.lo_encrypt_key_size > LO_KEY_SIZE)
  1637. + if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
  1638. return -EINVAL;
  1639. - type = info.lo_encrypt_type;
  1640. + type = info->lo_encrypt_type;
  1641. if (type >= MAX_LO_CRYPT || xfer_funcs[type] == NULL)
  1642. return -EINVAL;
  1643. - if (type == LO_CRYPT_XOR && info.lo_encrypt_key_size == 0)
  1644. + if (type == LO_CRYPT_XOR && info->lo_encrypt_key_size == 0)
  1645. return -EINVAL;
  1646. err = loop_release_xfer(lo);
  1647. - if (!err)
  1648. - err = loop_init_xfer(lo, type, &info);
  1649. if (err)
  1650. return err;
  1651. - lo->lo_offset = info.lo_offset;
  1652. - strncpy(lo->lo_name, info.lo_name, LO_NAME_SIZE);
  1653. + if ((loff_t)info->lo_offset < 0) {
  1654. + /* negative offset == remove offset from IV computations */
  1655. + lo->lo_offset = -(info->lo_offset);
  1656. + lo->lo_iv_remove = lo->lo_offset >> 9;
  1657. + } else {
  1658. + /* positive offset == include offset in IV computations */
  1659. + lo->lo_offset = info->lo_offset;
  1660. + lo->lo_iv_remove = 0;
  1661. + }
  1662. + lo->lo_offs_sec = lo->lo_offset >> 9;
  1663. + lo->lo_sizelimit = info->lo_sizelimit;
  1664. + err = figure_loop_size(lo);
  1665. + if (err)
  1666. + return err;
  1667. + loop_set_softblksz(lo, dev);
  1668. +
  1669. + /* transfer init function for 2.4 kernels takes old style struct */
  1670. + err = loop_init_xfer(lo, type, oldinfo);
  1671. + /* copy key -- just in case transfer init func modified it */
  1672. + memcpy(info->lo_encrypt_key, oldinfo->lo_encrypt_key, sizeof(info->lo_encrypt_key));
  1673. + if (err)
  1674. + return err;
  1675. + strncpy(lo->lo_name, info->lo_file_name, LO_NAME_SIZE);
  1676. + strncpy(lo->lo_crypt_name, info->lo_crypt_name, LO_NAME_SIZE);
  1677. lo->transfer = xfer_funcs[type]->transfer;
  1678. lo->ioctl = xfer_funcs[type]->ioctl;
  1679. - lo->lo_encrypt_key_size = info.lo_encrypt_key_size;
  1680. - lo->lo_init[0] = info.lo_init[0];
  1681. - lo->lo_init[1] = info.lo_init[1];
  1682. - if (info.lo_encrypt_key_size) {
  1683. - memcpy(lo->lo_encrypt_key, info.lo_encrypt_key,
  1684. - info.lo_encrypt_key_size);
  1685. - lo->lo_key_owner = current->uid;
  1686. - }
  1687. - figure_loop_size(lo);
  1688. + lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
  1689. + lo->lo_init[0] = info->lo_init[0];
  1690. + lo->lo_init[1] = info->lo_init[1];
  1691. + if (info->lo_encrypt_key_size) {
  1692. + memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
  1693. + info->lo_encrypt_key_size);
  1694. + lo->lo_key_owner = current->uid;
  1695. + }
  1696. +
  1697. return 0;
  1698. }
  1699. -static int loop_get_status(struct loop_device *lo, struct loop_info *arg)
  1700. +static int loop_get_status(struct loop_device *lo, struct loop_info64 *info)
  1701. {
  1702. - struct loop_info info;
  1703. struct file *file = lo->lo_backing_file;
  1704. if (lo->lo_state != Lo_bound)
  1705. return -ENXIO;
  1706. - if (!arg)
  1707. - return -EINVAL;
  1708. - memset(&info, 0, sizeof(info));
  1709. - info.lo_number = lo->lo_number;
  1710. - info.lo_device = kdev_t_to_nr(file->f_dentry->d_inode->i_dev);
  1711. - info.lo_inode = file->f_dentry->d_inode->i_ino;
  1712. - info.lo_rdevice = kdev_t_to_nr(lo->lo_device);
  1713. - info.lo_offset = lo->lo_offset;
  1714. - info.lo_flags = lo->lo_flags;
  1715. - strncpy(info.lo_name, lo->lo_name, LO_NAME_SIZE);
  1716. - info.lo_encrypt_type = lo->lo_encrypt_type;
  1717. + memset(info, 0, sizeof(*info));
  1718. + info->lo_number = lo->lo_number;
  1719. + info->lo_device = kdev_t_to_nr(file->f_dentry->d_inode->i_dev);
  1720. + info->lo_inode = file->f_dentry->d_inode->i_ino;
  1721. + info->lo_rdevice = kdev_t_to_nr(lo->lo_device);
  1722. + info->lo_offset = lo->lo_iv_remove ? -(lo->lo_offset) : lo->lo_offset;
  1723. + info->lo_sizelimit = lo->lo_sizelimit;
  1724. + info->lo_flags = lo->lo_flags;
  1725. + strncpy(info->lo_file_name, lo->lo_name, LO_NAME_SIZE);
  1726. + strncpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
  1727. + info->lo_encrypt_type = lo->lo_encrypt_type;
  1728. if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
  1729. - info.lo_encrypt_key_size = lo->lo_encrypt_key_size;
  1730. - memcpy(info.lo_encrypt_key, lo->lo_encrypt_key,
  1731. + info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
  1732. + memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
  1733. lo->lo_encrypt_key_size);
  1734. + info->lo_init[0] = lo->lo_init[0];
  1735. + info->lo_init[1] = lo->lo_init[1];
  1736. }
  1737. - return copy_to_user(arg, &info, sizeof(info)) ? -EFAULT : 0;
  1738. + return 0;
  1739. +}
  1740. +
  1741. +static int
  1742. +loop_set_status_n(struct loop_device *lo, kdev_t dev, void *arg, int n)
  1743. +{
  1744. + struct loop_info info;
  1745. + struct loop_info64 info64;
  1746. + int err;
  1747. +
  1748. + if (n) {
  1749. + if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
  1750. + return -EFAULT;
  1751. + /* truncation errors can be ignored here as transfer init func only wants key bits */
  1752. + loop_info64_to_old(&info64, &info);
  1753. + } else {
  1754. + if (copy_from_user(&info, arg, sizeof (struct loop_info)))
  1755. + return -EFAULT;
  1756. + loop_info64_from_old(&info, &info64);
  1757. + }
  1758. + err = loop_set_status(lo, dev, &info64, &info);
  1759. + memset(&info.lo_encrypt_key[0], 0, sizeof(info.lo_encrypt_key));
  1760. + memset(&info64.lo_encrypt_key[0], 0, sizeof(info64.lo_encrypt_key));
  1761. + return err;
  1762. +}
  1763. +
  1764. +static int
  1765. +loop_get_status_old(struct loop_device *lo, struct loop_info *arg) {
  1766. + struct loop_info info;
  1767. + struct loop_info64 info64;
  1768. + int err = 0;
  1769. +
  1770. + if (!arg)
  1771. + err = -EINVAL;
  1772. + if (!err)
  1773. + err = loop_get_status(lo, &info64);
  1774. + if (!err)
  1775. + err = loop_info64_to_old(&info64, &info);
  1776. + if (!err && copy_to_user(arg, &info, sizeof(info)))
  1777. + err = -EFAULT;
  1778. +
  1779. + return err;
  1780. +}
  1781. +
  1782. +static int
  1783. +loop_get_status64(struct loop_device *lo, struct loop_info64 *arg) {
  1784. + struct loop_info64 info64;
  1785. + int err = 0;
  1786. +
  1787. + if (!arg)
  1788. + err = -EINVAL;
  1789. + if (!err)
  1790. + err = loop_get_status(lo, &info64);
  1791. + if (!err && copy_to_user(arg, &info64, sizeof(info64)))
  1792. + err = -EFAULT;
  1793. +
  1794. + return err;
  1795. }
  1796. static int lo_ioctl(struct inode * inode, struct file * file,
  1797. @@ -872,10 +1583,16 @@ static int lo_ioctl(struct inode * inode
  1798. err = loop_clr_fd(lo, inode->i_bdev);
  1799. break;
  1800. case LOOP_SET_STATUS:
  1801. - err = loop_set_status(lo, (struct loop_info *) arg);
  1802. + err = loop_set_status_n(lo, inode->i_rdev, (void *) arg, 0);
  1803. break;
  1804. case LOOP_GET_STATUS:
  1805. - err = loop_get_status(lo, (struct loop_info *) arg);
  1806. + err = loop_get_status_old(lo, (struct loop_info *) arg);
  1807. + break;
  1808. + case LOOP_SET_STATUS64:
  1809. + err = loop_set_status_n(lo, inode->i_rdev, (void *) arg, 1);
  1810. + break;
  1811. + case LOOP_GET_STATUS64:
  1812. + err = loop_get_status64(lo, (struct loop_info64 *) arg);
  1813. break;
  1814. case BLKGETSIZE:
  1815. if (lo->lo_state != Lo_bound) {
  1816. @@ -894,6 +1611,8 @@ static int lo_ioctl(struct inode * inode
  1817. case BLKBSZGET:
  1818. case BLKBSZSET:
  1819. case BLKSSZGET:
  1820. + case BLKROGET:
  1821. + case BLKROSET:
  1822. err = blk_ioctl(inode->i_rdev, cmd, arg);
  1823. break;
  1824. default:
  1825. @@ -906,7 +1625,7 @@ static int lo_ioctl(struct inode * inode
  1826. static int lo_open(struct inode *inode, struct file *file)
  1827. {
  1828. struct loop_device *lo;
  1829. - int dev, type;
  1830. + int dev;
  1831. if (!inode)
  1832. return -EINVAL;
  1833. @@ -921,10 +1640,6 @@ static int lo_open(struct inode *inode,
  1834. lo = &loop_dev[dev];
  1835. MOD_INC_USE_COUNT;
  1836. down(&lo->lo_ctl_mutex);
  1837. -
  1838. - type = lo->lo_encrypt_type;
  1839. - if (type && xfer_funcs[type] && xfer_funcs[type]->lock)
  1840. - xfer_funcs[type]->lock(lo);
  1841. lo->lo_refcnt++;
  1842. up(&lo->lo_ctl_mutex);
  1843. return 0;
  1844. @@ -933,7 +1648,7 @@ static int lo_open(struct inode *inode,
  1845. static int lo_release(struct inode *inode, struct file *file)
  1846. {
  1847. struct loop_device *lo;
  1848. - int dev, type;
  1849. + int dev;
  1850. if (!inode)
  1851. return 0;
  1852. @@ -948,11 +1663,7 @@ static int lo_release(struct inode *inod
  1853. lo = &loop_dev[dev];
  1854. down(&lo->lo_ctl_mutex);
  1855. - type = lo->lo_encrypt_type;
  1856. --lo->lo_refcnt;
  1857. - if (xfer_funcs[type] && xfer_funcs[type]->unlock)
  1858. - xfer_funcs[type]->unlock(lo);
  1859. -
  1860. up(&lo->lo_ctl_mutex);
  1861. MOD_DEC_USE_COUNT;
  1862. return 0;
  1863. @@ -974,34 +1685,32 @@ MODULE_LICENSE("GPL");
  1864. int loop_register_transfer(struct loop_func_table *funcs)
  1865. {
  1866. - if ((unsigned)funcs->number > MAX_LO_CRYPT || xfer_funcs[funcs->number])
  1867. + if ((unsigned)funcs->number >= MAX_LO_CRYPT || xfer_funcs[funcs->number])
  1868. return -EINVAL;
  1869. xfer_funcs[funcs->number] = funcs;
  1870. - return 0;
  1871. + return 0;
  1872. }
  1873. int loop_unregister_transfer(int number)
  1874. {
  1875. - struct loop_device *lo;
  1876. + struct loop_device *lo;
  1877. if ((unsigned)number >= MAX_LO_CRYPT)
  1878. - return -EINVAL;
  1879. - for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) {
  1880. + return -EINVAL;
  1881. + for (lo = &loop_dev[0]; lo < &loop_dev[max_loop]; lo++) {
  1882. int type = lo->lo_encrypt_type;
  1883. - if (type == number) {
  1884. - xfer_funcs[type]->release(lo);
  1885. - lo->transfer = NULL;
  1886. - lo->lo_encrypt_type = 0;
  1887. + if (type == number) {
  1888. + loop_release_xfer(lo);
  1889. }
  1890. }
  1891. - xfer_funcs[number] = NULL;
  1892. - return 0;
  1893. + xfer_funcs[number] = NULL;
  1894. + return 0;
  1895. }
  1896. EXPORT_SYMBOL(loop_register_transfer);
  1897. EXPORT_SYMBOL(loop_unregister_transfer);
  1898. -int __init loop_init(void)
  1899. +int __init loop_init(void)
  1900. {
  1901. int i;
  1902. @@ -1017,10 +1726,9 @@ int __init loop_init(void)
  1903. return -EIO;
  1904. }
  1905. -
  1906. loop_dev = kmalloc(max_loop * sizeof(struct loop_device), GFP_KERNEL);
  1907. if (!loop_dev)
  1908. - return -ENOMEM;
  1909. + goto out_dev;
  1910. loop_sizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
  1911. if (!loop_sizes)
  1912. @@ -1030,6 +1738,10 @@ int __init loop_init(void)
  1913. if (!loop_blksizes)
  1914. goto out_blksizes;
  1915. + loop_hardsizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
  1916. + if (!loop_hardsizes)
  1917. + goto out_hardsizes;
  1918. +
  1919. blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), loop_make_request);
  1920. for (i = 0; i < max_loop; i++) {
  1921. @@ -1037,18 +1749,34 @@ int __init loop_init(void)
  1922. memset(lo, 0, sizeof(struct loop_device));
  1923. init_MUTEX(&lo->lo_ctl_mutex);
  1924. init_MUTEX_LOCKED(&lo->lo_sem);
  1925. - init_MUTEX_LOCKED(&lo->lo_bh_mutex);
  1926. lo->lo_number = i;
  1927. spin_lock_init(&lo->lo_lock);
  1928. }
  1929. memset(loop_sizes, 0, max_loop * sizeof(int));
  1930. memset(loop_blksizes, 0, max_loop * sizeof(int));
  1931. + memset(loop_hardsizes, 0, max_loop * sizeof(int));
  1932. blk_size[MAJOR_NR] = loop_sizes;
  1933. blksize_size[MAJOR_NR] = loop_blksizes;
  1934. + hardsect_size[MAJOR_NR] = loop_hardsizes;
  1935. for (i = 0; i < max_loop; i++)
  1936. register_disk(NULL, MKDEV(MAJOR_NR, i), 1, &lo_fops, 0);
  1937. + for (i = 0; i < (sizeof(lo_prealloc) / sizeof(int)); i += 2) {
  1938. + if (!lo_prealloc[i])
  1939. + continue;
  1940. + if (lo_prealloc[i] < LO_PREALLOC_MIN)
  1941. + lo_prealloc[i] = LO_PREALLOC_MIN;
  1942. + if (lo_prealloc[i] > LO_PREALLOC_MAX)
  1943. + lo_prealloc[i] = LO_PREALLOC_MAX;
  1944. + }
  1945. +
  1946. +#if defined(IOCTL32_COMPATIBLE_PTR)
  1947. + register_ioctl32_conversion(LOOP_SET_STATUS64, IOCTL32_COMPATIBLE_PTR);
  1948. + register_ioctl32_conversion(LOOP_GET_STATUS64, IOCTL32_COMPATIBLE_PTR);
  1949. + register_ioctl32_conversion(LOOP_MULTI_KEY_SETUP, IOCTL32_COMPATIBLE_PTR);
  1950. +#endif
  1951. +
  1952. devfs_handle = devfs_mk_dir(NULL, "loop", NULL);
  1953. devfs_register_series(devfs_handle, "%u", max_loop, DEVFS_FL_DEFAULT,
  1954. MAJOR_NR, 0,
  1955. @@ -1058,24 +1786,38 @@ int __init loop_init(void)
  1956. printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop);
  1957. return 0;
  1958. +out_hardsizes:
  1959. + kfree(loop_blksizes);
  1960. out_blksizes:
  1961. kfree(loop_sizes);
  1962. out_sizes:
  1963. kfree(loop_dev);
  1964. +out_dev:
  1965. if (devfs_unregister_blkdev(MAJOR_NR, "loop"))
  1966. printk(KERN_WARNING "loop: cannot unregister blkdev\n");
  1967. printk(KERN_ERR "loop: ran out of memory\n");
  1968. return -ENOMEM;
  1969. }
  1970. -void loop_exit(void)
  1971. +void loop_exit(void)
  1972. {
  1973. devfs_unregister(devfs_handle);
  1974. if (devfs_unregister_blkdev(MAJOR_NR, "loop"))
  1975. printk(KERN_WARNING "loop: cannot unregister blkdev\n");
  1976. +
  1977. + blk_size[MAJOR_NR] = 0;
  1978. + blksize_size[MAJOR_NR] = 0;
  1979. + hardsect_size[MAJOR_NR] = 0;
  1980. kfree(loop_dev);
  1981. kfree(loop_sizes);
  1982. kfree(loop_blksizes);
  1983. + kfree(loop_hardsizes);
  1984. +
  1985. +#if defined(IOCTL32_COMPATIBLE_PTR)
  1986. + unregister_ioctl32_conversion(LOOP_SET_STATUS64);
  1987. + unregister_ioctl32_conversion(LOOP_GET_STATUS64);
  1988. + unregister_ioctl32_conversion(LOOP_MULTI_KEY_SETUP);
  1989. +#endif
  1990. }
  1991. module_init(loop_init);
  1992. diff -pruN linux-2.4.26/drivers/misc/Makefile linux-2.4.26_crypto/drivers/misc/Makefile
  1993. --- linux-2.4.26/drivers/misc/Makefile 2000-12-29 23:07:22.000000000 +0100
  1994. +++ linux-2.4.26_crypto/drivers/misc/Makefile 2004-05-16 14:57:30.000000000 +0200
  1995. @@ -9,8 +9,33 @@
  1996. # parent makes..
  1997. #
  1998. +.S.o:
  1999. + $(CC) $(AFLAGS) $(AFLAGS_$@) -c $< -o $*.o
  2000. +
  2001. O_TARGET := misc.o
  2002. +ifeq ($(CONFIG_BLK_DEV_LOOP_AES),y)
  2003. +AES_PENTIUM_ASM=n
  2004. +ifeq ($(CONFIG_X86),y)
  2005. +ifneq ($(CONFIG_X86_64),y)
  2006. +ifneq ($(CONFIG_M386),y)
  2007. +ifneq ($(CONFIG_M486),y)
  2008. + AES_PENTIUM_ASM=y
  2009. +endif
  2010. +endif
  2011. +endif
  2012. +endif
  2013. +ifeq ($(AES_PENTIUM_ASM),y)
  2014. + export-objs += crypto-ksym.o
  2015. + obj-y += aes-i586.o md5-i586.o crypto-ksym.o
  2016. + AFLAGS_aes-i586.o := -DUSE_UNDERLINE=1
  2017. +else
  2018. + export-objs += crypto-ksym.o
  2019. + obj-y += aes.o md5.o crypto-ksym.o
  2020. + CFLAGS_aes.o := -DDATA_ALWAYS_ALIGNED=1
  2021. +endif
  2022. +endif
  2023. +
  2024. include $(TOPDIR)/Rules.make
  2025. fastdep:
  2026. diff -pruN linux-2.4.26/drivers/misc/aes-i586.S linux-2.4.26_crypto/drivers/misc/aes-i586.S
  2027. --- linux-2.4.26/drivers/misc/aes-i586.S 1970-01-01 01:00:00.000000000 +0100
  2028. +++ linux-2.4.26_crypto/drivers/misc/aes-i586.S 2004-05-16 14:57:30.000000000 +0200
  2029. @@ -0,0 +1,922 @@
  2030. +//
  2031. +// Copyright (c) 2001, Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK.
  2032. +// All rights reserved.
  2033. +//
  2034. +// TERMS
  2035. +//
  2036. +// Redistribution and use in source and binary forms, with or without
  2037. +// modification, are permitted subject to the following conditions:
  2038. +//
  2039. +// 1. Redistributions of source code must retain the above copyright
  2040. +// notice, this list of conditions and the following disclaimer.
  2041. +//
  2042. +// 2. Redistributions in binary form must reproduce the above copyright
  2043. +// notice, this list of conditions and the following disclaimer in the
  2044. +// documentation and/or other materials provided with the distribution.
  2045. +//
  2046. +// 3. The copyright holder's name must not be used to endorse or promote
  2047. +// any products derived from this software without his specific prior
  2048. +// written permission.
  2049. +//
  2050. +// This software is provided 'as is' with no express or implied warranties
  2051. +// of correctness or fitness for purpose.
  2052. +
  2053. +// Modified by Jari Ruusu, December 24 2001
  2054. +// - Converted syntax to GNU CPP/assembler syntax
  2055. +// - C programming interface converted back to "old" API
  2056. +// - Minor portability cleanups and speed optimizations
  2057. +
  2058. +// Modified by Jari Ruusu, April 11 2002
  2059. +// - Added above copyright and terms to resulting object code so that
  2060. +// binary distributions can avoid legal trouble
  2061. +
  2062. +// An AES (Rijndael) implementation for the Pentium. This version only
  2063. +// implements the standard AES block length (128 bits, 16 bytes). This code
  2064. +// does not preserve the eax, ecx or edx registers or the artihmetic status
  2065. +// flags. However, the ebx, esi, edi, and ebp registers are preserved across
  2066. +// calls.
  2067. +
  2068. +// void aes_set_key(aes_context *cx, const unsigned char key[], const int key_len, const int f)
  2069. +// void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  2070. +// void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  2071. +
  2072. +#if defined(USE_UNDERLINE)
  2073. +# define aes_set_key _aes_set_key
  2074. +# define aes_encrypt _aes_encrypt
  2075. +# define aes_decrypt _aes_decrypt
  2076. +#endif
  2077. +#if !defined(ALIGN32BYTES)
  2078. +# define ALIGN32BYTES 32
  2079. +#endif
  2080. +
  2081. + .file "aes-i586.S"
  2082. + .globl aes_set_key
  2083. + .globl aes_encrypt
  2084. + .globl aes_decrypt
  2085. +
  2086. + .text
  2087. +copyright:
  2088. + .ascii " \000"
  2089. + .ascii "Copyright (c) 2001, Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK.\000"
  2090. + .ascii "All rights reserved.\000"
  2091. + .ascii " \000"
  2092. + .ascii "TERMS\000"
  2093. + .ascii " \000"
  2094. + .ascii " Redistribution and use in source and binary forms, with or without\000"
  2095. + .ascii " modification, are permitted subject to the following conditions:\000"
  2096. + .ascii " \000"
  2097. + .ascii " 1. Redistributions of source code must retain the above copyright\000"
  2098. + .ascii " notice, this list of conditions and the following disclaimer.\000"
  2099. + .ascii " \000"
  2100. + .ascii " 2. Redistributions in binary form must reproduce the above copyright\000"
  2101. + .ascii " notice, this list of conditions and the following disclaimer in the\000"
  2102. + .ascii " documentation and/or other materials provided with the distribution.\000"
  2103. + .ascii " \000"
  2104. + .ascii " 3. The copyright holder's name must not be used to endorse or promote\000"
  2105. + .ascii " any products derived from this software without his specific prior\000"
  2106. + .ascii " written permission.\000"
  2107. + .ascii " \000"
  2108. + .ascii " This software is provided 'as is' with no express or implied warranties\000"
  2109. + .ascii " of correctness or fitness for purpose.\000"
  2110. + .ascii " \000"
  2111. +
  2112. +#define tlen 1024 // length of each of 4 'xor' arrays (256 32-bit words)
  2113. +
  2114. +// offsets to parameters with one register pushed onto stack
  2115. +
  2116. +#define ctx 8 // AES context structure
  2117. +#define in_blk 12 // input byte array address parameter
  2118. +#define out_blk 16 // output byte array address parameter
  2119. +
  2120. +// offsets in context structure
  2121. +
  2122. +#define nkey 0 // key length, size 4
  2123. +#define nrnd 4 // number of rounds, size 4
  2124. +#define ekey 8 // encryption key schedule base address, size 256
  2125. +#define dkey 264 // decryption key schedule base address, size 256
  2126. +
  2127. +// This macro performs a forward encryption cycle. It is entered with
  2128. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  2129. +// exits with the final values in the same registers.
  2130. +
  2131. +#define fwd_rnd(p1,p2) \
  2132. + mov %ebx,(%esp) ;\
  2133. + movzbl %al,%edx ;\
  2134. + mov %eax,%ecx ;\
  2135. + mov p2(%ebp),%eax ;\
  2136. + mov %edi,4(%esp) ;\
  2137. + mov p2+12(%ebp),%edi ;\
  2138. + xor p1(,%edx,4),%eax ;\
  2139. + movzbl %ch,%edx ;\
  2140. + shr $16,%ecx ;\
  2141. + mov p2+4(%ebp),%ebx ;\
  2142. + xor p1+tlen(,%edx,4),%edi ;\
  2143. + movzbl %cl,%edx ;\
  2144. + movzbl %ch,%ecx ;\
  2145. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  2146. + mov %esi,%ecx ;\
  2147. + mov p1+2*tlen(,%edx,4),%esi ;\
  2148. + movzbl %cl,%edx ;\
  2149. + xor p1(,%edx,4),%esi ;\
  2150. + movzbl %ch,%edx ;\
  2151. + shr $16,%ecx ;\
  2152. + xor p1+tlen(,%edx,4),%ebx ;\
  2153. + movzbl %cl,%edx ;\
  2154. + movzbl %ch,%ecx ;\
  2155. + xor p1+2*tlen(,%edx,4),%eax ;\
  2156. + mov (%esp),%edx ;\
  2157. + xor p1+3*tlen(,%ecx,4),%edi ;\
  2158. + movzbl %dl,%ecx ;\
  2159. + xor p2+8(%ebp),%esi ;\
  2160. + xor p1(,%ecx,4),%ebx ;\
  2161. + movzbl %dh,%ecx ;\
  2162. + shr $16,%edx ;\
  2163. + xor p1+tlen(,%ecx,4),%eax ;\
  2164. + movzbl %dl,%ecx ;\
  2165. + movzbl %dh,%edx ;\
  2166. + xor p1+2*tlen(,%ecx,4),%edi ;\
  2167. + mov 4(%esp),%ecx ;\
  2168. + xor p1+3*tlen(,%edx,4),%esi ;\
  2169. + movzbl %cl,%edx ;\
  2170. + xor p1(,%edx,4),%edi ;\
  2171. + movzbl %ch,%edx ;\
  2172. + shr $16,%ecx ;\
  2173. + xor p1+tlen(,%edx,4),%esi ;\
  2174. + movzbl %cl,%edx ;\
  2175. + movzbl %ch,%ecx ;\
  2176. + xor p1+2*tlen(,%edx,4),%ebx ;\
  2177. + xor p1+3*tlen(,%ecx,4),%eax
  2178. +
  2179. +// This macro performs an inverse encryption cycle. It is entered with
  2180. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  2181. +// exits with the final values in the same registers.
  2182. +
  2183. +#define inv_rnd(p1,p2) \
  2184. + movzbl %al,%edx ;\
  2185. + mov %ebx,(%esp) ;\
  2186. + mov %eax,%ecx ;\
  2187. + mov p2(%ebp),%eax ;\
  2188. + mov %edi,4(%esp) ;\
  2189. + mov p2+4(%ebp),%ebx ;\
  2190. + xor p1(,%edx,4),%eax ;\
  2191. + movzbl %ch,%edx ;\
  2192. + shr $16,%ecx ;\
  2193. + mov p2+12(%ebp),%edi ;\
  2194. + xor p1+tlen(,%edx,4),%ebx ;\
  2195. + movzbl %cl,%edx ;\
  2196. + movzbl %ch,%ecx ;\
  2197. + xor p1+3*tlen(,%ecx,4),%edi ;\
  2198. + mov %esi,%ecx ;\
  2199. + mov p1+2*tlen(,%edx,4),%esi ;\
  2200. + movzbl %cl,%edx ;\
  2201. + xor p1(,%edx,4),%esi ;\
  2202. + movzbl %ch,%edx ;\
  2203. + shr $16,%ecx ;\
  2204. + xor p1+tlen(,%edx,4),%edi ;\
  2205. + movzbl %cl,%edx ;\
  2206. + movzbl %ch,%ecx ;\
  2207. + xor p1+2*tlen(,%edx,4),%eax ;\
  2208. + mov (%esp),%edx ;\
  2209. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  2210. + movzbl %dl,%ecx ;\
  2211. + xor p2+8(%ebp),%esi ;\
  2212. + xor p1(,%ecx,4),%ebx ;\
  2213. + movzbl %dh,%ecx ;\
  2214. + shr $16,%edx ;\
  2215. + xor p1+tlen(,%ecx,4),%esi ;\
  2216. + movzbl %dl,%ecx ;\
  2217. + movzbl %dh,%edx ;\
  2218. + xor p1+2*tlen(,%ecx,4),%edi ;\
  2219. + mov 4(%esp),%ecx ;\
  2220. + xor p1+3*tlen(,%edx,4),%eax ;\
  2221. + movzbl %cl,%edx ;\
  2222. + xor p1(,%edx,4),%edi ;\
  2223. + movzbl %ch,%edx ;\
  2224. + shr $16,%ecx ;\
  2225. + xor p1+tlen(,%edx,4),%eax ;\
  2226. + movzbl %cl,%edx ;\
  2227. + movzbl %ch,%ecx ;\
  2228. + xor p1+2*tlen(,%edx,4),%ebx ;\
  2229. + xor p1+3*tlen(,%ecx,4),%esi
  2230. +
  2231. +// AES (Rijndael) Encryption Subroutine
  2232. +
  2233. + .text
  2234. + .align ALIGN32BYTES
  2235. +aes_encrypt:
  2236. + push %ebp
  2237. + mov ctx(%esp),%ebp // pointer to context
  2238. + mov in_blk(%esp),%ecx
  2239. + push %ebx
  2240. + push %esi
  2241. + push %edi
  2242. + mov nrnd(%ebp),%edx // number of rounds
  2243. + lea ekey+16(%ebp),%ebp // key pointer
  2244. +
  2245. +// input four columns and xor in first round key
  2246. +
  2247. + mov (%ecx),%eax
  2248. + mov 4(%ecx),%ebx
  2249. + mov 8(%ecx),%esi
  2250. + mov 12(%ecx),%edi
  2251. + xor -16(%ebp),%eax
  2252. + xor -12(%ebp),%ebx
  2253. + xor -8(%ebp),%esi
  2254. + xor -4(%ebp),%edi
  2255. +
  2256. + sub $8,%esp // space for register saves on stack
  2257. +
  2258. + sub $10,%edx
  2259. + je aes_15
  2260. + add $32,%ebp
  2261. + sub $2,%edx
  2262. + je aes_13
  2263. + add $32,%ebp
  2264. +
  2265. + fwd_rnd(aes_ft_tab,-64) // 14 rounds for 256-bit key
  2266. + fwd_rnd(aes_ft_tab,-48)
  2267. +aes_13: fwd_rnd(aes_ft_tab,-32) // 12 rounds for 192-bit key
  2268. + fwd_rnd(aes_ft_tab,-16)
  2269. +aes_15: fwd_rnd(aes_ft_tab,0) // 10 rounds for 128-bit key
  2270. + fwd_rnd(aes_ft_tab,16)
  2271. + fwd_rnd(aes_ft_tab,32)
  2272. + fwd_rnd(aes_ft_tab,48)
  2273. + fwd_rnd(aes_ft_tab,64)
  2274. + fwd_rnd(aes_ft_tab,80)
  2275. + fwd_rnd(aes_ft_tab,96)
  2276. + fwd_rnd(aes_ft_tab,112)
  2277. + fwd_rnd(aes_ft_tab,128)
  2278. + fwd_rnd(aes_fl_tab,144) // last round uses a different table
  2279. +
  2280. +// move final values to the output array.
  2281. +
  2282. + mov out_blk+20(%esp),%ebp
  2283. + add $8,%esp
  2284. + mov %eax,(%ebp)
  2285. + mov %ebx,4(%ebp)
  2286. + mov %esi,8(%ebp)
  2287. + mov %edi,12(%ebp)
  2288. + pop %edi
  2289. + pop %esi
  2290. + pop %ebx
  2291. + pop %ebp
  2292. + ret
  2293. +
  2294. +
  2295. +// AES (Rijndael) Decryption Subroutine
  2296. +
  2297. + .align ALIGN32BYTES
  2298. +aes_decrypt:
  2299. + push %ebp
  2300. + mov ctx(%esp),%ebp // pointer to context
  2301. + mov in_blk(%esp),%ecx
  2302. + push %ebx
  2303. + push %esi
  2304. + push %edi
  2305. + mov nrnd(%ebp),%edx // number of rounds
  2306. + lea dkey+16(%ebp),%ebp // key pointer
  2307. +
  2308. +// input four columns and xor in first round key
  2309. +
  2310. + mov (%ecx),%eax
  2311. + mov 4(%ecx),%ebx
  2312. + mov 8(%ecx),%esi
  2313. + mov 12(%ecx),%edi
  2314. + xor -16(%ebp),%eax
  2315. + xor -12(%ebp),%ebx
  2316. + xor -8(%ebp),%esi
  2317. + xor -4(%ebp),%edi
  2318. +
  2319. + sub $8,%esp // space for register saves on stack
  2320. +
  2321. + sub $10,%edx
  2322. + je aes_25
  2323. + add $32,%ebp
  2324. + sub $2,%edx
  2325. + je aes_23
  2326. + add $32,%ebp
  2327. +
  2328. + inv_rnd(aes_it_tab,-64) // 14 rounds for 256-bit key
  2329. + inv_rnd(aes_it_tab,-48)
  2330. +aes_23: inv_rnd(aes_it_tab,-32) // 12 rounds for 192-bit key
  2331. + inv_rnd(aes_it_tab,-16)
  2332. +aes_25: inv_rnd(aes_it_tab,0) // 10 rounds for 128-bit key
  2333. + inv_rnd(aes_it_tab,16)
  2334. + inv_rnd(aes_it_tab,32)
  2335. + inv_rnd(aes_it_tab,48)
  2336. + inv_rnd(aes_it_tab,64)
  2337. + inv_rnd(aes_it_tab,80)
  2338. + inv_rnd(aes_it_tab,96)
  2339. + inv_rnd(aes_it_tab,112)
  2340. + inv_rnd(aes_it_tab,128)
  2341. + inv_rnd(aes_il_tab,144) // last round uses a different table
  2342. +
  2343. +// move final values to the output array.
  2344. +
  2345. + mov out_blk+20(%esp),%ebp
  2346. + add $8,%esp
  2347. + mov %eax,(%ebp)
  2348. + mov %ebx,4(%ebp)
  2349. + mov %esi,8(%ebp)
  2350. + mov %edi,12(%ebp)
  2351. + pop %edi
  2352. + pop %esi
  2353. + pop %ebx
  2354. + pop %ebp
  2355. + ret
  2356. +
  2357. +// AES (Rijndael) Key Schedule Subroutine
  2358. +
  2359. +// input/output parameters
  2360. +
  2361. +#define aes_cx 12 // AES context
  2362. +#define in_key 16 // key input array address
  2363. +#define key_ln 20 // key length, bytes (16,24,32) or bits (128,192,256)
  2364. +#define ed_flg 24 // 0=create both encr/decr keys, 1=create encr key only
  2365. +
  2366. +// offsets for locals
  2367. +
  2368. +#define cnt -4
  2369. +#define kpf -8
  2370. +#define slen 8
  2371. +
  2372. +// This macro performs a column mixing operation on an input 32-bit
  2373. +// word to give a 32-bit result. It uses each of the 4 bytes in the
  2374. +// the input column to index 4 different tables of 256 32-bit words
  2375. +// that are xored together to form the output value.
  2376. +
  2377. +#define mix_col(p1) \
  2378. + movzbl %bl,%ecx ;\
  2379. + mov p1(,%ecx,4),%eax ;\
  2380. + movzbl %bh,%ecx ;\
  2381. + ror $16,%ebx ;\
  2382. + xor p1+tlen(,%ecx,4),%eax ;\
  2383. + movzbl %bl,%ecx ;\
  2384. + xor p1+2*tlen(,%ecx,4),%eax ;\
  2385. + movzbl %bh,%ecx ;\
  2386. + xor p1+3*tlen(,%ecx,4),%eax
  2387. +
  2388. +// Key Schedule Macros
  2389. +
  2390. +#define ksc4(p1) \
  2391. + rol $24,%ebx ;\
  2392. + mix_col(aes_fl_tab) ;\
  2393. + ror $8,%ebx ;\
  2394. + xor 4*p1+aes_rcon_tab,%eax ;\
  2395. + xor %eax,%esi ;\
  2396. + xor %esi,%ebp ;\
  2397. + mov %esi,16*p1(%edi) ;\
  2398. + mov %ebp,16*p1+4(%edi) ;\
  2399. + xor %ebp,%edx ;\
  2400. + xor %edx,%ebx ;\
  2401. + mov %edx,16*p1+8(%edi) ;\
  2402. + mov %ebx,16*p1+12(%edi)
  2403. +
  2404. +#define ksc6(p1) \
  2405. + rol $24,%ebx ;\
  2406. + mix_col(aes_fl_tab) ;\
  2407. + ror $8,%ebx ;\
  2408. + xor 4*p1+aes_rcon_tab,%eax ;\
  2409. + xor 24*p1-24(%edi),%eax ;\
  2410. + mov %eax,24*p1(%edi) ;\
  2411. + xor 24*p1-20(%edi),%eax ;\
  2412. + mov %eax,24*p1+4(%edi) ;\
  2413. + xor %eax,%esi ;\
  2414. + xor %esi,%ebp ;\
  2415. + mov %esi,24*p1+8(%edi) ;\
  2416. + mov %ebp,24*p1+12(%edi) ;\
  2417. + xor %ebp,%edx ;\
  2418. + xor %edx,%ebx ;\
  2419. + mov %edx,24*p1+16(%edi) ;\
  2420. + mov %ebx,24*p1+20(%edi)
  2421. +
  2422. +#define ksc8(p1) \
  2423. + rol $24,%ebx ;\
  2424. + mix_col(aes_fl_tab) ;\
  2425. + ror $8,%ebx ;\
  2426. + xor 4*p1+aes_rcon_tab,%eax ;\
  2427. + xor 32*p1-32(%edi),%eax ;\
  2428. + mov %eax,32*p1(%edi) ;\
  2429. + xor 32*p1-28(%edi),%eax ;\
  2430. + mov %eax,32*p1+4(%edi) ;\
  2431. + xor 32*p1-24(%edi),%eax ;\
  2432. + mov %eax,32*p1+8(%edi) ;\
  2433. + xor 32*p1-20(%edi),%eax ;\
  2434. + mov %eax,32*p1+12(%edi) ;\
  2435. + push %ebx ;\
  2436. + mov %eax,%ebx ;\
  2437. + mix_col(aes_fl_tab) ;\
  2438. + pop %ebx ;\
  2439. + xor %eax,%esi ;\
  2440. + xor %esi,%ebp ;\
  2441. + mov %esi,32*p1+16(%edi) ;\
  2442. + mov %ebp,32*p1+20(%edi) ;\
  2443. + xor %ebp,%edx ;\
  2444. + xor %edx,%ebx ;\
  2445. + mov %edx,32*p1+24(%edi) ;\
  2446. + mov %ebx,32*p1+28(%edi)
  2447. +
  2448. + .align ALIGN32BYTES
  2449. +aes_set_key:
  2450. + pushfl
  2451. + push %ebp
  2452. + mov %esp,%ebp
  2453. + sub $slen,%esp
  2454. + push %ebx
  2455. + push %esi
  2456. + push %edi
  2457. +
  2458. + mov aes_cx(%ebp),%edx // edx -> AES context
  2459. +
  2460. + mov key_ln(%ebp),%ecx // key length
  2461. + cmpl $128,%ecx
  2462. + jb aes_30
  2463. + shr $3,%ecx
  2464. +aes_30: cmpl $32,%ecx
  2465. + je aes_32
  2466. + cmpl $24,%ecx
  2467. + je aes_32
  2468. + mov $16,%ecx
  2469. +aes_32: shr $2,%ecx
  2470. + mov %ecx,nkey(%edx)
  2471. +
  2472. + lea 6(%ecx),%eax // 10/12/14 for 4/6/8 32-bit key length
  2473. + mov %eax,nrnd(%edx)
  2474. +
  2475. + mov in_key(%ebp),%esi // key input array
  2476. + lea ekey(%edx),%edi // key position in AES context
  2477. + cld
  2478. + push %ebp
  2479. + mov %ecx,%eax // save key length in eax
  2480. + rep ; movsl // words in the key schedule
  2481. + mov -4(%esi),%ebx // put some values in registers
  2482. + mov -8(%esi),%edx // to allow faster code
  2483. + mov -12(%esi),%ebp
  2484. + mov -16(%esi),%esi
  2485. +
  2486. + cmpl $4,%eax // jump on key size
  2487. + je aes_36
  2488. + cmpl $6,%eax
  2489. + je aes_35
  2490. +
  2491. + ksc8(0)
  2492. + ksc8(1)
  2493. + ksc8(2)
  2494. + ksc8(3)
  2495. + ksc8(4)
  2496. + ksc8(5)
  2497. + ksc8(6)
  2498. + jmp aes_37
  2499. +aes_35: ksc6(0)
  2500. + ksc6(1)
  2501. + ksc6(2)
  2502. + ksc6(3)
  2503. + ksc6(4)
  2504. + ksc6(5)
  2505. + ksc6(6)
  2506. + ksc6(7)
  2507. + jmp aes_37
  2508. +aes_36: ksc4(0)
  2509. + ksc4(1)
  2510. + ksc4(2)
  2511. + ksc4(3)
  2512. + ksc4(4)
  2513. + ksc4(5)
  2514. + ksc4(6)
  2515. + ksc4(7)
  2516. + ksc4(8)
  2517. + ksc4(9)
  2518. +aes_37: pop %ebp
  2519. + mov aes_cx(%ebp),%edx // edx -> AES context
  2520. + cmpl $0,ed_flg(%ebp)
  2521. + jne aes_39
  2522. +
  2523. +// compile decryption key schedule from encryption schedule - reverse
  2524. +// order and do mix_column operation on round keys except first and last
  2525. +
  2526. + mov nrnd(%edx),%eax // kt = cx->d_key + nc * cx->Nrnd
  2527. + shl $2,%eax
  2528. + lea dkey(%edx,%eax,4),%edi
  2529. + lea ekey(%edx),%esi // kf = cx->e_key
  2530. +
  2531. + movsl // copy first round key (unmodified)
  2532. + movsl
  2533. + movsl
  2534. + movsl
  2535. + sub $32,%edi
  2536. + movl $1,cnt(%ebp)
  2537. +aes_38: // do mix column on each column of
  2538. + lodsl // each round key
  2539. + mov %eax,%ebx
  2540. + mix_col(aes_im_tab)
  2541. + stosl
  2542. + lodsl
  2543. + mov %eax,%ebx
  2544. + mix_col(aes_im_tab)
  2545. + stosl
  2546. + lodsl
  2547. + mov %eax,%ebx
  2548. + mix_col(aes_im_tab)
  2549. + stosl
  2550. + lodsl
  2551. + mov %eax,%ebx
  2552. + mix_col(aes_im_tab)
  2553. + stosl
  2554. + sub $32,%edi
  2555. +
  2556. + incl cnt(%ebp)
  2557. + mov cnt(%ebp),%eax
  2558. + cmp nrnd(%edx),%eax
  2559. + jb aes_38
  2560. +
  2561. + movsl // copy last round key (unmodified)
  2562. + movsl
  2563. + movsl
  2564. + movsl
  2565. +aes_39: pop %edi
  2566. + pop %esi
  2567. + pop %ebx
  2568. + mov %ebp,%esp
  2569. + pop %ebp
  2570. + popfl
  2571. + ret
  2572. +
  2573. +
  2574. +// finite field multiplies by {02}, {04} and {08}
  2575. +
  2576. +#define f2(x) ((x<<1)^(((x>>7)&1)*0x11b))
  2577. +#define f4(x) ((x<<2)^(((x>>6)&1)*0x11b)^(((x>>6)&2)*0x11b))
  2578. +#define f8(x) ((x<<3)^(((x>>5)&1)*0x11b)^(((x>>5)&2)*0x11b)^(((x>>5)&4)*0x11b))
  2579. +
  2580. +// finite field multiplies required in table generation
  2581. +
  2582. +#define f3(x) (f2(x) ^ x)
  2583. +#define f9(x) (f8(x) ^ x)
  2584. +#define fb(x) (f8(x) ^ f2(x) ^ x)
  2585. +#define fd(x) (f8(x) ^ f4(x) ^ x)
  2586. +#define fe(x) (f8(x) ^ f4(x) ^ f2(x))
  2587. +
  2588. +// These defines generate the forward table entries
  2589. +
  2590. +#define u0(x) ((f3(x) << 24) | (x << 16) | (x << 8) | f2(x))
  2591. +#define u1(x) ((x << 24) | (x << 16) | (f2(x) << 8) | f3(x))
  2592. +#define u2(x) ((x << 24) | (f2(x) << 16) | (f3(x) << 8) | x)
  2593. +#define u3(x) ((f2(x) << 24) | (f3(x) << 16) | (x << 8) | x)
  2594. +
  2595. +// These defines generate the inverse table entries
  2596. +
  2597. +#define v0(x) ((fb(x) << 24) | (fd(x) << 16) | (f9(x) << 8) | fe(x))
  2598. +#define v1(x) ((fd(x) << 24) | (f9(x) << 16) | (fe(x) << 8) | fb(x))
  2599. +#define v2(x) ((f9(x) << 24) | (fe(x) << 16) | (fb(x) << 8) | fd(x))
  2600. +#define v3(x) ((fe(x) << 24) | (fb(x) << 16) | (fd(x) << 8) | f9(x))
  2601. +
  2602. +// These defines generate entries for the last round tables
  2603. +
  2604. +#define w0(x) (x)
  2605. +#define w1(x) (x << 8)
  2606. +#define w2(x) (x << 16)
  2607. +#define w3(x) (x << 24)
  2608. +
  2609. +// macro to generate inverse mix column tables (needed for the key schedule)
  2610. +
  2611. +#define im_data0(p1) \
  2612. + .long p1(0x00),p1(0x01),p1(0x02),p1(0x03),p1(0x04),p1(0x05),p1(0x06),p1(0x07) ;\
  2613. + .long p1(0x08),p1(0x09),p1(0x0a),p1(0x0b),p1(0x0c),p1(0x0d),p1(0x0e),p1(0x0f) ;\
  2614. + .long p1(0x10),p1(0x11),p1(0x12),p1(0x13),p1(0x14),p1(0x15),p1(0x16),p1(0x17) ;\
  2615. + .long p1(0x18),p1(0x19),p1(0x1a),p1(0x1b),p1(0x1c),p1(0x1d),p1(0x1e),p1(0x1f)
  2616. +#define im_data1(p1) \
  2617. + .long p1(0x20),p1(0x21),p1(0x22),p1(0x23),p1(0x24),p1(0x25),p1(0x26),p1(0x27) ;\
  2618. + .long p1(0x28),p1(0x29),p1(0x2a),p1(0x2b),p1(0x2c),p1(0x2d),p1(0x2e),p1(0x2f) ;\
  2619. + .long p1(0x30),p1(0x31),p1(0x32),p1(0x33),p1(0x34),p1(0x35),p1(0x36),p1(0x37) ;\
  2620. + .long p1(0x38),p1(0x39),p1(0x3a),p1(0x3b),p1(0x3c),p1(0x3d),p1(0x3e),p1(0x3f)
  2621. +#define im_data2(p1) \
  2622. + .long p1(0x40),p1(0x41),p1(0x42),p1(0x43),p1(0x44),p1(0x45),p1(0x46),p1(0x47) ;\
  2623. + .long p1(0x48),p1(0x49),p1(0x4a),p1(0x4b),p1(0x4c),p1(0x4d),p1(0x4e),p1(0x4f) ;\
  2624. + .long p1(0x50),p1(0x51),p1(0x52),p1(0x53),p1(0x54),p1(0x55),p1(0x56),p1(0x57) ;\
  2625. + .long p1(0x58),p1(0x59),p1(0x5a),p1(0x5b),p1(0x5c),p1(0x5d),p1(0x5e),p1(0x5f)
  2626. +#define im_data3(p1) \
  2627. + .long p1(0x60),p1(0x61),p1(0x62),p1(0x63),p1(0x64),p1(0x65),p1(0x66),p1(0x67) ;\
  2628. + .long p1(0x68),p1(0x69),p1(0x6a),p1(0x6b),p1(0x6c),p1(0x6d),p1(0x6e),p1(0x6f) ;\
  2629. + .long p1(0x70),p1(0x71),p1(0x72),p1(0x73),p1(0x74),p1(0x75),p1(0x76),p1(0x77) ;\
  2630. + .long p1(0x78),p1(0x79),p1(0x7a),p1(0x7b),p1(0x7c),p1(0x7d),p1(0x7e),p1(0x7f)
  2631. +#define im_data4(p1) \
  2632. + .long p1(0x80),p1(0x81),p1(0x82),p1(0x83),p1(0x84),p1(0x85),p1(0x86),p1(0x87) ;\
  2633. + .long p1(0x88),p1(0x89),p1(0x8a),p1(0x8b),p1(0x8c),p1(0x8d),p1(0x8e),p1(0x8f) ;\
  2634. + .long p1(0x90),p1(0x91),p1(0x92),p1(0x93),p1(0x94),p1(0x95),p1(0x96),p1(0x97) ;\
  2635. + .long p1(0x98),p1(0x99),p1(0x9a),p1(0x9b),p1(0x9c),p1(0x9d),p1(0x9e),p1(0x9f)
  2636. +#define im_data5(p1) \
  2637. + .long p1(0xa0),p1(0xa1),p1(0xa2),p1(0xa3),p1(0xa4),p1(0xa5),p1(0xa6),p1(0xa7) ;\
  2638. + .long p1(0xa8),p1(0xa9),p1(0xaa),p1(0xab),p1(0xac),p1(0xad),p1(0xae),p1(0xaf) ;\
  2639. + .long p1(0xb0),p1(0xb1),p1(0xb2),p1(0xb3),p1(0xb4),p1(0xb5),p1(0xb6),p1(0xb7) ;\
  2640. + .long p1(0xb8),p1(0xb9),p1(0xba),p1(0xbb),p1(0xbc),p1(0xbd),p1(0xbe),p1(0xbf)
  2641. +#define im_data6(p1) \
  2642. + .long p1(0xc0),p1(0xc1),p1(0xc2),p1(0xc3),p1(0xc4),p1(0xc5),p1(0xc6),p1(0xc7) ;\
  2643. + .long p1(0xc8),p1(0xc9),p1(0xca),p1(0xcb),p1(0xcc),p1(0xcd),p1(0xce),p1(0xcf) ;\
  2644. + .long p1(0xd0),p1(0xd1),p1(0xd2),p1(0xd3),p1(0xd4),p1(0xd5),p1(0xd6),p1(0xd7) ;\
  2645. + .long p1(0xd8),p1(0xd9),p1(0xda),p1(0xdb),p1(0xdc),p1(0xdd),p1(0xde),p1(0xdf)
  2646. +#define im_data7(p1) \
  2647. + .long p1(0xe0),p1(0xe1),p1(0xe2),p1(0xe3),p1(0xe4),p1(0xe5),p1(0xe6),p1(0xe7) ;\
  2648. + .long p1(0xe8),p1(0xe9),p1(0xea),p1(0xeb),p1(0xec),p1(0xed),p1(0xee),p1(0xef) ;\
  2649. + .long p1(0xf0),p1(0xf1),p1(0xf2),p1(0xf3),p1(0xf4),p1(0xf5),p1(0xf6),p1(0xf7) ;\
  2650. + .long p1(0xf8),p1(0xf9),p1(0xfa),p1(0xfb),p1(0xfc),p1(0xfd),p1(0xfe),p1(0xff)
  2651. +
  2652. +// S-box data - 256 entries
  2653. +
  2654. +#define sb_data0(p1) \
  2655. + .long p1(0x63),p1(0x7c),p1(0x77),p1(0x7b),p1(0xf2),p1(0x6b),p1(0x6f),p1(0xc5) ;\
  2656. + .long p1(0x30),p1(0x01),p1(0x67),p1(0x2b),p1(0xfe),p1(0xd7),p1(0xab),p1(0x76) ;\
  2657. + .long p1(0xca),p1(0x82),p1(0xc9),p1(0x7d),p1(0xfa),p1(0x59),p1(0x47),p1(0xf0) ;\
  2658. + .long p1(0xad),p1(0xd4),p1(0xa2),p1(0xaf),p1(0x9c),p1(0xa4),p1(0x72),p1(0xc0)
  2659. +#define sb_data1(p1) \
  2660. + .long p1(0xb7),p1(0xfd),p1(0x93),p1(0x26),p1(0x36),p1(0x3f),p1(0xf7),p1(0xcc) ;\
  2661. + .long p1(0x34),p1(0xa5),p1(0xe5),p1(0xf1),p1(0x71),p1(0xd8),p1(0x31),p1(0x15) ;\
  2662. + .long p1(0x04),p1(0xc7),p1(0x23),p1(0xc3),p1(0x18),p1(0x96),p1(0x05),p1(0x9a) ;\
  2663. + .long p1(0x07),p1(0x12),p1(0x80),p1(0xe2),p1(0xeb),p1(0x27),p1(0xb2),p1(0x75)
  2664. +#define sb_data2(p1) \
  2665. + .long p1(0x09),p1(0x83),p1(0x2c),p1(0x1a),p1(0x1b),p1(0x6e),p1(0x5a),p1(0xa0) ;\
  2666. + .long p1(0x52),p1(0x3b),p1(0xd6),p1(0xb3),p1(0x29),p1(0xe3),p1(0x2f),p1(0x84) ;\
  2667. + .long p1(0x53),p1(0xd1),p1(0x00),p1(0xed),p1(0x20),p1(0xfc),p1(0xb1),p1(0x5b) ;\
  2668. + .long p1(0x6a),p1(0xcb),p1(0xbe),p1(0x39),p1(0x4a),p1(0x4c),p1(0x58),p1(0xcf)
  2669. +#define sb_data3(p1) \
  2670. + .long p1(0xd0),p1(0xef),p1(0xaa),p1(0xfb),p1(0x43),p1(0x4d),p1(0x33),p1(0x85) ;\
  2671. + .long p1(0x45),p1(0xf9),p1(0x02),p1(0x7f),p1(0x50),p1(0x3c),p1(0x9f),p1(0xa8) ;\
  2672. + .long p1(0x51),p1(0xa3),p1(0x40),p1(0x8f),p1(0x92),p1(0x9d),p1(0x38),p1(0xf5) ;\
  2673. + .long p1(0xbc),p1(0xb6),p1(0xda),p1(0x21),p1(0x10),p1(0xff),p1(0xf3),p1(0xd2)
  2674. +#define sb_data4(p1) \
  2675. + .long p1(0xcd),p1(0x0c),p1(0x13),p1(0xec),p1(0x5f),p1(0x97),p1(0x44),p1(0x17) ;\
  2676. + .long p1(0xc4),p1(0xa7),p1(0x7e),p1(0x3d),p1(0x64),p1(0x5d),p1(0x19),p1(0x73) ;\
  2677. + .long p1(0x60),p1(0x81),p1(0x4f),p1(0xdc),p1(0x22),p1(0x2a),p1(0x90),p1(0x88) ;\
  2678. + .long p1(0x46),p1(0xee),p1(0xb8),p1(0x14),p1(0xde),p1(0x5e),p1(0x0b),p1(0xdb)
  2679. +#define sb_data5(p1) \
  2680. + .long p1(0xe0),p1(0x32),p1(0x3a),p1(0x0a),p1(0x49),p1(0x06),p1(0x24),p1(0x5c) ;\
  2681. + .long p1(0xc2),p1(0xd3),p1(0xac),p1(0x62),p1(0x91),p1(0x95),p1(0xe4),p1(0x79) ;\
  2682. + .long p1(0xe7),p1(0xc8),p1(0x37),p1(0x6d),p1(0x8d),p1(0xd5),p1(0x4e),p1(0xa9) ;\
  2683. + .long p1(0x6c),p1(0x56),p1(0xf4),p1(0xea),p1(0x65),p1(0x7a),p1(0xae),p1(0x08)
  2684. +#define sb_data6(p1) \
  2685. + .long p1(0xba),p1(0x78),p1(0x25),p1(0x2e),p1(0x1c),p1(0xa6),p1(0xb4),p1(0xc6) ;\
  2686. + .long p1(0xe8),p1(0xdd),p1(0x74),p1(0x1f),p1(0x4b),p1(0xbd),p1(0x8b),p1(0x8a) ;\
  2687. + .long p1(0x70),p1(0x3e),p1(0xb5),p1(0x66),p1(0x48),p1(0x03),p1(0xf6),p1(0x0e) ;\
  2688. + .long p1(0x61),p1(0x35),p1(0x57),p1(0xb9),p1(0x86),p1(0xc1),p1(0x1d),p1(0x9e)
  2689. +#define sb_data7(p1) \
  2690. + .long p1(0xe1),p1(0xf8),p1(0x98),p1(0x11),p1(0x69),p1(0xd9),p1(0x8e),p1(0x94) ;\
  2691. + .long p1(0x9b),p1(0x1e),p1(0x87),p1(0xe9),p1(0xce),p1(0x55),p1(0x28),p1(0xdf) ;\
  2692. + .long p1(0x8c),p1(0xa1),p1(0x89),p1(0x0d),p1(0xbf),p1(0xe6),p1(0x42),p1(0x68) ;\
  2693. + .long p1(0x41),p1(0x99),p1(0x2d),p1(0x0f),p1(0xb0),p1(0x54),p1(0xbb),p1(0x16)
  2694. +
  2695. +// Inverse S-box data - 256 entries
  2696. +
  2697. +#define ib_data0(p1) \
  2698. + .long p1(0x52),p1(0x09),p1(0x6a),p1(0xd5),p1(0x30),p1(0x36),p1(0xa5),p1(0x38) ;\
  2699. + .long p1(0xbf),p1(0x40),p1(0xa3),p1(0x9e),p1(0x81),p1(0xf3),p1(0xd7),p1(0xfb) ;\
  2700. + .long p1(0x7c),p1(0xe3),p1(0x39),p1(0x82),p1(0x9b),p1(0x2f),p1(0xff),p1(0x87) ;\
  2701. + .long p1(0x34),p1(0x8e),p1(0x43),p1(0x44),p1(0xc4),p1(0xde),p1(0xe9),p1(0xcb)
  2702. +#define ib_data1(p1) \
  2703. + .long p1(0x54),p1(0x7b),p1(0x94),p1(0x32),p1(0xa6),p1(0xc2),p1(0x23),p1(0x3d) ;\
  2704. + .long p1(0xee),p1(0x4c),p1(0x95),p1(0x0b),p1(0x42),p1(0xfa),p1(0xc3),p1(0x4e) ;\
  2705. + .long p1(0x08),p1(0x2e),p1(0xa1),p1(0x66),p1(0x28),p1(0xd9),p1(0x24),p1(0xb2) ;\
  2706. + .long p1(0x76),p1(0x5b),p1(0xa2),p1(0x49),p1(0x6d),p1(0x8b),p1(0xd1),p1(0x25)
  2707. +#define ib_data2(p1) \
  2708. + .long p1(0x72),p1(0xf8),p1(0xf6),p1(0x64),p1(0x86),p1(0x68),p1(0x98),p1(0x16) ;\
  2709. + .long p1(0xd4),p1(0xa4),p1(0x5c),p1(0xcc),p1(0x5d),p1(0x65),p1(0xb6),p1(0x92) ;\
  2710. + .long p1(0x6c),p1(0x70),p1(0x48),p1(0x50),p1(0xfd),p1(0xed),p1(0xb9),p1(0xda) ;\
  2711. + .long p1(0x5e),p1(0x15),p1(0x46),p1(0x57),p1(0xa7),p1(0x8d),p1(0x9d),p1(0x84)
  2712. +#define ib_data3(p1) \
  2713. + .long p1(0x90),p1(0xd8),p1(0xab),p1(0x00),p1(0x8c),p1(0xbc),p1(0xd3),p1(0x0a) ;\
  2714. + .long p1(0xf7),p1(0xe4),p1(0x58),p1(0x05),p1(0xb8),p1(0xb3),p1(0x45),p1(0x06) ;\
  2715. + .long p1(0xd0),p1(0x2c),p1(0x1e),p1(0x8f),p1(0xca),p1(0x3f),p1(0x0f),p1(0x02) ;\
  2716. + .long p1(0xc1),p1(0xaf),p1(0xbd),p1(0x03),p1(0x01),p1(0x13),p1(0x8a),p1(0x6b)
  2717. +#define ib_data4(p1) \
  2718. + .long p1(0x3a),p1(0x91),p1(0x11),p1(0x41),p1(0x4f),p1(0x67),p1(0xdc),p1(0xea) ;\
  2719. + .long p1(0x97),p1(0xf2),p1(0xcf),p1(0xce),p1(0xf0),p1(0xb4),p1(0xe6),p1(0x73) ;\
  2720. + .long p1(0x96),p1(0xac),p1(0x74),p1(0x22),p1(0xe7),p1(0xad),p1(0x35),p1(0x85) ;\
  2721. + .long p1(0xe2),p1(0xf9),p1(0x37),p1(0xe8),p1(0x1c),p1(0x75),p1(0xdf),p1(0x6e)
  2722. +#define ib_data5(p1) \
  2723. + .long p1(0x47),p1(0xf1),p1(0x1a),p1(0x71),p1(0x1d),p1(0x29),p1(0xc5),p1(0x89) ;\
  2724. + .long p1(0x6f),p1(0xb7),p1(0x62),p1(0x0e),p1(0xaa),p1(0x18),p1(0xbe),p1(0x1b) ;\
  2725. + .long p1(0xfc),p1(0x56),p1(0x3e),p1(0x4b),p1(0xc6),p1(0xd2),p1(0x79),p1(0x20) ;\
  2726. + .long p1(0x9a),p1(0xdb),p1(0xc0),p1(0xfe),p1(0x78),p1(0xcd),p1(0x5a),p1(0xf4)
  2727. +#define ib_data6(p1) \
  2728. + .long p1(0x1f),p1(0xdd),p1(0xa8),p1(0x33),p1(0x88),p1(0x07),p1(0xc7),p1(0x31) ;\
  2729. + .long p1(0xb1),p1(0x12),p1(0x10),p1(0x59),p1(0x27),p1(0x80),p1(0xec),p1(0x5f) ;\
  2730. + .long p1(0x60),p1(0x51),p1(0x7f),p1(0xa9),p1(0x19),p1(0xb5),p1(0x4a),p1(0x0d) ;\
  2731. + .long p1(0x2d),p1(0xe5),p1(0x7a),p1(0x9f),p1(0x93),p1(0xc9),p1(0x9c),p1(0xef)
  2732. +#define ib_data7(p1) \
  2733. + .long p1(0xa0),p1(0xe0),p1(0x3b),p1(0x4d),p1(0xae),p1(0x2a),p1(0xf5),p1(0xb0) ;\
  2734. + .long p1(0xc8),p1(0xeb),p1(0xbb),p1(0x3c),p1(0x83),p1(0x53),p1(0x99),p1(0x61) ;\
  2735. + .long p1(0x17),p1(0x2b),p1(0x04),p1(0x7e),p1(0xba),p1(0x77),p1(0xd6),p1(0x26) ;\
  2736. + .long p1(0xe1),p1(0x69),p1(0x14),p1(0x63),p1(0x55),p1(0x21),p1(0x0c),p1(0x7d)
  2737. +
  2738. +// The rcon_table (needed for the key schedule)
  2739. +//
  2740. +// Here is original Dr Brian Gladman's source code:
  2741. +// _rcon_tab:
  2742. +// %assign x 1
  2743. +// %rep 29
  2744. +// dd x
  2745. +// %assign x f2(x)
  2746. +// %endrep
  2747. +//
  2748. +// Here is precomputed output (it's more portable this way):
  2749. +
  2750. + .align ALIGN32BYTES
  2751. +aes_rcon_tab:
  2752. + .long 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
  2753. + .long 0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f
  2754. + .long 0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4
  2755. + .long 0xb3,0x7d,0xfa,0xef,0xc5
  2756. +
  2757. +// The forward xor tables
  2758. +
  2759. + .align ALIGN32BYTES
  2760. +aes_ft_tab:
  2761. + sb_data0(u0)
  2762. + sb_data1(u0)
  2763. + sb_data2(u0)
  2764. + sb_data3(u0)
  2765. + sb_data4(u0)
  2766. + sb_data5(u0)
  2767. + sb_data6(u0)
  2768. + sb_data7(u0)
  2769. +
  2770. + sb_data0(u1)
  2771. + sb_data1(u1)
  2772. + sb_data2(u1)
  2773. + sb_data3(u1)
  2774. + sb_data4(u1)
  2775. + sb_data5(u1)
  2776. + sb_data6(u1)
  2777. + sb_data7(u1)
  2778. +
  2779. + sb_data0(u2)
  2780. + sb_data1(u2)
  2781. + sb_data2(u2)
  2782. + sb_data3(u2)
  2783. + sb_data4(u2)
  2784. + sb_data5(u2)
  2785. + sb_data6(u2)
  2786. + sb_data7(u2)
  2787. +
  2788. + sb_data0(u3)
  2789. + sb_data1(u3)
  2790. + sb_data2(u3)
  2791. + sb_data3(u3)
  2792. + sb_data4(u3)
  2793. + sb_data5(u3)
  2794. + sb_data6(u3)
  2795. + sb_data7(u3)
  2796. +
  2797. + .align ALIGN32BYTES
  2798. +aes_fl_tab:
  2799. + sb_data0(w0)
  2800. + sb_data1(w0)
  2801. + sb_data2(w0)
  2802. + sb_data3(w0)
  2803. + sb_data4(w0)
  2804. + sb_data5(w0)
  2805. + sb_data6(w0)
  2806. + sb_data7(w0)
  2807. +
  2808. + sb_data0(w1)
  2809. + sb_data1(w1)
  2810. + sb_data2(w1)
  2811. + sb_data3(w1)
  2812. + sb_data4(w1)
  2813. + sb_data5(w1)
  2814. + sb_data6(w1)
  2815. + sb_data7(w1)
  2816. +
  2817. + sb_data0(w2)
  2818. + sb_data1(w2)
  2819. + sb_data2(w2)
  2820. + sb_data3(w2)
  2821. + sb_data4(w2)
  2822. + sb_data5(w2)
  2823. + sb_data6(w2)
  2824. + sb_data7(w2)
  2825. +
  2826. + sb_data0(w3)
  2827. + sb_data1(w3)
  2828. + sb_data2(w3)
  2829. + sb_data3(w3)
  2830. + sb_data4(w3)
  2831. + sb_data5(w3)
  2832. + sb_data6(w3)
  2833. + sb_data7(w3)
  2834. +
  2835. +// The inverse xor tables
  2836. +
  2837. + .align ALIGN32BYTES
  2838. +aes_it_tab:
  2839. + ib_data0(v0)
  2840. + ib_data1(v0)
  2841. + ib_data2(v0)
  2842. + ib_data3(v0)
  2843. + ib_data4(v0)
  2844. + ib_data5(v0)
  2845. + ib_data6(v0)
  2846. + ib_data7(v0)
  2847. +
  2848. + ib_data0(v1)
  2849. + ib_data1(v1)
  2850. + ib_data2(v1)
  2851. + ib_data3(v1)
  2852. + ib_data4(v1)
  2853. + ib_data5(v1)
  2854. + ib_data6(v1)
  2855. + ib_data7(v1)
  2856. +
  2857. + ib_data0(v2)
  2858. + ib_data1(v2)
  2859. + ib_data2(v2)
  2860. + ib_data3(v2)
  2861. + ib_data4(v2)
  2862. + ib_data5(v2)
  2863. + ib_data6(v2)
  2864. + ib_data7(v2)
  2865. +
  2866. + ib_data0(v3)
  2867. + ib_data1(v3)
  2868. + ib_data2(v3)
  2869. + ib_data3(v3)
  2870. + ib_data4(v3)
  2871. + ib_data5(v3)
  2872. + ib_data6(v3)
  2873. + ib_data7(v3)
  2874. +
  2875. + .align ALIGN32BYTES
  2876. +aes_il_tab:
  2877. + ib_data0(w0)
  2878. + ib_data1(w0)
  2879. + ib_data2(w0)
  2880. + ib_data3(w0)
  2881. + ib_data4(w0)
  2882. + ib_data5(w0)
  2883. + ib_data6(w0)
  2884. + ib_data7(w0)
  2885. +
  2886. + ib_data0(w1)
  2887. + ib_data1(w1)
  2888. + ib_data2(w1)
  2889. + ib_data3(w1)
  2890. + ib_data4(w1)
  2891. + ib_data5(w1)
  2892. + ib_data6(w1)
  2893. + ib_data7(w1)
  2894. +
  2895. + ib_data0(w2)
  2896. + ib_data1(w2)
  2897. + ib_data2(w2)
  2898. + ib_data3(w2)
  2899. + ib_data4(w2)
  2900. + ib_data5(w2)
  2901. + ib_data6(w2)
  2902. + ib_data7(w2)
  2903. +
  2904. + ib_data0(w3)
  2905. + ib_data1(w3)
  2906. + ib_data2(w3)
  2907. + ib_data3(w3)
  2908. + ib_data4(w3)
  2909. + ib_data5(w3)
  2910. + ib_data6(w3)
  2911. + ib_data7(w3)
  2912. +
  2913. +// The inverse mix column tables
  2914. +
  2915. + .align ALIGN32BYTES
  2916. +aes_im_tab:
  2917. + im_data0(v0)
  2918. + im_data1(v0)
  2919. + im_data2(v0)
  2920. + im_data3(v0)
  2921. + im_data4(v0)
  2922. + im_data5(v0)
  2923. + im_data6(v0)
  2924. + im_data7(v0)
  2925. +
  2926. + im_data0(v1)
  2927. + im_data1(v1)
  2928. + im_data2(v1)
  2929. + im_data3(v1)
  2930. + im_data4(v1)
  2931. + im_data5(v1)
  2932. + im_data6(v1)
  2933. + im_data7(v1)
  2934. +
  2935. + im_data0(v2)
  2936. + im_data1(v2)
  2937. + im_data2(v2)
  2938. + im_data3(v2)
  2939. + im_data4(v2)
  2940. + im_data5(v2)
  2941. + im_data6(v2)
  2942. + im_data7(v2)
  2943. +
  2944. + im_data0(v3)
  2945. + im_data1(v3)
  2946. + im_data2(v3)
  2947. + im_data3(v3)
  2948. + im_data4(v3)
  2949. + im_data5(v3)
  2950. + im_data6(v3)
  2951. + im_data7(v3)
  2952. diff -pruN linux-2.4.26/drivers/misc/aes.c linux-2.4.26_crypto/drivers/misc/aes.c
  2953. --- linux-2.4.26/drivers/misc/aes.c 1970-01-01 01:00:00.000000000 +0100
  2954. +++ linux-2.4.26_crypto/drivers/misc/aes.c 2004-05-16 14:57:30.000000000 +0200
  2955. @@ -0,0 +1,1479 @@
  2956. +// I retain copyright in this code but I encourage its free use provided
  2957. +// that I don't carry any responsibility for the results. I am especially
  2958. +// happy to see it used in free and open source software. If you do use
  2959. +// it I would appreciate an acknowledgement of its origin in the code or
  2960. +// the product that results and I would also appreciate knowing a little
  2961. +// about the use to which it is being put. I am grateful to Frank Yellin
  2962. +// for some ideas that are used in this implementation.
  2963. +//
  2964. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  2965. +//
  2966. +// This is an implementation of the AES encryption algorithm (Rijndael)
  2967. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  2968. +// to provide both fixed and dynamic block and key lengths and can also
  2969. +// run with either big or little endian internal byte order (see aes.h).
  2970. +// It inputs block and key lengths in bytes with the legal values being
  2971. +// 16, 24 and 32.
  2972. +
  2973. +/*
  2974. + * Modified by Jari Ruusu, May 1 2001
  2975. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  2976. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  2977. + * - Major name space cleanup: Names visible to outside now begin
  2978. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  2979. + * - Removed C++ and DLL support as part of name space cleanup.
  2980. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  2981. + * - Merged precomputed constant tables to aes.c file.
  2982. + * - Removed data alignment restrictions for portability reasons.
  2983. + * - Made block and key lengths accept bit count (128/192/256)
  2984. + * as well byte count (16/24/32).
  2985. + * - Removed all error checks. This change also eliminated the need
  2986. + * to preinitialize the context struct to zero.
  2987. + * - Removed some totally unused constants.
  2988. + */
  2989. +/*
  2990. + * Modified by Jari Ruusu, April 21 2004
  2991. + * - Added back code that avoids byte swaps on big endian boxes.
  2992. + */
  2993. +
  2994. +#include "aes.h"
  2995. +
  2996. +// CONFIGURATION OPTIONS (see also aes.h)
  2997. +//
  2998. +// 1. Define UNROLL for full loop unrolling in encryption and decryption.
  2999. +// 2. Define PARTIAL_UNROLL to unroll two loops in encryption and decryption.
  3000. +// 3. Define FIXED_TABLES for compiled rather than dynamic tables.
  3001. +// 4. Define FF_TABLES to use tables for field multiplies and inverses.
  3002. +// Do not enable this without understanding stack space requirements.
  3003. +// 5. Define ARRAYS to use arrays to hold the local state block. If this
  3004. +// is not defined, individually declared 32-bit words are used.
  3005. +// 6. Define FAST_VARIABLE if a high speed variable block implementation
  3006. +// is needed (essentially three separate fixed block size code sequences)
  3007. +// 7. Define either ONE_TABLE or FOUR_TABLES for a fast table driven
  3008. +// version using 1 table (2 kbytes of table space) or 4 tables (8
  3009. +// kbytes of table space) for higher speed.
  3010. +// 8. Define either ONE_LR_TABLE or FOUR_LR_TABLES for a further speed
  3011. +// increase by using tables for the last rounds but with more table
  3012. +// space (2 or 8 kbytes extra).
  3013. +// 9. If neither ONE_TABLE nor FOUR_TABLES is defined, a compact but
  3014. +// slower version is provided.
  3015. +// 10. If fast decryption key scheduling is needed define ONE_IM_TABLE
  3016. +// or FOUR_IM_TABLES for higher speed (2 or 8 kbytes extra).
  3017. +
  3018. +#define UNROLL
  3019. +//#define PARTIAL_UNROLL
  3020. +
  3021. +#define FIXED_TABLES
  3022. +//#define FF_TABLES
  3023. +//#define ARRAYS
  3024. +#define FAST_VARIABLE
  3025. +
  3026. +//#define ONE_TABLE
  3027. +#define FOUR_TABLES
  3028. +
  3029. +//#define ONE_LR_TABLE
  3030. +#define FOUR_LR_TABLES
  3031. +
  3032. +//#define ONE_IM_TABLE
  3033. +#define FOUR_IM_TABLES
  3034. +
  3035. +#if defined(UNROLL) && defined (PARTIAL_UNROLL)
  3036. +#error both UNROLL and PARTIAL_UNROLL are defined
  3037. +#endif
  3038. +
  3039. +#if defined(ONE_TABLE) && defined (FOUR_TABLES)
  3040. +#error both ONE_TABLE and FOUR_TABLES are defined
  3041. +#endif
  3042. +
  3043. +#if defined(ONE_LR_TABLE) && defined (FOUR_LR_TABLES)
  3044. +#error both ONE_LR_TABLE and FOUR_LR_TABLES are defined
  3045. +#endif
  3046. +
  3047. +#if defined(ONE_IM_TABLE) && defined (FOUR_IM_TABLES)
  3048. +#error both ONE_IM_TABLE and FOUR_IM_TABLES are defined
  3049. +#endif
  3050. +
  3051. +#if defined(AES_BLOCK_SIZE) && AES_BLOCK_SIZE != 16 && AES_BLOCK_SIZE != 24 && AES_BLOCK_SIZE != 32
  3052. +#error an illegal block size has been specified
  3053. +#endif
  3054. +
  3055. +/* INTERNAL_BYTE_ORDER: 0=unknown, 1=little endian, 2=big endian */
  3056. +#if defined(INTERNAL_BYTE_ORDER)
  3057. +#elif defined(__i386__)||defined(__i386)||defined(__x86_64__)||defined(__x86_64)||defined(__amd64__)||defined(__amd64)||defined(__AMD64__)||defined(__AMD64)
  3058. +# define INTERNAL_BYTE_ORDER 1
  3059. +# undef DATA_ALWAYS_ALIGNED
  3060. +# define DATA_ALWAYS_ALIGNED 1 /* unaligned access is always ok */
  3061. +#elif defined(__ppc__)||defined(__ppc)||defined(__PPC__)||defined(__PPC)||defined(__powerpc__)||defined(__powerpc)||defined(__POWERPC__)||defined(__POWERPC)||defined(__PowerPC__)||defined(__PowerPC)||defined(__ppc64__)||defined(__ppc64)||defined(__PPC64__)||defined(__PPC64)||defined(__powerpc64__)||defined(__powerpc64)||defined(__s390__)||defined(__s390)
  3062. +# define INTERNAL_BYTE_ORDER 2
  3063. +# undef DATA_ALWAYS_ALIGNED
  3064. +# define DATA_ALWAYS_ALIGNED 1 /* unaligned access is always ok */
  3065. +#elif defined(__alpha__)||defined(__alpha)||defined(__ia64__)||defined(__ia64)
  3066. +# define INTERNAL_BYTE_ORDER 1
  3067. +#elif defined(__hppa__)||defined(__hppa)||defined(__HPPA__)||defined(__HPPA)||defined(__parisc__)||defined(__parisc)||defined(__sparc__)||defined(__sparc)||defined(__sparc_v9__)||defined(__sparc_v9)||defined(__sparc64__)||defined(__sparc64)||defined(__mc68000__)||defined(__mc68000)
  3068. +# define INTERNAL_BYTE_ORDER 2
  3069. +#elif defined(CONFIGURE_DETECTS_BYTE_ORDER)
  3070. +# if defined(WORDS_BIGENDIAN)
  3071. +# define INTERNAL_BYTE_ORDER 2
  3072. +# else
  3073. +# define INTERNAL_BYTE_ORDER 1
  3074. +# endif
  3075. +#elif defined(__linux__) && defined(__KERNEL__)
  3076. +# include <asm/byteorder.h>
  3077. +# if defined(__BIG_ENDIAN)
  3078. +# define INTERNAL_BYTE_ORDER 2
  3079. +# else
  3080. +# define INTERNAL_BYTE_ORDER 1
  3081. +# endif
  3082. +#else
  3083. +# include <sys/param.h>
  3084. +# if (defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && (BYTE_ORDER == LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN))
  3085. +# define INTERNAL_BYTE_ORDER 1
  3086. +# elif defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) || (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN))
  3087. +# define INTERNAL_BYTE_ORDER 2
  3088. +# else
  3089. +# define INTERNAL_BYTE_ORDER 0
  3090. +# endif
  3091. +#endif
  3092. +
  3093. +#if defined(DATA_ALWAYS_ALIGNED) && (INTERNAL_BYTE_ORDER > 0)
  3094. +# define word_in(x) *(u_int32_t*)(x)
  3095. +# define word_out(x,v) *(u_int32_t*)(x) = (v)
  3096. +#elif defined(__linux__) && defined(__KERNEL__)
  3097. +# include <asm/unaligned.h>
  3098. +# define word_in(x) get_unaligned((u_int32_t*)(x))
  3099. +# define word_out(x,v) put_unaligned((v),(u_int32_t*)(x))
  3100. +#else
  3101. +/* unknown endianness and/or unable to handle unaligned data */
  3102. +# undef INTERNAL_BYTE_ORDER
  3103. +# define INTERNAL_BYTE_ORDER 1
  3104. +# define word_in(x) ((u_int32_t)(((unsigned char *)(x))[0])|((u_int32_t)(((unsigned char *)(x))[1])<<8)|((u_int32_t)(((unsigned char *)(x))[2])<<16)|((u_int32_t)(((unsigned char *)(x))[3])<<24))
  3105. +# define word_out(x,v) ((unsigned char *)(x))[0]=(v),((unsigned char *)(x))[1]=((v)>>8),((unsigned char *)(x))[2]=((v)>>16),((unsigned char *)(x))[3]=((v)>>24)
  3106. +#endif
  3107. +
  3108. +// upr(x,n): rotates bytes within words by n positions, moving bytes
  3109. +// to higher index positions with wrap around into low positions
  3110. +// ups(x,n): moves bytes by n positions to higher index positions in
  3111. +// words but without wrap around
  3112. +// bval(x,n): extracts a byte from a word
  3113. +
  3114. +#if (INTERNAL_BYTE_ORDER < 2)
  3115. +/* little endian */
  3116. +#define upr(x,n) (((x) << 8 * (n)) | ((x) >> (32 - 8 * (n))))
  3117. +#define ups(x,n) ((x) << 8 * (n))
  3118. +#define bval(x,n) ((unsigned char)((x) >> 8 * (n)))
  3119. +#define bytes2word(b0, b1, b2, b3) \
  3120. + ((u_int32_t)(b3) << 24 | (u_int32_t)(b2) << 16 | (u_int32_t)(b1) << 8 | (b0))
  3121. +#else
  3122. +/* big endian */
  3123. +#define upr(x,n) (((x) >> 8 * (n)) | ((x) << (32 - 8 * (n))))
  3124. +#define ups(x,n) ((x) >> 8 * (n)))
  3125. +#define bval(x,n) ((unsigned char)((x) >> (24 - 8 * (n))))
  3126. +#define bytes2word(b0, b1, b2, b3) \
  3127. + ((u_int32_t)(b0) << 24 | (u_int32_t)(b1) << 16 | (u_int32_t)(b2) << 8 | (b3))
  3128. +#endif
  3129. +
  3130. +// Disable at least some poor combinations of options
  3131. +
  3132. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  3133. +#define FIXED_TABLES
  3134. +#undef UNROLL
  3135. +#undef ONE_LR_TABLE
  3136. +#undef FOUR_LR_TABLES
  3137. +#undef ONE_IM_TABLE
  3138. +#undef FOUR_IM_TABLES
  3139. +#elif !defined(FOUR_TABLES)
  3140. +#ifdef FOUR_LR_TABLES
  3141. +#undef FOUR_LR_TABLES
  3142. +#define ONE_LR_TABLE
  3143. +#endif
  3144. +#ifdef FOUR_IM_TABLES
  3145. +#undef FOUR_IM_TABLES
  3146. +#define ONE_IM_TABLE
  3147. +#endif
  3148. +#elif !defined(AES_BLOCK_SIZE)
  3149. +#if defined(UNROLL)
  3150. +#define PARTIAL_UNROLL
  3151. +#undef UNROLL
  3152. +#endif
  3153. +#endif
  3154. +
  3155. +// the finite field modular polynomial and elements
  3156. +
  3157. +#define ff_poly 0x011b
  3158. +#define ff_hi 0x80
  3159. +
  3160. +// multiply four bytes in GF(2^8) by 'x' {02} in parallel
  3161. +
  3162. +#define m1 0x80808080
  3163. +#define m2 0x7f7f7f7f
  3164. +#define m3 0x0000001b
  3165. +#define FFmulX(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * m3))
  3166. +
  3167. +// The following defines provide alternative definitions of FFmulX that might
  3168. +// give improved performance if a fast 32-bit multiply is not available. Note
  3169. +// that a temporary variable u needs to be defined where FFmulX is used.
  3170. +
  3171. +// #define FFmulX(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
  3172. +// #define m4 0x1b1b1b1b
  3173. +// #define FFmulX(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
  3174. +
  3175. +// perform column mix operation on four bytes in parallel
  3176. +
  3177. +#define fwd_mcol(x) (f2 = FFmulX(x), f2 ^ upr(x ^ f2,3) ^ upr(x,2) ^ upr(x,1))
  3178. +
  3179. +#if defined(FIXED_TABLES)
  3180. +
  3181. +// the S-Box table
  3182. +
  3183. +static const unsigned char s_box[256] =
  3184. +{
  3185. + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  3186. + 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  3187. + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  3188. + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  3189. + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  3190. + 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  3191. + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  3192. + 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  3193. + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  3194. + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  3195. + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  3196. + 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  3197. + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  3198. + 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  3199. + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  3200. + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  3201. + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  3202. + 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  3203. + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  3204. + 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  3205. + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  3206. + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  3207. + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  3208. + 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  3209. + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  3210. + 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  3211. + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  3212. + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  3213. + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  3214. + 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  3215. + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  3216. + 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  3217. +};
  3218. +
  3219. +// the inverse S-Box table
  3220. +
  3221. +static const unsigned char inv_s_box[256] =
  3222. +{
  3223. + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
  3224. + 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  3225. + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
  3226. + 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  3227. + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d,
  3228. + 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  3229. + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2,
  3230. + 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  3231. + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
  3232. + 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  3233. + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda,
  3234. + 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  3235. + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
  3236. + 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  3237. + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
  3238. + 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  3239. + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea,
  3240. + 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  3241. + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85,
  3242. + 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  3243. + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
  3244. + 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  3245. + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20,
  3246. + 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  3247. + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31,
  3248. + 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  3249. + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
  3250. + 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  3251. + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0,
  3252. + 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  3253. + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26,
  3254. + 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  3255. +};
  3256. +
  3257. +// used to ensure table is generated in the right format
  3258. +// depending on the internal byte order required
  3259. +
  3260. +#if (INTERNAL_BYTE_ORDER < 2)
  3261. +/* little endian */
  3262. +#define w0(p) 0x000000##p
  3263. +#else
  3264. +/* big endian */
  3265. +#define w0(p) 0x##p##000000
  3266. +#endif
  3267. +
  3268. +// Number of elements required in this table for different
  3269. +// block and key lengths is:
  3270. +//
  3271. +// Nk = 4 6 8
  3272. +// ----------
  3273. +// Nb = 4 | 10 8 7
  3274. +// 6 | 19 12 11
  3275. +// 8 | 29 19 14
  3276. +//
  3277. +// this table can be a table of bytes if the key schedule
  3278. +// code is adjusted accordingly
  3279. +
  3280. +static const u_int32_t rcon_tab[29] =
  3281. +{
  3282. + w0(01), w0(02), w0(04), w0(08),
  3283. + w0(10), w0(20), w0(40), w0(80),
  3284. + w0(1b), w0(36), w0(6c), w0(d8),
  3285. + w0(ab), w0(4d), w0(9a), w0(2f),
  3286. + w0(5e), w0(bc), w0(63), w0(c6),
  3287. + w0(97), w0(35), w0(6a), w0(d4),
  3288. + w0(b3), w0(7d), w0(fa), w0(ef),
  3289. + w0(c5)
  3290. +};
  3291. +
  3292. +#undef w0
  3293. +
  3294. +// used to ensure table is generated in the right format
  3295. +// depending on the internal byte order required
  3296. +
  3297. +#if (INTERNAL_BYTE_ORDER < 2)
  3298. +/* little endian */
  3299. +#define r0(p,q,r,s) 0x##p##q##r##s
  3300. +#define r1(p,q,r,s) 0x##q##r##s##p
  3301. +#define r2(p,q,r,s) 0x##r##s##p##q
  3302. +#define r3(p,q,r,s) 0x##s##p##q##r
  3303. +#define w0(p) 0x000000##p
  3304. +#define w1(p) 0x0000##p##00
  3305. +#define w2(p) 0x00##p##0000
  3306. +#define w3(p) 0x##p##000000
  3307. +#else
  3308. +/* big endian */
  3309. +#define r0(p,q,r,s) 0x##s##r##q##p
  3310. +#define r1(p,q,r,s) 0x##p##s##r##q
  3311. +#define r2(p,q,r,s) 0x##q##p##s##r
  3312. +#define r3(p,q,r,s) 0x##r##q##p##s
  3313. +#define w0(p) 0x##p##000000
  3314. +#define w1(p) 0x00##p##0000
  3315. +#define w2(p) 0x0000##p##00
  3316. +#define w3(p) 0x000000##p
  3317. +#endif
  3318. +
  3319. +#if defined(FIXED_TABLES) && (defined(ONE_TABLE) || defined(FOUR_TABLES))
  3320. +
  3321. +// data for forward tables (other than last round)
  3322. +
  3323. +#define f_table \
  3324. + r(a5,63,63,c6), r(84,7c,7c,f8), r(99,77,77,ee), r(8d,7b,7b,f6),\
  3325. + r(0d,f2,f2,ff), r(bd,6b,6b,d6), r(b1,6f,6f,de), r(54,c5,c5,91),\
  3326. + r(50,30,30,60), r(03,01,01,02), r(a9,67,67,ce), r(7d,2b,2b,56),\
  3327. + r(19,fe,fe,e7), r(62,d7,d7,b5), r(e6,ab,ab,4d), r(9a,76,76,ec),\
  3328. + r(45,ca,ca,8f), r(9d,82,82,1f), r(40,c9,c9,89), r(87,7d,7d,fa),\
  3329. + r(15,fa,fa,ef), r(eb,59,59,b2), r(c9,47,47,8e), r(0b,f0,f0,fb),\
  3330. + r(ec,ad,ad,41), r(67,d4,d4,b3), r(fd,a2,a2,5f), r(ea,af,af,45),\
  3331. + r(bf,9c,9c,23), r(f7,a4,a4,53), r(96,72,72,e4), r(5b,c0,c0,9b),\
  3332. + r(c2,b7,b7,75), r(1c,fd,fd,e1), r(ae,93,93,3d), r(6a,26,26,4c),\
  3333. + r(5a,36,36,6c), r(41,3f,3f,7e), r(02,f7,f7,f5), r(4f,cc,cc,83),\
  3334. + r(5c,34,34,68), r(f4,a5,a5,51), r(34,e5,e5,d1), r(08,f1,f1,f9),\
  3335. + r(93,71,71,e2), r(73,d8,d8,ab), r(53,31,31,62), r(3f,15,15,2a),\
  3336. + r(0c,04,04,08), r(52,c7,c7,95), r(65,23,23,46), r(5e,c3,c3,9d),\
  3337. + r(28,18,18,30), r(a1,96,96,37), r(0f,05,05,0a), r(b5,9a,9a,2f),\
  3338. + r(09,07,07,0e), r(36,12,12,24), r(9b,80,80,1b), r(3d,e2,e2,df),\
  3339. + r(26,eb,eb,cd), r(69,27,27,4e), r(cd,b2,b2,7f), r(9f,75,75,ea),\
  3340. + r(1b,09,09,12), r(9e,83,83,1d), r(74,2c,2c,58), r(2e,1a,1a,34),\
  3341. + r(2d,1b,1b,36), r(b2,6e,6e,dc), r(ee,5a,5a,b4), r(fb,a0,a0,5b),\
  3342. + r(f6,52,52,a4), r(4d,3b,3b,76), r(61,d6,d6,b7), r(ce,b3,b3,7d),\
  3343. + r(7b,29,29,52), r(3e,e3,e3,dd), r(71,2f,2f,5e), r(97,84,84,13),\
  3344. + r(f5,53,53,a6), r(68,d1,d1,b9), r(00,00,00,00), r(2c,ed,ed,c1),\
  3345. + r(60,20,20,40), r(1f,fc,fc,e3), r(c8,b1,b1,79), r(ed,5b,5b,b6),\
  3346. + r(be,6a,6a,d4), r(46,cb,cb,8d), r(d9,be,be,67), r(4b,39,39,72),\
  3347. + r(de,4a,4a,94), r(d4,4c,4c,98), r(e8,58,58,b0), r(4a,cf,cf,85),\
  3348. + r(6b,d0,d0,bb), r(2a,ef,ef,c5), r(e5,aa,aa,4f), r(16,fb,fb,ed),\
  3349. + r(c5,43,43,86), r(d7,4d,4d,9a), r(55,33,33,66), r(94,85,85,11),\
  3350. + r(cf,45,45,8a), r(10,f9,f9,e9), r(06,02,02,04), r(81,7f,7f,fe),\
  3351. + r(f0,50,50,a0), r(44,3c,3c,78), r(ba,9f,9f,25), r(e3,a8,a8,4b),\
  3352. + r(f3,51,51,a2), r(fe,a3,a3,5d), r(c0,40,40,80), r(8a,8f,8f,05),\
  3353. + r(ad,92,92,3f), r(bc,9d,9d,21), r(48,38,38,70), r(04,f5,f5,f1),\
  3354. + r(df,bc,bc,63), r(c1,b6,b6,77), r(75,da,da,af), r(63,21,21,42),\
  3355. + r(30,10,10,20), r(1a,ff,ff,e5), r(0e,f3,f3,fd), r(6d,d2,d2,bf),\
  3356. + r(4c,cd,cd,81), r(14,0c,0c,18), r(35,13,13,26), r(2f,ec,ec,c3),\
  3357. + r(e1,5f,5f,be), r(a2,97,97,35), r(cc,44,44,88), r(39,17,17,2e),\
  3358. + r(57,c4,c4,93), r(f2,a7,a7,55), r(82,7e,7e,fc), r(47,3d,3d,7a),\
  3359. + r(ac,64,64,c8), r(e7,5d,5d,ba), r(2b,19,19,32), r(95,73,73,e6),\
  3360. + r(a0,60,60,c0), r(98,81,81,19), r(d1,4f,4f,9e), r(7f,dc,dc,a3),\
  3361. + r(66,22,22,44), r(7e,2a,2a,54), r(ab,90,90,3b), r(83,88,88,0b),\
  3362. + r(ca,46,46,8c), r(29,ee,ee,c7), r(d3,b8,b8,6b), r(3c,14,14,28),\
  3363. + r(79,de,de,a7), r(e2,5e,5e,bc), r(1d,0b,0b,16), r(76,db,db,ad),\
  3364. + r(3b,e0,e0,db), r(56,32,32,64), r(4e,3a,3a,74), r(1e,0a,0a,14),\
  3365. + r(db,49,49,92), r(0a,06,06,0c), r(6c,24,24,48), r(e4,5c,5c,b8),\
  3366. + r(5d,c2,c2,9f), r(6e,d3,d3,bd), r(ef,ac,ac,43), r(a6,62,62,c4),\
  3367. + r(a8,91,91,39), r(a4,95,95,31), r(37,e4,e4,d3), r(8b,79,79,f2),\
  3368. + r(32,e7,e7,d5), r(43,c8,c8,8b), r(59,37,37,6e), r(b7,6d,6d,da),\
  3369. + r(8c,8d,8d,01), r(64,d5,d5,b1), r(d2,4e,4e,9c), r(e0,a9,a9,49),\
  3370. + r(b4,6c,6c,d8), r(fa,56,56,ac), r(07,f4,f4,f3), r(25,ea,ea,cf),\
  3371. + r(af,65,65,ca), r(8e,7a,7a,f4), r(e9,ae,ae,47), r(18,08,08,10),\
  3372. + r(d5,ba,ba,6f), r(88,78,78,f0), r(6f,25,25,4a), r(72,2e,2e,5c),\
  3373. + r(24,1c,1c,38), r(f1,a6,a6,57), r(c7,b4,b4,73), r(51,c6,c6,97),\
  3374. + r(23,e8,e8,cb), r(7c,dd,dd,a1), r(9c,74,74,e8), r(21,1f,1f,3e),\
  3375. + r(dd,4b,4b,96), r(dc,bd,bd,61), r(86,8b,8b,0d), r(85,8a,8a,0f),\
  3376. + r(90,70,70,e0), r(42,3e,3e,7c), r(c4,b5,b5,71), r(aa,66,66,cc),\
  3377. + r(d8,48,48,90), r(05,03,03,06), r(01,f6,f6,f7), r(12,0e,0e,1c),\
  3378. + r(a3,61,61,c2), r(5f,35,35,6a), r(f9,57,57,ae), r(d0,b9,b9,69),\
  3379. + r(91,86,86,17), r(58,c1,c1,99), r(27,1d,1d,3a), r(b9,9e,9e,27),\
  3380. + r(38,e1,e1,d9), r(13,f8,f8,eb), r(b3,98,98,2b), r(33,11,11,22),\
  3381. + r(bb,69,69,d2), r(70,d9,d9,a9), r(89,8e,8e,07), r(a7,94,94,33),\
  3382. + r(b6,9b,9b,2d), r(22,1e,1e,3c), r(92,87,87,15), r(20,e9,e9,c9),\
  3383. + r(49,ce,ce,87), r(ff,55,55,aa), r(78,28,28,50), r(7a,df,df,a5),\
  3384. + r(8f,8c,8c,03), r(f8,a1,a1,59), r(80,89,89,09), r(17,0d,0d,1a),\
  3385. + r(da,bf,bf,65), r(31,e6,e6,d7), r(c6,42,42,84), r(b8,68,68,d0),\
  3386. + r(c3,41,41,82), r(b0,99,99,29), r(77,2d,2d,5a), r(11,0f,0f,1e),\
  3387. + r(cb,b0,b0,7b), r(fc,54,54,a8), r(d6,bb,bb,6d), r(3a,16,16,2c)
  3388. +
  3389. +// data for inverse tables (other than last round)
  3390. +
  3391. +#define i_table \
  3392. + r(50,a7,f4,51), r(53,65,41,7e), r(c3,a4,17,1a), r(96,5e,27,3a),\
  3393. + r(cb,6b,ab,3b), r(f1,45,9d,1f), r(ab,58,fa,ac), r(93,03,e3,4b),\
  3394. + r(55,fa,30,20), r(f6,6d,76,ad), r(91,76,cc,88), r(25,4c,02,f5),\
  3395. + r(fc,d7,e5,4f), r(d7,cb,2a,c5), r(80,44,35,26), r(8f,a3,62,b5),\
  3396. + r(49,5a,b1,de), r(67,1b,ba,25), r(98,0e,ea,45), r(e1,c0,fe,5d),\
  3397. + r(02,75,2f,c3), r(12,f0,4c,81), r(a3,97,46,8d), r(c6,f9,d3,6b),\
  3398. + r(e7,5f,8f,03), r(95,9c,92,15), r(eb,7a,6d,bf), r(da,59,52,95),\
  3399. + r(2d,83,be,d4), r(d3,21,74,58), r(29,69,e0,49), r(44,c8,c9,8e),\
  3400. + r(6a,89,c2,75), r(78,79,8e,f4), r(6b,3e,58,99), r(dd,71,b9,27),\
  3401. + r(b6,4f,e1,be), r(17,ad,88,f0), r(66,ac,20,c9), r(b4,3a,ce,7d),\
  3402. + r(18,4a,df,63), r(82,31,1a,e5), r(60,33,51,97), r(45,7f,53,62),\
  3403. + r(e0,77,64,b1), r(84,ae,6b,bb), r(1c,a0,81,fe), r(94,2b,08,f9),\
  3404. + r(58,68,48,70), r(19,fd,45,8f), r(87,6c,de,94), r(b7,f8,7b,52),\
  3405. + r(23,d3,73,ab), r(e2,02,4b,72), r(57,8f,1f,e3), r(2a,ab,55,66),\
  3406. + r(07,28,eb,b2), r(03,c2,b5,2f), r(9a,7b,c5,86), r(a5,08,37,d3),\
  3407. + r(f2,87,28,30), r(b2,a5,bf,23), r(ba,6a,03,02), r(5c,82,16,ed),\
  3408. + r(2b,1c,cf,8a), r(92,b4,79,a7), r(f0,f2,07,f3), r(a1,e2,69,4e),\
  3409. + r(cd,f4,da,65), r(d5,be,05,06), r(1f,62,34,d1), r(8a,fe,a6,c4),\
  3410. + r(9d,53,2e,34), r(a0,55,f3,a2), r(32,e1,8a,05), r(75,eb,f6,a4),\
  3411. + r(39,ec,83,0b), r(aa,ef,60,40), r(06,9f,71,5e), r(51,10,6e,bd),\
  3412. + r(f9,8a,21,3e), r(3d,06,dd,96), r(ae,05,3e,dd), r(46,bd,e6,4d),\
  3413. + r(b5,8d,54,91), r(05,5d,c4,71), r(6f,d4,06,04), r(ff,15,50,60),\
  3414. + r(24,fb,98,19), r(97,e9,bd,d6), r(cc,43,40,89), r(77,9e,d9,67),\
  3415. + r(bd,42,e8,b0), r(88,8b,89,07), r(38,5b,19,e7), r(db,ee,c8,79),\
  3416. + r(47,0a,7c,a1), r(e9,0f,42,7c), r(c9,1e,84,f8), r(00,00,00,00),\
  3417. + r(83,86,80,09), r(48,ed,2b,32), r(ac,70,11,1e), r(4e,72,5a,6c),\
  3418. + r(fb,ff,0e,fd), r(56,38,85,0f), r(1e,d5,ae,3d), r(27,39,2d,36),\
  3419. + r(64,d9,0f,0a), r(21,a6,5c,68), r(d1,54,5b,9b), r(3a,2e,36,24),\
  3420. + r(b1,67,0a,0c), r(0f,e7,57,93), r(d2,96,ee,b4), r(9e,91,9b,1b),\
  3421. + r(4f,c5,c0,80), r(a2,20,dc,61), r(69,4b,77,5a), r(16,1a,12,1c),\
  3422. + r(0a,ba,93,e2), r(e5,2a,a0,c0), r(43,e0,22,3c), r(1d,17,1b,12),\
  3423. + r(0b,0d,09,0e), r(ad,c7,8b,f2), r(b9,a8,b6,2d), r(c8,a9,1e,14),\
  3424. + r(85,19,f1,57), r(4c,07,75,af), r(bb,dd,99,ee), r(fd,60,7f,a3),\
  3425. + r(9f,26,01,f7), r(bc,f5,72,5c), r(c5,3b,66,44), r(34,7e,fb,5b),\
  3426. + r(76,29,43,8b), r(dc,c6,23,cb), r(68,fc,ed,b6), r(63,f1,e4,b8),\
  3427. + r(ca,dc,31,d7), r(10,85,63,42), r(40,22,97,13), r(20,11,c6,84),\
  3428. + r(7d,24,4a,85), r(f8,3d,bb,d2), r(11,32,f9,ae), r(6d,a1,29,c7),\
  3429. + r(4b,2f,9e,1d), r(f3,30,b2,dc), r(ec,52,86,0d), r(d0,e3,c1,77),\
  3430. + r(6c,16,b3,2b), r(99,b9,70,a9), r(fa,48,94,11), r(22,64,e9,47),\
  3431. + r(c4,8c,fc,a8), r(1a,3f,f0,a0), r(d8,2c,7d,56), r(ef,90,33,22),\
  3432. + r(c7,4e,49,87), r(c1,d1,38,d9), r(fe,a2,ca,8c), r(36,0b,d4,98),\
  3433. + r(cf,81,f5,a6), r(28,de,7a,a5), r(26,8e,b7,da), r(a4,bf,ad,3f),\
  3434. + r(e4,9d,3a,2c), r(0d,92,78,50), r(9b,cc,5f,6a), r(62,46,7e,54),\
  3435. + r(c2,13,8d,f6), r(e8,b8,d8,90), r(5e,f7,39,2e), r(f5,af,c3,82),\
  3436. + r(be,80,5d,9f), r(7c,93,d0,69), r(a9,2d,d5,6f), r(b3,12,25,cf),\
  3437. + r(3b,99,ac,c8), r(a7,7d,18,10), r(6e,63,9c,e8), r(7b,bb,3b,db),\
  3438. + r(09,78,26,cd), r(f4,18,59,6e), r(01,b7,9a,ec), r(a8,9a,4f,83),\
  3439. + r(65,6e,95,e6), r(7e,e6,ff,aa), r(08,cf,bc,21), r(e6,e8,15,ef),\
  3440. + r(d9,9b,e7,ba), r(ce,36,6f,4a), r(d4,09,9f,ea), r(d6,7c,b0,29),\
  3441. + r(af,b2,a4,31), r(31,23,3f,2a), r(30,94,a5,c6), r(c0,66,a2,35),\
  3442. + r(37,bc,4e,74), r(a6,ca,82,fc), r(b0,d0,90,e0), r(15,d8,a7,33),\
  3443. + r(4a,98,04,f1), r(f7,da,ec,41), r(0e,50,cd,7f), r(2f,f6,91,17),\
  3444. + r(8d,d6,4d,76), r(4d,b0,ef,43), r(54,4d,aa,cc), r(df,04,96,e4),\
  3445. + r(e3,b5,d1,9e), r(1b,88,6a,4c), r(b8,1f,2c,c1), r(7f,51,65,46),\
  3446. + r(04,ea,5e,9d), r(5d,35,8c,01), r(73,74,87,fa), r(2e,41,0b,fb),\
  3447. + r(5a,1d,67,b3), r(52,d2,db,92), r(33,56,10,e9), r(13,47,d6,6d),\
  3448. + r(8c,61,d7,9a), r(7a,0c,a1,37), r(8e,14,f8,59), r(89,3c,13,eb),\
  3449. + r(ee,27,a9,ce), r(35,c9,61,b7), r(ed,e5,1c,e1), r(3c,b1,47,7a),\
  3450. + r(59,df,d2,9c), r(3f,73,f2,55), r(79,ce,14,18), r(bf,37,c7,73),\
  3451. + r(ea,cd,f7,53), r(5b,aa,fd,5f), r(14,6f,3d,df), r(86,db,44,78),\
  3452. + r(81,f3,af,ca), r(3e,c4,68,b9), r(2c,34,24,38), r(5f,40,a3,c2),\
  3453. + r(72,c3,1d,16), r(0c,25,e2,bc), r(8b,49,3c,28), r(41,95,0d,ff),\
  3454. + r(71,01,a8,39), r(de,b3,0c,08), r(9c,e4,b4,d8), r(90,c1,56,64),\
  3455. + r(61,84,cb,7b), r(70,b6,32,d5), r(74,5c,6c,48), r(42,57,b8,d0)
  3456. +
  3457. +// generate the required tables in the desired endian format
  3458. +
  3459. +#undef r
  3460. +#define r r0
  3461. +
  3462. +#if defined(ONE_TABLE)
  3463. +static const u_int32_t ft_tab[256] =
  3464. + { f_table };
  3465. +#elif defined(FOUR_TABLES)
  3466. +static const u_int32_t ft_tab[4][256] =
  3467. +{ { f_table },
  3468. +#undef r
  3469. +#define r r1
  3470. + { f_table },
  3471. +#undef r
  3472. +#define r r2
  3473. + { f_table },
  3474. +#undef r
  3475. +#define r r3
  3476. + { f_table }
  3477. +};
  3478. +#endif
  3479. +
  3480. +#undef r
  3481. +#define r r0
  3482. +#if defined(ONE_TABLE)
  3483. +static const u_int32_t it_tab[256] =
  3484. + { i_table };
  3485. +#elif defined(FOUR_TABLES)
  3486. +static const u_int32_t it_tab[4][256] =
  3487. +{ { i_table },
  3488. +#undef r
  3489. +#define r r1
  3490. + { i_table },
  3491. +#undef r
  3492. +#define r r2
  3493. + { i_table },
  3494. +#undef r
  3495. +#define r r3
  3496. + { i_table }
  3497. +};
  3498. +#endif
  3499. +
  3500. +#endif
  3501. +
  3502. +#if defined(FIXED_TABLES) && (defined(ONE_LR_TABLE) || defined(FOUR_LR_TABLES))
  3503. +
  3504. +// data for inverse tables (last round)
  3505. +
  3506. +#define li_table \
  3507. + w(52), w(09), w(6a), w(d5), w(30), w(36), w(a5), w(38),\
  3508. + w(bf), w(40), w(a3), w(9e), w(81), w(f3), w(d7), w(fb),\
  3509. + w(7c), w(e3), w(39), w(82), w(9b), w(2f), w(ff), w(87),\
  3510. + w(34), w(8e), w(43), w(44), w(c4), w(de), w(e9), w(cb),\
  3511. + w(54), w(7b), w(94), w(32), w(a6), w(c2), w(23), w(3d),\
  3512. + w(ee), w(4c), w(95), w(0b), w(42), w(fa), w(c3), w(4e),\
  3513. + w(08), w(2e), w(a1), w(66), w(28), w(d9), w(24), w(b2),\
  3514. + w(76), w(5b), w(a2), w(49), w(6d), w(8b), w(d1), w(25),\
  3515. + w(72), w(f8), w(f6), w(64), w(86), w(68), w(98), w(16),\
  3516. + w(d4), w(a4), w(5c), w(cc), w(5d), w(65), w(b6), w(92),\
  3517. + w(6c), w(70), w(48), w(50), w(fd), w(ed), w(b9), w(da),\
  3518. + w(5e), w(15), w(46), w(57), w(a7), w(8d), w(9d), w(84),\
  3519. + w(90), w(d8), w(ab), w(00), w(8c), w(bc), w(d3), w(0a),\
  3520. + w(f7), w(e4), w(58), w(05), w(b8), w(b3), w(45), w(06),\
  3521. + w(d0), w(2c), w(1e), w(8f), w(ca), w(3f), w(0f), w(02),\
  3522. + w(c1), w(af), w(bd), w(03), w(01), w(13), w(8a), w(6b),\
  3523. + w(3a), w(91), w(11), w(41), w(4f), w(67), w(dc), w(ea),\
  3524. + w(97), w(f2), w(cf), w(ce), w(f0), w(b4), w(e6), w(73),\
  3525. + w(96), w(ac), w(74), w(22), w(e7), w(ad), w(35), w(85),\
  3526. + w(e2), w(f9), w(37), w(e8), w(1c), w(75), w(df), w(6e),\
  3527. + w(47), w(f1), w(1a), w(71), w(1d), w(29), w(c5), w(89),\
  3528. + w(6f), w(b7), w(62), w(0e), w(aa), w(18), w(be), w(1b),\
  3529. + w(fc), w(56), w(3e), w(4b), w(c6), w(d2), w(79), w(20),\
  3530. + w(9a), w(db), w(c0), w(fe), w(78), w(cd), w(5a), w(f4),\
  3531. + w(1f), w(dd), w(a8), w(33), w(88), w(07), w(c7), w(31),\
  3532. + w(b1), w(12), w(10), w(59), w(27), w(80), w(ec), w(5f),\
  3533. + w(60), w(51), w(7f), w(a9), w(19), w(b5), w(4a), w(0d),\
  3534. + w(2d), w(e5), w(7a), w(9f), w(93), w(c9), w(9c), w(ef),\
  3535. + w(a0), w(e0), w(3b), w(4d), w(ae), w(2a), w(f5), w(b0),\
  3536. + w(c8), w(eb), w(bb), w(3c), w(83), w(53), w(99), w(61),\
  3537. + w(17), w(2b), w(04), w(7e), w(ba), w(77), w(d6), w(26),\
  3538. + w(e1), w(69), w(14), w(63), w(55), w(21), w(0c), w(7d),
  3539. +
  3540. +// generate the required tables in the desired endian format
  3541. +
  3542. +#undef r
  3543. +#define r(p,q,r,s) w0(q)
  3544. +#if defined(ONE_LR_TABLE)
  3545. +static const u_int32_t fl_tab[256] =
  3546. + { f_table };
  3547. +#elif defined(FOUR_LR_TABLES)
  3548. +static const u_int32_t fl_tab[4][256] =
  3549. +{ { f_table },
  3550. +#undef r
  3551. +#define r(p,q,r,s) w1(q)
  3552. + { f_table },
  3553. +#undef r
  3554. +#define r(p,q,r,s) w2(q)
  3555. + { f_table },
  3556. +#undef r
  3557. +#define r(p,q,r,s) w3(q)
  3558. + { f_table }
  3559. +};
  3560. +#endif
  3561. +
  3562. +#undef w
  3563. +#define w w0
  3564. +#if defined(ONE_LR_TABLE)
  3565. +static const u_int32_t il_tab[256] =
  3566. + { li_table };
  3567. +#elif defined(FOUR_LR_TABLES)
  3568. +static const u_int32_t il_tab[4][256] =
  3569. +{ { li_table },
  3570. +#undef w
  3571. +#define w w1
  3572. + { li_table },
  3573. +#undef w
  3574. +#define w w2
  3575. + { li_table },
  3576. +#undef w
  3577. +#define w w3
  3578. + { li_table }
  3579. +};
  3580. +#endif
  3581. +
  3582. +#endif
  3583. +
  3584. +#if defined(FIXED_TABLES) && (defined(ONE_IM_TABLE) || defined(FOUR_IM_TABLES))
  3585. +
  3586. +#define m_table \
  3587. + r(00,00,00,00), r(0b,0d,09,0e), r(16,1a,12,1c), r(1d,17,1b,12),\
  3588. + r(2c,34,24,38), r(27,39,2d,36), r(3a,2e,36,24), r(31,23,3f,2a),\
  3589. + r(58,68,48,70), r(53,65,41,7e), r(4e,72,5a,6c), r(45,7f,53,62),\
  3590. + r(74,5c,6c,48), r(7f,51,65,46), r(62,46,7e,54), r(69,4b,77,5a),\
  3591. + r(b0,d0,90,e0), r(bb,dd,99,ee), r(a6,ca,82,fc), r(ad,c7,8b,f2),\
  3592. + r(9c,e4,b4,d8), r(97,e9,bd,d6), r(8a,fe,a6,c4), r(81,f3,af,ca),\
  3593. + r(e8,b8,d8,90), r(e3,b5,d1,9e), r(fe,a2,ca,8c), r(f5,af,c3,82),\
  3594. + r(c4,8c,fc,a8), r(cf,81,f5,a6), r(d2,96,ee,b4), r(d9,9b,e7,ba),\
  3595. + r(7b,bb,3b,db), r(70,b6,32,d5), r(6d,a1,29,c7), r(66,ac,20,c9),\
  3596. + r(57,8f,1f,e3), r(5c,82,16,ed), r(41,95,0d,ff), r(4a,98,04,f1),\
  3597. + r(23,d3,73,ab), r(28,de,7a,a5), r(35,c9,61,b7), r(3e,c4,68,b9),\
  3598. + r(0f,e7,57,93), r(04,ea,5e,9d), r(19,fd,45,8f), r(12,f0,4c,81),\
  3599. + r(cb,6b,ab,3b), r(c0,66,a2,35), r(dd,71,b9,27), r(d6,7c,b0,29),\
  3600. + r(e7,5f,8f,03), r(ec,52,86,0d), r(f1,45,9d,1f), r(fa,48,94,11),\
  3601. + r(93,03,e3,4b), r(98,0e,ea,45), r(85,19,f1,57), r(8e,14,f8,59),\
  3602. + r(bf,37,c7,73), r(b4,3a,ce,7d), r(a9,2d,d5,6f), r(a2,20,dc,61),\
  3603. + r(f6,6d,76,ad), r(fd,60,7f,a3), r(e0,77,64,b1), r(eb,7a,6d,bf),\
  3604. + r(da,59,52,95), r(d1,54,5b,9b), r(cc,43,40,89), r(c7,4e,49,87),\
  3605. + r(ae,05,3e,dd), r(a5,08,37,d3), r(b8,1f,2c,c1), r(b3,12,25,cf),\
  3606. + r(82,31,1a,e5), r(89,3c,13,eb), r(94,2b,08,f9), r(9f,26,01,f7),\
  3607. + r(46,bd,e6,4d), r(4d,b0,ef,43), r(50,a7,f4,51), r(5b,aa,fd,5f),\
  3608. + r(6a,89,c2,75), r(61,84,cb,7b), r(7c,93,d0,69), r(77,9e,d9,67),\
  3609. + r(1e,d5,ae,3d), r(15,d8,a7,33), r(08,cf,bc,21), r(03,c2,b5,2f),\
  3610. + r(32,e1,8a,05), r(39,ec,83,0b), r(24,fb,98,19), r(2f,f6,91,17),\
  3611. + r(8d,d6,4d,76), r(86,db,44,78), r(9b,cc,5f,6a), r(90,c1,56,64),\
  3612. + r(a1,e2,69,4e), r(aa,ef,60,40), r(b7,f8,7b,52), r(bc,f5,72,5c),\
  3613. + r(d5,be,05,06), r(de,b3,0c,08), r(c3,a4,17,1a), r(c8,a9,1e,14),\
  3614. + r(f9,8a,21,3e), r(f2,87,28,30), r(ef,90,33,22), r(e4,9d,3a,2c),\
  3615. + r(3d,06,dd,96), r(36,0b,d4,98), r(2b,1c,cf,8a), r(20,11,c6,84),\
  3616. + r(11,32,f9,ae), r(1a,3f,f0,a0), r(07,28,eb,b2), r(0c,25,e2,bc),\
  3617. + r(65,6e,95,e6), r(6e,63,9c,e8), r(73,74,87,fa), r(78,79,8e,f4),\
  3618. + r(49,5a,b1,de), r(42,57,b8,d0), r(5f,40,a3,c2), r(54,4d,aa,cc),\
  3619. + r(f7,da,ec,41), r(fc,d7,e5,4f), r(e1,c0,fe,5d), r(ea,cd,f7,53),\
  3620. + r(db,ee,c8,79), r(d0,e3,c1,77), r(cd,f4,da,65), r(c6,f9,d3,6b),\
  3621. + r(af,b2,a4,31), r(a4,bf,ad,3f), r(b9,a8,b6,2d), r(b2,a5,bf,23),\
  3622. + r(83,86,80,09), r(88,8b,89,07), r(95,9c,92,15), r(9e,91,9b,1b),\
  3623. + r(47,0a,7c,a1), r(4c,07,75,af), r(51,10,6e,bd), r(5a,1d,67,b3),\
  3624. + r(6b,3e,58,99), r(60,33,51,97), r(7d,24,4a,85), r(76,29,43,8b),\
  3625. + r(1f,62,34,d1), r(14,6f,3d,df), r(09,78,26,cd), r(02,75,2f,c3),\
  3626. + r(33,56,10,e9), r(38,5b,19,e7), r(25,4c,02,f5), r(2e,41,0b,fb),\
  3627. + r(8c,61,d7,9a), r(87,6c,de,94), r(9a,7b,c5,86), r(91,76,cc,88),\
  3628. + r(a0,55,f3,a2), r(ab,58,fa,ac), r(b6,4f,e1,be), r(bd,42,e8,b0),\
  3629. + r(d4,09,9f,ea), r(df,04,96,e4), r(c2,13,8d,f6), r(c9,1e,84,f8),\
  3630. + r(f8,3d,bb,d2), r(f3,30,b2,dc), r(ee,27,a9,ce), r(e5,2a,a0,c0),\
  3631. + r(3c,b1,47,7a), r(37,bc,4e,74), r(2a,ab,55,66), r(21,a6,5c,68),\
  3632. + r(10,85,63,42), r(1b,88,6a,4c), r(06,9f,71,5e), r(0d,92,78,50),\
  3633. + r(64,d9,0f,0a), r(6f,d4,06,04), r(72,c3,1d,16), r(79,ce,14,18),\
  3634. + r(48,ed,2b,32), r(43,e0,22,3c), r(5e,f7,39,2e), r(55,fa,30,20),\
  3635. + r(01,b7,9a,ec), r(0a,ba,93,e2), r(17,ad,88,f0), r(1c,a0,81,fe),\
  3636. + r(2d,83,be,d4), r(26,8e,b7,da), r(3b,99,ac,c8), r(30,94,a5,c6),\
  3637. + r(59,df,d2,9c), r(52,d2,db,92), r(4f,c5,c0,80), r(44,c8,c9,8e),\
  3638. + r(75,eb,f6,a4), r(7e,e6,ff,aa), r(63,f1,e4,b8), r(68,fc,ed,b6),\
  3639. + r(b1,67,0a,0c), r(ba,6a,03,02), r(a7,7d,18,10), r(ac,70,11,1e),\
  3640. + r(9d,53,2e,34), r(96,5e,27,3a), r(8b,49,3c,28), r(80,44,35,26),\
  3641. + r(e9,0f,42,7c), r(e2,02,4b,72), r(ff,15,50,60), r(f4,18,59,6e),\
  3642. + r(c5,3b,66,44), r(ce,36,6f,4a), r(d3,21,74,58), r(d8,2c,7d,56),\
  3643. + r(7a,0c,a1,37), r(71,01,a8,39), r(6c,16,b3,2b), r(67,1b,ba,25),\
  3644. + r(56,38,85,0f), r(5d,35,8c,01), r(40,22,97,13), r(4b,2f,9e,1d),\
  3645. + r(22,64,e9,47), r(29,69,e0,49), r(34,7e,fb,5b), r(3f,73,f2,55),\
  3646. + r(0e,50,cd,7f), r(05,5d,c4,71), r(18,4a,df,63), r(13,47,d6,6d),\
  3647. + r(ca,dc,31,d7), r(c1,d1,38,d9), r(dc,c6,23,cb), r(d7,cb,2a,c5),\
  3648. + r(e6,e8,15,ef), r(ed,e5,1c,e1), r(f0,f2,07,f3), r(fb,ff,0e,fd),\
  3649. + r(92,b4,79,a7), r(99,b9,70,a9), r(84,ae,6b,bb), r(8f,a3,62,b5),\
  3650. + r(be,80,5d,9f), r(b5,8d,54,91), r(a8,9a,4f,83), r(a3,97,46,8d)
  3651. +
  3652. +#undef r
  3653. +#define r r0
  3654. +
  3655. +#if defined(ONE_IM_TABLE)
  3656. +static const u_int32_t im_tab[256] =
  3657. + { m_table };
  3658. +#elif defined(FOUR_IM_TABLES)
  3659. +static const u_int32_t im_tab[4][256] =
  3660. +{ { m_table },
  3661. +#undef r
  3662. +#define r r1
  3663. + { m_table },
  3664. +#undef r
  3665. +#define r r2
  3666. + { m_table },
  3667. +#undef r
  3668. +#define r r3
  3669. + { m_table }
  3670. +};
  3671. +#endif
  3672. +
  3673. +#endif
  3674. +
  3675. +#else
  3676. +
  3677. +static int tab_gen = 0;
  3678. +
  3679. +static unsigned char s_box[256]; // the S box
  3680. +static unsigned char inv_s_box[256]; // the inverse S box
  3681. +static u_int32_t rcon_tab[AES_RC_LENGTH]; // table of round constants
  3682. +
  3683. +#if defined(ONE_TABLE)
  3684. +static u_int32_t ft_tab[256];
  3685. +static u_int32_t it_tab[256];
  3686. +#elif defined(FOUR_TABLES)
  3687. +static u_int32_t ft_tab[4][256];
  3688. +static u_int32_t it_tab[4][256];
  3689. +#endif
  3690. +
  3691. +#if defined(ONE_LR_TABLE)
  3692. +static u_int32_t fl_tab[256];
  3693. +static u_int32_t il_tab[256];
  3694. +#elif defined(FOUR_LR_TABLES)
  3695. +static u_int32_t fl_tab[4][256];
  3696. +static u_int32_t il_tab[4][256];
  3697. +#endif
  3698. +
  3699. +#if defined(ONE_IM_TABLE)
  3700. +static u_int32_t im_tab[256];
  3701. +#elif defined(FOUR_IM_TABLES)
  3702. +static u_int32_t im_tab[4][256];
  3703. +#endif
  3704. +
  3705. +// Generate the tables for the dynamic table option
  3706. +
  3707. +#if !defined(FF_TABLES)
  3708. +
  3709. +// It will generally be sensible to use tables to compute finite
  3710. +// field multiplies and inverses but where memory is scarse this
  3711. +// code might sometimes be better.
  3712. +
  3713. +// return 2 ^ (n - 1) where n is the bit number of the highest bit
  3714. +// set in x with x in the range 1 < x < 0x00000200. This form is
  3715. +// used so that locals within FFinv can be bytes rather than words
  3716. +
  3717. +static unsigned char hibit(const u_int32_t x)
  3718. +{ unsigned char r = (unsigned char)((x >> 1) | (x >> 2));
  3719. +
  3720. + r |= (r >> 2);
  3721. + r |= (r >> 4);
  3722. + return (r + 1) >> 1;
  3723. +}
  3724. +
  3725. +// return the inverse of the finite field element x
  3726. +
  3727. +static unsigned char FFinv(const unsigned char x)
  3728. +{ unsigned char p1 = x, p2 = 0x1b, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  3729. +
  3730. + if(x < 2) return x;
  3731. +
  3732. + for(;;)
  3733. + {
  3734. + if(!n1) return v1;
  3735. +
  3736. + while(n2 >= n1)
  3737. + {
  3738. + n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
  3739. + }
  3740. +
  3741. + if(!n2) return v2;
  3742. +
  3743. + while(n1 >= n2)
  3744. + {
  3745. + n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
  3746. + }
  3747. + }
  3748. +}
  3749. +
  3750. +// define the finite field multiplies required for Rijndael
  3751. +
  3752. +#define FFmul02(x) ((((x) & 0x7f) << 1) ^ ((x) & 0x80 ? 0x1b : 0))
  3753. +#define FFmul03(x) ((x) ^ FFmul02(x))
  3754. +#define FFmul09(x) ((x) ^ FFmul02(FFmul02(FFmul02(x))))
  3755. +#define FFmul0b(x) ((x) ^ FFmul02((x) ^ FFmul02(FFmul02(x))))
  3756. +#define FFmul0d(x) ((x) ^ FFmul02(FFmul02((x) ^ FFmul02(x))))
  3757. +#define FFmul0e(x) FFmul02((x) ^ FFmul02((x) ^ FFmul02(x)))
  3758. +
  3759. +#else
  3760. +
  3761. +#define FFinv(x) ((x) ? pow[255 - log[x]]: 0)
  3762. +
  3763. +#define FFmul02(x) (x ? pow[log[x] + 0x19] : 0)
  3764. +#define FFmul03(x) (x ? pow[log[x] + 0x01] : 0)
  3765. +#define FFmul09(x) (x ? pow[log[x] + 0xc7] : 0)
  3766. +#define FFmul0b(x) (x ? pow[log[x] + 0x68] : 0)
  3767. +#define FFmul0d(x) (x ? pow[log[x] + 0xee] : 0)
  3768. +#define FFmul0e(x) (x ? pow[log[x] + 0xdf] : 0)
  3769. +
  3770. +#endif
  3771. +
  3772. +// The forward and inverse affine transformations used in the S-box
  3773. +
  3774. +#define fwd_affine(x) \
  3775. + (w = (u_int32_t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(unsigned char)(w^(w>>8)))
  3776. +
  3777. +#define inv_affine(x) \
  3778. + (w = (u_int32_t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(unsigned char)(w^(w>>8)))
  3779. +
  3780. +static void gen_tabs(void)
  3781. +{ u_int32_t i, w;
  3782. +
  3783. +#if defined(FF_TABLES)
  3784. +
  3785. + unsigned char pow[512], log[256];
  3786. +
  3787. + // log and power tables for GF(2^8) finite field with
  3788. + // 0x011b as modular polynomial - the simplest primitive
  3789. + // root is 0x03, used here to generate the tables
  3790. +
  3791. + i = 0; w = 1;
  3792. + do
  3793. + {
  3794. + pow[i] = (unsigned char)w;
  3795. + pow[i + 255] = (unsigned char)w;
  3796. + log[w] = (unsigned char)i++;
  3797. + w ^= (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  3798. + }
  3799. + while (w != 1);
  3800. +
  3801. +#endif
  3802. +
  3803. + for(i = 0, w = 1; i < AES_RC_LENGTH; ++i)
  3804. + {
  3805. + rcon_tab[i] = bytes2word(w, 0, 0, 0);
  3806. + w = (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  3807. + }
  3808. +
  3809. + for(i = 0; i < 256; ++i)
  3810. + { unsigned char b;
  3811. +
  3812. + s_box[i] = b = fwd_affine(FFinv((unsigned char)i));
  3813. +
  3814. + w = bytes2word(b, 0, 0, 0);
  3815. +#if defined(ONE_LR_TABLE)
  3816. + fl_tab[i] = w;
  3817. +#elif defined(FOUR_LR_TABLES)
  3818. + fl_tab[0][i] = w;
  3819. + fl_tab[1][i] = upr(w,1);
  3820. + fl_tab[2][i] = upr(w,2);
  3821. + fl_tab[3][i] = upr(w,3);
  3822. +#endif
  3823. + w = bytes2word(FFmul02(b), b, b, FFmul03(b));
  3824. +#if defined(ONE_TABLE)
  3825. + ft_tab[i] = w;
  3826. +#elif defined(FOUR_TABLES)
  3827. + ft_tab[0][i] = w;
  3828. + ft_tab[1][i] = upr(w,1);
  3829. + ft_tab[2][i] = upr(w,2);
  3830. + ft_tab[3][i] = upr(w,3);
  3831. +#endif
  3832. + inv_s_box[i] = b = FFinv(inv_affine((unsigned char)i));
  3833. +
  3834. + w = bytes2word(b, 0, 0, 0);
  3835. +#if defined(ONE_LR_TABLE)
  3836. + il_tab[i] = w;
  3837. +#elif defined(FOUR_LR_TABLES)
  3838. + il_tab[0][i] = w;
  3839. + il_tab[1][i] = upr(w,1);
  3840. + il_tab[2][i] = upr(w,2);
  3841. + il_tab[3][i] = upr(w,3);
  3842. +#endif
  3843. + w = bytes2word(FFmul0e(b), FFmul09(b), FFmul0d(b), FFmul0b(b));
  3844. +#if defined(ONE_TABLE)
  3845. + it_tab[i] = w;
  3846. +#elif defined(FOUR_TABLES)
  3847. + it_tab[0][i] = w;
  3848. + it_tab[1][i] = upr(w,1);
  3849. + it_tab[2][i] = upr(w,2);
  3850. + it_tab[3][i] = upr(w,3);
  3851. +#endif
  3852. +#if defined(ONE_IM_TABLE)
  3853. + im_tab[b] = w;
  3854. +#elif defined(FOUR_IM_TABLES)
  3855. + im_tab[0][b] = w;
  3856. + im_tab[1][b] = upr(w,1);
  3857. + im_tab[2][b] = upr(w,2);
  3858. + im_tab[3][b] = upr(w,3);
  3859. +#endif
  3860. +
  3861. + }
  3862. +}
  3863. +
  3864. +#endif
  3865. +
  3866. +#define no_table(x,box,vf,rf,c) bytes2word( \
  3867. + box[bval(vf(x,0,c),rf(0,c))], \
  3868. + box[bval(vf(x,1,c),rf(1,c))], \
  3869. + box[bval(vf(x,2,c),rf(2,c))], \
  3870. + box[bval(vf(x,3,c),rf(3,c))])
  3871. +
  3872. +#define one_table(x,op,tab,vf,rf,c) \
  3873. + ( tab[bval(vf(x,0,c),rf(0,c))] \
  3874. + ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
  3875. + ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
  3876. + ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
  3877. +
  3878. +#define four_tables(x,tab,vf,rf,c) \
  3879. + ( tab[0][bval(vf(x,0,c),rf(0,c))] \
  3880. + ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
  3881. + ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
  3882. + ^ tab[3][bval(vf(x,3,c),rf(3,c))])
  3883. +
  3884. +#define vf1(x,r,c) (x)
  3885. +#define rf1(r,c) (r)
  3886. +#define rf2(r,c) ((r-c)&3)
  3887. +
  3888. +#if defined(FOUR_LR_TABLES)
  3889. +#define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
  3890. +#elif defined(ONE_LR_TABLE)
  3891. +#define ls_box(x,c) one_table(x,upr,fl_tab,vf1,rf2,c)
  3892. +#else
  3893. +#define ls_box(x,c) no_table(x,s_box,vf1,rf2,c)
  3894. +#endif
  3895. +
  3896. +#if defined(FOUR_IM_TABLES)
  3897. +#define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
  3898. +#elif defined(ONE_IM_TABLE)
  3899. +#define inv_mcol(x) one_table(x,upr,im_tab,vf1,rf1,0)
  3900. +#else
  3901. +#define inv_mcol(x) \
  3902. + (f9 = (x),f2 = FFmulX(f9), f4 = FFmulX(f2), f8 = FFmulX(f4), f9 ^= f8, \
  3903. + f2 ^= f4 ^ f8 ^ upr(f2 ^ f9,3) ^ upr(f4 ^ f9,2) ^ upr(f9,1))
  3904. +#endif
  3905. +
  3906. +// Subroutine to set the block size (if variable) in bytes, legal
  3907. +// values being 16, 24 and 32.
  3908. +
  3909. +#if defined(AES_BLOCK_SIZE)
  3910. +#define nc (AES_BLOCK_SIZE / 4)
  3911. +#else
  3912. +#define nc (cx->aes_Ncol)
  3913. +
  3914. +void aes_set_blk(aes_context *cx, int n_bytes)
  3915. +{
  3916. +#if !defined(FIXED_TABLES)
  3917. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  3918. +#endif
  3919. +
  3920. + switch(n_bytes) {
  3921. + case 32: /* bytes */
  3922. + case 256: /* bits */
  3923. + nc = 8;
  3924. + break;
  3925. + case 24: /* bytes */
  3926. + case 192: /* bits */
  3927. + nc = 6;
  3928. + break;
  3929. + case 16: /* bytes */
  3930. + case 128: /* bits */
  3931. + default:
  3932. + nc = 4;
  3933. + break;
  3934. + }
  3935. +}
  3936. +
  3937. +#endif
  3938. +
  3939. +// Initialise the key schedule from the user supplied key. The key
  3940. +// length is now specified in bytes - 16, 24 or 32 as appropriate.
  3941. +// This corresponds to bit lengths of 128, 192 and 256 bits, and
  3942. +// to Nk values of 4, 6 and 8 respectively.
  3943. +
  3944. +#define mx(t,f) (*t++ = inv_mcol(*f),f++)
  3945. +#define cp(t,f) *t++ = *f++
  3946. +
  3947. +#if AES_BLOCK_SIZE == 16
  3948. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  3949. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  3950. +#elif AES_BLOCK_SIZE == 24
  3951. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  3952. + cp(d,s); cp(d,s)
  3953. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  3954. + mx(d,s); mx(d,s)
  3955. +#elif AES_BLOCK_SIZE == 32
  3956. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  3957. + cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  3958. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  3959. + mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  3960. +#else
  3961. +
  3962. +#define cpy(d,s) \
  3963. +switch(nc) \
  3964. +{ case 8: cp(d,s); cp(d,s); \
  3965. + case 6: cp(d,s); cp(d,s); \
  3966. + case 4: cp(d,s); cp(d,s); \
  3967. + cp(d,s); cp(d,s); \
  3968. +}
  3969. +
  3970. +#define mix(d,s) \
  3971. +switch(nc) \
  3972. +{ case 8: mx(d,s); mx(d,s); \
  3973. + case 6: mx(d,s); mx(d,s); \
  3974. + case 4: mx(d,s); mx(d,s); \
  3975. + mx(d,s); mx(d,s); \
  3976. +}
  3977. +
  3978. +#endif
  3979. +
  3980. +void aes_set_key(aes_context *cx, const unsigned char in_key[], int n_bytes, const int f)
  3981. +{ u_int32_t *kf, *kt, rci;
  3982. +
  3983. +#if !defined(FIXED_TABLES)
  3984. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  3985. +#endif
  3986. +
  3987. + switch(n_bytes) {
  3988. + case 32: /* bytes */
  3989. + case 256: /* bits */
  3990. + cx->aes_Nkey = 8;
  3991. + break;
  3992. + case 24: /* bytes */
  3993. + case 192: /* bits */
  3994. + cx->aes_Nkey = 6;
  3995. + break;
  3996. + case 16: /* bytes */
  3997. + case 128: /* bits */
  3998. + default:
  3999. + cx->aes_Nkey = 4;
  4000. + break;
  4001. + }
  4002. +
  4003. + cx->aes_Nrnd = (cx->aes_Nkey > nc ? cx->aes_Nkey : nc) + 6;
  4004. +
  4005. + cx->aes_e_key[0] = word_in(in_key );
  4006. + cx->aes_e_key[1] = word_in(in_key + 4);
  4007. + cx->aes_e_key[2] = word_in(in_key + 8);
  4008. + cx->aes_e_key[3] = word_in(in_key + 12);
  4009. +
  4010. + kf = cx->aes_e_key;
  4011. + kt = kf + nc * (cx->aes_Nrnd + 1) - cx->aes_Nkey;
  4012. + rci = 0;
  4013. +
  4014. + switch(cx->aes_Nkey)
  4015. + {
  4016. + case 4: do
  4017. + { kf[4] = kf[0] ^ ls_box(kf[3],3) ^ rcon_tab[rci++];
  4018. + kf[5] = kf[1] ^ kf[4];
  4019. + kf[6] = kf[2] ^ kf[5];
  4020. + kf[7] = kf[3] ^ kf[6];
  4021. + kf += 4;
  4022. + }
  4023. + while(kf < kt);
  4024. + break;
  4025. +
  4026. + case 6: cx->aes_e_key[4] = word_in(in_key + 16);
  4027. + cx->aes_e_key[5] = word_in(in_key + 20);
  4028. + do
  4029. + { kf[ 6] = kf[0] ^ ls_box(kf[5],3) ^ rcon_tab[rci++];
  4030. + kf[ 7] = kf[1] ^ kf[ 6];
  4031. + kf[ 8] = kf[2] ^ kf[ 7];
  4032. + kf[ 9] = kf[3] ^ kf[ 8];
  4033. + kf[10] = kf[4] ^ kf[ 9];
  4034. + kf[11] = kf[5] ^ kf[10];
  4035. + kf += 6;
  4036. + }
  4037. + while(kf < kt);
  4038. + break;
  4039. +
  4040. + case 8: cx->aes_e_key[4] = word_in(in_key + 16);
  4041. + cx->aes_e_key[5] = word_in(in_key + 20);
  4042. + cx->aes_e_key[6] = word_in(in_key + 24);
  4043. + cx->aes_e_key[7] = word_in(in_key + 28);
  4044. + do
  4045. + { kf[ 8] = kf[0] ^ ls_box(kf[7],3) ^ rcon_tab[rci++];
  4046. + kf[ 9] = kf[1] ^ kf[ 8];
  4047. + kf[10] = kf[2] ^ kf[ 9];
  4048. + kf[11] = kf[3] ^ kf[10];
  4049. + kf[12] = kf[4] ^ ls_box(kf[11],0);
  4050. + kf[13] = kf[5] ^ kf[12];
  4051. + kf[14] = kf[6] ^ kf[13];
  4052. + kf[15] = kf[7] ^ kf[14];
  4053. + kf += 8;
  4054. + }
  4055. + while (kf < kt);
  4056. + break;
  4057. + }
  4058. +
  4059. + if(!f)
  4060. + { u_int32_t i;
  4061. +
  4062. + kt = cx->aes_d_key + nc * cx->aes_Nrnd;
  4063. + kf = cx->aes_e_key;
  4064. +
  4065. + cpy(kt, kf); kt -= 2 * nc;
  4066. +
  4067. + for(i = 1; i < cx->aes_Nrnd; ++i)
  4068. + {
  4069. +#if defined(ONE_TABLE) || defined(FOUR_TABLES)
  4070. +#if !defined(ONE_IM_TABLE) && !defined(FOUR_IM_TABLES)
  4071. + u_int32_t f2, f4, f8, f9;
  4072. +#endif
  4073. + mix(kt, kf);
  4074. +#else
  4075. + cpy(kt, kf);
  4076. +#endif
  4077. + kt -= 2 * nc;
  4078. + }
  4079. +
  4080. + cpy(kt, kf);
  4081. + }
  4082. +}
  4083. +
  4084. +// y = output word, x = input word, r = row, c = column
  4085. +// for r = 0, 1, 2 and 3 = column accessed for row r
  4086. +
  4087. +#if defined(ARRAYS)
  4088. +#define s(x,c) x[c]
  4089. +#else
  4090. +#define s(x,c) x##c
  4091. +#endif
  4092. +
  4093. +// I am grateful to Frank Yellin for the following constructions
  4094. +// which, given the column (c) of the output state variable that
  4095. +// is being computed, return the input state variables which are
  4096. +// needed for each row (r) of the state
  4097. +
  4098. +// For the fixed block size options, compilers reduce these two
  4099. +// expressions to fixed variable references. For variable block
  4100. +// size code conditional clauses will sometimes be returned
  4101. +
  4102. +#define unused 77 // Sunset Strip
  4103. +
  4104. +#define fwd_var(x,r,c) \
  4105. + ( r==0 ? \
  4106. + ( c==0 ? s(x,0) \
  4107. + : c==1 ? s(x,1) \
  4108. + : c==2 ? s(x,2) \
  4109. + : c==3 ? s(x,3) \
  4110. + : c==4 ? s(x,4) \
  4111. + : c==5 ? s(x,5) \
  4112. + : c==6 ? s(x,6) \
  4113. + : s(x,7)) \
  4114. + : r==1 ? \
  4115. + ( c==0 ? s(x,1) \
  4116. + : c==1 ? s(x,2) \
  4117. + : c==2 ? s(x,3) \
  4118. + : c==3 ? nc==4 ? s(x,0) : s(x,4) \
  4119. + : c==4 ? s(x,5) \
  4120. + : c==5 ? nc==8 ? s(x,6) : s(x,0) \
  4121. + : c==6 ? s(x,7) \
  4122. + : s(x,0)) \
  4123. + : r==2 ? \
  4124. + ( c==0 ? nc==8 ? s(x,3) : s(x,2) \
  4125. + : c==1 ? nc==8 ? s(x,4) : s(x,3) \
  4126. + : c==2 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  4127. + : c==3 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  4128. + : c==4 ? nc==8 ? s(x,7) : s(x,0) \
  4129. + : c==5 ? nc==8 ? s(x,0) : s(x,1) \
  4130. + : c==6 ? s(x,1) \
  4131. + : s(x,2)) \
  4132. + : \
  4133. + ( c==0 ? nc==8 ? s(x,4) : s(x,3) \
  4134. + : c==1 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  4135. + : c==2 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  4136. + : c==3 ? nc==4 ? s(x,2) : nc==8 ? s(x,7) : s(x,0) \
  4137. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  4138. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  4139. + : c==6 ? s(x,2) \
  4140. + : s(x,3)))
  4141. +
  4142. +#define inv_var(x,r,c) \
  4143. + ( r==0 ? \
  4144. + ( c==0 ? s(x,0) \
  4145. + : c==1 ? s(x,1) \
  4146. + : c==2 ? s(x,2) \
  4147. + : c==3 ? s(x,3) \
  4148. + : c==4 ? s(x,4) \
  4149. + : c==5 ? s(x,5) \
  4150. + : c==6 ? s(x,6) \
  4151. + : s(x,7)) \
  4152. + : r==1 ? \
  4153. + ( c==0 ? nc==4 ? s(x,3) : nc==8 ? s(x,7) : s(x,5) \
  4154. + : c==1 ? s(x,0) \
  4155. + : c==2 ? s(x,1) \
  4156. + : c==3 ? s(x,2) \
  4157. + : c==4 ? s(x,3) \
  4158. + : c==5 ? s(x,4) \
  4159. + : c==6 ? s(x,5) \
  4160. + : s(x,6)) \
  4161. + : r==2 ? \
  4162. + ( c==0 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  4163. + : c==1 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  4164. + : c==2 ? nc==8 ? s(x,7) : s(x,0) \
  4165. + : c==3 ? nc==8 ? s(x,0) : s(x,1) \
  4166. + : c==4 ? nc==8 ? s(x,1) : s(x,2) \
  4167. + : c==5 ? nc==8 ? s(x,2) : s(x,3) \
  4168. + : c==6 ? s(x,3) \
  4169. + : s(x,4)) \
  4170. + : \
  4171. + ( c==0 ? nc==4 ? s(x,1) : nc==8 ? s(x,4) : s(x,3) \
  4172. + : c==1 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  4173. + : c==2 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  4174. + : c==3 ? nc==8 ? s(x,7) : s(x,0) \
  4175. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  4176. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  4177. + : c==6 ? s(x,2) \
  4178. + : s(x,3)))
  4179. +
  4180. +#define si(y,x,k,c) s(y,c) = word_in(x + 4 * c) ^ k[c]
  4181. +#define so(y,x,c) word_out(y + 4 * c, s(x,c))
  4182. +
  4183. +#if defined(FOUR_TABLES)
  4184. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,ft_tab,fwd_var,rf1,c)
  4185. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,it_tab,inv_var,rf1,c)
  4186. +#elif defined(ONE_TABLE)
  4187. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,ft_tab,fwd_var,rf1,c)
  4188. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,it_tab,inv_var,rf1,c)
  4189. +#else
  4190. +#define fwd_rnd(y,x,k,c) s(y,c) = fwd_mcol(no_table(x,s_box,fwd_var,rf1,c)) ^ (k)[c]
  4191. +#define inv_rnd(y,x,k,c) s(y,c) = inv_mcol(no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c])
  4192. +#endif
  4193. +
  4194. +#if defined(FOUR_LR_TABLES)
  4195. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,fl_tab,fwd_var,rf1,c)
  4196. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,il_tab,inv_var,rf1,c)
  4197. +#elif defined(ONE_LR_TABLE)
  4198. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,fl_tab,fwd_var,rf1,c)
  4199. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,il_tab,inv_var,rf1,c)
  4200. +#else
  4201. +#define fwd_lrnd(y,x,k,c) s(y,c) = no_table(x,s_box,fwd_var,rf1,c) ^ (k)[c]
  4202. +#define inv_lrnd(y,x,k,c) s(y,c) = no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c]
  4203. +#endif
  4204. +
  4205. +#if AES_BLOCK_SIZE == 16
  4206. +
  4207. +#if defined(ARRAYS)
  4208. +#define locals(y,x) x[4],y[4]
  4209. +#else
  4210. +#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
  4211. +// the following defines prevent the compiler requiring the declaration
  4212. +// of generated but unused variables in the fwd_var and inv_var macros
  4213. +#define b04 unused
  4214. +#define b05 unused
  4215. +#define b06 unused
  4216. +#define b07 unused
  4217. +#define b14 unused
  4218. +#define b15 unused
  4219. +#define b16 unused
  4220. +#define b17 unused
  4221. +#endif
  4222. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  4223. + s(y,2) = s(x,2); s(y,3) = s(x,3);
  4224. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
  4225. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
  4226. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
  4227. +
  4228. +#elif AES_BLOCK_SIZE == 24
  4229. +
  4230. +#if defined(ARRAYS)
  4231. +#define locals(y,x) x[6],y[6]
  4232. +#else
  4233. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5, \
  4234. + y##0,y##1,y##2,y##3,y##4,y##5
  4235. +#define b06 unused
  4236. +#define b07 unused
  4237. +#define b16 unused
  4238. +#define b17 unused
  4239. +#endif
  4240. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  4241. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  4242. + s(y,4) = s(x,4); s(y,5) = s(x,5);
  4243. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); \
  4244. + si(y,x,k,3); si(y,x,k,4); si(y,x,k,5)
  4245. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); \
  4246. + so(y,x,3); so(y,x,4); so(y,x,5)
  4247. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); \
  4248. + rm(y,x,k,3); rm(y,x,k,4); rm(y,x,k,5)
  4249. +#else
  4250. +
  4251. +#if defined(ARRAYS)
  4252. +#define locals(y,x) x[8],y[8]
  4253. +#else
  4254. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5,x##6,x##7, \
  4255. + y##0,y##1,y##2,y##3,y##4,y##5,y##6,y##7
  4256. +#endif
  4257. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  4258. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  4259. + s(y,4) = s(x,4); s(y,5) = s(x,5); \
  4260. + s(y,6) = s(x,6); s(y,7) = s(x,7);
  4261. +
  4262. +#if AES_BLOCK_SIZE == 32
  4263. +
  4264. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3); \
  4265. + si(y,x,k,4); si(y,x,k,5); si(y,x,k,6); si(y,x,k,7)
  4266. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3); \
  4267. + so(y,x,4); so(y,x,5); so(y,x,6); so(y,x,7)
  4268. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3); \
  4269. + rm(y,x,k,4); rm(y,x,k,5); rm(y,x,k,6); rm(y,x,k,7)
  4270. +#else
  4271. +
  4272. +#define state_in(y,x,k) \
  4273. +switch(nc) \
  4274. +{ case 8: si(y,x,k,7); si(y,x,k,6); \
  4275. + case 6: si(y,x,k,5); si(y,x,k,4); \
  4276. + case 4: si(y,x,k,3); si(y,x,k,2); \
  4277. + si(y,x,k,1); si(y,x,k,0); \
  4278. +}
  4279. +
  4280. +#define state_out(y,x) \
  4281. +switch(nc) \
  4282. +{ case 8: so(y,x,7); so(y,x,6); \
  4283. + case 6: so(y,x,5); so(y,x,4); \
  4284. + case 4: so(y,x,3); so(y,x,2); \
  4285. + so(y,x,1); so(y,x,0); \
  4286. +}
  4287. +
  4288. +#if defined(FAST_VARIABLE)
  4289. +
  4290. +#define round(rm,y,x,k) \
  4291. +switch(nc) \
  4292. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  4293. + rm(y,x,k,5); rm(y,x,k,4); \
  4294. + rm(y,x,k,3); rm(y,x,k,2); \
  4295. + rm(y,x,k,1); rm(y,x,k,0); \
  4296. + break; \
  4297. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  4298. + rm(y,x,k,3); rm(y,x,k,2); \
  4299. + rm(y,x,k,1); rm(y,x,k,0); \
  4300. + break; \
  4301. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  4302. + rm(y,x,k,1); rm(y,x,k,0); \
  4303. + break; \
  4304. +}
  4305. +#else
  4306. +
  4307. +#define round(rm,y,x,k) \
  4308. +switch(nc) \
  4309. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  4310. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  4311. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  4312. + rm(y,x,k,1); rm(y,x,k,0); \
  4313. +}
  4314. +
  4315. +#endif
  4316. +
  4317. +#endif
  4318. +#endif
  4319. +
  4320. +void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  4321. +{ u_int32_t locals(b0, b1);
  4322. + const u_int32_t *kp = cx->aes_e_key;
  4323. +
  4324. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  4325. + u_int32_t f2;
  4326. +#endif
  4327. +
  4328. + state_in(b0, in_blk, kp); kp += nc;
  4329. +
  4330. +#if defined(UNROLL)
  4331. +
  4332. + switch(cx->aes_Nrnd)
  4333. + {
  4334. + case 14: round(fwd_rnd, b1, b0, kp );
  4335. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  4336. + case 12: round(fwd_rnd, b1, b0, kp );
  4337. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  4338. + case 10: round(fwd_rnd, b1, b0, kp );
  4339. + round(fwd_rnd, b0, b1, kp + nc);
  4340. + round(fwd_rnd, b1, b0, kp + 2 * nc);
  4341. + round(fwd_rnd, b0, b1, kp + 3 * nc);
  4342. + round(fwd_rnd, b1, b0, kp + 4 * nc);
  4343. + round(fwd_rnd, b0, b1, kp + 5 * nc);
  4344. + round(fwd_rnd, b1, b0, kp + 6 * nc);
  4345. + round(fwd_rnd, b0, b1, kp + 7 * nc);
  4346. + round(fwd_rnd, b1, b0, kp + 8 * nc);
  4347. + round(fwd_lrnd, b0, b1, kp + 9 * nc);
  4348. + }
  4349. +
  4350. +#elif defined(PARTIAL_UNROLL)
  4351. + { u_int32_t rnd;
  4352. +
  4353. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  4354. + {
  4355. + round(fwd_rnd, b1, b0, kp);
  4356. + round(fwd_rnd, b0, b1, kp + nc); kp += 2 * nc;
  4357. + }
  4358. +
  4359. + round(fwd_rnd, b1, b0, kp);
  4360. + round(fwd_lrnd, b0, b1, kp + nc);
  4361. + }
  4362. +#else
  4363. + { u_int32_t rnd;
  4364. +
  4365. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  4366. + {
  4367. + round(fwd_rnd, b1, b0, kp);
  4368. + l_copy(b0, b1); kp += nc;
  4369. + }
  4370. +
  4371. + round(fwd_lrnd, b0, b1, kp);
  4372. + }
  4373. +#endif
  4374. +
  4375. + state_out(out_blk, b0);
  4376. +}
  4377. +
  4378. +void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  4379. +{ u_int32_t locals(b0, b1);
  4380. + const u_int32_t *kp = cx->aes_d_key;
  4381. +
  4382. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  4383. + u_int32_t f2, f4, f8, f9;
  4384. +#endif
  4385. +
  4386. + state_in(b0, in_blk, kp); kp += nc;
  4387. +
  4388. +#if defined(UNROLL)
  4389. +
  4390. + switch(cx->aes_Nrnd)
  4391. + {
  4392. + case 14: round(inv_rnd, b1, b0, kp );
  4393. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  4394. + case 12: round(inv_rnd, b1, b0, kp );
  4395. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  4396. + case 10: round(inv_rnd, b1, b0, kp );
  4397. + round(inv_rnd, b0, b1, kp + nc);
  4398. + round(inv_rnd, b1, b0, kp + 2 * nc);
  4399. + round(inv_rnd, b0, b1, kp + 3 * nc);
  4400. + round(inv_rnd, b1, b0, kp + 4 * nc);
  4401. + round(inv_rnd, b0, b1, kp + 5 * nc);
  4402. + round(inv_rnd, b1, b0, kp + 6 * nc);
  4403. + round(inv_rnd, b0, b1, kp + 7 * nc);
  4404. + round(inv_rnd, b1, b0, kp + 8 * nc);
  4405. + round(inv_lrnd, b0, b1, kp + 9 * nc);
  4406. + }
  4407. +
  4408. +#elif defined(PARTIAL_UNROLL)
  4409. + { u_int32_t rnd;
  4410. +
  4411. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  4412. + {
  4413. + round(inv_rnd, b1, b0, kp);
  4414. + round(inv_rnd, b0, b1, kp + nc); kp += 2 * nc;
  4415. + }
  4416. +
  4417. + round(inv_rnd, b1, b0, kp);
  4418. + round(inv_lrnd, b0, b1, kp + nc);
  4419. + }
  4420. +#else
  4421. + { u_int32_t rnd;
  4422. +
  4423. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  4424. + {
  4425. + round(inv_rnd, b1, b0, kp);
  4426. + l_copy(b0, b1); kp += nc;
  4427. + }
  4428. +
  4429. + round(inv_lrnd, b0, b1, kp);
  4430. + }
  4431. +#endif
  4432. +
  4433. + state_out(out_blk, b0);
  4434. +}
  4435. diff -pruN linux-2.4.26/drivers/misc/aes.h linux-2.4.26_crypto/drivers/misc/aes.h
  4436. --- linux-2.4.26/drivers/misc/aes.h 1970-01-01 01:00:00.000000000 +0100
  4437. +++ linux-2.4.26_crypto/drivers/misc/aes.h 2004-05-16 14:57:30.000000000 +0200
  4438. @@ -0,0 +1,113 @@
  4439. +// I retain copyright in this code but I encourage its free use provided
  4440. +// that I don't carry any responsibility for the results. I am especially
  4441. +// happy to see it used in free and open source software. If you do use
  4442. +// it I would appreciate an acknowledgement of its origin in the code or
  4443. +// the product that results and I would also appreciate knowing a little
  4444. +// about the use to which it is being put. I am grateful to Frank Yellin
  4445. +// for some ideas that are used in this implementation.
  4446. +//
  4447. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  4448. +//
  4449. +// This is an implementation of the AES encryption algorithm (Rijndael)
  4450. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  4451. +// to provide both fixed and dynamic block and key lengths and can also
  4452. +// run with either big or little endian internal byte order (see aes.h).
  4453. +// It inputs block and key lengths in bytes with the legal values being
  4454. +// 16, 24 and 32.
  4455. +
  4456. +/*
  4457. + * Modified by Jari Ruusu, May 1 2001
  4458. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  4459. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  4460. + * - Major name space cleanup: Names visible to outside now begin
  4461. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  4462. + * - Removed C++ and DLL support as part of name space cleanup.
  4463. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  4464. + * - Merged precomputed constant tables to aes.c file.
  4465. + * - Removed data alignment restrictions for portability reasons.
  4466. + * - Made block and key lengths accept bit count (128/192/256)
  4467. + * as well byte count (16/24/32).
  4468. + * - Removed all error checks. This change also eliminated the need
  4469. + * to preinitialize the context struct to zero.
  4470. + * - Removed some totally unused constants.
  4471. + */
  4472. +
  4473. +#ifndef _AES_H
  4474. +#define _AES_H
  4475. +
  4476. +#include <linux/types.h>
  4477. +#include <linux/linkage.h>
  4478. +#include <linux/config.h>
  4479. +#include <linux/module.h>
  4480. +
  4481. +// CONFIGURATION OPTIONS (see also aes.c)
  4482. +//
  4483. +// Define AES_BLOCK_SIZE to set the cipher block size (16, 24 or 32) or
  4484. +// leave this undefined for dynamically variable block size (this will
  4485. +// result in much slower code).
  4486. +// IMPORTANT NOTE: AES_BLOCK_SIZE is in BYTES (16, 24, 32 or undefined). If
  4487. +// left undefined a slower version providing variable block length is compiled
  4488. +
  4489. +#define AES_BLOCK_SIZE 16
  4490. +
  4491. +// The number of key schedule words for different block and key lengths
  4492. +// allowing for method of computation which requires the length to be a
  4493. +// multiple of the key length
  4494. +//
  4495. +// Nk = 4 6 8
  4496. +// -------------
  4497. +// Nb = 4 | 60 60 64
  4498. +// 6 | 96 90 96
  4499. +// 8 | 120 120 120
  4500. +
  4501. +#if !defined(AES_BLOCK_SIZE) || (AES_BLOCK_SIZE == 32)
  4502. +#define AES_KS_LENGTH 120
  4503. +#define AES_RC_LENGTH 29
  4504. +#else
  4505. +#define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
  4506. +#define AES_RC_LENGTH (9 * AES_BLOCK_SIZE) / 8 - 8
  4507. +#endif
  4508. +
  4509. +typedef struct
  4510. +{
  4511. + u_int32_t aes_Nkey; // the number of words in the key input block
  4512. + u_int32_t aes_Nrnd; // the number of cipher rounds
  4513. + u_int32_t aes_e_key[AES_KS_LENGTH]; // the encryption key schedule
  4514. + u_int32_t aes_d_key[AES_KS_LENGTH]; // the decryption key schedule
  4515. +#if !defined(AES_BLOCK_SIZE)
  4516. + u_int32_t aes_Ncol; // the number of columns in the cipher state
  4517. +#endif
  4518. +} aes_context;
  4519. +
  4520. +// avoid global name conflict with mainline kernel
  4521. +#define aes_set_key _aes_set_key
  4522. +#define aes_encrypt _aes_encrypt
  4523. +#define aes_decrypt _aes_decrypt
  4524. +
  4525. +// THE CIPHER INTERFACE
  4526. +
  4527. +#if !defined(AES_BLOCK_SIZE)
  4528. +extern void aes_set_blk(aes_context *, const int);
  4529. +#endif
  4530. +
  4531. +#if defined(CONFIG_X86) && !defined(CONFIG_X86_64) && !defined(CONFIG_M386) && !defined(CONFIG_M486)
  4532. + asmlinkage
  4533. +#endif
  4534. +extern void aes_set_key(aes_context *, const unsigned char [], const int, const int);
  4535. +
  4536. +#if defined(CONFIG_X86) && !defined(CONFIG_X86_64) && !defined(CONFIG_M386) && !defined(CONFIG_M486)
  4537. + asmlinkage
  4538. +#endif
  4539. +extern void aes_encrypt(const aes_context *, const unsigned char [], unsigned char []);
  4540. +
  4541. +#if defined(CONFIG_X86) && !defined(CONFIG_X86_64) && !defined(CONFIG_M386) && !defined(CONFIG_M486)
  4542. + asmlinkage
  4543. +#endif
  4544. +extern void aes_decrypt(const aes_context *, const unsigned char [], unsigned char []);
  4545. +
  4546. +// The block length inputs to aes_set_block and aes_set_key are in numbers
  4547. +// of bytes or bits. The calls to subroutines must be made in the above
  4548. +// order but multiple calls can be made without repeating earlier calls
  4549. +// if their parameters have not changed.
  4550. +
  4551. +#endif // _AES_H
  4552. diff -pruN linux-2.4.26/drivers/misc/crypto-ksym.c linux-2.4.26_crypto/drivers/misc/crypto-ksym.c
  4553. --- linux-2.4.26/drivers/misc/crypto-ksym.c 1970-01-01 01:00:00.000000000 +0100
  4554. +++ linux-2.4.26_crypto/drivers/misc/crypto-ksym.c 2004-05-16 14:57:30.000000000 +0200
  4555. @@ -0,0 +1,7 @@
  4556. +#include <linux/module.h>
  4557. +#include "aes.h"
  4558. +#include "md5.h"
  4559. +EXPORT_SYMBOL_NOVERS(aes_set_key);
  4560. +EXPORT_SYMBOL_NOVERS(aes_encrypt);
  4561. +EXPORT_SYMBOL_NOVERS(aes_decrypt);
  4562. +EXPORT_SYMBOL_NOVERS(md5_transform_CPUbyteorder);
  4563. diff -pruN linux-2.4.26/drivers/misc/md5-i586.S linux-2.4.26_crypto/drivers/misc/md5-i586.S
  4564. --- linux-2.4.26/drivers/misc/md5-i586.S 1970-01-01 01:00:00.000000000 +0100
  4565. +++ linux-2.4.26_crypto/drivers/misc/md5-i586.S 2004-05-16 14:57:30.000000000 +0200
  4566. @@ -0,0 +1,201 @@
  4567. +//
  4568. +// md5-i586.S
  4569. +//
  4570. +// Written by Jari Ruusu, October 1 2003
  4571. +//
  4572. +// Copyright 2003 by Jari Ruusu.
  4573. +// Redistribution of this file is permitted under the GNU Public License.
  4574. +//
  4575. +
  4576. +// void md5_transform_CPUbyteorder(u_int32_t *hash, u_int32_t *in)
  4577. +
  4578. +#if defined(USE_UNDERLINE)
  4579. +# define md5_transform_CPUbyteorder _md5_transform_CPUbyteorder
  4580. +#endif
  4581. +#if !defined(ALIGN32BYTES)
  4582. +# define ALIGN32BYTES 32
  4583. +#endif
  4584. +
  4585. + .file "md5-i586.S"
  4586. + .globl md5_transform_CPUbyteorder
  4587. + .text
  4588. + .align ALIGN32BYTES
  4589. +
  4590. +md5_transform_CPUbyteorder:
  4591. + push %ebp
  4592. + mov 4+4(%esp),%eax // pointer to 'hash' input
  4593. + mov 8+4(%esp),%ebp // pointer to 'in' array
  4594. + push %ebx
  4595. + push %esi
  4596. + push %edi
  4597. +
  4598. + mov (%eax),%esi
  4599. + mov 4(%eax),%edi
  4600. + mov 8(%eax),%ecx
  4601. + mov 12(%eax),%eax
  4602. + mov (%ebp),%ebx
  4603. + mov %eax,%edx
  4604. + xor %ecx,%eax
  4605. +
  4606. +#define REPEAT1(p1w,p2x,p3z,p4c,p5s,p6Nin,p7Nz,p8Ny) \
  4607. + add $p4c,p1w ;\
  4608. + and p2x,%eax ;\
  4609. + add %ebx,p1w ;\
  4610. + xor p3z,%eax ;\
  4611. + mov p6Nin*4(%ebp),%ebx ;\
  4612. + add %eax,p1w ;\
  4613. + mov p7Nz,%eax ;\
  4614. + rol $p5s,p1w ;\
  4615. + xor p8Ny,%eax ;\
  4616. + add p2x,p1w
  4617. +
  4618. + REPEAT1(%esi,%edi,%edx,0xd76aa478, 7, 1,%ecx,%edi)
  4619. + REPEAT1(%edx,%esi,%ecx,0xe8c7b756,12, 2,%edi,%esi)
  4620. + REPEAT1(%ecx,%edx,%edi,0x242070db,17, 3,%esi,%edx)
  4621. + REPEAT1(%edi,%ecx,%esi,0xc1bdceee,22, 4,%edx,%ecx)
  4622. + REPEAT1(%esi,%edi,%edx,0xf57c0faf, 7, 5,%ecx,%edi)
  4623. + REPEAT1(%edx,%esi,%ecx,0x4787c62a,12, 6,%edi,%esi)
  4624. + REPEAT1(%ecx,%edx,%edi,0xa8304613,17, 7,%esi,%edx)
  4625. + REPEAT1(%edi,%ecx,%esi,0xfd469501,22, 8,%edx,%ecx)
  4626. + REPEAT1(%esi,%edi,%edx,0x698098d8, 7, 9,%ecx,%edi)
  4627. + REPEAT1(%edx,%esi,%ecx,0x8b44f7af,12,10,%edi,%esi)
  4628. + REPEAT1(%ecx,%edx,%edi,0xffff5bb1,17,11,%esi,%edx)
  4629. + REPEAT1(%edi,%ecx,%esi,0x895cd7be,22,12,%edx,%ecx)
  4630. + REPEAT1(%esi,%edi,%edx,0x6b901122, 7,13,%ecx,%edi)
  4631. + REPEAT1(%edx,%esi,%ecx,0xfd987193,12,14,%edi,%esi)
  4632. + REPEAT1(%ecx,%edx,%edi,0xa679438e,17,15,%esi,%edx)
  4633. +
  4634. + add $0x49b40821,%edi
  4635. + and %ecx,%eax
  4636. + add %ebx,%edi
  4637. + xor %esi,%eax
  4638. + mov 1*4(%ebp),%ebx
  4639. + add %eax,%edi
  4640. + mov %ecx,%eax
  4641. + rol $22,%edi
  4642. + add %ecx,%edi
  4643. +
  4644. +#define REPEAT2(p1w,p2x,p3y,p4z,p5c,p6s,p7Nin,p8Ny) \
  4645. + xor p2x,%eax ;\
  4646. + add $p5c,p1w ;\
  4647. + and p4z,%eax ;\
  4648. + add %ebx,p1w ;\
  4649. + xor p3y,%eax ;\
  4650. + mov p7Nin*4(%ebp),%ebx ;\
  4651. + add %eax,p1w ;\
  4652. + mov p8Ny,%eax ;\
  4653. + rol $p6s,p1w ;\
  4654. + add p2x,p1w
  4655. +
  4656. + REPEAT2(%esi,%edi,%ecx,%edx,0xf61e2562, 5, 6,%edi)
  4657. + REPEAT2(%edx,%esi,%edi,%ecx,0xc040b340, 9,11,%esi)
  4658. + REPEAT2(%ecx,%edx,%esi,%edi,0x265e5a51,14, 0,%edx)
  4659. + REPEAT2(%edi,%ecx,%edx,%esi,0xe9b6c7aa,20, 5,%ecx)
  4660. + REPEAT2(%esi,%edi,%ecx,%edx,0xd62f105d, 5,10,%edi)
  4661. + REPEAT2(%edx,%esi,%edi,%ecx,0x02441453, 9,15,%esi)
  4662. + REPEAT2(%ecx,%edx,%esi,%edi,0xd8a1e681,14, 4,%edx)
  4663. + REPEAT2(%edi,%ecx,%edx,%esi,0xe7d3fbc8,20, 9,%ecx)
  4664. + REPEAT2(%esi,%edi,%ecx,%edx,0x21e1cde6, 5,14,%edi)
  4665. + REPEAT2(%edx,%esi,%edi,%ecx,0xc33707d6, 9, 3,%esi)
  4666. + REPEAT2(%ecx,%edx,%esi,%edi,0xf4d50d87,14, 8,%edx)
  4667. + REPEAT2(%edi,%ecx,%edx,%esi,0x455a14ed,20,13,%ecx)
  4668. + REPEAT2(%esi,%edi,%ecx,%edx,0xa9e3e905, 5, 2,%edi)
  4669. + REPEAT2(%edx,%esi,%edi,%ecx,0xfcefa3f8, 9, 7,%esi)
  4670. + REPEAT2(%ecx,%edx,%esi,%edi,0x676f02d9,14,12,%edx)
  4671. +
  4672. + xor %ecx,%eax
  4673. + add $0x8d2a4c8a,%edi
  4674. + and %esi,%eax
  4675. + add %ebx,%edi
  4676. + xor %edx,%eax
  4677. + mov 5*4(%ebp),%ebx
  4678. + add %eax,%edi
  4679. + mov %ecx,%eax
  4680. + rol $20,%edi
  4681. + xor %edx,%eax
  4682. + add %ecx,%edi
  4683. +
  4684. +#define REPEAT3(p1w,p2x,p3c,p4s,p5Nin,p6Ny,p7Nz) \
  4685. + add $p3c,p1w ;\
  4686. + xor p2x,%eax ;\
  4687. + add %ebx,p1w ;\
  4688. + mov p5Nin*4(%ebp),%ebx ;\
  4689. + add %eax,p1w ;\
  4690. + mov p6Ny,%eax ;\
  4691. + rol $p4s,p1w ;\
  4692. + xor p7Nz,%eax ;\
  4693. + add p2x,p1w
  4694. +
  4695. + REPEAT3(%esi,%edi,0xfffa3942, 4, 8,%edi,%ecx)
  4696. + REPEAT3(%edx,%esi,0x8771f681,11,11,%esi,%edi)
  4697. + REPEAT3(%ecx,%edx,0x6d9d6122,16,14,%edx,%esi)
  4698. + REPEAT3(%edi,%ecx,0xfde5380c,23, 1,%ecx,%edx)
  4699. + REPEAT3(%esi,%edi,0xa4beea44, 4, 4,%edi,%ecx)
  4700. + REPEAT3(%edx,%esi,0x4bdecfa9,11, 7,%esi,%edi)
  4701. + REPEAT3(%ecx,%edx,0xf6bb4b60,16,10,%edx,%esi)
  4702. + REPEAT3(%edi,%ecx,0xbebfbc70,23,13,%ecx,%edx)
  4703. + REPEAT3(%esi,%edi,0x289b7ec6, 4, 0,%edi,%ecx)
  4704. + REPEAT3(%edx,%esi,0xeaa127fa,11, 3,%esi,%edi)
  4705. + REPEAT3(%ecx,%edx,0xd4ef3085,16, 6,%edx,%esi)
  4706. + REPEAT3(%edi,%ecx,0x04881d05,23, 9,%ecx,%edx)
  4707. + REPEAT3(%esi,%edi,0xd9d4d039, 4,12,%edi,%ecx)
  4708. + REPEAT3(%edx,%esi,0xe6db99e5,11,15,%esi,%edi)
  4709. + REPEAT3(%ecx,%edx,0x1fa27cf8,16, 2,%edx,%esi)
  4710. +
  4711. + add $0xc4ac5665,%edi
  4712. + xor %ecx,%eax
  4713. + add %ebx,%edi
  4714. + mov (%ebp),%ebx
  4715. + add %eax,%edi
  4716. + mov %edx,%eax
  4717. + rol $23,%edi
  4718. + not %eax
  4719. + add %ecx,%edi
  4720. +
  4721. +#define REPEAT4(p1w,p2x,p3y,p4c,p5s,p6Nin,p7Nz) \
  4722. + add $p4c,p1w ;\
  4723. + or p2x,%eax ;\
  4724. + add %ebx,p1w ;\
  4725. + xor p3y,%eax ;\
  4726. + mov p6Nin*4(%ebp),%ebx ;\
  4727. + add %eax,p1w ;\
  4728. + mov p7Nz,%eax ;\
  4729. + rol $p5s,p1w ;\
  4730. + not %eax ;\
  4731. + add p2x,p1w
  4732. +
  4733. + REPEAT4(%esi,%edi,%ecx,0xf4292244, 6, 7,%ecx)
  4734. + REPEAT4(%edx,%esi,%edi,0x432aff97,10,14,%edi)
  4735. + REPEAT4(%ecx,%edx,%esi,0xab9423a7,15, 5,%esi)
  4736. + REPEAT4(%edi,%ecx,%edx,0xfc93a039,21,12,%edx)
  4737. + REPEAT4(%esi,%edi,%ecx,0x655b59c3, 6, 3,%ecx)
  4738. + REPEAT4(%edx,%esi,%edi,0x8f0ccc92,10,10,%edi)
  4739. + REPEAT4(%ecx,%edx,%esi,0xffeff47d,15, 1,%esi)
  4740. + REPEAT4(%edi,%ecx,%edx,0x85845dd1,21, 8,%edx)
  4741. + REPEAT4(%esi,%edi,%ecx,0x6fa87e4f, 6,15,%ecx)
  4742. + REPEAT4(%edx,%esi,%edi,0xfe2ce6e0,10, 6,%edi)
  4743. + REPEAT4(%ecx,%edx,%esi,0xa3014314,15,13,%esi)
  4744. + REPEAT4(%edi,%ecx,%edx,0x4e0811a1,21, 4,%edx)
  4745. + REPEAT4(%esi,%edi,%ecx,0xf7537e82, 6,11,%ecx)
  4746. + REPEAT4(%edx,%esi,%edi,0xbd3af235,10, 2,%edi)
  4747. + REPEAT4(%ecx,%edx,%esi,0x2ad7d2bb,15, 9,%esi)
  4748. +
  4749. + add $0xeb86d391,%edi
  4750. + or %ecx,%eax
  4751. + add %ebx,%edi
  4752. + xor %edx,%eax
  4753. + mov 4+16(%esp),%ebp // pointer to 'hash' output
  4754. + add %eax,%edi
  4755. + rol $21,%edi
  4756. + add %ecx,%edi
  4757. +
  4758. + add %esi,(%ebp)
  4759. + add %edi,4(%ebp)
  4760. + add %ecx,8(%ebp)
  4761. + add %edx,12(%ebp)
  4762. +
  4763. + pop %edi
  4764. + pop %esi
  4765. + pop %ebx
  4766. + pop %ebp
  4767. + ret
  4768. diff -pruN linux-2.4.26/drivers/misc/md5.c linux-2.4.26_crypto/drivers/misc/md5.c
  4769. --- linux-2.4.26/drivers/misc/md5.c 1970-01-01 01:00:00.000000000 +0100
  4770. +++ linux-2.4.26_crypto/drivers/misc/md5.c 2004-05-16 14:57:30.000000000 +0200
  4771. @@ -0,0 +1,106 @@
  4772. +/*
  4773. + * MD5 Message Digest Algorithm (RFC1321).
  4774. + *
  4775. + * Derived from cryptoapi implementation, originally based on the
  4776. + * public domain implementation written by Colin Plumb in 1993.
  4777. + *
  4778. + * Copyright (c) Cryptoapi developers.
  4779. + * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  4780. + *
  4781. + * This program is free software; you can redistribute it and/or modify it
  4782. + * under the terms of the GNU General Public License as published by the Free
  4783. + * Software Foundation; either version 2 of the License, or (at your option)
  4784. + * any later version.
  4785. + */
  4786. +
  4787. +#include "md5.h"
  4788. +
  4789. +#define MD5_F1(x, y, z) (z ^ (x & (y ^ z)))
  4790. +#define MD5_F2(x, y, z) MD5_F1(z, x, y)
  4791. +#define MD5_F3(x, y, z) (x ^ y ^ z)
  4792. +#define MD5_F4(x, y, z) (y ^ (x | ~z))
  4793. +#define MD5_STEP(f, w, x, y, z, in, s) \
  4794. + (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
  4795. +
  4796. +void md5_transform_CPUbyteorder(u_int32_t *hash, u_int32_t const *in)
  4797. +{
  4798. + u_int32_t a, b, c, d;
  4799. +
  4800. + a = hash[0];
  4801. + b = hash[1];
  4802. + c = hash[2];
  4803. + d = hash[3];
  4804. +
  4805. + MD5_STEP(MD5_F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  4806. + MD5_STEP(MD5_F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  4807. + MD5_STEP(MD5_F1, c, d, a, b, in[2] + 0x242070db, 17);
  4808. + MD5_STEP(MD5_F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  4809. + MD5_STEP(MD5_F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  4810. + MD5_STEP(MD5_F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  4811. + MD5_STEP(MD5_F1, c, d, a, b, in[6] + 0xa8304613, 17);
  4812. + MD5_STEP(MD5_F1, b, c, d, a, in[7] + 0xfd469501, 22);
  4813. + MD5_STEP(MD5_F1, a, b, c, d, in[8] + 0x698098d8, 7);
  4814. + MD5_STEP(MD5_F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  4815. + MD5_STEP(MD5_F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  4816. + MD5_STEP(MD5_F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  4817. + MD5_STEP(MD5_F1, a, b, c, d, in[12] + 0x6b901122, 7);
  4818. + MD5_STEP(MD5_F1, d, a, b, c, in[13] + 0xfd987193, 12);
  4819. + MD5_STEP(MD5_F1, c, d, a, b, in[14] + 0xa679438e, 17);
  4820. + MD5_STEP(MD5_F1, b, c, d, a, in[15] + 0x49b40821, 22);
  4821. +
  4822. + MD5_STEP(MD5_F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  4823. + MD5_STEP(MD5_F2, d, a, b, c, in[6] + 0xc040b340, 9);
  4824. + MD5_STEP(MD5_F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  4825. + MD5_STEP(MD5_F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  4826. + MD5_STEP(MD5_F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  4827. + MD5_STEP(MD5_F2, d, a, b, c, in[10] + 0x02441453, 9);
  4828. + MD5_STEP(MD5_F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  4829. + MD5_STEP(MD5_F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  4830. + MD5_STEP(MD5_F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  4831. + MD5_STEP(MD5_F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  4832. + MD5_STEP(MD5_F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  4833. + MD5_STEP(MD5_F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  4834. + MD5_STEP(MD5_F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  4835. + MD5_STEP(MD5_F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  4836. + MD5_STEP(MD5_F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  4837. + MD5_STEP(MD5_F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  4838. +
  4839. + MD5_STEP(MD5_F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  4840. + MD5_STEP(MD5_F3, d, a, b, c, in[8] + 0x8771f681, 11);
  4841. + MD5_STEP(MD5_F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  4842. + MD5_STEP(MD5_F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  4843. + MD5_STEP(MD5_F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  4844. + MD5_STEP(MD5_F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  4845. + MD5_STEP(MD5_F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  4846. + MD5_STEP(MD5_F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  4847. + MD5_STEP(MD5_F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  4848. + MD5_STEP(MD5_F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  4849. + MD5_STEP(MD5_F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  4850. + MD5_STEP(MD5_F3, b, c, d, a, in[6] + 0x04881d05, 23);
  4851. + MD5_STEP(MD5_F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  4852. + MD5_STEP(MD5_F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  4853. + MD5_STEP(MD5_F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  4854. + MD5_STEP(MD5_F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  4855. +
  4856. + MD5_STEP(MD5_F4, a, b, c, d, in[0] + 0xf4292244, 6);
  4857. + MD5_STEP(MD5_F4, d, a, b, c, in[7] + 0x432aff97, 10);
  4858. + MD5_STEP(MD5_F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  4859. + MD5_STEP(MD5_F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  4860. + MD5_STEP(MD5_F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  4861. + MD5_STEP(MD5_F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  4862. + MD5_STEP(MD5_F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  4863. + MD5_STEP(MD5_F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  4864. + MD5_STEP(MD5_F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  4865. + MD5_STEP(MD5_F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  4866. + MD5_STEP(MD5_F4, c, d, a, b, in[6] + 0xa3014314, 15);
  4867. + MD5_STEP(MD5_F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  4868. + MD5_STEP(MD5_F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  4869. + MD5_STEP(MD5_F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  4870. + MD5_STEP(MD5_F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  4871. + MD5_STEP(MD5_F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  4872. +
  4873. + hash[0] += a;
  4874. + hash[1] += b;
  4875. + hash[2] += c;
  4876. + hash[3] += d;
  4877. +}
  4878. diff -pruN linux-2.4.26/drivers/misc/md5.h linux-2.4.26_crypto/drivers/misc/md5.h
  4879. --- linux-2.4.26/drivers/misc/md5.h 1970-01-01 01:00:00.000000000 +0100
  4880. +++ linux-2.4.26_crypto/drivers/misc/md5.h 2004-05-16 14:57:30.000000000 +0200
  4881. @@ -0,0 +1,11 @@
  4882. +/* md5.h */
  4883. +
  4884. +#include <linux/types.h>
  4885. +#include <linux/linkage.h>
  4886. +#include <linux/config.h>
  4887. +#include <linux/module.h>
  4888. +
  4889. +#if defined(CONFIG_X86) && !defined(CONFIG_X86_64) && !defined(CONFIG_M386) && !defined(CONFIG_M486)
  4890. + asmlinkage
  4891. +#endif
  4892. +extern void md5_transform_CPUbyteorder(u_int32_t *, u_int32_t const *);
  4893. diff -pruN linux-2.4.26/include/linux/loop.h linux-2.4.26_crypto/include/linux/loop.h
  4894. --- linux-2.4.26/include/linux/loop.h 2001-09-17 22:16:30.000000000 +0200
  4895. +++ linux-2.4.26_crypto/include/linux/loop.h 2004-05-16 14:57:30.000000000 +0200
  4896. @@ -17,6 +17,11 @@
  4897. #ifdef __KERNEL__
  4898. +/* definitions for IV metric -- cryptoapi specific */
  4899. +#define LOOP_IV_SECTOR_BITS 9
  4900. +#define LOOP_IV_SECTOR_SIZE (1 << LOOP_IV_SECTOR_BITS)
  4901. +typedef int loop_iv_t;
  4902. +
  4903. /* Possible states of device */
  4904. enum {
  4905. Lo_unbound,
  4906. @@ -27,35 +32,43 @@ enum {
  4907. struct loop_device {
  4908. int lo_number;
  4909. int lo_refcnt;
  4910. - kdev_t lo_device;
  4911. - int lo_offset;
  4912. + loff_t lo_offset;
  4913. + loff_t lo_sizelimit;
  4914. int lo_encrypt_type;
  4915. int lo_encrypt_key_size;
  4916. - int lo_flags;
  4917. int (*transfer)(struct loop_device *, int cmd,
  4918. char *raw_buf, char *loop_buf, int size,
  4919. int real_block);
  4920. + int (*ioctl)(struct loop_device *, int cmd,
  4921. + unsigned long arg);
  4922. char lo_name[LO_NAME_SIZE];
  4923. char lo_encrypt_key[LO_KEY_SIZE];
  4924. __u32 lo_init[2];
  4925. uid_t lo_key_owner; /* Who set the key */
  4926. - int (*ioctl)(struct loop_device *, int cmd,
  4927. - unsigned long arg);
  4928. + kdev_t lo_device;
  4929. + int lo_flags;
  4930. struct file * lo_backing_file;
  4931. - void *key_data;
  4932. + void *key_data;
  4933. char key_reserved[48]; /* for use by the filter modules */
  4934. int old_gfp_mask;
  4935. + int lo_state;
  4936. + struct buffer_head *lo_bh_que0;
  4937. + struct buffer_head *lo_bh_que1;
  4938. + struct buffer_head *lo_bh_que2;
  4939. + struct buffer_head *lo_bh_free;
  4940. spinlock_t lo_lock;
  4941. - struct buffer_head *lo_bh;
  4942. - struct buffer_head *lo_bhtail;
  4943. - int lo_state;
  4944. struct semaphore lo_sem;
  4945. struct semaphore lo_ctl_mutex;
  4946. - struct semaphore lo_bh_mutex;
  4947. atomic_t lo_pending;
  4948. + int lo_bh_flsh;
  4949. + int lo_bh_need;
  4950. + wait_queue_head_t lo_bh_wait;
  4951. + unsigned long lo_offs_sec;
  4952. + unsigned long lo_iv_remove;
  4953. + unsigned char lo_crypt_name[LO_NAME_SIZE];
  4954. };
  4955. typedef int (* transfer_proc_t)(struct loop_device *, int cmd,
  4956. @@ -77,20 +90,19 @@ static inline int lo_do_transfer(struct
  4957. */
  4958. #define LO_FLAGS_DO_BMAP 1
  4959. #define LO_FLAGS_READ_ONLY 2
  4960. -#define LO_FLAGS_BH_REMAP 4
  4961. -/*
  4962. +/*
  4963. * Note that this structure gets the wrong offsets when directly used
  4964. * from a glibc program, because glibc has a 32bit dev_t.
  4965. - * Prevent people from shooting in their own foot.
  4966. + * Prevent people from shooting in their own foot.
  4967. */
  4968. #if __GLIBC__ >= 2 && !defined(dev_t)
  4969. #error "Wrong dev_t in loop.h"
  4970. -#endif
  4971. +#endif
  4972. /*
  4973. * This uses kdev_t because glibc currently has no appropiate
  4974. - * conversion version for the loop ioctls.
  4975. + * conversion version for the loop ioctls.
  4976. * The situation is very unpleasant
  4977. */
  4978. @@ -109,6 +121,22 @@ struct loop_info {
  4979. char reserved[4];
  4980. };
  4981. +struct loop_info64 {
  4982. + __u64 lo_device; /* ioctl r/o */
  4983. + __u64 lo_inode; /* ioctl r/o */
  4984. + __u64 lo_rdevice; /* ioctl r/o */
  4985. + __u64 lo_offset;
  4986. + __u64 lo_sizelimit;/* bytes, 0 == max available */
  4987. + __u32 lo_number; /* ioctl r/o */
  4988. + __u32 lo_encrypt_type;
  4989. + __u32 lo_encrypt_key_size; /* ioctl w/o */
  4990. + __u32 lo_flags; /* ioctl r/o */
  4991. + __u8 lo_file_name[LO_NAME_SIZE];
  4992. + __u8 lo_crypt_name[LO_NAME_SIZE];
  4993. + __u8 lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
  4994. + __u64 lo_init[2];
  4995. +};
  4996. +
  4997. /*
  4998. * Loop filter types
  4999. */
  5000. @@ -122,25 +150,27 @@ struct loop_info {
  5001. #define LO_CRYPT_IDEA 6
  5002. #define LO_CRYPT_DUMMY 9
  5003. #define LO_CRYPT_SKIPJACK 10
  5004. +#define LO_CRYPT_AES 16
  5005. +#define LO_CRYPT_CRYPTOAPI 18
  5006. #define MAX_LO_CRYPT 20
  5007. #ifdef __KERNEL__
  5008. /* Support for loadable transfer modules */
  5009. struct loop_func_table {
  5010. - int number; /* filter type */
  5011. + int number; /* filter type */
  5012. int (*transfer)(struct loop_device *lo, int cmd, char *raw_buf,
  5013. char *loop_buf, int size, int real_block);
  5014. - int (*init)(struct loop_device *, struct loop_info *);
  5015. + int (*init)(struct loop_device *, struct loop_info *);
  5016. /* release is called from loop_unregister_transfer or clr_fd */
  5017. - int (*release)(struct loop_device *);
  5018. + int (*release)(struct loop_device *);
  5019. int (*ioctl)(struct loop_device *, int cmd, unsigned long arg);
  5020. - /* lock and unlock manage the module use counts */
  5021. + /* lock and unlock manage the module use counts */
  5022. void (*lock)(struct loop_device *);
  5023. void (*unlock)(struct loop_device *);
  5024. -};
  5025. +};
  5026. -int loop_register_transfer(struct loop_func_table *funcs);
  5027. -int loop_unregister_transfer(int number);
  5028. +int loop_register_transfer(struct loop_func_table *funcs);
  5029. +int loop_unregister_transfer(int number);
  5030. #endif
  5031. /*
  5032. @@ -151,5 +181,9 @@ int loop_unregister_transfer(int number)
  5033. #define LOOP_CLR_FD 0x4C01
  5034. #define LOOP_SET_STATUS 0x4C02
  5035. #define LOOP_GET_STATUS 0x4C03
  5036. +#define LOOP_SET_STATUS64 0x4C04
  5037. +#define LOOP_GET_STATUS64 0x4C05
  5038. +
  5039. +#define LOOP_MULTI_KEY_SETUP 0x4C4D
  5040. #endif