ecdhes.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { diffieHellman, generateKeyPair as generateKeyPairCb, KeyObject } from 'crypto';
  2. import { promisify } from 'util';
  3. import getNamedCurve from './get_named_curve.js';
  4. import { encoder, concat, uint32be, lengthAndInput, concatKdf } from '../lib/buffer_utils.js';
  5. import { JOSENotSupported } from '../util/errors.js';
  6. import { isCryptoKey } from './webcrypto.js';
  7. import { checkEncCryptoKey } from '../lib/crypto_key.js';
  8. import isKeyObject from './is_key_object.js';
  9. import invalidKeyInput from '../lib/invalid_key_input.js';
  10. import { types } from './is_key_like.js';
  11. const generateKeyPair = promisify(generateKeyPairCb);
  12. export async function deriveKey(publicKee, privateKee, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) {
  13. let publicKey;
  14. if (isCryptoKey(publicKee)) {
  15. checkEncCryptoKey(publicKee, 'ECDH');
  16. publicKey = KeyObject.from(publicKee);
  17. }
  18. else if (isKeyObject(publicKee)) {
  19. publicKey = publicKee;
  20. }
  21. else {
  22. throw new TypeError(invalidKeyInput(publicKee, ...types));
  23. }
  24. let privateKey;
  25. if (isCryptoKey(privateKee)) {
  26. checkEncCryptoKey(privateKee, 'ECDH', 'deriveBits');
  27. privateKey = KeyObject.from(privateKee);
  28. }
  29. else if (isKeyObject(privateKee)) {
  30. privateKey = privateKee;
  31. }
  32. else {
  33. throw new TypeError(invalidKeyInput(privateKee, ...types));
  34. }
  35. const value = concat(lengthAndInput(encoder.encode(algorithm)), lengthAndInput(apu), lengthAndInput(apv), uint32be(keyLength));
  36. const sharedSecret = diffieHellman({ privateKey, publicKey });
  37. return concatKdf(sharedSecret, keyLength, value);
  38. }
  39. export async function generateEpk(kee) {
  40. let key;
  41. if (isCryptoKey(kee)) {
  42. key = KeyObject.from(kee);
  43. }
  44. else if (isKeyObject(kee)) {
  45. key = kee;
  46. }
  47. else {
  48. throw new TypeError(invalidKeyInput(kee, ...types));
  49. }
  50. switch (key.asymmetricKeyType) {
  51. case 'x25519':
  52. return generateKeyPair('x25519');
  53. case 'x448': {
  54. return generateKeyPair('x448');
  55. }
  56. case 'ec': {
  57. const namedCurve = getNamedCurve(key);
  58. return generateKeyPair('ec', { namedCurve });
  59. }
  60. default:
  61. throw new JOSENotSupported('Invalid or unsupported EPK');
  62. }
  63. }
  64. export const ecdhAllowed = (key) => ['P-256', 'P-384', 'P-521', 'X25519', 'X448'].includes(getNamedCurve(key));