You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.3 KiB

  1. // Copyright 2015 Joyent, Inc.
  2. module.exports = {
  3. Verifier: Verifier,
  4. Signer: Signer
  5. };
  6. var nacl = require('tweetnacl');
  7. var stream = require('stream');
  8. var util = require('util');
  9. var assert = require('assert-plus');
  10. var Buffer = require('safer-buffer').Buffer;
  11. var Signature = require('./signature');
  12. function Verifier(key, hashAlgo) {
  13. if (hashAlgo.toLowerCase() !== 'sha512')
  14. throw (new Error('ED25519 only supports the use of ' +
  15. 'SHA-512 hashes'));
  16. this.key = key;
  17. this.chunks = [];
  18. stream.Writable.call(this, {});
  19. }
  20. util.inherits(Verifier, stream.Writable);
  21. Verifier.prototype._write = function (chunk, enc, cb) {
  22. this.chunks.push(chunk);
  23. cb();
  24. };
  25. Verifier.prototype.update = function (chunk) {
  26. if (typeof (chunk) === 'string')
  27. chunk = Buffer.from(chunk, 'binary');
  28. this.chunks.push(chunk);
  29. };
  30. Verifier.prototype.verify = function (signature, fmt) {
  31. var sig;
  32. if (Signature.isSignature(signature, [2, 0])) {
  33. if (signature.type !== 'ed25519')
  34. return (false);
  35. sig = signature.toBuffer('raw');
  36. } else if (typeof (signature) === 'string') {
  37. sig = Buffer.from(signature, 'base64');
  38. } else if (Signature.isSignature(signature, [1, 0])) {
  39. throw (new Error('signature was created by too old ' +
  40. 'a version of sshpk and cannot be verified'));
  41. }
  42. assert.buffer(sig);
  43. return (nacl.sign.detached.verify(
  44. new Uint8Array(Buffer.concat(this.chunks)),
  45. new Uint8Array(sig),
  46. new Uint8Array(this.key.part.A.data)));
  47. };
  48. function Signer(key, hashAlgo) {
  49. if (hashAlgo.toLowerCase() !== 'sha512')
  50. throw (new Error('ED25519 only supports the use of ' +
  51. 'SHA-512 hashes'));
  52. this.key = key;
  53. this.chunks = [];
  54. stream.Writable.call(this, {});
  55. }
  56. util.inherits(Signer, stream.Writable);
  57. Signer.prototype._write = function (chunk, enc, cb) {
  58. this.chunks.push(chunk);
  59. cb();
  60. };
  61. Signer.prototype.update = function (chunk) {
  62. if (typeof (chunk) === 'string')
  63. chunk = Buffer.from(chunk, 'binary');
  64. this.chunks.push(chunk);
  65. };
  66. Signer.prototype.sign = function () {
  67. var sig = nacl.sign.detached(
  68. new Uint8Array(Buffer.concat(this.chunks)),
  69. new Uint8Array(Buffer.concat([
  70. this.key.part.k.data, this.key.part.A.data])));
  71. var sigBuf = Buffer.from(sig);
  72. var sigObj = Signature.parse(sigBuf, 'ed25519', 'raw');
  73. sigObj.hashAlgorithm = 'sha512';
  74. return (sigObj);
  75. };