1
0
mirror of https://git.code.sf.net/p/lazarus-ccr/dcpcrypt synced 2025-07-02 22:56:50 +02:00

Normalise line endings

This commit is contained in:
Graeme Geldenhuys
2014-01-17 15:22:53 +00:00
parent fa93073a71
commit 610d6570e9
50 changed files with 15367 additions and 15367 deletions

View File

@ -1,264 +1,264 @@
const
PBoxOrg: array[0..17] of DWord= (
$243f6a88, $85a308d3, $13198a2e, $03707344,
$a4093822, $299f31d0, $082efa98, $ec4e6c89,
$452821e6, $38d01377, $be5466cf, $34e90c6c,
$c0ac29b7, $c97c50dd, $3f84d5b5, $b5470917,
$9216d5d9, $8979fb1b);
SBoxOrg: array[0..3,0..255] of DWord= ((
$d1310ba6, $98dfb5ac, $2ffd72db, $d01adfb7,
$b8e1afed, $6a267e96, $ba7c9045, $f12c7f99,
$24a19947, $b3916cf7, $0801f2e2, $858efc16,
$636920d8, $71574e69, $a458fea3, $f4933d7e,
$0d95748f, $728eb658, $718bcd58, $82154aee,
$7b54a41d, $c25a59b5, $9c30d539, $2af26013,
$c5d1b023, $286085f0, $ca417918, $b8db38ef,
$8e79dcb0, $603a180e, $6c9e0e8b, $b01e8a3e,
$d71577c1, $bd314b27, $78af2fda, $55605c60,
$e65525f3, $aa55ab94, $57489862, $63e81440,
$55ca396a, $2aab10b6, $b4cc5c34, $1141e8ce,
$a15486af, $7c72e993, $b3ee1411, $636fbc2a,
$2ba9c55d, $741831f6, $ce5c3e16, $9b87931e,
$afd6ba33, $6c24cf5c, $7a325381, $28958677,
$3b8f4898, $6b4bb9af, $c4bfe81b, $66282193,
$61d809cc, $fb21a991, $487cac60, $5dec8032,
$ef845d5d, $e98575b1, $dc262302, $eb651b88,
$23893e81, $d396acc5, $0f6d6ff3, $83f44239,
$2e0b4482, $a4842004, $69c8f04a, $9e1f9b5e,
$21c66842, $f6e96c9a, $670c9c61, $abd388f0,
$6a51a0d2, $d8542f68, $960fa728, $ab5133a3,
$6eef0b6c, $137a3be4, $ba3bf050, $7efb2a98,
$a1f1651d, $39af0176, $66ca593e, $82430e88,
$8cee8619, $456f9fb4, $7d84a5c3, $3b8b5ebe,
$e06f75d8, $85c12073, $401a449f, $56c16aa6,
$4ed3aa62, $363f7706, $1bfedf72, $429b023d,
$37d0d724, $d00a1248, $db0fead3, $49f1c09b,
$075372c9, $80991b7b, $25d479d8, $f6e8def7,
$e3fe501a, $b6794c3b, $976ce0bd, $04c006ba,
$c1a94fb6, $409f60c4, $5e5c9ec2, $196a2463,
$68fb6faf, $3e6c53b5, $1339b2eb, $3b52ec6f,
$6dfc511f, $9b30952c, $cc814544, $af5ebd09,
$bee3d004, $de334afd, $660f2807, $192e4bb3,
$c0cba857, $45c8740f, $d20b5f39, $b9d3fbdb,
$5579c0bd, $1a60320a, $d6a100c6, $402c7279,
$679f25fe, $fb1fa3cc, $8ea5e9f8, $db3222f8,
$3c7516df, $fd616b15, $2f501ec8, $ad0552ab,
$323db5fa, $fd238760, $53317b48, $3e00df82,
$9e5c57bb, $ca6f8ca0, $1a87562e, $df1769db,
$d542a8f6, $287effc3, $ac6732c6, $8c4f5573,
$695b27b0, $bbca58c8, $e1ffa35d, $b8f011a0,
$10fa3d98, $fd2183b8, $4afcb56c, $2dd1d35b,
$9a53e479, $b6f84565, $d28e49bc, $4bfb9790,
$e1ddf2da, $a4cb7e33, $62fb1341, $cee4c6e8,
$ef20cada, $36774c01, $d07e9efe, $2bf11fb4,
$95dbda4d, $ae909198, $eaad8e71, $6b93d5a0,
$d08ed1d0, $afc725e0, $8e3c5b2f, $8e7594b7,
$8ff6e2fb, $f2122b64, $8888b812, $900df01c,
$4fad5ea0, $688fc31c, $d1cff191, $b3a8c1ad,
$2f2f2218, $be0e1777, $ea752dfe, $8b021fa1,
$e5a0cc0f, $b56f74e8, $18acf3d6, $ce89e299,
$b4a84fe0, $fd13e0b7, $7cc43b81, $d2ada8d9,
$165fa266, $80957705, $93cc7314, $211a1477,
$e6ad2065, $77b5fa86, $c75442f5, $fb9d35cf,
$ebcdaf0c, $7b3e89a0, $d6411bd3, $ae1e7e49,
$00250e2d, $2071b35e, $226800bb, $57b8e0af,
$2464369b, $f009b91e, $5563911d, $59dfa6aa,
$78c14389, $d95a537f, $207d5ba2, $02e5b9c5,
$83260376, $6295cfa9, $11c81968, $4e734a41,
$b3472dca, $7b14a94a, $1b510052, $9a532915,
$d60f573f, $bc9bc6e4, $2b60a476, $81e67400,
$08ba6fb5, $571be91f, $f296ec6b, $2a0dd915,
$b6636521, $e7b9f9b6, $ff34052e, $c5855664,
$53b02d5d, $a99f8fa1, $08ba4799, $6e85076a),(
$4b7a70e9, $b5b32944, $db75092e, $c4192623,
$ad6ea6b0, $49a7df7d, $9cee60b8, $8fedb266,
$ecaa8c71, $699a17ff, $5664526c, $c2b19ee1,
$193602a5, $75094c29, $a0591340, $e4183a3e,
$3f54989a, $5b429d65, $6b8fe4d6, $99f73fd6,
$a1d29c07, $efe830f5, $4d2d38e6, $f0255dc1,
$4cdd2086, $8470eb26, $6382e9c6, $021ecc5e,
$09686b3f, $3ebaefc9, $3c971814, $6b6a70a1,
$687f3584, $52a0e286, $b79c5305, $aa500737,
$3e07841c, $7fdeae5c, $8e7d44ec, $5716f2b8,
$b03ada37, $f0500c0d, $f01c1f04, $0200b3ff,
$ae0cf51a, $3cb574b2, $25837a58, $dc0921bd,
$d19113f9, $7ca92ff6, $94324773, $22f54701,
$3ae5e581, $37c2dadc, $c8b57634, $9af3dda7,
$a9446146, $0fd0030e, $ecc8c73e, $a4751e41,
$e238cd99, $3bea0e2f, $3280bba1, $183eb331,
$4e548b38, $4f6db908, $6f420d03, $f60a04bf,
$2cb81290, $24977c79, $5679b072, $bcaf89af,
$de9a771f, $d9930810, $b38bae12, $dccf3f2e,
$5512721f, $2e6b7124, $501adde6, $9f84cd87,
$7a584718, $7408da17, $bc9f9abc, $e94b7d8c,
$ec7aec3a, $db851dfa, $63094366, $c464c3d2,
$ef1c1847, $3215d908, $dd433b37, $24c2ba16,
$12a14d43, $2a65c451, $50940002, $133ae4dd,
$71dff89e, $10314e55, $81ac77d6, $5f11199b,
$043556f1, $d7a3c76b, $3c11183b, $5924a509,
$f28fe6ed, $97f1fbfa, $9ebabf2c, $1e153c6e,
$86e34570, $eae96fb1, $860e5e0a, $5a3e2ab3,
$771fe71c, $4e3d06fa, $2965dcb9, $99e71d0f,
$803e89d6, $5266c825, $2e4cc978, $9c10b36a,
$c6150eba, $94e2ea78, $a5fc3c53, $1e0a2df4,
$f2f74ea7, $361d2b3d, $1939260f, $19c27960,
$5223a708, $f71312b6, $ebadfe6e, $eac31f66,
$e3bc4595, $a67bc883, $b17f37d1, $018cff28,
$c332ddef, $be6c5aa5, $65582185, $68ab9802,
$eecea50f, $db2f953b, $2aef7dad, $5b6e2f84,
$1521b628, $29076170, $ecdd4775, $619f1510,
$13cca830, $eb61bd96, $0334fe1e, $aa0363cf,
$b5735c90, $4c70a239, $d59e9e0b, $cbaade14,
$eecc86bc, $60622ca7, $9cab5cab, $b2f3846e,
$648b1eaf, $19bdf0ca, $a02369b9, $655abb50,
$40685a32, $3c2ab4b3, $319ee9d5, $c021b8f7,
$9b540b19, $875fa099, $95f7997e, $623d7da8,
$f837889a, $97e32d77, $11ed935f, $16681281,
$0e358829, $c7e61fd6, $96dedfa1, $7858ba99,
$57f584a5, $1b227263, $9b83c3ff, $1ac24696,
$cdb30aeb, $532e3054, $8fd948e4, $6dbc3128,
$58ebf2ef, $34c6ffea, $fe28ed61, $ee7c3c73,
$5d4a14d9, $e864b7e3, $42105d14, $203e13e0,
$45eee2b6, $a3aaabea, $db6c4f15, $facb4fd0,
$c742f442, $ef6abbb5, $654f3b1d, $41cd2105,
$d81e799e, $86854dc7, $e44b476a, $3d816250,
$cf62a1f2, $5b8d2646, $fc8883a0, $c1c7b6a3,
$7f1524c3, $69cb7492, $47848a0b, $5692b285,
$095bbf00, $ad19489d, $1462b174, $23820e00,
$58428d2a, $0c55f5ea, $1dadf43e, $233f7061,
$3372f092, $8d937e41, $d65fecf1, $6c223bdb,
$7cde3759, $cbee7460, $4085f2a7, $ce77326e,
$a6078084, $19f8509e, $e8efd855, $61d99735,
$a969a7aa, $c50c06c2, $5a04abfc, $800bcadc,
$9e447a2e, $c3453484, $fdd56705, $0e1e9ec9,
$db73dbd3, $105588cd, $675fda79, $e3674340,
$c5c43465, $713e38d8, $3d28f89e, $f16dff20,
$153e21e7, $8fb03d4a, $e6e39f2b, $db83adf7),(
$e93d5a68, $948140f7, $f64c261c, $94692934,
$411520f7, $7602d4f7, $bcf46b2e, $d4a20068,
$d4082471, $3320f46a, $43b7d4b7, $500061af,
$1e39f62e, $97244546, $14214f74, $bf8b8840,
$4d95fc1d, $96b591af, $70f4ddd3, $66a02f45,
$bfbc09ec, $03bd9785, $7fac6dd0, $31cb8504,
$96eb27b3, $55fd3941, $da2547e6, $abca0a9a,
$28507825, $530429f4, $0a2c86da, $e9b66dfb,
$68dc1462, $d7486900, $680ec0a4, $27a18dee,
$4f3ffea2, $e887ad8c, $b58ce006, $7af4d6b6,
$aace1e7c, $d3375fec, $ce78a399, $406b2a42,
$20fe9e35, $d9f385b9, $ee39d7ab, $3b124e8b,
$1dc9faf7, $4b6d1856, $26a36631, $eae397b2,
$3a6efa74, $dd5b4332, $6841e7f7, $ca7820fb,
$fb0af54e, $d8feb397, $454056ac, $ba489527,
$55533a3a, $20838d87, $fe6ba9b7, $d096954b,
$55a867bc, $a1159a58, $cca92963, $99e1db33,
$a62a4a56, $3f3125f9, $5ef47e1c, $9029317c,
$fdf8e802, $04272f70, $80bb155c, $05282ce3,
$95c11548, $e4c66d22, $48c1133f, $c70f86dc,
$07f9c9ee, $41041f0f, $404779a4, $5d886e17,
$325f51eb, $d59bc0d1, $f2bcc18f, $41113564,
$257b7834, $602a9c60, $dff8e8a3, $1f636c1b,
$0e12b4c2, $02e1329e, $af664fd1, $cad18115,
$6b2395e0, $333e92e1, $3b240b62, $eebeb922,
$85b2a20e, $e6ba0d99, $de720c8c, $2da2f728,
$d0127845, $95b794fd, $647d0862, $e7ccf5f0,
$5449a36f, $877d48fa, $c39dfd27, $f33e8d1e,
$0a476341, $992eff74, $3a6f6eab, $f4f8fd37,
$a812dc60, $a1ebddf8, $991be14c, $db6e6b0d,
$c67b5510, $6d672c37, $2765d43b, $dcd0e804,
$f1290dc7, $cc00ffa3, $b5390f92, $690fed0b,
$667b9ffb, $cedb7d9c, $a091cf0b, $d9155ea3,
$bb132f88, $515bad24, $7b9479bf, $763bd6eb,
$37392eb3, $cc115979, $8026e297, $f42e312d,
$6842ada7, $c66a2b3b, $12754ccc, $782ef11c,
$6a124237, $b79251e7, $06a1bbe6, $4bfb6350,
$1a6b1018, $11caedfa, $3d25bdd8, $e2e1c3c9,
$44421659, $0a121386, $d90cec6e, $d5abea2a,
$64af674e, $da86a85f, $bebfe988, $64e4c3fe,
$9dbc8057, $f0f7c086, $60787bf8, $6003604d,
$d1fd8346, $f6381fb0, $7745ae04, $d736fccc,
$83426b33, $f01eab71, $b0804187, $3c005e5f,
$77a057be, $bde8ae24, $55464299, $bf582e61,
$4e58f48f, $f2ddfda2, $f474ef38, $8789bdc2,
$5366f9c3, $c8b38e74, $b475f255, $46fcd9b9,
$7aeb2661, $8b1ddf84, $846a0e79, $915f95e2,
$466e598e, $20b45770, $8cd55591, $c902de4c,
$b90bace1, $bb8205d0, $11a86248, $7574a99e,
$b77f19b6, $e0a9dc09, $662d09a1, $c4324633,
$e85a1f02, $09f0be8c, $4a99a025, $1d6efe10,
$1ab93d1d, $0ba5a4df, $a186f20f, $2868f169,
$dcb7da83, $573906fe, $a1e2ce9b, $4fcd7f52,
$50115e01, $a70683fa, $a002b5c4, $0de6d027,
$9af88c27, $773f8641, $c3604c06, $61a806b5,
$f0177a28, $c0f586e0, $006058aa, $30dc7d62,
$11e69ed7, $2338ea63, $53c2dd94, $c2c21634,
$bbcbee56, $90bcb6de, $ebfc7da1, $ce591d76,
$6f05e409, $4b7c0188, $39720a3d, $7c927c24,
$86e3725f, $724d9db9, $1ac15bb4, $d39eb8fc,
$ed545578, $08fca5b5, $d83d7cd3, $4dad0fc4,
$1e50ef5e, $b161e6f8, $a28514d9, $6c51133c,
$6fd5c7e7, $56e14ec4, $362abfce, $ddc6c837,
$d79a3234, $92638212, $670efa8e, $406000e0),(
$3a39ce37, $d3faf5cf, $abc27737, $5ac52d1b,
$5cb0679e, $4fa33742, $d3822740, $99bc9bbe,
$d5118e9d, $bf0f7315, $d62d1c7e, $c700c47b,
$b78c1b6b, $21a19045, $b26eb1be, $6a366eb4,
$5748ab2f, $bc946e79, $c6a376d2, $6549c2c8,
$530ff8ee, $468dde7d, $d5730a1d, $4cd04dc6,
$2939bbdb, $a9ba4650, $ac9526e8, $be5ee304,
$a1fad5f0, $6a2d519a, $63ef8ce2, $9a86ee22,
$c089c2b8, $43242ef6, $a51e03aa, $9cf2d0a4,
$83c061ba, $9be96a4d, $8fe51550, $ba645bd6,
$2826a2f9, $a73a3ae1, $4ba99586, $ef5562e9,
$c72fefd3, $f752f7da, $3f046f69, $77fa0a59,
$80e4a915, $87b08601, $9b09e6ad, $3b3ee593,
$e990fd5a, $9e34d797, $2cf0b7d9, $022b8b51,
$96d5ac3a, $017da67d, $d1cf3ed6, $7c7d2d28,
$1f9f25cf, $adf2b89b, $5ad6b472, $5a88f54c,
$e029ac71, $e019a5e6, $47b0acfd, $ed93fa9b,
$e8d3c48d, $283b57cc, $f8d56629, $79132e28,
$785f0191, $ed756055, $f7960e44, $e3d35e8c,
$15056dd4, $88f46dba, $03a16125, $0564f0bd,
$c3eb9e15, $3c9057a2, $97271aec, $a93a072a,
$1b3f6d9b, $1e6321f5, $f59c66fb, $26dcf319,
$7533d928, $b155fdf5, $03563482, $8aba3cbb,
$28517711, $c20ad9f8, $abcc5167, $ccad925f,
$4de81751, $3830dc8e, $379d5862, $9320f991,
$ea7a90c2, $fb3e7bce, $5121ce64, $774fbe32,
$a8b6e37e, $c3293d46, $48de5369, $6413e680,
$a2ae0810, $dd6db224, $69852dfd, $09072166,
$b39a460a, $6445c0dd, $586cdecf, $1c20c8ae,
$5bbef7dd, $1b588d40, $ccd2017f, $6bb4e3bb,
$dda26a7e, $3a59ff45, $3e350a44, $bcb4cdd5,
$72eacea8, $fa6484bb, $8d6612ae, $bf3c6f47,
$d29be463, $542f5d9e, $aec2771b, $f64e6370,
$740e0d8d, $e75b1357, $f8721671, $af537d5d,
$4040cb08, $4eb4e2cc, $34d2466a, $0115af84,
$e1b00428, $95983a1d, $06b89fb4, $ce6ea048,
$6f3f3b82, $3520ab82, $011a1d4b, $277227f8,
$611560b1, $e7933fdc, $bb3a792b, $344525bd,
$a08839e1, $51ce794b, $2f32c9b7, $a01fbac9,
$e01cc87e, $bcc7d1f6, $cf0111c3, $a1e8aac7,
$1a908749, $d44fbd9a, $d0dadecb, $d50ada38,
$0339c32a, $c6913667, $8df9317c, $e0b12b4f,
$f79e59b7, $43f5bb3a, $f2d519ff, $27d9459c,
$bf97222c, $15e6fc2a, $0f91fc71, $9b941525,
$fae59361, $ceb69ceb, $c2a86459, $12baa8d1,
$b6c1075e, $e3056a0c, $10d25065, $cb03a442,
$e0ec6e0e, $1698db3b, $4c98a0be, $3278e964,
$9f1f9532, $e0d392df, $d3a0342b, $8971f21e,
$1b0a7441, $4ba3348c, $c5be7120, $c37632d8,
$df359f8d, $9b992f2e, $e60b6f47, $0fe3f11d,
$e54cda54, $1edad891, $ce6279cf, $cd3e7e6f,
$1618b166, $fd2c1d05, $848fd2c5, $f6fb2299,
$f523f357, $a6327623, $93a83531, $56cccd02,
$acf08162, $5a75ebb5, $6e163697, $88d273cc,
$de966292, $81b949d0, $4c50901b, $71c65614,
$e6c6c7bd, $327a140a, $45e1d006, $c3f27b9a,
$c9aa53fd, $62a80f00, $bb25bfe2, $35bdd2f6,
$71126905, $b2040222, $b6cbcf7c, $cd769c2b,
$53113ec0, $1640e3d3, $38abbd60, $2547adf0,
$ba38209c, $f746ce76, $77afa1c5, $20756060,
$85cbfe4e, $8ae88dd8, $7aaaf9b0, $4cf9aa7e,
$1948c25c, $02fb8a8c, $01c36ae4, $d6ebe1f9,
$90d4f869, $a65cdea0, $3f09252d, $c208e69f,
$b74e6132, $ce77e25b, $578fdfe3, $3ac372e6));
const
PBoxOrg: array[0..17] of DWord= (
$243f6a88, $85a308d3, $13198a2e, $03707344,
$a4093822, $299f31d0, $082efa98, $ec4e6c89,
$452821e6, $38d01377, $be5466cf, $34e90c6c,
$c0ac29b7, $c97c50dd, $3f84d5b5, $b5470917,
$9216d5d9, $8979fb1b);
SBoxOrg: array[0..3,0..255] of DWord= ((
$d1310ba6, $98dfb5ac, $2ffd72db, $d01adfb7,
$b8e1afed, $6a267e96, $ba7c9045, $f12c7f99,
$24a19947, $b3916cf7, $0801f2e2, $858efc16,
$636920d8, $71574e69, $a458fea3, $f4933d7e,
$0d95748f, $728eb658, $718bcd58, $82154aee,
$7b54a41d, $c25a59b5, $9c30d539, $2af26013,
$c5d1b023, $286085f0, $ca417918, $b8db38ef,
$8e79dcb0, $603a180e, $6c9e0e8b, $b01e8a3e,
$d71577c1, $bd314b27, $78af2fda, $55605c60,
$e65525f3, $aa55ab94, $57489862, $63e81440,
$55ca396a, $2aab10b6, $b4cc5c34, $1141e8ce,
$a15486af, $7c72e993, $b3ee1411, $636fbc2a,
$2ba9c55d, $741831f6, $ce5c3e16, $9b87931e,
$afd6ba33, $6c24cf5c, $7a325381, $28958677,
$3b8f4898, $6b4bb9af, $c4bfe81b, $66282193,
$61d809cc, $fb21a991, $487cac60, $5dec8032,
$ef845d5d, $e98575b1, $dc262302, $eb651b88,
$23893e81, $d396acc5, $0f6d6ff3, $83f44239,
$2e0b4482, $a4842004, $69c8f04a, $9e1f9b5e,
$21c66842, $f6e96c9a, $670c9c61, $abd388f0,
$6a51a0d2, $d8542f68, $960fa728, $ab5133a3,
$6eef0b6c, $137a3be4, $ba3bf050, $7efb2a98,
$a1f1651d, $39af0176, $66ca593e, $82430e88,
$8cee8619, $456f9fb4, $7d84a5c3, $3b8b5ebe,
$e06f75d8, $85c12073, $401a449f, $56c16aa6,
$4ed3aa62, $363f7706, $1bfedf72, $429b023d,
$37d0d724, $d00a1248, $db0fead3, $49f1c09b,
$075372c9, $80991b7b, $25d479d8, $f6e8def7,
$e3fe501a, $b6794c3b, $976ce0bd, $04c006ba,
$c1a94fb6, $409f60c4, $5e5c9ec2, $196a2463,
$68fb6faf, $3e6c53b5, $1339b2eb, $3b52ec6f,
$6dfc511f, $9b30952c, $cc814544, $af5ebd09,
$bee3d004, $de334afd, $660f2807, $192e4bb3,
$c0cba857, $45c8740f, $d20b5f39, $b9d3fbdb,
$5579c0bd, $1a60320a, $d6a100c6, $402c7279,
$679f25fe, $fb1fa3cc, $8ea5e9f8, $db3222f8,
$3c7516df, $fd616b15, $2f501ec8, $ad0552ab,
$323db5fa, $fd238760, $53317b48, $3e00df82,
$9e5c57bb, $ca6f8ca0, $1a87562e, $df1769db,
$d542a8f6, $287effc3, $ac6732c6, $8c4f5573,
$695b27b0, $bbca58c8, $e1ffa35d, $b8f011a0,
$10fa3d98, $fd2183b8, $4afcb56c, $2dd1d35b,
$9a53e479, $b6f84565, $d28e49bc, $4bfb9790,
$e1ddf2da, $a4cb7e33, $62fb1341, $cee4c6e8,
$ef20cada, $36774c01, $d07e9efe, $2bf11fb4,
$95dbda4d, $ae909198, $eaad8e71, $6b93d5a0,
$d08ed1d0, $afc725e0, $8e3c5b2f, $8e7594b7,
$8ff6e2fb, $f2122b64, $8888b812, $900df01c,
$4fad5ea0, $688fc31c, $d1cff191, $b3a8c1ad,
$2f2f2218, $be0e1777, $ea752dfe, $8b021fa1,
$e5a0cc0f, $b56f74e8, $18acf3d6, $ce89e299,
$b4a84fe0, $fd13e0b7, $7cc43b81, $d2ada8d9,
$165fa266, $80957705, $93cc7314, $211a1477,
$e6ad2065, $77b5fa86, $c75442f5, $fb9d35cf,
$ebcdaf0c, $7b3e89a0, $d6411bd3, $ae1e7e49,
$00250e2d, $2071b35e, $226800bb, $57b8e0af,
$2464369b, $f009b91e, $5563911d, $59dfa6aa,
$78c14389, $d95a537f, $207d5ba2, $02e5b9c5,
$83260376, $6295cfa9, $11c81968, $4e734a41,
$b3472dca, $7b14a94a, $1b510052, $9a532915,
$d60f573f, $bc9bc6e4, $2b60a476, $81e67400,
$08ba6fb5, $571be91f, $f296ec6b, $2a0dd915,
$b6636521, $e7b9f9b6, $ff34052e, $c5855664,
$53b02d5d, $a99f8fa1, $08ba4799, $6e85076a),(
$4b7a70e9, $b5b32944, $db75092e, $c4192623,
$ad6ea6b0, $49a7df7d, $9cee60b8, $8fedb266,
$ecaa8c71, $699a17ff, $5664526c, $c2b19ee1,
$193602a5, $75094c29, $a0591340, $e4183a3e,
$3f54989a, $5b429d65, $6b8fe4d6, $99f73fd6,
$a1d29c07, $efe830f5, $4d2d38e6, $f0255dc1,
$4cdd2086, $8470eb26, $6382e9c6, $021ecc5e,
$09686b3f, $3ebaefc9, $3c971814, $6b6a70a1,
$687f3584, $52a0e286, $b79c5305, $aa500737,
$3e07841c, $7fdeae5c, $8e7d44ec, $5716f2b8,
$b03ada37, $f0500c0d, $f01c1f04, $0200b3ff,
$ae0cf51a, $3cb574b2, $25837a58, $dc0921bd,
$d19113f9, $7ca92ff6, $94324773, $22f54701,
$3ae5e581, $37c2dadc, $c8b57634, $9af3dda7,
$a9446146, $0fd0030e, $ecc8c73e, $a4751e41,
$e238cd99, $3bea0e2f, $3280bba1, $183eb331,
$4e548b38, $4f6db908, $6f420d03, $f60a04bf,
$2cb81290, $24977c79, $5679b072, $bcaf89af,
$de9a771f, $d9930810, $b38bae12, $dccf3f2e,
$5512721f, $2e6b7124, $501adde6, $9f84cd87,
$7a584718, $7408da17, $bc9f9abc, $e94b7d8c,
$ec7aec3a, $db851dfa, $63094366, $c464c3d2,
$ef1c1847, $3215d908, $dd433b37, $24c2ba16,
$12a14d43, $2a65c451, $50940002, $133ae4dd,
$71dff89e, $10314e55, $81ac77d6, $5f11199b,
$043556f1, $d7a3c76b, $3c11183b, $5924a509,
$f28fe6ed, $97f1fbfa, $9ebabf2c, $1e153c6e,
$86e34570, $eae96fb1, $860e5e0a, $5a3e2ab3,
$771fe71c, $4e3d06fa, $2965dcb9, $99e71d0f,
$803e89d6, $5266c825, $2e4cc978, $9c10b36a,
$c6150eba, $94e2ea78, $a5fc3c53, $1e0a2df4,
$f2f74ea7, $361d2b3d, $1939260f, $19c27960,
$5223a708, $f71312b6, $ebadfe6e, $eac31f66,
$e3bc4595, $a67bc883, $b17f37d1, $018cff28,
$c332ddef, $be6c5aa5, $65582185, $68ab9802,
$eecea50f, $db2f953b, $2aef7dad, $5b6e2f84,
$1521b628, $29076170, $ecdd4775, $619f1510,
$13cca830, $eb61bd96, $0334fe1e, $aa0363cf,
$b5735c90, $4c70a239, $d59e9e0b, $cbaade14,
$eecc86bc, $60622ca7, $9cab5cab, $b2f3846e,
$648b1eaf, $19bdf0ca, $a02369b9, $655abb50,
$40685a32, $3c2ab4b3, $319ee9d5, $c021b8f7,
$9b540b19, $875fa099, $95f7997e, $623d7da8,
$f837889a, $97e32d77, $11ed935f, $16681281,
$0e358829, $c7e61fd6, $96dedfa1, $7858ba99,
$57f584a5, $1b227263, $9b83c3ff, $1ac24696,
$cdb30aeb, $532e3054, $8fd948e4, $6dbc3128,
$58ebf2ef, $34c6ffea, $fe28ed61, $ee7c3c73,
$5d4a14d9, $e864b7e3, $42105d14, $203e13e0,
$45eee2b6, $a3aaabea, $db6c4f15, $facb4fd0,
$c742f442, $ef6abbb5, $654f3b1d, $41cd2105,
$d81e799e, $86854dc7, $e44b476a, $3d816250,
$cf62a1f2, $5b8d2646, $fc8883a0, $c1c7b6a3,
$7f1524c3, $69cb7492, $47848a0b, $5692b285,
$095bbf00, $ad19489d, $1462b174, $23820e00,
$58428d2a, $0c55f5ea, $1dadf43e, $233f7061,
$3372f092, $8d937e41, $d65fecf1, $6c223bdb,
$7cde3759, $cbee7460, $4085f2a7, $ce77326e,
$a6078084, $19f8509e, $e8efd855, $61d99735,
$a969a7aa, $c50c06c2, $5a04abfc, $800bcadc,
$9e447a2e, $c3453484, $fdd56705, $0e1e9ec9,
$db73dbd3, $105588cd, $675fda79, $e3674340,
$c5c43465, $713e38d8, $3d28f89e, $f16dff20,
$153e21e7, $8fb03d4a, $e6e39f2b, $db83adf7),(
$e93d5a68, $948140f7, $f64c261c, $94692934,
$411520f7, $7602d4f7, $bcf46b2e, $d4a20068,
$d4082471, $3320f46a, $43b7d4b7, $500061af,
$1e39f62e, $97244546, $14214f74, $bf8b8840,
$4d95fc1d, $96b591af, $70f4ddd3, $66a02f45,
$bfbc09ec, $03bd9785, $7fac6dd0, $31cb8504,
$96eb27b3, $55fd3941, $da2547e6, $abca0a9a,
$28507825, $530429f4, $0a2c86da, $e9b66dfb,
$68dc1462, $d7486900, $680ec0a4, $27a18dee,
$4f3ffea2, $e887ad8c, $b58ce006, $7af4d6b6,
$aace1e7c, $d3375fec, $ce78a399, $406b2a42,
$20fe9e35, $d9f385b9, $ee39d7ab, $3b124e8b,
$1dc9faf7, $4b6d1856, $26a36631, $eae397b2,
$3a6efa74, $dd5b4332, $6841e7f7, $ca7820fb,
$fb0af54e, $d8feb397, $454056ac, $ba489527,
$55533a3a, $20838d87, $fe6ba9b7, $d096954b,
$55a867bc, $a1159a58, $cca92963, $99e1db33,
$a62a4a56, $3f3125f9, $5ef47e1c, $9029317c,
$fdf8e802, $04272f70, $80bb155c, $05282ce3,
$95c11548, $e4c66d22, $48c1133f, $c70f86dc,
$07f9c9ee, $41041f0f, $404779a4, $5d886e17,
$325f51eb, $d59bc0d1, $f2bcc18f, $41113564,
$257b7834, $602a9c60, $dff8e8a3, $1f636c1b,
$0e12b4c2, $02e1329e, $af664fd1, $cad18115,
$6b2395e0, $333e92e1, $3b240b62, $eebeb922,
$85b2a20e, $e6ba0d99, $de720c8c, $2da2f728,
$d0127845, $95b794fd, $647d0862, $e7ccf5f0,
$5449a36f, $877d48fa, $c39dfd27, $f33e8d1e,
$0a476341, $992eff74, $3a6f6eab, $f4f8fd37,
$a812dc60, $a1ebddf8, $991be14c, $db6e6b0d,
$c67b5510, $6d672c37, $2765d43b, $dcd0e804,
$f1290dc7, $cc00ffa3, $b5390f92, $690fed0b,
$667b9ffb, $cedb7d9c, $a091cf0b, $d9155ea3,
$bb132f88, $515bad24, $7b9479bf, $763bd6eb,
$37392eb3, $cc115979, $8026e297, $f42e312d,
$6842ada7, $c66a2b3b, $12754ccc, $782ef11c,
$6a124237, $b79251e7, $06a1bbe6, $4bfb6350,
$1a6b1018, $11caedfa, $3d25bdd8, $e2e1c3c9,
$44421659, $0a121386, $d90cec6e, $d5abea2a,
$64af674e, $da86a85f, $bebfe988, $64e4c3fe,
$9dbc8057, $f0f7c086, $60787bf8, $6003604d,
$d1fd8346, $f6381fb0, $7745ae04, $d736fccc,
$83426b33, $f01eab71, $b0804187, $3c005e5f,
$77a057be, $bde8ae24, $55464299, $bf582e61,
$4e58f48f, $f2ddfda2, $f474ef38, $8789bdc2,
$5366f9c3, $c8b38e74, $b475f255, $46fcd9b9,
$7aeb2661, $8b1ddf84, $846a0e79, $915f95e2,
$466e598e, $20b45770, $8cd55591, $c902de4c,
$b90bace1, $bb8205d0, $11a86248, $7574a99e,
$b77f19b6, $e0a9dc09, $662d09a1, $c4324633,
$e85a1f02, $09f0be8c, $4a99a025, $1d6efe10,
$1ab93d1d, $0ba5a4df, $a186f20f, $2868f169,
$dcb7da83, $573906fe, $a1e2ce9b, $4fcd7f52,
$50115e01, $a70683fa, $a002b5c4, $0de6d027,
$9af88c27, $773f8641, $c3604c06, $61a806b5,
$f0177a28, $c0f586e0, $006058aa, $30dc7d62,
$11e69ed7, $2338ea63, $53c2dd94, $c2c21634,
$bbcbee56, $90bcb6de, $ebfc7da1, $ce591d76,
$6f05e409, $4b7c0188, $39720a3d, $7c927c24,
$86e3725f, $724d9db9, $1ac15bb4, $d39eb8fc,
$ed545578, $08fca5b5, $d83d7cd3, $4dad0fc4,
$1e50ef5e, $b161e6f8, $a28514d9, $6c51133c,
$6fd5c7e7, $56e14ec4, $362abfce, $ddc6c837,
$d79a3234, $92638212, $670efa8e, $406000e0),(
$3a39ce37, $d3faf5cf, $abc27737, $5ac52d1b,
$5cb0679e, $4fa33742, $d3822740, $99bc9bbe,
$d5118e9d, $bf0f7315, $d62d1c7e, $c700c47b,
$b78c1b6b, $21a19045, $b26eb1be, $6a366eb4,
$5748ab2f, $bc946e79, $c6a376d2, $6549c2c8,
$530ff8ee, $468dde7d, $d5730a1d, $4cd04dc6,
$2939bbdb, $a9ba4650, $ac9526e8, $be5ee304,
$a1fad5f0, $6a2d519a, $63ef8ce2, $9a86ee22,
$c089c2b8, $43242ef6, $a51e03aa, $9cf2d0a4,
$83c061ba, $9be96a4d, $8fe51550, $ba645bd6,
$2826a2f9, $a73a3ae1, $4ba99586, $ef5562e9,
$c72fefd3, $f752f7da, $3f046f69, $77fa0a59,
$80e4a915, $87b08601, $9b09e6ad, $3b3ee593,
$e990fd5a, $9e34d797, $2cf0b7d9, $022b8b51,
$96d5ac3a, $017da67d, $d1cf3ed6, $7c7d2d28,
$1f9f25cf, $adf2b89b, $5ad6b472, $5a88f54c,
$e029ac71, $e019a5e6, $47b0acfd, $ed93fa9b,
$e8d3c48d, $283b57cc, $f8d56629, $79132e28,
$785f0191, $ed756055, $f7960e44, $e3d35e8c,
$15056dd4, $88f46dba, $03a16125, $0564f0bd,
$c3eb9e15, $3c9057a2, $97271aec, $a93a072a,
$1b3f6d9b, $1e6321f5, $f59c66fb, $26dcf319,
$7533d928, $b155fdf5, $03563482, $8aba3cbb,
$28517711, $c20ad9f8, $abcc5167, $ccad925f,
$4de81751, $3830dc8e, $379d5862, $9320f991,
$ea7a90c2, $fb3e7bce, $5121ce64, $774fbe32,
$a8b6e37e, $c3293d46, $48de5369, $6413e680,
$a2ae0810, $dd6db224, $69852dfd, $09072166,
$b39a460a, $6445c0dd, $586cdecf, $1c20c8ae,
$5bbef7dd, $1b588d40, $ccd2017f, $6bb4e3bb,
$dda26a7e, $3a59ff45, $3e350a44, $bcb4cdd5,
$72eacea8, $fa6484bb, $8d6612ae, $bf3c6f47,
$d29be463, $542f5d9e, $aec2771b, $f64e6370,
$740e0d8d, $e75b1357, $f8721671, $af537d5d,
$4040cb08, $4eb4e2cc, $34d2466a, $0115af84,
$e1b00428, $95983a1d, $06b89fb4, $ce6ea048,
$6f3f3b82, $3520ab82, $011a1d4b, $277227f8,
$611560b1, $e7933fdc, $bb3a792b, $344525bd,
$a08839e1, $51ce794b, $2f32c9b7, $a01fbac9,
$e01cc87e, $bcc7d1f6, $cf0111c3, $a1e8aac7,
$1a908749, $d44fbd9a, $d0dadecb, $d50ada38,
$0339c32a, $c6913667, $8df9317c, $e0b12b4f,
$f79e59b7, $43f5bb3a, $f2d519ff, $27d9459c,
$bf97222c, $15e6fc2a, $0f91fc71, $9b941525,
$fae59361, $ceb69ceb, $c2a86459, $12baa8d1,
$b6c1075e, $e3056a0c, $10d25065, $cb03a442,
$e0ec6e0e, $1698db3b, $4c98a0be, $3278e964,
$9f1f9532, $e0d392df, $d3a0342b, $8971f21e,
$1b0a7441, $4ba3348c, $c5be7120, $c37632d8,
$df359f8d, $9b992f2e, $e60b6f47, $0fe3f11d,
$e54cda54, $1edad891, $ce6279cf, $cd3e7e6f,
$1618b166, $fd2c1d05, $848fd2c5, $f6fb2299,
$f523f357, $a6327623, $93a83531, $56cccd02,
$acf08162, $5a75ebb5, $6e163697, $88d273cc,
$de966292, $81b949d0, $4c50901b, $71c65614,
$e6c6c7bd, $327a140a, $45e1d006, $c3f27b9a,
$c9aa53fd, $62a80f00, $bb25bfe2, $35bdd2f6,
$71126905, $b2040222, $b6cbcf7c, $cd769c2b,
$53113ec0, $1640e3d3, $38abbd60, $2547adf0,
$ba38209c, $f746ce76, $77afa1c5, $20756060,
$85cbfe4e, $8ae88dd8, $7aaaf9b0, $4cf9aa7e,
$1948c25c, $02fb8a8c, $01c36ae4, $d6ebe1f9,
$90d4f869, $a65cdea0, $3f09252d, $c208e69f,
$b74e6132, $ce77e25b, $578fdfe3, $3ac372e6));

File diff suppressed because it is too large Load Diff

View File

@ -1,178 +1,178 @@
const
S1: array[0..255] of DWord= (
$30fb40d4, $9fa0ff0b, $6beccd2f, $3f258c7a, $1e213f2f, $9C004dd3,
$6003e540, $cf9fc949, $bfd4af27, $88bbbdb5, $e2034090, $98d09675,
$6e63a0e0, $15c361d2, $c2e7661d, $22d4ff8e, $28683b6f, $c07fd059,
$ff2379c8, $775f50e2, $43c340d3, $df2f8656, $887ca41a, $a2d2bd2d,
$a1c9e0d6, $346c4819, $61b76d87, $22540f2f, $2abe32e1, $aa54166b,
$22568e3a, $a2d341d0, $66db40c8, $a784392f, $004dff2f, $2db9d2de,
$97943fac, $4a97c1d8, $527644b7, $b5f437a7, $b82cbaef, $d751d159,
$6ff7f0ed, $5a097a1f, $827b68d0, $90ecf52e, $22b0c054, $bc8e5935,
$4b6d2f7f, $50bb64a2, $d2664910, $bee5812d, $b7332290, $e93b159f,
$b48ee411, $4bff345d, $fd45c240, $ad31973f, $c4f6d02e, $55fc8165,
$d5b1caad, $a1ac2dae, $a2d4b76d, $c19b0C50, $882240f2, $0c6e4f38,
$a4e4bfd7, $4f5ba272, $564c1d2f, $c59c5319, $b949e354, $b04669fe,
$b1b6ab8a, $c71358dd, $6385c545, $110f935d, $57538ad5, $6a390493,
$e63d37e0, $2a54f6b3, $3a787d5f, $6276a0b5, $19a6fcdf, $7a42206a,
$29f9d4d5, $f61b1891, $bb72275e, $aa508167, $38901091, $c6b505eb,
$84c7cb8c, $2ad75a0f, $874a1427, $a2d1936b, $2ad286af, $aa56d291,
$d7894360, $425c750d, $93b39e26, $187184c9, $6c00b32d, $73e2bb14,
$a0bebc3c, $54623779, $64459eab, $3f328b82, $7718cf82, $59a2cea6,
$04ee002e, $89fe78e6, $3fab0950, $325ff6C2, $81383f05, $6963c5c8,
$76cb5ad6, $d49974c9, $ca180dcf, $380782d5, $c7fa5cf6, $8ac31511,
$35e79e13, $47da91d0, $f40f9086, $a7e2419e, $31366241, $051ef495,
$aa573b04, $4a805d8d, $548300d0, $00322a3c, $bf64cddf, $ba57a68e,
$75c6372b, $50afd341, $a7c13275, $915a0bf5, $6b54bfab, $2b0b1426,
$ab4cc9d7, $449ccd82, $f7fbf265, $ab85c5f3, $1b55db94, $aad4e324,
$cfa4bd3f, $2deaa3e2, $9e204d02, $c8bd25ac, $eadf55b3, $d5bd9e98,
$e31231b2, $2ad5ad6c, $954329de, $adbe4528, $d8710f69, $aa51c90f,
$aa786bf6, $22513f1e, $aa51a79b, $2ad344cc, $7b5a41f0, $d37cfbad,
$1b069505, $41ece491, $b4c332e6, $032268d4, $c9600acc, $ce387e6d,
$bf6bb16c, $6a70fb78, $0d03d9c9, $d4df39de, $e01063da, $4736f464,
$5ad328d8, $b347cc96, $75bb0fc3, $98511bfb, $4ffbcc35, $b58bcf6a,
$e11f0abc, $bfc5fe4a, $a70aec10, $ac39570a, $3f04442f, $6188b153,
$e0397a2e, $5727cb79, $9ceb418f, $1cacd68d, $2ad37c96, $0175cb9d,
$c69dff09, $c75b65f0, $d9db40d8, $ec0e7779, $4744ead4, $b11c3274,
$dd24cb9e, $7e1c54bd, $f01144f9, $d2240eb1, $9675b3fd, $a3ac3755,
$d47c27af, $51c85f4d, $56907596, $a5bb15e6, $580304f0, $ca042cf1,
$011a37ea, $8dbfaadb, $35ba3e4a, $3526ffa0, $c37b4d09, $bc306ed9,
$98a52666, $5648f725, $ff5e569d, $0ced63d0, $7c63b2cf, $700b45e1,
$d5ea50f1, $85a92872, $af1fbda7, $d4234870, $a7870bf3, $2d3b4d79,
$42e04198, $0cd0ede7, $26470db8, $f881814C, $474d6ad7, $7c0c5e5c,
$d1231959, $381b7298, $f5d2f4db, $ab838653, $6e2f1e23, $83719c9e,
$bd91e046, $9a56456e, $dc39200c, $20c8c571, $962bda1c, $e1e696ff,
$b141ab08, $7cca89b9, $1a69e783, $02cc4843, $a2f7c579, $429ef47d,
$427b169c, $5ac9f049, $dd8f0f00, $5c8165bf);
S2: array[0..255] of DWord= (
$1f201094, $ef0ba75b, $69e3cf7e, $393f4380, $fe61cf7a, $eec5207a,
$55889c94, $72fc0651, $ada7ef79, $4e1d7235, $d55a63ce, $de0436ba,
$99c430ef, $5f0c0794, $18dcdb7d, $a1d6eff3, $a0b52f7b, $59e83605,
$ee15b094, $e9ffd909, $dc440086, $ef944459, $ba83ccb3, $e0c3cdfb,
$d1da4181, $3b092ab1, $f997f1c1, $a5e6cf7b, $01420ddb, $e4e7ef5b,
$25a1ff41, $e180f806, $1fc41080, $179bee7a, $d37ac6a9, $fe5830a4,
$98de8b7f, $77e83f4e, $79929269, $24fa9f7b, $e113c85b, $acc40083,
$d7503525, $f7ea615f, $62143154, $0d554b63, $5d681121, $c866c359,
$3d63cf73, $cee234c0, $d4d87e87, $5c672b21, $071f6181, $39f7627f,
$361e3084, $e4eb573b, $602f64a4, $d63acd9c, $1bbc4635, $9e81032d,
$2701f50c, $99847ab4, $a0e3df79, $ba6cf38c, $10843094, $2537a95e,
$f46f6ffe, $a1ff3b1f, $208cfb6a, $8f458c74, $d9e0a227, $4ec73a34,
$fc884f69, $3e4de8df, $ef0e0088, $3559648d, $8a45388c, $1d804366,
$721d9bfd, $a58684bb, $e8256333, $844e8212, $128d8098, $fed33fb4,
$ce280ae1, $27e19ba5, $d5a6c252, $e49754bd, $c5d655dd, $eb667064,
$77840b4d, $a1b6a801, $84db26a9, $e0b56714, $21f043b7, $e5d05860,
$54f03084, $066ff472, $a31aa153, $dadc4755, $b5625dbf, $68561be6,
$83ca6b94, $2d6ed23b, $eccf01db, $a6d3d0ba, $b6803d5c, $af77a709,
$33b4a34c, $397bc8d6, $5ee22b95, $5f0e5304, $81ed6f61, $20e74364,
$b45e1378, $de18639b, $881ca122, $b96726d1, $8049a7e8, $22b7da7b,
$5e552d25, $5272d237, $79d2951c, $c60d894c, $488cb402, $1ba4fe5b,
$a4b09f6b, $1ca815cf, $a20c3005, $8871df63, $b9de2fcb, $0cc6c9e9,
$0beeff53, $e3214517, $b4542835, $9f63293c, $ee41e729, $6e1d2d7c,
$50045286, $1e6685f3, $f33401c6, $30a22c95, $31a70850, $60930f13,
$73f98417, $a1269859, $ec645c44, $52c877a9, $cdff33a6, $a02b1741,
$7cbad9a2, $2180036f, $50d99c08, $cb3f4861, $c26bd765, $64a3f6ab,
$80342676, $25a75e7b, $e4e6d1fc, $20c710e6, $cdf0b680, $17844d3b,
$31eef84d, $7e0824e4, $2ccb49eb, $846a3bae, $8ff77888, $ee5d60f6,
$7af75673, $2fdd5cdb, $a11631c1, $30f66f43, $b3faec54, $157fd7fa,
$ef8579cc, $d152de58, $db2ffd5e, $8f32ce19, $306af97a, $02f03ef8,
$99319ad5, $c242fa0f, $a7e3ebb0, $c68e4906, $b8da230c, $80823028,
$dcdef3c8, $d35fb171, $088a1bc8, $bec0c560, $61a3c9e8, $bca8f54d,
$c72feffa, $22822e99, $82c570b4, $d8d94e89, $8b1c34bc, $301e16e6,
$273be979, $b0ffeaa6, $61d9b8c6, $00b24869, $b7ffce3f, $08dc283b,
$43daf65a, $f7e19798, $7619b72f, $8f1c9ba4, $dc8637a0, $16a7d3b1,
$9fc393b7, $a7136eeb, $c6bcc63e, $1a513742, $ef6828bc, $520365d6,
$2d6a77ab, $3527ed4b, $821fd216, $095c6e2e, $db92f2fb, $5eea29cb,
$145892f5, $91584f7f, $5483697b, $2667a8cc, $85196048, $8c4bacea,
$833860d4, $0d23e0f9, $6c387e8a, $0ae6d249, $b284600c, $d835731d,
$dcb1c647, $ac4c56ea, $3ebd81b3, $230eabb0, $6438bc87, $f0b5b1fa,
$8f5ea2b3, $fc184642, $0a036b7a, $4fb089bd, $649da589, $a345415e,
$5c038323, $3e5d3bb9, $43d79572, $7e6dd07c, $06dfdf1e, $6c6cc4ef,
$7160a539, $73bfbe70, $83877605, $4523ecf1);
S3: array[0..255] of DWord= (
$8defc240, $25fa5d9f, $eb903dbf, $e810c907, $47607fff, $369fe44b,
$8c1fc644, $aececa90, $beb1f9bf, $eefbcaea, $e8cf1950, $51df07ae,
$920e8806, $f0ad0548, $e13c8d83, $927010d5, $11107d9f, $07647db9,
$b2e3e4d4, $3d4f285e, $b9afa820, $fade82e0, $a067268b, $8272792e,
$553fb2c0, $489ae22b, $d4ef9794, $125e3fbc, $21fffcee, $825b1bfd,
$9255c5ed, $1257a240, $4e1a8302, $bae07fff, $528246e7, $8e57140e,
$3373f7bf, $8c9f8188, $a6fc4ee8, $c982b5a5, $a8c01db7, $579fc264,
$67094f31, $f2bd3f5f, $40fff7c1, $1fb78dfc, $8e6bd2c1, $437be59b,
$99b03dbf, $b5dbc64b, $638dc0e6, $55819d99, $a197c81c, $4a012d6e,
$c5884a28, $ccc36f71, $b843c213, $6c0743f1, $8309893c, $0feddd5f,
$2f7fe850, $d7c07f7e, $02507fbf, $5afb9a04, $a747d2d0, $1651192e,
$af70bf3e, $58c31380, $5f98302e, $727cc3c4, $0a0fb402, $0f7fef82,
$8c96fdad, $5d2c2aae, $8ee99a49, $50da88b8, $8427f4a0, $1eac5790,
$796fb449, $8252dc15, $efbd7d9b, $a672597d, $ada840d8, $45f54504,
$fa5d7403, $e83ec305, $4f91751a, $925669c2, $23efe941, $a903f12e,
$60270df2, $0276e4b6, $94fd6574, $927985b2, $8276dbcb, $02778176,
$f8af918d, $4e48f79e, $8f616ddf, $e29d840e, $842f7d83, $340ce5c8,
$96bbb682, $93b4b148, $ef303cab, $984faf28, $779faf9b, $92dc560d,
$224d1e20, $8437aa88, $7d29dc96, $2756d3dc, $8b907cee, $b51fd240,
$e7c07ce3, $e566b4a1, $c3e9615e, $3cf8209d, $6094d1e3, $cd9ca341,
$5c76460e, $00ea983b, $d4d67881, $fd47572c, $f76cedd9, $bda8229c,
$127dadaa, $438a074e, $1f97c090, $081bdb8a, $93a07ebe, $b938ca15,
$97b03cff, $3dc2c0f8, $8d1ab2ec, $64380e51, $68cc7bfb, $d90f2788,
$12490181, $5de5ffd4, $dd7ef86a, $76a2e214, $b9a40368, $925d958f,
$4b39fffa, $ba39aee9, $a4ffd30b, $faf7933b, $6d498623, $193cbcfa,
$27627545, $825cf47a, $61bd8ba0, $d11e42d1, $cead04f4, $127ea392,
$10428db7, $8272a972, $9270c4a8, $127de50b, $285ba1c8, $3c62f44f,
$35c0eaa5, $e805d231, $428929fb, $b4fcdf82, $4fb66a53, $0e7dc15b,
$1f081fab, $108618ae, $fcfd086d, $f9ff2889, $694bcc11, $236a5cae,
$12deca4d, $2c3f8cc5, $d2d02dfe, $f8ef5896, $e4cf52da, $95155b67,
$494a488c, $b9b6a80c, $5c8f82bc, $89d36b45, $3a609437, $ec00c9a9,
$44715253, $0a874b49, $d773bc40, $7c34671c, $02717ef6, $4feb5536,
$a2d02fff, $d2bf60c4, $d43f03c0, $50b4ef6d, $07478cd1, $006e1888,
$a2e53f55, $b9e6d4bc, $a2048016, $97573833, $d7207d67, $de0f8f3d,
$72f87b33, $abcc4f33, $7688c55d, $7b00a6b0, $947b0001, $570075d2,
$f9bb88f8, $8942019e, $4264a5ff, $856302e0, $72dbd92b, $ee971b69,
$6ea22fde, $5f08ae2b, $af7a616d, $e5c98767, $cf1febd2, $61efc8c2,
$f1ac2571, $cc8239c2, $67214cb8, $b1e583d1, $b7dc3e62, $7f10bdce,
$f90a5c38, $0ff0443d, $606e6dc6, $60543a49, $5727c148, $2be98a1d,
$8ab41738, $20e1be24, $af96da0f, $68458425, $99833be5, $600d457d,
$282f9350, $8334b362, $d91d1120, $2b6d8da0, $642b1e31, $9c305a00,
$52bce688, $1b03588a, $f7baefd5, $4142ed9c, $a4315c11, $83323ec5,
$dfef4636, $a133c501, $e9d3531c, $ee353783);
S4: array[0..255] of DWord= (
$9db30420, $1fb6e9de, $a7be7bef, $d273a298, $4a4f7bdb, $64ad8c57,
$85510443, $fa020ed1, $7e287aff, $e60fb663, $095f35a1, $79ebf120,
$fd059d43, $6497b7b1, $f3641f63, $241e4adf, $28147f5f, $4fa2b8cd,
$c9430040, $0cc32220, $fdd30b30, $c0a5374f, $1d2d00d9, $24147b15,
$ee4d111a, $0fca5167, $71ff904c, $2d195ffe, $1a05645f, $0c13fefe,
$081b08ca, $05170121, $80530100, $e83e5efe, $ac9af4f8, $7fe72701,
$d2b8ee5f, $06df4261, $bb9e9b8a, $7293ea25, $ce84ffdf, $f5718801,
$3dd64b04, $a26f263b, $7ed48400, $547eebe6, $446d4ca0, $6cf3d6f5,
$2649abdf, $aea0c7f5, $36338cc1, $503f7e93, $d3772061, $11b638e1,
$72500e03, $f80eb2bb, $abe0502e, $ec8d77de, $57971e81, $e14f6746,
$c9335400, $6920318f, $081dbb99, $ffc304a5, $4d351805, $7f3d5ce3,
$a6c866c6, $5d5bcca9, $daec6fea, $9f926f91, $9f46222f, $3991467d,
$a5bf6d8e, $1143c44f, $43958302, $d0214eeb, $022083b8, $3fb6180c,
$18f8931e, $281658e6, $26486e3e, $8bd78a70, $7477e4c1, $b506e07c,
$f32d0a25, $79098b02, $e4eabb81, $28123b23, $69dead38, $1574ca16,
$df871b62, $211c40b7, $a51a9ef9, $0014377b, $041e8ac8, $09114003,
$bd59e4d2, $e3d156d5, $4fe876d5, $2f91a340, $557be8de, $00eae4a7,
$0ce5c2ec, $4db4bba6, $e756bdff, $dd3369ac, $ec17b035, $06572327,
$99afc8b0, $56c8c391, $6b65811c, $5e146119, $6e85cb75, $be07c002,
$c2325577, $893ff4ec, $5bbfc92d, $d0ec3b25, $b7801ab7, $8d6d3b24,
$20c763ef, $c366a5fc, $9c382880, $0ace3205, $aac9548a, $eca1d7c7,
$041afa32, $1d16625a, $6701902c, $9b757a54, $31d477f7, $9126b031,
$36cc6fdb, $c70b8b46, $d9e66a48, $56e55a79, $026a4ceb, $52437eff,
$2f8f76b4, $0df980a5, $8674cde3, $edda04eb, $17a9be04, $2c18f4df,
$b7747f9d, $ab2af7b4, $efc34d20, $2e096b7c, $1741a254, $e5b6a035,
$213d42f6, $2c1c7c26, $61c2f50f, $6552daf9, $d2c231f8, $25130f69,
$d8167fa2, $0418f2c8, $001a96a6, $0d1526ab, $63315c21, $5e0a72ec,
$49bafefd, $187908d9, $8d0dbd86, $311170a7, $3e9b640c, $cc3e10d7,
$d5cad3b6, $0caec388, $f73001e1, $6c728aff, $71eae2a1, $1f9af36e,
$cfcbd12f, $c1de8417, $ac07be6b, $cb44a1d8, $8b9b0f56, $013988c3,
$b1c52fca, $b4be31cd, $d8782806, $12a3a4e2, $6f7de532, $58fd7eb6,
$d01ee900, $24adffc2, $f4990fc5, $9711aac5, $001d7b95, $82e5e7d2,
$109873f6, $00613096, $c32d9521, $ada121ff, $29908415, $7fbb977f,
$af9eb3db, $29c9ed2a, $5ce2a465, $a730f32c, $d0aa3fe8, $8a5cc091,
$d49e2ce7, $0ce454a9, $d60acd86, $015f1919, $77079103, $dea03af6,
$78a8565e, $dee356df, $21f05cbe, $8b75e387, $b3c50651, $b8a5c3ef,
$d8eeb6d2, $e523be77, $c2154529, $2f69efdf, $afe67afb, $f470c4b2,
$f3e0eb5b, $d6cc9876, $39e4460c, $1fda8538, $1987832f, $ca007367,
$a99144f8, $296b299e, $492fc295, $9266beab, $b5676e69, $9bd3ddda,
$df7e052f, $db25701c, $1b5e51ee, $f65324e6, $6afce36c, $0316cc04,
$8644213e, $b7dc59d0, $7965291f, $ccd6fd43, $41823979, $932bcdf6,
$b657c34d, $4edfd282, $7ae5290c, $3cb9536b, $851e20fe, $9833557e,
$13ecf0b0, $d3ffb372, $3f85c5c1, $0aef7ed2);
const
S1: array[0..255] of DWord= (
$30fb40d4, $9fa0ff0b, $6beccd2f, $3f258c7a, $1e213f2f, $9C004dd3,
$6003e540, $cf9fc949, $bfd4af27, $88bbbdb5, $e2034090, $98d09675,
$6e63a0e0, $15c361d2, $c2e7661d, $22d4ff8e, $28683b6f, $c07fd059,
$ff2379c8, $775f50e2, $43c340d3, $df2f8656, $887ca41a, $a2d2bd2d,
$a1c9e0d6, $346c4819, $61b76d87, $22540f2f, $2abe32e1, $aa54166b,
$22568e3a, $a2d341d0, $66db40c8, $a784392f, $004dff2f, $2db9d2de,
$97943fac, $4a97c1d8, $527644b7, $b5f437a7, $b82cbaef, $d751d159,
$6ff7f0ed, $5a097a1f, $827b68d0, $90ecf52e, $22b0c054, $bc8e5935,
$4b6d2f7f, $50bb64a2, $d2664910, $bee5812d, $b7332290, $e93b159f,
$b48ee411, $4bff345d, $fd45c240, $ad31973f, $c4f6d02e, $55fc8165,
$d5b1caad, $a1ac2dae, $a2d4b76d, $c19b0C50, $882240f2, $0c6e4f38,
$a4e4bfd7, $4f5ba272, $564c1d2f, $c59c5319, $b949e354, $b04669fe,
$b1b6ab8a, $c71358dd, $6385c545, $110f935d, $57538ad5, $6a390493,
$e63d37e0, $2a54f6b3, $3a787d5f, $6276a0b5, $19a6fcdf, $7a42206a,
$29f9d4d5, $f61b1891, $bb72275e, $aa508167, $38901091, $c6b505eb,
$84c7cb8c, $2ad75a0f, $874a1427, $a2d1936b, $2ad286af, $aa56d291,
$d7894360, $425c750d, $93b39e26, $187184c9, $6c00b32d, $73e2bb14,
$a0bebc3c, $54623779, $64459eab, $3f328b82, $7718cf82, $59a2cea6,
$04ee002e, $89fe78e6, $3fab0950, $325ff6C2, $81383f05, $6963c5c8,
$76cb5ad6, $d49974c9, $ca180dcf, $380782d5, $c7fa5cf6, $8ac31511,
$35e79e13, $47da91d0, $f40f9086, $a7e2419e, $31366241, $051ef495,
$aa573b04, $4a805d8d, $548300d0, $00322a3c, $bf64cddf, $ba57a68e,
$75c6372b, $50afd341, $a7c13275, $915a0bf5, $6b54bfab, $2b0b1426,
$ab4cc9d7, $449ccd82, $f7fbf265, $ab85c5f3, $1b55db94, $aad4e324,
$cfa4bd3f, $2deaa3e2, $9e204d02, $c8bd25ac, $eadf55b3, $d5bd9e98,
$e31231b2, $2ad5ad6c, $954329de, $adbe4528, $d8710f69, $aa51c90f,
$aa786bf6, $22513f1e, $aa51a79b, $2ad344cc, $7b5a41f0, $d37cfbad,
$1b069505, $41ece491, $b4c332e6, $032268d4, $c9600acc, $ce387e6d,
$bf6bb16c, $6a70fb78, $0d03d9c9, $d4df39de, $e01063da, $4736f464,
$5ad328d8, $b347cc96, $75bb0fc3, $98511bfb, $4ffbcc35, $b58bcf6a,
$e11f0abc, $bfc5fe4a, $a70aec10, $ac39570a, $3f04442f, $6188b153,
$e0397a2e, $5727cb79, $9ceb418f, $1cacd68d, $2ad37c96, $0175cb9d,
$c69dff09, $c75b65f0, $d9db40d8, $ec0e7779, $4744ead4, $b11c3274,
$dd24cb9e, $7e1c54bd, $f01144f9, $d2240eb1, $9675b3fd, $a3ac3755,
$d47c27af, $51c85f4d, $56907596, $a5bb15e6, $580304f0, $ca042cf1,
$011a37ea, $8dbfaadb, $35ba3e4a, $3526ffa0, $c37b4d09, $bc306ed9,
$98a52666, $5648f725, $ff5e569d, $0ced63d0, $7c63b2cf, $700b45e1,
$d5ea50f1, $85a92872, $af1fbda7, $d4234870, $a7870bf3, $2d3b4d79,
$42e04198, $0cd0ede7, $26470db8, $f881814C, $474d6ad7, $7c0c5e5c,
$d1231959, $381b7298, $f5d2f4db, $ab838653, $6e2f1e23, $83719c9e,
$bd91e046, $9a56456e, $dc39200c, $20c8c571, $962bda1c, $e1e696ff,
$b141ab08, $7cca89b9, $1a69e783, $02cc4843, $a2f7c579, $429ef47d,
$427b169c, $5ac9f049, $dd8f0f00, $5c8165bf);
S2: array[0..255] of DWord= (
$1f201094, $ef0ba75b, $69e3cf7e, $393f4380, $fe61cf7a, $eec5207a,
$55889c94, $72fc0651, $ada7ef79, $4e1d7235, $d55a63ce, $de0436ba,
$99c430ef, $5f0c0794, $18dcdb7d, $a1d6eff3, $a0b52f7b, $59e83605,
$ee15b094, $e9ffd909, $dc440086, $ef944459, $ba83ccb3, $e0c3cdfb,
$d1da4181, $3b092ab1, $f997f1c1, $a5e6cf7b, $01420ddb, $e4e7ef5b,
$25a1ff41, $e180f806, $1fc41080, $179bee7a, $d37ac6a9, $fe5830a4,
$98de8b7f, $77e83f4e, $79929269, $24fa9f7b, $e113c85b, $acc40083,
$d7503525, $f7ea615f, $62143154, $0d554b63, $5d681121, $c866c359,
$3d63cf73, $cee234c0, $d4d87e87, $5c672b21, $071f6181, $39f7627f,
$361e3084, $e4eb573b, $602f64a4, $d63acd9c, $1bbc4635, $9e81032d,
$2701f50c, $99847ab4, $a0e3df79, $ba6cf38c, $10843094, $2537a95e,
$f46f6ffe, $a1ff3b1f, $208cfb6a, $8f458c74, $d9e0a227, $4ec73a34,
$fc884f69, $3e4de8df, $ef0e0088, $3559648d, $8a45388c, $1d804366,
$721d9bfd, $a58684bb, $e8256333, $844e8212, $128d8098, $fed33fb4,
$ce280ae1, $27e19ba5, $d5a6c252, $e49754bd, $c5d655dd, $eb667064,
$77840b4d, $a1b6a801, $84db26a9, $e0b56714, $21f043b7, $e5d05860,
$54f03084, $066ff472, $a31aa153, $dadc4755, $b5625dbf, $68561be6,
$83ca6b94, $2d6ed23b, $eccf01db, $a6d3d0ba, $b6803d5c, $af77a709,
$33b4a34c, $397bc8d6, $5ee22b95, $5f0e5304, $81ed6f61, $20e74364,
$b45e1378, $de18639b, $881ca122, $b96726d1, $8049a7e8, $22b7da7b,
$5e552d25, $5272d237, $79d2951c, $c60d894c, $488cb402, $1ba4fe5b,
$a4b09f6b, $1ca815cf, $a20c3005, $8871df63, $b9de2fcb, $0cc6c9e9,
$0beeff53, $e3214517, $b4542835, $9f63293c, $ee41e729, $6e1d2d7c,
$50045286, $1e6685f3, $f33401c6, $30a22c95, $31a70850, $60930f13,
$73f98417, $a1269859, $ec645c44, $52c877a9, $cdff33a6, $a02b1741,
$7cbad9a2, $2180036f, $50d99c08, $cb3f4861, $c26bd765, $64a3f6ab,
$80342676, $25a75e7b, $e4e6d1fc, $20c710e6, $cdf0b680, $17844d3b,
$31eef84d, $7e0824e4, $2ccb49eb, $846a3bae, $8ff77888, $ee5d60f6,
$7af75673, $2fdd5cdb, $a11631c1, $30f66f43, $b3faec54, $157fd7fa,
$ef8579cc, $d152de58, $db2ffd5e, $8f32ce19, $306af97a, $02f03ef8,
$99319ad5, $c242fa0f, $a7e3ebb0, $c68e4906, $b8da230c, $80823028,
$dcdef3c8, $d35fb171, $088a1bc8, $bec0c560, $61a3c9e8, $bca8f54d,
$c72feffa, $22822e99, $82c570b4, $d8d94e89, $8b1c34bc, $301e16e6,
$273be979, $b0ffeaa6, $61d9b8c6, $00b24869, $b7ffce3f, $08dc283b,
$43daf65a, $f7e19798, $7619b72f, $8f1c9ba4, $dc8637a0, $16a7d3b1,
$9fc393b7, $a7136eeb, $c6bcc63e, $1a513742, $ef6828bc, $520365d6,
$2d6a77ab, $3527ed4b, $821fd216, $095c6e2e, $db92f2fb, $5eea29cb,
$145892f5, $91584f7f, $5483697b, $2667a8cc, $85196048, $8c4bacea,
$833860d4, $0d23e0f9, $6c387e8a, $0ae6d249, $b284600c, $d835731d,
$dcb1c647, $ac4c56ea, $3ebd81b3, $230eabb0, $6438bc87, $f0b5b1fa,
$8f5ea2b3, $fc184642, $0a036b7a, $4fb089bd, $649da589, $a345415e,
$5c038323, $3e5d3bb9, $43d79572, $7e6dd07c, $06dfdf1e, $6c6cc4ef,
$7160a539, $73bfbe70, $83877605, $4523ecf1);
S3: array[0..255] of DWord= (
$8defc240, $25fa5d9f, $eb903dbf, $e810c907, $47607fff, $369fe44b,
$8c1fc644, $aececa90, $beb1f9bf, $eefbcaea, $e8cf1950, $51df07ae,
$920e8806, $f0ad0548, $e13c8d83, $927010d5, $11107d9f, $07647db9,
$b2e3e4d4, $3d4f285e, $b9afa820, $fade82e0, $a067268b, $8272792e,
$553fb2c0, $489ae22b, $d4ef9794, $125e3fbc, $21fffcee, $825b1bfd,
$9255c5ed, $1257a240, $4e1a8302, $bae07fff, $528246e7, $8e57140e,
$3373f7bf, $8c9f8188, $a6fc4ee8, $c982b5a5, $a8c01db7, $579fc264,
$67094f31, $f2bd3f5f, $40fff7c1, $1fb78dfc, $8e6bd2c1, $437be59b,
$99b03dbf, $b5dbc64b, $638dc0e6, $55819d99, $a197c81c, $4a012d6e,
$c5884a28, $ccc36f71, $b843c213, $6c0743f1, $8309893c, $0feddd5f,
$2f7fe850, $d7c07f7e, $02507fbf, $5afb9a04, $a747d2d0, $1651192e,
$af70bf3e, $58c31380, $5f98302e, $727cc3c4, $0a0fb402, $0f7fef82,
$8c96fdad, $5d2c2aae, $8ee99a49, $50da88b8, $8427f4a0, $1eac5790,
$796fb449, $8252dc15, $efbd7d9b, $a672597d, $ada840d8, $45f54504,
$fa5d7403, $e83ec305, $4f91751a, $925669c2, $23efe941, $a903f12e,
$60270df2, $0276e4b6, $94fd6574, $927985b2, $8276dbcb, $02778176,
$f8af918d, $4e48f79e, $8f616ddf, $e29d840e, $842f7d83, $340ce5c8,
$96bbb682, $93b4b148, $ef303cab, $984faf28, $779faf9b, $92dc560d,
$224d1e20, $8437aa88, $7d29dc96, $2756d3dc, $8b907cee, $b51fd240,
$e7c07ce3, $e566b4a1, $c3e9615e, $3cf8209d, $6094d1e3, $cd9ca341,
$5c76460e, $00ea983b, $d4d67881, $fd47572c, $f76cedd9, $bda8229c,
$127dadaa, $438a074e, $1f97c090, $081bdb8a, $93a07ebe, $b938ca15,
$97b03cff, $3dc2c0f8, $8d1ab2ec, $64380e51, $68cc7bfb, $d90f2788,
$12490181, $5de5ffd4, $dd7ef86a, $76a2e214, $b9a40368, $925d958f,
$4b39fffa, $ba39aee9, $a4ffd30b, $faf7933b, $6d498623, $193cbcfa,
$27627545, $825cf47a, $61bd8ba0, $d11e42d1, $cead04f4, $127ea392,
$10428db7, $8272a972, $9270c4a8, $127de50b, $285ba1c8, $3c62f44f,
$35c0eaa5, $e805d231, $428929fb, $b4fcdf82, $4fb66a53, $0e7dc15b,
$1f081fab, $108618ae, $fcfd086d, $f9ff2889, $694bcc11, $236a5cae,
$12deca4d, $2c3f8cc5, $d2d02dfe, $f8ef5896, $e4cf52da, $95155b67,
$494a488c, $b9b6a80c, $5c8f82bc, $89d36b45, $3a609437, $ec00c9a9,
$44715253, $0a874b49, $d773bc40, $7c34671c, $02717ef6, $4feb5536,
$a2d02fff, $d2bf60c4, $d43f03c0, $50b4ef6d, $07478cd1, $006e1888,
$a2e53f55, $b9e6d4bc, $a2048016, $97573833, $d7207d67, $de0f8f3d,
$72f87b33, $abcc4f33, $7688c55d, $7b00a6b0, $947b0001, $570075d2,
$f9bb88f8, $8942019e, $4264a5ff, $856302e0, $72dbd92b, $ee971b69,
$6ea22fde, $5f08ae2b, $af7a616d, $e5c98767, $cf1febd2, $61efc8c2,
$f1ac2571, $cc8239c2, $67214cb8, $b1e583d1, $b7dc3e62, $7f10bdce,
$f90a5c38, $0ff0443d, $606e6dc6, $60543a49, $5727c148, $2be98a1d,
$8ab41738, $20e1be24, $af96da0f, $68458425, $99833be5, $600d457d,
$282f9350, $8334b362, $d91d1120, $2b6d8da0, $642b1e31, $9c305a00,
$52bce688, $1b03588a, $f7baefd5, $4142ed9c, $a4315c11, $83323ec5,
$dfef4636, $a133c501, $e9d3531c, $ee353783);
S4: array[0..255] of DWord= (
$9db30420, $1fb6e9de, $a7be7bef, $d273a298, $4a4f7bdb, $64ad8c57,
$85510443, $fa020ed1, $7e287aff, $e60fb663, $095f35a1, $79ebf120,
$fd059d43, $6497b7b1, $f3641f63, $241e4adf, $28147f5f, $4fa2b8cd,
$c9430040, $0cc32220, $fdd30b30, $c0a5374f, $1d2d00d9, $24147b15,
$ee4d111a, $0fca5167, $71ff904c, $2d195ffe, $1a05645f, $0c13fefe,
$081b08ca, $05170121, $80530100, $e83e5efe, $ac9af4f8, $7fe72701,
$d2b8ee5f, $06df4261, $bb9e9b8a, $7293ea25, $ce84ffdf, $f5718801,
$3dd64b04, $a26f263b, $7ed48400, $547eebe6, $446d4ca0, $6cf3d6f5,
$2649abdf, $aea0c7f5, $36338cc1, $503f7e93, $d3772061, $11b638e1,
$72500e03, $f80eb2bb, $abe0502e, $ec8d77de, $57971e81, $e14f6746,
$c9335400, $6920318f, $081dbb99, $ffc304a5, $4d351805, $7f3d5ce3,
$a6c866c6, $5d5bcca9, $daec6fea, $9f926f91, $9f46222f, $3991467d,
$a5bf6d8e, $1143c44f, $43958302, $d0214eeb, $022083b8, $3fb6180c,
$18f8931e, $281658e6, $26486e3e, $8bd78a70, $7477e4c1, $b506e07c,
$f32d0a25, $79098b02, $e4eabb81, $28123b23, $69dead38, $1574ca16,
$df871b62, $211c40b7, $a51a9ef9, $0014377b, $041e8ac8, $09114003,
$bd59e4d2, $e3d156d5, $4fe876d5, $2f91a340, $557be8de, $00eae4a7,
$0ce5c2ec, $4db4bba6, $e756bdff, $dd3369ac, $ec17b035, $06572327,
$99afc8b0, $56c8c391, $6b65811c, $5e146119, $6e85cb75, $be07c002,
$c2325577, $893ff4ec, $5bbfc92d, $d0ec3b25, $b7801ab7, $8d6d3b24,
$20c763ef, $c366a5fc, $9c382880, $0ace3205, $aac9548a, $eca1d7c7,
$041afa32, $1d16625a, $6701902c, $9b757a54, $31d477f7, $9126b031,
$36cc6fdb, $c70b8b46, $d9e66a48, $56e55a79, $026a4ceb, $52437eff,
$2f8f76b4, $0df980a5, $8674cde3, $edda04eb, $17a9be04, $2c18f4df,
$b7747f9d, $ab2af7b4, $efc34d20, $2e096b7c, $1741a254, $e5b6a035,
$213d42f6, $2c1c7c26, $61c2f50f, $6552daf9, $d2c231f8, $25130f69,
$d8167fa2, $0418f2c8, $001a96a6, $0d1526ab, $63315c21, $5e0a72ec,
$49bafefd, $187908d9, $8d0dbd86, $311170a7, $3e9b640c, $cc3e10d7,
$d5cad3b6, $0caec388, $f73001e1, $6c728aff, $71eae2a1, $1f9af36e,
$cfcbd12f, $c1de8417, $ac07be6b, $cb44a1d8, $8b9b0f56, $013988c3,
$b1c52fca, $b4be31cd, $d8782806, $12a3a4e2, $6f7de532, $58fd7eb6,
$d01ee900, $24adffc2, $f4990fc5, $9711aac5, $001d7b95, $82e5e7d2,
$109873f6, $00613096, $c32d9521, $ada121ff, $29908415, $7fbb977f,
$af9eb3db, $29c9ed2a, $5ce2a465, $a730f32c, $d0aa3fe8, $8a5cc091,
$d49e2ce7, $0ce454a9, $d60acd86, $015f1919, $77079103, $dea03af6,
$78a8565e, $dee356df, $21f05cbe, $8b75e387, $b3c50651, $b8a5c3ef,
$d8eeb6d2, $e523be77, $c2154529, $2f69efdf, $afe67afb, $f470c4b2,
$f3e0eb5b, $d6cc9876, $39e4460c, $1fda8538, $1987832f, $ca007367,
$a99144f8, $296b299e, $492fc295, $9266beab, $b5676e69, $9bd3ddda,
$df7e052f, $db25701c, $1b5e51ee, $f65324e6, $6afce36c, $0316cc04,
$8644213e, $b7dc59d0, $7965291f, $ccd6fd43, $41823979, $932bcdf6,
$b657c34d, $4edfd282, $7ae5290c, $3cb9536b, $851e20fe, $9833557e,
$13ecf0b0, $d3ffb372, $3f85c5c1, $0aef7ed2);

View File

@ -1,297 +1,297 @@
const
shifts2: array[0..15]of byte=
(0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0);
des_skb: array[0..7,0..63]of dword=(
(
(* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 *)
$00000000,$00000010,$20000000,$20000010,
$00010000,$00010010,$20010000,$20010010,
$00000800,$00000810,$20000800,$20000810,
$00010800,$00010810,$20010800,$20010810,
$00000020,$00000030,$20000020,$20000030,
$00010020,$00010030,$20010020,$20010030,
$00000820,$00000830,$20000820,$20000830,
$00010820,$00010830,$20010820,$20010830,
$00080000,$00080010,$20080000,$20080010,
$00090000,$00090010,$20090000,$20090010,
$00080800,$00080810,$20080800,$20080810,
$00090800,$00090810,$20090800,$20090810,
$00080020,$00080030,$20080020,$20080030,
$00090020,$00090030,$20090020,$20090030,
$00080820,$00080830,$20080820,$20080830,
$00090820,$00090830,$20090820,$20090830
),(
(* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 *)
$00000000,$02000000,$00002000,$02002000,
$00200000,$02200000,$00202000,$02202000,
$00000004,$02000004,$00002004,$02002004,
$00200004,$02200004,$00202004,$02202004,
$00000400,$02000400,$00002400,$02002400,
$00200400,$02200400,$00202400,$02202400,
$00000404,$02000404,$00002404,$02002404,
$00200404,$02200404,$00202404,$02202404,
$10000000,$12000000,$10002000,$12002000,
$10200000,$12200000,$10202000,$12202000,
$10000004,$12000004,$10002004,$12002004,
$10200004,$12200004,$10202004,$12202004,
$10000400,$12000400,$10002400,$12002400,
$10200400,$12200400,$10202400,$12202400,
$10000404,$12000404,$10002404,$12002404,
$10200404,$12200404,$10202404,$12202404
),(
(* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 *)
$00000000,$00000001,$00040000,$00040001,
$01000000,$01000001,$01040000,$01040001,
$00000002,$00000003,$00040002,$00040003,
$01000002,$01000003,$01040002,$01040003,
$00000200,$00000201,$00040200,$00040201,
$01000200,$01000201,$01040200,$01040201,
$00000202,$00000203,$00040202,$00040203,
$01000202,$01000203,$01040202,$01040203,
$08000000,$08000001,$08040000,$08040001,
$09000000,$09000001,$09040000,$09040001,
$08000002,$08000003,$08040002,$08040003,
$09000002,$09000003,$09040002,$09040003,
$08000200,$08000201,$08040200,$08040201,
$09000200,$09000201,$09040200,$09040201,
$08000202,$08000203,$08040202,$08040203,
$09000202,$09000203,$09040202,$09040203
),(
(* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 *)
$00000000,$00100000,$00000100,$00100100,
$00000008,$00100008,$00000108,$00100108,
$00001000,$00101000,$00001100,$00101100,
$00001008,$00101008,$00001108,$00101108,
$04000000,$04100000,$04000100,$04100100,
$04000008,$04100008,$04000108,$04100108,
$04001000,$04101000,$04001100,$04101100,
$04001008,$04101008,$04001108,$04101108,
$00020000,$00120000,$00020100,$00120100,
$00020008,$00120008,$00020108,$00120108,
$00021000,$00121000,$00021100,$00121100,
$00021008,$00121008,$00021108,$00121108,
$04020000,$04120000,$04020100,$04120100,
$04020008,$04120008,$04020108,$04120108,
$04021000,$04121000,$04021100,$04121100,
$04021008,$04121008,$04021108,$04121108
),(
(* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 *)
$00000000,$10000000,$00010000,$10010000,
$00000004,$10000004,$00010004,$10010004,
$20000000,$30000000,$20010000,$30010000,
$20000004,$30000004,$20010004,$30010004,
$00100000,$10100000,$00110000,$10110000,
$00100004,$10100004,$00110004,$10110004,
$20100000,$30100000,$20110000,$30110000,
$20100004,$30100004,$20110004,$30110004,
$00001000,$10001000,$00011000,$10011000,
$00001004,$10001004,$00011004,$10011004,
$20001000,$30001000,$20011000,$30011000,
$20001004,$30001004,$20011004,$30011004,
$00101000,$10101000,$00111000,$10111000,
$00101004,$10101004,$00111004,$10111004,
$20101000,$30101000,$20111000,$30111000,
$20101004,$30101004,$20111004,$30111004
),(
(* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 *)
$00000000,$08000000,$00000008,$08000008,
$00000400,$08000400,$00000408,$08000408,
$00020000,$08020000,$00020008,$08020008,
$00020400,$08020400,$00020408,$08020408,
$00000001,$08000001,$00000009,$08000009,
$00000401,$08000401,$00000409,$08000409,
$00020001,$08020001,$00020009,$08020009,
$00020401,$08020401,$00020409,$08020409,
$02000000,$0A000000,$02000008,$0A000008,
$02000400,$0A000400,$02000408,$0A000408,
$02020000,$0A020000,$02020008,$0A020008,
$02020400,$0A020400,$02020408,$0A020408,
$02000001,$0A000001,$02000009,$0A000009,
$02000401,$0A000401,$02000409,$0A000409,
$02020001,$0A020001,$02020009,$0A020009,
$02020401,$0A020401,$02020409,$0A020409
),(
(* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 *)
$00000000,$00000100,$00080000,$00080100,
$01000000,$01000100,$01080000,$01080100,
$00000010,$00000110,$00080010,$00080110,
$01000010,$01000110,$01080010,$01080110,
$00200000,$00200100,$00280000,$00280100,
$01200000,$01200100,$01280000,$01280100,
$00200010,$00200110,$00280010,$00280110,
$01200010,$01200110,$01280010,$01280110,
$00000200,$00000300,$00080200,$00080300,
$01000200,$01000300,$01080200,$01080300,
$00000210,$00000310,$00080210,$00080310,
$01000210,$01000310,$01080210,$01080310,
$00200200,$00200300,$00280200,$00280300,
$01200200,$01200300,$01280200,$01280300,
$00200210,$00200310,$00280210,$00280310,
$01200210,$01200310,$01280210,$01280310
),(
(* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 *)
$00000000,$04000000,$00040000,$04040000,
$00000002,$04000002,$00040002,$04040002,
$00002000,$04002000,$00042000,$04042000,
$00002002,$04002002,$00042002,$04042002,
$00000020,$04000020,$00040020,$04040020,
$00000022,$04000022,$00040022,$04040022,
$00002020,$04002020,$00042020,$04042020,
$00002022,$04002022,$00042022,$04042022,
$00000800,$04000800,$00040800,$04040800,
$00000802,$04000802,$00040802,$04040802,
$00002800,$04002800,$00042800,$04042800,
$00002802,$04002802,$00042802,$04042802,
$00000820,$04000820,$00040820,$04040820,
$00000822,$04000822,$00040822,$04040822,
$00002820,$04002820,$00042820,$04042820,
$00002822,$04002822,$00042822,$04042822
));
des_sptrans: array[0..7,0..63] of dword=(
(
(* nibble 0 *)
$02080800, $00080000, $02000002, $02080802,
$02000000, $00080802, $00080002, $02000002,
$00080802, $02080800, $02080000, $00000802,
$02000802, $02000000, $00000000, $00080002,
$00080000, $00000002, $02000800, $00080800,
$02080802, $02080000, $00000802, $02000800,
$00000002, $00000800, $00080800, $02080002,
$00000800, $02000802, $02080002, $00000000,
$00000000, $02080802, $02000800, $00080002,
$02080800, $00080000, $00000802, $02000800,
$02080002, $00000800, $00080800, $02000002,
$00080802, $00000002, $02000002, $02080000,
$02080802, $00080800, $02080000, $02000802,
$02000000, $00000802, $00080002, $00000000,
$00080000, $02000000, $02000802, $02080800,
$00000002, $02080002, $00000800, $00080802
),(
(* nibble 1 *)
$40108010, $00000000, $00108000, $40100000,
$40000010, $00008010, $40008000, $00108000,
$00008000, $40100010, $00000010, $40008000,
$00100010, $40108000, $40100000, $00000010,
$00100000, $40008010, $40100010, $00008000,
$00108010, $40000000, $00000000, $00100010,
$40008010, $00108010, $40108000, $40000010,
$40000000, $00100000, $00008010, $40108010,
$00100010, $40108000, $40008000, $00108010,
$40108010, $00100010, $40000010, $00000000,
$40000000, $00008010, $00100000, $40100010,
$00008000, $40000000, $00108010, $40008010,
$40108000, $00008000, $00000000, $40000010,
$00000010, $40108010, $00108000, $40100000,
$40100010, $00100000, $00008010, $40008000,
$40008010, $00000010, $40100000, $00108000
),(
(* nibble 2 *)
$04000001, $04040100, $00000100, $04000101,
$00040001, $04000000, $04000101, $00040100,
$04000100, $00040000, $04040000, $00000001,
$04040101, $00000101, $00000001, $04040001,
$00000000, $00040001, $04040100, $00000100,
$00000101, $04040101, $00040000, $04000001,
$04040001, $04000100, $00040101, $04040000,
$00040100, $00000000, $04000000, $00040101,
$04040100, $00000100, $00000001, $00040000,
$00000101, $00040001, $04040000, $04000101,
$00000000, $04040100, $00040100, $04040001,
$00040001, $04000000, $04040101, $00000001,
$00040101, $04000001, $04000000, $04040101,
$00040000, $04000100, $04000101, $00040100,
$04000100, $00000000, $04040001, $00000101,
$04000001, $00040101, $00000100, $04040000
),(
(* nibble 3 *)
$00401008, $10001000, $00000008, $10401008,
$00000000, $10400000, $10001008, $00400008,
$10401000, $10000008, $10000000, $00001008,
$10000008, $00401008, $00400000, $10000000,
$10400008, $00401000, $00001000, $00000008,
$00401000, $10001008, $10400000, $00001000,
$00001008, $00000000, $00400008, $10401000,
$10001000, $10400008, $10401008, $00400000,
$10400008, $00001008, $00400000, $10000008,
$00401000, $10001000, $00000008, $10400000,
$10001008, $00000000, $00001000, $00400008,
$00000000, $10400008, $10401000, $00001000,
$10000000, $10401008, $00401008, $00400000,
$10401008, $00000008, $10001000, $00401008,
$00400008, $00401000, $10400000, $10001008,
$00001008, $10000000, $10000008, $10401000
),(
(* nibble 4 *)
$08000000, $00010000, $00000400, $08010420,
$08010020, $08000400, $00010420, $08010000,
$00010000, $00000020, $08000020, $00010400,
$08000420, $08010020, $08010400, $00000000,
$00010400, $08000000, $00010020, $00000420,
$08000400, $00010420, $00000000, $08000020,
$00000020, $08000420, $08010420, $00010020,
$08010000, $00000400, $00000420, $08010400,
$08010400, $08000420, $00010020, $08010000,
$00010000, $00000020, $08000020, $08000400,
$08000000, $00010400, $08010420, $00000000,
$00010420, $08000000, $00000400, $00010020,
$08000420, $00000400, $00000000, $08010420,
$08010020, $08010400, $00000420, $00010000,
$00010400, $08010020, $08000400, $00000420,
$00000020, $00010420, $08010000, $08000020
),(
(* nibble 5 *)
$80000040, $00200040, $00000000, $80202000,
$00200040, $00002000, $80002040, $00200000,
$00002040, $80202040, $00202000, $80000000,
$80002000, $80000040, $80200000, $00202040,
$00200000, $80002040, $80200040, $00000000,
$00002000, $00000040, $80202000, $80200040,
$80202040, $80200000, $80000000, $00002040,
$00000040, $00202000, $00202040, $80002000,
$00002040, $80000000, $80002000, $00202040,
$80202000, $00200040, $00000000, $80002000,
$80000000, $00002000, $80200040, $00200000,
$00200040, $80202040, $00202000, $00000040,
$80202040, $00202000, $00200000, $80002040,
$80000040, $80200000, $00202040, $00000000,
$00002000, $80000040, $80002040, $80202000,
$80200000, $00002040, $00000040, $80200040
),(
(* nibble 6 *)
$00004000, $00000200, $01000200, $01000004,
$01004204, $00004004, $00004200, $00000000,
$01000000, $01000204, $00000204, $01004000,
$00000004, $01004200, $01004000, $00000204,
$01000204, $00004000, $00004004, $01004204,
$00000000, $01000200, $01000004, $00004200,
$01004004, $00004204, $01004200, $00000004,
$00004204, $01004004, $00000200, $01000000,
$00004204, $01004000, $01004004, $00000204,
$00004000, $00000200, $01000000, $01004004,
$01000204, $00004204, $00004200, $00000000,
$00000200, $01000004, $00000004, $01000200,
$00000000, $01000204, $01000200, $00004200,
$00000204, $00004000, $01004204, $01000000,
$01004200, $00000004, $00004004, $01004204,
$01000004, $01004200, $01004000, $00004004
),(
(* nibble 7 *)
$20800080, $20820000, $00020080, $00000000,
$20020000, $00800080, $20800000, $20820080,
$00000080, $20000000, $00820000, $00020080,
$00820080, $20020080, $20000080, $20800000,
$00020000, $00820080, $00800080, $20020000,
$20820080, $20000080, $00000000, $00820000,
$20000000, $00800000, $20020080, $20800080,
$00800000, $00020000, $20820000, $00000080,
$00800000, $00020000, $20000080, $20820080,
$00020080, $20000000, $00000000, $00820000,
$20800080, $20020080, $20020000, $00800080,
$20820000, $00000080, $00800080, $20020000,
$20820080, $00800000, $20800000, $20000080,
$00820000, $00020080, $20020080, $20800000,
$00000080, $20820000, $00820080, $00000000,
$20000000, $20800080, $00020000, $00820080
));
const
shifts2: array[0..15]of byte=
(0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0);
des_skb: array[0..7,0..63]of dword=(
(
(* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 *)
$00000000,$00000010,$20000000,$20000010,
$00010000,$00010010,$20010000,$20010010,
$00000800,$00000810,$20000800,$20000810,
$00010800,$00010810,$20010800,$20010810,
$00000020,$00000030,$20000020,$20000030,
$00010020,$00010030,$20010020,$20010030,
$00000820,$00000830,$20000820,$20000830,
$00010820,$00010830,$20010820,$20010830,
$00080000,$00080010,$20080000,$20080010,
$00090000,$00090010,$20090000,$20090010,
$00080800,$00080810,$20080800,$20080810,
$00090800,$00090810,$20090800,$20090810,
$00080020,$00080030,$20080020,$20080030,
$00090020,$00090030,$20090020,$20090030,
$00080820,$00080830,$20080820,$20080830,
$00090820,$00090830,$20090820,$20090830
),(
(* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 *)
$00000000,$02000000,$00002000,$02002000,
$00200000,$02200000,$00202000,$02202000,
$00000004,$02000004,$00002004,$02002004,
$00200004,$02200004,$00202004,$02202004,
$00000400,$02000400,$00002400,$02002400,
$00200400,$02200400,$00202400,$02202400,
$00000404,$02000404,$00002404,$02002404,
$00200404,$02200404,$00202404,$02202404,
$10000000,$12000000,$10002000,$12002000,
$10200000,$12200000,$10202000,$12202000,
$10000004,$12000004,$10002004,$12002004,
$10200004,$12200004,$10202004,$12202004,
$10000400,$12000400,$10002400,$12002400,
$10200400,$12200400,$10202400,$12202400,
$10000404,$12000404,$10002404,$12002404,
$10200404,$12200404,$10202404,$12202404
),(
(* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 *)
$00000000,$00000001,$00040000,$00040001,
$01000000,$01000001,$01040000,$01040001,
$00000002,$00000003,$00040002,$00040003,
$01000002,$01000003,$01040002,$01040003,
$00000200,$00000201,$00040200,$00040201,
$01000200,$01000201,$01040200,$01040201,
$00000202,$00000203,$00040202,$00040203,
$01000202,$01000203,$01040202,$01040203,
$08000000,$08000001,$08040000,$08040001,
$09000000,$09000001,$09040000,$09040001,
$08000002,$08000003,$08040002,$08040003,
$09000002,$09000003,$09040002,$09040003,
$08000200,$08000201,$08040200,$08040201,
$09000200,$09000201,$09040200,$09040201,
$08000202,$08000203,$08040202,$08040203,
$09000202,$09000203,$09040202,$09040203
),(
(* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 *)
$00000000,$00100000,$00000100,$00100100,
$00000008,$00100008,$00000108,$00100108,
$00001000,$00101000,$00001100,$00101100,
$00001008,$00101008,$00001108,$00101108,
$04000000,$04100000,$04000100,$04100100,
$04000008,$04100008,$04000108,$04100108,
$04001000,$04101000,$04001100,$04101100,
$04001008,$04101008,$04001108,$04101108,
$00020000,$00120000,$00020100,$00120100,
$00020008,$00120008,$00020108,$00120108,
$00021000,$00121000,$00021100,$00121100,
$00021008,$00121008,$00021108,$00121108,
$04020000,$04120000,$04020100,$04120100,
$04020008,$04120008,$04020108,$04120108,
$04021000,$04121000,$04021100,$04121100,
$04021008,$04121008,$04021108,$04121108
),(
(* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 *)
$00000000,$10000000,$00010000,$10010000,
$00000004,$10000004,$00010004,$10010004,
$20000000,$30000000,$20010000,$30010000,
$20000004,$30000004,$20010004,$30010004,
$00100000,$10100000,$00110000,$10110000,
$00100004,$10100004,$00110004,$10110004,
$20100000,$30100000,$20110000,$30110000,
$20100004,$30100004,$20110004,$30110004,
$00001000,$10001000,$00011000,$10011000,
$00001004,$10001004,$00011004,$10011004,
$20001000,$30001000,$20011000,$30011000,
$20001004,$30001004,$20011004,$30011004,
$00101000,$10101000,$00111000,$10111000,
$00101004,$10101004,$00111004,$10111004,
$20101000,$30101000,$20111000,$30111000,
$20101004,$30101004,$20111004,$30111004
),(
(* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 *)
$00000000,$08000000,$00000008,$08000008,
$00000400,$08000400,$00000408,$08000408,
$00020000,$08020000,$00020008,$08020008,
$00020400,$08020400,$00020408,$08020408,
$00000001,$08000001,$00000009,$08000009,
$00000401,$08000401,$00000409,$08000409,
$00020001,$08020001,$00020009,$08020009,
$00020401,$08020401,$00020409,$08020409,
$02000000,$0A000000,$02000008,$0A000008,
$02000400,$0A000400,$02000408,$0A000408,
$02020000,$0A020000,$02020008,$0A020008,
$02020400,$0A020400,$02020408,$0A020408,
$02000001,$0A000001,$02000009,$0A000009,
$02000401,$0A000401,$02000409,$0A000409,
$02020001,$0A020001,$02020009,$0A020009,
$02020401,$0A020401,$02020409,$0A020409
),(
(* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 *)
$00000000,$00000100,$00080000,$00080100,
$01000000,$01000100,$01080000,$01080100,
$00000010,$00000110,$00080010,$00080110,
$01000010,$01000110,$01080010,$01080110,
$00200000,$00200100,$00280000,$00280100,
$01200000,$01200100,$01280000,$01280100,
$00200010,$00200110,$00280010,$00280110,
$01200010,$01200110,$01280010,$01280110,
$00000200,$00000300,$00080200,$00080300,
$01000200,$01000300,$01080200,$01080300,
$00000210,$00000310,$00080210,$00080310,
$01000210,$01000310,$01080210,$01080310,
$00200200,$00200300,$00280200,$00280300,
$01200200,$01200300,$01280200,$01280300,
$00200210,$00200310,$00280210,$00280310,
$01200210,$01200310,$01280210,$01280310
),(
(* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 *)
$00000000,$04000000,$00040000,$04040000,
$00000002,$04000002,$00040002,$04040002,
$00002000,$04002000,$00042000,$04042000,
$00002002,$04002002,$00042002,$04042002,
$00000020,$04000020,$00040020,$04040020,
$00000022,$04000022,$00040022,$04040022,
$00002020,$04002020,$00042020,$04042020,
$00002022,$04002022,$00042022,$04042022,
$00000800,$04000800,$00040800,$04040800,
$00000802,$04000802,$00040802,$04040802,
$00002800,$04002800,$00042800,$04042800,
$00002802,$04002802,$00042802,$04042802,
$00000820,$04000820,$00040820,$04040820,
$00000822,$04000822,$00040822,$04040822,
$00002820,$04002820,$00042820,$04042820,
$00002822,$04002822,$00042822,$04042822
));
des_sptrans: array[0..7,0..63] of dword=(
(
(* nibble 0 *)
$02080800, $00080000, $02000002, $02080802,
$02000000, $00080802, $00080002, $02000002,
$00080802, $02080800, $02080000, $00000802,
$02000802, $02000000, $00000000, $00080002,
$00080000, $00000002, $02000800, $00080800,
$02080802, $02080000, $00000802, $02000800,
$00000002, $00000800, $00080800, $02080002,
$00000800, $02000802, $02080002, $00000000,
$00000000, $02080802, $02000800, $00080002,
$02080800, $00080000, $00000802, $02000800,
$02080002, $00000800, $00080800, $02000002,
$00080802, $00000002, $02000002, $02080000,
$02080802, $00080800, $02080000, $02000802,
$02000000, $00000802, $00080002, $00000000,
$00080000, $02000000, $02000802, $02080800,
$00000002, $02080002, $00000800, $00080802
),(
(* nibble 1 *)
$40108010, $00000000, $00108000, $40100000,
$40000010, $00008010, $40008000, $00108000,
$00008000, $40100010, $00000010, $40008000,
$00100010, $40108000, $40100000, $00000010,
$00100000, $40008010, $40100010, $00008000,
$00108010, $40000000, $00000000, $00100010,
$40008010, $00108010, $40108000, $40000010,
$40000000, $00100000, $00008010, $40108010,
$00100010, $40108000, $40008000, $00108010,
$40108010, $00100010, $40000010, $00000000,
$40000000, $00008010, $00100000, $40100010,
$00008000, $40000000, $00108010, $40008010,
$40108000, $00008000, $00000000, $40000010,
$00000010, $40108010, $00108000, $40100000,
$40100010, $00100000, $00008010, $40008000,
$40008010, $00000010, $40100000, $00108000
),(
(* nibble 2 *)
$04000001, $04040100, $00000100, $04000101,
$00040001, $04000000, $04000101, $00040100,
$04000100, $00040000, $04040000, $00000001,
$04040101, $00000101, $00000001, $04040001,
$00000000, $00040001, $04040100, $00000100,
$00000101, $04040101, $00040000, $04000001,
$04040001, $04000100, $00040101, $04040000,
$00040100, $00000000, $04000000, $00040101,
$04040100, $00000100, $00000001, $00040000,
$00000101, $00040001, $04040000, $04000101,
$00000000, $04040100, $00040100, $04040001,
$00040001, $04000000, $04040101, $00000001,
$00040101, $04000001, $04000000, $04040101,
$00040000, $04000100, $04000101, $00040100,
$04000100, $00000000, $04040001, $00000101,
$04000001, $00040101, $00000100, $04040000
),(
(* nibble 3 *)
$00401008, $10001000, $00000008, $10401008,
$00000000, $10400000, $10001008, $00400008,
$10401000, $10000008, $10000000, $00001008,
$10000008, $00401008, $00400000, $10000000,
$10400008, $00401000, $00001000, $00000008,
$00401000, $10001008, $10400000, $00001000,
$00001008, $00000000, $00400008, $10401000,
$10001000, $10400008, $10401008, $00400000,
$10400008, $00001008, $00400000, $10000008,
$00401000, $10001000, $00000008, $10400000,
$10001008, $00000000, $00001000, $00400008,
$00000000, $10400008, $10401000, $00001000,
$10000000, $10401008, $00401008, $00400000,
$10401008, $00000008, $10001000, $00401008,
$00400008, $00401000, $10400000, $10001008,
$00001008, $10000000, $10000008, $10401000
),(
(* nibble 4 *)
$08000000, $00010000, $00000400, $08010420,
$08010020, $08000400, $00010420, $08010000,
$00010000, $00000020, $08000020, $00010400,
$08000420, $08010020, $08010400, $00000000,
$00010400, $08000000, $00010020, $00000420,
$08000400, $00010420, $00000000, $08000020,
$00000020, $08000420, $08010420, $00010020,
$08010000, $00000400, $00000420, $08010400,
$08010400, $08000420, $00010020, $08010000,
$00010000, $00000020, $08000020, $08000400,
$08000000, $00010400, $08010420, $00000000,
$00010420, $08000000, $00000400, $00010020,
$08000420, $00000400, $00000000, $08010420,
$08010020, $08010400, $00000420, $00010000,
$00010400, $08010020, $08000400, $00000420,
$00000020, $00010420, $08010000, $08000020
),(
(* nibble 5 *)
$80000040, $00200040, $00000000, $80202000,
$00200040, $00002000, $80002040, $00200000,
$00002040, $80202040, $00202000, $80000000,
$80002000, $80000040, $80200000, $00202040,
$00200000, $80002040, $80200040, $00000000,
$00002000, $00000040, $80202000, $80200040,
$80202040, $80200000, $80000000, $00002040,
$00000040, $00202000, $00202040, $80002000,
$00002040, $80000000, $80002000, $00202040,
$80202000, $00200040, $00000000, $80002000,
$80000000, $00002000, $80200040, $00200000,
$00200040, $80202040, $00202000, $00000040,
$80202040, $00202000, $00200000, $80002040,
$80000040, $80200000, $00202040, $00000000,
$00002000, $80000040, $80002040, $80202000,
$80200000, $00002040, $00000040, $80200040
),(
(* nibble 6 *)
$00004000, $00000200, $01000200, $01000004,
$01004204, $00004004, $00004200, $00000000,
$01000000, $01000204, $00000204, $01004000,
$00000004, $01004200, $01004000, $00000204,
$01000204, $00004000, $00004004, $01004204,
$00000000, $01000200, $01000004, $00004200,
$01004004, $00004204, $01004200, $00000004,
$00004204, $01004004, $00000200, $01000000,
$00004204, $01004000, $01004004, $00000204,
$00004000, $00000200, $01000000, $01004004,
$01000204, $00004204, $00004200, $00000000,
$00000200, $01000004, $00000004, $01000200,
$00000000, $01000204, $01000200, $00004200,
$00000204, $00004000, $01004204, $01000000,
$01004200, $00000004, $00004004, $01004204,
$01000004, $01004200, $01004000, $00004004
),(
(* nibble 7 *)
$20800080, $20820000, $00020080, $00000000,
$20020000, $00800080, $20800000, $20820080,
$00000080, $20000000, $00820000, $00020080,
$00820080, $20020080, $20000080, $20800000,
$00020000, $00820080, $00800080, $20020000,
$20820080, $20000080, $00000000, $00820000,
$20000000, $00800000, $20020080, $20800080,
$00800000, $00020000, $20820000, $00000080,
$00800000, $00020000, $20000080, $20820080,
$00020080, $20000000, $00000000, $00820000,
$20800080, $20020080, $20020000, $00800080,
$20820000, $00000080, $00800080, $20020000,
$20820080, $00800000, $20800000, $20000080,
$00820000, $00020080, $20020080, $20800000,
$00000080, $20820000, $00820080, $00000000,
$20000000, $20800080, $00020000, $00820080
));

View File

@ -1,130 +1,130 @@
const
sTable: array[0..3, 0..255] of DWord= (
($00072000,$00075000,$00074800,$00071000,$00076800,$00074000,$00070000,$00077000,
$00073000,$00075800,$00070800,$00076000,$00073800,$00077800,$00072800,$00071800,
$0005A000,$0005D000,$0005C800,$00059000,$0005E800,$0005C000,$00058000,$0005F000,
$0005B000,$0005D800,$00058800,$0005E000,$0005B800,$0005F800,$0005A800,$00059800,
$00022000,$00025000,$00024800,$00021000,$00026800,$00024000,$00020000,$00027000,
$00023000,$00025800,$00020800,$00026000,$00023800,$00027800,$00022800,$00021800,
$00062000,$00065000,$00064800,$00061000,$00066800,$00064000,$00060000,$00067000,
$00063000,$00065800,$00060800,$00066000,$00063800,$00067800,$00062800,$00061800,
$00032000,$00035000,$00034800,$00031000,$00036800,$00034000,$00030000,$00037000,
$00033000,$00035800,$00030800,$00036000,$00033800,$00037800,$00032800,$00031800,
$0006A000,$0006D000,$0006C800,$00069000,$0006E800,$0006C000,$00068000,$0006F000,
$0006B000,$0006D800,$00068800,$0006E000,$0006B800,$0006F800,$0006A800,$00069800,
$0007A000,$0007D000,$0007C800,$00079000,$0007E800,$0007C000,$00078000,$0007F000,
$0007B000,$0007D800,$00078800,$0007E000,$0007B800,$0007F800,$0007A800,$00079800,
$00052000,$00055000,$00054800,$00051000,$00056800,$00054000,$00050000,$00057000,
$00053000,$00055800,$00050800,$00056000,$00053800,$00057800,$00052800,$00051800,
$00012000,$00015000,$00014800,$00011000,$00016800,$00014000,$00010000,$00017000,
$00013000,$00015800,$00010800,$00016000,$00013800,$00017800,$00012800,$00011800,
$0001A000,$0001D000,$0001C800,$00019000,$0001E800,$0001C000,$00018000,$0001F000,
$0001B000,$0001D800,$00018800,$0001E000,$0001B800,$0001F800,$0001A800,$00019800,
$00042000,$00045000,$00044800,$00041000,$00046800,$00044000,$00040000,$00047000,
$00043000,$00045800,$00040800,$00046000,$00043800,$00047800,$00042800,$00041800,
$0000A000,$0000D000,$0000C800,$00009000,$0000E800,$0000C000,$00008000,$0000F000,
$0000B000,$0000D800,$00008800,$0000E000,$0000B800,$0000F800,$0000A800,$00009800,
$00002000,$00005000,$00004800,$00001000,$00006800,$00004000,$00000000,$00007000,
$00003000,$00005800,$00000800,$00006000,$00003800,$00007800,$00002800,$00001800,
$0003A000,$0003D000,$0003C800,$00039000,$0003E800,$0003C000,$00038000,$0003F000,
$0003B000,$0003D800,$00038800,$0003E000,$0003B800,$0003F800,$0003A800,$00039800,
$0002A000,$0002D000,$0002C800,$00029000,$0002E800,$0002C000,$00028000,$0002F000,
$0002B000,$0002D800,$00028800,$0002E000,$0002B800,$0002F800,$0002A800,$00029800,
$0004A000,$0004D000,$0004C800,$00049000,$0004E800,$0004C000,$00048000,$0004F000,
$0004B000,$0004D800,$00048800,$0004E000,$0004B800,$0004F800,$0004A800,$00049800),
($03A80000,$03C00000,$03880000,$03E80000,$03D00000,$03980000,$03A00000,$03900000,
$03F00000,$03F80000,$03E00000,$03B80000,$03B00000,$03800000,$03C80000,$03D80000,
$06A80000,$06C00000,$06880000,$06E80000,$06D00000,$06980000,$06A00000,$06900000,
$06F00000,$06F80000,$06E00000,$06B80000,$06B00000,$06800000,$06C80000,$06D80000,
$05280000,$05400000,$05080000,$05680000,$05500000,$05180000,$05200000,$05100000,
$05700000,$05780000,$05600000,$05380000,$05300000,$05000000,$05480000,$05580000,
$00A80000,$00C00000,$00880000,$00E80000,$00D00000,$00980000,$00A00000,$00900000,
$00F00000,$00F80000,$00E00000,$00B80000,$00B00000,$00800000,$00C80000,$00D80000,
$00280000,$00400000,$00080000,$00680000,$00500000,$00180000,$00200000,$00100000,
$00700000,$00780000,$00600000,$00380000,$00300000,$00000000,$00480000,$00580000,
$04280000,$04400000,$04080000,$04680000,$04500000,$04180000,$04200000,$04100000,
$04700000,$04780000,$04600000,$04380000,$04300000,$04000000,$04480000,$04580000,
$04A80000,$04C00000,$04880000,$04E80000,$04D00000,$04980000,$04A00000,$04900000,
$04F00000,$04F80000,$04E00000,$04B80000,$04B00000,$04800000,$04C80000,$04D80000,
$07A80000,$07C00000,$07880000,$07E80000,$07D00000,$07980000,$07A00000,$07900000,
$07F00000,$07F80000,$07E00000,$07B80000,$07B00000,$07800000,$07C80000,$07D80000,
$07280000,$07400000,$07080000,$07680000,$07500000,$07180000,$07200000,$07100000,
$07700000,$07780000,$07600000,$07380000,$07300000,$07000000,$07480000,$07580000,
$02280000,$02400000,$02080000,$02680000,$02500000,$02180000,$02200000,$02100000,
$02700000,$02780000,$02600000,$02380000,$02300000,$02000000,$02480000,$02580000,
$03280000,$03400000,$03080000,$03680000,$03500000,$03180000,$03200000,$03100000,
$03700000,$03780000,$03600000,$03380000,$03300000,$03000000,$03480000,$03580000,
$06280000,$06400000,$06080000,$06680000,$06500000,$06180000,$06200000,$06100000,
$06700000,$06780000,$06600000,$06380000,$06300000,$06000000,$06480000,$06580000,
$05A80000,$05C00000,$05880000,$05E80000,$05D00000,$05980000,$05A00000,$05900000,
$05F00000,$05F80000,$05E00000,$05B80000,$05B00000,$05800000,$05C80000,$05D80000,
$01280000,$01400000,$01080000,$01680000,$01500000,$01180000,$01200000,$01100000,
$01700000,$01780000,$01600000,$01380000,$01300000,$01000000,$01480000,$01580000,
$02A80000,$02C00000,$02880000,$02E80000,$02D00000,$02980000,$02A00000,$02900000,
$02F00000,$02F80000,$02E00000,$02B80000,$02B00000,$02800000,$02C80000,$02D80000,
$01A80000,$01C00000,$01880000,$01E80000,$01D00000,$01980000,$01A00000,$01900000,
$01F00000,$01F80000,$01E00000,$01B80000,$01B00000,$01800000,$01C80000,$01D80000),
($30000002,$60000002,$38000002,$08000002,$28000002,$78000002,$68000002,$40000002,
$20000002,$50000002,$48000002,$70000002,$00000002,$18000002,$58000002,$10000002,
$B0000005,$E0000005,$B8000005,$88000005,$A8000005,$F8000005,$E8000005,$C0000005,
$A0000005,$D0000005,$C8000005,$F0000005,$80000005,$98000005,$D8000005,$90000005,
$30000005,$60000005,$38000005,$08000005,$28000005,$78000005,$68000005,$40000005,
$20000005,$50000005,$48000005,$70000005,$00000005,$18000005,$58000005,$10000005,
$30000000,$60000000,$38000000,$08000000,$28000000,$78000000,$68000000,$40000000,
$20000000,$50000000,$48000000,$70000000,$00000000,$18000000,$58000000,$10000000,
$B0000003,$E0000003,$B8000003,$88000003,$A8000003,$F8000003,$E8000003,$C0000003,
$A0000003,$D0000003,$C8000003,$F0000003,$80000003,$98000003,$D8000003,$90000003,
$30000001,$60000001,$38000001,$08000001,$28000001,$78000001,$68000001,$40000001,
$20000001,$50000001,$48000001,$70000001,$00000001,$18000001,$58000001,$10000001,
$B0000000,$E0000000,$B8000000,$88000000,$A8000000,$F8000000,$E8000000,$C0000000,
$A0000000,$D0000000,$C8000000,$F0000000,$80000000,$98000000,$D8000000,$90000000,
$B0000006,$E0000006,$B8000006,$88000006,$A8000006,$F8000006,$E8000006,$C0000006,
$A0000006,$D0000006,$C8000006,$F0000006,$80000006,$98000006,$D8000006,$90000006,
$B0000001,$E0000001,$B8000001,$88000001,$A8000001,$F8000001,$E8000001,$C0000001,
$A0000001,$D0000001,$C8000001,$F0000001,$80000001,$98000001,$D8000001,$90000001,
$30000003,$60000003,$38000003,$08000003,$28000003,$78000003,$68000003,$40000003,
$20000003,$50000003,$48000003,$70000003,$00000003,$18000003,$58000003,$10000003,
$30000004,$60000004,$38000004,$08000004,$28000004,$78000004,$68000004,$40000004,
$20000004,$50000004,$48000004,$70000004,$00000004,$18000004,$58000004,$10000004,
$B0000002,$E0000002,$B8000002,$88000002,$A8000002,$F8000002,$E8000002,$C0000002,
$A0000002,$D0000002,$C8000002,$F0000002,$80000002,$98000002,$D8000002,$90000002,
$B0000004,$E0000004,$B8000004,$88000004,$A8000004,$F8000004,$E8000004,$C0000004,
$A0000004,$D0000004,$C8000004,$F0000004,$80000004,$98000004,$D8000004,$90000004,
$30000006,$60000006,$38000006,$08000006,$28000006,$78000006,$68000006,$40000006,
$20000006,$50000006,$48000006,$70000006,$00000006,$18000006,$58000006,$10000006,
$B0000007,$E0000007,$B8000007,$88000007,$A8000007,$F8000007,$E8000007,$C0000007,
$A0000007,$D0000007,$C8000007,$F0000007,$80000007,$98000007,$D8000007,$90000007,
$30000007,$60000007,$38000007,$08000007,$28000007,$78000007,$68000007,$40000007,
$20000007,$50000007,$48000007,$70000007,$00000007,$18000007,$58000007,$10000007),
($000000E8,$000000D8,$000000A0,$00000088,$00000098,$000000F8,$000000A8,$000000C8,
$00000080,$000000D0,$000000F0,$000000B8,$000000B0,$000000C0,$00000090,$000000E0,
$000007E8,$000007D8,$000007A0,$00000788,$00000798,$000007F8,$000007A8,$000007C8,
$00000780,$000007D0,$000007F0,$000007B8,$000007B0,$000007C0,$00000790,$000007E0,
$000006E8,$000006D8,$000006A0,$00000688,$00000698,$000006F8,$000006A8,$000006C8,
$00000680,$000006D0,$000006F0,$000006B8,$000006B0,$000006C0,$00000690,$000006E0,
$00000068,$00000058,$00000020,$00000008,$00000018,$00000078,$00000028,$00000048,
$00000000,$00000050,$00000070,$00000038,$00000030,$00000040,$00000010,$00000060,
$000002E8,$000002D8,$000002A0,$00000288,$00000298,$000002F8,$000002A8,$000002C8,
$00000280,$000002D0,$000002F0,$000002B8,$000002B0,$000002C0,$00000290,$000002E0,
$000003E8,$000003D8,$000003A0,$00000388,$00000398,$000003F8,$000003A8,$000003C8,
$00000380,$000003D0,$000003F0,$000003B8,$000003B0,$000003C0,$00000390,$000003E0,
$00000568,$00000558,$00000520,$00000508,$00000518,$00000578,$00000528,$00000548,
$00000500,$00000550,$00000570,$00000538,$00000530,$00000540,$00000510,$00000560,
$00000268,$00000258,$00000220,$00000208,$00000218,$00000278,$00000228,$00000248,
$00000200,$00000250,$00000270,$00000238,$00000230,$00000240,$00000210,$00000260,
$000004E8,$000004D8,$000004A0,$00000488,$00000498,$000004F8,$000004A8,$000004C8,
$00000480,$000004D0,$000004F0,$000004B8,$000004B0,$000004C0,$00000490,$000004E0,
$00000168,$00000158,$00000120,$00000108,$00000118,$00000178,$00000128,$00000148,
$00000100,$00000150,$00000170,$00000138,$00000130,$00000140,$00000110,$00000160,
$000001E8,$000001D8,$000001A0,$00000188,$00000198,$000001F8,$000001A8,$000001C8,
$00000180,$000001D0,$000001F0,$000001B8,$000001B0,$000001C0,$00000190,$000001E0,
$00000768,$00000758,$00000720,$00000708,$00000718,$00000778,$00000728,$00000748,
$00000700,$00000750,$00000770,$00000738,$00000730,$00000740,$00000710,$00000760,
$00000368,$00000358,$00000320,$00000308,$00000318,$00000378,$00000328,$00000348,
$00000300,$00000350,$00000370,$00000338,$00000330,$00000340,$00000310,$00000360,
$000005E8,$000005D8,$000005A0,$00000588,$00000598,$000005F8,$000005A8,$000005C8,
$00000580,$000005D0,$000005F0,$000005B8,$000005B0,$000005C0,$00000590,$000005E0,
$00000468,$00000458,$00000420,$00000408,$00000418,$00000478,$00000428,$00000448,
$00000400,$00000450,$00000470,$00000438,$00000430,$00000440,$00000410,$00000460,
$00000668,$00000658,$00000620,$00000608,$00000618,$00000678,$00000628,$00000648,
$00000600,$00000650,$00000670,$00000638,$00000630,$00000640,$00000610,$00000660));
const
sTable: array[0..3, 0..255] of DWord= (
($00072000,$00075000,$00074800,$00071000,$00076800,$00074000,$00070000,$00077000,
$00073000,$00075800,$00070800,$00076000,$00073800,$00077800,$00072800,$00071800,
$0005A000,$0005D000,$0005C800,$00059000,$0005E800,$0005C000,$00058000,$0005F000,
$0005B000,$0005D800,$00058800,$0005E000,$0005B800,$0005F800,$0005A800,$00059800,
$00022000,$00025000,$00024800,$00021000,$00026800,$00024000,$00020000,$00027000,
$00023000,$00025800,$00020800,$00026000,$00023800,$00027800,$00022800,$00021800,
$00062000,$00065000,$00064800,$00061000,$00066800,$00064000,$00060000,$00067000,
$00063000,$00065800,$00060800,$00066000,$00063800,$00067800,$00062800,$00061800,
$00032000,$00035000,$00034800,$00031000,$00036800,$00034000,$00030000,$00037000,
$00033000,$00035800,$00030800,$00036000,$00033800,$00037800,$00032800,$00031800,
$0006A000,$0006D000,$0006C800,$00069000,$0006E800,$0006C000,$00068000,$0006F000,
$0006B000,$0006D800,$00068800,$0006E000,$0006B800,$0006F800,$0006A800,$00069800,
$0007A000,$0007D000,$0007C800,$00079000,$0007E800,$0007C000,$00078000,$0007F000,
$0007B000,$0007D800,$00078800,$0007E000,$0007B800,$0007F800,$0007A800,$00079800,
$00052000,$00055000,$00054800,$00051000,$00056800,$00054000,$00050000,$00057000,
$00053000,$00055800,$00050800,$00056000,$00053800,$00057800,$00052800,$00051800,
$00012000,$00015000,$00014800,$00011000,$00016800,$00014000,$00010000,$00017000,
$00013000,$00015800,$00010800,$00016000,$00013800,$00017800,$00012800,$00011800,
$0001A000,$0001D000,$0001C800,$00019000,$0001E800,$0001C000,$00018000,$0001F000,
$0001B000,$0001D800,$00018800,$0001E000,$0001B800,$0001F800,$0001A800,$00019800,
$00042000,$00045000,$00044800,$00041000,$00046800,$00044000,$00040000,$00047000,
$00043000,$00045800,$00040800,$00046000,$00043800,$00047800,$00042800,$00041800,
$0000A000,$0000D000,$0000C800,$00009000,$0000E800,$0000C000,$00008000,$0000F000,
$0000B000,$0000D800,$00008800,$0000E000,$0000B800,$0000F800,$0000A800,$00009800,
$00002000,$00005000,$00004800,$00001000,$00006800,$00004000,$00000000,$00007000,
$00003000,$00005800,$00000800,$00006000,$00003800,$00007800,$00002800,$00001800,
$0003A000,$0003D000,$0003C800,$00039000,$0003E800,$0003C000,$00038000,$0003F000,
$0003B000,$0003D800,$00038800,$0003E000,$0003B800,$0003F800,$0003A800,$00039800,
$0002A000,$0002D000,$0002C800,$00029000,$0002E800,$0002C000,$00028000,$0002F000,
$0002B000,$0002D800,$00028800,$0002E000,$0002B800,$0002F800,$0002A800,$00029800,
$0004A000,$0004D000,$0004C800,$00049000,$0004E800,$0004C000,$00048000,$0004F000,
$0004B000,$0004D800,$00048800,$0004E000,$0004B800,$0004F800,$0004A800,$00049800),
($03A80000,$03C00000,$03880000,$03E80000,$03D00000,$03980000,$03A00000,$03900000,
$03F00000,$03F80000,$03E00000,$03B80000,$03B00000,$03800000,$03C80000,$03D80000,
$06A80000,$06C00000,$06880000,$06E80000,$06D00000,$06980000,$06A00000,$06900000,
$06F00000,$06F80000,$06E00000,$06B80000,$06B00000,$06800000,$06C80000,$06D80000,
$05280000,$05400000,$05080000,$05680000,$05500000,$05180000,$05200000,$05100000,
$05700000,$05780000,$05600000,$05380000,$05300000,$05000000,$05480000,$05580000,
$00A80000,$00C00000,$00880000,$00E80000,$00D00000,$00980000,$00A00000,$00900000,
$00F00000,$00F80000,$00E00000,$00B80000,$00B00000,$00800000,$00C80000,$00D80000,
$00280000,$00400000,$00080000,$00680000,$00500000,$00180000,$00200000,$00100000,
$00700000,$00780000,$00600000,$00380000,$00300000,$00000000,$00480000,$00580000,
$04280000,$04400000,$04080000,$04680000,$04500000,$04180000,$04200000,$04100000,
$04700000,$04780000,$04600000,$04380000,$04300000,$04000000,$04480000,$04580000,
$04A80000,$04C00000,$04880000,$04E80000,$04D00000,$04980000,$04A00000,$04900000,
$04F00000,$04F80000,$04E00000,$04B80000,$04B00000,$04800000,$04C80000,$04D80000,
$07A80000,$07C00000,$07880000,$07E80000,$07D00000,$07980000,$07A00000,$07900000,
$07F00000,$07F80000,$07E00000,$07B80000,$07B00000,$07800000,$07C80000,$07D80000,
$07280000,$07400000,$07080000,$07680000,$07500000,$07180000,$07200000,$07100000,
$07700000,$07780000,$07600000,$07380000,$07300000,$07000000,$07480000,$07580000,
$02280000,$02400000,$02080000,$02680000,$02500000,$02180000,$02200000,$02100000,
$02700000,$02780000,$02600000,$02380000,$02300000,$02000000,$02480000,$02580000,
$03280000,$03400000,$03080000,$03680000,$03500000,$03180000,$03200000,$03100000,
$03700000,$03780000,$03600000,$03380000,$03300000,$03000000,$03480000,$03580000,
$06280000,$06400000,$06080000,$06680000,$06500000,$06180000,$06200000,$06100000,
$06700000,$06780000,$06600000,$06380000,$06300000,$06000000,$06480000,$06580000,
$05A80000,$05C00000,$05880000,$05E80000,$05D00000,$05980000,$05A00000,$05900000,
$05F00000,$05F80000,$05E00000,$05B80000,$05B00000,$05800000,$05C80000,$05D80000,
$01280000,$01400000,$01080000,$01680000,$01500000,$01180000,$01200000,$01100000,
$01700000,$01780000,$01600000,$01380000,$01300000,$01000000,$01480000,$01580000,
$02A80000,$02C00000,$02880000,$02E80000,$02D00000,$02980000,$02A00000,$02900000,
$02F00000,$02F80000,$02E00000,$02B80000,$02B00000,$02800000,$02C80000,$02D80000,
$01A80000,$01C00000,$01880000,$01E80000,$01D00000,$01980000,$01A00000,$01900000,
$01F00000,$01F80000,$01E00000,$01B80000,$01B00000,$01800000,$01C80000,$01D80000),
($30000002,$60000002,$38000002,$08000002,$28000002,$78000002,$68000002,$40000002,
$20000002,$50000002,$48000002,$70000002,$00000002,$18000002,$58000002,$10000002,
$B0000005,$E0000005,$B8000005,$88000005,$A8000005,$F8000005,$E8000005,$C0000005,
$A0000005,$D0000005,$C8000005,$F0000005,$80000005,$98000005,$D8000005,$90000005,
$30000005,$60000005,$38000005,$08000005,$28000005,$78000005,$68000005,$40000005,
$20000005,$50000005,$48000005,$70000005,$00000005,$18000005,$58000005,$10000005,
$30000000,$60000000,$38000000,$08000000,$28000000,$78000000,$68000000,$40000000,
$20000000,$50000000,$48000000,$70000000,$00000000,$18000000,$58000000,$10000000,
$B0000003,$E0000003,$B8000003,$88000003,$A8000003,$F8000003,$E8000003,$C0000003,
$A0000003,$D0000003,$C8000003,$F0000003,$80000003,$98000003,$D8000003,$90000003,
$30000001,$60000001,$38000001,$08000001,$28000001,$78000001,$68000001,$40000001,
$20000001,$50000001,$48000001,$70000001,$00000001,$18000001,$58000001,$10000001,
$B0000000,$E0000000,$B8000000,$88000000,$A8000000,$F8000000,$E8000000,$C0000000,
$A0000000,$D0000000,$C8000000,$F0000000,$80000000,$98000000,$D8000000,$90000000,
$B0000006,$E0000006,$B8000006,$88000006,$A8000006,$F8000006,$E8000006,$C0000006,
$A0000006,$D0000006,$C8000006,$F0000006,$80000006,$98000006,$D8000006,$90000006,
$B0000001,$E0000001,$B8000001,$88000001,$A8000001,$F8000001,$E8000001,$C0000001,
$A0000001,$D0000001,$C8000001,$F0000001,$80000001,$98000001,$D8000001,$90000001,
$30000003,$60000003,$38000003,$08000003,$28000003,$78000003,$68000003,$40000003,
$20000003,$50000003,$48000003,$70000003,$00000003,$18000003,$58000003,$10000003,
$30000004,$60000004,$38000004,$08000004,$28000004,$78000004,$68000004,$40000004,
$20000004,$50000004,$48000004,$70000004,$00000004,$18000004,$58000004,$10000004,
$B0000002,$E0000002,$B8000002,$88000002,$A8000002,$F8000002,$E8000002,$C0000002,
$A0000002,$D0000002,$C8000002,$F0000002,$80000002,$98000002,$D8000002,$90000002,
$B0000004,$E0000004,$B8000004,$88000004,$A8000004,$F8000004,$E8000004,$C0000004,
$A0000004,$D0000004,$C8000004,$F0000004,$80000004,$98000004,$D8000004,$90000004,
$30000006,$60000006,$38000006,$08000006,$28000006,$78000006,$68000006,$40000006,
$20000006,$50000006,$48000006,$70000006,$00000006,$18000006,$58000006,$10000006,
$B0000007,$E0000007,$B8000007,$88000007,$A8000007,$F8000007,$E8000007,$C0000007,
$A0000007,$D0000007,$C8000007,$F0000007,$80000007,$98000007,$D8000007,$90000007,
$30000007,$60000007,$38000007,$08000007,$28000007,$78000007,$68000007,$40000007,
$20000007,$50000007,$48000007,$70000007,$00000007,$18000007,$58000007,$10000007),
($000000E8,$000000D8,$000000A0,$00000088,$00000098,$000000F8,$000000A8,$000000C8,
$00000080,$000000D0,$000000F0,$000000B8,$000000B0,$000000C0,$00000090,$000000E0,
$000007E8,$000007D8,$000007A0,$00000788,$00000798,$000007F8,$000007A8,$000007C8,
$00000780,$000007D0,$000007F0,$000007B8,$000007B0,$000007C0,$00000790,$000007E0,
$000006E8,$000006D8,$000006A0,$00000688,$00000698,$000006F8,$000006A8,$000006C8,
$00000680,$000006D0,$000006F0,$000006B8,$000006B0,$000006C0,$00000690,$000006E0,
$00000068,$00000058,$00000020,$00000008,$00000018,$00000078,$00000028,$00000048,
$00000000,$00000050,$00000070,$00000038,$00000030,$00000040,$00000010,$00000060,
$000002E8,$000002D8,$000002A0,$00000288,$00000298,$000002F8,$000002A8,$000002C8,
$00000280,$000002D0,$000002F0,$000002B8,$000002B0,$000002C0,$00000290,$000002E0,
$000003E8,$000003D8,$000003A0,$00000388,$00000398,$000003F8,$000003A8,$000003C8,
$00000380,$000003D0,$000003F0,$000003B8,$000003B0,$000003C0,$00000390,$000003E0,
$00000568,$00000558,$00000520,$00000508,$00000518,$00000578,$00000528,$00000548,
$00000500,$00000550,$00000570,$00000538,$00000530,$00000540,$00000510,$00000560,
$00000268,$00000258,$00000220,$00000208,$00000218,$00000278,$00000228,$00000248,
$00000200,$00000250,$00000270,$00000238,$00000230,$00000240,$00000210,$00000260,
$000004E8,$000004D8,$000004A0,$00000488,$00000498,$000004F8,$000004A8,$000004C8,
$00000480,$000004D0,$000004F0,$000004B8,$000004B0,$000004C0,$00000490,$000004E0,
$00000168,$00000158,$00000120,$00000108,$00000118,$00000178,$00000128,$00000148,
$00000100,$00000150,$00000170,$00000138,$00000130,$00000140,$00000110,$00000160,
$000001E8,$000001D8,$000001A0,$00000188,$00000198,$000001F8,$000001A8,$000001C8,
$00000180,$000001D0,$000001F0,$000001B8,$000001B0,$000001C0,$00000190,$000001E0,
$00000768,$00000758,$00000720,$00000708,$00000718,$00000778,$00000728,$00000748,
$00000700,$00000750,$00000770,$00000738,$00000730,$00000740,$00000710,$00000760,
$00000368,$00000358,$00000320,$00000308,$00000318,$00000378,$00000328,$00000348,
$00000300,$00000350,$00000370,$00000338,$00000330,$00000340,$00000310,$00000360,
$000005E8,$000005D8,$000005A0,$00000588,$00000598,$000005F8,$000005A8,$000005C8,
$00000580,$000005D0,$000005F0,$000005B8,$000005B0,$000005C0,$00000590,$000005E0,
$00000468,$00000458,$00000420,$00000408,$00000418,$00000478,$00000428,$00000448,
$00000400,$00000450,$00000470,$00000438,$00000430,$00000440,$00000410,$00000460,
$00000668,$00000658,$00000620,$00000608,$00000618,$00000678,$00000628,$00000648,
$00000600,$00000650,$00000670,$00000638,$00000630,$00000640,$00000610,$00000660));

View File

@ -1,134 +1,134 @@
const
S_Box: array[0..511] of DWord= (
$09d0c479, $28c8ffe0, $84aa6c39, $9dad7287,
$7dff9be3, $d4268361, $c96da1d4, $7974cc93,
$85d0582e, $2a4b5705, $1ca16a62, $c3bd279d,
$0f1f25e5, $5160372f, $c695c1fb, $4d7ff1e4,
$ae5f6bf4, $0d72ee46, $ff23de8a, $b1cf8e83,
$f14902e2, $3e981e42, $8bf53eb6, $7f4bf8ac,
$83631f83, $25970205, $76afe784, $3a7931d4,
$4f846450, $5c64c3f6, $210a5f18, $c6986a26,
$28f4e826, $3a60a81c, $d340a664, $7ea820c4,
$526687c5, $7eddd12b, $32a11d1d, $9c9ef086,
$80f6e831, $ab6f04ad, $56fb9b53, $8b2e095c,
$b68556ae, $d2250b0d, $294a7721, $e21fb253,
$ae136749, $e82aae86, $93365104, $99404a66,
$78a784dc, $b69ba84b, $04046793, $23db5c1e,
$46cae1d6, $2fe28134, $5a223942, $1863cd5b,
$c190c6e3, $07dfb846, $6eb88816, $2d0dcc4a,
$a4ccae59, $3798670d, $cbfa9493, $4f481d45,
$eafc8ca8, $db1129d6, $b0449e20, $0f5407fb,
$6167d9a8, $d1f45763, $4daa96c3, $3bec5958,
$ababa014, $b6ccd201, $38d6279f, $02682215,
$8f376cd5, $092c237e, $bfc56593, $32889d2c,
$854b3e95, $05bb9b43, $7dcd5dcd, $a02e926c,
$fae527e5, $36a1c330, $3412e1ae, $f257f462,
$3c4f1d71, $30a2e809, $68e5f551, $9c61ba44,
$5ded0ab8, $75ce09c8, $9654f93e, $698c0cca,
$243cb3e4, $2b062b97, $0f3b8d9e, $00e050df,
$fc5d6166, $e35f9288, $c079550d, $0591aee8,
$8e531e74, $75fe3578, $2f6d829a, $f60b21ae,
$95e8eb8d, $6699486b, $901d7d9b, $fd6d6e31,
$1090acef, $e0670dd8, $dab2e692, $cd6d4365,
$e5393514, $3af345f0, $6241fc4d, $460da3a3,
$7bcf3729, $8bf1d1e0, $14aac070, $1587ed55,
$3afd7d3e, $d2f29e01, $29a9d1f6, $efb10c53,
$cf3b870f, $b414935c, $664465ed, $024acac7,
$59a744c1, $1d2936a7, $dc580aa6, $cf574ca8,
$040a7a10, $6cd81807, $8a98be4c, $accea063,
$c33e92b5, $d1e0e03d, $b322517e, $2092bd13,
$386b2c4a, $52e8dd58, $58656dfb, $50820371,
$41811896, $e337ef7e, $d39fb119, $c97f0df6,
$68fea01b, $a150a6e5, $55258962, $eb6ff41b,
$d7c9cd7a, $a619cd9e, $bcf09576, $2672c073,
$f003fb3c, $4ab7a50b, $1484126a, $487ba9b1,
$a64fc9c6, $f6957d49, $38b06a75, $dd805fcd,
$63d094cf, $f51c999e, $1aa4d343, $b8495294,
$ce9f8e99, $bffcd770, $c7c275cc, $378453a7,
$7b21be33, $397f41bd, $4e94d131, $92cc1f98,
$5915ea51, $99f861b7, $c9980a88, $1d74fd5f,
$b0a495f8, $614deed0, $b5778eea, $5941792d,
$fa90c1f8, $33f824b4, $c4965372, $3ff6d550,
$4ca5fec0, $8630e964, $5b3fbbd6, $7da26a48,
$b203231a, $04297514, $2d639306, $2eb13149,
$16a45272, $532459a0, $8e5f4872, $f966c7d9,
$07128dc0, $0d44db62, $afc8d52d, $06316131,
$d838e7ce, $1bc41d00, $3a2e8c0f, $ea83837e,
$b984737d, $13ba4891, $c4f8b949, $a6d6acb3,
$a215cdce, $8359838b, $6bd1aa31, $f579dd52,
$21b93f93, $f5176781, $187dfdde, $e94aeb76,
$2b38fd54, $431de1da, $ab394825, $9ad3048f,
$dfea32aa, $659473e3, $623f7863, $f3346c59,
$ab3ab685, $3346a90b, $6b56443e, $c6de01f8,
$8d421fc0, $9b0ed10c, $88f1a1e9, $54c1f029,
$7dead57b, $8d7ba426, $4cf5178a, $551a7cca,
$1a9a5f08, $fcd651b9, $25605182, $e11fc6c3,
$b6fd9676, $337b3027, $b7c8eb14, $9e5fd030,
$6b57e354, $ad913cf7, $7e16688d, $58872a69,
$2c2fc7df, $e389ccc6, $30738df1, $0824a734,
$e1797a8b, $a4a8d57b, $5b5d193b, $c8a8309b,
$73f9a978, $73398d32, $0f59573e, $e9df2b03,
$e8a5b6c8, $848d0704, $98df93c2, $720a1dc3,
$684f259a, $943ba848, $a6370152, $863b5ea3,
$d17b978b, $6d9b58ef, $0a700dd4, $a73d36bf,
$8e6a0829, $8695bc14, $e35b3447, $933ac568,
$8894b022, $2f511c27, $ddfbcc3c, $006662b6,
$117c83fe, $4e12b414, $c2bca766, $3a2fec10,
$f4562420, $55792e2a, $46f5d857, $ceda25ce,
$c3601d3b, $6c00ab46, $efac9c28, $b3c35047,
$611dfee3, $257c3207, $fdd58482, $3b14d84f,
$23becb64, $a075f3a3, $088f8ead, $07adf158,
$7796943c, $facabf3d, $c09730cd, $f7679969,
$da44e9ed, $2c854c12, $35935fa3, $2f057d9f,
$690624f8, $1cb0bafd, $7b0dbdc6, $810f23bb,
$fa929a1a, $6d969a17, $6742979b, $74ac7d05,
$010e65c4, $86a3d963, $f907b5a0, $d0042bd3,
$158d7d03, $287a8255, $bba8366f, $096edc33,
$21916a7b, $77b56b86, $951622f9, $a6c5e650,
$8cea17d1, $cd8c62bc, $a3d63433, $358a68fd,
$0f9b9d3c, $d6aa295b, $fe33384a, $c000738e,
$cd67eb2f, $e2eb6dc2, $97338b02, $06c9f246,
$419cf1ad, $2b83c045, $3723f18a, $cb5b3089,
$160bead7, $5d494656, $35f8a74b, $1e4e6c9e,
$000399bd, $67466880, $b4174831, $acf423b2,
$ca815ab3, $5a6395e7, $302a67c5, $8bdb446b,
$108f8fa4, $10223eda, $92b8b48b, $7f38d0ee,
$ab2701d4, $0262d415, $af224a30, $b3d88aba,
$f8b2c3af, $daf7ef70, $cc97d3b7, $e9614b6c,
$2baebff4, $70f687cf, $386c9156, $ce092ee5,
$01e87da6, $6ce91e6a, $bb7bcc84, $c7922c20,
$9d3b71fd, $060e41c6, $d7590f15, $4e03bb47,
$183c198e, $63eeb240, $2ddbf49a, $6d5cba54,
$923750af, $f9e14236, $7838162b, $59726c72,
$81b66760, $bb2926c1, $48a0ce0d, $a6c0496d,
$ad43507b, $718d496a, $9df057af, $44b1bde6,
$054356dc, $de7ced35, $d51a138b, $62088cc9,
$35830311, $c96efca2, $686f86ec, $8e77cb68,
$63e1d6b8, $c80f9778, $79c491fd, $1b4c67f2,
$72698d7d, $5e368c31, $f7d95e2e, $a1d3493f,
$dcd9433e, $896f1552, $4bc4ca7a, $a6d1baf4,
$a5a96dcc, $0bef8b46, $a169fda7, $74df40b7,
$4e208804, $9a756607, $038e87c8, $20211e44,
$8b7ad4bf, $c6403f35, $1848e36d, $80bdb038,
$1e62891c, $643d2107, $bf04d6f8, $21092c8c,
$f644f389, $0778404e, $7b78adb8, $a2c52d53,
$42157abe, $a2253e2e, $7bf3f4ae, $80f594f9,
$953194e7, $77eb92ed, $b3816930, $da8d9336,
$bf447469, $f26d9483, $ee6faed5, $71371235,
$de425f73, $b4e59f43, $7dbe2d4e, $2d37b185,
$49dc9a63, $98c39d98, $1301c9a2, $389b1bbf,
$0c18588d, $a421c1ba, $7aa3865c, $71e08558,
$3c5cfcaa, $7d239ca4, $0297d9dd, $d7dc2830,
$4b37802b, $7428ab54, $aeee0347, $4b3fbb85,
$692f2f08, $134e578e, $36d9e0bf, $ae8b5fcf,
$edb93ecf, $2b27248e, $170eb1ef, $7dc57fd6,
$1e760f16, $b1136601, $864e1b9b, $d7ea7319,
$3ab871bd, $cfa4d76f, $e31bd782, $0dbeb469,
$abb96061, $5370f85d, $ffb07e37, $da30d0fb,
$ebc977b6, $0b98b40f, $3a4d0fe6, $df4fc26b,
$159cf22a, $c298d6e2, $2b78ef6a, $61a94ac0,
$ab561187, $14eea0f0, $df0d4164, $19af70ee);
vk: array[0..6] of DWord= (
$09d0c479, $28c8ffe0, $84aa6c39, $9dad7287, $7dff9be3, $d4268361,
$c96da1d4);
const
S_Box: array[0..511] of DWord= (
$09d0c479, $28c8ffe0, $84aa6c39, $9dad7287,
$7dff9be3, $d4268361, $c96da1d4, $7974cc93,
$85d0582e, $2a4b5705, $1ca16a62, $c3bd279d,
$0f1f25e5, $5160372f, $c695c1fb, $4d7ff1e4,
$ae5f6bf4, $0d72ee46, $ff23de8a, $b1cf8e83,
$f14902e2, $3e981e42, $8bf53eb6, $7f4bf8ac,
$83631f83, $25970205, $76afe784, $3a7931d4,
$4f846450, $5c64c3f6, $210a5f18, $c6986a26,
$28f4e826, $3a60a81c, $d340a664, $7ea820c4,
$526687c5, $7eddd12b, $32a11d1d, $9c9ef086,
$80f6e831, $ab6f04ad, $56fb9b53, $8b2e095c,
$b68556ae, $d2250b0d, $294a7721, $e21fb253,
$ae136749, $e82aae86, $93365104, $99404a66,
$78a784dc, $b69ba84b, $04046793, $23db5c1e,
$46cae1d6, $2fe28134, $5a223942, $1863cd5b,
$c190c6e3, $07dfb846, $6eb88816, $2d0dcc4a,
$a4ccae59, $3798670d, $cbfa9493, $4f481d45,
$eafc8ca8, $db1129d6, $b0449e20, $0f5407fb,
$6167d9a8, $d1f45763, $4daa96c3, $3bec5958,
$ababa014, $b6ccd201, $38d6279f, $02682215,
$8f376cd5, $092c237e, $bfc56593, $32889d2c,
$854b3e95, $05bb9b43, $7dcd5dcd, $a02e926c,
$fae527e5, $36a1c330, $3412e1ae, $f257f462,
$3c4f1d71, $30a2e809, $68e5f551, $9c61ba44,
$5ded0ab8, $75ce09c8, $9654f93e, $698c0cca,
$243cb3e4, $2b062b97, $0f3b8d9e, $00e050df,
$fc5d6166, $e35f9288, $c079550d, $0591aee8,
$8e531e74, $75fe3578, $2f6d829a, $f60b21ae,
$95e8eb8d, $6699486b, $901d7d9b, $fd6d6e31,
$1090acef, $e0670dd8, $dab2e692, $cd6d4365,
$e5393514, $3af345f0, $6241fc4d, $460da3a3,
$7bcf3729, $8bf1d1e0, $14aac070, $1587ed55,
$3afd7d3e, $d2f29e01, $29a9d1f6, $efb10c53,
$cf3b870f, $b414935c, $664465ed, $024acac7,
$59a744c1, $1d2936a7, $dc580aa6, $cf574ca8,
$040a7a10, $6cd81807, $8a98be4c, $accea063,
$c33e92b5, $d1e0e03d, $b322517e, $2092bd13,
$386b2c4a, $52e8dd58, $58656dfb, $50820371,
$41811896, $e337ef7e, $d39fb119, $c97f0df6,
$68fea01b, $a150a6e5, $55258962, $eb6ff41b,
$d7c9cd7a, $a619cd9e, $bcf09576, $2672c073,
$f003fb3c, $4ab7a50b, $1484126a, $487ba9b1,
$a64fc9c6, $f6957d49, $38b06a75, $dd805fcd,
$63d094cf, $f51c999e, $1aa4d343, $b8495294,
$ce9f8e99, $bffcd770, $c7c275cc, $378453a7,
$7b21be33, $397f41bd, $4e94d131, $92cc1f98,
$5915ea51, $99f861b7, $c9980a88, $1d74fd5f,
$b0a495f8, $614deed0, $b5778eea, $5941792d,
$fa90c1f8, $33f824b4, $c4965372, $3ff6d550,
$4ca5fec0, $8630e964, $5b3fbbd6, $7da26a48,
$b203231a, $04297514, $2d639306, $2eb13149,
$16a45272, $532459a0, $8e5f4872, $f966c7d9,
$07128dc0, $0d44db62, $afc8d52d, $06316131,
$d838e7ce, $1bc41d00, $3a2e8c0f, $ea83837e,
$b984737d, $13ba4891, $c4f8b949, $a6d6acb3,
$a215cdce, $8359838b, $6bd1aa31, $f579dd52,
$21b93f93, $f5176781, $187dfdde, $e94aeb76,
$2b38fd54, $431de1da, $ab394825, $9ad3048f,
$dfea32aa, $659473e3, $623f7863, $f3346c59,
$ab3ab685, $3346a90b, $6b56443e, $c6de01f8,
$8d421fc0, $9b0ed10c, $88f1a1e9, $54c1f029,
$7dead57b, $8d7ba426, $4cf5178a, $551a7cca,
$1a9a5f08, $fcd651b9, $25605182, $e11fc6c3,
$b6fd9676, $337b3027, $b7c8eb14, $9e5fd030,
$6b57e354, $ad913cf7, $7e16688d, $58872a69,
$2c2fc7df, $e389ccc6, $30738df1, $0824a734,
$e1797a8b, $a4a8d57b, $5b5d193b, $c8a8309b,
$73f9a978, $73398d32, $0f59573e, $e9df2b03,
$e8a5b6c8, $848d0704, $98df93c2, $720a1dc3,
$684f259a, $943ba848, $a6370152, $863b5ea3,
$d17b978b, $6d9b58ef, $0a700dd4, $a73d36bf,
$8e6a0829, $8695bc14, $e35b3447, $933ac568,
$8894b022, $2f511c27, $ddfbcc3c, $006662b6,
$117c83fe, $4e12b414, $c2bca766, $3a2fec10,
$f4562420, $55792e2a, $46f5d857, $ceda25ce,
$c3601d3b, $6c00ab46, $efac9c28, $b3c35047,
$611dfee3, $257c3207, $fdd58482, $3b14d84f,
$23becb64, $a075f3a3, $088f8ead, $07adf158,
$7796943c, $facabf3d, $c09730cd, $f7679969,
$da44e9ed, $2c854c12, $35935fa3, $2f057d9f,
$690624f8, $1cb0bafd, $7b0dbdc6, $810f23bb,
$fa929a1a, $6d969a17, $6742979b, $74ac7d05,
$010e65c4, $86a3d963, $f907b5a0, $d0042bd3,
$158d7d03, $287a8255, $bba8366f, $096edc33,
$21916a7b, $77b56b86, $951622f9, $a6c5e650,
$8cea17d1, $cd8c62bc, $a3d63433, $358a68fd,
$0f9b9d3c, $d6aa295b, $fe33384a, $c000738e,
$cd67eb2f, $e2eb6dc2, $97338b02, $06c9f246,
$419cf1ad, $2b83c045, $3723f18a, $cb5b3089,
$160bead7, $5d494656, $35f8a74b, $1e4e6c9e,
$000399bd, $67466880, $b4174831, $acf423b2,
$ca815ab3, $5a6395e7, $302a67c5, $8bdb446b,
$108f8fa4, $10223eda, $92b8b48b, $7f38d0ee,
$ab2701d4, $0262d415, $af224a30, $b3d88aba,
$f8b2c3af, $daf7ef70, $cc97d3b7, $e9614b6c,
$2baebff4, $70f687cf, $386c9156, $ce092ee5,
$01e87da6, $6ce91e6a, $bb7bcc84, $c7922c20,
$9d3b71fd, $060e41c6, $d7590f15, $4e03bb47,
$183c198e, $63eeb240, $2ddbf49a, $6d5cba54,
$923750af, $f9e14236, $7838162b, $59726c72,
$81b66760, $bb2926c1, $48a0ce0d, $a6c0496d,
$ad43507b, $718d496a, $9df057af, $44b1bde6,
$054356dc, $de7ced35, $d51a138b, $62088cc9,
$35830311, $c96efca2, $686f86ec, $8e77cb68,
$63e1d6b8, $c80f9778, $79c491fd, $1b4c67f2,
$72698d7d, $5e368c31, $f7d95e2e, $a1d3493f,
$dcd9433e, $896f1552, $4bc4ca7a, $a6d1baf4,
$a5a96dcc, $0bef8b46, $a169fda7, $74df40b7,
$4e208804, $9a756607, $038e87c8, $20211e44,
$8b7ad4bf, $c6403f35, $1848e36d, $80bdb038,
$1e62891c, $643d2107, $bf04d6f8, $21092c8c,
$f644f389, $0778404e, $7b78adb8, $a2c52d53,
$42157abe, $a2253e2e, $7bf3f4ae, $80f594f9,
$953194e7, $77eb92ed, $b3816930, $da8d9336,
$bf447469, $f26d9483, $ee6faed5, $71371235,
$de425f73, $b4e59f43, $7dbe2d4e, $2d37b185,
$49dc9a63, $98c39d98, $1301c9a2, $389b1bbf,
$0c18588d, $a421c1ba, $7aa3865c, $71e08558,
$3c5cfcaa, $7d239ca4, $0297d9dd, $d7dc2830,
$4b37802b, $7428ab54, $aeee0347, $4b3fbb85,
$692f2f08, $134e578e, $36d9e0bf, $ae8b5fcf,
$edb93ecf, $2b27248e, $170eb1ef, $7dc57fd6,
$1e760f16, $b1136601, $864e1b9b, $d7ea7319,
$3ab871bd, $cfa4d76f, $e31bd782, $0dbeb469,
$abb96061, $5370f85d, $ffb07e37, $da30d0fb,
$ebc977b6, $0b98b40f, $3a4d0fe6, $df4fc26b,
$159cf22a, $c298d6e2, $2b78ef6a, $61a94ac0,
$ab561187, $14eea0f0, $df0d4164, $19af70ee);
vk: array[0..6] of DWord= (
$09d0c479, $28c8ffe0, $84aa6c39, $9dad7287, $7dff9be3, $d4268361,
$c96da1d4);

View File

@ -1,43 +1,43 @@
const
S7TABLE: array[0..$7F] of byte= (
$1b, $32, $33, $5a, $3b, $10, $17, $54, $5b, $1a, $72, $73, $6b, $2c, $66, $49,
$1f, $24, $13, $6c, $37, $2e, $3f, $4a, $5d, $0f, $40, $56, $25, $51, $1c, $04,
$0b, $46, $20, $0d, $7b, $35, $44, $42, $2b, $1e, $41, $14, $4b, $79, $15, $6f,
$0e, $55, $09, $36, $74, $0c, $67, $53, $28, $0a, $7e, $38, $02, $07, $60, $29,
$19, $12, $65, $2f, $30, $39, $08, $68, $5f, $78, $2a, $4c, $64, $45, $75, $3d,
$59, $48, $03, $57, $7c, $4f, $62, $3c, $1d, $21, $5e, $27, $6a, $70, $4d, $3a,
$01, $6d, $6e, $63, $18, $77, $23, $05, $26, $76, $00, $31, $2d, $7a, $7f, $61,
$50, $22, $11, $06, $47, $16, $52, $4e, $71, $3e, $69, $43, $34, $5c, $58, $7d);
S9TABLE: array[0..$1FF] of Dword= (
$1c3, $0cb, $153, $19f, $1e3, $0e9, $0fb, $035, $181, $0b9, $117, $1eb, $133, $009, $02d, $0d3,
$0c7, $14a, $037, $07e, $0eb, $164, $193, $1d8, $0a3, $11e, $055, $02c, $01d, $1a2, $163, $118,
$14b, $152, $1d2, $00f, $02b, $030, $13a, $0e5, $111, $138, $18e, $063, $0e3, $0c8, $1f4, $01b,
$001, $09d, $0f8, $1a0, $16d, $1f3, $01c, $146, $07d, $0d1, $082, $1ea, $183, $12d, $0f4, $19e,
$1d3, $0dd, $1e2, $128, $1e0, $0ec, $059, $091, $011, $12f, $026, $0dc, $0b0, $18c, $10f, $1f7,
$0e7, $16c, $0b6, $0f9, $0d8, $151, $101, $14c, $103, $0b8, $154, $12b, $1ae, $017, $071, $00c,
$047, $058, $07f, $1a4, $134, $129, $084, $15d, $19d, $1b2, $1a3, $048, $07c, $051, $1ca, $023,
$13d, $1a7, $165, $03b, $042, $0da, $192, $0ce, $0c1, $06b, $09f, $1f1, $12c, $184, $0fa, $196,
$1e1, $169, $17d, $031, $180, $10a, $094, $1da, $186, $13e, $11c, $060, $175, $1cf, $067, $119,
$065, $068, $099, $150, $008, $007, $17c, $0b7, $024, $019, $0de, $127, $0db, $0e4, $1a9, $052,
$109, $090, $19c, $1c1, $028, $1b3, $135, $16a, $176, $0df, $1e5, $188, $0c5, $16e, $1de, $1b1,
$0c3, $1df, $036, $0ee, $1ee, $0f0, $093, $049, $09a, $1b6, $069, $081, $125, $00b, $05e, $0b4,
$149, $1c7, $174, $03e, $13b, $1b7, $08e, $1c6, $0ae, $010, $095, $1ef, $04e, $0f2, $1fd, $085,
$0fd, $0f6, $0a0, $16f, $083, $08a, $156, $09b, $13c, $107, $167, $098, $1d0, $1e9, $003, $1fe,
$0bd, $122, $089, $0d2, $18f, $012, $033, $06a, $142, $0ed, $170, $11b, $0e2, $14f, $158, $131,
$147, $05d, $113, $1cd, $079, $161, $1a5, $179, $09e, $1b4, $0cc, $022, $132, $01a, $0e8, $004,
$187, $1ed, $197, $039, $1bf, $1d7, $027, $18b, $0c6, $09c, $0d0, $14e, $06c, $034, $1f2, $06e,
$0ca, $025, $0ba, $191, $0fe, $013, $106, $02f, $1ad, $172, $1db, $0c0, $10b, $1d6, $0f5, $1ec,
$10d, $076, $114, $1ab, $075, $10c, $1e4, $159, $054, $11f, $04b, $0c4, $1be, $0f7, $029, $0a4,
$00e, $1f0, $077, $04d, $17a, $086, $08b, $0b3, $171, $0bf, $10e, $104, $097, $15b, $160, $168,
$0d7, $0bb, $066, $1ce, $0fc, $092, $1c5, $06f, $016, $04a, $0a1, $139, $0af, $0f1, $190, $00a,
$1aa, $143, $17b, $056, $18d, $166, $0d4, $1fb, $14d, $194, $19a, $087, $1f8, $123, $0a7, $1b8,
$141, $03c, $1f9, $140, $02a, $155, $11a, $1a1, $198, $0d5, $126, $1af, $061, $12e, $157, $1dc,
$072, $18a, $0aa, $096, $115, $0ef, $045, $07b, $08d, $145, $053, $05f, $178, $0b2, $02e, $020,
$1d5, $03f, $1c9, $1e7, $1ac, $044, $038, $014, $0b1, $16b, $0ab, $0b5, $05a, $182, $1c8, $1d4,
$018, $177, $064, $0cf, $06d, $100, $199, $130, $15a, $005, $120, $1bb, $1bd, $0e0, $04f, $0d6,
$13f, $1c4, $12a, $015, $006, $0ff, $19b, $0a6, $043, $088, $050, $15f, $1e8, $121, $073, $17e,
$0bc, $0c2, $0c9, $173, $189, $1f5, $074, $1cc, $1e6, $1a8, $195, $01f, $041, $00d, $1ba, $032,
$03d, $1d1, $080, $0a8, $057, $1b9, $162, $148, $0d9, $105, $062, $07a, $021, $1ff, $112, $108,
$1c0, $0a9, $11d, $1b0, $1a6, $0cd, $0f3, $05c, $102, $05b, $1d9, $144, $1f6, $0ad, $0a5, $03a,
$1cb, $136, $17f, $046, $0e1, $01e, $1dd, $0e6, $137, $1fa, $185, $08c, $08f, $040, $1b5, $0be,
$078, $000, $0ac, $110, $15e, $124, $002, $1bc, $0a2, $0ea, $070, $1fc, $116, $15c, $04c, $1c2);
const
S7TABLE: array[0..$7F] of byte= (
$1b, $32, $33, $5a, $3b, $10, $17, $54, $5b, $1a, $72, $73, $6b, $2c, $66, $49,
$1f, $24, $13, $6c, $37, $2e, $3f, $4a, $5d, $0f, $40, $56, $25, $51, $1c, $04,
$0b, $46, $20, $0d, $7b, $35, $44, $42, $2b, $1e, $41, $14, $4b, $79, $15, $6f,
$0e, $55, $09, $36, $74, $0c, $67, $53, $28, $0a, $7e, $38, $02, $07, $60, $29,
$19, $12, $65, $2f, $30, $39, $08, $68, $5f, $78, $2a, $4c, $64, $45, $75, $3d,
$59, $48, $03, $57, $7c, $4f, $62, $3c, $1d, $21, $5e, $27, $6a, $70, $4d, $3a,
$01, $6d, $6e, $63, $18, $77, $23, $05, $26, $76, $00, $31, $2d, $7a, $7f, $61,
$50, $22, $11, $06, $47, $16, $52, $4e, $71, $3e, $69, $43, $34, $5c, $58, $7d);
S9TABLE: array[0..$1FF] of Dword= (
$1c3, $0cb, $153, $19f, $1e3, $0e9, $0fb, $035, $181, $0b9, $117, $1eb, $133, $009, $02d, $0d3,
$0c7, $14a, $037, $07e, $0eb, $164, $193, $1d8, $0a3, $11e, $055, $02c, $01d, $1a2, $163, $118,
$14b, $152, $1d2, $00f, $02b, $030, $13a, $0e5, $111, $138, $18e, $063, $0e3, $0c8, $1f4, $01b,
$001, $09d, $0f8, $1a0, $16d, $1f3, $01c, $146, $07d, $0d1, $082, $1ea, $183, $12d, $0f4, $19e,
$1d3, $0dd, $1e2, $128, $1e0, $0ec, $059, $091, $011, $12f, $026, $0dc, $0b0, $18c, $10f, $1f7,
$0e7, $16c, $0b6, $0f9, $0d8, $151, $101, $14c, $103, $0b8, $154, $12b, $1ae, $017, $071, $00c,
$047, $058, $07f, $1a4, $134, $129, $084, $15d, $19d, $1b2, $1a3, $048, $07c, $051, $1ca, $023,
$13d, $1a7, $165, $03b, $042, $0da, $192, $0ce, $0c1, $06b, $09f, $1f1, $12c, $184, $0fa, $196,
$1e1, $169, $17d, $031, $180, $10a, $094, $1da, $186, $13e, $11c, $060, $175, $1cf, $067, $119,
$065, $068, $099, $150, $008, $007, $17c, $0b7, $024, $019, $0de, $127, $0db, $0e4, $1a9, $052,
$109, $090, $19c, $1c1, $028, $1b3, $135, $16a, $176, $0df, $1e5, $188, $0c5, $16e, $1de, $1b1,
$0c3, $1df, $036, $0ee, $1ee, $0f0, $093, $049, $09a, $1b6, $069, $081, $125, $00b, $05e, $0b4,
$149, $1c7, $174, $03e, $13b, $1b7, $08e, $1c6, $0ae, $010, $095, $1ef, $04e, $0f2, $1fd, $085,
$0fd, $0f6, $0a0, $16f, $083, $08a, $156, $09b, $13c, $107, $167, $098, $1d0, $1e9, $003, $1fe,
$0bd, $122, $089, $0d2, $18f, $012, $033, $06a, $142, $0ed, $170, $11b, $0e2, $14f, $158, $131,
$147, $05d, $113, $1cd, $079, $161, $1a5, $179, $09e, $1b4, $0cc, $022, $132, $01a, $0e8, $004,
$187, $1ed, $197, $039, $1bf, $1d7, $027, $18b, $0c6, $09c, $0d0, $14e, $06c, $034, $1f2, $06e,
$0ca, $025, $0ba, $191, $0fe, $013, $106, $02f, $1ad, $172, $1db, $0c0, $10b, $1d6, $0f5, $1ec,
$10d, $076, $114, $1ab, $075, $10c, $1e4, $159, $054, $11f, $04b, $0c4, $1be, $0f7, $029, $0a4,
$00e, $1f0, $077, $04d, $17a, $086, $08b, $0b3, $171, $0bf, $10e, $104, $097, $15b, $160, $168,
$0d7, $0bb, $066, $1ce, $0fc, $092, $1c5, $06f, $016, $04a, $0a1, $139, $0af, $0f1, $190, $00a,
$1aa, $143, $17b, $056, $18d, $166, $0d4, $1fb, $14d, $194, $19a, $087, $1f8, $123, $0a7, $1b8,
$141, $03c, $1f9, $140, $02a, $155, $11a, $1a1, $198, $0d5, $126, $1af, $061, $12e, $157, $1dc,
$072, $18a, $0aa, $096, $115, $0ef, $045, $07b, $08d, $145, $053, $05f, $178, $0b2, $02e, $020,
$1d5, $03f, $1c9, $1e7, $1ac, $044, $038, $014, $0b1, $16b, $0ab, $0b5, $05a, $182, $1c8, $1d4,
$018, $177, $064, $0cf, $06d, $100, $199, $130, $15a, $005, $120, $1bb, $1bd, $0e0, $04f, $0d6,
$13f, $1c4, $12a, $015, $006, $0ff, $19b, $0a6, $043, $088, $050, $15f, $1e8, $121, $073, $17e,
$0bc, $0c2, $0c9, $173, $189, $1f5, $074, $1cc, $1e6, $1a8, $195, $01f, $041, $00d, $1ba, $032,
$03d, $1d1, $080, $0a8, $057, $1b9, $162, $148, $0d9, $105, $062, $07a, $021, $1ff, $112, $108,
$1c0, $0a9, $11d, $1b0, $1a6, $0cd, $0f3, $05c, $102, $05b, $1d9, $144, $1f6, $0ad, $0a5, $03a,
$1cb, $136, $17f, $046, $0e1, $01e, $1dd, $0e6, $137, $1fa, $185, $08c, $08f, $040, $1b5, $0be,
$078, $000, $0ac, $110, $15e, $124, $002, $1bc, $0a2, $0ea, $070, $1fc, $116, $15c, $04c, $1c2);

View File

@ -1,18 +1,18 @@
const
sBox: array[0..255] of byte= (
$D9,$78,$F9,$C4,$19,$DD,$B5,$ED,$28,$E9,$FD,$79,$4A,$A0,$D8,$9D,
$C6,$7E,$37,$83,$2B,$76,$53,$8E,$62,$4C,$64,$88,$44,$8B,$FB,$A2,
$17,$9A,$59,$F5,$87,$B3,$4F,$13,$61,$45,$6D,$8D,$09,$81,$7D,$32,
$BD,$8F,$40,$EB,$86,$B7,$7B,$0B,$F0,$95,$21,$22,$5C,$6B,$4E,$82,
$54,$D6,$65,$93,$CE,$60,$B2,$1C,$73,$56,$C0,$14,$A7,$8C,$F1,$DC,
$12,$75,$CA,$1F,$3B,$BE,$E4,$D1,$42,$3D,$D4,$30,$A3,$3C,$B6,$26,
$6F,$BF,$0E,$DA,$46,$69,$07,$57,$27,$F2,$1D,$9B,$BC,$94,$43,$03,
$F8,$11,$C7,$F6,$90,$EF,$3E,$E7,$06,$C3,$D5,$2F,$C8,$66,$1E,$D7,
$08,$E8,$EA,$DE,$80,$52,$EE,$F7,$84,$AA,$72,$AC,$35,$4D,$6A,$2A,
$96,$1A,$D2,$71,$5A,$15,$49,$74,$4B,$9F,$D0,$5E,$04,$18,$A4,$EC,
$C2,$E0,$41,$6E,$0F,$51,$CB,$CC,$24,$91,$AF,$50,$A1,$F4,$70,$39,
$99,$7C,$3A,$85,$23,$B8,$B4,$7A,$FC,$02,$36,$5B,$25,$55,$97,$31,
$2D,$5D,$FA,$98,$E3,$8A,$92,$AE,$05,$DF,$29,$10,$67,$6C,$BA,$C9,
$D3,$00,$E6,$CF,$E1,$9E,$A8,$2C,$63,$16,$01,$3F,$58,$E2,$89,$A9,
$0D,$38,$34,$1B,$AB,$33,$FF,$B0,$BB,$48,$0C,$5F,$B9,$B1,$CD,$2E,
$C5,$F3,$DB,$47,$E5,$A5,$9C,$77,$0A,$A6,$20,$68,$FE,$7F,$C1,$AD);
const
sBox: array[0..255] of byte= (
$D9,$78,$F9,$C4,$19,$DD,$B5,$ED,$28,$E9,$FD,$79,$4A,$A0,$D8,$9D,
$C6,$7E,$37,$83,$2B,$76,$53,$8E,$62,$4C,$64,$88,$44,$8B,$FB,$A2,
$17,$9A,$59,$F5,$87,$B3,$4F,$13,$61,$45,$6D,$8D,$09,$81,$7D,$32,
$BD,$8F,$40,$EB,$86,$B7,$7B,$0B,$F0,$95,$21,$22,$5C,$6B,$4E,$82,
$54,$D6,$65,$93,$CE,$60,$B2,$1C,$73,$56,$C0,$14,$A7,$8C,$F1,$DC,
$12,$75,$CA,$1F,$3B,$BE,$E4,$D1,$42,$3D,$D4,$30,$A3,$3C,$B6,$26,
$6F,$BF,$0E,$DA,$46,$69,$07,$57,$27,$F2,$1D,$9B,$BC,$94,$43,$03,
$F8,$11,$C7,$F6,$90,$EF,$3E,$E7,$06,$C3,$D5,$2F,$C8,$66,$1E,$D7,
$08,$E8,$EA,$DE,$80,$52,$EE,$F7,$84,$AA,$72,$AC,$35,$4D,$6A,$2A,
$96,$1A,$D2,$71,$5A,$15,$49,$74,$4B,$9F,$D0,$5E,$04,$18,$A4,$EC,
$C2,$E0,$41,$6E,$0F,$51,$CB,$CC,$24,$91,$AF,$50,$A1,$F4,$70,$39,
$99,$7C,$3A,$85,$23,$B8,$B4,$7A,$FC,$02,$36,$5B,$25,$55,$97,$31,
$2D,$5D,$FA,$98,$E3,$8A,$92,$AE,$05,$DF,$29,$10,$67,$6C,$BA,$C9,
$D3,$00,$E6,$CF,$E1,$9E,$A8,$2C,$63,$16,$01,$3F,$58,$E2,$89,$A9,
$0D,$38,$34,$1B,$AB,$33,$FF,$B0,$BB,$48,$0C,$5F,$B9,$B1,$CD,$2E,
$C5,$F3,$DB,$47,$E5,$A5,$9C,$77,$0A,$A6,$20,$68,$FE,$7F,$C1,$AD);

File diff suppressed because it is too large Load Diff

View File

@ -1,66 +1,66 @@
const
p8x8: array[0..1,0..255] of byte= ((
$a9, $67, $b3, $e8, $04, $fd, $a3, $76,
$9a, $92, $80, $78, $e4, $dd, $d1, $38,
$0d, $c6, $35, $98, $18, $f7, $ec, $6c,
$43, $75, $37, $26, $fa, $13, $94, $48,
$f2, $d0, $8b, $30, $84, $54, $df, $23,
$19, $5b, $3d, $59, $f3, $ae, $a2, $82,
$63, $01, $83, $2e, $d9, $51, $9b, $7c,
$a6, $eb, $a5, $be, $16, $0c, $e3, $61,
$c0, $8c, $3a, $f5, $73, $2c, $25, $0b,
$bb, $4e, $89, $6b, $53, $6a, $b4, $f1,
$e1, $e6, $bd, $45, $e2, $f4, $b6, $66,
$cc, $95, $03, $56, $d4, $1c, $1e, $d7,
$fb, $c3, $8e, $b5, $e9, $cf, $bf, $ba,
$ea, $77, $39, $af, $33, $c9, $62, $71,
$81, $79, $09, $ad, $24, $cd, $f9, $d8,
$e5, $c5, $b9, $4d, $44, $08, $86, $e7,
$a1, $1d, $aa, $ed, $06, $70, $b2, $d2,
$41, $7b, $a0, $11, $31, $c2, $27, $90,
$20, $f6, $60, $ff, $96, $5c, $b1, $ab,
$9e, $9c, $52, $1b, $5f, $93, $0a, $ef,
$91, $85, $49, $ee, $2d, $4f, $8f, $3b,
$47, $87, $6d, $46, $d6, $3e, $69, $64,
$2a, $ce, $cb, $2f, $fc, $97, $05, $7a,
$ac, $7f, $d5, $1a, $4b, $0e, $a7, $5a,
$28, $14, $3f, $29, $88, $3c, $4c, $02,
$b8, $da, $b0, $17, $55, $1f, $8a, $7d,
$57, $c7, $8d, $74, $b7, $c4, $9f, $72,
$7e, $15, $22, $12, $58, $07, $99, $34,
$6e, $50, $de, $68, $65, $bc, $db, $f8,
$c8, $a8, $2b, $40, $dc, $fe, $32, $a4,
$ca, $10, $21, $f0, $d3, $5d, $0f, $00,
$6f, $9d, $36, $42, $4a, $5e, $c1, $e0),(
$75, $f3, $c6, $f4, $db, $7b, $fb, $c8,
$4a, $d3, $e6, $6b, $45, $7d, $e8, $4b,
$d6, $32, $d8, $fd, $37, $71, $f1, $e1,
$30, $0f, $f8, $1b, $87, $fa, $06, $3f,
$5e, $ba, $ae, $5b, $8a, $00, $bc, $9d,
$6d, $c1, $b1, $0e, $80, $5d, $d2, $d5,
$a0, $84, $07, $14, $b5, $90, $2c, $a3,
$b2, $73, $4c, $54, $92, $74, $36, $51,
$38, $b0, $bd, $5a, $fc, $60, $62, $96,
$6c, $42, $f7, $10, $7c, $28, $27, $8c,
$13, $95, $9c, $c7, $24, $46, $3b, $70,
$ca, $e3, $85, $cb, $11, $d0, $93, $b8,
$a6, $83, $20, $ff, $9f, $77, $c3, $cc,
$03, $6f, $08, $bf, $40, $e7, $2b, $e2,
$79, $0c, $aa, $82, $41, $3a, $ea, $b9,
$e4, $9a, $a4, $97, $7e, $da, $7a, $17,
$66, $94, $a1, $1d, $3d, $f0, $de, $b3,
$0b, $72, $a7, $1c, $ef, $d1, $53, $3e,
$8f, $33, $26, $5f, $ec, $76, $2a, $49,
$81, $88, $ee, $21, $c4, $1a, $eb, $d9,
$c5, $39, $99, $cd, $ad, $31, $8b, $01,
$18, $23, $dd, $1f, $4e, $2d, $f9, $48,
$4f, $f2, $65, $8e, $78, $5c, $58, $19,
$8d, $e5, $98, $57, $67, $7f, $05, $64,
$af, $63, $b6, $fe, $f5, $b7, $3c, $a5,
$ce, $e9, $68, $44, $e0, $4d, $43, $69,
$29, $2e, $ac, $15, $59, $a8, $0a, $9e,
$6e, $47, $df, $34, $35, $6a, $cf, $dc,
$22, $c9, $c0, $9b, $89, $d4, $ed, $ab,
$12, $a2, $0d, $52, $bb, $02, $2f, $a9,
$d7, $61, $1e, $b4, $50, $04, $f6, $c2,
$16, $25, $86, $56, $55, $09, $be, $91));
const
p8x8: array[0..1,0..255] of byte= ((
$a9, $67, $b3, $e8, $04, $fd, $a3, $76,
$9a, $92, $80, $78, $e4, $dd, $d1, $38,
$0d, $c6, $35, $98, $18, $f7, $ec, $6c,
$43, $75, $37, $26, $fa, $13, $94, $48,
$f2, $d0, $8b, $30, $84, $54, $df, $23,
$19, $5b, $3d, $59, $f3, $ae, $a2, $82,
$63, $01, $83, $2e, $d9, $51, $9b, $7c,
$a6, $eb, $a5, $be, $16, $0c, $e3, $61,
$c0, $8c, $3a, $f5, $73, $2c, $25, $0b,
$bb, $4e, $89, $6b, $53, $6a, $b4, $f1,
$e1, $e6, $bd, $45, $e2, $f4, $b6, $66,
$cc, $95, $03, $56, $d4, $1c, $1e, $d7,
$fb, $c3, $8e, $b5, $e9, $cf, $bf, $ba,
$ea, $77, $39, $af, $33, $c9, $62, $71,
$81, $79, $09, $ad, $24, $cd, $f9, $d8,
$e5, $c5, $b9, $4d, $44, $08, $86, $e7,
$a1, $1d, $aa, $ed, $06, $70, $b2, $d2,
$41, $7b, $a0, $11, $31, $c2, $27, $90,
$20, $f6, $60, $ff, $96, $5c, $b1, $ab,
$9e, $9c, $52, $1b, $5f, $93, $0a, $ef,
$91, $85, $49, $ee, $2d, $4f, $8f, $3b,
$47, $87, $6d, $46, $d6, $3e, $69, $64,
$2a, $ce, $cb, $2f, $fc, $97, $05, $7a,
$ac, $7f, $d5, $1a, $4b, $0e, $a7, $5a,
$28, $14, $3f, $29, $88, $3c, $4c, $02,
$b8, $da, $b0, $17, $55, $1f, $8a, $7d,
$57, $c7, $8d, $74, $b7, $c4, $9f, $72,
$7e, $15, $22, $12, $58, $07, $99, $34,
$6e, $50, $de, $68, $65, $bc, $db, $f8,
$c8, $a8, $2b, $40, $dc, $fe, $32, $a4,
$ca, $10, $21, $f0, $d3, $5d, $0f, $00,
$6f, $9d, $36, $42, $4a, $5e, $c1, $e0),(
$75, $f3, $c6, $f4, $db, $7b, $fb, $c8,
$4a, $d3, $e6, $6b, $45, $7d, $e8, $4b,
$d6, $32, $d8, $fd, $37, $71, $f1, $e1,
$30, $0f, $f8, $1b, $87, $fa, $06, $3f,
$5e, $ba, $ae, $5b, $8a, $00, $bc, $9d,
$6d, $c1, $b1, $0e, $80, $5d, $d2, $d5,
$a0, $84, $07, $14, $b5, $90, $2c, $a3,
$b2, $73, $4c, $54, $92, $74, $36, $51,
$38, $b0, $bd, $5a, $fc, $60, $62, $96,
$6c, $42, $f7, $10, $7c, $28, $27, $8c,
$13, $95, $9c, $c7, $24, $46, $3b, $70,
$ca, $e3, $85, $cb, $11, $d0, $93, $b8,
$a6, $83, $20, $ff, $9f, $77, $c3, $cc,
$03, $6f, $08, $bf, $40, $e7, $2b, $e2,
$79, $0c, $aa, $82, $41, $3a, $ea, $b9,
$e4, $9a, $a4, $97, $7e, $da, $7a, $17,
$66, $94, $a1, $1d, $3d, $f0, $de, $b3,
$0b, $72, $a7, $1c, $ef, $d1, $53, $3e,
$8f, $33, $26, $5f, $ec, $76, $2a, $49,
$81, $88, $ee, $21, $c4, $1a, $eb, $d9,
$c5, $39, $99, $cd, $ad, $31, $8b, $01,
$18, $23, $dd, $1f, $4e, $2d, $f9, $48,
$4f, $f2, $65, $8e, $78, $5c, $58, $19,
$8d, $e5, $98, $57, $67, $7f, $05, $64,
$af, $63, $b6, $fe, $f5, $b7, $3c, $a5,
$ce, $e9, $68, $44, $e0, $4d, $43, $69,
$29, $2e, $ac, $15, $59, $a8, $0a, $9e,
$6e, $47, $df, $34, $35, $6a, $cf, $dc,
$22, $c9, $c0, $9b, $89, $d4, $ed, $ab,
$12, $a2, $0d, $52, $bb, $02, $2f, $a9,
$d7, $61, $1e, $b4, $50, $04, $f6, $c2,
$16, $25, $86, $56, $55, $09, $be, $91));

View File

@ -1,248 +1,248 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Blowfish *****************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPblowfish;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_blowfish= class(TDCP_blockcipher64)
protected
SBox: array[0..3,0..255] of DWord;
PBox: array[0..17] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
implementation
{$R-}{$Q-}
{$I DCPblowfish.inc}
class function TDCP_blowfish.GetID: integer;
begin
Result:= DCP_blowfish;
end;
class function TDCP_blowfish.GetAlgorithm: string;
begin
Result:= 'Blowfish';
end;
class function TDCP_blowfish.GetMaxKeySize: integer;
begin
Result:= 448;
end;
class function TDCP_blowfish.SelfTest: boolean;
const
Key1: array[0..7] of byte= ($00,$00,$00,$00,$00,$00,$00,$00);
Key2: array[0..7] of byte= ($7C,$A1,$10,$45,$4A,$1A,$6E,$57);
InData1: array[0..7] of byte= ($00,$00,$00,$00,$00,$00,$00,$00);
InData2: array[0..7] of byte= ($01,$A1,$D6,$D0,$39,$77,$67,$42);
OutData1: array[0..7] of byte= ($4E,$F9,$97,$45,$61,$98,$DD,$78);
OutData2: array[0..7] of byte= ($59,$C6,$82,$45,$EB,$05,$28,$2B);
var
Cipher: TDCP_blowfish;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_blowfish.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= boolean(CompareMem(@Data,@OutData2,Sizeof(Data))) and Result;
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData2,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_blowfish.InitKey(const Key; Size: longword);
var
i, k: longword;
A: DWord;
KeyB: PByteArray;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Size:= Size div 8;
KeyB:= @Key;
Move(SBoxOrg,SBox,Sizeof(SBox));
Move(PBoxOrg,PBox,Sizeof(PBox));
k:= 0;
for i:= 0 to 17 do
begin
A:= dword(KeyB^[(k+3) mod Size]);
A:= A + (dword(KeyB^[(k+2) mod Size]) shl 8);
A:= A + (dword(KeyB^[(k+1) mod Size]) shl 16);
A:= A + (dword(KeyB^[k]) shl 24);
PBox[i]:= PBox[i] xor A;
k:= (k+4) mod Size;
end;
FillChar(Block,Sizeof(Block),0);
for i:= 0 to 8 do
begin
EncryptECB(Block,Block);
PBox[i*2]:= dword(Block[3]) + (dword(Block[2]) shl 8) + (dword(Block[1]) shl 16) + (dword(Block[0]) shl 24);
PBox[i*2+1]:= dword(Block[7]) + (dword(Block[6]) shl 8) + (dword(Block[5]) shl 16) + (dword(Block[4]) shl 24);
end;
for k:= 0 to 3 do
begin
for i:= 0 to 127 do
begin
EncryptECB(Block,Block);
SBox[k,i*2]:= dword(Block[3]) + (dword(Block[2]) shl 8) + (dword(Block[1]) shl 16) + (dword(Block[0]) shl 24);
SBox[k,i*2+1]:= dword(Block[7]) + (dword(Block[6]) shl 8) + (dword(Block[5]) shl 16) + (dword(Block[4]) shl 24);
end;
end;
end;
procedure TDCP_blowfish.Burn;
begin
FillChar(SBox,Sizeof(SBox),$FF);
FillChar(PBox,Sizeof(PBox),$FF);
inherited Burn;
end;
procedure TDCP_blowfish.EncryptECB(const InData; var OutData);
var
xL, xR: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
xL:= Pdword(@InData)^;
xR:= Pdword(pointer(@InData)+4)^;
xL:= ((xL and $FF) shl 24) or ((xL and $FF00) shl 8) or ((xL and $FF0000) shr 8) or ((xL and $FF000000) shr 24);
xR:= ((xR and $FF) shl 24) or ((xR and $FF00) shl 8) or ((xR and $FF0000) shr 8) or ((xR and $FF000000) shr 24);
xL:= xL xor PBox[0];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[1];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[2];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[3];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[4];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[5];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[6];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[7];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[8];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[9];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[10];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[11];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[12];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[13];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[14];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[15];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[16];
xR:= xR xor PBox[17];
xL:= ((xL and $FF) shl 24) or ((xL and $FF00) shl 8) or ((xL and $FF0000) shr 8) or ((xL and $FF000000) shr 24);
xR:= ((xR and $FF) shl 24) or ((xR and $FF00) shl 8) or ((xR and $FF0000) shr 8) or ((xR and $FF000000) shr 24);
Pdword(@OutData)^:= xR;
Pdword(pointer(@OutData)+4)^:= xL;
end;
procedure TDCP_blowfish.DecryptECB(const InData; var OutData);
var
xL, xR: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
xL:= Pdword(@InData)^;
xR:= Pdword(pointer(@InData)+4)^;
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
xL:= xL xor PBox[17];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[16];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[15];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[14];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[13];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[12];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[11];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[10];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[9];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[8];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[7];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[6];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[5];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[4];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[3];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[2];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[1];
xR:= xR xor PBox[0];
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
Pdword(@OutData)^:= xR;
Pdword(pointer(@OutData)+4)^:= xL;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Blowfish *****************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPblowfish;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_blowfish= class(TDCP_blockcipher64)
protected
SBox: array[0..3,0..255] of DWord;
PBox: array[0..17] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
implementation
{$R-}{$Q-}
{$I DCPblowfish.inc}
class function TDCP_blowfish.GetID: integer;
begin
Result:= DCP_blowfish;
end;
class function TDCP_blowfish.GetAlgorithm: string;
begin
Result:= 'Blowfish';
end;
class function TDCP_blowfish.GetMaxKeySize: integer;
begin
Result:= 448;
end;
class function TDCP_blowfish.SelfTest: boolean;
const
Key1: array[0..7] of byte= ($00,$00,$00,$00,$00,$00,$00,$00);
Key2: array[0..7] of byte= ($7C,$A1,$10,$45,$4A,$1A,$6E,$57);
InData1: array[0..7] of byte= ($00,$00,$00,$00,$00,$00,$00,$00);
InData2: array[0..7] of byte= ($01,$A1,$D6,$D0,$39,$77,$67,$42);
OutData1: array[0..7] of byte= ($4E,$F9,$97,$45,$61,$98,$DD,$78);
OutData2: array[0..7] of byte= ($59,$C6,$82,$45,$EB,$05,$28,$2B);
var
Cipher: TDCP_blowfish;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_blowfish.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= boolean(CompareMem(@Data,@OutData2,Sizeof(Data))) and Result;
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData2,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_blowfish.InitKey(const Key; Size: longword);
var
i, k: longword;
A: DWord;
KeyB: PByteArray;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Size:= Size div 8;
KeyB:= @Key;
Move(SBoxOrg,SBox,Sizeof(SBox));
Move(PBoxOrg,PBox,Sizeof(PBox));
k:= 0;
for i:= 0 to 17 do
begin
A:= dword(KeyB^[(k+3) mod Size]);
A:= A + (dword(KeyB^[(k+2) mod Size]) shl 8);
A:= A + (dword(KeyB^[(k+1) mod Size]) shl 16);
A:= A + (dword(KeyB^[k]) shl 24);
PBox[i]:= PBox[i] xor A;
k:= (k+4) mod Size;
end;
FillChar(Block,Sizeof(Block),0);
for i:= 0 to 8 do
begin
EncryptECB(Block,Block);
PBox[i*2]:= dword(Block[3]) + (dword(Block[2]) shl 8) + (dword(Block[1]) shl 16) + (dword(Block[0]) shl 24);
PBox[i*2+1]:= dword(Block[7]) + (dword(Block[6]) shl 8) + (dword(Block[5]) shl 16) + (dword(Block[4]) shl 24);
end;
for k:= 0 to 3 do
begin
for i:= 0 to 127 do
begin
EncryptECB(Block,Block);
SBox[k,i*2]:= dword(Block[3]) + (dword(Block[2]) shl 8) + (dword(Block[1]) shl 16) + (dword(Block[0]) shl 24);
SBox[k,i*2+1]:= dword(Block[7]) + (dword(Block[6]) shl 8) + (dword(Block[5]) shl 16) + (dword(Block[4]) shl 24);
end;
end;
end;
procedure TDCP_blowfish.Burn;
begin
FillChar(SBox,Sizeof(SBox),$FF);
FillChar(PBox,Sizeof(PBox),$FF);
inherited Burn;
end;
procedure TDCP_blowfish.EncryptECB(const InData; var OutData);
var
xL, xR: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
xL:= Pdword(@InData)^;
xR:= Pdword(pointer(@InData)+4)^;
xL:= ((xL and $FF) shl 24) or ((xL and $FF00) shl 8) or ((xL and $FF0000) shr 8) or ((xL and $FF000000) shr 24);
xR:= ((xR and $FF) shl 24) or ((xR and $FF00) shl 8) or ((xR and $FF0000) shr 8) or ((xR and $FF000000) shr 24);
xL:= xL xor PBox[0];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[1];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[2];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[3];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[4];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[5];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[6];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[7];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[8];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[9];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[10];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[11];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[12];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[13];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[14];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[15];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[16];
xR:= xR xor PBox[17];
xL:= ((xL and $FF) shl 24) or ((xL and $FF00) shl 8) or ((xL and $FF0000) shr 8) or ((xL and $FF000000) shr 24);
xR:= ((xR and $FF) shl 24) or ((xR and $FF00) shl 8) or ((xR and $FF0000) shr 8) or ((xR and $FF000000) shr 24);
Pdword(@OutData)^:= xR;
Pdword(pointer(@OutData)+4)^:= xL;
end;
procedure TDCP_blowfish.DecryptECB(const InData; var OutData);
var
xL, xR: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
xL:= Pdword(@InData)^;
xR:= Pdword(pointer(@InData)+4)^;
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
xL:= xL xor PBox[17];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[16];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[15];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[14];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[13];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[12];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[11];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[10];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[9];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[8];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[7];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[6];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[5];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[4];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[3];
xR:= xR xor (((SBox[0,(xL shr 24) and $FF] + SBox[1,(xL shr 16) and $FF]) xor
SBox[2,(xL shr 8) and $FF]) + SBox[3,xL and $FF]) xor PBox[2];
xL:= xL xor (((SBox[0,(xR shr 24) and $FF] + SBox[1,(xR shr 16) and $FF]) xor
SBox[2,(xR shr 8) and $FF]) + SBox[3,xR and $FF]) xor PBox[1];
xR:= xR xor PBox[0];
xL:= (xL shr 24) or ((xL shr 8) and $FF00) or ((xL shl 8) and $FF0000) or (xL shl 24);
xR:= (xR shr 24) or ((xR shr 8) and $FF00) or ((xR shl 8) and $FF0000) or (xR shl 24);
Pdword(@OutData)^:= xR;
Pdword(pointer(@OutData)+4)^:= xL;
end;
end.

View File

@ -1,382 +1,382 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Cast128 ******************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPcast128;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_cast128= class(TDCP_blockcipher64)
protected
KeyData: array[0..31] of DWord;
Rounds: longword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPcast128.inc}
function LRot32(a, n: dword): dword;
begin
Result:= (a shl n) or (a shr (32-n));
end;
class function TDCP_cast128.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_cast128.GetID: integer;
begin
Result:= DCP_cast128;
end;
class function TDCP_cast128.GetAlgorithm: string;
begin
Result:= 'Cast128';
end;
class function TDCP_cast128.SelfTest: boolean;
const
Key: array[0..15] of byte=
($01,$23,$45,$67,$12,$34,$56,$78,$23,$45,$67,$89,$34,$56,$78,$9A);
InBlock: array[0..7] of byte=
($01,$23,$45,$67,$89,$AB,$CD,$EF);
Out128: array[0..7] of byte=
($23,$8B,$4F,$E5,$84,$7E,$44,$B2);
Out80: array[0..7] of byte=
($EB,$6A,$71,$1A,$2C,$02,$27,$1B);
Out40: array[0..7] of byte=
($7A,$C8,$16,$D1,$6E,$9B,$30,$2E);
var
Block: array[0..7] of byte;
Cipher: TDCP_cast128;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_cast128.Create(nil);
Cipher.Init(Key,128,nil);
Cipher.EncryptECB(InBlock,Block);
Result:= boolean(CompareMem(@Block,@Out128,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock,8));
Cipher.Burn;
Cipher.Init(Key,80,nil);
Cipher.EncryptECB(InBlock,Block);
Result:= Result and boolean(CompareMem(@Block,@Out80,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock,8));
Cipher.Burn;
Cipher.Init(Key,40,nil);
Cipher.EncryptECB(InBlock,Block);
Result:= Result and boolean(CompareMem(@Block,@Out40,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock,8));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_cast128.InitKey(const Key; Size: longword);
var
x, t, z: array[0..3] of DWord;
i: longword;
begin
Size:= Size div 8;
if Size<= 10 then
Rounds:= 12
else
Rounds:= 16;
dcpFillChar(x, Sizeof(x), 0);
Move(Key,x,Size);
x[0]:= (x[0] shr 24) or ((x[0] shr 8) and $FF00) or ((x[0] shl 8) and $FF0000) or (x[0] shl 24);
x[1]:= (x[1] shr 24) or ((x[1] shr 8) and $FF00) or ((x[1] shl 8) and $FF0000) or (x[1] shl 24);
x[2]:= (x[2] shr 24) or ((x[2] shr 8) and $FF00) or ((x[2] shl 8) and $FF0000) or (x[2] shl 24);
x[3]:= (x[3] shr 24) or ((x[3] shr 8) and $FF00) or ((x[3] shl 8) and $FF0000) or (x[3] shl 24);
i:= 0;
while i< 32 do
begin
case (i and 4) of
0:
begin
z[0]:= x[0] xor cast_sbox5[(x[3] shr 16) and $FF] xor
cast_sbox6[x[3] and $FF] xor cast_sbox7[x[3] shr 24] xor
cast_sbox8[(x[3] shr 8) and $FF] xor cast_sbox7[x[2] shr 24];
t[0]:= z[0];
z[1]:= x[2] xor cast_sbox5[z[0] shr 24] xor
cast_sbox6[(z[0] shr 8) and $FF] xor cast_sbox7[(z[0] shr 16) and $FF] xor
cast_sbox8[z[0] and $FF] xor cast_sbox8[(x[2] shr 8) and $FF];
t[1]:= z[1];
z[2]:= x[3] xor cast_sbox5[z[1] and $FF] xor
cast_sbox6[(z[1] shr 8) and $FF] xor cast_sbox7[(z[1] shr 16) and $FF] xor
cast_sbox8[z[1] shr 24] xor cast_sbox5[(x[2] shr 16) and $FF];
t[2]:= z[2];
z[3]:= x[1] xor cast_sbox5[(z[2] shr 8) and $FF] xor
cast_sbox6[(z[2] shr 16) and $FF] xor cast_sbox7[z[2] and $FF] xor
cast_sbox8[z[2] shr 24] xor cast_sbox6[x[2] and $FF];
t[3]:= z[3];
end;
4:
begin
x[0]:= z[2] xor cast_sbox5[(z[1] shr 16) and $FF] xor
cast_sbox6[z[1] and $FF] xor cast_sbox7[z[1] shr 24] xor
cast_sbox8[(z[1] shr 8) and $FF] xor cast_sbox7[z[0] shr 24];
t[0]:= x[0];
x[1]:= z[0] xor cast_sbox5[x[0] shr 24] xor
cast_sbox6[(x[0] shr 8) and $FF] xor cast_sbox7[(x[0] shr 16) and $FF] xor
cast_sbox8[x[0] and $FF] xor cast_sbox8[(z[0] shr 8) and $FF];
t[1]:= x[1];
x[2]:= z[1] xor cast_sbox5[x[1] and $FF] xor
cast_sbox6[(x[1] shr 8) and $FF] xor cast_sbox7[(x[1] shr 16) and $FF] xor
cast_sbox8[x[1] shr 24] xor cast_sbox5[(z[0] shr 16) and $FF];
t[2]:= x[2];
x[3]:= z[3] xor cast_sbox5[(x[2] shr 8) and $FF] xor
cast_sbox6[(x[2] shr 16) and $FF] xor cast_sbox7[x[2] and $FF] xor
cast_sbox8[x[2] shr 24] xor cast_sbox6[z[0] and $FF];
t[3]:= x[3];
end;
end;
case (i and 12) of
0,12:
begin
KeyData[i+0]:= cast_sbox5[t[2] shr 24] xor cast_sbox6[(t[2] shr 16) and $FF] xor
cast_sbox7[t[1] and $FF] xor cast_sbox8[(t[1] shr 8) and $FF];
KeyData[i+1]:= cast_sbox5[(t[2] shr 8) and $FF] xor cast_sbox6[t[2] and $FF] xor
cast_sbox7[(t[1] shr 16) and $FF] xor cast_sbox8[t[1] shr 24];
KeyData[i+2]:= cast_sbox5[t[3] shr 24] xor cast_sbox6[(t[3] shr 16) and $FF] xor
cast_sbox7[t[0] and $FF] xor cast_sbox8[(t[0] shr 8) and $FF];
KeyData[i+3]:= cast_sbox5[(t[3] shr 8) and $FF] xor cast_sbox6[t[3] and $FF] xor
cast_sbox7[(t[0] shr 16) and $FF] xor cast_sbox8[t[0] shr 24];
end;
4,8:
begin
KeyData[i+0]:= cast_sbox5[t[0] and $FF] xor cast_sbox6[(t[0] shr 8) and $FF] xor
cast_sbox7[t[3] shr 24] xor cast_sbox8[(t[3] shr 16) and $FF];
KeyData[i+1]:= cast_sbox5[(t[0] shr 16) and $FF] xor cast_sbox6[t[0] shr 24] xor
cast_sbox7[(t[3] shr 8) and $FF] xor cast_sbox8[t[3] and $FF];
KeyData[i+2]:= cast_sbox5[t[1] and $FF] xor cast_sbox6[(t[1] shr 8) and $FF] xor
cast_sbox7[t[2] shr 24] xor cast_sbox8[(t[2] shr 16) and $FF];
KeyData[i+3]:= cast_sbox5[(t[1] shr 16) and $FF] xor cast_sbox6[t[1] shr 24] xor
cast_sbox7[(t[2] shr 8) and $FF] xor cast_sbox8[t[2] and $FF];
end;
end;
case (i and 12) of
0:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[(z[0] shr 8) and $FF];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[(z[1] shr 8) and $FF];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[(z[2] shr 16) and $FF];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[z[3] shr 24];
end;
4:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[x[2] shr 24];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[(x[3] shr 16) and $FF];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[x[0] and $FF];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[x[1] and $FF];
end;
8:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[(z[2] shr 16) and $FF];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[z[3] shr 24];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[(z[0] shr 8) and $FF];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[(z[1] shr 8) and $FF];
end;
12:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[x[0] and $FF];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[x[1] and $FF];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[x[2] shr 24];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[(x[3] shr 16) and $FF];
end;
end;
if (i >= 16) then
begin
KeyData[i+0]:= KeyData[i+0] and 31;
KeyData[i+1]:= KeyData[i+1] and 31;
KeyData[i+2]:= KeyData[i+2] and 31;
KeyData[i+3]:= KeyData[i+3] and 31;
end;
Inc(i,4);
end;
end;
procedure TDCP_cast128.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),$FF);
Rounds:= 0;
inherited Burn;
end;
procedure TDCP_cast128.EncryptECB(const InData; var OutData);
var
t, l, r: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
l:= Pdword(@InData)^;
r:= Pdword(Pointer(@InData)+4)^;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
t:= LRot32(KeyData[0]+r, KeyData[0+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[1] xor l, KeyData[1+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[2]-r, KeyData[2+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[3]+l, KeyData[3+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[4] xor r, KeyData[4+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[5]-l, KeyData[5+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[6]+r, KeyData[6+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[7] xor l, KeyData[7+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[8]-r, KeyData[8+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[9]+l, KeyData[9+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[10] xor r, KeyData[10+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[11]-l, KeyData[11+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
if Rounds> 12 then
begin
t:= LRot32(KeyData[12]+r, KeyData[12+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[13] xor l, KeyData[13+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[14]-r, KeyData[14+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[15]+l, KeyData[15+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
end;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
Pdword(@OutData)^:= r;
Pdword(pointer(@OutData)+4)^:= l;
end;
procedure TDCP_cast128.DecryptECB(const InData; var OutData);
var
t, l, r: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
r:= Pdword(@InData)^;
l:= Pdword(pointer(@InData)+4)^;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
if Rounds> 12 then
begin
t:= LRot32(KeyData[15]+l, KeyData[15+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[14]-r, KeyData[14+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[13] xor l, KeyData[13+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[12]+r, KeyData[12+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
end;
t:= LRot32(KeyData[11]-l, KeyData[11+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[10] xor r, KeyData[10+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[9]+l, KeyData[9+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[8]-r, KeyData[8+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[7] xor l, KeyData[7+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[6]+r, KeyData[6+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[5]-l, KeyData[5+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[4] xor r, KeyData[4+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[3]+l, KeyData[3+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[2]-r, KeyData[2+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[1] xor l, KeyData[1+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[0]+r, KeyData[0+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
Pdword(@OutData)^:= l;
Pdword(pointer(@OutData)+4)^:= r;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Cast128 ******************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPcast128;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_cast128= class(TDCP_blockcipher64)
protected
KeyData: array[0..31] of DWord;
Rounds: longword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPcast128.inc}
function LRot32(a, n: dword): dword;
begin
Result:= (a shl n) or (a shr (32-n));
end;
class function TDCP_cast128.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_cast128.GetID: integer;
begin
Result:= DCP_cast128;
end;
class function TDCP_cast128.GetAlgorithm: string;
begin
Result:= 'Cast128';
end;
class function TDCP_cast128.SelfTest: boolean;
const
Key: array[0..15] of byte=
($01,$23,$45,$67,$12,$34,$56,$78,$23,$45,$67,$89,$34,$56,$78,$9A);
InBlock: array[0..7] of byte=
($01,$23,$45,$67,$89,$AB,$CD,$EF);
Out128: array[0..7] of byte=
($23,$8B,$4F,$E5,$84,$7E,$44,$B2);
Out80: array[0..7] of byte=
($EB,$6A,$71,$1A,$2C,$02,$27,$1B);
Out40: array[0..7] of byte=
($7A,$C8,$16,$D1,$6E,$9B,$30,$2E);
var
Block: array[0..7] of byte;
Cipher: TDCP_cast128;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_cast128.Create(nil);
Cipher.Init(Key,128,nil);
Cipher.EncryptECB(InBlock,Block);
Result:= boolean(CompareMem(@Block,@Out128,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock,8));
Cipher.Burn;
Cipher.Init(Key,80,nil);
Cipher.EncryptECB(InBlock,Block);
Result:= Result and boolean(CompareMem(@Block,@Out80,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock,8));
Cipher.Burn;
Cipher.Init(Key,40,nil);
Cipher.EncryptECB(InBlock,Block);
Result:= Result and boolean(CompareMem(@Block,@Out40,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock,8));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_cast128.InitKey(const Key; Size: longword);
var
x, t, z: array[0..3] of DWord;
i: longword;
begin
Size:= Size div 8;
if Size<= 10 then
Rounds:= 12
else
Rounds:= 16;
dcpFillChar(x, Sizeof(x), 0);
Move(Key,x,Size);
x[0]:= (x[0] shr 24) or ((x[0] shr 8) and $FF00) or ((x[0] shl 8) and $FF0000) or (x[0] shl 24);
x[1]:= (x[1] shr 24) or ((x[1] shr 8) and $FF00) or ((x[1] shl 8) and $FF0000) or (x[1] shl 24);
x[2]:= (x[2] shr 24) or ((x[2] shr 8) and $FF00) or ((x[2] shl 8) and $FF0000) or (x[2] shl 24);
x[3]:= (x[3] shr 24) or ((x[3] shr 8) and $FF00) or ((x[3] shl 8) and $FF0000) or (x[3] shl 24);
i:= 0;
while i< 32 do
begin
case (i and 4) of
0:
begin
z[0]:= x[0] xor cast_sbox5[(x[3] shr 16) and $FF] xor
cast_sbox6[x[3] and $FF] xor cast_sbox7[x[3] shr 24] xor
cast_sbox8[(x[3] shr 8) and $FF] xor cast_sbox7[x[2] shr 24];
t[0]:= z[0];
z[1]:= x[2] xor cast_sbox5[z[0] shr 24] xor
cast_sbox6[(z[0] shr 8) and $FF] xor cast_sbox7[(z[0] shr 16) and $FF] xor
cast_sbox8[z[0] and $FF] xor cast_sbox8[(x[2] shr 8) and $FF];
t[1]:= z[1];
z[2]:= x[3] xor cast_sbox5[z[1] and $FF] xor
cast_sbox6[(z[1] shr 8) and $FF] xor cast_sbox7[(z[1] shr 16) and $FF] xor
cast_sbox8[z[1] shr 24] xor cast_sbox5[(x[2] shr 16) and $FF];
t[2]:= z[2];
z[3]:= x[1] xor cast_sbox5[(z[2] shr 8) and $FF] xor
cast_sbox6[(z[2] shr 16) and $FF] xor cast_sbox7[z[2] and $FF] xor
cast_sbox8[z[2] shr 24] xor cast_sbox6[x[2] and $FF];
t[3]:= z[3];
end;
4:
begin
x[0]:= z[2] xor cast_sbox5[(z[1] shr 16) and $FF] xor
cast_sbox6[z[1] and $FF] xor cast_sbox7[z[1] shr 24] xor
cast_sbox8[(z[1] shr 8) and $FF] xor cast_sbox7[z[0] shr 24];
t[0]:= x[0];
x[1]:= z[0] xor cast_sbox5[x[0] shr 24] xor
cast_sbox6[(x[0] shr 8) and $FF] xor cast_sbox7[(x[0] shr 16) and $FF] xor
cast_sbox8[x[0] and $FF] xor cast_sbox8[(z[0] shr 8) and $FF];
t[1]:= x[1];
x[2]:= z[1] xor cast_sbox5[x[1] and $FF] xor
cast_sbox6[(x[1] shr 8) and $FF] xor cast_sbox7[(x[1] shr 16) and $FF] xor
cast_sbox8[x[1] shr 24] xor cast_sbox5[(z[0] shr 16) and $FF];
t[2]:= x[2];
x[3]:= z[3] xor cast_sbox5[(x[2] shr 8) and $FF] xor
cast_sbox6[(x[2] shr 16) and $FF] xor cast_sbox7[x[2] and $FF] xor
cast_sbox8[x[2] shr 24] xor cast_sbox6[z[0] and $FF];
t[3]:= x[3];
end;
end;
case (i and 12) of
0,12:
begin
KeyData[i+0]:= cast_sbox5[t[2] shr 24] xor cast_sbox6[(t[2] shr 16) and $FF] xor
cast_sbox7[t[1] and $FF] xor cast_sbox8[(t[1] shr 8) and $FF];
KeyData[i+1]:= cast_sbox5[(t[2] shr 8) and $FF] xor cast_sbox6[t[2] and $FF] xor
cast_sbox7[(t[1] shr 16) and $FF] xor cast_sbox8[t[1] shr 24];
KeyData[i+2]:= cast_sbox5[t[3] shr 24] xor cast_sbox6[(t[3] shr 16) and $FF] xor
cast_sbox7[t[0] and $FF] xor cast_sbox8[(t[0] shr 8) and $FF];
KeyData[i+3]:= cast_sbox5[(t[3] shr 8) and $FF] xor cast_sbox6[t[3] and $FF] xor
cast_sbox7[(t[0] shr 16) and $FF] xor cast_sbox8[t[0] shr 24];
end;
4,8:
begin
KeyData[i+0]:= cast_sbox5[t[0] and $FF] xor cast_sbox6[(t[0] shr 8) and $FF] xor
cast_sbox7[t[3] shr 24] xor cast_sbox8[(t[3] shr 16) and $FF];
KeyData[i+1]:= cast_sbox5[(t[0] shr 16) and $FF] xor cast_sbox6[t[0] shr 24] xor
cast_sbox7[(t[3] shr 8) and $FF] xor cast_sbox8[t[3] and $FF];
KeyData[i+2]:= cast_sbox5[t[1] and $FF] xor cast_sbox6[(t[1] shr 8) and $FF] xor
cast_sbox7[t[2] shr 24] xor cast_sbox8[(t[2] shr 16) and $FF];
KeyData[i+3]:= cast_sbox5[(t[1] shr 16) and $FF] xor cast_sbox6[t[1] shr 24] xor
cast_sbox7[(t[2] shr 8) and $FF] xor cast_sbox8[t[2] and $FF];
end;
end;
case (i and 12) of
0:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[(z[0] shr 8) and $FF];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[(z[1] shr 8) and $FF];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[(z[2] shr 16) and $FF];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[z[3] shr 24];
end;
4:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[x[2] shr 24];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[(x[3] shr 16) and $FF];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[x[0] and $FF];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[x[1] and $FF];
end;
8:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[(z[2] shr 16) and $FF];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[z[3] shr 24];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[(z[0] shr 8) and $FF];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[(z[1] shr 8) and $FF];
end;
12:
begin
KeyData[i+0]:= KeyData[i+0] xor cast_sbox5[x[0] and $FF];
KeyData[i+1]:= KeyData[i+1] xor cast_sbox6[x[1] and $FF];
KeyData[i+2]:= KeyData[i+2] xor cast_sbox7[x[2] shr 24];
KeyData[i+3]:= KeyData[i+3] xor cast_sbox8[(x[3] shr 16) and $FF];
end;
end;
if (i >= 16) then
begin
KeyData[i+0]:= KeyData[i+0] and 31;
KeyData[i+1]:= KeyData[i+1] and 31;
KeyData[i+2]:= KeyData[i+2] and 31;
KeyData[i+3]:= KeyData[i+3] and 31;
end;
Inc(i,4);
end;
end;
procedure TDCP_cast128.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),$FF);
Rounds:= 0;
inherited Burn;
end;
procedure TDCP_cast128.EncryptECB(const InData; var OutData);
var
t, l, r: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
l:= Pdword(@InData)^;
r:= Pdword(Pointer(@InData)+4)^;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
t:= LRot32(KeyData[0]+r, KeyData[0+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[1] xor l, KeyData[1+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[2]-r, KeyData[2+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[3]+l, KeyData[3+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[4] xor r, KeyData[4+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[5]-l, KeyData[5+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[6]+r, KeyData[6+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[7] xor l, KeyData[7+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[8]-r, KeyData[8+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[9]+l, KeyData[9+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[10] xor r, KeyData[10+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[11]-l, KeyData[11+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
if Rounds> 12 then
begin
t:= LRot32(KeyData[12]+r, KeyData[12+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[13] xor l, KeyData[13+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[14]-r, KeyData[14+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[15]+l, KeyData[15+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
end;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
Pdword(@OutData)^:= r;
Pdword(pointer(@OutData)+4)^:= l;
end;
procedure TDCP_cast128.DecryptECB(const InData; var OutData);
var
t, l, r: DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
r:= Pdword(@InData)^;
l:= Pdword(pointer(@InData)+4)^;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
if Rounds> 12 then
begin
t:= LRot32(KeyData[15]+l, KeyData[15+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[14]-r, KeyData[14+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[13] xor l, KeyData[13+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[12]+r, KeyData[12+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
end;
t:= LRot32(KeyData[11]-l, KeyData[11+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[10] xor r, KeyData[10+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[9]+l, KeyData[9+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[8]-r, KeyData[8+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[7] xor l, KeyData[7+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[6]+r, KeyData[6+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[5]-l, KeyData[5+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[4] xor r, KeyData[4+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[3]+l, KeyData[3+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(KeyData[2]-r, KeyData[2+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(KeyData[1] xor l, KeyData[1+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(KeyData[0]+r, KeyData[0+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
Pdword(@OutData)^:= l;
Pdword(pointer(@OutData)+4)^:= r;
end;
end.

View File

@ -1,369 +1,369 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Cast256 ******************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPcast256;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_cast256= class(TDCP_blockcipher128)
protected
Kr, Km: array[0..11,0..3] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPcast256.inc}
function LRot32(a, n: dword): dword;
begin
Result:= (a shl n) or (a shr (32-n));
end;
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
function F1(a,rk,mk: DWord): DWord;
var
t: DWord;
begin
t:= LRot32(mk + a,rk);
Result:= ((S1[t shr 24] xor S2[(t shr 16) and $FF]) - S3[(t shr 8) and $FF]) + S4[t and $FF];
end;
function F2(a,rk,mk: DWord): DWord;
var
t: DWord;
begin
t:= LRot32(mk xor a,rk);
Result:= ((S1[t shr 24] - S2[(t shr 16) and $FF]) + S3[(t shr 8) and $FF]) xor S4[t and $FF];
end;
function F3(a,rk,mk: DWord): DWord;
var
t: DWord;
begin
t:= LRot32(mk - a,rk);
Result:= ((S1[t shr 24] + S2[(t shr 16) and $FF]) xor S3[(t shr 8) and $FF]) - S4[t and $FF];
end;
class function TDCP_cast256.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_cast256.GetId: integer;
begin
Result:= DCP_cast256;
end;
class function TDCP_cast256.GetAlgorithm: string;
begin
Result:= 'Cast256';
end;
class function TDCP_cast256.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($23,$42,$bb,$9e,$fa,$38,$54,$2c,$0a,$f7,$56,$47,$f2,$9f,$61,$5d);
InBlock1: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$0c,$9b,$28,$07);
OutBlock1: array[0..15] of byte=
($96,$3a,$8a,$50,$ce,$b5,$4d,$08,$e0,$de,$e0,$f1,$d0,$41,$3d,$cf);
Key2: array[0..23] of byte=
($23,$42,$bb,$9e,$fa,$38,$54,$2c,$be,$d0,$ac,$83,$94,$0a,$c2,$98,$ba,$c7,$7a,$77,$17,$94,$28,$63);
InBlock2: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$de,$25,$5a,$ff);
OutBlock2: array[0..15] of byte=
($2b,$c1,$92,$9f,$30,$13,$47,$a9,$9d,$3f,$3e,$45,$ad,$34,$01,$e8);
Key3: array[0..31] of byte=
($23,$42,$bb,$9e,$fa,$38,$54,$2c,$be,$d0,$ac,$83,$94,$0a,$c2,$98,$8d,$7c,$47,$ce,$26,$49,$08,$46,$1c,$c1,$b5,$13,$7a,$e6,$b6,$04);
InBlock3: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$c5,$fc,$eb,$19);
OutBlock3: array[0..15] of byte=
($1e,$2e,$bc,$6c,$9f,$2e,$43,$8e,$1d,$90,$d9,$b9,$c6,$85,$32,$86);
var
Block: array[0..15] of byte;
Cipher: TDCP_cast256;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_cast256.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InBlock1,Block);
Result:= boolean(CompareMem(@Block,@OutBlock1,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock1,16));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InBlock2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutBlock2,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock2,16));
Cipher.Burn;
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InBlock3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutBlock3,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock3,16));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_cast256.InitKey(const Key; Size: longword);
var
x: array[0..7] of DWord;
cm, cr: DWord;
i, j: longword;
tr, tm: array[0..7] of DWord;
begin
Size:= Size div 8;
dcpFillChar(x,Sizeof(x),0);
Move(Key,x,Size);
cm:= $5a827999;
cr:= 19;
for i:= 0 to 7 do
x[i]:= (x[i] shl 24) or ((x[i] shl 8) and $FF0000) or ((x[i] shr 8) and $FF00) or (x[i] shr 24);
for i:= 0 to 11 do
begin
for j:= 0 to 7 do
begin
tm[j]:= cm;
Inc(cm,$6ed9eba1);
tr[j]:= cr;
Inc(cr,17);
end;
x[6]:= x[6] xor f1(x[7],tr[0],tm[0]);
x[5]:= x[5] xor f2(x[6],tr[1],tm[1]);
x[4]:= x[4] xor f3(x[5],tr[2],tm[2]);
x[3]:= x[3] xor f1(x[4],tr[3],tm[3]);
x[2]:= x[2] xor f2(x[3],tr[4],tm[4]);
x[1]:= x[1] xor f3(x[2],tr[5],tm[5]);
x[0]:= x[0] xor f1(x[1],tr[6],tm[6]);
x[7]:= x[7] xor f2(x[0],tr[7],tm[7]);
for j:= 0 to 7 do
begin
tm[j]:= cm;
Inc(cm,$6ed9eba1);
tr[j]:= cr;
Inc(cr,17);
end;
x[6]:= x[6] xor f1(x[7],tr[0],tm[0]);
x[5]:= x[5] xor f2(x[6],tr[1],tm[1]);
x[4]:= x[4] xor f3(x[5],tr[2],tm[2]);
x[3]:= x[3] xor f1(x[4],tr[3],tm[3]);
x[2]:= x[2] xor f2(x[3],tr[4],tm[4]);
x[1]:= x[1] xor f3(x[2],tr[5],tm[5]);
x[0]:= x[0] xor f1(x[1],tr[6],tm[6]);
x[7]:= x[7] xor f2(x[0],tr[7],tm[7]);
Kr[i,0]:= x[0] and 31;
Kr[i,1]:= x[2] and 31;
Kr[i,2]:= x[4] and 31;
Kr[i,3]:= x[6] and 31;
Km[i,0]:= x[7];
Km[i,1]:= x[5];
Km[i,2]:= x[3];
Km[i,3]:= x[1];
end;
FillChar(x,Sizeof(x),$FF);
end;
procedure TDCP_cast256.Burn;
begin
FillChar(Kr,Sizeof(Kr),$FF);
FillChar(Km,Sizeof(Km),$FF);
inherited Burn;
end;
procedure TDCP_cast256.EncryptECB(const InData; var OutData);
var
A: array[0..3] of DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A[0]:= PDWord(@InData)^;
A[1]:= PDWord(pointer(@InData)+4)^;
A[2]:= PDWord(pointer(@InData)+8)^;
A[3]:= PDWord(pointer(@InData)+12)^;
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
A[2]:= A[2] xor f1(A[3],kr[0,0],km[0,0]);
A[1]:= A[1] xor f2(A[2],kr[0,1],km[0,1]);
A[0]:= A[0] xor f3(A[1],kr[0,2],km[0,2]);
A[3]:= A[3] xor f1(A[0],kr[0,3],km[0,3]);
A[2]:= A[2] xor f1(A[3],kr[1,0],km[1,0]);
A[1]:= A[1] xor f2(A[2],kr[1,1],km[1,1]);
A[0]:= A[0] xor f3(A[1],kr[1,2],km[1,2]);
A[3]:= A[3] xor f1(A[0],kr[1,3],km[1,3]);
A[2]:= A[2] xor f1(A[3],kr[2,0],km[2,0]);
A[1]:= A[1] xor f2(A[2],kr[2,1],km[2,1]);
A[0]:= A[0] xor f3(A[1],kr[2,2],km[2,2]);
A[3]:= A[3] xor f1(A[0],kr[2,3],km[2,3]);
A[2]:= A[2] xor f1(A[3],kr[3,0],km[3,0]);
A[1]:= A[1] xor f2(A[2],kr[3,1],km[3,1]);
A[0]:= A[0] xor f3(A[1],kr[3,2],km[3,2]);
A[3]:= A[3] xor f1(A[0],kr[3,3],km[3,3]);
A[2]:= A[2] xor f1(A[3],kr[4,0],km[4,0]);
A[1]:= A[1] xor f2(A[2],kr[4,1],km[4,1]);
A[0]:= A[0] xor f3(A[1],kr[4,2],km[4,2]);
A[3]:= A[3] xor f1(A[0],kr[4,3],km[4,3]);
A[2]:= A[2] xor f1(A[3],kr[5,0],km[5,0]);
A[1]:= A[1] xor f2(A[2],kr[5,1],km[5,1]);
A[0]:= A[0] xor f3(A[1],kr[5,2],km[5,2]);
A[3]:= A[3] xor f1(A[0],kr[5,3],km[5,3]);
A[3]:= A[3] xor f1(A[0],kr[6,3],km[6,3]);
A[0]:= A[0] xor f3(A[1],kr[6,2],km[6,2]);
A[1]:= A[1] xor f2(A[2],kr[6,1],km[6,1]);
A[2]:= A[2] xor f1(A[3],kr[6,0],km[6,0]);
A[3]:= A[3] xor f1(A[0],kr[7,3],km[7,3]);
A[0]:= A[0] xor f3(A[1],kr[7,2],km[7,2]);
A[1]:= A[1] xor f2(A[2],kr[7,1],km[7,1]);
A[2]:= A[2] xor f1(A[3],kr[7,0],km[7,0]);
A[3]:= A[3] xor f1(A[0],kr[8,3],km[8,3]);
A[0]:= A[0] xor f3(A[1],kr[8,2],km[8,2]);
A[1]:= A[1] xor f2(A[2],kr[8,1],km[8,1]);
A[2]:= A[2] xor f1(A[3],kr[8,0],km[8,0]);
A[3]:= A[3] xor f1(A[0],kr[9,3],km[9,3]);
A[0]:= A[0] xor f3(A[1],kr[9,2],km[9,2]);
A[1]:= A[1] xor f2(A[2],kr[9,1],km[9,1]);
A[2]:= A[2] xor f1(A[3],kr[9,0],km[9,0]);
A[3]:= A[3] xor f1(A[0],kr[10,3],km[10,3]);
A[0]:= A[0] xor f3(A[1],kr[10,2],km[10,2]);
A[1]:= A[1] xor f2(A[2],kr[10,1],km[10,1]);
A[2]:= A[2] xor f1(A[3],kr[10,0],km[10,0]);
A[3]:= A[3] xor f1(A[0],kr[11,3],km[11,3]);
A[0]:= A[0] xor f3(A[1],kr[11,2],km[11,2]);
A[1]:= A[1] xor f2(A[2],kr[11,1],km[11,1]);
A[2]:= A[2] xor f1(A[3],kr[11,0],km[11,0]);
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
PDWord(@OutData)^:= A[0];
PDWord(pointer(@OutData)+4)^:= A[1];
PDWord(pointer(@OutData)+8)^:= A[2];
PDWord(pointer(@OutData)+12)^:= A[3];
end;
procedure TDCP_cast256.DecryptECB(const InData; var OutData);
var
A: array[0..3] of DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A[0]:= PDWord(@InData)^;
A[1]:= PDWord(pointer(@InData)+4)^;
A[2]:= PDWord(pointer(@InData)+8)^;
A[3]:= PDWord(pointer(@InData)+12)^;
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
A[2]:= A[2] xor f1(A[3],kr[11,0],km[11,0]);
A[1]:= A[1] xor f2(A[2],kr[11,1],km[11,1]);
A[0]:= A[0] xor f3(A[1],kr[11,2],km[11,2]);
A[3]:= A[3] xor f1(A[0],kr[11,3],km[11,3]);
A[2]:= A[2] xor f1(A[3],kr[10,0],km[10,0]);
A[1]:= A[1] xor f2(A[2],kr[10,1],km[10,1]);
A[0]:= A[0] xor f3(A[1],kr[10,2],km[10,2]);
A[3]:= A[3] xor f1(A[0],kr[10,3],km[10,3]);
A[2]:= A[2] xor f1(A[3],kr[9,0],km[9,0]);
A[1]:= A[1] xor f2(A[2],kr[9,1],km[9,1]);
A[0]:= A[0] xor f3(A[1],kr[9,2],km[9,2]);
A[3]:= A[3] xor f1(A[0],kr[9,3],km[9,3]);
A[2]:= A[2] xor f1(A[3],kr[8,0],km[8,0]);
A[1]:= A[1] xor f2(A[2],kr[8,1],km[8,1]);
A[0]:= A[0] xor f3(A[1],kr[8,2],km[8,2]);
A[3]:= A[3] xor f1(A[0],kr[8,3],km[8,3]);
A[2]:= A[2] xor f1(A[3],kr[7,0],km[7,0]);
A[1]:= A[1] xor f2(A[2],kr[7,1],km[7,1]);
A[0]:= A[0] xor f3(A[1],kr[7,2],km[7,2]);
A[3]:= A[3] xor f1(A[0],kr[7,3],km[7,3]);
A[2]:= A[2] xor f1(A[3],kr[6,0],km[6,0]);
A[1]:= A[1] xor f2(A[2],kr[6,1],km[6,1]);
A[0]:= A[0] xor f3(A[1],kr[6,2],km[6,2]);
A[3]:= A[3] xor f1(A[0],kr[6,3],km[6,3]);
A[3]:= A[3] xor f1(A[0],kr[5,3],km[5,3]);
A[0]:= A[0] xor f3(A[1],kr[5,2],km[5,2]);
A[1]:= A[1] xor f2(A[2],kr[5,1],km[5,1]);
A[2]:= A[2] xor f1(A[3],kr[5,0],km[5,0]);
A[3]:= A[3] xor f1(A[0],kr[4,3],km[4,3]);
A[0]:= A[0] xor f3(A[1],kr[4,2],km[4,2]);
A[1]:= A[1] xor f2(A[2],kr[4,1],km[4,1]);
A[2]:= A[2] xor f1(A[3],kr[4,0],km[4,0]);
A[3]:= A[3] xor f1(A[0],kr[3,3],km[3,3]);
A[0]:= A[0] xor f3(A[1],kr[3,2],km[3,2]);
A[1]:= A[1] xor f2(A[2],kr[3,1],km[3,1]);
A[2]:= A[2] xor f1(A[3],kr[3,0],km[3,0]);
A[3]:= A[3] xor f1(A[0],kr[2,3],km[2,3]);
A[0]:= A[0] xor f3(A[1],kr[2,2],km[2,2]);
A[1]:= A[1] xor f2(A[2],kr[2,1],km[2,1]);
A[2]:= A[2] xor f1(A[3],kr[2,0],km[2,0]);
A[3]:= A[3] xor f1(A[0],kr[1,3],km[1,3]);
A[0]:= A[0] xor f3(A[1],kr[1,2],km[1,2]);
A[1]:= A[1] xor f2(A[2],kr[1,1],km[1,1]);
A[2]:= A[2] xor f1(A[3],kr[1,0],km[1,0]);
A[3]:= A[3] xor f1(A[0],kr[0,3],km[0,3]);
A[0]:= A[0] xor f3(A[1],kr[0,2],km[0,2]);
A[1]:= A[1] xor f2(A[2],kr[0,1],km[0,1]);
A[2]:= A[2] xor f1(A[3],kr[0,0],km[0,0]);
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
PDWord(@OutData)^:= A[0];
PDWord(pointer(@OutData)+4)^:= A[1];
PDWord(pointer(@OutData)+8)^:= A[2];
PDWord(pointer(@OutData)+12)^:= A[3];
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Cast256 ******************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPcast256;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_cast256= class(TDCP_blockcipher128)
protected
Kr, Km: array[0..11,0..3] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPcast256.inc}
function LRot32(a, n: dword): dword;
begin
Result:= (a shl n) or (a shr (32-n));
end;
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
function F1(a,rk,mk: DWord): DWord;
var
t: DWord;
begin
t:= LRot32(mk + a,rk);
Result:= ((S1[t shr 24] xor S2[(t shr 16) and $FF]) - S3[(t shr 8) and $FF]) + S4[t and $FF];
end;
function F2(a,rk,mk: DWord): DWord;
var
t: DWord;
begin
t:= LRot32(mk xor a,rk);
Result:= ((S1[t shr 24] - S2[(t shr 16) and $FF]) + S3[(t shr 8) and $FF]) xor S4[t and $FF];
end;
function F3(a,rk,mk: DWord): DWord;
var
t: DWord;
begin
t:= LRot32(mk - a,rk);
Result:= ((S1[t shr 24] + S2[(t shr 16) and $FF]) xor S3[(t shr 8) and $FF]) - S4[t and $FF];
end;
class function TDCP_cast256.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_cast256.GetId: integer;
begin
Result:= DCP_cast256;
end;
class function TDCP_cast256.GetAlgorithm: string;
begin
Result:= 'Cast256';
end;
class function TDCP_cast256.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($23,$42,$bb,$9e,$fa,$38,$54,$2c,$0a,$f7,$56,$47,$f2,$9f,$61,$5d);
InBlock1: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$0c,$9b,$28,$07);
OutBlock1: array[0..15] of byte=
($96,$3a,$8a,$50,$ce,$b5,$4d,$08,$e0,$de,$e0,$f1,$d0,$41,$3d,$cf);
Key2: array[0..23] of byte=
($23,$42,$bb,$9e,$fa,$38,$54,$2c,$be,$d0,$ac,$83,$94,$0a,$c2,$98,$ba,$c7,$7a,$77,$17,$94,$28,$63);
InBlock2: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$de,$25,$5a,$ff);
OutBlock2: array[0..15] of byte=
($2b,$c1,$92,$9f,$30,$13,$47,$a9,$9d,$3f,$3e,$45,$ad,$34,$01,$e8);
Key3: array[0..31] of byte=
($23,$42,$bb,$9e,$fa,$38,$54,$2c,$be,$d0,$ac,$83,$94,$0a,$c2,$98,$8d,$7c,$47,$ce,$26,$49,$08,$46,$1c,$c1,$b5,$13,$7a,$e6,$b6,$04);
InBlock3: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$c5,$fc,$eb,$19);
OutBlock3: array[0..15] of byte=
($1e,$2e,$bc,$6c,$9f,$2e,$43,$8e,$1d,$90,$d9,$b9,$c6,$85,$32,$86);
var
Block: array[0..15] of byte;
Cipher: TDCP_cast256;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_cast256.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InBlock1,Block);
Result:= boolean(CompareMem(@Block,@OutBlock1,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock1,16));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InBlock2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutBlock2,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock2,16));
Cipher.Burn;
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InBlock3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutBlock3,8));
Cipher.DecryptECB(Block,Block);
Result:= Result and boolean(CompareMem(@Block,@InBlock3,16));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_cast256.InitKey(const Key; Size: longword);
var
x: array[0..7] of DWord;
cm, cr: DWord;
i, j: longword;
tr, tm: array[0..7] of DWord;
begin
Size:= Size div 8;
dcpFillChar(x,Sizeof(x),0);
Move(Key,x,Size);
cm:= $5a827999;
cr:= 19;
for i:= 0 to 7 do
x[i]:= (x[i] shl 24) or ((x[i] shl 8) and $FF0000) or ((x[i] shr 8) and $FF00) or (x[i] shr 24);
for i:= 0 to 11 do
begin
for j:= 0 to 7 do
begin
tm[j]:= cm;
Inc(cm,$6ed9eba1);
tr[j]:= cr;
Inc(cr,17);
end;
x[6]:= x[6] xor f1(x[7],tr[0],tm[0]);
x[5]:= x[5] xor f2(x[6],tr[1],tm[1]);
x[4]:= x[4] xor f3(x[5],tr[2],tm[2]);
x[3]:= x[3] xor f1(x[4],tr[3],tm[3]);
x[2]:= x[2] xor f2(x[3],tr[4],tm[4]);
x[1]:= x[1] xor f3(x[2],tr[5],tm[5]);
x[0]:= x[0] xor f1(x[1],tr[6],tm[6]);
x[7]:= x[7] xor f2(x[0],tr[7],tm[7]);
for j:= 0 to 7 do
begin
tm[j]:= cm;
Inc(cm,$6ed9eba1);
tr[j]:= cr;
Inc(cr,17);
end;
x[6]:= x[6] xor f1(x[7],tr[0],tm[0]);
x[5]:= x[5] xor f2(x[6],tr[1],tm[1]);
x[4]:= x[4] xor f3(x[5],tr[2],tm[2]);
x[3]:= x[3] xor f1(x[4],tr[3],tm[3]);
x[2]:= x[2] xor f2(x[3],tr[4],tm[4]);
x[1]:= x[1] xor f3(x[2],tr[5],tm[5]);
x[0]:= x[0] xor f1(x[1],tr[6],tm[6]);
x[7]:= x[7] xor f2(x[0],tr[7],tm[7]);
Kr[i,0]:= x[0] and 31;
Kr[i,1]:= x[2] and 31;
Kr[i,2]:= x[4] and 31;
Kr[i,3]:= x[6] and 31;
Km[i,0]:= x[7];
Km[i,1]:= x[5];
Km[i,2]:= x[3];
Km[i,3]:= x[1];
end;
FillChar(x,Sizeof(x),$FF);
end;
procedure TDCP_cast256.Burn;
begin
FillChar(Kr,Sizeof(Kr),$FF);
FillChar(Km,Sizeof(Km),$FF);
inherited Burn;
end;
procedure TDCP_cast256.EncryptECB(const InData; var OutData);
var
A: array[0..3] of DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A[0]:= PDWord(@InData)^;
A[1]:= PDWord(pointer(@InData)+4)^;
A[2]:= PDWord(pointer(@InData)+8)^;
A[3]:= PDWord(pointer(@InData)+12)^;
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
A[2]:= A[2] xor f1(A[3],kr[0,0],km[0,0]);
A[1]:= A[1] xor f2(A[2],kr[0,1],km[0,1]);
A[0]:= A[0] xor f3(A[1],kr[0,2],km[0,2]);
A[3]:= A[3] xor f1(A[0],kr[0,3],km[0,3]);
A[2]:= A[2] xor f1(A[3],kr[1,0],km[1,0]);
A[1]:= A[1] xor f2(A[2],kr[1,1],km[1,1]);
A[0]:= A[0] xor f3(A[1],kr[1,2],km[1,2]);
A[3]:= A[3] xor f1(A[0],kr[1,3],km[1,3]);
A[2]:= A[2] xor f1(A[3],kr[2,0],km[2,0]);
A[1]:= A[1] xor f2(A[2],kr[2,1],km[2,1]);
A[0]:= A[0] xor f3(A[1],kr[2,2],km[2,2]);
A[3]:= A[3] xor f1(A[0],kr[2,3],km[2,3]);
A[2]:= A[2] xor f1(A[3],kr[3,0],km[3,0]);
A[1]:= A[1] xor f2(A[2],kr[3,1],km[3,1]);
A[0]:= A[0] xor f3(A[1],kr[3,2],km[3,2]);
A[3]:= A[3] xor f1(A[0],kr[3,3],km[3,3]);
A[2]:= A[2] xor f1(A[3],kr[4,0],km[4,0]);
A[1]:= A[1] xor f2(A[2],kr[4,1],km[4,1]);
A[0]:= A[0] xor f3(A[1],kr[4,2],km[4,2]);
A[3]:= A[3] xor f1(A[0],kr[4,3],km[4,3]);
A[2]:= A[2] xor f1(A[3],kr[5,0],km[5,0]);
A[1]:= A[1] xor f2(A[2],kr[5,1],km[5,1]);
A[0]:= A[0] xor f3(A[1],kr[5,2],km[5,2]);
A[3]:= A[3] xor f1(A[0],kr[5,3],km[5,3]);
A[3]:= A[3] xor f1(A[0],kr[6,3],km[6,3]);
A[0]:= A[0] xor f3(A[1],kr[6,2],km[6,2]);
A[1]:= A[1] xor f2(A[2],kr[6,1],km[6,1]);
A[2]:= A[2] xor f1(A[3],kr[6,0],km[6,0]);
A[3]:= A[3] xor f1(A[0],kr[7,3],km[7,3]);
A[0]:= A[0] xor f3(A[1],kr[7,2],km[7,2]);
A[1]:= A[1] xor f2(A[2],kr[7,1],km[7,1]);
A[2]:= A[2] xor f1(A[3],kr[7,0],km[7,0]);
A[3]:= A[3] xor f1(A[0],kr[8,3],km[8,3]);
A[0]:= A[0] xor f3(A[1],kr[8,2],km[8,2]);
A[1]:= A[1] xor f2(A[2],kr[8,1],km[8,1]);
A[2]:= A[2] xor f1(A[3],kr[8,0],km[8,0]);
A[3]:= A[3] xor f1(A[0],kr[9,3],km[9,3]);
A[0]:= A[0] xor f3(A[1],kr[9,2],km[9,2]);
A[1]:= A[1] xor f2(A[2],kr[9,1],km[9,1]);
A[2]:= A[2] xor f1(A[3],kr[9,0],km[9,0]);
A[3]:= A[3] xor f1(A[0],kr[10,3],km[10,3]);
A[0]:= A[0] xor f3(A[1],kr[10,2],km[10,2]);
A[1]:= A[1] xor f2(A[2],kr[10,1],km[10,1]);
A[2]:= A[2] xor f1(A[3],kr[10,0],km[10,0]);
A[3]:= A[3] xor f1(A[0],kr[11,3],km[11,3]);
A[0]:= A[0] xor f3(A[1],kr[11,2],km[11,2]);
A[1]:= A[1] xor f2(A[2],kr[11,1],km[11,1]);
A[2]:= A[2] xor f1(A[3],kr[11,0],km[11,0]);
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
PDWord(@OutData)^:= A[0];
PDWord(pointer(@OutData)+4)^:= A[1];
PDWord(pointer(@OutData)+8)^:= A[2];
PDWord(pointer(@OutData)+12)^:= A[3];
end;
procedure TDCP_cast256.DecryptECB(const InData; var OutData);
var
A: array[0..3] of DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A[0]:= PDWord(@InData)^;
A[1]:= PDWord(pointer(@InData)+4)^;
A[2]:= PDWord(pointer(@InData)+8)^;
A[3]:= PDWord(pointer(@InData)+12)^;
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
A[2]:= A[2] xor f1(A[3],kr[11,0],km[11,0]);
A[1]:= A[1] xor f2(A[2],kr[11,1],km[11,1]);
A[0]:= A[0] xor f3(A[1],kr[11,2],km[11,2]);
A[3]:= A[3] xor f1(A[0],kr[11,3],km[11,3]);
A[2]:= A[2] xor f1(A[3],kr[10,0],km[10,0]);
A[1]:= A[1] xor f2(A[2],kr[10,1],km[10,1]);
A[0]:= A[0] xor f3(A[1],kr[10,2],km[10,2]);
A[3]:= A[3] xor f1(A[0],kr[10,3],km[10,3]);
A[2]:= A[2] xor f1(A[3],kr[9,0],km[9,0]);
A[1]:= A[1] xor f2(A[2],kr[9,1],km[9,1]);
A[0]:= A[0] xor f3(A[1],kr[9,2],km[9,2]);
A[3]:= A[3] xor f1(A[0],kr[9,3],km[9,3]);
A[2]:= A[2] xor f1(A[3],kr[8,0],km[8,0]);
A[1]:= A[1] xor f2(A[2],kr[8,1],km[8,1]);
A[0]:= A[0] xor f3(A[1],kr[8,2],km[8,2]);
A[3]:= A[3] xor f1(A[0],kr[8,3],km[8,3]);
A[2]:= A[2] xor f1(A[3],kr[7,0],km[7,0]);
A[1]:= A[1] xor f2(A[2],kr[7,1],km[7,1]);
A[0]:= A[0] xor f3(A[1],kr[7,2],km[7,2]);
A[3]:= A[3] xor f1(A[0],kr[7,3],km[7,3]);
A[2]:= A[2] xor f1(A[3],kr[6,0],km[6,0]);
A[1]:= A[1] xor f2(A[2],kr[6,1],km[6,1]);
A[0]:= A[0] xor f3(A[1],kr[6,2],km[6,2]);
A[3]:= A[3] xor f1(A[0],kr[6,3],km[6,3]);
A[3]:= A[3] xor f1(A[0],kr[5,3],km[5,3]);
A[0]:= A[0] xor f3(A[1],kr[5,2],km[5,2]);
A[1]:= A[1] xor f2(A[2],kr[5,1],km[5,1]);
A[2]:= A[2] xor f1(A[3],kr[5,0],km[5,0]);
A[3]:= A[3] xor f1(A[0],kr[4,3],km[4,3]);
A[0]:= A[0] xor f3(A[1],kr[4,2],km[4,2]);
A[1]:= A[1] xor f2(A[2],kr[4,1],km[4,1]);
A[2]:= A[2] xor f1(A[3],kr[4,0],km[4,0]);
A[3]:= A[3] xor f1(A[0],kr[3,3],km[3,3]);
A[0]:= A[0] xor f3(A[1],kr[3,2],km[3,2]);
A[1]:= A[1] xor f2(A[2],kr[3,1],km[3,1]);
A[2]:= A[2] xor f1(A[3],kr[3,0],km[3,0]);
A[3]:= A[3] xor f1(A[0],kr[2,3],km[2,3]);
A[0]:= A[0] xor f3(A[1],kr[2,2],km[2,2]);
A[1]:= A[1] xor f2(A[2],kr[2,1],km[2,1]);
A[2]:= A[2] xor f1(A[3],kr[2,0],km[2,0]);
A[3]:= A[3] xor f1(A[0],kr[1,3],km[1,3]);
A[0]:= A[0] xor f3(A[1],kr[1,2],km[1,2]);
A[1]:= A[1] xor f2(A[2],kr[1,1],km[1,1]);
A[2]:= A[2] xor f1(A[3],kr[1,0],km[1,0]);
A[3]:= A[3] xor f1(A[0],kr[0,3],km[0,3]);
A[0]:= A[0] xor f3(A[1],kr[0,2],km[0,2]);
A[1]:= A[1] xor f2(A[2],kr[0,1],km[0,1]);
A[2]:= A[2] xor f1(A[3],kr[0,0],km[0,0]);
A[0]:= SwapDWord(A[0]);
A[1]:= SwapDWord(A[1]);
A[2]:= SwapDWord(A[2]);
A[3]:= SwapDWord(A[3]);
PDWord(@OutData)^:= A[0];
PDWord(pointer(@OutData)+4)^:= A[1];
PDWord(pointer(@OutData)+8)^:= A[2];
PDWord(pointer(@OutData)+12)^:= A[3];
end;
end.

View File

@ -1,489 +1,489 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of DES and Triple DES *******************}
{* Based on C source code by Eric Young ***************************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
{******************************************************************************}
{* This implementation of DES is based on the C implementation by *}
{* Eric Young (eay@mincom.oz.au) *}
{******************************************************************************}
{* DES takes a 64bit key and discards every 8th bit (56bit effectively) *}
{* 3DES takes either a <= 128bit key and uses one key twice or takes a *}
{* <= 192bit key and uses each once (again discarding every 8th bit) *}
{******************************************************************************}
unit DCPdes;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_customdes= class(TDCP_blockcipher64)
protected
procedure DoInit(KeyB: PByteArray; KeyData: PDWordArray);
procedure EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
procedure DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
end;
type
TDCP_des= class(TDCP_customdes)
protected
KeyData: array[0..31] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
TDCP_3des= class(TDCP_customdes)
protected
KeyData: array[0..2,0..31] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPdes.inc}
procedure hperm_op(var a, t: dword; n, m: dword);
begin
t:= ((a shl (16 - n)) xor a) and m;
a:= a xor t xor (t shr (16 - n));
end;
procedure perm_op(var a, b, t: dword; n, m: dword);
begin
t:= ((a shr n) xor b) and m;
b:= b xor t;
a:= a xor (t shl n);
end;
procedure TDCP_customdes.DoInit(KeyB: PByteArray; KeyData: PDwordArray);
var
c, d, t, s, t2, i: dword;
begin
t := 0;
c:= KeyB^[0] or (KeyB^[1] shl 8) or (KeyB^[2] shl 16) or (KeyB^[3] shl 24);
d:= KeyB^[4] or (KeyB^[5] shl 8) or (KeyB^[6] shl 16) or (KeyB^[7] shl 24);
perm_op(d,c,t,4,$0f0f0f0f);
hperm_op(c,t,dword(-2),$cccc0000);
hperm_op(d,t,dword(-2),$cccc0000);
perm_op(d,c,t,1,$55555555);
perm_op(c,d,t,8,$00ff00ff);
perm_op(d,c,t,1,$55555555);
d:= ((d and $ff) shl 16) or (d and $ff00) or ((d and $ff0000) shr 16) or
((c and $f0000000) shr 4);
c:= c and $fffffff;
for i:= 0 to 15 do
begin
if shifts2[i]<> 0 then
begin
c:= ((c shr 2) or (c shl 26));
d:= ((d shr 2) or (d shl 26));
end
else
begin
c:= ((c shr 1) or (c shl 27));
d:= ((d shr 1) or (d shl 27));
end;
c:= c and $fffffff;
d:= d and $fffffff;
s:= des_skb[0,c and $3f] or
des_skb[1,((c shr 6) and $03) or ((c shr 7) and $3c)] or
des_skb[2,((c shr 13) and $0f) or ((c shr 14) and $30)] or
des_skb[3,((c shr 20) and $01) or ((c shr 21) and $06) or ((c shr 22) and $38)];
t:= des_skb[4,d and $3f] or
des_skb[5,((d shr 7) and $03) or ((d shr 8) and $3c)] or
des_skb[6, (d shr 15) and $3f ] or
des_skb[7,((d shr 21) and $0f) or ((d shr 22) and $30)];
t2:= ((t shl 16) or (s and $ffff));
KeyData^[(i shl 1)]:= ((t2 shl 2) or (t2 shr 30));
t2:= ((s shr 16) or (t and $ffff0000));
KeyData^[(i shl 1)+1]:= ((t2 shl 6) or (t2 shr 26));
end;
end;
procedure TDCP_customdes.EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
var
l, r, t, u: dword;
i: longint;
begin
r:= PDword(@InData)^;
l:= PDword(pointer(@InData)+4)^;
t:= ((l shr 4) xor r) and $0f0f0f0f;
r:= r xor t;
l:= l xor (t shl 4);
t:= ((r shr 16) xor l) and $0000ffff;
l:= l xor t;
r:= r xor (t shl 16);
t:= ((l shr 2) xor r) and $33333333;
r:= r xor t;
l:= l xor (t shl 2);
t:= ((r shr 8) xor l) and $00ff00ff;
l:= l xor t;
r:= r xor (t shl 8);
t:= ((l shr 1) xor r) and $55555555;
r:= r xor t;
l:= l xor (t shl 1);
r:= (r shr 29) or (r shl 3);
l:= (l shr 29) or (l shl 3);
i:= 0;
while i< 32 do
begin
u:= r xor KeyData^[i ];
t:= r xor KeyData^[i+1];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i+2];
t:= l xor KeyData^[i+3];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= r xor KeyData^[i+4];
t:= r xor KeyData^[i+5];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i+6];
t:= l xor KeyData^[i+7];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
Inc(i,8);
end;
r:= (r shr 3) or (r shl 29);
l:= (l shr 3) or (l shl 29);
t:= ((r shr 1) xor l) and $55555555;
l:= l xor t;
r:= r xor (t shl 1);
t:= ((l shr 8) xor r) and $00ff00ff;
r:= r xor t;
l:= l xor (t shl 8);
t:= ((r shr 2) xor l) and $33333333;
l:= l xor t;
r:= r xor (t shl 2);
t:= ((l shr 16) xor r) and $0000ffff;
r:= r xor t;
l:= l xor (t shl 16);
t:= ((r shr 4) xor l) and $0f0f0f0f;
l:= l xor t;
r:= r xor (t shl 4);
PDword(@OutData)^:= l;
PDword(pointer(@OutData)+4)^:= r;
end;
procedure TDCP_customdes.DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
var
l, r, t, u: dword;
i: longint;
begin
r:= PDword(@InData)^;
l:= PDword(pointer(@InData)+4)^;
t:= ((l shr 4) xor r) and $0f0f0f0f;
r:= r xor t;
l:= l xor (t shl 4);
t:= ((r shr 16) xor l) and $0000ffff;
l:= l xor t;
r:= r xor (t shl 16);
t:= ((l shr 2) xor r) and $33333333;
r:= r xor t;
l:= l xor (t shl 2);
t:= ((r shr 8) xor l) and $00ff00ff;
l:= l xor t;
r:= r xor (t shl 8);
t:= ((l shr 1) xor r) and $55555555;
r:= r xor t;
l:= l xor (t shl 1);
r:= (r shr 29) or (r shl 3);
l:= (l shr 29) or (l shl 3);
i:= 30;
while i> 0 do
begin
u:= r xor KeyData^[i ];
t:= r xor KeyData^[i+1];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i-2];
t:= l xor KeyData^[i-1];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= r xor KeyData^[i-4];
t:= r xor KeyData^[i-3];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i-6];
t:= l xor KeyData^[i-5];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
Dec(i,8);
end;
r:= (r shr 3) or (r shl 29);
l:= (l shr 3) or (l shl 29);
t:= ((r shr 1) xor l) and $55555555;
l:= l xor t;
r:= r xor (t shl 1);
t:= ((l shr 8) xor r) and $00ff00ff;
r:= r xor t;
l:= l xor (t shl 8);
t:= ((r shr 2) xor l) and $33333333;
l:= l xor t;
r:= r xor (t shl 2);
t:= ((l shr 16) xor r) and $0000ffff;
r:= r xor t;
l:= l xor (t shl 16);
t:= ((r shr 4) xor l) and $0f0f0f0f;
l:= l xor t;
r:= r xor (t shl 4);
PDword(@OutData)^:= l;
PDword(pointer(@OutData)+4)^:= r;
end;
class function TDCP_des.GetMaxKeySize: integer;
begin
Result:= 64;
end;
class function TDCP_des.GetID: integer;
begin
Result:= DCP_des;
end;
class function TDCP_des.GetAlgorithm: string;
begin
Result:= 'DES';
end;
class function TDCP_des.SelfTest: boolean;
const
InData1: array[0..7] of byte=
($07,$56,$D8,$E0,$77,$47,$61,$D2);
OutData1: array[0..7] of byte=
($0C,$D3,$DA,$02,$00,$21,$DC,$09);
Key1: array[0..7] of byte=
($01,$70,$F1,$75,$46,$8F,$B5,$E6);
InData2: array[0..7] of byte=
($48,$0D,$39,$00,$6E,$E7,$62,$F2);
OutData2: array[0..7] of byte=
($A1,$F9,$91,$55,$41,$02,$0B,$56);
Key2: array[0..7] of byte=
($02,$58,$16,$16,$46,$29,$B0,$07);
var
Cipher: TDCP_des;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_des.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= Result and boolean(CompareMem(@Data,@OutData2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_des.InitKey(const Key; Size: longword);
var
KeyB: array[0..7] of byte;
begin
dcpFillChar(KeyB,Sizeof(KeyB),0);
Move(Key,KeyB,Size div 8);
DoInit(@KeyB,@KeyData);
end;
procedure TDCP_des.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_des.EncryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
EncryptBlock(InData,OutData,@KeyData);
end;
procedure TDCP_des.DecryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
DecryptBlock(InData,OutData,@KeyData);
end;
{******************************************************************************}
class function TDCP_3des.GetMaxKeySize: integer;
begin
Result:= 192;
end;
class function TDCP_3des.GetID: integer;
begin
Result:= DCP_3des;
end;
class function TDCP_3des.GetAlgorithm: string;
begin
Result:= '3DES';
end;
class function TDCP_3des.SelfTest: boolean;
const
Key: array[0..23] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$ef,$fe,$dc,$ba,$98,
$76,$54,$32,$10,$89,$ab,$cd,$ef,$01,$23,$45,$67);
PlainText: array[0..7] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$e7);
CipherText: array[0..7] of byte=
($de,$0b,$7c,$06,$ae,$5e,$0e,$d5);
var
Cipher: TDCP_3des;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_3des.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(PlainText,Block);
Result:= CompareMem(@Block,@CipherText,Sizeof(CipherText));
Cipher.DecryptECB(Block,Block);
Result:= Result and CompareMem(@Block,@PlainText,Sizeof(PlainText));
Cipher.Free;
end;
procedure TDCP_3des.InitKey(const Key; Size: longword);
var
KeyB: array[0..2,0..7] of byte;
begin
dcpFillChar(KeyB,Sizeof(KeyB),0);
Move(Key,KeyB,Size div 8);
DoInit(@KeyB[0],@KeyData[0]);
DoInit(@KeyB[1],@KeyData[1]);
if Size> 128 then
DoInit(@KeyB[2],@KeyData[2])
else
Move(KeyData[0],KeyData[2],128);
end;
procedure TDCP_3des.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_3des.EncryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
EncryptBlock(InData,OutData,@KeyData[0]);
DecryptBlock(OutData,OutData,@KeyData[1]);
EncryptBlock(OutData,OutData,@KeyData[2]);
end;
procedure TDCP_3des.DecryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
DecryptBlock(InData,OutData,@KeyData[2]);
EncryptBlock(OutData,OutData,@KeyData[1]);
DecryptBlock(OutData,OutData,@KeyData[0]);
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of DES and Triple DES *******************}
{* Based on C source code by Eric Young ***************************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
{******************************************************************************}
{* This implementation of DES is based on the C implementation by *}
{* Eric Young (eay@mincom.oz.au) *}
{******************************************************************************}
{* DES takes a 64bit key and discards every 8th bit (56bit effectively) *}
{* 3DES takes either a <= 128bit key and uses one key twice or takes a *}
{* <= 192bit key and uses each once (again discarding every 8th bit) *}
{******************************************************************************}
unit DCPdes;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_customdes= class(TDCP_blockcipher64)
protected
procedure DoInit(KeyB: PByteArray; KeyData: PDWordArray);
procedure EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
procedure DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
end;
type
TDCP_des= class(TDCP_customdes)
protected
KeyData: array[0..31] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
TDCP_3des= class(TDCP_customdes)
protected
KeyData: array[0..2,0..31] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPdes.inc}
procedure hperm_op(var a, t: dword; n, m: dword);
begin
t:= ((a shl (16 - n)) xor a) and m;
a:= a xor t xor (t shr (16 - n));
end;
procedure perm_op(var a, b, t: dword; n, m: dword);
begin
t:= ((a shr n) xor b) and m;
b:= b xor t;
a:= a xor (t shl n);
end;
procedure TDCP_customdes.DoInit(KeyB: PByteArray; KeyData: PDwordArray);
var
c, d, t, s, t2, i: dword;
begin
t := 0;
c:= KeyB^[0] or (KeyB^[1] shl 8) or (KeyB^[2] shl 16) or (KeyB^[3] shl 24);
d:= KeyB^[4] or (KeyB^[5] shl 8) or (KeyB^[6] shl 16) or (KeyB^[7] shl 24);
perm_op(d,c,t,4,$0f0f0f0f);
hperm_op(c,t,dword(-2),$cccc0000);
hperm_op(d,t,dword(-2),$cccc0000);
perm_op(d,c,t,1,$55555555);
perm_op(c,d,t,8,$00ff00ff);
perm_op(d,c,t,1,$55555555);
d:= ((d and $ff) shl 16) or (d and $ff00) or ((d and $ff0000) shr 16) or
((c and $f0000000) shr 4);
c:= c and $fffffff;
for i:= 0 to 15 do
begin
if shifts2[i]<> 0 then
begin
c:= ((c shr 2) or (c shl 26));
d:= ((d shr 2) or (d shl 26));
end
else
begin
c:= ((c shr 1) or (c shl 27));
d:= ((d shr 1) or (d shl 27));
end;
c:= c and $fffffff;
d:= d and $fffffff;
s:= des_skb[0,c and $3f] or
des_skb[1,((c shr 6) and $03) or ((c shr 7) and $3c)] or
des_skb[2,((c shr 13) and $0f) or ((c shr 14) and $30)] or
des_skb[3,((c shr 20) and $01) or ((c shr 21) and $06) or ((c shr 22) and $38)];
t:= des_skb[4,d and $3f] or
des_skb[5,((d shr 7) and $03) or ((d shr 8) and $3c)] or
des_skb[6, (d shr 15) and $3f ] or
des_skb[7,((d shr 21) and $0f) or ((d shr 22) and $30)];
t2:= ((t shl 16) or (s and $ffff));
KeyData^[(i shl 1)]:= ((t2 shl 2) or (t2 shr 30));
t2:= ((s shr 16) or (t and $ffff0000));
KeyData^[(i shl 1)+1]:= ((t2 shl 6) or (t2 shr 26));
end;
end;
procedure TDCP_customdes.EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
var
l, r, t, u: dword;
i: longint;
begin
r:= PDword(@InData)^;
l:= PDword(pointer(@InData)+4)^;
t:= ((l shr 4) xor r) and $0f0f0f0f;
r:= r xor t;
l:= l xor (t shl 4);
t:= ((r shr 16) xor l) and $0000ffff;
l:= l xor t;
r:= r xor (t shl 16);
t:= ((l shr 2) xor r) and $33333333;
r:= r xor t;
l:= l xor (t shl 2);
t:= ((r shr 8) xor l) and $00ff00ff;
l:= l xor t;
r:= r xor (t shl 8);
t:= ((l shr 1) xor r) and $55555555;
r:= r xor t;
l:= l xor (t shl 1);
r:= (r shr 29) or (r shl 3);
l:= (l shr 29) or (l shl 3);
i:= 0;
while i< 32 do
begin
u:= r xor KeyData^[i ];
t:= r xor KeyData^[i+1];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i+2];
t:= l xor KeyData^[i+3];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= r xor KeyData^[i+4];
t:= r xor KeyData^[i+5];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i+6];
t:= l xor KeyData^[i+7];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
Inc(i,8);
end;
r:= (r shr 3) or (r shl 29);
l:= (l shr 3) or (l shl 29);
t:= ((r shr 1) xor l) and $55555555;
l:= l xor t;
r:= r xor (t shl 1);
t:= ((l shr 8) xor r) and $00ff00ff;
r:= r xor t;
l:= l xor (t shl 8);
t:= ((r shr 2) xor l) and $33333333;
l:= l xor t;
r:= r xor (t shl 2);
t:= ((l shr 16) xor r) and $0000ffff;
r:= r xor t;
l:= l xor (t shl 16);
t:= ((r shr 4) xor l) and $0f0f0f0f;
l:= l xor t;
r:= r xor (t shl 4);
PDword(@OutData)^:= l;
PDword(pointer(@OutData)+4)^:= r;
end;
procedure TDCP_customdes.DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
var
l, r, t, u: dword;
i: longint;
begin
r:= PDword(@InData)^;
l:= PDword(pointer(@InData)+4)^;
t:= ((l shr 4) xor r) and $0f0f0f0f;
r:= r xor t;
l:= l xor (t shl 4);
t:= ((r shr 16) xor l) and $0000ffff;
l:= l xor t;
r:= r xor (t shl 16);
t:= ((l shr 2) xor r) and $33333333;
r:= r xor t;
l:= l xor (t shl 2);
t:= ((r shr 8) xor l) and $00ff00ff;
l:= l xor t;
r:= r xor (t shl 8);
t:= ((l shr 1) xor r) and $55555555;
r:= r xor t;
l:= l xor (t shl 1);
r:= (r shr 29) or (r shl 3);
l:= (l shr 29) or (l shl 3);
i:= 30;
while i> 0 do
begin
u:= r xor KeyData^[i ];
t:= r xor KeyData^[i+1];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i-2];
t:= l xor KeyData^[i-1];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= r xor KeyData^[i-4];
t:= r xor KeyData^[i-3];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i-6];
t:= l xor KeyData^[i-5];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
Dec(i,8);
end;
r:= (r shr 3) or (r shl 29);
l:= (l shr 3) or (l shl 29);
t:= ((r shr 1) xor l) and $55555555;
l:= l xor t;
r:= r xor (t shl 1);
t:= ((l shr 8) xor r) and $00ff00ff;
r:= r xor t;
l:= l xor (t shl 8);
t:= ((r shr 2) xor l) and $33333333;
l:= l xor t;
r:= r xor (t shl 2);
t:= ((l shr 16) xor r) and $0000ffff;
r:= r xor t;
l:= l xor (t shl 16);
t:= ((r shr 4) xor l) and $0f0f0f0f;
l:= l xor t;
r:= r xor (t shl 4);
PDword(@OutData)^:= l;
PDword(pointer(@OutData)+4)^:= r;
end;
class function TDCP_des.GetMaxKeySize: integer;
begin
Result:= 64;
end;
class function TDCP_des.GetID: integer;
begin
Result:= DCP_des;
end;
class function TDCP_des.GetAlgorithm: string;
begin
Result:= 'DES';
end;
class function TDCP_des.SelfTest: boolean;
const
InData1: array[0..7] of byte=
($07,$56,$D8,$E0,$77,$47,$61,$D2);
OutData1: array[0..7] of byte=
($0C,$D3,$DA,$02,$00,$21,$DC,$09);
Key1: array[0..7] of byte=
($01,$70,$F1,$75,$46,$8F,$B5,$E6);
InData2: array[0..7] of byte=
($48,$0D,$39,$00,$6E,$E7,$62,$F2);
OutData2: array[0..7] of byte=
($A1,$F9,$91,$55,$41,$02,$0B,$56);
Key2: array[0..7] of byte=
($02,$58,$16,$16,$46,$29,$B0,$07);
var
Cipher: TDCP_des;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_des.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= Result and boolean(CompareMem(@Data,@OutData2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_des.InitKey(const Key; Size: longword);
var
KeyB: array[0..7] of byte;
begin
dcpFillChar(KeyB,Sizeof(KeyB),0);
Move(Key,KeyB,Size div 8);
DoInit(@KeyB,@KeyData);
end;
procedure TDCP_des.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_des.EncryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
EncryptBlock(InData,OutData,@KeyData);
end;
procedure TDCP_des.DecryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
DecryptBlock(InData,OutData,@KeyData);
end;
{******************************************************************************}
class function TDCP_3des.GetMaxKeySize: integer;
begin
Result:= 192;
end;
class function TDCP_3des.GetID: integer;
begin
Result:= DCP_3des;
end;
class function TDCP_3des.GetAlgorithm: string;
begin
Result:= '3DES';
end;
class function TDCP_3des.SelfTest: boolean;
const
Key: array[0..23] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$ef,$fe,$dc,$ba,$98,
$76,$54,$32,$10,$89,$ab,$cd,$ef,$01,$23,$45,$67);
PlainText: array[0..7] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$e7);
CipherText: array[0..7] of byte=
($de,$0b,$7c,$06,$ae,$5e,$0e,$d5);
var
Cipher: TDCP_3des;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_3des.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(PlainText,Block);
Result:= CompareMem(@Block,@CipherText,Sizeof(CipherText));
Cipher.DecryptECB(Block,Block);
Result:= Result and CompareMem(@Block,@PlainText,Sizeof(PlainText));
Cipher.Free;
end;
procedure TDCP_3des.InitKey(const Key; Size: longword);
var
KeyB: array[0..2,0..7] of byte;
begin
dcpFillChar(KeyB,Sizeof(KeyB),0);
Move(Key,KeyB,Size div 8);
DoInit(@KeyB[0],@KeyData[0]);
DoInit(@KeyB[1],@KeyData[1]);
if Size> 128 then
DoInit(@KeyB[2],@KeyData[2])
else
Move(KeyData[0],KeyData[2],128);
end;
procedure TDCP_3des.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_3des.EncryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
EncryptBlock(InData,OutData,@KeyData[0]);
DecryptBlock(OutData,OutData,@KeyData[1]);
EncryptBlock(OutData,OutData,@KeyData[2]);
end;
procedure TDCP_3des.DecryptECB(const InData; var OutData);
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
DecryptBlock(InData,OutData,@KeyData[2]);
EncryptBlock(OutData,OutData,@KeyData[1]);
DecryptBlock(OutData,OutData,@KeyData[0]);
end;
end.

View File

@ -1,232 +1,232 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Gost *********************************}
{******************************************************************************}
{******************************************************************************}
{* !!WARNING!! !!WARNING!! !!WARNING!!!!WARNING!! !!WARNING!! !!WARNING!! *}
{* *}
{* I am not sure if this is compatible with the actual GOST standard *}
{* as a result, use this implementation with extreme caution. *}
{* *}
{* !!WARNING!! !!WARNING!! !!WARNING!!!!WARNING!! !!WARNING!! !!WARNING!! *}
{******************************************************************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPgost;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_gost= class(TDCP_blockcipher64)
protected
KeyData: array[0..7] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPgost.inc}
class function TDCP_gost.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_gost.GetID: integer;
begin
Result:= DCP_gost;
end;
class function TDCP_gost.GetAlgorithm: string;
begin
Result:= 'Gost';
end;
class function TDCP_gost.SelfTest: boolean;
const
Key1: array[0..31] of byte=
($BE,$5E,$C2,$00,$6C,$FF,$9D,$CF,$52,$35,$49,$59,$F1,$FF,$0C,$BF,
$E9,$50,$61,$B5,$A6,$48,$C1,$03,$87,$06,$9C,$25,$99,$7C,$06,$72);
InData1: array[0..7] of byte=
($0D,$F8,$28,$02,$B7,$41,$A2,$92);
OutData1: array[0..7] of byte=
($07,$F9,$02,$7D,$F7,$F7,$DF,$89);
Key2: array[0..31] of byte=
($B3,$85,$27,$2A,$C8,$D7,$2A,$5A,$8B,$34,$4B,$C8,$03,$63,$AC,$4D,
$09,$BF,$58,$F4,$1F,$54,$06,$24,$CB,$CB,$8F,$DC,$F5,$53,$07,$D7);
InData2: array[0..7] of byte=
($13,$54,$EE,$9C,$0A,$11,$CD,$4C);
OutData2: array[0..7] of byte=
($4F,$B5,$05,$36,$F9,$60,$A7,$B1);
var
Block: array[0..7] of byte;
Cipher: TDCP_gost;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_gost.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,8));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,8));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,8));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,8));
Cipher.Free;
end;
procedure TDCP_gost.InitKey(const Key; Size: longword);
var
i: longword;
userkey: array[0..31] of byte;
begin
Size:= Size div 8;
dcpFillChar(userkey,Sizeof(userkey),0);
Move(Key,userkey,Size);
for i:= 0 to 7 do
KeyData[i]:= (dword(UserKey[4*i+3]) shl 24) or (dword(UserKey[4*i+2]) shl 16) or
(dword(UserKey[4*i+1]) shl 8) or (dword(UserKey[4*i+0]));
end;
procedure TDCP_gost.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_gost.EncryptECB(const InData; var OutData);
var
n1, n2: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
n1:= PDword(@InData)^;
n2:= PDword(pointer(@InData)+4)^;
for i:= 0 to 2 do
begin
n2:= n2 xor (sTable[3,(n1+KeyData[0]) shr 24] xor sTable[2,((n1+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[0]) shr 8) and $FF] xor sTable[0,(n1+KeyData[0]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[1]) shr 24] xor sTable[2,((n2+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[1]) shr 8) and $FF] xor sTable[0,(n2+KeyData[1]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[2]) shr 24] xor sTable[2,((n1+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[2]) shr 8) and $FF] xor sTable[0,(n1+KeyData[2]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[3]) shr 24] xor sTable[2,((n2+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[3]) shr 8) and $FF] xor sTable[0,(n2+KeyData[3]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[4]) shr 24] xor sTable[2,((n1+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[4]) shr 8) and $FF] xor sTable[0,(n1+KeyData[4]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[5]) shr 24] xor sTable[2,((n2+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[5]) shr 8) and $FF] xor sTable[0,(n2+KeyData[5]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[6]) shr 24] xor sTable[2,((n1+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[6]) shr 8) and $FF] xor sTable[0,(n1+KeyData[6]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[7]) shr 24] xor sTable[2,((n2+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[7]) shr 8) and $FF] xor sTable[0,(n2+KeyData[7]) and $FF]);
end;
n2:= n2 xor (sTable[3,(n1+KeyData[7]) shr 24] xor sTable[2,((n1+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[7]) shr 8) and $FF] xor sTable[0,(n1+KeyData[7]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[6]) shr 24] xor sTable[2,((n2+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[6]) shr 8) and $FF] xor sTable[0,(n2+KeyData[6]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[5]) shr 24] xor sTable[2,((n1+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[5]) shr 8) and $FF] xor sTable[0,(n1+KeyData[5]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[4]) shr 24] xor sTable[2,((n2+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[4]) shr 8) and $FF] xor sTable[0,(n2+KeyData[4]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[3]) shr 24] xor sTable[2,((n1+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[3]) shr 8) and $FF] xor sTable[0,(n1+KeyData[3]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[2]) shr 24] xor sTable[2,((n2+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[2]) shr 8) and $FF] xor sTable[0,(n2+KeyData[2]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[1]) shr 24] xor sTable[2,((n1+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[1]) shr 8) and $FF] xor sTable[0,(n1+KeyData[1]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[0]) shr 24] xor sTable[2,((n2+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[0]) shr 8) and $FF] xor sTable[0,(n2+KeyData[0]) and $FF]);
PDword(@OutData)^:= n2;
PDword(pointer(@OutData)+4)^:= n1;
end;
procedure TDCP_gost.DecryptECB(const InData; var OutData);
var
n1, n2: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
n1:= PDword(@InData)^;
n2:= PDword(pointer(@InData)+4)^;
n2:= n2 xor (sTable[3,(n1+KeyData[0]) shr 24] xor sTable[2,((n1+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[0]) shr 8) and $FF] xor sTable[0,(n1+KeyData[0]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[1]) shr 24] xor sTable[2,((n2+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[1]) shr 8) and $FF] xor sTable[0,(n2+KeyData[1]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[2]) shr 24] xor sTable[2,((n1+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[2]) shr 8) and $FF] xor sTable[0,(n1+KeyData[2]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[3]) shr 24] xor sTable[2,((n2+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[3]) shr 8) and $FF] xor sTable[0,(n2+KeyData[3]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[4]) shr 24] xor sTable[2,((n1+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[4]) shr 8) and $FF] xor sTable[0,(n1+KeyData[4]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[5]) shr 24] xor sTable[2,((n2+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[5]) shr 8) and $FF] xor sTable[0,(n2+KeyData[5]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[6]) shr 24] xor sTable[2,((n1+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[6]) shr 8) and $FF] xor sTable[0,(n1+KeyData[6]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[7]) shr 24] xor sTable[2,((n2+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[7]) shr 8) and $FF] xor sTable[0,(n2+KeyData[7]) and $FF]);
for i:= 0 to 2 do
begin
n2:= n2 xor (sTable[3,(n1+KeyData[7]) shr 24] xor sTable[2,((n1+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[7]) shr 8) and $FF] xor sTable[0,(n1+KeyData[7]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[6]) shr 24] xor sTable[2,((n2+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[6]) shr 8) and $FF] xor sTable[0,(n2+KeyData[6]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[5]) shr 24] xor sTable[2,((n1+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[5]) shr 8) and $FF] xor sTable[0,(n1+KeyData[5]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[4]) shr 24] xor sTable[2,((n2+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[4]) shr 8) and $FF] xor sTable[0,(n2+KeyData[4]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[3]) shr 24] xor sTable[2,((n1+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[3]) shr 8) and $FF] xor sTable[0,(n1+KeyData[3]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[2]) shr 24] xor sTable[2,((n2+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[2]) shr 8) and $FF] xor sTable[0,(n2+KeyData[2]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[1]) shr 24] xor sTable[2,((n1+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[1]) shr 8) and $FF] xor sTable[0,(n1+KeyData[1]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[0]) shr 24] xor sTable[2,((n2+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[0]) shr 8) and $FF] xor sTable[0,(n2+KeyData[0]) and $FF]);
end;
PDword(@OutData)^:= n2;
PDword(pointer(@OutData)+4)^:= n1;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Gost *********************************}
{******************************************************************************}
{******************************************************************************}
{* !!WARNING!! !!WARNING!! !!WARNING!!!!WARNING!! !!WARNING!! !!WARNING!! *}
{* *}
{* I am not sure if this is compatible with the actual GOST standard *}
{* as a result, use this implementation with extreme caution. *}
{* *}
{* !!WARNING!! !!WARNING!! !!WARNING!!!!WARNING!! !!WARNING!! !!WARNING!! *}
{******************************************************************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPgost;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_gost= class(TDCP_blockcipher64)
protected
KeyData: array[0..7] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPgost.inc}
class function TDCP_gost.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_gost.GetID: integer;
begin
Result:= DCP_gost;
end;
class function TDCP_gost.GetAlgorithm: string;
begin
Result:= 'Gost';
end;
class function TDCP_gost.SelfTest: boolean;
const
Key1: array[0..31] of byte=
($BE,$5E,$C2,$00,$6C,$FF,$9D,$CF,$52,$35,$49,$59,$F1,$FF,$0C,$BF,
$E9,$50,$61,$B5,$A6,$48,$C1,$03,$87,$06,$9C,$25,$99,$7C,$06,$72);
InData1: array[0..7] of byte=
($0D,$F8,$28,$02,$B7,$41,$A2,$92);
OutData1: array[0..7] of byte=
($07,$F9,$02,$7D,$F7,$F7,$DF,$89);
Key2: array[0..31] of byte=
($B3,$85,$27,$2A,$C8,$D7,$2A,$5A,$8B,$34,$4B,$C8,$03,$63,$AC,$4D,
$09,$BF,$58,$F4,$1F,$54,$06,$24,$CB,$CB,$8F,$DC,$F5,$53,$07,$D7);
InData2: array[0..7] of byte=
($13,$54,$EE,$9C,$0A,$11,$CD,$4C);
OutData2: array[0..7] of byte=
($4F,$B5,$05,$36,$F9,$60,$A7,$B1);
var
Block: array[0..7] of byte;
Cipher: TDCP_gost;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_gost.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,8));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,8));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,8));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,8));
Cipher.Free;
end;
procedure TDCP_gost.InitKey(const Key; Size: longword);
var
i: longword;
userkey: array[0..31] of byte;
begin
Size:= Size div 8;
dcpFillChar(userkey,Sizeof(userkey),0);
Move(Key,userkey,Size);
for i:= 0 to 7 do
KeyData[i]:= (dword(UserKey[4*i+3]) shl 24) or (dword(UserKey[4*i+2]) shl 16) or
(dword(UserKey[4*i+1]) shl 8) or (dword(UserKey[4*i+0]));
end;
procedure TDCP_gost.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_gost.EncryptECB(const InData; var OutData);
var
n1, n2: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
n1:= PDword(@InData)^;
n2:= PDword(pointer(@InData)+4)^;
for i:= 0 to 2 do
begin
n2:= n2 xor (sTable[3,(n1+KeyData[0]) shr 24] xor sTable[2,((n1+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[0]) shr 8) and $FF] xor sTable[0,(n1+KeyData[0]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[1]) shr 24] xor sTable[2,((n2+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[1]) shr 8) and $FF] xor sTable[0,(n2+KeyData[1]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[2]) shr 24] xor sTable[2,((n1+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[2]) shr 8) and $FF] xor sTable[0,(n1+KeyData[2]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[3]) shr 24] xor sTable[2,((n2+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[3]) shr 8) and $FF] xor sTable[0,(n2+KeyData[3]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[4]) shr 24] xor sTable[2,((n1+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[4]) shr 8) and $FF] xor sTable[0,(n1+KeyData[4]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[5]) shr 24] xor sTable[2,((n2+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[5]) shr 8) and $FF] xor sTable[0,(n2+KeyData[5]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[6]) shr 24] xor sTable[2,((n1+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[6]) shr 8) and $FF] xor sTable[0,(n1+KeyData[6]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[7]) shr 24] xor sTable[2,((n2+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[7]) shr 8) and $FF] xor sTable[0,(n2+KeyData[7]) and $FF]);
end;
n2:= n2 xor (sTable[3,(n1+KeyData[7]) shr 24] xor sTable[2,((n1+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[7]) shr 8) and $FF] xor sTable[0,(n1+KeyData[7]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[6]) shr 24] xor sTable[2,((n2+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[6]) shr 8) and $FF] xor sTable[0,(n2+KeyData[6]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[5]) shr 24] xor sTable[2,((n1+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[5]) shr 8) and $FF] xor sTable[0,(n1+KeyData[5]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[4]) shr 24] xor sTable[2,((n2+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[4]) shr 8) and $FF] xor sTable[0,(n2+KeyData[4]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[3]) shr 24] xor sTable[2,((n1+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[3]) shr 8) and $FF] xor sTable[0,(n1+KeyData[3]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[2]) shr 24] xor sTable[2,((n2+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[2]) shr 8) and $FF] xor sTable[0,(n2+KeyData[2]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[1]) shr 24] xor sTable[2,((n1+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[1]) shr 8) and $FF] xor sTable[0,(n1+KeyData[1]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[0]) shr 24] xor sTable[2,((n2+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[0]) shr 8) and $FF] xor sTable[0,(n2+KeyData[0]) and $FF]);
PDword(@OutData)^:= n2;
PDword(pointer(@OutData)+4)^:= n1;
end;
procedure TDCP_gost.DecryptECB(const InData; var OutData);
var
n1, n2: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
n1:= PDword(@InData)^;
n2:= PDword(pointer(@InData)+4)^;
n2:= n2 xor (sTable[3,(n1+KeyData[0]) shr 24] xor sTable[2,((n1+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[0]) shr 8) and $FF] xor sTable[0,(n1+KeyData[0]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[1]) shr 24] xor sTable[2,((n2+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[1]) shr 8) and $FF] xor sTable[0,(n2+KeyData[1]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[2]) shr 24] xor sTable[2,((n1+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[2]) shr 8) and $FF] xor sTable[0,(n1+KeyData[2]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[3]) shr 24] xor sTable[2,((n2+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[3]) shr 8) and $FF] xor sTable[0,(n2+KeyData[3]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[4]) shr 24] xor sTable[2,((n1+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[4]) shr 8) and $FF] xor sTable[0,(n1+KeyData[4]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[5]) shr 24] xor sTable[2,((n2+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[5]) shr 8) and $FF] xor sTable[0,(n2+KeyData[5]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[6]) shr 24] xor sTable[2,((n1+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[6]) shr 8) and $FF] xor sTable[0,(n1+KeyData[6]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[7]) shr 24] xor sTable[2,((n2+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[7]) shr 8) and $FF] xor sTable[0,(n2+KeyData[7]) and $FF]);
for i:= 0 to 2 do
begin
n2:= n2 xor (sTable[3,(n1+KeyData[7]) shr 24] xor sTable[2,((n1+KeyData[7]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[7]) shr 8) and $FF] xor sTable[0,(n1+KeyData[7]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[6]) shr 24] xor sTable[2,((n2+KeyData[6]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[6]) shr 8) and $FF] xor sTable[0,(n2+KeyData[6]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[5]) shr 24] xor sTable[2,((n1+KeyData[5]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[5]) shr 8) and $FF] xor sTable[0,(n1+KeyData[5]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[4]) shr 24] xor sTable[2,((n2+KeyData[4]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[4]) shr 8) and $FF] xor sTable[0,(n2+KeyData[4]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[3]) shr 24] xor sTable[2,((n1+KeyData[3]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[3]) shr 8) and $FF] xor sTable[0,(n1+KeyData[3]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[2]) shr 24] xor sTable[2,((n2+KeyData[2]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[2]) shr 8) and $FF] xor sTable[0,(n2+KeyData[2]) and $FF]);
n2:= n2 xor (sTable[3,(n1+KeyData[1]) shr 24] xor sTable[2,((n1+KeyData[1]) shr 16) and $FF]
xor sTable[1,((n1+KeyData[1]) shr 8) and $FF] xor sTable[0,(n1+KeyData[1]) and $FF]);
n1:= n1 xor (sTable[3,(n2+KeyData[0]) shr 24] xor sTable[2,((n2+KeyData[0]) shr 16) and $FF]
xor sTable[1,((n2+KeyData[0]) shr 8) and $FF] xor sTable[0,(n2+KeyData[0]) and $FF]);
end;
PDword(@OutData)^:= n2;
PDword(pointer(@OutData)+4)^:= n1;
end;
end.

View File

@ -1,448 +1,448 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Ice and it's variants ****************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPice;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_customice= class(TDCP_blockcipher64)
protected
rounds: dword;
ik_keysched: array[0..31,0..2] of dword;
function f(p, sk: dword): dword;
procedure key_sched_build(kb: pwordarray; n: dword; keyrot: pdwordarray);
procedure InitIce(const Key; Size: longword; n: dword);
public
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
constructor Create(AOwner: TComponent); override;
end;
TDCP_ice= class(TDCP_customice)
protected
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
end;
TDCP_thinice= class(TDCP_customice)
protected
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
end;
TDCP_ice2= class(TDCP_customice)
protected
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
var
ice_sbox: array[0..3,0..1023] of dword;
ice_sboxdone: boolean;
const
ice_smod: array[0..3,0..3] of dword= (
(333, 313, 505, 369),
(379, 375, 319, 391),
(361, 445, 451, 397),
(397, 425, 395, 505));
ice_sxor: array[0..3,0..3] of dword= (
($83, $85, $9b, $cd),
($cc, $a7, $ad, $41),
($4b, $2e, $d4, $33),
($ea, $cb, $2e, $04));
ice_keyrot: array[0..15] of dword= (
0, 1, 2, 3, 2, 1, 3, 0,
1, 3, 2, 0, 3, 1, 0, 2);
ice_pbox: array[0..31] of dword= (
$00000001, $00000080, $00000400, $00002000,
$00080000, $00200000, $01000000, $40000000,
$00000008, $00000020, $00000100, $00004000,
$00010000, $00800000, $04000000, $20000000,
$00000004, $00000010, $00000200, $00008000,
$00020000, $00400000, $08000000, $10000000,
$00000002, $00000040, $00000800, $00001000,
$00040000, $00100000, $02000000, $80000000);
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
{******************************************************************************}
function gf_mult(a, b, m: dword): dword;
var
res: dword;
begin
res:= 0;
while b<> 0 do
begin
if (b and 1)<> 0 then
res:= res xor a;
a:= a shl 1;
b:= b shr 1;
if a>= 256 then
a:= a xor m;
end;
Result:= res;
end;
function gf_exp7(b, m: dword): dword;
var
x: dword;
begin
if b= 0 then
Result:= 0
else
begin
x:= gf_mult(b,b,m);
x:= gf_mult(b,x,m);
x:= gf_mult(x,x,m);
Result:= gf_mult(b,x,m);
end;
end;
function ice_perm32(x: dword): dword;
var
res: dword;
pbox: pdword;
begin
res:= 0;
pbox:= @ice_pbox;
while x<> 0 do
begin
if (x and 1)<> 0 then
res:= res or pbox^;
Inc(pbox);
x:= x shr 1;
end;
Result:= res;
end;
procedure ice_sboxes_init;
var
i, col, row: dword;
x: dword;
begin
for i:= 0 to 1023 do
begin
col:= (i shr 1) and $FF;
row:= (i and 1) or ((i and $200) shr 8);
x:= gf_exp7(col xor ice_sxor[0,row],ice_smod[0,row]) shl 24;
ice_sbox[0,i]:= ice_perm32(x);
x:= gf_exp7(col xor ice_sxor[1,row],ice_smod[1,row]) shl 16;
ice_sbox[1,i]:= ice_perm32(x);
x:= gf_exp7(col xor ice_sxor[2,row],ice_smod[2,row]) shl 8;
ice_sbox[2,i]:= ice_perm32(x);
x:= gf_exp7(col xor ice_sxor[3,row],ice_smod[3,row]);
ice_sbox[3,i]:= ice_perm32(x);
end;
end;
function TDCP_customice.f(p, sk: dword): dword;
var
tl, tr, al, ar: dword;
begin
tl:= ((p shr 16) and $3ff) or (((p shr 14) or (p shl 18)) and $ffc00);
tr:= (p and $3ff) or ((p shl 2) and $ffc00);
al:= ik_keysched[sk,2] and (tl xor tr);
ar:= al xor tr;
al:= al xor tl;
al:= al xor ik_keysched[sk,0];
ar:= ar xor ik_keysched[sk,1];
Result:= ice_sbox[0,al shr 10] or ice_sbox[1,al and $3ff] or
ice_sbox[2,ar shr 10] or ice_sbox[3,ar and $3ff];
end;
procedure TDCP_customice.key_sched_build(kb: pwordarray; n: dword; keyrot: pdwordarray);
var
i, j, k, kr: dword;
keys: pdwordarray;
currentsk: pdword;
currentkb: pword;
bit: dword;
begin
for i:= 0 to 7 do
begin
kr:= keyrot^[i];
keys:= @ik_keysched[n+i];
for j:= 0 to 2 do
keys^[j]:= 0;
for j:= 0 to 14 do
begin
currentsk:= @keys^[j mod 3];
for k:= 0 to 3 do
begin
currentkb:= @kb^[(kr + k) and 3];
bit:= currentkb^ and 1;
currentsk^:= (currentsk^ shl 1) or bit;
currentkb^:= (currentkb^ shr 1) or ((bit xor 1) shl 15);
end;
end;
end;
end;
procedure TDCP_customice.InitIce(const Key; Size: longword; n: dword);
var
i, j: dword;
kb: array[0..3] of word;
keyb: array[0..15] of byte;
begin
dcpFillChar(keyb,Sizeof(keyb),0);
Move(key,keyb,Size div 8);
if n> 0 then
rounds:= 16 * n
else
rounds:= 8;
if rounds= 8 then
begin
for i:= 0 to 4 do
kb[3 - i]:= (keyb[i*2] shl 8) or keyb[i*2 + 1];
key_sched_build(@kb,0,@ice_keyrot);
end
else
begin
for i:= 0 to (n-1) do
begin
for j:= 0 to 3 do
kb[3-j]:= (keyb[i*8 + j*2] shl 8) or keyb[i*8 + j*2 + 1];
key_sched_build(@kb,i*8,@ice_keyrot);
key_sched_build(@kb,rounds - 8 - i*8,@ice_keyrot[8]);
end;
end;
end;
procedure TDCP_customice.Burn;
begin
FillChar(ik_keysched,Sizeof(ik_keysched),0);
Rounds:= 0;
inherited Burn;
end;
procedure TDCP_customice.EncryptECB(const InData; var OutData);
var
i, l, r: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
l:= SwapDWord(Pdword(@InData)^);
r:= SwapDWord(Pdword(pointer(@InData)+4)^);
i:= 0;
while i< rounds do
begin
l:= l xor f(r,i);
r:= r xor f(l,i+1);
Inc(i,2);
end;
Pdword(@OutData)^:= SwapDWord(r);
Pdword(pointer(@OutData)+4)^:= SwapDWord(l);
end;
procedure TDCP_customice.DecryptECB(const InData; var OutData);
var
l, r: dword;
i: integer;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
l:= SwapDWord(Pdword(@InData)^);
r:= SwapDWord(Pdword(pointer(@InData)+4)^);
i:= rounds-1;
while i> 0 do
begin
l:= l xor f(r,i);
r:= r xor f(l,i-1);
Dec(i,2);
end;
Pdword(@OutData)^:= SwapDWord(r);
Pdword(pointer(@OutData)+4)^:= SwapDWord(l);
end;
constructor TDCP_customice.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if not ice_sboxdone then
begin
ice_sboxes_init;
ice_sboxdone:= true;
end;
end;
{******************************************************************************}
class function TDCP_ice.GetMaxKeySize: integer;
begin
Result:= 64;
end;
class function TDCP_ice.GetID: integer;
begin
Result:= DCP_ice;
end;
class function TDCP_ice.GetAlgorithm: string;
begin
Result:= 'Ice';
end;
class function TDCP_ice.SelfTest: boolean;
const
Key1: array[0..7] of byte= ($de,$ad,$be,$ef,$01,$23,$45,$67);
InData1: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
OutData1: array[0..7] of byte= ($7d,$6e,$f1,$ef,$30,$d4,$7a,$96);
var
Cipher: TDCP_ice;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_ice.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_ice.InitKey(const Key; Size: longword);
begin
InitIce(Key,Size,1);
end;
{******************************************************************************}
class function TDCP_thinice.GetMaxKeySize: integer;
begin
Result:= 64;
end;
class function TDCP_thinice.GetID: integer;
begin
Result:= DCP_thinice;
end;
class function TDCP_thinice.GetAlgorithm: string;
begin
Result:= 'Thin Ice';
end;
class function TDCP_thinice.SelfTest: boolean;
const
Key1: array[0..7] of byte= ($de,$ad,$be,$ef,$01,$23,$45,$67);
InData1: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
OutData1: array[0..7] of byte= ($de,$24,$0d,$83,$a0,$0a,$9c,$c0);
var
Cipher: TDCP_thinice;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_thinice.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_thinice.InitKey(const Key; Size: longword);
begin
InitIce(Key,Size,0);
end;
{******************************************************************************}
class function TDCP_ice2.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_ice2.GetID: integer;
begin
Result:= DCP_ice2;
end;
class function TDCP_ice2.GetAlgorithm: string;
begin
Result:= 'Ice2';
end;
class function TDCP_ice2.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff);
InData1: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
OutData1: array[0..7] of byte= ($f9,$48,$40,$d8,$69,$72,$f2,$1c);
var
Cipher: TDCP_ice2;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_ice2.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_ice2.InitKey(const Key; Size: longword);
begin
InitIce(Key,Size,2);
end;
initialization
ice_sboxdone:= false;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Ice and it's variants ****************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPice;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_customice= class(TDCP_blockcipher64)
protected
rounds: dword;
ik_keysched: array[0..31,0..2] of dword;
function f(p, sk: dword): dword;
procedure key_sched_build(kb: pwordarray; n: dword; keyrot: pdwordarray);
procedure InitIce(const Key; Size: longword; n: dword);
public
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
constructor Create(AOwner: TComponent); override;
end;
TDCP_ice= class(TDCP_customice)
protected
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
end;
TDCP_thinice= class(TDCP_customice)
protected
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
end;
TDCP_ice2= class(TDCP_customice)
protected
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
var
ice_sbox: array[0..3,0..1023] of dword;
ice_sboxdone: boolean;
const
ice_smod: array[0..3,0..3] of dword= (
(333, 313, 505, 369),
(379, 375, 319, 391),
(361, 445, 451, 397),
(397, 425, 395, 505));
ice_sxor: array[0..3,0..3] of dword= (
($83, $85, $9b, $cd),
($cc, $a7, $ad, $41),
($4b, $2e, $d4, $33),
($ea, $cb, $2e, $04));
ice_keyrot: array[0..15] of dword= (
0, 1, 2, 3, 2, 1, 3, 0,
1, 3, 2, 0, 3, 1, 0, 2);
ice_pbox: array[0..31] of dword= (
$00000001, $00000080, $00000400, $00002000,
$00080000, $00200000, $01000000, $40000000,
$00000008, $00000020, $00000100, $00004000,
$00010000, $00800000, $04000000, $20000000,
$00000004, $00000010, $00000200, $00008000,
$00020000, $00400000, $08000000, $10000000,
$00000002, $00000040, $00000800, $00001000,
$00040000, $00100000, $02000000, $80000000);
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
{******************************************************************************}
function gf_mult(a, b, m: dword): dword;
var
res: dword;
begin
res:= 0;
while b<> 0 do
begin
if (b and 1)<> 0 then
res:= res xor a;
a:= a shl 1;
b:= b shr 1;
if a>= 256 then
a:= a xor m;
end;
Result:= res;
end;
function gf_exp7(b, m: dword): dword;
var
x: dword;
begin
if b= 0 then
Result:= 0
else
begin
x:= gf_mult(b,b,m);
x:= gf_mult(b,x,m);
x:= gf_mult(x,x,m);
Result:= gf_mult(b,x,m);
end;
end;
function ice_perm32(x: dword): dword;
var
res: dword;
pbox: pdword;
begin
res:= 0;
pbox:= @ice_pbox;
while x<> 0 do
begin
if (x and 1)<> 0 then
res:= res or pbox^;
Inc(pbox);
x:= x shr 1;
end;
Result:= res;
end;
procedure ice_sboxes_init;
var
i, col, row: dword;
x: dword;
begin
for i:= 0 to 1023 do
begin
col:= (i shr 1) and $FF;
row:= (i and 1) or ((i and $200) shr 8);
x:= gf_exp7(col xor ice_sxor[0,row],ice_smod[0,row]) shl 24;
ice_sbox[0,i]:= ice_perm32(x);
x:= gf_exp7(col xor ice_sxor[1,row],ice_smod[1,row]) shl 16;
ice_sbox[1,i]:= ice_perm32(x);
x:= gf_exp7(col xor ice_sxor[2,row],ice_smod[2,row]) shl 8;
ice_sbox[2,i]:= ice_perm32(x);
x:= gf_exp7(col xor ice_sxor[3,row],ice_smod[3,row]);
ice_sbox[3,i]:= ice_perm32(x);
end;
end;
function TDCP_customice.f(p, sk: dword): dword;
var
tl, tr, al, ar: dword;
begin
tl:= ((p shr 16) and $3ff) or (((p shr 14) or (p shl 18)) and $ffc00);
tr:= (p and $3ff) or ((p shl 2) and $ffc00);
al:= ik_keysched[sk,2] and (tl xor tr);
ar:= al xor tr;
al:= al xor tl;
al:= al xor ik_keysched[sk,0];
ar:= ar xor ik_keysched[sk,1];
Result:= ice_sbox[0,al shr 10] or ice_sbox[1,al and $3ff] or
ice_sbox[2,ar shr 10] or ice_sbox[3,ar and $3ff];
end;
procedure TDCP_customice.key_sched_build(kb: pwordarray; n: dword; keyrot: pdwordarray);
var
i, j, k, kr: dword;
keys: pdwordarray;
currentsk: pdword;
currentkb: pword;
bit: dword;
begin
for i:= 0 to 7 do
begin
kr:= keyrot^[i];
keys:= @ik_keysched[n+i];
for j:= 0 to 2 do
keys^[j]:= 0;
for j:= 0 to 14 do
begin
currentsk:= @keys^[j mod 3];
for k:= 0 to 3 do
begin
currentkb:= @kb^[(kr + k) and 3];
bit:= currentkb^ and 1;
currentsk^:= (currentsk^ shl 1) or bit;
currentkb^:= (currentkb^ shr 1) or ((bit xor 1) shl 15);
end;
end;
end;
end;
procedure TDCP_customice.InitIce(const Key; Size: longword; n: dword);
var
i, j: dword;
kb: array[0..3] of word;
keyb: array[0..15] of byte;
begin
dcpFillChar(keyb,Sizeof(keyb),0);
Move(key,keyb,Size div 8);
if n> 0 then
rounds:= 16 * n
else
rounds:= 8;
if rounds= 8 then
begin
for i:= 0 to 4 do
kb[3 - i]:= (keyb[i*2] shl 8) or keyb[i*2 + 1];
key_sched_build(@kb,0,@ice_keyrot);
end
else
begin
for i:= 0 to (n-1) do
begin
for j:= 0 to 3 do
kb[3-j]:= (keyb[i*8 + j*2] shl 8) or keyb[i*8 + j*2 + 1];
key_sched_build(@kb,i*8,@ice_keyrot);
key_sched_build(@kb,rounds - 8 - i*8,@ice_keyrot[8]);
end;
end;
end;
procedure TDCP_customice.Burn;
begin
FillChar(ik_keysched,Sizeof(ik_keysched),0);
Rounds:= 0;
inherited Burn;
end;
procedure TDCP_customice.EncryptECB(const InData; var OutData);
var
i, l, r: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
l:= SwapDWord(Pdword(@InData)^);
r:= SwapDWord(Pdword(pointer(@InData)+4)^);
i:= 0;
while i< rounds do
begin
l:= l xor f(r,i);
r:= r xor f(l,i+1);
Inc(i,2);
end;
Pdword(@OutData)^:= SwapDWord(r);
Pdword(pointer(@OutData)+4)^:= SwapDWord(l);
end;
procedure TDCP_customice.DecryptECB(const InData; var OutData);
var
l, r: dword;
i: integer;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
l:= SwapDWord(Pdword(@InData)^);
r:= SwapDWord(Pdword(pointer(@InData)+4)^);
i:= rounds-1;
while i> 0 do
begin
l:= l xor f(r,i);
r:= r xor f(l,i-1);
Dec(i,2);
end;
Pdword(@OutData)^:= SwapDWord(r);
Pdword(pointer(@OutData)+4)^:= SwapDWord(l);
end;
constructor TDCP_customice.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if not ice_sboxdone then
begin
ice_sboxes_init;
ice_sboxdone:= true;
end;
end;
{******************************************************************************}
class function TDCP_ice.GetMaxKeySize: integer;
begin
Result:= 64;
end;
class function TDCP_ice.GetID: integer;
begin
Result:= DCP_ice;
end;
class function TDCP_ice.GetAlgorithm: string;
begin
Result:= 'Ice';
end;
class function TDCP_ice.SelfTest: boolean;
const
Key1: array[0..7] of byte= ($de,$ad,$be,$ef,$01,$23,$45,$67);
InData1: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
OutData1: array[0..7] of byte= ($7d,$6e,$f1,$ef,$30,$d4,$7a,$96);
var
Cipher: TDCP_ice;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_ice.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_ice.InitKey(const Key; Size: longword);
begin
InitIce(Key,Size,1);
end;
{******************************************************************************}
class function TDCP_thinice.GetMaxKeySize: integer;
begin
Result:= 64;
end;
class function TDCP_thinice.GetID: integer;
begin
Result:= DCP_thinice;
end;
class function TDCP_thinice.GetAlgorithm: string;
begin
Result:= 'Thin Ice';
end;
class function TDCP_thinice.SelfTest: boolean;
const
Key1: array[0..7] of byte= ($de,$ad,$be,$ef,$01,$23,$45,$67);
InData1: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
OutData1: array[0..7] of byte= ($de,$24,$0d,$83,$a0,$0a,$9c,$c0);
var
Cipher: TDCP_thinice;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_thinice.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_thinice.InitKey(const Key; Size: longword);
begin
InitIce(Key,Size,0);
end;
{******************************************************************************}
class function TDCP_ice2.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_ice2.GetID: integer;
begin
Result:= DCP_ice2;
end;
class function TDCP_ice2.GetAlgorithm: string;
begin
Result:= 'Ice2';
end;
class function TDCP_ice2.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff);
InData1: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
OutData1: array[0..7] of byte= ($f9,$48,$40,$d8,$69,$72,$f2,$1c);
var
Cipher: TDCP_ice2;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_ice2.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_ice2.InitKey(const Key; Size: longword);
begin
InitIce(Key,Size,2);
end;
initialization
ice_sboxdone:= false;
end.

View File

@ -1,298 +1,298 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of IDEA *********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPidea;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_idea= class(TDCP_blockcipher64)
protected
EK, DK: array[0..51] of word;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
class function TDCP_idea.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_idea.GetID: integer;
begin
Result:= DCP_idea;
end;
class function TDCP_idea.GetAlgorithm: string;
begin
Result:= 'IDEA';
end;
class function TDCP_idea.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($3A,$98,$4E,$20,$00,$19,$5D,$B3,$2E,$E5,$01,$C8,$C4,$7C,$EA,$60);
InData1: array[0..7] of byte=
($01,$02,$03,$04,$05,$06,$07,$08);
OutData1: array[0..7] of byte=
($97,$BC,$D8,$20,$07,$80,$DA,$86);
Key2: array[0..15] of byte=
($00,$64,$00,$C8,$01,$2C,$01,$90,$01,$F4,$02,$58,$02,$BC,$03,$20);
InData2: array[0..7] of byte=
($05,$32,$0A,$64,$14,$C8,$19,$FA);
OutData2: array[0..7] of byte=
($65,$BE,$87,$E7,$A2,$53,$8A,$ED);
var
Cipher: TDCP_idea;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_idea.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= Result and boolean(CompareMem(@Data,@OutData2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
function MulInv(x: word): word;
var
t0, t1, q, y: word;
begin
if x<= 1 then
begin
Result:= x;
Exit;
end;
t1:= DWord($10001) div x;
y:= DWord($10001) mod x;
if y= 1 then
begin
Result:= (1 - t1) and $FFFF;
Exit;
end;
t0:= 1;
repeat
q:= x div y;
x:= x mod y;
t0:= t0 + (q*t1);
if x= 1 then
begin
Result:= t0;
Exit;
end;
q:= y div x;
y:= y mod x;
t1:= t1 + (q*t0);
until y= 1;
Result:= (1-t1) and $FFFF;
end;
procedure TDCP_idea.InitKey(const Key; Size: longword);
var
i: integer;
begin
Size:= Size div 8;
FillChar(EK,Sizeof(EK),0);
Move(Key,EK,Size);
for i:= 0 to 7 do
EK[i]:= (EK[i] shl 8) or (EK[i] shr 8);
for i:= 1 to 5 do
begin
EK[(i*8)+0]:= (EK[((i-1)*8)+1] shl 9) or (EK[((i-1)*8)+2] shr 7);
EK[(i*8)+1]:= (EK[((i-1)*8)+2] shl 9) or (EK[((i-1)*8)+3] shr 7);
EK[(i*8)+2]:= (EK[((i-1)*8)+3] shl 9) or (EK[((i-1)*8)+4] shr 7);
EK[(i*8)+3]:= (EK[((i-1)*8)+4] shl 9) or (EK[((i-1)*8)+5] shr 7);
EK[(i*8)+4]:= (EK[((i-1)*8)+5] shl 9) or (EK[((i-1)*8)+6] shr 7);
EK[(i*8)+5]:= (EK[((i-1)*8)+6] shl 9) or (EK[((i-1)*8)+7] shr 7);
EK[(i*8)+6]:= (EK[((i-1)*8)+7] shl 9) or (EK[((i-1)*8)+0] shr 7);
EK[(i*8)+7]:= (EK[((i-1)*8)+0] shl 9) or (EK[((i-1)*8)+1] shr 7);
end;
EK[48]:= (EK[41] shl 9) or (EK[42] shr 7);
EK[49]:= (EK[42] shl 9) or (EK[43] shr 7);
EK[50]:= (EK[43] shl 9) or (EK[44] shr 7);
EK[51]:= (EK[44] shl 9) or (EK[45] shr 7);
DK[51]:= MulInv(EK[3]);
DK[50]:= -EK[2];
DK[49]:= -EK[1];
DK[48]:= MulInv(EK[0]);
for i:= 0 to 6 do
begin
DK[47-i*6]:= EK[i*6+5];
DK[46-i*6]:= EK[i*6+4];
DK[45-i*6]:= MulInv(EK[i*6+9]);
DK[44-i*6]:= -EK[i*6+7];
DK[43-i*6]:= -EK[i*6+8];
DK[42-i*6]:= MulInv(EK[i*6+6]);
end;
DK[5]:= EK[47];
DK[4]:= EK[46];
DK[3]:= MulInv(EK[51]);
DK[2]:= -EK[50];
DK[1]:= -EK[49];
DK[0]:= MulInv(EK[48]);
end;
procedure TDCP_idea.Burn;
begin
FillChar(EK,Sizeof(EK),0);
FillChar(DK,Sizeof(DK),0);
inherited Burn;
end;
procedure Mul(var x: word; const y: word);
var
p: DWord;
t16: word;
begin
p:= DWord(x)*y;
if p= 0 then
x:= 1 - x - y
else
begin
x:= p shr 16;
t16:= p and $FFFF;
x:= t16 - x;
if (t16 < x) then
Inc(x);
end;
end;
procedure TDCP_idea.EncryptECB(const InData; var OutData);
var
x: array[1..4] of word;
s3, s2: word;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@X[1])^:= PDword(@InData)^;
PDword(@X[3])^:= PDword(pointer(@InData)+4)^;
for i:= 1 to 4 do
x[i]:= (x[i] shl 8) or (x[i] shr 8);
for i:= 0 to 7 do
begin
Mul(x[1],EK[(i*6)+0]);
Inc(x[2],EK[(i*6)+1]);
Inc(x[3],EK[(i*6)+2]);
Mul(x[4],EK[(i*6)+3]);
s3:= x[3];
x[3]:= x[3] xor x[1];
Mul(x[3],EK[(i*6)+4]);
s2:= x[2];
x[2]:= x[2] xor x[4];
Inc(x[2],x[3]);
Mul(x[2],EK[(i*6)+5]);
Inc(x[3],x[2]);
x[1]:= x[1] xor x[2];
x[4]:= x[4] xor x[3];
x[2]:= x[2] xor s3;
x[3]:= x[3] xor s2;
end;
Mul(x[1],EK[48]);
Inc(x[3],EK[49]);
Inc(x[2],EK[50]);
Mul(x[4],EK[51]);
x[1]:= (x[1] shl 8) or (x[1] shr 8);
s2:= (x[3] shl 8) or (x[3] shr 8);
x[3]:= (x[2] shl 8) or (x[2] shr 8);
x[4]:= (x[4] shl 8) or (x[4] shr 8);
x[2]:= s2;
PDword(@OutData)^:= PDword(@x[1])^;
PDword(pointer(@OutData)+4)^:= PDword(@x[3])^;
end;
procedure TDCP_idea.DecryptECB(const InData; var OutData);
var
x: array[1..4] of word;
s3, s2: word;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@X[1])^:= PDword(@InData)^;
PDword(@X[3])^:= PDword(pointer(@InData)+4)^;
for i:= 1 to 4 do
x[i]:= (x[i] shl 8) or (x[i] shr 8);
for i:= 0 to 7 do
begin
Mul(x[1],DK[(i*6)+0]);
Inc(x[2],DK[(i*6)+1]);
Inc(x[3],DK[(i*6)+2]);
Mul(x[4],DK[(i*6)+3]);
s3:= x[3];
x[3]:= x[3] xor x[1];
Mul(x[3],DK[(i*6)+4]);
s2:= x[2];
x[2]:= x[2] xor x[4];
Inc(x[2],x[3]);
Mul(x[2],DK[(i*6)+5]);
Inc(x[3],x[2]);
x[1]:= x[1] xor x[2];
x[4]:= x[4] xor x[3];
x[2]:= x[2] xor s3;
x[3]:= x[3] xor s2;
end;
Mul(x[1],DK[48]);
Inc(x[3],DK[49]);
Inc(x[2],DK[50]);
Mul(x[4],DK[51]);
x[1]:= (x[1] shl 8) or (x[1] shr 8);
s2:= (x[3] shl 8) or (x[3] shr 8);
x[3]:= (x[2] shl 8) or (x[2] shr 8);
x[4]:= (x[4] shl 8) or (x[4] shr 8);
x[2]:= s2;
PDword(@OutData)^:= PDword(@x[1])^;
PDword(pointer(@OutData)+4)^:= PDword(@x[3])^;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of IDEA *********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPidea;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_idea= class(TDCP_blockcipher64)
protected
EK, DK: array[0..51] of word;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
class function TDCP_idea.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_idea.GetID: integer;
begin
Result:= DCP_idea;
end;
class function TDCP_idea.GetAlgorithm: string;
begin
Result:= 'IDEA';
end;
class function TDCP_idea.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($3A,$98,$4E,$20,$00,$19,$5D,$B3,$2E,$E5,$01,$C8,$C4,$7C,$EA,$60);
InData1: array[0..7] of byte=
($01,$02,$03,$04,$05,$06,$07,$08);
OutData1: array[0..7] of byte=
($97,$BC,$D8,$20,$07,$80,$DA,$86);
Key2: array[0..15] of byte=
($00,$64,$00,$C8,$01,$2C,$01,$90,$01,$F4,$02,$58,$02,$BC,$03,$20);
InData2: array[0..7] of byte=
($05,$32,$0A,$64,$14,$C8,$19,$FA);
OutData2: array[0..7] of byte=
($65,$BE,$87,$E7,$A2,$53,$8A,$ED);
var
Cipher: TDCP_idea;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_idea.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= Result and boolean(CompareMem(@Data,@OutData2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@InData2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
function MulInv(x: word): word;
var
t0, t1, q, y: word;
begin
if x<= 1 then
begin
Result:= x;
Exit;
end;
t1:= DWord($10001) div x;
y:= DWord($10001) mod x;
if y= 1 then
begin
Result:= (1 - t1) and $FFFF;
Exit;
end;
t0:= 1;
repeat
q:= x div y;
x:= x mod y;
t0:= t0 + (q*t1);
if x= 1 then
begin
Result:= t0;
Exit;
end;
q:= y div x;
y:= y mod x;
t1:= t1 + (q*t0);
until y= 1;
Result:= (1-t1) and $FFFF;
end;
procedure TDCP_idea.InitKey(const Key; Size: longword);
var
i: integer;
begin
Size:= Size div 8;
FillChar(EK,Sizeof(EK),0);
Move(Key,EK,Size);
for i:= 0 to 7 do
EK[i]:= (EK[i] shl 8) or (EK[i] shr 8);
for i:= 1 to 5 do
begin
EK[(i*8)+0]:= (EK[((i-1)*8)+1] shl 9) or (EK[((i-1)*8)+2] shr 7);
EK[(i*8)+1]:= (EK[((i-1)*8)+2] shl 9) or (EK[((i-1)*8)+3] shr 7);
EK[(i*8)+2]:= (EK[((i-1)*8)+3] shl 9) or (EK[((i-1)*8)+4] shr 7);
EK[(i*8)+3]:= (EK[((i-1)*8)+4] shl 9) or (EK[((i-1)*8)+5] shr 7);
EK[(i*8)+4]:= (EK[((i-1)*8)+5] shl 9) or (EK[((i-1)*8)+6] shr 7);
EK[(i*8)+5]:= (EK[((i-1)*8)+6] shl 9) or (EK[((i-1)*8)+7] shr 7);
EK[(i*8)+6]:= (EK[((i-1)*8)+7] shl 9) or (EK[((i-1)*8)+0] shr 7);
EK[(i*8)+7]:= (EK[((i-1)*8)+0] shl 9) or (EK[((i-1)*8)+1] shr 7);
end;
EK[48]:= (EK[41] shl 9) or (EK[42] shr 7);
EK[49]:= (EK[42] shl 9) or (EK[43] shr 7);
EK[50]:= (EK[43] shl 9) or (EK[44] shr 7);
EK[51]:= (EK[44] shl 9) or (EK[45] shr 7);
DK[51]:= MulInv(EK[3]);
DK[50]:= -EK[2];
DK[49]:= -EK[1];
DK[48]:= MulInv(EK[0]);
for i:= 0 to 6 do
begin
DK[47-i*6]:= EK[i*6+5];
DK[46-i*6]:= EK[i*6+4];
DK[45-i*6]:= MulInv(EK[i*6+9]);
DK[44-i*6]:= -EK[i*6+7];
DK[43-i*6]:= -EK[i*6+8];
DK[42-i*6]:= MulInv(EK[i*6+6]);
end;
DK[5]:= EK[47];
DK[4]:= EK[46];
DK[3]:= MulInv(EK[51]);
DK[2]:= -EK[50];
DK[1]:= -EK[49];
DK[0]:= MulInv(EK[48]);
end;
procedure TDCP_idea.Burn;
begin
FillChar(EK,Sizeof(EK),0);
FillChar(DK,Sizeof(DK),0);
inherited Burn;
end;
procedure Mul(var x: word; const y: word);
var
p: DWord;
t16: word;
begin
p:= DWord(x)*y;
if p= 0 then
x:= 1 - x - y
else
begin
x:= p shr 16;
t16:= p and $FFFF;
x:= t16 - x;
if (t16 < x) then
Inc(x);
end;
end;
procedure TDCP_idea.EncryptECB(const InData; var OutData);
var
x: array[1..4] of word;
s3, s2: word;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@X[1])^:= PDword(@InData)^;
PDword(@X[3])^:= PDword(pointer(@InData)+4)^;
for i:= 1 to 4 do
x[i]:= (x[i] shl 8) or (x[i] shr 8);
for i:= 0 to 7 do
begin
Mul(x[1],EK[(i*6)+0]);
Inc(x[2],EK[(i*6)+1]);
Inc(x[3],EK[(i*6)+2]);
Mul(x[4],EK[(i*6)+3]);
s3:= x[3];
x[3]:= x[3] xor x[1];
Mul(x[3],EK[(i*6)+4]);
s2:= x[2];
x[2]:= x[2] xor x[4];
Inc(x[2],x[3]);
Mul(x[2],EK[(i*6)+5]);
Inc(x[3],x[2]);
x[1]:= x[1] xor x[2];
x[4]:= x[4] xor x[3];
x[2]:= x[2] xor s3;
x[3]:= x[3] xor s2;
end;
Mul(x[1],EK[48]);
Inc(x[3],EK[49]);
Inc(x[2],EK[50]);
Mul(x[4],EK[51]);
x[1]:= (x[1] shl 8) or (x[1] shr 8);
s2:= (x[3] shl 8) or (x[3] shr 8);
x[3]:= (x[2] shl 8) or (x[2] shr 8);
x[4]:= (x[4] shl 8) or (x[4] shr 8);
x[2]:= s2;
PDword(@OutData)^:= PDword(@x[1])^;
PDword(pointer(@OutData)+4)^:= PDword(@x[3])^;
end;
procedure TDCP_idea.DecryptECB(const InData; var OutData);
var
x: array[1..4] of word;
s3, s2: word;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@X[1])^:= PDword(@InData)^;
PDword(@X[3])^:= PDword(pointer(@InData)+4)^;
for i:= 1 to 4 do
x[i]:= (x[i] shl 8) or (x[i] shr 8);
for i:= 0 to 7 do
begin
Mul(x[1],DK[(i*6)+0]);
Inc(x[2],DK[(i*6)+1]);
Inc(x[3],DK[(i*6)+2]);
Mul(x[4],DK[(i*6)+3]);
s3:= x[3];
x[3]:= x[3] xor x[1];
Mul(x[3],DK[(i*6)+4]);
s2:= x[2];
x[2]:= x[2] xor x[4];
Inc(x[2],x[3]);
Mul(x[2],DK[(i*6)+5]);
Inc(x[3],x[2]);
x[1]:= x[1] xor x[2];
x[4]:= x[4] xor x[3];
x[2]:= x[2] xor s3;
x[3]:= x[3] xor s2;
end;
Mul(x[1],DK[48]);
Inc(x[3],DK[49]);
Inc(x[2],DK[50]);
Mul(x[4],DK[51]);
x[1]:= (x[1] shl 8) or (x[1] shr 8);
s2:= (x[3] shl 8) or (x[3] shr 8);
x[3]:= (x[2] shl 8) or (x[2] shr 8);
x[4]:= (x[4] shl 8) or (x[4] shr 8);
x[2]:= s2;
PDword(@OutData)^:= PDword(@x[1])^;
PDword(pointer(@OutData)+4)^:= PDword(@x[3])^;
end;
end.

File diff suppressed because it is too large Load Diff

View File

@ -1,262 +1,262 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Misty1 *******************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPmisty1;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
NUMROUNDS= 8;
type
TDCP_misty1= class(TDCP_blockcipher64)
protected
KeyData: array[0..31] of DWord;
function FI(const FI_IN, FI_KEY: DWord): DWord;
function FO(const FO_IN: DWord; const k: longword): DWord;
function FL(const FL_IN: DWord; const k: longword): DWord;
function FLINV(const FL_IN: DWord; const k: longword): DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPmisty1.inc}
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
class function TDCP_misty1.GetID: integer;
begin
Result:= DCP_misty1;
end;
class function TDCP_misty1.GetAlgorithm: string;
begin
Result:= 'Misty1';
end;
class function TDCP_misty1.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_misty1.SelfTest: boolean;
const
Key: array[0..15] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff);
Plain1: array[0..7] of byte= ($01,$23,$45,$67,$89,$ab,$cd,$ef);
Plain2: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
Cipher1: array[0..7] of byte= ($8b,$1d,$a5,$f5,$6a,$b3,$d0,$7c);
Cipher2: array[0..7] of byte= ($04,$b6,$82,$40,$b1,$3b,$e9,$5d);
var
Cipher: TDCP_misty1;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_misty1.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(Plain1,Block);
Result:= CompareMem(@Cipher1,@Block,Sizeof(Block));
Cipher.DecryptECB(Block,Block);
Result:= Result and CompareMem(@Plain1,@Block,Sizeof(Block));
Cipher.EncryptECB(Plain2,Block);
Result:= Result and CompareMem(@Cipher2,@Block,Sizeof(Block));
Cipher.DecryptECB(Block,Block);
Result:= Result and CompareMem(@Plain2,@Block,Sizeof(Block));
Cipher.Burn;
Cipher.Free;
end;
function TDCP_misty1.FI(const FI_IN, FI_KEY: DWord): DWord;
var
d7, d9: DWord;
begin
d9:= (FI_IN shr 7) and $1ff;
d7:= FI_IN and $7f;
d9:= S9Table[d9] xor d7;
d7:= (S7Table[d7] xor d9) and $7f;
d7:= d7 xor ((FI_KEY shr 9) and $7f);
d9:= d9 xor (FI_KEY and $1ff);
d9:= S9Table[d9] xor d7;
Result:= (d7 shl 9) or d9;
end;
function TDCP_misty1.FO(const FO_IN: DWord; const k: longword): DWord;
var
t0, t1: DWord;
begin
t0:= FO_IN shr 16;
t1:= FO_IN and $FFFF;
t0:= t0 xor KeyData[k];
t0:= FI(t0,KeyData[((k+5) mod 8) + 8]);
t0:= t0 xor t1;
t1:= t1 xor KeyData[(k+2) mod 8];
t1:= FI(t1,KeyData[((k+1) mod 8) + 8]);
t1:= t1 xor t0;
t0:= t0 xor KeyData[(k+7) mod 8];
t0:= FI(t0,KeyData[((k+3) mod 8) + 8]);
t0:= t0 xor t1;
t1:= t1 xor KeyData[(k+4) mod 8];
Result:= (t1 shl 16) or t0;
end;
function TDCP_misty1.FL(const FL_IN: DWord; const k: longword): DWord;
var
d0, d1: DWord;
t: byte;
begin
d0:= FL_IN shr 16;
d1:= FL_IN and $FFFF;
if (k mod 2)<> 0 then
begin
t:= (k-1) div 2;
d1:= d1 xor (d0 and KeyData[((t + 2) mod 8) + 8]);
d0:= d0 xor (d1 or KeyData[(t + 4) mod 8]);
end
else
begin
t:= k div 2;
d1:= d1 xor (d0 and KeyData[t]);
d0:= d0 xor (d1 or KeyData[((t+6) mod 8) + 8]);
end;
Result:= (d0 shl 16) or d1;
end;
function TDCP_misty1.FLINV(const FL_IN: DWord; const k: longword): DWord;
var
d0, d1: DWord;
t: byte;
begin
d0:= FL_IN shr 16;
d1:= FL_IN and $FFFF;
if (k mod 2)<> 0 then
begin
t:= (k-1) div 2;
d0:= d0 xor (d1 or KeyData[(t+4) mod 8]);
d1:= d1 xor (d0 and KeyData[((t+2) mod 8) + 8]);
end
else
begin
t:= k div 2;
d0:= d0 xor (d1 or KeyData[((t+6) mod 8) + 8]);
d1:= d1 xor (d0 and KeyData[t]);
end;
Result:= (d0 shl 16) or d1;
end;
procedure TDCP_misty1.InitKey(const Key; Size: longword);
var
KeyB: array[0..15] of byte;
i: longword;
begin
dcpFillChar(KeyB,Sizeof(KeyB),0);
Move(Key,KeyB,Size div 8);
for i:= 0 to 7 do
KeyData[i]:= (KeyB[i*2] * 256) + KeyB[i*2+1];
for i:= 0 to 7 do
begin
KeyData[i+8]:= FI(KeyData[i],KeyData[(i+1) mod 8]);
KeyData[i+16]:= KeyData[i+8] and $1FF;
KeyData[i+24]:= KeyData[i+8] shr 9;
end;
end;
procedure TDCP_misty1.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_misty1.EncryptECB(const InData; var OutData);
var
d0, d1: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
d0:= SwapDWord(PDWord(@InData)^);
d1:= SwapDWord(PDWord(pointer(@InData)+4)^);
for i:= 0 to NUMROUNDS-1 do
begin
if (i mod 2)= 0 then
begin
d0:= FL(D0,i);
d1:= FL(D1,i+1);
d1:= d1 xor FO(d0,i);
end
else
d0:= d0 xor FO(d1,i);
end;
d0:= FL(d0,NUMROUNDS);
d1:= FL(d1,NUMROUNDS+1);
PDWord(@OutData)^:= SwapDWord(d1);
PDWord(pointer(@OutData)+4)^:= SwapDWord(d0);
end;
procedure TDCP_misty1.DecryptECB(const InData; var OutData);
var
d0, d1: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
d1:= SwapDWord(PDWord(@InData)^);
d0:= SwapDWord(PDWord(pointer(@InData)+4)^);
d1:= FLINV(d1,NUMROUNDS+1);
d0:= FLINV(d0,NUMROUNDS);
for i:= NUMROUNDS-1 downto 0 do
begin
if (i mod 2)= 0 then
begin
d1:= d1 xor FO(d0,i);
d0:= FLINV(D0,i);
d1:= FLINV(D1,i+1);
end
else
d0:= d0 xor FO(d1,i);
end;
PDWord(@OutData)^:= SwapDWord(d0);
PDWord(pointer(@OutData)+4)^:= SwapDWord(d1);
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Misty1 *******************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPmisty1;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
NUMROUNDS= 8;
type
TDCP_misty1= class(TDCP_blockcipher64)
protected
KeyData: array[0..31] of DWord;
function FI(const FI_IN, FI_KEY: DWord): DWord;
function FO(const FO_IN: DWord; const k: longword): DWord;
function FL(const FL_IN: DWord; const k: longword): DWord;
function FLINV(const FL_IN: DWord; const k: longword): DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPmisty1.inc}
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
class function TDCP_misty1.GetID: integer;
begin
Result:= DCP_misty1;
end;
class function TDCP_misty1.GetAlgorithm: string;
begin
Result:= 'Misty1';
end;
class function TDCP_misty1.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_misty1.SelfTest: boolean;
const
Key: array[0..15] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff);
Plain1: array[0..7] of byte= ($01,$23,$45,$67,$89,$ab,$cd,$ef);
Plain2: array[0..7] of byte= ($fe,$dc,$ba,$98,$76,$54,$32,$10);
Cipher1: array[0..7] of byte= ($8b,$1d,$a5,$f5,$6a,$b3,$d0,$7c);
Cipher2: array[0..7] of byte= ($04,$b6,$82,$40,$b1,$3b,$e9,$5d);
var
Cipher: TDCP_misty1;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_misty1.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(Plain1,Block);
Result:= CompareMem(@Cipher1,@Block,Sizeof(Block));
Cipher.DecryptECB(Block,Block);
Result:= Result and CompareMem(@Plain1,@Block,Sizeof(Block));
Cipher.EncryptECB(Plain2,Block);
Result:= Result and CompareMem(@Cipher2,@Block,Sizeof(Block));
Cipher.DecryptECB(Block,Block);
Result:= Result and CompareMem(@Plain2,@Block,Sizeof(Block));
Cipher.Burn;
Cipher.Free;
end;
function TDCP_misty1.FI(const FI_IN, FI_KEY: DWord): DWord;
var
d7, d9: DWord;
begin
d9:= (FI_IN shr 7) and $1ff;
d7:= FI_IN and $7f;
d9:= S9Table[d9] xor d7;
d7:= (S7Table[d7] xor d9) and $7f;
d7:= d7 xor ((FI_KEY shr 9) and $7f);
d9:= d9 xor (FI_KEY and $1ff);
d9:= S9Table[d9] xor d7;
Result:= (d7 shl 9) or d9;
end;
function TDCP_misty1.FO(const FO_IN: DWord; const k: longword): DWord;
var
t0, t1: DWord;
begin
t0:= FO_IN shr 16;
t1:= FO_IN and $FFFF;
t0:= t0 xor KeyData[k];
t0:= FI(t0,KeyData[((k+5) mod 8) + 8]);
t0:= t0 xor t1;
t1:= t1 xor KeyData[(k+2) mod 8];
t1:= FI(t1,KeyData[((k+1) mod 8) + 8]);
t1:= t1 xor t0;
t0:= t0 xor KeyData[(k+7) mod 8];
t0:= FI(t0,KeyData[((k+3) mod 8) + 8]);
t0:= t0 xor t1;
t1:= t1 xor KeyData[(k+4) mod 8];
Result:= (t1 shl 16) or t0;
end;
function TDCP_misty1.FL(const FL_IN: DWord; const k: longword): DWord;
var
d0, d1: DWord;
t: byte;
begin
d0:= FL_IN shr 16;
d1:= FL_IN and $FFFF;
if (k mod 2)<> 0 then
begin
t:= (k-1) div 2;
d1:= d1 xor (d0 and KeyData[((t + 2) mod 8) + 8]);
d0:= d0 xor (d1 or KeyData[(t + 4) mod 8]);
end
else
begin
t:= k div 2;
d1:= d1 xor (d0 and KeyData[t]);
d0:= d0 xor (d1 or KeyData[((t+6) mod 8) + 8]);
end;
Result:= (d0 shl 16) or d1;
end;
function TDCP_misty1.FLINV(const FL_IN: DWord; const k: longword): DWord;
var
d0, d1: DWord;
t: byte;
begin
d0:= FL_IN shr 16;
d1:= FL_IN and $FFFF;
if (k mod 2)<> 0 then
begin
t:= (k-1) div 2;
d0:= d0 xor (d1 or KeyData[(t+4) mod 8]);
d1:= d1 xor (d0 and KeyData[((t+2) mod 8) + 8]);
end
else
begin
t:= k div 2;
d0:= d0 xor (d1 or KeyData[((t+6) mod 8) + 8]);
d1:= d1 xor (d0 and KeyData[t]);
end;
Result:= (d0 shl 16) or d1;
end;
procedure TDCP_misty1.InitKey(const Key; Size: longword);
var
KeyB: array[0..15] of byte;
i: longword;
begin
dcpFillChar(KeyB,Sizeof(KeyB),0);
Move(Key,KeyB,Size div 8);
for i:= 0 to 7 do
KeyData[i]:= (KeyB[i*2] * 256) + KeyB[i*2+1];
for i:= 0 to 7 do
begin
KeyData[i+8]:= FI(KeyData[i],KeyData[(i+1) mod 8]);
KeyData[i+16]:= KeyData[i+8] and $1FF;
KeyData[i+24]:= KeyData[i+8] shr 9;
end;
end;
procedure TDCP_misty1.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_misty1.EncryptECB(const InData; var OutData);
var
d0, d1: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
d0:= SwapDWord(PDWord(@InData)^);
d1:= SwapDWord(PDWord(pointer(@InData)+4)^);
for i:= 0 to NUMROUNDS-1 do
begin
if (i mod 2)= 0 then
begin
d0:= FL(D0,i);
d1:= FL(D1,i+1);
d1:= d1 xor FO(d0,i);
end
else
d0:= d0 xor FO(d1,i);
end;
d0:= FL(d0,NUMROUNDS);
d1:= FL(d1,NUMROUNDS+1);
PDWord(@OutData)^:= SwapDWord(d1);
PDWord(pointer(@OutData)+4)^:= SwapDWord(d0);
end;
procedure TDCP_misty1.DecryptECB(const InData; var OutData);
var
d0, d1: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
d1:= SwapDWord(PDWord(@InData)^);
d0:= SwapDWord(PDWord(pointer(@InData)+4)^);
d1:= FLINV(d1,NUMROUNDS+1);
d0:= FLINV(d0,NUMROUNDS);
for i:= NUMROUNDS-1 downto 0 do
begin
if (i mod 2)= 0 then
begin
d1:= d1 xor FO(d0,i);
d0:= FLINV(D0,i);
d1:= FLINV(D1,i+1);
end
else
d0:= d0 xor FO(d1,i);
end;
PDWord(@OutData)^:= SwapDWord(d0);
PDWord(pointer(@OutData)+4)^:= SwapDWord(d1);
end;
end.

View File

@ -1,191 +1,191 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC2 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc2;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_rc2= class(TDCP_blockcipher64)
protected
KeyData: array[0..63] of word;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPrc2.inc}
function LRot16(a, n: word): word;
begin
Result:= (a shl n) or (a shr (16-n));
end;
function RRot16(a, n: word): word;
begin
Result:= (a shr n) or (a shl (16-n));
end;
class function TDCP_rc2.GetMaxKeySize: integer;
begin
Result:= 1024;
end;
class function TDCP_rc2.GetID: integer;
begin
Result:= DCP_rc2;
end;
class function TDCP_rc2.GetAlgorithm: string;
begin
Result:= 'RC2';
end;
class function TDCP_rc2.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F);
InData1: array[0..7] of byte=
($00,$00,$00,$00,$00,$00,$00,$00);
OutData1: array[0..7] of byte=
($50,$DC,$01,$62,$BD,$75,$7F,$31);
Key2: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$01);
InData2: array[0..7] of byte=
($00,$00,$00,$00,$00,$00,$00,$00);
OutData2: array[0..7] of byte=
($21,$82,$9C,$78,$A9,$F9,$C0,$74);
var
Cipher: TDCP_rc2;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc2.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= boolean(CompareMem(@Data,@OutData2,Sizeof(Data))) and Result;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData2,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc2.InitKey(const Key; Size: longword);
var
i: longword;
KeyB: array[0..127] of byte;
begin
dcpFillChar(KeyB, SizeOf(KeyB), 0);
Move(Key,KeyB,Size div 8);
for i:= (Size div 8) to 127 do
KeyB[i]:= sBox[(KeyB[i-(Size div 8)]+KeyB[i-1]) and $FF];
KeyB[0]:= sBox[KeyB[0]];
Move(KeyB,KeyData,Sizeof(KeyData));
end;
procedure TDCP_rc2.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_rc2.EncryptECB(const InData; var OutData);
var
i, j: longword;
w: array[0..3] of word;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
Pdword(@w[0])^:= Pdword(@InData)^;
Pdword(@w[2])^:= Pdword(pointer(@InData)+4)^;
for i:= 0 to 15 do
begin
j:= i*4;
w[0]:= LRot16((w[0]+(w[1] and (not w[3]))+(w[2] and w[3])+KeyData[j+0]),1);
w[1]:= LRot16((w[1]+(w[2] and (not w[0]))+(w[3] and w[0])+KeyData[j+1]),2);
w[2]:= LRot16((w[2]+(w[3] and (not w[1]))+(w[0] and w[1])+KeyData[j+2]),3);
w[3]:= LRot16((w[3]+(w[0] and (not w[2]))+(w[1] and w[2])+KeyData[j+3]),5);
if (i= 4) or (i= 10) then
begin
w[0]:= w[0]+KeyData[w[3] and 63];
w[1]:= w[1]+KeyData[w[0] and 63];
w[2]:= w[2]+KeyData[w[1] and 63];
w[3]:= w[3]+KeyData[w[2] and 63];
end;
end;
Pdword(@OutData)^:= Pdword(@w[0])^;
Pdword(pointer(@OutData)+4)^:= Pdword(@w[2])^;
end;
procedure TDCP_rc2.DecryptECB(const InData; var OutData);
var
i, j: longword;
w: array[0..3] of word;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
Pdword(@w[0])^:= Pdword(@InData)^;
Pdword(@w[2])^:= Pdword(pointer(@InData)+4)^;
for i:= 15 downto 0 do
begin
j:= i*4;
w[3]:= RRot16(w[3],5)-(w[0] and (not w[2]))-(w[1] and w[2])-KeyData[j+3];
w[2]:= RRot16(w[2],3)-(w[3] and (not w[1]))-(w[0] and w[1])-KeyData[j+2];
w[1]:= RRot16(w[1],2)-(w[2] and (not w[0]))-(w[3] and w[0])-KeyData[j+1];
w[0]:= RRot16(w[0],1)-(w[1] and (not w[3]))-(w[2] and w[3])-KeyData[j+0];
if (i= 5) or (i= 11) then
begin
w[3]:= w[3]-KeyData[w[2] and 63];
w[2]:= w[2]-KeyData[w[1] and 63];
w[1]:= w[1]-KeyData[w[0] and 63];
w[0]:= w[0]-KeyData[w[3] and 63];
end;
end;
Pdword(@OutData)^:= Pdword(@w[0])^;
Pdword(pointer(@OutData)+4)^:= Pdword(@w[2])^;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC2 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc2;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_rc2= class(TDCP_blockcipher64)
protected
KeyData: array[0..63] of word;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPrc2.inc}
function LRot16(a, n: word): word;
begin
Result:= (a shl n) or (a shr (16-n));
end;
function RRot16(a, n: word): word;
begin
Result:= (a shr n) or (a shl (16-n));
end;
class function TDCP_rc2.GetMaxKeySize: integer;
begin
Result:= 1024;
end;
class function TDCP_rc2.GetID: integer;
begin
Result:= DCP_rc2;
end;
class function TDCP_rc2.GetAlgorithm: string;
begin
Result:= 'RC2';
end;
class function TDCP_rc2.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F);
InData1: array[0..7] of byte=
($00,$00,$00,$00,$00,$00,$00,$00);
OutData1: array[0..7] of byte=
($50,$DC,$01,$62,$BD,$75,$7F,$31);
Key2: array[0..15] of byte=
($00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$01);
InData2: array[0..7] of byte=
($00,$00,$00,$00,$00,$00,$00,$00);
OutData2: array[0..7] of byte=
($21,$82,$9C,$78,$A9,$F9,$C0,$74);
var
Cipher: TDCP_rc2;
Data: array[0..7] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc2.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Data);
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Data);
Result:= boolean(CompareMem(@Data,@OutData2,Sizeof(Data))) and Result;
Cipher.DecryptECB(Data,Data);
Result:= boolean(CompareMem(@Data,@InData2,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc2.InitKey(const Key; Size: longword);
var
i: longword;
KeyB: array[0..127] of byte;
begin
dcpFillChar(KeyB, SizeOf(KeyB), 0);
Move(Key,KeyB,Size div 8);
for i:= (Size div 8) to 127 do
KeyB[i]:= sBox[(KeyB[i-(Size div 8)]+KeyB[i-1]) and $FF];
KeyB[0]:= sBox[KeyB[0]];
Move(KeyB,KeyData,Sizeof(KeyData));
end;
procedure TDCP_rc2.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_rc2.EncryptECB(const InData; var OutData);
var
i, j: longword;
w: array[0..3] of word;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
Pdword(@w[0])^:= Pdword(@InData)^;
Pdword(@w[2])^:= Pdword(pointer(@InData)+4)^;
for i:= 0 to 15 do
begin
j:= i*4;
w[0]:= LRot16((w[0]+(w[1] and (not w[3]))+(w[2] and w[3])+KeyData[j+0]),1);
w[1]:= LRot16((w[1]+(w[2] and (not w[0]))+(w[3] and w[0])+KeyData[j+1]),2);
w[2]:= LRot16((w[2]+(w[3] and (not w[1]))+(w[0] and w[1])+KeyData[j+2]),3);
w[3]:= LRot16((w[3]+(w[0] and (not w[2]))+(w[1] and w[2])+KeyData[j+3]),5);
if (i= 4) or (i= 10) then
begin
w[0]:= w[0]+KeyData[w[3] and 63];
w[1]:= w[1]+KeyData[w[0] and 63];
w[2]:= w[2]+KeyData[w[1] and 63];
w[3]:= w[3]+KeyData[w[2] and 63];
end;
end;
Pdword(@OutData)^:= Pdword(@w[0])^;
Pdword(pointer(@OutData)+4)^:= Pdword(@w[2])^;
end;
procedure TDCP_rc2.DecryptECB(const InData; var OutData);
var
i, j: longword;
w: array[0..3] of word;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
Pdword(@w[0])^:= Pdword(@InData)^;
Pdword(@w[2])^:= Pdword(pointer(@InData)+4)^;
for i:= 15 downto 0 do
begin
j:= i*4;
w[3]:= RRot16(w[3],5)-(w[0] and (not w[2]))-(w[1] and w[2])-KeyData[j+3];
w[2]:= RRot16(w[2],3)-(w[3] and (not w[1]))-(w[0] and w[1])-KeyData[j+2];
w[1]:= RRot16(w[1],2)-(w[2] and (not w[0]))-(w[3] and w[0])-KeyData[j+1];
w[0]:= RRot16(w[0],1)-(w[1] and (not w[3]))-(w[2] and w[3])-KeyData[j+0];
if (i= 5) or (i= 11) then
begin
w[3]:= w[3]-KeyData[w[2] and 63];
w[2]:= w[2]-KeyData[w[1] and 63];
w[1]:= w[1]-KeyData[w[0] and 63];
w[0]:= w[0]-KeyData[w[3] and 63];
end;
end;
Pdword(@OutData)^:= Pdword(@w[0])^;
Pdword(pointer(@OutData)+4)^:= Pdword(@w[2])^;
end;
end.

View File

@ -1,212 +1,212 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC4 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc4;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst;
type
TDCP_rc4= class(TDCP_cipher)
protected
KeyData, KeyOrg: array[0..255] of byte;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Init(const Key; Size: longword; InitVector: pointer); override;
procedure Reset; override;
procedure Burn; override;
procedure Encrypt(const InData; var OutData; Size: longword); override;
procedure Decrypt(const InData; var OutData; Size: longword); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
class function TDCP_rc4.GetID: integer;
begin
Result:= DCP_rc4;
end;
class function TDCP_rc4.GetAlgorithm: string;
begin
Result:= 'RC4';
end;
class function TDCP_rc4.GetMaxKeySize: integer;
begin
Result:= 2048;
end;
class function TDCP_rc4.SelfTest: boolean;
const
Key1: array[0..4] of byte= ($61,$8A,$63,$D2,$FB);
InData1: array[0..4] of byte= ($DC,$EE,$4C,$F9,$2C);
OutData1: array[0..4] of byte= ($F1,$38,$29,$C9,$DE);
var
Cipher: TDCP_rc4;
Data: array[0..4] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc4.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.Encrypt(InData1,Data,Sizeof(Data));
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.Decrypt(Data,Data,Sizeof(Data));
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc4.Init(const Key; Size: longword; InitVector: pointer);
var
i, j, t: longword;
xKey: array[0..255] of byte;
begin
if fInitialized then
Burn;
inherited Init(Key,Size,nil);
Size:= Size div 8;
i:= 0;
while i< 255 do
begin
KeyData[i]:= i;
xKey[i]:= PByte(pointer(@Key)+(i mod Size))^;
KeyData[i+1]:= i+1;
xKey[i+1]:= PByte(pointer(@Key)+((i+1) mod Size))^;
KeyData[i+2]:= i+2;
xKey[i+2]:= PByte(pointer(@Key)+((i+2) mod Size))^;
KeyData[i+3]:= i+3;
xKey[i+3]:= PByte(pointer(@Key)+((i+3) mod Size))^;
KeyData[i+4]:= i+4;
xKey[i+4]:= PByte(pointer(@Key)+((i+4) mod Size))^;
KeyData[i+5]:= i+5;
xKey[i+5]:= PByte(pointer(@Key)+((i+5) mod Size))^;
KeyData[i+6]:= i+6;
xKey[i+6]:= PByte(pointer(@Key)+((i+6) mod Size))^;
KeyData[i+7]:= i+7;
xKey[i+7]:= PByte(pointer(@Key)+((i+7) mod Size))^;
Inc(i,8);
end;
j:= 0;
i:= 0;
while i< 255 do
begin
j:= (j+KeyData[i]+xKey[i]) and $FF;
t:= KeyData[i];
KeyData[i]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+1]+xKey[i+1]) and $FF;
t:= KeyData[i+1];
KeyData[i+1]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+2]+xKey[i+2]) and $FF;
t:= KeyData[i+2];
KeyData[i+2]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+3]+xKey[i+3]) and $FF;
t:= KeyData[i+3];
KeyData[i+3]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+4]+xKey[i+4]) and $FF;
t:= KeyData[i+4];
KeyData[i+4]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+5]+xKey[i+5]) and $FF;
t:= KeyData[i+5];
KeyData[i+5]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+6]+xKey[i+6]) and $FF;
t:= KeyData[i+6];
KeyData[i+6]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+7]+xKey[i+7]) and $FF;
t:= KeyData[i+7];
KeyData[i+7]:= KeyData[j];
KeyData[j]:= t;
Inc(i,8);
end;
Move(KeyData,KeyOrg,Sizeof(KeyOrg));
end;
procedure TDCP_rc4.Reset;
begin
Move(KeyOrg,KeyData,Sizeof(KeyData));
end;
procedure TDCP_rc4.Burn;
begin
FillChar(KeyOrg,Sizeof(KeyOrg),$FF);
FillChar(KeyData,Sizeof(KeyData),$FF);
inherited Burn;
end;
procedure TDCP_rc4.Encrypt(const InData; var OutData; Size: longword);
var
i, j, t, k: longword;
begin
if not fInitialized then
raise EDCP_cipher.Create('Cipher not initialized');
i:= 0; j:= 0;
for k:= 0 to Size-1 do
begin
i:= (i + 1) and $FF;
t:= KeyData[i];
j:= (j + t) and $FF;
KeyData[i]:= KeyData[j];
KeyData[j]:= t;
t:= (t + KeyData[i]) and $FF;
Pbytearray(@OutData)^[k]:= Pbytearray(@InData)^[k] xor KeyData[t];
end;
end;
procedure TDCP_rc4.Decrypt(const InData; var OutData; Size: longword);
var
i, j, t, k: longword;
begin
if not fInitialized then
raise EDCP_cipher.Create('Cipher not initialized');
i:= 0; j:= 0;
for k:= 0 to Size-1 do
begin
i:= (i + 1) and $FF;
t:= KeyData[i];
j:= (j + t) and $FF;
KeyData[i]:= KeyData[j];
KeyData[j]:= t;
t:= (t + KeyData[i]) and $FF;
Pbytearray(@OutData)^[k]:= Pbytearray(@InData)^[k] xor KeyData[t];
end;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC4 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc4;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst;
type
TDCP_rc4= class(TDCP_cipher)
protected
KeyData, KeyOrg: array[0..255] of byte;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Init(const Key; Size: longword; InitVector: pointer); override;
procedure Reset; override;
procedure Burn; override;
procedure Encrypt(const InData; var OutData; Size: longword); override;
procedure Decrypt(const InData; var OutData; Size: longword); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
class function TDCP_rc4.GetID: integer;
begin
Result:= DCP_rc4;
end;
class function TDCP_rc4.GetAlgorithm: string;
begin
Result:= 'RC4';
end;
class function TDCP_rc4.GetMaxKeySize: integer;
begin
Result:= 2048;
end;
class function TDCP_rc4.SelfTest: boolean;
const
Key1: array[0..4] of byte= ($61,$8A,$63,$D2,$FB);
InData1: array[0..4] of byte= ($DC,$EE,$4C,$F9,$2C);
OutData1: array[0..4] of byte= ($F1,$38,$29,$C9,$DE);
var
Cipher: TDCP_rc4;
Data: array[0..4] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc4.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.Encrypt(InData1,Data,Sizeof(Data));
Result:= boolean(CompareMem(@Data,@OutData1,Sizeof(Data)));
Cipher.Reset;
Cipher.Decrypt(Data,Data,Sizeof(Data));
Result:= boolean(CompareMem(@Data,@InData1,Sizeof(Data))) and Result;
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc4.Init(const Key; Size: longword; InitVector: pointer);
var
i, j, t: longword;
xKey: array[0..255] of byte;
begin
if fInitialized then
Burn;
inherited Init(Key,Size,nil);
Size:= Size div 8;
i:= 0;
while i< 255 do
begin
KeyData[i]:= i;
xKey[i]:= PByte(pointer(@Key)+(i mod Size))^;
KeyData[i+1]:= i+1;
xKey[i+1]:= PByte(pointer(@Key)+((i+1) mod Size))^;
KeyData[i+2]:= i+2;
xKey[i+2]:= PByte(pointer(@Key)+((i+2) mod Size))^;
KeyData[i+3]:= i+3;
xKey[i+3]:= PByte(pointer(@Key)+((i+3) mod Size))^;
KeyData[i+4]:= i+4;
xKey[i+4]:= PByte(pointer(@Key)+((i+4) mod Size))^;
KeyData[i+5]:= i+5;
xKey[i+5]:= PByte(pointer(@Key)+((i+5) mod Size))^;
KeyData[i+6]:= i+6;
xKey[i+6]:= PByte(pointer(@Key)+((i+6) mod Size))^;
KeyData[i+7]:= i+7;
xKey[i+7]:= PByte(pointer(@Key)+((i+7) mod Size))^;
Inc(i,8);
end;
j:= 0;
i:= 0;
while i< 255 do
begin
j:= (j+KeyData[i]+xKey[i]) and $FF;
t:= KeyData[i];
KeyData[i]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+1]+xKey[i+1]) and $FF;
t:= KeyData[i+1];
KeyData[i+1]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+2]+xKey[i+2]) and $FF;
t:= KeyData[i+2];
KeyData[i+2]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+3]+xKey[i+3]) and $FF;
t:= KeyData[i+3];
KeyData[i+3]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+4]+xKey[i+4]) and $FF;
t:= KeyData[i+4];
KeyData[i+4]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+5]+xKey[i+5]) and $FF;
t:= KeyData[i+5];
KeyData[i+5]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+6]+xKey[i+6]) and $FF;
t:= KeyData[i+6];
KeyData[i+6]:= KeyData[j];
KeyData[j]:= t;
j:= (j+KeyData[i+7]+xKey[i+7]) and $FF;
t:= KeyData[i+7];
KeyData[i+7]:= KeyData[j];
KeyData[j]:= t;
Inc(i,8);
end;
Move(KeyData,KeyOrg,Sizeof(KeyOrg));
end;
procedure TDCP_rc4.Reset;
begin
Move(KeyOrg,KeyData,Sizeof(KeyData));
end;
procedure TDCP_rc4.Burn;
begin
FillChar(KeyOrg,Sizeof(KeyOrg),$FF);
FillChar(KeyData,Sizeof(KeyData),$FF);
inherited Burn;
end;
procedure TDCP_rc4.Encrypt(const InData; var OutData; Size: longword);
var
i, j, t, k: longword;
begin
if not fInitialized then
raise EDCP_cipher.Create('Cipher not initialized');
i:= 0; j:= 0;
for k:= 0 to Size-1 do
begin
i:= (i + 1) and $FF;
t:= KeyData[i];
j:= (j + t) and $FF;
KeyData[i]:= KeyData[j];
KeyData[j]:= t;
t:= (t + KeyData[i]) and $FF;
Pbytearray(@OutData)^[k]:= Pbytearray(@InData)^[k] xor KeyData[t];
end;
end;
procedure TDCP_rc4.Decrypt(const InData; var OutData; Size: longword);
var
i, j, t, k: longword;
begin
if not fInitialized then
raise EDCP_cipher.Create('Cipher not initialized');
i:= 0; j:= 0;
for k:= 0 to Size-1 do
begin
i:= (i + 1) and $FF;
t:= KeyData[i];
j:= (j + t) and $FF;
KeyData[i]:= KeyData[j];
KeyData[j]:= t;
t:= (t + KeyData[i]) and $FF;
Pbytearray(@OutData)^[k]:= Pbytearray(@InData)^[k] xor KeyData[t];
end;
end;
end.

View File

@ -1,203 +1,203 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC5 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc5;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
NUMROUNDS= 12; { number of rounds must be between 12-16 }
type
TDCP_rc5= class(TDCP_blockcipher64)
protected
KeyData: array[0..((NUMROUNDS*2)+1)] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
const
sBox: array[0..33] of dword= (
$B7E15163,$5618CB1C,$F45044D5,$9287BE8E,$30BF3847,$CEF6B200,
$6D2E2BB9,$0B65A572,$A99D1F2B,$47D498E4,$E60C129D,$84438C56,
$227B060F,$C0B27FC8,$5EE9F981,$FD21733A,$9B58ECF3,$399066AC,
$D7C7E065,$75FF5A1E,$1436D3D7,$B26E4D90,$50A5C749,$EEDD4102,
$8D14BABB,$2B4C3474,$C983AE2D,$67BB27E6,$05F2A19F,$A42A1B58,
$42619511,$E0990ECA,$7ED08883,$1D08023C);
function LRot32(a, b: longword): longword;
begin
Result:= (a shl b) or (a shr (32-b));
end;
function RRot32(a, b: longword): longword;
begin
Result:= (a shr b) or (a shl (32-b));
end;
class function TDCP_rc5.GetID: integer;
begin
Result:= DCP_rc5;
end;
class function TDCP_rc5.GetAlgorithm: string;
begin
Result:= 'RC5';
end;
class function TDCP_rc5.GetMaxKeySize: integer;
begin
Result:= 2048;
end;
class function TDCP_rc5.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($DC,$49,$DB,$13,$75,$A5,$58,$4F,$64,$85,$B4,$13,$B5,$F1,$2B,$AF);
Plain1: array[0..1] of dword=
($B7B3422F,$92FC6903);
Cipher1: array[0..1] of dword=
($B278C165,$CC97D184);
Key2: array[0..15] of byte=
($52,$69,$F1,$49,$D4,$1B,$A0,$15,$24,$97,$57,$4D,$7F,$15,$31,$25);
Plain2: array[0..1] of dword=
($B278C165,$CC97D184);
Cipher2: array[0..1] of dword=
($15E444EB,$249831DA);
var
Cipher: TDCP_rc5;
Data: array[0..1] of dword;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc5.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(Plain1,Data);
Result:= boolean(CompareMem(@Data,@Cipher1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(Plain2,Data);
Result:= Result and boolean(CompareMem(@Data,@Cipher2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc5.InitKey(const Key; Size: longword);
var
xKeyD: array[0..63] of DWord;
i, j, k, xKeyLen: longword;
A, B: DWord;
begin
dcpFillChar(xKeyD,Sizeof(xKeyD),0);
Size:= Size div 8;
Move(Key,xKeyD,Size);
xKeyLen:= Size div 4;
if (Size mod 4)<> 0 then
Inc(xKeyLen);
Move(sBox,KeyData,(NUMROUNDS+1)*8);
i:= 0; j:= 0;
A:= 0; B:= 0;
if xKeyLen> ((NUMROUNDS+1)*2) then
k:= xKeyLen*3
else
k:= (NUMROUNDS+1)*6;
for k:= k downto 1 do
begin
A:= LRot32(KeyData[i]+A+B,3);
KeyData[i]:= A;
B:= LRot32(xKeyD[j]+A+B,A+B);
xKeyD[j]:= B;
i:= (i+1) mod ((NUMROUNDS+1)*2);
j:= (j+1) mod xKeyLen;
end;
FillChar(xKeyD,Sizeof(xKeyD),0);
end;
procedure TDCP_rc5.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),$FF);
inherited Burn;
end;
procedure TDCP_rc5.EncryptECB(const InData; var OutData);
var
A, B: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A:= PDword(@InData)^ + KeyData[0];
B:= PDword(pointer(@InData)+4)^ + KeyData[1];
for i:= 1 to NUMROUNDS do
begin
A:= A xor B;
A:= LRot32(A,B)+KeyData[2*i];
B:= B xor A;
B:= LRot32(B,A)+KeyData[(2*i)+1];
end;
PDword(@OutData)^:= A;
PDword(pointer(@OutData)+4)^:= B;
end;
procedure TDCP_rc5.DecryptECB(const InData; var OutData);
var
A, B: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A:= PDword(@InData)^;
B:= PDword(pointer(@InData)+4)^;
for i:= NUMROUNDS downto 1 do
begin
B:= RRot32(B-KeyData[(2*i)+1],A);
B:= B xor A;
A:= RRot32(A-KeyData[2*i],B);
A:= A xor B;
end;
PDword(@OutData)^:= A - KeyData[0];
PDword(pointer(@OutData)+4)^:= B - KeyData[1];
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC5 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc5;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
NUMROUNDS= 12; { number of rounds must be between 12-16 }
type
TDCP_rc5= class(TDCP_blockcipher64)
protected
KeyData: array[0..((NUMROUNDS*2)+1)] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
const
sBox: array[0..33] of dword= (
$B7E15163,$5618CB1C,$F45044D5,$9287BE8E,$30BF3847,$CEF6B200,
$6D2E2BB9,$0B65A572,$A99D1F2B,$47D498E4,$E60C129D,$84438C56,
$227B060F,$C0B27FC8,$5EE9F981,$FD21733A,$9B58ECF3,$399066AC,
$D7C7E065,$75FF5A1E,$1436D3D7,$B26E4D90,$50A5C749,$EEDD4102,
$8D14BABB,$2B4C3474,$C983AE2D,$67BB27E6,$05F2A19F,$A42A1B58,
$42619511,$E0990ECA,$7ED08883,$1D08023C);
function LRot32(a, b: longword): longword;
begin
Result:= (a shl b) or (a shr (32-b));
end;
function RRot32(a, b: longword): longword;
begin
Result:= (a shr b) or (a shl (32-b));
end;
class function TDCP_rc5.GetID: integer;
begin
Result:= DCP_rc5;
end;
class function TDCP_rc5.GetAlgorithm: string;
begin
Result:= 'RC5';
end;
class function TDCP_rc5.GetMaxKeySize: integer;
begin
Result:= 2048;
end;
class function TDCP_rc5.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($DC,$49,$DB,$13,$75,$A5,$58,$4F,$64,$85,$B4,$13,$B5,$F1,$2B,$AF);
Plain1: array[0..1] of dword=
($B7B3422F,$92FC6903);
Cipher1: array[0..1] of dword=
($B278C165,$CC97D184);
Key2: array[0..15] of byte=
($52,$69,$F1,$49,$D4,$1B,$A0,$15,$24,$97,$57,$4D,$7F,$15,$31,$25);
Plain2: array[0..1] of dword=
($B278C165,$CC97D184);
Cipher2: array[0..1] of dword=
($15E444EB,$249831DA);
var
Cipher: TDCP_rc5;
Data: array[0..1] of dword;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc5.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(Plain1,Data);
Result:= boolean(CompareMem(@Data,@Cipher1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(Plain2,Data);
Result:= Result and boolean(CompareMem(@Data,@Cipher2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc5.InitKey(const Key; Size: longword);
var
xKeyD: array[0..63] of DWord;
i, j, k, xKeyLen: longword;
A, B: DWord;
begin
dcpFillChar(xKeyD,Sizeof(xKeyD),0);
Size:= Size div 8;
Move(Key,xKeyD,Size);
xKeyLen:= Size div 4;
if (Size mod 4)<> 0 then
Inc(xKeyLen);
Move(sBox,KeyData,(NUMROUNDS+1)*8);
i:= 0; j:= 0;
A:= 0; B:= 0;
if xKeyLen> ((NUMROUNDS+1)*2) then
k:= xKeyLen*3
else
k:= (NUMROUNDS+1)*6;
for k:= k downto 1 do
begin
A:= LRot32(KeyData[i]+A+B,3);
KeyData[i]:= A;
B:= LRot32(xKeyD[j]+A+B,A+B);
xKeyD[j]:= B;
i:= (i+1) mod ((NUMROUNDS+1)*2);
j:= (j+1) mod xKeyLen;
end;
FillChar(xKeyD,Sizeof(xKeyD),0);
end;
procedure TDCP_rc5.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),$FF);
inherited Burn;
end;
procedure TDCP_rc5.EncryptECB(const InData; var OutData);
var
A, B: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A:= PDword(@InData)^ + KeyData[0];
B:= PDword(pointer(@InData)+4)^ + KeyData[1];
for i:= 1 to NUMROUNDS do
begin
A:= A xor B;
A:= LRot32(A,B)+KeyData[2*i];
B:= B xor A;
B:= LRot32(B,A)+KeyData[(2*i)+1];
end;
PDword(@OutData)^:= A;
PDword(pointer(@OutData)+4)^:= B;
end;
procedure TDCP_rc5.DecryptECB(const InData; var OutData);
var
A, B: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
A:= PDword(@InData)^;
B:= PDword(pointer(@InData)+4)^;
for i:= NUMROUNDS downto 1 do
begin
B:= RRot32(B-KeyData[(2*i)+1],A);
B:= B xor A;
A:= RRot32(A-KeyData[2*i],B);
A:= A xor B;
end;
PDword(@OutData)^:= A - KeyData[0];
PDword(pointer(@OutData)+4)^:= B - KeyData[1];
end;
end.

View File

@ -1,228 +1,228 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC6 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc6;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
NUMROUNDS= 20; { number of rounds must be between 16-24 }
type
TDCP_rc6= class(TDCP_blockcipher128)
protected
KeyData: array[0..((NUMROUNDS*2)+3)] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
const
sBox: array[0..51] of DWord= (
$B7E15163,$5618CB1C,$F45044D5,$9287BE8E,$30BF3847,$CEF6B200,
$6D2E2BB9,$0B65A572,$A99D1F2B,$47D498E4,$E60C129D,$84438C56,
$227B060F,$C0B27FC8,$5EE9F981,$FD21733A,$9B58ECF3,$399066AC,
$D7C7E065,$75FF5A1E,$1436D3D7,$B26E4D90,$50A5C749,$EEDD4102,
$8D14BABB,$2B4C3474,$C983AE2D,$67BB27E6,$05F2A19F,$A42A1B58,
$42619511,$E0990ECA,$7ED08883,$1D08023C,$BB3F7BF5,$5976F5AE,
$F7AE6F67,$95E5E920,$341D62D9,$D254DC92,$708C564B,$0EC3D004,
$ACFB49BD,$4B32C376,$E96A3D2F,$87A1B6E8,$25D930A1,$C410AA5A,
$62482413,$007F9DCC,$9EB71785,$3CEE913E);
function LRot32(X: DWord; c: longword): DWord;
begin
LRot32:= (X shl c) or (X shr (32 - c));
end;
function RRot32(X: DWord; c: longword): DWord;
begin
RRot32:= (X shr c) or (X shl (32 - c));
end;
class function TDCP_rc6.GetID: integer;
begin
Result:= DCP_rc6;
end;
class function TDCP_rc6.GetAlgorithm: string;
begin
Result:= 'RC6';
end;
class function TDCP_rc6.GetMaxKeySize: integer;
begin
Result:= 2048;
end;
class function TDCP_rc6.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$ef,$01,$12,$23,$34,$45,$56,$67,$78);
Plain1: array[0..15] of byte=
($02,$13,$24,$35,$46,$57,$68,$79,$8a,$9b,$ac,$bd,$ce,$df,$e0,$f1);
Cipher1: array[0..15] of byte=
($52,$4e,$19,$2f,$47,$15,$c6,$23,$1f,$51,$f6,$36,$7e,$a4,$3f,$18);
Key2: array[0..31] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$ef,$01,$12,$23,$34,$45,$56,$67,$78,
$89,$9a,$ab,$bc,$cd,$de,$ef,$f0,$10,$32,$54,$76,$98,$ba,$dc,$fe);
Plain2: array[0..15] of byte=
($02,$13,$24,$35,$46,$57,$68,$79,$8a,$9b,$ac,$bd,$ce,$df,$e0,$f1);
Cipher2: array[0..15] of byte=
($c8,$24,$18,$16,$f0,$d7,$e4,$89,$20,$ad,$16,$a1,$67,$4e,$5d,$48);
var
Cipher: TDCP_rc6;
Data: array[0..15] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc6.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(Plain1,Data);
Result:= boolean(CompareMem(@Data,@Cipher1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(Plain2,Data);
Result:= Result and boolean(CompareMem(@Data,@Cipher2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc6.InitKey(const Key; Size: longword);
var
xKeyD: array[0..63] of DWord;
i, j, k, xKeyLen: longword;
A, B: DWord;
begin
Size:= Size div 8;
dcpFillChar(xKeyD,Sizeof(xKeyD),0);
Move(Key,xKeyD,Size);
xKeyLen:= Size div 4;
if (Size mod 4)<> 0 then
Inc(xKeyLen);
Move(sBox,KeyData,((NUMROUNDS*2)+4)*4);
i:= 0; j:= 0;
A:= 0; B:= 0;
if xKeyLen> ((NUMROUNDS*2)+4) then
k:= xKeyLen*3
else
k:= ((NUMROUNDS*2)+4)*3;
for k:= 1 to k do
begin
A:= LRot32(KeyData[i]+A+B,3);
KeyData[i]:= A;
B:= LRot32(xKeyD[j]+A+B,A+B);
xKeyD[j]:= B;
i:= (i+1) mod ((NUMROUNDS*2)+4);
j:= (j+1) mod xKeyLen;
end;
FillChar(xKeyD,Sizeof(xKeyD),0);
end;
procedure TDCP_rc6.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),$FF);
inherited Burn;
end;
procedure TDCP_rc6.EncryptECB(const InData; var OutData);
var
x0, x1, x2, x3: DWord;
u, t: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x0:= PDword(@InData)^;
x1:= PDword(pointer(@InData)+4)^;
x2:= PDword(pointer(@InData)+8)^;
x3:= PDword(pointer(@InData)+12)^;
x1:= x1 + KeyData[0];
x3:= x3 + KeyData[1];
for i:= 1 to NUMROUNDS do
begin
t:= Lrot32(x1 * (2*x1 + 1),5);
u:= Lrot32(x3 * (2*x3 + 1),5);
x0:= Lrot32(x0 xor t,u) + KeyData[2*i];
x2:= Lrot32(x2 xor u,t) + KeyData[2*i+1];
t:= x0; x0:= x1; x1:= x2; x2:= x3; x3:= t;
end;
x0:= x0 + KeyData[(2*NUMROUNDS)+2];
x2:= x2 + KeyData[(2*NUMROUNDS)+3];
PDword(@OutData)^:= x0;
PDword(pointer(@OutData)+4)^:= x1;
PDword(pointer(@OutData)+8)^:= x2;
PDword(pointer(@OutData)+12)^:= x3;
end;
procedure TDCP_rc6.DecryptECB(const InData; var OutData);
var
x0, x1, x2, x3: DWord;
u, t: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x0:= PDword(@InData)^;
x1:= PDword(pointer(@InData)+4)^;
x2:= PDword(pointer(@InData)+8)^;
x3:= PDword(pointer(@InData)+12)^;
x2:= x2 - KeyData[(2*NUMROUNDS)+3];
x0:= x0 - KeyData[(2*NUMROUNDS)+2];
for i:= NUMROUNDS downto 1 do
begin
t:= x0; x0:= x3; x3:= x2; x2:= x1; x1:= t;
u:= Lrot32(x3 * (2*x3 + 1),5);
t:= Lrot32(x1 * (2*x1 + 1),5);
x2:= Rrot32(x2 - KeyData[2*i+1],t) xor u;
x0:= Rrot32(x0 - KeyData[2*i],u) xor t;
end;
x3:= x3 - KeyData[1];
x1:= x1 - KeyData[0];
PDword(@OutData)^:= x0;
PDword(pointer(@OutData)+4)^:= x1;
PDword(pointer(@OutData)+8)^:= x2;
PDword(pointer(@OutData)+12)^:= x3;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of RC6 **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrc6;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
NUMROUNDS= 20; { number of rounds must be between 16-24 }
type
TDCP_rc6= class(TDCP_blockcipher128)
protected
KeyData: array[0..((NUMROUNDS*2)+3)] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
const
sBox: array[0..51] of DWord= (
$B7E15163,$5618CB1C,$F45044D5,$9287BE8E,$30BF3847,$CEF6B200,
$6D2E2BB9,$0B65A572,$A99D1F2B,$47D498E4,$E60C129D,$84438C56,
$227B060F,$C0B27FC8,$5EE9F981,$FD21733A,$9B58ECF3,$399066AC,
$D7C7E065,$75FF5A1E,$1436D3D7,$B26E4D90,$50A5C749,$EEDD4102,
$8D14BABB,$2B4C3474,$C983AE2D,$67BB27E6,$05F2A19F,$A42A1B58,
$42619511,$E0990ECA,$7ED08883,$1D08023C,$BB3F7BF5,$5976F5AE,
$F7AE6F67,$95E5E920,$341D62D9,$D254DC92,$708C564B,$0EC3D004,
$ACFB49BD,$4B32C376,$E96A3D2F,$87A1B6E8,$25D930A1,$C410AA5A,
$62482413,$007F9DCC,$9EB71785,$3CEE913E);
function LRot32(X: DWord; c: longword): DWord;
begin
LRot32:= (X shl c) or (X shr (32 - c));
end;
function RRot32(X: DWord; c: longword): DWord;
begin
RRot32:= (X shr c) or (X shl (32 - c));
end;
class function TDCP_rc6.GetID: integer;
begin
Result:= DCP_rc6;
end;
class function TDCP_rc6.GetAlgorithm: string;
begin
Result:= 'RC6';
end;
class function TDCP_rc6.GetMaxKeySize: integer;
begin
Result:= 2048;
end;
class function TDCP_rc6.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$ef,$01,$12,$23,$34,$45,$56,$67,$78);
Plain1: array[0..15] of byte=
($02,$13,$24,$35,$46,$57,$68,$79,$8a,$9b,$ac,$bd,$ce,$df,$e0,$f1);
Cipher1: array[0..15] of byte=
($52,$4e,$19,$2f,$47,$15,$c6,$23,$1f,$51,$f6,$36,$7e,$a4,$3f,$18);
Key2: array[0..31] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$ef,$01,$12,$23,$34,$45,$56,$67,$78,
$89,$9a,$ab,$bc,$cd,$de,$ef,$f0,$10,$32,$54,$76,$98,$ba,$dc,$fe);
Plain2: array[0..15] of byte=
($02,$13,$24,$35,$46,$57,$68,$79,$8a,$9b,$ac,$bd,$ce,$df,$e0,$f1);
Cipher2: array[0..15] of byte=
($c8,$24,$18,$16,$f0,$d7,$e4,$89,$20,$ad,$16,$a1,$67,$4e,$5d,$48);
var
Cipher: TDCP_rc6;
Data: array[0..15] of byte;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_rc6.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(Plain1,Data);
Result:= boolean(CompareMem(@Data,@Cipher1,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain1,Sizeof(Data)));
Cipher.Burn;
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(Plain2,Data);
Result:= Result and boolean(CompareMem(@Data,@Cipher2,Sizeof(Data)));
Cipher.DecryptECB(Data,Data);
Result:= Result and boolean(CompareMem(@Data,@Plain2,Sizeof(Data)));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_rc6.InitKey(const Key; Size: longword);
var
xKeyD: array[0..63] of DWord;
i, j, k, xKeyLen: longword;
A, B: DWord;
begin
Size:= Size div 8;
dcpFillChar(xKeyD,Sizeof(xKeyD),0);
Move(Key,xKeyD,Size);
xKeyLen:= Size div 4;
if (Size mod 4)<> 0 then
Inc(xKeyLen);
Move(sBox,KeyData,((NUMROUNDS*2)+4)*4);
i:= 0; j:= 0;
A:= 0; B:= 0;
if xKeyLen> ((NUMROUNDS*2)+4) then
k:= xKeyLen*3
else
k:= ((NUMROUNDS*2)+4)*3;
for k:= 1 to k do
begin
A:= LRot32(KeyData[i]+A+B,3);
KeyData[i]:= A;
B:= LRot32(xKeyD[j]+A+B,A+B);
xKeyD[j]:= B;
i:= (i+1) mod ((NUMROUNDS*2)+4);
j:= (j+1) mod xKeyLen;
end;
FillChar(xKeyD,Sizeof(xKeyD),0);
end;
procedure TDCP_rc6.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),$FF);
inherited Burn;
end;
procedure TDCP_rc6.EncryptECB(const InData; var OutData);
var
x0, x1, x2, x3: DWord;
u, t: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x0:= PDword(@InData)^;
x1:= PDword(pointer(@InData)+4)^;
x2:= PDword(pointer(@InData)+8)^;
x3:= PDword(pointer(@InData)+12)^;
x1:= x1 + KeyData[0];
x3:= x3 + KeyData[1];
for i:= 1 to NUMROUNDS do
begin
t:= Lrot32(x1 * (2*x1 + 1),5);
u:= Lrot32(x3 * (2*x3 + 1),5);
x0:= Lrot32(x0 xor t,u) + KeyData[2*i];
x2:= Lrot32(x2 xor u,t) + KeyData[2*i+1];
t:= x0; x0:= x1; x1:= x2; x2:= x3; x3:= t;
end;
x0:= x0 + KeyData[(2*NUMROUNDS)+2];
x2:= x2 + KeyData[(2*NUMROUNDS)+3];
PDword(@OutData)^:= x0;
PDword(pointer(@OutData)+4)^:= x1;
PDword(pointer(@OutData)+8)^:= x2;
PDword(pointer(@OutData)+12)^:= x3;
end;
procedure TDCP_rc6.DecryptECB(const InData; var OutData);
var
x0, x1, x2, x3: DWord;
u, t: DWord;
i: longword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x0:= PDword(@InData)^;
x1:= PDword(pointer(@InData)+4)^;
x2:= PDword(pointer(@InData)+8)^;
x3:= PDword(pointer(@InData)+12)^;
x2:= x2 - KeyData[(2*NUMROUNDS)+3];
x0:= x0 - KeyData[(2*NUMROUNDS)+2];
for i:= NUMROUNDS downto 1 do
begin
t:= x0; x0:= x3; x3:= x2; x2:= x1; x1:= t;
u:= Lrot32(x3 * (2*x3 + 1),5);
t:= Lrot32(x1 * (2*x1 + 1),5);
x2:= Rrot32(x2 - KeyData[2*i+1],t) xor u;
x0:= Rrot32(x0 - KeyData[2*i],u) xor t;
end;
x3:= x3 - KeyData[1];
x1:= x1 - KeyData[0];
PDword(@OutData)^:= x0;
PDword(pointer(@OutData)+4)^:= x1;
PDword(pointer(@OutData)+8)^:= x2;
PDword(pointer(@OutData)+12)^:= x3;
end;
end.

View File

@ -1,363 +1,363 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Rijndael *****************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrijndael;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
BC= 4;
MAXROUNDS= 14;
type
TDCP_rijndael= class(TDCP_blockcipher128)
protected
numrounds: longword;
rk, drk: array[0..MAXROUNDS,0..7] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPrijndael.inc}
class function TDCP_rijndael.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_rijndael.GetID: integer;
begin
Result:= DCP_rijndael;
end;
class function TDCP_rijndael.GetAlgorithm: string;
begin
Result:= 'Rijndael';
end;
class function TDCP_rijndael.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($00,$01,$02,$03,$05,$06,$07,$08,$0A,$0B,$0C,$0D,$0F,$10,$11,$12);
InData1: array[0..15] of byte=
($50,$68,$12,$A4,$5F,$08,$C8,$89,$B9,$7F,$59,$80,$03,$8B,$83,$59);
OutData1: array[0..15] of byte=
($D8,$F5,$32,$53,$82,$89,$EF,$7D,$06,$B5,$06,$A4,$FD,$5B,$E9,$C9);
Key2: array[0..23] of byte=
($A0,$A1,$A2,$A3,$A5,$A6,$A7,$A8,$AA,$AB,$AC,$AD,$AF,$B0,$B1,$B2,
$B4,$B5,$B6,$B7,$B9,$BA,$BB,$BC);
InData2: array[0..15] of byte=
($4F,$1C,$76,$9D,$1E,$5B,$05,$52,$C7,$EC,$A8,$4D,$EA,$26,$A5,$49);
OutData2: array[0..15] of byte=
($F3,$84,$72,$10,$D5,$39,$1E,$23,$60,$60,$8E,$5A,$CB,$56,$05,$81);
Key3: array[0..31] of byte=
($00,$01,$02,$03,$05,$06,$07,$08,$0A,$0B,$0C,$0D,$0F,$10,$11,$12,
$14,$15,$16,$17,$19,$1A,$1B,$1C,$1E,$1F,$20,$21,$23,$24,$25,$26);
InData3: array[0..15] of byte=
($5E,$25,$CA,$78,$F0,$DE,$55,$80,$25,$24,$D3,$8D,$A3,$FE,$44,$56);
OutData3: array[0..15] of byte=
($E8,$B7,$2B,$4E,$8B,$E2,$43,$43,$8C,$9F,$FF,$1F,$0E,$20,$58,$72);
var
Block: array[0..15] of byte;
Cipher: TDCP_rijndael;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_rijndael.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,16));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,16));
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InData3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData3,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData3,16));
Cipher.Free;
end;
procedure InvMixColumn(a: PByteArray; BC: byte);
var
j: longword;
begin
for j:= 0 to (BC-1) do
PDWord(@(a^[j*4]))^:= PDWord(@U1[a^[j*4+0]])^ xor
PDWord(@U2[a^[j*4+1]])^ xor
PDWord(@U3[a^[j*4+2]])^ xor
PDWord(@U4[a^[j*4+3]])^;
end;
procedure TDCP_rijndael.InitKey(const Key; Size: longword);
var
KC, ROUNDS, j, r, t, rconpointer: longword;
tk: array[0..MAXKC-1,0..3] of byte;
begin
Size:= Size div 8;
dcpFillChar(tk,Sizeof(tk),0);
Move(Key,tk,Size);
if Size<= 16 then
begin
KC:= 4;
Rounds:= 10;
end
else if Size<= 24 then
begin
KC:= 6;
Rounds:= 12;
end
else
begin
KC:= 8;
Rounds:= 14;
end;
numrounds:= rounds;
r:= 0;
t:= 0;
j:= 0;
while (j< KC) and (r< (rounds+1)) do
begin
while (j< KC) and (t< BC) do
begin
rk[r,t]:= PDWord(@tk[j])^;
Inc(j);
Inc(t);
end;
if t= BC then
begin
t:= 0;
Inc(r);
end;
end;
rconpointer:= 0;
while (r< (rounds+1)) do
begin
tk[0,0]:= tk[0,0] xor S[tk[KC-1,1]];
tk[0,1]:= tk[0,1] xor S[tk[KC-1,2]];
tk[0,2]:= tk[0,2] xor S[tk[KC-1,3]];
tk[0,3]:= tk[0,3] xor S[tk[KC-1,0]];
tk[0,0]:= tk[0,0] xor rcon[rconpointer];
Inc(rconpointer);
if KC<> 8 then
begin
for j:= 1 to (KC-1) do
PDWord(@tk[j])^:= PDWord(@tk[j])^ xor PDWord(@tk[j-1])^;
end
else
begin
for j:= 1 to ((KC div 2)-1) do
PDWord(@tk[j])^:= PDWord(@tk[j])^ xor PDWord(@tk[j-1])^;
tk[KC div 2,0]:= tk[KC div 2,0] xor S[tk[KC div 2 - 1,0]];
tk[KC div 2,1]:= tk[KC div 2,1] xor S[tk[KC div 2 - 1,1]];
tk[KC div 2,2]:= tk[KC div 2,2] xor S[tk[KC div 2 - 1,2]];
tk[KC div 2,3]:= tk[KC div 2,3] xor S[tk[KC div 2 - 1,3]];
for j:= ((KC div 2) + 1) to (KC-1) do
PDWord(@tk[j])^:= PDWord(@tk[j])^ xor PDWord(@tk[j-1])^;
end;
j:= 0;
while (j< KC) and (r< (rounds+1)) do
begin
while (j< KC) and (t< BC) do
begin
rk[r,t]:= PDWord(@tk[j])^;
Inc(j);
Inc(t);
end;
if t= BC then
begin
Inc(r);
t:= 0;
end;
end;
end;
Move(rk,drk,Sizeof(rk));
for r:= 1 to (numrounds-1) do
InvMixColumn(@drk[r],BC);
end;
procedure TDCP_rijndael.Burn;
begin
numrounds:= 0;
FillChar(rk,Sizeof(rk),0);
FillChar(drk,Sizeof(drk),0);
inherited Burn;
end;
procedure TDCP_rijndael.EncryptECB(const InData; var OutData);
var
r: longword;
tempb: array[0..MAXBC-1,0..3] of byte;
a: array[0..MAXBC,0..3] of byte;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@a[0,0])^:= PDword(@InData)^;
PDword(@a[1,0])^:= PDword(pointer(@InData)+4)^;
PDword(@a[2,0])^:= PDword(pointer(@InData)+8)^;
PDword(@a[3,0])^:= PDword(pointer(@InData)+12)^;
for r:= 0 to (numrounds-2) do
begin
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor rk[r,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor rk[r,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor rk[r,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor rk[r,3];
PDWord(@a[0])^:= PDWord(@T1[tempb[0,0]])^ xor
PDWord(@T2[tempb[1,1]])^ xor
PDWord(@T3[tempb[2,2]])^ xor
PDWord(@T4[tempb[3,3]])^;
PDWord(@a[1])^:= PDWord(@T1[tempb[1,0]])^ xor
PDWord(@T2[tempb[2,1]])^ xor
PDWord(@T3[tempb[3,2]])^ xor
PDWord(@T4[tempb[0,3]])^;
PDWord(@a[2])^:= PDWord(@T1[tempb[2,0]])^ xor
PDWord(@T2[tempb[3,1]])^ xor
PDWord(@T3[tempb[0,2]])^ xor
PDWord(@T4[tempb[1,3]])^;
PDWord(@a[3])^:= PDWord(@T1[tempb[3,0]])^ xor
PDWord(@T2[tempb[0,1]])^ xor
PDWord(@T3[tempb[1,2]])^ xor
PDWord(@T4[tempb[2,3]])^;
end;
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor rk[numrounds-1,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor rk[numrounds-1,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor rk[numrounds-1,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor rk[numrounds-1,3];
a[0,0]:= T1[tempb[0,0],1];
a[0,1]:= T1[tempb[1,1],1];
a[0,2]:= T1[tempb[2,2],1];
a[0,3]:= T1[tempb[3,3],1];
a[1,0]:= T1[tempb[1,0],1];
a[1,1]:= T1[tempb[2,1],1];
a[1,2]:= T1[tempb[3,2],1];
a[1,3]:= T1[tempb[0,3],1];
a[2,0]:= T1[tempb[2,0],1];
a[2,1]:= T1[tempb[3,1],1];
a[2,2]:= T1[tempb[0,2],1];
a[2,3]:= T1[tempb[1,3],1];
a[3,0]:= T1[tempb[3,0],1];
a[3,1]:= T1[tempb[0,1],1];
a[3,2]:= T1[tempb[1,2],1];
a[3,3]:= T1[tempb[2,3],1];
PDWord(@a[0])^:= PDWord(@a[0])^ xor rk[numrounds,0];
PDWord(@a[1])^:= PDWord(@a[1])^ xor rk[numrounds,1];
PDWord(@a[2])^:= PDWord(@a[2])^ xor rk[numrounds,2];
PDWord(@a[3])^:= PDWord(@a[3])^ xor rk[numrounds,3];
PDword(@OutData)^:= PDword(@a[0,0])^;
PDword(pointer(@OutData)+4)^:= PDword(@a[1,0])^;
PDword(pointer(@OutData)+8)^:= PDword(@a[2,0])^;
PDword(pointer(@OutData)+12)^:= PDword(@a[3,0])^;
end;
procedure TDCP_rijndael.DecryptECB(const InData; var OutData);
var
r: longword;
tempb: array[0..MAXBC-1,0..3] of byte;
a: array[0..MAXBC,0..3] of byte;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@a[0,0])^:= PDword(@InData)^;
PDword(@a[1,0])^:= PDword(pointer(@InData)+4)^;
PDword(@a[2,0])^:= PDword(pointer(@InData)+8)^;
PDword(@a[3,0])^:= PDword(pointer(@InData)+12)^;
for r:= NumRounds downto 2 do
begin
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor drk[r,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor drk[r,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor drk[r,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor drk[r,3];
PDWord(@a[0])^:= PDWord(@T5[tempb[0,0]])^ xor
PDWord(@T6[tempb[3,1]])^ xor
PDWord(@T7[tempb[2,2]])^ xor
PDWord(@T8[tempb[1,3]])^;
PDWord(@a[1])^:= PDWord(@T5[tempb[1,0]])^ xor
PDWord(@T6[tempb[0,1]])^ xor
PDWord(@T7[tempb[3,2]])^ xor
PDWord(@T8[tempb[2,3]])^;
PDWord(@a[2])^:= PDWord(@T5[tempb[2,0]])^ xor
PDWord(@T6[tempb[1,1]])^ xor
PDWord(@T7[tempb[0,2]])^ xor
PDWord(@T8[tempb[3,3]])^;
PDWord(@a[3])^:= PDWord(@T5[tempb[3,0]])^ xor
PDWord(@T6[tempb[2,1]])^ xor
PDWord(@T7[tempb[1,2]])^ xor
PDWord(@T8[tempb[0,3]])^;
end;
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor drk[1,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor drk[1,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor drk[1,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor drk[1,3];
a[0,0]:= S5[tempb[0,0]];
a[0,1]:= S5[tempb[3,1]];
a[0,2]:= S5[tempb[2,2]];
a[0,3]:= S5[tempb[1,3]];
a[1,0]:= S5[tempb[1,0]];
a[1,1]:= S5[tempb[0,1]];
a[1,2]:= S5[tempb[3,2]];
a[1,3]:= S5[tempb[2,3]];
a[2,0]:= S5[tempb[2,0]];
a[2,1]:= S5[tempb[1,1]];
a[2,2]:= S5[tempb[0,2]];
a[2,3]:= S5[tempb[3,3]];
a[3,0]:= S5[tempb[3,0]];
a[3,1]:= S5[tempb[2,1]];
a[3,2]:= S5[tempb[1,2]];
a[3,3]:= S5[tempb[0,3]];
PDWord(@a[0])^:= PDWord(@a[0])^ xor drk[0,0];
PDWord(@a[1])^:= PDWord(@a[1])^ xor drk[0,1];
PDWord(@a[2])^:= PDWord(@a[2])^ xor drk[0,2];
PDWord(@a[3])^:= PDWord(@a[3])^ xor drk[0,3];
PDword(@OutData)^:= PDword(@a[0,0])^;
PDword(pointer(@OutData)+4)^:= PDword(@a[1,0])^;
PDword(pointer(@OutData)+8)^:= PDword(@a[2,0])^;
PDword(pointer(@OutData)+12)^:= PDword(@a[3,0])^;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Rijndael *****************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPrijndael;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
const
BC= 4;
MAXROUNDS= 14;
type
TDCP_rijndael= class(TDCP_blockcipher128)
protected
numrounds: longword;
rk, drk: array[0..MAXROUNDS,0..7] of DWord;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPrijndael.inc}
class function TDCP_rijndael.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_rijndael.GetID: integer;
begin
Result:= DCP_rijndael;
end;
class function TDCP_rijndael.GetAlgorithm: string;
begin
Result:= 'Rijndael';
end;
class function TDCP_rijndael.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($00,$01,$02,$03,$05,$06,$07,$08,$0A,$0B,$0C,$0D,$0F,$10,$11,$12);
InData1: array[0..15] of byte=
($50,$68,$12,$A4,$5F,$08,$C8,$89,$B9,$7F,$59,$80,$03,$8B,$83,$59);
OutData1: array[0..15] of byte=
($D8,$F5,$32,$53,$82,$89,$EF,$7D,$06,$B5,$06,$A4,$FD,$5B,$E9,$C9);
Key2: array[0..23] of byte=
($A0,$A1,$A2,$A3,$A5,$A6,$A7,$A8,$AA,$AB,$AC,$AD,$AF,$B0,$B1,$B2,
$B4,$B5,$B6,$B7,$B9,$BA,$BB,$BC);
InData2: array[0..15] of byte=
($4F,$1C,$76,$9D,$1E,$5B,$05,$52,$C7,$EC,$A8,$4D,$EA,$26,$A5,$49);
OutData2: array[0..15] of byte=
($F3,$84,$72,$10,$D5,$39,$1E,$23,$60,$60,$8E,$5A,$CB,$56,$05,$81);
Key3: array[0..31] of byte=
($00,$01,$02,$03,$05,$06,$07,$08,$0A,$0B,$0C,$0D,$0F,$10,$11,$12,
$14,$15,$16,$17,$19,$1A,$1B,$1C,$1E,$1F,$20,$21,$23,$24,$25,$26);
InData3: array[0..15] of byte=
($5E,$25,$CA,$78,$F0,$DE,$55,$80,$25,$24,$D3,$8D,$A3,$FE,$44,$56);
OutData3: array[0..15] of byte=
($E8,$B7,$2B,$4E,$8B,$E2,$43,$43,$8C,$9F,$FF,$1F,$0E,$20,$58,$72);
var
Block: array[0..15] of byte;
Cipher: TDCP_rijndael;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_rijndael.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,16));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,16));
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InData3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData3,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData3,16));
Cipher.Free;
end;
procedure InvMixColumn(a: PByteArray; BC: byte);
var
j: longword;
begin
for j:= 0 to (BC-1) do
PDWord(@(a^[j*4]))^:= PDWord(@U1[a^[j*4+0]])^ xor
PDWord(@U2[a^[j*4+1]])^ xor
PDWord(@U3[a^[j*4+2]])^ xor
PDWord(@U4[a^[j*4+3]])^;
end;
procedure TDCP_rijndael.InitKey(const Key; Size: longword);
var
KC, ROUNDS, j, r, t, rconpointer: longword;
tk: array[0..MAXKC-1,0..3] of byte;
begin
Size:= Size div 8;
dcpFillChar(tk,Sizeof(tk),0);
Move(Key,tk,Size);
if Size<= 16 then
begin
KC:= 4;
Rounds:= 10;
end
else if Size<= 24 then
begin
KC:= 6;
Rounds:= 12;
end
else
begin
KC:= 8;
Rounds:= 14;
end;
numrounds:= rounds;
r:= 0;
t:= 0;
j:= 0;
while (j< KC) and (r< (rounds+1)) do
begin
while (j< KC) and (t< BC) do
begin
rk[r,t]:= PDWord(@tk[j])^;
Inc(j);
Inc(t);
end;
if t= BC then
begin
t:= 0;
Inc(r);
end;
end;
rconpointer:= 0;
while (r< (rounds+1)) do
begin
tk[0,0]:= tk[0,0] xor S[tk[KC-1,1]];
tk[0,1]:= tk[0,1] xor S[tk[KC-1,2]];
tk[0,2]:= tk[0,2] xor S[tk[KC-1,3]];
tk[0,3]:= tk[0,3] xor S[tk[KC-1,0]];
tk[0,0]:= tk[0,0] xor rcon[rconpointer];
Inc(rconpointer);
if KC<> 8 then
begin
for j:= 1 to (KC-1) do
PDWord(@tk[j])^:= PDWord(@tk[j])^ xor PDWord(@tk[j-1])^;
end
else
begin
for j:= 1 to ((KC div 2)-1) do
PDWord(@tk[j])^:= PDWord(@tk[j])^ xor PDWord(@tk[j-1])^;
tk[KC div 2,0]:= tk[KC div 2,0] xor S[tk[KC div 2 - 1,0]];
tk[KC div 2,1]:= tk[KC div 2,1] xor S[tk[KC div 2 - 1,1]];
tk[KC div 2,2]:= tk[KC div 2,2] xor S[tk[KC div 2 - 1,2]];
tk[KC div 2,3]:= tk[KC div 2,3] xor S[tk[KC div 2 - 1,3]];
for j:= ((KC div 2) + 1) to (KC-1) do
PDWord(@tk[j])^:= PDWord(@tk[j])^ xor PDWord(@tk[j-1])^;
end;
j:= 0;
while (j< KC) and (r< (rounds+1)) do
begin
while (j< KC) and (t< BC) do
begin
rk[r,t]:= PDWord(@tk[j])^;
Inc(j);
Inc(t);
end;
if t= BC then
begin
Inc(r);
t:= 0;
end;
end;
end;
Move(rk,drk,Sizeof(rk));
for r:= 1 to (numrounds-1) do
InvMixColumn(@drk[r],BC);
end;
procedure TDCP_rijndael.Burn;
begin
numrounds:= 0;
FillChar(rk,Sizeof(rk),0);
FillChar(drk,Sizeof(drk),0);
inherited Burn;
end;
procedure TDCP_rijndael.EncryptECB(const InData; var OutData);
var
r: longword;
tempb: array[0..MAXBC-1,0..3] of byte;
a: array[0..MAXBC,0..3] of byte;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@a[0,0])^:= PDword(@InData)^;
PDword(@a[1,0])^:= PDword(pointer(@InData)+4)^;
PDword(@a[2,0])^:= PDword(pointer(@InData)+8)^;
PDword(@a[3,0])^:= PDword(pointer(@InData)+12)^;
for r:= 0 to (numrounds-2) do
begin
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor rk[r,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor rk[r,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor rk[r,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor rk[r,3];
PDWord(@a[0])^:= PDWord(@T1[tempb[0,0]])^ xor
PDWord(@T2[tempb[1,1]])^ xor
PDWord(@T3[tempb[2,2]])^ xor
PDWord(@T4[tempb[3,3]])^;
PDWord(@a[1])^:= PDWord(@T1[tempb[1,0]])^ xor
PDWord(@T2[tempb[2,1]])^ xor
PDWord(@T3[tempb[3,2]])^ xor
PDWord(@T4[tempb[0,3]])^;
PDWord(@a[2])^:= PDWord(@T1[tempb[2,0]])^ xor
PDWord(@T2[tempb[3,1]])^ xor
PDWord(@T3[tempb[0,2]])^ xor
PDWord(@T4[tempb[1,3]])^;
PDWord(@a[3])^:= PDWord(@T1[tempb[3,0]])^ xor
PDWord(@T2[tempb[0,1]])^ xor
PDWord(@T3[tempb[1,2]])^ xor
PDWord(@T4[tempb[2,3]])^;
end;
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor rk[numrounds-1,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor rk[numrounds-1,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor rk[numrounds-1,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor rk[numrounds-1,3];
a[0,0]:= T1[tempb[0,0],1];
a[0,1]:= T1[tempb[1,1],1];
a[0,2]:= T1[tempb[2,2],1];
a[0,3]:= T1[tempb[3,3],1];
a[1,0]:= T1[tempb[1,0],1];
a[1,1]:= T1[tempb[2,1],1];
a[1,2]:= T1[tempb[3,2],1];
a[1,3]:= T1[tempb[0,3],1];
a[2,0]:= T1[tempb[2,0],1];
a[2,1]:= T1[tempb[3,1],1];
a[2,2]:= T1[tempb[0,2],1];
a[2,3]:= T1[tempb[1,3],1];
a[3,0]:= T1[tempb[3,0],1];
a[3,1]:= T1[tempb[0,1],1];
a[3,2]:= T1[tempb[1,2],1];
a[3,3]:= T1[tempb[2,3],1];
PDWord(@a[0])^:= PDWord(@a[0])^ xor rk[numrounds,0];
PDWord(@a[1])^:= PDWord(@a[1])^ xor rk[numrounds,1];
PDWord(@a[2])^:= PDWord(@a[2])^ xor rk[numrounds,2];
PDWord(@a[3])^:= PDWord(@a[3])^ xor rk[numrounds,3];
PDword(@OutData)^:= PDword(@a[0,0])^;
PDword(pointer(@OutData)+4)^:= PDword(@a[1,0])^;
PDword(pointer(@OutData)+8)^:= PDword(@a[2,0])^;
PDword(pointer(@OutData)+12)^:= PDword(@a[3,0])^;
end;
procedure TDCP_rijndael.DecryptECB(const InData; var OutData);
var
r: longword;
tempb: array[0..MAXBC-1,0..3] of byte;
a: array[0..MAXBC,0..3] of byte;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
PDword(@a[0,0])^:= PDword(@InData)^;
PDword(@a[1,0])^:= PDword(pointer(@InData)+4)^;
PDword(@a[2,0])^:= PDword(pointer(@InData)+8)^;
PDword(@a[3,0])^:= PDword(pointer(@InData)+12)^;
for r:= NumRounds downto 2 do
begin
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor drk[r,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor drk[r,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor drk[r,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor drk[r,3];
PDWord(@a[0])^:= PDWord(@T5[tempb[0,0]])^ xor
PDWord(@T6[tempb[3,1]])^ xor
PDWord(@T7[tempb[2,2]])^ xor
PDWord(@T8[tempb[1,3]])^;
PDWord(@a[1])^:= PDWord(@T5[tempb[1,0]])^ xor
PDWord(@T6[tempb[0,1]])^ xor
PDWord(@T7[tempb[3,2]])^ xor
PDWord(@T8[tempb[2,3]])^;
PDWord(@a[2])^:= PDWord(@T5[tempb[2,0]])^ xor
PDWord(@T6[tempb[1,1]])^ xor
PDWord(@T7[tempb[0,2]])^ xor
PDWord(@T8[tempb[3,3]])^;
PDWord(@a[3])^:= PDWord(@T5[tempb[3,0]])^ xor
PDWord(@T6[tempb[2,1]])^ xor
PDWord(@T7[tempb[1,2]])^ xor
PDWord(@T8[tempb[0,3]])^;
end;
PDWord(@tempb[0])^:= PDWord(@a[0])^ xor drk[1,0];
PDWord(@tempb[1])^:= PDWord(@a[1])^ xor drk[1,1];
PDWord(@tempb[2])^:= PDWord(@a[2])^ xor drk[1,2];
PDWord(@tempb[3])^:= PDWord(@a[3])^ xor drk[1,3];
a[0,0]:= S5[tempb[0,0]];
a[0,1]:= S5[tempb[3,1]];
a[0,2]:= S5[tempb[2,2]];
a[0,3]:= S5[tempb[1,3]];
a[1,0]:= S5[tempb[1,0]];
a[1,1]:= S5[tempb[0,1]];
a[1,2]:= S5[tempb[3,2]];
a[1,3]:= S5[tempb[2,3]];
a[2,0]:= S5[tempb[2,0]];
a[2,1]:= S5[tempb[1,1]];
a[2,2]:= S5[tempb[0,2]];
a[2,3]:= S5[tempb[3,3]];
a[3,0]:= S5[tempb[3,0]];
a[3,1]:= S5[tempb[2,1]];
a[3,2]:= S5[tempb[1,2]];
a[3,3]:= S5[tempb[0,3]];
PDWord(@a[0])^:= PDWord(@a[0])^ xor drk[0,0];
PDWord(@a[1])^:= PDWord(@a[1])^ xor drk[0,1];
PDWord(@a[2])^:= PDWord(@a[2])^ xor drk[0,2];
PDWord(@a[3])^:= PDWord(@a[3])^ xor drk[0,3];
PDword(@OutData)^:= PDword(@a[0,0])^;
PDword(pointer(@OutData)+4)^:= PDword(@a[1,0])^;
PDword(pointer(@OutData)+8)^:= PDword(@a[2,0])^;
PDword(pointer(@OutData)+12)^:= PDword(@a[3,0])^;
end;
end.

View File

@ -1,282 +1,282 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Serpent ******************************}
{* Based on C source written by Brian Gladman (gladman@seven77.demon.co.uk) ***}
{* Thanks to Bruce Christensen for the initial Delphi translation *************}
{******************************************************************************}
{* Copyright (c) 2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPserpent;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_serpent= class(TDCP_blockcipher128)
protected
l_key: array[0..131] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
class function TDCP_serpent.GetID: integer;
begin
Result:= DCP_serpent;
end;
class function TDCP_serpent.GetAlgorithm: string;
begin
Result:= 'Serpent';
end;
class function TDCP_serpent.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_serpent.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($ff,$ee,$dd,$cc,$bb,$aa,$99,$88,$77,$66,$55,$44,$33,$22,$11,$00);
InData1: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData1: array[0..15] of byte=
($d5,$ba,$a0,$0a,$4b,$b9,$d8,$a7,$c9,$81,$c8,$dc,$90,$d8,$9d,$92);
Key2: array[0..23] of byte=
($88,$99,$aa,$bb,$cc,$dd,$ee,$ff,$ff,$ee,$dd,$cc,$bb,$aa,$99,$88,
$77,$66,$55,$44,$33,$22,$11,$00);
InData2: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData2: array[0..15] of byte=
($da,$86,$08,$42,$b7,$20,$80,$2b,$f4,$04,$a4,$c7,$10,$34,$87,$9a);
Key3: array[0..31] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff,
$ff,$ee,$dd,$cc,$bb,$aa,$99,$88,$77,$66,$55,$44,$33,$22,$11,$00);
InData3: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData3: array[0..15] of byte=
($93,$df,$9a,$3c,$af,$e3,$87,$bd,$99,$9e,$eb,$e3,$93,$a1,$7f,$ca);
var
Block: array[0..15] of byte;
Cipher: TDCP_serpent;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_serpent.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,16));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,16));
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InData3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData3,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData3,16));
Cipher.Free;
end;
procedure TDCP_serpent.InitKey(const Key; Size: longword);
var
kp: array[0..139] of dword;
i, n: integer;
t, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17: dword;
a, b, c, d: dword;
begin
dcpFillChar(kp, SizeOf(kp), 0);
Move(Key,kp,Size div 8);
if Size < 256 then
begin
i:= Size div 32;
t:= 1 shl (Size mod 32);
kp[i]:= (kp[i] and (t - 1)) or t;
end;
for i:= 8 to 139 do
begin
t:= kp[i - 8] xor kp[i - 5] xor kp[i - 3] xor kp[i - 1] xor $9e3779b9 xor longword(i-8);
kp[i]:= (t shl 11) or (t shr 21);
end;
for i:= 0 to 3 do
begin
n:= i*32;
a:= kp[n + 4*0 + 8]; b:= kp[n + 4*0 + 9]; c:= kp[n + 4*0 + 10]; d:= kp[n + 4*0 + 11];
t1:= a xor c; t2:= a or d; t3:= a and b; t4:= a and d; t5:= b or t4; t6:= t1 and t2; kp[ 9+n]:= t5 xor t6; t8:= b xor d; t9:= c or t3; t10:= t6 xor t8; kp[ 11+n]:= t9 xor t10; t12:= c xor t3; t13:= t2 and kp[ 11+n]; kp[ 10+n]:= t12 xor t13; t15:= not kp[ 10+n]; t16:= t2 xor t3; t17:= kp[ 9+n] and t15; kp[ 8+n]:= t16 xor t17;
a:= kp[n + 4*1 + 8]; b:= kp[n + 4*1 + 9]; c:= kp[n + 4*1 + 10]; d:= kp[n + 4*1 + 11];
t1:= not a; t2:= b xor d; t3:= c and t1; kp[ 12+n]:= t2 xor t3; t5:= c xor t1; t6:= c xor kp[ 12+n]; t7:= b and t6; kp[ 15+n]:= t5 xor t7; t9:= d or t7; t10:= kp[ 12+n] or t5; t11:= t9 and t10; kp[ 14+n]:= a xor t11; t13:= d or t1; t14:= t2 xor kp[ 15+n]; t15:= kp[ 14+n] xor t13; kp[ 13+n]:= t14 xor t15;
a:= kp[n + 4*2 + 8]; b:= kp[n + 4*2 + 9]; c:= kp[n + 4*2 + 10]; d:= kp[n + 4*2 + 11];
t1:= a xor d; t2:= b xor d; t3:= a and b; t4:= not c; t5:= t2 xor t3; kp[ 18+n]:= t4 xor t5; t7:= a xor t2; t8:= b or t4; t9:= d or kp[ 18+n]; t10:= t7 and t9; kp[ 17+n]:= t8 xor t10; t12:= c xor d; t13:= t1 or t2; t14:= kp[ 17+n] xor t12; kp[ 19+n]:= t13 xor t14; t16:= t1 or kp[ 18+n]; t17:= t8 xor t14; kp[ 16+n]:= t16 xor t17;
a:= kp[n + 4*3 + 8]; b:= kp[n + 4*3 + 9]; c:= kp[n + 4*3 + 10]; d:= kp[n + 4*3 + 11];
t1:= b xor d; t2:= not t1; t3:= a or d; t4:= b xor c; kp[ 23+n]:= t3 xor t4; t6:= a xor b; t7:= a or t4; t8:= c and t6; t9:= t2 or t8; kp[ 20+n]:= t7 xor t9; t11:= a xor kp[ 23+n]; t12:= t1 and t6; t13:= kp[ 20+n] xor t11; kp[ 21+n]:= t12 xor t13; t15:= kp[ 20+n] or kp[ 21+n]; t16:= t3 and t15; kp[ 22+n]:= b xor t16;
a:= kp[n + 4*4 + 8]; b:= kp[n + 4*4 + 9]; c:= kp[n + 4*4 + 10]; d:= kp[n + 4*4 + 11];
t1:= not c; t2:= b xor c; t3:= b or t1; t4:= d xor t3; t5:= a and t4; kp[ 27+n]:= t2 xor t5; t7:= a xor d; t8:= b xor t5; t9:= t2 or t8; kp[ 25+n]:= t7 xor t9; t11:= d and t3; t12:= t5 xor kp[ 25+n]; t13:= kp[ 27+n] and t12; kp[ 26+n]:= t11 xor t13; t15:= t1 or t4; t16:= t12 xor kp[ 26+n]; kp[ 24+n]:= t15 xor t16;
a:= kp[n + 4*5 + 8]; b:= kp[n + 4*5 + 9]; c:= kp[n + 4*5 + 10]; d:= kp[n + 4*5 + 11];
t1:= a xor c; t2:= b or d; t3:= b xor c; t4:= not t3; t5:= a and d; kp[ 29+n]:= t4 xor t5; t7:= b or c; t8:= d xor t1; t9:= t7 and t8; kp[ 31+n]:= t2 xor t9; t11:= t1 and t7; t12:= t4 xor t8; t13:= kp[ 31+n] and t11; kp[ 28+n]:= t12 xor t13; t15:= t3 xor t11; t16:= kp[ 31+n] or t15; kp[ 30+n]:= t12 xor t16;
a:= kp[n + 4*6 + 8]; b:= kp[n + 4*6 + 9]; c:= kp[n + 4*6 + 10]; d:= kp[n + 4*6 + 11];
t1:= not a; t2:= a xor b; t3:= a xor d; t4:= c xor t1; t5:= t2 or t3; kp[ 32+n]:= t4 xor t5; t7:= not d; t8:= kp[ 32+n] and t7; kp[ 33+n]:= t2 xor t8; t10:= b or kp[ 33+n]; t11:= c or kp[ 32+n]; t12:= t7 xor t10; kp[ 35+n]:= t11 xor t12; t14:= d or kp[ 33+n]; t15:= t1 xor t14; t16:= kp[ 32+n] or kp[ 35+n]; kp[ 34+n]:= t15 xor t16;
a:= kp[n + 4*7 + 8]; b:= kp[n + 4*7 + 9]; c:= kp[n + 4*7 + 10]; d:= kp[n + 4*7 + 11];
t1:= not a; t2:= a xor d; t3:= a xor b; t4:= c xor t1; t5:= t2 or t3; kp[ 36+n]:= t4 xor t5; t7:= not kp[ 36+n]; t8:= b or t7; kp[ 39+n]:= t2 xor t8; t10:= a and kp[ 36+n]; t11:= b xor kp[ 39+n]; t12:= t8 and t11; kp[ 38+n]:= t10 xor t12; t14:= a or t7; t15:= t3 xor t14; t16:= kp[ 39+n] and kp[ 38+n]; kp[ 37+n]:= t15 xor t16;
end;
a:= kp[136]; b:= kp[137]; c:= kp[138]; d:= kp[139];
t1:= a xor c; t2:= a or d; t3:= a and b; t4:= a and d; t5:= b or t4; t6:= t1 and t2; kp[137]:= t5 xor t6; t8:= b xor d; t9:= c or t3; t10:= t6 xor t8; kp[139]:= t9 xor t10; t12:= c xor t3; t13:= t2 and kp[139]; kp[138]:= t12 xor t13; t15:= not kp[138]; t16:= t2 xor t3; t17:= kp[137] and t15; kp[136]:= t16 xor t17;
Move(kp[8],l_key,Sizeof(l_key));
FillChar(kp,Sizeof(kp),0);
end;
procedure TDCP_serpent.Burn;
begin
FillChar(l_key,Sizeof(l_key),0);
inherited Burn;
end;
procedure TDCP_serpent.EncryptECB(const InData; var OutData);
var
i: integer;
a, b, c, d, e, f, g, h: dword;
t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
a:= PDWord(@InData)^;
b:= PDWord(pointer(@InData)+4)^;
c:= PDWord(pointer(@InData)+8)^;
d:= PDWord(pointer(@InData)+12)^;
i:= 0;
while i < 32 do
begin
a:= a xor l_key[4*(i)]; b:= b xor l_key[4*(i)+1]; c:= c xor l_key[4*(i)+2]; d:= d xor l_key[4*(i)+3];
t1:= b xor d; t2:= not t1; t3:= a or d; t4:= b xor c; h:= t3 xor t4; t6:= a xor b; t7:= a or t4; t8:= c and t6; t9:= t2 or t8; e:= t7 xor t9; t11:= a xor h; t12:= t1 and t6; t13:= e xor t11; f:= t12 xor t13; t15:= e or f; t16:= t3 and t15; g:= b xor t16;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+1)]; f:= f xor l_key[4*(i+1)+1]; g:= g xor l_key[4*(i+1)+2]; h:= h xor l_key[4*(i+1)+3];
t1:= e xor h; t2:= f xor h; t3:= e and f; t4:= not g; t5:= t2 xor t3; c:= t4 xor t5; t7:= e xor t2; t8:= f or t4; t9:= h or c; t10:= t7 and t9; b:= t8 xor t10; t12:= g xor h; t13:= t1 or t2; t14:= b xor t12; d:= t13 xor t14; t16:= t1 or c; t17:= t8 xor t14; a:= t16 xor t17;
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
a:= a xor l_key[4*(i+2)]; b:= b xor l_key[4*(i+2)+1]; c:= c xor l_key[4*(i+2)+2]; d:= d xor l_key[4*(i+2)+3];
t1:= not a; t2:= b xor d; t3:= c and t1; e:= t2 xor t3; t5:= c xor t1; t6:= c xor e; t7:= b and t6; h:= t5 xor t7; t9:= d or t7; t10:= e or t5; t11:= t9 and t10; g:= a xor t11; t13:= d or t1; t14:= t2 xor h; t15:= g xor t13; f:= t14 xor t15;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+3)]; f:= f xor l_key[4*(i+3)+1]; g:= g xor l_key[4*(i+3)+2]; h:= h xor l_key[4*(i+3)+3];
t1:= e xor g; t2:= e or h; t3:= e and f; t4:= e and h; t5:= f or t4; t6:= t1 and t2; b:= t5 xor t6; t8:= f xor h; t9:= g or t3; t10:= t6 xor t8; d:= t9 xor t10; t12:= g xor t3; t13:= t2 and d; c:= t12 xor t13; t15:= not c; t16:= t2 xor t3; t17:= b and t15; a:= t16 xor t17;
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
a:= a xor l_key[4*(i+4)]; b:= b xor l_key[4*(i+4)+1]; c:= c xor l_key[4*(i+4)+2]; d:= d xor l_key[4*(i+4)+3];
t1:= not a; t2:= a xor d; t3:= a xor b; t4:= c xor t1; t5:= t2 or t3; e:= t4 xor t5; t7:= not e; t8:= b or t7; h:= t2 xor t8; t10:= a and e; t11:= b xor h; t12:= t8 and t11; g:= t10 xor t12; t14:= a or t7; t15:= t3 xor t14; t16:= h and g; f:= t15 xor t16;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+5)]; f:= f xor l_key[4*(i+5)+1]; g:= g xor l_key[4*(i+5)+2]; h:= h xor l_key[4*(i+5)+3];
t1:= not e; t2:= e xor f; t3:= e xor h; t4:= g xor t1; t5:= t2 or t3; a:= t4 xor t5; t7:= not h; t8:= a and t7; b:= t2 xor t8; t10:= f or b; t11:= g or a; t12:= t7 xor t10; d:= t11 xor t12; t14:= h or b; t15:= t1 xor t14; t16:= a or d; c:= t15 xor t16;
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
a:= a xor l_key[4*(i+6)]; b:= b xor l_key[4*(i+6)+1]; c:= c xor l_key[4*(i+6)+2]; d:= d xor l_key[4*(i+6)+3];
t1:= a xor c; t2:= b or d; t3:= b xor c; t4:= not t3; t5:= a and d; f:= t4 xor t5; t7:= b or c; t8:= d xor t1; t9:= t7 and t8; h:= t2 xor t9; t11:= t1 and t7; t12:= t4 xor t8; t13:= h and t11; e:= t12 xor t13; t15:= t3 xor t11; t16:= h or t15; g:= t12 xor t16;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+7)]; f:= f xor l_key[4*(i+7)+1]; g:= g xor l_key[4*(i+7)+2]; h:= h xor l_key[4*(i+7)+3];
t1:= not g; t2:= f xor g; t3:= f or t1; t4:= h xor t3; t5:= e and t4; d:= t2 xor t5; t7:= e xor h; t8:= f xor t5; t9:= t2 or t8; b:= t7 xor t9; t11:= h and t3; t12:= t5 xor b; t13:= d and t12; c:= t11 xor t13; t15:= t1 or t4; t16:= t12 xor c; a:= t15 xor t16;
Inc(i,8);
if i < 32 then
begin
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
end;
end;
a:= a xor l_key[128]; b:= b xor l_key[128+1]; c:= c xor l_key[128+2]; d:= d xor l_key[128+3];
PDWord(pointer(@OutData)+ 0)^:= a;
PDWord(pointer(@OutData)+ 4)^:= b;
PDWord(pointer(@OutData)+ 8)^:= c;
PDWord(pointer(@OutData)+12)^:= d;
end;
procedure TDCP_serpent.DecryptECB(const InData; var OutData);
var
i: integer;
a, b, c, d, e, f, g, h: dword;
t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
a:= PDWord(@InData)^;
b:= PDWord(pointer(@InData)+4)^;
c:= PDWord(pointer(@InData)+8)^;
d:= PDWord(pointer(@InData)+12)^;
i:= 32;
a:= a xor l_key[4*32]; b:= b xor l_key[4*32+1]; c:= c xor l_key[4*32+2]; d:= d xor l_key[4*32+3];
while i > 0 do
begin
if i < 32 then
begin
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
end;
t1:= a and b; t2:= a or b; t3:= c or t1; t4:= d and t2; h:= t3 xor t4; t6:= not d; t7:= b xor t4; t8:= h xor t6; t9:= t7 or t8; f:= a xor t9; t11:= c xor t7; t12:= d or f; e:= t11 xor t12; t14:= a and h; t15:= t3 xor f; t16:= e xor t14; g:= t15 xor t16;
e:= e xor l_key[4*(i-1)]; f:= f xor l_key[4*(i-1)+1]; g:= g xor l_key[4*(i-1)+2]; h:= h xor l_key[4*(i-1)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= not g; t2:= e xor g; t3:= f xor h; t4:= e or t1; b:= t3 xor t4; t6:= e or f; t7:= f and t2; t8:= b xor t6; t9:= t7 or t8; a:= g xor t9; t11:= not b; t12:= h or t2; t13:= t9 xor t11; d:= t12 xor t13; t15:= f xor t11; t16:= a and d; c:= t15 xor t16;
a:= a xor l_key[4*(i-2)]; b:= b xor l_key[4*(i-2)+1]; c:= c xor l_key[4*(i-2)+2]; d:= d xor l_key[4*(i-2)+3];
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
t1:= not c; t2:= b and t1; t3:= d xor t2; t4:= a and t3; t5:= b xor t1; h:= t4 xor t5; t7:= b or h; t8:= a and t7; f:= t3 xor t8; t10:= a or d; t11:= t1 xor t7; e:= t10 xor t11; t13:= a xor c; t14:= b and t10; t15:= t4 or t13; g:= t14 xor t15;
e:= e xor l_key[4*(i-3)]; f:= f xor l_key[4*(i-3)+1]; g:= g xor l_key[4*(i-3)+2]; h:= h xor l_key[4*(i-3)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= g xor h; t2:= g or h; t3:= f xor t2; t4:= e and t3; b:= t1 xor t4; t6:= e xor h; t7:= f or h; t8:= t6 and t7; d:= t3 xor t8; t10:= not e; t11:= g xor d; t12:= t10 or t11; a:= t3 xor t12; t14:= g or t4; t15:= t7 xor t14; t16:= d or t10; c:= t15 xor t16;
a:= a xor l_key[4*(i-4)]; b:= b xor l_key[4*(i-4)+1]; c:= c xor l_key[4*(i-4)+2]; d:= d xor l_key[4*(i-4)+3];
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
t1:= b xor c; t2:= b or c; t3:= a xor c; t4:= t2 xor t3; t5:= d or t4; e:= t1 xor t5; t7:= a xor d; t8:= t1 or t5; t9:= t2 xor t7; g:= t8 xor t9; t11:= a and t4; t12:= e or t9; f:= t11 xor t12; t14:= a and g; t15:= t2 xor t14; t16:= e and t15; h:= t4 xor t16;
e:= e xor l_key[4*(i-5)]; f:= f xor l_key[4*(i-5)+1]; g:= g xor l_key[4*(i-5)+2]; h:= h xor l_key[4*(i-5)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= f xor h; t2:= not t1; t3:= e xor g; t4:= g xor t1; t5:= f and t4; a:= t3 xor t5; t7:= e or t2; t8:= h xor t7; t9:= t3 or t8; d:= t1 xor t9; t11:= not t4; t12:= a or d; b:= t11 xor t12; t14:= h and t11; t15:= t3 xor t12; c:= t14 xor t15;
a:= a xor l_key[4*(i-6)]; b:= b xor l_key[4*(i-6)+1]; c:= c xor l_key[4*(i-6)+2]; d:= d xor l_key[4*(i-6)+3];
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
t1:= a xor d; t2:= a and b; t3:= b xor c; t4:= a xor t3; t5:= b or d; h:= t4 xor t5; t7:= c or t1; t8:= b xor t7; t9:= t4 and t8; f:= t1 xor t9; t11:= not t2; t12:= h and f; t13:= t9 xor t11; g:= t12 xor t13; t15:= a and d; t16:= c xor t13; e:= t15 xor t16;
e:= e xor l_key[4*(i-7)]; f:= f xor l_key[4*(i-7)+1]; g:= g xor l_key[4*(i-7)+2]; h:= h xor l_key[4*(i-7)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= e xor h; t2:= g xor h; t3:= not t2; t4:= e or f; c:= t3 xor t4; t6:= f xor t1; t7:= g or t6; t8:= e xor t7; t9:= t2 and t8; b:= t6 xor t9; t11:= not t8; t12:= f and h; t13:= b or t12; d:= t11 xor t13; t15:= t2 xor t12; t16:= b or d; a:= t15 xor t16;
a:= a xor l_key[4*(i-8)]; b:= b xor l_key[4*(i-8)+1]; c:= c xor l_key[4*(i-8)+2]; d:= d xor l_key[4*(i-8)+3];
Dec(i,8);
end;
PDWord(pointer(@OutData)+ 0)^:= a;
PDWord(pointer(@OutData)+ 4)^:= b;
PDWord(pointer(@OutData)+ 8)^:= c;
PDWord(pointer(@OutData)+12)^:= d;
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Serpent ******************************}
{* Based on C source written by Brian Gladman (gladman@seven77.demon.co.uk) ***}
{* Thanks to Bruce Christensen for the initial Delphi translation *************}
{******************************************************************************}
{* Copyright (c) 2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPserpent;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_serpent= class(TDCP_blockcipher128)
protected
l_key: array[0..131] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
class function TDCP_serpent.GetID: integer;
begin
Result:= DCP_serpent;
end;
class function TDCP_serpent.GetAlgorithm: string;
begin
Result:= 'Serpent';
end;
class function TDCP_serpent.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_serpent.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($ff,$ee,$dd,$cc,$bb,$aa,$99,$88,$77,$66,$55,$44,$33,$22,$11,$00);
InData1: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData1: array[0..15] of byte=
($d5,$ba,$a0,$0a,$4b,$b9,$d8,$a7,$c9,$81,$c8,$dc,$90,$d8,$9d,$92);
Key2: array[0..23] of byte=
($88,$99,$aa,$bb,$cc,$dd,$ee,$ff,$ff,$ee,$dd,$cc,$bb,$aa,$99,$88,
$77,$66,$55,$44,$33,$22,$11,$00);
InData2: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData2: array[0..15] of byte=
($da,$86,$08,$42,$b7,$20,$80,$2b,$f4,$04,$a4,$c7,$10,$34,$87,$9a);
Key3: array[0..31] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff,
$ff,$ee,$dd,$cc,$bb,$aa,$99,$88,$77,$66,$55,$44,$33,$22,$11,$00);
InData3: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData3: array[0..15] of byte=
($93,$df,$9a,$3c,$af,$e3,$87,$bd,$99,$9e,$eb,$e3,$93,$a1,$7f,$ca);
var
Block: array[0..15] of byte;
Cipher: TDCP_serpent;
begin
dcpFillChar(Block, SizeOf(Block), 0);
Cipher:= TDCP_serpent.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,16));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,16));
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InData3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData3,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData3,16));
Cipher.Free;
end;
procedure TDCP_serpent.InitKey(const Key; Size: longword);
var
kp: array[0..139] of dword;
i, n: integer;
t, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17: dword;
a, b, c, d: dword;
begin
dcpFillChar(kp, SizeOf(kp), 0);
Move(Key,kp,Size div 8);
if Size < 256 then
begin
i:= Size div 32;
t:= 1 shl (Size mod 32);
kp[i]:= (kp[i] and (t - 1)) or t;
end;
for i:= 8 to 139 do
begin
t:= kp[i - 8] xor kp[i - 5] xor kp[i - 3] xor kp[i - 1] xor $9e3779b9 xor longword(i-8);
kp[i]:= (t shl 11) or (t shr 21);
end;
for i:= 0 to 3 do
begin
n:= i*32;
a:= kp[n + 4*0 + 8]; b:= kp[n + 4*0 + 9]; c:= kp[n + 4*0 + 10]; d:= kp[n + 4*0 + 11];
t1:= a xor c; t2:= a or d; t3:= a and b; t4:= a and d; t5:= b or t4; t6:= t1 and t2; kp[ 9+n]:= t5 xor t6; t8:= b xor d; t9:= c or t3; t10:= t6 xor t8; kp[ 11+n]:= t9 xor t10; t12:= c xor t3; t13:= t2 and kp[ 11+n]; kp[ 10+n]:= t12 xor t13; t15:= not kp[ 10+n]; t16:= t2 xor t3; t17:= kp[ 9+n] and t15; kp[ 8+n]:= t16 xor t17;
a:= kp[n + 4*1 + 8]; b:= kp[n + 4*1 + 9]; c:= kp[n + 4*1 + 10]; d:= kp[n + 4*1 + 11];
t1:= not a; t2:= b xor d; t3:= c and t1; kp[ 12+n]:= t2 xor t3; t5:= c xor t1; t6:= c xor kp[ 12+n]; t7:= b and t6; kp[ 15+n]:= t5 xor t7; t9:= d or t7; t10:= kp[ 12+n] or t5; t11:= t9 and t10; kp[ 14+n]:= a xor t11; t13:= d or t1; t14:= t2 xor kp[ 15+n]; t15:= kp[ 14+n] xor t13; kp[ 13+n]:= t14 xor t15;
a:= kp[n + 4*2 + 8]; b:= kp[n + 4*2 + 9]; c:= kp[n + 4*2 + 10]; d:= kp[n + 4*2 + 11];
t1:= a xor d; t2:= b xor d; t3:= a and b; t4:= not c; t5:= t2 xor t3; kp[ 18+n]:= t4 xor t5; t7:= a xor t2; t8:= b or t4; t9:= d or kp[ 18+n]; t10:= t7 and t9; kp[ 17+n]:= t8 xor t10; t12:= c xor d; t13:= t1 or t2; t14:= kp[ 17+n] xor t12; kp[ 19+n]:= t13 xor t14; t16:= t1 or kp[ 18+n]; t17:= t8 xor t14; kp[ 16+n]:= t16 xor t17;
a:= kp[n + 4*3 + 8]; b:= kp[n + 4*3 + 9]; c:= kp[n + 4*3 + 10]; d:= kp[n + 4*3 + 11];
t1:= b xor d; t2:= not t1; t3:= a or d; t4:= b xor c; kp[ 23+n]:= t3 xor t4; t6:= a xor b; t7:= a or t4; t8:= c and t6; t9:= t2 or t8; kp[ 20+n]:= t7 xor t9; t11:= a xor kp[ 23+n]; t12:= t1 and t6; t13:= kp[ 20+n] xor t11; kp[ 21+n]:= t12 xor t13; t15:= kp[ 20+n] or kp[ 21+n]; t16:= t3 and t15; kp[ 22+n]:= b xor t16;
a:= kp[n + 4*4 + 8]; b:= kp[n + 4*4 + 9]; c:= kp[n + 4*4 + 10]; d:= kp[n + 4*4 + 11];
t1:= not c; t2:= b xor c; t3:= b or t1; t4:= d xor t3; t5:= a and t4; kp[ 27+n]:= t2 xor t5; t7:= a xor d; t8:= b xor t5; t9:= t2 or t8; kp[ 25+n]:= t7 xor t9; t11:= d and t3; t12:= t5 xor kp[ 25+n]; t13:= kp[ 27+n] and t12; kp[ 26+n]:= t11 xor t13; t15:= t1 or t4; t16:= t12 xor kp[ 26+n]; kp[ 24+n]:= t15 xor t16;
a:= kp[n + 4*5 + 8]; b:= kp[n + 4*5 + 9]; c:= kp[n + 4*5 + 10]; d:= kp[n + 4*5 + 11];
t1:= a xor c; t2:= b or d; t3:= b xor c; t4:= not t3; t5:= a and d; kp[ 29+n]:= t4 xor t5; t7:= b or c; t8:= d xor t1; t9:= t7 and t8; kp[ 31+n]:= t2 xor t9; t11:= t1 and t7; t12:= t4 xor t8; t13:= kp[ 31+n] and t11; kp[ 28+n]:= t12 xor t13; t15:= t3 xor t11; t16:= kp[ 31+n] or t15; kp[ 30+n]:= t12 xor t16;
a:= kp[n + 4*6 + 8]; b:= kp[n + 4*6 + 9]; c:= kp[n + 4*6 + 10]; d:= kp[n + 4*6 + 11];
t1:= not a; t2:= a xor b; t3:= a xor d; t4:= c xor t1; t5:= t2 or t3; kp[ 32+n]:= t4 xor t5; t7:= not d; t8:= kp[ 32+n] and t7; kp[ 33+n]:= t2 xor t8; t10:= b or kp[ 33+n]; t11:= c or kp[ 32+n]; t12:= t7 xor t10; kp[ 35+n]:= t11 xor t12; t14:= d or kp[ 33+n]; t15:= t1 xor t14; t16:= kp[ 32+n] or kp[ 35+n]; kp[ 34+n]:= t15 xor t16;
a:= kp[n + 4*7 + 8]; b:= kp[n + 4*7 + 9]; c:= kp[n + 4*7 + 10]; d:= kp[n + 4*7 + 11];
t1:= not a; t2:= a xor d; t3:= a xor b; t4:= c xor t1; t5:= t2 or t3; kp[ 36+n]:= t4 xor t5; t7:= not kp[ 36+n]; t8:= b or t7; kp[ 39+n]:= t2 xor t8; t10:= a and kp[ 36+n]; t11:= b xor kp[ 39+n]; t12:= t8 and t11; kp[ 38+n]:= t10 xor t12; t14:= a or t7; t15:= t3 xor t14; t16:= kp[ 39+n] and kp[ 38+n]; kp[ 37+n]:= t15 xor t16;
end;
a:= kp[136]; b:= kp[137]; c:= kp[138]; d:= kp[139];
t1:= a xor c; t2:= a or d; t3:= a and b; t4:= a and d; t5:= b or t4; t6:= t1 and t2; kp[137]:= t5 xor t6; t8:= b xor d; t9:= c or t3; t10:= t6 xor t8; kp[139]:= t9 xor t10; t12:= c xor t3; t13:= t2 and kp[139]; kp[138]:= t12 xor t13; t15:= not kp[138]; t16:= t2 xor t3; t17:= kp[137] and t15; kp[136]:= t16 xor t17;
Move(kp[8],l_key,Sizeof(l_key));
FillChar(kp,Sizeof(kp),0);
end;
procedure TDCP_serpent.Burn;
begin
FillChar(l_key,Sizeof(l_key),0);
inherited Burn;
end;
procedure TDCP_serpent.EncryptECB(const InData; var OutData);
var
i: integer;
a, b, c, d, e, f, g, h: dword;
t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
a:= PDWord(@InData)^;
b:= PDWord(pointer(@InData)+4)^;
c:= PDWord(pointer(@InData)+8)^;
d:= PDWord(pointer(@InData)+12)^;
i:= 0;
while i < 32 do
begin
a:= a xor l_key[4*(i)]; b:= b xor l_key[4*(i)+1]; c:= c xor l_key[4*(i)+2]; d:= d xor l_key[4*(i)+3];
t1:= b xor d; t2:= not t1; t3:= a or d; t4:= b xor c; h:= t3 xor t4; t6:= a xor b; t7:= a or t4; t8:= c and t6; t9:= t2 or t8; e:= t7 xor t9; t11:= a xor h; t12:= t1 and t6; t13:= e xor t11; f:= t12 xor t13; t15:= e or f; t16:= t3 and t15; g:= b xor t16;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+1)]; f:= f xor l_key[4*(i+1)+1]; g:= g xor l_key[4*(i+1)+2]; h:= h xor l_key[4*(i+1)+3];
t1:= e xor h; t2:= f xor h; t3:= e and f; t4:= not g; t5:= t2 xor t3; c:= t4 xor t5; t7:= e xor t2; t8:= f or t4; t9:= h or c; t10:= t7 and t9; b:= t8 xor t10; t12:= g xor h; t13:= t1 or t2; t14:= b xor t12; d:= t13 xor t14; t16:= t1 or c; t17:= t8 xor t14; a:= t16 xor t17;
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
a:= a xor l_key[4*(i+2)]; b:= b xor l_key[4*(i+2)+1]; c:= c xor l_key[4*(i+2)+2]; d:= d xor l_key[4*(i+2)+3];
t1:= not a; t2:= b xor d; t3:= c and t1; e:= t2 xor t3; t5:= c xor t1; t6:= c xor e; t7:= b and t6; h:= t5 xor t7; t9:= d or t7; t10:= e or t5; t11:= t9 and t10; g:= a xor t11; t13:= d or t1; t14:= t2 xor h; t15:= g xor t13; f:= t14 xor t15;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+3)]; f:= f xor l_key[4*(i+3)+1]; g:= g xor l_key[4*(i+3)+2]; h:= h xor l_key[4*(i+3)+3];
t1:= e xor g; t2:= e or h; t3:= e and f; t4:= e and h; t5:= f or t4; t6:= t1 and t2; b:= t5 xor t6; t8:= f xor h; t9:= g or t3; t10:= t6 xor t8; d:= t9 xor t10; t12:= g xor t3; t13:= t2 and d; c:= t12 xor t13; t15:= not c; t16:= t2 xor t3; t17:= b and t15; a:= t16 xor t17;
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
a:= a xor l_key[4*(i+4)]; b:= b xor l_key[4*(i+4)+1]; c:= c xor l_key[4*(i+4)+2]; d:= d xor l_key[4*(i+4)+3];
t1:= not a; t2:= a xor d; t3:= a xor b; t4:= c xor t1; t5:= t2 or t3; e:= t4 xor t5; t7:= not e; t8:= b or t7; h:= t2 xor t8; t10:= a and e; t11:= b xor h; t12:= t8 and t11; g:= t10 xor t12; t14:= a or t7; t15:= t3 xor t14; t16:= h and g; f:= t15 xor t16;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+5)]; f:= f xor l_key[4*(i+5)+1]; g:= g xor l_key[4*(i+5)+2]; h:= h xor l_key[4*(i+5)+3];
t1:= not e; t2:= e xor f; t3:= e xor h; t4:= g xor t1; t5:= t2 or t3; a:= t4 xor t5; t7:= not h; t8:= a and t7; b:= t2 xor t8; t10:= f or b; t11:= g or a; t12:= t7 xor t10; d:= t11 xor t12; t14:= h or b; t15:= t1 xor t14; t16:= a or d; c:= t15 xor t16;
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
a:= a xor l_key[4*(i+6)]; b:= b xor l_key[4*(i+6)+1]; c:= c xor l_key[4*(i+6)+2]; d:= d xor l_key[4*(i+6)+3];
t1:= a xor c; t2:= b or d; t3:= b xor c; t4:= not t3; t5:= a and d; f:= t4 xor t5; t7:= b or c; t8:= d xor t1; t9:= t7 and t8; h:= t2 xor t9; t11:= t1 and t7; t12:= t4 xor t8; t13:= h and t11; e:= t12 xor t13; t15:= t3 xor t11; t16:= h or t15; g:= t12 xor t16;
e:= (e shl 13) or (e shr 19); g:= (g shl 3) or (g shr 29); f:= f xor e xor g; h:= h xor g xor (e shl 3); f:= (f shl 1) or (f shr 31); h:= (h shl 7) or (h shr 25); e:= e xor f xor h; g:= g xor h xor (f shl 7); e:= (e shl 5) or (e shr 27); g:= (g shl 22) or (g shr 10);
e:= e xor l_key[4*(i+7)]; f:= f xor l_key[4*(i+7)+1]; g:= g xor l_key[4*(i+7)+2]; h:= h xor l_key[4*(i+7)+3];
t1:= not g; t2:= f xor g; t3:= f or t1; t4:= h xor t3; t5:= e and t4; d:= t2 xor t5; t7:= e xor h; t8:= f xor t5; t9:= t2 or t8; b:= t7 xor t9; t11:= h and t3; t12:= t5 xor b; t13:= d and t12; c:= t11 xor t13; t15:= t1 or t4; t16:= t12 xor c; a:= t15 xor t16;
Inc(i,8);
if i < 32 then
begin
a:= (a shl 13) or (a shr 19); c:= (c shl 3) or (c shr 29); b:= b xor a xor c; d:= d xor c xor (a shl 3); b:= (b shl 1) or (b shr 31); d:= (d shl 7) or (d shr 25); a:= a xor b xor d; c:= c xor d xor (b shl 7); a:= (a shl 5) or (a shr 27); c:= (c shl 22) or (c shr 10);
end;
end;
a:= a xor l_key[128]; b:= b xor l_key[128+1]; c:= c xor l_key[128+2]; d:= d xor l_key[128+3];
PDWord(pointer(@OutData)+ 0)^:= a;
PDWord(pointer(@OutData)+ 4)^:= b;
PDWord(pointer(@OutData)+ 8)^:= c;
PDWord(pointer(@OutData)+12)^:= d;
end;
procedure TDCP_serpent.DecryptECB(const InData; var OutData);
var
i: integer;
a, b, c, d, e, f, g, h: dword;
t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
a:= PDWord(@InData)^;
b:= PDWord(pointer(@InData)+4)^;
c:= PDWord(pointer(@InData)+8)^;
d:= PDWord(pointer(@InData)+12)^;
i:= 32;
a:= a xor l_key[4*32]; b:= b xor l_key[4*32+1]; c:= c xor l_key[4*32+2]; d:= d xor l_key[4*32+3];
while i > 0 do
begin
if i < 32 then
begin
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
end;
t1:= a and b; t2:= a or b; t3:= c or t1; t4:= d and t2; h:= t3 xor t4; t6:= not d; t7:= b xor t4; t8:= h xor t6; t9:= t7 or t8; f:= a xor t9; t11:= c xor t7; t12:= d or f; e:= t11 xor t12; t14:= a and h; t15:= t3 xor f; t16:= e xor t14; g:= t15 xor t16;
e:= e xor l_key[4*(i-1)]; f:= f xor l_key[4*(i-1)+1]; g:= g xor l_key[4*(i-1)+2]; h:= h xor l_key[4*(i-1)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= not g; t2:= e xor g; t3:= f xor h; t4:= e or t1; b:= t3 xor t4; t6:= e or f; t7:= f and t2; t8:= b xor t6; t9:= t7 or t8; a:= g xor t9; t11:= not b; t12:= h or t2; t13:= t9 xor t11; d:= t12 xor t13; t15:= f xor t11; t16:= a and d; c:= t15 xor t16;
a:= a xor l_key[4*(i-2)]; b:= b xor l_key[4*(i-2)+1]; c:= c xor l_key[4*(i-2)+2]; d:= d xor l_key[4*(i-2)+3];
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
t1:= not c; t2:= b and t1; t3:= d xor t2; t4:= a and t3; t5:= b xor t1; h:= t4 xor t5; t7:= b or h; t8:= a and t7; f:= t3 xor t8; t10:= a or d; t11:= t1 xor t7; e:= t10 xor t11; t13:= a xor c; t14:= b and t10; t15:= t4 or t13; g:= t14 xor t15;
e:= e xor l_key[4*(i-3)]; f:= f xor l_key[4*(i-3)+1]; g:= g xor l_key[4*(i-3)+2]; h:= h xor l_key[4*(i-3)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= g xor h; t2:= g or h; t3:= f xor t2; t4:= e and t3; b:= t1 xor t4; t6:= e xor h; t7:= f or h; t8:= t6 and t7; d:= t3 xor t8; t10:= not e; t11:= g xor d; t12:= t10 or t11; a:= t3 xor t12; t14:= g or t4; t15:= t7 xor t14; t16:= d or t10; c:= t15 xor t16;
a:= a xor l_key[4*(i-4)]; b:= b xor l_key[4*(i-4)+1]; c:= c xor l_key[4*(i-4)+2]; d:= d xor l_key[4*(i-4)+3];
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
t1:= b xor c; t2:= b or c; t3:= a xor c; t4:= t2 xor t3; t5:= d or t4; e:= t1 xor t5; t7:= a xor d; t8:= t1 or t5; t9:= t2 xor t7; g:= t8 xor t9; t11:= a and t4; t12:= e or t9; f:= t11 xor t12; t14:= a and g; t15:= t2 xor t14; t16:= e and t15; h:= t4 xor t16;
e:= e xor l_key[4*(i-5)]; f:= f xor l_key[4*(i-5)+1]; g:= g xor l_key[4*(i-5)+2]; h:= h xor l_key[4*(i-5)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= f xor h; t2:= not t1; t3:= e xor g; t4:= g xor t1; t5:= f and t4; a:= t3 xor t5; t7:= e or t2; t8:= h xor t7; t9:= t3 or t8; d:= t1 xor t9; t11:= not t4; t12:= a or d; b:= t11 xor t12; t14:= h and t11; t15:= t3 xor t12; c:= t14 xor t15;
a:= a xor l_key[4*(i-6)]; b:= b xor l_key[4*(i-6)+1]; c:= c xor l_key[4*(i-6)+2]; d:= d xor l_key[4*(i-6)+3];
c:= (c shr 22) or (c shl 10); a:= (a shr 5) or (a shl 27); c:= c xor d xor (b shl 7); a:= a xor b xor d; d:= (d shr 7) or (d shl 25); b:= (b shr 1) or (b shl 31); d:= d xor c xor (a shl 3); b:= b xor a xor c; c:= (c shr 3) or (c shl 29); a:= (a shr 13) or (a shl 19);
t1:= a xor d; t2:= a and b; t3:= b xor c; t4:= a xor t3; t5:= b or d; h:= t4 xor t5; t7:= c or t1; t8:= b xor t7; t9:= t4 and t8; f:= t1 xor t9; t11:= not t2; t12:= h and f; t13:= t9 xor t11; g:= t12 xor t13; t15:= a and d; t16:= c xor t13; e:= t15 xor t16;
e:= e xor l_key[4*(i-7)]; f:= f xor l_key[4*(i-7)+1]; g:= g xor l_key[4*(i-7)+2]; h:= h xor l_key[4*(i-7)+3];
g:= (g shr 22) or (g shl 10); e:= (e shr 5) or (e shl 27); g:= g xor h xor (f shl 7); e:= e xor f xor h; h:= (h shr 7) or (h shl 25); f:= (f shr 1) or (f shl 31); h:= h xor g xor (e shl 3); f:= f xor e xor g; g:= (g shr 3) or (g shl 29); e:= (e shr 13) or (e shl 19);
t1:= e xor h; t2:= g xor h; t3:= not t2; t4:= e or f; c:= t3 xor t4; t6:= f xor t1; t7:= g or t6; t8:= e xor t7; t9:= t2 and t8; b:= t6 xor t9; t11:= not t8; t12:= f and h; t13:= b or t12; d:= t11 xor t13; t15:= t2 xor t12; t16:= b or d; a:= t15 xor t16;
a:= a xor l_key[4*(i-8)]; b:= b xor l_key[4*(i-8)+1]; c:= c xor l_key[4*(i-8)+2]; d:= d xor l_key[4*(i-8)+3];
Dec(i,8);
end;
PDWord(pointer(@OutData)+ 0)^:= a;
PDWord(pointer(@OutData)+ 4)^:= b;
PDWord(pointer(@OutData)+ 8)^:= c;
PDWord(pointer(@OutData)+12)^:= d;
end;
end.

View File

@ -1,155 +1,155 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Tea **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPtea;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_tea= class(TDCP_blockcipher64)
protected
KeyData: array[0..3] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
const
Delta: DWord = $9e3779b9;
Rounds= 32;
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
class function TDCP_tea.GetID: integer;
begin
Result:= DCP_tea;
end;
class function TDCP_tea.GetAlgorithm: string;
begin
Result:= 'Tea';
end;
class function TDCP_tea.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_tea.SelfTest: boolean;
const
Key: array[0..3] of dword= ($12345678,$9ABCDEF0,$0FEDCBA9,$87654321);
PT: array[0..1] of dword= ($12345678,$9ABCDEF0);
var
Data: array[0..1] of dword;
Cipher: TDCP_tea;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_tea.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(PT,Data);
Result:= not CompareMem(@Data,@PT,Sizeof(PT));
Cipher.DecryptECB(Data,Data);
Result:= Result and CompareMem(@Data,@PT,Sizeof(PT));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_tea.InitKey(const Key; Size: longword);
begin
FillChar(KeyData,Sizeof(KeyData),0);
Move(Key,KeyData,Size div 8);
KeyData[0]:= SwapDWord(KeyData[0]); KeyData[1]:= SwapDWord(KeyData[1]);
KeyData[2]:= SwapDWord(KeyData[2]); KeyData[3]:= SwapDWord(KeyData[3]);
end;
procedure TDCP_tea.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_tea.EncryptECB(const InData; var OutData);
var
a, b, c, d, x, y, n, sum: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x:= SwapDWord(pdword(@InData)^);
y:= SwapDWord(pdword(pointer(@InData)+4)^);
sum:= 0; a:= KeyData[0]; b:= KeyData[1]; c:= KeyData[2]; d:= KeyData[3];
for n:= 1 to Rounds do
begin
Inc(sum,Delta);
Inc(x,(y shl 4) + (a xor y) + (sum xor (y shr 5)) + b);
Inc(y,(x shl 4) + (c xor x) + (sum xor (x shr 5)) + d);
end;
pdword(@OutData)^:= SwapDWord(x);
pdword(pointer(@OutData)+4)^:= SwapDWord(y);
end;
procedure TDCP_tea.DecryptECB(const InData; var OutData);
var
a, b, c, d, x, y, n, sum: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x:= SwapDWord(pdword(@InData)^);
y:= SwapDWord(pdword(pointer(@InData)+4)^);
sum:= Delta shl 5;
a:= KeyData[0];
b:= KeyData[1];
c:= KeyData[2];
d:= KeyData[3];
for n:= 1 to Rounds do
begin
Dec(y,(x shl 4) + (c xor x) + (sum xor (x shr 5)) + d);
Dec(x,(y shl 4) + (a xor y) + (sum xor (y shr 5)) + b);
Dec(sum,Delta);
end;
pdword(@OutData)^:= SwapDWord(x);
pdword(pointer(@OutData)+4)^:= SwapDWord(y);
end;
end.
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Tea **********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPtea;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_tea= class(TDCP_blockcipher64)
protected
KeyData: array[0..3] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetID: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
const
Delta: DWord = $9e3779b9;
Rounds= 32;
function SwapDword(a: dword): dword;
begin
Result:= ((a and $FF) shl 24) or ((a and $FF00) shl 8) or ((a and $FF0000) shr 8) or ((a and $FF000000) shr 24);
end;
class function TDCP_tea.GetID: integer;
begin
Result:= DCP_tea;
end;
class function TDCP_tea.GetAlgorithm: string;
begin
Result:= 'Tea';
end;
class function TDCP_tea.GetMaxKeySize: integer;
begin
Result:= 128;
end;
class function TDCP_tea.SelfTest: boolean;
const
Key: array[0..3] of dword= ($12345678,$9ABCDEF0,$0FEDCBA9,$87654321);
PT: array[0..1] of dword= ($12345678,$9ABCDEF0);
var
Data: array[0..1] of dword;
Cipher: TDCP_tea;
begin
dcpFillChar(Data, SizeOf(Data), 0);
Cipher:= TDCP_tea.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(PT,Data);
Result:= not CompareMem(@Data,@PT,Sizeof(PT));
Cipher.DecryptECB(Data,Data);
Result:= Result and CompareMem(@Data,@PT,Sizeof(PT));
Cipher.Burn;
Cipher.Free;
end;
procedure TDCP_tea.InitKey(const Key; Size: longword);
begin
FillChar(KeyData,Sizeof(KeyData),0);
Move(Key,KeyData,Size div 8);
KeyData[0]:= SwapDWord(KeyData[0]); KeyData[1]:= SwapDWord(KeyData[1]);
KeyData[2]:= SwapDWord(KeyData[2]); KeyData[3]:= SwapDWord(KeyData[3]);
end;
procedure TDCP_tea.Burn;
begin
FillChar(KeyData,Sizeof(KeyData),0);
inherited Burn;
end;
procedure TDCP_tea.EncryptECB(const InData; var OutData);
var
a, b, c, d, x, y, n, sum: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x:= SwapDWord(pdword(@InData)^);
y:= SwapDWord(pdword(pointer(@InData)+4)^);
sum:= 0; a:= KeyData[0]; b:= KeyData[1]; c:= KeyData[2]; d:= KeyData[3];
for n:= 1 to Rounds do
begin
Inc(sum,Delta);
Inc(x,(y shl 4) + (a xor y) + (sum xor (y shr 5)) + b);
Inc(y,(x shl 4) + (c xor x) + (sum xor (x shr 5)) + d);
end;
pdword(@OutData)^:= SwapDWord(x);
pdword(pointer(@OutData)+4)^:= SwapDWord(y);
end;
procedure TDCP_tea.DecryptECB(const InData; var OutData);
var
a, b, c, d, x, y, n, sum: dword;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x:= SwapDWord(pdword(@InData)^);
y:= SwapDWord(pdword(pointer(@InData)+4)^);
sum:= Delta shl 5;
a:= KeyData[0];
b:= KeyData[1];
c:= KeyData[2];
d:= KeyData[3];
for n:= 1 to Rounds do
begin
Dec(y,(x shl 4) + (c xor x) + (sum xor (x shr 5)) + d);
Dec(x,(y shl 4) + (a xor y) + (sum xor (y shr 5)) + b);
Dec(sum,Delta);
end;
pdword(@OutData)^:= SwapDWord(x);
pdword(pointer(@OutData)+4)^:= SwapDWord(y);
end;
end.

File diff suppressed because it is too large Load Diff