Debian Codegraph @

texk/web2c/luatexdir/luamd5/md5lib.c

raw
1 /**
2 * $Id: md5lib.c,v 1.2 2003/04/28 16:25:44 roberto Exp $
3 * Cryptographic and Hash functions for Lua
4 * @version 1.0
5 * @author Roberto Ierusalimschy
6 */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11
12 #include <lua.h>
13 #include <lauxlib.h>
14
15 #include "luamd5.h"
16
17 /**
18 * Hash function. Returns a hash for a given string.
19 * @param message: arbitrary binary string.
20 * @return A 128-bit hash string.
21 */
22
23 static int lmd5 (lua_State *L) {
24 char buff[16];
25 size_t l;
26 const char *message = luaL_checklstring(L, 1, &l);
27 md5(message, l, buff);
28 lua_pushlstring(L, buff, 16L);
29 return 1;
30 }
31
32 /**
33 * X-Or. Does a bit-a-bit exclusive-or of two strings.
34 * @param s1: arbitrary binary string.
35 * @param s2: arbitrary binary string with same length as s1.
36 * @return a binary string with same length as s1 and s2,
37 * where each bit is the exclusive-or of the corresponding bits in s1-s2.
38 */
39
40 static int ex_or (lua_State *L) {
41 size_t l1, l2;
42 const char *s1 = luaL_checklstring(L, 1, &l1);
43 const char *s2 = luaL_checklstring(L, 2, &l2);
44 luaL_Buffer b;
45 luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" );
46 luaL_buffinit(L, &b);
47 while (l1--)
48 luaL_addchar(&b, (*s1++)^(*s2++));
49 luaL_pushresult(&b);
50 return 1;
51 }
52
53 static void checkseed (lua_State *L) {
54 if (lua_isnone(L, 3)) { /* no seed? */
55 time_t tm = time(NULL); /* for `random' seed */
56 lua_pushlstring(L, (char *)&tm, sizeof(tm));
57 }
58 }
59
60 #define MAXKEY 256
61 #define BLOCKSIZE 16
62
63 static int initblock (lua_State *L, const char *seed, int lseed, char *block) {
64 size_t lkey;
65 const char *key = luaL_checklstring(L, 2, &lkey);
66 if (lkey > MAXKEY)
67 luaL_error(L, "key too long (> %d)", MAXKEY);
68 memset(block, 0, BLOCKSIZE);
69 memcpy(block, seed, lseed);
70 memcpy(block+BLOCKSIZE, key, lkey);
71 return (int)lkey+BLOCKSIZE;
72 }
73
74 static void codestream (lua_State *L, const char *msg, size_t lmsg,
75 char *block, int lblock) {
76 luaL_Buffer b;
77 luaL_buffinit(L, &b);
78 while (lmsg > 0) {
79 char code[BLOCKSIZE];
80 int i;
81 md5(block, lblock, code);
82 for (i=0; i<BLOCKSIZE && lmsg > 0; i++, lmsg--)
83 code[i] ^= *msg++;
84 luaL_addlstring(&b, code, i);
85 memcpy(block, code, i); /* update seed */
86 }
87 luaL_pushresult(&b);
88 }
89
90 static void decodestream (lua_State *L, const char *cypher, size_t lcypher,
91 char *block, int lblock) {
92 luaL_Buffer b;
93 luaL_buffinit(L, &b);
94 while (lcypher > 0) {
95 char code[BLOCKSIZE];
96 int i;
97 md5(block, lblock, code); /* update seed */
98 for (i=0; i<BLOCKSIZE && lcypher > 0; i++, lcypher--)
99 code[i] ^= *cypher++;
100 luaL_addlstring(&b, code, i);
101 memcpy(block, cypher-i, i);
102 }
103 luaL_pushresult(&b);
104 }
105
106 /**
107 * Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback
108 * mode).
109 * @param message: arbitrary binary string to be encrypted.
110 * @param key: arbitrary binary string to be used as a key.
111 * @param [seed]: optional arbitrary binary string to be used as a seed.
112 * if no seed is provided, the function uses the result of
113 * <code>time()</code> as a seed.
114 * @return The cyphertext (as a binary string).
115 */
116
117 static int crypt (lua_State *L) {
118 size_t lmsg;
119 const char *msg = luaL_checklstring(L, 1, &lmsg);
120 size_t lseed;
121 const char *seed;
122 int lblock;
123 char block[BLOCKSIZE+MAXKEY];
124 checkseed(L);
125 seed = luaL_checklstring(L, 3, &lseed);
126 if (lseed > BLOCKSIZE)
127 luaL_error(L, "seed too long (> %d)", BLOCKSIZE);
128 /* put seed and seed length at the beginning of result */
129 block[0] = (char)lseed;
130 memcpy(block+1, seed, lseed);
131 lua_pushlstring(L, block, lseed+1); /* to concat with result */
132 lblock = initblock(L, seed, lseed, block);
133 codestream(L, msg, lmsg, block, lblock);
134 lua_concat(L, 2);
135 return 1;
136 }
137
138
139 /**
140 * Decrypts a string. For any message, key, and seed, we have that
141 * <code>decrypt(crypt(msg, key, seed), key) == msg</code>.
142 * @param cyphertext: message to be decrypted (this must be the result of
143 a previous call to <code>crypt</code>.
144 * @param key: arbitrary binary string to be used as a key.
145 * @return The plaintext.
146 */
147 static int decrypt (lua_State *L) {
148 size_t lcyphertext;
149 const char *cyphertext = luaL_checklstring(L, 1, &lcyphertext);
150 size_t lseed = cyphertext[0];
151 const char *seed = cyphertext+1;
152 int lblock;
153 char block[BLOCKSIZE+MAXKEY];
154 luaL_argcheck(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1,
155 "invalid cyphered string");
156 cyphertext += lseed+1;
157 lcyphertext -= lseed+1;
158 lblock = initblock(L, seed, lseed, block);
159 decodestream(L, cyphertext, lcyphertext, block, lblock);
160 return 1;
161 }
162
163 /* not now .. doesn't compile anyway
164
165 #include "../luapplib/src/util/utilmd5.h"
166
167 static int pdfelib_md_5(lua_State * L)
168 {
169 if (lua_type(L,1) == LUA_TSTRING) {
170 uint8_t result[16];
171 size_t size = 0;
172 const char *data = lua_tolstring(L,1,&size);
173 md5(data,size,result);
174 lua_pushlstring(L,(const char *)result,16);
175 return 1;
176 }
177 return 0;
178 }
179
180 */
181
182 static struct luaL_Reg md5lib[] = {
183 { "sum", lmd5},
184 { "exor", ex_or},
185 { "crypt", crypt},
186 { "decrypt", decrypt},
187 { NULL, NULL}
188 };
189
190 int luaopen_md5(lua_State *L) {
191 luaL_openlib(L, "md5", md5lib, 0);
192 return 1;
193 }
194
195 /* We could use a different file but this is as easy. */
196
197 #include "utilsha.h"
198
199 static int sha2_256(lua_State * L)
200 {
201 if (lua_type(L,1) == LUA_TSTRING) {
202 uint8_t result[SHA256_DIGEST_LENGTH];
203 size_t size = 0;
204 const char *data = lua_tolstring(L,1,&size);
205 sha256_digest(data,size,result,SHA_BYTES);
206 lua_pushlstring(L,(const char *)result,SHA256_DIGEST_LENGTH);
207 return 1;
208 }
209 return 0;
210 }
211
212 static int sha2_384(lua_State * L)
213 {
214 if (lua_type(L,1) == LUA_TSTRING) {
215 size_t size = 0;
216 uint8_t result[SHA384_DIGEST_LENGTH];
217 const char *data = lua_tolstring(L,1,&size);
218 sha384_digest(data,size,result,SHA_BYTES);
219 lua_pushlstring(L,(const char *)result,SHA384_DIGEST_LENGTH);
220 return 1;
221 }
222 return 0;
223 }
224
225 static int sha2_512(lua_State * L)
226 {
227 if (lua_type(L,1) == LUA_TSTRING) {
228 uint8_t result[SHA512_DIGEST_LENGTH];
229 size_t size = 0;
230 const char *data = lua_tolstring(L,1,&size);
231 sha512_digest(data,size,result,SHA_BYTES);
232 lua_pushlstring(L,(const char *)result,SHA512_DIGEST_LENGTH);
233 return 1;
234 }
235 return 0;
236 }
237
238 static struct luaL_Reg sha2lib[] = {
239 { "digest256", sha2_256 },
240 { "digest384", sha2_384 },
241 { "digest512", sha2_512 },
242 { NULL, NULL}
243 };
244
245 int luaopen_sha2(lua_State *L) {
246 luaL_openlib(L, "sha2", sha2lib, 0);
247 return 1;
248 }