Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.49 KB | None | 0 0
  1. aessimple.c
  2.  
  3. //
  4. // main.c
  5. // test
  6. //
  7. // Created by Oleg Belykh on 9/18/13.
  8. // Copyright (c) 2013 Oleg Belykh. All rights reserved.
  9. //
  10. // You can use it how can you want but do not modify copyrights
  11. //
  12.  
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include "TI_aes.h"
  16.  
  17. #define c_BufferSize 128
  18.  
  19. char checkarg(const char* opts)
  20. {
  21. if (opts[0]=='-')
  22. return opts[1];
  23.  
  24. return -1;
  25. }
  26.  
  27. int main(int argc, const char * argv[])
  28. {
  29. int keyLenght=0;
  30. unsigned char buffer [c_BufferSize], operation=0;
  31. ssize_t realRead,count;
  32. char flag;
  33. unsigned char *lp;
  34. unsigned char key[16]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
  35.  
  36. if (argc<=1){
  37. printf("Simple AES encryption tool\nUsage: cat [infile] | ./aessimple -[d: decrypt, e: encrypt], -k [16 byte key] > [outfile]\n");
  38. return -1;
  39. };
  40.  
  41. count = 1;
  42. while (count < argc){
  43. flag = checkarg(argv[count]);
  44.  
  45. switch ((int)flag){
  46. case 'e':
  47. case 'd':
  48. if (operation!=0){
  49. printf("Can't understand what to do. Use -e for encrypt or -d for decrypt separately\n");
  50. return -1;
  51. };
  52.  
  53. operation = flag;
  54. break;
  55. case 'k':
  56. if (count<argc-1){
  57. lp=argv[count+1];
  58. if (checkarg(lp) == -1){
  59. while (keyLenght<16){
  60. if (*lp==0)
  61. goto terminate;
  62. key[keyLenght++]=*lp++;
  63. }
  64. }else
  65. printf("Key expected.\n");
  66. }else
  67. printf("Key expected.\n");
  68. break;
  69. }
  70.  
  71. count++;
  72. }
  73.  
  74. terminate:
  75. if (keyLenght<16){
  76. printf("Expecting 16 byte key value\n");
  77. return -1;
  78. }
  79.  
  80. void (*wFunc)(unsigned char* buffer, unsigned char* key);
  81. switch(operation){
  82. case 'e':
  83. wFunc=aes_encrypt;
  84. break;
  85. case 'd':
  86. wFunc=aes_decrypt;
  87. break;
  88. default:
  89. printf("Operation expected\n");
  90. return -1;
  91. };
  92.  
  93. do{
  94. realRead=read(STDIN_FILENO, buffer, c_BufferSize);
  95.  
  96. count=realRead;
  97.  
  98. long emptyArea=c_BufferSize-realRead;
  99. lp=buffer+realRead;
  100. while(emptyArea-->0)
  101. *lp++=0x20;
  102.  
  103. lp=buffer;
  104. while (count>0){
  105. (wFunc)(lp, key);
  106.  
  107. write (STDOUT_FILENO, lp , count>16?16:16);
  108.  
  109. lp+=16;
  110. count-=16;
  111. }
  112. }while (realRead>=c_BufferSize);
  113.  
  114. return 0;
  115. }
  116.  
  117. TI_aes.c
  118.  
  119. /**************************************************************
  120. AES128
  121. Author: Uli Kretzschmar
  122. MSP430 Systems
  123. Freising
  124. AES software support for encryption and decryption
  125. ECCN 5D002 TSU - Technology / Software Unrestricted
  126. **************************************************************/
  127.  
  128. // foreward sbox
  129. const unsigned char sbox[256] = {
  130. //0 1 2 3 4 5 6 7 8 9 A B C D E F
  131. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0
  132. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1
  133. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2
  134. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3
  135. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4
  136. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5
  137. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6
  138. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7
  139. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8
  140. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9
  141. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A
  142. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B
  143. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C
  144. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D
  145. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E
  146. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F
  147. // inverse sbox
  148. const unsigned char rsbox[256] =
  149. { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
  150. , 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
  151. , 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
  152. , 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
  153. , 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
  154. , 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
  155. , 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
  156. , 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
  157. , 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
  158. , 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
  159. , 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
  160. , 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
  161. , 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
  162. , 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
  163. , 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
  164. , 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
  165. // round constant
  166. const unsigned char Rcon[11] = {
  167. 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
  168.  
  169.  
  170. // expand the key
  171. void expandKey(unsigned char *expandedKey,
  172. unsigned char *key)
  173. {
  174. unsigned short ii, buf1;
  175. for (ii=0;ii<16;ii++)
  176. expandedKey[ii] = key[ii];
  177. for (ii=1;ii<11;ii++){
  178. buf1 = expandedKey[ii*16 - 4];
  179. expandedKey[ii*16 + 0] = sbox[expandedKey[ii*16 - 3]]^expandedKey[(ii-1)*16 + 0]^Rcon[ii];
  180. expandedKey[ii*16 + 1] = sbox[expandedKey[ii*16 - 2]]^expandedKey[(ii-1)*16 + 1];
  181. expandedKey[ii*16 + 2] = sbox[expandedKey[ii*16 - 1]]^expandedKey[(ii-1)*16 + 2];
  182. expandedKey[ii*16 + 3] = sbox[buf1 ]^expandedKey[(ii-1)*16 + 3];
  183. expandedKey[ii*16 + 4] = expandedKey[(ii-1)*16 + 4]^expandedKey[ii*16 + 0];
  184. expandedKey[ii*16 + 5] = expandedKey[(ii-1)*16 + 5]^expandedKey[ii*16 + 1];
  185. expandedKey[ii*16 + 6] = expandedKey[(ii-1)*16 + 6]^expandedKey[ii*16 + 2];
  186. expandedKey[ii*16 + 7] = expandedKey[(ii-1)*16 + 7]^expandedKey[ii*16 + 3];
  187. expandedKey[ii*16 + 8] = expandedKey[(ii-1)*16 + 8]^expandedKey[ii*16 + 4];
  188. expandedKey[ii*16 + 9] = expandedKey[(ii-1)*16 + 9]^expandedKey[ii*16 + 5];
  189. expandedKey[ii*16 +10] = expandedKey[(ii-1)*16 +10]^expandedKey[ii*16 + 6];
  190. expandedKey[ii*16 +11] = expandedKey[(ii-1)*16 +11]^expandedKey[ii*16 + 7];
  191. expandedKey[ii*16 +12] = expandedKey[(ii-1)*16 +12]^expandedKey[ii*16 + 8];
  192. expandedKey[ii*16 +13] = expandedKey[(ii-1)*16 +13]^expandedKey[ii*16 + 9];
  193. expandedKey[ii*16 +14] = expandedKey[(ii-1)*16 +14]^expandedKey[ii*16 +10];
  194. expandedKey[ii*16 +15] = expandedKey[(ii-1)*16 +15]^expandedKey[ii*16 +11];
  195. }
  196.  
  197.  
  198. }
  199.  
  200. // multiply by 2 in the galois field
  201. unsigned char galois_mul2(unsigned char value)
  202. {
  203. if (value>>7)
  204. {
  205. value = value << 1;
  206. return (value^0x1b);
  207. } else
  208. return value<<1;
  209. }
  210.  
  211. // straight foreward aes encryption implementation
  212. // first the group of operations
  213. // - addroundkey
  214. // - subbytes
  215. // - shiftrows
  216. // - mixcolums
  217. // is executed 9 times, after this addroundkey to finish the 9th round,
  218. // after that the 10th round without mixcolums
  219. // no further subfunctions to save cycles for function calls
  220. // no structuring with "for (....)" to save cycles
  221. void aes_encr(unsigned char *state, unsigned char *expandedKey)
  222. {
  223. unsigned char buf1, buf2, buf3, round;
  224.  
  225.  
  226. for (round = 0; round < 9; round ++){
  227. // addroundkey, sbox and shiftrows
  228. // row 0
  229. state[ 0] = sbox[(state[ 0] ^ expandedKey[(round*16) ])];
  230. state[ 4] = sbox[(state[ 4] ^ expandedKey[(round*16) + 4])];
  231. state[ 8] = sbox[(state[ 8] ^ expandedKey[(round*16) + 8])];
  232. state[12] = sbox[(state[12] ^ expandedKey[(round*16) + 12])];
  233. // row 1
  234. buf1 = state[1] ^ expandedKey[(round*16) + 1];
  235. state[ 1] = sbox[(state[ 5] ^ expandedKey[(round*16) + 5])];
  236. state[ 5] = sbox[(state[ 9] ^ expandedKey[(round*16) + 9])];
  237. state[ 9] = sbox[(state[13] ^ expandedKey[(round*16) + 13])];
  238. state[13] = sbox[buf1];
  239. // row 2
  240. buf1 = state[2] ^ expandedKey[(round*16) + 2];
  241. buf2 = state[6] ^ expandedKey[(round*16) + 6];
  242. state[ 2] = sbox[(state[10] ^ expandedKey[(round*16) + 10])];
  243. state[ 6] = sbox[(state[14] ^ expandedKey[(round*16) + 14])];
  244. state[10] = sbox[buf1];
  245. state[14] = sbox[buf2];
  246. // row 3
  247. buf1 = state[15] ^ expandedKey[(round*16) + 15];
  248. state[15] = sbox[(state[11] ^ expandedKey[(round*16) + 11])];
  249. state[11] = sbox[(state[ 7] ^ expandedKey[(round*16) + 7])];
  250. state[ 7] = sbox[(state[ 3] ^ expandedKey[(round*16) + 3])];
  251. state[ 3] = sbox[buf1];
  252.  
  253. // mixcolums //////////
  254. // col1
  255. buf1 = state[0] ^ state[1] ^ state[2] ^ state[3];
  256. buf2 = state[0];
  257. buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1;
  258. buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1;
  259. buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1;
  260. buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1;
  261. // col2
  262. buf1 = state[4] ^ state[5] ^ state[6] ^ state[7];
  263. buf2 = state[4];
  264. buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1;
  265. buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1;
  266. buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1;
  267. buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1;
  268. // col3
  269. buf1 = state[8] ^ state[9] ^ state[10] ^ state[11];
  270. buf2 = state[8];
  271. buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1;
  272. buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1;
  273. buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1;
  274. buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1;
  275. // col4
  276. buf1 = state[12] ^ state[13] ^ state[14] ^ state[15];
  277. buf2 = state[12];
  278. buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1;
  279. buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1;
  280. buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1;
  281. buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1;
  282.  
  283. }
  284. // 10th round without mixcols
  285. state[ 0] = sbox[(state[ 0] ^ expandedKey[(round*16) ])];
  286. state[ 4] = sbox[(state[ 4] ^ expandedKey[(round*16) + 4])];
  287. state[ 8] = sbox[(state[ 8] ^ expandedKey[(round*16) + 8])];
  288. state[12] = sbox[(state[12] ^ expandedKey[(round*16) + 12])];
  289. // row 1
  290. buf1 = state[1] ^ expandedKey[(round*16) + 1];
  291. state[ 1] = sbox[(state[ 5] ^ expandedKey[(round*16) + 5])];
  292. state[ 5] = sbox[(state[ 9] ^ expandedKey[(round*16) + 9])];
  293. state[ 9] = sbox[(state[13] ^ expandedKey[(round*16) + 13])];
  294. state[13] = sbox[buf1];
  295. // row 2
  296. buf1 = state[2] ^ expandedKey[(round*16) + 2];
  297. buf2 = state[6] ^ expandedKey[(round*16) + 6];
  298. state[ 2] = sbox[(state[10] ^ expandedKey[(round*16) + 10])];
  299. state[ 6] = sbox[(state[14] ^ expandedKey[(round*16) + 14])];
  300. state[10] = sbox[buf1];
  301. state[14] = sbox[buf2];
  302. // row 3
  303. buf1 = state[15] ^ expandedKey[(round*16) + 15];
  304. state[15] = sbox[(state[11] ^ expandedKey[(round*16) + 11])];
  305. state[11] = sbox[(state[ 7] ^ expandedKey[(round*16) + 7])];
  306. state[ 7] = sbox[(state[ 3] ^ expandedKey[(round*16) + 3])];
  307. state[ 3] = sbox[buf1];
  308. // last addroundkey
  309. state[ 0]^=expandedKey[160];
  310. state[ 1]^=expandedKey[161];
  311. state[ 2]^=expandedKey[162];
  312. state[ 3]^=expandedKey[163];
  313. state[ 4]^=expandedKey[164];
  314. state[ 5]^=expandedKey[165];
  315. state[ 6]^=expandedKey[166];
  316. state[ 7]^=expandedKey[167];
  317. state[ 8]^=expandedKey[168];
  318. state[ 9]^=expandedKey[169];
  319. state[10]^=expandedKey[170];
  320. state[11]^=expandedKey[171];
  321. state[12]^=expandedKey[172];
  322. state[13]^=expandedKey[173];
  323. state[14]^=expandedKey[174];
  324. state[15]^=expandedKey[175];
  325. }
  326.  
  327. // straight foreward aes decryption implementation
  328. // the order of substeps is the exact reverse of decryption
  329. // inverse functions:
  330. // - addRoundKey is its own inverse
  331. // - rsbox is inverse of sbox
  332. // - rightshift instead of leftshift
  333. // - invMixColumns = barreto + mixColumns
  334. // no further subfunctions to save cycles for function calls
  335. // no structuring with "for (....)" to save cycles
  336. void aes_decr(unsigned char *state, unsigned char *expandedKey)
  337. {
  338. unsigned char buf1, buf2, buf3;
  339. signed char round;
  340. round = 9;
  341.  
  342. // initial addroundkey
  343. state[ 0]^=expandedKey[160];
  344. state[ 1]^=expandedKey[161];
  345. state[ 2]^=expandedKey[162];
  346. state[ 3]^=expandedKey[163];
  347. state[ 4]^=expandedKey[164];
  348. state[ 5]^=expandedKey[165];
  349. state[ 6]^=expandedKey[166];
  350. state[ 7]^=expandedKey[167];
  351. state[ 8]^=expandedKey[168];
  352. state[ 9]^=expandedKey[169];
  353. state[10]^=expandedKey[170];
  354. state[11]^=expandedKey[171];
  355. state[12]^=expandedKey[172];
  356. state[13]^=expandedKey[173];
  357. state[14]^=expandedKey[174];
  358. state[15]^=expandedKey[175];
  359.  
  360. // 10th round without mixcols
  361. state[ 0] = rsbox[state[ 0]] ^ expandedKey[(round*16) ];
  362. state[ 4] = rsbox[state[ 4]] ^ expandedKey[(round*16) + 4];
  363. state[ 8] = rsbox[state[ 8]] ^ expandedKey[(round*16) + 8];
  364. state[12] = rsbox[state[12]] ^ expandedKey[(round*16) + 12];
  365. // row 1
  366. buf1 = rsbox[state[13]] ^ expandedKey[(round*16) + 1];
  367. state[13] = rsbox[state[ 9]] ^ expandedKey[(round*16) + 13];
  368. state[ 9] = rsbox[state[ 5]] ^ expandedKey[(round*16) + 9];
  369. state[ 5] = rsbox[state[ 1]] ^ expandedKey[(round*16) + 5];
  370. state[ 1] = buf1;
  371. // row 2
  372. buf1 = rsbox[state[ 2]] ^ expandedKey[(round*16) + 10];
  373. buf2 = rsbox[state[ 6]] ^ expandedKey[(round*16) + 14];
  374. state[ 2] = rsbox[state[10]] ^ expandedKey[(round*16) + 2];
  375. state[ 6] = rsbox[state[14]] ^ expandedKey[(round*16) + 6];
  376. state[10] = buf1;
  377. state[14] = buf2;
  378. // row 3
  379. buf1 = rsbox[state[ 3]] ^ expandedKey[(round*16) + 15];
  380. state[ 3] = rsbox[state[ 7]] ^ expandedKey[(round*16) + 3];
  381. state[ 7] = rsbox[state[11]] ^ expandedKey[(round*16) + 7];
  382. state[11] = rsbox[state[15]] ^ expandedKey[(round*16) + 11];
  383. state[15] = buf1;
  384.  
  385. for (round = 8; round >= 0; round--){
  386. // barreto
  387. //col1
  388. buf1 = galois_mul2(galois_mul2(state[0]^state[2]));
  389. buf2 = galois_mul2(galois_mul2(state[1]^state[3]));
  390. state[0] ^= buf1; state[1] ^= buf2; state[2] ^= buf1; state[3] ^= buf2;
  391. //col2
  392. buf1 = galois_mul2(galois_mul2(state[4]^state[6]));
  393. buf2 = galois_mul2(galois_mul2(state[5]^state[7]));
  394. state[4] ^= buf1; state[5] ^= buf2; state[6] ^= buf1; state[7] ^= buf2;
  395. //col3
  396. buf1 = galois_mul2(galois_mul2(state[8]^state[10]));
  397. buf2 = galois_mul2(galois_mul2(state[9]^state[11]));
  398. state[8] ^= buf1; state[9] ^= buf2; state[10] ^= buf1; state[11] ^= buf2;
  399. //col4
  400. buf1 = galois_mul2(galois_mul2(state[12]^state[14]));
  401. buf2 = galois_mul2(galois_mul2(state[13]^state[15]));
  402. state[12] ^= buf1; state[13] ^= buf2; state[14] ^= buf1; state[15] ^= buf2;
  403. // mixcolums //////////
  404. // col1
  405. buf1 = state[0] ^ state[1] ^ state[2] ^ state[3];
  406. buf2 = state[0];
  407. buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1;
  408. buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1;
  409. buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1;
  410. buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1;
  411. // col2
  412. buf1 = state[4] ^ state[5] ^ state[6] ^ state[7];
  413. buf2 = state[4];
  414. buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1;
  415. buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1;
  416. buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1;
  417. buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1;
  418. // col3
  419. buf1 = state[8] ^ state[9] ^ state[10] ^ state[11];
  420. buf2 = state[8];
  421. buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1;
  422. buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1;
  423. buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1;
  424. buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1;
  425. // col4
  426. buf1 = state[12] ^ state[13] ^ state[14] ^ state[15];
  427. buf2 = state[12];
  428. buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1;
  429. buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1;
  430. buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1;
  431. buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1;
  432.  
  433. // addroundkey, rsbox and shiftrows
  434. // row 0
  435. state[ 0] = rsbox[state[ 0]] ^ expandedKey[(round*16) ];
  436. state[ 4] = rsbox[state[ 4]] ^ expandedKey[(round*16) + 4];
  437. state[ 8] = rsbox[state[ 8]] ^ expandedKey[(round*16) + 8];
  438. state[12] = rsbox[state[12]] ^ expandedKey[(round*16) + 12];
  439. // row 1
  440. buf1 = rsbox[state[13]] ^ expandedKey[(round*16) + 1];
  441. state[13] = rsbox[state[ 9]] ^ expandedKey[(round*16) + 13];
  442. state[ 9] = rsbox[state[ 5]] ^ expandedKey[(round*16) + 9];
  443. state[ 5] = rsbox[state[ 1]] ^ expandedKey[(round*16) + 5];
  444. state[ 1] = buf1;
  445. // row 2
  446. buf1 = rsbox[state[ 2]] ^ expandedKey[(round*16) + 10];
  447. buf2 = rsbox[state[ 6]] ^ expandedKey[(round*16) + 14];
  448. state[ 2] = rsbox[state[10]] ^ expandedKey[(round*16) + 2];
  449. state[ 6] = rsbox[state[14]] ^ expandedKey[(round*16) + 6];
  450. state[10] = buf1;
  451. state[14] = buf2;
  452. // row 3
  453. buf1 = rsbox[state[ 3]] ^ expandedKey[(round*16) + 15];
  454. state[ 3] = rsbox[state[ 7]] ^ expandedKey[(round*16) + 3];
  455. state[ 7] = rsbox[state[11]] ^ expandedKey[(round*16) + 7];
  456. state[11] = rsbox[state[15]] ^ expandedKey[(round*16) + 11];
  457. state[15] = buf1;
  458. }
  459.  
  460.  
  461. }
  462.  
  463. // encrypt
  464. void aes_encrypt(unsigned char *state,
  465. unsigned char *key)
  466. {
  467. unsigned char expandedKey[176];
  468.  
  469. expandKey(expandedKey, key); // expand the key into 176 bytes
  470. aes_encr(state, expandedKey);
  471. }
  472. // decrypt
  473. void aes_decrypt(unsigned char *state,
  474. unsigned char *key)
  475. {
  476. unsigned char expandedKey[176];
  477.  
  478. expandKey(expandedKey, key); // expand the key into 176 bytes
  479. aes_decr(state, expandedKey);
  480. }
  481.  
  482. TI_aes.h
  483.  
  484. #ifndef TI_AES
  485. #define TI_AES
  486.  
  487. void aes_encrypt(unsigned char *state, unsigned char *key);
  488. void aes_decrypt(unsigned char *state, unsigned char *key);
  489.  
  490. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement