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
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
13 diff -Nur lua-5.1.4.orig/src/linit.c lua-5.1.4/src/linit.c
14 --- lua-5.1.4.orig/src/linit.c 2009-04-27 00:36:06.000000000 +0200
15 +++ lua-5.1.4/src/linit.c 2009-04-27 00:44:54.000000000 +0200
17 {LUA_DBLIBNAME, luaopen_debug},
18 {LUA_POSIXLIBNAME, luaopen_posix},
19 {LUA_BITLIBNAME, luaopen_bit},
20 + {LUA_MD5LIBNAME, luaopen_md5_core},
24 diff -Nur lua-5.1.4.orig/src/lualib.h lua-5.1.4/src/lualib.h
25 --- lua-5.1.4.orig/src/lualib.h 2009-04-27 00:36:06.000000000 +0200
26 +++ lua-5.1.4/src/lualib.h 2009-04-27 00:46:01.000000000 +0200
28 #define LUA_BITLIBNAME "bit"
29 LUALIB_API int (luaopen_bit) (lua_State *L);
31 +#define LUA_MD5LIBNAME "md5"
32 +LUALIB_API int (luaopen_md5_core) (lua_State *L);
34 /* open all previous libraries */
35 LUALIB_API void (luaL_openlibs) (lua_State *L);
36 diff -Nur lua-5.1.4.orig/src/md5.c lua-5.1.4/src/md5.c
37 --- lua-5.1.4.orig/src/md5.c 1970-01-01 01:00:00.000000000 +0100
38 +++ lua-5.1.4/src/md5.c 2008-03-24 21:59:12.000000000 +0100
41 +* $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $
43 +* @author Marcela Ozorio Suarez, Roberto I.
53 +#define MASK 0xFFFFFFFF
54 +#if __STDC_VERSION__ >= 199901L
56 +typedef uint32_t WORD32;
58 +typedef unsigned int WORD32;
64 +* @param message: aribtary string.
65 +* @param len: message length.
66 +* @param output: buffer to receive the hash value. Its size must be
67 +* (at least) HASHSIZE.
69 +void md5 (const char *message, long len, char *output);
74 +** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32.
75 +** Os bits sao deslocados de 'num' posicoes
77 +#define rotate(D, num) (D<<num) | (D>>(WORD-num))
79 +/*Macros que definem operacoes relizadas pelo algoritmo md5 */
80 +#define F(x, y, z) (((x) & (y)) | ((~(x)) & (z)))
81 +#define G(x, y, z) (((x) & (z)) | ((y) & (~(z))))
82 +#define H(x, y, z) ((x) ^ (y) ^ (z))
83 +#define I(x, y, z) ((y) ^ ((x) | (~(z))))
86 +/*vetor de numeros utilizados pelo algoritmo md5 para embaralhar bits */
87 +static const WORD32 T[64]={
88 + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
89 + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
90 + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
91 + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
92 + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
93 + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
94 + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
95 + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
96 + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
97 + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
98 + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
99 + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
100 + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
101 + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
102 + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
103 + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
107 +static void word32tobytes (const WORD32 *input, char *output) {
110 + WORD32 v = *input++;
111 + output[j++] = (char)(v & 0xff); v >>= 8;
112 + output[j++] = (char)(v & 0xff); v >>= 8;
113 + output[j++] = (char)(v & 0xff); v >>= 8;
114 + output[j++] = (char)(v & 0xff);
119 +static void inic_digest(WORD32 *d) {
127 +/*funcao que implemeta os quatro passos principais do algoritmo MD5 */
128 +static void digest(const WORD32 *m, WORD32 *d) {
131 + for (j=0; j<4*4; j+=4) {
132 + d[0] = d[0]+ F(d[1], d[2], d[3])+ m[j] + T[j]; d[0]=rotate(d[0], 7);
134 + d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12);
136 + d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17);
138 + d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22);
142 + for (j=0; j<4*4; j+=4) {
143 + d[0] = d[0]+ G(d[1], d[2], d[3])+ m[(5*j+1)&0x0f] + T[(j-1)+17];
144 + d[0] = rotate(d[0],5);
146 + d[3] = d[3]+ G(d[0], d[1], d[2])+ m[((5*(j+1)+1)&0x0f)] + T[(j+0)+17];
147 + d[3] = rotate(d[3], 9);
149 + d[2] = d[2]+ G(d[3], d[0], d[1])+ m[((5*(j+2)+1)&0x0f)] + T[(j+1)+17];
150 + d[2] = rotate(d[2], 14);
152 + d[1] = d[1]+ G(d[2], d[3], d[0])+ m[((5*(j+3)+1)&0x0f)] + T[(j+2)+17];
153 + d[1] = rotate(d[1], 20);
157 + for (j=0; j<4*4; j+=4) {
158 + d[0] = d[0]+ H(d[1], d[2], d[3])+ m[(3*j+5)&0x0f] + T[(j-1)+33];
159 + d[0] = rotate(d[0], 4);
161 + d[3] = d[3]+ H(d[0], d[1], d[2])+ m[(3*(j+1)+5)&0x0f] + T[(j+0)+33];
162 + d[3] = rotate(d[3], 11);
164 + d[2] = d[2]+ H(d[3], d[0], d[1])+ m[(3*(j+2)+5)&0x0f] + T[(j+1)+33];
165 + d[2] = rotate(d[2], 16);
167 + d[1] = d[1]+ H(d[2], d[3], d[0])+ m[(3*(j+3)+5)&0x0f] + T[(j+2)+33];
168 + d[1] = rotate(d[1], 23);
172 + for (j=0; j<4*4; j+=4) {
173 + d[0] = d[0]+ I(d[1], d[2], d[3])+ m[(7*j)&0x0f] + T[(j-1)+49];
174 + d[0] = rotate(d[0], 6);
176 + d[3] = d[3]+ I(d[0], d[1], d[2])+ m[(7*(j+1))&0x0f] + T[(j+0)+49];
177 + d[3] = rotate(d[3], 10);
179 + d[2] = d[2]+ I(d[3], d[0], d[1])+ m[(7*(j+2))&0x0f] + T[(j+1)+49];
180 + d[2] = rotate(d[2], 15);
182 + d[1] = d[1]+ I(d[2], d[3], d[0])+ m[(7*(j+3))&0x0f] + T[(j+2)+49];
183 + d[1] = rotate(d[1], 21);
189 +static void bytestoword32 (WORD32 *x, const char *pt) {
191 + for (i=0; i<16; i++) {
193 + x[i] = (((WORD32)(unsigned char)pt[j+3] << 8 |
194 + (WORD32)(unsigned char)pt[j+2]) << 8 |
195 + (WORD32)(unsigned char)pt[j+1]) << 8 |
196 + (WORD32)(unsigned char)pt[j];
202 +static void put_length(WORD32 *x, long len) {
204 + x[14] = (WORD32)((len<<3) & MASK);
205 + x[15] = (WORD32)(len>>(32-3) & 0x7);
211 +* 0 - normal message (full 64 bytes)
212 +* 1 - enough room for 0x80, but not for message length (two 4-byte words)
213 +* 2 - enough room for 0x80 plus message length (at least 9 bytes free)
215 +static int converte (WORD32 *x, const char *pt, int num, int old_status) {
216 + int new_status = 0;
219 + memcpy(buff, pt, num); /* to avoid changing original string */
220 + memset(buff+num, 0, 64-num);
221 + if (old_status == 0)
222 + buff[num] = '\200';
226 + bytestoword32(x, pt);
227 + if (num <= (64 - 9))
234 +void md5 (const char *message, long len, char *output) {
239 + while (status != 2) {
242 + int numbytes = (len-i >= 64) ? 64 : len-i;
243 + /*salva os valores do vetor digest*/
244 + d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3];
245 + status = converte(wbuff, message+i, numbytes, status);
246 + if (status == 2) put_length(wbuff, len);
248 + d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3];
251 + word32tobytes(d, output);
254 diff -Nur lua-5.1.4.orig/src/md5.h lua-5.1.4/src/md5.h
255 --- lua-5.1.4.orig/src/md5.h 1970-01-01 01:00:00.000000000 +0100
256 +++ lua-5.1.4/src/md5.h 2009-04-27 00:49:13.000000000 +0200
259 +* $Id: md5.h,v 1.2 2006/03/03 15:04:49 tomas Exp $
260 +* Cryptographic module for Lua.
261 +* @author Roberto Ierusalimschy
273 +void md5 (const char *message, long len, char *output);
274 +int luaopen_md5_core (lua_State *L);
278 diff -Nur lua-5.1.4.orig/src/md5lib.c lua-5.1.4/src/md5lib.c
279 --- lua-5.1.4.orig/src/md5lib.c 1970-01-01 01:00:00.000000000 +0100
280 +++ lua-5.1.4/src/md5lib.c 2009-04-27 00:49:55.000000000 +0200
283 +* $Id: md5lib.c,v 1.10 2008/05/12 20:51:27 carregal Exp $
284 +* Cryptographic and Hash functions for Lua
285 +* @author Roberto Ierusalimschy
294 +#include "lauxlib.h"
296 +#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
297 +#include "compat-5.1.h"
304 +* Hash function. Returns a hash for a given string.
305 +* @param message: arbitrary binary string.
306 +* @return A 128-bit hash string.
308 +static int lmd5 (lua_State *L) {
311 + const char *message = luaL_checklstring(L, 1, &l);
312 + md5(message, l, buff);
313 + lua_pushlstring(L, buff, 16L);
319 +* X-Or. Does a bit-a-bit exclusive-or of two strings.
320 +* @param s1: arbitrary binary string.
321 +* @param s2: arbitrary binary string with same length as s1.
322 +* @return a binary string with same length as s1 and s2,
323 +* where each bit is the exclusive-or of the corresponding bits in s1-s2.
325 +static int ex_or (lua_State *L) {
327 + const char *s1 = luaL_checklstring(L, 1, &l1);
328 + const char *s2 = luaL_checklstring(L, 2, &l2);
330 + luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" );
331 + luaL_buffinit(L, &b);
332 + while (l1--) luaL_putchar(&b, (*s1++)^(*s2++));
333 + luaL_pushresult(&b);
338 +static void checkseed (lua_State *L) {
339 + if (lua_isnone(L, 3)) { /* no seed? */
340 + time_t tm = time(NULL); /* for `random' seed */
341 + lua_pushlstring(L, (char *)&tm, sizeof(tm));
347 +#define BLOCKSIZE 16
351 +static int initblock (lua_State *L, const char *seed, int lseed, char *block) {
353 + const char *key = luaL_checklstring(L, 2, &lkey);
355 + luaL_error(L, "key too long (> %d)", MAXKEY);
356 + memset(block, 0, BLOCKSIZE);
357 + memcpy(block, seed, lseed);
358 + memcpy(block+BLOCKSIZE, key, lkey);
359 + return (int)lkey+BLOCKSIZE;
363 +static void codestream (lua_State *L, const char *msg, size_t lmsg,
364 + char *block, int lblock) {
366 + luaL_buffinit(L, &b);
368 + char code[BLOCKSIZE];
370 + md5(block, lblock, code);
371 + for (i=0; i<BLOCKSIZE && lmsg > 0; i++, lmsg--)
373 + luaL_addlstring(&b, code, i);
374 + memcpy(block, code, i); /* update seed */
376 + luaL_pushresult(&b);
380 +static void decodestream (lua_State *L, const char *cypher, size_t lcypher,
381 + char *block, int lblock) {
383 + luaL_buffinit(L, &b);
384 + while (lcypher > 0) {
385 + char code[BLOCKSIZE];
387 + md5(block, lblock, code); /* update seed */
388 + for (i=0; i<BLOCKSIZE && lcypher > 0; i++, lcypher--)
389 + code[i] ^= *cypher++;
390 + luaL_addlstring(&b, code, i);
391 + memcpy(block, cypher-i, i);
393 + luaL_pushresult(&b);
398 +* Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback
400 +* @param message: arbitrary binary string to be encrypted.
401 +* @param key: arbitrary binary string to be used as a key.
402 +* @param [seed]: optional arbitrary binary string to be used as a seed.
403 +* if no seed is provided, the function uses the result of
404 +* <code>time()</code> as a seed.
405 +* @return The cyphertext (as a binary string).
407 +static int crypt (lua_State *L) {
409 + const char *msg = luaL_checklstring(L, 1, &lmsg);
413 + char block[BLOCKSIZE+MAXKEY];
415 + seed = luaL_checklstring(L, 3, &lseed);
416 + if (lseed > BLOCKSIZE)
417 + luaL_error(L, "seed too long (> %d)", BLOCKSIZE);
418 + /* put seed and seed length at the beginning of result */
419 + block[0] = (char)lseed;
420 + memcpy(block+1, seed, lseed);
421 + lua_pushlstring(L, block, lseed+1); /* to concat with result */
422 + lblock = initblock(L, seed, lseed, block);
423 + codestream(L, msg, lmsg, block, lblock);
430 +* Decrypts a string. For any message, key, and seed, we have that
431 +* <code>decrypt(crypt(msg, key, seed), key) == msg</code>.
432 +* @param cyphertext: message to be decrypted (this must be the result of
433 + a previous call to <code>crypt</code>.
434 +* @param key: arbitrary binary string to be used as a key.
435 +* @return The plaintext.
437 +static int decrypt (lua_State *L) {
438 + size_t lcyphertext;
439 + const char *cyphertext = luaL_checklstring(L, 1, &lcyphertext);
440 + size_t lseed = cyphertext[0];
441 + const char *seed = cyphertext+1;
443 + char block[BLOCKSIZE+MAXKEY];
444 + luaL_argcheck(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1,
445 + "invalid cyphered string");
446 + cyphertext += lseed+1;
447 + lcyphertext -= lseed+1;
448 + lblock = initblock(L, seed, lseed, block);
449 + decodestream(L, cyphertext, lcyphertext, block, lblock);
455 +** Assumes the table is on top of the stack.
457 +static void set_info (lua_State *L) {
458 + lua_pushliteral (L, "_COPYRIGHT");
459 + lua_pushliteral (L, "Copyright (C) 2003 PUC-Rio");
460 + lua_settable (L, -3);
461 + lua_pushliteral (L, "_DESCRIPTION");
462 + lua_pushliteral (L, "Basic cryptographic facilities");
463 + lua_settable (L, -3);
464 + lua_pushliteral (L, "_VERSION");
465 + lua_pushliteral (L, "MD5 1.1.2");
466 + lua_settable (L, -3);
470 +static struct luaL_reg md5lib[] = {
474 + {"decrypt", decrypt},
479 +int luaopen_md5_core (lua_State *L) {
480 + luaL_register(L, "md5", md5lib);