Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

sign.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
  2. var createHmac = require('create-hmac')
  3. var crt = require('browserify-rsa')
  4. var EC = require('elliptic').ec
  5. var BN = require('bn.js')
  6. var parseKeys = require('parse-asn1')
  7. var curves = require('./curves.json')
  8. function sign (hash, key, hashType, signType, tag) {
  9. var priv = parseKeys(key)
  10. if (priv.curve) {
  11. // rsa keys can be interpreted as ecdsa ones in openssl
  12. if (signType !== 'ecdsa' && signType !== 'ecdsa/rsa') throw new Error('wrong private key type')
  13. return ecSign(hash, priv)
  14. } else if (priv.type === 'dsa') {
  15. if (signType !== 'dsa') throw new Error('wrong private key type')
  16. return dsaSign(hash, priv, hashType)
  17. } else {
  18. if (signType !== 'rsa' && signType !== 'ecdsa/rsa') throw new Error('wrong private key type')
  19. }
  20. hash = Buffer.concat([tag, hash])
  21. var len = priv.modulus.byteLength()
  22. var pad = [ 0, 1 ]
  23. while (hash.length + pad.length + 1 < len) pad.push(0xff)
  24. pad.push(0x00)
  25. var i = -1
  26. while (++i < hash.length) pad.push(hash[i])
  27. var out = crt(pad, priv)
  28. return out
  29. }
  30. function ecSign (hash, priv) {
  31. var curveId = curves[priv.curve.join('.')]
  32. if (!curveId) throw new Error('unknown curve ' + priv.curve.join('.'))
  33. var curve = new EC(curveId)
  34. var key = curve.keyFromPrivate(priv.privateKey)
  35. var out = key.sign(hash)
  36. return new Buffer(out.toDER())
  37. }
  38. function dsaSign (hash, priv, algo) {
  39. var x = priv.params.priv_key
  40. var p = priv.params.p
  41. var q = priv.params.q
  42. var g = priv.params.g
  43. var r = new BN(0)
  44. var k
  45. var H = bits2int(hash, q).mod(q)
  46. var s = false
  47. var kv = getKey(x, q, hash, algo)
  48. while (s === false) {
  49. k = makeKey(q, kv, algo)
  50. r = makeR(g, k, p, q)
  51. s = k.invm(q).imul(H.add(x.mul(r))).mod(q)
  52. if (s.cmpn(0) === 0) {
  53. s = false
  54. r = new BN(0)
  55. }
  56. }
  57. return toDER(r, s)
  58. }
  59. function toDER (r, s) {
  60. r = r.toArray()
  61. s = s.toArray()
  62. // Pad values
  63. if (r[0] & 0x80) r = [ 0 ].concat(r)
  64. if (s[0] & 0x80) s = [ 0 ].concat(s)
  65. var total = r.length + s.length + 4
  66. var res = [ 0x30, total, 0x02, r.length ]
  67. res = res.concat(r, [ 0x02, s.length ], s)
  68. return new Buffer(res)
  69. }
  70. function getKey (x, q, hash, algo) {
  71. x = new Buffer(x.toArray())
  72. if (x.length < q.byteLength()) {
  73. var zeros = new Buffer(q.byteLength() - x.length)
  74. zeros.fill(0)
  75. x = Buffer.concat([ zeros, x ])
  76. }
  77. var hlen = hash.length
  78. var hbits = bits2octets(hash, q)
  79. var v = new Buffer(hlen)
  80. v.fill(1)
  81. var k = new Buffer(hlen)
  82. k.fill(0)
  83. k = createHmac(algo, k).update(v).update(new Buffer([ 0 ])).update(x).update(hbits).digest()
  84. v = createHmac(algo, k).update(v).digest()
  85. k = createHmac(algo, k).update(v).update(new Buffer([ 1 ])).update(x).update(hbits).digest()
  86. v = createHmac(algo, k).update(v).digest()
  87. return { k: k, v: v }
  88. }
  89. function bits2int (obits, q) {
  90. var bits = new BN(obits)
  91. var shift = (obits.length << 3) - q.bitLength()
  92. if (shift > 0) bits.ishrn(shift)
  93. return bits
  94. }
  95. function bits2octets (bits, q) {
  96. bits = bits2int(bits, q)
  97. bits = bits.mod(q)
  98. var out = new Buffer(bits.toArray())
  99. if (out.length < q.byteLength()) {
  100. var zeros = new Buffer(q.byteLength() - out.length)
  101. zeros.fill(0)
  102. out = Buffer.concat([ zeros, out ])
  103. }
  104. return out
  105. }
  106. function makeKey (q, kv, algo) {
  107. var t
  108. var k
  109. do {
  110. t = new Buffer(0)
  111. while (t.length * 8 < q.bitLength()) {
  112. kv.v = createHmac(algo, kv.k).update(kv.v).digest()
  113. t = Buffer.concat([ t, kv.v ])
  114. }
  115. k = bits2int(t, q)
  116. kv.k = createHmac(algo, kv.k).update(kv.v).update(new Buffer([ 0 ])).digest()
  117. kv.v = createHmac(algo, kv.k).update(kv.v).digest()
  118. } while (k.cmp(q) !== -1)
  119. return k
  120. }
  121. function makeR (g, k, p, q) {
  122. return g.toRed(BN.mont(p)).redPow(k).fromRed().mod(q)
  123. }
  124. module.exports = sign
  125. module.exports.getKey = getKey
  126. module.exports.makeKey = makeKey