Starting to change MPI
This commit is contained in:
parent
2f3c0a86e9
commit
9200f026f3
|
@ -18,6 +18,7 @@
|
||||||
// The GPG4Browsers crypto interface
|
// The GPG4Browsers crypto interface
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @requires bn.js
|
||||||
* @requires asmcrypto.js
|
* @requires asmcrypto.js
|
||||||
* @requires crypto/public_key
|
* @requires crypto/public_key
|
||||||
* @requires crypto/cipher
|
* @requires crypto/cipher
|
||||||
|
@ -30,8 +31,8 @@
|
||||||
* @module crypto/crypto
|
* @module crypto/crypto
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import BN from 'bn.js';
|
||||||
import { RSA_RAW, BigNumber, Modulus } from 'asmcrypto.js';
|
import { RSA_RAW, BigNumber, Modulus } from 'asmcrypto.js';
|
||||||
import BigInteger from './public_key/jsbn';
|
|
||||||
import publicKey from './public_key';
|
import publicKey from './public_key';
|
||||||
import cipher from './cipher';
|
import cipher from './cipher';
|
||||||
import random from './random';
|
import random from './random';
|
||||||
|
@ -67,12 +68,10 @@ export default {
|
||||||
switch (algo) {
|
switch (algo) {
|
||||||
case 'rsa_encrypt':
|
case 'rsa_encrypt':
|
||||||
case 'rsa_encrypt_sign': {
|
case 'rsa_encrypt_sign': {
|
||||||
const n = util.str2Uint8Array(publicParams[0].toBytes());
|
const n = publicParams[0].toUint8Array();
|
||||||
const e = util.str2Uint8Array(publicParams[1].toBytes());
|
const e = publicParams[1].toUint8Array();
|
||||||
m = data.write().slice(2); // FIXME
|
m = data.toUint8Array();
|
||||||
return constructParams(types, [
|
return constructParams(types, [new BN(RSA_RAW.encrypt(m, [n, e]))]);
|
||||||
new BigInteger(util.hexidump(RSA_RAW.encrypt(m, [n, e])), 16) // FIXME
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
case 'elgamal': {
|
case 'elgamal': {
|
||||||
const elgamal = new publicKey.elgamal();
|
const elgamal = new publicKey.elgamal();
|
||||||
|
@ -114,13 +113,13 @@ export default {
|
||||||
switch (algo) {
|
switch (algo) {
|
||||||
case 'rsa_encrypt_sign':
|
case 'rsa_encrypt_sign':
|
||||||
case 'rsa_encrypt': {
|
case 'rsa_encrypt': {
|
||||||
const c = util.str2Uint8Array(dataIntegers[0].toBytes());
|
const c = dataIntegers[0].toUint8Array();
|
||||||
const n = util.str2Uint8Array(keyIntegers[0].toBytes()); // pq
|
const n = keyIntegers[0].toUint8Array(); // pq
|
||||||
const e = util.str2Uint8Array(keyIntegers[1].toBytes());
|
const e = keyIntegers[1].toUint8Array();
|
||||||
const d = util.str2Uint8Array(keyIntegers[2].toBytes()); // de = 1 mod (p-1)(q-1)
|
const d = keyIntegers[2].toUint8Array(); // de = 1 mod (p-1)(q-1)
|
||||||
const p = util.str2Uint8Array(keyIntegers[3].toBytes());
|
const p = keyIntegers[3].toUint8Array();
|
||||||
const q = util.str2Uint8Array(keyIntegers[4].toBytes());
|
const q = keyIntegers[4].toUint8Array();
|
||||||
const u = util.str2Uint8Array(keyIntegers[5].toBytes()); // q^-1 mod p
|
const u = keyIntegers[5].toUint8Array(); // q^-1 mod p
|
||||||
const dd = BigNumber.fromArrayBuffer(d);
|
const dd = BigNumber.fromArrayBuffer(d);
|
||||||
const dp = new Modulus(
|
const dp = new Modulus(
|
||||||
BigNumber.fromArrayBuffer(p).subtract(BigNumber.ONE)
|
BigNumber.fromArrayBuffer(p).subtract(BigNumber.ONE)
|
||||||
|
@ -128,9 +127,7 @@ export default {
|
||||||
const dq = new Modulus(
|
const dq = new Modulus(
|
||||||
BigNumber.fromArrayBuffer(q).subtract(BigNumber.ONE)
|
BigNumber.fromArrayBuffer(q).subtract(BigNumber.ONE)
|
||||||
).reduce(dd).toBytes(); // d mod (q-1)
|
).reduce(dd).toBytes(); // d mod (q-1)
|
||||||
return new BigInteger(
|
return new BN(RSA_RAW.decrypt(c, [n, e, d, q, p, dq, dp, u]).slice(1)); // FIXME remove slice
|
||||||
util.hexidump(RSA_RAW.decrypt(c, [n, e, d, q, p, dq, dp, u]).slice(1)), 16 // FIXME
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
case 'elgamal': {
|
case 'elgamal': {
|
||||||
const elgamal = new publicKey.elgamal();
|
const elgamal = new publicKey.elgamal();
|
||||||
|
|
|
@ -38,10 +38,10 @@ export default {
|
||||||
// RSA Encrypt-Only [HAC]
|
// RSA Encrypt-Only [HAC]
|
||||||
case 3: {
|
case 3: {
|
||||||
// RSA Sign-Only [HAC]
|
// 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 k = publickey_MPIs[0].byteLength();
|
||||||
const e = util.str2Uint8Array(publickey_MPIs[1].toBytes());
|
const e = publickey_MPIs[1].toUint8Array();
|
||||||
m = msg_MPIs[0].write().slice(2); // FIXME
|
m = msg_MPIs[0].toUint8Array();
|
||||||
const EM = RSA_RAW.verify(m, [n, e]);
|
const EM = RSA_RAW.verify(m, [n, e]);
|
||||||
const EM2 = pkcs1.emsa.encode(hash_algo, data, k);
|
const EM2 = pkcs1.emsa.encode(hash_algo, data, k);
|
||||||
return util.hexidump(EM) === EM2;
|
return util.hexidump(EM) === EM2;
|
||||||
|
@ -111,13 +111,11 @@ export default {
|
||||||
// RSA Encrypt-Only [HAC]
|
// RSA Encrypt-Only [HAC]
|
||||||
case 3: {
|
case 3: {
|
||||||
// RSA Sign-Only [HAC]
|
// RSA Sign-Only [HAC]
|
||||||
const n = util.str2Uint8Array(keyIntegers[0].toBytes());
|
const n = keyIntegers[0].toUint8Array();
|
||||||
const k = keyIntegers[0].byteLength();
|
const k = keyIntegers[0].byteLength();
|
||||||
const e = util.str2Uint8Array(keyIntegers[1].toBytes());
|
const e = keyIntegers[1].toUint8Array();
|
||||||
d = util.str2Uint8Array(keyIntegers[2].toBytes());
|
d = keyIntegers[2].toUint8Array();
|
||||||
m = util.hex2Uint8Array(
|
m = util.hex2Uint8Array('00'+pkcs1.emsa.encode(hash_algo, data, k)); // FIXME remove '00'
|
||||||
'00'+pkcs1.emsa.encode(hash_algo, data, k) // FIXME
|
|
||||||
);
|
|
||||||
return util.Uint8Array2MPI(RSA_RAW.sign(m, [n, e, d]));
|
return util.Uint8Array2MPI(RSA_RAW.sign(m, [n, e, d]));
|
||||||
}
|
}
|
||||||
case 17: {
|
case 17: {
|
||||||
|
|
|
@ -46,6 +46,8 @@ export default function MPI(data) {
|
||||||
/** An implementation dependent integer */
|
/** An implementation dependent integer */
|
||||||
if (data instanceof BigInteger) {
|
if (data instanceof BigInteger) {
|
||||||
this.fromBigInteger(data);
|
this.fromBigInteger(data);
|
||||||
|
} else if (data instanceof BN) {
|
||||||
|
this.fromBytes(util.Uint8Array2str(data.toArrayLike(Uint8Array)));
|
||||||
} else if (util.isString(data)) {
|
} else if (util.isString(data)) {
|
||||||
this.fromBytes(data);
|
this.fromBytes(data);
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,8 +94,11 @@ MPI.prototype.fromBytes = function (bytes) {
|
||||||
};
|
};
|
||||||
|
|
||||||
MPI.prototype.toBytes = function () {
|
MPI.prototype.toBytes = function () {
|
||||||
const bytes = util.Uint8Array2str(this.write());
|
return util.Uint8Array2str(this.toUint8Array());
|
||||||
return bytes.substr(2);
|
};
|
||||||
|
|
||||||
|
MPI.prototype.toUint8Array = function () {
|
||||||
|
return this.write().slice(2);
|
||||||
};
|
};
|
||||||
|
|
||||||
MPI.prototype.byteLength = function () {
|
MPI.prototype.byteLength = function () {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user