|
| 1 | +/* global BigInt */ |
| 2 | + |
| 3 | +const { randomBytes } = require('crypto') |
| 4 | + |
| 5 | +const base64url = require('./base64url') |
| 6 | + |
| 7 | +const ZERO = BigInt(0) |
| 8 | +const ONE = BigInt(1) |
| 9 | +const TWO = BigInt(2) |
| 10 | + |
| 11 | +const toJWKParameter = n => base64url.encodeBuffer(Buffer.from(n.toString(16), 'hex')) |
| 12 | +const fromBuffer = buf => BigInt(`0x${buf.toString('hex')}`) |
| 13 | +const bitLength = n => n.toString(2).length |
| 14 | + |
| 15 | +const eGcdX = (a, b) => { |
| 16 | + let x = ZERO |
| 17 | + let y = ONE |
| 18 | + let u = ONE |
| 19 | + let v = ZERO |
| 20 | + |
| 21 | + while (a !== ZERO) { |
| 22 | + let q = b / a |
| 23 | + let r = b % a |
| 24 | + let m = x - (u * q) |
| 25 | + let n = y - (v * q) |
| 26 | + b = a |
| 27 | + a = r |
| 28 | + x = u |
| 29 | + y = v |
| 30 | + u = m |
| 31 | + v = n |
| 32 | + } |
| 33 | + return x |
| 34 | +} |
| 35 | + |
| 36 | +const gcd = (a, b) => { |
| 37 | + let shift = ZERO |
| 38 | + while (!((a | b) & ONE)) { |
| 39 | + a >>= ONE |
| 40 | + b >>= ONE |
| 41 | + shift++ |
| 42 | + } |
| 43 | + while (!(a & ONE)) { |
| 44 | + a >>= ONE |
| 45 | + } |
| 46 | + do { |
| 47 | + while (!(b & ONE)) { |
| 48 | + b >>= ONE |
| 49 | + } |
| 50 | + if (a > b) { |
| 51 | + let x = a |
| 52 | + a = b |
| 53 | + b = x |
| 54 | + } |
| 55 | + b -= a |
| 56 | + } while (b) |
| 57 | + |
| 58 | + return a << shift |
| 59 | +} |
| 60 | + |
| 61 | +const modPow = (a, b, n) => { |
| 62 | + a = toZn(a, n) |
| 63 | + let result = ONE |
| 64 | + let x = a |
| 65 | + while (b > 0) { |
| 66 | + var leastSignificantBit = b % TWO |
| 67 | + b = b / TWO |
| 68 | + if (leastSignificantBit === ONE) { |
| 69 | + result = result * x |
| 70 | + result = result % n |
| 71 | + } |
| 72 | + x = x * x |
| 73 | + x = x % n |
| 74 | + } |
| 75 | + return result |
| 76 | +} |
| 77 | + |
| 78 | +const randBetween = (min, max) => { |
| 79 | + const interval = max - min |
| 80 | + const bitLen = bitLength(interval) |
| 81 | + let rnd |
| 82 | + do { |
| 83 | + rnd = fromBuffer(randBits(bitLen)) |
| 84 | + } while (rnd > interval) |
| 85 | + return rnd + min |
| 86 | +} |
| 87 | + |
| 88 | +const randBits = (bitLength) => { |
| 89 | + const byteLength = Math.ceil(bitLength / 8) |
| 90 | + const rndBytes = randomBytes(byteLength) |
| 91 | + // Fill with 0's the extra bits |
| 92 | + rndBytes[0] = rndBytes[0] & (2 ** (bitLength % 8) - 1) |
| 93 | + return rndBytes |
| 94 | +} |
| 95 | + |
| 96 | +const toZn = (a, n) => { |
| 97 | + a = a % n |
| 98 | + return (a < 0) ? a + n : a |
| 99 | +} |
| 100 | + |
| 101 | +const odd = (n) => { |
| 102 | + let r = n |
| 103 | + while (r % TWO === ZERO) { |
| 104 | + r = r / TWO |
| 105 | + } |
| 106 | + return r |
| 107 | +} |
| 108 | + |
| 109 | +const getPrimeFactors = (e, d, n) => { |
| 110 | + const r = odd(e * d - ONE) |
| 111 | + |
| 112 | + let y |
| 113 | + do { |
| 114 | + let i = modPow(randBetween(TWO, n), r, n) |
| 115 | + let o = ZERO |
| 116 | + while (i !== ONE) { |
| 117 | + o = i |
| 118 | + i = (i * i) % n |
| 119 | + } |
| 120 | + if (o !== (n - ONE)) { |
| 121 | + y = o |
| 122 | + } |
| 123 | + } while (!y) |
| 124 | + |
| 125 | + const p = gcd(y - ONE, n) |
| 126 | + const q = n / p |
| 127 | + |
| 128 | + return p > q ? { p, q } : { p: q, q: p } |
| 129 | +} |
| 130 | + |
| 131 | +module.exports = (jwk) => { |
| 132 | + const e = fromBuffer(base64url.decodeToBuffer(jwk.e)) |
| 133 | + const d = fromBuffer(base64url.decodeToBuffer(jwk.d)) |
| 134 | + const n = fromBuffer(base64url.decodeToBuffer(jwk.n)) |
| 135 | + |
| 136 | + const { p, q } = getPrimeFactors(e, d, n) |
| 137 | + const dp = d % (p - ONE) |
| 138 | + const dq = d % (q - ONE) |
| 139 | + const qi = toZn(eGcdX(toZn(q, p), p), p) |
| 140 | + |
| 141 | + return { |
| 142 | + ...jwk, |
| 143 | + p: toJWKParameter(p), |
| 144 | + q: toJWKParameter(q), |
| 145 | + dp: toJWKParameter(dp), |
| 146 | + dq: toJWKParameter(dq), |
| 147 | + qi: toJWKParameter(qi) |
| 148 | + } |
| 149 | +} |
0 commit comments