|
| 1 | +import { emsa_pss_encode, i2osp, os2ip, rsasp1, rsavp1 } from './util'; |
| 2 | + |
| 3 | +import sjcl from './sjcl'; |
| 4 | + |
| 5 | +export async function blind( |
| 6 | + publicKey: CryptoKey, |
| 7 | + msg: Uint8Array, |
| 8 | + saltLength = 0, |
| 9 | +): Promise<{ |
| 10 | + blindedMsg: Uint8Array; |
| 11 | + blindInv: Uint8Array; |
| 12 | +}> { |
| 13 | + if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'RSA-PSS') { |
| 14 | + throw new Error('key is not RSA-PSS'); |
| 15 | + } |
| 16 | + if (!publicKey.extractable) { |
| 17 | + throw new Error('key is not extractable'); |
| 18 | + } |
| 19 | + |
| 20 | + const { modulusLength, hash: hashFn } = publicKey.algorithm as RsaHashedKeyGenParams; |
| 21 | + const kBits = modulusLength; |
| 22 | + const kLen = Math.ceil(kBits / 8); |
| 23 | + const hash = (hashFn as Algorithm).name; |
| 24 | + |
| 25 | + // 1. encoded_msg = EMSA-PSS-ENCODE(msg, kBits - 1) |
| 26 | + // with MGF and HF as defined in the parameters |
| 27 | + // 2. If EMSA-PSS-ENCODE raises an error, raise the error and stop |
| 28 | + const encoded_msg = await emsa_pss_encode(msg, kBits - 1, { sLen: saltLength, hash }); |
| 29 | + |
| 30 | + // 3. m = bytes_to_int(encoded_msg) |
| 31 | + const m = os2ip(encoded_msg); |
| 32 | + const jwkKey = await crypto.subtle.exportKey('jwk', publicKey); |
| 33 | + if (!jwkKey.n || !jwkKey.e) { |
| 34 | + throw new Error('key has invalid parameters'); |
| 35 | + } |
| 36 | + const n = new sjcl.bn(Buffer.from(jwkKey.n, 'base64url').toString('hex')); |
| 37 | + const e = new sjcl.bn(Buffer.from(jwkKey.e, 'base64url').toString('hex')); |
| 38 | + |
| 39 | + // 4. r = random_integer_uniform(1, n) |
| 40 | + let r: sjcl.bn; |
| 41 | + do { |
| 42 | + r = os2ip(crypto.getRandomValues(new Uint8Array(kLen))); |
| 43 | + } while (r.greaterEquals(n)); |
| 44 | + |
| 45 | + // 5. r_inv = inverse_mod(r, n) |
| 46 | + // 6. If inverse_mod fails, raise an "invalid blind" error |
| 47 | + // and stop |
| 48 | + let r_inv: sjcl.bn; |
| 49 | + try { |
| 50 | + r_inv = r.inverseMod(new sjcl.bn(n)); |
| 51 | + } catch (e) { |
| 52 | + throw new Error('invalid blind'); |
| 53 | + } |
| 54 | + // 7. x = RSAVP1(pkS, r) |
| 55 | + const x = rsavp1({ n, e }, r); |
| 56 | + |
| 57 | + // 8. z = m * x mod n |
| 58 | + const z = m.mulmod(x, n); |
| 59 | + |
| 60 | + // 9. blinded_msg = int_to_bytes(z, kLen) |
| 61 | + const blindedMsg = i2osp(z, kLen); |
| 62 | + |
| 63 | + // 10. inv = int_to_bytes(r_inv, kLen) |
| 64 | + const blindInv = i2osp(r_inv, kLen); |
| 65 | + |
| 66 | + // 11. output blinded_msg, inv |
| 67 | + return { blindedMsg, blindInv }; |
| 68 | +} |
| 69 | + |
| 70 | +export async function finalize( |
| 71 | + publicKey: CryptoKey, |
| 72 | + msg: Uint8Array, |
| 73 | + blindInv: Uint8Array, |
| 74 | + blindSig: Uint8Array, |
| 75 | + saltLength = 0, |
| 76 | +): Promise<Uint8Array> { |
| 77 | + if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'RSA-PSS') { |
| 78 | + throw new Error('key is not RSA-PSS'); |
| 79 | + } |
| 80 | + if (!publicKey.extractable) { |
| 81 | + throw new Error('key is not extractable'); |
| 82 | + } |
| 83 | + const { modulusLength } = publicKey.algorithm as RsaHashedKeyGenParams; |
| 84 | + const kLen = Math.ceil(modulusLength / 8); |
| 85 | + |
| 86 | + // 1. If len(blind_sig) != kLen, raise "unexpected input size" and stop |
| 87 | + // 2. If len(inv) != kLen, raise "unexpected input size" and stop |
| 88 | + if (blindSig.length != kLen || blindInv.length != kLen) { |
| 89 | + throw new Error('unexpected input size'); |
| 90 | + } |
| 91 | + |
| 92 | + // 3. z = bytes_to_int(blind_sig) |
| 93 | + const z = os2ip(blindSig); |
| 94 | + |
| 95 | + // 4. r_inv = bytes_to_int(inv) |
| 96 | + const r_inv = os2ip(blindInv); |
| 97 | + |
| 98 | + // 5. s = z * r_inv mod n |
| 99 | + const jwkKey = await crypto.subtle.exportKey('jwk', publicKey); |
| 100 | + if (!jwkKey.n) { |
| 101 | + throw new Error('key has invalid parameters'); |
| 102 | + } |
| 103 | + const n = new sjcl.bn(Buffer.from(jwkKey.n, 'base64url').toString('hex')); |
| 104 | + const s = z.mulmod(r_inv, n); |
| 105 | + |
| 106 | + // 6. sig = int_to_bytes(s, kLen) |
| 107 | + const sig = i2osp(s, kLen); |
| 108 | + |
| 109 | + // 7. result = RSASSA-PSS-VERIFY(pkS, msg, sig) |
| 110 | + // 8. If result = "valid signature", output sig, else |
| 111 | + // raise "invalid signature" and stop |
| 112 | + const algorithm = { name: 'RSA-PSS', saltLength }; |
| 113 | + if (!(await crypto.subtle.verify(algorithm, publicKey, sig, msg))) { |
| 114 | + throw new Error('invalid signature'); |
| 115 | + } |
| 116 | + |
| 117 | + return sig; |
| 118 | +} |
| 119 | + |
| 120 | +export async function blindSign(privateKey: CryptoKey, blindMsg: Uint8Array): Promise<Uint8Array> { |
| 121 | + if (privateKey.type !== 'private' || privateKey.algorithm.name !== 'RSA-PSS') { |
| 122 | + throw new Error('key is not RSA-PSS'); |
| 123 | + } |
| 124 | + if (!privateKey.extractable) { |
| 125 | + throw new Error('key is not extractable'); |
| 126 | + } |
| 127 | + const { modulusLength } = privateKey.algorithm as RsaHashedKeyGenParams; |
| 128 | + const kLen = Math.ceil(modulusLength / 8); |
| 129 | + |
| 130 | + // 1. If len(blinded_msg) != kLen, raise "unexpected input size" |
| 131 | + // and stop |
| 132 | + if (blindMsg.length != kLen) { |
| 133 | + throw new Error('unexpected input size'); |
| 134 | + } |
| 135 | + |
| 136 | + // 2. m = bytes_to_int(blinded_msg) |
| 137 | + const m = os2ip(blindMsg); |
| 138 | + |
| 139 | + // 3. If m >= n, raise "invalid message length" and stop |
| 140 | + const jwkKey = await crypto.subtle.exportKey('jwk', privateKey); |
| 141 | + if (!jwkKey.n || !jwkKey.d) { |
| 142 | + throw new Error('key is not a private key'); |
| 143 | + } |
| 144 | + const n = new sjcl.bn(Buffer.from(jwkKey.n, 'base64url').toString('hex')); |
| 145 | + const d = new sjcl.bn(Buffer.from(jwkKey.d, 'base64url').toString('hex')); |
| 146 | + if (m.greaterEquals(n)) { |
| 147 | + throw new Error('invalid message length'); |
| 148 | + } |
| 149 | + |
| 150 | + // 4. s = RSASP1(skS, m) |
| 151 | + const s = rsasp1({ n, d }, m); |
| 152 | + |
| 153 | + // 5. blind_sig = int_to_bytes(s, kLen) |
| 154 | + // 6. output blind_sig |
| 155 | + return i2osp(s, kLen); |
| 156 | +} |
0 commit comments