lua-crypt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. #define LUA_LIB
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <time.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #define PADDING_MODE_ISO7816_4 0
  10. #define PADDING_MODE_PKCS7 1
  11. #define PADDING_MODE_COUNT 2
  12. #define SMALL_CHUNK 256
  13. /* the eight DES S-boxes */
  14. static uint32_t SB1[64] = {
  15. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  16. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  17. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  18. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  19. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  20. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  21. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  22. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  23. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  24. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  25. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  26. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  27. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  28. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  29. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  30. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  31. };
  32. static uint32_t SB2[64] = {
  33. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  34. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  35. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  36. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  37. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  38. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  39. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  40. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  41. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  42. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  43. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  44. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  45. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  46. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  47. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  48. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  49. };
  50. static uint32_t SB3[64] = {
  51. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  52. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  53. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  54. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  55. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  56. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  57. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  58. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  59. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  60. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  61. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  62. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  63. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  64. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  65. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  66. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  67. };
  68. static uint32_t SB4[64] = {
  69. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  70. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  71. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  72. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  73. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  74. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  75. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  76. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  77. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  78. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  79. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  80. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  81. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  82. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  83. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  84. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  85. };
  86. static uint32_t SB5[64] = {
  87. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  88. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  89. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  90. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  91. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  92. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  93. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  94. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  95. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  96. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  97. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  98. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  99. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  100. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  101. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  102. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  103. };
  104. static uint32_t SB6[64] = {
  105. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  106. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  107. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  108. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  109. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  110. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  111. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  112. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  113. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  114. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  115. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  116. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  117. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  118. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  119. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  120. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  121. };
  122. static uint32_t SB7[64] = {
  123. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  124. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  125. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  126. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  127. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  128. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  129. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  130. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  131. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  132. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  133. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  134. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  135. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  136. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  137. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  138. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  139. };
  140. static uint32_t SB8[64] = {
  141. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  142. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  143. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  144. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  145. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  146. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  147. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  148. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  149. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  150. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  151. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  152. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  153. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  154. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  155. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  156. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  157. };
  158. /* PC1: left and right halves bit-swap */
  159. static uint32_t LHs[16] = {
  160. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  161. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  162. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  163. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  164. };
  165. static uint32_t RHs[16] = {
  166. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  167. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  168. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  169. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  170. };
  171. /* platform-independant 32-bit integer manipulation macros */
  172. #define GET_UINT32(n,b,i) \
  173. { \
  174. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  175. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  176. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  177. | ( (uint32_t) (b)[(i) + 3] ); \
  178. }
  179. #define PUT_UINT32(n,b,i) \
  180. { \
  181. (b)[(i) ] = (uint8_t) ( (n) >> 24 ); \
  182. (b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \
  183. (b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \
  184. (b)[(i) + 3] = (uint8_t) ( (n) ); \
  185. }
  186. /* Initial Permutation macro */
  187. #define DES_IP(X,Y) \
  188. { \
  189. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  190. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  191. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  192. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  193. Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
  194. T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
  195. X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
  196. }
  197. /* Final Permutation macro */
  198. #define DES_FP(X,Y) \
  199. { \
  200. X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
  201. T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
  202. Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
  203. T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
  204. T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
  205. T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
  206. T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
  207. }
  208. /* DES round macro */
  209. #define DES_ROUND(X,Y) \
  210. { \
  211. T = *SK++ ^ X; \
  212. Y ^= SB8[ (T ) & 0x3F ] ^ \
  213. SB6[ (T >> 8) & 0x3F ] ^ \
  214. SB4[ (T >> 16) & 0x3F ] ^ \
  215. SB2[ (T >> 24) & 0x3F ]; \
  216. \
  217. T = *SK++ ^ ((X << 28) | (X >> 4)); \
  218. Y ^= SB7[ (T ) & 0x3F ] ^ \
  219. SB5[ (T >> 8) & 0x3F ] ^ \
  220. SB3[ (T >> 16) & 0x3F ] ^ \
  221. SB1[ (T >> 24) & 0x3F ]; \
  222. }
  223. /* DES key schedule */
  224. static void
  225. des_main_ks( uint32_t SK[32], const uint8_t key[8] ) {
  226. int i;
  227. uint32_t X, Y, T;
  228. GET_UINT32( X, key, 0 );
  229. GET_UINT32( Y, key, 4 );
  230. /* Permuted Choice 1 */
  231. T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
  232. T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
  233. X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
  234. | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
  235. | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  236. | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  237. Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
  238. | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
  239. | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  240. | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  241. X &= 0x0FFFFFFF;
  242. Y &= 0x0FFFFFFF;
  243. /* calculate subkeys */
  244. for( i = 0; i < 16; i++ )
  245. {
  246. if( i < 2 || i == 8 || i == 15 )
  247. {
  248. X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
  249. Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
  250. }
  251. else
  252. {
  253. X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
  254. Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
  255. }
  256. *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
  257. | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  258. | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
  259. | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
  260. | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
  261. | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
  262. | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
  263. | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
  264. | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  265. | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  266. | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  267. *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  268. | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  269. | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
  270. | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  271. | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
  272. | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
  273. | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
  274. | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
  275. | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
  276. | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
  277. | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  278. }
  279. }
  280. /* DES 64-bit block encryption/decryption */
  281. static void
  282. des_crypt( const uint32_t SK[32], const uint8_t input[8], uint8_t output[8] ) {
  283. uint32_t X, Y, T;
  284. GET_UINT32( X, input, 0 );
  285. GET_UINT32( Y, input, 4 );
  286. DES_IP( X, Y );
  287. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  288. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  289. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  290. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  291. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  292. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  293. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  294. DES_ROUND( Y, X ); DES_ROUND( X, Y );
  295. DES_FP( Y, X );
  296. PUT_UINT32( Y, output, 0 );
  297. PUT_UINT32( X, output, 4 );
  298. }
  299. static int
  300. lrandomkey(lua_State *L) {
  301. char tmp[8];
  302. int i;
  303. char x = 0;
  304. for (i=0;i<8;i++) {
  305. tmp[i] = random() & 0xff;
  306. x ^= tmp[i];
  307. }
  308. if (x==0) {
  309. tmp[0] |= 1; // avoid 0
  310. }
  311. lua_pushlstring(L, tmp, 8);
  312. return 1;
  313. }
  314. static void
  315. padding_mode_table(lua_State *L) {
  316. // see macros PADDING_MODE_ISO7816_4, etc.
  317. const char * mode[] = {
  318. "iso7816_4",
  319. "pkcs7",
  320. };
  321. int n = sizeof(mode) / sizeof(mode[0]);
  322. int i;
  323. lua_createtable(L,0,n);
  324. for (i=0;i<n;i++) {
  325. lua_pushinteger(L, i);
  326. lua_setfield(L, -2, mode[i]);
  327. }
  328. }
  329. typedef void (*padding_add)(uint8_t buf[8], int offset);
  330. typedef int (*padding_remove)(const uint8_t *last);
  331. static void
  332. padding_add_iso7816_4(uint8_t buf[8], int offset) {
  333. buf[offset] = 0x80;
  334. memset(buf+offset+1, 0, 7-offset);
  335. }
  336. static int
  337. padding_remove_iso7816_4(const uint8_t *last) {
  338. int padding = 1;
  339. int i;
  340. for (i=0;i<8;i++,last--) {
  341. if (*last == 0) {
  342. padding++;
  343. } else if (*last == 0x80) {
  344. return padding;
  345. } else {
  346. break;
  347. }
  348. }
  349. // invalid
  350. return 0;
  351. }
  352. static void
  353. padding_add_pkcs7(uint8_t buf[8], int offset) {
  354. uint8_t x = 8-offset;
  355. memset(buf+offset, x, 8-offset);
  356. }
  357. static int
  358. padding_remove_pkcs7(const uint8_t *last) {
  359. int padding = *last;
  360. int i;
  361. for (i=1;i<padding;i++) {
  362. --last;
  363. if (*last != padding)
  364. return 0; // invalid
  365. }
  366. return padding;
  367. }
  368. static padding_add padding_add_func[] = {
  369. padding_add_iso7816_4,
  370. padding_add_pkcs7,
  371. };
  372. static padding_remove padding_remove_func[] = {
  373. padding_remove_iso7816_4,
  374. padding_remove_pkcs7,
  375. };
  376. static inline void
  377. check_padding_mode(lua_State *L, int mode) {
  378. if (mode < 0 || mode >= PADDING_MODE_COUNT)
  379. luaL_error(L, "Invalid padding mode %d", mode);
  380. }
  381. static void
  382. add_padding(lua_State *L, uint8_t buf[8], const uint8_t *src, int offset, int mode) {
  383. check_padding_mode(L, mode);
  384. if (offset >= 8)
  385. luaL_error(L, "Invalid padding");
  386. memcpy(buf, src, offset);
  387. padding_add_func[mode](buf, offset);
  388. }
  389. static int
  390. remove_padding(lua_State *L, const uint8_t *last, int mode) {
  391. check_padding_mode(L, mode);
  392. return padding_remove_func[mode](last);
  393. }
  394. static void
  395. des_key(lua_State *L, uint32_t SK[32]) {
  396. size_t keysz = 0;
  397. const void * key = luaL_checklstring(L, 1, &keysz);
  398. if (keysz != 8) {
  399. luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz);
  400. }
  401. des_main_ks(SK, (const uint8_t*)key);
  402. }
  403. static int
  404. ldesencode(lua_State *L) {
  405. uint32_t SK[32];
  406. des_key(L, SK);
  407. size_t textsz = 0;
  408. const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 2, &textsz);
  409. size_t chunksz = (textsz + 8) & ~7;
  410. int padding_mode = luaL_optinteger(L, 3, PADDING_MODE_ISO7816_4);
  411. uint8_t tmp[SMALL_CHUNK];
  412. uint8_t *buffer = tmp;
  413. if (chunksz > SMALL_CHUNK) {
  414. buffer = (uint8_t*)lua_newuserdatauv(L, chunksz, 0);
  415. }
  416. int i;
  417. for (i=0;i<(int)textsz-7;i+=8) {
  418. des_crypt(SK, text+i, buffer+i);
  419. }
  420. uint8_t tail[8];
  421. add_padding(L, tail, text+i, textsz - i, padding_mode);
  422. des_crypt(SK, tail, buffer+i);
  423. lua_pushlstring(L, (const char *)buffer, chunksz);
  424. return 1;
  425. }
  426. static int
  427. ldesdecode(lua_State *L) {
  428. uint32_t ESK[32];
  429. des_key(L, ESK);
  430. uint32_t SK[32];
  431. int i;
  432. for( i = 0; i < 32; i += 2 ) {
  433. SK[i] = ESK[30 - i];
  434. SK[i + 1] = ESK[31 - i];
  435. }
  436. size_t textsz = 0;
  437. const uint8_t *text = (const uint8_t *)luaL_checklstring(L, 2, &textsz);
  438. if ((textsz & 7) || textsz == 0) {
  439. return luaL_error(L, "Invalid des crypt text length %d", (int)textsz);
  440. }
  441. int padding_mode = luaL_optinteger(L, 3, PADDING_MODE_ISO7816_4);
  442. uint8_t tmp[SMALL_CHUNK];
  443. uint8_t *buffer = tmp;
  444. if (textsz > SMALL_CHUNK) {
  445. buffer = (uint8_t*)lua_newuserdatauv(L, textsz, 0);
  446. }
  447. for (i=0;i<textsz;i+=8) {
  448. des_crypt(SK, text+i, buffer+i);
  449. }
  450. int padding = remove_padding(L, buffer + textsz - 1, padding_mode);
  451. if (padding <= 0 || padding > 8) {
  452. return luaL_error(L, "Invalid des crypt text");
  453. }
  454. lua_pushlstring(L, (const char *)buffer, textsz - padding);
  455. return 1;
  456. }
  457. static void
  458. Hash(const char * str, int sz, uint8_t key[8]) {
  459. uint32_t djb_hash = 5381L;
  460. uint32_t js_hash = 1315423911L;
  461. int i;
  462. for (i=0;i<sz;i++) {
  463. uint8_t c = (uint8_t)str[i];
  464. djb_hash += (djb_hash << 5) + c;
  465. js_hash ^= ((js_hash << 5) + c + (js_hash >> 2));
  466. }
  467. key[0] = djb_hash & 0xff;
  468. key[1] = (djb_hash >> 8) & 0xff;
  469. key[2] = (djb_hash >> 16) & 0xff;
  470. key[3] = (djb_hash >> 24) & 0xff;
  471. key[4] = js_hash & 0xff;
  472. key[5] = (js_hash >> 8) & 0xff;
  473. key[6] = (js_hash >> 16) & 0xff;
  474. key[7] = (js_hash >> 24) & 0xff;
  475. }
  476. static int
  477. lhashkey(lua_State *L) {
  478. size_t sz = 0;
  479. const char * key = luaL_checklstring(L, 1, &sz);
  480. uint8_t realkey[8];
  481. Hash(key,(int)sz,realkey);
  482. lua_pushlstring(L, (const char *)realkey, 8);
  483. return 1;
  484. }
  485. static int
  486. ltohex(lua_State *L) {
  487. static char hex[] = "0123456789abcdef";
  488. size_t sz = 0;
  489. const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz);
  490. char tmp[SMALL_CHUNK];
  491. char *buffer = tmp;
  492. if (sz > SMALL_CHUNK/2) {
  493. buffer = (char*)lua_newuserdatauv(L, sz * 2, 0);
  494. }
  495. int i;
  496. for (i=0;i<sz;i++) {
  497. buffer[i*2] = hex[text[i] >> 4];
  498. buffer[i*2+1] = hex[text[i] & 0xf];
  499. }
  500. lua_pushlstring(L, buffer, sz * 2);
  501. return 1;
  502. }
  503. #define HEX(v,c) { char tmp = (char) c; if (tmp >= '0' && tmp <= '9') { v = tmp-'0'; } else { v = tmp - 'a' + 10; } }
  504. static int
  505. lfromhex(lua_State *L) {
  506. size_t sz = 0;
  507. const char * text = luaL_checklstring(L, 1, &sz);
  508. if (sz & 1) {
  509. return luaL_error(L, "Invalid hex text size %d", (int)sz);
  510. }
  511. char tmp[SMALL_CHUNK];
  512. char *buffer = tmp;
  513. if (sz > SMALL_CHUNK*2) {
  514. buffer = (char*)lua_newuserdatauv(L, sz / 2, 0);
  515. }
  516. int i;
  517. for (i=0;i<sz;i+=2) {
  518. uint8_t hi,low;
  519. HEX(hi, text[i]);
  520. HEX(low, text[i+1]);
  521. if (hi > 16 || low > 16) {
  522. return luaL_error(L, "Invalid hex text", text);
  523. }
  524. buffer[i/2] = hi<<4 | low;
  525. }
  526. lua_pushlstring(L, buffer, i/2);
  527. return 1;
  528. }
  529. // Constants are the integer part of the sines of integers (in radians) * 2^32.
  530. static const uint32_t k[64] = {
  531. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
  532. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
  533. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
  534. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
  535. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
  536. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
  537. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
  538. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
  539. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
  540. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
  541. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
  542. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
  543. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
  544. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
  545. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
  546. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
  547. // r specifies the per-round shift amounts
  548. static const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  549. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  550. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  551. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
  552. // leftrotate function definition
  553. #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
  554. static void
  555. digest_md5(uint32_t w[16], uint32_t result[4]) {
  556. uint32_t a, b, c, d, f, g, temp;
  557. int i;
  558. a = 0x67452301u;
  559. b = 0xefcdab89u;
  560. c = 0x98badcfeu;
  561. d = 0x10325476u;
  562. for(i = 0; i<64; i++) {
  563. if (i < 16) {
  564. f = (b & c) | ((~b) & d);
  565. g = i;
  566. } else if (i < 32) {
  567. f = (d & b) | ((~d) & c);
  568. g = (5*i + 1) % 16;
  569. } else if (i < 48) {
  570. f = b ^ c ^ d;
  571. g = (3*i + 5) % 16;
  572. } else {
  573. f = c ^ (b | (~d));
  574. g = (7*i) % 16;
  575. }
  576. temp = d;
  577. d = c;
  578. c = b;
  579. b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
  580. a = temp;
  581. }
  582. result[0] = a;
  583. result[1] = b;
  584. result[2] = c;
  585. result[3] = d;
  586. }
  587. // hmac64 use md5 algorithm without padding, and the result is (c^d .. a^b)
  588. static void
  589. hmac(uint32_t x[2], uint32_t y[2], uint32_t result[2]) {
  590. uint32_t w[16];
  591. uint32_t r[4];
  592. int i;
  593. for (i=0;i<16;i+=4) {
  594. w[i] = x[1];
  595. w[i+1] = x[0];
  596. w[i+2] = y[1];
  597. w[i+3] = y[0];
  598. }
  599. digest_md5(w,r);
  600. result[0] = r[2]^r[3];
  601. result[1] = r[0]^r[1];
  602. }
  603. static void
  604. hmac_md5(uint32_t x[2], uint32_t y[2], uint32_t result[2]) {
  605. uint32_t w[16];
  606. uint32_t r[4];
  607. int i;
  608. for (i=0;i<12;i+=4) {
  609. w[i] = x[0];
  610. w[i+1] = x[1];
  611. w[i+2] = y[0];
  612. w[i+3] = y[1];
  613. }
  614. w[12] = 0x80;
  615. w[13] = 0;
  616. w[14] = 384;
  617. w[15] = 0;
  618. digest_md5(w,r);
  619. result[0] = (r[0] + 0x67452301u) ^ (r[2] + 0x98badcfeu);
  620. result[1] = (r[1] + 0xefcdab89u) ^ (r[3] + 0x10325476u);
  621. }
  622. static void
  623. read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) {
  624. size_t sz = 0;
  625. const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz);
  626. if (sz != 8) {
  627. luaL_error(L, "Invalid uint64 x");
  628. }
  629. const uint8_t *y = (const uint8_t *)luaL_checklstring(L, 2, &sz);
  630. if (sz != 8) {
  631. luaL_error(L, "Invalid uint64 y");
  632. }
  633. xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24;
  634. xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24;
  635. yy[0] = y[0] | y[1]<<8 | y[2]<<16 | y[3]<<24;
  636. yy[1] = y[4] | y[5]<<8 | y[6]<<16 | y[7]<<24;
  637. }
  638. static int
  639. pushqword(lua_State *L, uint32_t result[2]) {
  640. uint8_t tmp[8];
  641. tmp[0] = result[0] & 0xff;
  642. tmp[1] = (result[0] >> 8 )& 0xff;
  643. tmp[2] = (result[0] >> 16 )& 0xff;
  644. tmp[3] = (result[0] >> 24 )& 0xff;
  645. tmp[4] = result[1] & 0xff;
  646. tmp[5] = (result[1] >> 8 )& 0xff;
  647. tmp[6] = (result[1] >> 16 )& 0xff;
  648. tmp[7] = (result[1] >> 24 )& 0xff;
  649. lua_pushlstring(L, (const char *)tmp, 8);
  650. return 1;
  651. }
  652. static int
  653. lhmac64(lua_State *L) {
  654. uint32_t x[2], y[2];
  655. read64(L, x, y);
  656. uint32_t result[2];
  657. hmac(x,y,result);
  658. return pushqword(L, result);
  659. }
  660. /*
  661. h1 = crypt.hmac64_md5(a,b)
  662. m = md5.sum((a..b):rep(3))
  663. h2 = crypt.xor_str(m:sub(1,8), m:sub(9,16))
  664. assert(h1 == h2)
  665. */
  666. static int
  667. lhmac64_md5(lua_State *L) {
  668. uint32_t x[2], y[2];
  669. read64(L, x, y);
  670. uint32_t result[2];
  671. hmac_md5(x,y,result);
  672. return pushqword(L, result);
  673. }
  674. /*
  675. 8bytes key
  676. string text
  677. */
  678. static int
  679. lhmac_hash(lua_State *L) {
  680. uint32_t key[2];
  681. size_t sz = 0;
  682. const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz);
  683. if (sz != 8) {
  684. luaL_error(L, "Invalid uint64 key");
  685. }
  686. key[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24;
  687. key[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24;
  688. const char * text = luaL_checklstring(L, 2, &sz);
  689. uint8_t h[8];
  690. Hash(text,(int)sz,h);
  691. uint32_t htext[2];
  692. htext[0] = h[0] | h[1]<<8 | h[2]<<16 | h[3]<<24;
  693. htext[1] = h[4] | h[5]<<8 | h[6]<<16 | h[7]<<24;
  694. uint32_t result[2];
  695. hmac(htext,key,result);
  696. return pushqword(L, result);
  697. }
  698. // powmodp64 for DH-key exchange
  699. // The biggest 64bit prime
  700. #define P 0xffffffffffffffc5ull
  701. static inline uint64_t
  702. mul_mod_p(uint64_t a, uint64_t b) {
  703. uint64_t m = 0;
  704. while(b) {
  705. if(b&1) {
  706. uint64_t t = P-a;
  707. if ( m >= t) {
  708. m -= t;
  709. } else {
  710. m += a;
  711. }
  712. }
  713. if (a >= P - a) {
  714. a = a * 2 - P;
  715. } else {
  716. a = a * 2;
  717. }
  718. b>>=1;
  719. }
  720. return m;
  721. }
  722. static inline uint64_t
  723. pow_mod_p(uint64_t a, uint64_t b) {
  724. if (b==1) {
  725. return a;
  726. }
  727. uint64_t t = pow_mod_p(a, b>>1);
  728. t = mul_mod_p(t,t);
  729. if (b % 2) {
  730. t = mul_mod_p(t, a);
  731. }
  732. return t;
  733. }
  734. // calc a^b % p
  735. static uint64_t
  736. powmodp(uint64_t a, uint64_t b) {
  737. if (a > P)
  738. a%=P;
  739. return pow_mod_p(a,b);
  740. }
  741. static void
  742. push64(lua_State *L, uint64_t r) {
  743. uint8_t tmp[8];
  744. tmp[0] = r & 0xff;
  745. tmp[1] = (r >> 8 )& 0xff;
  746. tmp[2] = (r >> 16 )& 0xff;
  747. tmp[3] = (r >> 24 )& 0xff;
  748. tmp[4] = (r >> 32 )& 0xff;
  749. tmp[5] = (r >> 40 )& 0xff;
  750. tmp[6] = (r >> 48 )& 0xff;
  751. tmp[7] = (r >> 56 )& 0xff;
  752. lua_pushlstring(L, (const char *)tmp, 8);
  753. }
  754. static int
  755. ldhsecret(lua_State *L) {
  756. uint32_t x[2], y[2];
  757. read64(L, x, y);
  758. uint64_t xx = (uint64_t)x[0] | (uint64_t)x[1]<<32;
  759. uint64_t yy = (uint64_t)y[0] | (uint64_t)y[1]<<32;
  760. if (xx == 0 || yy == 0)
  761. return luaL_error(L, "Can't be 0");
  762. uint64_t r = powmodp(xx, yy);
  763. push64(L, r);
  764. return 1;
  765. }
  766. #define G 5
  767. static int
  768. ldhexchange(lua_State *L) {
  769. size_t sz = 0;
  770. const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz);
  771. if (sz != 8) {
  772. luaL_error(L, "Invalid dh uint64 key");
  773. }
  774. uint32_t xx[2];
  775. xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24;
  776. xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24;
  777. uint64_t x64 = (uint64_t)xx[0] | (uint64_t)xx[1]<<32;
  778. if (x64 == 0)
  779. return luaL_error(L, "Can't be 0");
  780. uint64_t r = powmodp(G, x64);
  781. push64(L, r);
  782. return 1;
  783. }
  784. // base64
  785. static int
  786. lb64encode(lua_State *L) {
  787. static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  788. size_t sz = 0;
  789. const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz);
  790. int encode_sz = (sz + 2)/3*4;
  791. char tmp[SMALL_CHUNK];
  792. char *buffer = tmp;
  793. if (encode_sz > SMALL_CHUNK) {
  794. buffer = (char*)lua_newuserdatauv(L, encode_sz, 0);
  795. }
  796. int i,j;
  797. j=0;
  798. for (i=0;i<(int)sz-2;i+=3) {
  799. uint32_t v = text[i] << 16 | text[i+1] << 8 | text[i+2];
  800. buffer[j] = encoding[v >> 18];
  801. buffer[j+1] = encoding[(v >> 12) & 0x3f];
  802. buffer[j+2] = encoding[(v >> 6) & 0x3f];
  803. buffer[j+3] = encoding[(v) & 0x3f];
  804. j+=4;
  805. }
  806. int padding = sz-i;
  807. uint32_t v;
  808. switch(padding) {
  809. case 1 :
  810. v = text[i];
  811. buffer[j] = encoding[v >> 2];
  812. buffer[j+1] = encoding[(v & 3) << 4];
  813. buffer[j+2] = '=';
  814. buffer[j+3] = '=';
  815. break;
  816. case 2 :
  817. v = text[i] << 8 | text[i+1];
  818. buffer[j] = encoding[v >> 10];
  819. buffer[j+1] = encoding[(v >> 4) & 0x3f];
  820. buffer[j+2] = encoding[(v & 0xf) << 2];
  821. buffer[j+3] = '=';
  822. break;
  823. }
  824. lua_pushlstring(L, buffer, encode_sz);
  825. return 1;
  826. }
  827. static inline int
  828. b64index(uint8_t c) {
  829. static const int decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
  830. int decoding_size = sizeof(decoding)/sizeof(decoding[0]);
  831. if (c<43) {
  832. return -1;
  833. }
  834. c -= 43;
  835. if (c>=decoding_size)
  836. return -1;
  837. return decoding[c];
  838. }
  839. static int
  840. lb64decode(lua_State *L) {
  841. size_t sz = 0;
  842. const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz);
  843. int decode_sz = (sz+3)/4*3;
  844. char tmp[SMALL_CHUNK];
  845. char *buffer = tmp;
  846. if (decode_sz > SMALL_CHUNK) {
  847. buffer = (char*)lua_newuserdatauv(L, decode_sz, 0);
  848. }
  849. int i,j;
  850. int output = 0;
  851. for (i=0;i<sz;) {
  852. int padding = 0;
  853. int c[4];
  854. for (j=0;j<4;) {
  855. if (i>=sz && 4>j){
  856. /*To improve compatibility, there may not be enough equal signs */
  857. c[j] = -2;
  858. }else{
  859. c[j] = b64index(text[i]);
  860. }
  861. if (c[j] == -1) {
  862. ++i;
  863. continue;
  864. }
  865. if (c[j] == -2) {
  866. ++padding;
  867. }
  868. ++i;
  869. ++j;
  870. }
  871. uint32_t v;
  872. switch (padding) {
  873. case 0:
  874. v = (unsigned)c[0] << 18 | c[1] << 12 | c[2] << 6 | c[3];
  875. buffer[output] = v >> 16;
  876. buffer[output+1] = (v >> 8) & 0xff;
  877. buffer[output+2] = v & 0xff;
  878. output += 3;
  879. break;
  880. case 1:
  881. if (c[3] != -2 || (c[2] & 3)!=0) {
  882. return luaL_error(L, "Invalid base64 text");
  883. }
  884. v = (unsigned)c[0] << 10 | c[1] << 4 | c[2] >> 2 ;
  885. buffer[output] = v >> 8;
  886. buffer[output+1] = v & 0xff;
  887. output += 2;
  888. break;
  889. case 2:
  890. if (c[3] != -2 || c[2] != -2 || (c[1] & 0xf) !=0) {
  891. return luaL_error(L, "Invalid base64 text");
  892. }
  893. v = (unsigned)c[0] << 2 | c[1] >> 4;
  894. buffer[output] = v;
  895. ++ output;
  896. break;
  897. default:
  898. return luaL_error(L, "Invalid base64 text");
  899. }
  900. }
  901. lua_pushlstring(L, buffer, output);
  902. return 1;
  903. }
  904. static int
  905. lxor_str(lua_State *L) {
  906. size_t len1,len2;
  907. const char *s1 = luaL_checklstring(L,1,&len1);
  908. const char *s2 = luaL_checklstring(L,2,&len2);
  909. if (len2 == 0) {
  910. return luaL_error(L, "Can't xor empty string");
  911. }
  912. luaL_Buffer b;
  913. char * buffer = luaL_buffinitsize(L, &b, len1);
  914. int i;
  915. for (i=0;i<len1;i++) {
  916. buffer[i] = s1[i] ^ s2[i % len2];
  917. }
  918. luaL_addsize(&b, len1);
  919. luaL_pushresult(&b);
  920. return 1;
  921. }
  922. // defined in lsha1.c
  923. int lsha1(lua_State *L);
  924. int lhmac_sha1(lua_State *L);
  925. LUAMOD_API int
  926. luaopen_skynet_crypt(lua_State *L) {
  927. luaL_checkversion(L);
  928. static int init = 0;
  929. if (!init) {
  930. // Don't need call srandom more than once.
  931. init = 1 ;
  932. srandom((random() << 8) ^ (time(NULL) << 16) ^ getpid());
  933. }
  934. luaL_Reg l[] = {
  935. { "hashkey", lhashkey },
  936. { "randomkey", lrandomkey },
  937. { "desencode", ldesencode },
  938. { "desdecode", ldesdecode },
  939. { "hexencode", ltohex },
  940. { "hexdecode", lfromhex },
  941. { "hmac64", lhmac64 },
  942. { "hmac64_md5", lhmac64_md5 },
  943. { "dhexchange", ldhexchange },
  944. { "dhsecret", ldhsecret },
  945. { "base64encode", lb64encode },
  946. { "base64decode", lb64decode },
  947. { "sha1", lsha1 },
  948. { "hmac_sha1", lhmac_sha1 },
  949. { "hmac_hash", lhmac_hash },
  950. { "xor_str", lxor_str },
  951. { "padding", NULL },
  952. { NULL, NULL },
  953. };
  954. luaL_newlib(L,l);
  955. padding_mode_table(L);
  956. lua_setfield(L, -2, "padding");
  957. return 1;
  958. }
  959. LUAMOD_API int
  960. luaopen_client_crypt(lua_State *L) {
  961. return luaopen_skynet_crypt(L);
  962. }