-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sig-utils): add browser support #417
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
040f303
Update Node.js version
raducristianpopa db25209
Update README
raducristianpopa 785d1d5
Crypto progress
raducristianpopa b94e5ae
Update Node versions in CI and node types
raducristianpopa a8e5ba9
Format
raducristianpopa fc75709
Update tests and improve crypto
raducristianpopa 2357f59
Fix jest `trasnformIgnorePattern`
raducristianpopa e8d1858
Merge branch 'main' into rp--update-crypto
raducristianpopa d9d52be
Attempt to fix jest tests
raducristianpopa 12c7a9b
Small clean-up
raducristianpopa 14fb4df
Format
raducristianpopa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
projects: ['<rootDir>/packages/*/jest.config.js'] | ||
} |
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,57 @@ | ||
import { generateEd25519KeyPair, exportPKCS8, exportJWK } from './crypto' | ||
|
||
describe('crypto', (): void => { | ||
describe('generateEd25519KeyPair', (): void => { | ||
test('generates key pair using the 25519 curve', async (): Promise<void> => { | ||
const { privateKey, publicKey } = generateEd25519KeyPair() | ||
|
||
expect(privateKey).toHaveLength(48) | ||
expect(publicKey).toHaveLength(32) | ||
}) | ||
}) | ||
|
||
describe('exportPCKS8', (): void => { | ||
test('exports private key as PKCS8 in PEM format', async (): Promise<void> => { | ||
const { privateKey } = generateEd25519KeyPair() | ||
const pem = exportPKCS8(privateKey) | ||
const [, body] = pem.split('\n') | ||
|
||
expect(pem).toContain('-----BEGIN PRIVATE KEY-----') | ||
expect(pem).toContain('-----END PRIVATE KEY-----') | ||
expect(Buffer.from(body, 'base64').toString('base64')).toStrictEqual(body) | ||
}) | ||
}) | ||
|
||
test('compatibility - noble, subtle crypto', async (): Promise<void> => { | ||
const { publicKey, privateKey } = generateEd25519KeyPair() | ||
const importedPublicKey = await crypto.subtle.importKey( | ||
'raw', | ||
publicKey, | ||
'Ed25519', | ||
true, | ||
['verify'] | ||
) | ||
const importedPrivateKey = await crypto.subtle.importKey( | ||
'pkcs8', | ||
privateKey, | ||
'Ed25519', | ||
true, | ||
['sign'] | ||
) | ||
|
||
const customPublicKeyExport = exportJWK(publicKey) | ||
const customPrivateKeyExport = exportJWK(privateKey) | ||
const subtlePublicKeyExport = await crypto.subtle.exportKey( | ||
'jwk', | ||
importedPublicKey | ||
) | ||
const subtlePrivateKeyExport = await crypto.subtle.exportKey( | ||
'jwk', | ||
importedPrivateKey | ||
) | ||
|
||
expect(customPublicKeyExport.x).toStrictEqual(subtlePublicKeyExport.x) | ||
expect(customPrivateKeyExport.x).toStrictEqual(subtlePrivateKeyExport.x) | ||
expect(customPrivateKeyExport.d).toStrictEqual(subtlePrivateKeyExport.d) | ||
}) | ||
}) |
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,77 @@ | ||
import * as ed from '@noble/ed25519' | ||
import { sha512 } from '@noble/hashes/sha512' | ||
import { type JWK } from './jwk' | ||
import { binaryToBase64url } from './helpers' | ||
|
||
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m)) | ||
|
||
/** | ||
* PKCS#8 PrivateKeyInfo SEQUENCE: | ||
* - version: INTEGER 0 | ||
* - algorithm: SEQUENCE | ||
* - OJBECT IDENTIFIER: 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm) | ||
* - privateKey OCTET STRING | ||
*/ | ||
const PCKS8_SEQUENCE_PARTS = new Uint8Array([ | ||
48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32 | ||
]) | ||
|
||
export function generateEd25519KeyPair() { | ||
const privateKey = ed.utils.randomPrivateKey() | ||
const publicKey = ed.getPublicKey(privateKey) | ||
|
||
return { | ||
privateKey: new Uint8Array([...PCKS8_SEQUENCE_PARTS, ...privateKey]), | ||
publicKey | ||
} | ||
} | ||
|
||
export function exportPKCS8(privateKey: Uint8Array) { | ||
const header = '-----BEGIN PRIVATE KEY-----' | ||
const footer = '-----END PRIVATE KEY-----' | ||
|
||
const body = | ||
typeof Buffer !== 'undefined' | ||
? Buffer.from(privateKey).toString('base64') | ||
: btoa(String.fromCharCode(...privateKey)) | ||
Comment on lines
+33
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a PEM format, the body needs to be only base64 encoded. |
||
|
||
return `${header}\n${body}\n${footer}` | ||
} | ||
|
||
export function exportJWK(key: Uint8Array): Omit<JWK, 'kid' | 'alg'> { | ||
if (![32, 48].includes(key.length)) throw new Error('Invalid key length.') | ||
|
||
let binary = '' | ||
const isPrivateKey = key.length === 48 | ||
|
||
if (isPrivateKey) { | ||
let publicKeyBinary = '' | ||
const privateKey = key.subarray(16, 48) | ||
const publicKey = ed.getPublicKey(privateKey) | ||
privateKey.forEach((byte) => { | ||
binary += String.fromCharCode(byte) | ||
}) | ||
publicKey.forEach((byte) => { | ||
publicKeyBinary += String.fromCharCode(byte) | ||
}) | ||
|
||
return { | ||
kty: 'OKP', | ||
crv: 'Ed25519', | ||
x: binaryToBase64url(publicKeyBinary), | ||
d: binaryToBase64url(binary) | ||
Comment on lines
+61
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a JWK, the
|
||
} | ||
} | ||
|
||
key.forEach((byte) => { | ||
binary += String.fromCharCode(byte) | ||
}) | ||
|
||
return { | ||
kty: 'OKP', | ||
crv: 'Ed25519', | ||
x: binaryToBase64url(binary) | ||
} | ||
} | ||
|
||
export { ed } |
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,24 @@ | ||
export function stringToUint8Array(str: string) { | ||
const buffer = new ArrayBuffer(str.length) | ||
const bufferView = new Uint8Array(buffer) | ||
|
||
for (let i = 0, strLen = str.length; i < strLen; i++) { | ||
bufferView[i] = str.charCodeAt(i) | ||
} | ||
|
||
return bufferView | ||
} | ||
|
||
export function binaryToBase64url(value: string): string { | ||
let base64: string | undefined | ||
if (typeof Buffer !== 'undefined') { | ||
base64 = Buffer.from(value, 'binary').toString('base64url') | ||
} else { | ||
base64 = btoa(value) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+$/, '') | ||
} | ||
|
||
return base64 | ||
} |
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ export interface JWK { | |
kty: 'OKP' | ||
crv: 'Ed25519' | ||
x: string | ||
d?: string | ||
} | ||
|
||
export const generateJwk = ({ | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This represents the first two PKCS#8 sequence elements (version, algorithm). Generating the private key with
@noble/ed25519
only returns the the raw value of the private key.