diff --git a/src/crypto/crypto.js b/src/crypto/crypto.js index 47873961..53085d08 100644 --- a/src/crypto/crypto.js +++ b/src/crypto/crypto.js @@ -18,6 +18,7 @@ // The GPG4Browsers crypto interface /** + * @requires bn.js * @requires asmcrypto.js * @requires crypto/public_key * @requires crypto/cipher @@ -30,8 +31,8 @@ * @module crypto/crypto */ +import BN from 'bn.js'; import { RSA_RAW, BigNumber, Modulus } from 'asmcrypto.js'; -import BigInteger from './public_key/jsbn'; import publicKey from './public_key'; import cipher from './cipher'; import random from './random'; @@ -67,12 +68,10 @@ export default { switch (algo) { case 'rsa_encrypt': case 'rsa_encrypt_sign': { - const n = util.str2Uint8Array(publicParams[0].toBytes()); - const e = util.str2Uint8Array(publicParams[1].toBytes()); - m = data.write().slice(2); // FIXME - return constructParams(types, [ - new BigInteger(util.hexidump(RSA_RAW.encrypt(m, [n, e])), 16) // FIXME - ]); + const n = publicParams[0].toUint8Array(); + const e = publicParams[1].toUint8Array(); + m = data.toUint8Array(); + return constructParams(types, [new BN(RSA_RAW.encrypt(m, [n, e]))]); } case 'elgamal': { const elgamal = new publicKey.elgamal(); @@ -114,13 +113,13 @@ export default { switch (algo) { case 'rsa_encrypt_sign': case 'rsa_encrypt': { - const c = util.str2Uint8Array(dataIntegers[0].toBytes()); - const n = util.str2Uint8Array(keyIntegers[0].toBytes()); // pq - const e = util.str2Uint8Array(keyIntegers[1].toBytes()); - const d = util.str2Uint8Array(keyIntegers[2].toBytes()); // de = 1 mod (p-1)(q-1) - const p = util.str2Uint8Array(keyIntegers[3].toBytes()); - const q = util.str2Uint8Array(keyIntegers[4].toBytes()); - const u = util.str2Uint8Array(keyIntegers[5].toBytes()); // q^-1 mod p + const c = dataIntegers[0].toUint8Array(); + const n = keyIntegers[0].toUint8Array(); // pq + const e = keyIntegers[1].toUint8Array(); + const d = keyIntegers[2].toUint8Array(); // de = 1 mod (p-1)(q-1) + const p = keyIntegers[3].toUint8Array(); + const q = keyIntegers[4].toUint8Array(); + const u = keyIntegers[5].toUint8Array(); // q^-1 mod p const dd = BigNumber.fromArrayBuffer(d); const dp = new Modulus( BigNumber.fromArrayBuffer(p).subtract(BigNumber.ONE) @@ -128,9 +127,7 @@ export default { const dq = new Modulus( BigNumber.fromArrayBuffer(q).subtract(BigNumber.ONE) ).reduce(dd).toBytes(); // d mod (q-1) - return new BigInteger( - util.hexidump(RSA_RAW.decrypt(c, [n, e, d, q, p, dq, dp, u]).slice(1)), 16 // FIXME - ); + return new BN(RSA_RAW.decrypt(c, [n, e, d, q, p, dq, dp, u]).slice(1)); // FIXME remove slice } case 'elgamal': { const elgamal = new publicKey.elgamal(); diff --git a/src/crypto/signature.js b/src/crypto/signature.js index 0bbbbbd6..4136802c 100644 --- a/src/crypto/signature.js +++ b/src/crypto/signature.js @@ -38,10 +38,10 @@ export default { // RSA Encrypt-Only [HAC] case 3: { // RSA Sign-Only [HAC] - const n = util.str2Uint8Array(publickey_MPIs[0].toBytes()); + const n = publickey_MPIs[0].toUint8Array(); const k = publickey_MPIs[0].byteLength(); - const e = util.str2Uint8Array(publickey_MPIs[1].toBytes()); - m = msg_MPIs[0].write().slice(2); // FIXME + const e = publickey_MPIs[1].toUint8Array(); + m = msg_MPIs[0].toUint8Array(); const EM = RSA_RAW.verify(m, [n, e]); const EM2 = pkcs1.emsa.encode(hash_algo, data, k); return util.hexidump(EM) === EM2; @@ -111,13 +111,11 @@ export default { // RSA Encrypt-Only [HAC] case 3: { // RSA Sign-Only [HAC] - const n = util.str2Uint8Array(keyIntegers[0].toBytes()); + const n = keyIntegers[0].toUint8Array(); const k = keyIntegers[0].byteLength(); - const e = util.str2Uint8Array(keyIntegers[1].toBytes()); - d = util.str2Uint8Array(keyIntegers[2].toBytes()); - m = util.hex2Uint8Array( - '00'+pkcs1.emsa.encode(hash_algo, data, k) // FIXME - ); + const e = keyIntegers[1].toUint8Array(); + d = keyIntegers[2].toUint8Array(); + m = util.hex2Uint8Array('00'+pkcs1.emsa.encode(hash_algo, data, k)); // FIXME remove '00' return util.Uint8Array2MPI(RSA_RAW.sign(m, [n, e, d])); } case 17: { diff --git a/src/type/mpi.js b/src/type/mpi.js index 466f6a5f..496e5a5c 100644 --- a/src/type/mpi.js +++ b/src/type/mpi.js @@ -46,6 +46,8 @@ export default function MPI(data) { /** An implementation dependent integer */ if (data instanceof BigInteger) { this.fromBigInteger(data); + } else if (data instanceof BN) { + this.fromBytes(util.Uint8Array2str(data.toArrayLike(Uint8Array))); } else if (util.isString(data)) { this.fromBytes(data); } else { @@ -92,8 +94,11 @@ MPI.prototype.fromBytes = function (bytes) { }; MPI.prototype.toBytes = function () { - const bytes = util.Uint8Array2str(this.write()); - return bytes.substr(2); + return util.Uint8Array2str(this.toUint8Array()); +}; + +MPI.prototype.toUint8Array = function () { + return this.write().slice(2); }; MPI.prototype.byteLength = function () {