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

Refactor @web5/crypto to replace Web Crypto CryptoKey with JWK #318

Merged
merged 18 commits into from
Nov 28, 2023

Conversation

frankhinek
Copy link
Contributor

@frankhinek frankhinek commented Nov 28, 2023

Summary

This PR primarily focuses on swapping the key format used in the @web5/crypto package from the Web Crypto API CryptoKey to JOSE JSON Web Key (JWK).

Context

The majority of cryptography usage across the @web5 packages deals with JOSE objects and operations. As a consequence, the use of CryptoKey introduces significant overhead and degraded developer experience due to the frequent need to convert to/from JWK. Using JWK as the key format for all public APIs in the @web5/crypto package will improve these and other related issues.

Note

All other packages in web5-js continue to depend on the prior version of the crypto package (v0.2.2). While this PR was unavoidably large, breaking up the refactor of downstream packages will occur in a future PR.

Changes

@web5/crypto

  • Replace all use of CryptoKey and byte array key formats in public APIs with JSON Web Key (JWK).
  • Add bytesToPrivateKey(), bytesToPublicKey(), privateKeyToBytes(), and privateKeyToBytes() to align with the API used in web5-kt.
  • All generateKey() methods now return a private key in JWK format rather than a key pair to align with the API used in web5-kt.
  • Begin adding test vectors in the format defined in sdk-development repo that can be contributed and then consumed by this SDK.
  • Rewrote tests to ensure 100% test coverage for the @web5/crypto package.
  • Significantly improved documentation and usage examples for all crypto-primitive implementations.
  • While not addressed in this PR, small changes were made to make it easier for the upcoming refactor to a common key management / crypto API across web5-kt, web5-js, and web5-rs SDKs.
  • Bump cryptography dependencies
    • @noble/ciphers from 0.1.4 to 0.4.0
    • @noble/curves from 1.1.0 to 1.2.0
    • @noble/hashes from 1.3.1 to 1.3.2

@web5/identity-agent

  • Fixed incorrect dependencies in package.json that were causing build failures.

Signed-off-by: Frank Hinek <[email protected]>
Signed-off-by: Frank Hinek <[email protected]>
Signed-off-by: Frank Hinek <[email protected]>
Signed-off-by: Frank Hinek <[email protected]>
@frankhinek frankhinek added the key-mgmt Key Management label Nov 28, 2023
@frankhinek frankhinek self-assigned this Nov 28, 2023
Copy link

codesandbox bot commented Nov 28, 2023

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

github-actions bot commented Nov 28, 2023

TBDocs Report

✅ No errors or warnings

@web5/api

  • Project entry file: packages/api/src/index.ts

Updated @ 2023-11-28T14:17:44.712Z - Commit: b14cdb4

Copy link

codecov bot commented Nov 28, 2023

Codecov Report

Merging #318 (d6b444d) into main (c417ba0) will increase coverage by 0.99%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #318      +/-   ##
==========================================
+ Coverage   90.78%   91.77%   +0.99%     
==========================================
  Files          74       73       -1     
  Lines       14090    15759    +1669     
  Branches     1390     1448      +58     
==========================================
+ Hits        12791    14463    +1672     
+ Misses       1273     1270       -3     
  Partials       26       26              
Components Coverage Δ
api 96.68% <ø> (+0.22%) ⬆️
common 95.00% <ø> (ø)
credentials 94.32% <ø> (ø)
crypto 100.00% <100.00%> (ø)
dids 88.75% <ø> (ø)
agent 88.07% <ø> (ø)
identity-agent 56.81% <ø> (ø)
proxy-agent 58.43% <ø> (ø)
user-agent 55.22% <ø> (ø)

Signed-off-by: Frank Hinek <[email protected]>
@frankhinek frankhinek merged commit 590a5fc into main Nov 28, 2023
29 of 30 checks passed
@frankhinek frankhinek deleted the crypto-jwk branch November 28, 2023 22:24
finn-block pushed a commit that referenced this pull request Mar 19, 2024
)

* Refactor Ed25519 to generateKey instead of generateKeyPair
* Refactor Secp256k1 to generateKey instead of generateKeyPair and simplify sign/verify
* Refactor X25519 to generateKey instead of generateKeyPair
* Refactor PBKDF2 to use JWKs
* Remove CryptoKeyToJwkMixin
* Improve test coverage for PBKDF2
* Refactor Ed25519, Secp256k1, and X25519 to use JWKs
* Refactor EcdhAlgorithm to use JWK
* Refactor EcdsaAlgorithm to use JWK
* Refactor EdDsaAlgorithm to use JWK
* Refactor AesCtrAlgorithm to use JWK
* Refactor AesCtrAlgorithm to JWK
* Refactor AesGcm to use JWK
* Bump @noble ciphers, curves, and hashes dependencies

---------

Signed-off-by: Frank Hinek <[email protected]>
finn-block pushed a commit that referenced this pull request Mar 19, 2024
)

* Refactor Ed25519 to generateKey instead of generateKeyPair
* Refactor Secp256k1 to generateKey instead of generateKeyPair and simplify sign/verify
* Refactor X25519 to generateKey instead of generateKeyPair
* Refactor PBKDF2 to use JWKs
* Remove CryptoKeyToJwkMixin
* Improve test coverage for PBKDF2
* Refactor Ed25519, Secp256k1, and X25519 to use JWKs
* Refactor EcdhAlgorithm to use JWK
* Refactor EcdsaAlgorithm to use JWK
* Refactor EdDsaAlgorithm to use JWK
* Refactor AesCtrAlgorithm to use JWK
* Refactor AesCtrAlgorithm to JWK
* Refactor AesGcm to use JWK
* Bump @noble ciphers, curves, and hashes dependencies

---------

Signed-off-by: Frank Hinek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
key-mgmt Key Management
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants