Skip to content

Support EdDSA and ECDH-ES mechanisms#25

Merged
microshine merged 5 commits intomasterfrom
eddsa
Feb 4, 2021
Merged

Support EdDSA and ECDH-ES mechanisms#25
microshine merged 5 commits intomasterfrom
eddsa

Conversation

@microshine
Copy link
Collaborator

@microshine microshine commented Feb 4, 2021

NOTE: These mechanisms are supported for NodeJS greater than v14. The module registers new providers if process.version is greater than v14 otherwise it throws 'Unrecognized name' exception

EdDSA

Key generation

const keys = await crypto.subtle.generateKey({name: "EdDSA", 
    namedCurve: "Ed28819", // or Ed448}, false, ["sign", "verify"]);

Export key

const jwk = await crypto.subtle.exportKey("jwk", keys.privateKey);

Structure of JWK private key

{
  kty: 'OKP',
  crv: 'Ed448',
  key_ops: [ 'sign' ],
  ext: true,
  d: 't5dCIp-pFh3bABBt3cwYzQDqvQS_peqjp4WcbB0eKh6zpftEoOTgXdEI4Y9QBjrcr4zOTmi1yOIL'
}

Structure of JWK public key

{
  kty: 'OKP',
  crv: 'Ed448',
  key_ops: ['verify'],
  ext: true,
  x: 'Rbvo6MVDZHPwqtAGLm1vNMAVLueu1fVLW3InZDxHltEmVgWW6PnHKc2vPh_XaI2cAWOpfXnoQyk'
}

Sign/Verify

const signature = await crypto.subtle.sign({name: "EdDSA"}, keys.privateKey, data);
const ok = await crypto.subtle.verify({name: "EdDSA"}, keys.publicKey, signature, data);

ECDH-ES

Key generation

const keys = await crypto.subtle.generateKey({name: "ECDH-ES", 
    namedCurve: "X28819", // or X448}, false, ["deriveBits", "deriveKey"]);

Export key

const jwk = await crypto.subtle.exportKey("jwk", keys.privateKey);

Structure of JWK private key

{
  kty: 'OKP',
  crv: 'X448',
  key_ops: [ 'deriveBits', 'deriveKey' ],
  ext: true,
  d: 't5dCIp-pFh3bABBt3cwYzQDqvQS_peqjp4WcbB0eKh6zpftEoOTgXdEI4Y9QBjrcr4zOTmi1yOIL'
}

Structure of JWK public key

{
  kty: 'OKP',
  crv: 'X448',
  key_ops: [],
  ext: true,
  x: 'Rbvo6MVDZHPwqtAGLm1vNMAVLueu1fVLW3InZDxHltEmVgWW6PnHKc2vPh_XaI2cAWOpfXnoQyk'
}

Derive bits

const bits = await crypto.subtle.deriveBits({name: "ECDH-ES", public: keys.publicKey}, keys.privateKey, 256);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant