开源的socket服务端客户端,支持C# C++

crypto.cpp 89KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. #include "stdafx.h"
  2. #include "crypto.h"
  3. /****************************** MACROS ******************************/
  4. #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
  5. #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
  6. // -------------------------------------------------- BASE64 -------------------------------------------------- //
  7. /****************************** MACROS ******************************/
  8. #define NEWLINE_INVL 76
  9. /**************************** VARIABLES *****************************/
  10. // Note: To change the charset to a URL encoding, replace the '+' and '/' with '*' and '-'
  11. static const BYTE charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
  12. /*********************** FUNCTION DEFINITIONS ***********************/
  13. BYTE revchar(char ch)
  14. {
  15. if (ch >= 'A' && ch <= 'Z')
  16. ch -= 'A';
  17. else if (ch >= 'a' && ch <='z')
  18. ch = ch - 'a' + 26;
  19. else if (ch >= '0' && ch <='9')
  20. ch = ch - '0' + 52;
  21. else if (ch == '+')
  22. ch = 62;
  23. else if (ch == '/')
  24. ch = 63;
  25. return(ch);
  26. }
  27. size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag)
  28. {
  29. size_t idx, idx2, blks, blk_ceiling, left_over, newline_count = 0;
  30. blks = (len / 3);
  31. left_over = len % 3;
  32. if (out == nullptr) {
  33. idx2 = blks * 4 ;
  34. if (left_over)
  35. idx2 += 4;
  36. if (newline_flag)
  37. idx2 += len / 57; // (NEWLINE_INVL / 4) * 3 = 57. One newline per 57 input bytes.
  38. }
  39. else {
  40. // Since 3 input bytes = 4 output bytes, determine out how many even sets of
  41. // 3 bytes the input has.
  42. blk_ceiling = blks * 3;
  43. for (idx = 0, idx2 = 0; idx < blk_ceiling; idx += 3, idx2 += 4) {
  44. out[idx2] = charset[in[idx] >> 2];
  45. out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
  46. out[idx2 + 2] = charset[((in[idx + 1] & 0x0f) << 2) | (in[idx + 2] >> 6)];
  47. out[idx2 + 3] = charset[in[idx + 2] & 0x3F];
  48. // The offical standard requires a newline every 76 characters.
  49. // (Eg, first newline is character 77 of the output.)
  50. if (((idx2 - newline_count + 4) % NEWLINE_INVL == 0) && newline_flag) {
  51. out[idx2 + 4] = '\n';
  52. idx2++;
  53. newline_count++;
  54. }
  55. }
  56. if (left_over == 1) {
  57. out[idx2] = charset[in[idx] >> 2];
  58. out[idx2 + 1] = charset[(in[idx] & 0x03) << 4];
  59. out[idx2 + 2] = '=';
  60. out[idx2 + 3] = '=';
  61. idx2 += 4;
  62. }
  63. else if (left_over == 2) {
  64. out[idx2] = charset[in[idx] >> 2];
  65. out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
  66. out[idx2 + 2] = charset[(in[idx + 1] & 0x0F) << 2];
  67. out[idx2 + 3] = '=';
  68. idx2 += 4;
  69. }
  70. }
  71. return(idx2);
  72. }
  73. size_t base64_decode(const BYTE in[], BYTE out[], size_t len)
  74. {
  75. size_t idx, idx2, blks, blk_ceiling, left_over;
  76. if (in[len - 1] == '=')
  77. len--;
  78. if (in[len - 1] == '=')
  79. len--;
  80. blks = len / 4;
  81. left_over = len % 4;
  82. if (out == nullptr) {
  83. if (len >= 77 && in[NEWLINE_INVL] == '\n') // Verify that newlines where used.
  84. len -= len / (NEWLINE_INVL + 1);
  85. blks = len / 4;
  86. left_over = len % 4;
  87. idx = blks * 3;
  88. if (left_over == 2)
  89. idx ++;
  90. else if (left_over == 3)
  91. idx += 2;
  92. }
  93. else {
  94. blk_ceiling = blks * 4;
  95. for (idx = 0, idx2 = 0; idx2 < blk_ceiling; idx += 3, idx2 += 4) {
  96. if (in[idx2] == '\n')
  97. idx2++;
  98. out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
  99. out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
  100. out[idx + 2] = (revchar(in[idx2 + 2]) << 6) | revchar(in[idx2 + 3]);
  101. }
  102. if (left_over == 2) {
  103. out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
  104. idx++;
  105. }
  106. else if (left_over == 3) {
  107. out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
  108. out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
  109. idx += 2;
  110. }
  111. }
  112. return(idx);
  113. }
  114. // -------------------------------------------------- URL -------------------------------------------------- //
  115. #define HEX_CHAR_TO_VALUE(c) (c <= '9' ? c - '0' : (c <= 'F' ? c - 'A' + 0x0A : c - 'a' + 0X0A))
  116. #define HEX_DOUBLE_CHAR_TO_VALUE(pc) (((HEX_CHAR_TO_VALUE(*(pc))) << 4) | (HEX_CHAR_TO_VALUE(*(pc + 1))))
  117. #define HEX_VALUE_TO_CHAR(n) (n <= 9 ? n + '0' : (n <= 'F' ? n + 'A' - 0X0A : n + 'a' - 0X0A))
  118. #define HEX_VALUE_TO_DOUBLE_CHAR(pc, n) {*(pc) = HEX_VALUE_TO_CHAR((n >> 4)); *((pc) + 1) = HEX_VALUE_TO_CHAR((n & 0X0F));}
  119. int url_encode(const char* src, const int src_size, char* dest, const int dest_size)
  120. {
  121. if(src == nullptr || dest == nullptr || src_size <= 0 || dest_size <= 0)
  122. return 0;
  123. char ch;
  124. int j = 0;
  125. for(int i = 0; (i < src_size) && (j < dest_size); ++i)
  126. {
  127. ch = src[i];
  128. if (((ch>='A') && (ch<'Z')) ||
  129. ((ch>='a') && (ch<'z')) ||
  130. ((ch>='0') && (ch<'9')) ||
  131. (ch == '.' || ch == '-' || ch == '_' || ch == '*'))
  132. dest[j++] = ch;
  133. else if(ch == ' ')
  134. dest[j++] = '+';
  135. else {
  136. if(j + 3 < dest_size)
  137. {
  138. dest[j++] = '%';
  139. HEX_VALUE_TO_DOUBLE_CHAR(dest + j, ch);
  140. j += 2;
  141. }
  142. else
  143. return 0;
  144. }
  145. }
  146. dest[j] = '\0';
  147. return j;
  148. }
  149. int url_decode(const char* src, const int src_size, char* dest, const int dest_size)
  150. {
  151. if(src == nullptr || dest == nullptr || src_size <= 0 || dest_size <= 0)
  152. return 0;
  153. char ch;
  154. int j = 0;
  155. for(int i = 0; i < src_size && j < dest_size; ++i)
  156. {
  157. ch = src[i];
  158. if(ch == '+')
  159. dest[j++] = ' ';
  160. else if(ch == '%')
  161. {
  162. if(i + 2 < src_size)
  163. {
  164. dest[j++] = HEX_DOUBLE_CHAR_TO_VALUE(src + i + 1);
  165. i += 2;
  166. }
  167. }
  168. else
  169. dest[j++] = ch;
  170. }
  171. dest[j] = 0;
  172. return j;
  173. }
  174. // -------------------------------------------------- AES -------------------------------------------------- //
  175. /****************************** MACROS ******************************/
  176. // The least significant byte of the word is rotated to the end.
  177. #define KE_ROTWORD(x) (((x) << 8) | ((x) >> 24))
  178. /**************************** DATA TYPES ****************************/
  179. #define AES_128_ROUNDS 10
  180. #define AES_192_ROUNDS 12
  181. #define AES_256_ROUNDS 14
  182. /*********************** FUNCTION DECLARATIONS **********************/
  183. void ccm_prepare_first_ctr_blk(BYTE counter[], const BYTE nonce[], int nonce_len, int payload_len_store_size);
  184. void ccm_prepare_first_format_blk(BYTE buf[], int assoc_len, int payload_len, int payload_len_store_size, int mac_len, const BYTE nonce[], int nonce_len);
  185. void ccm_format_assoc_data(BYTE buf[], int *end_of_buf, const BYTE assoc[], int assoc_len);
  186. void ccm_format_payload_data(BYTE buf[], int *end_of_buf, const BYTE payload[], int payload_len);
  187. /**************************** VARIABLES *****************************/
  188. // This is the specified AES SBox. To look up a substitution value, put the first
  189. // nibble in the first index (row) and the second nibble in the second index (column).
  190. static const BYTE aes_sbox[16][16] = {
  191. {0x63,0x7C,0x77,0x7B,0xF2,0x6B,0x6F,0xC5,0x30,0x01,0x67,0x2B,0xFE,0xD7,0xAB,0x76},
  192. {0xCA,0x82,0xC9,0x7D,0xFA,0x59,0x47,0xF0,0xAD,0xD4,0xA2,0xAF,0x9C,0xA4,0x72,0xC0},
  193. {0xB7,0xFD,0x93,0x26,0x36,0x3F,0xF7,0xCC,0x34,0xA5,0xE5,0xF1,0x71,0xD8,0x31,0x15},
  194. {0x04,0xC7,0x23,0xC3,0x18,0x96,0x05,0x9A,0x07,0x12,0x80,0xE2,0xEB,0x27,0xB2,0x75},
  195. {0x09,0x83,0x2C,0x1A,0x1B,0x6E,0x5A,0xA0,0x52,0x3B,0xD6,0xB3,0x29,0xE3,0x2F,0x84},
  196. {0x53,0xD1,0x00,0xED,0x20,0xFC,0xB1,0x5B,0x6A,0xCB,0xBE,0x39,0x4A,0x4C,0x58,0xCF},
  197. {0xD0,0xEF,0xAA,0xFB,0x43,0x4D,0x33,0x85,0x45,0xF9,0x02,0x7F,0x50,0x3C,0x9F,0xA8},
  198. {0x51,0xA3,0x40,0x8F,0x92,0x9D,0x38,0xF5,0xBC,0xB6,0xDA,0x21,0x10,0xFF,0xF3,0xD2},
  199. {0xCD,0x0C,0x13,0xEC,0x5F,0x97,0x44,0x17,0xC4,0xA7,0x7E,0x3D,0x64,0x5D,0x19,0x73},
  200. {0x60,0x81,0x4F,0xDC,0x22,0x2A,0x90,0x88,0x46,0xEE,0xB8,0x14,0xDE,0x5E,0x0B,0xDB},
  201. {0xE0,0x32,0x3A,0x0A,0x49,0x06,0x24,0x5C,0xC2,0xD3,0xAC,0x62,0x91,0x95,0xE4,0x79},
  202. {0xE7,0xC8,0x37,0x6D,0x8D,0xD5,0x4E,0xA9,0x6C,0x56,0xF4,0xEA,0x65,0x7A,0xAE,0x08},
  203. {0xBA,0x78,0x25,0x2E,0x1C,0xA6,0xB4,0xC6,0xE8,0xDD,0x74,0x1F,0x4B,0xBD,0x8B,0x8A},
  204. {0x70,0x3E,0xB5,0x66,0x48,0x03,0xF6,0x0E,0x61,0x35,0x57,0xB9,0x86,0xC1,0x1D,0x9E},
  205. {0xE1,0xF8,0x98,0x11,0x69,0xD9,0x8E,0x94,0x9B,0x1E,0x87,0xE9,0xCE,0x55,0x28,0xDF},
  206. {0x8C,0xA1,0x89,0x0D,0xBF,0xE6,0x42,0x68,0x41,0x99,0x2D,0x0F,0xB0,0x54,0xBB,0x16}
  207. };
  208. static const BYTE aes_invsbox[16][16] = {
  209. {0x52,0x09,0x6A,0xD5,0x30,0x36,0xA5,0x38,0xBF,0x40,0xA3,0x9E,0x81,0xF3,0xD7,0xFB},
  210. {0x7C,0xE3,0x39,0x82,0x9B,0x2F,0xFF,0x87,0x34,0x8E,0x43,0x44,0xC4,0xDE,0xE9,0xCB},
  211. {0x54,0x7B,0x94,0x32,0xA6,0xC2,0x23,0x3D,0xEE,0x4C,0x95,0x0B,0x42,0xFA,0xC3,0x4E},
  212. {0x08,0x2E,0xA1,0x66,0x28,0xD9,0x24,0xB2,0x76,0x5B,0xA2,0x49,0x6D,0x8B,0xD1,0x25},
  213. {0x72,0xF8,0xF6,0x64,0x86,0x68,0x98,0x16,0xD4,0xA4,0x5C,0xCC,0x5D,0x65,0xB6,0x92},
  214. {0x6C,0x70,0x48,0x50,0xFD,0xED,0xB9,0xDA,0x5E,0x15,0x46,0x57,0xA7,0x8D,0x9D,0x84},
  215. {0x90,0xD8,0xAB,0x00,0x8C,0xBC,0xD3,0x0A,0xF7,0xE4,0x58,0x05,0xB8,0xB3,0x45,0x06},
  216. {0xD0,0x2C,0x1E,0x8F,0xCA,0x3F,0x0F,0x02,0xC1,0xAF,0xBD,0x03,0x01,0x13,0x8A,0x6B},
  217. {0x3A,0x91,0x11,0x41,0x4F,0x67,0xDC,0xEA,0x97,0xF2,0xCF,0xCE,0xF0,0xB4,0xE6,0x73},
  218. {0x96,0xAC,0x74,0x22,0xE7,0xAD,0x35,0x85,0xE2,0xF9,0x37,0xE8,0x1C,0x75,0xDF,0x6E},
  219. {0x47,0xF1,0x1A,0x71,0x1D,0x29,0xC5,0x89,0x6F,0xB7,0x62,0x0E,0xAA,0x18,0xBE,0x1B},
  220. {0xFC,0x56,0x3E,0x4B,0xC6,0xD2,0x79,0x20,0x9A,0xDB,0xC0,0xFE,0x78,0xCD,0x5A,0xF4},
  221. {0x1F,0xDD,0xA8,0x33,0x88,0x07,0xC7,0x31,0xB1,0x12,0x10,0x59,0x27,0x80,0xEC,0x5F},
  222. {0x60,0x51,0x7F,0xA9,0x19,0xB5,0x4A,0x0D,0x2D,0xE5,0x7A,0x9F,0x93,0xC9,0x9C,0xEF},
  223. {0xA0,0xE0,0x3B,0x4D,0xAE,0x2A,0xF5,0xB0,0xC8,0xEB,0xBB,0x3C,0x83,0x53,0x99,0x61},
  224. {0x17,0x2B,0x04,0x7E,0xBA,0x77,0xD6,0x26,0xE1,0x69,0x14,0x63,0x55,0x21,0x0C,0x7D}
  225. };
  226. // This table stores pre-calculated values for all possible GF(2^8) calculations.This
  227. // table is only used by the (Inv)MixColumns steps.
  228. // USAGE: The second index (column) is the coefficient of multiplication. Only 7 different
  229. // coefficients are used: 0x01, 0x02, 0x03, 0x09, 0x0b, 0x0d, 0x0e, but multiplication by
  230. // 1 is negligible leaving only 6 coefficients. Each column of the table is devoted to one
  231. // of these coefficients, in the ascending order of value, from values 0x00 to 0xFF.
  232. static const BYTE gf_mul[256][6] = {
  233. {0x00,0x00,0x00,0x00,0x00,0x00},{0x02,0x03,0x09,0x0b,0x0d,0x0e},
  234. {0x04,0x06,0x12,0x16,0x1a,0x1c},{0x06,0x05,0x1b,0x1d,0x17,0x12},
  235. {0x08,0x0c,0x24,0x2c,0x34,0x38},{0x0a,0x0f,0x2d,0x27,0x39,0x36},
  236. {0x0c,0x0a,0x36,0x3a,0x2e,0x24},{0x0e,0x09,0x3f,0x31,0x23,0x2a},
  237. {0x10,0x18,0x48,0x58,0x68,0x70},{0x12,0x1b,0x41,0x53,0x65,0x7e},
  238. {0x14,0x1e,0x5a,0x4e,0x72,0x6c},{0x16,0x1d,0x53,0x45,0x7f,0x62},
  239. {0x18,0x14,0x6c,0x74,0x5c,0x48},{0x1a,0x17,0x65,0x7f,0x51,0x46},
  240. {0x1c,0x12,0x7e,0x62,0x46,0x54},{0x1e,0x11,0x77,0x69,0x4b,0x5a},
  241. {0x20,0x30,0x90,0xb0,0xd0,0xe0},{0x22,0x33,0x99,0xbb,0xdd,0xee},
  242. {0x24,0x36,0x82,0xa6,0xca,0xfc},{0x26,0x35,0x8b,0xad,0xc7,0xf2},
  243. {0x28,0x3c,0xb4,0x9c,0xe4,0xd8},{0x2a,0x3f,0xbd,0x97,0xe9,0xd6},
  244. {0x2c,0x3a,0xa6,0x8a,0xfe,0xc4},{0x2e,0x39,0xaf,0x81,0xf3,0xca},
  245. {0x30,0x28,0xd8,0xe8,0xb8,0x90},{0x32,0x2b,0xd1,0xe3,0xb5,0x9e},
  246. {0x34,0x2e,0xca,0xfe,0xa2,0x8c},{0x36,0x2d,0xc3,0xf5,0xaf,0x82},
  247. {0x38,0x24,0xfc,0xc4,0x8c,0xa8},{0x3a,0x27,0xf5,0xcf,0x81,0xa6},
  248. {0x3c,0x22,0xee,0xd2,0x96,0xb4},{0x3e,0x21,0xe7,0xd9,0x9b,0xba},
  249. {0x40,0x60,0x3b,0x7b,0xbb,0xdb},{0x42,0x63,0x32,0x70,0xb6,0xd5},
  250. {0x44,0x66,0x29,0x6d,0xa1,0xc7},{0x46,0x65,0x20,0x66,0xac,0xc9},
  251. {0x48,0x6c,0x1f,0x57,0x8f,0xe3},{0x4a,0x6f,0x16,0x5c,0x82,0xed},
  252. {0x4c,0x6a,0x0d,0x41,0x95,0xff},{0x4e,0x69,0x04,0x4a,0x98,0xf1},
  253. {0x50,0x78,0x73,0x23,0xd3,0xab},{0x52,0x7b,0x7a,0x28,0xde,0xa5},
  254. {0x54,0x7e,0x61,0x35,0xc9,0xb7},{0x56,0x7d,0x68,0x3e,0xc4,0xb9},
  255. {0x58,0x74,0x57,0x0f,0xe7,0x93},{0x5a,0x77,0x5e,0x04,0xea,0x9d},
  256. {0x5c,0x72,0x45,0x19,0xfd,0x8f},{0x5e,0x71,0x4c,0x12,0xf0,0x81},
  257. {0x60,0x50,0xab,0xcb,0x6b,0x3b},{0x62,0x53,0xa2,0xc0,0x66,0x35},
  258. {0x64,0x56,0xb9,0xdd,0x71,0x27},{0x66,0x55,0xb0,0xd6,0x7c,0x29},
  259. {0x68,0x5c,0x8f,0xe7,0x5f,0x03},{0x6a,0x5f,0x86,0xec,0x52,0x0d},
  260. {0x6c,0x5a,0x9d,0xf1,0x45,0x1f},{0x6e,0x59,0x94,0xfa,0x48,0x11},
  261. {0x70,0x48,0xe3,0x93,0x03,0x4b},{0x72,0x4b,0xea,0x98,0x0e,0x45},
  262. {0x74,0x4e,0xf1,0x85,0x19,0x57},{0x76,0x4d,0xf8,0x8e,0x14,0x59},
  263. {0x78,0x44,0xc7,0xbf,0x37,0x73},{0x7a,0x47,0xce,0xb4,0x3a,0x7d},
  264. {0x7c,0x42,0xd5,0xa9,0x2d,0x6f},{0x7e,0x41,0xdc,0xa2,0x20,0x61},
  265. {0x80,0xc0,0x76,0xf6,0x6d,0xad},{0x82,0xc3,0x7f,0xfd,0x60,0xa3},
  266. {0x84,0xc6,0x64,0xe0,0x77,0xb1},{0x86,0xc5,0x6d,0xeb,0x7a,0xbf},
  267. {0x88,0xcc,0x52,0xda,0x59,0x95},{0x8a,0xcf,0x5b,0xd1,0x54,0x9b},
  268. {0x8c,0xca,0x40,0xcc,0x43,0x89},{0x8e,0xc9,0x49,0xc7,0x4e,0x87},
  269. {0x90,0xd8,0x3e,0xae,0x05,0xdd},{0x92,0xdb,0x37,0xa5,0x08,0xd3},
  270. {0x94,0xde,0x2c,0xb8,0x1f,0xc1},{0x96,0xdd,0x25,0xb3,0x12,0xcf},
  271. {0x98,0xd4,0x1a,0x82,0x31,0xe5},{0x9a,0xd7,0x13,0x89,0x3c,0xeb},
  272. {0x9c,0xd2,0x08,0x94,0x2b,0xf9},{0x9e,0xd1,0x01,0x9f,0x26,0xf7},
  273. {0xa0,0xf0,0xe6,0x46,0xbd,0x4d},{0xa2,0xf3,0xef,0x4d,0xb0,0x43},
  274. {0xa4,0xf6,0xf4,0x50,0xa7,0x51},{0xa6,0xf5,0xfd,0x5b,0xaa,0x5f},
  275. {0xa8,0xfc,0xc2,0x6a,0x89,0x75},{0xaa,0xff,0xcb,0x61,0x84,0x7b},
  276. {0xac,0xfa,0xd0,0x7c,0x93,0x69},{0xae,0xf9,0xd9,0x77,0x9e,0x67},
  277. {0xb0,0xe8,0xae,0x1e,0xd5,0x3d},{0xb2,0xeb,0xa7,0x15,0xd8,0x33},
  278. {0xb4,0xee,0xbc,0x08,0xcf,0x21},{0xb6,0xed,0xb5,0x03,0xc2,0x2f},
  279. {0xb8,0xe4,0x8a,0x32,0xe1,0x05},{0xba,0xe7,0x83,0x39,0xec,0x0b},
  280. {0xbc,0xe2,0x98,0x24,0xfb,0x19},{0xbe,0xe1,0x91,0x2f,0xf6,0x17},
  281. {0xc0,0xa0,0x4d,0x8d,0xd6,0x76},{0xc2,0xa3,0x44,0x86,0xdb,0x78},
  282. {0xc4,0xa6,0x5f,0x9b,0xcc,0x6a},{0xc6,0xa5,0x56,0x90,0xc1,0x64},
  283. {0xc8,0xac,0x69,0xa1,0xe2,0x4e},{0xca,0xaf,0x60,0xaa,0xef,0x40},
  284. {0xcc,0xaa,0x7b,0xb7,0xf8,0x52},{0xce,0xa9,0x72,0xbc,0xf5,0x5c},
  285. {0xd0,0xb8,0x05,0xd5,0xbe,0x06},{0xd2,0xbb,0x0c,0xde,0xb3,0x08},
  286. {0xd4,0xbe,0x17,0xc3,0xa4,0x1a},{0xd6,0xbd,0x1e,0xc8,0xa9,0x14},
  287. {0xd8,0xb4,0x21,0xf9,0x8a,0x3e},{0xda,0xb7,0x28,0xf2,0x87,0x30},
  288. {0xdc,0xb2,0x33,0xef,0x90,0x22},{0xde,0xb1,0x3a,0xe4,0x9d,0x2c},
  289. {0xe0,0x90,0xdd,0x3d,0x06,0x96},{0xe2,0x93,0xd4,0x36,0x0b,0x98},
  290. {0xe4,0x96,0xcf,0x2b,0x1c,0x8a},{0xe6,0x95,0xc6,0x20,0x11,0x84},
  291. {0xe8,0x9c,0xf9,0x11,0x32,0xae},{0xea,0x9f,0xf0,0x1a,0x3f,0xa0},
  292. {0xec,0x9a,0xeb,0x07,0x28,0xb2},{0xee,0x99,0xe2,0x0c,0x25,0xbc},
  293. {0xf0,0x88,0x95,0x65,0x6e,0xe6},{0xf2,0x8b,0x9c,0x6e,0x63,0xe8},
  294. {0xf4,0x8e,0x87,0x73,0x74,0xfa},{0xf6,0x8d,0x8e,0x78,0x79,0xf4},
  295. {0xf8,0x84,0xb1,0x49,0x5a,0xde},{0xfa,0x87,0xb8,0x42,0x57,0xd0},
  296. {0xfc,0x82,0xa3,0x5f,0x40,0xc2},{0xfe,0x81,0xaa,0x54,0x4d,0xcc},
  297. {0x1b,0x9b,0xec,0xf7,0xda,0x41},{0x19,0x98,0xe5,0xfc,0xd7,0x4f},
  298. {0x1f,0x9d,0xfe,0xe1,0xc0,0x5d},{0x1d,0x9e,0xf7,0xea,0xcd,0x53},
  299. {0x13,0x97,0xc8,0xdb,0xee,0x79},{0x11,0x94,0xc1,0xd0,0xe3,0x77},
  300. {0x17,0x91,0xda,0xcd,0xf4,0x65},{0x15,0x92,0xd3,0xc6,0xf9,0x6b},
  301. {0x0b,0x83,0xa4,0xaf,0xb2,0x31},{0x09,0x80,0xad,0xa4,0xbf,0x3f},
  302. {0x0f,0x85,0xb6,0xb9,0xa8,0x2d},{0x0d,0x86,0xbf,0xb2,0xa5,0x23},
  303. {0x03,0x8f,0x80,0x83,0x86,0x09},{0x01,0x8c,0x89,0x88,0x8b,0x07},
  304. {0x07,0x89,0x92,0x95,0x9c,0x15},{0x05,0x8a,0x9b,0x9e,0x91,0x1b},
  305. {0x3b,0xab,0x7c,0x47,0x0a,0xa1},{0x39,0xa8,0x75,0x4c,0x07,0xaf},
  306. {0x3f,0xad,0x6e,0x51,0x10,0xbd},{0x3d,0xae,0x67,0x5a,0x1d,0xb3},
  307. {0x33,0xa7,0x58,0x6b,0x3e,0x99},{0x31,0xa4,0x51,0x60,0x33,0x97},
  308. {0x37,0xa1,0x4a,0x7d,0x24,0x85},{0x35,0xa2,0x43,0x76,0x29,0x8b},
  309. {0x2b,0xb3,0x34,0x1f,0x62,0xd1},{0x29,0xb0,0x3d,0x14,0x6f,0xdf},
  310. {0x2f,0xb5,0x26,0x09,0x78,0xcd},{0x2d,0xb6,0x2f,0x02,0x75,0xc3},
  311. {0x23,0xbf,0x10,0x33,0x56,0xe9},{0x21,0xbc,0x19,0x38,0x5b,0xe7},
  312. {0x27,0xb9,0x02,0x25,0x4c,0xf5},{0x25,0xba,0x0b,0x2e,0x41,0xfb},
  313. {0x5b,0xfb,0xd7,0x8c,0x61,0x9a},{0x59,0xf8,0xde,0x87,0x6c,0x94},
  314. {0x5f,0xfd,0xc5,0x9a,0x7b,0x86},{0x5d,0xfe,0xcc,0x91,0x76,0x88},
  315. {0x53,0xf7,0xf3,0xa0,0x55,0xa2},{0x51,0xf4,0xfa,0xab,0x58,0xac},
  316. {0x57,0xf1,0xe1,0xb6,0x4f,0xbe},{0x55,0xf2,0xe8,0xbd,0x42,0xb0},
  317. {0x4b,0xe3,0x9f,0xd4,0x09,0xea},{0x49,0xe0,0x96,0xdf,0x04,0xe4},
  318. {0x4f,0xe5,0x8d,0xc2,0x13,0xf6},{0x4d,0xe6,0x84,0xc9,0x1e,0xf8},
  319. {0x43,0xef,0xbb,0xf8,0x3d,0xd2},{0x41,0xec,0xb2,0xf3,0x30,0xdc},
  320. {0x47,0xe9,0xa9,0xee,0x27,0xce},{0x45,0xea,0xa0,0xe5,0x2a,0xc0},
  321. {0x7b,0xcb,0x47,0x3c,0xb1,0x7a},{0x79,0xc8,0x4e,0x37,0xbc,0x74},
  322. {0x7f,0xcd,0x55,0x2a,0xab,0x66},{0x7d,0xce,0x5c,0x21,0xa6,0x68},
  323. {0x73,0xc7,0x63,0x10,0x85,0x42},{0x71,0xc4,0x6a,0x1b,0x88,0x4c},
  324. {0x77,0xc1,0x71,0x06,0x9f,0x5e},{0x75,0xc2,0x78,0x0d,0x92,0x50},
  325. {0x6b,0xd3,0x0f,0x64,0xd9,0x0a},{0x69,0xd0,0x06,0x6f,0xd4,0x04},
  326. {0x6f,0xd5,0x1d,0x72,0xc3,0x16},{0x6d,0xd6,0x14,0x79,0xce,0x18},
  327. {0x63,0xdf,0x2b,0x48,0xed,0x32},{0x61,0xdc,0x22,0x43,0xe0,0x3c},
  328. {0x67,0xd9,0x39,0x5e,0xf7,0x2e},{0x65,0xda,0x30,0x55,0xfa,0x20},
  329. {0x9b,0x5b,0x9a,0x01,0xb7,0xec},{0x99,0x58,0x93,0x0a,0xba,0xe2},
  330. {0x9f,0x5d,0x88,0x17,0xad,0xf0},{0x9d,0x5e,0x81,0x1c,0xa0,0xfe},
  331. {0x93,0x57,0xbe,0x2d,0x83,0xd4},{0x91,0x54,0xb7,0x26,0x8e,0xda},
  332. {0x97,0x51,0xac,0x3b,0x99,0xc8},{0x95,0x52,0xa5,0x30,0x94,0xc6},
  333. {0x8b,0x43,0xd2,0x59,0xdf,0x9c},{0x89,0x40,0xdb,0x52,0xd2,0x92},
  334. {0x8f,0x45,0xc0,0x4f,0xc5,0x80},{0x8d,0x46,0xc9,0x44,0xc8,0x8e},
  335. {0x83,0x4f,0xf6,0x75,0xeb,0xa4},{0x81,0x4c,0xff,0x7e,0xe6,0xaa},
  336. {0x87,0x49,0xe4,0x63,0xf1,0xb8},{0x85,0x4a,0xed,0x68,0xfc,0xb6},
  337. {0xbb,0x6b,0x0a,0xb1,0x67,0x0c},{0xb9,0x68,0x03,0xba,0x6a,0x02},
  338. {0xbf,0x6d,0x18,0xa7,0x7d,0x10},{0xbd,0x6e,0x11,0xac,0x70,0x1e},
  339. {0xb3,0x67,0x2e,0x9d,0x53,0x34},{0xb1,0x64,0x27,0x96,0x5e,0x3a},
  340. {0xb7,0x61,0x3c,0x8b,0x49,0x28},{0xb5,0x62,0x35,0x80,0x44,0x26},
  341. {0xab,0x73,0x42,0xe9,0x0f,0x7c},{0xa9,0x70,0x4b,0xe2,0x02,0x72},
  342. {0xaf,0x75,0x50,0xff,0x15,0x60},{0xad,0x76,0x59,0xf4,0x18,0x6e},
  343. {0xa3,0x7f,0x66,0xc5,0x3b,0x44},{0xa1,0x7c,0x6f,0xce,0x36,0x4a},
  344. {0xa7,0x79,0x74,0xd3,0x21,0x58},{0xa5,0x7a,0x7d,0xd8,0x2c,0x56},
  345. {0xdb,0x3b,0xa1,0x7a,0x0c,0x37},{0xd9,0x38,0xa8,0x71,0x01,0x39},
  346. {0xdf,0x3d,0xb3,0x6c,0x16,0x2b},{0xdd,0x3e,0xba,0x67,0x1b,0x25},
  347. {0xd3,0x37,0x85,0x56,0x38,0x0f},{0xd1,0x34,0x8c,0x5d,0x35,0x01},
  348. {0xd7,0x31,0x97,0x40,0x22,0x13},{0xd5,0x32,0x9e,0x4b,0x2f,0x1d},
  349. {0xcb,0x23,0xe9,0x22,0x64,0x47},{0xc9,0x20,0xe0,0x29,0x69,0x49},
  350. {0xcf,0x25,0xfb,0x34,0x7e,0x5b},{0xcd,0x26,0xf2,0x3f,0x73,0x55},
  351. {0xc3,0x2f,0xcd,0x0e,0x50,0x7f},{0xc1,0x2c,0xc4,0x05,0x5d,0x71},
  352. {0xc7,0x29,0xdf,0x18,0x4a,0x63},{0xc5,0x2a,0xd6,0x13,0x47,0x6d},
  353. {0xfb,0x0b,0x31,0xca,0xdc,0xd7},{0xf9,0x08,0x38,0xc1,0xd1,0xd9},
  354. {0xff,0x0d,0x23,0xdc,0xc6,0xcb},{0xfd,0x0e,0x2a,0xd7,0xcb,0xc5},
  355. {0xf3,0x07,0x15,0xe6,0xe8,0xef},{0xf1,0x04,0x1c,0xed,0xe5,0xe1},
  356. {0xf7,0x01,0x07,0xf0,0xf2,0xf3},{0xf5,0x02,0x0e,0xfb,0xff,0xfd},
  357. {0xeb,0x13,0x79,0x92,0xb4,0xa7},{0xe9,0x10,0x70,0x99,0xb9,0xa9},
  358. {0xef,0x15,0x6b,0x84,0xae,0xbb},{0xed,0x16,0x62,0x8f,0xa3,0xb5},
  359. {0xe3,0x1f,0x5d,0xbe,0x80,0x9f},{0xe1,0x1c,0x54,0xb5,0x8d,0x91},
  360. {0xe7,0x19,0x4f,0xa8,0x9a,0x83},{0xe5,0x1a,0x46,0xa3,0x97,0x8d}
  361. };
  362. /*********************** FUNCTION DEFINITIONS ***********************/
  363. // XORs the in and out buffers, storing the result in out. Length is in bytes.
  364. void xor_buf(const BYTE in[], BYTE out[], size_t len)
  365. {
  366. size_t idx;
  367. for (idx = 0; idx < len; idx++)
  368. out[idx] ^= in[idx];
  369. }
  370. /*******************
  371. * AES - CBC
  372. *******************/
  373. int aes_encrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const UINT key[], int keysize, const BYTE iv[])
  374. {
  375. BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
  376. int blocks, idx;
  377. if (in_len % AES_BLOCK_SIZE != 0)
  378. return(FALSE);
  379. blocks = (int)(in_len / AES_BLOCK_SIZE);
  380. memcpy(iv_buf, iv, AES_BLOCK_SIZE);
  381. for (idx = 0; idx < blocks; idx++) {
  382. memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
  383. xor_buf(iv_buf, buf_in, AES_BLOCK_SIZE);
  384. aes_encrypt(buf_in, buf_out, key, keysize);
  385. memcpy(&out[idx * AES_BLOCK_SIZE], buf_out, AES_BLOCK_SIZE);
  386. memcpy(iv_buf, buf_out, AES_BLOCK_SIZE);
  387. }
  388. return(TRUE);
  389. }
  390. int aes_encrypt_cbc_mac(const BYTE in[], size_t in_len, BYTE out[], const UINT key[], int keysize, const BYTE iv[])
  391. {
  392. BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
  393. int blocks, idx;
  394. if (in_len % AES_BLOCK_SIZE != 0)
  395. return(FALSE);
  396. blocks = (int)(in_len / AES_BLOCK_SIZE);
  397. memcpy(iv_buf, iv, AES_BLOCK_SIZE);
  398. for (idx = 0; idx < blocks; idx++) {
  399. memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
  400. xor_buf(iv_buf, buf_in, AES_BLOCK_SIZE);
  401. aes_encrypt(buf_in, buf_out, key, keysize);
  402. memcpy(iv_buf, buf_out, AES_BLOCK_SIZE);
  403. // Do not output all encrypted blocks.
  404. }
  405. memcpy(out, buf_out, AES_BLOCK_SIZE); // Only output the last block.
  406. return(TRUE);
  407. }
  408. int aes_decrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const UINT key[], int keysize, const BYTE iv[])
  409. {
  410. BYTE buf_in[AES_BLOCK_SIZE], buf_out[AES_BLOCK_SIZE], iv_buf[AES_BLOCK_SIZE];
  411. int blocks, idx;
  412. if (in_len % AES_BLOCK_SIZE != 0)
  413. return(FALSE);
  414. blocks = (int)(in_len / AES_BLOCK_SIZE);
  415. memcpy(iv_buf, iv, AES_BLOCK_SIZE);
  416. for (idx = 0; idx < blocks; idx++) {
  417. memcpy(buf_in, &in[idx * AES_BLOCK_SIZE], AES_BLOCK_SIZE);
  418. aes_decrypt(buf_in, buf_out, key, keysize);
  419. xor_buf(iv_buf, buf_out, AES_BLOCK_SIZE);
  420. memcpy(&out[idx * AES_BLOCK_SIZE], buf_out, AES_BLOCK_SIZE);
  421. memcpy(iv_buf, buf_in, AES_BLOCK_SIZE);
  422. }
  423. return(TRUE);
  424. }
  425. /*******************
  426. * AES - CTR
  427. *******************/
  428. void increment_iv(BYTE iv[], int counter_size)
  429. {
  430. int idx;
  431. // Use counter_size bytes at the end of the IV as the big-endian integer to increment.
  432. for (idx = AES_BLOCK_SIZE - 1; idx >= AES_BLOCK_SIZE - counter_size; idx--) {
  433. iv[idx]++;
  434. if (iv[idx] != 0 || idx == AES_BLOCK_SIZE - counter_size)
  435. break;
  436. }
  437. }
  438. // Performs the encryption in-place, the input and output buffers may be the same.
  439. // Input may be an arbitrary length (in bytes).
  440. void aes_encrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const UINT key[], int keysize, const BYTE iv[])
  441. {
  442. size_t idx = 0, last_block_length;
  443. BYTE iv_buf[AES_BLOCK_SIZE], out_buf[AES_BLOCK_SIZE];
  444. if (in != out)
  445. memcpy(out, in, in_len);
  446. memcpy(iv_buf, iv, AES_BLOCK_SIZE);
  447. last_block_length = in_len - AES_BLOCK_SIZE;
  448. if (in_len > AES_BLOCK_SIZE) {
  449. for (idx = 0; idx < last_block_length; idx += AES_BLOCK_SIZE) {
  450. aes_encrypt(iv_buf, out_buf, key, keysize);
  451. xor_buf(out_buf, &out[idx], AES_BLOCK_SIZE);
  452. increment_iv(iv_buf, AES_BLOCK_SIZE);
  453. }
  454. }
  455. aes_encrypt(iv_buf, out_buf, key, keysize);
  456. xor_buf(out_buf, &out[idx], in_len - idx); // Use the Most Significant bytes.
  457. }
  458. void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const UINT key[], int keysize, const BYTE iv[])
  459. {
  460. // CTR encryption is its own inverse function.
  461. aes_encrypt_ctr(in, in_len, out, key, keysize, iv);
  462. }
  463. /*******************
  464. * AES - CCM
  465. *******************/
  466. // out_len = payload_len + assoc_len
  467. int aes_encrypt_ccm(const BYTE payload[], UINT payload_len, const BYTE assoc[], unsigned short assoc_len,
  468. const BYTE nonce[], unsigned short nonce_len, BYTE out[], UINT *out_len,
  469. UINT mac_len, const BYTE key_str[], int keysize)
  470. {
  471. BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], *buf;
  472. int end_of_buf, payload_len_store_size;
  473. UINT key[60];
  474. if (mac_len != 4 && mac_len != 6 && mac_len != 8 && mac_len != 10 &&
  475. mac_len != 12 && mac_len != 14 && mac_len != 16)
  476. return(FALSE);
  477. if (nonce_len < 7 || nonce_len > 13)
  478. return(FALSE);
  479. if (assoc_len > 32768 /* = 2^15 */)
  480. return(FALSE);
  481. buf = (BYTE*)malloc(payload_len + assoc_len + 48 /*Round both payload and associated data up a block size and add an extra block.*/);
  482. if (! buf)
  483. return(FALSE);
  484. // Prepare the key for usage.
  485. aes_key_setup(key_str, key, keysize);
  486. // Format the first block of the formatted data.
  487. payload_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
  488. ccm_prepare_first_format_blk(buf, assoc_len, payload_len, payload_len_store_size, mac_len, nonce, nonce_len);
  489. end_of_buf = AES_BLOCK_SIZE;
  490. // Format the Associated Data, aka, assoc[].
  491. ccm_format_assoc_data(buf, &end_of_buf, assoc, assoc_len);
  492. // Format the Payload, aka payload[].
  493. ccm_format_payload_data(buf, &end_of_buf, payload, payload_len);
  494. // Create the first counter block.
  495. ccm_prepare_first_ctr_blk(counter, nonce, nonce_len, payload_len_store_size);
  496. // Perform the CBC operation with an IV of zeros on the formatted buffer to calculate the MAC.
  497. memset(temp_iv, 0, AES_BLOCK_SIZE);
  498. aes_encrypt_cbc_mac(buf, end_of_buf, mac, key, keysize, temp_iv);
  499. // Copy the Payload and MAC to the output buffer.
  500. memcpy(out, payload, payload_len);
  501. memcpy(&out[payload_len], mac, mac_len);
  502. // Encrypt the Payload with CTR mode with a counter starting at 1.
  503. memcpy(temp_iv, counter, AES_BLOCK_SIZE);
  504. increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // Last argument is the byte size of the counting portion of the counter block. /*BUG?*/
  505. aes_encrypt_ctr(out, payload_len, out, key, keysize, temp_iv);
  506. // Encrypt the MAC with CTR mode with a counter starting at 0.
  507. aes_encrypt_ctr(&out[payload_len], mac_len, &out[payload_len], key, keysize, counter);
  508. free(buf);
  509. *out_len = payload_len + mac_len;
  510. return(TRUE);
  511. }
  512. // plaintext_len = ciphertext_len - mac_len
  513. // Needs a flag for whether the MAC matches.
  514. int aes_decrypt_ccm(const BYTE ciphertext[], UINT ciphertext_len, const BYTE assoc[], unsigned short assoc_len,
  515. const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], UINT *plaintext_len,
  516. UINT mac_len, int *mac_auth, const BYTE key_str[], int keysize)
  517. {
  518. BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], mac_buf[16], *buf;
  519. int end_of_buf, plaintext_len_store_size;
  520. UINT key[60];
  521. if (ciphertext_len <= mac_len)
  522. return(FALSE);
  523. buf = (BYTE*)malloc(assoc_len + ciphertext_len /*ciphertext_len = plaintext_len + mac_len*/ + 48);
  524. if (! buf)
  525. return(FALSE);
  526. // Prepare the key for usage.
  527. aes_key_setup(key_str, key, keysize);
  528. // Copy the plaintext and MAC to the output buffers.
  529. *plaintext_len = ciphertext_len - mac_len;
  530. plaintext_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
  531. memcpy(plaintext, ciphertext, *plaintext_len);
  532. memcpy(mac, &ciphertext[*plaintext_len], mac_len);
  533. // Prepare the first counter block for use in decryption.
  534. ccm_prepare_first_ctr_blk(counter, nonce, nonce_len, plaintext_len_store_size);
  535. // Decrypt the Payload with CTR mode with a counter starting at 1.
  536. memcpy(temp_iv, counter, AES_BLOCK_SIZE);
  537. increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // (AES_BLOCK_SIZE - 1 - mac_len) is the byte size of the counting portion of the counter block.
  538. aes_decrypt_ctr(plaintext, *plaintext_len, plaintext, key, keysize, temp_iv);
  539. // Setting mac_auth to nullptr disables the authentication check.
  540. if (mac_auth != nullptr) {
  541. // Decrypt the MAC with CTR mode with a counter starting at 0.
  542. aes_decrypt_ctr(mac, mac_len, mac, key, keysize, counter);
  543. // Format the first block of the formatted data.
  544. plaintext_len_store_size = AES_BLOCK_SIZE - 1 - nonce_len;
  545. ccm_prepare_first_format_blk(buf, assoc_len, *plaintext_len, plaintext_len_store_size, mac_len, nonce, nonce_len);
  546. end_of_buf = AES_BLOCK_SIZE;
  547. // Format the Associated Data into the authentication buffer.
  548. ccm_format_assoc_data(buf, &end_of_buf, assoc, assoc_len);
  549. // Format the Payload into the authentication buffer.
  550. ccm_format_payload_data(buf, &end_of_buf, plaintext, *plaintext_len);
  551. // Perform the CBC operation with an IV of zeros on the formatted buffer to calculate the MAC.
  552. memset(temp_iv, 0, AES_BLOCK_SIZE);
  553. aes_encrypt_cbc_mac(buf, end_of_buf, mac_buf, key, keysize, temp_iv);
  554. // Compare the calculated MAC against the MAC embedded in the ciphertext to see if they are the same.
  555. if (! memcmp(mac, mac_buf, mac_len)) {
  556. *mac_auth = TRUE;
  557. }
  558. else {
  559. *mac_auth = FALSE;
  560. memset(plaintext, 0, *plaintext_len);
  561. }
  562. }
  563. free(buf);
  564. return(TRUE);
  565. }
  566. // Creates the first counter block. First byte is flags, then the nonce, then the incremented part.
  567. void ccm_prepare_first_ctr_blk(BYTE counter[], const BYTE nonce[], int nonce_len, int payload_len_store_size)
  568. {
  569. memset(counter, 0, AES_BLOCK_SIZE);
  570. counter[0] = (payload_len_store_size - 1) & 0x07;
  571. memcpy(&counter[1], nonce, nonce_len);
  572. }
  573. void ccm_prepare_first_format_blk(BYTE buf[], int assoc_len, int payload_len, int payload_len_store_size, int mac_len, const BYTE nonce[], int nonce_len)
  574. {
  575. // Set the flags for the first byte of the first block.
  576. buf[0] = ((((mac_len - 2) / 2) & 0x07) << 3) | ((payload_len_store_size - 1) & 0x07);
  577. if (assoc_len > 0)
  578. buf[0] += 0x40;
  579. // Format the rest of the first block, storing the nonce and the size of the payload.
  580. memcpy(&buf[1], nonce, nonce_len);
  581. memset(&buf[1 + nonce_len], 0, AES_BLOCK_SIZE - 1 - nonce_len);
  582. buf[15] = payload_len & 0x000000FF;
  583. buf[14] = (payload_len >> 8) & 0x000000FF;
  584. }
  585. void ccm_format_assoc_data(BYTE buf[], int *end_of_buf, const BYTE assoc[], int assoc_len)
  586. {
  587. int pad;
  588. buf[*end_of_buf + 1] = assoc_len & 0x00FF;
  589. buf[*end_of_buf] = (assoc_len >> 8) & 0x00FF;
  590. *end_of_buf += 2;
  591. memcpy(&buf[*end_of_buf], assoc, assoc_len);
  592. *end_of_buf += assoc_len;
  593. pad = AES_BLOCK_SIZE - (*end_of_buf % AES_BLOCK_SIZE); /*BUG?*/
  594. memset(&buf[*end_of_buf], 0, pad);
  595. *end_of_buf += pad;
  596. }
  597. void ccm_format_payload_data(BYTE buf[], int *end_of_buf, const BYTE payload[], int payload_len)
  598. {
  599. int pad;
  600. memcpy(&buf[*end_of_buf], payload, payload_len);
  601. *end_of_buf += payload_len;
  602. pad = *end_of_buf % AES_BLOCK_SIZE;
  603. if (pad != 0)
  604. pad = AES_BLOCK_SIZE - pad;
  605. memset(&buf[*end_of_buf], 0, pad);
  606. *end_of_buf += pad;
  607. }
  608. /*******************
  609. * AES
  610. *******************/
  611. /////////////////
  612. // KEY EXPANSION
  613. /////////////////
  614. // Substitutes a word using the AES S-Box.
  615. UINT SubWord(UINT word)
  616. {
  617. unsigned int result;
  618. result = (int)aes_sbox[(word >> 4) & 0x0000000F][word & 0x0000000F];
  619. result += (int)aes_sbox[(word >> 12) & 0x0000000F][(word >> 8) & 0x0000000F] << 8;
  620. result += (int)aes_sbox[(word >> 20) & 0x0000000F][(word >> 16) & 0x0000000F] << 16;
  621. result += (int)aes_sbox[(word >> 28) & 0x0000000F][(word >> 24) & 0x0000000F] << 24;
  622. return(result);
  623. }
  624. // Performs the action of generating the keys that will be used in every round of
  625. // encryption. "key" is the user-supplied input key, "w" is the output key schedule,
  626. // "keysize" is the length in bits of "key", must be 128, 192, or 256.
  627. void aes_key_setup(const BYTE key[], UINT w[], int keysize)
  628. {
  629. int Nb=4,Nr,Nk,idx;
  630. UINT temp,Rcon[]={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,
  631. 0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000,
  632. 0xab000000,0x4d000000,0x9a000000};
  633. switch (keysize) {
  634. case 128: Nr = 10; Nk = 4; break;
  635. case 192: Nr = 12; Nk = 6; break;
  636. case 256: Nr = 14; Nk = 8; break;
  637. default: return;
  638. }
  639. for (idx=0; idx < Nk; ++idx) {
  640. w[idx] = ((key[4 * idx]) << 24) | ((key[4 * idx + 1]) << 16) |
  641. ((key[4 * idx + 2]) << 8) | ((key[4 * idx + 3]));
  642. }
  643. for (idx = Nk; idx < Nb * (Nr+1); ++idx) {
  644. temp = w[idx - 1];
  645. if ((idx % Nk) == 0)
  646. temp = SubWord(KE_ROTWORD(temp)) ^ Rcon[(idx-1)/Nk];
  647. else if (Nk > 6 && (idx % Nk) == 4)
  648. temp = SubWord(temp);
  649. w[idx] = w[idx-Nk] ^ temp;
  650. }
  651. }
  652. /////////////////
  653. // ADD ROUND KEY
  654. /////////////////
  655. // Performs the AddRoundKey step. Each round has its own pre-generated 16-byte key in the
  656. // form of 4 integers (the "w" array). Each integer is XOR'd by one column of the state.
  657. // Also performs the job of InvAddRoundKey(); since the function is a simple XOR process,
  658. // it is its own inverse.
  659. void AddRoundKey(BYTE state[][4], const UINT w[])
  660. {
  661. BYTE subkey[4];
  662. // memcpy(subkey,&w[idx],4); // Not accurate for big endian machines
  663. // Subkey 1
  664. subkey[0] = w[0] >> 24;
  665. subkey[1] = w[0] >> 16;
  666. subkey[2] = w[0] >> 8;
  667. subkey[3] = w[0];
  668. state[0][0] ^= subkey[0];
  669. state[1][0] ^= subkey[1];
  670. state[2][0] ^= subkey[2];
  671. state[3][0] ^= subkey[3];
  672. // Subkey 2
  673. subkey[0] = w[1] >> 24;
  674. subkey[1] = w[1] >> 16;
  675. subkey[2] = w[1] >> 8;
  676. subkey[3] = w[1];
  677. state[0][1] ^= subkey[0];
  678. state[1][1] ^= subkey[1];
  679. state[2][1] ^= subkey[2];
  680. state[3][1] ^= subkey[3];
  681. // Subkey 3
  682. subkey[0] = w[2] >> 24;
  683. subkey[1] = w[2] >> 16;
  684. subkey[2] = w[2] >> 8;
  685. subkey[3] = w[2];
  686. state[0][2] ^= subkey[0];
  687. state[1][2] ^= subkey[1];
  688. state[2][2] ^= subkey[2];
  689. state[3][2] ^= subkey[3];
  690. // Subkey 4
  691. subkey[0] = w[3] >> 24;
  692. subkey[1] = w[3] >> 16;
  693. subkey[2] = w[3] >> 8;
  694. subkey[3] = w[3];
  695. state[0][3] ^= subkey[0];
  696. state[1][3] ^= subkey[1];
  697. state[2][3] ^= subkey[2];
  698. state[3][3] ^= subkey[3];
  699. }
  700. /////////////////
  701. // (Inv)SubBytes
  702. /////////////////
  703. // Performs the SubBytes step. All bytes in the state are substituted with a
  704. // pre-calculated value from a lookup table.
  705. void SubBytes(BYTE state[][4])
  706. {
  707. state[0][0] = aes_sbox[state[0][0] >> 4][state[0][0] & 0x0F];
  708. state[0][1] = aes_sbox[state[0][1] >> 4][state[0][1] & 0x0F];
  709. state[0][2] = aes_sbox[state[0][2] >> 4][state[0][2] & 0x0F];
  710. state[0][3] = aes_sbox[state[0][3] >> 4][state[0][3] & 0x0F];
  711. state[1][0] = aes_sbox[state[1][0] >> 4][state[1][0] & 0x0F];
  712. state[1][1] = aes_sbox[state[1][1] >> 4][state[1][1] & 0x0F];
  713. state[1][2] = aes_sbox[state[1][2] >> 4][state[1][2] & 0x0F];
  714. state[1][3] = aes_sbox[state[1][3] >> 4][state[1][3] & 0x0F];
  715. state[2][0] = aes_sbox[state[2][0] >> 4][state[2][0] & 0x0F];
  716. state[2][1] = aes_sbox[state[2][1] >> 4][state[2][1] & 0x0F];
  717. state[2][2] = aes_sbox[state[2][2] >> 4][state[2][2] & 0x0F];
  718. state[2][3] = aes_sbox[state[2][3] >> 4][state[2][3] & 0x0F];
  719. state[3][0] = aes_sbox[state[3][0] >> 4][state[3][0] & 0x0F];
  720. state[3][1] = aes_sbox[state[3][1] >> 4][state[3][1] & 0x0F];
  721. state[3][2] = aes_sbox[state[3][2] >> 4][state[3][2] & 0x0F];
  722. state[3][3] = aes_sbox[state[3][3] >> 4][state[3][3] & 0x0F];
  723. }
  724. void InvSubBytes(BYTE state[][4])
  725. {
  726. state[0][0] = aes_invsbox[state[0][0] >> 4][state[0][0] & 0x0F];
  727. state[0][1] = aes_invsbox[state[0][1] >> 4][state[0][1] & 0x0F];
  728. state[0][2] = aes_invsbox[state[0][2] >> 4][state[0][2] & 0x0F];
  729. state[0][3] = aes_invsbox[state[0][3] >> 4][state[0][3] & 0x0F];
  730. state[1][0] = aes_invsbox[state[1][0] >> 4][state[1][0] & 0x0F];
  731. state[1][1] = aes_invsbox[state[1][1] >> 4][state[1][1] & 0x0F];
  732. state[1][2] = aes_invsbox[state[1][2] >> 4][state[1][2] & 0x0F];
  733. state[1][3] = aes_invsbox[state[1][3] >> 4][state[1][3] & 0x0F];
  734. state[2][0] = aes_invsbox[state[2][0] >> 4][state[2][0] & 0x0F];
  735. state[2][1] = aes_invsbox[state[2][1] >> 4][state[2][1] & 0x0F];
  736. state[2][2] = aes_invsbox[state[2][2] >> 4][state[2][2] & 0x0F];
  737. state[2][3] = aes_invsbox[state[2][3] >> 4][state[2][3] & 0x0F];
  738. state[3][0] = aes_invsbox[state[3][0] >> 4][state[3][0] & 0x0F];
  739. state[3][1] = aes_invsbox[state[3][1] >> 4][state[3][1] & 0x0F];
  740. state[3][2] = aes_invsbox[state[3][2] >> 4][state[3][2] & 0x0F];
  741. state[3][3] = aes_invsbox[state[3][3] >> 4][state[3][3] & 0x0F];
  742. }
  743. /////////////////
  744. // (Inv)ShiftRows
  745. /////////////////
  746. // Performs the ShiftRows step. All rows are shifted cylindrically to the left.
  747. void ShiftRows(BYTE state[][4])
  748. {
  749. int t;
  750. // Shift left by 1
  751. t = state[1][0];
  752. state[1][0] = state[1][1];
  753. state[1][1] = state[1][2];
  754. state[1][2] = state[1][3];
  755. state[1][3] = t;
  756. // Shift left by 2
  757. t = state[2][0];
  758. state[2][0] = state[2][2];
  759. state[2][2] = t;
  760. t = state[2][1];
  761. state[2][1] = state[2][3];
  762. state[2][3] = t;
  763. // Shift left by 3
  764. t = state[3][0];
  765. state[3][0] = state[3][3];
  766. state[3][3] = state[3][2];
  767. state[3][2] = state[3][1];
  768. state[3][1] = t;
  769. }
  770. // All rows are shifted cylindrically to the right.
  771. void InvShiftRows(BYTE state[][4])
  772. {
  773. int t;
  774. // Shift right by 1
  775. t = state[1][3];
  776. state[1][3] = state[1][2];
  777. state[1][2] = state[1][1];
  778. state[1][1] = state[1][0];
  779. state[1][0] = t;
  780. // Shift right by 2
  781. t = state[2][3];
  782. state[2][3] = state[2][1];
  783. state[2][1] = t;
  784. t = state[2][2];
  785. state[2][2] = state[2][0];
  786. state[2][0] = t;
  787. // Shift right by 3
  788. t = state[3][3];
  789. state[3][3] = state[3][0];
  790. state[3][0] = state[3][1];
  791. state[3][1] = state[3][2];
  792. state[3][2] = t;
  793. }
  794. /////////////////
  795. // (Inv)MixColumns
  796. /////////////////
  797. // Performs the MixColums step. The state is multiplied by itself using matrix
  798. // multiplication in a Galios Field 2^8. All multiplication is pre-computed in a table.
  799. // Addition is equivilent to XOR. (Must always make a copy of the column as the original
  800. // values will be destoyed.)
  801. void MixColumns(BYTE state[][4])
  802. {
  803. BYTE col[4];
  804. // Column 1
  805. col[0] = state[0][0];
  806. col[1] = state[1][0];
  807. col[2] = state[2][0];
  808. col[3] = state[3][0];
  809. state[0][0] = gf_mul[col[0]][0];
  810. state[0][0] ^= gf_mul[col[1]][1];
  811. state[0][0] ^= col[2];
  812. state[0][0] ^= col[3];
  813. state[1][0] = col[0];
  814. state[1][0] ^= gf_mul[col[1]][0];
  815. state[1][0] ^= gf_mul[col[2]][1];
  816. state[1][0] ^= col[3];
  817. state[2][0] = col[0];
  818. state[2][0] ^= col[1];
  819. state[2][0] ^= gf_mul[col[2]][0];
  820. state[2][0] ^= gf_mul[col[3]][1];
  821. state[3][0] = gf_mul[col[0]][1];
  822. state[3][0] ^= col[1];
  823. state[3][0] ^= col[2];
  824. state[3][0] ^= gf_mul[col[3]][0];
  825. // Column 2
  826. col[0] = state[0][1];
  827. col[1] = state[1][1];
  828. col[2] = state[2][1];
  829. col[3] = state[3][1];
  830. state[0][1] = gf_mul[col[0]][0];
  831. state[0][1] ^= gf_mul[col[1]][1];
  832. state[0][1] ^= col[2];
  833. state[0][1] ^= col[3];
  834. state[1][1] = col[0];
  835. state[1][1] ^= gf_mul[col[1]][0];
  836. state[1][1] ^= gf_mul[col[2]][1];
  837. state[1][1] ^= col[3];
  838. state[2][1] = col[0];
  839. state[2][1] ^= col[1];
  840. state[2][1] ^= gf_mul[col[2]][0];
  841. state[2][1] ^= gf_mul[col[3]][1];
  842. state[3][1] = gf_mul[col[0]][1];
  843. state[3][1] ^= col[1];
  844. state[3][1] ^= col[2];
  845. state[3][1] ^= gf_mul[col[3]][0];
  846. // Column 3
  847. col[0] = state[0][2];
  848. col[1] = state[1][2];
  849. col[2] = state[2][2];
  850. col[3] = state[3][2];
  851. state[0][2] = gf_mul[col[0]][0];
  852. state[0][2] ^= gf_mul[col[1]][1];
  853. state[0][2] ^= col[2];
  854. state[0][2] ^= col[3];
  855. state[1][2] = col[0];
  856. state[1][2] ^= gf_mul[col[1]][0];
  857. state[1][2] ^= gf_mul[col[2]][1];
  858. state[1][2] ^= col[3];
  859. state[2][2] = col[0];
  860. state[2][2] ^= col[1];
  861. state[2][2] ^= gf_mul[col[2]][0];
  862. state[2][2] ^= gf_mul[col[3]][1];
  863. state[3][2] = gf_mul[col[0]][1];
  864. state[3][2] ^= col[1];
  865. state[3][2] ^= col[2];
  866. state[3][2] ^= gf_mul[col[3]][0];
  867. // Column 4
  868. col[0] = state[0][3];
  869. col[1] = state[1][3];
  870. col[2] = state[2][3];
  871. col[3] = state[3][3];
  872. state[0][3] = gf_mul[col[0]][0];
  873. state[0][3] ^= gf_mul[col[1]][1];
  874. state[0][3] ^= col[2];
  875. state[0][3] ^= col[3];
  876. state[1][3] = col[0];
  877. state[1][3] ^= gf_mul[col[1]][0];
  878. state[1][3] ^= gf_mul[col[2]][1];
  879. state[1][3] ^= col[3];
  880. state[2][3] = col[0];
  881. state[2][3] ^= col[1];
  882. state[2][3] ^= gf_mul[col[2]][0];
  883. state[2][3] ^= gf_mul[col[3]][1];
  884. state[3][3] = gf_mul[col[0]][1];
  885. state[3][3] ^= col[1];
  886. state[3][3] ^= col[2];
  887. state[3][3] ^= gf_mul[col[3]][0];
  888. }
  889. void InvMixColumns(BYTE state[][4])
  890. {
  891. BYTE col[4];
  892. // Column 1
  893. col[0] = state[0][0];
  894. col[1] = state[1][0];
  895. col[2] = state[2][0];
  896. col[3] = state[3][0];
  897. state[0][0] = gf_mul[col[0]][5];
  898. state[0][0] ^= gf_mul[col[1]][3];
  899. state[0][0] ^= gf_mul[col[2]][4];
  900. state[0][0] ^= gf_mul[col[3]][2];
  901. state[1][0] = gf_mul[col[0]][2];
  902. state[1][0] ^= gf_mul[col[1]][5];
  903. state[1][0] ^= gf_mul[col[2]][3];
  904. state[1][0] ^= gf_mul[col[3]][4];
  905. state[2][0] = gf_mul[col[0]][4];
  906. state[2][0] ^= gf_mul[col[1]][2];
  907. state[2][0] ^= gf_mul[col[2]][5];
  908. state[2][0] ^= gf_mul[col[3]][3];
  909. state[3][0] = gf_mul[col[0]][3];
  910. state[3][0] ^= gf_mul[col[1]][4];
  911. state[3][0] ^= gf_mul[col[2]][2];
  912. state[3][0] ^= gf_mul[col[3]][5];
  913. // Column 2
  914. col[0] = state[0][1];
  915. col[1] = state[1][1];
  916. col[2] = state[2][1];
  917. col[3] = state[3][1];
  918. state[0][1] = gf_mul[col[0]][5];
  919. state[0][1] ^= gf_mul[col[1]][3];
  920. state[0][1] ^= gf_mul[col[2]][4];
  921. state[0][1] ^= gf_mul[col[3]][2];
  922. state[1][1] = gf_mul[col[0]][2];
  923. state[1][1] ^= gf_mul[col[1]][5];
  924. state[1][1] ^= gf_mul[col[2]][3];
  925. state[1][1] ^= gf_mul[col[3]][4];
  926. state[2][1] = gf_mul[col[0]][4];
  927. state[2][1] ^= gf_mul[col[1]][2];
  928. state[2][1] ^= gf_mul[col[2]][5];
  929. state[2][1] ^= gf_mul[col[3]][3];
  930. state[3][1] = gf_mul[col[0]][3];
  931. state[3][1] ^= gf_mul[col[1]][4];
  932. state[3][1] ^= gf_mul[col[2]][2];
  933. state[3][1] ^= gf_mul[col[3]][5];
  934. // Column 3
  935. col[0] = state[0][2];
  936. col[1] = state[1][2];
  937. col[2] = state[2][2];
  938. col[3] = state[3][2];
  939. state[0][2] = gf_mul[col[0]][5];
  940. state[0][2] ^= gf_mul[col[1]][3];
  941. state[0][2] ^= gf_mul[col[2]][4];
  942. state[0][2] ^= gf_mul[col[3]][2];
  943. state[1][2] = gf_mul[col[0]][2];
  944. state[1][2] ^= gf_mul[col[1]][5];
  945. state[1][2] ^= gf_mul[col[2]][3];
  946. state[1][2] ^= gf_mul[col[3]][4];
  947. state[2][2] = gf_mul[col[0]][4];
  948. state[2][2] ^= gf_mul[col[1]][2];
  949. state[2][2] ^= gf_mul[col[2]][5];
  950. state[2][2] ^= gf_mul[col[3]][3];
  951. state[3][2] = gf_mul[col[0]][3];
  952. state[3][2] ^= gf_mul[col[1]][4];
  953. state[3][2] ^= gf_mul[col[2]][2];
  954. state[3][2] ^= gf_mul[col[3]][5];
  955. // Column 4
  956. col[0] = state[0][3];
  957. col[1] = state[1][3];
  958. col[2] = state[2][3];
  959. col[3] = state[3][3];
  960. state[0][3] = gf_mul[col[0]][5];
  961. state[0][3] ^= gf_mul[col[1]][3];
  962. state[0][3] ^= gf_mul[col[2]][4];
  963. state[0][3] ^= gf_mul[col[3]][2];
  964. state[1][3] = gf_mul[col[0]][2];
  965. state[1][3] ^= gf_mul[col[1]][5];
  966. state[1][3] ^= gf_mul[col[2]][3];
  967. state[1][3] ^= gf_mul[col[3]][4];
  968. state[2][3] = gf_mul[col[0]][4];
  969. state[2][3] ^= gf_mul[col[1]][2];
  970. state[2][3] ^= gf_mul[col[2]][5];
  971. state[2][3] ^= gf_mul[col[3]][3];
  972. state[3][3] = gf_mul[col[0]][3];
  973. state[3][3] ^= gf_mul[col[1]][4];
  974. state[3][3] ^= gf_mul[col[2]][2];
  975. state[3][3] ^= gf_mul[col[3]][5];
  976. }
  977. /////////////////
  978. // (En/De)Crypt
  979. /////////////////
  980. void aes_encrypt(const BYTE in[], BYTE out[], const UINT key[], int keysize)
  981. {
  982. BYTE state[4][4];
  983. // Copy input array (should be 16 bytes long) to a matrix (sequential bytes are ordered
  984. // by row, not col) called "state" for processing.
  985. // *** Implementation note: The official AES documentation references the state by
  986. // column, then row. Accessing an element in C requires row then column. Thus, all state
  987. // references in AES must have the column and row indexes reversed for C implementation.
  988. state[0][0] = in[0];
  989. state[1][0] = in[1];
  990. state[2][0] = in[2];
  991. state[3][0] = in[3];
  992. state[0][1] = in[4];
  993. state[1][1] = in[5];
  994. state[2][1] = in[6];
  995. state[3][1] = in[7];
  996. state[0][2] = in[8];
  997. state[1][2] = in[9];
  998. state[2][2] = in[10];
  999. state[3][2] = in[11];
  1000. state[0][3] = in[12];
  1001. state[1][3] = in[13];
  1002. state[2][3] = in[14];
  1003. state[3][3] = in[15];
  1004. // Perform the necessary number of rounds. The round key is added first.
  1005. // The last round does not perform the MixColumns step.
  1006. AddRoundKey(state,&key[0]);
  1007. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[4]);
  1008. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[8]);
  1009. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[12]);
  1010. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[16]);
  1011. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[20]);
  1012. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[24]);
  1013. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[28]);
  1014. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[32]);
  1015. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[36]);
  1016. if (keysize != 128) {
  1017. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[40]);
  1018. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[44]);
  1019. if (keysize != 192) {
  1020. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[48]);
  1021. SubBytes(state); ShiftRows(state); MixColumns(state); AddRoundKey(state,&key[52]);
  1022. SubBytes(state); ShiftRows(state); AddRoundKey(state,&key[56]);
  1023. }
  1024. else {
  1025. SubBytes(state); ShiftRows(state); AddRoundKey(state,&key[48]);
  1026. }
  1027. }
  1028. else {
  1029. SubBytes(state); ShiftRows(state); AddRoundKey(state,&key[40]);
  1030. }
  1031. // Copy the state to the output array.
  1032. out[0] = state[0][0];
  1033. out[1] = state[1][0];
  1034. out[2] = state[2][0];
  1035. out[3] = state[3][0];
  1036. out[4] = state[0][1];
  1037. out[5] = state[1][1];
  1038. out[6] = state[2][1];
  1039. out[7] = state[3][1];
  1040. out[8] = state[0][2];
  1041. out[9] = state[1][2];
  1042. out[10] = state[2][2];
  1043. out[11] = state[3][2];
  1044. out[12] = state[0][3];
  1045. out[13] = state[1][3];
  1046. out[14] = state[2][3];
  1047. out[15] = state[3][3];
  1048. }
  1049. void aes_decrypt(const BYTE in[], BYTE out[], const UINT key[], int keysize)
  1050. {
  1051. BYTE state[4][4];
  1052. // Copy the input to the state.
  1053. state[0][0] = in[0];
  1054. state[1][0] = in[1];
  1055. state[2][0] = in[2];
  1056. state[3][0] = in[3];
  1057. state[0][1] = in[4];
  1058. state[1][1] = in[5];
  1059. state[2][1] = in[6];
  1060. state[3][1] = in[7];
  1061. state[0][2] = in[8];
  1062. state[1][2] = in[9];
  1063. state[2][2] = in[10];
  1064. state[3][2] = in[11];
  1065. state[0][3] = in[12];
  1066. state[1][3] = in[13];
  1067. state[2][3] = in[14];
  1068. state[3][3] = in[15];
  1069. // Perform the necessary number of rounds. The round key is added first.
  1070. // The last round does not perform the MixColumns step.
  1071. if (keysize > 128) {
  1072. if (keysize > 192) {
  1073. AddRoundKey(state,&key[56]);
  1074. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[52]);InvMixColumns(state);
  1075. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[48]);InvMixColumns(state);
  1076. }
  1077. else {
  1078. AddRoundKey(state,&key[48]);
  1079. }
  1080. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[44]);InvMixColumns(state);
  1081. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[40]);InvMixColumns(state);
  1082. }
  1083. else {
  1084. AddRoundKey(state,&key[40]);
  1085. }
  1086. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[36]);InvMixColumns(state);
  1087. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[32]);InvMixColumns(state);
  1088. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[28]);InvMixColumns(state);
  1089. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[24]);InvMixColumns(state);
  1090. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[20]);InvMixColumns(state);
  1091. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[16]);InvMixColumns(state);
  1092. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[12]);InvMixColumns(state);
  1093. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[8]);InvMixColumns(state);
  1094. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[4]);InvMixColumns(state);
  1095. InvShiftRows(state);InvSubBytes(state);AddRoundKey(state,&key[0]);
  1096. // Copy the state to the output array.
  1097. out[0] = state[0][0];
  1098. out[1] = state[1][0];
  1099. out[2] = state[2][0];
  1100. out[3] = state[3][0];
  1101. out[4] = state[0][1];
  1102. out[5] = state[1][1];
  1103. out[6] = state[2][1];
  1104. out[7] = state[3][1];
  1105. out[8] = state[0][2];
  1106. out[9] = state[1][2];
  1107. out[10] = state[2][2];
  1108. out[11] = state[3][2];
  1109. out[12] = state[0][3];
  1110. out[13] = state[1][3];
  1111. out[14] = state[2][3];
  1112. out[15] = state[3][3];
  1113. }
  1114. // -------------------------------------------------- DES -------------------------------------------------- //
  1115. /****************************** MACROS ******************************/
  1116. // Obtain bit "b" from the left and shift it "c" places from the right
  1117. #define BITNUM(a,b,c) (((a[(b)/8] >> (7 - (b%8))) & 0x01) << (c))
  1118. #define BITNUMINTR(a,b,c) ((((a) >> (31 - (b))) & 0x00000001) << (c))
  1119. #define BITNUMINTL(a,b,c) ((((a) << (b)) & 0x80000000) >> (c))
  1120. // This macro converts a 6 bit block with the S-Box row defined as the first and last
  1121. // bits to a 6 bit block with the row defined by the first two bits.
  1122. #define SBOXBIT(a) (((a) & 0x20) | (((a) & 0x1f) >> 1) | (((a) & 0x01) << 4))
  1123. /**************************** VARIABLES *****************************/
  1124. static const BYTE sbox1[64] = {
  1125. 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  1126. 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  1127. 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  1128. 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
  1129. };
  1130. static const BYTE sbox2[64] = {
  1131. 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  1132. 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  1133. 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  1134. 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
  1135. };
  1136. static const BYTE sbox3[64] = {
  1137. 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  1138. 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  1139. 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  1140. 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
  1141. };
  1142. static const BYTE sbox4[64] = {
  1143. 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  1144. 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  1145. 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  1146. 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
  1147. };
  1148. static const BYTE sbox5[64] = {
  1149. 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  1150. 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  1151. 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  1152. 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
  1153. };
  1154. static const BYTE sbox6[64] = {
  1155. 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  1156. 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  1157. 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  1158. 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
  1159. };
  1160. static const BYTE sbox7[64] = {
  1161. 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  1162. 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  1163. 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  1164. 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
  1165. };
  1166. static const BYTE sbox8[64] = {
  1167. 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  1168. 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  1169. 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  1170. 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
  1171. };
  1172. /*********************** FUNCTION DEFINITIONS ***********************/
  1173. // Initial (Inv)Permutation step
  1174. void IP(UINT state[], const BYTE in[])
  1175. {
  1176. state[0] = BITNUM(in,57,31) | BITNUM(in,49,30) | BITNUM(in,41,29) | BITNUM(in,33,28) |
  1177. BITNUM(in,25,27) | BITNUM(in,17,26) | BITNUM(in,9,25) | BITNUM(in,1,24) |
  1178. BITNUM(in,59,23) | BITNUM(in,51,22) | BITNUM(in,43,21) | BITNUM(in,35,20) |
  1179. BITNUM(in,27,19) | BITNUM(in,19,18) | BITNUM(in,11,17) | BITNUM(in,3,16) |
  1180. BITNUM(in,61,15) | BITNUM(in,53,14) | BITNUM(in,45,13) | BITNUM(in,37,12) |
  1181. BITNUM(in,29,11) | BITNUM(in,21,10) | BITNUM(in,13,9) | BITNUM(in,5,8) |
  1182. BITNUM(in,63,7) | BITNUM(in,55,6) | BITNUM(in,47,5) | BITNUM(in,39,4) |
  1183. BITNUM(in,31,3) | BITNUM(in,23,2) | BITNUM(in,15,1) | BITNUM(in,7,0);
  1184. state[1] = BITNUM(in,56,31) | BITNUM(in,48,30) | BITNUM(in,40,29) | BITNUM(in,32,28) |
  1185. BITNUM(in,24,27) | BITNUM(in,16,26) | BITNUM(in,8,25) | BITNUM(in,0,24) |
  1186. BITNUM(in,58,23) | BITNUM(in,50,22) | BITNUM(in,42,21) | BITNUM(in,34,20) |
  1187. BITNUM(in,26,19) | BITNUM(in,18,18) | BITNUM(in,10,17) | BITNUM(in,2,16) |
  1188. BITNUM(in,60,15) | BITNUM(in,52,14) | BITNUM(in,44,13) | BITNUM(in,36,12) |
  1189. BITNUM(in,28,11) | BITNUM(in,20,10) | BITNUM(in,12,9) | BITNUM(in,4,8) |
  1190. BITNUM(in,62,7) | BITNUM(in,54,6) | BITNUM(in,46,5) | BITNUM(in,38,4) |
  1191. BITNUM(in,30,3) | BITNUM(in,22,2) | BITNUM(in,14,1) | BITNUM(in,6,0);
  1192. }
  1193. void InvIP(UINT state[], BYTE in[])
  1194. {
  1195. in[0] = BITNUMINTR(state[1],7,7) | BITNUMINTR(state[0],7,6) | BITNUMINTR(state[1],15,5) |
  1196. BITNUMINTR(state[0],15,4) | BITNUMINTR(state[1],23,3) | BITNUMINTR(state[0],23,2) |
  1197. BITNUMINTR(state[1],31,1) | BITNUMINTR(state[0],31,0);
  1198. in[1] = BITNUMINTR(state[1],6,7) | BITNUMINTR(state[0],6,6) | BITNUMINTR(state[1],14,5) |
  1199. BITNUMINTR(state[0],14,4) | BITNUMINTR(state[1],22,3) | BITNUMINTR(state[0],22,2) |
  1200. BITNUMINTR(state[1],30,1) | BITNUMINTR(state[0],30,0);
  1201. in[2] = BITNUMINTR(state[1],5,7) | BITNUMINTR(state[0],5,6) | BITNUMINTR(state[1],13,5) |
  1202. BITNUMINTR(state[0],13,4) | BITNUMINTR(state[1],21,3) | BITNUMINTR(state[0],21,2) |
  1203. BITNUMINTR(state[1],29,1) | BITNUMINTR(state[0],29,0);
  1204. in[3] = BITNUMINTR(state[1],4,7) | BITNUMINTR(state[0],4,6) | BITNUMINTR(state[1],12,5) |
  1205. BITNUMINTR(state[0],12,4) | BITNUMINTR(state[1],20,3) | BITNUMINTR(state[0],20,2) |
  1206. BITNUMINTR(state[1],28,1) | BITNUMINTR(state[0],28,0);
  1207. in[4] = BITNUMINTR(state[1],3,7) | BITNUMINTR(state[0],3,6) | BITNUMINTR(state[1],11,5) |
  1208. BITNUMINTR(state[0],11,4) | BITNUMINTR(state[1],19,3) | BITNUMINTR(state[0],19,2) |
  1209. BITNUMINTR(state[1],27,1) | BITNUMINTR(state[0],27,0);
  1210. in[5] = BITNUMINTR(state[1],2,7) | BITNUMINTR(state[0],2,6) | BITNUMINTR(state[1],10,5) |
  1211. BITNUMINTR(state[0],10,4) | BITNUMINTR(state[1],18,3) | BITNUMINTR(state[0],18,2) |
  1212. BITNUMINTR(state[1],26,1) | BITNUMINTR(state[0],26,0);
  1213. in[6] = BITNUMINTR(state[1],1,7) | BITNUMINTR(state[0],1,6) | BITNUMINTR(state[1],9,5) |
  1214. BITNUMINTR(state[0],9,4) | BITNUMINTR(state[1],17,3) | BITNUMINTR(state[0],17,2) |
  1215. BITNUMINTR(state[1],25,1) | BITNUMINTR(state[0],25,0);
  1216. in[7] = BITNUMINTR(state[1],0,7) | BITNUMINTR(state[0],0,6) | BITNUMINTR(state[1],8,5) |
  1217. BITNUMINTR(state[0],8,4) | BITNUMINTR(state[1],16,3) | BITNUMINTR(state[0],16,2) |
  1218. BITNUMINTR(state[1],24,1) | BITNUMINTR(state[0],24,0);
  1219. }
  1220. UINT f(UINT state, const BYTE key[])
  1221. {
  1222. BYTE lrgstate[6]; //,i;
  1223. UINT t1,t2;
  1224. // Expantion Permutation
  1225. t1 = BITNUMINTL(state,31,0) | ((state & 0xf0000000) >> 1) | BITNUMINTL(state,4,5) |
  1226. BITNUMINTL(state,3,6) | ((state & 0x0f000000) >> 3) | BITNUMINTL(state,8,11) |
  1227. BITNUMINTL(state,7,12) | ((state & 0x00f00000) >> 5) | BITNUMINTL(state,12,17) |
  1228. BITNUMINTL(state,11,18) | ((state & 0x000f0000) >> 7) | BITNUMINTL(state,16,23);
  1229. t2 = BITNUMINTL(state,15,0) | ((state & 0x0000f000) << 15) | BITNUMINTL(state,20,5) |
  1230. BITNUMINTL(state,19,6) | ((state & 0x00000f00) << 13) | BITNUMINTL(state,24,11) |
  1231. BITNUMINTL(state,23,12) | ((state & 0x000000f0) << 11) | BITNUMINTL(state,28,17) |
  1232. BITNUMINTL(state,27,18) | ((state & 0x0000000f) << 9) | BITNUMINTL(state,0,23);
  1233. lrgstate[0] = (t1 >> 24) & 0x000000ff;
  1234. lrgstate[1] = (t1 >> 16) & 0x000000ff;
  1235. lrgstate[2] = (t1 >> 8) & 0x000000ff;
  1236. lrgstate[3] = (t2 >> 24) & 0x000000ff;
  1237. lrgstate[4] = (t2 >> 16) & 0x000000ff;
  1238. lrgstate[5] = (t2 >> 8) & 0x000000ff;
  1239. // Key XOR
  1240. lrgstate[0] ^= key[0];
  1241. lrgstate[1] ^= key[1];
  1242. lrgstate[2] ^= key[2];
  1243. lrgstate[3] ^= key[3];
  1244. lrgstate[4] ^= key[4];
  1245. lrgstate[5] ^= key[5];
  1246. // S-Box Permutation
  1247. state = (sbox1[SBOXBIT(lrgstate[0] >> 2)] << 28) |
  1248. (sbox2[SBOXBIT(((lrgstate[0] & 0x03) << 4) | (lrgstate[1] >> 4))] << 24) |
  1249. (sbox3[SBOXBIT(((lrgstate[1] & 0x0f) << 2) | (lrgstate[2] >> 6))] << 20) |
  1250. (sbox4[SBOXBIT(lrgstate[2] & 0x3f)] << 16) |
  1251. (sbox5[SBOXBIT(lrgstate[3] >> 2)] << 12) |
  1252. (sbox6[SBOXBIT(((lrgstate[3] & 0x03) << 4) | (lrgstate[4] >> 4))] << 8) |
  1253. (sbox7[SBOXBIT(((lrgstate[4] & 0x0f) << 2) | (lrgstate[5] >> 6))] << 4) |
  1254. sbox8[SBOXBIT(lrgstate[5] & 0x3f)];
  1255. // P-Box Permutation
  1256. state = BITNUMINTL(state,15,0) | BITNUMINTL(state,6,1) | BITNUMINTL(state,19,2) |
  1257. BITNUMINTL(state,20,3) | BITNUMINTL(state,28,4) | BITNUMINTL(state,11,5) |
  1258. BITNUMINTL(state,27,6) | BITNUMINTL(state,16,7) | BITNUMINTL(state,0,8) |
  1259. BITNUMINTL(state,14,9) | BITNUMINTL(state,22,10) | BITNUMINTL(state,25,11) |
  1260. BITNUMINTL(state,4,12) | BITNUMINTL(state,17,13) | BITNUMINTL(state,30,14) |
  1261. BITNUMINTL(state,9,15) | BITNUMINTL(state,1,16) | BITNUMINTL(state,7,17) |
  1262. BITNUMINTL(state,23,18) | BITNUMINTL(state,13,19) | BITNUMINTL(state,31,20) |
  1263. BITNUMINTL(state,26,21) | BITNUMINTL(state,2,22) | BITNUMINTL(state,8,23) |
  1264. BITNUMINTL(state,18,24) | BITNUMINTL(state,12,25) | BITNUMINTL(state,29,26) |
  1265. BITNUMINTL(state,5,27) | BITNUMINTL(state,21,28) | BITNUMINTL(state,10,29) |
  1266. BITNUMINTL(state,3,30) | BITNUMINTL(state,24,31);
  1267. // Return the final state value
  1268. return(state);
  1269. }
  1270. void des_key_setup(const BYTE key[], BYTE schedule[][6], DES_MODE mode)
  1271. {
  1272. UINT i, j, to_gen, C, D;
  1273. const UINT key_rnd_shift[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
  1274. const UINT key_perm_c[28] = {56,48,40,32,24,16,8,0,57,49,41,33,25,17,
  1275. 9,1,58,50,42,34,26,18,10,2,59,51,43,35};
  1276. const UINT key_perm_d[28] = {62,54,46,38,30,22,14,6,61,53,45,37,29,21,
  1277. 13,5,60,52,44,36,28,20,12,4,27,19,11,3};
  1278. const UINT key_compression[48] = {13,16,10,23,0,4,2,27,14,5,20,9,
  1279. 22,18,11,3,25,7,15,6,26,19,12,1,
  1280. 40,51,30,36,46,54,29,39,50,44,32,47,
  1281. 43,48,38,55,33,52,45,41,49,35,28,31};
  1282. // Permutated Choice #1 (copy the key in, ignoring parity bits).
  1283. for (i = 0, j = 31, C = 0; i < 28; ++i, --j)
  1284. C |= BITNUM(key,key_perm_c[i],j);
  1285. for (i = 0, j = 31, D = 0; i < 28; ++i, --j)
  1286. D |= BITNUM(key,key_perm_d[i],j);
  1287. // Generate the 16 subkeys.
  1288. for (i = 0; i < 16; ++i) {
  1289. C = ((C << key_rnd_shift[i]) | (C >> (28-key_rnd_shift[i]))) & 0xfffffff0;
  1290. D = ((D << key_rnd_shift[i]) | (D >> (28-key_rnd_shift[i]))) & 0xfffffff0;
  1291. // Decryption subkeys are reverse order of encryption subkeys so
  1292. // generate them in reverse if the key schedule is for decryption useage.
  1293. if (mode == DES_DECRYPT)
  1294. to_gen = 15 - i;
  1295. else /*(if mode == DES_ENCRYPT)*/
  1296. to_gen = i;
  1297. // Initialize the array
  1298. for (j = 0; j < 6; ++j)
  1299. schedule[to_gen][j] = 0;
  1300. for (j = 0; j < 24; ++j)
  1301. schedule[to_gen][j/8] |= BITNUMINTR(C,key_compression[j],7 - (j%8));
  1302. for ( ; j < 48; ++j)
  1303. schedule[to_gen][j/8] |= BITNUMINTR(D,key_compression[j] - 28,7 - (j%8));
  1304. }
  1305. }
  1306. void des_crypt(const BYTE in[], BYTE out[], const BYTE key[][6])
  1307. {
  1308. UINT state[2],idx,t;
  1309. IP(state,in);
  1310. for (idx=0; idx < 15; ++idx) {
  1311. t = state[1];
  1312. state[1] = f(state[1],key[idx]) ^ state[0];
  1313. state[0] = t;
  1314. }
  1315. // Perform the final loop manually as it doesn't switch sides
  1316. state[0] = f(state[1],key[15]) ^ state[0];
  1317. InvIP(state,out);
  1318. }
  1319. void three_des_key_setup(const BYTE key[], BYTE schedule[][16][6], DES_MODE mode)
  1320. {
  1321. if (mode == DES_ENCRYPT) {
  1322. des_key_setup(&key[0],schedule[0],mode);
  1323. des_key_setup(&key[8],schedule[1],(DES_MODE)(!mode));
  1324. des_key_setup(&key[16],schedule[2],mode);
  1325. }
  1326. else /*if (mode == DES_DECRYPT*/ {
  1327. des_key_setup(&key[16],schedule[0],mode);
  1328. des_key_setup(&key[8],schedule[1],(DES_MODE)(!mode));
  1329. des_key_setup(&key[0],schedule[2],mode);
  1330. }
  1331. }
  1332. void three_des_crypt(const BYTE in[], BYTE out[], const BYTE key[][16][6])
  1333. {
  1334. des_crypt(in,out,key[0]);
  1335. des_crypt(out,out,key[1]);
  1336. des_crypt(out,out,key[2]);
  1337. }
  1338. // -------------------------------------------------- MD2 -------------------------------------------------- //
  1339. /**************************** VARIABLES *****************************/
  1340. static const BYTE s[256] = {
  1341. 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
  1342. 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
  1343. 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
  1344. 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
  1345. 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
  1346. 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
  1347. 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
  1348. 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
  1349. 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
  1350. 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
  1351. 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  1352. 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
  1353. 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
  1354. 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
  1355. 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
  1356. 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
  1357. 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
  1358. 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
  1359. };
  1360. /*********************** FUNCTION DEFINITIONS ***********************/
  1361. void md2_transform(_MD2_CTX *ctx, BYTE data[])
  1362. {
  1363. int j,k,t;
  1364. //memcpy(&ctx->state[16], data);
  1365. for (j=0; j < 16; ++j) {
  1366. ctx->state[j + 16] = data[j];
  1367. ctx->state[j + 32] = (ctx->state[j+16] ^ ctx->state[j]);
  1368. }
  1369. t = 0;
  1370. for (j = 0; j < 18; ++j) {
  1371. for (k = 0; k < 48; ++k) {
  1372. ctx->state[k] ^= s[t];
  1373. t = ctx->state[k];
  1374. }
  1375. t = (t+j) & 0xFF;
  1376. }
  1377. t = ctx->checksum[15];
  1378. for (j=0; j < 16; ++j) {
  1379. ctx->checksum[j] ^= s[data[j] ^ t];
  1380. t = ctx->checksum[j];
  1381. }
  1382. }
  1383. void md2_init(_MD2_CTX *ctx)
  1384. {
  1385. int i;
  1386. for (i=0; i < 48; ++i)
  1387. ctx->state[i] = 0;
  1388. for (i=0; i < 16; ++i)
  1389. ctx->checksum[i] = 0;
  1390. ctx->len = 0;
  1391. }
  1392. void md2_update(_MD2_CTX *ctx, const BYTE data[], size_t len)
  1393. {
  1394. size_t i;
  1395. for (i = 0; i < len; ++i) {
  1396. ctx->data[ctx->len] = data[i];
  1397. ctx->len++;
  1398. if (ctx->len == MD2_BLOCK_SIZE) {
  1399. md2_transform(ctx, ctx->data);
  1400. ctx->len = 0;
  1401. }
  1402. }
  1403. }
  1404. void md2_final(_MD2_CTX *ctx, BYTE hash[])
  1405. {
  1406. int to_pad;
  1407. to_pad = MD2_BLOCK_SIZE - ctx->len;
  1408. while (ctx->len < MD2_BLOCK_SIZE)
  1409. ctx->data[ctx->len++] = to_pad;
  1410. md2_transform(ctx, ctx->data);
  1411. md2_transform(ctx, ctx->checksum);
  1412. memcpy(hash, ctx->state, MD2_BLOCK_SIZE);
  1413. }
  1414. // -------------------------------------------------- MD5 -------------------------------------------------- //
  1415. /****************************** MACROS ******************************/
  1416. #define F(x,y,z) ((x & y) | (~x & z))
  1417. #define G(x,y,z) ((x & z) | (y & ~z))
  1418. #define H(x,y,z) (x ^ y ^ z)
  1419. #define I(x,y,z) (y ^ (x | ~z))
  1420. #define FF(a,b,c,d,m,s,t) { a += F(b,c,d) + m + t; \
  1421. a = b + ROTLEFT(a,s); }
  1422. #define GG(a,b,c,d,m,s,t) { a += G(b,c,d) + m + t; \
  1423. a = b + ROTLEFT(a,s); }
  1424. #define HH(a,b,c,d,m,s,t) { a += H(b,c,d) + m + t; \
  1425. a = b + ROTLEFT(a,s); }
  1426. #define II(a,b,c,d,m,s,t) { a += I(b,c,d) + m + t; \
  1427. a = b + ROTLEFT(a,s); }
  1428. /*********************** FUNCTION DEFINITIONS ***********************/
  1429. void md5_transform(_MD5_CTX *ctx, const BYTE data[])
  1430. {
  1431. UINT a, b, c, d, m[16], i, j;
  1432. // MD5 specifies big endian byte order, but this implementation assumes a little
  1433. // endian byte order CPU. Reverse all the bytes upon input, and re-reverse them
  1434. // on output (in md5_final()).
  1435. for (i = 0, j = 0; i < 16; ++i, j += 4)
  1436. m[i] = (data[j]) + (data[j + 1] << 8) + (data[j + 2] << 16) + (data[j + 3] << 24);
  1437. a = ctx->state[0];
  1438. b = ctx->state[1];
  1439. c = ctx->state[2];
  1440. d = ctx->state[3];
  1441. FF(a,b,c,d,m[0], 7,0xd76aa478);
  1442. FF(d,a,b,c,m[1], 12,0xe8c7b756);
  1443. FF(c,d,a,b,m[2], 17,0x242070db);
  1444. FF(b,c,d,a,m[3], 22,0xc1bdceee);
  1445. FF(a,b,c,d,m[4], 7,0xf57c0faf);
  1446. FF(d,a,b,c,m[5], 12,0x4787c62a);
  1447. FF(c,d,a,b,m[6], 17,0xa8304613);
  1448. FF(b,c,d,a,m[7], 22,0xfd469501);
  1449. FF(a,b,c,d,m[8], 7,0x698098d8);
  1450. FF(d,a,b,c,m[9], 12,0x8b44f7af);
  1451. FF(c,d,a,b,m[10],17,0xffff5bb1);
  1452. FF(b,c,d,a,m[11],22,0x895cd7be);
  1453. FF(a,b,c,d,m[12], 7,0x6b901122);
  1454. FF(d,a,b,c,m[13],12,0xfd987193);
  1455. FF(c,d,a,b,m[14],17,0xa679438e);
  1456. FF(b,c,d,a,m[15],22,0x49b40821);
  1457. GG(a,b,c,d,m[1], 5,0xf61e2562);
  1458. GG(d,a,b,c,m[6], 9,0xc040b340);
  1459. GG(c,d,a,b,m[11],14,0x265e5a51);
  1460. GG(b,c,d,a,m[0], 20,0xe9b6c7aa);
  1461. GG(a,b,c,d,m[5], 5,0xd62f105d);
  1462. GG(d,a,b,c,m[10], 9,0x02441453);
  1463. GG(c,d,a,b,m[15],14,0xd8a1e681);
  1464. GG(b,c,d,a,m[4], 20,0xe7d3fbc8);
  1465. GG(a,b,c,d,m[9], 5,0x21e1cde6);
  1466. GG(d,a,b,c,m[14], 9,0xc33707d6);
  1467. GG(c,d,a,b,m[3], 14,0xf4d50d87);
  1468. GG(b,c,d,a,m[8], 20,0x455a14ed);
  1469. GG(a,b,c,d,m[13], 5,0xa9e3e905);
  1470. GG(d,a,b,c,m[2], 9,0xfcefa3f8);
  1471. GG(c,d,a,b,m[7], 14,0x676f02d9);
  1472. GG(b,c,d,a,m[12],20,0x8d2a4c8a);
  1473. HH(a,b,c,d,m[5], 4,0xfffa3942);
  1474. HH(d,a,b,c,m[8], 11,0x8771f681);
  1475. HH(c,d,a,b,m[11],16,0x6d9d6122);
  1476. HH(b,c,d,a,m[14],23,0xfde5380c);
  1477. HH(a,b,c,d,m[1], 4,0xa4beea44);
  1478. HH(d,a,b,c,m[4], 11,0x4bdecfa9);
  1479. HH(c,d,a,b,m[7], 16,0xf6bb4b60);
  1480. HH(b,c,d,a,m[10],23,0xbebfbc70);
  1481. HH(a,b,c,d,m[13], 4,0x289b7ec6);
  1482. HH(d,a,b,c,m[0], 11,0xeaa127fa);
  1483. HH(c,d,a,b,m[3], 16,0xd4ef3085);
  1484. HH(b,c,d,a,m[6], 23,0x04881d05);
  1485. HH(a,b,c,d,m[9], 4,0xd9d4d039);
  1486. HH(d,a,b,c,m[12],11,0xe6db99e5);
  1487. HH(c,d,a,b,m[15],16,0x1fa27cf8);
  1488. HH(b,c,d,a,m[2], 23,0xc4ac5665);
  1489. II(a,b,c,d,m[0], 6,0xf4292244);
  1490. II(d,a,b,c,m[7], 10,0x432aff97);
  1491. II(c,d,a,b,m[14],15,0xab9423a7);
  1492. II(b,c,d,a,m[5], 21,0xfc93a039);
  1493. II(a,b,c,d,m[12], 6,0x655b59c3);
  1494. II(d,a,b,c,m[3], 10,0x8f0ccc92);
  1495. II(c,d,a,b,m[10],15,0xffeff47d);
  1496. II(b,c,d,a,m[1], 21,0x85845dd1);
  1497. II(a,b,c,d,m[8], 6,0x6fa87e4f);
  1498. II(d,a,b,c,m[15],10,0xfe2ce6e0);
  1499. II(c,d,a,b,m[6], 15,0xa3014314);
  1500. II(b,c,d,a,m[13],21,0x4e0811a1);
  1501. II(a,b,c,d,m[4], 6,0xf7537e82);
  1502. II(d,a,b,c,m[11],10,0xbd3af235);
  1503. II(c,d,a,b,m[2], 15,0x2ad7d2bb);
  1504. II(b,c,d,a,m[9], 21,0xeb86d391);
  1505. ctx->state[0] += a;
  1506. ctx->state[1] += b;
  1507. ctx->state[2] += c;
  1508. ctx->state[3] += d;
  1509. }
  1510. void md5_init(_MD5_CTX *ctx)
  1511. {
  1512. ctx->datalen = 0;
  1513. ctx->bitlen = 0;
  1514. ctx->state[0] = 0x67452301;
  1515. ctx->state[1] = 0xEFCDAB89;
  1516. ctx->state[2] = 0x98BADCFE;
  1517. ctx->state[3] = 0x10325476;
  1518. }
  1519. void md5_update(_MD5_CTX *ctx, const BYTE data[], size_t len)
  1520. {
  1521. size_t i;
  1522. for (i = 0; i < len; ++i) {
  1523. ctx->data[ctx->datalen] = data[i];
  1524. ctx->datalen++;
  1525. if (ctx->datalen == 64) {
  1526. md5_transform(ctx, ctx->data);
  1527. ctx->bitlen += 512;
  1528. ctx->datalen = 0;
  1529. }
  1530. }
  1531. }
  1532. void md5_final(_MD5_CTX *ctx, BYTE hash[])
  1533. {
  1534. size_t i;
  1535. i = ctx->datalen;
  1536. // Pad whatever data is left in the buffer.
  1537. if (ctx->datalen < 56) {
  1538. ctx->data[i++] = 0x80;
  1539. while (i < 56)
  1540. ctx->data[i++] = 0x00;
  1541. }
  1542. else if (ctx->datalen >= 56) {
  1543. ctx->data[i++] = 0x80;
  1544. while (i < 64)
  1545. ctx->data[i++] = 0x00;
  1546. md5_transform(ctx, ctx->data);
  1547. memset(ctx->data, 0, 56);
  1548. }
  1549. // Append to the padding the total message's length in bits and transform.
  1550. ctx->bitlen += ctx->datalen * 8;
  1551. ctx->data[56] = (BYTE)(ctx->bitlen);
  1552. ctx->data[57] = (BYTE)(ctx->bitlen >> 8);
  1553. ctx->data[58] = (BYTE)(ctx->bitlen >> 16);
  1554. ctx->data[59] = (BYTE)(ctx->bitlen >> 24);
  1555. ctx->data[60] = (BYTE)(ctx->bitlen >> 32);
  1556. ctx->data[61] = (BYTE)(ctx->bitlen >> 40);
  1557. ctx->data[62] = (BYTE)(ctx->bitlen >> 48);
  1558. ctx->data[63] = (BYTE)(ctx->bitlen >> 56);
  1559. md5_transform(ctx, ctx->data);
  1560. // Since this implementation uses little endian byte ordering and MD uses big endian,
  1561. // reverse all the bytes when copying the final state to the output hash.
  1562. for (i = 0; i < 4; ++i) {
  1563. hash[i] = (ctx->state[0] >> (i * 8)) & 0x000000ff;
  1564. hash[i + 4] = (ctx->state[1] >> (i * 8)) & 0x000000ff;
  1565. hash[i + 8] = (ctx->state[2] >> (i * 8)) & 0x000000ff;
  1566. hash[i + 12] = (ctx->state[3] >> (i * 8)) & 0x000000ff;
  1567. }
  1568. }
  1569. // -------------------------------------------------- SHA1 -------------------------------------------------- //
  1570. /****************************** MACROS ******************************/
  1571. /*********************** FUNCTION DEFINITIONS ***********************/
  1572. void sha1_transform(_SHA1_CTX *ctx, const BYTE data[])
  1573. {
  1574. UINT a, b, c, d, e, i, j, t, m[80];
  1575. for (i = 0, j = 0; i < 16; ++i, j += 4)
  1576. m[i] = (data[j] << 24) + (data[j + 1] << 16) + (data[j + 2] << 8) + (data[j + 3]);
  1577. for ( ; i < 80; ++i) {
  1578. m[i] = (m[i - 3] ^ m[i - 8] ^ m[i - 14] ^ m[i - 16]);
  1579. m[i] = (m[i] << 1) | (m[i] >> 31);
  1580. }
  1581. a = ctx->state[0];
  1582. b = ctx->state[1];
  1583. c = ctx->state[2];
  1584. d = ctx->state[3];
  1585. e = ctx->state[4];
  1586. for (i = 0; i < 20; ++i) {
  1587. t = ROTLEFT(a, 5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i];
  1588. e = d;
  1589. d = c;
  1590. c = ROTLEFT(b, 30);
  1591. b = a;
  1592. a = t;
  1593. }
  1594. for ( ; i < 40; ++i) {
  1595. t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[1] + m[i];
  1596. e = d;
  1597. d = c;
  1598. c = ROTLEFT(b, 30);
  1599. b = a;
  1600. a = t;
  1601. }
  1602. for ( ; i < 60; ++i) {
  1603. t = ROTLEFT(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2] + m[i];
  1604. e = d;
  1605. d = c;
  1606. c = ROTLEFT(b, 30);
  1607. b = a;
  1608. a = t;
  1609. }
  1610. for ( ; i < 80; ++i) {
  1611. t = ROTLEFT(a, 5) + (b ^ c ^ d) + e + ctx->k[3] + m[i];
  1612. e = d;
  1613. d = c;
  1614. c = ROTLEFT(b, 30);
  1615. b = a;
  1616. a = t;
  1617. }
  1618. ctx->state[0] += a;
  1619. ctx->state[1] += b;
  1620. ctx->state[2] += c;
  1621. ctx->state[3] += d;
  1622. ctx->state[4] += e;
  1623. }
  1624. void sha1_init(_SHA1_CTX *ctx)
  1625. {
  1626. ctx->datalen = 0;
  1627. ctx->bitlen = 0;
  1628. ctx->state[0] = 0x67452301;
  1629. ctx->state[1] = 0xEFCDAB89;
  1630. ctx->state[2] = 0x98BADCFE;
  1631. ctx->state[3] = 0x10325476;
  1632. ctx->state[4] = 0xc3d2e1f0;
  1633. ctx->k[0] = 0x5a827999;
  1634. ctx->k[1] = 0x6ed9eba1;
  1635. ctx->k[2] = 0x8f1bbcdc;
  1636. ctx->k[3] = 0xca62c1d6;
  1637. }
  1638. void sha1_update(_SHA1_CTX *ctx, const BYTE data[], size_t len)
  1639. {
  1640. size_t i;
  1641. for (i = 0; i < len; ++i) {
  1642. ctx->data[ctx->datalen] = data[i];
  1643. ctx->datalen++;
  1644. if (ctx->datalen == 64) {
  1645. sha1_transform(ctx, ctx->data);
  1646. ctx->bitlen += 512;
  1647. ctx->datalen = 0;
  1648. }
  1649. }
  1650. }
  1651. void sha1_final(_SHA1_CTX *ctx, BYTE hash[])
  1652. {
  1653. UINT i;
  1654. i = ctx->datalen;
  1655. // Pad whatever data is left in the buffer.
  1656. if (ctx->datalen < 56) {
  1657. ctx->data[i++] = 0x80;
  1658. while (i < 56)
  1659. ctx->data[i++] = 0x00;
  1660. }
  1661. else {
  1662. ctx->data[i++] = 0x80;
  1663. while (i < 64)
  1664. ctx->data[i++] = 0x00;
  1665. sha1_transform(ctx, ctx->data);
  1666. memset(ctx->data, 0, 56);
  1667. }
  1668. // Append to the padding the total message's length in bits and transform.
  1669. ctx->bitlen += ctx->datalen * 8;
  1670. ctx->data[63] = (BYTE)(ctx->bitlen);
  1671. ctx->data[62] = (BYTE)(ctx->bitlen >> 8);
  1672. ctx->data[61] = (BYTE)(ctx->bitlen >> 16);
  1673. ctx->data[60] = (BYTE)(ctx->bitlen >> 24);
  1674. ctx->data[59] = (BYTE)(ctx->bitlen >> 32);
  1675. ctx->data[58] = (BYTE)(ctx->bitlen >> 40);
  1676. ctx->data[57] = (BYTE)(ctx->bitlen >> 48);
  1677. ctx->data[56] = (BYTE)(ctx->bitlen >> 56);
  1678. sha1_transform(ctx, ctx->data);
  1679. // Since this implementation uses little endian byte ordering and MD uses big endian,
  1680. // reverse all the bytes when copying the final state to the output hash.
  1681. for (i = 0; i < 4; ++i) {
  1682. hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
  1683. hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
  1684. hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
  1685. hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
  1686. hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
  1687. }
  1688. }
  1689. // -------------------------------------------------- SHA256 -------------------------------------------------- //
  1690. /****************************** MACROS ******************************/
  1691. #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  1692. #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  1693. #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
  1694. #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
  1695. #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
  1696. #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
  1697. /**************************** VARIABLES *****************************/
  1698. static const UINT k[64] = {
  1699. 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
  1700. 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
  1701. 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
  1702. 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
  1703. 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
  1704. 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
  1705. 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
  1706. 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  1707. };
  1708. /*********************** FUNCTION DEFINITIONS ***********************/
  1709. void sha256_transform(_SHA256_CTX *ctx, const BYTE data[])
  1710. {
  1711. UINT a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
  1712. for (i = 0, j = 0; i < 16; ++i, j += 4)
  1713. m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
  1714. for ( ; i < 64; ++i)
  1715. m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
  1716. a = ctx->state[0];
  1717. b = ctx->state[1];
  1718. c = ctx->state[2];
  1719. d = ctx->state[3];
  1720. e = ctx->state[4];
  1721. f = ctx->state[5];
  1722. g = ctx->state[6];
  1723. h = ctx->state[7];
  1724. for (i = 0; i < 64; ++i) {
  1725. t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
  1726. t2 = EP0(a) + MAJ(a,b,c);
  1727. h = g;
  1728. g = f;
  1729. f = e;
  1730. e = d + t1;
  1731. d = c;
  1732. c = b;
  1733. b = a;
  1734. a = t1 + t2;
  1735. }
  1736. ctx->state[0] += a;
  1737. ctx->state[1] += b;
  1738. ctx->state[2] += c;
  1739. ctx->state[3] += d;
  1740. ctx->state[4] += e;
  1741. ctx->state[5] += f;
  1742. ctx->state[6] += g;
  1743. ctx->state[7] += h;
  1744. }
  1745. void sha256_init(_SHA256_CTX *ctx)
  1746. {
  1747. ctx->datalen = 0;
  1748. ctx->bitlen = 0;
  1749. ctx->state[0] = 0x6a09e667;
  1750. ctx->state[1] = 0xbb67ae85;
  1751. ctx->state[2] = 0x3c6ef372;
  1752. ctx->state[3] = 0xa54ff53a;
  1753. ctx->state[4] = 0x510e527f;
  1754. ctx->state[5] = 0x9b05688c;
  1755. ctx->state[6] = 0x1f83d9ab;
  1756. ctx->state[7] = 0x5be0cd19;
  1757. }
  1758. void sha256_update(_SHA256_CTX *ctx, const BYTE data[], size_t len)
  1759. {
  1760. UINT i;
  1761. for (i = 0; i < len; ++i) {
  1762. ctx->data[ctx->datalen] = data[i];
  1763. ctx->datalen++;
  1764. if (ctx->datalen == 64) {
  1765. sha256_transform(ctx, ctx->data);
  1766. ctx->bitlen += 512;
  1767. ctx->datalen = 0;
  1768. }
  1769. }
  1770. }
  1771. void sha256_final(_SHA256_CTX *ctx, BYTE hash[])
  1772. {
  1773. UINT i;
  1774. i = ctx->datalen;
  1775. // Pad whatever data is left in the buffer.
  1776. if (ctx->datalen < 56) {
  1777. ctx->data[i++] = 0x80;
  1778. while (i < 56)
  1779. ctx->data[i++] = 0x00;
  1780. }
  1781. else {
  1782. ctx->data[i++] = 0x80;
  1783. while (i < 64)
  1784. ctx->data[i++] = 0x00;
  1785. sha256_transform(ctx, ctx->data);
  1786. memset(ctx->data, 0, 56);
  1787. }
  1788. // Append to the padding the total message's length in bits and transform.
  1789. ctx->bitlen += ctx->datalen * 8;
  1790. ctx->data[63] = (BYTE)(ctx->bitlen);
  1791. ctx->data[62] = (BYTE)(ctx->bitlen >> 8);
  1792. ctx->data[61] = (BYTE)(ctx->bitlen >> 16);
  1793. ctx->data[60] = (BYTE)(ctx->bitlen >> 24);
  1794. ctx->data[59] = (BYTE)(ctx->bitlen >> 32);
  1795. ctx->data[58] = (BYTE)(ctx->bitlen >> 40);
  1796. ctx->data[57] = (BYTE)(ctx->bitlen >> 48);
  1797. ctx->data[56] = (BYTE)(ctx->bitlen >> 56);
  1798. sha256_transform(ctx, ctx->data);
  1799. // Since this implementation uses little endian byte ordering and SHA uses big endian,
  1800. // reverse all the bytes when copying the final state to the output hash.
  1801. for (i = 0; i < 4; ++i) {
  1802. hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
  1803. hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
  1804. hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
  1805. hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
  1806. hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
  1807. hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
  1808. hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
  1809. hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
  1810. }
  1811. }
  1812. // -------------------------------------------------- ARCFOUR -------------------------------------------------- //
  1813. /*********************** FUNCTION DEFINITIONS ***********************/
  1814. void arcfour_key_setup(BYTE state[], const BYTE key[], int len)
  1815. {
  1816. int i, j;
  1817. BYTE t;
  1818. for (i = 0; i < 256; ++i)
  1819. state[i] = i;
  1820. for (i = 0, j = 0; i < 256; ++i) {
  1821. j = (j + state[i] + key[i % len]) % 256;
  1822. t = state[i];
  1823. state[i] = state[j];
  1824. state[j] = t;
  1825. }
  1826. }
  1827. // This does not hold state between calls. It always generates the
  1828. // stream starting from the first output byte.
  1829. void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len)
  1830. {
  1831. int i, j;
  1832. size_t idx;
  1833. BYTE t;
  1834. for (idx = 0, i = 0, j = 0; idx < len; ++idx) {
  1835. i = (i + 1) % 256;
  1836. j = (j + state[i]) % 256;
  1837. t = state[i];
  1838. state[i] = state[j];
  1839. state[j] = t;
  1840. out[idx] = state[(state[i] + state[j]) % 256];
  1841. }
  1842. }
  1843. // -------------------------------------------------- BLOWFISH -------------------------------------------------- //
  1844. /****************************** MACROS ******************************/
  1845. #define BF(x,t) t = keystruct->s[0][(x) >> 24]; \
  1846. t += keystruct->s[1][((x) >> 16) & 0xff]; \
  1847. t ^= keystruct->s[2][((x) >> 8) & 0xff]; \
  1848. t += keystruct->s[3][(x) & 0xff];
  1849. #define swap(r,l,t) t = l; l = r; r = t;
  1850. #define ITERATION(l,r,t,pval) l ^= keystruct->p[pval]; BF(l,t); r^= t; swap(r,l,t);
  1851. /**************************** VARIABLES *****************************/
  1852. static const UINT p_perm[18] = {
  1853. 0x243F6A88,0x85A308D3,0x13198A2E,0x03707344,0xA4093822,0x299F31D0,0x082EFA98,
  1854. 0xEC4E6C89,0x452821E6,0x38D01377,0xBE5466CF,0x34E90C6C,0xC0AC29B7,0xC97C50DD,
  1855. 0x3F84D5B5,0xB5470917,0x9216D5D9,0x8979FB1B
  1856. };
  1857. static const UINT s_perm[4][256] = { {
  1858. 0xD1310BA6,0x98DFB5AC,0x2FFD72DB,0xD01ADFB7,0xB8E1AFED,0x6A267E96,0xBA7C9045,0xF12C7F99,
  1859. 0x24A19947,0xB3916CF7,0x0801F2E2,0x858EFC16,0x636920D8,0x71574E69,0xA458FEA3,0xF4933D7E,
  1860. 0x0D95748F,0x728EB658,0x718BCD58,0x82154AEE,0x7B54A41D,0xC25A59B5,0x9C30D539,0x2AF26013,
  1861. 0xC5D1B023,0x286085F0,0xCA417918,0xB8DB38EF,0x8E79DCB0,0x603A180E,0x6C9E0E8B,0xB01E8A3E,
  1862. 0xD71577C1,0xBD314B27,0x78AF2FDA,0x55605C60,0xE65525F3,0xAA55AB94,0x57489862,0x63E81440,
  1863. 0x55CA396A,0x2AAB10B6,0xB4CC5C34,0x1141E8CE,0xA15486AF,0x7C72E993,0xB3EE1411,0x636FBC2A,
  1864. 0x2BA9C55D,0x741831F6,0xCE5C3E16,0x9B87931E,0xAFD6BA33,0x6C24CF5C,0x7A325381,0x28958677,
  1865. 0x3B8F4898,0x6B4BB9AF,0xC4BFE81B,0x66282193,0x61D809CC,0xFB21A991,0x487CAC60,0x5DEC8032,
  1866. 0xEF845D5D,0xE98575B1,0xDC262302,0xEB651B88,0x23893E81,0xD396ACC5,0x0F6D6FF3,0x83F44239,
  1867. 0x2E0B4482,0xA4842004,0x69C8F04A,0x9E1F9B5E,0x21C66842,0xF6E96C9A,0x670C9C61,0xABD388F0,
  1868. 0x6A51A0D2,0xD8542F68,0x960FA728,0xAB5133A3,0x6EEF0B6C,0x137A3BE4,0xBA3BF050,0x7EFB2A98,
  1869. 0xA1F1651D,0x39AF0176,0x66CA593E,0x82430E88,0x8CEE8619,0x456F9FB4,0x7D84A5C3,0x3B8B5EBE,
  1870. 0xE06F75D8,0x85C12073,0x401A449F,0x56C16AA6,0x4ED3AA62,0x363F7706,0x1BFEDF72,0x429B023D,
  1871. 0x37D0D724,0xD00A1248,0xDB0FEAD3,0x49F1C09B,0x075372C9,0x80991B7B,0x25D479D8,0xF6E8DEF7,
  1872. 0xE3FE501A,0xB6794C3B,0x976CE0BD,0x04C006BA,0xC1A94FB6,0x409F60C4,0x5E5C9EC2,0x196A2463,
  1873. 0x68FB6FAF,0x3E6C53B5,0x1339B2EB,0x3B52EC6F,0x6DFC511F,0x9B30952C,0xCC814544,0xAF5EBD09,
  1874. 0xBEE3D004,0xDE334AFD,0x660F2807,0x192E4BB3,0xC0CBA857,0x45C8740F,0xD20B5F39,0xB9D3FBDB,
  1875. 0x5579C0BD,0x1A60320A,0xD6A100C6,0x402C7279,0x679F25FE,0xFB1FA3CC,0x8EA5E9F8,0xDB3222F8,
  1876. 0x3C7516DF,0xFD616B15,0x2F501EC8,0xAD0552AB,0x323DB5FA,0xFD238760,0x53317B48,0x3E00DF82,
  1877. 0x9E5C57BB,0xCA6F8CA0,0x1A87562E,0xDF1769DB,0xD542A8F6,0x287EFFC3,0xAC6732C6,0x8C4F5573,
  1878. 0x695B27B0,0xBBCA58C8,0xE1FFA35D,0xB8F011A0,0x10FA3D98,0xFD2183B8,0x4AFCB56C,0x2DD1D35B,
  1879. 0x9A53E479,0xB6F84565,0xD28E49BC,0x4BFB9790,0xE1DDF2DA,0xA4CB7E33,0x62FB1341,0xCEE4C6E8,
  1880. 0xEF20CADA,0x36774C01,0xD07E9EFE,0x2BF11FB4,0x95DBDA4D,0xAE909198,0xEAAD8E71,0x6B93D5A0,
  1881. 0xD08ED1D0,0xAFC725E0,0x8E3C5B2F,0x8E7594B7,0x8FF6E2FB,0xF2122B64,0x8888B812,0x900DF01C,
  1882. 0x4FAD5EA0,0x688FC31C,0xD1CFF191,0xB3A8C1AD,0x2F2F2218,0xBE0E1777,0xEA752DFE,0x8B021FA1,
  1883. 0xE5A0CC0F,0xB56F74E8,0x18ACF3D6,0xCE89E299,0xB4A84FE0,0xFD13E0B7,0x7CC43B81,0xD2ADA8D9,
  1884. 0x165FA266,0x80957705,0x93CC7314,0x211A1477,0xE6AD2065,0x77B5FA86,0xC75442F5,0xFB9D35CF,
  1885. 0xEBCDAF0C,0x7B3E89A0,0xD6411BD3,0xAE1E7E49,0x00250E2D,0x2071B35E,0x226800BB,0x57B8E0AF,
  1886. 0x2464369B,0xF009B91E,0x5563911D,0x59DFA6AA,0x78C14389,0xD95A537F,0x207D5BA2,0x02E5B9C5,
  1887. 0x83260376,0x6295CFA9,0x11C81968,0x4E734A41,0xB3472DCA,0x7B14A94A,0x1B510052,0x9A532915,
  1888. 0xD60F573F,0xBC9BC6E4,0x2B60A476,0x81E67400,0x08BA6FB5,0x571BE91F,0xF296EC6B,0x2A0DD915,
  1889. 0xB6636521,0xE7B9F9B6,0xFF34052E,0xC5855664,0x53B02D5D,0xA99F8FA1,0x08BA4799,0x6E85076A
  1890. },{
  1891. 0x4B7A70E9,0xB5B32944,0xDB75092E,0xC4192623,0xAD6EA6B0,0x49A7DF7D,0x9CEE60B8,0x8FEDB266,
  1892. 0xECAA8C71,0x699A17FF,0x5664526C,0xC2B19EE1,0x193602A5,0x75094C29,0xA0591340,0xE4183A3E,
  1893. 0x3F54989A,0x5B429D65,0x6B8FE4D6,0x99F73FD6,0xA1D29C07,0xEFE830F5,0x4D2D38E6,0xF0255DC1,
  1894. 0x4CDD2086,0x8470EB26,0x6382E9C6,0x021ECC5E,0x09686B3F,0x3EBAEFC9,0x3C971814,0x6B6A70A1,
  1895. 0x687F3584,0x52A0E286,0xB79C5305,0xAA500737,0x3E07841C,0x7FDEAE5C,0x8E7D44EC,0x5716F2B8,
  1896. 0xB03ADA37,0xF0500C0D,0xF01C1F04,0x0200B3FF,0xAE0CF51A,0x3CB574B2,0x25837A58,0xDC0921BD,
  1897. 0xD19113F9,0x7CA92FF6,0x94324773,0x22F54701,0x3AE5E581,0x37C2DADC,0xC8B57634,0x9AF3DDA7,
  1898. 0xA9446146,0x0FD0030E,0xECC8C73E,0xA4751E41,0xE238CD99,0x3BEA0E2F,0x3280BBA1,0x183EB331,
  1899. 0x4E548B38,0x4F6DB908,0x6F420D03,0xF60A04BF,0x2CB81290,0x24977C79,0x5679B072,0xBCAF89AF,
  1900. 0xDE9A771F,0xD9930810,0xB38BAE12,0xDCCF3F2E,0x5512721F,0x2E6B7124,0x501ADDE6,0x9F84CD87,
  1901. 0x7A584718,0x7408DA17,0xBC9F9ABC,0xE94B7D8C,0xEC7AEC3A,0xDB851DFA,0x63094366,0xC464C3D2,
  1902. 0xEF1C1847,0x3215D908,0xDD433B37,0x24C2BA16,0x12A14D43,0x2A65C451,0x50940002,0x133AE4DD,
  1903. 0x71DFF89E,0x10314E55,0x81AC77D6,0x5F11199B,0x043556F1,0xD7A3C76B,0x3C11183B,0x5924A509,
  1904. 0xF28FE6ED,0x97F1FBFA,0x9EBABF2C,0x1E153C6E,0x86E34570,0xEAE96FB1,0x860E5E0A,0x5A3E2AB3,
  1905. 0x771FE71C,0x4E3D06FA,0x2965DCB9,0x99E71D0F,0x803E89D6,0x5266C825,0x2E4CC978,0x9C10B36A,
  1906. 0xC6150EBA,0x94E2EA78,0xA5FC3C53,0x1E0A2DF4,0xF2F74EA7,0x361D2B3D,0x1939260F,0x19C27960,
  1907. 0x5223A708,0xF71312B6,0xEBADFE6E,0xEAC31F66,0xE3BC4595,0xA67BC883,0xB17F37D1,0x018CFF28,
  1908. 0xC332DDEF,0xBE6C5AA5,0x65582185,0x68AB9802,0xEECEA50F,0xDB2F953B,0x2AEF7DAD,0x5B6E2F84,
  1909. 0x1521B628,0x29076170,0xECDD4775,0x619F1510,0x13CCA830,0xEB61BD96,0x0334FE1E,0xAA0363CF,
  1910. 0xB5735C90,0x4C70A239,0xD59E9E0B,0xCBAADE14,0xEECC86BC,0x60622CA7,0x9CAB5CAB,0xB2F3846E,
  1911. 0x648B1EAF,0x19BDF0CA,0xA02369B9,0x655ABB50,0x40685A32,0x3C2AB4B3,0x319EE9D5,0xC021B8F7,
  1912. 0x9B540B19,0x875FA099,0x95F7997E,0x623D7DA8,0xF837889A,0x97E32D77,0x11ED935F,0x16681281,
  1913. 0x0E358829,0xC7E61FD6,0x96DEDFA1,0x7858BA99,0x57F584A5,0x1B227263,0x9B83C3FF,0x1AC24696,
  1914. 0xCDB30AEB,0x532E3054,0x8FD948E4,0x6DBC3128,0x58EBF2EF,0x34C6FFEA,0xFE28ED61,0xEE7C3C73,
  1915. 0x5D4A14D9,0xE864B7E3,0x42105D14,0x203E13E0,0x45EEE2B6,0xA3AAABEA,0xDB6C4F15,0xFACB4FD0,
  1916. 0xC742F442,0xEF6ABBB5,0x654F3B1D,0x41CD2105,0xD81E799E,0x86854DC7,0xE44B476A,0x3D816250,
  1917. 0xCF62A1F2,0x5B8D2646,0xFC8883A0,0xC1C7B6A3,0x7F1524C3,0x69CB7492,0x47848A0B,0x5692B285,
  1918. 0x095BBF00,0xAD19489D,0x1462B174,0x23820E00,0x58428D2A,0x0C55F5EA,0x1DADF43E,0x233F7061,
  1919. 0x3372F092,0x8D937E41,0xD65FECF1,0x6C223BDB,0x7CDE3759,0xCBEE7460,0x4085F2A7,0xCE77326E,
  1920. 0xA6078084,0x19F8509E,0xE8EFD855,0x61D99735,0xA969A7AA,0xC50C06C2,0x5A04ABFC,0x800BCADC,
  1921. 0x9E447A2E,0xC3453484,0xFDD56705,0x0E1E9EC9,0xDB73DBD3,0x105588CD,0x675FDA79,0xE3674340,
  1922. 0xC5C43465,0x713E38D8,0x3D28F89E,0xF16DFF20,0x153E21E7,0x8FB03D4A,0xE6E39F2B,0xDB83ADF7
  1923. },{
  1924. 0xE93D5A68,0x948140F7,0xF64C261C,0x94692934,0x411520F7,0x7602D4F7,0xBCF46B2E,0xD4A20068,
  1925. 0xD4082471,0x3320F46A,0x43B7D4B7,0x500061AF,0x1E39F62E,0x97244546,0x14214F74,0xBF8B8840,
  1926. 0x4D95FC1D,0x96B591AF,0x70F4DDD3,0x66A02F45,0xBFBC09EC,0x03BD9785,0x7FAC6DD0,0x31CB8504,
  1927. 0x96EB27B3,0x55FD3941,0xDA2547E6,0xABCA0A9A,0x28507825,0x530429F4,0x0A2C86DA,0xE9B66DFB,
  1928. 0x68DC1462,0xD7486900,0x680EC0A4,0x27A18DEE,0x4F3FFEA2,0xE887AD8C,0xB58CE006,0x7AF4D6B6,
  1929. 0xAACE1E7C,0xD3375FEC,0xCE78A399,0x406B2A42,0x20FE9E35,0xD9F385B9,0xEE39D7AB,0x3B124E8B,
  1930. 0x1DC9FAF7,0x4B6D1856,0x26A36631,0xEAE397B2,0x3A6EFA74,0xDD5B4332,0x6841E7F7,0xCA7820FB,
  1931. 0xFB0AF54E,0xD8FEB397,0x454056AC,0xBA489527,0x55533A3A,0x20838D87,0xFE6BA9B7,0xD096954B,
  1932. 0x55A867BC,0xA1159A58,0xCCA92963,0x99E1DB33,0xA62A4A56,0x3F3125F9,0x5EF47E1C,0x9029317C,
  1933. 0xFDF8E802,0x04272F70,0x80BB155C,0x05282CE3,0x95C11548,0xE4C66D22,0x48C1133F,0xC70F86DC,
  1934. 0x07F9C9EE,0x41041F0F,0x404779A4,0x5D886E17,0x325F51EB,0xD59BC0D1,0xF2BCC18F,0x41113564,
  1935. 0x257B7834,0x602A9C60,0xDFF8E8A3,0x1F636C1B,0x0E12B4C2,0x02E1329E,0xAF664FD1,0xCAD18115,
  1936. 0x6B2395E0,0x333E92E1,0x3B240B62,0xEEBEB922,0x85B2A20E,0xE6BA0D99,0xDE720C8C,0x2DA2F728,
  1937. 0xD0127845,0x95B794FD,0x647D0862,0xE7CCF5F0,0x5449A36F,0x877D48FA,0xC39DFD27,0xF33E8D1E,
  1938. 0x0A476341,0x992EFF74,0x3A6F6EAB,0xF4F8FD37,0xA812DC60,0xA1EBDDF8,0x991BE14C,0xDB6E6B0D,
  1939. 0xC67B5510,0x6D672C37,0x2765D43B,0xDCD0E804,0xF1290DC7,0xCC00FFA3,0xB5390F92,0x690FED0B,
  1940. 0x667B9FFB,0xCEDB7D9C,0xA091CF0B,0xD9155EA3,0xBB132F88,0x515BAD24,0x7B9479BF,0x763BD6EB,
  1941. 0x37392EB3,0xCC115979,0x8026E297,0xF42E312D,0x6842ADA7,0xC66A2B3B,0x12754CCC,0x782EF11C,
  1942. 0x6A124237,0xB79251E7,0x06A1BBE6,0x4BFB6350,0x1A6B1018,0x11CAEDFA,0x3D25BDD8,0xE2E1C3C9,
  1943. 0x44421659,0x0A121386,0xD90CEC6E,0xD5ABEA2A,0x64AF674E,0xDA86A85F,0xBEBFE988,0x64E4C3FE,
  1944. 0x9DBC8057,0xF0F7C086,0x60787BF8,0x6003604D,0xD1FD8346,0xF6381FB0,0x7745AE04,0xD736FCCC,
  1945. 0x83426B33,0xF01EAB71,0xB0804187,0x3C005E5F,0x77A057BE,0xBDE8AE24,0x55464299,0xBF582E61,
  1946. 0x4E58F48F,0xF2DDFDA2,0xF474EF38,0x8789BDC2,0x5366F9C3,0xC8B38E74,0xB475F255,0x46FCD9B9,
  1947. 0x7AEB2661,0x8B1DDF84,0x846A0E79,0x915F95E2,0x466E598E,0x20B45770,0x8CD55591,0xC902DE4C,
  1948. 0xB90BACE1,0xBB8205D0,0x11A86248,0x7574A99E,0xB77F19B6,0xE0A9DC09,0x662D09A1,0xC4324633,
  1949. 0xE85A1F02,0x09F0BE8C,0x4A99A025,0x1D6EFE10,0x1AB93D1D,0x0BA5A4DF,0xA186F20F,0x2868F169,
  1950. 0xDCB7DA83,0x573906FE,0xA1E2CE9B,0x4FCD7F52,0x50115E01,0xA70683FA,0xA002B5C4,0x0DE6D027,
  1951. 0x9AF88C27,0x773F8641,0xC3604C06,0x61A806B5,0xF0177A28,0xC0F586E0,0x006058AA,0x30DC7D62,
  1952. 0x11E69ED7,0x2338EA63,0x53C2DD94,0xC2C21634,0xBBCBEE56,0x90BCB6DE,0xEBFC7DA1,0xCE591D76,
  1953. 0x6F05E409,0x4B7C0188,0x39720A3D,0x7C927C24,0x86E3725F,0x724D9DB9,0x1AC15BB4,0xD39EB8FC,
  1954. 0xED545578,0x08FCA5B5,0xD83D7CD3,0x4DAD0FC4,0x1E50EF5E,0xB161E6F8,0xA28514D9,0x6C51133C,
  1955. 0x6FD5C7E7,0x56E14EC4,0x362ABFCE,0xDDC6C837,0xD79A3234,0x92638212,0x670EFA8E,0x406000E0
  1956. },{
  1957. 0x3A39CE37,0xD3FAF5CF,0xABC27737,0x5AC52D1B,0x5CB0679E,0x4FA33742,0xD3822740,0x99BC9BBE,
  1958. 0xD5118E9D,0xBF0F7315,0xD62D1C7E,0xC700C47B,0xB78C1B6B,0x21A19045,0xB26EB1BE,0x6A366EB4,
  1959. 0x5748AB2F,0xBC946E79,0xC6A376D2,0x6549C2C8,0x530FF8EE,0x468DDE7D,0xD5730A1D,0x4CD04DC6,
  1960. 0x2939BBDB,0xA9BA4650,0xAC9526E8,0xBE5EE304,0xA1FAD5F0,0x6A2D519A,0x63EF8CE2,0x9A86EE22,
  1961. 0xC089C2B8,0x43242EF6,0xA51E03AA,0x9CF2D0A4,0x83C061BA,0x9BE96A4D,0x8FE51550,0xBA645BD6,
  1962. 0x2826A2F9,0xA73A3AE1,0x4BA99586,0xEF5562E9,0xC72FEFD3,0xF752F7DA,0x3F046F69,0x77FA0A59,
  1963. 0x80E4A915,0x87B08601,0x9B09E6AD,0x3B3EE593,0xE990FD5A,0x9E34D797,0x2CF0B7D9,0x022B8B51,
  1964. 0x96D5AC3A,0x017DA67D,0xD1CF3ED6,0x7C7D2D28,0x1F9F25CF,0xADF2B89B,0x5AD6B472,0x5A88F54C,
  1965. 0xE029AC71,0xE019A5E6,0x47B0ACFD,0xED93FA9B,0xE8D3C48D,0x283B57CC,0xF8D56629,0x79132E28,
  1966. 0x785F0191,0xED756055,0xF7960E44,0xE3D35E8C,0x15056DD4,0x88F46DBA,0x03A16125,0x0564F0BD,
  1967. 0xC3EB9E15,0x3C9057A2,0x97271AEC,0xA93A072A,0x1B3F6D9B,0x1E6321F5,0xF59C66FB,0x26DCF319,
  1968. 0x7533D928,0xB155FDF5,0x03563482,0x8ABA3CBB,0x28517711,0xC20AD9F8,0xABCC5167,0xCCAD925F,
  1969. 0x4DE81751,0x3830DC8E,0x379D5862,0x9320F991,0xEA7A90C2,0xFB3E7BCE,0x5121CE64,0x774FBE32,
  1970. 0xA8B6E37E,0xC3293D46,0x48DE5369,0x6413E680,0xA2AE0810,0xDD6DB224,0x69852DFD,0x09072166,
  1971. 0xB39A460A,0x6445C0DD,0x586CDECF,0x1C20C8AE,0x5BBEF7DD,0x1B588D40,0xCCD2017F,0x6BB4E3BB,
  1972. 0xDDA26A7E,0x3A59FF45,0x3E350A44,0xBCB4CDD5,0x72EACEA8,0xFA6484BB,0x8D6612AE,0xBF3C6F47,
  1973. 0xD29BE463,0x542F5D9E,0xAEC2771B,0xF64E6370,0x740E0D8D,0xE75B1357,0xF8721671,0xAF537D5D,
  1974. 0x4040CB08,0x4EB4E2CC,0x34D2466A,0x0115AF84,0xE1B00428,0x95983A1D,0x06B89FB4,0xCE6EA048,
  1975. 0x6F3F3B82,0x3520AB82,0x011A1D4B,0x277227F8,0x611560B1,0xE7933FDC,0xBB3A792B,0x344525BD,
  1976. 0xA08839E1,0x51CE794B,0x2F32C9B7,0xA01FBAC9,0xE01CC87E,0xBCC7D1F6,0xCF0111C3,0xA1E8AAC7,
  1977. 0x1A908749,0xD44FBD9A,0xD0DADECB,0xD50ADA38,0x0339C32A,0xC6913667,0x8DF9317C,0xE0B12B4F,
  1978. 0xF79E59B7,0x43F5BB3A,0xF2D519FF,0x27D9459C,0xBF97222C,0x15E6FC2A,0x0F91FC71,0x9B941525,
  1979. 0xFAE59361,0xCEB69CEB,0xC2A86459,0x12BAA8D1,0xB6C1075E,0xE3056A0C,0x10D25065,0xCB03A442,
  1980. 0xE0EC6E0E,0x1698DB3B,0x4C98A0BE,0x3278E964,0x9F1F9532,0xE0D392DF,0xD3A0342B,0x8971F21E,
  1981. 0x1B0A7441,0x4BA3348C,0xC5BE7120,0xC37632D8,0xDF359F8D,0x9B992F2E,0xE60B6F47,0x0FE3F11D,
  1982. 0xE54CDA54,0x1EDAD891,0xCE6279CF,0xCD3E7E6F,0x1618B166,0xFD2C1D05,0x848FD2C5,0xF6FB2299,
  1983. 0xF523F357,0xA6327623,0x93A83531,0x56CCCD02,0xACF08162,0x5A75EBB5,0x6E163697,0x88D273CC,
  1984. 0xDE966292,0x81B949D0,0x4C50901B,0x71C65614,0xE6C6C7BD,0x327A140A,0x45E1D006,0xC3F27B9A,
  1985. 0xC9AA53FD,0x62A80F00,0xBB25BFE2,0x35BDD2F6,0x71126905,0xB2040222,0xB6CBCF7C,0xCD769C2B,
  1986. 0x53113EC0,0x1640E3D3,0x38ABBD60,0x2547ADF0,0xBA38209C,0xF746CE76,0x77AFA1C5,0x20756060,
  1987. 0x85CBFE4E,0x8AE88DD8,0x7AAAF9B0,0x4CF9AA7E,0x1948C25C,0x02FB8A8C,0x01C36AE4,0xD6EBE1F9,
  1988. 0x90D4F869,0xA65CDEA0,0x3F09252D,0xC208E69F,0xB74E6132,0xCE77E25B,0x578FDFE3,0x3AC372E6
  1989. } };
  1990. /*********************** FUNCTION DEFINITIONS ***********************/
  1991. void blowfish_encrypt(const BYTE in[], BYTE out[], const _BLOWFISH_KEY *keystruct)
  1992. {
  1993. UINT l,r,t; //,i;
  1994. l = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | (in[3]);
  1995. r = (in[4] << 24) | (in[5] << 16) | (in[6] << 8) | (in[7]);
  1996. ITERATION(l,r,t,0);
  1997. ITERATION(l,r,t,1);
  1998. ITERATION(l,r,t,2);
  1999. ITERATION(l,r,t,3);
  2000. ITERATION(l,r,t,4);
  2001. ITERATION(l,r,t,5);
  2002. ITERATION(l,r,t,6);
  2003. ITERATION(l,r,t,7);
  2004. ITERATION(l,r,t,8);
  2005. ITERATION(l,r,t,9);
  2006. ITERATION(l,r,t,10);
  2007. ITERATION(l,r,t,11);
  2008. ITERATION(l,r,t,12);
  2009. ITERATION(l,r,t,13);
  2010. ITERATION(l,r,t,14);
  2011. l ^= keystruct->p[15]; BF(l,t); r^= t; //Last iteration has no swap()
  2012. r ^= keystruct->p[16];
  2013. l ^= keystruct->p[17];
  2014. out[0] = l >> 24;
  2015. out[1] = l >> 16;
  2016. out[2] = l >> 8;
  2017. out[3] = l;
  2018. out[4] = r >> 24;
  2019. out[5] = r >> 16;
  2020. out[6] = r >> 8;
  2021. out[7] = r;
  2022. }
  2023. void blowfish_decrypt(const BYTE in[], BYTE out[], const _BLOWFISH_KEY *keystruct)
  2024. {
  2025. UINT l,r,t; //,i;
  2026. l = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | (in[3]);
  2027. r = (in[4] << 24) | (in[5] << 16) | (in[6] << 8) | (in[7]);
  2028. ITERATION(l,r,t,17);
  2029. ITERATION(l,r,t,16);
  2030. ITERATION(l,r,t,15);
  2031. ITERATION(l,r,t,14);
  2032. ITERATION(l,r,t,13);
  2033. ITERATION(l,r,t,12);
  2034. ITERATION(l,r,t,11);
  2035. ITERATION(l,r,t,10);
  2036. ITERATION(l,r,t,9);
  2037. ITERATION(l,r,t,8);
  2038. ITERATION(l,r,t,7);
  2039. ITERATION(l,r,t,6);
  2040. ITERATION(l,r,t,5);
  2041. ITERATION(l,r,t,4);
  2042. ITERATION(l,r,t,3);
  2043. l ^= keystruct->p[2]; BF(l,t); r^= t; //Last iteration has no swap()
  2044. r ^= keystruct->p[1];
  2045. l ^= keystruct->p[0];
  2046. out[0] = l >> 24;
  2047. out[1] = l >> 16;
  2048. out[2] = l >> 8;
  2049. out[3] = l;
  2050. out[4] = r >> 24;
  2051. out[5] = r >> 16;
  2052. out[6] = r >> 8;
  2053. out[7] = r;
  2054. }
  2055. void blowfish_key_setup(const BYTE user_key[], _BLOWFISH_KEY *keystruct, size_t len)
  2056. {
  2057. BYTE block[8];
  2058. int idx,idx2;
  2059. // Copy over the constant init array vals (so the originals aren't destroyed).
  2060. memcpy(keystruct->p,p_perm,sizeof(UINT) * 18);
  2061. memcpy(keystruct->s,s_perm,sizeof(UINT) * 1024);
  2062. // Combine the key with the P box. Assume key is standard 448 bits (56 bytes) or less.
  2063. for (idx = 0, idx2 = 0; idx < 18; ++idx, idx2 += 4)
  2064. keystruct->p[idx] ^= (user_key[idx2 % len] << 24) | (user_key[(idx2+1) % len] << 16)
  2065. | (user_key[(idx2+2) % len] << 8) | (user_key[(idx2+3) % len]);
  2066. // Re-calculate the P box.
  2067. memset(block, 0, 8);
  2068. for (idx = 0; idx < 18; idx += 2) {
  2069. blowfish_encrypt(block,block,keystruct);
  2070. keystruct->p[idx] = (block[0] << 24) | (block[1] << 16) | (block[2] << 8) | block[3];
  2071. keystruct->p[idx+1]=(block[4] << 24) | (block[5] << 16) | (block[6] << 8) | block[7];
  2072. }
  2073. // Recalculate the S-boxes.
  2074. for (idx = 0; idx < 4; ++idx) {
  2075. for (idx2 = 0; idx2 < 256; idx2 += 2) {
  2076. blowfish_encrypt(block,block,keystruct);
  2077. keystruct->s[idx][idx2] = (block[0] << 24) | (block[1] << 16) |
  2078. (block[2] << 8) | block[3];
  2079. keystruct->s[idx][idx2+1] = (block[4] << 24) | (block[5] << 16) |
  2080. (block[6] << 8) | block[7];
  2081. }
  2082. }
  2083. }
  2084. // -------------------------------------------------- ROT-13 -------------------------------------------------- //
  2085. /*********************** FUNCTION DEFINITIONS ***********************/
  2086. void rot13(char str[])
  2087. {
  2088. int case_type, idx, len;
  2089. for (idx = 0, len = (int)strlen(str); idx < len; idx++) {
  2090. // Only process alphabetic characters.
  2091. if (str[idx] < 'A' || (str[idx] > 'Z' && str[idx] < 'a') || str[idx] > 'z')
  2092. continue;
  2093. // Determine if the char is upper or lower case.
  2094. if (str[idx] >= 'a')
  2095. case_type = 'a';
  2096. else
  2097. case_type = 'A';
  2098. // Rotate the char's value, ensuring it doesn't accidentally "fall off" the end.
  2099. str[idx] = (str[idx] + 13) % (case_type + 26);
  2100. if (str[idx] < 26)
  2101. str[idx] += case_type;
  2102. }
  2103. }