]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/md5.lua
add lf securakey
[proxmark3-svn] / client / lualibs / md5.lua
1 local md5 = {
2 _VERSION = "md5.lua 0.5.0",
3 _DESCRIPTION = "MD5 computation in Lua (5.1)",
4 _URL = "https://github.com/kikito/md5.lua",
5 _LICENSE = [[
6 MIT LICENSE
7
8 Copyright (c) 2013 Enrique GarcĂ­a Cota + Adam Baldwin + hanzao + Equi 4 Software
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 ]]
29 }
30
31 -- bit lib implementions
32
33 local floor, abs, max = math.floor, math.abs, math.max
34 local char, byte, format, rep, sub =
35 string.char, string.byte, string.format, string.rep, string.sub
36
37 local function check_int(n)
38 -- checking not float
39 if(n - floor(n) > 0) then
40 error("trying to use bitwise operation on non-integer!")
41 end
42 end
43
44 local function tbl2number(tbl)
45 local n = #tbl
46
47 local rslt = 0
48 local power = 1
49 for i = 1, n do
50 rslt = rslt + tbl[i]*power
51 power = power*2
52 end
53
54 return rslt
55 end
56
57 local function expand(tbl_m, tbl_n)
58 local big = {}
59 local small = {}
60 if(#tbl_m > #tbl_n) then
61 big = tbl_m
62 small = tbl_n
63 else
64 big = tbl_n
65 small = tbl_m
66 end
67 -- expand small
68 for i = #small + 1, #big do
69 small[i] = 0
70 end
71
72 end
73
74 local to_bits -- needs to be declared before bit_not
75
76 local function bit_not(n)
77 local tbl = to_bits(n)
78 local size = max(#tbl, 32)
79 for i = 1, size do
80 if(tbl[i] == 1) then
81 tbl[i] = 0
82 else
83 tbl[i] = 1
84 end
85 end
86 return tbl2number(tbl)
87 end
88
89 -- defined as local above
90 to_bits = function (n)
91 check_int(n)
92 if(n < 0) then
93 -- negative
94 return to_bits(bit_not(abs(n)) + 1)
95 end
96 -- to bits table
97 local tbl = {}
98 local cnt = 1
99 while (n > 0) do
100 local last = math.fmod(n,2)
101 if(last == 1) then
102 tbl[cnt] = 1
103 else
104 tbl[cnt] = 0
105 end
106 n = (n-last)/2
107 cnt = cnt + 1
108 end
109
110 return tbl
111 end
112
113 local function bit_or(m, n)
114 local tbl_m = to_bits(m)
115 local tbl_n = to_bits(n)
116 expand(tbl_m, tbl_n)
117
118 local tbl = {}
119 local rslt = max(#tbl_m, #tbl_n)
120 for i = 1, rslt do
121 if(tbl_m[i]== 0 and tbl_n[i] == 0) then
122 tbl[i] = 0
123 else
124 tbl[i] = 1
125 end
126 end
127
128 return tbl2number(tbl)
129 end
130
131 local function bit_and(m, n)
132 local tbl_m = to_bits(m)
133 local tbl_n = to_bits(n)
134 expand(tbl_m, tbl_n)
135
136 local tbl = {}
137 local rslt = max(#tbl_m, #tbl_n)
138 for i = 1, rslt do
139 if(tbl_m[i]== 0 or tbl_n[i] == 0) then
140 tbl[i] = 0
141 else
142 tbl[i] = 1
143 end
144 end
145
146 return tbl2number(tbl)
147 end
148
149 local function bit_xor(m, n)
150 local tbl_m = to_bits(m)
151 local tbl_n = to_bits(n)
152 expand(tbl_m, tbl_n)
153
154 local tbl = {}
155 local rslt = max(#tbl_m, #tbl_n)
156 for i = 1, rslt do
157 if(tbl_m[i] ~= tbl_n[i]) then
158 tbl[i] = 1
159 else
160 tbl[i] = 0
161 end
162 end
163
164 return tbl2number(tbl)
165 end
166
167 local function bit_rshift(n, bits)
168 check_int(n)
169
170 local high_bit = 0
171 if(n < 0) then
172 -- negative
173 n = bit_not(abs(n)) + 1
174 high_bit = 2147483648 -- 0x80000000
175 end
176
177 for i=1, bits do
178 n = n/2
179 n = bit_or(floor(n), high_bit)
180 end
181 return floor(n)
182 end
183
184 local function bit_lshift(n, bits)
185 check_int(n)
186
187 if(n < 0) then
188 -- negative
189 n = bit_not(abs(n)) + 1
190 end
191
192 for i=1, bits do
193 n = n*2
194 end
195 return bit_and(n, 4294967295) -- 0xFFFFFFFF
196 end
197
198 -- convert little-endian 32-bit int to a 4-char string
199 local function lei2str(i)
200 local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end
201 return f(0)..f(8)..f(16)..f(24)
202 end
203
204 -- convert raw string to big-endian int
205 local function str2bei(s)
206 local v=0
207 for i=1, #s do
208 v = v * 256 + byte(s, i)
209 end
210 return v
211 end
212
213 -- convert raw string to little-endian int
214 local function str2lei(s)
215 local v=0
216 for i = #s,1,-1 do
217 v = v*256 + byte(s, i)
218 end
219 return v
220 end
221
222 -- cut up a string in little-endian ints of given size
223 local function cut_le_str(s,...)
224 local o, r = 1, {}
225 local args = {...}
226 for i=1, #args do
227 table.insert(r, str2lei(sub(s, o, o + args[i] - 1)))
228 o = o + args[i]
229 end
230 return r
231 end
232
233 local swap = function (w) return str2bei(lei2str(w)) end
234
235 local function hex2binaryaux(hexval)
236 return char(tonumber(hexval, 16))
237 end
238
239 local function hex2binary(hex)
240 local result, _ = hex:gsub('..', hex2binaryaux)
241 return result
242 end
243
244 -- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh)
245 -- 10/02/2001 jcw@equi4.com
246
247 local FF = 0xffffffff
248 local CONSTS = {
249 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
250 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
251 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
252 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
253 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
254 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
255 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
256 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
257 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
258 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
259 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
260 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
261 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
262 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
263 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
264 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
265 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476
266 }
267
268 local f=function (x,y,z) return bit_or(bit_and(x,y),bit_and(-x-1,z)) end
269 local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end
270 local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end
271 local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end
272 local z=function (f,a,b,c,d,x,s,ac)
273 a=bit_and(a+f(b,c,d)+x+ac,FF)
274 -- be *very* careful that left shift does not cause rounding!
275 return bit_or(bit_lshift(bit_and(a,bit_rshift(FF,s)),s),bit_rshift(a,32-s))+b
276 end
277
278 local function transform(A,B,C,D,X)
279 local a,b,c,d=A,B,C,D
280 local t=CONSTS
281
282 a=z(f,a,b,c,d,X[ 0], 7,t[ 1])
283 d=z(f,d,a,b,c,X[ 1],12,t[ 2])
284 c=z(f,c,d,a,b,X[ 2],17,t[ 3])
285 b=z(f,b,c,d,a,X[ 3],22,t[ 4])
286 a=z(f,a,b,c,d,X[ 4], 7,t[ 5])
287 d=z(f,d,a,b,c,X[ 5],12,t[ 6])
288 c=z(f,c,d,a,b,X[ 6],17,t[ 7])
289 b=z(f,b,c,d,a,X[ 7],22,t[ 8])
290 a=z(f,a,b,c,d,X[ 8], 7,t[ 9])
291 d=z(f,d,a,b,c,X[ 9],12,t[10])
292 c=z(f,c,d,a,b,X[10],17,t[11])
293 b=z(f,b,c,d,a,X[11],22,t[12])
294 a=z(f,a,b,c,d,X[12], 7,t[13])
295 d=z(f,d,a,b,c,X[13],12,t[14])
296 c=z(f,c,d,a,b,X[14],17,t[15])
297 b=z(f,b,c,d,a,X[15],22,t[16])
298
299 a=z(g,a,b,c,d,X[ 1], 5,t[17])
300 d=z(g,d,a,b,c,X[ 6], 9,t[18])
301 c=z(g,c,d,a,b,X[11],14,t[19])
302 b=z(g,b,c,d,a,X[ 0],20,t[20])
303 a=z(g,a,b,c,d,X[ 5], 5,t[21])
304 d=z(g,d,a,b,c,X[10], 9,t[22])
305 c=z(g,c,d,a,b,X[15],14,t[23])
306 b=z(g,b,c,d,a,X[ 4],20,t[24])
307 a=z(g,a,b,c,d,X[ 9], 5,t[25])
308 d=z(g,d,a,b,c,X[14], 9,t[26])
309 c=z(g,c,d,a,b,X[ 3],14,t[27])
310 b=z(g,b,c,d,a,X[ 8],20,t[28])
311 a=z(g,a,b,c,d,X[13], 5,t[29])
312 d=z(g,d,a,b,c,X[ 2], 9,t[30])
313 c=z(g,c,d,a,b,X[ 7],14,t[31])
314 b=z(g,b,c,d,a,X[12],20,t[32])
315
316 a=z(h,a,b,c,d,X[ 5], 4,t[33])
317 d=z(h,d,a,b,c,X[ 8],11,t[34])
318 c=z(h,c,d,a,b,X[11],16,t[35])
319 b=z(h,b,c,d,a,X[14],23,t[36])
320 a=z(h,a,b,c,d,X[ 1], 4,t[37])
321 d=z(h,d,a,b,c,X[ 4],11,t[38])
322 c=z(h,c,d,a,b,X[ 7],16,t[39])
323 b=z(h,b,c,d,a,X[10],23,t[40])
324 a=z(h,a,b,c,d,X[13], 4,t[41])
325 d=z(h,d,a,b,c,X[ 0],11,t[42])
326 c=z(h,c,d,a,b,X[ 3],16,t[43])
327 b=z(h,b,c,d,a,X[ 6],23,t[44])
328 a=z(h,a,b,c,d,X[ 9], 4,t[45])
329 d=z(h,d,a,b,c,X[12],11,t[46])
330 c=z(h,c,d,a,b,X[15],16,t[47])
331 b=z(h,b,c,d,a,X[ 2],23,t[48])
332
333 a=z(i,a,b,c,d,X[ 0], 6,t[49])
334 d=z(i,d,a,b,c,X[ 7],10,t[50])
335 c=z(i,c,d,a,b,X[14],15,t[51])
336 b=z(i,b,c,d,a,X[ 5],21,t[52])
337 a=z(i,a,b,c,d,X[12], 6,t[53])
338 d=z(i,d,a,b,c,X[ 3],10,t[54])
339 c=z(i,c,d,a,b,X[10],15,t[55])
340 b=z(i,b,c,d,a,X[ 1],21,t[56])
341 a=z(i,a,b,c,d,X[ 8], 6,t[57])
342 d=z(i,d,a,b,c,X[15],10,t[58])
343 c=z(i,c,d,a,b,X[ 6],15,t[59])
344 b=z(i,b,c,d,a,X[13],21,t[60])
345 a=z(i,a,b,c,d,X[ 4], 6,t[61])
346 d=z(i,d,a,b,c,X[11],10,t[62])
347 c=z(i,c,d,a,b,X[ 2],15,t[63])
348 b=z(i,b,c,d,a,X[ 9],21,t[64])
349
350 return A+a,B+b,C+c,D+d
351 end
352
353 ----------------------------------------------------------------
354
355 function md5.sumhexa(s)
356 local msgLen = #s
357 local padLen = 56 - msgLen % 64
358
359 if msgLen % 64 > 56 then padLen = padLen + 64 end
360
361 if padLen == 0 then padLen = 64 end
362
363 s = s .. char(128) .. rep(char(0),padLen-1) .. lei2str(8*msgLen) .. lei2str(0)
364
365 assert(#s % 64 == 0)
366
367 local t = CONSTS
368 local a,b,c,d = t[65],t[66],t[67],t[68]
369
370 for i=1,#s,64 do
371 local X = cut_le_str(sub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
372 assert(#X == 16)
373 X[0] = table.remove(X,1) -- zero based!
374 a,b,c,d = transform(a,b,c,d,X)
375 end
376
377 return format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d))
378 end
379
380 function md5.sum(s)
381 return hex2binary(md5.sumhexa(s))
382 end
383
384 return md5
Impressum, Datenschutz