@@ -10,7 +10,7 @@ local openssl_rand = require "resty.openssl.rand"
1010local kdf = require " resty.openssl.kdf"
1111local utils = require " resty.utils"
1212
13- local _M = { _VERSION = " 0.3.0 " }
13+ local _M = { _VERSION = " 0.3.1 " }
1414
1515local mt = {
1616 __index = _M
@@ -187,7 +187,8 @@ local ec_nid_to_curve = {
187187 [716 ] = " secp521r1" ,
188188}
189189
190- -- RFC 7518 Section 4.6.2 - Concat KDF (single-pass SHA-256)
190+ -- RFC 7518 Section 4.6.2 - Concat KDF (multi-round SHA-256)
191+ -- reps = ceil(keydatalen / hashlen); hashlen = 256 for SHA-256
191192local function derive_shared_key (header , shared_secret_Z )
192193 local enc = header .enc
193194 local keydatalen = keydatalen_map [enc ]
@@ -219,21 +220,32 @@ local function derive_shared_key(header, shared_secret_Z)
219220
220221 utils .append_array (other_info , utils .integer_to_32_bit_big_endian (keydatalen ))
221222
223+ local hashlen = 256
224+ local reps = math.ceil (keydatalen / hashlen )
222225 local z_bytes = utils .string_to_byte_array (shared_secret_Z )
223- local round_concat = utils .append_array ({0 , 0 , 0 , 1 }, z_bytes )
224- utils .append_array (round_concat , other_info )
225-
226- local input = string_char (unpack (round_concat ))
227- local d , err = digest .new (" SHA256" )
228- if not d then
229- error ({reason = " failed to create SHA256 digest: " .. (err or " " )})
230- end
231- local md , hash_err = d :final (input )
232- if not md then
233- error ({reason = " failed to compute KDF hash: " .. (hash_err or " " )})
226+ local derived = {}
227+
228+ for round = 1 , reps do
229+ local counter = utils .integer_to_32_bit_big_endian (round )
230+ local round_concat = {}
231+ utils .append_array (round_concat , counter )
232+ utils .append_array (round_concat , z_bytes )
233+ utils .append_array (round_concat , other_info )
234+
235+ local input = string_char (unpack (round_concat ))
236+ local d , err = digest .new (" SHA256" )
237+ if not d then
238+ error ({reason = " failed to create SHA256 digest: " .. (err or " " )})
239+ end
240+ local md , hash_err = d :final (input )
241+ if not md then
242+ error ({reason = " failed to compute KDF hash: " .. (hash_err or " " )})
243+ end
244+ derived [round ] = md
234245 end
235246
236- return string_sub (md , 1 , keydatalen / 8 )
247+ local full = table_concat (derived )
248+ return string_sub (full , 1 , keydatalen / 8 )
237249end
238250
239251-- AES Key Wrap (RFC 3394) default IV
0 commit comments