all files / core/security/digital-signature/signature/ algorithm-handler.js

25.93% Statements 21/81
0% Branches 0/25
18.75% Functions 3/16
25.93% Lines 21/81
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                                                                                                                                                                                
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;
});