Guest User

Untitled

a guest
Oct 5th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 66.58 KB | None | 0 0
  1.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  2.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  3.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  4.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  5.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  6.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  7.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  8.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  9.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  10.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  11.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  12.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  13.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  14.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  15.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  16.   {vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv}
  17.   (* crypto/aes/aes_core.c -*- mode:C; c-file-style: "eay" -*- *)
  18.   (**
  19.    * rijndael-alg-fst.c
  20.    *
  21.    * @version 3.0 (December 2000)
  22.    *
  23.    * Optimised ANSI C code for the Rijndael cipher (now AES)
  24.    *
  25.    * @author Vincent Rijmen <[email protected]>
  26.    * @author Antoon Bosselaers <[email protected]>
  27.    * @author Paulo Barreto <[email protected]>
  28.    *
  29.    * This code is hereby placed in the public domain.
  30.    *
  31.    * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
  32.    * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  33.    * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34.    * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
  35.    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36.    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37.    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  38.    * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  39.    * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  40.    * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  41.    * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.    *)
  43.  
  44.   (*
  45.    * This is experimental x86[_64] derivative. It assumes little-endian
  46.    * byte order and expects CPU to sustain unaligned memory references.
  47.    * It is used as playground for cache-time attack mitigations and
  48.    * serves as reference C implementation for x86[_64] assembler.
  49.    *
  50.    *          <[email protected]>
  51.    *)
  52.  
  53.  
  54.   {$ifndef AES_DEBUG}
  55.   {$ ifndef NDEBUG}
  56.   {$  define NDEBUG}
  57.   {$ endif}
  58.   {$endif}
  59.   //{$include <assert.h>}
  60.  
  61.   //{$include <stdlib.h>}
  62.   //{$include <openssl/aes.h>}
  63.   //{$include "aes_locl.h"}
  64.  
  65.   (*
  66.    * These two parameters control which table, 256-byte or 2KB, is
  67.    * referenced in outer and respectively inner rounds.
  68.    *)
  69.   {$define AES_COMPACT_IN_OUTER_ROUNDS}
  70.   {$ifdef  AES_COMPACT_IN_OUTER_ROUNDS}
  71.   (* AES_COMPACT_IN_OUTER_ROUNDS costs ~30% in performance, while
  72.    * adding AES_COMPACT_IN_INNER_ROUNDS reduces benchmark *further*
  73.    * by factor of ~2. *)
  74.   {$ undef  AES_COMPACT_IN_INNER_ROUNDS}
  75.   {$endif}
  76.  
  77.   {$if 1} {
  78.   static void prefetch256(const void *table)
  79.   begin
  80.     volatile unsigned long *t:=(void *)table,ret;
  81.     unsigned long sum;
  82.     int i;
  83.  
  84.     (* 32 is common least cache-line size *)
  85.     for (sum:=0,i:=0;i<256/sizeof(t[0]);i+:=32/sizeof(t[0]))  sum xor:= t[i];
  86.  
  87.     ret := sum;
  88.   end;       }
  89.   {$else}
  90.   {$ define prefetch256(t)}
  91.   {$endif}
  92.  
  93.   {$undef GETU32}
  94.   //{$define GETU32(p) (*((u32*)(p)))}
  95.   function GETU32(const p:pchar):dword;
  96.   begin
  97.     result:=(pdword(p))^;
  98.   end;
  99.   {$if (defined(_WIN32) or defined(_WIN64)) and not defined(__MINGW32__)}
  100.   //typedef unsigned __int64 u64;
  101.   {$define U64(C)  C##UI64}
  102.   {$elif defined(__arch64__)}
  103.   //typedef unsigned long u64;
  104.   {$define U64(C)  C##UL}
  105.   {$else}
  106.   //typedef unsigned long long u64;
  107.   {$define U64(C)  C##ULL}
  108.   {$endif}
  109.  
  110.   {$undef ROTATE}
  111.   {$if defined(_MSC_VER) or defined(__ICC)}
  112.   {$ define ROTATE(a,n)  _lrotl(a,n)}
  113.   {$elif defined(__GNUC__) and __GNUC__>:=2}
  114.   {$ if defined(__i386) or defined(__i386__) or defined(__x86_64) or defined(__x86_64__)}
  115.   {$   define ROTATE(a,n)  (begin register unsigned int ret;  \}
  116.           asm (      \
  117.           "roll %1,%0"    \
  118.           : ":=r"(ret)    \
  119.           : "I"(n), "0"(a)  \
  120.           : "cc");    \
  121.            ret;        \
  122.         end;)
  123.   {$ endif}
  124.   {$endif}
  125.   (*
  126.   Te [x] := S [x].[02, 01, 01, 03, 02, 01, 01, 03];
  127.   Te0[x] := S [x].[02, 01, 01, 03];
  128.   Te1[x] := S [x].[03, 02, 01, 01];
  129.   Te2[x] := S [x].[01, 03, 02, 01];
  130.   Te3[x] := S [x].[01, 01, 03, 02];
  131.   *)
  132.   {$define Te0 (u32)((u64*)((u8*)Te+0))}
  133.   {$define Te1 (u32)((u64*)((u8*)Te+3))}
  134.   {$define Te2 (u32)((u64*)((u8*)Te+2))}
  135.   {$define Te3 (u32)((u64*)((u8*)Te+1))}
  136.   (*
  137.   Td [x] := Si[x].[0e, 09, 0d, 0b, 0e, 09, 0d, 0b];
  138.   Td0[x] := Si[x].[0e, 09, 0d, 0b];
  139.   Td1[x] := Si[x].[0b, 0e, 09, 0d];
  140.   Td2[x] := Si[x].[0d, 0b, 0e, 09];
  141.   Td3[x] := Si[x].[09, 0d, 0b, 0e];
  142.   Td4[x] := Si[x].[01];
  143.   *)
  144.   {$define Td0 (u32)((u64*)((u8*)Td+0))}
  145.   {$define Td1 (u32)((u64*)((u8*)Td+3))}
  146.   {$define Td2 (u32)((u64*)((u8*)Td+2))}
  147.   {$define Td3 (u32)((u64*)((u8*)Td+1))}
  148.  
  149.   const Te:array[0..255]of Int64 = (
  150.       $a56363c6a56363c6, $847c7cf8847c7cf8,
  151.       $997777ee997777ee, $8d7b7bf68d7b7bf6,
  152.       $0df2f2ff0df2f2ff, $bd6b6bd6bd6b6bd6,
  153.       $b16f6fdeb16f6fde, $54c5c59154c5c591,
  154.       $5030306050303060, $0301010203010102,
  155.       $a96767cea96767ce, $7d2b2b567d2b2b56,
  156.       $19fefee719fefee7, $62d7d7b562d7d7b5,
  157.       $e6abab4de6abab4d, $9a7676ec9a7676ec,
  158.       $45caca8f45caca8f, $9d82821f9d82821f,
  159.       $40c9c98940c9c989, $877d7dfa877d7dfa,
  160.       $15fafaef15fafaef, $eb5959b2eb5959b2,
  161.       $c947478ec947478e, $0bf0f0fb0bf0f0fb,
  162.       $ecadad41ecadad41, $67d4d4b367d4d4b3,
  163.       $fda2a25ffda2a25f, $eaafaf45eaafaf45,
  164.       $bf9c9c23bf9c9c23, $f7a4a453f7a4a453,
  165.       $967272e4967272e4, $5bc0c09b5bc0c09b,
  166.       $c2b7b775c2b7b775, $1cfdfde11cfdfde1,
  167.       $ae93933dae93933d, $6a26264c6a26264c,
  168.       $5a36366c5a36366c, $413f3f7e413f3f7e,
  169.       $02f7f7f502f7f7f5, $4fcccc834fcccc83,
  170.       $5c3434685c343468, $f4a5a551f4a5a551,
  171.       $34e5e5d134e5e5d1, $08f1f1f908f1f1f9,
  172.       $937171e2937171e2, $73d8d8ab73d8d8ab,
  173.       $5331316253313162, $3f15152a3f15152a,
  174.       $0c0404080c040408, $52c7c79552c7c795,
  175.       $6523234665232346, $5ec3c39d5ec3c39d,
  176.       $2818183028181830, $a1969637a1969637,
  177.       $0f05050a0f05050a, $b59a9a2fb59a9a2f,
  178.       $0907070e0907070e, $3612122436121224,
  179.       $9b80801b9b80801b, $3de2e2df3de2e2df,
  180.       $26ebebcd26ebebcd, $6927274e6927274e,
  181.       $cdb2b27fcdb2b27f, $9f7575ea9f7575ea,
  182.       $1b0909121b090912, $9e83831d9e83831d,
  183.       $742c2c58742c2c58, $2e1a1a342e1a1a34,
  184.       $2d1b1b362d1b1b36, $b26e6edcb26e6edc,
  185.       $ee5a5ab4ee5a5ab4, $fba0a05bfba0a05b,
  186.       $f65252a4f65252a4, $4d3b3b764d3b3b76,
  187.       $61d6d6b761d6d6b7, $ceb3b37dceb3b37d,
  188.       $7b2929527b292952, $3ee3e3dd3ee3e3dd,
  189.       $712f2f5e712f2f5e, $9784841397848413,
  190.       $f55353a6f55353a6, $68d1d1b968d1d1b9,
  191.       $0000000000000000, $2cededc12cededc1,
  192.       $6020204060202040, $1ffcfce31ffcfce3,
  193.       $c8b1b179c8b1b179, $ed5b5bb6ed5b5bb6,
  194.       $be6a6ad4be6a6ad4, $46cbcb8d46cbcb8d,
  195.       $d9bebe67d9bebe67, $4b3939724b393972,
  196.       $de4a4a94de4a4a94, $d44c4c98d44c4c98,
  197.       $e85858b0e85858b0, $4acfcf854acfcf85,
  198.       $6bd0d0bb6bd0d0bb, $2aefefc52aefefc5,
  199.       $e5aaaa4fe5aaaa4f, $16fbfbed16fbfbed,
  200.       $c5434386c5434386, $d74d4d9ad74d4d9a,
  201.       $5533336655333366, $9485851194858511,
  202.       $cf45458acf45458a, $10f9f9e910f9f9e9,
  203.       $0602020406020204, $817f7ffe817f7ffe,
  204.       $f05050a0f05050a0, $443c3c78443c3c78,
  205.       $ba9f9f25ba9f9f25, $e3a8a84be3a8a84b,
  206.       $f35151a2f35151a2, $fea3a35dfea3a35d,
  207.       $c0404080c0404080, $8a8f8f058a8f8f05,
  208.       $ad92923fad92923f, $bc9d9d21bc9d9d21,
  209.       $4838387048383870, $04f5f5f104f5f5f1,
  210.       $dfbcbc63dfbcbc63, $c1b6b677c1b6b677,
  211.       $75dadaaf75dadaaf, $6321214263212142,
  212.       $3010102030101020, $1affffe51affffe5,
  213.       $0ef3f3fd0ef3f3fd, $6dd2d2bf6dd2d2bf,
  214.       $4ccdcd814ccdcd81, $140c0c18140c0c18,
  215.       $3513132635131326, $2fececc32fececc3,
  216.       $e15f5fbee15f5fbe, $a2979735a2979735,
  217.       $cc444488cc444488, $3917172e3917172e,
  218.       $57c4c49357c4c493, $f2a7a755f2a7a755,
  219.       $827e7efc827e7efc, $473d3d7a473d3d7a,
  220.       $ac6464c8ac6464c8, $e75d5dbae75d5dba,
  221.       $2b1919322b191932, $957373e6957373e6,
  222.       $a06060c0a06060c0, $9881811998818119,
  223.       $d14f4f9ed14f4f9e, $7fdcdca37fdcdca3,
  224.       $6622224466222244, $7e2a2a547e2a2a54,
  225.       $ab90903bab90903b, $8388880b8388880b,
  226.       $ca46468cca46468c, $29eeeec729eeeec7,
  227.       $d3b8b86bd3b8b86b, $3c1414283c141428,
  228.       $79dedea779dedea7, $e25e5ebce25e5ebc,
  229.       $1d0b0b161d0b0b16, $76dbdbad76dbdbad,
  230.       $3be0e0db3be0e0db, $5632326456323264,
  231.       $4e3a3a744e3a3a74, $1e0a0a141e0a0a14,
  232.       $db494992db494992, $0a06060c0a06060c,
  233.       $6c2424486c242448, $e45c5cb8e45c5cb8,
  234.       $5dc2c29f5dc2c29f, $6ed3d3bd6ed3d3bd,
  235.       $efacac43efacac43, $a66262c4a66262c4,
  236.       $a8919139a8919139, $a4959531a4959531,
  237.       $37e4e4d337e4e4d3, $8b7979f28b7979f2,
  238.       $32e7e7d532e7e7d5, $43c8c88b43c8c88b,
  239.       $5937376e5937376e, $b76d6ddab76d6dda,
  240.       $8c8d8d018c8d8d01, $64d5d5b164d5d5b1,
  241.       $d24e4e9cd24e4e9c, $e0a9a949e0a9a949,
  242.       $b46c6cd8b46c6cd8, $fa5656acfa5656ac,
  243.       $07f4f4f307f4f4f3, $25eaeacf25eaeacf,
  244.       $af6565caaf6565ca, $8e7a7af48e7a7af4,
  245.       $e9aeae47e9aeae47, $1808081018080810,
  246.       $d5baba6fd5baba6f, $887878f0887878f0,
  247.       $6f25254a6f25254a, $722e2e5c722e2e5c,
  248.       $241c1c38241c1c38, $f1a6a657f1a6a657,
  249.       $c7b4b473c7b4b473, $51c6c69751c6c697,
  250.       $23e8e8cb23e8e8cb, $7cdddda17cdddda1,
  251.       $9c7474e89c7474e8, $211f1f3e211f1f3e,
  252.       $dd4b4b96dd4b4b96, $dcbdbd61dcbdbd61,
  253.       $868b8b0d868b8b0d, $858a8a0f858a8a0f,
  254.       $907070e0907070e0, $423e3e7c423e3e7c,
  255.       $c4b5b571c4b5b571, $aa6666ccaa6666cc,
  256.       $d8484890d8484890, $0503030605030306,
  257.       $01f6f6f701f6f6f7, $120e0e1c120e0e1c,
  258.       $a36161c2a36161c2, $5f35356a5f35356a,
  259.       $f95757aef95757ae, $d0b9b969d0b9b969,
  260.       $9186861791868617, $58c1c19958c1c199,
  261.       $271d1d3a271d1d3a, $b99e9e27b99e9e27,
  262.       $38e1e1d938e1e1d9, $13f8f8eb13f8f8eb,
  263.       $b398982bb398982b, $3311112233111122,
  264.       $bb6969d2bb6969d2, $70d9d9a970d9d9a9,
  265.       $898e8e07898e8e07, $a7949433a7949433,
  266.       $b69b9b2db69b9b2d, $221e1e3c221e1e3c,
  267.       $9287871592878715, $20e9e9c920e9e9c9,
  268.       $49cece8749cece87, $ff5555aaff5555aa,
  269.       $7828285078282850, $7adfdfa57adfdfa5,
  270.       $8f8c8c038f8c8c03, $f8a1a159f8a1a159,
  271.       $8089890980898909, $170d0d1a170d0d1a,
  272.       $dabfbf65dabfbf65, $31e6e6d731e6e6d7,
  273.       $c6424284c6424284, $b86868d0b86868d0,
  274.       $c3414182c3414182, $b0999929b0999929,
  275.       $772d2d5a772d2d5a, $110f0f1e110f0f1e,
  276.       $cbb0b07bcbb0b07b, $fc5454a8fc5454a8,
  277.       $d6bbbb6dd6bbbb6d, $3a16162c3a16162c
  278.   );
  279.  
  280.   const Te4:array[0..255]of byte = (
  281.       $63, $7c, $77, $7b, $f2, $6b, $6f, $c5,
  282.       $30, $01, $67, $2b, $fe, $d7, $ab, $76,
  283.       $ca, $82, $c9, $7d, $fa, $59, $47, $f0,
  284.       $ad, $d4, $a2, $af, $9c, $a4, $72, $c0,
  285.       $b7, $fd, $93, $26, $36, $3f, $f7, $cc,
  286.       $34, $a5, $e5, $f1, $71, $d8, $31, $15,
  287.       $04, $c7, $23, $c3, $18, $96, $05, $9a,
  288.       $07, $12, $80, $e2, $eb, $27, $b2, $75,
  289.       $09, $83, $2c, $1a, $1b, $6e, $5a, $a0,
  290.       $52, $3b, $d6, $b3, $29, $e3, $2f, $84,
  291.       $53, $d1, $00, $ed, $20, $fc, $b1, $5b,
  292.       $6a, $cb, $be, $39, $4a, $4c, $58, $cf,
  293.       $d0, $ef, $aa, $fb, $43, $4d, $33, $85,
  294.       $45, $f9, $02, $7f, $50, $3c, $9f, $a8,
  295.       $51, $a3, $40, $8f, $92, $9d, $38, $f5,
  296.       $bc, $b6, $da, $21, $10, $ff, $f3, $d2,
  297.       $cd, $0c, $13, $ec, $5f, $97, $44, $17,
  298.       $c4, $a7, $7e, $3d, $64, $5d, $19, $73,
  299.       $60, $81, $4f, $dc, $22, $2a, $90, $88,
  300.       $46, $ee, $b8, $14, $de, $5e, $0b, $db,
  301.       $e0, $32, $3a, $0a, $49, $06, $24, $5c,
  302.       $c2, $d3, $ac, $62, $91, $95, $e4, $79,
  303.       $e7, $c8, $37, $6d, $8d, $d5, $4e, $a9,
  304.       $6c, $56, $f4, $ea, $65, $7a, $ae, $08,
  305.       $ba, $78, $25, $2e, $1c, $a6, $b4, $c6,
  306.       $e8, $dd, $74, $1f, $4b, $bd, $8b, $8a,
  307.       $70, $3e, $b5, $66, $48, $03, $f6, $0e,
  308.       $61, $35, $57, $b9, $86, $c1, $1d, $9e,
  309.       $e1, $f8, $98, $11, $69, $d9, $8e, $94,
  310.       $9b, $1e, $87, $e9, $ce, $55, $28, $df,
  311.       $8c, $a1, $89, $0d, $bf, $e6, $42, $68,
  312.       $41, $99, $2d, $0f, $b0, $54, $bb, $16
  313.   );
  314.  
  315.   const Td:array[0..255]of Int64 = (
  316.       $50a7f45150a7f451, $5365417e5365417e,
  317.       $c3a4171ac3a4171a, $965e273a965e273a,
  318.       $cb6bab3bcb6bab3b, $f1459d1ff1459d1f,
  319.       $ab58faacab58faac, $9303e34b9303e34b,
  320.       $55fa302055fa3020, $f66d76adf66d76ad,
  321.       $9176cc889176cc88, $254c02f5254c02f5,
  322.       $fcd7e54ffcd7e54f, $d7cb2ac5d7cb2ac5,
  323.       $8044352680443526, $8fa362b58fa362b5,
  324.       $495ab1de495ab1de, $671bba25671bba25,
  325.       $980eea45980eea45, $e1c0fe5de1c0fe5d,
  326.       $02752fc302752fc3, $12f04c8112f04c81,
  327.       $a397468da397468d, $c6f9d36bc6f9d36b,
  328.       $e75f8f03e75f8f03, $959c9215959c9215,
  329.       $eb7a6dbfeb7a6dbf, $da595295da595295,
  330.       $2d83bed42d83bed4, $d3217458d3217458,
  331.       $2969e0492969e049, $44c8c98e44c8c98e,
  332.       $6a89c2756a89c275, $78798ef478798ef4,
  333.       $6b3e58996b3e5899, $dd71b927dd71b927,
  334.       $b64fe1beb64fe1be, $17ad88f017ad88f0,
  335.       $66ac20c966ac20c9, $b43ace7db43ace7d,
  336.       $184adf63184adf63, $82311ae582311ae5,
  337.       $6033519760335197, $457f5362457f5362,
  338.       $e07764b1e07764b1, $84ae6bbb84ae6bbb,
  339.       $1ca081fe1ca081fe, $942b08f9942b08f9,
  340.       $5868487058684870, $19fd458f19fd458f,
  341.       $876cde94876cde94, $b7f87b52b7f87b52,
  342.       $23d373ab23d373ab, $e2024b72e2024b72,
  343.       $578f1fe3578f1fe3, $2aab55662aab5566,
  344.       $0728ebb20728ebb2, $03c2b52f03c2b52f,
  345.       $9a7bc5869a7bc586, $a50837d3a50837d3,
  346.       $f2872830f2872830, $b2a5bf23b2a5bf23,
  347.       $ba6a0302ba6a0302, $5c8216ed5c8216ed,
  348.       $2b1ccf8a2b1ccf8a, $92b479a792b479a7,
  349.       $f0f207f3f0f207f3, $a1e2694ea1e2694e,
  350.       $cdf4da65cdf4da65, $d5be0506d5be0506,
  351.       $1f6234d11f6234d1, $8afea6c48afea6c4,
  352.       $9d532e349d532e34, $a055f3a2a055f3a2,
  353.       $32e18a0532e18a05, $75ebf6a475ebf6a4,
  354.       $39ec830b39ec830b, $aaef6040aaef6040,
  355.       $069f715e069f715e, $51106ebd51106ebd,
  356.       $f98a213ef98a213e, $3d06dd963d06dd96,
  357.       $ae053eddae053edd, $46bde64d46bde64d,
  358.       $b58d5491b58d5491, $055dc471055dc471,
  359.       $6fd406046fd40604, $ff155060ff155060,
  360.       $24fb981924fb9819, $97e9bdd697e9bdd6,
  361.       $cc434089cc434089, $779ed967779ed967,
  362.       $bd42e8b0bd42e8b0, $888b8907888b8907,
  363.       $385b19e7385b19e7, $dbeec879dbeec879,
  364.       $470a7ca1470a7ca1, $e90f427ce90f427c,
  365.       $c91e84f8c91e84f8, $0000000000000000,
  366.       $8386800983868009, $48ed2b3248ed2b32,
  367.       $ac70111eac70111e, $4e725a6c4e725a6c,
  368.       $fbff0efdfbff0efd, $5638850f5638850f,
  369.       $1ed5ae3d1ed5ae3d, $27392d3627392d36,
  370.       $64d90f0a64d90f0a, $21a65c6821a65c68,
  371.       $d1545b9bd1545b9b, $3a2e36243a2e3624,
  372.       $b1670a0cb1670a0c, $0fe757930fe75793,
  373.       $d296eeb4d296eeb4, $9e919b1b9e919b1b,
  374.       $4fc5c0804fc5c080, $a220dc61a220dc61,
  375.       $694b775a694b775a, $161a121c161a121c,
  376.       $0aba93e20aba93e2, $e52aa0c0e52aa0c0,
  377.       $43e0223c43e0223c, $1d171b121d171b12,
  378.       $0b0d090e0b0d090e, $adc78bf2adc78bf2,
  379.       $b9a8b62db9a8b62d, $c8a91e14c8a91e14,
  380.       $8519f1578519f157, $4c0775af4c0775af,
  381.       $bbdd99eebbdd99ee, $fd607fa3fd607fa3,
  382.       $9f2601f79f2601f7, $bcf5725cbcf5725c,
  383.       $c53b6644c53b6644, $347efb5b347efb5b,
  384.       $7629438b7629438b, $dcc623cbdcc623cb,
  385.       $68fcedb668fcedb6, $63f1e4b863f1e4b8,
  386.       $cadc31d7cadc31d7, $1085634210856342,
  387.       $4022971340229713, $2011c6842011c684,
  388.       $7d244a857d244a85, $f83dbbd2f83dbbd2,
  389.       $1132f9ae1132f9ae, $6da129c76da129c7,
  390.       $4b2f9e1d4b2f9e1d, $f330b2dcf330b2dc,
  391.       $ec52860dec52860d, $d0e3c177d0e3c177,
  392.       $6c16b32b6c16b32b, $99b970a999b970a9,
  393.       $fa489411fa489411, $2264e9472264e947,
  394.       $c48cfca8c48cfca8, $1a3ff0a01a3ff0a0,
  395.       $d82c7d56d82c7d56, $ef903322ef903322,
  396.       $c74e4987c74e4987, $c1d138d9c1d138d9,
  397.       $fea2ca8cfea2ca8c, $360bd498360bd498,
  398.       $cf81f5a6cf81f5a6, $28de7aa528de7aa5,
  399.       $268eb7da268eb7da, $a4bfad3fa4bfad3f,
  400.       $e49d3a2ce49d3a2c, $0d9278500d927850,
  401.       $9bcc5f6a9bcc5f6a, $62467e5462467e54,
  402.       $c2138df6c2138df6, $e8b8d890e8b8d890,
  403.       $5ef7392e5ef7392e, $f5afc382f5afc382,
  404.       $be805d9fbe805d9f, $7c93d0697c93d069,
  405.       $a92dd56fa92dd56f, $b31225cfb31225cf,
  406.       $3b99acc83b99acc8, $a77d1810a77d1810,
  407.       $6e639ce86e639ce8, $7bbb3bdb7bbb3bdb,
  408.       $097826cd097826cd, $f418596ef418596e,
  409.       $01b79aec01b79aec, $a89a4f83a89a4f83,
  410.       $656e95e6656e95e6, $7ee6ffaa7ee6ffaa,
  411.       $08cfbc2108cfbc21, $e6e815efe6e815ef,
  412.       $d99be7bad99be7ba, $ce366f4ace366f4a,
  413.       $d4099fead4099fea, $d67cb029d67cb029,
  414.       $afb2a431afb2a431, $31233f2a31233f2a,
  415.       $3094a5c63094a5c6, $c066a235c066a235,
  416.       $37bc4e7437bc4e74, $a6ca82fca6ca82fc,
  417.       $b0d090e0b0d090e0, $15d8a73315d8a733,
  418.       $4a9804f14a9804f1, $f7daec41f7daec41,
  419.       $0e50cd7f0e50cd7f, $2ff691172ff69117,
  420.       $8dd64d768dd64d76, $4db0ef434db0ef43,
  421.       $544daacc544daacc, $df0496e4df0496e4,
  422.       $e3b5d19ee3b5d19e, $1b886a4c1b886a4c,
  423.       $b81f2cc1b81f2cc1, $7f5165467f516546,
  424.       $04ea5e9d04ea5e9d, $5d358c015d358c01,
  425.       $737487fa737487fa, $2e410bfb2e410bfb,
  426.       $5a1d67b35a1d67b3, $52d2db9252d2db92,
  427.       $335610e9335610e9, $1347d66d1347d66d,
  428.       $8c61d79a8c61d79a, $7a0ca1377a0ca137,
  429.       $8e14f8598e14f859, $893c13eb893c13eb,
  430.       $ee27a9ceee27a9ce, $35c961b735c961b7,
  431.       $ede51ce1ede51ce1, $3cb1477a3cb1477a,
  432.       $59dfd29c59dfd29c, $3f73f2553f73f255,
  433.       $79ce141879ce1418, $bf37c773bf37c773,
  434.       $eacdf753eacdf753, $5baafd5f5baafd5f,
  435.       $146f3ddf146f3ddf, $86db447886db4478,
  436.       $81f3afca81f3afca, $3ec468b93ec468b9,
  437.       $2c3424382c342438, $5f40a3c25f40a3c2,
  438.       $72c31d1672c31d16, $0c25e2bc0c25e2bc,
  439.       $8b493c288b493c28, $41950dff41950dff,
  440.       $7101a8397101a839, $deb30c08deb30c08,
  441.       $9ce4b4d89ce4b4d8, $90c1566490c15664,
  442.       $6184cb7b6184cb7b, $70b632d570b632d5,
  443.       $745c6c48745c6c48, $4257b8d04257b8d0
  444.   );
  445.   const Td4:array[0..255]of byte = (
  446.       $52, $09, $6a, $d5, $30, $36, $a5, $38,
  447.       $bf, $40, $a3, $9e, $81, $f3, $d7, $fb,
  448.       $7c, $e3, $39, $82, $9b, $2f, $ff, $87,
  449.       $34, $8e, $43, $44, $c4, $de, $e9, $cb,
  450.       $54, $7b, $94, $32, $a6, $c2, $23, $3d,
  451.       $ee, $4c, $95, $0b, $42, $fa, $c3, $4e,
  452.       $08, $2e, $a1, $66, $28, $d9, $24, $b2,
  453.       $76, $5b, $a2, $49, $6d, $8b, $d1, $25,
  454.       $72, $f8, $f6, $64, $86, $68, $98, $16,
  455.       $d4, $a4, $5c, $cc, $5d, $65, $b6, $92,
  456.       $6c, $70, $48, $50, $fd, $ed, $b9, $da,
  457.       $5e, $15, $46, $57, $a7, $8d, $9d, $84,
  458.       $90, $d8, $ab, $00, $8c, $bc, $d3, $0a,
  459.       $f7, $e4, $58, $05, $b8, $b3, $45, $06,
  460.       $d0, $2c, $1e, $8f, $ca, $3f, $0f, $02,
  461.       $c1, $af, $bd, $03, $01, $13, $8a, $6b,
  462.       $3a, $91, $11, $41, $4f, $67, $dc, $ea,
  463.       $97, $f2, $cf, $ce, $f0, $b4, $e6, $73,
  464.       $96, $ac, $74, $22, $e7, $ad, $35, $85,
  465.       $e2, $f9, $37, $e8, $1c, $75, $df, $6e,
  466.       $47, $f1, $1a, $71, $1d, $29, $c5, $89,
  467.       $6f, $b7, $62, $0e, $aa, $18, $be, $1b,
  468.       $fc, $56, $3e, $4b, $c6, $d2, $79, $20,
  469.       $9a, $db, $c0, $fe, $78, $cd, $5a, $f4,
  470.       $1f, $dd, $a8, $33, $88, $07, $c7, $31,
  471.       $b1, $12, $10, $59, $27, $80, $ec, $5f,
  472.       $60, $51, $7f, $a9, $19, $b5, $4a, $0d,
  473.       $2d, $e5, $7a, $9f, $93, $c9, $9c, $ef,
  474.       $a0, $e0, $3b, $4d, $ae, $2a, $f5, $b0,
  475.       $c8, $eb, $bb, $3c, $83, $53, $99, $61,
  476.       $17, $2b, $04, $7e, $ba, $77, $d6, $26,
  477.       $e1, $69, $14, $63, $55, $21, $0c, $7d
  478.   );
  479.  
  480.   const rcon:array[0..9]of dword = (
  481.       $00000001, $00000002, $00000004, $00000008,
  482.       $00000010, $00000020, $00000040, $00000080,
  483.       $0000001b, $00000036  (* for 128-bit blocks, Rijndael never uses more than 10 rcon values *)
  484.   );
  485.  
  486.   (**
  487.    * Expand the cipher key into the encryption key schedule.
  488.    *)
  489.   const
  490.     AES_MAXNR=14;
  491.     AES_BLOCK_SIZE=16;
  492.   type
  493.     AES_KEY = record
  494.       rd_key: array [0..(4 *(AES_MAXNR + 1))-1] of dword;
  495.       rounds:dword;
  496.     end;
  497.     pAES_KEY = ^AES_KEY;
  498.   function AES_set_encrypt_key(
  499.     const userKey:pchar;
  500.     const bits   :dword;
  501.     var   key    :pAES_KEY):dword;
  502.   var rk:pdword;
  503.   const i :dword = 0;
  504.   var temp:dword;
  505.   begin
  506.  
  507.  
  508.  
  509.     if ((userKey=nil) or (key=nil)) then
  510.       result:= -1;
  511.     if ((bits <> 128) and (bits <> 192) and (bits <> 256)) then
  512.       result:= -2;
  513.  
  514.     rk := key^.rd_key;
  515.  
  516.     if (bits=128) then
  517.       key^.rounds := 10
  518.     else if (bits=192) then
  519.       key^.rounds := 12
  520.     else
  521.       key^.rounds := 14;
  522.  
  523.     rk[0] := GETU32(userKey     );
  524.     rk[1] := GETU32(userKey +  4);
  525.     rk[2] := GETU32(userKey +  8);
  526.     rk[3] := GETU32(userKey + 12);
  527.     if (bits = 128) then begin
  528.       while true do begin
  529.         temp  := rk[3];
  530.         rk[4] := rk[0] xor
  531.           (Te4[(temp shr  8) AND $ff]       ) xor
  532.           (Te4[(temp shr 16) AND $ff] shl  8) xor
  533.           (Te4[(temp shr 24)      ] shl 16) xor
  534.           (Te4[(temp       ) AND $ff] shl 24) xor
  535.           rcon[i];
  536.         rk[5] := rk[1] xor rk[4];
  537.         rk[6] := rk[2] xor rk[5];
  538.         rk[7] := rk[3] xor rk[6];
  539.                           inc(i);
  540.         if (i = 10) then begin
  541.           result:= 0;break;
  542.         end;
  543.         rk := rk + 4;
  544.       end;
  545.     end;
  546.     rk[4] := GETU32(userKey + 16);
  547.     rk[5] := GETU32(userKey + 20);
  548.     if (bits = 192) then begin
  549.       while true do begin
  550.         temp := rk[ 5];
  551.         rk[ 6] := rk[ 0] xor
  552.           (Te4[(temp shr  8) AND $ff]      ) xor
  553.           (Te4[(temp shr 16) AND $ff] shl  8) xor
  554.           (Te4[(temp shr 24)       ] shl 16) xor
  555.           (Te4[(temp      ) AND $ff] shl 24) xor
  556.           rcon[i];
  557.         rk[ 7] := rk[ 1] xor rk[ 6];
  558.         rk[ 8] := rk[ 2] xor rk[ 7];
  559.         rk[ 9] := rk[ 3] xor rk[ 8];
  560.                           inc(i);
  561.         if (i = 8) then begin
  562.           result:= 0;break;
  563.         end;
  564.         rk[10] := rk[ 4] xor rk[ 9];
  565.         rk[11] := rk[ 5] xor rk[10];
  566.         rk := rk + 6;
  567.       end;
  568.     end;
  569.     rk[6] := GETU32(userKey + 24);
  570.     rk[7] := GETU32(userKey + 28);
  571.     if (bits = 256) then begin
  572.       while true do begin
  573.         temp := rk[ 7];
  574.         rk[ 8] := rk[ 0] xor
  575.           (Te4[(temp shr  8) AND $ff]      ) xor
  576.           (Te4[(temp shr 16) AND $ff] shl  8) xor
  577.           (Te4[(temp shr 24)       ] shl 16) xor
  578.           (Te4[(temp      ) AND $ff] shl 24) xor
  579.           rcon[i];
  580.         rk[ 9] := rk[ 1] xor rk[ 8];
  581.         rk[10] := rk[ 2] xor rk[ 9];
  582.         rk[11] := rk[ 3] xor rk[10];
  583.                           inc(i);
  584.         if (i = 7) then begin
  585.           result:= 0;break;
  586.         end;
  587.         temp := rk[11];
  588.         rk[12] := rk[ 4] xor
  589.           (Te4[(temp      ) AND $ff]      ) xor
  590.           (Te4[(temp shr  8) AND $ff] shl  8) xor
  591.           (Te4[(temp shr 16) AND $ff] shl 16) xor
  592.           (Te4[(temp shr 24)       ] shl 24);
  593.         rk[13] := rk[ 5] xor rk[12];
  594.         rk[14] := rk[ 6] xor rk[13];
  595.         rk[15] := rk[ 7] xor rk[14];
  596.  
  597.         rk := rk+8;
  598.             end;
  599.     end;
  600.     result:= 0;
  601.   end;
  602.  
  603.   (**
  604.    * Expand the cipher key into the decryption key schedule.
  605.    *)
  606.   function AES_set_decrypt_key(
  607.     const userKey:pchar;
  608.     const bits   :dword;
  609.     var   key    :pAES_KEY):dword;
  610.   var
  611.     rk:pdword;
  612.     i, j, status:dword;
  613.     temp:dword;
  614.   var
  615.     tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m:dword;
  616.   begin
  617.  
  618.  
  619.  
  620.     (* first, start with an encryption schedule *)
  621.     status := AES_set_encrypt_key(userKey, bits, key);
  622.     if (status < 0) then
  623.       result:= status;
  624.  
  625.     rk := key^.rd_key;
  626.  
  627.     (* invert the order of the round keys: *)
  628.           i := 0; j := 4*(key^.rounds);
  629.     while ( i < j ) do begin
  630.       temp := rk[i    ]; rk[i    ] := rk[j    ]; rk[j    ] := temp;
  631.       temp := rk[i + 1]; rk[i + 1] := rk[j + 1]; rk[j + 1] := temp;
  632.       temp := rk[i + 2]; rk[i + 2] := rk[j + 2]; rk[j + 2] := temp;
  633.       temp := rk[i + 3]; rk[i + 3] := rk[j + 3]; rk[j + 3] := temp;
  634.                   i := i+4; j := j -4
  635.     end;
  636.     (* apply the inverse MixColumn transform to all round keys but the first and the last: *)
  637.           i := 1;
  638.     while ( i < (key^.rounds) ) do begin
  639.       rk :=rk + 4;
  640.   {$if 1}
  641.                   j := 0;
  642.                   while ( j < 4 ) do begin
  643.         //u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
  644.  
  645.         tp1 := rk[j];
  646.         m := tp1 AND $80808080;
  647.         tp2 := ((tp1 AND $7f7f7f7f) shl 1) xor
  648.           ((m - (m shr 7)) AND $1b1b1b1b);
  649.         m := tp2 AND $80808080;
  650.         tp4 := ((tp2 AND $7f7f7f7f) shl 1) xor
  651.           ((m - (m shr 7)) AND $1b1b1b1b);
  652.         m := tp4 AND $80808080;
  653.         tp8 := ((tp4 AND $7f7f7f7f) shl 1) xor
  654.           ((m - (m shr 7)) AND $1b1b1b1b);
  655.         tp9 := tp8 xor tp1;
  656.         tpb := tp9 xor tp2;
  657.         tpd := tp9 xor tp4;
  658.         tpe := tp8 xor tp4 xor tp2;
  659.   {$if defined(ROTATE)}
  660.         rk[j] := tpe xor ROTATE(tpd,16) xor
  661.           ROTATE(tp9,8) xor ROTATE(tpb,24);
  662.   {$else}
  663.         rk[j] := tpe xor (tpd shr 16) xor (tpd shl 16) xor
  664.           (tp9 shr 24) xor (tp9 shl 8) xor
  665.           (tpb shr 8) xor (tpb shl 24);
  666.   {$endif}
  667.                   inc(j);
  668.       end;
  669.   {$else}
  670.       rk[0] :=
  671.         Td0[Te2[(rk[0]      ) AND $ff] AND $ff] xor
  672.         Td1[Te2[(rk[0] shr  8) AND $ff] AND $ff] xor
  673.         Td2[Te2[(rk[0] shr 16) AND $ff] AND $ff] xor
  674.         Td3[Te2[(rk[0] shr 24)       ] AND $ff];
  675.       rk[1] :=
  676.         Td0[Te2[(rk[1]      ) AND $ff] AND $ff] xor
  677.         Td1[Te2[(rk[1] shr  8) AND $ff] AND $ff] xor
  678.         Td2[Te2[(rk[1] shr 16) AND $ff] AND $ff] xor
  679.         Td3[Te2[(rk[1] shr 24)       ] AND $ff];
  680.       rk[2] :=
  681.         Td0[Te2[(rk[2]      ) AND $ff] AND $ff] xor
  682.         Td1[Te2[(rk[2] shr  8) AND $ff] AND $ff] xor
  683.         Td2[Te2[(rk[2] shr 16) AND $ff] AND $ff] xor
  684.         Td3[Te2[(rk[2] shr 24)       ] AND $ff];
  685.       rk[3] :=
  686.         Td0[Te2[(rk[3]      ) AND $ff] AND $ff] xor
  687.         Td1[Te2[(rk[3] shr  8) AND $ff] AND $ff] xor
  688.         Td2[Te2[(rk[3] shr 16) AND $ff] AND $ff] xor
  689.         Td3[Te2[(rk[3] shr 24)       ] AND $ff];
  690.   {$endif}
  691.             inc(i);
  692.     end;
  693.     result:= 0;
  694.   end;
  695.  
  696.   (*
  697.    * Encrypt a single block
  698.    * in and out can overlap
  699.    *)
  700.   const Te0:array[0..255]of dword=(
  701.       $c66363a5, $f87c7c84, $ee777799, $f67b7b8d,
  702.       $fff2f20d, $d66b6bbd, $de6f6fb1, $91c5c554,
  703.       $60303050, $02010103, $ce6767a9, $562b2b7d,
  704.       $e7fefe19, $b5d7d762, $4dababe6, $ec76769a,
  705.       $8fcaca45, $1f82829d, $89c9c940, $fa7d7d87,
  706.       $effafa15, $b25959eb, $8e4747c9, $fbf0f00b,
  707.       $41adadec, $b3d4d467, $5fa2a2fd, $45afafea,
  708.       $239c9cbf, $53a4a4f7, $e4727296, $9bc0c05b,
  709.       $75b7b7c2, $e1fdfd1c, $3d9393ae, $4c26266a,
  710.       $6c36365a, $7e3f3f41, $f5f7f702, $83cccc4f,
  711.       $6834345c, $51a5a5f4, $d1e5e534, $f9f1f108,
  712.       $e2717193, $abd8d873, $62313153, $2a15153f,
  713.       $0804040c, $95c7c752, $46232365, $9dc3c35e,
  714.       $30181828, $379696a1, $0a05050f, $2f9a9ab5,
  715.       $0e070709, $24121236, $1b80809b, $dfe2e23d,
  716.       $cdebeb26, $4e272769, $7fb2b2cd, $ea75759f,
  717.       $1209091b, $1d83839e, $582c2c74, $341a1a2e,
  718.       $361b1b2d, $dc6e6eb2, $b45a5aee, $5ba0a0fb,
  719.       $a45252f6, $763b3b4d, $b7d6d661, $7db3b3ce,
  720.       $5229297b, $dde3e33e, $5e2f2f71, $13848497,
  721.       $a65353f5, $b9d1d168, $00000000, $c1eded2c,
  722.       $40202060, $e3fcfc1f, $79b1b1c8, $b65b5bed,
  723.       $d46a6abe, $8dcbcb46, $67bebed9, $7239394b,
  724.       $944a4ade, $984c4cd4, $b05858e8, $85cfcf4a,
  725.       $bbd0d06b, $c5efef2a, $4faaaae5, $edfbfb16,
  726.       $864343c5, $9a4d4dd7, $66333355, $11858594,
  727.       $8a4545cf, $e9f9f910, $04020206, $fe7f7f81,
  728.       $a05050f0, $783c3c44, $259f9fba, $4ba8a8e3,
  729.       $a25151f3, $5da3a3fe, $804040c0, $058f8f8a,
  730.       $3f9292ad, $219d9dbc, $70383848, $f1f5f504,
  731.       $63bcbcdf, $77b6b6c1, $afdada75, $42212163,
  732.       $20101030, $e5ffff1a, $fdf3f30e, $bfd2d26d,
  733.       $81cdcd4c, $180c0c14, $26131335, $c3ecec2f,
  734.       $be5f5fe1, $359797a2, $884444cc, $2e171739,
  735.       $93c4c457, $55a7a7f2, $fc7e7e82, $7a3d3d47,
  736.       $c86464ac, $ba5d5de7, $3219192b, $e6737395,
  737.       $c06060a0, $19818198, $9e4f4fd1, $a3dcdc7f,
  738.       $44222266, $542a2a7e, $3b9090ab, $0b888883,
  739.       $8c4646ca, $c7eeee29, $6bb8b8d3, $2814143c,
  740.       $a7dede79, $bc5e5ee2, $160b0b1d, $addbdb76,
  741.       $dbe0e03b, $64323256, $743a3a4e, $140a0a1e,
  742.       $924949db, $0c06060a, $4824246c, $b85c5ce4,
  743.       $9fc2c25d, $bdd3d36e, $43acacef, $c46262a6,
  744.       $399191a8, $319595a4, $d3e4e437, $f279798b,
  745.       $d5e7e732, $8bc8c843, $6e373759, $da6d6db7,
  746.       $018d8d8c, $b1d5d564, $9c4e4ed2, $49a9a9e0,
  747.       $d86c6cb4, $ac5656fa, $f3f4f407, $cfeaea25,
  748.       $ca6565af, $f47a7a8e, $47aeaee9, $10080818,
  749.       $6fbabad5, $f0787888, $4a25256f, $5c2e2e72,
  750.       $381c1c24, $57a6a6f1, $73b4b4c7, $97c6c651,
  751.       $cbe8e823, $a1dddd7c, $e874749c, $3e1f1f21,
  752.       $964b4bdd, $61bdbddc, $0d8b8b86, $0f8a8a85,
  753.       $e0707090, $7c3e3e42, $71b5b5c4, $cc6666aa,
  754.       $904848d8, $06030305, $f7f6f601, $1c0e0e12,
  755.       $c26161a3, $6a35355f, $ae5757f9, $69b9b9d0,
  756.       $17868691, $99c1c158, $3a1d1d27, $279e9eb9,
  757.       $d9e1e138, $ebf8f813, $2b9898b3, $22111133,
  758.       $d26969bb, $a9d9d970, $078e8e89, $339494a7,
  759.       $2d9b9bb6, $3c1e1e22, $15878792, $c9e9e920,
  760.       $87cece49, $aa5555ff, $50282878, $a5dfdf7a,
  761.       $038c8c8f, $59a1a1f8, $09898980, $1a0d0d17,
  762.       $65bfbfda, $d7e6e631, $844242c6, $d06868b8,
  763.       $824141c3, $299999b0, $5a2d2d77, $1e0f0f11,
  764.       $7bb0b0cb, $a85454fc, $6dbbbbd6, $2c16163a
  765.   );
  766.   const Te1:array[0..255]of dword=(
  767.       $a5c66363, $84f87c7c, $99ee7777, $8df67b7b,
  768.       $0dfff2f2, $bdd66b6b, $b1de6f6f, $5491c5c5,
  769.       $50603030, $03020101, $a9ce6767, $7d562b2b,
  770.       $19e7fefe, $62b5d7d7, $e64dabab, $9aec7676,
  771.       $458fcaca, $9d1f8282, $4089c9c9, $87fa7d7d,
  772.       $15effafa, $ebb25959, $c98e4747, $0bfbf0f0,
  773.       $ec41adad, $67b3d4d4, $fd5fa2a2, $ea45afaf,
  774.       $bf239c9c, $f753a4a4, $96e47272, $5b9bc0c0,
  775.       $c275b7b7, $1ce1fdfd, $ae3d9393, $6a4c2626,
  776.       $5a6c3636, $417e3f3f, $02f5f7f7, $4f83cccc,
  777.       $5c683434, $f451a5a5, $34d1e5e5, $08f9f1f1,
  778.       $93e27171, $73abd8d8, $53623131, $3f2a1515,
  779.       $0c080404, $5295c7c7, $65462323, $5e9dc3c3,
  780.       $28301818, $a1379696, $0f0a0505, $b52f9a9a,
  781.       $090e0707, $36241212, $9b1b8080, $3ddfe2e2,
  782.       $26cdebeb, $694e2727, $cd7fb2b2, $9fea7575,
  783.       $1b120909, $9e1d8383, $74582c2c, $2e341a1a,
  784.       $2d361b1b, $b2dc6e6e, $eeb45a5a, $fb5ba0a0,
  785.       $f6a45252, $4d763b3b, $61b7d6d6, $ce7db3b3,
  786.       $7b522929, $3edde3e3, $715e2f2f, $97138484,
  787.       $f5a65353, $68b9d1d1, $00000000, $2cc1eded,
  788.       $60402020, $1fe3fcfc, $c879b1b1, $edb65b5b,
  789.       $bed46a6a, $468dcbcb, $d967bebe, $4b723939,
  790.       $de944a4a, $d4984c4c, $e8b05858, $4a85cfcf,
  791.       $6bbbd0d0, $2ac5efef, $e54faaaa, $16edfbfb,
  792.       $c5864343, $d79a4d4d, $55663333, $94118585,
  793.       $cf8a4545, $10e9f9f9, $06040202, $81fe7f7f,
  794.       $f0a05050, $44783c3c, $ba259f9f, $e34ba8a8,
  795.       $f3a25151, $fe5da3a3, $c0804040, $8a058f8f,
  796.       $ad3f9292, $bc219d9d, $48703838, $04f1f5f5,
  797.       $df63bcbc, $c177b6b6, $75afdada, $63422121,
  798.       $30201010, $1ae5ffff, $0efdf3f3, $6dbfd2d2,
  799.       $4c81cdcd, $14180c0c, $35261313, $2fc3ecec,
  800.       $e1be5f5f, $a2359797, $cc884444, $392e1717,
  801.       $5793c4c4, $f255a7a7, $82fc7e7e, $477a3d3d,
  802.       $acc86464, $e7ba5d5d, $2b321919, $95e67373,
  803.       $a0c06060, $98198181, $d19e4f4f, $7fa3dcdc,
  804.       $66442222, $7e542a2a, $ab3b9090, $830b8888,
  805.       $ca8c4646, $29c7eeee, $d36bb8b8, $3c281414,
  806.       $79a7dede, $e2bc5e5e, $1d160b0b, $76addbdb,
  807.       $3bdbe0e0, $56643232, $4e743a3a, $1e140a0a,
  808.       $db924949, $0a0c0606, $6c482424, $e4b85c5c,
  809.       $5d9fc2c2, $6ebdd3d3, $ef43acac, $a6c46262,
  810.       $a8399191, $a4319595, $37d3e4e4, $8bf27979,
  811.       $32d5e7e7, $438bc8c8, $596e3737, $b7da6d6d,
  812.       $8c018d8d, $64b1d5d5, $d29c4e4e, $e049a9a9,
  813.       $b4d86c6c, $faac5656, $07f3f4f4, $25cfeaea,
  814.       $afca6565, $8ef47a7a, $e947aeae, $18100808,
  815.       $d56fbaba, $88f07878, $6f4a2525, $725c2e2e,
  816.       $24381c1c, $f157a6a6, $c773b4b4, $5197c6c6,
  817.       $23cbe8e8, $7ca1dddd, $9ce87474, $213e1f1f,
  818.       $dd964b4b, $dc61bdbd, $860d8b8b, $850f8a8a,
  819.       $90e07070, $427c3e3e, $c471b5b5, $aacc6666,
  820.       $d8904848, $05060303, $01f7f6f6, $121c0e0e,
  821.       $a3c26161, $5f6a3535, $f9ae5757, $d069b9b9,
  822.       $91178686, $5899c1c1, $273a1d1d, $b9279e9e,
  823.       $38d9e1e1, $13ebf8f8, $b32b9898, $33221111,
  824.       $bbd26969, $70a9d9d9, $89078e8e, $a7339494,
  825.       $b62d9b9b, $223c1e1e, $92158787, $20c9e9e9,
  826.       $4987cece, $ffaa5555, $78502828, $7aa5dfdf,
  827.       $8f038c8c, $f859a1a1, $80098989, $171a0d0d,
  828.       $da65bfbf, $31d7e6e6, $c6844242, $b8d06868,
  829.       $c3824141, $b0299999, $775a2d2d, $111e0f0f,
  830.       $cb7bb0b0, $fca85454, $d66dbbbb, $3a2c1616
  831.   );
  832.   const Te2:array[0..255]of dword=(
  833.       $63a5c663, $7c84f87c, $7799ee77, $7b8df67b,
  834.       $f20dfff2, $6bbdd66b, $6fb1de6f, $c55491c5,
  835.       $30506030, $01030201, $67a9ce67, $2b7d562b,
  836.       $fe19e7fe, $d762b5d7, $abe64dab, $769aec76,
  837.       $ca458fca, $829d1f82, $c94089c9, $7d87fa7d,
  838.       $fa15effa, $59ebb259, $47c98e47, $f00bfbf0,
  839.       $adec41ad, $d467b3d4, $a2fd5fa2, $afea45af,
  840.       $9cbf239c, $a4f753a4, $7296e472, $c05b9bc0,
  841.       $b7c275b7, $fd1ce1fd, $93ae3d93, $266a4c26,
  842.       $365a6c36, $3f417e3f, $f702f5f7, $cc4f83cc,
  843.       $345c6834, $a5f451a5, $e534d1e5, $f108f9f1,
  844.       $7193e271, $d873abd8, $31536231, $153f2a15,
  845.       $040c0804, $c75295c7, $23654623, $c35e9dc3,
  846.       $18283018, $96a13796, $050f0a05, $9ab52f9a,
  847.       $07090e07, $12362412, $809b1b80, $e23ddfe2,
  848.       $eb26cdeb, $27694e27, $b2cd7fb2, $759fea75,
  849.       $091b1209, $839e1d83, $2c74582c, $1a2e341a,
  850.       $1b2d361b, $6eb2dc6e, $5aeeb45a, $a0fb5ba0,
  851.       $52f6a452, $3b4d763b, $d661b7d6, $b3ce7db3,
  852.       $297b5229, $e33edde3, $2f715e2f, $84971384,
  853.       $53f5a653, $d168b9d1, $00000000, $ed2cc1ed,
  854.       $20604020, $fc1fe3fc, $b1c879b1, $5bedb65b,
  855.       $6abed46a, $cb468dcb, $bed967be, $394b7239,
  856.       $4ade944a, $4cd4984c, $58e8b058, $cf4a85cf,
  857.       $d06bbbd0, $ef2ac5ef, $aae54faa, $fb16edfb,
  858.       $43c58643, $4dd79a4d, $33556633, $85941185,
  859.       $45cf8a45, $f910e9f9, $02060402, $7f81fe7f,
  860.       $50f0a050, $3c44783c, $9fba259f, $a8e34ba8,
  861.       $51f3a251, $a3fe5da3, $40c08040, $8f8a058f,
  862.       $92ad3f92, $9dbc219d, $38487038, $f504f1f5,
  863.       $bcdf63bc, $b6c177b6, $da75afda, $21634221,
  864.       $10302010, $ff1ae5ff, $f30efdf3, $d26dbfd2,
  865.       $cd4c81cd, $0c14180c, $13352613, $ec2fc3ec,
  866.       $5fe1be5f, $97a23597, $44cc8844, $17392e17,
  867.       $c45793c4, $a7f255a7, $7e82fc7e, $3d477a3d,
  868.       $64acc864, $5de7ba5d, $192b3219, $7395e673,
  869.       $60a0c060, $81981981, $4fd19e4f, $dc7fa3dc,
  870.       $22664422, $2a7e542a, $90ab3b90, $88830b88,
  871.       $46ca8c46, $ee29c7ee, $b8d36bb8, $143c2814,
  872.       $de79a7de, $5ee2bc5e, $0b1d160b, $db76addb,
  873.       $e03bdbe0, $32566432, $3a4e743a, $0a1e140a,
  874.       $49db9249, $060a0c06, $246c4824, $5ce4b85c,
  875.       $c25d9fc2, $d36ebdd3, $acef43ac, $62a6c462,
  876.       $91a83991, $95a43195, $e437d3e4, $798bf279,
  877.       $e732d5e7, $c8438bc8, $37596e37, $6db7da6d,
  878.       $8d8c018d, $d564b1d5, $4ed29c4e, $a9e049a9,
  879.       $6cb4d86c, $56faac56, $f407f3f4, $ea25cfea,
  880.       $65afca65, $7a8ef47a, $aee947ae, $08181008,
  881.       $bad56fba, $7888f078, $256f4a25, $2e725c2e,
  882.       $1c24381c, $a6f157a6, $b4c773b4, $c65197c6,
  883.       $e823cbe8, $dd7ca1dd, $749ce874, $1f213e1f,
  884.       $4bdd964b, $bddc61bd, $8b860d8b, $8a850f8a,
  885.       $7090e070, $3e427c3e, $b5c471b5, $66aacc66,
  886.       $48d89048, $03050603, $f601f7f6, $0e121c0e,
  887.       $61a3c261, $355f6a35, $57f9ae57, $b9d069b9,
  888.       $86911786, $c15899c1, $1d273a1d, $9eb9279e,
  889.       $e138d9e1, $f813ebf8, $98b32b98, $11332211,
  890.       $69bbd269, $d970a9d9, $8e89078e, $94a73394,
  891.       $9bb62d9b, $1e223c1e, $87921587, $e920c9e9,
  892.       $ce4987ce, $55ffaa55, $28785028, $df7aa5df,
  893.       $8c8f038c, $a1f859a1, $89800989, $0d171a0d,
  894.       $bfda65bf, $e631d7e6, $42c68442, $68b8d068,
  895.       $41c38241, $99b02999, $2d775a2d, $0f111e0f,
  896.       $b0cb7bb0, $54fca854, $bbd66dbb, $163a2c16
  897.   );
  898.   const Te3:array[0..255]of dword=(
  899.       $6363a5c6, $7c7c84f8, $777799ee, $7b7b8df6,
  900.       $f2f20dff, $6b6bbdd6, $6f6fb1de, $c5c55491,
  901.       $30305060, $01010302, $6767a9ce, $2b2b7d56,
  902.       $fefe19e7, $d7d762b5, $ababe64d, $76769aec,
  903.       $caca458f, $82829d1f, $c9c94089, $7d7d87fa,
  904.       $fafa15ef, $5959ebb2, $4747c98e, $f0f00bfb,
  905.       $adadec41, $d4d467b3, $a2a2fd5f, $afafea45,
  906.       $9c9cbf23, $a4a4f753, $727296e4, $c0c05b9b,
  907.       $b7b7c275, $fdfd1ce1, $9393ae3d, $26266a4c,
  908.       $36365a6c, $3f3f417e, $f7f702f5, $cccc4f83,
  909.       $34345c68, $a5a5f451, $e5e534d1, $f1f108f9,
  910.       $717193e2, $d8d873ab, $31315362, $15153f2a,
  911.       $04040c08, $c7c75295, $23236546, $c3c35e9d,
  912.       $18182830, $9696a137, $05050f0a, $9a9ab52f,
  913.       $0707090e, $12123624, $80809b1b, $e2e23ddf,
  914.       $ebeb26cd, $2727694e, $b2b2cd7f, $75759fea,
  915.       $09091b12, $83839e1d, $2c2c7458, $1a1a2e34,
  916.       $1b1b2d36, $6e6eb2dc, $5a5aeeb4, $a0a0fb5b,
  917.       $5252f6a4, $3b3b4d76, $d6d661b7, $b3b3ce7d,
  918.       $29297b52, $e3e33edd, $2f2f715e, $84849713,
  919.       $5353f5a6, $d1d168b9, $00000000, $eded2cc1,
  920.       $20206040, $fcfc1fe3, $b1b1c879, $5b5bedb6,
  921.       $6a6abed4, $cbcb468d, $bebed967, $39394b72,
  922.       $4a4ade94, $4c4cd498, $5858e8b0, $cfcf4a85,
  923.       $d0d06bbb, $efef2ac5, $aaaae54f, $fbfb16ed,
  924.       $4343c586, $4d4dd79a, $33335566, $85859411,
  925.       $4545cf8a, $f9f910e9, $02020604, $7f7f81fe,
  926.       $5050f0a0, $3c3c4478, $9f9fba25, $a8a8e34b,
  927.       $5151f3a2, $a3a3fe5d, $4040c080, $8f8f8a05,
  928.       $9292ad3f, $9d9dbc21, $38384870, $f5f504f1,
  929.       $bcbcdf63, $b6b6c177, $dada75af, $21216342,
  930.       $10103020, $ffff1ae5, $f3f30efd, $d2d26dbf,
  931.       $cdcd4c81, $0c0c1418, $13133526, $ecec2fc3,
  932.       $5f5fe1be, $9797a235, $4444cc88, $1717392e,
  933.       $c4c45793, $a7a7f255, $7e7e82fc, $3d3d477a,
  934.       $6464acc8, $5d5de7ba, $19192b32, $737395e6,
  935.       $6060a0c0, $81819819, $4f4fd19e, $dcdc7fa3,
  936.       $22226644, $2a2a7e54, $9090ab3b, $8888830b,
  937.       $4646ca8c, $eeee29c7, $b8b8d36b, $14143c28,
  938.       $dede79a7, $5e5ee2bc, $0b0b1d16, $dbdb76ad,
  939.       $e0e03bdb, $32325664, $3a3a4e74, $0a0a1e14,
  940.       $4949db92, $06060a0c, $24246c48, $5c5ce4b8,
  941.       $c2c25d9f, $d3d36ebd, $acacef43, $6262a6c4,
  942.       $9191a839, $9595a431, $e4e437d3, $79798bf2,
  943.       $e7e732d5, $c8c8438b, $3737596e, $6d6db7da,
  944.       $8d8d8c01, $d5d564b1, $4e4ed29c, $a9a9e049,
  945.       $6c6cb4d8, $5656faac, $f4f407f3, $eaea25cf,
  946.       $6565afca, $7a7a8ef4, $aeaee947, $08081810,
  947.       $babad56f, $787888f0, $25256f4a, $2e2e725c,
  948.       $1c1c2438, $a6a6f157, $b4b4c773, $c6c65197,
  949.       $e8e823cb, $dddd7ca1, $74749ce8, $1f1f213e,
  950.       $4b4bdd96, $bdbddc61, $8b8b860d, $8a8a850f,
  951.       $707090e0, $3e3e427c, $b5b5c471, $6666aacc,
  952.       $4848d890, $03030506, $f6f601f7, $0e0e121c,
  953.       $6161a3c2, $35355f6a, $5757f9ae, $b9b9d069,
  954.       $86869117, $c1c15899, $1d1d273a, $9e9eb927,
  955.       $e1e138d9, $f8f813eb, $9898b32b, $11113322,
  956.       $6969bbd2, $d9d970a9, $8e8e8907, $9494a733,
  957.       $9b9bb62d, $1e1e223c, $87879215, $e9e920c9,
  958.       $cece4987, $5555ffaa, $28287850, $dfdf7aa5,
  959.       $8c8c8f03, $a1a1f859, $89898009, $0d0d171a,
  960.       $bfbfda65, $e6e631d7, $4242c684, $6868b8d0,
  961.       $4141c382, $9999b029, $2d2d775a, $0f0f111e,
  962.       $b0b0cb7b, $5454fca8, $bbbbd66d, $16163a2c
  963.   );
  964.  
  965.   const Td0:array[0..255]of dword=(
  966.       $51f4a750, $7e416553, $1a17a4c3, $3a275e96,
  967.       $3bab6bcb, $1f9d45f1, $acfa58ab, $4be30393,
  968.       $2030fa55, $ad766df6, $88cc7691, $f5024c25,
  969.       $4fe5d7fc, $c52acbd7, $26354480, $b562a38f,
  970.       $deb15a49, $25ba1b67, $45ea0e98, $5dfec0e1,
  971.       $c32f7502, $814cf012, $8d4697a3, $6bd3f9c6,
  972.       $038f5fe7, $15929c95, $bf6d7aeb, $955259da,
  973.       $d4be832d, $587421d3, $49e06929, $8ec9c844,
  974.       $75c2896a, $f48e7978, $99583e6b, $27b971dd,
  975.       $bee14fb6, $f088ad17, $c920ac66, $7dce3ab4,
  976.       $63df4a18, $e51a3182, $97513360, $62537f45,
  977.       $b16477e0, $bb6bae84, $fe81a01c, $f9082b94,
  978.       $70486858, $8f45fd19, $94de6c87, $527bf8b7,
  979.       $ab73d323, $724b02e2, $e31f8f57, $6655ab2a,
  980.       $b2eb2807, $2fb5c203, $86c57b9a, $d33708a5,
  981.       $302887f2, $23bfa5b2, $02036aba, $ed16825c,
  982.       $8acf1c2b, $a779b492, $f307f2f0, $4e69e2a1,
  983.       $65daf4cd, $0605bed5, $d134621f, $c4a6fe8a,
  984.       $342e539d, $a2f355a0, $058ae132, $a4f6eb75,
  985.       $0b83ec39, $4060efaa, $5e719f06, $bd6e1051,
  986.       $3e218af9, $96dd063d, $dd3e05ae, $4de6bd46,
  987.       $91548db5, $71c45d05, $0406d46f, $605015ff,
  988.       $1998fb24, $d6bde997, $894043cc, $67d99e77,
  989.       $b0e842bd, $07898b88, $e7195b38, $79c8eedb,
  990.       $a17c0a47, $7c420fe9, $f8841ec9, $00000000,
  991.       $09808683, $322bed48, $1e1170ac, $6c5a724e,
  992.       $fd0efffb, $0f853856, $3daed51e, $362d3927,
  993.       $0a0fd964, $685ca621, $9b5b54d1, $24362e3a,
  994.       $0c0a67b1, $9357e70f, $b4ee96d2, $1b9b919e,
  995.       $80c0c54f, $61dc20a2, $5a774b69, $1c121a16,
  996.       $e293ba0a, $c0a02ae5, $3c22e043, $121b171d,
  997.       $0e090d0b, $f28bc7ad, $2db6a8b9, $141ea9c8,
  998.       $57f11985, $af75074c, $ee99ddbb, $a37f60fd,
  999.       $f701269f, $5c72f5bc, $44663bc5, $5bfb7e34,
  1000.       $8b432976, $cb23c6dc, $b6edfc68, $b8e4f163,
  1001.       $d731dcca, $42638510, $13972240, $84c61120,
  1002.       $854a247d, $d2bb3df8, $aef93211, $c729a16d,
  1003.       $1d9e2f4b, $dcb230f3, $0d8652ec, $77c1e3d0,
  1004.       $2bb3166c, $a970b999, $119448fa, $47e96422,
  1005.       $a8fc8cc4, $a0f03f1a, $567d2cd8, $223390ef,
  1006.       $87494ec7, $d938d1c1, $8ccaa2fe, $98d40b36,
  1007.       $a6f581cf, $a57ade28, $dab78e26, $3fadbfa4,
  1008.       $2c3a9de4, $5078920d, $6a5fcc9b, $547e4662,
  1009.       $f68d13c2, $90d8b8e8, $2e39f75e, $82c3aff5,
  1010.       $9f5d80be, $69d0937c, $6fd52da9, $cf2512b3,
  1011.       $c8ac993b, $10187da7, $e89c636e, $db3bbb7b,
  1012.       $cd267809, $6e5918f4, $ec9ab701, $834f9aa8,
  1013.       $e6956e65, $aaffe67e, $21bccf08, $ef15e8e6,
  1014.       $bae79bd9, $4a6f36ce, $ea9f09d4, $29b07cd6,
  1015.       $31a4b2af, $2a3f2331, $c6a59430, $35a266c0,
  1016.       $744ebc37, $fc82caa6, $e090d0b0, $33a7d815,
  1017.       $f104984a, $41ecdaf7, $7fcd500e, $1791f62f,
  1018.       $764dd68d, $43efb04d, $ccaa4d54, $e49604df,
  1019.       $9ed1b5e3, $4c6a881b, $c12c1fb8, $4665517f,
  1020.       $9d5eea04, $018c355d, $fa877473, $fb0b412e,
  1021.       $b3671d5a, $92dbd252, $e9105633, $6dd64713,
  1022.       $9ad7618c, $37a10c7a, $59f8148e, $eb133c89,
  1023.       $cea927ee, $b761c935, $e11ce5ed, $7a47b13c,
  1024.       $9cd2df59, $55f2733f, $1814ce79, $73c737bf,
  1025.       $53f7cdea, $5ffdaa5b, $df3d6f14, $7844db86,
  1026.       $caaff381, $b968c43e, $3824342c, $c2a3405f,
  1027.       $161dc372, $bce2250c, $283c498b, $ff0d9541,
  1028.       $39a80171, $080cb3de, $d8b4e49c, $6456c190,
  1029.       $7bcb8461, $d532b670, $486c5c74, $d0b85742
  1030.   );
  1031.   const Td1:array[0..255]of dword=(
  1032.       $5051f4a7, $537e4165, $c31a17a4, $963a275e,
  1033.       $cb3bab6b, $f11f9d45, $abacfa58, $934be303,
  1034.       $552030fa, $f6ad766d, $9188cc76, $25f5024c,
  1035.       $fc4fe5d7, $d7c52acb, $80263544, $8fb562a3,
  1036.       $49deb15a, $6725ba1b, $9845ea0e, $e15dfec0,
  1037.       $02c32f75, $12814cf0, $a38d4697, $c66bd3f9,
  1038.       $e7038f5f, $9515929c, $ebbf6d7a, $da955259,
  1039.       $2dd4be83, $d3587421, $2949e069, $448ec9c8,
  1040.       $6a75c289, $78f48e79, $6b99583e, $dd27b971,
  1041.       $b6bee14f, $17f088ad, $66c920ac, $b47dce3a,
  1042.       $1863df4a, $82e51a31, $60975133, $4562537f,
  1043.       $e0b16477, $84bb6bae, $1cfe81a0, $94f9082b,
  1044.       $58704868, $198f45fd, $8794de6c, $b7527bf8,
  1045.       $23ab73d3, $e2724b02, $57e31f8f, $2a6655ab,
  1046.       $07b2eb28, $032fb5c2, $9a86c57b, $a5d33708,
  1047.       $f2302887, $b223bfa5, $ba02036a, $5ced1682,
  1048.       $2b8acf1c, $92a779b4, $f0f307f2, $a14e69e2,
  1049.       $cd65daf4, $d50605be, $1fd13462, $8ac4a6fe,
  1050.       $9d342e53, $a0a2f355, $32058ae1, $75a4f6eb,
  1051.       $390b83ec, $aa4060ef, $065e719f, $51bd6e10,
  1052.       $f93e218a, $3d96dd06, $aedd3e05, $464de6bd,
  1053.       $b591548d, $0571c45d, $6f0406d4, $ff605015,
  1054.       $241998fb, $97d6bde9, $cc894043, $7767d99e,
  1055.       $bdb0e842, $8807898b, $38e7195b, $db79c8ee,
  1056.       $47a17c0a, $e97c420f, $c9f8841e, $00000000,
  1057.       $83098086, $48322bed, $ac1e1170, $4e6c5a72,
  1058.       $fbfd0eff, $560f8538, $1e3daed5, $27362d39,
  1059.       $640a0fd9, $21685ca6, $d19b5b54, $3a24362e,
  1060.       $b10c0a67, $0f9357e7, $d2b4ee96, $9e1b9b91,
  1061.       $4f80c0c5, $a261dc20, $695a774b, $161c121a,
  1062.       $0ae293ba, $e5c0a02a, $433c22e0, $1d121b17,
  1063.       $0b0e090d, $adf28bc7, $b92db6a8, $c8141ea9,
  1064.       $8557f119, $4caf7507, $bbee99dd, $fda37f60,
  1065.       $9ff70126, $bc5c72f5, $c544663b, $345bfb7e,
  1066.       $768b4329, $dccb23c6, $68b6edfc, $63b8e4f1,
  1067.       $cad731dc, $10426385, $40139722, $2084c611,
  1068.       $7d854a24, $f8d2bb3d, $11aef932, $6dc729a1,
  1069.       $4b1d9e2f, $f3dcb230, $ec0d8652, $d077c1e3,
  1070.       $6c2bb316, $99a970b9, $fa119448, $2247e964,
  1071.       $c4a8fc8c, $1aa0f03f, $d8567d2c, $ef223390,
  1072.       $c787494e, $c1d938d1, $fe8ccaa2, $3698d40b,
  1073.       $cfa6f581, $28a57ade, $26dab78e, $a43fadbf,
  1074.       $e42c3a9d, $0d507892, $9b6a5fcc, $62547e46,
  1075.       $c2f68d13, $e890d8b8, $5e2e39f7, $f582c3af,
  1076.       $be9f5d80, $7c69d093, $a96fd52d, $b3cf2512,
  1077.       $3bc8ac99, $a710187d, $6ee89c63, $7bdb3bbb,
  1078.       $09cd2678, $f46e5918, $01ec9ab7, $a8834f9a,
  1079.       $65e6956e, $7eaaffe6, $0821bccf, $e6ef15e8,
  1080.       $d9bae79b, $ce4a6f36, $d4ea9f09, $d629b07c,
  1081.       $af31a4b2, $312a3f23, $30c6a594, $c035a266,
  1082.       $37744ebc, $a6fc82ca, $b0e090d0, $1533a7d8,
  1083.       $4af10498, $f741ecda, $0e7fcd50, $2f1791f6,
  1084.       $8d764dd6, $4d43efb0, $54ccaa4d, $dfe49604,
  1085.       $e39ed1b5, $1b4c6a88, $b8c12c1f, $7f466551,
  1086.       $049d5eea, $5d018c35, $73fa8774, $2efb0b41,
  1087.       $5ab3671d, $5292dbd2, $33e91056, $136dd647,
  1088.       $8c9ad761, $7a37a10c, $8e59f814, $89eb133c,
  1089.       $eecea927, $35b761c9, $ede11ce5, $3c7a47b1,
  1090.       $599cd2df, $3f55f273, $791814ce, $bf73c737,
  1091.       $ea53f7cd, $5b5ffdaa, $14df3d6f, $867844db,
  1092.       $81caaff3, $3eb968c4, $2c382434, $5fc2a340,
  1093.       $72161dc3, $0cbce225, $8b283c49, $41ff0d95,
  1094.       $7139a801, $de080cb3, $9cd8b4e4, $906456c1,
  1095.       $617bcb84, $70d532b6, $74486c5c, $42d0b857
  1096.   );
  1097.   const Td2:array[0..255]of dword=(
  1098.       $a75051f4, $65537e41, $a4c31a17, $5e963a27,
  1099.       $6bcb3bab, $45f11f9d, $58abacfa, $03934be3,
  1100.       $fa552030, $6df6ad76, $769188cc, $4c25f502,
  1101.       $d7fc4fe5, $cbd7c52a, $44802635, $a38fb562,
  1102.       $5a49deb1, $1b6725ba, $0e9845ea, $c0e15dfe,
  1103.       $7502c32f, $f012814c, $97a38d46, $f9c66bd3,
  1104.       $5fe7038f, $9c951592, $7aebbf6d, $59da9552,
  1105.       $832dd4be, $21d35874, $692949e0, $c8448ec9,
  1106.       $896a75c2, $7978f48e, $3e6b9958, $71dd27b9,
  1107.       $4fb6bee1, $ad17f088, $ac66c920, $3ab47dce,
  1108.       $4a1863df, $3182e51a, $33609751, $7f456253,
  1109.       $77e0b164, $ae84bb6b, $a01cfe81, $2b94f908,
  1110.       $68587048, $fd198f45, $6c8794de, $f8b7527b,
  1111.       $d323ab73, $02e2724b, $8f57e31f, $ab2a6655,
  1112.       $2807b2eb, $c2032fb5, $7b9a86c5, $08a5d337,
  1113.       $87f23028, $a5b223bf, $6aba0203, $825ced16,
  1114.       $1c2b8acf, $b492a779, $f2f0f307, $e2a14e69,
  1115.       $f4cd65da, $bed50605, $621fd134, $fe8ac4a6,
  1116.       $539d342e, $55a0a2f3, $e132058a, $eb75a4f6,
  1117.       $ec390b83, $efaa4060, $9f065e71, $1051bd6e,
  1118.       $8af93e21, $063d96dd, $05aedd3e, $bd464de6,
  1119.       $8db59154, $5d0571c4, $d46f0406, $15ff6050,
  1120.       $fb241998, $e997d6bd, $43cc8940, $9e7767d9,
  1121.       $42bdb0e8, $8b880789, $5b38e719, $eedb79c8,
  1122.       $0a47a17c, $0fe97c42, $1ec9f884, $00000000,
  1123.       $86830980, $ed48322b, $70ac1e11, $724e6c5a,
  1124.       $fffbfd0e, $38560f85, $d51e3dae, $3927362d,
  1125.       $d9640a0f, $a621685c, $54d19b5b, $2e3a2436,
  1126.       $67b10c0a, $e70f9357, $96d2b4ee, $919e1b9b,
  1127.       $c54f80c0, $20a261dc, $4b695a77, $1a161c12,
  1128.       $ba0ae293, $2ae5c0a0, $e0433c22, $171d121b,
  1129.       $0d0b0e09, $c7adf28b, $a8b92db6, $a9c8141e,
  1130.       $198557f1, $074caf75, $ddbbee99, $60fda37f,
  1131.       $269ff701, $f5bc5c72, $3bc54466, $7e345bfb,
  1132.       $29768b43, $c6dccb23, $fc68b6ed, $f163b8e4,
  1133.       $dccad731, $85104263, $22401397, $112084c6,
  1134.       $247d854a, $3df8d2bb, $3211aef9, $a16dc729,
  1135.       $2f4b1d9e, $30f3dcb2, $52ec0d86, $e3d077c1,
  1136.       $166c2bb3, $b999a970, $48fa1194, $642247e9,
  1137.       $8cc4a8fc, $3f1aa0f0, $2cd8567d, $90ef2233,
  1138.       $4ec78749, $d1c1d938, $a2fe8cca, $0b3698d4,
  1139.       $81cfa6f5, $de28a57a, $8e26dab7, $bfa43fad,
  1140.       $9de42c3a, $920d5078, $cc9b6a5f, $4662547e,
  1141.       $13c2f68d, $b8e890d8, $f75e2e39, $aff582c3,
  1142.       $80be9f5d, $937c69d0, $2da96fd5, $12b3cf25,
  1143.       $993bc8ac, $7da71018, $636ee89c, $bb7bdb3b,
  1144.       $7809cd26, $18f46e59, $b701ec9a, $9aa8834f,
  1145.       $6e65e695, $e67eaaff, $cf0821bc, $e8e6ef15,
  1146.       $9bd9bae7, $36ce4a6f, $09d4ea9f, $7cd629b0,
  1147.       $b2af31a4, $23312a3f, $9430c6a5, $66c035a2,
  1148.       $bc37744e, $caa6fc82, $d0b0e090, $d81533a7,
  1149.       $984af104, $daf741ec, $500e7fcd, $f62f1791,
  1150.       $d68d764d, $b04d43ef, $4d54ccaa, $04dfe496,
  1151.       $b5e39ed1, $881b4c6a, $1fb8c12c, $517f4665,
  1152.       $ea049d5e, $355d018c, $7473fa87, $412efb0b,
  1153.       $1d5ab367, $d25292db, $5633e910, $47136dd6,
  1154.       $618c9ad7, $0c7a37a1, $148e59f8, $3c89eb13,
  1155.       $27eecea9, $c935b761, $e5ede11c, $b13c7a47,
  1156.       $df599cd2, $733f55f2, $ce791814, $37bf73c7,
  1157.       $cdea53f7, $aa5b5ffd, $6f14df3d, $db867844,
  1158.       $f381caaf, $c43eb968, $342c3824, $405fc2a3,
  1159.       $c372161d, $250cbce2, $498b283c, $9541ff0d,
  1160.       $017139a8, $b3de080c, $e49cd8b4, $c1906456,
  1161.       $84617bcb, $b670d532, $5c74486c, $5742d0b8
  1162.   );
  1163.   const Td3:array[0..255]of dword=(
  1164.       $f4a75051, $4165537e, $17a4c31a, $275e963a,
  1165.       $ab6bcb3b, $9d45f11f, $fa58abac, $e303934b,
  1166.       $30fa5520, $766df6ad, $cc769188, $024c25f5,
  1167.       $e5d7fc4f, $2acbd7c5, $35448026, $62a38fb5,
  1168.       $b15a49de, $ba1b6725, $ea0e9845, $fec0e15d,
  1169.       $2f7502c3, $4cf01281, $4697a38d, $d3f9c66b,
  1170.       $8f5fe703, $929c9515, $6d7aebbf, $5259da95,
  1171.       $be832dd4, $7421d358, $e0692949, $c9c8448e,
  1172.       $c2896a75, $8e7978f4, $583e6b99, $b971dd27,
  1173.       $e14fb6be, $88ad17f0, $20ac66c9, $ce3ab47d,
  1174.       $df4a1863, $1a3182e5, $51336097, $537f4562,
  1175.       $6477e0b1, $6bae84bb, $81a01cfe, $082b94f9,
  1176.       $48685870, $45fd198f, $de6c8794, $7bf8b752,
  1177.       $73d323ab, $4b02e272, $1f8f57e3, $55ab2a66,
  1178.       $eb2807b2, $b5c2032f, $c57b9a86, $3708a5d3,
  1179.       $2887f230, $bfa5b223, $036aba02, $16825ced,
  1180.       $cf1c2b8a, $79b492a7, $07f2f0f3, $69e2a14e,
  1181.       $daf4cd65, $05bed506, $34621fd1, $a6fe8ac4,
  1182.       $2e539d34, $f355a0a2, $8ae13205, $f6eb75a4,
  1183.       $83ec390b, $60efaa40, $719f065e, $6e1051bd,
  1184.       $218af93e, $dd063d96, $3e05aedd, $e6bd464d,
  1185.       $548db591, $c45d0571, $06d46f04, $5015ff60,
  1186.       $98fb2419, $bde997d6, $4043cc89, $d99e7767,
  1187.       $e842bdb0, $898b8807, $195b38e7, $c8eedb79,
  1188.       $7c0a47a1, $420fe97c, $841ec9f8, $00000000,
  1189.       $80868309, $2bed4832, $1170ac1e, $5a724e6c,
  1190.       $0efffbfd, $8538560f, $aed51e3d, $2d392736,
  1191.       $0fd9640a, $5ca62168, $5b54d19b, $362e3a24,
  1192.       $0a67b10c, $57e70f93, $ee96d2b4, $9b919e1b,
  1193.       $c0c54f80, $dc20a261, $774b695a, $121a161c,
  1194.       $93ba0ae2, $a02ae5c0, $22e0433c, $1b171d12,
  1195.       $090d0b0e, $8bc7adf2, $b6a8b92d, $1ea9c814,
  1196.       $f1198557, $75074caf, $99ddbbee, $7f60fda3,
  1197.       $01269ff7, $72f5bc5c, $663bc544, $fb7e345b,
  1198.       $4329768b, $23c6dccb, $edfc68b6, $e4f163b8,
  1199.       $31dccad7, $63851042, $97224013, $c6112084,
  1200.       $4a247d85, $bb3df8d2, $f93211ae, $29a16dc7,
  1201.       $9e2f4b1d, $b230f3dc, $8652ec0d, $c1e3d077,
  1202.       $b3166c2b, $70b999a9, $9448fa11, $e9642247,
  1203.       $fc8cc4a8, $f03f1aa0, $7d2cd856, $3390ef22,
  1204.       $494ec787, $38d1c1d9, $caa2fe8c, $d40b3698,
  1205.       $f581cfa6, $7ade28a5, $b78e26da, $adbfa43f,
  1206.       $3a9de42c, $78920d50, $5fcc9b6a, $7e466254,
  1207.       $8d13c2f6, $d8b8e890, $39f75e2e, $c3aff582,
  1208.       $5d80be9f, $d0937c69, $d52da96f, $2512b3cf,
  1209.       $ac993bc8, $187da710, $9c636ee8, $3bbb7bdb,
  1210.       $267809cd, $5918f46e, $9ab701ec, $4f9aa883,
  1211.       $956e65e6, $ffe67eaa, $bccf0821, $15e8e6ef,
  1212.       $e79bd9ba, $6f36ce4a, $9f09d4ea, $b07cd629,
  1213.       $a4b2af31, $3f23312a, $a59430c6, $a266c035,
  1214.       $4ebc3774, $82caa6fc, $90d0b0e0, $a7d81533,
  1215.       $04984af1, $ecdaf741, $cd500e7f, $91f62f17,
  1216.       $4dd68d76, $efb04d43, $aa4d54cc, $9604dfe4,
  1217.       $d1b5e39e, $6a881b4c, $2c1fb8c1, $65517f46,
  1218.       $5eea049d, $8c355d01, $877473fa, $0b412efb,
  1219.       $671d5ab3, $dbd25292, $105633e9, $d647136d,
  1220.       $d7618c9a, $a10c7a37, $f8148e59, $133c89eb,
  1221.       $a927eece, $61c935b7, $1ce5ede1, $47b13c7a,
  1222.       $d2df599c, $f2733f55, $14ce7918, $c737bf73,
  1223.       $f7cdea53, $fdaa5b5f, $3d6f14df, $44db8678,
  1224.       $aff381ca, $68c43eb9, $24342c38, $a3405fc2,
  1225.       $1dc37216, $e2250cbc, $3c498b28, $0d9541ff,
  1226.       $a8017139, $0cb3de08, $b4e49cd8, $56c19064,
  1227.       $cb84617b, $32b670d5, $6c5c7448, $b85742d0
  1228.   );
  1229.   procedure AES_encrypt(
  1230.     const in_AES_encrypt  : pchar;
  1231.     var   out_AES_encrypt : pchar;
  1232.     const            key  : pAES_KEY
  1233.   );
  1234.   var
  1235.     rk:pdword;
  1236.     s0, s1, s2, s3:dword;
  1237.     t:array[0..3]of dword;
  1238.     r:dword;
  1239.  
  1240.   var
  1241.     i:dword;
  1242.     r0, r1, r2:dword;
  1243.   begin
  1244.  
  1245.  
  1246.  
  1247.   //  assert(in and out and key);
  1248.     rk := key^.rd_key;
  1249.  
  1250.     (*
  1251.      * map byte array block to cipher state
  1252.      * and add initial round key:
  1253.      *)
  1254.     s0 := GETU32(in_AES_encrypt     ) xor rk[0];
  1255.     s1 := GETU32(in_AES_encrypt +  4) xor rk[1];
  1256.     s2 := GETU32(in_AES_encrypt +  8) xor rk[2];
  1257.     s3 := GETU32(in_AES_encrypt + 12) xor rk[3];
  1258.  
  1259.   {$if defined(AES_COMPACT_IN_OUTER_ROUNDS)}
  1260.   //  prefetch256(Te4);
  1261.  
  1262.     t[0] :=  Te4[(s0      ) AND $ff]       xor
  1263.       Te4[(s1 shr  8) AND $ff] shl  8 xor
  1264.       Te4[(s2 shr 16) AND $ff] shl 16 xor
  1265.       Te4[(s3 shr 24)       ] shl 24;
  1266.     t[1] :=  Te4[(s1      ) AND $ff]       xor
  1267.       Te4[(s2 shr  8) AND $ff] shl  8 xor
  1268.       Te4[(s3 shr 16) AND $ff] shl 16 xor
  1269.       Te4[(s0 shr 24)       ] shl 24;
  1270.     t[2] :=  Te4[(s2      ) AND $ff]       xor
  1271.       Te4[(s3 shr  8) AND $ff] shl  8 xor
  1272.       Te4[(s0 shr 16) AND $ff] shl 16 xor
  1273.       Te4[(s1 shr 24)       ] shl 24;
  1274.     t[3] :=  Te4[(s3      ) AND $ff]       xor
  1275.       Te4[(s0 shr  8) AND $ff] shl  8 xor
  1276.       Te4[(s1 shr 16) AND $ff] shl 16 xor
  1277.       Te4[(s2 shr 24)       ] shl 24;
  1278.  
  1279.     (* now do the linear transform using words *)
  1280.     begin
  1281.                   i := 0;
  1282.       while ( i < 4) do begin
  1283.         r0 := t[i];
  1284.         r1 := r0 AND $80808080;
  1285.         r2 := ((r0 AND $7f7f7f7f) shl 1) xor
  1286.           ((r1 - (r1 shr 7)) AND $1b1b1b1b);
  1287.   {$if defined(ROTATE)}
  1288.         t[i] := r2 xor ROTATE(r2,24) xor ROTATE(r0,24) xor
  1289.           ROTATE(r0,16) xor ROTATE(r0,8);
  1290.   {$else}
  1291.         t[i] := r2 xor ((r2 xor r0) shl 24) xor ((r2 xor r0) shr 8) xor
  1292.           (r0 shl 16) xor (r0 shr 16) xor
  1293.           (r0 shl 8) xor (r0 shr 24);
  1294.   {$endif}
  1295.         t[i] := t[i] xor rk[4+i];
  1296.                     inc(i);
  1297.       end;
  1298.     end;
  1299.   {$else}
  1300.     t[0] :=  Te0[(s0      ) AND $ff] xor
  1301.       Te1[(s1 shr  8) AND $ff] xor
  1302.       Te2[(s2 shr 16) AND $ff] xor
  1303.       Te3[(s3 shr 24)       ] xor
  1304.       rk[4];
  1305.     t[1] :=  Te0[(s1      ) AND $ff] xor
  1306.       Te1[(s2 shr  8) AND $ff] xor
  1307.       Te2[(s3 shr 16) AND $ff] xor
  1308.       Te3[(s0 shr 24)       ] xor
  1309.       rk[5];
  1310.     t[2] :=  Te0[(s2      ) AND $ff] xor
  1311.       Te1[(s3 shr  8) AND $ff] xor
  1312.       Te2[(s0 shr 16) AND $ff] xor
  1313.       Te3[(s1 shr 24)       ] xor
  1314.       rk[6];
  1315.     t[3] :=  Te0[(s3      ) AND $ff] xor
  1316.       Te1[(s0 shr  8) AND $ff] xor
  1317.       Te2[(s1 shr 16) AND $ff] xor
  1318.       Te3[(s2 shr 24)       ] xor
  1319.       rk[7];
  1320.   {$endif}
  1321.     s0 := t[0]; s1 := t[1]; s2 := t[2]; s3 := t[3];
  1322.  
  1323.       (*
  1324.        * Nr - 2 full rounds:
  1325.        *)
  1326.       rk:=rk+8;r:=key^.rounds-2;
  1327.       while ( r>0) do begin
  1328.   {$if defined(AES_COMPACT_IN_INNER_ROUNDS)}
  1329.     t[0] :=  Te4[(s0      ) AND $ff]       xor
  1330.       Te4[(s1 shr  8) AND $ff] shl  8 xor
  1331.       Te4[(s2 shr 16) AND $ff] shl 16 xor
  1332.       Te4[(s3 shr 24)       ] shl 24;
  1333.     t[1] :=  Te4[(s1      ) AND $ff]       xor
  1334.       Te4[(s2 shr  8) AND $ff] shl  8 xor
  1335.       Te4[(s3 shr 16) AND $ff] shl 16 xor
  1336.       Te4[(s0 shr 24)       ] shl 24;
  1337.     t[2] :=  Te4[(s2      ) AND $ff]       xor
  1338.       Te4[(s3 shr  8) AND $ff] shl  8 xor
  1339.       Te4[(s0 shr 16) AND $ff] shl 16 xor
  1340.       Te4[(s1 shr 24)       ] shl 24;
  1341.     t[3] :=  Te4[(s3      ) AND $ff]       xor
  1342.       Te4[(s0 shr  8) AND $ff] shl  8 xor
  1343.       Te4[(s1 shr 16) AND $ff] shl 16 xor
  1344.       Te4[(s2 shr 24)       ] shl 24;
  1345.  
  1346.     (* now do the linear transform using words *)
  1347.     begin  int i;
  1348.       u32 r0, r1, r2;
  1349.  
  1350.       for (i := 0; i < 4; i++) begin
  1351.         r0 := t[i];
  1352.         r1 := r0 AND $80808080;
  1353.         r2 := ((r0 AND $7f7f7f7f) shl 1) xor
  1354.           ((r1 - (r1 shr 7)) AND $1b1b1b1b);
  1355.   {$if defined(ROTATE)}
  1356.         t[i] := r2 xor ROTATE(r2,24) xor ROTATE(r0,24) xor
  1357.           ROTATE(r0,16) xor ROTATE(r0,8);
  1358.   {$else}
  1359.         t[i] := r2 xor ((r2 xor r0) shl 24) xor ((r2 xor r0) shr 8) xor
  1360.           (r0 shl 16) xor (r0 shr 16) xor
  1361.           (r0 shl 8) xor (r0 shr 24);
  1362.   {$endif}
  1363.         t[i] xor:= rk[i];
  1364.       end;
  1365.     end;
  1366.   {$else}
  1367.     t[0] :=  Te0[(s0       ) AND $ff] xor
  1368.       Te1[(s1 shr  8) AND $ff] xor
  1369.       Te2[(s2 shr 16) AND $ff] xor
  1370.       Te3[(s3 shr 24)        ] xor
  1371.       rk[0];
  1372.     t[1] :=  Te0[(s1       ) AND $ff] xor
  1373.       Te1[(s2 shr  8) AND $ff] xor
  1374.       Te2[(s3 shr 16) AND $ff] xor
  1375.       Te3[(s0 shr 24)        ] xor
  1376.       rk[1];
  1377.     t[2] :=  Te0[(s2       ) AND $ff] xor
  1378.       Te1[(s3 shr  8) AND $ff] xor
  1379.       Te2[(s0 shr 16) AND $ff] xor
  1380.       Te3[(s1 shr 24)        ] xor
  1381.       rk[2];
  1382.     t[3] :=  Te0[(s3       ) AND $ff] xor
  1383.       Te1[(s0 shr  8) AND $ff] xor
  1384.       Te2[(s1 shr 16) AND $ff] xor
  1385.       Te3[(s2 shr 24)        ] xor
  1386.       rk[3];
  1387.   {$endif}
  1388.     s0 := t[0]; s1 := t[1]; s2 := t[2]; s3 := t[3];
  1389.           rk:=rk+4;dec(r);
  1390.       end;
  1391.       (*
  1392.      * apply last round and
  1393.      * map cipher state to byte array block:
  1394.      *)
  1395.   {$if defined(AES_COMPACT_IN_OUTER_ROUNDS)}
  1396.   //  prefetch256(Te4);
  1397.  
  1398.     (pdword(out_AES_encrypt+0))^ :=
  1399.       Te4[(s0      ) AND $ff]       xor
  1400.       Te4[(s1 shr  8) AND $ff] shl  8 xor
  1401.       Te4[(s2 shr 16) AND $ff] shl 16 xor
  1402.       Te4[(s3 shr 24)       ] shl 24 xor
  1403.       rk[0];
  1404.     (pdword(out_AES_encrypt+4))^ :=
  1405.       Te4[(s1      ) AND $ff]       xor
  1406.       Te4[(s2 shr  8) AND $ff] shl  8 xor
  1407.       Te4[(s3 shr 16) AND $ff] shl 16 xor
  1408.       Te4[(s0 shr 24)       ] shl 24 xor
  1409.       rk[1];
  1410.     (pdword(out_AES_encrypt+8))^ :=
  1411.       Te4[(s2      ) AND $ff]       xor
  1412.       Te4[(s3 shr  8) AND $ff] shl  8 xor
  1413.       Te4[(s0 shr 16) AND $ff] shl 16 xor
  1414.       Te4[(s1 shr 24)       ] shl 24 xor
  1415.       rk[2];
  1416.     (pdword(out_AES_encrypt+12))^ :=
  1417.       Te4[(s3      ) AND $ff]       xor
  1418.       Te4[(s0 shr  8) AND $ff] shl  8 xor
  1419.       Te4[(s1 shr 16) AND $ff] shl 16 xor
  1420.       Te4[(s2 shr 24)       ] shl 24 xor
  1421.       rk[3];
  1422.   {$else}
  1423.     *(u32*)(out_AES_encrypt+0) :=
  1424.       (Te2[(s0      ) AND $ff] AND $000000ffU) xor
  1425.       (Te3[(s1 shr  8) AND $ff] AND $0000ff00U) xor
  1426.       (Te0[(s2 shr 16) AND $ff] AND $00ff0000U) xor
  1427.       (Te1[(s3 shr 24)       ] AND $ff000000U) xor
  1428.       rk[0];
  1429.     *(u32*)(out_AES_encrypt+4) :=
  1430.       (Te2[(s1      ) AND $ff] AND $000000ffU) xor
  1431.       (Te3[(s2 shr  8) AND $ff] AND $0000ff00U) xor
  1432.       (Te0[(s3 shr 16) AND $ff] AND $00ff0000U) xor
  1433.       (Te1[(s0 shr 24)       ] AND $ff000000U) xor
  1434.       rk[1];
  1435.     *(u32*)(out_AES_encrypt+8) :=
  1436.       (Te2[(s2      ) AND $ff] AND $000000ffU) xor
  1437.       (Te3[(s3 shr  8) AND $ff] AND $0000ff00U) xor
  1438.       (Te0[(s0 shr 16) AND $ff] AND $00ff0000U) xor
  1439.       (Te1[(s1 shr 24)       ] AND $ff000000U) xor
  1440.       rk[2];
  1441.     *(u32*)(out_AES_encrypt+12) :=
  1442.       (Te2[(s3      ) AND $ff] AND $000000ffU) xor
  1443.       (Te3[(s0 shr  8) AND $ff] AND $0000ff00U) xor
  1444.       (Te0[(s1 shr 16) AND $ff] AND $00ff0000U) xor
  1445.       (Te1[(s2 shr 24)       ] AND $ff000000U) xor
  1446.       rk[3];
  1447.   {$endif}
  1448.   end;
  1449.  
  1450.   (*
  1451.    * Decrypt a single block
  1452.    * in and out can overlap
  1453.    *)
  1454.   procedure AES_decrypt(
  1455.     const in_AES_decrypt  : pchar;
  1456.     var   out_AES_decrypt : pchar;
  1457.     const key             : pAES_KEY
  1458.   );
  1459.   var
  1460.     rk:pdword;
  1461.     s0, s1, s2, s3:dword;
  1462.     t:array[0..3]of dword;
  1463.     r:dword;
  1464.  
  1465.     i:dword;
  1466.     tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m:dword;
  1467.   begin
  1468.  
  1469.  
  1470.  
  1471.   //  assert(in and out and key);
  1472.     rk := key^.rd_key;
  1473.  
  1474.     (*
  1475.      * map byte array block to cipher state
  1476.      * and add initial round key:
  1477.      *)
  1478.     s0 := GETU32(in_AES_decrypt     ) xor rk[0];
  1479.     s1 := GETU32(in_AES_decrypt +  4) xor rk[1];
  1480.     s2 := GETU32(in_AES_decrypt +  8) xor rk[2];
  1481.     s3 := GETU32(in_AES_decrypt + 12) xor rk[3];
  1482.  
  1483.   {$if defined(AES_COMPACT_IN_OUTER_ROUNDS)}
  1484.   //  prefetch256(Td4);
  1485.  
  1486.           t[0] :=  Td4[(s0      ) AND $ff]       xor
  1487.       Td4[(s3 shr  8) AND $ff] shl  8 xor
  1488.       Td4[(s2 shr 16) AND $ff] shl 16 xor
  1489.       Td4[(s1 shr 24)       ] shl 24;
  1490.           t[1] :=  Td4[(s1      ) AND $ff]       xor
  1491.       Td4[(s0 shr  8) AND $ff] shl  8 xor
  1492.       Td4[(s3 shr 16) AND $ff] shl 16 xor
  1493.       Td4[(s2 shr 24)       ] shl 24;
  1494.           t[2] :=  Td4[(s2      ) AND $ff]       xor
  1495.       Td4[(s1 shr  8) AND $ff] shl  8 xor
  1496.       Td4[(s0 shr 16) AND $ff] shl 16 xor
  1497.       Td4[(s3 shr 24)       ] shl 24;
  1498.           t[3] :=  Td4[(s3      ) AND $ff]       xor
  1499.       Td4[(s2 shr  8) AND $ff] shl  8 xor
  1500.       Td4[(s1 shr 16) AND $ff] shl 16 xor
  1501.       Td4[(s0 shr 24)       ] shl 24;
  1502.  
  1503.     (* now do the linear transform using words *)
  1504.     begin
  1505.                   i := 0;
  1506.       while ( i < 4) do begin
  1507.         tp1 := t[i];
  1508.         m := tp1 AND $80808080;
  1509.         tp2 := ((tp1 AND $7f7f7f7f) shl 1) xor
  1510.           ((m - (m shr 7)) AND $1b1b1b1b);
  1511.         m := tp2 AND $80808080;
  1512.         tp4 := ((tp2 AND $7f7f7f7f) shl 1) xor
  1513.           ((m - (m shr 7)) AND $1b1b1b1b);
  1514.         m := tp4 AND $80808080;
  1515.         tp8 := ((tp4 AND $7f7f7f7f) shl 1) xor
  1516.           ((m - (m shr 7)) AND $1b1b1b1b);
  1517.         tp9 := tp8 xor tp1;
  1518.         tpb := tp9 xor tp2;
  1519.         tpd := tp9 xor tp4;
  1520.         tpe := tp8 xor tp4 xor tp2;
  1521.   {$if defined(ROTATE)}
  1522.         t[i] := tpe xor ROTATE(tpd,16) xor
  1523.           ROTATE(tp9,8) xor ROTATE(tpb,24);
  1524.   {$else}
  1525.         t[i] := tpe xor (tpd shr 16) xor (tpd shl 16) xor
  1526.           (tp9 shr 24) xor (tp9 shl 8) xor
  1527.           (tpb shr 8) xor (tpb shl 24);
  1528.   {$endif}
  1529.         t[i] := t[i] xor rk[4+i];
  1530.                     inc(i);
  1531.       end;
  1532.     end;
  1533.   {$else}
  1534.     t[0] :=  Td0[(s0      ) AND $ff] xor
  1535.       Td1[(s3 shr  8) AND $ff] xor
  1536.       Td2[(s2 shr 16) AND $ff] xor
  1537.       Td3[(s1 shr 24)       ] xor
  1538.       rk[4];
  1539.     t[1] :=  Td0[(s1      ) AND $ff] xor
  1540.       Td1[(s0 shr  8) AND $ff] xor
  1541.       Td2[(s3 shr 16) AND $ff] xor
  1542.       Td3[(s2 shr 24)       ] xor
  1543.       rk[5];
  1544.     t[2] :=  Td0[(s2      ) AND $ff] xor
  1545.       Td1[(s1 shr  8) AND $ff] xor
  1546.       Td2[(s0 shr 16) AND $ff] xor
  1547.       Td3[(s3 shr 24)       ] xor
  1548.       rk[6];
  1549.     t[3] :=  Td0[(s3      ) AND $ff] xor
  1550.       Td1[(s2 shr  8) AND $ff] xor
  1551.       Td2[(s1 shr 16) AND $ff] xor
  1552.       Td3[(s0 shr 24)       ] xor
  1553.       rk[7];
  1554.   {$endif}
  1555.     s0 := t[0]; s1 := t[1]; s2 := t[2]; s3 := t[3];
  1556.  
  1557.       (*
  1558.        * Nr - 2 full rounds:
  1559.        *)
  1560.       rk:=rk+8;r:=key^.rounds-2;
  1561.       while ( r>0) do begin
  1562.   {$if defined(AES_COMPACT_IN_INNER_ROUNDS)}
  1563.           t[0] :=  Td4[(s0      ) AND $ff]       xor
  1564.       Td4[(s3 shr  8) AND $ff] shl  8 xor
  1565.       Td4[(s2 shr 16) AND $ff] shl 16 xor
  1566.       Td4[(s1 shr 24)       ] shl 24;
  1567.           t[1] :=  Td4[(s1      ) AND $ff]       xor
  1568.       Td4[(s0 shr  8) AND $ff] shl  8 xor
  1569.       Td4[(s3 shr 16) AND $ff] shl 16 xor
  1570.       Td4[(s2 shr 24)       ] shl 24;
  1571.           t[2] :=  Td4[(s2      ) AND $ff]       xor
  1572.       Td4[(s1 shr  8) AND $ff] shl  8 xor
  1573.       Td4[(s0 shr 16) AND $ff] shl 16 xor
  1574.       Td4[(s3 shr 24)       ] shl 24;
  1575.           t[3] :=  Td4[(s3      ) AND $ff]       xor
  1576.       Td4[(s2 shr  8) AND $ff] shl  8 xor
  1577.       Td4[(s1 shr 16) AND $ff] shl 16 xor
  1578.       Td4[(s0 shr 24)       ] shl 24;
  1579.  
  1580.     (* now do the linear transform using words *)
  1581.     begin  int i;
  1582.       u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
  1583.  
  1584.       for (i := 0; i < 4; i++) begin
  1585.         tp1 := t[i];
  1586.         m := tp1 AND $80808080;
  1587.         tp2 := ((tp1 AND $7f7f7f7f) shl 1) xor
  1588.           ((m - (m shr 7)) AND $1b1b1b1b);
  1589.         m := tp2 AND $80808080;
  1590.         tp4 := ((tp2 AND $7f7f7f7f) shl 1) xor
  1591.           ((m - (m shr 7)) AND $1b1b1b1b);
  1592.         m := tp4 AND $80808080;
  1593.         tp8 := ((tp4 AND $7f7f7f7f) shl 1) xor
  1594.           ((m - (m shr 7)) AND $1b1b1b1b);
  1595.         tp9 := tp8 xor tp1;
  1596.         tpb := tp9 xor tp2;
  1597.         tpd := tp9 xor tp4;
  1598.         tpe := tp8 xor tp4 xor tp2;
  1599.   {$if defined(ROTATE)}
  1600.         t[i] := tpe xor ROTATE(tpd,16) xor
  1601.           ROTATE(tp9,8) xor ROTATE(tpb,24);
  1602.   {$else}
  1603.         t[i] := tpe xor (tpd shr 16) xor (tpd shl 16) xor
  1604.           (tp9 shr 24) xor (tp9 shl 8) xor
  1605.           (tpb shr 8) xor (tpb shl 24);
  1606.   {$endif}
  1607.         t[i] xor:= rk[i];
  1608.       end;
  1609.     end;
  1610.   {$else}
  1611.     t[0] :=  Td0[(s0      ) AND $ff] xor
  1612.       Td1[(s3 shr  8) AND $ff] xor
  1613.       Td2[(s2 shr 16) AND $ff] xor
  1614.       Td3[(s1 shr 24)       ] xor
  1615.       rk[0];
  1616.     t[1] :=  Td0[(s1      ) AND $ff] xor
  1617.       Td1[(s0 shr  8) AND $ff] xor
  1618.       Td2[(s3 shr 16) AND $ff] xor
  1619.       Td3[(s2 shr 24)       ] xor
  1620.       rk[1];
  1621.     t[2] :=  Td0[(s2      ) AND $ff] xor
  1622.       Td1[(s1 shr  8) AND $ff] xor
  1623.       Td2[(s0 shr 16) AND $ff] xor
  1624.       Td3[(s3 shr 24)       ] xor
  1625.       rk[2];
  1626.     t[3] :=  Td0[(s3      ) AND $ff] xor
  1627.       Td1[(s2 shr  8) AND $ff] xor
  1628.       Td2[(s1 shr 16) AND $ff] xor
  1629.       Td3[(s0 shr 24)       ] xor
  1630.       rk[3];
  1631.   {$endif}
  1632.     s0 := t[0]; s1 := t[1]; s2 := t[2]; s3 := t[3];
  1633.         rk:=rk+4;dec(r);
  1634.       end;
  1635.       (*
  1636.      * apply last round and
  1637.      * map cipher state to byte array block:
  1638.      *)
  1639.   //  prefetch256(Td4);
  1640.  
  1641.     (pdword(out_AES_decrypt+0))^ :=
  1642.       (Td4[(s0      ) AND $ff])  xor
  1643.       (Td4[(s3 shr  8) AND $ff] shl  8) xor
  1644.       (Td4[(s2 shr 16) AND $ff] shl 16) xor
  1645.       (Td4[(s1 shr 24)       ] shl 24) xor
  1646.       rk[0];
  1647.     (pdword(out_AES_decrypt+4))^ :=
  1648.       (Td4[(s1      ) AND $ff])   xor
  1649.       (Td4[(s0 shr  8) AND $ff] shl  8) xor
  1650.       (Td4[(s3 shr 16) AND $ff] shl 16) xor
  1651.       (Td4[(s2 shr 24)       ] shl 24) xor
  1652.       rk[1];
  1653.     (pdword(out_AES_decrypt+8))^ :=
  1654.       (Td4[(s2      ) AND $ff])   xor
  1655.       (Td4[(s1 shr  8) AND $ff] shl  8) xor
  1656.       (Td4[(s0 shr 16) AND $ff] shl 16) xor
  1657.       (Td4[(s3 shr 24)       ] shl 24) xor
  1658.       rk[2];
  1659.     (pdword(out_AES_decrypt+12))^ :=
  1660.       (Td4[(s3      ) AND $ff])   xor
  1661.       (Td4[(s2 shr  8) AND $ff] shl  8) xor
  1662.       (Td4[(s1 shr 16) AND $ff] shl 16) xor
  1663.       (Td4[(s0 shr 24)       ] shl 24) xor
  1664.       rk[3];
  1665.   end;
  1666.  
  1667.  
  1668.  
  1669.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1670.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1671.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1672.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1673.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1674.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1675.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1676.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1677.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1678.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1679.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1680.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1681.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1682.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1683.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1684.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1685.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1686.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1687.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1688.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1689.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
  1690.   {^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^}
Advertisement
Add Comment
Please, Sign In to add comment