[package] lua: embed md5lib
[openwrt/svn-archive/archive.git] / package / lua / patches / 420-md5lib-embedded.patch
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
11 LUA_T= lua
12 LUA_O= lua.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
16 @@ -25,6 +25,7 @@
17 {LUA_DBLIBNAME, luaopen_debug},
18 {LUA_POSIXLIBNAME, luaopen_posix},
19 {LUA_BITLIBNAME, luaopen_bit},
20 + {LUA_MD5LIBNAME, luaopen_md5_core},
21 {NULL, NULL}
22 };
23
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
27 @@ -45,6 +45,8 @@
28 #define LUA_BITLIBNAME "bit"
29 LUALIB_API int (luaopen_bit) (lua_State *L);
30
31 +#define LUA_MD5LIBNAME "md5"
32 +LUALIB_API int (luaopen_md5_core) (lua_State *L);
33
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
39 @@ -0,0 +1,214 @@
40 +/**
41 +* $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $
42 +* Hash function MD5
43 +* @author Marcela Ozorio Suarez, Roberto I.
44 +*/
45 +
46 +
47 +#include <string.h>
48 +
49 +#include "md5.h"
50 +
51 +
52 +#define WORD 32
53 +#define MASK 0xFFFFFFFF
54 +#if __STDC_VERSION__ >= 199901L
55 +#include <stdint.h>
56 +typedef uint32_t WORD32;
57 +#else
58 +typedef unsigned int WORD32;
59 +#endif
60 +
61 +
62 +/**
63 +* md5 hash function.
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.
68 +*/
69 +void md5 (const char *message, long len, char *output);
70 +
71 +
72 +
73 +/*
74 +** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32.
75 +** Os bits sao deslocados de 'num' posicoes
76 +*/
77 +#define rotate(D, num) (D<<num) | (D>>(WORD-num))
78 +
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))))
84 +
85 +
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
104 +};
105 +
106 +
107 +static void word32tobytes (const WORD32 *input, char *output) {
108 + int j = 0;
109 + while (j<4*4) {
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);
115 + }
116 +}
117 +
118 +
119 +static void inic_digest(WORD32 *d) {
120 + d[0] = 0x67452301;
121 + d[1] = 0xEFCDAB89;
122 + d[2] = 0x98BADCFE;
123 + d[3] = 0x10325476;
124 +}
125 +
126 +
127 +/*funcao que implemeta os quatro passos principais do algoritmo MD5 */
128 +static void digest(const WORD32 *m, WORD32 *d) {
129 + int j;
130 + /*MD5 PASSO1 */
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);
133 + d[0]+=d[1];
134 + d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12);
135 + d[3]+=d[0];
136 + d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17);
137 + d[2]+=d[3];
138 + d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22);
139 + d[1]+=d[2];
140 + }
141 + /*MD5 PASSO2 */
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);
145 + d[0]+=d[1];
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);
148 + d[3]+=d[0];
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);
151 + d[2]+=d[3];
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);
154 + d[1]+=d[2];
155 + }
156 + /*MD5 PASSO3 */
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);
160 + d[0]+=d[1];
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);
163 + d[3]+=d[0];
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);
166 + d[2]+=d[3];
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);
169 + d[1]+=d[2];
170 + }
171 + /*MD5 PASSO4 */
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);
175 + d[0]+=d[1];
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);
178 + d[3]+=d[0];
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);
181 + d[2]+=d[3];
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);
184 + d[1]+=d[2];
185 + }
186 +}
187 +
188 +
189 +static void bytestoword32 (WORD32 *x, const char *pt) {
190 + int i;
191 + for (i=0; i<16; i++) {
192 + int j=i*4;
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];
197 + }
198 +
199 +}
200 +
201 +
202 +static void put_length(WORD32 *x, long len) {
203 + /* in bits! */
204 + x[14] = (WORD32)((len<<3) & MASK);
205 + x[15] = (WORD32)(len>>(32-3) & 0x7);
206 +}
207 +
208 +
209 +/*
210 +** returned status:
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)
214 +*/
215 +static int converte (WORD32 *x, const char *pt, int num, int old_status) {
216 + int new_status = 0;
217 + char buff[64];
218 + if (num<64) {
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';
223 + new_status = 1;
224 + pt = buff;
225 + }
226 + bytestoword32(x, pt);
227 + if (num <= (64 - 9))
228 + new_status = 2;
229 + return new_status;
230 +}
231 +
232 +
233 +
234 +void md5 (const char *message, long len, char *output) {
235 + WORD32 d[4];
236 + int status = 0;
237 + long i = 0;
238 + inic_digest(d);
239 + while (status != 2) {
240 + WORD32 d_old[4];
241 + WORD32 wbuff[16];
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);
247 + digest(wbuff, d);
248 + d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3];
249 + i += numbytes;
250 + }
251 + word32tobytes(d, output);
252 +}
253 +
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
257 @@ -0,0 +1,20 @@
258 +/**
259 +* $Id: md5.h,v 1.2 2006/03/03 15:04:49 tomas Exp $
260 +* Cryptographic module for Lua.
261 +* @author Roberto Ierusalimschy
262 +*/
263 +
264 +
265 +#ifndef md5_h
266 +#define md5_h
267 +
268 +#include "lua.h"
269 +
270 +
271 +#define HASHSIZE 16
272 +
273 +void md5 (const char *message, long len, char *output);
274 +int luaopen_md5_core (lua_State *L);
275 +
276 +
277 +#endif
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
281 @@ -0,0 +1,203 @@
282 +/**
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
286 +*/
287 +
288 +
289 +#include <stdlib.h>
290 +#include <string.h>
291 +#include <time.h>
292 +
293 +#include "lua.h"
294 +#include "lauxlib.h"
295 +
296 +#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
297 +#include "compat-5.1.h"
298 +#endif
299 +
300 +#include "md5.h"
301 +
302 +
303 +/**
304 +* Hash function. Returns a hash for a given string.
305 +* @param message: arbitrary binary string.
306 +* @return A 128-bit hash string.
307 +*/
308 +static int lmd5 (lua_State *L) {
309 + char buff[16];
310 + size_t l;
311 + const char *message = luaL_checklstring(L, 1, &l);
312 + md5(message, l, buff);
313 + lua_pushlstring(L, buff, 16L);
314 + return 1;
315 +}
316 +
317 +
318 +/**
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.
324 +*/
325 +static int ex_or (lua_State *L) {
326 + size_t l1, l2;
327 + const char *s1 = luaL_checklstring(L, 1, &l1);
328 + const char *s2 = luaL_checklstring(L, 2, &l2);
329 + luaL_Buffer b;
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);
334 + return 1;
335 +}
336 +
337 +
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));
342 + }
343 +}
344 +
345 +
346 +#define MAXKEY 256
347 +#define BLOCKSIZE 16
348 +
349 +
350 +
351 +static int initblock (lua_State *L, const char *seed, int lseed, char *block) {
352 + size_t lkey;
353 + const char *key = luaL_checklstring(L, 2, &lkey);
354 + if (lkey > MAXKEY)
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;
360 +}
361 +
362 +
363 +static void codestream (lua_State *L, const char *msg, size_t lmsg,
364 + char *block, int lblock) {
365 + luaL_Buffer b;
366 + luaL_buffinit(L, &b);
367 + while (lmsg > 0) {
368 + char code[BLOCKSIZE];
369 + int i;
370 + md5(block, lblock, code);
371 + for (i=0; i<BLOCKSIZE && lmsg > 0; i++, lmsg--)
372 + code[i] ^= *msg++;
373 + luaL_addlstring(&b, code, i);
374 + memcpy(block, code, i); /* update seed */
375 + }
376 + luaL_pushresult(&b);
377 +}
378 +
379 +
380 +static void decodestream (lua_State *L, const char *cypher, size_t lcypher,
381 + char *block, int lblock) {
382 + luaL_Buffer b;
383 + luaL_buffinit(L, &b);
384 + while (lcypher > 0) {
385 + char code[BLOCKSIZE];
386 + int i;
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);
392 + }
393 + luaL_pushresult(&b);
394 +}
395 +
396 +
397 +/**
398 +* Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback
399 +* mode).
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).
406 +*/
407 +static int crypt (lua_State *L) {
408 + size_t lmsg;
409 + const char *msg = luaL_checklstring(L, 1, &lmsg);
410 + size_t lseed;
411 + const char *seed;
412 + int lblock;
413 + char block[BLOCKSIZE+MAXKEY];
414 + checkseed(L);
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);
424 + lua_concat(L, 2);
425 + return 1;
426 +}
427 +
428 +
429 +/**
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.
436 +*/
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;
442 + int lblock;
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);
450 + return 1;
451 +}
452 +
453 +
454 +/*
455 +** Assumes the table is on top of the stack.
456 +*/
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);
467 +}
468 +
469 +
470 +static struct luaL_reg md5lib[] = {
471 + {"sum", lmd5},
472 + {"exor", ex_or},
473 + {"crypt", crypt},
474 + {"decrypt", decrypt},
475 + {NULL, NULL}
476 +};
477 +
478 +
479 +int luaopen_md5_core (lua_State *L) {
480 + luaL_register(L, "md5", md5lib);
481 + set_info (L);
482 + return 1;
483 +}
484 +