Skip to content

Commit

Permalink
refactor: improve extra KW header parameter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Dec 3, 2019
1 parent a9f1d4b commit b103874
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/jwa/ecdh/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const wrapKey = (key, payload, { enc }) => {
}
}

const unwrapKey = (key, payload, { apu, apv, epk, enc }) => {
return derive(enc, KEYLENGTHS.get(enc), key, epk, { apu, apv })
const unwrapKey = (key, payload, header) => {
const { enc, epk } = header
return derive(enc, KEYLENGTHS.get(enc), key, epk, header)
}

module.exports = (JWA, JWK) => {
Expand Down
10 changes: 6 additions & 4 deletions lib/jwa/ecdh/kw.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ const wrapKey = (wrap, derive, key, payload) => {
const derivedKey = derive(epk, key, payload)

const result = wrap({ [KEYOBJECT]: derivedKey }, payload)
result.header = { epk: { kty: key.kty, crv: key.crv, x: epk.x, y: epk.y } }
result.header = result.header || {}
Object.assign(result.header, { epk: { kty: key.kty, crv: key.crv, x: epk.x, y: epk.y } })

return result
}

const unwrapKey = (unwrap, derive, key, payload, { apu, apv, epk }) => {
const derivedKey = derive(key, epk, { apu, apv })
const unwrapKey = (unwrap, derive, key, payload, header) => {
const { epk } = header
const derivedKey = derive(key, epk, header)

return unwrap({ [KEYOBJECT]: derivedKey }, payload)
return unwrap({ [KEYOBJECT]: derivedKey }, payload, header)
}

module.exports = (JWA, JWK) => {
Expand Down
8 changes: 5 additions & 3 deletions lib/jwa/pbes2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ const wrapKey = (keylen, sha, concat, wrap, { [KEYOBJECT]: keyObject }, payload)
const derivedKey = pbkdf2(keyObject.export(), salt, p2c, keylen, sha)

const result = wrap({ [KEYOBJECT]: derivedKey }, payload)
result.header = { p2c, p2s: base64url.encodeBuffer(p2s) }
result.header = result.header || {}
Object.assign(result.header, { p2c, p2s: base64url.encodeBuffer(p2s) })

return result
}

const unwrapKey = (keylen, sha, concat, unwrap, { [KEYOBJECT]: keyObject }, payload, { p2c, p2s }) => {
const unwrapKey = (keylen, sha, concat, unwrap, { [KEYOBJECT]: keyObject }, payload, header) => {
const { p2s, p2c } = header
const salt = concat(p2s)
const derivedKey = pbkdf2(keyObject.export(), salt, p2c, keylen, sha)
return unwrap({ [KEYOBJECT]: derivedKey }, payload)
return unwrap({ [KEYOBJECT]: derivedKey }, payload, header)
}

module.exports = (JWA, JWK) => {
Expand Down

0 comments on commit b103874

Please sign in to comment.