CryptoTestCiphers.Mod 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. MODULE CryptoTestCiphers; (** AUTHOR "F.N."; PURPOSE "Ciphers Test"; *)
  2. IMPORT
  3. U := CryptoUtils, Ciphers := CryptoCiphers, Kernel, Commands, Out := KernelLog;
  4. PROCEDURE Ecb1*(context : Commands.Context);
  5. VAR
  6. hex, bindata, binkey, modname, orig: ARRAY 64 OF CHAR; cipher: Ciphers.Cipher;
  7. keybits: LONGINT;
  8. BEGIN
  9. (* read in the parameter *)
  10. context.arg.SkipWhitespace; context.arg.String(modname);
  11. context.arg.SkipWhitespace; context.arg.Int(keybits, FALSE);
  12. (* encryption *)
  13. cipher := Ciphers.NewCipher( modname );
  14. hex := "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
  15. U.Hex2Bin( hex, 0, binkey, 0, 24 ); U.Hex2Bin( hex, 0, bindata, 0, 8 );
  16. orig := bindata; orig[8] := 0X;
  17. cipher.InitKey( binkey, keybits );
  18. Out.Ln; Out.String( "*********************************" );
  19. Out.Ln; Out.String( "Encrypt-Decrypt-Test in ECB-mode: " ); Out.String( cipher.name );
  20. Out.Ln; Out.String( "Key: " ); U.PrintHex( binkey, 0, keybits DIV 8 );
  21. Out.Ln; Out.String( "Original: " ); U.PrintHex( bindata, 0, 8 );
  22. cipher.Encrypt( bindata, 0, 8 );
  23. Out.Ln; Out.String( "Encrypted: " ); U.PrintHex( bindata, 0, 8 );
  24. (* decryption *)
  25. cipher.Decrypt( bindata, 0, 8 );
  26. Out.Ln; Out.String( "Decrypted: " ); U.PrintHex( bindata, 0, 8 );
  27. bindata[8] := 0X;
  28. Out.Ln;
  29. IF bindata = orig THEN Out.String( "OK" ) ELSE Out.String( "FAIL" ) END;
  30. Out.Ln
  31. END Ecb1;
  32. PROCEDURE Cbc1*(context : Commands.Context);
  33. VAR
  34. hex, bindata, binkey, modname, iv, orig: ARRAY 64 OF CHAR; cipher: Ciphers.Cipher;
  35. keybits: LONGINT;
  36. BEGIN
  37. (* read in the parameter *)
  38. context.arg.SkipWhitespace; context.arg.String(modname);
  39. context.arg.SkipWhitespace; context.arg.Int(keybits, FALSE);
  40. (* encryption *)
  41. cipher := Ciphers.NewCipher( modname );
  42. hex := "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
  43. U.Hex2Bin( hex, 0, binkey, 0, 24 ); U.Hex2Bin( hex, 0, bindata, 0, 8 );
  44. cipher.InitKey( binkey, keybits );
  45. U.RandomBytes( iv, 0, cipher.blockSize );
  46. cipher.SetIV( iv, Ciphers.CBC );
  47. Out.Ln; Out.String( "*********************************" );
  48. Out.Ln; Out.String( "Encrypt-Decrypt-Test in CBC-mode: " ); Out.String( cipher.name );
  49. Out.Ln; Out.String( "Key: " ); U.PrintHex( binkey, 0, keybits DIV 8 );
  50. Out.Ln; Out.String( "Original: " ); U.PrintHex( bindata, 0, 8 );
  51. orig := bindata; orig[8] := 0X;
  52. cipher.Encrypt( bindata, 0, 8 );
  53. Out.Ln; Out.String( "Encrypted: " ); U.PrintHex( bindata, 0, 8 );
  54. (* decryption *)
  55. cipher.SetIV( iv, Ciphers.CBC );
  56. cipher.Decrypt( bindata, 0, 8 );
  57. Out.Ln; Out.String( "Decrypted: " ); U.PrintHex( bindata, 0, 8 );
  58. bindata[8] := 0X;
  59. Out.Ln;
  60. IF bindata = orig THEN Out.String( "OK" ) ELSE Out.String( "FAIL" ) END;
  61. Out.Ln
  62. END Cbc1;
  63. PROCEDURE Ctr1*(context : Commands.Context);
  64. VAR
  65. hex, bindata, binkey, modname, iv, orig: ARRAY 64 OF CHAR; cipher: Ciphers.Cipher;
  66. keybits: LONGINT;
  67. BEGIN
  68. (* read in the parameter *)
  69. context.arg.SkipWhitespace; context.arg.String(modname);
  70. context.arg.SkipWhitespace; context.arg.Int(keybits, FALSE);
  71. (* encryption *)
  72. cipher := Ciphers.NewCipher( modname );
  73. hex := "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
  74. U.Hex2Bin( hex, 0, binkey, 0, 24 ); U.Hex2Bin( hex, 0, bindata, 0, 16 );
  75. cipher.InitKey( binkey, keybits );
  76. U.RandomBytes( iv, 0, cipher.blockSize );
  77. cipher.SetIV( iv, Ciphers.CTR );
  78. Out.Ln; Out.String( "*********************************" );
  79. Out.Ln; Out.String( "Encrypt-Decrypt-Test in CTR-mode: " ); Out.String( cipher.name );
  80. Out.Ln; Out.String( "Key: " ); U.PrintHex( binkey, 0, keybits DIV 8 );
  81. Out.Ln; Out.String( "Original: " ); U.PrintHex( bindata, 0, 16 );
  82. orig := bindata; orig[16] := 0X;
  83. cipher.Encrypt( bindata, 0, 16 );
  84. Out.Ln; Out.String( "Encrypted: " ); U.PrintHex( bindata, 0, 16 );
  85. (* decryption *)
  86. cipher.InitKey( binkey, keybits );
  87. cipher.SetIV( iv, Ciphers.CTR );
  88. cipher.Decrypt( bindata, 0, 16 );
  89. Out.Ln; Out.String( "Decrypted: " ); U.PrintHex( bindata, 0, 16 );
  90. bindata[16] := 0X;
  91. Out.Ln;
  92. IF bindata = orig THEN Out.String( "OK" ) ELSE Out.String( "FAIL" ) END;
  93. Out.Ln
  94. END Ctr1;
  95. (** encrypt input with key (ebc-mode). output is a testvector *)
  96. PROCEDURE Ecb2*( CONST modname, input, output, key: ARRAY OF CHAR; datalen, keybits: LONGINT );
  97. VAR
  98. cipher: Ciphers.Cipher;
  99. temp1, temp2: ARRAY 256 OF CHAR;
  100. BEGIN
  101. cipher := Ciphers.NewCipher( modname );
  102. U.Hex2Bin( key, 0, temp1, 0, keybits DIV 8 );
  103. cipher.InitKey( temp1, keybits );
  104. Out.Ln; Out.String( "*********************************" );
  105. Out.Ln; Out.String( "Encryption-Test: " ); Out.String( cipher.name );
  106. Out.Ln; Out.String( "Key: " ); U.PrintHex( temp1, 0, keybits DIV 8 );
  107. U.Hex2Bin( input, 0, temp1, 0, datalen );
  108. Out.Ln; Out.String( "plaintext: " ); U.PrintHex( temp1, 0, datalen );
  109. cipher.Encrypt( temp1, 0, datalen );
  110. Out.Ln; Out.String( "encryption: " ); U.PrintHex( temp1, 0, datalen );
  111. U.Hex2Bin( output, 0, temp2, 0, datalen );
  112. Out.Ln; Out.String( "correct encryption: " ); U.PrintHex( temp2, 0, datalen );
  113. Out.Ln;
  114. temp1[datalen] := 0X; temp2[datalen] := 0X;
  115. IF temp1 = temp2 THEN Out.String( "OK" ) ELSE Out.String( "FAIL" ) END;
  116. Out.Ln
  117. END Ecb2;
  118. (** encrypt input with key (cbc-mode). output is a testvector *)
  119. PROCEDURE Cbc2*( CONST modname, input, output, key, iv: ARRAY OF CHAR; datalen, keybits: LONGINT );
  120. VAR
  121. cipher: Ciphers.Cipher;
  122. temp1, temp2: ARRAY 64 OF CHAR;
  123. BEGIN
  124. cipher := Ciphers.NewCipher( modname );
  125. U.Hex2Bin( key, 0, temp1, 0, keybits DIV 8 );
  126. cipher.InitKey( temp1, keybits );
  127. U.Hex2Bin( iv, 0, temp2, 0, cipher.blockSize );
  128. cipher.SetIV( temp2, Ciphers.CBC );
  129. Out.Ln; Out.String( "*********************************" );
  130. Out.Ln; Out.String( "Encryption-Test: " ); Out.String( cipher.name );
  131. Out.Ln; Out.String( "Key: " ); U.PrintHex( temp1, 0, keybits DIV 8 );
  132. U.Hex2Bin( input, 0, temp1, 0, datalen );
  133. Out.Ln; Out.String( "plaintext: " ); U.PrintHex( temp1, 0, datalen );
  134. cipher.Encrypt( temp1, 0, datalen );
  135. Out.Ln; Out.String( "encryption: " ); U.PrintHex( temp1, 0, datalen );
  136. U.Hex2Bin( output, 0, temp2, 0, datalen );
  137. Out.Ln; Out.String( "correct encryption: " ); U.PrintHex( temp2, 0, datalen );
  138. Out.Ln;
  139. temp1[datalen] := 0X; temp2[datalen] := 0X;
  140. IF temp1 = temp2 THEN Out.String( "OK" ) ELSE Out.String( "FAIL" ) END;
  141. Out.Ln
  142. END Cbc2;
  143. (** encrypt input with key (counter-mode). output is a testvector *)
  144. PROCEDURE Ctr2*( CONST modname, input, output, key, iv: ARRAY OF CHAR; datalen, keybits: LONGINT );
  145. VAR
  146. cipher: Ciphers.Cipher;
  147. temp1, temp2: ARRAY 64 OF CHAR;
  148. BEGIN
  149. cipher := Ciphers.NewCipher( modname );
  150. U.Hex2Bin( key, 0, temp1, 0, keybits DIV 8 );
  151. cipher.InitKey( temp1, keybits );
  152. U.Hex2Bin( iv, 0, temp2, 0, cipher.blockSize );
  153. cipher.SetIV( temp2, Ciphers.CTR );
  154. Out.Ln; Out.String( "*********************************" );
  155. Out.Ln; Out.String( "Encryption-Test: " ); Out.String( cipher.name );
  156. Out.Ln; Out.String( "Key: " ); U.PrintHex( temp1, 0, keybits DIV 8 );
  157. U.Hex2Bin( input, 0, temp1, 0, datalen );
  158. Out.Ln; Out.String( "plaintext: " ); U.PrintHex( temp1, 0, datalen );
  159. cipher.Encrypt( temp1, 0, datalen );
  160. Out.Ln; Out.String( "encryption: " ); U.PrintHex( temp1, 0, datalen );
  161. U.Hex2Bin( output, 0, temp2, 0, datalen );
  162. Out.Ln; Out.String( "correct encryption: " ); U.PrintHex( temp2, 0, datalen );
  163. Out.Ln;
  164. temp1[datalen] := 0X; temp2[datalen] := 0X;
  165. IF temp1 = temp2 THEN Out.String( "OK" ) ELSE Out.String( "FAIL" ) END;
  166. Out.Ln
  167. END Ctr2;
  168. PROCEDURE MeasureTime*(context : Commands.Context);
  169. VAR
  170. buf, key: ARRAY 1024 OF CHAR;
  171. milliTimer : Kernel.MilliTimer;
  172. i, j, k, t, keybits: LONGINT;
  173. c: Ciphers.Cipher;
  174. modname, mode, iv: ARRAY 64 OF CHAR;
  175. BEGIN
  176. (* read in the parameter *)
  177. context.arg.SkipWhitespace; context.arg.String(modname);
  178. context.arg.SkipWhitespace; context.arg.String(mode);
  179. context.arg.SkipWhitespace; context.arg.Int(keybits, FALSE);
  180. (* measure time *)
  181. FOR i := 0 TO 1023 DO buf[i] := 'a' END;
  182. c := Ciphers.NewCipher( modname );
  183. c.InitKey( key, keybits );
  184. IF mode = "CBC" THEN c.SetIV( iv, Ciphers.CBC ) END;
  185. Out.Ln; Out.String( "***********************************" );
  186. Out.Ln; Out.String( "Encrypting 100 MB with " ); Out.String( c.name );
  187. Kernel.SetTimer(milliTimer, 0);
  188. FOR k := 0 TO 9 DO
  189. Out.String( "." );
  190. FOR j := 0 TO 9 DO
  191. FOR i := 0 TO 999 DO c.Encrypt( buf, 0, 1024 ) END(* 100 MB data *)
  192. END
  193. END;
  194. t := Kernel.Elapsed(milliTimer);
  195. Out.Int( t, 4 ); Out.String( " ms" ); Out.Ln;
  196. END MeasureTime;
  197. PROCEDURE DesEcb2*;
  198. VAR input, output, key: ARRAY 64 OF CHAR;
  199. BEGIN
  200. key := "0123456789ABCDEF";
  201. input := "4E6F772069732074"; output := "3FA40E8A984D4815";
  202. Ecb2( "CryptoDES", input, output, key, 8, 64 );
  203. END DesEcb2;
  204. PROCEDURE Des3Ecb2*;
  205. VAR input, output, key: ARRAY 64 OF CHAR;
  206. BEGIN
  207. key := "0123456789ABCDEF23456789ABCDEF01456789ABCDEF0123";
  208. input := "4E6F772069732074"; output := "314F8327FA7A09A8";
  209. Ecb2( "CryptoDES3", input, output, key, 8, 192 );
  210. END Des3Ecb2;
  211. PROCEDURE IdeaEcb2*;
  212. VAR input, output, key: ARRAY 64 OF CHAR;
  213. BEGIN
  214. key := "00010002000300040005000600070008";
  215. input := "0000000100020003"; output := "11FBED2B01986DE5";
  216. Ecb2( "CryptoIDEA", input, output, key, 8, 128 );
  217. END IdeaEcb2;
  218. PROCEDURE AesEcb2*;
  219. VAR input, output, key: ARRAY 128 OF CHAR;
  220. BEGIN
  221. key := "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b";
  222. input := "6bc1bee22e409f96e93d7e117393172a";
  223. output := "bd334f1d6e45f25ff712a214571fa5cc";
  224. Ecb2( "CryptoAES", input, output, key, 16, 192 );
  225. key := "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4";
  226. input := "6bc1bee22e409f96e93d7e117393172a";
  227. output := "f3eed1bdb5d2a03c064b5a7e3db181f8";
  228. Ecb2( "CryptoAES", input, output, key, 16, 256 );
  229. END AesEcb2;
  230. PROCEDURE Arc4Ecb2*;
  231. VAR input, output, key: ARRAY 64 OF CHAR;
  232. BEGIN
  233. key := "0123456789abcdef";
  234. input := "0123456789abcdef"; output := "75b7878099e0c596";
  235. Ecb2( "CryptoARC4", input, output, key, 8, 64 );
  236. END Arc4Ecb2;
  237. PROCEDURE CastEcb2*;
  238. VAR input, output, key: ARRAY 64 OF CHAR;
  239. BEGIN
  240. key := "0123456712345678234567893456789A";
  241. input := "0123456789abcdef"; output := "238B4FE5847E44B2";
  242. Ecb2( "CryptoCAST", input, output, key, 8, 128 );
  243. output := "EB6A711A2C02271B";
  244. Ecb2( "CryptoCAST", input, output, key, 8, 80 );
  245. output := "7AC816D16E9B302E";
  246. Ecb2( "CryptoCAST", input, output, key, 8, 40 );
  247. END CastEcb2;
  248. PROCEDURE DesCbc2*;
  249. VAR input, output, key, iv: ARRAY 64 OF CHAR;
  250. BEGIN
  251. key := "0123456789ABCDEF";
  252. iv := "0123456789ABCDEF";
  253. input := "4E6F772069732074"; output := "96C3D4A6DC1C0117";
  254. Cbc2( "CryptoDES", input, output, key, iv, 8, 64 );
  255. END DesCbc2;
  256. PROCEDURE IdeaCbc2*;
  257. VAR input, output, key, iv: ARRAY 64 OF CHAR;
  258. BEGIN
  259. key := "00010002000300040005000600070008";
  260. iv := "0000000000000000";
  261. input := "0000000100020003"; output := "11FBED2B01986DE5";
  262. Cbc2( "CryptoIDEA", input, output, key, iv, 8, 128 );
  263. END IdeaCbc2;
  264. PROCEDURE AesCbc2*;
  265. VAR input, output, key, iv: ARRAY 256 OF CHAR;
  266. BEGIN
  267. key := "2b7e151628aed2a6abf7158809cf4f3c";
  268. iv := "000102030405060708090A0B0C0D0E0F";
  269. input := "6bc1bee22e409f96e93d7e117393172a";
  270. output := "7649abac8119b246cee98e9b12e9197d";
  271. Cbc2( "CryptoAES", input, output, key, iv, 16, 128 );
  272. key := "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4";
  273. iv := "000102030405060708090A0B0C0D0E0F";
  274. input := "6bc1bee22e409f96e93d7e117393172a";
  275. output := "f58c4c04d6e5f1ba779eabfb5f7bfbd6";
  276. Cbc2( "CryptoAES", input, output, key, iv, 16, 256 );
  277. END AesCbc2;
  278. PROCEDURE AesCtr2*;
  279. VAR input, output, key, iv: ARRAY 256 OF CHAR;
  280. BEGIN
  281. key := "2b7e151628aed2a6abf7158809cf4f3c";
  282. iv := "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
  283. input := "6bc1bee22e409f96e93d7e117393172a";
  284. output := "874d6191b620e3261bef6864990db6ce";
  285. Ctr2( "CryptoAES", input, output, key, iv, 16, 128 );
  286. key := "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4";
  287. iv := "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
  288. input := "6bc1bee22e409f96e93d7e117393172a";
  289. output := "601ec313775789a5b7a7f504bbf3d228";
  290. Ctr2( "CryptoAES", input, output, key, iv, 16, 256 );
  291. END AesCtr2;
  292. END CryptoTestCiphers.
  293. SystemTools.Free
  294. CryptoTestCiphers CryptoDES3 CryptoDES CryptoIDEA
  295. CryptoARC4 CryptoCAST CryptoAES ~
  296. CryptoTestCiphers.DesEcb2 ~
  297. CryptoTestCiphers.Des3Ecb2 ~
  298. CryptoTestCiphers.IdeaEcb2 ~
  299. CryptoTestCiphers.Arc4Ecb2 ~
  300. CryptoTestCiphers.BlowfishEcb2 ~
  301. CryptoTestCiphers.TwofishEcb2 ~
  302. CryptoTestCiphers.CastEcb2 ~
  303. CryptoTestCiphers.AesEcb2 ~
  304. CryptoTestCiphers.DesCbc2 ~
  305. CryptoTestCiphers.IdeaCbc2 ~
  306. CryptoTestCiphers.AesCbc2 ~
  307. CryptoTestCiphers.AesCtr2 ~
  308. CryptoTestCiphers.Ecb1 CryptoDES 64 ~
  309. CryptoTestCiphers.Ecb1 CryptoDES3 192 ~
  310. CryptoTestCiphers.Ecb1 CryptoIDEA 128 ~
  311. CryptoTestCiphers.Ecb1 CryptoARC4 128 ~
  312. CryptoTestCiphers.Ecb1 CryptoCAST 128 ~
  313. CryptoTestCiphers.Cbc1 CryptoDES 64 ~
  314. CryptoTestCiphers.Cbc1 CryptoDES3 192 ~
  315. CryptoTestCiphers.Cbc1 CryptoIDEA 128 ~
  316. CryptoTestCiphers.Ctr1 CryptoAES 128 ~
  317. CryptoTestCiphers.Ctr1 CryptoAES 256 ~
  318. CryptoTestCiphers.MeasureTime CryptoDES ECB 64 ~
  319. CryptoTestCiphers.MeasureTime CryptoDES CBC 64 ~
  320. CryptoTestCiphers.MeasureTime CryptoDES3 ECB 192 ~
  321. CryptoTestCiphers.MeasureTime CryptoDES3 CBC 192 ~
  322. CryptoTestCiphers.MeasureTime CryptoAES ECB 128 ~
  323. CryptoTestCiphers.MeasureTime CryptoAES CBC 128 ~
  324. CryptoTestCiphers.MeasureTime CryptoIDEA ECB 128 ~
  325. CryptoTestCiphers.MeasureTime CryptoIDEA CBC 128 ~
  326. CryptoTestCiphers.MeasureTime CryptoARC4 ECB 128 ~