Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 10.67 KB | None | 0 0
  1. package require tcc4tcl
  2. package require aes
  3.  
  4. set tcc4tcl [tcc4tcl::new]
  5.  
  6. $tcc4tcl ccode {
  7.   #include <string.h>
  8.   unsigned char Sbox[256] = {
  9.     0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  10.     0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  11.     0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  12.     0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  13.     0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  14.     0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  15.     0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  16.     0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  17.     0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  18.     0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  19.     0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  20.     0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  21.     0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  22.     0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  23.     0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  24.     0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  25.     0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  26.     0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  27.     0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  28.     0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  29.     0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  30.     0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  31.     0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  32.     0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  33.     0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  34.     0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  35.     0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  36.     0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  37.     0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  38.     0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  39.     0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  40.     0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  41.   };
  42.  
  43.   unsigned char xtime[256] = {
  44.     0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
  45.     0x20, 0x22, 0x24, 0x26, 0x28, 0x2a, 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
  46.     0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e,
  47.     0x60, 0x62, 0x64, 0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c, 0x7e,
  48.     0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9c, 0x9e,
  49.     0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac, 0xae, 0xb0, 0xb2, 0xb4, 0xb6, 0xb8, 0xba, 0xbc, 0xbe,
  50.     0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
  51.     0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
  52.     0x1b, 0x19, 0x1f, 0x1d, 0x13, 0x11, 0x17, 0x15, 0x0b, 0x09, 0x0f, 0x0d, 0x03, 0x01, 0x07, 0x05,
  53.     0x3b, 0x39, 0x3f, 0x3d, 0x33, 0x31, 0x37, 0x35, 0x2b, 0x29, 0x2f, 0x2d, 0x23, 0x21, 0x27, 0x25,
  54.     0x5b, 0x59, 0x5f, 0x5d, 0x53, 0x51, 0x57, 0x55, 0x4b, 0x49, 0x4f, 0x4d, 0x43, 0x41, 0x47, 0x45,
  55.     0x7b, 0x79, 0x7f, 0x7d, 0x73, 0x71, 0x77, 0x75, 0x6b, 0x69, 0x6f, 0x6d, 0x63, 0x61, 0x67, 0x65,
  56.     0x9b, 0x99, 0x9f, 0x9d, 0x93, 0x91, 0x97, 0x95, 0x8b, 0x89, 0x8f, 0x8d, 0x83, 0x81, 0x87, 0x85,
  57.     0xbb, 0xb9, 0xbf, 0xbd, 0xb3, 0xb1, 0xb7, 0xb5, 0xab, 0xa9, 0xaf, 0xad, 0xa3, 0xa1, 0xa7, 0xa5,
  58.     0xdb, 0xd9, 0xdf, 0xdd, 0xd3, 0xd1, 0xd7, 0xd5, 0xcb, 0xc9, 0xcf, 0xcd, 0xc3, 0xc1, 0xc7, 0xc5,
  59.     0xfb, 0xf9, 0xff, 0xfd, 0xf3, 0xf1, 0xf7, 0xf5, 0xeb, 0xe9, 0xef, 0xed, 0xe3, 0xe1, 0xe7, 0xe5
  60.   };
  61.  
  62.   unsigned char Xobs[256] = {
  63.     0x52, 0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
  64.     0x7c, 0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
  65.     0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
  66.     0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
  67.     0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
  68.     0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
  69.     0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
  70.     0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
  71.     0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
  72.     0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
  73.     0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
  74.     0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
  75.     0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
  76.     0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
  77.     0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
  78.     0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d,
  79.   };
  80. }
  81.  
  82. $tcc4tcl ccommand MixColumns {cd interp objc objv} {
  83.   Tcl_Obj * resObj = Tcl_NewListObj (0,0);
  84.   Tcl_Obj * tempObj = NULL;
  85.   int i;
  86.   int r0,r1,r2,r3,s0,s1,s2,s3;
  87.   Tcl_WideInt w;
  88.   for (i=0;i<4;i++) {
  89.     Tcl_ListObjIndex(interp,objv[1],i,&tempObj);
  90.     Tcl_GetWideIntFromObj(interp,tempObj,&w);
  91.     r0= (w >> 24) & 255;
  92.     r1= (w >> 16) & 255;
  93.  
  94.     r2= (w >> 8 ) & 255;
  95.     r3=  w        & 255;
  96.     s0= xtime[r0] ^ (r1^xtime[r1]) ^ r2 ^ r3;
  97.     s1= r0 ^ xtime[r1] ^ (r2^xtime[r2])^ r3;
  98.     s2= r0 ^ r1 ^ (xtime[r2]) ^ (r3^xtime[r3]);
  99.     s3= r0^xtime[r0] ^ r1 ^ r2 ^ xtime[r3];
  100.     Tcl_Obj * l=Tcl_NewWideIntObj((s0 << 24) | (s1 << 16) | (s2 << 8) | s3 );
  101.     Tcl_ListObjAppendElement(interp,resObj,l);
  102.   }
  103.   Tcl_SetObjResult(interp,resObj);
  104.   return TCL_OK;
  105. }
  106.  
  107. $tcc4tcl ccommand InvMixColumns {cd interp objc objv} {
  108.   Tcl_Obj * resObj = Tcl_NewListObj (0,0);
  109.   Tcl_Obj * tempObj = NULL;
  110.   int i;
  111.   int r0,r1,r2,r3,s0,s1,s2,s3;
  112.   Tcl_WideInt w;
  113.   for (i=0;i<4;i++) {
  114.     Tcl_ListObjIndex(interp,objv[1],i,&tempObj);
  115.     Tcl_GetWideIntFromObj(interp,tempObj,&w);
  116.     r0= (w >> 24) & 255;
  117.     r1= (w >> 16) & 255;
  118.     r2= (w >> 8 ) & 255;
  119.     r3=  w        & 255;
  120.  
  121.     s0= (xtime[xtime[xtime[r0]]] ^ (xtime[xtime[r0]]) ^ xtime[r0])  ^  (xtime[xtime[xtime[r1]]] ^ xtime[r1] ^ r1)  ^  (xtime[xtime[xtime[r2]]] ^ (xtime[xtime[r2]] ^ r2))  ^  (xtime[xtime[xtime[r3]]] ^ r3);
  122.     s1=(xtime[xtime[xtime[r0]]] ^ r0)  ^  (xtime[xtime[xtime[r1]]] ^ (xtime[xtime[r1]]) ^ xtime[r1])  ^  (xtime[xtime[xtime[r2]]]^xtime[r2]^r2) ^ (xtime[xtime[xtime[r3]]]^(xtime[xtime[r3]] ^ r3));
  123.     s2=(xtime[xtime[xtime[r0]]] ^ (xtime[xtime[r0]] ^ r0)) ^ (xtime[xtime[xtime[r1]]] ^ r1)  ^  (xtime[xtime[xtime[r2]]] ^ (xtime[xtime[r2]]) ^ xtime[r2])  ^  (xtime[xtime[xtime[r3]]]^xtime[r3]^r3);
  124.     s3=(xtime[xtime[xtime[r0]]]^xtime[r0]^r0)  ^  (xtime[xtime[xtime[r1]]]^(xtime[xtime[r1]] ^ r1))  ^  (xtime[xtime[xtime[r2]]] ^ r2)  ^  (xtime[xtime[xtime[r3]]] ^ (xtime[xtime[r3]]) ^ xtime[r3]);
  125.  
  126.     Tcl_Obj * l=Tcl_NewWideIntObj((s0 << 24) | (s1 << 16) | (s2 << 8) | s3 );
  127.     Tcl_ListObjAppendElement(interp,resObj,l);
  128.   }
  129.   Tcl_SetObjResult(interp,resObj);
  130.   return TCL_OK;
  131. }
  132.  
  133. $tcc4tcl ccommand ShiftRows {cd interp obj objv} {
  134.   SubBytes(interp,objv);
  135.   int n0,n1,n2,n3;
  136.   Tcl_Obj * tempObj = NULL;
  137.   Tcl_Obj * resObj = Tcl_NewListObj (0,0);
  138.   Tcl_WideInt n0l;
  139.   Tcl_WideInt n1l;
  140.   Tcl_WideInt n2l;
  141.   Tcl_WideInt n3l;
  142.   for (n0=0;n0 < 4; n0++) {
  143.     n1= (n0 + 1) % 4;
  144.     n2= (n0 + 2) % 4;
  145.     n3= (n0 + 3) % 4;
  146.     and Tcl_ListObjIndex(interp,objv[1],n0,&tempObj);
  147.     Tcl_GetWideIntFromObj(interp,tempObj,&n0l);
  148.     Tcl_ListObjIndex(interp,objv[1],n1,&tempObj);
  149.     Tcl_GetWideIntFromObj(interp,tempObj,&n1l);
  150.     Tcl_ListObjIndex(interp,objv[1],n2,&tempObj);
  151.     Tcl_GetWideIntFromObj(interp,tempObj,&n2l);
  152.     Tcl_ListObjIndex(interp,objv[1],n3,&tempObj);
  153.     Tcl_GetWideIntFromObj(interp,tempObj,&n3l);
  154.     Tcl_Obj * l=Tcl_NewWideIntObj((n0l & 0xff000000) | (n1l & 0x00ff0000) | (n2l & 0x0000ff00) | (n3l& 0x000000ff));
  155.     Tcl_ListObjAppendElement(interp,resObj,l);
  156.   }
  157.   Tcl_SetObjResult(interp,resObj);
  158.   return TCL_OK;
  159. }
  160.  
  161. $tcc4tcl ccommand InvShiftRows {cd interp obj objv} {
  162.   int n0,n1,n2,n3;
  163.   Tcl_Obj * tempObj = NULL;
  164.   Tcl_WideInt n0l;
  165.   Tcl_WideInt n1l;
  166.   Tcl_WideInt n2l;
  167.   Tcl_WideInt n3l;
  168.   for (n0=0;n0 < 4; n0++) {
  169.     n1= (n0 + 1) % 4;
  170.     n2= (n0 + 2) % 4;
  171.     n3= (n0 + 3) % 4;
  172.     Tcl_ListObjIndex(interp,objv[1],n0,&tempObj);
  173.     Tcl_GetWideIntFromObj(interp,tempObj,&n0l);
  174.     Tcl_ListObjIndex(interp,objv[1],n1,&tempObj);
  175.     Tcl_GetWideIntFromObj(interp,tempObj,&n1l);
  176.     Tcl_ListObjIndex(interp,objv[1],n2,&tempObj);
  177.     Tcl_GetWideIntFromObj(interp,tempObj,&n2l);
  178.     Tcl_ListObjIndex(interp,objv[1],n3,&tempObj);
  179.     Tcl_GetWideIntFromObj(interp,tempObj,&n3l);
  180.     Tcl_Obj * l=Tcl_NewWideIntObj((n0l & 0xff000000) | (n3l & 0x00ff0000) | (n2l & 0x0000ff00) | (n1l& 0x000000ff));
  181.   }
  182.   return TCL_OK;
  183.  }
  184.  
  185. $tcc4tcl ccommand SubWord {cd interp obj objv} {
  186.   int s0,s1,s2,s3;
  187.   Tcl_WideInt w;
  188.   Tcl_GetWideIntFromObj(interp,objv[1],&w);
  189.   s3=Sbox[((w >> 24) & 255)];
  190.   s2=Sbox[((w >> 16) & 255)];
  191.   s1=Sbox[((w >> 8 ) & 255)];
  192.   s0=Sbox[(w        & 255)];
  193.   Tcl_Obj * resObj = Tcl_NewWideIntObj((s3 << 24) | (s2 << 16) | (s1 << 8) | s0);
  194.   Tcl_SetObjResult(interp,resObj);
  195.   return TCL_OK;
  196. }
  197.  
  198. $tcc4tcl ccommand InvSubWord {cd interp obj objv} {
  199.   int s0,s1,s2,s3;
  200.   Tcl_WideInt w;
  201.   Tcl_GetWideIntFromObj(interp,objv[1],&w);
  202.   s3=Xobs[((w >> 24) & 255)];
  203.   s2=Xobs[((w >> 16) & 255)];
  204.   s1=Xobs[((w >> 8 ) & 255)];
  205.   s0=Xobs[(w         & 255)];
  206.   Tcl_Obj * resObj = Tcl_NewWideIntObj((s3 << 24) | (s2 << 16) | (s1 << 8) | s0);
  207.   Tcl_SetObjResult(interp,resObj);
  208.   return TCL_OK;
  209. }
  210.  
  211. $tcc4tcl ccommand SubBytes {cd interp obj objv} {
  212.   int i;
  213.   int s0,s1,s2,s3;
  214.   Tcl_Obj * tempObj = NULL;
  215.   Tcl_WideInt w;
  216.   for (i=0;i<4;i++) {
  217.     Tcl_ListObjIndex(interp,objv[1],i,&tempObj);
  218.     Tcl_GetWideIntFromObj(interp,tempObj,&w);
  219.     s3=Sbox[((w >> 24) & 255)];
  220.     s2=Sbox[((w >> 16) & 255)];
  221.     s1=Sbox[((w >> 8 ) & 255)];
  222.     s0=Sbox[(w        & 255)];
  223.     tempObj = Tcl_NewWideIntObj((s3 << 24) | (s2 << 16) | (s1 << 8) | s0);
  224.   }
  225.   return TCL_OK;
  226. }
  227.  
  228. $tcc4tcl ccommand InvSubBytes {cd interp obj objv} {
  229.   Tcl_Obj * listObj=InvShiftRows(interp,objv);
  230.   int i;
  231.   int s0,s1,s2,s3;
  232.   Tcl_Obj * tempObj = NULL;
  233.   Tcl_Obj * resObj = Tcl_NewListObj (0,0);
  234.   Tcl_WideInt w;
  235.   for (i=0;i<4;i++) {
  236.     Tcl_ListObjIndex(interp,listObj,i,&tempObj);
  237.     Tcl_GetWideIntFromObj(interp,tempObj,&w);
  238.     s3=Xobs[((w >> 24) & 255)];
  239.     s2=Xobs[((w >> 16) & 255)];
  240.     s1=Xobs[((w >> 8 ) & 255)];
  241.     s0=Xobs[(w        & 255)];
  242.     tempObj = Tcl_NewWideIntObj((s3 << 24) | (s2 << 16) | (s1 << 8) | s0);
  243.     Tcl_ListObjAppendElement(interp,resObj,tempObj);
  244.   }
  245.   Tcl_DecrRefCount(listObj);
  246.   Tcl_SetObjResult(interp,resObj);
  247.   return TCL_OK;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement