diff options
Diffstat (limited to 'vendor/md5')
-rw-r--r-- | vendor/md5/.gitignore | 4 | ||||
-rw-r--r-- | vendor/md5/README.md | 47 | ||||
-rw-r--r-- | vendor/md5/rockspec/md5-1.2-1.rockspec | 35 | ||||
-rw-r--r-- | vendor/md5/src/compat-5.2.c | 21 | ||||
-rw-r--r-- | vendor/md5/src/compat-5.2.h | 15 | ||||
-rwxr-xr-x | vendor/md5/src/des56.c | 548 | ||||
-rw-r--r-- | vendor/md5/src/des56.def | 5 | ||||
-rwxr-xr-x | vendor/md5/src/des56.h | 77 | ||||
-rw-r--r-- | vendor/md5/src/ldes56.c | 152 | ||||
-rwxr-xr-x | vendor/md5/src/ldes56.h | 1 | ||||
-rwxr-xr-x | vendor/md5/src/md5.c | 262 | ||||
-rw-r--r-- | vendor/md5/src/md5.def | 5 | ||||
-rwxr-xr-x | vendor/md5/src/md5.h | 40 | ||||
-rw-r--r-- | vendor/md5/src/md5.lua | 26 | ||||
-rw-r--r-- | vendor/md5/src/md5lib.c | 200 |
15 files changed, 1438 insertions, 0 deletions
diff --git a/vendor/md5/.gitignore b/vendor/md5/.gitignore new file mode 100644 index 00000000..9776091d --- /dev/null +++ b/vendor/md5/.gitignore | |||
@@ -0,0 +1,4 @@ | |||
1 | *.o | ||
2 | *.obj | ||
3 | *.so | ||
4 | *.dll | ||
diff --git a/vendor/md5/README.md b/vendor/md5/README.md new file mode 100644 index 00000000..d51492e0 --- /dev/null +++ b/vendor/md5/README.md | |||
@@ -0,0 +1,47 @@ | |||
1 | # MD5 - Cryptographic Library for Lua | ||
2 | |||
3 | http://keplerproject.github.io/md5/ | ||
4 | |||
5 | MD5 offers basic cryptographic facilities for Lua 5.1: a hash (digest) | ||
6 | function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based | ||
7 | on DES with 56-bit keys. | ||
8 | |||
9 | MD5 current version is 1.2. | ||
10 | |||
11 | Please check the documentation at /doc/us/ for more information. | ||
12 | |||
13 | ## Installation | ||
14 | |||
15 | To install using [LuaRocks](https://github.com/keplerproject/luarocks) run: | ||
16 | |||
17 | ``` | ||
18 | luarocks install md5 | ||
19 | ``` | ||
20 | |||
21 | To install on Linux/OSX/BSD, please edit the config file and then call | ||
22 | |||
23 | ``` | ||
24 | make | ||
25 | make install | ||
26 | ``` | ||
27 | |||
28 | The last step may require root privileges. | ||
29 | |||
30 | ## History | ||
31 | |||
32 | Version 1.2 [06/Sep/2013] | ||
33 | |||
34 | * Code adapted to compile for Lua 5.0, 5.1 and 5.2 | ||
35 | |||
36 | Version 1.1.2 [12/May/2008] | ||
37 | |||
38 | * Fixed bug in 64-bit systems | ||
39 | * Fixed the Windows makefile to accept longer directory names | ||
40 | (patch by Alessandro Hecht and Ignacio BurgueƱo). | ||
41 | |||
42 | |||
43 | ## License | ||
44 | |||
45 | MD5 is free software and uses the same license as Lua (MIT). | ||
46 | |||
47 | The DES 56 C library was implemented by Stuart Levy and uses a MIT license too (check the source). | ||
diff --git a/vendor/md5/rockspec/md5-1.2-1.rockspec b/vendor/md5/rockspec/md5-1.2-1.rockspec new file mode 100644 index 00000000..04f61ce4 --- /dev/null +++ b/vendor/md5/rockspec/md5-1.2-1.rockspec | |||
@@ -0,0 +1,35 @@ | |||
1 | package = "MD5" | ||
2 | version = "1.2-1" | ||
3 | source = { | ||
4 | url = "https://github.com/keplerproject/md5/archive/v1.2.tar.gz", | ||
5 | md5 = "c166f8a983401802a86655a8c733441e", | ||
6 | dir = "md5-1.2", | ||
7 | } | ||
8 | description = { | ||
9 | summary = "Basic cryptographic library", | ||
10 | detailed = [[ | ||
11 | MD5 offers basic cryptographic facilities for Lua 5.X: | ||
12 | a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, | ||
13 | and a pair crypt/decrypt based on DES with 56-bit keys. | ||
14 | ]], | ||
15 | license = "MIT/X11", | ||
16 | homepage = "http://www.keplerproject.org/md5/", | ||
17 | } | ||
18 | dependencies = { | ||
19 | "lua >= 5.0" | ||
20 | } | ||
21 | build = { | ||
22 | type = "builtin", | ||
23 | modules = { | ||
24 | md5 = "src/md5.lua", | ||
25 | ["md5.core"] = { | ||
26 | sources = { "src/compat-5.2.c", "src/md5.c", "src/md5lib.c", }, | ||
27 | incdirs = { "src/", }, | ||
28 | }, | ||
29 | des56 = { | ||
30 | sources = { "src/compat-5.2.c", "src/des56.c", "src/ldes56.c", }, | ||
31 | incdirs = { "src/", }, | ||
32 | }, | ||
33 | }, | ||
34 | copy_directories = { "doc", "tests", }, | ||
35 | } | ||
diff --git a/vendor/md5/src/compat-5.2.c b/vendor/md5/src/compat-5.2.c new file mode 100644 index 00000000..ce57660b --- /dev/null +++ b/vendor/md5/src/compat-5.2.c | |||
@@ -0,0 +1,21 @@ | |||
1 | #include "lua.h" | ||
2 | #include "lauxlib.h" | ||
3 | #include "compat-5.2.h" | ||
4 | |||
5 | #if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 | ||
6 | /* | ||
7 | ** Adapted from Lua 5.2.0 | ||
8 | */ | ||
9 | void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { | ||
10 | luaL_checkstack(L, nup+1, "too many upvalues"); | ||
11 | for (; l->name != NULL; l++) { /* fill the table with given functions */ | ||
12 | int i; | ||
13 | lua_pushstring(L, l->name); | ||
14 | for (i = 0; i < nup; i++) /* copy upvalues to the top */ | ||
15 | lua_pushvalue(L, -(nup + 1)); | ||
16 | lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ | ||
17 | lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ | ||
18 | } | ||
19 | lua_pop(L, nup); /* remove upvalues */ | ||
20 | } | ||
21 | #endif | ||
diff --git a/vendor/md5/src/compat-5.2.h b/vendor/md5/src/compat-5.2.h new file mode 100644 index 00000000..c80fd616 --- /dev/null +++ b/vendor/md5/src/compat-5.2.h | |||
@@ -0,0 +1,15 @@ | |||
1 | #if !defined LUA_VERSION_NUM | ||
2 | /* Lua 5.0 */ | ||
3 | #define luaL_Reg luaL_reg | ||
4 | |||
5 | #define luaL_addchar(B,c) \ | ||
6 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ | ||
7 | (*(B)->p++ = (char)(c))) | ||
8 | #endif | ||
9 | |||
10 | #if LUA_VERSION_NUM==501 | ||
11 | /* Lua 5.1 */ | ||
12 | #define lua_rawlen lua_objlen | ||
13 | #endif | ||
14 | |||
15 | void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); | ||
diff --git a/vendor/md5/src/des56.c b/vendor/md5/src/des56.c new file mode 100755 index 00000000..1fb377b9 --- /dev/null +++ b/vendor/md5/src/des56.c | |||
@@ -0,0 +1,548 @@ | |||
1 | |||
2 | /* | ||
3 | * Fast implementation of the DES, as described in the Federal Register, | ||
4 | * Vol. 40, No. 52, p. 12134, March 17, 1975. | ||
5 | * | ||
6 | * Stuart Levy, Minnesota Supercomputer Center, April 1988. | ||
7 | * Currently (2007) slevy@ncsa.uiuc.edu | ||
8 | * NCSA, University of Illinois Urbana-Champaign | ||
9 | * | ||
10 | * Calling sequence: | ||
11 | * | ||
12 | * typedef unsigned long keysched[32]; | ||
13 | * | ||
14 | * fsetkey(key, keysched) / * Converts a DES key to a "key schedule" * / | ||
15 | * unsigned char key[8]; | ||
16 | * keysched *ks; | ||
17 | * | ||
18 | * fencrypt(block, decrypt, keysched) / * En/decrypts one 64-bit block * / | ||
19 | * unsigned char block[8]; / * data, en/decrypted in place * / | ||
20 | * int decrypt; / * 0=>encrypt, 1=>decrypt * / | ||
21 | * keysched *ks; / * key schedule, as set by fsetkey * / | ||
22 | * | ||
23 | * Key and data block representation: | ||
24 | * The 56-bit key (bits 1..64 including "parity" bits 8, 16, 24, ..., 64) | ||
25 | * and the 64-bit data block (bits 1..64) | ||
26 | * are each stored in arrays of 8 bytes. | ||
27 | * Following the NBS numbering, the MSB has the bit number 1, so | ||
28 | * key[0] = 128*bit1 + 64*bit2 + ... + 1*bit8, ... through | ||
29 | * key[7] = 128*bit57 + 64*bit58 + ... + 1*bit64. | ||
30 | * In the key, "parity" bits are not checked; their values are ignored. | ||
31 | * | ||
32 | */ | ||
33 | |||
34 | /* | ||
35 | =============================================================================== | ||
36 | License | ||
37 | |||
38 | des56.c is licensed under the terms of the MIT license reproduced below. | ||
39 | This means that des56.c is free software and can be used for both academic | ||
40 | and commercial purposes at absolutely no cost. | ||
41 | =============================================================================== | ||
42 | Copyright (C) 1988 Stuart Levy | ||
43 | |||
44 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
45 | of this software and associated documentation files (the "Software"), to deal | ||
46 | in the Software without restriction, including without limitation the rights | ||
47 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
48 | copies of the Software, and to permit persons to whom the Software is | ||
49 | furnished to do so, subject to the following conditions: | ||
50 | |||
51 | The above copyright notice and this permission notice shall be included in | ||
52 | all copies or substantial portions of the Software. | ||
53 | |||
54 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
55 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
56 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
57 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
58 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
59 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
60 | THE SOFTWARE. | ||
61 | */ | ||
62 | |||
63 | |||
64 | #include "des56.h" | ||
65 | |||
66 | |||
67 | /* | ||
68 | * Key schedule generation. | ||
69 | * We begin by pointlessly permuting the 56 useful key bits into | ||
70 | * two groups of 28 bits called C and D. | ||
71 | * bK_C and bK_D are indexed by C and D bit numbers, respectively, | ||
72 | * and give the key bit number (1..64) which should initialize that C/D bit. | ||
73 | * This is the "permuted choice 1" table. | ||
74 | */ | ||
75 | |||
76 | static tiny bK_C[28] = { | ||
77 | 57, 49, 41, 33, 25, 17, 9, | ||
78 | 1, 58, 50, 42, 34, 26, 18, | ||
79 | 10, 2, 59, 51, 43, 35, 27, | ||
80 | 19, 11, 3, 60, 52, 44, 36, | ||
81 | }; | ||
82 | static tiny bK_D[28] = { | ||
83 | 63, 55, 47, 39, 31, 23, 15, | ||
84 | 7, 62, 54, 46, 38, 30, 22, | ||
85 | 14, 6, 61, 53, 45, 37, 29, | ||
86 | 21, 13, 5, 28, 20, 12, 4, | ||
87 | }; | ||
88 | |||
89 | /* | ||
90 | * For speed, we invert these, building tables to map groups of | ||
91 | * key bits into the corresponding C and D bits. | ||
92 | * We represent C and D each as 28 contiguous bits right-justified in a | ||
93 | * word, padded on the left with zeros. | ||
94 | * If key byte `i' is said to contain bits Ki,0 (MSB) Ki,1 ... Ki,7 (LSB) | ||
95 | * then | ||
96 | * wC_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] gives the C bits for Ki,0..3, | ||
97 | * wD_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] the corresponding D bits, | ||
98 | * wC_K3[i][Ki,4 Ki,5 Ki,6] the C bits for Ki,4..6, | ||
99 | * and wD_K3[i][Ki,4 Ki,5 Ki,6] the D bits for Ki,4..6. | ||
100 | * Ki,7 is ignored since it is the nominal parity bit. | ||
101 | * We could just use a single table for [i][Ki,0 .. Ki,6] but that | ||
102 | * would take a lot of storage for such a rarely-used function. | ||
103 | */ | ||
104 | |||
105 | static word32 wC_K4[8][16], wC_K3[8][8]; | ||
106 | static word32 wD_K4[8][16], wD_K3[8][8]; | ||
107 | |||
108 | /* | ||
109 | * Successive Ci and Di for the sixteen steps in the key schedule are | ||
110 | * created by independent 28-bit left circular shifts on C and D. | ||
111 | * The shift count varies with the step number. | ||
112 | */ | ||
113 | static tiny preshift[16] = { | ||
114 | 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, | ||
115 | }; | ||
116 | |||
117 | /* | ||
118 | * Each step in the key schedule is generated by selecting 48 bits | ||
119 | * (8 groups of 6 bits) from the appropriately shifted Ci and Di. | ||
120 | * bCD_KS, indexed by the key schedule bit number, gives the bit number | ||
121 | * in CD (CD1 = MSB of C, CD28 = LSB of C, CD29 = MSB of D, CD56 = LSB of D) | ||
122 | * which determines that bit of the key schedule. | ||
123 | * Note that only C bits (1..28) appear in the first (upper) 24 bits of | ||
124 | * the key schedule, and D bits (29..56) in the second (lower) 24 bits. | ||
125 | * This is the "permuted-choice-2" table. | ||
126 | */ | ||
127 | |||
128 | static tiny bCD_KS[48] = { | ||
129 | 14, 17, 11, 24, 1, 5, | ||
130 | 3, 28, 15, 6, 21, 10, | ||
131 | 23, 19, 12, 4, 26, 8, | ||
132 | 16, 7, 27, 20, 13, 2, | ||
133 | 41, 52, 31, 37, 47, 55, | ||
134 | 30, 40, 51, 45, 33, 48, | ||
135 | 44, 49, 39, 56, 34, 53, | ||
136 | 46, 42, 50, 36, 29, 32, | ||
137 | }; | ||
138 | |||
139 | /* | ||
140 | * We invert bCD_KS into a pair of tables which map groups of 4 | ||
141 | * C or D bits into corresponding key schedule bits. | ||
142 | * We represent each step of the key schedule as 8 groups of 8 bits, | ||
143 | * with the 6 real bits right-justified in each 8-bit group. | ||
144 | * hKS_C4[i][C4i+1 .. C4i+4] gives the bits in the high order (first four) | ||
145 | * key schedule "bytes" which correspond to C bits 4i+1 .. 4i+4. | ||
146 | * lKS_D4[i][D4i+1 .. D4i+4] gives the appropriate bits in the latter (last 4) | ||
147 | * key schedule bytes, from the corresponding D bits. | ||
148 | */ | ||
149 | |||
150 | static word32 hKS_C4[7][16]; | ||
151 | static word32 lKS_D4[7][16]; | ||
152 | |||
153 | /* | ||
154 | * Encryption/decryption. | ||
155 | * Before beginning, and after ending, we perform another useless permutation | ||
156 | * on the bits in the data block. | ||
157 | * | ||
158 | * The initial permutation and its inverse, final permutation | ||
159 | * are too simple to need a table for. If we break the input I1 .. I64 into | ||
160 | * 8-bit chunks I0,0 I0,1 ... I0,7 I1,0 I1,1 ... I7,7 | ||
161 | * then the initial permutation sets LR as follows: | ||
162 | * L = I7,1 I6,1 I5,1 ... I0,1 I7,3 I6,3 ... I0,3 I7,5 ... I0,5 I7,7 ... I0,7 | ||
163 | * and | ||
164 | * R = I7,0 I6,0 I5,0 ... I0,0 I7,2 I6,2 ... I0,2 I7,4 ... I0,4 I7,6 ... I0,6 | ||
165 | * | ||
166 | * If we number the bits in the final LR similarly, | ||
167 | * L = L0,0 L0,1 ... L3,7 R = R0,0 R0,1 ... R3,7 | ||
168 | * then the output is | ||
169 | * O = R0,7 L0,7 R1,7 L1,7 ... R3,7 L3,7 R0,6 L0,6 ... L3,6 R0,5 ... R3,0 L3,0 | ||
170 | * | ||
171 | * To speed I => LR shuffling we use an array of 32-bit values indexed by | ||
172 | * 8-bit input bytes. | ||
173 | * wL_I8[ 0 I0,1 0 I0,3 0 I0,5 0 I0,7 ] = the corresponding L bits. | ||
174 | * Other R and L bits are derived from wL_I8 by shifting. | ||
175 | * | ||
176 | * To speed LR => O shuffling, an array of 32-bit values indexed by 4-bit lumps: | ||
177 | * wO_L4[ L0,4 L0,5 L0,6 L0,7 ] = the corresponding high-order 32 O bits. | ||
178 | */ | ||
179 | |||
180 | static word32 wL_I8[0x55 + 1]; | ||
181 | static word32 wO_L4[16]; | ||
182 | |||
183 | /* | ||
184 | * Core of encryption/decryption. | ||
185 | * In each key schedule stage, we: | ||
186 | * take 8 overlapping groups of 6 bits each from R | ||
187 | * (the NBS tabulates the bit selections in the E table, | ||
188 | * but it's so simple we just use shifting to get the right bits) | ||
189 | * XOR each group with the corresponding bits from the key schedule | ||
190 | * Use the resulting 6 bits as an index into the appropriate S table | ||
191 | * (there are 8 such tables, one per group of 6 bits) | ||
192 | * Each S entry yields 4 bits. | ||
193 | * The 8 groups of 4 bits are catenated into a 32-bit value. | ||
194 | * Those 32 bits are permuted according to the P table. | ||
195 | * Finally the permuted 32-bit value is XORed with L and becomes | ||
196 | * the R value for the next stage, while the previous R becomes the new L. | ||
197 | * | ||
198 | * Here, we merge the P permutation with the S tables by making the | ||
199 | * S entries be 32-bit masks, already suitably permuted. | ||
200 | * Also, the bits in each six-bit group must be permuted before use as | ||
201 | * an index into the NBS-tabulated S tables. | ||
202 | * We rearrange entries in wPS so that natural bit order can be used. | ||
203 | */ | ||
204 | |||
205 | static word32 wPS[8][64]; | ||
206 | |||
207 | static tiny P[32] = { | ||
208 | 16, 7, 20, 21, | ||
209 | 29, 12, 28, 17, | ||
210 | 1, 15, 23, 26, | ||
211 | 5, 18, 31, 10, | ||
212 | 2, 8, 24, 14, | ||
213 | 32, 27, 3, 9, | ||
214 | 19, 13, 30, 6, | ||
215 | 22, 11, 4, 25, | ||
216 | }; | ||
217 | |||
218 | static tiny S[8][64] = { | ||
219 | { | ||
220 | 14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7, | ||
221 | 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8, | ||
222 | 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0, | ||
223 | 15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13, | ||
224 | }, | ||
225 | |||
226 | { | ||
227 | 15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10, | ||
228 | 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5, | ||
229 | 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15, | ||
230 | 13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9, | ||
231 | }, | ||
232 | |||
233 | { | ||
234 | 10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8, | ||
235 | 13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1, | ||
236 | 13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7, | ||
237 | 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12, | ||
238 | }, | ||
239 | |||
240 | { | ||
241 | 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15, | ||
242 | 13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9, | ||
243 | 10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4, | ||
244 | 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14, | ||
245 | }, | ||
246 | |||
247 | { | ||
248 | 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9, | ||
249 | 14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6, | ||
250 | 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14, | ||
251 | 11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3, | ||
252 | }, | ||
253 | |||
254 | { | ||
255 | 12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11, | ||
256 | 10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8, | ||
257 | 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6, | ||
258 | 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13, | ||
259 | }, | ||
260 | |||
261 | { | ||
262 | 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1, | ||
263 | 13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6, | ||
264 | 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2, | ||
265 | 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12, | ||
266 | }, | ||
267 | |||
268 | { | ||
269 | 13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7, | ||
270 | 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2, | ||
271 | 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8, | ||
272 | 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11, | ||
273 | }, | ||
274 | }; | ||
275 | |||
276 | static void buildtables( void ) | ||
277 | { | ||
278 | register int i, j; | ||
279 | register word32 v; | ||
280 | word32 wC_K[64], wD_K[64]; | ||
281 | word32 hKS_C[28], lKS_D[28]; | ||
282 | int Smap[64]; | ||
283 | word32 wP[32]; | ||
284 | |||
285 | #if USG | ||
286 | # define ZERO(array) memset((char *)(array), '\0', sizeof(array)) | ||
287 | #else | ||
288 | # if BSD | ||
289 | # define ZERO(array) bzero((char *)(array), sizeof(array)) | ||
290 | # else | ||
291 | # define ZERO(array) { register word32 *p = (word32 *)(array); \ | ||
292 | i = sizeof(array) / sizeof(*p); \ | ||
293 | do { *p++ = 0; } while(--i > 0); \ | ||
294 | } | ||
295 | # endif | ||
296 | #endif | ||
297 | |||
298 | |||
299 | /* Invert permuted-choice-1 (key => C,D) */ | ||
300 | |||
301 | ZERO(wC_K); | ||
302 | ZERO(wD_K); | ||
303 | v = 1; | ||
304 | for(j = 28; --j >= 0; ) { | ||
305 | wC_K[ bK_C[j] - 1 ] = wD_K[ bK_D[j] - 1 ] = v; | ||
306 | v += v; /* (i.e. v <<= 1) */ | ||
307 | } | ||
308 | |||
309 | for(i = 0; i < 64; i++) { | ||
310 | int t = 8 >> (i & 3); | ||
311 | for(j = 0; j < 16; j++) { | ||
312 | if(j & t) { | ||
313 | wC_K4[i >> 3][j] |= wC_K[i]; | ||
314 | wD_K4[i >> 3][j] |= wD_K[i]; | ||
315 | if(j < 8) { | ||
316 | wC_K3[i >> 3][j] |= wC_K[i + 3]; | ||
317 | wD_K3[i >> 3][j] |= wD_K[i + 3]; | ||
318 | } | ||
319 | } | ||
320 | } | ||
321 | /* Generate the sequence 0,1,2,3, 8,9,10,11, ..., 56,57,58,59. */ | ||
322 | if(t == 1) i += 4; | ||
323 | } | ||
324 | |||
325 | /* Invert permuted-choice-2 */ | ||
326 | |||
327 | ZERO(hKS_C); | ||
328 | ZERO(lKS_D); | ||
329 | v = 1; | ||
330 | for(i = 24; (i -= 6) >= 0; ) { | ||
331 | j = i+5; | ||
332 | do { | ||
333 | hKS_C[ bCD_KS[j] - 1 ] = lKS_D[ bCD_KS[j+24] - 28 - 1 ] = v; | ||
334 | v += v; /* Like v <<= 1 but may be faster */ | ||
335 | } while(--j >= i); | ||
336 | v <<= 2; /* Keep byte aligned */ | ||
337 | } | ||
338 | |||
339 | for(i = 0; i < 28; i++) { | ||
340 | v = 8 >> (i & 3); | ||
341 | for(j = 0; j < 16; j++) { | ||
342 | if(j & v) { | ||
343 | hKS_C4[i >> 2][j] |= hKS_C[i]; | ||
344 | lKS_D4[i >> 2][j] |= lKS_D[i]; | ||
345 | } | ||
346 | } | ||
347 | } | ||
348 | |||
349 | /* Initial permutation */ | ||
350 | |||
351 | for(i = 0; i <= 0x55; i++) { | ||
352 | v = 0; | ||
353 | if(i & 64) v = (word32) 1 << 24; | ||
354 | if(i & 16) v |= (word32) 1 << 16; | ||
355 | if(i & 4) v |= (word32) 1 << 8; | ||
356 | if(i & 1) v |= 1; | ||
357 | wL_I8[i] = v; | ||
358 | } | ||
359 | |||
360 | /* Final permutation */ | ||
361 | |||
362 | for(i = 0; i < 16; i++) { | ||
363 | v = 0; | ||
364 | if(i & 1) v = (word32) 1 << 24; | ||
365 | if(i & 2) v |= (word32) 1 << 16; | ||
366 | if(i & 4) v |= (word32) 1 << 8; | ||
367 | if(i & 8) v |= (word32) 1; | ||
368 | wO_L4[i] = v; | ||
369 | } | ||
370 | |||
371 | /* Funny bit rearrangement on second index into S tables */ | ||
372 | |||
373 | for(i = 0; i < 64; i++) { | ||
374 | Smap[i] = (i & 0x20) | (i & 1) << 4 | (i & 0x1e) >> 1; | ||
375 | } | ||
376 | |||
377 | /* Invert permutation P into mask indexed by R bit number */ | ||
378 | |||
379 | v = 1; | ||
380 | for(i = 32; --i >= 0; ) { | ||
381 | wP[ P[i] - 1 ] = v; | ||
382 | v += v; | ||
383 | } | ||
384 | |||
385 | /* Build bit-mask versions of S tables, indexed in natural bit order */ | ||
386 | |||
387 | for(i = 0; i < 8; i++) { | ||
388 | for(j = 0; j < 64; j++) { | ||
389 | int k, t; | ||
390 | |||
391 | t = S[i][ Smap[j] ]; | ||
392 | for(k = 0; k < 4; k++) { | ||
393 | if(t & 8) | ||
394 | wPS[i][j] |= wP[4*i + k]; | ||
395 | t += t; | ||
396 | } | ||
397 | } | ||
398 | } | ||
399 | } | ||
400 | |||
401 | |||
402 | void fsetkey(char key[8], keysched *ks) | ||
403 | { | ||
404 | register int i; | ||
405 | register word32 C, D; | ||
406 | static int built = 0; | ||
407 | |||
408 | if(!built) { | ||
409 | buildtables(); | ||
410 | built = 1; | ||
411 | } | ||
412 | |||
413 | C = D = 0; | ||
414 | for(i = 0; i < 8; i++) { | ||
415 | register int v; | ||
416 | |||
417 | v = key[i] >> 1; /* Discard "parity" bit */ | ||
418 | C |= wC_K4[i][(v>>3) & 15] | wC_K3[i][v & 7]; | ||
419 | D |= wD_K4[i][(v>>3) & 15] | wD_K3[i][v & 7]; | ||
420 | } | ||
421 | |||
422 | /* | ||
423 | * C and D now hold the suitably right-justified | ||
424 | * 28 permuted key bits each. | ||
425 | */ | ||
426 | for(i = 0; i < 16; i++) { | ||
427 | #ifdef CRAY | ||
428 | #define choice2(x, v) x[6][v&15] | x[5][(v>>4)&15] | x[4][(v>>8)&15] | \ | ||
429 | x[3][(v>>12)&15] | x[2][(v>>16)&15] | x[1][(v>>20)&15] | \ | ||
430 | x[0][(v>>24)&15] | ||
431 | #else | ||
432 | register word32 *ap; | ||
433 | |||
434 | # define choice2(x, v) ( \ | ||
435 | ap = &(x)[0][0], \ | ||
436 | ap[16*6 + (v&15)] | \ | ||
437 | ap[16*5 + ((v>>4)&15)] | ap[16*4 + ((v>>8)&15)] | \ | ||
438 | ap[16*3 + ((v>>12)&15)] | ap[16*2 + ((v>>16)&15)] | \ | ||
439 | ap[16*1 + ((v>>20)&15)] | ap[16*0 + ((v>>24)&15)] ) | ||
440 | #endif | ||
441 | |||
442 | |||
443 | /* 28-bit left circular shift */ | ||
444 | C <<= preshift[i]; | ||
445 | C = ((C >> 28) & 3) | (C & (((word32)1<<28) - 1)); | ||
446 | ks->KS[i].h = choice2(hKS_C4, C); | ||
447 | |||
448 | D <<= preshift[i]; | ||
449 | D = ((D >> 28) & 3) | (D & (((word32)1<<28) - 1)); | ||
450 | ks->KS[i].l = choice2(lKS_D4, D); | ||
451 | } | ||
452 | } | ||
453 | |||
454 | void | ||
455 | fencrypt(char block[8], int decrypt, keysched *ks) | ||
456 | { | ||
457 | int i; | ||
458 | register word32 L, R; | ||
459 | register struct keystage *ksp; | ||
460 | register word32 *ap; | ||
461 | |||
462 | /* Initial permutation */ | ||
463 | |||
464 | L = R = 0; | ||
465 | i = 7; | ||
466 | ap = wL_I8; | ||
467 | do { | ||
468 | register int v; | ||
469 | |||
470 | v = block[i]; /* Could optimize according to ENDIAN */ | ||
471 | L = ap[v & 0x55] | (L << 1); | ||
472 | R = ap[(v >> 1) & 0x55] | (R << 1); | ||
473 | } while(--i >= 0); | ||
474 | |||
475 | if(decrypt) { | ||
476 | ksp = &ks->KS[15]; | ||
477 | } else { | ||
478 | ksp = &ks->KS[0]; | ||
479 | } | ||
480 | |||
481 | #ifdef CRAY | ||
482 | # define PS(i,j) wPS[i][j] | ||
483 | #else | ||
484 | # define PS(i,j) ap[64*(i) + (j)] | ||
485 | ap = &wPS[0][0]; | ||
486 | #endif | ||
487 | |||
488 | i = 16; | ||
489 | do { | ||
490 | register word32 k, tR; | ||
491 | |||
492 | tR = (R >> 15) | (R << 17); | ||
493 | |||
494 | k = ksp->h; | ||
495 | L ^= PS(0, ((tR >> 12) ^ (k >> 24)) & 63) | ||
496 | | PS(1, ((tR >> 8) ^ (k >> 16)) & 63) | ||
497 | | PS(2, ((tR >> 4) ^ (k >> 8)) & 63) | ||
498 | | PS(3, (tR ^ k) & 63); | ||
499 | |||
500 | k = ksp->l; | ||
501 | L ^= PS(4, ((R >> 11) ^ (k >> 24)) & 63) | ||
502 | | PS(5, ((R >> 7) ^ (k >> 16)) & 63) | ||
503 | | PS(6, ((R >> 3) ^ (k >> 8)) & 63) | ||
504 | | PS(7, ((tR >> 16) ^ k) & 63); | ||
505 | |||
506 | tR = L; | ||
507 | L = R; | ||
508 | R = tR; | ||
509 | |||
510 | |||
511 | if(decrypt) | ||
512 | ksp--; | ||
513 | else | ||
514 | ksp++; | ||
515 | } while(--i > 0); | ||
516 | { | ||
517 | register word32 t; | ||
518 | |||
519 | #ifdef CRAY | ||
520 | # define FP(k) (wO_L4[ (L >> (k)) & 15 ] << 1 | wO_L4[ (R >> (k)) & 15 ]) | ||
521 | #else | ||
522 | # define FP(k) (ap[ (L >> (k)) & 15 ] << 1 | ap[ (R >> (k)) & 15 ]) | ||
523 | |||
524 | ap = wO_L4; | ||
525 | #endif | ||
526 | |||
527 | t = FP(0) | (FP(8) | (FP(16) | (FP(24) << 2)) << 2) << 2; | ||
528 | R = FP(4) | (FP(12) | (FP(20) | (FP(28) << 2)) << 2) << 2; | ||
529 | L = t; | ||
530 | } | ||
531 | { | ||
532 | register word32 t; | ||
533 | register char *bp; | ||
534 | |||
535 | bp = &block[7]; | ||
536 | t = R; | ||
537 | *bp = t & 255; | ||
538 | *--bp = (t >>= 8) & 255; | ||
539 | *--bp = (t >>= 8) & 255; | ||
540 | *--bp = (t >> 8) & 255; | ||
541 | t = L; | ||
542 | *--bp = t & 255; | ||
543 | *--bp = (t >>= 8) & 255; | ||
544 | *--bp = (t >>= 8) & 255; | ||
545 | *--bp = (t >> 8) & 255; | ||
546 | } | ||
547 | } | ||
548 | |||
diff --git a/vendor/md5/src/des56.def b/vendor/md5/src/des56.def new file mode 100644 index 00000000..c0493a73 --- /dev/null +++ b/vendor/md5/src/des56.def | |||
@@ -0,0 +1,5 @@ | |||
1 | LIBRARY des56.dll | ||
2 | DESCRIPTION "DES56" | ||
3 | VERSION 1.3 | ||
4 | EXPORTS | ||
5 | luaopen_des56 | ||
diff --git a/vendor/md5/src/des56.h b/vendor/md5/src/des56.h new file mode 100755 index 00000000..88e5f137 --- /dev/null +++ b/vendor/md5/src/des56.h | |||
@@ -0,0 +1,77 @@ | |||
1 | #ifndef DES56_H | ||
2 | #define DES56_H 1 | ||
3 | /* | ||
4 | * Fast implementation of the DES, as described in the Federal Register, | ||
5 | * Vol. 40, No. 52, p. 12134, March 17, 1975. | ||
6 | * | ||
7 | * Stuart Levy, Minnesota Supercomputer Center, April 1988. | ||
8 | * Currently (2007) slevy@ncsa.uiuc.edu | ||
9 | * NCSA, University of Illinois Urbana-Champaign | ||
10 | * | ||
11 | * Calling sequence: | ||
12 | * | ||
13 | * typedef unsigned long keysched[32]; | ||
14 | * | ||
15 | * fsetkey(key, keysched) / * Converts a DES key to a "key schedule" * / | ||
16 | * unsigned char key[8]; | ||
17 | * keysched *ks; | ||
18 | * | ||
19 | * fencrypt(block, decrypt, keysched) / * En/decrypts one 64-bit block * / | ||
20 | * unsigned char block[8]; / * data, en/decrypted in place * / | ||
21 | * int decrypt; / * 0=>encrypt, 1=>decrypt * / | ||
22 | * keysched *ks; / * key schedule, as set by fsetkey * / | ||
23 | * | ||
24 | * Key and data block representation: | ||
25 | * The 56-bit key (bits 1..64 including "parity" bits 8, 16, 24, ..., 64) | ||
26 | * and the 64-bit data block (bits 1..64) | ||
27 | * are each stored in arrays of 8 bytes. | ||
28 | * Following the NBS numbering, the MSB has the bit number 1, so | ||
29 | * key[0] = 128*bit1 + 64*bit2 + ... + 1*bit8, ... through | ||
30 | * key[7] = 128*bit57 + 64*bit58 + ... + 1*bit64. | ||
31 | * In the key, "parity" bits are not checked; their values are ignored. | ||
32 | * | ||
33 | */ | ||
34 | |||
35 | /* | ||
36 | =============================================================================== | ||
37 | License | ||
38 | |||
39 | des56.c is licensed under the terms of the MIT license reproduced below. | ||
40 | This means that des56.c is free software and can be used for both academic | ||
41 | and commercial purposes at absolutely no cost. | ||
42 | =============================================================================== | ||
43 | Copyright (C) 1988 Stuart Levy | ||
44 | |||
45 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
46 | of this software and associated documentation files (the "Software"), to deal | ||
47 | in the Software without restriction, including without limitation the rights | ||
48 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
49 | copies of the Software, and to permit persons to whom the Software is | ||
50 | furnished to do so, subject to the following conditions: | ||
51 | |||
52 | The above copyright notice and this permission notice shall be included in | ||
53 | all copies or substantial portions of the Software. | ||
54 | |||
55 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
56 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
57 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
58 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
59 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
60 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
61 | THE SOFTWARE. | ||
62 | */ | ||
63 | |||
64 | typedef unsigned long word32; | ||
65 | typedef unsigned char tiny; | ||
66 | |||
67 | typedef struct keysched { | ||
68 | struct keystage { | ||
69 | word32 h, l; | ||
70 | } KS[16]; | ||
71 | } keysched; | ||
72 | |||
73 | extern void fsetkey(char key[8], keysched *ks); | ||
74 | |||
75 | extern void fencrypt(char block[8], int decrypt, keysched *ks); | ||
76 | |||
77 | #endif /*DES56_H*/ | ||
diff --git a/vendor/md5/src/ldes56.c b/vendor/md5/src/ldes56.c new file mode 100644 index 00000000..8c29ec1d --- /dev/null +++ b/vendor/md5/src/ldes56.c | |||
@@ -0,0 +1,152 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | |||
4 | #include "des56.h" | ||
5 | |||
6 | #include "lua.h" | ||
7 | #include "lauxlib.h" | ||
8 | |||
9 | #include "compat-5.2.h" | ||
10 | #include "ldes56.h" | ||
11 | |||
12 | static int des56_decrypt( lua_State *L ) | ||
13 | { | ||
14 | char* decypheredText; | ||
15 | keysched KS; | ||
16 | int rel_index, abs_index; | ||
17 | size_t cypherlen; | ||
18 | const char *cypheredText = | ||
19 | luaL_checklstring( L, 1, &cypherlen ); | ||
20 | const char *key = luaL_optstring( L, 2, NULL ); | ||
21 | int padinfo; | ||
22 | |||
23 | padinfo = cypheredText[cypherlen-1]; | ||
24 | cypherlen--; | ||
25 | |||
26 | /* Aloca array */ | ||
27 | decypheredText = | ||
28 | (char *) malloc( (cypherlen+1) * sizeof(char)); | ||
29 | if(decypheredText == NULL) { | ||
30 | lua_pushstring(L, "Error decrypting file. Not enough memory."); | ||
31 | lua_error(L); | ||
32 | } | ||
33 | |||
34 | /* Inicia decifragem */ | ||
35 | if (key && strlen(key) >= 8) | ||
36 | { | ||
37 | char k[8]; | ||
38 | int i; | ||
39 | |||
40 | for (i=0; i<8; i++) | ||
41 | k[i] = (unsigned char)key[i]; | ||
42 | fsetkey(k, &KS); | ||
43 | } else { | ||
44 | lua_pushstring(L, "Error decrypting file. Invalid key."); | ||
45 | lua_error(L); | ||
46 | } | ||
47 | |||
48 | rel_index = 0; | ||
49 | abs_index = 0; | ||
50 | |||
51 | while (abs_index < (int) cypherlen) | ||
52 | { | ||
53 | decypheredText[abs_index] = cypheredText[abs_index]; | ||
54 | abs_index++; | ||
55 | rel_index++; | ||
56 | if( rel_index == 8 ) | ||
57 | { | ||
58 | rel_index = 0; | ||
59 | fencrypt(&(decypheredText[abs_index - 8]), 1, &KS); | ||
60 | } | ||
61 | } | ||
62 | decypheredText[abs_index] = 0; | ||
63 | |||
64 | lua_pushlstring(L, decypheredText, (abs_index-padinfo)); | ||
65 | free( decypheredText ); | ||
66 | return 1; | ||
67 | } | ||
68 | |||
69 | static int des56_crypt( lua_State *L ) | ||
70 | { | ||
71 | char *cypheredText; | ||
72 | keysched KS; | ||
73 | int rel_index, pad, abs_index; | ||
74 | size_t plainlen; | ||
75 | const char *plainText = luaL_checklstring( L, 1, &plainlen ); | ||
76 | const char *key = luaL_optstring( L, 2, NULL ); | ||
77 | |||
78 | cypheredText = (char *) malloc( (plainlen+8) * sizeof(char)); | ||
79 | if(cypheredText == NULL) { | ||
80 | lua_pushstring(L, "Error encrypting file. Not enough memory."); | ||
81 | lua_error(L); | ||
82 | } | ||
83 | |||
84 | if (key && strlen(key) >= 8) | ||
85 | { | ||
86 | char k[8]; | ||
87 | int i; | ||
88 | |||
89 | for (i=0; i<8; i++) | ||
90 | k[i] = (unsigned char)key[i]; | ||
91 | fsetkey(k, &KS); | ||
92 | } else { | ||
93 | lua_pushstring(L, "Error encrypting file. Invalid key."); | ||
94 | lua_error(L); | ||
95 | } | ||
96 | |||
97 | rel_index = 0; | ||
98 | abs_index = 0; | ||
99 | while (abs_index < (int) plainlen) { | ||
100 | cypheredText[abs_index] = plainText[abs_index]; | ||
101 | abs_index++; | ||
102 | rel_index++; | ||
103 | if( rel_index == 8 ) { | ||
104 | rel_index = 0; | ||
105 | fencrypt(&(cypheredText[abs_index - 8]), 0, &KS); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | pad = 0; | ||
110 | if(rel_index != 0) { /* Pads remaining bytes with zeroes */ | ||
111 | while(rel_index < 8) | ||
112 | { | ||
113 | pad++; | ||
114 | cypheredText[abs_index++] = 0; | ||
115 | rel_index++; | ||
116 | } | ||
117 | fencrypt(&(cypheredText[abs_index - 8]), 0, &KS); | ||
118 | } | ||
119 | cypheredText[abs_index] = pad; | ||
120 | |||
121 | lua_pushlstring( L, cypheredText, abs_index+1 ); | ||
122 | free( cypheredText ); | ||
123 | return 1; | ||
124 | } | ||
125 | |||
126 | /* | ||
127 | ** Assumes the table is on top of the stack. | ||
128 | */ | ||
129 | static void set_info (lua_State *L) { | ||
130 | lua_pushliteral (L, "_COPYRIGHT"); | ||
131 | lua_pushliteral (L, "Copyright (C) 2007-2019 PUC-Rio"); | ||
132 | lua_settable (L, -3); | ||
133 | lua_pushliteral (L, "_DESCRIPTION"); | ||
134 | lua_pushliteral (L, "DES 56 cryptographic facilities for Lua"); | ||
135 | lua_settable (L, -3); | ||
136 | lua_pushliteral (L, "_VERSION"); | ||
137 | lua_pushliteral (L, "DES56 1.3"); | ||
138 | lua_settable (L, -3); | ||
139 | } | ||
140 | |||
141 | static const struct luaL_Reg des56lib[] = { | ||
142 | {"crypt", des56_crypt}, | ||
143 | {"decrypt", des56_decrypt}, | ||
144 | {NULL, NULL}, | ||
145 | }; | ||
146 | |||
147 | int luaopen_des56 (lua_State *L) { | ||
148 | lua_newtable(L); | ||
149 | luaL_setfuncs(L, des56lib, 0); | ||
150 | set_info (L); | ||
151 | return 1; | ||
152 | } | ||
diff --git a/vendor/md5/src/ldes56.h b/vendor/md5/src/ldes56.h new file mode 100755 index 00000000..8c2e86a8 --- /dev/null +++ b/vendor/md5/src/ldes56.h | |||
@@ -0,0 +1 @@ | |||
int luaopen_des56 (lua_State *L); | |||
diff --git a/vendor/md5/src/md5.c b/vendor/md5/src/md5.c new file mode 100755 index 00000000..f35c8e14 --- /dev/null +++ b/vendor/md5/src/md5.c | |||
@@ -0,0 +1,262 @@ | |||
1 | /** | ||
2 | * $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $ | ||
3 | * Hash function MD5 | ||
4 | * @author Marcela Ozorio Suarez, Roberto I. | ||
5 | */ | ||
6 | |||
7 | |||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <string.h> | ||
11 | #include <time.h> | ||
12 | |||
13 | #include "md5.h" | ||
14 | |||
15 | |||
16 | #define WORD 32 | ||
17 | #define MASK 0xFFFFFFFF | ||
18 | |||
19 | |||
20 | /** | ||
21 | * md5 hash function. | ||
22 | * @param message: aribtary string. | ||
23 | * @param len: message length. | ||
24 | * @param output: buffer to receive the hash value. Its size must be | ||
25 | * (at least) HASHSIZE. | ||
26 | */ | ||
27 | void md5 (const char *message, size_t len, char output[HASHSIZE]); | ||
28 | |||
29 | /** | ||
30 | * init a new md5 calculate context | ||
31 | * @param m a uninitialized md5_t type context | ||
32 | */ | ||
33 | void md5_init (md5_t *m); | ||
34 | |||
35 | /** | ||
36 | * update message to md5 context | ||
37 | * @param m a initialized md5_t type context | ||
38 | * @param message aribtary string | ||
39 | * @param len message length | ||
40 | * @return true if update completed, means len is not the multiples of 64. | ||
41 | * false if you can continue update. | ||
42 | */ | ||
43 | int md5_update (md5_t *m, const char *message, size_t len); | ||
44 | |||
45 | /** | ||
46 | * finish md5 calculate. | ||
47 | * @param m a md5_type context which previous md5_update on it return true. | ||
48 | * @param output buffer to receive the hash value. its size must be | ||
49 | * (at least) HASHSIZE. | ||
50 | */ | ||
51 | void md5_finish (md5_t *m, char output[HASHSIZE]); | ||
52 | |||
53 | /* | ||
54 | ** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32. | ||
55 | ** Os bits sao deslocados de 'num' posicoes | ||
56 | */ | ||
57 | #define rotate(D, num) (D<<num) | (D>>(WORD-num)) | ||
58 | |||
59 | /*Macros que definem operacoes relizadas pelo algoritmo md5 */ | ||
60 | #define F(x, y, z) (((x) & (y)) | ((~(x)) & (z))) | ||
61 | #define G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) | ||
62 | #define H(x, y, z) ((x) ^ (y) ^ (z)) | ||
63 | #define I(x, y, z) ((y) ^ ((x) | (~(z)))) | ||
64 | |||
65 | |||
66 | /*vetor de numeros utilizados pelo algoritmo md5 para embaralhar bits */ | ||
67 | static const WORD32 T[64]={ | ||
68 | 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, | ||
69 | 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, | ||
70 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, | ||
71 | 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, | ||
72 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, | ||
73 | 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | ||
74 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, | ||
75 | 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, | ||
76 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, | ||
77 | 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, | ||
78 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, | ||
79 | 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, | ||
80 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, | ||
81 | 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, | ||
82 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, | ||
83 | 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 | ||
84 | }; | ||
85 | |||
86 | |||
87 | static void word32tobytes (const WORD32 *input, char *output) { | ||
88 | int j = 0; | ||
89 | while (j<4*4) { | ||
90 | WORD32 v = *input++; | ||
91 | output[j++] = (char)(v & 0xff); v >>= 8; | ||
92 | output[j++] = (char)(v & 0xff); v >>= 8; | ||
93 | output[j++] = (char)(v & 0xff); v >>= 8; | ||
94 | output[j++] = (char)(v & 0xff); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | |||
99 | static void inic_digest(WORD32 *d) { | ||
100 | d[0] = 0x67452301; | ||
101 | d[1] = 0xEFCDAB89; | ||
102 | d[2] = 0x98BADCFE; | ||
103 | d[3] = 0x10325476; | ||
104 | } | ||
105 | |||
106 | |||
107 | /*funcao que implemeta os quatro passos principais do algoritmo MD5 */ | ||
108 | static void digest(const WORD32 *m, WORD32 *d) { | ||
109 | int j; | ||
110 | /*MD5 PASSO1 */ | ||
111 | for (j=0; j<4*4; j+=4) { | ||
112 | d[0] = d[0]+ F(d[1], d[2], d[3])+ m[j] + T[j]; d[0]=rotate(d[0], 7); | ||
113 | d[0]+=d[1]; | ||
114 | d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12); | ||
115 | d[3]+=d[0]; | ||
116 | d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17); | ||
117 | d[2]+=d[3]; | ||
118 | d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22); | ||
119 | d[1]+=d[2]; | ||
120 | } | ||
121 | /*MD5 PASSO2 */ | ||
122 | for (j=0; j<4*4; j+=4) { | ||
123 | d[0] = d[0]+ G(d[1], d[2], d[3])+ m[(5*j+1)&0x0f] + T[(j-1)+17]; | ||
124 | d[0] = rotate(d[0],5); | ||
125 | d[0]+=d[1]; | ||
126 | d[3] = d[3]+ G(d[0], d[1], d[2])+ m[((5*(j+1)+1)&0x0f)] + T[(j+0)+17]; | ||
127 | d[3] = rotate(d[3], 9); | ||
128 | d[3]+=d[0]; | ||
129 | d[2] = d[2]+ G(d[3], d[0], d[1])+ m[((5*(j+2)+1)&0x0f)] + T[(j+1)+17]; | ||
130 | d[2] = rotate(d[2], 14); | ||
131 | d[2]+=d[3]; | ||
132 | d[1] = d[1]+ G(d[2], d[3], d[0])+ m[((5*(j+3)+1)&0x0f)] + T[(j+2)+17]; | ||
133 | d[1] = rotate(d[1], 20); | ||
134 | d[1]+=d[2]; | ||
135 | } | ||
136 | /*MD5 PASSO3 */ | ||
137 | for (j=0; j<4*4; j+=4) { | ||
138 | d[0] = d[0]+ H(d[1], d[2], d[3])+ m[(3*j+5)&0x0f] + T[(j-1)+33]; | ||
139 | d[0] = rotate(d[0], 4); | ||
140 | d[0]+=d[1]; | ||
141 | d[3] = d[3]+ H(d[0], d[1], d[2])+ m[(3*(j+1)+5)&0x0f] + T[(j+0)+33]; | ||
142 | d[3] = rotate(d[3], 11); | ||
143 | d[3]+=d[0]; | ||
144 | d[2] = d[2]+ H(d[3], d[0], d[1])+ m[(3*(j+2)+5)&0x0f] + T[(j+1)+33]; | ||
145 | d[2] = rotate(d[2], 16); | ||
146 | d[2]+=d[3]; | ||
147 | d[1] = d[1]+ H(d[2], d[3], d[0])+ m[(3*(j+3)+5)&0x0f] + T[(j+2)+33]; | ||
148 | d[1] = rotate(d[1], 23); | ||
149 | d[1]+=d[2]; | ||
150 | } | ||
151 | /*MD5 PASSO4 */ | ||
152 | for (j=0; j<4*4; j+=4) { | ||
153 | d[0] = d[0]+ I(d[1], d[2], d[3])+ m[(7*j)&0x0f] + T[(j-1)+49]; | ||
154 | d[0] = rotate(d[0], 6); | ||
155 | d[0]+=d[1]; | ||
156 | d[3] = d[3]+ I(d[0], d[1], d[2])+ m[(7*(j+1))&0x0f] + T[(j+0)+49]; | ||
157 | d[3] = rotate(d[3], 10); | ||
158 | d[3]+=d[0]; | ||
159 | d[2] = d[2]+ I(d[3], d[0], d[1])+ m[(7*(j+2))&0x0f] + T[(j+1)+49]; | ||
160 | d[2] = rotate(d[2], 15); | ||
161 | d[2]+=d[3]; | ||
162 | d[1] = d[1]+ I(d[2], d[3], d[0])+ m[(7*(j+3))&0x0f] + T[(j+2)+49]; | ||
163 | d[1] = rotate(d[1], 21); | ||
164 | d[1]+=d[2]; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | |||
169 | static void bytestoword32 (WORD32 *x, const char *pt) { | ||
170 | int i; | ||
171 | for (i=0; i<16; i++) { | ||
172 | int j=i*4; | ||
173 | x[i] = (((WORD32)(unsigned char)pt[j+3] << 8 | | ||
174 | (WORD32)(unsigned char)pt[j+2]) << 8 | | ||
175 | (WORD32)(unsigned char)pt[j+1]) << 8 | | ||
176 | (WORD32)(unsigned char)pt[j]; | ||
177 | } | ||
178 | |||
179 | } | ||
180 | |||
181 | |||
182 | static void put_length(WORD32 *x, long len) { | ||
183 | /* in bits! */ | ||
184 | x[14] = (WORD32)((len<<3) & MASK); | ||
185 | x[15] = (WORD32)(len>>(32-3) & 0x7); | ||
186 | } | ||
187 | |||
188 | |||
189 | /* | ||
190 | ** returned status: | ||
191 | * 0 - normal message (full 64 bytes) | ||
192 | * 1 - enough room for 0x80, but not for message length (two 4-byte words) | ||
193 | * 2 - enough room for 0x80 plus message length (at least 9 bytes free) | ||
194 | */ | ||
195 | static int converte (WORD32 *x, const char *pt, int num, int old_status) { | ||
196 | int new_status = 0; | ||
197 | char buff[64]; | ||
198 | if (num<64) { | ||
199 | memcpy(buff, pt, num); /* to avoid changing original string */ | ||
200 | memset(buff+num, 0, 64-num); | ||
201 | if (old_status == 0) | ||
202 | buff[num] = '\200'; | ||
203 | new_status = 1; | ||
204 | pt = buff; | ||
205 | } | ||
206 | bytestoword32(x, pt); | ||
207 | if (num <= (64 - 9)) | ||
208 | new_status = 2; | ||
209 | return new_status; | ||
210 | } | ||
211 | |||
212 | |||
213 | void md5 (const char *message, size_t len, char output[HASHSIZE]) { | ||
214 | WORD32 d[4]; | ||
215 | int status = 0; | ||
216 | long i = 0; | ||
217 | inic_digest(d); | ||
218 | while (status != 2) { | ||
219 | WORD32 d_old[4]; | ||
220 | WORD32 wbuff[16]; | ||
221 | int numbytes = (len-i >= 64) ? 64 : len-i; | ||
222 | /*salva os valores do vetor digest*/ | ||
223 | d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3]; | ||
224 | status = converte(wbuff, message+i, numbytes, status); | ||
225 | if (status == 2) put_length(wbuff, len); | ||
226 | digest(wbuff, d); | ||
227 | d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3]; | ||
228 | i += numbytes; | ||
229 | } | ||
230 | word32tobytes(d, output); | ||
231 | } | ||
232 | |||
233 | void md5_init(md5_t *m) { | ||
234 | inic_digest(m->d); | ||
235 | m->len = 0; | ||
236 | } | ||
237 | |||
238 | int md5_update(md5_t *m, const char *message, size_t len) { | ||
239 | WORD32 *d = m->d; | ||
240 | size_t addlen = m->len; | ||
241 | int status = 0, i = 0; | ||
242 | while (status != 2) { | ||
243 | WORD32 d_old[4]; | ||
244 | WORD32 wbuff[16]; | ||
245 | int numbytes = (len-i >= 64) ? 64 : len-i; | ||
246 | if (status != 1 && numbytes == 0 && len != 0) | ||
247 | break; | ||
248 | /*salva os valores do vetor digest*/ | ||
249 | d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3]; | ||
250 | status = converte(wbuff, message+i, numbytes, status); | ||
251 | if (status == 2) put_length(wbuff, addlen+len); | ||
252 | digest(wbuff, d); | ||
253 | d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3]; | ||
254 | i += numbytes; | ||
255 | } | ||
256 | m->len += len; | ||
257 | return status == 2; | ||
258 | } | ||
259 | |||
260 | void md5_finish (md5_t *m, char output[HASHSIZE]) { | ||
261 | word32tobytes(m->d, output); | ||
262 | } | ||
diff --git a/vendor/md5/src/md5.def b/vendor/md5/src/md5.def new file mode 100644 index 00000000..8ab09e76 --- /dev/null +++ b/vendor/md5/src/md5.def | |||
@@ -0,0 +1,5 @@ | |||
1 | LIBRARY core.dll | ||
2 | DESCRIPTION "LuaMD5" | ||
3 | VERSION 1.3 | ||
4 | EXPORTS | ||
5 | luaopen_md5_core | ||
diff --git a/vendor/md5/src/md5.h b/vendor/md5/src/md5.h new file mode 100755 index 00000000..522ede81 --- /dev/null +++ b/vendor/md5/src/md5.h | |||
@@ -0,0 +1,40 @@ | |||
1 | /** | ||
2 | * $Id: md5.h,v 1.2 2006/03/03 15:04:49 tomas Exp $ | ||
3 | * Cryptographic module for Lua. | ||
4 | * @author Roberto Ierusalimschy | ||
5 | */ | ||
6 | |||
7 | |||
8 | #ifndef md5_h | ||
9 | #define md5_h | ||
10 | |||
11 | #include <lua.h> | ||
12 | #include <stddef.h> | ||
13 | |||
14 | |||
15 | #define HASHSIZE 16 | ||
16 | |||
17 | #if __STDC_VERSION__ >= 199901L | ||
18 | #include <stdint.h> | ||
19 | typedef uint32_t WORD32; | ||
20 | #else | ||
21 | /* static assert that int equal or greater than 32bit. */ | ||
22 | typedef char static_assert_sizeof_int | ||
23 | [sizeof(unsigned int) >= 4 ? 1 : -1]; | ||
24 | typedef unsigned int WORD32; | ||
25 | #endif | ||
26 | |||
27 | typedef struct md5_t { | ||
28 | WORD32 d[4]; | ||
29 | size_t len; | ||
30 | } md5_t; | ||
31 | |||
32 | void md5_init (md5_t *m); | ||
33 | int md5_update (md5_t *m, const char *message, size_t len); | ||
34 | void md5_finish (md5_t *m, char output[HASHSIZE]); | ||
35 | void md5 (const char *message, size_t len, char output[HASHSIZE]); | ||
36 | |||
37 | LUALIB_API int luaopen_md5_core (lua_State *L); | ||
38 | |||
39 | |||
40 | #endif | ||
diff --git a/vendor/md5/src/md5.lua b/vendor/md5/src/md5.lua new file mode 100644 index 00000000..4eeda2dd --- /dev/null +++ b/vendor/md5/src/md5.lua | |||
@@ -0,0 +1,26 @@ | |||
1 | ---------------------------------------------------------------------------- | ||
2 | -- $Id: md5.lua,v 1.4 2006/08/21 19:24:21 carregal Exp $ | ||
3 | ---------------------------------------------------------------------------- | ||
4 | |||
5 | local core | ||
6 | local string = string or require"string" | ||
7 | if string.find(_VERSION, "Lua 5.0") then | ||
8 | local cpath = os.getenv"LUA_CPATH" or "/usr/local/lib/lua/5.0/" | ||
9 | core = loadlib(cpath.."md5/core.so", "luaopen_md5_core")() | ||
10 | else | ||
11 | core = require"md5.core" | ||
12 | end | ||
13 | |||
14 | |||
15 | ---------------------------------------------------------------------------- | ||
16 | -- @param k String with original message. | ||
17 | -- @return String with the md5 hash value converted to hexadecimal digits | ||
18 | |||
19 | function core.sumhexa (k) | ||
20 | k = core.sum(k) | ||
21 | return (string.gsub(k, ".", function (c) | ||
22 | return string.format("%02x", string.byte(c)) | ||
23 | end)) | ||
24 | end | ||
25 | |||
26 | return core | ||
diff --git a/vendor/md5/src/md5lib.c b/vendor/md5/src/md5lib.c new file mode 100644 index 00000000..19abc85f --- /dev/null +++ b/vendor/md5/src/md5lib.c | |||
@@ -0,0 +1,200 @@ | |||
1 | /** | ||
2 | * $Id: md5lib.c,v 1.10 2008/05/12 20:51:27 carregal Exp $ | ||
3 | * Cryptographic and Hash functions for Lua | ||
4 | * @author Roberto Ierusalimschy | ||
5 | */ | ||
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 "md5.h" | ||
16 | #include "compat-5.2.h" | ||
17 | |||
18 | |||
19 | /** | ||
20 | * Hash function. Returns a hash for a given string. | ||
21 | * @param message: arbitrary binary string. | ||
22 | * @return A 128-bit hash string. | ||
23 | */ | ||
24 | static int lmd5 (lua_State *L) { | ||
25 | char buff[16]; | ||
26 | size_t l; | ||
27 | const char *message = luaL_checklstring(L, 1, &l); | ||
28 | md5(message, l, buff); | ||
29 | lua_pushlstring(L, buff, 16L); | ||
30 | return 1; | ||
31 | } | ||
32 | |||
33 | |||
34 | /** | ||
35 | * X-Or. Does a bit-a-bit exclusive-or of two strings. | ||
36 | * @param s1: arbitrary binary string. | ||
37 | * @param s2: arbitrary binary string with same length as s1. | ||
38 | * @return a binary string with same length as s1 and s2, | ||
39 | * where each bit is the exclusive-or of the corresponding bits in s1-s2. | ||
40 | */ | ||
41 | static int ex_or (lua_State *L) { | ||
42 | size_t l1, l2; | ||
43 | const char *s1 = luaL_checklstring(L, 1, &l1); | ||
44 | const char *s2 = luaL_checklstring(L, 2, &l2); | ||
45 | luaL_Buffer b; | ||
46 | luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" ); | ||
47 | luaL_buffinit(L, &b); | ||
48 | while (l1--) luaL_addchar(&b, (*s1++)^(*s2++)); | ||
49 | luaL_pushresult(&b); | ||
50 | return 1; | ||
51 | } | ||
52 | |||
53 | |||
54 | static void checkseed (lua_State *L) { | ||
55 | if (lua_isnone(L, 3)) { /* no seed? */ | ||
56 | time_t tm = time(NULL); /* for `random' seed */ | ||
57 | lua_pushlstring(L, (char *)&tm, sizeof(tm)); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | |||
62 | #define MAXKEY 256 | ||
63 | #define BLOCKSIZE 16 | ||
64 | |||
65 | |||
66 | |||
67 | static int initblock (lua_State *L, const char *seed, int lseed, char *block) { | ||
68 | size_t lkey; | ||
69 | const char *key = luaL_checklstring(L, 2, &lkey); | ||
70 | if (lkey > MAXKEY) | ||
71 | luaL_error(L, "key too long (> %d)", MAXKEY); | ||
72 | memset(block, 0, BLOCKSIZE); | ||
73 | memcpy(block, seed, lseed); | ||
74 | memcpy(block+BLOCKSIZE, key, lkey); | ||
75 | return (int)lkey+BLOCKSIZE; | ||
76 | } | ||
77 | |||
78 | |||
79 | static void codestream (lua_State *L, const char *msg, size_t lmsg, | ||
80 | char *block, int lblock) { | ||
81 | luaL_Buffer b; | ||
82 | luaL_buffinit(L, &b); | ||
83 | while (lmsg > 0) { | ||
84 | char code[BLOCKSIZE]; | ||
85 | int i; | ||
86 | md5(block, lblock, code); | ||
87 | for (i=0; i<BLOCKSIZE && lmsg > 0; i++, lmsg--) | ||
88 | code[i] ^= *msg++; | ||
89 | luaL_addlstring(&b, code, i); | ||
90 | memcpy(block, code, i); /* update seed */ | ||
91 | } | ||
92 | luaL_pushresult(&b); | ||
93 | } | ||
94 | |||
95 | |||
96 | static void decodestream (lua_State *L, const char *cypher, size_t lcypher, | ||
97 | char *block, int lblock) { | ||
98 | luaL_Buffer b; | ||
99 | luaL_buffinit(L, &b); | ||
100 | while (lcypher > 0) { | ||
101 | char code[BLOCKSIZE]; | ||
102 | int i; | ||
103 | md5(block, lblock, code); /* update seed */ | ||
104 | for (i=0; i<BLOCKSIZE && lcypher > 0; i++, lcypher--) | ||
105 | code[i] ^= *cypher++; | ||
106 | luaL_addlstring(&b, code, i); | ||
107 | memcpy(block, cypher-i, i); | ||
108 | } | ||
109 | luaL_pushresult(&b); | ||
110 | } | ||
111 | |||
112 | |||
113 | /** | ||
114 | * Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback | ||
115 | * mode). | ||
116 | * @param message: arbitrary binary string to be encrypted. | ||
117 | * @param key: arbitrary binary string to be used as a key. | ||
118 | * @param [seed]: optional arbitrary binary string to be used as a seed. | ||
119 | * if no seed is provided, the function uses the result of | ||
120 | * <code>time()</code> as a seed. | ||
121 | * @return The cyphertext (as a binary string). | ||
122 | */ | ||
123 | static int crypt (lua_State *L) { | ||
124 | size_t lmsg; | ||
125 | const char *msg = luaL_checklstring(L, 1, &lmsg); | ||
126 | size_t lseed; | ||
127 | const char *seed; | ||
128 | int lblock; | ||
129 | char block[BLOCKSIZE+MAXKEY]; | ||
130 | checkseed(L); | ||
131 | seed = luaL_checklstring(L, 3, &lseed); | ||
132 | if (lseed > BLOCKSIZE) | ||
133 | luaL_error(L, "seed too long (> %d)", BLOCKSIZE); | ||
134 | /* put seed and seed length at the beginning of result */ | ||
135 | block[0] = (char)lseed; | ||
136 | memcpy(block+1, seed, lseed); | ||
137 | lua_pushlstring(L, block, lseed+1); /* to concat with result */ | ||
138 | lblock = initblock(L, seed, lseed, block); | ||
139 | codestream(L, msg, lmsg, block, lblock); | ||
140 | lua_concat(L, 2); | ||
141 | return 1; | ||
142 | } | ||
143 | |||
144 | |||
145 | /** | ||
146 | * Decrypts a string. For any message, key, and seed, we have that | ||
147 | * <code>decrypt(crypt(msg, key, seed), key) == msg</code>. | ||
148 | * @param cyphertext: message to be decrypted (this must be the result of | ||
149 | a previous call to <code>crypt</code>. | ||
150 | * @param key: arbitrary binary string to be used as a key. | ||
151 | * @return The plaintext. | ||
152 | */ | ||
153 | static int decrypt (lua_State *L) { | ||
154 | size_t lcyphertext; | ||
155 | const char *cyphertext = luaL_checklstring(L, 1, &lcyphertext); | ||
156 | size_t lseed = cyphertext[0]; | ||
157 | const char *seed = cyphertext+1; | ||
158 | int lblock; | ||
159 | char block[BLOCKSIZE+MAXKEY]; | ||
160 | luaL_argcheck(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1, | ||
161 | "invalid cyphered string"); | ||
162 | cyphertext += lseed+1; | ||
163 | lcyphertext -= lseed+1; | ||
164 | lblock = initblock(L, seed, lseed, block); | ||
165 | decodestream(L, cyphertext, lcyphertext, block, lblock); | ||
166 | return 1; | ||
167 | } | ||
168 | |||
169 | |||
170 | /* | ||
171 | ** Assumes the table is on top of the stack. | ||
172 | */ | ||
173 | static void set_info (lua_State *L) { | ||
174 | lua_pushliteral (L, "_COPYRIGHT"); | ||
175 | lua_pushliteral (L, "Copyright (C) 2003-2019 PUC-Rio"); | ||
176 | lua_settable (L, -3); | ||
177 | lua_pushliteral (L, "_DESCRIPTION"); | ||
178 | lua_pushliteral (L, "Checksum facilities for Lua"); | ||
179 | lua_settable (L, -3); | ||
180 | lua_pushliteral (L, "_VERSION"); | ||
181 | lua_pushliteral (L, "MD5 1.3"); | ||
182 | lua_settable (L, -3); | ||
183 | } | ||
184 | |||
185 | |||
186 | static struct luaL_Reg md5lib[] = { | ||
187 | {"sum", lmd5}, | ||
188 | {"exor", ex_or}, | ||
189 | {"crypt", crypt}, | ||
190 | {"decrypt", decrypt}, | ||
191 | {NULL, NULL} | ||
192 | }; | ||
193 | |||
194 | |||
195 | int luaopen_md5_core (lua_State *L) { | ||
196 | lua_newtable(L); | ||
197 | luaL_setfuncs(L, md5lib, 0); | ||
198 | set_info (L); | ||
199 | return 1; | ||
200 | } | ||