Advertisement
programcreator

Add-On Programs: AES by KillaVanilla

Feb 21st, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 41.44 KB | None | 0 0
  1. -- AES implementation
  2. -- By KillaVanilla
  3.  
  4. local sbox = {
  5. [0]=0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  6. 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  7. 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  8. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  9. 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  10. 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  11. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  12. 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  13. 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  14. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  15. 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  16. 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  17. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  18. 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  19. 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  20. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16}
  21.  
  22. local inv_sbox = {
  23. [0]=0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  24. 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  25. 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  26. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  27. 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  28. 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  29. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  30. 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  31. 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  32. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  33. 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  34. 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  35. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  36. 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  37. 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  38. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D}
  39.  
  40. local Rcon = {
  41. [0]=0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
  42. 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
  43. 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
  44. 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
  45. 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
  46. 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
  47. 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
  48. 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
  49. 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
  50. 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
  51. 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
  52. 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
  53. 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
  54. 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
  55. 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
  56. 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d}
  57.  
  58. -- Finite-field multiplication lookup tables:
  59.  
  60. local mul_2 = {
  61. [0]=0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
  62. 0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
  63. 0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
  64. 0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
  65. 0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
  66. 0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
  67. 0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
  68. 0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
  69. 0x1b,0x19,0x1f,0x1d,0x13,0x11,0x17,0x15,0x0b,0x09,0x0f,0x0d,0x03,0x01,0x07,0x05,
  70. 0x3b,0x39,0x3f,0x3d,0x33,0x31,0x37,0x35,0x2b,0x29,0x2f,0x2d,0x23,0x21,0x27,0x25,
  71. 0x5b,0x59,0x5f,0x5d,0x53,0x51,0x57,0x55,0x4b,0x49,0x4f,0x4d,0x43,0x41,0x47,0x45,
  72. 0x7b,0x79,0x7f,0x7d,0x73,0x71,0x77,0x75,0x6b,0x69,0x6f,0x6d,0x63,0x61,0x67,0x65,
  73. 0x9b,0x99,0x9f,0x9d,0x93,0x91,0x97,0x95,0x8b,0x89,0x8f,0x8d,0x83,0x81,0x87,0x85,
  74. 0xbb,0xb9,0xbf,0xbd,0xb3,0xb1,0xb7,0xb5,0xab,0xa9,0xaf,0xad,0xa3,0xa1,0xa7,0xa5,
  75. 0xdb,0xd9,0xdf,0xdd,0xd3,0xd1,0xd7,0xd5,0xcb,0xc9,0xcf,0xcd,0xc3,0xc1,0xc7,0xc5,
  76. 0xfb,0xf9,0xff,0xfd,0xf3,0xf1,0xf7,0xf5,0xeb,0xe9,0xef,0xed,0xe3,0xe1,0xe7,0xe5,
  77. }
  78.  
  79. local mul_3 = {
  80. [0]=0x00,0x03,0x06,0x05,0x0c,0x0f,0x0a,0x09,0x18,0x1b,0x1e,0x1d,0x14,0x17,0x12,0x11,
  81. 0x30,0x33,0x36,0x35,0x3c,0x3f,0x3a,0x39,0x28,0x2b,0x2e,0x2d,0x24,0x27,0x22,0x21,
  82. 0x60,0x63,0x66,0x65,0x6c,0x6f,0x6a,0x69,0x78,0x7b,0x7e,0x7d,0x74,0x77,0x72,0x71,
  83. 0x50,0x53,0x56,0x55,0x5c,0x5f,0x5a,0x59,0x48,0x4b,0x4e,0x4d,0x44,0x47,0x42,0x41,
  84. 0xc0,0xc3,0xc6,0xc5,0xcc,0xcf,0xca,0xc9,0xd8,0xdb,0xde,0xdd,0xd4,0xd7,0xd2,0xd1,
  85. 0xf0,0xf3,0xf6,0xf5,0xfc,0xff,0xfa,0xf9,0xe8,0xeb,0xee,0xed,0xe4,0xe7,0xe2,0xe1,
  86. 0xa0,0xa3,0xa6,0xa5,0xac,0xaf,0xaa,0xa9,0xb8,0xbb,0xbe,0xbd,0xb4,0xb7,0xb2,0xb1,
  87. 0x90,0x93,0x96,0x95,0x9c,0x9f,0x9a,0x99,0x88,0x8b,0x8e,0x8d,0x84,0x87,0x82,0x81,
  88. 0x9b,0x98,0x9d,0x9e,0x97,0x94,0x91,0x92,0x83,0x80,0x85,0x86,0x8f,0x8c,0x89,0x8a,
  89. 0xab,0xa8,0xad,0xae,0xa7,0xa4,0xa1,0xa2,0xb3,0xb0,0xb5,0xb6,0xbf,0xbc,0xb9,0xba,
  90. 0xfb,0xf8,0xfd,0xfe,0xf7,0xf4,0xf1,0xf2,0xe3,0xe0,0xe5,0xe6,0xef,0xec,0xe9,0xea,
  91. 0xcb,0xc8,0xcd,0xce,0xc7,0xc4,0xc1,0xc2,0xd3,0xd0,0xd5,0xd6,0xdf,0xdc,0xd9,0xda,
  92. 0x5b,0x58,0x5d,0x5e,0x57,0x54,0x51,0x52,0x43,0x40,0x45,0x46,0x4f,0x4c,0x49,0x4a,
  93. 0x6b,0x68,0x6d,0x6e,0x67,0x64,0x61,0x62,0x73,0x70,0x75,0x76,0x7f,0x7c,0x79,0x7a,
  94. 0x3b,0x38,0x3d,0x3e,0x37,0x34,0x31,0x32,0x23,0x20,0x25,0x26,0x2f,0x2c,0x29,0x2a,
  95. 0x0b,0x08,0x0d,0x0e,0x07,0x04,0x01,0x02,0x13,0x10,0x15,0x16,0x1f,0x1c,0x19,0x1a,
  96. }
  97.  
  98. local mul_9 = {
  99. [0]=0x00,0x09,0x12,0x1b,0x24,0x2d,0x36,0x3f,0x48,0x41,0x5a,0x53,0x6c,0x65,0x7e,0x77,
  100. 0x90,0x99,0x82,0x8b,0xb4,0xbd,0xa6,0xaf,0xd8,0xd1,0xca,0xc3,0xfc,0xf5,0xee,0xe7,
  101. 0x3b,0x32,0x29,0x20,0x1f,0x16,0x0d,0x04,0x73,0x7a,0x61,0x68,0x57,0x5e,0x45,0x4c,
  102. 0xab,0xa2,0xb9,0xb0,0x8f,0x86,0x9d,0x94,0xe3,0xea,0xf1,0xf8,0xc7,0xce,0xd5,0xdc,
  103. 0x76,0x7f,0x64,0x6d,0x52,0x5b,0x40,0x49,0x3e,0x37,0x2c,0x25,0x1a,0x13,0x08,0x01,
  104. 0xe6,0xef,0xf4,0xfd,0xc2,0xcb,0xd0,0xd9,0xae,0xa7,0xbc,0xb5,0x8a,0x83,0x98,0x91,
  105. 0x4d,0x44,0x5f,0x56,0x69,0x60,0x7b,0x72,0x05,0x0c,0x17,0x1e,0x21,0x28,0x33,0x3a,
  106. 0xdd,0xd4,0xcf,0xc6,0xf9,0xf0,0xeb,0xe2,0x95,0x9c,0x87,0x8e,0xb1,0xb8,0xa3,0xaa,
  107. 0xec,0xe5,0xfe,0xf7,0xc8,0xc1,0xda,0xd3,0xa4,0xad,0xb6,0xbf,0x80,0x89,0x92,0x9b,
  108. 0x7c,0x75,0x6e,0x67,0x58,0x51,0x4a,0x43,0x34,0x3d,0x26,0x2f,0x10,0x19,0x02,0x0b,
  109. 0xd7,0xde,0xc5,0xcc,0xf3,0xfa,0xe1,0xe8,0x9f,0x96,0x8d,0x84,0xbb,0xb2,0xa9,0xa0,
  110. 0x47,0x4e,0x55,0x5c,0x63,0x6a,0x71,0x78,0x0f,0x06,0x1d,0x14,0x2b,0x22,0x39,0x30,
  111. 0x9a,0x93,0x88,0x81,0xbe,0xb7,0xac,0xa5,0xd2,0xdb,0xc0,0xc9,0xf6,0xff,0xe4,0xed,
  112. 0x0a,0x03,0x18,0x11,0x2e,0x27,0x3c,0x35,0x42,0x4b,0x50,0x59,0x66,0x6f,0x74,0x7d,
  113. 0xa1,0xa8,0xb3,0xba,0x85,0x8c,0x97,0x9e,0xe9,0xe0,0xfb,0xf2,0xcd,0xc4,0xdf,0xd6,
  114. 0x31,0x38,0x23,0x2a,0x15,0x1c,0x07,0x0e,0x79,0x70,0x6b,0x62,0x5d,0x54,0x4f,0x46,
  115. }
  116.  
  117. local mul_11 = {
  118. [0]=0x00,0x0b,0x16,0x1d,0x2c,0x27,0x3a,0x31,0x58,0x53,0x4e,0x45,0x74,0x7f,0x62,0x69,
  119. 0xb0,0xbb,0xa6,0xad,0x9c,0x97,0x8a,0x81,0xe8,0xe3,0xfe,0xf5,0xc4,0xcf,0xd2,0xd9,
  120. 0x7b,0x70,0x6d,0x66,0x57,0x5c,0x41,0x4a,0x23,0x28,0x35,0x3e,0x0f,0x04,0x19,0x12,
  121. 0xcb,0xc0,0xdd,0xd6,0xe7,0xec,0xf1,0xfa,0x93,0x98,0x85,0x8e,0xbf,0xb4,0xa9,0xa2,
  122. 0xf6,0xfd,0xe0,0xeb,0xda,0xd1,0xcc,0xc7,0xae,0xa5,0xb8,0xb3,0x82,0x89,0x94,0x9f,
  123. 0x46,0x4d,0x50,0x5b,0x6a,0x61,0x7c,0x77,0x1e,0x15,0x08,0x03,0x32,0x39,0x24,0x2f,
  124. 0x8d,0x86,0x9b,0x90,0xa1,0xaa,0xb7,0xbc,0xd5,0xde,0xc3,0xc8,0xf9,0xf2,0xef,0xe4,
  125. 0x3d,0x36,0x2b,0x20,0x11,0x1a,0x07,0x0c,0x65,0x6e,0x73,0x78,0x49,0x42,0x5f,0x54,
  126. 0xf7,0xfc,0xe1,0xea,0xdb,0xd0,0xcd,0xc6,0xaf,0xa4,0xb9,0xb2,0x83,0x88,0x95,0x9e,
  127. 0x47,0x4c,0x51,0x5a,0x6b,0x60,0x7d,0x76,0x1f,0x14,0x09,0x02,0x33,0x38,0x25,0x2e,
  128. 0x8c,0x87,0x9a,0x91,0xa0,0xab,0xb6,0xbd,0xd4,0xdf,0xc2,0xc9,0xf8,0xf3,0xee,0xe5,
  129. 0x3c,0x37,0x2a,0x21,0x10,0x1b,0x06,0x0d,0x64,0x6f,0x72,0x79,0x48,0x43,0x5e,0x55,
  130. 0x01,0x0a,0x17,0x1c,0x2d,0x26,0x3b,0x30,0x59,0x52,0x4f,0x44,0x75,0x7e,0x63,0x68,
  131. 0xb1,0xba,0xa7,0xac,0x9d,0x96,0x8b,0x80,0xe9,0xe2,0xff,0xf4,0xc5,0xce,0xd3,0xd8,
  132. 0x7a,0x71,0x6c,0x67,0x56,0x5d,0x40,0x4b,0x22,0x29,0x34,0x3f,0x0e,0x05,0x18,0x13,
  133. 0xca,0xc1,0xdc,0xd7,0xe6,0xed,0xf0,0xfb,0x92,0x99,0x84,0x8f,0xbe,0xb5,0xa8,0xa3,
  134. }
  135.  
  136. local mul_13 = {
  137. [0]=0x00,0x0d,0x1a,0x17,0x34,0x39,0x2e,0x23,0x68,0x65,0x72,0x7f,0x5c,0x51,0x46,0x4b,
  138. 0xd0,0xdd,0xca,0xc7,0xe4,0xe9,0xfe,0xf3,0xb8,0xb5,0xa2,0xaf,0x8c,0x81,0x96,0x9b,
  139. 0xbb,0xb6,0xa1,0xac,0x8f,0x82,0x95,0x98,0xd3,0xde,0xc9,0xc4,0xe7,0xea,0xfd,0xf0,
  140. 0x6b,0x66,0x71,0x7c,0x5f,0x52,0x45,0x48,0x03,0x0e,0x19,0x14,0x37,0x3a,0x2d,0x20,
  141. 0x6d,0x60,0x77,0x7a,0x59,0x54,0x43,0x4e,0x05,0x08,0x1f,0x12,0x31,0x3c,0x2b,0x26,
  142. 0xbd,0xb0,0xa7,0xaa,0x89,0x84,0x93,0x9e,0xd5,0xd8,0xcf,0xc2,0xe1,0xec,0xfb,0xf6,
  143. 0xd6,0xdb,0xcc,0xc1,0xe2,0xef,0xf8,0xf5,0xbe,0xb3,0xa4,0xa9,0x8a,0x87,0x90,0x9d,
  144. 0x06,0x0b,0x1c,0x11,0x32,0x3f,0x28,0x25,0x6e,0x63,0x74,0x79,0x5a,0x57,0x40,0x4d,
  145. 0xda,0xd7,0xc0,0xcd,0xee,0xe3,0xf4,0xf9,0xb2,0xbf,0xa8,0xa5,0x86,0x8b,0x9c,0x91,
  146. 0x0a,0x07,0x10,0x1d,0x3e,0x33,0x24,0x29,0x62,0x6f,0x78,0x75,0x56,0x5b,0x4c,0x41,
  147. 0x61,0x6c,0x7b,0x76,0x55,0x58,0x4f,0x42,0x09,0x04,0x13,0x1e,0x3d,0x30,0x27,0x2a,
  148. 0xb1,0xbc,0xab,0xa6,0x85,0x88,0x9f,0x92,0xd9,0xd4,0xc3,0xce,0xed,0xe0,0xf7,0xfa,
  149. 0xb7,0xba,0xad,0xa0,0x83,0x8e,0x99,0x94,0xdf,0xd2,0xc5,0xc8,0xeb,0xe6,0xf1,0xfc,
  150. 0x67,0x6a,0x7d,0x70,0x53,0x5e,0x49,0x44,0x0f,0x02,0x15,0x18,0x3b,0x36,0x21,0x2c,
  151. 0x0c,0x01,0x16,0x1b,0x38,0x35,0x22,0x2f,0x64,0x69,0x7e,0x73,0x50,0x5d,0x4a,0x47,
  152. 0xdc,0xd1,0xc6,0xcb,0xe8,0xe5,0xf2,0xff,0xb4,0xb9,0xae,0xa3,0x80,0x8d,0x9a,0x97,
  153. }
  154.  
  155. local mul_14 = {
  156. [0]=0x00,0x0e,0x1c,0x12,0x38,0x36,0x24,0x2a,0x70,0x7e,0x6c,0x62,0x48,0x46,0x54,0x5a,
  157. 0xe0,0xee,0xfc,0xf2,0xd8,0xd6,0xc4,0xca,0x90,0x9e,0x8c,0x82,0xa8,0xa6,0xb4,0xba,
  158. 0xdb,0xd5,0xc7,0xc9,0xe3,0xed,0xff,0xf1,0xab,0xa5,0xb7,0xb9,0x93,0x9d,0x8f,0x81,
  159. 0x3b,0x35,0x27,0x29,0x03,0x0d,0x1f,0x11,0x4b,0x45,0x57,0x59,0x73,0x7d,0x6f,0x61,
  160. 0xad,0xa3,0xb1,0xbf,0x95,0x9b,0x89,0x87,0xdd,0xd3,0xc1,0xcf,0xe5,0xeb,0xf9,0xf7,
  161. 0x4d,0x43,0x51,0x5f,0x75,0x7b,0x69,0x67,0x3d,0x33,0x21,0x2f,0x05,0x0b,0x19,0x17,
  162. 0x76,0x78,0x6a,0x64,0x4e,0x40,0x52,0x5c,0x06,0x08,0x1a,0x14,0x3e,0x30,0x22,0x2c,
  163. 0x96,0x98,0x8a,0x84,0xae,0xa0,0xb2,0xbc,0xe6,0xe8,0xfa,0xf4,0xde,0xd0,0xc2,0xcc,
  164. 0x41,0x4f,0x5d,0x53,0x79,0x77,0x65,0x6b,0x31,0x3f,0x2d,0x23,0x09,0x07,0x15,0x1b,
  165. 0xa1,0xaf,0xbd,0xb3,0x99,0x97,0x85,0x8b,0xd1,0xdf,0xcd,0xc3,0xe9,0xe7,0xf5,0xfb,
  166. 0x9a,0x94,0x86,0x88,0xa2,0xac,0xbe,0xb0,0xea,0xe4,0xf6,0xf8,0xd2,0xdc,0xce,0xc0,
  167. 0x7a,0x74,0x66,0x68,0x42,0x4c,0x5e,0x50,0x0a,0x04,0x16,0x18,0x32,0x3c,0x2e,0x20,
  168. 0xec,0xe2,0xf0,0xfe,0xd4,0xda,0xc8,0xc6,0x9c,0x92,0x80,0x8e,0xa4,0xaa,0xb8,0xb6,
  169. 0x0c,0x02,0x10,0x1e,0x34,0x3a,0x28,0x26,0x7c,0x72,0x60,0x6e,0x44,0x4a,0x58,0x56,
  170. 0x37,0x39,0x2b,0x25,0x0f,0x01,0x13,0x1d,0x47,0x49,0x5b,0x55,0x7f,0x71,0x63,0x6d,
  171. 0xd7,0xd9,0xcb,0xc5,0xef,0xe1,0xf3,0xfd,0xa7,0xa9,0xbb,0xb5,0x9f,0x91,0x83,0x8d,
  172. }
  173.  
  174. local bxor = bit.bxor
  175. local insert = table.insert
  176.  
  177. local function copy(input)
  178.         local c = {}
  179.         for i, v in pairs(input) do
  180.                 c[i] = v
  181.         end
  182.         return c
  183. end
  184.  
  185. local function subBytes(input, invert)
  186.         for i=1, #input do
  187.                 if not (sbox[input[i]] and inv_sbox[input[i]]) then
  188.                         error("subBytes: input["..i.."] > 0xFF")
  189.                 end
  190.                 if invert then
  191.                         input[i] = inv_sbox[input[i]]
  192.                 else
  193.                         input[i] = sbox[input[i]]
  194.                 end
  195.         end
  196.         return input
  197. end
  198.  
  199. local function shiftRows(input)
  200.         local copy = {}
  201.         -- Row 1: No change
  202.         copy[1] = input[1]
  203.         copy[2] = input[2]
  204.         copy[3] = input[3]
  205.         copy[4] = input[4]
  206.         -- Row 2: Offset 1
  207.         copy[5] = input[6]
  208.         copy[6] = input[7]
  209.         copy[7] = input[8]
  210.         copy[8] = input[5]
  211.         -- Row 3: Offset 2
  212.         copy[9] = input[11]
  213.         copy[10] = input[12]
  214.         copy[11] = input[9]
  215.         copy[12] = input[10]
  216.         -- Row 4: Offset 3
  217.         copy[13] = input[16]
  218.         copy[14] = input[13]
  219.         copy[15] = input[14]
  220.         copy[16] = input[15]
  221.         return copy
  222. end
  223.  
  224. local function invShiftRows(input)
  225.         local copy = {}
  226.         -- Row 1: No change
  227.         copy[1] = input[1]
  228.         copy[2] = input[2]
  229.         copy[3] = input[3]
  230.         copy[4] = input[4]
  231.         -- Row 2: Offset 1
  232.         copy[5] = input[8]
  233.         copy[6] = input[5]
  234.         copy[7] = input[6]
  235.         copy[8] = input[7]
  236.         -- Row 3: Offset 2
  237.         copy[9] = input[11]
  238.         copy[10] = input[12]
  239.         copy[11] = input[9]
  240.         copy[12] = input[10]
  241.         -- Row 4: Offset 3
  242.         copy[13] = input[14]
  243.         copy[14] = input[15]
  244.         copy[15] = input[16]
  245.         copy[16] = input[13]
  246.         return copy
  247. end
  248.  
  249. local function finite_field_mul(a,b) -- Multiply two numbers in GF(256), assuming that polynomials are 8 bits wide
  250.         local product = 0
  251.         local mulA, mulB = a,b
  252.         for i=1, 8 do
  253.                 --print("FFMul: MulA: "..mulA.." MulB: "..mulB)
  254.                 if mulA == 0 or mulB == 0 then
  255.                         break
  256.                 end
  257.                 if bit.band(1, mulB) > 0 then
  258.                         product = bxor(product, mulA)
  259.                 end
  260.                 mulB = bit.brshift(mulB, 1)
  261.                 local carry = bit.band(0x80, mulA)
  262.                 mulA = bit.band(0xFF, bit.blshift(mulA, 1))
  263.                 if carry > 0 then
  264.                         mulA = bxor( mulA, 0x1B )
  265.                 end
  266.         end
  267.         return product
  268. end
  269.  
  270. local function mixColumn(column)
  271.         local output = {}
  272.         --print("MixColumn: #column: "..#column)
  273.         output[1] = bxor( mul_2[column[1]], bxor( mul_3[column[2]], bxor( column[3], column[4] ) ) )
  274.         output[2] = bxor( column[1], bxor( mul_2[column[2]], bxor( mul_3[column[3]], column[4] ) ) )
  275.         output[3] = bxor( column[1], bxor( column[2], bxor( mul_2[column[3]], mul_3[column[4]] ) ) )
  276.         output[4] = bxor( mul_3[column[1]], bxor( column[2], bxor( column[3], mul_2[column[4]] ) ) )
  277.         return output
  278. end
  279.  
  280. local function invMixColumn(column)
  281.         local output = {}
  282.         --print("InvMixColumn: #column: "..#column)
  283.         output[1] = bxor( mul_14[column[1]], bxor( mul_11[column[2]], bxor( mul_13[column[3]], mul_9[column[4]] ) ) )
  284.         output[2] = bxor( mul_9[column[1]], bxor( mul_14[column[2]], bxor( mul_11[column[3]], mul_13[column[4]] ) ) )
  285.         output[3] = bxor( mul_13[column[1]], bxor( mul_9[column[2]], bxor( mul_14[column[3]], mul_11[column[4]] ) ) )
  286.         output[4] = bxor( mul_11[column[1]], bxor( mul_13[column[2]], bxor( mul_9[column[3]], mul_14[column[4]] ) ) )
  287.         return output
  288. end
  289.  
  290. local function mixColumns(input, invert)
  291.         --print("MixColumns: #input: "..#input)
  292.         -- Ooops. I mixed the ROWS instead of the COLUMNS on accident.
  293.         local output = {}
  294.         --[[
  295.         local c1 = { input[1], input[2], input[3], input[4] }
  296.         local c2 = { input[5], input[6], input[7], input[8] }
  297.         local c3 = { input[9], input[10], input[11], input[12] }
  298.         local c4 = { input[13], input[14], input[15], input[16] }
  299.         ]]
  300.         local c1 = { input[1], input[5], input[9], input[13] }
  301.         local c2 = { input[2], input[6], input[10], input[14] }
  302.         local c3 = { input[3], input[7], input[11], input[15] }
  303.         local c4 = { input[4], input[8], input[12], input[16] }
  304.         if invert then
  305.                 c1 = invMixColumn(c1)
  306.                 c2 = invMixColumn(c2)
  307.                 c3 = invMixColumn(c3)
  308.                 c4 = invMixColumn(c4)
  309.         else
  310.                 c1 = mixColumn(c1)
  311.                 c2 = mixColumn(c2)
  312.                 c3 = mixColumn(c3)
  313.                 c4 = mixColumn(c4)
  314.         end
  315.         --[[
  316.         output[1] = c1[1]
  317.         output[2] = c1[2]
  318.         output[3] = c1[3]
  319.         output[4] = c1[4]
  320.        
  321.         output[5] = c2[1]
  322.         output[6] = c2[2]
  323.         output[7] = c2[3]
  324.         output[8] = c2[4]
  325.        
  326.         output[9] = c3[1]
  327.         output[10] = c3[2]
  328.         output[11] = c3[3]
  329.         output[12] = c3[4]
  330.        
  331.         output[13] = c4[1]
  332.         output[14] = c4[2]
  333.         output[15] = c4[3]
  334.         output[16] = c4[4]
  335.         ]]
  336.        
  337.         output[1] = c1[1]
  338.         output[5] = c1[2]
  339.         output[9] = c1[3]
  340.         output[13] = c1[4]
  341.        
  342.         output[2] = c2[1]
  343.         output[6] = c2[2]
  344.         output[10] = c2[3]
  345.         output[14] = c2[4]
  346.        
  347.         output[3] = c3[1]
  348.         output[7] = c3[2]
  349.         output[11] = c3[3]
  350.         output[15] = c3[4]
  351.        
  352.         output[4] = c4[1]
  353.         output[8] = c4[2]
  354.         output[12] = c4[3]
  355.         output[16] = c4[4]
  356.        
  357.         return output
  358. end
  359.  
  360. local function addRoundKey(input, exp_key, round)
  361.         local output = {}
  362.         for i=1, 16 do
  363.                 assert(input[i], "input["..i.."]=nil!")
  364.                 assert(exp_key[ ((round-1)*16)+i ], "round_key["..(((round-1)*16)+i).."]=nil!")
  365.                 output[i] = bxor( input[i], exp_key[ ((round-1)*16)+i ] )
  366.         end
  367.         return output
  368. end
  369.  
  370. function key_schedule(enc_key)
  371.         local function core(in1, in2, in3, in4, i)
  372.                 local s1 = in2
  373.                 local s2 = in3
  374.                 local s3 = in4
  375.                 local s4 = in1
  376.                 s1 = bxor(sbox[s1], Rcon[i])
  377.                 s2 = sbox[s2]
  378.                 s3 = sbox[s3]
  379.                 s4 = sbox[s4]
  380.                 return s1, s2, s3, s4
  381.         end
  382.        
  383.         local n, b, key_type = 0, 0, 0
  384.        
  385.         -- Len | n | b |
  386.         -- 128 |16 |176|
  387.         -- 192 |24 |208|
  388.         -- 256 |32 |240|
  389.        
  390.         -- Determine keysize:
  391.        
  392.         if #enc_key < 16 then
  393.                 error("Encryption key is too small; key size must be more than 16 bytes.")
  394.         elseif #enc_key >= 16 and #enc_key < 24 then
  395.                 n = 16
  396.                 b = 176
  397.                 --key_type = 1
  398.         elseif #enc_key >= 24 and #enc_key < 32 then
  399.                 n = 24
  400.                 b = 208
  401.                 --key_type = 2
  402.         else
  403.                 n = 32
  404.                 b = 240
  405.                 --key_type = 3
  406.         end
  407.        
  408.         local exp_key = {}
  409.         local rcon_iter = 1
  410.         for i=1, n do
  411.                 exp_key[i] = enc_key[i]
  412.         end
  413.         while #exp_key < b do
  414.                 local t1 = exp_key[#exp_key]
  415.                 local t2 = exp_key[#exp_key-1]
  416.                 local t3 = exp_key[#exp_key-2]
  417.                 local t4 = exp_key[#exp_key-3]
  418.                 t1, t2, t3, t4 = core(t1, t2, t3, t4, rcon_iter)
  419.                 rcon_iter = rcon_iter+1
  420.                 t1 = bxor(t1, exp_key[#exp_key-(n-1)])
  421.                 t2 = bxor(t2, exp_key[#exp_key-(n-2)])
  422.                 t3 = bxor(t3, exp_key[#exp_key-(n-3)])
  423.                 t4 = bxor(t4, exp_key[#exp_key-(n-4)])
  424.                 insert(exp_key, t1)
  425.                 insert(exp_key, t2)
  426.                 insert(exp_key, t3)
  427.                 insert(exp_key, t4)
  428.                 for i=1, 3 do
  429.                         t1 = bxor(exp_key[#exp_key], exp_key[#exp_key-(n-1)])
  430.                         t2 = bxor(exp_key[#exp_key-1], exp_key[#exp_key-(n-2)])
  431.                         t3 = bxor(exp_key[#exp_key-2], exp_key[#exp_key-(n-3)])
  432.                         t4 = bxor(exp_key[#exp_key-3], exp_key[#exp_key-(n-4)])
  433.                         insert(exp_key, t1)
  434.                         insert(exp_key, t2)
  435.                         insert(exp_key, t3)
  436.                         insert(exp_key, t4)
  437.                 end
  438.                 if key_type == 3 then -- If we're processing a 256 bit key...
  439.                         -- Take the previous 4 bytes of the expanded key, run them through the sbox,
  440.                         -- then XOR them with the previous n bytes of the expanded key, then output them
  441.                         -- as the next 4 bytes of expanded key.
  442.                         t1 = bxor(sbox[exp_key[#exp_key]], exp_key[#exp_key-(n-1)])
  443.                         t2 = bxor(sbox[exp_key[#exp_key-1]], exp_key[#exp_key-(n-2)])
  444.                         t3 = bxor(sbox[exp_key[#exp_key-2]], exp_key[#exp_key-(n-3)])
  445.                         t4 = bxor(sbox[exp_key[#exp_key-3]], exp_key[#exp_key-(n-4)])
  446.                         insert(exp_key, t1)
  447.                         insert(exp_key, t2)
  448.                         insert(exp_key, t3)
  449.                         insert(exp_key, t4)
  450.                 end
  451.                 if key_type == 2 or key_type == 3 then -- If we're processing a 192-bit or 256-bit key..
  452.                         local i = 2
  453.                         if key_type == 3 then
  454.                                 i = 3
  455.                         end
  456.                         for j=1, i do
  457.                                 t1 = bxor(exp_key[#exp_key], exp_key[#exp_key-(n-1)])
  458.                                 t2 = bxor(exp_key[#exp_key-1], exp_key[#exp_key-(n-2)])
  459.                                 t3 = bxor(exp_key[#exp_key-2], exp_key[#exp_key-(n-3)])
  460.                                 t4 = bxor(exp_key[#exp_key-3], exp_key[#exp_key-(n-4)])
  461.                                 insert(exp_key, t1)
  462.                                 insert(exp_key, t2)
  463.                                 insert(exp_key, t3)
  464.                                 insert(exp_key, t4)
  465.                         end
  466.                 end
  467.         end
  468.         return exp_key
  469. end
  470.  
  471. -- Transform a string of bytes into 16 byte blocks, adding padding to ensure that each block contains 16 bytes.
  472. -- For example:
  473. -- "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" (contains 28 0xFF bytes)
  474. -- Is transformed into this:
  475. -- {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0,0,0,0} (16 0xFF bytes, followed by 12 0xFF bytes and 4 0x00 bytes for padding)
  476.  
  477. local function breakIntoBlocks(data)
  478.         if type(data) ~= "string" then
  479.                 error("breakIntoBlocks: data is not a string", 2)
  480.         end
  481.         while (#data % 16) ~= 0 do
  482.                 data = data.."\0"
  483.         end
  484.         local blocks = {}
  485.         local blockNum = 1
  486.         local output = {}
  487.         for i=1, #data, 16 do
  488.                 blocks[blockNum] = {}
  489.                 for j=1, 16 do
  490.                         blocks[blockNum][j] = string.byte(data, ((blockNum-1)*16)+j, ((blockNum-1)*16)+j)
  491.                 end
  492.                 blockNum = blockNum+1
  493.         end
  494.         return blocks
  495. end
  496.  
  497. -- Transform a string into a series of blocks.
  498.  
  499. -- For example, to get a key from a string:
  500. -- local key = strToBlocks(keyStr)
  501. -- key = key[1]
  502.  
  503. function strToBlocks(str)
  504.         local rawBytestream = {}
  505.         local blocks = {}
  506.         for i=1, #str do
  507.                 rawBytestream[i] = string.byte(str, i, i)
  508.         end
  509.         for i=1, math.ceil(#rawBytestream / 16) do
  510.                 blocks[i] = {}
  511.                 for j=1, 16 do
  512.                         blocks[i][j] = rawBytestream[ ((i-1)*16)+j ] or 0
  513.                 end
  514.         end
  515.         return blocks
  516. end
  517.  
  518. -- Encrypt / Decrypt individual blocks:
  519.  
  520. function encrypt_block(data, key)
  521.         local exp_key = key_schedule(key)
  522.         local state = data
  523.         local nr = 0
  524.        
  525.         if #exp_key == 176 then -- Key type 1 (128-bits)
  526.                 nr = 10
  527.         elseif #exp_key == 208 then -- Key type 2 (192-bits)
  528.                 nr = 12
  529.         elseif #exp_key == 240 then -- Key type 3 (256-bits)
  530.                 nr = 14
  531.         else
  532.                 error("encrypt_block: Unknown key size?", 2)
  533.         end
  534.        
  535.         -- Inital round:
  536.         state = addRoundKey(state, exp_key, 1)
  537.        
  538.         -- Repeat (Nr-1) times:
  539.         for round_num = 2, nr-1 do    
  540.                 state = subBytes(state)
  541.                 state = shiftRows(state)
  542.                 state = mixColumns(state)
  543.                 state = addRoundKey(state, exp_key, round_num)
  544.         end
  545.        
  546.         -- Final round (No mixColumns()):
  547.         state = subBytes(state)
  548.         state = shiftRows(state)
  549.         state = addRoundKey(state, exp_key, nr)
  550.         return state
  551. end
  552.  
  553. function decrypt_block(data, key)
  554.         local exp_key = key_schedule(key)
  555.         local state = data
  556.         local nr = 0
  557.        
  558.         if #exp_key == 176 then -- Key type 1 (128-bits)
  559.                 nr = 10
  560.         elseif #exp_key == 208 then -- Key type 2 (192-bits)
  561.                 nr = 12
  562.         elseif #exp_key == 240 then -- Key type 3 (256-bits)
  563.                 nr = 14
  564.         else
  565.                 error("decrypt_block: Unknown key size?", 2)
  566.         end
  567.        
  568.         -- Inital round:
  569.         state = addRoundKey(state, exp_key, nr)
  570.        
  571.         -- Repeat (Nr-1) times:
  572.         for round_num = nr-1, 2, -1 do
  573.                 state = invShiftRows(state)
  574.                 state = subBytes(state, true)
  575.                 state = addRoundKey(state, exp_key, round_num)
  576.                 state = mixColumns(state, true)
  577.         end
  578.        
  579.         -- Final round (No mixColumns()):
  580.         state = invShiftRows(state)
  581.         state = subBytes(state, true)
  582.         state = addRoundKey(state, exp_key, 1)
  583.         return state
  584. end
  585.  
  586. function encrypt_block_customExpKey(data, exp_key--[[, key_type]]) -- Encrypt blocks, but using a precalculated expanded key instead of performing the key expansion on every step like with the normal encrypt_block(2) call
  587.         local state = data
  588.         local nr = 0
  589.         if #exp_key == 176 then -- Key type 1 (128-bits)
  590.                 nr = 10
  591.         elseif #exp_key == 208 then -- Key type 2 (192-bits)
  592.                 nr = 12
  593.         elseif #exp_key == 240 then -- Key type 3 (256-bits)
  594.                 nr = 14
  595.         else
  596.                 error("encrypt_block: Unknown key size?", 2)
  597.         end
  598.        
  599.         -- Inital round:
  600.         state = addRoundKey(state, exp_key, 1)
  601.        
  602.         -- Repeat (Nr-1) times:
  603.         for round_num = 2, nr-1 do    
  604.                 state = subBytes(state)
  605.                 state = shiftRows(state)
  606.                 state = mixColumns(state)
  607.                 state = addRoundKey(state, exp_key, round_num)
  608.         end
  609.        
  610.         -- Final round (No mixColumns()):
  611.         state = subBytes(state)
  612.         state = shiftRows(state)
  613.         state = addRoundKey(state, exp_key, nr)
  614.         return state
  615. end
  616.  
  617. function decrypt_block_customExpKey(data, exp_key--[[, key_type]])
  618.         local state = data
  619.         local nr = 0
  620.         if #exp_key == 176 then -- Key type 1 (128-bits)
  621.                 nr = 10
  622.         elseif #exp_key == 208 then -- Key type 2 (192-bits)
  623.                 nr = 12
  624.         elseif #exp_key == 240 then -- Key type 3 (256-bits)
  625.                 nr = 14
  626.         else
  627.                 error("decrypt_block: Unknown key size?", 2)
  628.         end
  629.        
  630.         -- Inital round:
  631.         state = addRoundKey(state, exp_key, nr)
  632.        
  633.         -- Repeat (Nr-1) times:
  634.         for round_num = nr-1, 2, -1 do
  635.                 state = invShiftRows(state)
  636.                 state = subBytes(state, true)
  637.                 state = addRoundKey(state, exp_key, round_num)
  638.                 state = mixColumns(state, true)
  639.         end
  640.        
  641.         -- Final round (No mixColumns()):
  642.         state = invShiftRows(state)
  643.         state = subBytes(state, true)
  644.         state = addRoundKey(state, exp_key, 1)
  645.         return state
  646. end
  647.  
  648. -- Encrypt / Decrypt bytestreams (tables of bytes):
  649.  
  650. -- ECB (electronic codebook) Mode (not secure, do not use):
  651.  
  652. function encrypt_bytestream_ecb(data, key)
  653.         local blocks = {}
  654.         local outputBytestream = {}
  655.         local exp_key = key_schedule(key)
  656.         for i=1, #data, 16 do
  657.                 local block = {}
  658.                 for j=1, 16 do
  659.                         block[j] = data[i+(j-1)] or 0
  660.                 end
  661.                 block = encrypt_block_customExpKey(block, exp_key)
  662.                 for j=1, 16 do
  663.                         table.insert(outputBytestream, block[j])
  664.                 end
  665.                 os.queueEvent("")
  666.                 os.pullEvent("")
  667.         end
  668.         return outputBytestream
  669. end
  670.  
  671. function decrypt_bytestream_ecb(data, key)
  672.         local outputBytestream = {}
  673.         local exp_key = key_schedule(key)
  674.         for i=1, #data, 16 do
  675.                 local block = {}
  676.                 for j=1, 16 do
  677.                         block[j] = data[i+(j-1)] or 0
  678.                 end
  679.                 block = decrypt_block_customExpKey(block, exp_key)
  680.                 for j=1, 16 do
  681.                         table.insert(outputBytestream, block[j])
  682.                 end
  683.                 os.queueEvent("")
  684.                 os.pullEvent("")
  685.         end
  686.         for i=#outputBytestream, 1, -1 do
  687.                 if outputBytestream[i] ~= 0 then
  688.                         break
  689.                 else
  690.                         outputBytestream[i] = nil
  691.                 end
  692.         end
  693.         return outputBytestream
  694. end
  695.  
  696. -- CBC (cipher-block chaining) mode:
  697.  
  698. function encrypt_bytestream(data, key, init_vector)
  699.         local blocks = { init_vector }
  700.         local outputBytestream = {}
  701.         local exp_key = key_schedule(key)
  702.         if not init_vector then
  703.                 error("encrypt_bytestream: No initalization vector was passed.", 2)
  704.         end
  705.         for i=1, #data do
  706.                 if data[i] == nil or data[i] >= 256 then
  707.                         if type(data[i]) == "number" then
  708.                                 error("encrypt_bytestream: Invalid data at i="..i.." data[i]="..data[i], 2)
  709.                         else
  710.                                 error("encrypt_bytestream: Invalid data at i="..i.." data[i]="..type(data[i]), 2)
  711.                         end
  712.                 end
  713.         end
  714.         local s = os.clock()
  715.         for i=1, math.ceil(#data/16) do
  716.                 local block = {}
  717.                 if not blocks[i] then
  718.                         error("encrypt_bytestream: blocks["..i.."] is nil! Input size: "..#data, 2)
  719.                 end
  720.                 for j=1, 16 do
  721.                         block[j] = data[((i-1)*16)+j] or 0
  722.                         block[j] = bxor(block[j], blocks[i][j]) -- XOR this block with the previous one
  723.                 end
  724.                 --print("#bytes: "..#block)
  725.                 block = encrypt_block_customExpKey(block, exp_key)
  726.                 table.insert(blocks, block)
  727.                 for j=1, 16 do
  728.                         insert(outputBytestream, block[j])
  729.                 end
  730.         if os.clock() - s >= 2.5 then
  731.             os.queueEvent("")
  732.             os.pullEvent("")
  733.             s = os.clock()
  734.         end
  735.         end
  736.         return outputBytestream
  737. end
  738.  
  739. function decrypt_bytestream(data, key, init_vector)
  740.         local blocks = { init_vector }
  741.         local outputBytestream = {}
  742.         local exp_key = key_schedule(key)
  743.         if not init_vector then
  744.                 error("decrypt_bytestream: No initalization vector was passed.", 2)
  745.         end
  746.         local s = os.clock()
  747.         for i=1, math.ceil(#data/16) do
  748.                 local block = {}
  749.                 if not blocks[i] then
  750.                         error("decrypt_bytestream: blocks["..i.."] is nil! Input size: "..#data, 2)
  751.                 end
  752.                 for j=1, 16 do
  753.                         block[j] = data[((i-1)*16)+j] or 0
  754.                 end
  755.                 table.insert(blocks, block)
  756.                 local dec_block = decrypt_block_customExpKey(block, exp_key)
  757.                 for j=1, 16 do
  758.                         dec_block[j] = bxor(dec_block[j], blocks[i][j]) -- We use XOR on the plaintext, not the ciphertext
  759.                         table.insert(outputBytestream, dec_block[j])
  760.                 end
  761.         if os.clock() - s >= 2.5 then
  762.             os.queueEvent("")
  763.             os.pullEvent("")
  764.             s = os.clock()
  765.         end
  766.         end
  767.         -- Remove padding:
  768.         for i=#outputBytestream, #outputBytestream-15, -1 do
  769.                 if outputBytestream[i] ~= 0 then
  770.                         break
  771.                 else
  772.                         outputBytestream[i] = nil
  773.                 end
  774.         end
  775.         return outputBytestream
  776. end
  777.  
  778. -- Encrypt / Decrypt strings:
  779.  
  780. function encrypt_str(data, key, iv)
  781.         local byteStream = {}
  782.         for i=1, #data do
  783.                 table.insert(byteStream, string.byte(data, i, i))
  784.         end
  785.         local output_bytestream = {}
  786.         if iv then
  787.                 output_bytestream = encrypt_bytestream(byteStream, key, iv)
  788.         else
  789.                 output_bytestream = encrypt_bytestream_ecb(byteStream, key)
  790.         end
  791.         local output = ""
  792.         for i=1, #output_bytestream do
  793.                 output = output..string.char(output_bytestream[i])
  794.         end
  795.         return output
  796. end
  797.  
  798. function decrypt_str(data, key, iv)
  799.         local byteStream = {}
  800.         for i=1, #data do
  801.                 table.insert(byteStream, string.byte(data, i, i))
  802.         end
  803.         local output_bytestream = {}
  804.         if iv then
  805.                 output_bytestream = decrypt_bytestream(byteStream, key, iv)
  806.         else
  807.                 output_bytestream = decrypt_bytestream_ecb(byteStream, key)
  808.         end
  809.         local output = ""
  810.         for i=1, #output_bytestream do
  811.                 output = output..string.char(output_bytestream[i])
  812.         end
  813.         return output
  814. end
  815.  
  816. function davies_meyer(data, h0)
  817.         local last_h = h0
  818.     for dm_iter=1, 16 do
  819.         for i=1, math.ceil(#data/16) do
  820.             local block = {}
  821.             for j=1, 16 do
  822.                 block[j] = data[((i-1)*16)+j] or 0
  823.             end
  824.             local block = encrypt_block(last_h, block)
  825.             for j=1, 16 do
  826.                 block[j] = bxor(block[j], last_h[j]) -- XOR h[i-1] with h[i].
  827.             end
  828.             last_h = block
  829.             os.queueEvent("")
  830.             os.pullEvent("")
  831.         end
  832.     end
  833.         return last_h
  834. end
  835.  
  836. local function increment_ctr(blk)
  837.         local cpy = {}
  838.         for i=1, 16 do
  839.                 cpy[i] = blk[i] or 0
  840.         end
  841.         cpy[1] = cpy[1] + incAmt
  842.         for i=2, 16 do
  843.                 if cpy[i-1] <= 255 then
  844.                         break
  845.                 end
  846.                 local carry = cpy[i-1] - 255
  847.                 cpy[i] = cpy[i]+carry
  848.         end
  849.         return cpy
  850. end
  851.  
  852. local counter_mode_context = {
  853.         key = {},
  854.         ctr = {},
  855.         stream_cache = {}, -- Use "leftover" bytes from generate() here.
  856.         set_key = function(self, key)
  857.                 if type(key) == "string" then
  858.                         if #key < 16 then
  859.                                 error("set_key: Key length ("..#key..") must be at least 16 characters!", 2)
  860.                         end
  861.                         for i=1, 16 do
  862.                                 self.key[i] = string.byte(key, i, i)
  863.                         end
  864.                 elseif type(key) == "table" then
  865.                         if #key < 16 then
  866.                                 error("set_key: Key length ("..#key..") must be at least 16 bytes!", 2)
  867.                         end
  868.                         for i=1, 16 do
  869.                                 if type(key[i]) ~= "number" or key[i] > 255 or key[i] < 0 then
  870.                                         if type(key[i]) == "nil" then
  871.                                                 error("set_key: Value key["..i.."] is invalid: nil", 2)
  872.                                         else
  873.                                                 error("set_key: Value key["..i.."] is invalid: "..key[i], 2)
  874.                                         end
  875.                                 end
  876.                                 self.key[i] = key[i]
  877.                         end
  878.                 else
  879.                         error("set_key: Key type is not supported: "..type(key), 2)
  880.                 end
  881.         end,
  882.         set_ctr = function(self, ctr)
  883.                 if type(ctr) == "string" then
  884.                         if #ctr < 16 then
  885.                                 error("set_ctr: Counter length ("..#ctr..") must be at least 16 characters!", 2)
  886.                         end
  887.                         for i=1, 16 do
  888.                                 self.ctr[i] = string.byte(ctr, i, i)
  889.                         end
  890.                 elseif type(ctr) == "table" then
  891.                         if #ctr < 16 then
  892.                                 error("set_ctr: Counter length ("..#ctr..") must be at least 16 bytes!", 2)
  893.                         end
  894.                         for i=1, 16 do
  895.                                 if type(ctr[i]) ~= "number" or ctr[i] > 255 or ctr[i] < 0 then
  896.                                         if type(ctr[i]) == "nil" then
  897.                                                 error("set_ctr: Value ctr["..i.."] is invalid: nil", 2)
  898.                                         else
  899.                                                 error("set_ctr: Value ctr["..i.."] is invalid: "..ctr[i], 2)
  900.                                         end
  901.                                 end
  902.                                 self.ctr[i] = ctr[i]
  903.                         end
  904.                 elseif type(ctr) == "number" then
  905.                         local b1 = bit.band( ctr, 0xFF )
  906.                         local b2 = bit.band( bit.brshift(bit.band( ctr, 0xFF00 ), 8), 0xFF )
  907.                         local b3 = bit.band( bit.brshift(bit.band( ctr, 0xFF0000 ), 16), 0xFF )
  908.                         local b4 = bit.band( bit.brshift(bit.band( ctr, 0xFF000000 ), 24), 0xFF )
  909.                         self.ctr = {}
  910.                         for i=1, 16 do
  911.                                 self.ctr[i] = 0
  912.                         end
  913.                         self.ctr[1] = b1
  914.                         self.ctr[2] = b2
  915.                         self.ctr[3] = b3
  916.                         self.ctr[4] = b4
  917.                 else
  918.                         error("set_ctr: Counter type is not supported: "..type(ctr), 2)
  919.                 end
  920.         end,
  921.         generate = function(self, bytes)
  922.                 local genBytes = {}
  923.                 if #self.stream_cache >= bytes then
  924.                         for i=1, bytes do
  925.                                 table.insert(genBytes, table.remove(self.stream_cache))
  926.                         end
  927.                 else
  928.                         for i=1, #self.stream_cache do
  929.                                 table.insert(genBytes, table.remove(self.stream_cache))
  930.                         end
  931.                         local blocksToGenerate = math.ceil((bytes - #genBytes) / 16)
  932.                         for i=1, blocksToGenerate-1 do
  933.                                 self.ctr = increment_ctr(self.ctr)
  934.                                 local block = encrypt_block(self.ctr, self.key)
  935.                                 for i=1, 16 do
  936.                                         table.insert(genBytes, block[i])
  937.                                 end
  938.                         end
  939.                         self.ctr = increment_ctr(self.ctr)
  940.                         local block = encrypt_block(self.ctr, self.key)
  941.                         for i=1, (bytes - #genBytes) do
  942.                                 table.insert(genBytes, table.remove(block))
  943.                         end
  944.                         for i=1, #block do
  945.                                 table.insert(self.stream_cache, table.remove(block))
  946.                         end
  947.                 end
  948.                 return genBytes
  949.         end,
  950. }
  951.  
  952. function new_ctrMode(key, iv)
  953.         local context = {
  954.                 stream_cache = {},
  955.                 key = {},
  956.                 iv = {},
  957.                 __index = counter_mode_context,
  958.         }
  959.         setmetatable(context, context)
  960.         context:set_key(key)
  961.         context:set_ctr(iv)
  962.         return context
  963. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement