-
Notifications
You must be signed in to change notification settings - Fork 15
/
luagcrypt_test.lua
322 lines (296 loc) · 11.5 KB
/
luagcrypt_test.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
--
-- Test suite for luagcrypt.
--
-- Copyright (C) 2016 Peter Wu <peter@lekensteyn.nl>
-- Licensed under the MIT license. See the LICENSE file for details.
--
-- Convert a string of hexadecimal numbers to a bytes string
function fromhex(hex)
if string.match(hex, "[^0-9a-fA-F]") then
error("Invalid chars in hex")
end
if string.len(hex) % 2 == 1 then
error("Hex string must be a multiple of two")
end
local s = string.gsub(hex, "..", function(v)
return string.char(tonumber(v, 16))
end)
return s
end
function test_check_version()
-- Request version
assert(gcrypt.check_version())
-- Minimum supported version
assert(gcrypt.check_version("1.4.2"))
-- Should return nil if the version is not supported
assert(gcrypt.check_version("99.9.9") == nil)
end
-- Ensure that advertised constants are never removed.
function test_constants()
assert(gcrypt.CIPHER_IDEA == 1)
assert(gcrypt.CIPHER_3DES == 2)
assert(gcrypt.CIPHER_CAST5 == 3)
assert(gcrypt.CIPHER_BLOWFISH == 4)
assert(gcrypt.CIPHER_AES128 == 7)
assert(gcrypt.CIPHER_AES192 == 8)
assert(gcrypt.CIPHER_AES256 == 9)
assert(gcrypt.CIPHER_TWOFISH == 10)
assert(gcrypt.CIPHER_ARCFOUR == 301)
assert(gcrypt.CIPHER_DES == 302)
assert(gcrypt.CIPHER_TWOFISH128 == 303)
assert(gcrypt.CIPHER_SERPENT128 == 304)
assert(gcrypt.CIPHER_SERPENT192 == 305)
assert(gcrypt.CIPHER_SERPENT256 == 306)
assert(gcrypt.CIPHER_RFC2268_40 == 307)
assert(gcrypt.CIPHER_RFC2268_128 == 308)
assert(gcrypt.CIPHER_SEED == 309)
assert(gcrypt.CIPHER_CAMELLIA128 == 310)
assert(gcrypt.CIPHER_CAMELLIA192 == 311)
assert(gcrypt.CIPHER_CAMELLIA256 == 312)
if check_version("1.6.0") then
assert(gcrypt.CIPHER_SALSA20 == 313)
assert(gcrypt.CIPHER_SALSA20R12 == 314)
assert(gcrypt.CIPHER_GOST28147 == 315)
end
if check_version("1.7.0") then
assert(gcrypt.CIPHER_CHACHA20 == 316)
end
assert(gcrypt.CIPHER_MODE_ECB == 1)
assert(gcrypt.CIPHER_MODE_CFB == 2)
assert(gcrypt.CIPHER_MODE_CBC == 3)
assert(gcrypt.CIPHER_MODE_STREAM == 4)
assert(gcrypt.CIPHER_MODE_OFB == 5)
assert(gcrypt.CIPHER_MODE_CTR == 6)
if check_version("1.5.0") then
assert(gcrypt.CIPHER_MODE_AESWRAP == 7)
end
if check_version("1.6.0") then
assert(gcrypt.CIPHER_MODE_CCM == 8)
assert(gcrypt.CIPHER_MODE_GCM == 9)
end
if check_version("1.7.0") then
assert(gcrypt.CIPHER_MODE_POLY1305 == 10)
assert(gcrypt.CIPHER_MODE_OCB == 11)
assert(gcrypt.CIPHER_MODE_CFB8 == 12)
end
assert(gcrypt.MD_SHA1 == 2)
assert(gcrypt.MD_RMD160 == 3)
assert(gcrypt.MD_MD5 == 1)
assert(gcrypt.MD_MD4 == 301)
assert(gcrypt.MD_TIGER == 6)
if check_version("1.5.0") then
assert(gcrypt.MD_TIGER1 == 306)
assert(gcrypt.MD_TIGER2 == 307)
end
assert(gcrypt.MD_SHA224 == 11)
assert(gcrypt.MD_SHA256 == 8)
assert(gcrypt.MD_SHA384 == 9)
assert(gcrypt.MD_SHA512 == 10)
assert(gcrypt.MD_CRC32 == 302)
assert(gcrypt.MD_CRC32_RFC1510 == 303)
assert(gcrypt.MD_CRC24_RFC2440 == 304)
assert(gcrypt.MD_WHIRLPOOL == 305)
if check_version("1.6.0") then
assert(gcrypt.MD_GOSTR3411_94 == 308)
assert(gcrypt.MD_STRIBOG256 == 309)
assert(gcrypt.MD_STRIBOG512 == 310)
end
if check_version("1.7.0") then
assert(gcrypt.MD_GOSTR3411_CP == 311)
assert(gcrypt.MD_SHA3_224 == 312)
assert(gcrypt.MD_SHA3_256 == 313)
assert(gcrypt.MD_SHA3_384 == 314)
assert(gcrypt.MD_SHA3_512 == 315)
assert(gcrypt.MD_SHAKE128 == 316)
assert(gcrypt.MD_SHAKE256 == 317)
end
assert(gcrypt.MD_FLAG_HMAC == 2)
end
function test_aes_cbc_128()
-- RFC 3602 -- 4. Test Vectors (Case #1)
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES128, gcrypt.CIPHER_MODE_CBC)
cipher:setkey(fromhex("06a9214036b8a15b512e03d534120006"))
cipher:setiv(fromhex("3dafba429d9eb430b422da802c9fac41"))
local ciphertext = cipher:encrypt("Single block msg")
assert(ciphertext == fromhex("e353779c1079aeb82708942dbe77181a"))
cipher:reset()
cipher:setiv(fromhex("3dafba429d9eb430b422da802c9fac41"))
local plaintext = cipher:decrypt(fromhex("e353779c1079aeb82708942dbe77181a"))
assert(plaintext == "Single block msg")
end
function test_aes_ctr_192()
-- RFC 3686 -- 6. Test Vectors (Test Vector #6)
local counter_iv_one = fromhex("0007bdfd5cbd60278dcc091200000001")
local plaintexts = {
fromhex("000102030405060708090a0b0c0d0e0f"),
fromhex("101112131415161718191a1b1c1d1e1f"),
fromhex("20212223")
}
local ciphertexts = {
fromhex("96893fc55e5c722f540b7dd1ddf7e758"),
fromhex("d288bc95c69165884536c811662f2188"),
fromhex("abee0935")
}
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES192, gcrypt.CIPHER_MODE_CTR)
cipher:setkey(fromhex("02bf391ee8ecb159b959617b0965279bf59b60a786d3e0fe"))
cipher:setctr(counter_iv_one)
assert(cipher:encrypt(plaintexts[1]) == ciphertexts[1])
assert(cipher:encrypt(plaintexts[2]) == ciphertexts[2])
assert(cipher:encrypt(plaintexts[3]) == ciphertexts[3])
cipher:setctr(counter_iv_one)
assert(cipher:decrypt(ciphertexts[1]) == plaintexts[1])
assert(cipher:decrypt(ciphertexts[2]) == plaintexts[2])
assert(cipher:decrypt(ciphertexts[3]) == plaintexts[3])
end
function test_aes_gcm_128()
if not check_version("1.6.0") then return end
-- http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
-- Test case 4
local plaintext_spec = fromhex("d9313225f88406e5a55909c5aff5269a" ..
"86a7a9531534f7da2e4c303d8a318a72" ..
"1c3c0c95956809532fcf0e2449a6b525" ..
"b16aedf5aa0de657ba637b39")
local ciphertext_spec = fromhex("42831ec2217774244b7221b784d0d49c" ..
"e3aa212f2c02a4e035c17e2329aca12e" ..
"21d514b25466931c7d8f6a5aac84aa05" ..
"1ba30b396a0aac973d58e091")
local adata = fromhex("feedfacedeadbeeffeedfacedeadbeefabaddad2")
local atag = fromhex("5bc94fbc3221a5db94fae95ae7121a47")
local iv = fromhex("cafebabefacedbaddecaf888")
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES128, gcrypt.CIPHER_MODE_GCM)
cipher:setkey(fromhex("feffe9928665731c6d6a8f9467308308"))
cipher:setiv(iv)
cipher:authenticate(adata)
assert(cipher:encrypt(plaintext_spec) == ciphertext_spec)
assert(cipher:gettag() == atag)
cipher:checktag(atag)
cipher:reset()
cipher:setiv(iv)
assert(cipher:decrypt(ciphertext_spec) == plaintext_spec)
end
function test_hmac_sha256()
-- RFC 4231 -- 4.2. Test Case 1
local md = gcrypt.Hash(gcrypt.MD_SHA256, gcrypt.MD_FLAG_HMAC)
md:setkey(fromhex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"))
md:write("Hi There")
local digest = md:read()
assert(digest == fromhex("b0344c61d8db38535ca8afceaf0bf12b" ..
"881dc200c9833da726e9376c2e32cff7"))
end
-- Check for SHA256 calculation with optional flags parameter and reset.
function test_sha256()
-- http://csrc.nist.gov/groups/ST/toolkit/examples.html
local md = gcrypt.Hash(gcrypt.MD_SHA256)
md:write("will be reset")
md:reset()
md:write("ab")
md:write("c")
local digest = md:read(gcrypt.MD_SHA256)
assert(digest == fromhex("ba7816bf8f01cfea414140de5dae2223" ..
"b00361a396177a9cb410ff61f20015ad"))
end
function assert_throws(func, message)
local ok, err = pcall(func)
if ok then
error("Expected \"" .. message .. "\", got no error")
end
if not string.find(err, message, 1, true) then
error("Expected \"" .. message .. "\", got \"" .. err .. "\"")
end
end
function test_cipher_bad()
assert_throws(function() gcrypt.Cipher(0, 0) end,
"gcry_cipher_open() failed with Invalid cipher algorithm")
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES128, gcrypt.CIPHER_MODE_CBC)
assert_throws(function() cipher:setkey("") end,
"gcry_cipher_setkey() failed with Invalid key length")
-- Set key or encrypt will fail with "Missing key" since Libgcrypt 1.7
cipher:setkey(string.rep("x", 16))
-- Must normally be a multiple of block size
assert_throws(function() cipher:encrypt("x") end,
"gcry_cipher_encrypt() failed with Invalid length")
assert_throws(function() cipher:decrypt("y") end,
"gcry_cipher_decrypt() failed with Invalid length")
-- Should not segfault.
cipher:__gc()
cipher:__gc()
assert_throws(function() cipher:reset() end,
"Called into a dead object")
end
function test_cipher_gettag()
if not check_version("1.6.0") then return end
-- ECB has no tag, it should not succeed
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES128, gcrypt.CIPHER_MODE_ECB)
assert_throws(function() cipher:gettag() end,
"Unsupported cipher mode")
end
function test_aes_ctr_bad()
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES128, gcrypt.CIPHER_MODE_CTR)
-- Counter must be a multiple of block size
assert_throws(function() cipher:setctr("x") end,
"gcry_cipher_setctr() failed with Invalid argument")
end
function test_aes_gcm_bad()
if not check_version("1.6.0") then return end
local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES128, gcrypt.CIPHER_MODE_GCM)
assert_throws(function() cipher:setiv("") end,
"gcry_cipher_setiv() failed with Invalid length")
end
function test_hash_bad()
-- Not all flags are valid, this should trigger an error. Alternatively, one
-- can set an invalid algorithm (such as -1), but that generates debug spew.
assert_throws(function() gcrypt.Hash(0, -1) end,
"gcry_md_open() failed with Invalid argument")
local md = gcrypt.Hash(gcrypt.MD_SHA256)
-- Not called with MD_FLAG_HMAC, so should fail
-- 1.6.5: "Conflicting use".
-- 1.7.0: "Invalid digest algorithm"
assert_throws(function() md:setkey("X") end,
"gcry_md_setkey() failed with ")
assert_throws(function() md:read(-1) end,
"Unable to obtain digest for a disabled algorithm")
-- Should not segfault.
md:__gc()
md:__gc()
assert_throws(function() md:reset() end,
"Called into a dead object")
end
function test_init_once()
-- TODO is this really desired behavior?
assert_throws(function() gcrypt.init() end,
"Libgcrypt was already initialized")
end
local all_tests = {
{"test_check_version", test_check_version},
{"test_constants", test_constants},
{"test_aes_cbc_128", test_aes_cbc_128},
{"test_aes_ctr_192", test_aes_ctr_192},
{"test_aes_gcm_128", test_aes_gcm_128},
{"test_hmac_sha256", test_hmac_sha256},
{"test_sha256", test_sha256},
{"test_cipher_bad", test_cipher_bad},
{"test_cipher_gettag", test_cipher_gettag},
{"test_aes_ctr_bad", test_aes_ctr_bad},
{"test_aes_gcm_bad", test_aes_gcm_bad},
{"test_hash_bad", test_hash_bad},
{"test_init_once", test_init_once},
}
function check_version(req_version)
if gcrypt.check_version(req_version) then
return true
end
print("Skipping test because Libgcrypt " .. req_version .. " is required")
end
function main()
for k, v in pairs(all_tests) do
local name, test = v[1], v[2]
print("Running " .. name .. "...")
test()
-- Trigger GC routines
collectgarbage()
end
print("All tests pass!")
end
gcrypt = require("luagcrypt")
gcrypt.init()
main()