420-md5lib-embedded.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. diff -Nur lua-5.1.4.orig/src/Makefile lua-5.1.4/src/Makefile
  2. --- lua-5.1.4.orig/src/Makefile 2009-04-27 00:36:06.000000000 +0200
  3. +++ lua-5.1.4/src/Makefile 2009-04-27 00:47:54.000000000 +0200
  4. @@ -29,7 +29,7 @@
  5. lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
  6. lundump.o lvm.o lzio.o lnum.o
  7. LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
  8. - lstrlib.o loadlib.o linit.o lposix.o lbitlib.o
  9. + lstrlib.o loadlib.o linit.o lposix.o lbitlib.o md5.o md5lib.o
  10. LUA_T= lua
  11. LUA_O= lua.o
  12. diff -Nur lua-5.1.4.orig/src/linit.c lua-5.1.4/src/linit.c
  13. --- lua-5.1.4.orig/src/linit.c 2009-04-27 00:36:06.000000000 +0200
  14. +++ lua-5.1.4/src/linit.c 2009-04-27 00:44:54.000000000 +0200
  15. @@ -25,6 +25,7 @@
  16. {LUA_DBLIBNAME, luaopen_debug},
  17. {LUA_POSIXLIBNAME, luaopen_posix},
  18. {LUA_BITLIBNAME, luaopen_bit},
  19. + {LUA_MD5LIBNAME, luaopen_md5_core},
  20. {NULL, NULL}
  21. };
  22. diff -Nur lua-5.1.4.orig/src/lualib.h lua-5.1.4/src/lualib.h
  23. --- lua-5.1.4.orig/src/lualib.h 2009-04-27 00:36:06.000000000 +0200
  24. +++ lua-5.1.4/src/lualib.h 2009-04-27 00:46:01.000000000 +0200
  25. @@ -45,6 +45,8 @@
  26. #define LUA_BITLIBNAME "bit"
  27. LUALIB_API int (luaopen_bit) (lua_State *L);
  28. +#define LUA_MD5LIBNAME "md5"
  29. +LUALIB_API int (luaopen_md5_core) (lua_State *L);
  30. /* open all previous libraries */
  31. LUALIB_API void (luaL_openlibs) (lua_State *L);
  32. diff -Nur lua-5.1.4.orig/src/md5.c lua-5.1.4/src/md5.c
  33. --- lua-5.1.4.orig/src/md5.c 1970-01-01 01:00:00.000000000 +0100
  34. +++ lua-5.1.4/src/md5.c 2008-03-24 21:59:12.000000000 +0100
  35. @@ -0,0 +1,214 @@
  36. +/**
  37. +* $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $
  38. +* Hash function MD5
  39. +* @author Marcela Ozorio Suarez, Roberto I.
  40. +*/
  41. +
  42. +
  43. +#include <string.h>
  44. +
  45. +#include "md5.h"
  46. +
  47. +
  48. +#define WORD 32
  49. +#define MASK 0xFFFFFFFF
  50. +#if __STDC_VERSION__ >= 199901L
  51. +#include <stdint.h>
  52. +typedef uint32_t WORD32;
  53. +#else
  54. +typedef unsigned int WORD32;
  55. +#endif
  56. +
  57. +
  58. +/**
  59. +* md5 hash function.
  60. +* @param message: aribtary string.
  61. +* @param len: message length.
  62. +* @param output: buffer to receive the hash value. Its size must be
  63. +* (at least) HASHSIZE.
  64. +*/
  65. +void md5 (const char *message, long len, char *output);
  66. +
  67. +
  68. +
  69. +/*
  70. +** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32.
  71. +** Os bits sao deslocados de 'num' posicoes
  72. +*/
  73. +#define rotate(D, num) (D<<num) | (D>>(WORD-num))
  74. +
  75. +/*Macros que definem operacoes relizadas pelo algoritmo md5 */
  76. +#define F(x, y, z) (((x) & (y)) | ((~(x)) & (z)))
  77. +#define G(x, y, z) (((x) & (z)) | ((y) & (~(z))))
  78. +#define H(x, y, z) ((x) ^ (y) ^ (z))
  79. +#define I(x, y, z) ((y) ^ ((x) | (~(z))))
  80. +
  81. +
  82. +/*vetor de numeros utilizados pelo algoritmo md5 para embaralhar bits */
  83. +static const WORD32 T[64]={
  84. + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  85. + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  86. + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  87. + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  88. + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  89. + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  90. + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  91. + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  92. + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  93. + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  94. + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  95. + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  96. + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  97. + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  98. + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  99. + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  100. +};
  101. +
  102. +
  103. +static void word32tobytes (const WORD32 *input, char *output) {
  104. + int j = 0;
  105. + while (j<4*4) {
  106. + WORD32 v = *input++;
  107. + output[j++] = (char)(v & 0xff); v >>= 8;
  108. + output[j++] = (char)(v & 0xff); v >>= 8;
  109. + output[j++] = (char)(v & 0xff); v >>= 8;
  110. + output[j++] = (char)(v & 0xff);
  111. + }
  112. +}
  113. +
  114. +
  115. +static void inic_digest(WORD32 *d) {
  116. + d[0] = 0x67452301;
  117. + d[1] = 0xEFCDAB89;
  118. + d[2] = 0x98BADCFE;
  119. + d[3] = 0x10325476;
  120. +}
  121. +
  122. +
  123. +/*funcao que implemeta os quatro passos principais do algoritmo MD5 */
  124. +static void digest(const WORD32 *m, WORD32 *d) {
  125. + int j;
  126. + /*MD5 PASSO1 */
  127. + for (j=0; j<4*4; j+=4) {
  128. + d[0] = d[0]+ F(d[1], d[2], d[3])+ m[j] + T[j]; d[0]=rotate(d[0], 7);
  129. + d[0]+=d[1];
  130. + d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12);
  131. + d[3]+=d[0];
  132. + d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17);
  133. + d[2]+=d[3];
  134. + d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22);
  135. + d[1]+=d[2];
  136. + }
  137. + /*MD5 PASSO2 */
  138. + for (j=0; j<4*4; j+=4) {
  139. + d[0] = d[0]+ G(d[1], d[2], d[3])+ m[(5*j+1)&0x0f] + T[(j-1)+17];
  140. + d[0] = rotate(d[0],5);
  141. + d[0]+=d[1];
  142. + d[3] = d[3]+ G(d[0], d[1], d[2])+ m[((5*(j+1)+1)&0x0f)] + T[(j+0)+17];
  143. + d[3] = rotate(d[3], 9);
  144. + d[3]+=d[0];
  145. + d[2] = d[2]+ G(d[3], d[0], d[1])+ m[((5*(j+2)+1)&0x0f)] + T[(j+1)+17];
  146. + d[2] = rotate(d[2], 14);
  147. + d[2]+=d[3];
  148. + d[1] = d[1]+ G(d[2], d[3], d[0])+ m[((5*(j+3)+1)&0x0f)] + T[(j+2)+17];
  149. + d[1] = rotate(d[1], 20);
  150. + d[1]+=d[2];
  151. + }
  152. + /*MD5 PASSO3 */
  153. + for (j=0; j<4*4; j+=4) {
  154. + d[0] = d[0]+ H(d[1], d[2], d[3])+ m[(3*j+5)&0x0f] + T[(j-1)+33];
  155. + d[0] = rotate(d[0], 4);
  156. + d[0]+=d[1];
  157. + d[3] = d[3]+ H(d[0], d[1], d[2])+ m[(3*(j+1)+5)&0x0f] + T[(j+0)+33];
  158. + d[3] = rotate(d[3], 11);
  159. + d[3]+=d[0];
  160. + d[2] = d[2]+ H(d[3], d[0], d[1])+ m[(3*(j+2)+5)&0x0f] + T[(j+1)+33];
  161. + d[2] = rotate(d[2], 16);
  162. + d[2]+=d[3];
  163. + d[1] = d[1]+ H(d[2], d[3], d[0])+ m[(3*(j+3)+5)&0x0f] + T[(j+2)+33];
  164. + d[1] = rotate(d[1], 23);
  165. + d[1]+=d[2];
  166. + }
  167. + /*MD5 PASSO4 */
  168. + for (j=0; j<4*4; j+=4) {
  169. + d[0] = d[0]+ I(d[1], d[2], d[3])+ m[(7*j)&0x0f] + T[(j-1)+49];
  170. + d[0] = rotate(d[0], 6);
  171. + d[0]+=d[1];
  172. + d[3] = d[3]+ I(d[0], d[1], d[2])+ m[(7*(j+1))&0x0f] + T[(j+0)+49];
  173. + d[3] = rotate(d[3], 10);
  174. + d[3]+=d[0];
  175. + d[2] = d[2]+ I(d[3], d[0], d[1])+ m[(7*(j+2))&0x0f] + T[(j+1)+49];
  176. + d[2] = rotate(d[2], 15);
  177. + d[2]+=d[3];
  178. + d[1] = d[1]+ I(d[2], d[3], d[0])+ m[(7*(j+3))&0x0f] + T[(j+2)+49];
  179. + d[1] = rotate(d[1], 21);
  180. + d[1]+=d[2];
  181. + }
  182. +}
  183. +
  184. +
  185. +static void bytestoword32 (WORD32 *x, const char *pt) {
  186. + int i;
  187. + for (i=0; i<16; i++) {
  188. + int j=i*4;
  189. + x[i] = (((WORD32)(unsigned char)pt[j+3] << 8 |
  190. + (WORD32)(unsigned char)pt[j+2]) << 8 |
  191. + (WORD32)(unsigned char)pt[j+1]) << 8 |
  192. + (WORD32)(unsigned char)pt[j];
  193. + }
  194. +
  195. +}
  196. +
  197. +
  198. +static void put_length(WORD32 *x, long len) {
  199. + /* in bits! */
  200. + x[14] = (WORD32)((len<<3) & MASK);
  201. + x[15] = (WORD32)(len>>(32-3) & 0x7);
  202. +}
  203. +
  204. +
  205. +/*
  206. +** returned status:
  207. +* 0 - normal message (full 64 bytes)
  208. +* 1 - enough room for 0x80, but not for message length (two 4-byte words)
  209. +* 2 - enough room for 0x80 plus message length (at least 9 bytes free)
  210. +*/
  211. +static int converte (WORD32 *x, const char *pt, int num, int old_status) {
  212. + int new_status = 0;
  213. + char buff[64];
  214. + if (num<64) {
  215. + memcpy(buff, pt, num); /* to avoid changing original string */
  216. + memset(buff+num, 0, 64-num);
  217. + if (old_status == 0)
  218. + buff[num] = '\200';
  219. + new_status = 1;
  220. + pt = buff;
  221. + }
  222. + bytestoword32(x, pt);
  223. + if (num <= (64 - 9))
  224. + new_status = 2;
  225. + return new_status;
  226. +}
  227. +
  228. +
  229. +
  230. +void md5 (const char *message, long len, char *output) {
  231. + WORD32 d[4];
  232. + int status = 0;
  233. + long i = 0;
  234. + inic_digest(d);
  235. + while (status != 2) {
  236. + WORD32 d_old[4];
  237. + WORD32 wbuff[16];
  238. + int numbytes = (len-i >= 64) ? 64 : len-i;
  239. + /*salva os valores do vetor digest*/
  240. + d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3];
  241. + status = converte(wbuff, message+i, numbytes, status);
  242. + if (status == 2) put_length(wbuff, len);
  243. + digest(wbuff, d);
  244. + d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3];
  245. + i += numbytes;
  246. + }
  247. + word32tobytes(d, output);
  248. +}
  249. +
  250. diff -Nur lua-5.1.4.orig/src/md5.h lua-5.1.4/src/md5.h
  251. --- lua-5.1.4.orig/src/md5.h 1970-01-01 01:00:00.000000000 +0100
  252. +++ lua-5.1.4/src/md5.h 2009-04-27 00:49:13.000000000 +0200
  253. @@ -0,0 +1,20 @@
  254. +/**
  255. +* $Id: md5.h,v 1.2 2006/03/03 15:04:49 tomas Exp $
  256. +* Cryptographic module for Lua.
  257. +* @author Roberto Ierusalimschy
  258. +*/
  259. +
  260. +
  261. +#ifndef md5_h
  262. +#define md5_h
  263. +
  264. +#include "lua.h"
  265. +
  266. +
  267. +#define HASHSIZE 16
  268. +
  269. +void md5 (const char *message, long len, char *output);
  270. +int luaopen_md5_core (lua_State *L);
  271. +
  272. +
  273. +#endif
  274. diff -Nur lua-5.1.4.orig/src/md5lib.c lua-5.1.4/src/md5lib.c
  275. --- lua-5.1.4.orig/src/md5lib.c 1970-01-01 01:00:00.000000000 +0100
  276. +++ lua-5.1.4/src/md5lib.c 2009-04-27 00:49:55.000000000 +0200
  277. @@ -0,0 +1,203 @@
  278. +/**
  279. +* $Id: md5lib.c,v 1.10 2008/05/12 20:51:27 carregal Exp $
  280. +* Cryptographic and Hash functions for Lua
  281. +* @author Roberto Ierusalimschy
  282. +*/
  283. +
  284. +
  285. +#include <stdlib.h>
  286. +#include <string.h>
  287. +#include <time.h>
  288. +
  289. +#include "lua.h"
  290. +#include "lauxlib.h"
  291. +
  292. +#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
  293. +#include "compat-5.1.h"
  294. +#endif
  295. +
  296. +#include "md5.h"
  297. +
  298. +
  299. +/**
  300. +* Hash function. Returns a hash for a given string.
  301. +* @param message: arbitrary binary string.
  302. +* @return A 128-bit hash string.
  303. +*/
  304. +static int lmd5 (lua_State *L) {
  305. + char buff[16];
  306. + size_t l;
  307. + const char *message = luaL_checklstring(L, 1, &l);
  308. + md5(message, l, buff);
  309. + lua_pushlstring(L, buff, 16L);
  310. + return 1;
  311. +}
  312. +
  313. +
  314. +/**
  315. +* X-Or. Does a bit-a-bit exclusive-or of two strings.
  316. +* @param s1: arbitrary binary string.
  317. +* @param s2: arbitrary binary string with same length as s1.
  318. +* @return a binary string with same length as s1 and s2,
  319. +* where each bit is the exclusive-or of the corresponding bits in s1-s2.
  320. +*/
  321. +static int ex_or (lua_State *L) {
  322. + size_t l1, l2;
  323. + const char *s1 = luaL_checklstring(L, 1, &l1);
  324. + const char *s2 = luaL_checklstring(L, 2, &l2);
  325. + luaL_Buffer b;
  326. + luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" );
  327. + luaL_buffinit(L, &b);
  328. + while (l1--) luaL_putchar(&b, (*s1++)^(*s2++));
  329. + luaL_pushresult(&b);
  330. + return 1;
  331. +}
  332. +
  333. +
  334. +static void checkseed (lua_State *L) {
  335. + if (lua_isnone(L, 3)) { /* no seed? */
  336. + time_t tm = time(NULL); /* for `random' seed */
  337. + lua_pushlstring(L, (char *)&tm, sizeof(tm));
  338. + }
  339. +}
  340. +
  341. +
  342. +#define MAXKEY 256
  343. +#define BLOCKSIZE 16
  344. +
  345. +
  346. +
  347. +static int initblock (lua_State *L, const char *seed, int lseed, char *block) {
  348. + size_t lkey;
  349. + const char *key = luaL_checklstring(L, 2, &lkey);
  350. + if (lkey > MAXKEY)
  351. + luaL_error(L, "key too long (> %d)", MAXKEY);
  352. + memset(block, 0, BLOCKSIZE);
  353. + memcpy(block, seed, lseed);
  354. + memcpy(block+BLOCKSIZE, key, lkey);
  355. + return (int)lkey+BLOCKSIZE;
  356. +}
  357. +
  358. +
  359. +static void codestream (lua_State *L, const char *msg, size_t lmsg,
  360. + char *block, int lblock) {
  361. + luaL_Buffer b;
  362. + luaL_buffinit(L, &b);
  363. + while (lmsg > 0) {
  364. + char code[BLOCKSIZE];
  365. + int i;
  366. + md5(block, lblock, code);
  367. + for (i=0; i<BLOCKSIZE && lmsg > 0; i++, lmsg--)
  368. + code[i] ^= *msg++;
  369. + luaL_addlstring(&b, code, i);
  370. + memcpy(block, code, i); /* update seed */
  371. + }
  372. + luaL_pushresult(&b);
  373. +}
  374. +
  375. +
  376. +static void decodestream (lua_State *L, const char *cypher, size_t lcypher,
  377. + char *block, int lblock) {
  378. + luaL_Buffer b;
  379. + luaL_buffinit(L, &b);
  380. + while (lcypher > 0) {
  381. + char code[BLOCKSIZE];
  382. + int i;
  383. + md5(block, lblock, code); /* update seed */
  384. + for (i=0; i<BLOCKSIZE && lcypher > 0; i++, lcypher--)
  385. + code[i] ^= *cypher++;
  386. + luaL_addlstring(&b, code, i);
  387. + memcpy(block, cypher-i, i);
  388. + }
  389. + luaL_pushresult(&b);
  390. +}
  391. +
  392. +
  393. +/**
  394. +* Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback
  395. +* mode).
  396. +* @param message: arbitrary binary string to be encrypted.
  397. +* @param key: arbitrary binary string to be used as a key.
  398. +* @param [seed]: optional arbitrary binary string to be used as a seed.
  399. +* if no seed is provided, the function uses the result of
  400. +* <code>time()</code> as a seed.
  401. +* @return The cyphertext (as a binary string).
  402. +*/
  403. +static int crypt (lua_State *L) {
  404. + size_t lmsg;
  405. + const char *msg = luaL_checklstring(L, 1, &lmsg);
  406. + size_t lseed;
  407. + const char *seed;
  408. + int lblock;
  409. + char block[BLOCKSIZE+MAXKEY];
  410. + checkseed(L);
  411. + seed = luaL_checklstring(L, 3, &lseed);
  412. + if (lseed > BLOCKSIZE)
  413. + luaL_error(L, "seed too long (> %d)", BLOCKSIZE);
  414. + /* put seed and seed length at the beginning of result */
  415. + block[0] = (char)lseed;
  416. + memcpy(block+1, seed, lseed);
  417. + lua_pushlstring(L, block, lseed+1); /* to concat with result */
  418. + lblock = initblock(L, seed, lseed, block);
  419. + codestream(L, msg, lmsg, block, lblock);
  420. + lua_concat(L, 2);
  421. + return 1;
  422. +}
  423. +
  424. +
  425. +/**
  426. +* Decrypts a string. For any message, key, and seed, we have that
  427. +* <code>decrypt(crypt(msg, key, seed), key) == msg</code>.
  428. +* @param cyphertext: message to be decrypted (this must be the result of
  429. + a previous call to <code>crypt</code>.
  430. +* @param key: arbitrary binary string to be used as a key.
  431. +* @return The plaintext.
  432. +*/
  433. +static int decrypt (lua_State *L) {
  434. + size_t lcyphertext;
  435. + const char *cyphertext = luaL_checklstring(L, 1, &lcyphertext);
  436. + size_t lseed = cyphertext[0];
  437. + const char *seed = cyphertext+1;
  438. + int lblock;
  439. + char block[BLOCKSIZE+MAXKEY];
  440. + luaL_argcheck(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1,
  441. + "invalid cyphered string");
  442. + cyphertext += lseed+1;
  443. + lcyphertext -= lseed+1;
  444. + lblock = initblock(L, seed, lseed, block);
  445. + decodestream(L, cyphertext, lcyphertext, block, lblock);
  446. + return 1;
  447. +}
  448. +
  449. +
  450. +/*
  451. +** Assumes the table is on top of the stack.
  452. +*/
  453. +static void set_info (lua_State *L) {
  454. + lua_pushliteral (L, "_COPYRIGHT");
  455. + lua_pushliteral (L, "Copyright (C) 2003 PUC-Rio");
  456. + lua_settable (L, -3);
  457. + lua_pushliteral (L, "_DESCRIPTION");
  458. + lua_pushliteral (L, "Basic cryptographic facilities");
  459. + lua_settable (L, -3);
  460. + lua_pushliteral (L, "_VERSION");
  461. + lua_pushliteral (L, "MD5 1.1.2");
  462. + lua_settable (L, -3);
  463. +}
  464. +
  465. +
  466. +static struct luaL_reg md5lib[] = {
  467. + {"sum", lmd5},
  468. + {"exor", ex_or},
  469. + {"crypt", crypt},
  470. + {"decrypt", decrypt},
  471. + {NULL, NULL}
  472. +};
  473. +
  474. +
  475. +int luaopen_md5_core (lua_State *L) {
  476. + luaL_register(L, "md5", md5lib);
  477. + set_info (L);
  478. + return 1;
  479. +}
  480. +