Skip to content
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: es crypto features to nodejs and browser #106

Merged
merged 16 commits into from
Feb 26, 2024

Conversation

lukasjhan
Copy link
Member

I implement

  • Ed25519 and ES256 signer and verifier for crypto-nodejs
  • ES256 signer and verifier for crypto-browser (not tested)

Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
@lukasjhan lukasjhan added the feature New functionality. label Feb 25, 2024
@lukasjhan lukasjhan self-assigned this Feb 25, 2024
Copy link

codecov bot commented Feb 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.54%. Comparing base (e105ef2) to head (b0a453e).
Report is 4 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next     #106      +/-   ##
==========================================
+ Coverage   94.96%   95.54%   +0.57%     
==========================================
  Files          21       21              
  Lines        1529     1705     +176     
  Branches      235      246      +11     
==========================================
+ Hits         1452     1629     +177     
  Misses         72       72              
+ Partials        5        4       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lukasjhan
Copy link
Member Author

hmm.. I have no idea how to test browser crypto subtle module. Any ideas?

@cre8
Copy link
Contributor

cre8 commented Feb 25, 2024

hmm.. I have no idea how to test browser crypto subtle module. Any ideas?

It depends on the test framework and I read that vitest with jsdom does not support the crypto apis... I tested it manually by running a crypress test with angular because then it is using a chrome instance in the background

@lukasjhan
Copy link
Member Author

@cre8 Yeah, we definitely use some framework that use headless chrome(or other browser)

How about trying this? https://playwright.dev/

@cre8
Copy link
Contributor

cre8 commented Feb 25, 2024

@cre8 Yeah, we definitely use some framework that use headless chrome(or other browser)

How about trying this? https://playwright.dev/

I can take a look at this

@cre8 cre8 self-assigned this Feb 25, 2024
@lukasjhan
Copy link
Member Author

@cre8 Yeah, we definitely use some framework that use headless chrome(or other browser)
How about trying this? https://playwright.dev/

I can take a look at this

Thank you :)

Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
@lukasjhan lukasjhan marked this pull request as draft February 25, 2024 14:53
@lukasjhan
Copy link
Member Author

I want to support Ed25519 and ES256 as JWK but,

browser - doesn't seem to support Ed25519 natively.
node - bad jwk support

I turned this PR to draft and keep working on it.

Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
@lukasjhan lukasjhan marked this pull request as ready for review February 25, 2024 15:08
@lukasjhan
Copy link
Member Author

In nodejs I used webcrypto

but it is only support node >= 20. I tested in node 18 it failed.

Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
@lukasjhan
Copy link
Member Author

@cre8 Looks like test are working well ;)

@cre8
Copy link
Contributor

cre8 commented Feb 25, 2024

@lukasjhan I think we need to support ES256 since it's the only algorithm supported by both platforms. Of course we can implement dozens of others, but in the first place we should have one algorithm people can use in both environments to sign and verify credentials.

Are you sure with the support? According to the docs webcrypto is supported since v15.

Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
@cre8
Copy link
Contributor

cre8 commented Feb 25, 2024

@lukasjhan I implemented a matrix check in #111 and it allowed to run the node test on version 16, 18 and 20 (more details in the last PR message).

To update the errors in the test, please add the same skip conditions as I have done with the others. I am not sure if we can skip it by wrapping it in a if statement or just return a standard test so the file is not empty.

@cre8 cre8 closed this Feb 25, 2024
@cre8 cre8 reopened this Feb 25, 2024
Signed-off-by: Lukas <Lukas@hopae.io>
Signed-off-by: Lukas <Lukas@hopae.io>
Signed-off-by: Lukas <Lukas@hopae.io>
Signed-off-by: Lukas <Lukas@hopae.io>
@lukasjhan
Copy link
Member Author

@cre8 I applied your matrix check :)
I remove Ed25519 in nodejs-crypto package. Like you said, At first let's supporting ES256

Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Copy link
Contributor

@cre8 cre8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

);

// Convert to PEM format
const publicKey = spkiToPEM(exportedPublicKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is PEM the correct way to go? I would suggest to use JWKs.

generateKeyPair: () => {
return generateKeyPairSync('ed25519', {
publicKeyEncoding: {
type: 'spki', // Recommended format for public key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same like browser, using JWK

createPrivateKey,
createPublicKey,
generateKeyPairSync,
} from 'crypto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we use the available methods from import { subtle } from 'node:crypto';, the function code is identical with the browser one and we would have more reusable code.

See: https://nodejs.org/api/webcrypto.html

exportPKCS8,
exportSPKI,
KeyLike,
} from 'jose';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is jose still required? @lukasjhan I don't see any usage of the imported methods here

@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
import { generateSalt, digest } from '../crypto';
import { generateSalt, digest, ES256, Ed25519 } from '../index';

describe('This file is for utility functions', () => {
test('crypto', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we included tests, we can delete this dummy test, can't we?

@@ -18,6 +18,9 @@
"test:node": "vitest run ./src/test/*.spec.ts",
"test:cov": "vitest run --coverage"
},
"dependencies": {
"jose": "^5.2.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this here?

@cre8 cre8 merged commit 2d8206e into openwallet-foundation-labs:next Feb 26, 2024
10 checks passed
cre8 added a commit to cre8/sd-jwt-js that referenced this pull request Mar 8, 2024
…-labs#106)

Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
Signed-off-by: Lukas <Lukas@hopae.io>
Co-authored-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
cre8 added a commit that referenced this pull request Mar 8, 2024
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
Signed-off-by: Lukas <Lukas@hopae.io>
Co-authored-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants