Crypto.cpp 89 KB

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