verify.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/browserify-sign/browser/verify.js';
  5. var __require = nodeEnv ? function (request) {
  6. return cc.require(request);
  7. } : function (request) {
  8. return __quick_compile_project__.require(request, __filename);
  9. };
  10. function __define (exports, require, module) {
  11. if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
  12. var Buffer = require('safe-buffer').Buffer
  13. var BN = require('bn.js')
  14. var EC = require('elliptic').ec
  15. var parseKeys = require('parse-asn1')
  16. var curves = require('./curves.json')
  17. function verify (sig, hash, key, signType, tag) {
  18. var pub = parseKeys(key)
  19. if (pub.type === 'ec') {
  20. // rsa keys can be interpreted as ecdsa ones in openssl
  21. if (signType !== 'ecdsa' && signType !== 'ecdsa/rsa') throw new Error('wrong public key type')
  22. return ecVerify(sig, hash, pub)
  23. } else if (pub.type === 'dsa') {
  24. if (signType !== 'dsa') throw new Error('wrong public key type')
  25. return dsaVerify(sig, hash, pub)
  26. } else {
  27. if (signType !== 'rsa' && signType !== 'ecdsa/rsa') throw new Error('wrong public key type')
  28. }
  29. hash = Buffer.concat([tag, hash])
  30. var len = pub.modulus.byteLength()
  31. var pad = [1]
  32. var padNum = 0
  33. while (hash.length + pad.length + 2 < len) {
  34. pad.push(0xff)
  35. padNum++
  36. }
  37. pad.push(0x00)
  38. var i = -1
  39. while (++i < hash.length) {
  40. pad.push(hash[i])
  41. }
  42. pad = Buffer.from(pad)
  43. var red = BN.mont(pub.modulus)
  44. sig = new BN(sig).toRed(red)
  45. sig = sig.redPow(new BN(pub.publicExponent))
  46. sig = Buffer.from(sig.fromRed().toArray())
  47. var out = padNum < 8 ? 1 : 0
  48. len = Math.min(sig.length, pad.length)
  49. if (sig.length !== pad.length) out = 1
  50. i = -1
  51. while (++i < len) out |= sig[i] ^ pad[i]
  52. return out === 0
  53. }
  54. function ecVerify (sig, hash, pub) {
  55. var curveId = curves[pub.data.algorithm.curve.join('.')]
  56. if (!curveId) throw new Error('unknown curve ' + pub.data.algorithm.curve.join('.'))
  57. var curve = new EC(curveId)
  58. var pubkey = pub.data.subjectPrivateKey.data
  59. return curve.verify(hash, sig, pubkey)
  60. }
  61. function dsaVerify (sig, hash, pub) {
  62. var p = pub.data.p
  63. var q = pub.data.q
  64. var g = pub.data.g
  65. var y = pub.data.pub_key
  66. var unpacked = parseKeys.signature.decode(sig, 'der')
  67. var s = unpacked.s
  68. var r = unpacked.r
  69. checkValue(s, q)
  70. checkValue(r, q)
  71. var montp = BN.mont(p)
  72. var w = s.invm(q)
  73. var v = g.toRed(montp)
  74. .redPow(new BN(hash).mul(w).mod(q))
  75. .fromRed()
  76. .mul(y.toRed(montp).redPow(r.mul(w).mod(q)).fromRed())
  77. .mod(p)
  78. .mod(q)
  79. return v.cmp(r) === 0
  80. }
  81. function checkValue (b, q) {
  82. if (b.cmpn(0) <= 0) throw new Error('invalid sig')
  83. if (b.cmp(q) >= q) throw new Error('invalid sig')
  84. }
  85. module.exports = verify
  86. }
  87. if (nodeEnv) {
  88. __define(__module.exports, __require, __module);
  89. }
  90. else {
  91. __quick_compile_project__.registerModuleFunc(__filename, function () {
  92. __define(__module.exports, __require, __module);
  93. });
  94. }
  95. })();