-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.lua
302 lines (265 loc) · 6.77 KB
/
crypto.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
local openssl = require'openssl'
local io = require'io'
local M = {}
M["_VERSION"] = "LuaCrypto 0.3.1"
M["_COPYRIGHT"] = "Copyright (C) 2014 GeorgeZhao";
M["_DESCRIPTION"] = "crypto is a high level Lua wrapper for OpenSSL based on lua-openssl";
-----------crypto------------------
function M.hex(s)
local s = openssl.hex(s)
return string.lower(s)
end
M.digest = openssl.digest
local dm = {}
dm.__call = function(self,alg,msg,raw)
raw = raw or true
return M.digest.digest(alg,msg)
end
setmetatable(M.digest,dm)
M.hmac = openssl.hmac
-----------crypto encrypt/decrypt compat----------
local cipher = openssl.cipher
local C = {}
C.__index = {
new = function(alg,key,iv)
local c = cipher.encrypt_new(alg,key,iv)
if c then
local I = c:info()
if (iv and #iv>I.iv_length) then
error('invalid iv')
end
if(#key>I.key_length) then
error('invalid key')
end
local t = {}
t.ctx = c
setmetatable(t,C)
return t
end
end,
update = function(self,input)
return self.ctx:update(input)
end,
final = function(self)
return self.ctx:final()
end
}
C.__call = function(self,alg,input,key,iv)
local c = cipher.get(alg)
local I = c:info()
if (iv and #iv>I.iv_length) then
error 'invalid iv'
end
if(#key>I.key_length) then
error 'invalid key'
end
local ret, msg = cipher.encrypt(alg,input,key,iv)
return ret,msg
end
setmetatable(C,C)
M.encrypt = C
local D = {}
D.__index = {
new = function(alg,key,iv)
local c = cipher.decrypt_new(alg,key,iv)
if c then
local I = c:info()
if (iv and #iv>I.iv_length) then
error('invalid iv')
end
if(#key>I.key_length) then
error('invalid key')
end
local t = {}
t.ctx = c
setmetatable(t,D)
return t
end
end,
update = function(self,input)
return self.ctx:update(input)
end,
final = function(self)
return self.ctx:final()
end
}
D.__call = function(self,alg,input,key,iv)
local c = cipher.get(alg)
local I = c:info()
if (iv and #iv>I.iv_length) then
error('invalid iv')
end
if(#key>I.key_length) then
error('invalid key')
end
local r,s = cipher.decrypt(alg,input,key,iv)
return r,s
end
setmetatable(D,D)
M.decrypt = D
-----------crypto random compat------------------
local R = {}
function R.load(file)
return openssl.rand_load(file)
end
function R.write(file)
return openssl.rand_write(file)
end
function R.cleanup()
return openssl.rand_cleanup()
end
function R.status()
return openssl.rand_status()
end
function R.pseudo_bytes(len)
return openssl.random(len,false)
end
function R.bytes(len)
return openssl.random(len,true)
end
M.rand = R
-----------crypto pkey compat------------------
local P = {}
local pkey = openssl.pkey
local PKEY_M = {}
PKEY_M.__index = {}
function PKEY_M.__index.to_pem(self,ispriv, israw)
israw = israw or false
if (ispriv) then
return self.evp_pkey:export(true, israw)
else
return self.evp_pkey:is_private()
and self.evp_pkey:get_public():export(true, israw)
or self.evp_pkey:export(true, israw)
end
end
function PKEY_M.__index.write(self,pubfile,prifile)
local PUB,PRI = self:to_pem(false), self:to_pem(true)
local f = io.open(pubfile,'w+')
if f then
f:write(PUB)
f:close()
end
local f = io.open(prifile,'w+')
if f then
f:write(PRI)
f:close()
end
end
function P.read(file,ispriv)
local f = io.open(file,'r')
if f then
local pem = f:read("*all")
f:close()
return P.from_pem(pem,ispriv)
end
end
function P.from_pem(pem, ispriv)
local k = pkey.read (pem, ispriv,'pem')
if k then
local key = {}
key.evp_pkey = k
setmetatable(key,PKEY_M)
return key
end
end
function P.generate(alg,bits)
local k = pkey.new (alg,bits)
if k then
local key = {}
key.evp_pkey = k
setmetatable(key,PKEY_M)
return key
end
end
M.pkey = P
------------------------------------------
function M.sign(alg,input,prikey)
local pk = prikey.evp_pkey
return pkey.sign(pk,input,alg)
end
function M.verify(alg,input,sig,pubkey)
local pk = pubkey.evp_pkey
return pkey.verify(pk,input,sig,alg)
end
-----------------crypto seal/open compat
local S = {}
S.__index = {
new = function(alg,pubkey)
local c,key,iv = pkey.seal_init(pubkey.evp_pkey,alg)
if c then
local t = {}
t.ctx = c
t.key = key
t.iv = iv
setmetatable(t,S)
return t
end
end,
update = function(self,data)
return pkey.seal_update(self.ctx,data)
end,
final = function(self)
local s = pkey.seal_final(self.ctx)
return s,self.key,self.iv
end,
}
S.__call = function(self,alg,input,pubkey)
local msg, key,iv = pkey.seal(pubkey.evp_pkey, input, alg)
return msg,key,iv
end
setmetatable(S,S)
M.seal = S
local O = {}
O.__index = {
new = function(alg,privkey, ekey, iv)
local c,key,iv = pkey.open_init(privkey.evp_pkey,ekey,iv,alg)
if c then
local t = {}
t.ctx = c
t.key = key
t.iv = iv
setmetatable(t,O)
return t
end
end,
update = function(self,data)
return pkey.open_update(self.ctx,data)
end,
final = function(self)
local s = pkey.open_final(self.ctx)
return s
end,
}
O.__call = function(self,alg,input,prikey,ek,iv)
return pkey.open(prikey.evp_pkey,input,ek,iv,alg)
end
setmetatable(O,O)
M.open = O
----------------crypto pki compat------------
local X = {}
X.__index = {
add_pem = function(self,pem)
local ret,x = pcall(openssl.x509.read,pem)
if ret then
self[#self+1] = x
return x
end
return nil
end,
verify_pem = function(self,pem)
local ret,x = pcall(openssl.x509.read,pem)
if ret then
local store = openssl.x509.store.new(self)
return x:check(store)
end
return false
end
}
function M.x509_ca()
local t = {}
setmetatable(t,X)
return t
end
----------------------------------------------
return M