-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from transmute-industries/feat/hpke-experiments
[WIP ]Add HPKE examples
- Loading branch information
Showing
29 changed files
with
994 additions
and
104 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,40 @@ | ||
import * as cbor from 'cbor-web' | ||
|
||
// const COSE_Sign1_TAG = 18 | ||
function toHexString(byteArray: Uint8Array) { | ||
return Array.prototype.map | ||
.call(byteArray, function (byte) { | ||
return ('0' + (byte & 0xff).toString(16)).slice(-2) | ||
const alternateDiagnostic = async ( | ||
data: any | ||
) => { | ||
const decoded = await cbor.decode(data) | ||
if (decoded.tag === 18 && decoded.value[2] === null) { | ||
decoded.value[2] = 'nil' | ||
const data2 = await cbor.encode(decoded) | ||
let text = await cbor.diagnose(data2, { | ||
separator: '\n' | ||
}) | ||
.join('') | ||
} | ||
text = text.replace(/"nil"/gm, 'nil') | ||
|
||
const prettyHeaderKey = (k: string) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return ({ | ||
[`1`]: 'alg', | ||
[`3`]: 'content_type', | ||
[`4`]: 'kid', | ||
// new | ||
[`100`]: 'inclusion-proof', | ||
[`200`]: 'consistency-proof', | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any)[`${k}`] | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const prettyHeaderValue = (v: any) => { | ||
const value = ({ | ||
[`-7`]: '"ES256"', | ||
[`-35`]: '"ES384"', | ||
[`-36`]: '"ES512"', | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any)[`${v}`] | ||
return value ? value : `h'${toHexString(new TextEncoder().encode(v))}'` | ||
} | ||
text = text.replace(/\(/gm, '(\n') | ||
text = text.replace(/\)/gm, '\n)') | ||
|
||
const diagnosticProtectedHeader = (data: Uint8Array) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const decoded = cbor.decode(data, { dictionary: 'map' } as any) | ||
const lines = [] | ||
for (const [k, v] of decoded.entries()) { | ||
lines.push(` # "${prettyHeaderKey(k)}" : ${prettyHeaderValue(v)}`) | ||
lines.push(` # ${k} : ${v}`) | ||
} | ||
return ` # Protected Header | ||
h'${toHexString(data)}', | ||
# { | ||
${lines.join(',\n')} | ||
# } | ||
` | ||
} | ||
text = text.replace(/\[/gm, '[\n') | ||
text = text.replace(/\]/gm, '\n]') | ||
|
||
const diagnosticData = (data: Uint8Array) => { | ||
return `h'${toHexString(data)}'` | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const diagnosticUnprotectedHeader = (decoded: any) => { | ||
if (!decoded.entries) { | ||
return ' # Unprotected Header\n {},\n' | ||
} | ||
const lines = [] | ||
for (const [k, v] of decoded.entries()) { | ||
lines.push( | ||
` # "${prettyHeaderKey(k)}" : "${prettyHeaderValue(v)}" | ||
${k} : ${prettyHeaderValue(v)} `, | ||
) | ||
} | ||
return ` # Unprotected Header | ||
{ | ||
${lines.join(',\n')} | ||
}, | ||
` | ||
} | ||
text = text.replace(/, /gm, ',\n') | ||
|
||
const default_options = { | ||
decode_payload: true, | ||
detached_payload: false, | ||
} | ||
const alternateDiagnostic = async ( | ||
data: Uint8Array, | ||
options = default_options, | ||
) => { | ||
let diagnostic = '' | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const { tag, value } = cbor.decode(data, { dictionary: 'map' } as any) | ||
const unprotectedHeader = diagnosticUnprotectedHeader(value[1]) | ||
diagnostic += `# COSE_Sign1\n${tag}([\n\n` | ||
diagnostic += diagnosticProtectedHeader(value[0]) | ||
diagnostic += '\n' | ||
diagnostic += unprotectedHeader | ||
diagnostic += '\n' | ||
if (options.detached_payload) { | ||
diagnostic += ' ' + '# Detached Payload\n' | ||
} else { | ||
diagnostic += ' ' + '# Protected Payload\n' | ||
diagnostic += ' ' + diagnosticData(value[2]) + ',\n' | ||
if (options.decode_payload) { | ||
diagnostic += ' ' + '# ' + new TextDecoder().decode(value[2]) + '\n' | ||
} | ||
return text | ||
} | ||
let text = await cbor.diagnose(data, { | ||
separator: '\n' | ||
}) | ||
text = text.replace(/\{/gm, '{\n') | ||
text = text.replace(/\}/gm, '\n}') | ||
|
||
text = text.replace(/\[/gm, '[\n') | ||
text = text.replace(/\]/gm, '\n]') | ||
|
||
text = text.replace(/, /gm, ',\n') | ||
|
||
return text | ||
|
||
diagnostic += '\n' | ||
diagnostic += ' ' + '# Signature\n' | ||
diagnostic += ' ' + diagnosticData(value[3]) + '\n' | ||
diagnostic += `])` | ||
return diagnostic | ||
} | ||
|
||
export default alternateDiagnostic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 🔥 Experimental HPKE Stuff 🔥 | ||
|
||
## JOSE | ||
|
||
- [1 layer](./jose/direct.md) | ||
- [2 layer](./jose/wrap.md) | ||
|
||
## COSE | ||
|
||
- [1 layer](./cose/direct.md) | ||
- [2 layer](./cose/wrap.md) |
Oops, something went wrong.