Starting to change MPI

This commit is contained in:
Mahrud Sayrafi 2018-02-15 19:09:09 -08:00
parent 2f3c0a86e9
commit 9200f026f3
No known key found for this signature in database
GPG Key ID: C24071B956C3245F
3 changed files with 28 additions and 28 deletions

View File

@ -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();

View File

@ -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: {

View File

@ -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 () {