-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBCryptUtils.lua
351 lines (265 loc) · 8.99 KB
/
BCryptUtils.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
local ffi = require "ffi"
local BCrypt = require "BCrypt"
local stringutils = require("stringutils")
BCryptKey = ffi.typeof("BCryptKey");
BCryptKey_mt = {
__gc = function(self)
local status = BCrypt.Lib.BCryptDestroyKey(self.Handle)
end,
__index = {
FinalizeKeyPair = function(self)
local status = BCrypt.Lib.BCryptFinalizeKeyPair(self.Handle);
return status == 0 or nil, status
end,
},
}
BCryptKey = ffi.metatype(BCryptKey, BCryptKey_mt);
BCryptHash = ffi.typeof("BCryptHash");
BCryptHash_mt = {
__gc = function(self)
local status = BCrypt.Lib.BCryptDestroyHash(self.Handle);
end,
__new = function(ct, algorithm)
local phHash = ffi.new("BCRYPT_HASH_HANDLE[1]");
local pbHashObject = nil
local cbHashObject = 0
local pbSecret = nil
local cbSecret = 0
local flags = 0
local status = BCrypt.Lib.BCryptCreateHash(algorithm.Handle,
phHash,
pbHashObject,
cbHashObject,
pbSecret,
cbSecret,
flags);
if status ~= 0 then
return nil, status
end
return ffi.new(ct, phHash[0]);
end,
__index = {
GetProperty = function(self, name, buffer, size)
local pcbResult = ffi.new("uint32_t[1]")
local buffptr = ffi.cast("uint8_t *", buffer)
local status = BCrypt.Lib.BCryptGetProperty(self.Handle,
name,
buffptr, size,
pcbResult,
0);
if status ~= 0 then
print("GetProperty, Error status: ", status);
return nil, status
end
-- got the result back
-- return it to the user
return buffptr, pcbResult[0]
end,
GetPropertyBuffer = function(self, name)
local pcbResult = ffi.new("uint32_t[1]")
local status = BCrypt.Lib.BCryptGetProperty(self.Handle,
name,
nil, 0,
pcbResult,
0);
if status ~= 0 then
return nil, status
end
local bytesneeded = pcbResult[0]
local pbOutput = ffi.new("uint8_t[?]", pcbResult[0]);
return pbOutput, bytesneeded
end,
GetHashDigestLength = function(self)
local size = ffi.sizeof("int32_t");
local buff = ffi.new("int[1]")
local outbuff, byteswritten = self:GetProperty(BCrypt.BCRYPT_HASH_LENGTH, buff, size)
--print("GetHashLength: ", outbuff, byteswritten);
if not outbuff then
return nil, byteswritten
end
return buff[0];
end,
Clone = function(self)
local phNewHash = ffi.new("BCRYPT_HASH_HANDLE[1]");
local pbHashObject = nil
local cbHashObject = 0
local pbSecret = nil
local cbSecret = 0
local flags = 0
local status = BCrypt.Lib.BCryptDuplicateHash(self.Handle,
phNewHash,
pbHashObject,
cbHashObject,
flags);
if status ~= 0 then
return nil, status
end
return ffi.new("BCryptHash", phNewHash[0]);
end,
HashMore = function(self, chunk, chunksize)
local pbInput = chunk
local cbInput
local flags = 0
if type(chunk) == "string" then
pbInput = ffi.cast("const uint8_t *", chunk);
if not chunksize then
cbInput = #chunk
end
else
cbInput = cbInput or 0
end
local status = BCrypt.Lib.BCryptHashData(self.Handle,
pbInput,
cbInput,
flags);
return status == 0 or nil, status
end,
Finish = function(self, pbOutput, cbOutput)
local flags = 0
local status = BCrypt.Lib.BCryptFinishHash(self.Handle,
pbOutput,
cbOutput,
flags);
return status == 0 or nil, status
end,
CreateDigest = function(self, input, inputLength)
local outlen = self:GetHashDigestLength();
local outbuff = ffi.new("uint8_t[?]", outlen);
self:HashMore(input);
self:Finish(outbuff, outlen);
local hex = stringutils.bintohex(outbuff, outlen);
return hex
end,
},
}
BCryptHash = ffi.metatype(BCryptHash, BCryptHash_mt);
local BCryptAlgorithm = ffi.typeof("struct BCryptAlgorithm")
local BCryptAlgorithm_mt = {
__gc = function(self)
--print("BCryptAlgorithm: GC")
if self.Handle ~= nil then
BCrypt.Lib.BCryptCloseAlgorithmProvider(self.Handle, 0)
end
end;
__new = function(ctype, ...)
--print("BCryptAlgorithm: NEW");
local params = {...}
local algoid = params[1]
local impl = params[2]
if not algoid then
return nil
end
local lphAlgo = ffi.new("BCRYPT_ALG_HANDLE[1]")
local algoidptr = ffi.cast("const uint16_t *", algoid)
local status = BCrypt.Lib.BCryptOpenAlgorithmProvider(lphAlgo, algoidptr, impl, 0);
if not BCrypt.BCRYPT_SUCCESS(status) then
print("BCryptAlgorithm(), status: ", status);
return nil
end
local newone = ffi.new("struct BCryptAlgorithm", lphAlgo[0])
return newone;
end;
__index = {
-- CreateHash
CreateHash = function(self)
return BCryptHash(self);
end,
CreateKeyPair = function(self, length, flags)
length = length or 384
flags = flags or 0
local fullKey = ffi.new("BCRYPT_KEY_HANDLE[1]");
local status = BCrypt.Lib.BCryptGenerateKeyPair(self.Handle,
fullKey, length, flags);
if status ~= 0 then
return nil, status
end
-- create the key pair
local fullKey = fullKey[0];
end,
}
};
BCryptAlgorithm = ffi.metatype(BCryptAlgorithm, BCryptAlgorithm_mt);
CryptUtils = {
BCryptKey = BCryptKey,
BCryptHash = BCryptHash,
BCryptAlgorithm = BCryptAlgorithm,
}
--[[
Pre allocated Algorithm objects
--]]
-- Hash Algorithms
CryptUtils.MD2Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_MD2_ALGORITHM);
CryptUtils.MD4Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_MD4_ALGORITHM);
CryptUtils.MD5Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_MD5_ALGORITHM);
CryptUtils.SHA1Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_SHA1_ALGORITHM);
CryptUtils.SHA256Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_SHA256_ALGORITHM);
CryptUtils.SHA384Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_SHA384_ALGORITHM);
CryptUtils.SHA512Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_SHA512_ALGORITHM);
-- Random Number Generators
CryptUtils.RNGAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RNG_ALGORITHM);
CryptUtils.RNGFIPS186DSAAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RNG_FIPS186_DSA_ALGORITHM);
CryptUtils.RNGDUALECAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RNG_DUAL_EC_ALGORITHM);
CryptUtils.RSAAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RSA_ALGORITHM);
CryptUtils.RSASignAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RSA_SIGN_ALGORITHM);
CryptUtils.DHAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_DH_ALGORITHM);
CryptUtils.DSAAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_DSA_ALGORITHM);
CryptUtils.RC2Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RC2_ALGORITHM);
CryptUtils.RC4Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_RC4_ALGORITHM);
CryptUtils.AESAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_AES_ALGORITHM);
CryptUtils.AESGMACAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_AES_GMAC_ALGORITHM);
CryptUtils.DESAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_DES_ALGORITHM);
CryptUtils.DESXAlgorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_DESX_ALGORITHM);
CryptUtils.DES3Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_3DES_ALGORITHM);
CryptUtils.DES3_112Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_3DES_112_ALGORITHM);
CryptUtils.ECDSA_P256Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_ECDSA_P256_ALGORITHM);
CryptUtils.ECDSA_P384Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_ECDSA_P384_ALGORITHM);
CryptUtils.ECDSA_P521Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_ECDSA_P521_ALGORITHM);
CryptUtils.ECDH_P256Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_ECDH_P256_ALGORITHM);
CryptUtils.ECDH_P384Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_ECDH_P384_ALGORITHM);
CryptUtils.ECDH_P521Algorithm = CryptUtils.BCryptAlgorithm(BCrypt.BCRYPT_ECDH_P521_ALGORITHM);
--[[
Hash Functions
--]]
CryptUtils.Hashes = {
MD2 = CryptUtils.MD2Algorithm:CreateHash();
MD4 = CryptUtils.MD4Algorithm:CreateHash();
MD5 = CryptUtils.MD5Algorithm:CreateHash();
SHA1 = CryptUtils.SHA1Algorithm:CreateHash();
SHA256 = CryptUtils.SHA256Algorithm:CreateHash();
SHA384 = CryptUtils.SHA384Algorithm:CreateHash();
SHA512 = CryptUtils.SHA512Algorithm:CreateHash();
}
CryptUtils.MD2 = function(content, len)
return CryptUtils.Hashes.MD2:CreateDigest(content, len);
end
CryptUtils.MD4 = function(content, len)
return CryptUtils.Hashes.MD4:CreateDigest(content, len);
end
CryptUtils.MD5 = function(content, len)
return CryptUtils.Hashes.MD5:CreateDigest(content, len);
end
CryptUtils.SHA1 = function(content, len)
return CryptUtils.Hashes.SHA1:CreateDigest(content, len);
end
CryptUtils.SHA256 = function(content, len)
return CryptUtils.Hashes.SHA256:CreateDigest(content, len);
end
CryptUtils.SHA384 = function(content, len)
return CryptUtils.Hashes.SHA384:CreateDigest(content, len);
end
CryptUtils.SHA512 = function(content, len)
return CryptUtils.Hashes.SHA512:CreateDigest(content, len);
end
--[[
Convenience Functions
--]]
CryptUtils.GetRandomBytes = function(howmany)
howmany = howmany or 4
local rngBuff = ffi.new("uint8_t[?]", howmany)
local status = BCrypt.Lib.BCryptGenRandom(nil, rngBuff, howmany, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if status >= 0 then
return rngBuff, howmany
end
return nil, status
end
return CryptUtils