ElAES.pas 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  1. (**************************************************)
  2. (* *)
  3. (* Advanced Encryption Standard (AES) *)
  4. (* *)
  5. (* Copyright (c) 1998-2001 *)
  6. (* EldoS, Alexander Ionov *)
  7. (* *)
  8. (**************************************************)
  9. unit ElAES;
  10. interface
  11. uses
  12. Classes, SysUtils, Windows;
  13. type
  14. EAESError = class(Exception);
  15. PInteger = ^Integer;
  16. TAESBuffer = array [0..15] of byte;
  17. TAESKey128 = array [0..15] of byte;
  18. TAESKey192 = array [0..23] of byte;
  19. TAESKey256 = array [0..31] of byte;
  20. TAESExpandedKey128 = array [0..43] of longword;
  21. TAESExpandedKey192 = array [0..53] of longword;
  22. TAESExpandedKey256 = array [0..63] of longword;
  23. PAESBuffer =^TAESBuffer;
  24. PAESKey128 =^TAESKey128;
  25. PAESKey192 =^TAESKey192;
  26. PAESKey256 =^TAESKey256;
  27. PAESExpandedKey128 =^TAESExpandedKey128;
  28. PAESExpandedKey192 =^TAESExpandedKey192;
  29. PAESExpandedKey256 =^TAESExpandedKey256;
  30. // Key expansion routines for encryption
  31. procedure ExpandAESKeyForEncryption(const Key: TAESKey128;
  32. var ExpandedKey: TAESExpandedKey128); overload;
  33. procedure ExpandAESKeyForEncryption(const Key: TAESKey192;
  34. var ExpandedKey: TAESExpandedKey192); overload;
  35. procedure ExpandAESKeyForEncryption(const Key: TAESKey256;
  36. var ExpandedKey: TAESExpandedKey256); overload;
  37. // Block encryption routines
  38. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
  39. var OutBuf: TAESBuffer); overload;
  40. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey192;
  41. var OutBuf: TAESBuffer); overload;
  42. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey256;
  43. var OutBuf: TAESBuffer); overload;
  44. // Stream encryption routines (ECB mode)
  45. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  46. const Key: TAESKey128; Dest: TStream); overload;
  47. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  48. const ExpandedKey: TAESExpandedKey128; Dest: TStream); overload;
  49. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  50. const Key: TAESKey192; Dest: TStream); overload;
  51. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  52. const ExpandedKey: TAESExpandedKey192; Dest: TStream); overload;
  53. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  54. const Key: TAESKey256; Dest: TStream); overload;
  55. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  56. const ExpandedKey: TAESExpandedKey256; Dest: TStream); overload;
  57. // Stream encryption routines (CBC mode)
  58. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  59. const Key: TAESKey128; const InitVector: TAESBuffer; Dest: TStream); overload;
  60. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  61. const ExpandedKey: TAESExpandedKey128; const InitVector: TAESBuffer;
  62. Dest: TStream); overload;
  63. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  64. const Key: TAESKey192; const InitVector: TAESBuffer; Dest: TStream); overload;
  65. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  66. const ExpandedKey: TAESExpandedKey192; const InitVector: TAESBuffer;
  67. Dest: TStream); overload;
  68. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  69. const Key: TAESKey256; const InitVector: TAESBuffer; Dest: TStream); overload;
  70. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  71. const ExpandedKey: TAESExpandedKey256; const InitVector: TAESBuffer;
  72. Dest: TStream); overload;
  73. // Key transformation routines for decryption
  74. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey128); overload;
  75. procedure ExpandAESKeyForDecryption(const Key: TAESKey128;
  76. var ExpandedKey: TAESExpandedKey128); overload;
  77. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey192); overload;
  78. procedure ExpandAESKeyForDecryption(const Key: TAESKey192;
  79. var ExpandedKey: TAESExpandedKey192); overload;
  80. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey256); overload;
  81. procedure ExpandAESKeyForDecryption(const Key: TAESKey256;
  82. var ExpandedKey: TAESExpandedKey256); overload;
  83. // Block decryption routines
  84. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
  85. var OutBuf: TAESBuffer); overload;
  86. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey192;
  87. var OutBuf: TAESBuffer); overload;
  88. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey256;
  89. var OutBuf: TAESBuffer); overload;
  90. // Stream decryption routines (ECB mode)
  91. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  92. const Key: TAESKey128; Dest: TStream); overload;
  93. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  94. const ExpandedKey: TAESExpandedKey128; Dest: TStream); overload;
  95. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  96. const Key: TAESKey192; Dest: TStream); overload;
  97. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  98. const ExpandedKey: TAESExpandedKey192; Dest: TStream); overload;
  99. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  100. const Key: TAESKey256; Dest: TStream); overload;
  101. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  102. const ExpandedKey: TAESExpandedKey256; Dest: TStream); overload;
  103. // Stream decryption routines (CBC mode)
  104. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  105. const Key: TAESKey128; const InitVector: TAESBuffer; Dest: TStream); overload;
  106. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  107. const ExpandedKey: TAESExpandedKey128; const InitVector: TAESBuffer;
  108. Dest: TStream); overload;
  109. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  110. const Key: TAESKey192; const InitVector: TAESBuffer; Dest: TStream); overload;
  111. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  112. const ExpandedKey: TAESExpandedKey192; const InitVector: TAESBuffer;
  113. Dest: TStream); overload;
  114. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  115. const Key: TAESKey256; const InitVector: TAESBuffer; Dest: TStream); overload;
  116. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  117. const ExpandedKey: TAESExpandedKey256; const InitVector: TAESBuffer;
  118. Dest: TStream); overload;
  119. function AESEncryptBuffer128(dest: Pchar; destLen: PLongint;
  120. source: Pchar; Count: Longint; const strKey: string): integer;
  121. function AESDecryptBuffer128(dest: Pchar; destLen: PLongint;
  122. source: Pchar; Count: Longint; const strKey: string): integer;
  123. resourcestring
  124. SInvalidInBufSize = 'Invalid buffer size for decryption';
  125. SReadError = 'Stream read error';
  126. SWriteError = 'Stream write error';
  127. implementation
  128. type
  129. PLongWord = ^LongWord;
  130. function Min(A, B: integer): integer;
  131. begin
  132. if A < B then
  133. Result := A
  134. else
  135. Result := B;
  136. end;
  137. const
  138. Rcon: array [1..30] of longword = (
  139. $00000001, $00000002, $00000004, $00000008, $00000010, $00000020,
  140. $00000040, $00000080, $0000001B, $00000036, $0000006C, $000000D8,
  141. $000000AB, $0000004D, $0000009A, $0000002F, $0000005E, $000000BC,
  142. $00000063, $000000C6, $00000097, $00000035, $0000006A, $000000D4,
  143. $000000B3, $0000007D, $000000FA, $000000EF, $000000C5, $00000091
  144. );
  145. ForwardTable: array [0..255] of longword = (
  146. $A56363C6, $847C7CF8, $997777EE, $8D7B7BF6, $0DF2F2FF, $BD6B6BD6, $B16F6FDE, $54C5C591,
  147. $50303060, $03010102, $A96767CE, $7D2B2B56, $19FEFEE7, $62D7D7B5, $E6ABAB4D, $9A7676EC,
  148. $45CACA8F, $9D82821F, $40C9C989, $877D7DFA, $15FAFAEF, $EB5959B2, $C947478E, $0BF0F0FB,
  149. $ECADAD41, $67D4D4B3, $FDA2A25F, $EAAFAF45, $BF9C9C23, $F7A4A453, $967272E4, $5BC0C09B,
  150. $C2B7B775, $1CFDFDE1, $AE93933D, $6A26264C, $5A36366C, $413F3F7E, $02F7F7F5, $4FCCCC83,
  151. $5C343468, $F4A5A551, $34E5E5D1, $08F1F1F9, $937171E2, $73D8D8AB, $53313162, $3F15152A,
  152. $0C040408, $52C7C795, $65232346, $5EC3C39D, $28181830, $A1969637, $0F05050A, $B59A9A2F,
  153. $0907070E, $36121224, $9B80801B, $3DE2E2DF, $26EBEBCD, $6927274E, $CDB2B27F, $9F7575EA,
  154. $1B090912, $9E83831D, $742C2C58, $2E1A1A34, $2D1B1B36, $B26E6EDC, $EE5A5AB4, $FBA0A05B,
  155. $F65252A4, $4D3B3B76, $61D6D6B7, $CEB3B37D, $7B292952, $3EE3E3DD, $712F2F5E, $97848413,
  156. $F55353A6, $68D1D1B9, $00000000, $2CEDEDC1, $60202040, $1FFCFCE3, $C8B1B179, $ED5B5BB6,
  157. $BE6A6AD4, $46CBCB8D, $D9BEBE67, $4B393972, $DE4A4A94, $D44C4C98, $E85858B0, $4ACFCF85,
  158. $6BD0D0BB, $2AEFEFC5, $E5AAAA4F, $16FBFBED, $C5434386, $D74D4D9A, $55333366, $94858511,
  159. $CF45458A, $10F9F9E9, $06020204, $817F7FFE, $F05050A0, $443C3C78, $BA9F9F25, $E3A8A84B,
  160. $F35151A2, $FEA3A35D, $C0404080, $8A8F8F05, $AD92923F, $BC9D9D21, $48383870, $04F5F5F1,
  161. $DFBCBC63, $C1B6B677, $75DADAAF, $63212142, $30101020, $1AFFFFE5, $0EF3F3FD, $6DD2D2BF,
  162. $4CCDCD81, $140C0C18, $35131326, $2FECECC3, $E15F5FBE, $A2979735, $CC444488, $3917172E,
  163. $57C4C493, $F2A7A755, $827E7EFC, $473D3D7A, $AC6464C8, $E75D5DBA, $2B191932, $957373E6,
  164. $A06060C0, $98818119, $D14F4F9E, $7FDCDCA3, $66222244, $7E2A2A54, $AB90903B, $8388880B,
  165. $CA46468C, $29EEEEC7, $D3B8B86B, $3C141428, $79DEDEA7, $E25E5EBC, $1D0B0B16, $76DBDBAD,
  166. $3BE0E0DB, $56323264, $4E3A3A74, $1E0A0A14, $DB494992, $0A06060C, $6C242448, $E45C5CB8,
  167. $5DC2C29F, $6ED3D3BD, $EFACAC43, $A66262C4, $A8919139, $A4959531, $37E4E4D3, $8B7979F2,
  168. $32E7E7D5, $43C8C88B, $5937376E, $B76D6DDA, $8C8D8D01, $64D5D5B1, $D24E4E9C, $E0A9A949,
  169. $B46C6CD8, $FA5656AC, $07F4F4F3, $25EAEACF, $AF6565CA, $8E7A7AF4, $E9AEAE47, $18080810,
  170. $D5BABA6F, $887878F0, $6F25254A, $722E2E5C, $241C1C38, $F1A6A657, $C7B4B473, $51C6C697,
  171. $23E8E8CB, $7CDDDDA1, $9C7474E8, $211F1F3E, $DD4B4B96, $DCBDBD61, $868B8B0D, $858A8A0F,
  172. $907070E0, $423E3E7C, $C4B5B571, $AA6666CC, $D8484890, $05030306, $01F6F6F7, $120E0E1C,
  173. $A36161C2, $5F35356A, $F95757AE, $D0B9B969, $91868617, $58C1C199, $271D1D3A, $B99E9E27,
  174. $38E1E1D9, $13F8F8EB, $B398982B, $33111122, $BB6969D2, $70D9D9A9, $898E8E07, $A7949433,
  175. $B69B9B2D, $221E1E3C, $92878715, $20E9E9C9, $49CECE87, $FF5555AA, $78282850, $7ADFDFA5,
  176. $8F8C8C03, $F8A1A159, $80898909, $170D0D1A, $DABFBF65, $31E6E6D7, $C6424284, $B86868D0,
  177. $C3414182, $B0999929, $772D2D5A, $110F0F1E, $CBB0B07B, $FC5454A8, $D6BBBB6D, $3A16162C
  178. );
  179. LastForwardTable: array [0..255] of longword = (
  180. $00000063, $0000007C, $00000077, $0000007B, $000000F2, $0000006B, $0000006F, $000000C5,
  181. $00000030, $00000001, $00000067, $0000002B, $000000FE, $000000D7, $000000AB, $00000076,
  182. $000000CA, $00000082, $000000C9, $0000007D, $000000FA, $00000059, $00000047, $000000F0,
  183. $000000AD, $000000D4, $000000A2, $000000AF, $0000009C, $000000A4, $00000072, $000000C0,
  184. $000000B7, $000000FD, $00000093, $00000026, $00000036, $0000003F, $000000F7, $000000CC,
  185. $00000034, $000000A5, $000000E5, $000000F1, $00000071, $000000D8, $00000031, $00000015,
  186. $00000004, $000000C7, $00000023, $000000C3, $00000018, $00000096, $00000005, $0000009A,
  187. $00000007, $00000012, $00000080, $000000E2, $000000EB, $00000027, $000000B2, $00000075,
  188. $00000009, $00000083, $0000002C, $0000001A, $0000001B, $0000006E, $0000005A, $000000A0,
  189. $00000052, $0000003B, $000000D6, $000000B3, $00000029, $000000E3, $0000002F, $00000084,
  190. $00000053, $000000D1, $00000000, $000000ED, $00000020, $000000FC, $000000B1, $0000005B,
  191. $0000006A, $000000CB, $000000BE, $00000039, $0000004A, $0000004C, $00000058, $000000CF,
  192. $000000D0, $000000EF, $000000AA, $000000FB, $00000043, $0000004D, $00000033, $00000085,
  193. $00000045, $000000F9, $00000002, $0000007F, $00000050, $0000003C, $0000009F, $000000A8,
  194. $00000051, $000000A3, $00000040, $0000008F, $00000092, $0000009D, $00000038, $000000F5,
  195. $000000BC, $000000B6, $000000DA, $00000021, $00000010, $000000FF, $000000F3, $000000D2,
  196. $000000CD, $0000000C, $00000013, $000000EC, $0000005F, $00000097, $00000044, $00000017,
  197. $000000C4, $000000A7, $0000007E, $0000003D, $00000064, $0000005D, $00000019, $00000073,
  198. $00000060, $00000081, $0000004F, $000000DC, $00000022, $0000002A, $00000090, $00000088,
  199. $00000046, $000000EE, $000000B8, $00000014, $000000DE, $0000005E, $0000000B, $000000DB,
  200. $000000E0, $00000032, $0000003A, $0000000A, $00000049, $00000006, $00000024, $0000005C,
  201. $000000C2, $000000D3, $000000AC, $00000062, $00000091, $00000095, $000000E4, $00000079,
  202. $000000E7, $000000C8, $00000037, $0000006D, $0000008D, $000000D5, $0000004E, $000000A9,
  203. $0000006C, $00000056, $000000F4, $000000EA, $00000065, $0000007A, $000000AE, $00000008,
  204. $000000BA, $00000078, $00000025, $0000002E, $0000001C, $000000A6, $000000B4, $000000C6,
  205. $000000E8, $000000DD, $00000074, $0000001F, $0000004B, $000000BD, $0000008B, $0000008A,
  206. $00000070, $0000003E, $000000B5, $00000066, $00000048, $00000003, $000000F6, $0000000E,
  207. $00000061, $00000035, $00000057, $000000B9, $00000086, $000000C1, $0000001D, $0000009E,
  208. $000000E1, $000000F8, $00000098, $00000011, $00000069, $000000D9, $0000008E, $00000094,
  209. $0000009B, $0000001E, $00000087, $000000E9, $000000CE, $00000055, $00000028, $000000DF,
  210. $0000008C, $000000A1, $00000089, $0000000D, $000000BF, $000000E6, $00000042, $00000068,
  211. $00000041, $00000099, $0000002D, $0000000F, $000000B0, $00000054, $000000BB, $00000016
  212. );
  213. InverseTable: array [0..255] of longword = (
  214. $50A7F451, $5365417E, $C3A4171A, $965E273A, $CB6BAB3B, $F1459D1F, $AB58FAAC, $9303E34B,
  215. $55FA3020, $F66D76AD, $9176CC88, $254C02F5, $FCD7E54F, $D7CB2AC5, $80443526, $8FA362B5,
  216. $495AB1DE, $671BBA25, $980EEA45, $E1C0FE5D, $02752FC3, $12F04C81, $A397468D, $C6F9D36B,
  217. $E75F8F03, $959C9215, $EB7A6DBF, $DA595295, $2D83BED4, $D3217458, $2969E049, $44C8C98E,
  218. $6A89C275, $78798EF4, $6B3E5899, $DD71B927, $B64FE1BE, $17AD88F0, $66AC20C9, $B43ACE7D,
  219. $184ADF63, $82311AE5, $60335197, $457F5362, $E07764B1, $84AE6BBB, $1CA081FE, $942B08F9,
  220. $58684870, $19FD458F, $876CDE94, $B7F87B52, $23D373AB, $E2024B72, $578F1FE3, $2AAB5566,
  221. $0728EBB2, $03C2B52F, $9A7BC586, $A50837D3, $F2872830, $B2A5BF23, $BA6A0302, $5C8216ED,
  222. $2B1CCF8A, $92B479A7, $F0F207F3, $A1E2694E, $CDF4DA65, $D5BE0506, $1F6234D1, $8AFEA6C4,
  223. $9D532E34, $A055F3A2, $32E18A05, $75EBF6A4, $39EC830B, $AAEF6040, $069F715E, $51106EBD,
  224. $F98A213E, $3D06DD96, $AE053EDD, $46BDE64D, $B58D5491, $055DC471, $6FD40604, $FF155060,
  225. $24FB9819, $97E9BDD6, $CC434089, $779ED967, $BD42E8B0, $888B8907, $385B19E7, $DBEEC879,
  226. $470A7CA1, $E90F427C, $C91E84F8, $00000000, $83868009, $48ED2B32, $AC70111E, $4E725A6C,
  227. $FBFF0EFD, $5638850F, $1ED5AE3D, $27392D36, $64D90F0A, $21A65C68, $D1545B9B, $3A2E3624,
  228. $B1670A0C, $0FE75793, $D296EEB4, $9E919B1B, $4FC5C080, $A220DC61, $694B775A, $161A121C,
  229. $0ABA93E2, $E52AA0C0, $43E0223C, $1D171B12, $0B0D090E, $ADC78BF2, $B9A8B62D, $C8A91E14,
  230. $8519F157, $4C0775AF, $BBDD99EE, $FD607FA3, $9F2601F7, $BCF5725C, $C53B6644, $347EFB5B,
  231. $7629438B, $DCC623CB, $68FCEDB6, $63F1E4B8, $CADC31D7, $10856342, $40229713, $2011C684,
  232. $7D244A85, $F83DBBD2, $1132F9AE, $6DA129C7, $4B2F9E1D, $F330B2DC, $EC52860D, $D0E3C177,
  233. $6C16B32B, $99B970A9, $FA489411, $2264E947, $C48CFCA8, $1A3FF0A0, $D82C7D56, $EF903322,
  234. $C74E4987, $C1D138D9, $FEA2CA8C, $360BD498, $CF81F5A6, $28DE7AA5, $268EB7DA, $A4BFAD3F,
  235. $E49D3A2C, $0D927850, $9BCC5F6A, $62467E54, $C2138DF6, $E8B8D890, $5EF7392E, $F5AFC382,
  236. $BE805D9F, $7C93D069, $A92DD56F, $B31225CF, $3B99ACC8, $A77D1810, $6E639CE8, $7BBB3BDB,
  237. $097826CD, $F418596E, $01B79AEC, $A89A4F83, $656E95E6, $7EE6FFAA, $08CFBC21, $E6E815EF,
  238. $D99BE7BA, $CE366F4A, $D4099FEA, $D67CB029, $AFB2A431, $31233F2A, $3094A5C6, $C066A235,
  239. $37BC4E74, $A6CA82FC, $B0D090E0, $15D8A733, $4A9804F1, $F7DAEC41, $0E50CD7F, $2FF69117,
  240. $8DD64D76, $4DB0EF43, $544DAACC, $DF0496E4, $E3B5D19E, $1B886A4C, $B81F2CC1, $7F516546,
  241. $04EA5E9D, $5D358C01, $737487FA, $2E410BFB, $5A1D67B3, $52D2DB92, $335610E9, $1347D66D,
  242. $8C61D79A, $7A0CA137, $8E14F859, $893C13EB, $EE27A9CE, $35C961B7, $EDE51CE1, $3CB1477A,
  243. $59DFD29C, $3F73F255, $79CE1418, $BF37C773, $EACDF753, $5BAAFD5F, $146F3DDF, $86DB4478,
  244. $81F3AFCA, $3EC468B9, $2C342438, $5F40A3C2, $72C31D16, $0C25E2BC, $8B493C28, $41950DFF,
  245. $7101A839, $DEB30C08, $9CE4B4D8, $90C15664, $6184CB7B, $70B632D5, $745C6C48, $4257B8D0
  246. );
  247. LastInverseTable: array [0..255] of longword = (
  248. $00000052, $00000009, $0000006A, $000000D5, $00000030, $00000036, $000000A5, $00000038,
  249. $000000BF, $00000040, $000000A3, $0000009E, $00000081, $000000F3, $000000D7, $000000FB,
  250. $0000007C, $000000E3, $00000039, $00000082, $0000009B, $0000002F, $000000FF, $00000087,
  251. $00000034, $0000008E, $00000043, $00000044, $000000C4, $000000DE, $000000E9, $000000CB,
  252. $00000054, $0000007B, $00000094, $00000032, $000000A6, $000000C2, $00000023, $0000003D,
  253. $000000EE, $0000004C, $00000095, $0000000B, $00000042, $000000FA, $000000C3, $0000004E,
  254. $00000008, $0000002E, $000000A1, $00000066, $00000028, $000000D9, $00000024, $000000B2,
  255. $00000076, $0000005B, $000000A2, $00000049, $0000006D, $0000008B, $000000D1, $00000025,
  256. $00000072, $000000F8, $000000F6, $00000064, $00000086, $00000068, $00000098, $00000016,
  257. $000000D4, $000000A4, $0000005C, $000000CC, $0000005D, $00000065, $000000B6, $00000092,
  258. $0000006C, $00000070, $00000048, $00000050, $000000FD, $000000ED, $000000B9, $000000DA,
  259. $0000005E, $00000015, $00000046, $00000057, $000000A7, $0000008D, $0000009D, $00000084,
  260. $00000090, $000000D8, $000000AB, $00000000, $0000008C, $000000BC, $000000D3, $0000000A,
  261. $000000F7, $000000E4, $00000058, $00000005, $000000B8, $000000B3, $00000045, $00000006,
  262. $000000D0, $0000002C, $0000001E, $0000008F, $000000CA, $0000003F, $0000000F, $00000002,
  263. $000000C1, $000000AF, $000000BD, $00000003, $00000001, $00000013, $0000008A, $0000006B,
  264. $0000003A, $00000091, $00000011, $00000041, $0000004F, $00000067, $000000DC, $000000EA,
  265. $00000097, $000000F2, $000000CF, $000000CE, $000000F0, $000000B4, $000000E6, $00000073,
  266. $00000096, $000000AC, $00000074, $00000022, $000000E7, $000000AD, $00000035, $00000085,
  267. $000000E2, $000000F9, $00000037, $000000E8, $0000001C, $00000075, $000000DF, $0000006E,
  268. $00000047, $000000F1, $0000001A, $00000071, $0000001D, $00000029, $000000C5, $00000089,
  269. $0000006F, $000000B7, $00000062, $0000000E, $000000AA, $00000018, $000000BE, $0000001B,
  270. $000000FC, $00000056, $0000003E, $0000004B, $000000C6, $000000D2, $00000079, $00000020,
  271. $0000009A, $000000DB, $000000C0, $000000FE, $00000078, $000000CD, $0000005A, $000000F4,
  272. $0000001F, $000000DD, $000000A8, $00000033, $00000088, $00000007, $000000C7, $00000031,
  273. $000000B1, $00000012, $00000010, $00000059, $00000027, $00000080, $000000EC, $0000005F,
  274. $00000060, $00000051, $0000007F, $000000A9, $00000019, $000000B5, $0000004A, $0000000D,
  275. $0000002D, $000000E5, $0000007A, $0000009F, $00000093, $000000C9, $0000009C, $000000EF,
  276. $000000A0, $000000E0, $0000003B, $0000004D, $000000AE, $0000002A, $000000F5, $000000B0,
  277. $000000C8, $000000EB, $000000BB, $0000003C, $00000083, $00000053, $00000099, $00000061,
  278. $00000017, $0000002B, $00000004, $0000007E, $000000BA, $00000077, $000000D6, $00000026,
  279. $000000E1, $00000069, $00000014, $00000063, $00000055, $00000021, $0000000C, $0000007D
  280. );
  281. procedure ExpandAESKeyForEncryption(const Key: TAESKey128; var ExpandedKey: TAESExpandedKey128);
  282. var
  283. I, J: integer;
  284. T: longword;
  285. W0, W1, W2, W3: longword;
  286. begin
  287. ExpandedKey[0] := PLongWord(@Key[0])^;
  288. ExpandedKey[1] := PLongWord(@Key[4])^;
  289. ExpandedKey[2] := PLongWord(@Key[8])^;
  290. ExpandedKey[3] := PLongWord(@Key[12])^;
  291. I := 0; J := 1;
  292. repeat
  293. T := (ExpandedKey[I + 3] shl 24) or (ExpandedKey[I + 3] shr 8);
  294. W0 := LastForwardTable[Byte(T)]; W1 := LastForwardTable[Byte(T shr 8)];
  295. W2 := LastForwardTable[Byte(T shr 16)]; W3 := LastForwardTable[Byte(T shr 24)];
  296. ExpandedKey[I + 4] := ExpandedKey[I] xor
  297. (W0 xor ((W1 shl 8) or (W1 shr 24)) xor
  298. ((W2 shl 16) or (W2 shr 16)) xor ((W3 shl 24) or (W3 shr 8))) xor Rcon[J];
  299. Inc(J);
  300. ExpandedKey[I + 5] := ExpandedKey[I + 1] xor ExpandedKey[I + 4];
  301. ExpandedKey[I + 6] := ExpandedKey[I + 2] xor ExpandedKey[I + 5];
  302. ExpandedKey[I + 7] := ExpandedKey[I + 3] xor ExpandedKey[I + 6];
  303. Inc(I, 4);
  304. until I >= 40;
  305. end;
  306. procedure ExpandAESKeyForEncryption(const Key: TAESKey192; var ExpandedKey: TAESExpandedKey192); overload;
  307. var
  308. I, J: integer;
  309. T: longword;
  310. W0, W1, W2, W3: longword;
  311. begin
  312. ExpandedKey[0] := PLongWord(@Key[0])^;
  313. ExpandedKey[1] := PLongWord(@Key[4])^;
  314. ExpandedKey[2] := PLongWord(@Key[8])^;
  315. ExpandedKey[3] := PLongWord(@Key[12])^;
  316. ExpandedKey[4] := PLongWord(@Key[16])^;
  317. ExpandedKey[5] := PLongWord(@Key[20])^;
  318. I := 0; J := 1;
  319. repeat
  320. T := (ExpandedKey[I + 5] shl 24) or (ExpandedKey[I + 5] shr 8);
  321. W0 := LastForwardTable[Byte(T)]; W1 := LastForwardTable[Byte(T shr 8)];
  322. W2 := LastForwardTable[Byte(T shr 16)]; W3 := LastForwardTable[Byte(T shr 24)];
  323. ExpandedKey[I + 6] := ExpandedKey[I] xor
  324. (W0 xor ((W1 shl 8) or (W1 shr 24)) xor
  325. ((W2 shl 16) or (W2 shr 16)) xor ((W3 shl 24) or (W3 shr 8))) xor Rcon[J];
  326. Inc(J);
  327. ExpandedKey[I + 7] := ExpandedKey[I + 1] xor ExpandedKey[I + 6];
  328. ExpandedKey[I + 8] := ExpandedKey[I + 2] xor ExpandedKey[I + 7];
  329. ExpandedKey[I + 9] := ExpandedKey[I + 3] xor ExpandedKey[I + 8];
  330. ExpandedKey[I + 10] := ExpandedKey[I + 4] xor ExpandedKey[I + 9];
  331. ExpandedKey[I + 11] := ExpandedKey[I + 5] xor ExpandedKey[I + 10];
  332. Inc(I, 6);
  333. until I >= 46;
  334. end;
  335. procedure ExpandAESKeyForEncryption(const Key: TAESKey256; var ExpandedKey: TAESExpandedKey256); overload;
  336. var
  337. I, J: integer;
  338. T: longword;
  339. W0, W1, W2, W3: longword;
  340. begin
  341. ExpandedKey[0] := PLongWord(@Key[0])^;
  342. ExpandedKey[1] := PLongWord(@Key[4])^;
  343. ExpandedKey[2] := PLongWord(@Key[8])^;
  344. ExpandedKey[3] := PLongWord(@Key[12])^;
  345. ExpandedKey[4] := PLongWord(@Key[16])^;
  346. ExpandedKey[5] := PLongWord(@Key[20])^;
  347. ExpandedKey[6] := PLongWord(@Key[24])^;
  348. ExpandedKey[7] := PLongWord(@Key[28])^;
  349. I := 0; J := 1;
  350. repeat
  351. T := (ExpandedKey[I + 7] shl 24) or (ExpandedKey[I + 7] shr 8);
  352. W0 := LastForwardTable[Byte(T)]; W1 := LastForwardTable[Byte(T shr 8)];
  353. W2 := LastForwardTable[Byte(T shr 16)]; W3 := LastForwardTable[Byte(T shr 24)];
  354. ExpandedKey[I + 8] := ExpandedKey[I] xor
  355. (W0 xor ((W1 shl 8) or (W1 shr 24)) xor
  356. ((W2 shl 16) or (W2 shr 16)) xor ((W3 shl 24) or (W3 shr 8))) xor Rcon[J];
  357. Inc(J);
  358. ExpandedKey[I + 9] := ExpandedKey[I + 1] xor ExpandedKey[I + 8];
  359. ExpandedKey[I + 10] := ExpandedKey[I + 2] xor ExpandedKey[I + 9];
  360. ExpandedKey[I + 11] := ExpandedKey[I + 3] xor ExpandedKey[I + 10];
  361. W0 := LastForwardTable[Byte(ExpandedKey[I + 11])];
  362. W1 := LastForwardTable[Byte(ExpandedKey[I + 11] shr 8)];
  363. W2 := LastForwardTable[Byte(ExpandedKey[I + 11] shr 16)];
  364. W3 := LastForwardTable[Byte(ExpandedKey[I + 11] shr 24)];
  365. ExpandedKey[I + 12] := ExpandedKey[I + 4] xor
  366. (W0 xor ((W1 shl 8) or (W1 shr 24)) xor
  367. ((W2 shl 16) or (W2 shr 16)) xor ((W3 shl 24) or (W3 shr 8)));
  368. ExpandedKey[I + 13] := ExpandedKey[I + 5] xor ExpandedKey[I + 12];
  369. ExpandedKey[I + 14] := ExpandedKey[I + 6] xor ExpandedKey[I + 13];
  370. ExpandedKey[I + 15] := ExpandedKey[I + 7] xor ExpandedKey[I + 14];
  371. Inc(I, 8);
  372. until I >= 52;
  373. end;
  374. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
  375. var OutBuf: TAESBuffer);
  376. var
  377. T0, T1: array [0..3] of longword;
  378. W0, W1, W2, W3: longword;
  379. begin
  380. // initializing
  381. T0[0] := PLongWord(@InBuf[0])^ xor Key[0];
  382. T0[1] := PLongWord(@InBuf[4])^ xor Key[1];
  383. T0[2] := PLongWord(@InBuf[8])^ xor Key[2];
  384. T0[3] := PLongWord(@InBuf[12])^ xor Key[3];
  385. // performing transformation 9 times
  386. // round 1
  387. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  388. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  389. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  390. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  391. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  392. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  393. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  394. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  395. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  396. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  397. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  398. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  399. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  400. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  401. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  402. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  403. // round 2
  404. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  405. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  406. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  407. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  408. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  409. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  410. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  411. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  412. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  413. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  414. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  415. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  416. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  417. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  418. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  419. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  420. // round 3
  421. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  422. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  423. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  424. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  425. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  426. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  427. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  428. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  429. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  430. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  431. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  432. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  433. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  434. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  435. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  436. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  437. // round 4
  438. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  439. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  440. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  441. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  442. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  443. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  444. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  445. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  446. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  447. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  448. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  449. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  450. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  451. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  452. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  453. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  454. // round 5
  455. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  456. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  457. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  458. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  459. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  460. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  461. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  462. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  463. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  464. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  465. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  466. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  467. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  468. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  469. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  470. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  471. // round 6
  472. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  473. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  474. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  475. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  476. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  477. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  478. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  479. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  480. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  481. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  482. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  483. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  484. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  485. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  486. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  487. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  488. // round 7
  489. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  490. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  491. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  492. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  493. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  494. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  495. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  496. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  497. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  498. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  499. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  500. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  501. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  502. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  503. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  504. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  505. // round 8
  506. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  507. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  508. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  509. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  510. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  511. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  512. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  513. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  514. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  515. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  516. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  517. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  518. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  519. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  520. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  521. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  522. // round 9
  523. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  524. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  525. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  526. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  527. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  528. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  529. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  530. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  531. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  532. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  533. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  534. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  535. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  536. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  537. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  538. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  539. // last round of transformations
  540. W0 := LastForwardTable[Byte(T1[0])]; W1 := LastForwardTable[Byte(T1[1] shr 8)];
  541. W2 := LastForwardTable[Byte(T1[2] shr 16)]; W3 := LastForwardTable[Byte(T1[3] shr 24)];
  542. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  543. xor ((W3 shl 24) or (W3 shr 8))) xor Key[40];
  544. W0 := LastForwardTable[Byte(T1[1])]; W1 := LastForwardTable[Byte(T1[2] shr 8)];
  545. W2 := LastForwardTable[Byte(T1[3] shr 16)]; W3 := LastForwardTable[Byte(T1[0] shr 24)];
  546. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  547. xor ((W3 shl 24) or (W3 shr 8))) xor Key[41];
  548. W0 := LastForwardTable[Byte(T1[2])]; W1 := LastForwardTable[Byte(T1[3] shr 8)];
  549. W2 := LastForwardTable[Byte(T1[0] shr 16)]; W3 := LastForwardTable[Byte(T1[1] shr 24)];
  550. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  551. xor ((W3 shl 24) or (W3 shr 8))) xor Key[42];
  552. W0 := LastForwardTable[Byte(T1[3])]; W1 := LastForwardTable[Byte(T1[0] shr 8)];
  553. W2 := LastForwardTable[Byte(T1[1] shr 16)]; W3 := LastForwardTable[Byte(T1[2] shr 24)];
  554. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  555. xor ((W3 shl 24) or (W3 shr 8))) xor Key[43];
  556. // finalizing
  557. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  558. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  559. end;
  560. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey192;
  561. var OutBuf: TAESBuffer);
  562. var
  563. T0, T1: array [0..3] of longword;
  564. W0, W1, W2, W3: longword;
  565. begin
  566. // initializing
  567. T0[0] := PLongWord(@InBuf[0])^ xor Key[0];
  568. T0[1] := PLongWord(@InBuf[4])^ xor Key[1];
  569. T0[2] := PLongWord(@InBuf[8])^ xor Key[2];
  570. T0[3] := PLongWord(@InBuf[12])^ xor Key[3];
  571. // performing transformation 11 times
  572. // round 1
  573. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  574. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  575. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  576. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  577. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  578. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  579. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  580. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  581. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  582. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  583. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  584. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  585. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  586. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  587. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  588. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  589. // round 2
  590. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  591. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  592. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  593. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  594. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  595. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  596. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  597. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  598. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  599. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  600. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  601. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  602. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  603. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  604. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  605. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  606. // round 3
  607. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  608. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  609. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  610. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  611. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  612. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  613. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  614. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  615. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  616. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  617. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  618. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  619. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  620. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  621. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  622. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  623. // round 4
  624. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  625. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  626. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  627. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  628. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  629. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  630. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  631. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  632. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  633. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  634. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  635. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  636. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  637. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  638. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  639. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  640. // round 5
  641. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  642. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  643. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  644. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  645. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  646. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  647. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  648. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  649. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  650. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  651. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  652. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  653. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  654. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  655. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  656. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  657. // round 6
  658. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  659. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  660. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  661. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  662. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  663. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  664. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  665. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  666. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  667. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  668. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  669. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  670. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  671. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  672. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  673. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  674. // round 7
  675. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  676. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  677. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  678. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  679. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  680. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  681. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  682. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  683. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  684. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  685. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  686. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  687. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  688. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  689. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  690. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  691. // round 8
  692. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  693. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  694. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  695. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  696. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  697. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  698. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  699. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  700. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  701. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  702. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  703. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  704. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  705. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  706. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  707. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  708. // round 9
  709. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  710. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  711. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  712. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  713. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  714. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  715. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  716. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  717. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  718. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  719. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  720. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  721. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  722. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  723. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  724. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  725. // round 10
  726. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  727. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  728. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  729. xor ((W3 shl 24) or (W3 shr 8))) xor Key[40];
  730. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  731. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  732. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  733. xor ((W3 shl 24) or (W3 shr 8))) xor Key[41];
  734. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  735. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  736. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  737. xor ((W3 shl 24) or (W3 shr 8))) xor Key[42];
  738. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  739. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  740. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  741. xor ((W3 shl 24) or (W3 shr 8))) xor Key[43];
  742. // round 11
  743. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  744. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  745. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  746. xor ((W3 shl 24) or (W3 shr 8))) xor Key[44];
  747. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  748. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  749. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  750. xor ((W3 shl 24) or (W3 shr 8))) xor Key[45];
  751. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  752. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  753. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  754. xor ((W3 shl 24) or (W3 shr 8))) xor Key[46];
  755. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  756. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  757. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  758. xor ((W3 shl 24) or (W3 shr 8))) xor Key[47];
  759. // last round of transformations
  760. W0 := LastForwardTable[Byte(T1[0])]; W1 := LastForwardTable[Byte(T1[1] shr 8)];
  761. W2 := LastForwardTable[Byte(T1[2] shr 16)]; W3 := LastForwardTable[Byte(T1[3] shr 24)];
  762. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  763. xor ((W3 shl 24) or (W3 shr 8))) xor Key[48];
  764. W0 := LastForwardTable[Byte(T1[1])]; W1 := LastForwardTable[Byte(T1[2] shr 8)];
  765. W2 := LastForwardTable[Byte(T1[3] shr 16)]; W3 := LastForwardTable[Byte(T1[0] shr 24)];
  766. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  767. xor ((W3 shl 24) or (W3 shr 8))) xor Key[49];
  768. W0 := LastForwardTable[Byte(T1[2])]; W1 := LastForwardTable[Byte(T1[3] shr 8)];
  769. W2 := LastForwardTable[Byte(T1[0] shr 16)]; W3 := LastForwardTable[Byte(T1[1] shr 24)];
  770. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  771. xor ((W3 shl 24) or (W3 shr 8))) xor Key[50];
  772. W0 := LastForwardTable[Byte(T1[3])]; W1 := LastForwardTable[Byte(T1[0] shr 8)];
  773. W2 := LastForwardTable[Byte(T1[1] shr 16)]; W3 := LastForwardTable[Byte(T1[2] shr 24)];
  774. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  775. xor ((W3 shl 24) or (W3 shr 8))) xor Key[51];
  776. // finalizing
  777. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  778. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  779. end;
  780. procedure EncryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey256;
  781. var OutBuf: TAESBuffer);
  782. var
  783. T0, T1: array [0..3] of longword;
  784. W0, W1, W2, W3: longword;
  785. begin
  786. // initializing
  787. T0[0] := PLongWord(@InBuf[0])^ xor Key[0];
  788. T0[1] := PLongWord(@InBuf[4])^ xor Key[1];
  789. T0[2] := PLongWord(@InBuf[8])^ xor Key[2];
  790. T0[3] := PLongWord(@InBuf[12])^ xor Key[3];
  791. // performing transformation 13 times
  792. // round 1
  793. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  794. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  795. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  796. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  797. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  798. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  799. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  800. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  801. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  802. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  803. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  804. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  805. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  806. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  807. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  808. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  809. // round 2
  810. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  811. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  812. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  813. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  814. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  815. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  816. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  817. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  818. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  819. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  820. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  821. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  822. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  823. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  824. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  825. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  826. // round 3
  827. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  828. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  829. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  830. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  831. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  832. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  833. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  834. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  835. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  836. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  837. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  838. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  839. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  840. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  841. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  842. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  843. // round 4
  844. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  845. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  846. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  847. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  848. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  849. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  850. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  851. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  852. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  853. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  854. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  855. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  856. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  857. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  858. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  859. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  860. // round 5
  861. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  862. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  863. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  864. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  865. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  866. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  867. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  868. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  869. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  870. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  871. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  872. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  873. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  874. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  875. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  876. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  877. // round 6
  878. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  879. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  880. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  881. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  882. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  883. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  884. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  885. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  886. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  887. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  888. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  889. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  890. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  891. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  892. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  893. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  894. // round 7
  895. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  896. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  897. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  898. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  899. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  900. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  901. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  902. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  903. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  904. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  905. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  906. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  907. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  908. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  909. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  910. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  911. // round 8
  912. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  913. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  914. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  915. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  916. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  917. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  918. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  919. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  920. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  921. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  922. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  923. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  924. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  925. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  926. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  927. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  928. // round 9
  929. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  930. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  931. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  932. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  933. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  934. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  935. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  936. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  937. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  938. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  939. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  940. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  941. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  942. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  943. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  944. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  945. // round 10
  946. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  947. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  948. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  949. xor ((W3 shl 24) or (W3 shr 8))) xor Key[40];
  950. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  951. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  952. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  953. xor ((W3 shl 24) or (W3 shr 8))) xor Key[41];
  954. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  955. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  956. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  957. xor ((W3 shl 24) or (W3 shr 8))) xor Key[42];
  958. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  959. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  960. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  961. xor ((W3 shl 24) or (W3 shr 8))) xor Key[43];
  962. // round 11
  963. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  964. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  965. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  966. xor ((W3 shl 24) or (W3 shr 8))) xor Key[44];
  967. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  968. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  969. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  970. xor ((W3 shl 24) or (W3 shr 8))) xor Key[45];
  971. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  972. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  973. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  974. xor ((W3 shl 24) or (W3 shr 8))) xor Key[46];
  975. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  976. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  977. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  978. xor ((W3 shl 24) or (W3 shr 8))) xor Key[47];
  979. // round 12
  980. W0 := ForwardTable[Byte(T1[0])]; W1 := ForwardTable[Byte(T1[1] shr 8)];
  981. W2 := ForwardTable[Byte(T1[2] shr 16)]; W3 := ForwardTable[Byte(T1[3] shr 24)];
  982. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  983. xor ((W3 shl 24) or (W3 shr 8))) xor Key[48];
  984. W0 := ForwardTable[Byte(T1[1])]; W1 := ForwardTable[Byte(T1[2] shr 8)];
  985. W2 := ForwardTable[Byte(T1[3] shr 16)]; W3 := ForwardTable[Byte(T1[0] shr 24)];
  986. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  987. xor ((W3 shl 24) or (W3 shr 8))) xor Key[49];
  988. W0 := ForwardTable[Byte(T1[2])]; W1 := ForwardTable[Byte(T1[3] shr 8)];
  989. W2 := ForwardTable[Byte(T1[0] shr 16)]; W3 := ForwardTable[Byte(T1[1] shr 24)];
  990. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  991. xor ((W3 shl 24) or (W3 shr 8))) xor Key[50];
  992. W0 := ForwardTable[Byte(T1[3])]; W1 := ForwardTable[Byte(T1[0] shr 8)];
  993. W2 := ForwardTable[Byte(T1[1] shr 16)]; W3 := ForwardTable[Byte(T1[2] shr 24)];
  994. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  995. xor ((W3 shl 24) or (W3 shr 8))) xor Key[51];
  996. // round 13
  997. W0 := ForwardTable[Byte(T0[0])]; W1 := ForwardTable[Byte(T0[1] shr 8)];
  998. W2 := ForwardTable[Byte(T0[2] shr 16)]; W3 := ForwardTable[Byte(T0[3] shr 24)];
  999. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1000. xor ((W3 shl 24) or (W3 shr 8))) xor Key[52];
  1001. W0 := ForwardTable[Byte(T0[1])]; W1 := ForwardTable[Byte(T0[2] shr 8)];
  1002. W2 := ForwardTable[Byte(T0[3] shr 16)]; W3 := ForwardTable[Byte(T0[0] shr 24)];
  1003. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1004. xor ((W3 shl 24) or (W3 shr 8))) xor Key[53];
  1005. W0 := ForwardTable[Byte(T0[2])]; W1 := ForwardTable[Byte(T0[3] shr 8)];
  1006. W2 := ForwardTable[Byte(T0[0] shr 16)]; W3 := ForwardTable[Byte(T0[1] shr 24)];
  1007. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1008. xor ((W3 shl 24) or (W3 shr 8))) xor Key[54];
  1009. W0 := ForwardTable[Byte(T0[3])]; W1 := ForwardTable[Byte(T0[0] shr 8)];
  1010. W2 := ForwardTable[Byte(T0[1] shr 16)]; W3 := ForwardTable[Byte(T0[2] shr 24)];
  1011. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1012. xor ((W3 shl 24) or (W3 shr 8))) xor Key[55];
  1013. // last round of transformations
  1014. W0 := LastForwardTable[Byte(T1[0])]; W1 := LastForwardTable[Byte(T1[1] shr 8)];
  1015. W2 := LastForwardTable[Byte(T1[2] shr 16)]; W3 := LastForwardTable[Byte(T1[3] shr 24)];
  1016. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1017. xor ((W3 shl 24) or (W3 shr 8))) xor Key[56];
  1018. W0 := LastForwardTable[Byte(T1[1])]; W1 := LastForwardTable[Byte(T1[2] shr 8)];
  1019. W2 := LastForwardTable[Byte(T1[3] shr 16)]; W3 := LastForwardTable[Byte(T1[0] shr 24)];
  1020. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1021. xor ((W3 shl 24) or (W3 shr 8))) xor Key[57];
  1022. W0 := LastForwardTable[Byte(T1[2])]; W1 := LastForwardTable[Byte(T1[3] shr 8)];
  1023. W2 := LastForwardTable[Byte(T1[0] shr 16)]; W3 := LastForwardTable[Byte(T1[1] shr 24)];
  1024. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1025. xor ((W3 shl 24) or (W3 shr 8))) xor Key[58];
  1026. W0 := LastForwardTable[Byte(T1[3])]; W1 := LastForwardTable[Byte(T1[0] shr 8)];
  1027. W2 := LastForwardTable[Byte(T1[1] shr 16)]; W3 := LastForwardTable[Byte(T1[2] shr 24)];
  1028. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1029. xor ((W3 shl 24) or (W3 shr 8))) xor Key[59];
  1030. // finalizing
  1031. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  1032. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  1033. end;
  1034. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey128);
  1035. var
  1036. I: integer;
  1037. U, F2, F4, F8, F9: longword;
  1038. begin
  1039. for I := 1 to 9 do
  1040. begin
  1041. F9 := ExpandedKey[I * 4];
  1042. U := F9 and $80808080;
  1043. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1044. U := F2 and $80808080;
  1045. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1046. U := F4 and $80808080;
  1047. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1048. F9 := F9 xor F8;
  1049. ExpandedKey[I * 4] := F2 xor F4 xor F8 xor
  1050. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1051. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1052. F9 := ExpandedKey[I * 4 + 1];
  1053. U := F9 and $80808080;
  1054. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1055. U := F2 and $80808080;
  1056. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1057. U := F4 and $80808080;
  1058. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1059. F9 := F9 xor F8;
  1060. ExpandedKey[I * 4 + 1] := F2 xor F4 xor F8 xor
  1061. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1062. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1063. F9 := ExpandedKey[I * 4 + 2];
  1064. U := F9 and $80808080;
  1065. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1066. U := F2 and $80808080;
  1067. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1068. U := F4 and $80808080;
  1069. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1070. F9 := F9 xor F8;
  1071. ExpandedKey[I * 4 + 2] := F2 xor F4 xor F8 xor
  1072. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1073. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1074. F9 := ExpandedKey[I * 4 + 3];
  1075. U := F9 and $80808080;
  1076. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1077. U := F2 and $80808080;
  1078. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1079. U := F4 and $80808080;
  1080. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1081. F9 := F9 xor F8;
  1082. ExpandedKey[I * 4 + 3] := F2 xor F4 xor F8 xor
  1083. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1084. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1085. end;
  1086. end;
  1087. procedure ExpandAESKeyForDecryption(const Key: TAESKey128; var ExpandedKey: TAESExpandedKey128);
  1088. begin
  1089. ExpandAESKeyForEncryption(Key, ExpandedKey);
  1090. ExpandAESKeyForDecryption(ExpandedKey);
  1091. end;
  1092. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey192);
  1093. var
  1094. I: integer;
  1095. U, F2, F4, F8, F9: longword;
  1096. begin
  1097. for I := 1 to 11 do
  1098. begin
  1099. F9 := ExpandedKey[I * 4];
  1100. U := F9 and $80808080;
  1101. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1102. U := F2 and $80808080;
  1103. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1104. U := F4 and $80808080;
  1105. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1106. F9 := F9 xor F8;
  1107. ExpandedKey[I * 4] := F2 xor F4 xor F8 xor
  1108. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1109. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1110. F9 := ExpandedKey[I * 4 + 1];
  1111. U := F9 and $80808080;
  1112. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1113. U := F2 and $80808080;
  1114. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1115. U := F4 and $80808080;
  1116. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1117. F9 := F9 xor F8;
  1118. ExpandedKey[I * 4 + 1] := F2 xor F4 xor F8 xor
  1119. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1120. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1121. F9 := ExpandedKey[I * 4 + 2];
  1122. U := F9 and $80808080;
  1123. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1124. U := F2 and $80808080;
  1125. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1126. U := F4 and $80808080;
  1127. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1128. F9 := F9 xor F8;
  1129. ExpandedKey[I * 4 + 2] := F2 xor F4 xor F8 xor
  1130. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1131. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1132. F9 := ExpandedKey[I * 4 + 3];
  1133. U := F9 and $80808080;
  1134. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1135. U := F2 and $80808080;
  1136. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1137. U := F4 and $80808080;
  1138. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1139. F9 := F9 xor F8;
  1140. ExpandedKey[I * 4 + 3] := F2 xor F4 xor F8 xor
  1141. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1142. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1143. end;
  1144. end;
  1145. procedure ExpandAESKeyForDecryption(const Key: TAESKey192; var ExpandedKey: TAESExpandedKey192);
  1146. begin
  1147. ExpandAESKeyForEncryption(Key, ExpandedKey);
  1148. ExpandAESKeyForDecryption(ExpandedKey);
  1149. end;
  1150. procedure ExpandAESKeyForDecryption(var ExpandedKey: TAESExpandedKey256);
  1151. var
  1152. I: integer;
  1153. U, F2, F4, F8, F9: longword;
  1154. begin
  1155. for I := 1 to 13 do
  1156. begin
  1157. F9 := ExpandedKey[I * 4];
  1158. U := F9 and $80808080;
  1159. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1160. U := F2 and $80808080;
  1161. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1162. U := F4 and $80808080;
  1163. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1164. F9 := F9 xor F8;
  1165. ExpandedKey[I * 4] := F2 xor F4 xor F8 xor
  1166. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1167. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1168. F9 := ExpandedKey[I * 4 + 1];
  1169. U := F9 and $80808080;
  1170. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1171. U := F2 and $80808080;
  1172. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1173. U := F4 and $80808080;
  1174. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1175. F9 := F9 xor F8;
  1176. ExpandedKey[I * 4 + 1] := F2 xor F4 xor F8 xor
  1177. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1178. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1179. F9 := ExpandedKey[I * 4 + 2];
  1180. U := F9 and $80808080;
  1181. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1182. U := F2 and $80808080;
  1183. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1184. U := F4 and $80808080;
  1185. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1186. F9 := F9 xor F8;
  1187. ExpandedKey[I * 4 + 2] := F2 xor F4 xor F8 xor
  1188. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1189. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1190. F9 := ExpandedKey[I * 4 + 3];
  1191. U := F9 and $80808080;
  1192. F2 := ((F9 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1193. U := F2 and $80808080;
  1194. F4 := ((F2 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1195. U := F4 and $80808080;
  1196. F8 := ((F4 and $7F7F7F7F) shl 1) xor ((U - (U shr 7)) and $1B1B1B1B);
  1197. F9 := F9 xor F8;
  1198. ExpandedKey[I * 4 + 3] := F2 xor F4 xor F8 xor
  1199. (((F2 xor F9) shl 24) or ((F2 xor F9) shr 8)) xor
  1200. (((F4 xor F9) shl 16) or ((F4 xor F9) shr 16)) xor ((F9 shl 8) or (F9 shr 24));
  1201. end;
  1202. end;
  1203. procedure ExpandAESKeyForDecryption(const Key: TAESKey256; var ExpandedKey: TAESExpandedKey256);
  1204. begin
  1205. ExpandAESKeyForEncryption(Key, ExpandedKey);
  1206. ExpandAESKeyForDecryption(ExpandedKey);
  1207. end;
  1208. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey128;
  1209. var OutBuf: TAESBuffer);
  1210. var
  1211. T0, T1: array [0..3] of longword;
  1212. W0, W1, W2, W3: longword;
  1213. begin
  1214. // initializing
  1215. T0[0] := PLongWord(@InBuf[0])^ xor Key[40];
  1216. T0[1] := PLongWord(@InBuf[4])^ xor Key[41];
  1217. T0[2] := PLongWord(@InBuf[8])^ xor Key[42];
  1218. T0[3] := PLongWord(@InBuf[12])^ xor Key[43];
  1219. // performing transformations 9 times
  1220. // round 1
  1221. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1222. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1223. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1224. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  1225. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1226. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1227. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1228. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  1229. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1230. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1231. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1232. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  1233. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1234. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1235. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1236. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  1237. // round 2
  1238. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1239. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1240. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1241. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  1242. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1243. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1244. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1245. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  1246. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1247. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1248. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1249. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  1250. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1251. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1252. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1253. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  1254. // round 3
  1255. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1256. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1257. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1258. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  1259. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1260. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1261. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1262. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  1263. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1264. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1265. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1266. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  1267. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1268. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1269. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1270. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  1271. // round 4
  1272. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1273. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1274. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1275. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  1276. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1277. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1278. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1279. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  1280. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1281. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1282. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1283. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  1284. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1285. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1286. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1287. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  1288. // round 5
  1289. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1290. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1291. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1292. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  1293. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1294. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1295. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1296. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  1297. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1298. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1299. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1300. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  1301. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1302. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1303. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1304. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  1305. // round 6
  1306. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1307. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1308. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1309. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  1310. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1311. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1312. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1313. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  1314. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1315. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1316. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1317. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  1318. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1319. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1320. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1321. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  1322. // round 7
  1323. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1324. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1325. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1326. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  1327. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1328. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1329. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1330. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  1331. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1332. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1333. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1334. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  1335. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1336. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1337. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1338. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  1339. // round 8
  1340. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1341. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1342. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1343. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  1344. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1345. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1346. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1347. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  1348. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1349. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1350. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1351. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  1352. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1353. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1354. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1355. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  1356. // round 9
  1357. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1358. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1359. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1360. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  1361. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1362. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1363. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1364. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  1365. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1366. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1367. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1368. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  1369. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1370. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1371. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1372. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  1373. // last round of transformations
  1374. W0 := LastInverseTable[Byte(T1[0])]; W1 := LastInverseTable[Byte(T1[3] shr 8)];
  1375. W2 := LastInverseTable[Byte(T1[2] shr 16)]; W3 := LastInverseTable[Byte(T1[1] shr 24)];
  1376. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1377. xor ((W3 shl 24) or (W3 shr 8))) xor Key[0];
  1378. W0 := LastInverseTable[Byte(T1[1])]; W1 := LastInverseTable[Byte(T1[0] shr 8)];
  1379. W2 := LastInverseTable[Byte(T1[3] shr 16)]; W3 := LastInverseTable[Byte(T1[2] shr 24)];
  1380. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1381. xor ((W3 shl 24) or (W3 shr 8))) xor Key[1];
  1382. W0 := LastInverseTable[Byte(T1[2])]; W1 := LastInverseTable[Byte(T1[1] shr 8)];
  1383. W2 := LastInverseTable[Byte(T1[0] shr 16)]; W3 := LastInverseTable[Byte(T1[3] shr 24)];
  1384. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1385. xor ((W3 shl 24) or (W3 shr 8))) xor Key[2];
  1386. W0 := LastInverseTable[Byte(T1[3])]; W1 := LastInverseTable[Byte(T1[2] shr 8)];
  1387. W2 := LastInverseTable[Byte(T1[1] shr 16)]; W3 := LastInverseTable[Byte(T1[0] shr 24)];
  1388. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1389. xor ((W3 shl 24) or (W3 shr 8))) xor Key[3];
  1390. // finalizing
  1391. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  1392. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  1393. end;
  1394. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey192;
  1395. var OutBuf: TAESBuffer);
  1396. var
  1397. T0, T1: array [0..3] of longword;
  1398. W0, W1, W2, W3: longword;
  1399. begin
  1400. // initializing
  1401. T0[0] := PLongWord(@InBuf[0])^ xor Key[48];
  1402. T0[1] := PLongWord(@InBuf[4])^ xor Key[49];
  1403. T0[2] := PLongWord(@InBuf[8])^ xor Key[50];
  1404. T0[3] := PLongWord(@InBuf[12])^ xor Key[51];
  1405. // performing transformations 11 times
  1406. // round 1
  1407. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1408. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1409. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1410. xor ((W3 shl 24) or (W3 shr 8))) xor Key[44];
  1411. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1412. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1413. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1414. xor ((W3 shl 24) or (W3 shr 8))) xor Key[45];
  1415. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1416. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1417. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1418. xor ((W3 shl 24) or (W3 shr 8))) xor Key[46];
  1419. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1420. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1421. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1422. xor ((W3 shl 24) or (W3 shr 8))) xor Key[47];
  1423. // round 2
  1424. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1425. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1426. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1427. xor ((W3 shl 24) or (W3 shr 8))) xor Key[40];
  1428. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1429. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1430. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1431. xor ((W3 shl 24) or (W3 shr 8))) xor Key[41];
  1432. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1433. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1434. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1435. xor ((W3 shl 24) or (W3 shr 8))) xor Key[42];
  1436. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1437. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1438. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1439. xor ((W3 shl 24) or (W3 shr 8))) xor Key[43];
  1440. // round 3
  1441. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1442. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1443. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1444. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  1445. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1446. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1447. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1448. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  1449. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1450. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1451. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1452. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  1453. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1454. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1455. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1456. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  1457. // round 4
  1458. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1459. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1460. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1461. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  1462. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1463. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1464. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1465. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  1466. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1467. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1468. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1469. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  1470. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1471. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1472. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1473. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  1474. // round 5
  1475. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1476. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1477. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1478. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  1479. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1480. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1481. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1482. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  1483. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1484. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1485. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1486. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  1487. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1488. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1489. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1490. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  1491. // round 6
  1492. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1493. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1494. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1495. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  1496. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1497. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1498. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1499. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  1500. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1501. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1502. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1503. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  1504. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1505. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1506. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1507. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  1508. // round 7
  1509. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1510. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1511. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1512. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  1513. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1514. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1515. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1516. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  1517. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1518. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1519. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1520. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  1521. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1522. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1523. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1524. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  1525. // round 8
  1526. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1527. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1528. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1529. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  1530. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1531. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1532. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1533. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  1534. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1535. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1536. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1537. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  1538. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1539. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1540. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1541. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  1542. // round 9
  1543. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1544. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1545. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1546. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  1547. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1548. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1549. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1550. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  1551. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1552. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1553. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1554. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  1555. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1556. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1557. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1558. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  1559. // round 10
  1560. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1561. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1562. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1563. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  1564. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1565. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1566. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1567. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  1568. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1569. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1570. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1571. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  1572. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1573. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1574. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1575. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  1576. // round 11
  1577. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1578. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1579. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1580. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  1581. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1582. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1583. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1584. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  1585. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1586. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1587. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1588. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  1589. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1590. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1591. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1592. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  1593. // last round of transformations
  1594. W0 := LastInverseTable[Byte(T1[0])]; W1 := LastInverseTable[Byte(T1[3] shr 8)];
  1595. W2 := LastInverseTable[Byte(T1[2] shr 16)]; W3 := LastInverseTable[Byte(T1[1] shr 24)];
  1596. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1597. xor ((W3 shl 24) or (W3 shr 8))) xor Key[0];
  1598. W0 := LastInverseTable[Byte(T1[1])]; W1 := LastInverseTable[Byte(T1[0] shr 8)];
  1599. W2 := LastInverseTable[Byte(T1[3] shr 16)]; W3 := LastInverseTable[Byte(T1[2] shr 24)];
  1600. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1601. xor ((W3 shl 24) or (W3 shr 8))) xor Key[1];
  1602. W0 := LastInverseTable[Byte(T1[2])]; W1 := LastInverseTable[Byte(T1[1] shr 8)];
  1603. W2 := LastInverseTable[Byte(T1[0] shr 16)]; W3 := LastInverseTable[Byte(T1[3] shr 24)];
  1604. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1605. xor ((W3 shl 24) or (W3 shr 8))) xor Key[2];
  1606. W0 := LastInverseTable[Byte(T1[3])]; W1 := LastInverseTable[Byte(T1[2] shr 8)];
  1607. W2 := LastInverseTable[Byte(T1[1] shr 16)]; W3 := LastInverseTable[Byte(T1[0] shr 24)];
  1608. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1609. xor ((W3 shl 24) or (W3 shr 8))) xor Key[3];
  1610. // finalizing
  1611. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  1612. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  1613. end;
  1614. procedure DecryptAES(const InBuf: TAESBuffer; const Key: TAESExpandedKey256;
  1615. var OutBuf: TAESBuffer);
  1616. var
  1617. T0, T1: array [0..3] of longword;
  1618. W0, W1, W2, W3: longword;
  1619. begin
  1620. // initializing
  1621. T0[0] := PLongWord(@InBuf[0])^ xor Key[56];
  1622. T0[1] := PLongWord(@InBuf[4])^ xor Key[57];
  1623. T0[2] := PLongWord(@InBuf[8])^ xor Key[58];
  1624. T0[3] := PLongWord(@InBuf[12])^ xor Key[59];
  1625. // performing transformations 13 times
  1626. // round 1
  1627. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1628. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1629. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1630. xor ((W3 shl 24) or (W3 shr 8))) xor Key[52];
  1631. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1632. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1633. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1634. xor ((W3 shl 24) or (W3 shr 8))) xor Key[53];
  1635. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1636. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1637. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1638. xor ((W3 shl 24) or (W3 shr 8))) xor Key[54];
  1639. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1640. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1641. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1642. xor ((W3 shl 24) or (W3 shr 8))) xor Key[55];
  1643. // round 2
  1644. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1645. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1646. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1647. xor ((W3 shl 24) or (W3 shr 8))) xor Key[48];
  1648. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1649. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1650. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1651. xor ((W3 shl 24) or (W3 shr 8))) xor Key[49];
  1652. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1653. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1654. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1655. xor ((W3 shl 24) or (W3 shr 8))) xor Key[50];
  1656. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1657. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1658. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1659. xor ((W3 shl 24) or (W3 shr 8))) xor Key[51];
  1660. // round 3
  1661. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1662. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1663. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1664. xor ((W3 shl 24) or (W3 shr 8))) xor Key[44];
  1665. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1666. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1667. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1668. xor ((W3 shl 24) or (W3 shr 8))) xor Key[45];
  1669. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1670. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1671. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1672. xor ((W3 shl 24) or (W3 shr 8))) xor Key[46];
  1673. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1674. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1675. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1676. xor ((W3 shl 24) or (W3 shr 8))) xor Key[47];
  1677. // round 4
  1678. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1679. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1680. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1681. xor ((W3 shl 24) or (W3 shr 8))) xor Key[40];
  1682. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1683. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1684. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1685. xor ((W3 shl 24) or (W3 shr 8))) xor Key[41];
  1686. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1687. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1688. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1689. xor ((W3 shl 24) or (W3 shr 8))) xor Key[42];
  1690. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1691. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1692. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1693. xor ((W3 shl 24) or (W3 shr 8))) xor Key[43];
  1694. // round 5
  1695. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1696. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1697. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1698. xor ((W3 shl 24) or (W3 shr 8))) xor Key[36];
  1699. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1700. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1701. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1702. xor ((W3 shl 24) or (W3 shr 8))) xor Key[37];
  1703. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1704. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1705. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1706. xor ((W3 shl 24) or (W3 shr 8))) xor Key[38];
  1707. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1708. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1709. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1710. xor ((W3 shl 24) or (W3 shr 8))) xor Key[39];
  1711. // round 6
  1712. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1713. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1714. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1715. xor ((W3 shl 24) or (W3 shr 8))) xor Key[32];
  1716. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1717. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1718. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1719. xor ((W3 shl 24) or (W3 shr 8))) xor Key[33];
  1720. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1721. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1722. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1723. xor ((W3 shl 24) or (W3 shr 8))) xor Key[34];
  1724. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1725. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1726. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1727. xor ((W3 shl 24) or (W3 shr 8))) xor Key[35];
  1728. // round 7
  1729. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1730. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1731. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1732. xor ((W3 shl 24) or (W3 shr 8))) xor Key[28];
  1733. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1734. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1735. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1736. xor ((W3 shl 24) or (W3 shr 8))) xor Key[29];
  1737. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1738. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1739. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1740. xor ((W3 shl 24) or (W3 shr 8))) xor Key[30];
  1741. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1742. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1743. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1744. xor ((W3 shl 24) or (W3 shr 8))) xor Key[31];
  1745. // round 8
  1746. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1747. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1748. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1749. xor ((W3 shl 24) or (W3 shr 8))) xor Key[24];
  1750. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1751. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1752. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1753. xor ((W3 shl 24) or (W3 shr 8))) xor Key[25];
  1754. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1755. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1756. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1757. xor ((W3 shl 24) or (W3 shr 8))) xor Key[26];
  1758. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1759. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1760. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1761. xor ((W3 shl 24) or (W3 shr 8))) xor Key[27];
  1762. // round 9
  1763. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1764. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1765. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1766. xor ((W3 shl 24) or (W3 shr 8))) xor Key[20];
  1767. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1768. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1769. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1770. xor ((W3 shl 24) or (W3 shr 8))) xor Key[21];
  1771. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1772. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1773. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1774. xor ((W3 shl 24) or (W3 shr 8))) xor Key[22];
  1775. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1776. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1777. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1778. xor ((W3 shl 24) or (W3 shr 8))) xor Key[23];
  1779. // round 10
  1780. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1781. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1782. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1783. xor ((W3 shl 24) or (W3 shr 8))) xor Key[16];
  1784. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1785. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1786. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1787. xor ((W3 shl 24) or (W3 shr 8))) xor Key[17];
  1788. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1789. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1790. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1791. xor ((W3 shl 24) or (W3 shr 8))) xor Key[18];
  1792. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1793. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1794. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1795. xor ((W3 shl 24) or (W3 shr 8))) xor Key[19];
  1796. // round 11
  1797. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1798. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1799. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1800. xor ((W3 shl 24) or (W3 shr 8))) xor Key[12];
  1801. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1802. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1803. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1804. xor ((W3 shl 24) or (W3 shr 8))) xor Key[13];
  1805. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1806. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1807. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1808. xor ((W3 shl 24) or (W3 shr 8))) xor Key[14];
  1809. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1810. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1811. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1812. xor ((W3 shl 24) or (W3 shr 8))) xor Key[15];
  1813. // round 12
  1814. W0 := InverseTable[Byte(T1[0])]; W1 := InverseTable[Byte(T1[3] shr 8)];
  1815. W2 := InverseTable[Byte(T1[2] shr 16)]; W3 := InverseTable[Byte(T1[1] shr 24)];
  1816. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1817. xor ((W3 shl 24) or (W3 shr 8))) xor Key[8];
  1818. W0 := InverseTable[Byte(T1[1])]; W1 := InverseTable[Byte(T1[0] shr 8)];
  1819. W2 := InverseTable[Byte(T1[3] shr 16)]; W3 := InverseTable[Byte(T1[2] shr 24)];
  1820. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1821. xor ((W3 shl 24) or (W3 shr 8))) xor Key[9];
  1822. W0 := InverseTable[Byte(T1[2])]; W1 := InverseTable[Byte(T1[1] shr 8)];
  1823. W2 := InverseTable[Byte(T1[0] shr 16)]; W3 := InverseTable[Byte(T1[3] shr 24)];
  1824. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1825. xor ((W3 shl 24) or (W3 shr 8))) xor Key[10];
  1826. W0 := InverseTable[Byte(T1[3])]; W1 := InverseTable[Byte(T1[2] shr 8)];
  1827. W2 := InverseTable[Byte(T1[1] shr 16)]; W3 := InverseTable[Byte(T1[0] shr 24)];
  1828. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1829. xor ((W3 shl 24) or (W3 shr 8))) xor Key[11];
  1830. // round 13
  1831. W0 := InverseTable[Byte(T0[0])]; W1 := InverseTable[Byte(T0[3] shr 8)];
  1832. W2 := InverseTable[Byte(T0[2] shr 16)]; W3 := InverseTable[Byte(T0[1] shr 24)];
  1833. T1[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1834. xor ((W3 shl 24) or (W3 shr 8))) xor Key[4];
  1835. W0 := InverseTable[Byte(T0[1])]; W1 := InverseTable[Byte(T0[0] shr 8)];
  1836. W2 := InverseTable[Byte(T0[3] shr 16)]; W3 := InverseTable[Byte(T0[2] shr 24)];
  1837. T1[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1838. xor ((W3 shl 24) or (W3 shr 8))) xor Key[5];
  1839. W0 := InverseTable[Byte(T0[2])]; W1 := InverseTable[Byte(T0[1] shr 8)];
  1840. W2 := InverseTable[Byte(T0[0] shr 16)]; W3 := InverseTable[Byte(T0[3] shr 24)];
  1841. T1[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1842. xor ((W3 shl 24) or (W3 shr 8))) xor Key[6];
  1843. W0 := InverseTable[Byte(T0[3])]; W1 := InverseTable[Byte(T0[2] shr 8)];
  1844. W2 := InverseTable[Byte(T0[1] shr 16)]; W3 := InverseTable[Byte(T0[0] shr 24)];
  1845. T1[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1846. xor ((W3 shl 24) or (W3 shr 8))) xor Key[7];
  1847. // last round of transformations
  1848. W0 := LastInverseTable[Byte(T1[0])]; W1 := LastInverseTable[Byte(T1[3] shr 8)];
  1849. W2 := LastInverseTable[Byte(T1[2] shr 16)]; W3 := LastInverseTable[Byte(T1[1] shr 24)];
  1850. T0[0] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1851. xor ((W3 shl 24) or (W3 shr 8))) xor Key[0];
  1852. W0 := LastInverseTable[Byte(T1[1])]; W1 := LastInverseTable[Byte(T1[0] shr 8)];
  1853. W2 := LastInverseTable[Byte(T1[3] shr 16)]; W3 := LastInverseTable[Byte(T1[2] shr 24)];
  1854. T0[1] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1855. xor ((W3 shl 24) or (W3 shr 8))) xor Key[1];
  1856. W0 := LastInverseTable[Byte(T1[2])]; W1 := LastInverseTable[Byte(T1[1] shr 8)];
  1857. W2 := LastInverseTable[Byte(T1[0] shr 16)]; W3 := LastInverseTable[Byte(T1[3] shr 24)];
  1858. T0[2] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1859. xor ((W3 shl 24) or (W3 shr 8))) xor Key[2];
  1860. W0 := LastInverseTable[Byte(T1[3])]; W1 := LastInverseTable[Byte(T1[2] shr 8)];
  1861. W2 := LastInverseTable[Byte(T1[1] shr 16)]; W3 := LastInverseTable[Byte(T1[0] shr 24)];
  1862. T0[3] := (W0 xor ((W1 shl 8) or (W1 shr 24)) xor ((W2 shl 16) or (W2 shr 16))
  1863. xor ((W3 shl 24) or (W3 shr 8))) xor Key[3];
  1864. // finalizing
  1865. PLongWord(@OutBuf[0])^ := T0[0]; PLongWord(@OutBuf[4])^ := T0[1];
  1866. PLongWord(@OutBuf[8])^ := T0[2]; PLongWord(@OutBuf[12])^ := T0[3];
  1867. end;
  1868. // Stream encryption routines (ECB mode)
  1869. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  1870. const Key: TAESKey128; Dest: TStream);
  1871. var
  1872. ExpandedKey: TAESExpandedKey128;
  1873. begin
  1874. ExpandAESKeyForEncryption(Key, ExpandedKey);
  1875. EncryptAESStreamECB(Source, Count, ExpandedKey, Dest);
  1876. end;
  1877. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  1878. const Key: TAESKey192; Dest: TStream);
  1879. var
  1880. ExpandedKey: TAESExpandedKey192;
  1881. begin
  1882. ExpandAESKeyForEncryption(Key, ExpandedKey);
  1883. EncryptAESStreamECB(Source, Count, ExpandedKey, Dest);
  1884. end;
  1885. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  1886. const Key: TAESKey256; Dest: TStream);
  1887. var
  1888. ExpandedKey: TAESExpandedKey256;
  1889. begin
  1890. ExpandAESKeyForEncryption(Key, ExpandedKey);
  1891. EncryptAESStreamECB(Source, Count, ExpandedKey, Dest);
  1892. end;
  1893. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  1894. const ExpandedKey: TAESExpandedKey128; Dest: TStream);
  1895. var
  1896. TempIn, TempOut: TAESBuffer;
  1897. Done: cardinal;
  1898. begin
  1899. if Count = 0 then
  1900. begin
  1901. Source.Position := 0;
  1902. Count := Source.Size;
  1903. end
  1904. else Count := Min(Count, Source.Size - Source.Position);
  1905. if Count = 0 then exit;
  1906. while Count >= SizeOf(TAESBuffer) do
  1907. begin
  1908. Done := Source.Read(TempIn, SizeOf(TempIn));
  1909. if Done < SizeOf(TempIn) then
  1910. raise EStreamError.Create(SReadError);
  1911. EncryptAES(TempIn, ExpandedKey, TempOut);
  1912. Done := Dest.Write(TempOut, SizeOf(TempOut));
  1913. if Done < SizeOf(TempOut) then
  1914. raise EStreamError.Create(SWriteError);
  1915. Dec(Count, SizeOf(TAESBuffer));
  1916. end;
  1917. if Count > 0 then
  1918. begin
  1919. Done := Source.Read(TempIn, Count);
  1920. if Done < Count then
  1921. raise EStreamError.Create(SReadError);
  1922. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  1923. EncryptAES(TempIn, ExpandedKey, TempOut);
  1924. Done := Dest.Write(TempOut, SizeOf(TempOut));
  1925. if Done < SizeOf(TempOut) then
  1926. raise EStreamError.Create(SWriteError);
  1927. end;
  1928. end;
  1929. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  1930. const ExpandedKey: TAESExpandedKey192; Dest: TStream);
  1931. var
  1932. TempIn, TempOut: TAESBuffer;
  1933. Done: cardinal;
  1934. begin
  1935. if Count = 0 then
  1936. begin
  1937. Source.Position := 0;
  1938. Count := Source.Size;
  1939. end
  1940. else Count := Min(Count, Source.Size - Source.Position);
  1941. if Count = 0 then exit;
  1942. while Count >= SizeOf(TAESBuffer) do
  1943. begin
  1944. Done := Source.Read(TempIn, SizeOf(TempIn));
  1945. if Done < SizeOf(TempIn) then
  1946. raise EStreamError.Create(SReadError);
  1947. EncryptAES(TempIn, ExpandedKey, TempOut);
  1948. Done := Dest.Write(TempOut, SizeOf(TempOut));
  1949. if Done < SizeOf(TempOut) then
  1950. raise EStreamError.Create(SWriteError);
  1951. Dec(Count, SizeOf(TAESBuffer));
  1952. end;
  1953. if Count > 0 then
  1954. begin
  1955. Done := Source.Read(TempIn, Count);
  1956. if Done < Count then
  1957. raise EStreamError.Create(SReadError);
  1958. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  1959. EncryptAES(TempIn, ExpandedKey, TempOut);
  1960. Done := Dest.Write(TempOut, SizeOf(TempOut));
  1961. if Done < SizeOf(TempOut) then
  1962. raise EStreamError.Create(SWriteError);
  1963. end;
  1964. end;
  1965. procedure EncryptAESStreamECB(Source: TStream; Count: cardinal;
  1966. const ExpandedKey: TAESExpandedKey256; Dest: TStream);
  1967. var
  1968. TempIn, TempOut: TAESBuffer;
  1969. Done: cardinal;
  1970. begin
  1971. if Count = 0 then
  1972. begin
  1973. Source.Position := 0;
  1974. Count := Source.Size;
  1975. end
  1976. else Count := Min(Count, Source.Size - Source.Position);
  1977. if Count = 0 then exit;
  1978. while Count >= SizeOf(TAESBuffer) do
  1979. begin
  1980. Done := Source.Read(TempIn, SizeOf(TempIn));
  1981. if Done < SizeOf(TempIn) then
  1982. raise EStreamError.Create(SReadError);
  1983. EncryptAES(TempIn, ExpandedKey, TempOut);
  1984. Done := Dest.Write(TempOut, SizeOf(TempOut));
  1985. if Done < SizeOf(TempOut) then
  1986. raise EStreamError.Create(SWriteError);
  1987. Dec(Count, SizeOf(TAESBuffer));
  1988. end;
  1989. if Count > 0 then
  1990. begin
  1991. Done := Source.Read(TempIn, Count);
  1992. if Done < Count then
  1993. raise EStreamError.Create(SReadError);
  1994. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  1995. EncryptAES(TempIn, ExpandedKey, TempOut);
  1996. Done := Dest.Write(TempOut, SizeOf(TempOut));
  1997. if Done < SizeOf(TempOut) then
  1998. raise EStreamError.Create(SWriteError);
  1999. end;
  2000. end;
  2001. // Stream decryption routines (ECB mode)
  2002. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  2003. const Key: TAESKey128; Dest: TStream);
  2004. var
  2005. ExpandedKey: TAESExpandedKey128;
  2006. begin
  2007. ExpandAESKeyForDecryption(Key, ExpandedKey);
  2008. DecryptAESStreamECB(Source, Count, ExpandedKey, Dest);
  2009. end;
  2010. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  2011. const ExpandedKey: TAESExpandedKey128; Dest: TStream);
  2012. var
  2013. TempIn, TempOut: TAESBuffer;
  2014. Done: cardinal;
  2015. begin
  2016. if Count = 0 then
  2017. begin
  2018. Source.Position := 0;
  2019. Count := Source.Size;
  2020. end
  2021. else Count := Min(Count, Source.Size - Source.Position);
  2022. if Count = 0 then exit;
  2023. if (Count mod SizeOf(TAESBuffer)) > 0 then
  2024. raise EAESError.Create(SInvalidInBufSize);
  2025. while Count >= SizeOf(TAESBuffer) do
  2026. begin
  2027. Done := Source.Read(TempIn, SizeOf(TempIn));
  2028. if Done < SizeOf(TempIn) then
  2029. raise EStreamError.Create(SReadError);
  2030. DecryptAES(TempIn, ExpandedKey, TempOut);
  2031. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2032. if Done < SizeOf(TempOut) then
  2033. raise EStreamError.Create(SWriteError);
  2034. Dec(Count, SizeOf(TAESBuffer));
  2035. end;
  2036. end;
  2037. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  2038. const Key: TAESKey192; Dest: TStream);
  2039. var
  2040. ExpandedKey: TAESExpandedKey192;
  2041. begin
  2042. ExpandAESKeyForDecryption(Key, ExpandedKey);
  2043. DecryptAESStreamECB(Source, Count, ExpandedKey, Dest);
  2044. end;
  2045. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  2046. const ExpandedKey: TAESExpandedKey192; Dest: TStream);
  2047. var
  2048. TempIn, TempOut: TAESBuffer;
  2049. Done: cardinal;
  2050. begin
  2051. if Count = 0 then
  2052. begin
  2053. Source.Position := 0;
  2054. Count := Source.Size;
  2055. end
  2056. else Count := Min(Count, Source.Size - Source.Position);
  2057. if Count = 0 then exit;
  2058. if (Count mod SizeOf(TAESBuffer)) > 0 then
  2059. raise EAESError.Create(SInvalidInBufSize);
  2060. while Count >= SizeOf(TAESBuffer) do
  2061. begin
  2062. Done := Source.Read(TempIn, SizeOf(TempIn));
  2063. if Done < SizeOf(TempIn) then
  2064. raise EStreamError.Create(SReadError);
  2065. DecryptAES(TempIn, ExpandedKey, TempOut);
  2066. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2067. if Done < SizeOf(TempOut) then
  2068. raise EStreamError.Create(SWriteError);
  2069. Dec(Count, SizeOf(TAESBuffer));
  2070. end;
  2071. end;
  2072. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  2073. const Key: TAESKey256; Dest: TStream);
  2074. var
  2075. ExpandedKey: TAESExpandedKey256;
  2076. begin
  2077. ExpandAESKeyForDecryption(Key, ExpandedKey);
  2078. DecryptAESStreamECB(Source, Count, ExpandedKey, Dest);
  2079. end;
  2080. procedure DecryptAESStreamECB(Source: TStream; Count: cardinal;
  2081. const ExpandedKey: TAESExpandedKey256; Dest: TStream);
  2082. var
  2083. TempIn, TempOut: TAESBuffer;
  2084. Done: cardinal;
  2085. begin
  2086. if Count = 0 then
  2087. begin
  2088. Source.Position := 0;
  2089. Count := Source.Size;
  2090. end
  2091. else Count := Min(Count, Source.Size - Source.Position);
  2092. if Count = 0 then exit;
  2093. if (Count mod SizeOf(TAESBuffer)) > 0 then
  2094. raise EAESError.Create(SInvalidInBufSize);
  2095. while Count >= SizeOf(TAESBuffer) do
  2096. begin
  2097. Done := Source.Read(TempIn, SizeOf(TempIn));
  2098. if Done < SizeOf(TempIn) then
  2099. raise EStreamError.Create(SReadError);
  2100. DecryptAES(TempIn, ExpandedKey, TempOut);
  2101. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2102. if Done < SizeOf(TempOut) then
  2103. raise EStreamError.Create(SWriteError);
  2104. Dec(Count, SizeOf(TAESBuffer));
  2105. end;
  2106. end;
  2107. // Stream encryption routines (CBC mode)
  2108. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  2109. const Key: TAESKey128; const InitVector: TAESBuffer; Dest: TStream);
  2110. var
  2111. ExpandedKey: TAESExpandedKey128;
  2112. begin
  2113. ExpandAESKeyForEncryption(Key, ExpandedKey);
  2114. EncryptAESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
  2115. end;
  2116. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  2117. const ExpandedKey: TAESExpandedKey128; const InitVector: TAESBuffer;
  2118. Dest: TStream);
  2119. var
  2120. TempIn, TempOut, Vector: TAESBuffer;
  2121. Done: cardinal;
  2122. begin
  2123. if Count = 0 then
  2124. begin
  2125. Source.Position := 0;
  2126. Count := Source.Size;
  2127. end
  2128. else Count := Min(Count, Source.Size - Source.Position);
  2129. if Count = 0 then exit;
  2130. Vector := InitVector;
  2131. while Count >= SizeOf(TAESBuffer) do
  2132. begin
  2133. Done := Source.Read(TempIn, SizeOf(TempIn));
  2134. if Done < SizeOf(TempIn) then
  2135. raise EStreamError.Create(SReadError);
  2136. PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
  2137. PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
  2138. PLongWord(@TempIn[8])^ := PLongWord(@TempIn[8])^ xor PLongWord(@Vector[8])^;
  2139. PLongWord(@TempIn[12])^ := PLongWord(@TempIn[12])^ xor PLongWord(@Vector[12])^;
  2140. EncryptAES(TempIn, ExpandedKey, TempOut);
  2141. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2142. if Done < SizeOf(TempOut) then
  2143. raise EStreamError.Create(SWriteError);
  2144. Vector := TempOut;
  2145. Dec(Count, SizeOf(TAESBuffer));
  2146. end;
  2147. if Count > 0 then
  2148. begin
  2149. Done := Source.Read(TempIn, Count);
  2150. if Done < Count then
  2151. raise EStreamError.Create(SReadError);
  2152. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  2153. PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
  2154. PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
  2155. PLongWord(@TempIn[8])^ := PLongWord(@TempIn[8])^ xor PLongWord(@Vector[8])^;
  2156. PLongWord(@TempIn[12])^ := PLongWord(@TempIn[12])^ xor PLongWord(@Vector[12])^;
  2157. EncryptAES(TempIn, ExpandedKey, TempOut);
  2158. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2159. if Done < SizeOf(TempOut) then
  2160. raise EStreamError.Create(SWriteError);
  2161. end;
  2162. end;
  2163. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  2164. const Key: TAESKey192; const InitVector: TAESBuffer; Dest: TStream);
  2165. var
  2166. ExpandedKey: TAESExpandedKey192;
  2167. begin
  2168. ExpandAESKeyForEncryption(Key, ExpandedKey);
  2169. EncryptAESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
  2170. end;
  2171. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  2172. const ExpandedKey: TAESExpandedKey192; const InitVector: TAESBuffer;
  2173. Dest: TStream);
  2174. var
  2175. TempIn, TempOut, Vector: TAESBuffer;
  2176. Done: cardinal;
  2177. begin
  2178. if Count = 0 then
  2179. begin
  2180. Source.Position := 0;
  2181. Count := Source.Size;
  2182. end
  2183. else Count := Min(Count, Source.Size - Source.Position);
  2184. if Count = 0 then exit;
  2185. Vector := InitVector;
  2186. while Count >= SizeOf(TAESBuffer) do
  2187. begin
  2188. Done := Source.Read(TempIn, SizeOf(TempIn));
  2189. if Done < SizeOf(TempIn) then
  2190. raise EStreamError.Create(SReadError);
  2191. PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
  2192. PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
  2193. PLongWord(@TempIn[8])^ := PLongWord(@TempIn[8])^ xor PLongWord(@Vector[8])^;
  2194. PLongWord(@TempIn[12])^ := PLongWord(@TempIn[12])^ xor PLongWord(@Vector[12])^;
  2195. EncryptAES(TempIn, ExpandedKey, TempOut);
  2196. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2197. if Done < SizeOf(TempOut) then
  2198. raise EStreamError.Create(SWriteError);
  2199. Vector := TempOut;
  2200. Dec(Count, SizeOf(TAESBuffer));
  2201. end;
  2202. if Count > 0 then
  2203. begin
  2204. Done := Source.Read(TempIn, Count);
  2205. if Done < Count then
  2206. raise EStreamError.Create(SReadError);
  2207. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  2208. PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
  2209. PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
  2210. PLongWord(@TempIn[8])^ := PLongWord(@TempIn[8])^ xor PLongWord(@Vector[8])^;
  2211. PLongWord(@TempIn[12])^ := PLongWord(@TempIn[12])^ xor PLongWord(@Vector[12])^;
  2212. EncryptAES(TempIn, ExpandedKey, TempOut);
  2213. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2214. if Done < SizeOf(TempOut) then
  2215. raise EStreamError.Create(SWriteError);
  2216. end;
  2217. end;
  2218. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  2219. const Key: TAESKey256; const InitVector: TAESBuffer; Dest: TStream);
  2220. var
  2221. ExpandedKey: TAESExpandedKey256;
  2222. begin
  2223. ExpandAESKeyForEncryption(Key, ExpandedKey);
  2224. EncryptAESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
  2225. end;
  2226. procedure EncryptAESStreamCBC(Source: TStream; Count: cardinal;
  2227. const ExpandedKey: TAESExpandedKey256; const InitVector: TAESBuffer;
  2228. Dest: TStream);
  2229. var
  2230. TempIn, TempOut, Vector: TAESBuffer;
  2231. Done: cardinal;
  2232. begin
  2233. if Count = 0 then
  2234. begin
  2235. Source.Position := 0;
  2236. Count := Source.Size;
  2237. end
  2238. else Count := Min(Count, Source.Size - Source.Position);
  2239. if Count = 0 then exit;
  2240. Vector := InitVector;
  2241. while Count >= SizeOf(TAESBuffer) do
  2242. begin
  2243. Done := Source.Read(TempIn, SizeOf(TempIn));
  2244. if Done < SizeOf(TempIn) then
  2245. raise EStreamError.Create(SReadError);
  2246. PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
  2247. PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
  2248. PLongWord(@TempIn[8])^ := PLongWord(@TempIn[8])^ xor PLongWord(@Vector[8])^;
  2249. PLongWord(@TempIn[12])^ := PLongWord(@TempIn[12])^ xor PLongWord(@Vector[12])^;
  2250. EncryptAES(TempIn, ExpandedKey, TempOut);
  2251. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2252. if Done < SizeOf(TempOut) then
  2253. raise EStreamError.Create(SWriteError);
  2254. Vector := TempOut;
  2255. Dec(Count, SizeOf(TAESBuffer));
  2256. end;
  2257. if Count > 0 then
  2258. begin
  2259. Done := Source.Read(TempIn, Count);
  2260. if Done < Count then
  2261. raise EStreamError.Create(SReadError);
  2262. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  2263. PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
  2264. PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
  2265. PLongWord(@TempIn[8])^ := PLongWord(@TempIn[8])^ xor PLongWord(@Vector[8])^;
  2266. PLongWord(@TempIn[12])^ := PLongWord(@TempIn[12])^ xor PLongWord(@Vector[12])^;
  2267. EncryptAES(TempIn, ExpandedKey, TempOut);
  2268. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2269. if Done < SizeOf(TempOut) then
  2270. raise EStreamError.Create(SWriteError);
  2271. end;
  2272. end;
  2273. // Stream decryption routines (CBC mode)
  2274. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  2275. const Key: TAESKey128; const InitVector: TAESBuffer; Dest: TStream);
  2276. var
  2277. ExpandedKey: TAESExpandedKey128;
  2278. begin
  2279. ExpandAESKeyForDecryption(Key, ExpandedKey);
  2280. DecryptAESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
  2281. end;
  2282. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  2283. const ExpandedKey: TAESExpandedKey128; const InitVector: TAESBuffer;
  2284. Dest: TStream);
  2285. var
  2286. TempIn, TempOut: TAESBuffer;
  2287. Vector1, Vector2: TAESBuffer;
  2288. Done: cardinal;
  2289. begin
  2290. if Count = 0 then
  2291. begin
  2292. Source.Position := 0;
  2293. Count := Source.Size;
  2294. end
  2295. else Count := Min(Count, Source.Size - Source.Position);
  2296. if Count = 0 then exit;
  2297. if (Count mod SizeOf(TAESBuffer)) > 0 then
  2298. raise EAESError.Create(SInvalidInBufSize);
  2299. Vector1 := InitVector;
  2300. while Count >= SizeOf(TAESBuffer) do
  2301. begin
  2302. Done := Source.Read(TempIn, SizeOf(TempIn));
  2303. if Done < SizeOf(TempIn) then
  2304. raise EStreamError(SReadError);
  2305. Vector2 := TempIn;
  2306. DecryptAES(TempIn, ExpandedKey, TempOut);
  2307. PLongWord(@TempOut[0])^ := PLongWord(@TempOut[0])^ xor PLongWord(@Vector1[0])^;
  2308. PLongWord(@TempOut[4])^ := PLongWord(@TempOut[4])^ xor PLongWord(@Vector1[4])^;
  2309. PLongWord(@TempOut[8])^ := PLongWord(@TempOut[8])^ xor PLongWord(@Vector1[8])^;
  2310. PLongWord(@TempOut[12])^ := PLongWord(@TempOut[12])^ xor PLongWord(@Vector1[12])^;
  2311. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2312. if Done < SizeOf(TempOut) then
  2313. raise EStreamError(SWriteError);
  2314. Vector1 := Vector2;
  2315. Dec(Count, SizeOf(TAESBuffer));
  2316. end;
  2317. end;
  2318. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  2319. const Key: TAESKey192; const InitVector: TAESBuffer; Dest: TStream);
  2320. var
  2321. ExpandedKey: TAESExpandedKey192;
  2322. begin
  2323. ExpandAESKeyForDecryption(Key, ExpandedKey);
  2324. DecryptAESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
  2325. end;
  2326. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  2327. const ExpandedKey: TAESExpandedKey192; const InitVector: TAESBuffer;
  2328. Dest: TStream);
  2329. var
  2330. TempIn, TempOut: TAESBuffer;
  2331. Vector1, Vector2: TAESBuffer;
  2332. Done: cardinal;
  2333. begin
  2334. if Count = 0 then
  2335. begin
  2336. Source.Position := 0;
  2337. Count := Source.Size;
  2338. end
  2339. else Count := Min(Count, Source.Size - Source.Position);
  2340. if Count = 0 then exit;
  2341. if (Count mod SizeOf(TAESBuffer)) > 0 then
  2342. raise EAESError.Create(SInvalidInBufSize);
  2343. Vector1 := InitVector;
  2344. while Count >= SizeOf(TAESBuffer) do
  2345. begin
  2346. Done := Source.Read(TempIn, SizeOf(TempIn));
  2347. if Done < SizeOf(TempIn) then
  2348. raise EStreamError(SReadError);
  2349. Vector2 := TempIn;
  2350. DecryptAES(TempIn, ExpandedKey, TempOut);
  2351. PLongWord(@TempOut[0])^ := PLongWord(@TempOut[0])^ xor PLongWord(@Vector1[0])^;
  2352. PLongWord(@TempOut[4])^ := PLongWord(@TempOut[4])^ xor PLongWord(@Vector1[4])^;
  2353. PLongWord(@TempOut[8])^ := PLongWord(@TempOut[8])^ xor PLongWord(@Vector1[8])^;
  2354. PLongWord(@TempOut[12])^ := PLongWord(@TempOut[12])^ xor PLongWord(@Vector1[12])^;
  2355. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2356. if Done < SizeOf(TempOut) then
  2357. raise EStreamError(SWriteError);
  2358. Vector1 := Vector2;
  2359. Dec(Count, SizeOf(TAESBuffer));
  2360. end;
  2361. end;
  2362. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  2363. const Key: TAESKey256; const InitVector: TAESBuffer; Dest: TStream);
  2364. var
  2365. ExpandedKey: TAESExpandedKey256;
  2366. begin
  2367. ExpandAESKeyForDecryption(Key, ExpandedKey);
  2368. DecryptAESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
  2369. end;
  2370. procedure DecryptAESStreamCBC(Source: TStream; Count: cardinal;
  2371. const ExpandedKey: TAESExpandedKey256; const InitVector: TAESBuffer;
  2372. Dest: TStream);
  2373. var
  2374. TempIn, TempOut: TAESBuffer;
  2375. Vector1, Vector2: TAESBuffer;
  2376. Done: cardinal;
  2377. begin
  2378. if Count = 0 then
  2379. begin
  2380. Source.Position := 0;
  2381. Count := Source.Size;
  2382. end
  2383. else Count := Min(Count, Source.Size - Source.Position);
  2384. if Count = 0 then exit;
  2385. if (Count mod SizeOf(TAESBuffer)) > 0 then
  2386. raise EAESError.Create(SInvalidInBufSize);
  2387. Vector1 := InitVector;
  2388. while Count >= SizeOf(TAESBuffer) do
  2389. begin
  2390. Done := Source.Read(TempIn, SizeOf(TempIn));
  2391. if Done < SizeOf(TempIn) then
  2392. raise EStreamError(SReadError);
  2393. Vector2 := TempIn;
  2394. DecryptAES(TempIn, ExpandedKey, TempOut);
  2395. PLongWord(@TempOut[0])^ := PLongWord(@TempOut[0])^ xor PLongWord(@Vector1[0])^;
  2396. PLongWord(@TempOut[4])^ := PLongWord(@TempOut[4])^ xor PLongWord(@Vector1[4])^;
  2397. PLongWord(@TempOut[8])^ := PLongWord(@TempOut[8])^ xor PLongWord(@Vector1[8])^;
  2398. PLongWord(@TempOut[12])^ := PLongWord(@TempOut[12])^ xor PLongWord(@Vector1[12])^;
  2399. Done := Dest.Write(TempOut, SizeOf(TempOut));
  2400. if Done < SizeOf(TempOut) then
  2401. raise EStreamError(SWriteError);
  2402. Vector1 := Vector2;
  2403. Dec(Count, SizeOf(TAESBuffer));
  2404. end;
  2405. end;
  2406. function AESEncryptBuffer128(dest: Pchar; destLen: PLongint;
  2407. source: Pchar; Count: Longint; const strKey: string): integer;
  2408. var
  2409. Key: TAESKey128;
  2410. ExpandedKey: TAESExpandedKey128;
  2411. TempIn, TempOut: TAESBuffer;//TAESBuffer = array [0..15] of byte;
  2412. begin
  2413. Result := 0;
  2414. if Count = 0 then exit;
  2415. // Prepare key...
  2416. FillChar(Key, SizeOf(Key), 0);
  2417. Move(PChar(strKey)^, Key, Min(SizeOf(Key), Length(strKey)));
  2418. ExpandAESKeyForEncryption(Key, ExpandedKey);
  2419. destLen^:=0;
  2420. while Count >= SizeOf(TAESBuffer) do
  2421. begin
  2422. Move(source^,TempIn, SizeOf(TempIn));
  2423. EncryptAES(TempIn, ExpandedKey, TempOut);
  2424. Move(TempOut,Pchar(dest)^, SizeOf(TempOut));
  2425. Dec(Count, SizeOf(TAESBuffer));
  2426. Inc(source,SizeOf(TAESBuffer));
  2427. Inc(dest,SizeOf(TAESBuffer));
  2428. Inc(destLen^,SizeOf(TAESBuffer));
  2429. end;
  2430. if Count > 0 then
  2431. begin
  2432. Move(source^,TempIn, count);
  2433. FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
  2434. EncryptAES(TempIn, ExpandedKey, TempOut);
  2435. Move(TempOut,Pchar(dest)^, SizeOf(TempOut));
  2436. Inc(destLen^,SizeOf(TAESBuffer));
  2437. end;
  2438. end;
  2439. function AESDecryptBuffer128(dest: Pchar; destLen: PLongint;
  2440. source: Pchar; Count: Longint; const strKey: string): integer;
  2441. var
  2442. Key: TAESKey128;
  2443. ExpandedKey: TAESExpandedKey128;
  2444. TempIn, TempOut: TAESBuffer;//TAESBuffer = array [0..15] of byte;
  2445. begin
  2446. Result := 0;
  2447. if Count = 0 then exit;
  2448. if (Count mod SizeOf(TAESBuffer)) > 0 then Exit;
  2449. // Prepare key...
  2450. FillChar(Key, SizeOf(Key), 0);
  2451. Move(PChar(strKey)^, Key, Min(SizeOf(Key), Length(strKey)));
  2452. ExpandAESKeyForEncryption(Key, ExpandedKey);
  2453. ExpandAESKeyForDecryption(ExpandedKey);
  2454. destLen^:=0;
  2455. while Count >= SizeOf(TAESBuffer) do
  2456. begin
  2457. CopyMemory(@TempIn,source+destLen^, SizeOf(TempIn));
  2458. DecryptAES(TempIn, ExpandedKey, TempOut);
  2459. CopyMemory(dest+destLen^,@TempOut, SizeOf(TempOut));
  2460. Dec(Count, SizeOf(TAESBuffer));
  2461. Inc(destLen^,SizeOf(TAESBuffer));
  2462. end;
  2463. end;
  2464. end.