wahern / luaossl

Most comprehensive OpenSSL module in the Lua universe.
http://25thandclement.com/~william/projects/luaossl.html
Other
140 stars 49 forks source link

lua: oops: no OpenSSL errors set #196

Open hiallo opened 2 years ago

hiallo commented 2 years ago

arch

mips

lua version

lua 5.1

openssl version

WARNING: can't open config file: /usr/local/ssl/openssl.cnf OpenSSL 1.0.2o 27 Mar 2018

exec following lua script,has exception : lua: oops: no OpenSSL errors set

#!/usr/bin/env lua
local kdf  = require("openssl.kdf")
local hmac = require("openssl.hmac")
local digest = require("openssl.digest")
local bit32  = require("bit32")

local passwd = "qwer1234"
local salt_str = "845c242ea25e7960b075c281f14abb27a31fef4ca1b1bb9338ac7e5c2a01490b"
local firstnonce = "a224e4aca9beeb03482e729fb9d6e98c36a95727587b27aa427af23d9076dbd4"
local servernonce = "a224e4aca9beeb03482e729fb9d6e98c36a95727587b27aa427af23d9076dbd49ys7zHd8m0EzNabQBr2qo8O7dCtV1bCx"

local iterations = 100
local outputBytes = 32

--local mac256 = hmac:new()

local function hexstring(str)
    return (str:gsub("..", function(b) return string.char(tonumber(b, 16)) end))
end

local function tohex(b)
    local x = ""
    for i = 1, #b do
        x = x .. string.format("%.2x", string.byte(b, i))
    end
    return x
end

--print(hexstring(salt_str))

local saltpasswd = kdf.derive({
    type = "PBKDF2",
    outlen = outputBytes,
    pass = passwd,
    salt = hexstring(salt_str),
    iter = iterations,
    md  = "sha256"
})

print(tohex(saltpasswd))

local clientKeyStr = "Client Key"

-- client key
local hmac_sha256 = hmac.new(clientKeyStr,"sha256")
hmac_sha256:update(saltpasswd)
local client_key = hmac_sha256:final()
print(tohex(client_key))

-- store key
local store_key = digest.new("sha256"):update(client_key):final()
print(tohex(store_key))

-- server key
local server_key = hmac.new(saltpasswd,"sha256"):update("Server Key"):final()
--print(tohex(server_key))

local auth_message = firstnonce .. "," .. servernonce .. "," .. servernonce
print(auth_message)

local client_sign = hmac.new(auth_message,"sha256"):update(store_key):final()
print(tohex(client_sign))

local key = ""
for i = 1,#client_sign do
     key =key .. string.char(bit32.bxor(string.byte(client_sign,i),string.byte(client_key,i)))
end 
print(tohex(key))