Skip to content

Commit 40fdbbb

Browse files
committed
feat(mac) add reset API
1 parent b36ccba commit 40fdbbb

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Table of Contents
124124
+ [mac:gettable_params, mac:settable_params, mac:get_param, mac:set_params](#macgettable_params-macsettable_params-macget_param-macset_params)
125125
+ [mac:update](#macupdate)
126126
+ [mac:final](#macfinal)
127+
+ [mac:reset](#macreset)
127128
* [resty.openssl.kdf](#restyopensslkdf)
128129
+ [kdf.derive (legacy)](#kdfderive-legacy)
129130
+ [kdf.new](#kdfnew)
@@ -2111,10 +2112,13 @@ Module to interact with message authentication code (EVP_MAC).
21112112

21122113
**syntax**: *h, err = mac.new(key, mac, cipher?, digest?, properties?)*
21132114

2114-
Creates a mac instance. `mac` is a case-insensitive string of digest algorithm name.
2115+
Creates a mac instance. `mac` is a case-insensitive string of MAC algorithm name.
21152116
To view a list of digest algorithms implemented, use
21162117
[openssl.list_mac_algorithms](#openssllist_mac_algorithms) or
21172118
`openssl list -mac-algorithms`.
2119+
2120+
At least one of `cipher` or `digest` must be specified.
2121+
21182122
`cipher` is a case-insensitive string of digest algorithm name.
21192123
To view a list of digest algorithms implemented, use
21202124
[openssl.list_cipher_algorithms](#openssllist_cipher_algorithms) or
@@ -2181,6 +2185,16 @@ ngx.say(ngx.encode_base64(mac))
21812185

21822186
[Back to TOC](#table-of-contents)
21832187

2188+
### mac:reset
2189+
2190+
**syntax**: *ok, err = mac:reset()*
2191+
2192+
Reset the internal state of `mac` instance as it's just created by [mac.new](#macnew).
2193+
It calls [EVP_MAC_Init](https://www.openssl.org/docs/manmaster/man3/EVP_MAC_init.html) under
2194+
the hood.
2195+
2196+
User must call this before reusing the same `mac` instance.
2197+
21842198
## resty.openssl.kdf
21852199

21862200
Module to interact with KDF (key derivation function).

examples/perf/test_other_libs.lua

+50
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ local set_iteration = require "framework".set_iteration
66
local write_seperator = require "framework".write_seperator
77
local cipher = require "resty.openssl.cipher"
88
local digest = require "resty.openssl.digest"
9+
local hmac = require "resty.openssl.hmac"
910
local pkey = require "resty.openssl.pkey"
1011
local version = require "resty.openssl.version"
1112
local rand = require "resty.openssl.rand"
1213
local kdf = require "resty.openssl.kdf"
1314
local aes = require "resty.aes"
1415
local resty_rsa = require "resty.rsa"
16+
local to_hex = require "resty.string".to_hex
1517

1618
-- ensure best performance
1719
require "framework".set_no_count_iter(true)
@@ -258,6 +260,54 @@ do
258260

259261
end
260262

263+
------------- hmac
264+
do
265+
write_seperator()
266+
267+
local data = string.rep("1", 4096)
268+
local key = rand.bytes(32)
269+
270+
local d = hmac.new(key, "sha256")
271+
272+
local expected = d:final(data)
273+
274+
test("lua-resty-openssl hmac sha256 on " .. #data .. " bytes", function()
275+
d:reset()
276+
return d:final(data)
277+
end, nil, expected)
278+
279+
if version.OPENSSL_3X then
280+
local mac = require "resty.openssl.mac"
281+
local m = mac.new(key, "HMAC", nil, "sha256")
282+
test("lua-resty-openssl hmac sha256 new API on " .. #data .. " bytes", function()
283+
m:reset()
284+
return m:final(data)
285+
end, nil, expected)
286+
end
287+
288+
if luaossl then
289+
local _hmac = require "_openssl.hmac"
290+
test("luaossl hmac sha256 " .. #data .. " bytes", function()
291+
local hh = _hmac.new(key, "sha256")
292+
return hh:final(data)
293+
end, nil, expected)
294+
end
295+
296+
if lua_openssl then
297+
local hh = lua_openssl.hmac
298+
test("lua_openssl hmac sha256 on " .. #data .. " bytes", function()
299+
return hh.hmac("sha256", data, key)
300+
end, nil, to_hex(expected))
301+
302+
if version.OPENSSL_3X then
303+
local mm = lua_openssl.mac
304+
test("lua_openssl hmac sha256 new API on " .. #data .. " bytes", function()
305+
return mm.mac("sha256", data, key)
306+
end, nil, to_hex(expected))
307+
end
308+
end
309+
end
310+
261311
------------- pkey
262312
do
263313
write_seperator()

lib/resty/openssl/mac.lua

+16-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ function _M.new(key, typ, cipher, digest, properties)
4141
params.cipher = cipher
4242
local p = param_lib.construct(params, 2, param_types)
4343

44-
local code = C.EVP_MAC_init(ctx, key, #key, p)
44+
local keyl = #key
45+
local code = C.EVP_MAC_init(ctx, key, keyl, p)
4546
if code ~= 1 then
4647
return nil, format_error(string.format("mac.new: invalid cipher or digest type"))
4748
end
@@ -53,6 +54,7 @@ function _M.new(key, typ, cipher, digest, properties)
5354
algo = algo,
5455
buf = ctypes.uchar_array(md_size),
5556
buf_size = md_size,
57+
_reset = function() return C.EVP_MAC_init(ctx, key, keyl, p) end,
5658
}, mt), nil
5759
end
5860

@@ -73,25 +75,33 @@ _M.settable_params, _M.set_params, _M.gettable_params, _M.get_param = param_lib.
7375
function _M:update(...)
7476
for _, s in ipairs({...}) do
7577
if C.EVP_MAC_update(self.ctx, s, #s) ~= 1 then
76-
return false, format_error("digest:update")
78+
return false, format_error("mac:update")
7779
end
7880
end
7981
return true, nil
8082
end
8183

8284
function _M:final(s)
8385
if s then
84-
local _, err = self:update(s)
85-
if err then
86-
return nil, err
86+
if C.EVP_MAC_update(self.ctx, s, #s) ~= 1 then
87+
return false, format_error("mac:final")
8788
end
8889
end
8990

9091
local length = ctypes.ptr_of_size_t()
9192
if C.EVP_MAC_final(self.ctx, self.buf, length, self.buf_size) ~= 1 then
92-
return nil, format_error("digest:final: EVP_MAC_final")
93+
return nil, format_error("mac:final: EVP_MAC_final")
9394
end
9495
return ffi_str(self.buf, length[0])
9596
end
9697

98+
function _M:reset()
99+
local code = self._reset()
100+
if code ~= 1 then
101+
return false, format_error("mac:reset")
102+
end
103+
104+
return true
105+
end
106+
97107
return _M

0 commit comments

Comments
 (0)