Store named key params in key objects (#1141)

- Store private and public params separately and by name in objects,
  instead of as an array

- Do not keep params in MPI form, but convert them to Uint8Arrays when
  generating/parsing the key

- Modify low-level crypto functions to always accept and return
  Uint8Arrays instead of BigIntegers

- Move PKCS1 padding to lower level functions
This commit is contained in:
larabr
2020-09-04 14:23:17 +02:00
committed by Daniel Huigens
parent 8854b097b4
commit 3a75eadaa0
24 changed files with 634 additions and 776 deletions

View File

@@ -167,6 +167,31 @@ export default {
return str;
},
/**
* Read one MPI from bytes in input
* @param {Uint8Array} bytes input data to parse
* @returns {Uint8Array} parsed MPI
*/
readMPI: function (bytes) {
const bits = (bytes[0] << 8) | bytes[1];
const bytelen = (bits + 7) >>> 3;
return bytes.subarray(2, 2 + bytelen);
},
/**
* Pad Uint8Array to length by adding 0x0 bytes
* @param {Uint8Array} bytes data to pad
* @param {Number} length padded length
* @param {'be'|'le'} endianess endianess of input data
* @return {Uint8Array} padded bytes
*/
padToLength(bytes, length, endianess = 'be') {
const padded = new Uint8Array(length);
const offset = (endianess === 'be') ? 0 : (length - bytes.length);
padded.set(bytes, offset);
return padded;
},
/**
* Convert a Uint8Array to an MPI-formatted Uint8Array.
* Note: the output is **not** an MPI object.