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
|
test_run = require('test_run').new()
---
...
test_run:cmd("push filter ".."'\\.lua.*:[0-9]+: ' to '.lua:<line>\"]: '")
---
- true
...
crypto = require('crypto')
---
...
type(crypto)
---
- table
...
ciph = crypto.cipher.aes128.cbc
---
...
pass = '1234567887654321'
---
...
iv = 'abcdefghijklmnop'
---
...
enc = ciph.encrypt('test', pass, iv)
---
...
enc
---
- !!binary WpJJu6l6oziZcyvND8KueA==
...
ciph.decrypt(enc, pass, iv)
---
- test
...
-- Failing scenarios.
crypto.cipher.aes128.cbc.encrypt('a')
---
- error: 'builtin/crypto.lua:<line>"]: Cipher not initialized'
...
crypto.cipher.aes128.cbc.encrypt('a', '123456', '435')
---
- error: key size expected 16, got 6
...
crypto.cipher.aes128.cbc.encrypt('a', '1234567887654321')
---
- error: 'builtin/crypto.lua:<line>"]: Cipher not initialized'
...
crypto.cipher.aes128.cbc.encrypt('a', '1234567887654321', '12')
---
- error: IV size expected 16, got 2
...
crypto.cipher.aes256.cbc.decrypt('a')
---
- error: 'builtin/crypto.lua:<line>"]: Cipher not initialized'
...
crypto.cipher.aes256.cbc.decrypt('a', '123456', '435')
---
- error: key size expected 32, got 6
...
crypto.cipher.aes256.cbc.decrypt('a', '12345678876543211234567887654321')
---
- error: 'builtin/crypto.lua:<line>"]: Cipher not initialized'
...
crypto.cipher.aes256.cbc.decrypt('12', '12345678876543211234567887654321', '12')
---
- error: IV size expected 16, got 2
...
crypto.cipher.aes192.cbc.decrypt.new('123456788765432112345678', '12345')
---
- error: IV size expected 16, got 5
...
-- Set key after codec creation.
c = crypto.cipher.aes128.cbc.encrypt.new()
---
...
key = '1234567812345678'
---
...
iv = key
---
...
c:init(key)
---
...
c:update('plain')
---
- error: Cipher not initialized
...
c:result()
---
- error: Cipher not initialized
...
c:init(nil, iv)
---
...
cipher = c:update('plain ')
---
...
cipher = cipher..c:update('next plain')
---
...
cipher = cipher..c:result()
---
...
crypto.cipher.aes128.cbc.decrypt(cipher, key, iv)
---
- plain next plain
...
-- Reuse.
key2 = '8765432187654321'
---
...
iv2 = key2
---
...
c:init(key2, iv2)
---
...
cipher = c:update('new plain ')
---
...
cipher = cipher..c:update('next new plain')
---
...
cipher = cipher..c:result()
---
...
crypto.cipher.aes128.cbc.decrypt(cipher, key2, iv2)
---
- new plain next new plain
...
crypto.cipher.aes100.efb
---
- error: '[string "return crypto.cipher.aes100.efb "]:1: Cipher method "aes100" is
not supported'
...
crypto.cipher.aes256.nomode
---
- error: 'builtin/crypto.lua:<line>"]: Cipher mode nomode is not supported'
...
crypto.digest.nodigest
---
- error: '[string "return crypto.digest.nodigest "]:1: Digest method "nodigest" is
not supported'
...
-- Check that GC really drops unused codecs and streams, and
-- nothing crashes.
weak = setmetatable({obj = c}, {__mode = 'v'})
---
...
c = nil
---
...
collectgarbage('collect')
---
- 0
...
weak.obj
---
- null
...
bad_pass = '8765432112345678'
---
...
bad_iv = '123456abcdefghij'
---
...
ciph.decrypt(enc, bad_pass, iv)
---
- error: 'OpenSSL error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad
decrypt'
...
ciph.decrypt(enc, pass, bad_iv)
---
- error: 'OpenSSL error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad
decrypt'
...
test_run:cmd("clear filter")
---
- true
...
|