rijndael.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* Rijndael Block Cipher - rijndael.c
  2. Written by Mike Scott 21st April 1999
  3. mike@compapp.dcu.ie
  4. Permission for free direct or derivative use is granted subject
  5. to compliance with any conditions that the originators of the
  6. algorithm place on its exploitation.
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #ifdef WIN32
  11. #include <tools.h>
  12. #else
  13. #define u8 unsigned char /* 8 bits */
  14. #define u32 unsigned long /* 32 bits */
  15. #define u64 unsigned long long
  16. #endif
  17. /* rotates x one bit to the left */
  18. #define ROTL(x) (((x)>>7)|((x)<<1))
  19. /* Rotates 32-bit word left by 1, 2 or 3 byte */
  20. #define ROTL8(x) (((x)<<8)|((x)>>24))
  21. #define ROTL16(x) (((x)<<16)|((x)>>16))
  22. #define ROTL24(x) (((x)<<24)|((x)>>8))
  23. /* Fixed Data */
  24. static u8 InCo[4]={0xB,0xD,0x9,0xE}; /* Inverse Coefficients */
  25. static u8 fbsub[256];
  26. static u8 rbsub[256];
  27. static u8 ptab[256],ltab[256];
  28. static u32 ftable[256];
  29. static u32 rtable[256];
  30. static u32 rco[30];
  31. /* Parameter-dependent data */
  32. int Nk,Nb,Nr;
  33. u8 fi[24],ri[24];
  34. u32 fkey[120];
  35. u32 rkey[120];
  36. static u32 pack(u8 *b)
  37. { /* pack bytes into a 32-bit Word */
  38. return ((u32)b[3]<<24)|((u32)b[2]<<16)|((u32)b[1]<<8)|(u32)b[0];
  39. }
  40. static void unpack(u32 a,u8 *b)
  41. { /* unpack bytes from a word */
  42. b[0]=(u8)a;
  43. b[1]=(u8)(a>>8);
  44. b[2]=(u8)(a>>16);
  45. b[3]=(u8)(a>>24);
  46. }
  47. static u8 xtime(u8 a)
  48. {
  49. u8 b;
  50. if (a&0x80) b=0x1B;
  51. else b=0;
  52. a<<=1;
  53. a^=b;
  54. return a;
  55. }
  56. static u8 bmul(u8 x,u8 y)
  57. { /* x.y= AntiLog(Log(x) + Log(y)) */
  58. if (x && y) return ptab[(ltab[x]+ltab[y])%255];
  59. else return 0;
  60. }
  61. static u32 SubByte(u32 a)
  62. {
  63. u8 b[4];
  64. unpack(a,b);
  65. b[0]=fbsub[b[0]];
  66. b[1]=fbsub[b[1]];
  67. b[2]=fbsub[b[2]];
  68. b[3]=fbsub[b[3]];
  69. return pack(b);
  70. }
  71. static u8 product(u32 x,u32 y)
  72. { /* dot product of two 4-byte arrays */
  73. u8 xb[4],yb[4];
  74. unpack(x,xb);
  75. unpack(y,yb);
  76. return bmul(xb[0],yb[0])^bmul(xb[1],yb[1])^bmul(xb[2],yb[2])^bmul(xb[3],yb[3]);
  77. }
  78. static u32 InvMixCol(u32 x)
  79. { /* matrix Multiplication */
  80. u32 y,m;
  81. u8 b[4];
  82. m=pack(InCo);
  83. b[3]=product(m,x);
  84. m=ROTL24(m);
  85. b[2]=product(m,x);
  86. m=ROTL24(m);
  87. b[1]=product(m,x);
  88. m=ROTL24(m);
  89. b[0]=product(m,x);
  90. y=pack(b);
  91. return y;
  92. }
  93. u8 ByteSub(u8 x)
  94. {
  95. u8 y=ptab[255-ltab[x]]; /* multiplicative inverse */
  96. x=y; x=ROTL(x);
  97. y^=x; x=ROTL(x);
  98. y^=x; x=ROTL(x);
  99. y^=x; x=ROTL(x);
  100. y^=x; y^=0x63;
  101. return y;
  102. }
  103. void gentables(void)
  104. { /* generate tables */
  105. int i;
  106. u8 y,b[4];
  107. /* use 3 as primitive root to generate power and log tables */
  108. ltab[0]=0;
  109. ptab[0]=1; ltab[1]=0;
  110. ptab[1]=3; ltab[3]=1;
  111. for (i=2;i<256;i++)
  112. {
  113. ptab[i]=ptab[i-1]^xtime(ptab[i-1]);
  114. ltab[ptab[i]]=i;
  115. }
  116. /* affine transformation:- each bit is xored with itself shifted one bit */
  117. fbsub[0]=0x63;
  118. rbsub[0x63]=0;
  119. for (i=1;i<256;i++)
  120. {
  121. y=ByteSub((u8)i);
  122. fbsub[i]=y; rbsub[y]=i;
  123. }
  124. for (i=0,y=1;i<30;i++)
  125. {
  126. rco[i]=y;
  127. y=xtime(y);
  128. }
  129. /* calculate forward and reverse tables */
  130. for (i=0;i<256;i++)
  131. {
  132. y=fbsub[i];
  133. b[3]=y^xtime(y); b[2]=y;
  134. b[1]=y; b[0]=xtime(y);
  135. ftable[i]=pack(b);
  136. y=rbsub[i];
  137. b[3]=bmul(InCo[0],y); b[2]=bmul(InCo[1],y);
  138. b[1]=bmul(InCo[2],y); b[0]=bmul(InCo[3],y);
  139. rtable[i]=pack(b);
  140. }
  141. }
  142. void gkey(int nb,int nk,char *key)
  143. { /* blocksize=32*nb bits. Key=32*nk bits */
  144. /* currently nb,bk = 4, 6 or 8 */
  145. /* key comes as 4*Nk bytes */
  146. /* Key Scheduler. Create expanded encryption key */
  147. int i,j,k,m,N;
  148. int C1,C2,C3;
  149. u32 CipherKey[8];
  150. Nb=nb; Nk=nk;
  151. /* Nr is number of rounds */
  152. if (Nb>=Nk) Nr=6+Nb;
  153. else Nr=6+Nk;
  154. C1=1;
  155. if (Nb<8) { C2=2; C3=3; }
  156. else { C2=3; C3=4; }
  157. /* pre-calculate forward and reverse increments */
  158. for (m=j=0;j<nb;j++,m+=3)
  159. {
  160. fi[m]=(j+C1)%nb;
  161. fi[m+1]=(j+C2)%nb;
  162. fi[m+2]=(j+C3)%nb;
  163. ri[m]=(nb+j-C1)%nb;
  164. ri[m+1]=(nb+j-C2)%nb;
  165. ri[m+2]=(nb+j-C3)%nb;
  166. }
  167. N=Nb*(Nr+1);
  168. for (i=j=0;i<Nk;i++,j+=4)
  169. {
  170. CipherKey[i]=pack((u8 *)&key[j]);
  171. }
  172. for (i=0;i<Nk;i++) fkey[i]=CipherKey[i];
  173. for (j=Nk,k=0;j<N;j+=Nk,k++)
  174. {
  175. fkey[j]=fkey[j-Nk]^SubByte(ROTL24(fkey[j-1]))^rco[k];
  176. if (Nk<=6)
  177. {
  178. for (i=1;i<Nk && (i+j)<N;i++)
  179. fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
  180. }
  181. else
  182. {
  183. for (i=1;i<4 &&(i+j)<N;i++)
  184. fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
  185. if ((j+4)<N) fkey[j+4]=fkey[j+4-Nk]^SubByte(fkey[j+3]);
  186. for (i=5;i<Nk && (i+j)<N;i++)
  187. fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
  188. }
  189. }
  190. /* now for the expanded decrypt key in reverse order */
  191. for (j=0;j<Nb;j++) rkey[j+N-Nb]=fkey[j];
  192. for (i=Nb;i<N-Nb;i+=Nb)
  193. {
  194. k=N-Nb-i;
  195. for (j=0;j<Nb;j++) rkey[k+j]=InvMixCol(fkey[i+j]);
  196. }
  197. for (j=N-Nb;j<N;j++) rkey[j-N+Nb]=fkey[j];
  198. }
  199. /* There is an obvious time/space trade-off possible here. *
  200. * Instead of just one ftable[], I could have 4, the other *
  201. * 3 pre-rotated to save the ROTL8, ROTL16 and ROTL24 overhead */
  202. void encrypt(char *buff)
  203. {
  204. int i,j,k,m;
  205. u32 a[8],b[8],*x,*y,*t;
  206. for (i=j=0;i<Nb;i++,j+=4)
  207. {
  208. a[i]=pack((u8 *)&buff[j]);
  209. a[i]^=fkey[i];
  210. }
  211. k=Nb;
  212. x=a; y=b;
  213. /* State alternates between a and b */
  214. for (i=1;i<Nr;i++)
  215. { /* Nr is number of rounds. May be odd. */
  216. /* if Nb is fixed - unroll this next
  217. loop and hard-code in the values of fi[] */
  218. for (m=j=0;j<Nb;j++,m+=3)
  219. { /* deal with each 32-bit element of the State */
  220. /* This is the time-critical bit */
  221. y[j]=fkey[k++]^ftable[(u8)x[j]]^
  222. ROTL8(ftable[(u8)(x[fi[m]]>>8)])^
  223. ROTL16(ftable[(u8)(x[fi[m+1]]>>16)])^
  224. ROTL24(ftable[(u8)(x[fi[m+2]]>>24)]);
  225. }
  226. t=x; x=y; y=t; /* swap pointers */
  227. }
  228. /* Last Round - unroll if possible */
  229. for (m=j=0;j<Nb;j++,m+=3)
  230. {
  231. y[j]=fkey[k++]^(u32)fbsub[(u8)x[j]]^
  232. ROTL8((u32)fbsub[(u8)(x[fi[m]]>>8)])^
  233. ROTL16((u32)fbsub[(u8)(x[fi[m+1]]>>16)])^
  234. ROTL24((u32)fbsub[(u8)(x[fi[m+2]]>>24)]);
  235. }
  236. for (i=j=0;i<Nb;i++,j+=4)
  237. {
  238. unpack(y[i],(u8 *)&buff[j]);
  239. x[i]=y[i]=0; /* clean up stack */
  240. }
  241. return;
  242. }
  243. void decrypt(char *buff)
  244. {
  245. int i,j,k,m;
  246. u32 a[8],b[8],*x,*y,*t;
  247. for (i=j=0;i<Nb;i++,j+=4)
  248. {
  249. a[i]=pack((u8 *)&buff[j]);
  250. a[i]^=rkey[i];
  251. }
  252. k=Nb;
  253. x=a; y=b;
  254. /* State alternates between a and b */
  255. for (i=1;i<Nr;i++)
  256. { /* Nr is number of rounds. May be odd. */
  257. /* if Nb is fixed - unroll this next
  258. loop and hard-code in the values of ri[] */
  259. for (m=j=0;j<Nb;j++,m+=3)
  260. { /* This is the time-critical bit */
  261. y[j]=rkey[k++]^rtable[(u8)x[j]]^
  262. ROTL8(rtable[(u8)(x[ri[m]]>>8)])^
  263. ROTL16(rtable[(u8)(x[ri[m+1]]>>16)])^
  264. ROTL24(rtable[(u8)(x[ri[m+2]]>>24)]);
  265. }
  266. t=x; x=y; y=t; /* swap pointers */
  267. }
  268. /* Last Round - unroll if possible */
  269. for (m=j=0;j<Nb;j++,m+=3)
  270. {
  271. y[j]=rkey[k++]^(u32)rbsub[(u8)x[j]]^
  272. ROTL8((u32)rbsub[(u8)(x[ri[m]]>>8)])^
  273. ROTL16((u32)rbsub[(u8)(x[ri[m+1]]>>16)])^
  274. ROTL24((u32)rbsub[(u8)(x[ri[m+2]]>>24)]);
  275. }
  276. for (i=j=0;i<Nb;i++,j+=4)
  277. {
  278. unpack(y[i],(u8 *)&buff[j]);
  279. x[i]=y[i]=0; /* clean up stack */
  280. }
  281. return;
  282. }
  283. void aes_set_key(u8 *key) {
  284. gentables();
  285. gkey(4, 4,(char*) key);
  286. }
  287. // CBC mode decryption
  288. void aes_decrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) {
  289. u8 block[16];
  290. u8 *ctext_ptr;
  291. unsigned int blockno = 0, i;
  292. //printf("aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len);
  293. for (blockno = 0; blockno <= (len / sizeof(block)); blockno++) {
  294. unsigned int fraction;
  295. if (blockno == (len / sizeof(block))) { // last block
  296. fraction = len % sizeof(block);
  297. if (fraction == 0) break;
  298. memset(block, 0, sizeof(block));
  299. } else fraction = 16;
  300. // debug_printf("block %d: fraction = %d\n", blockno, fraction);
  301. memcpy(block, inbuf + blockno * sizeof(block), fraction);
  302. decrypt((char*)block);
  303. if (blockno == 0) {
  304. ctext_ptr = iv;
  305. }
  306. else {
  307. ctext_ptr = inbuf + (blockno-1) * sizeof(block);
  308. }
  309. for(i=0; i < fraction; i++)
  310. outbuf[blockno * sizeof(block) + i] =
  311. ctext_ptr[i] ^ block[i];
  312. // debug_printf("Block %d output: ", blockno);
  313. // hexdump(outbuf + blockno*sizeof(block), 16);
  314. }
  315. }
  316. // CBC mode encryption
  317. void aes_encrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) {
  318. u8 block[16];
  319. unsigned int blockno = 0, i;
  320. // debug_printf("aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len);
  321. for (blockno = 0; blockno <= (len / sizeof(block)); blockno++) {
  322. unsigned int fraction;
  323. if (blockno == (len / sizeof(block))) { // last block
  324. fraction = len % sizeof(block);
  325. if (fraction == 0) break;
  326. memset(block, 0, sizeof(block));
  327. } else fraction = 16;
  328. // debug_printf("block %d: fraction = %d\n", blockno, fraction);
  329. memcpy(block, inbuf + blockno * sizeof(block), fraction);
  330. for(i=0; i < fraction; i++)
  331. block[i] = inbuf[blockno * sizeof(block) + i] ^ iv[i];
  332. encrypt((char*)block);
  333. memcpy(iv, block, sizeof(block));
  334. memcpy(outbuf + blockno * sizeof(block), block, sizeof(block));
  335. // debug_printf("Block %d output: ", blockno);
  336. // hexdump(outbuf + blockno*sizeof(block), 16);
  337. }
  338. }