define(["require", "exports", "../../../utils"], function (require, exports, utils_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _PdfRsaCoreAlgorithm = (function () {
function _PdfRsaCoreAlgorithm() {
}
_PdfRsaCoreAlgorithm.prototype._getInputBlockSize = function () {
return this._isEncryption ? (this._bitSize - 1) >>> 3 : (this._bitSize + 7) >>> 3;
};
_PdfRsaCoreAlgorithm.prototype._getOutputBlockSize = function () {
return this._isEncryption ? (this._bitSize + 7) >>> 3 : (this._bitSize - 1) >>> 3;
};
_PdfRsaCoreAlgorithm.prototype._initialize = function (isEncryption, parameters) {
this._key = parameters;
this._isEncryption = isEncryption;
this._bitSize = this._key.modulus._bitLength();
};
_PdfRsaCoreAlgorithm.prototype._convertInput = function (bytes, offset, length) {
var subBytes = bytes.subarray(offset, offset + length);
if (subBytes.length > this._getInputBlockSize() + 1) {
throw new Error('Input data too large for RSA block.');
}
var input = utils_1._bytesToBigInt(subBytes);
if (input >= this._key.modulus) {
throw new Error('Input data is larger than modulus.');
}
return input;
};
_PdfRsaCoreAlgorithm.prototype._convertOutput = function (result) {
var output = utils_1._bigIntToBytes(result);
if (this._isEncryption) {
var outSize = this._getOutputBlockSize();
if (output.length < outSize) {
var paddedOutput = new Uint8Array(outSize);
paddedOutput.set(output, outSize - output.length);
return paddedOutput;
}
}
return output;
};
_PdfRsaCoreAlgorithm.prototype._processBlock = function (input) {
var bigInt = utils_1._getBigInt();
var privateKey = this._key;
if (privateKey._isPrivate && privateKey.p && privateKey.q) {
var p = privateKey.p._toBigInt();
var q = privateKey.q._toBigInt();
var dP = privateKey.dP._toBigInt();
var dQ = privateKey.dQ._toBigInt();
var qInv = privateKey.inverse._toBigInt();
var mP = utils_1._modPow(input % p, dP, p);
var mQ = utils_1._modPow(input % q, dQ, q);
var h = (mP - mQ) * qInv;
h = h % p;
if (h < bigInt('0')) {
h += p;
}
var m = h * q;
m = m + mQ;
return m;
}
return utils_1._modPow(input, this._key.exponent, this._key.modulus);
};
return _PdfRsaCoreAlgorithm;
}());
exports._PdfRsaCoreAlgorithm = _PdfRsaCoreAlgorithm;
var _PdfRsaAlgorithm = (function () {
function _PdfRsaAlgorithm() {
this._rsaCoreEngine = new _PdfRsaCoreAlgorithm();
}
_PdfRsaAlgorithm.prototype._getAlgorithmName = function () {
return 'RSA';
};
_PdfRsaAlgorithm.prototype._getInputBlock = function () {
return this._rsaCoreEngine._getInputBlockSize();
};
_PdfRsaAlgorithm.prototype._getOutputBlock = function () {
return this._rsaCoreEngine._getOutputBlockSize();
};
_PdfRsaAlgorithm.prototype._initialize = function (isEncryption, parameter) {
this._rsaCoreEngine._initialize(isEncryption, parameter);
this._key = parameter;
};
_PdfRsaAlgorithm.prototype._processBlock = function (bytes, offset, length) {
var bigInt = utils_1._getBigInt();
if (!this._key) {
throw new Error('RSA engine not initialized.');
}
var input = this._rsaCoreEngine._convertInput(bytes, offset, length);
var result;
var privateKey = this._key;
if (privateKey._isPrivate && privateKey.publicExponent) {
var e = privateKey.publicExponent._toBigInt();
var m = this._key.modulus._toBigInt();
var r = utils_1._createRandomInRange(bigInt(1), m - bigInt(1));
var blindedInput = (utils_1._modPow(r, e, m) * input) % m;
var blindedResult = this._rsaCoreEngine._processBlock(blindedInput);
var rInverse = utils_1._modInverse(r, m);
result = (blindedResult * rInverse) % m;
}
else {
result = this._rsaCoreEngine._processBlock(input);
}
return this._rsaCoreEngine._convertOutput(result);
};
return _PdfRsaAlgorithm;
}());
exports._PdfRsaAlgorithm = _PdfRsaAlgorithm;
});
|