mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-11-26 23:46:03 +00:00
refactor: s2k uses gnuType for dummy keys
This commit is contained in:
parent
df654e7b5e
commit
2983d225cc
@ -134,7 +134,7 @@ class SecretKeyPacket extends PublicKeyPacket {
|
|||||||
this.s2k = newS2KFromType(s2kType);
|
this.s2k = newS2KFromType(s2kType);
|
||||||
i += this.s2k.read(bytes.subarray(i, bytes.length));
|
i += this.s2k.read(bytes.subarray(i, bytes.length));
|
||||||
|
|
||||||
if (this.s2k.type === 'gnu-dummy') {
|
if (this.s2k.gnuType === 'gnu-dummy') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (this.s2kUsage) {
|
} else if (this.s2kUsage) {
|
||||||
@ -253,7 +253,7 @@ class SecretKeyPacket extends PublicKeyPacket {
|
|||||||
// - [Optional] If secret data is encrypted (string-to-key usage octet
|
// - [Optional] If secret data is encrypted (string-to-key usage octet
|
||||||
// not zero), an Initial Vector (IV) of the same length as the
|
// not zero), an Initial Vector (IV) of the same length as the
|
||||||
// cipher's block size.
|
// cipher's block size.
|
||||||
if (this.s2kUsage && this.s2k.type !== 'gnu-dummy') {
|
if (this.s2kUsage && this.s2k.gnuType !== 'gnu-dummy') {
|
||||||
optionalFieldsArr.push(...this.iv);
|
optionalFieldsArr.push(...this.iv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,7 +306,7 @@ class SecretKeyPacket extends PublicKeyPacket {
|
|||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
isDummy() {
|
isDummy() {
|
||||||
return !!(this.s2k && this.s2k.type === 'gnu-dummy');
|
return !!(this.s2k && this.s2k.gnuType === 'gnu-dummy');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -327,7 +327,8 @@ class SecretKeyPacket extends PublicKeyPacket {
|
|||||||
this.s2k = newS2KFromType(enums.s2k.gnu, config);
|
this.s2k = newS2KFromType(enums.s2k.gnu, config);
|
||||||
this.s2k.algorithm = 0;
|
this.s2k.algorithm = 0;
|
||||||
this.s2k.c = 0;
|
this.s2k.c = 0;
|
||||||
this.s2k.type = 'gnu-dummy';
|
this.s2k.type = 'gnu';
|
||||||
|
this.s2k.gnuType = 'gnu-dummy';
|
||||||
this.s2kUsage = 254;
|
this.s2kUsage = 254;
|
||||||
this.symmetric = enums.symmetric.aes256;
|
this.symmetric = enums.symmetric.aes256;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,46 +15,14 @@
|
|||||||
// License along with this library; if not, write to the Free Software
|
// License along with this library; if not, write to the Free Software
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
/**
|
|
||||||
* Implementation of the String-to-key specifier
|
|
||||||
*
|
|
||||||
* {@link https://tools.ietf.org/html/rfc4880#section-3.7|RFC4880 3.7}:
|
|
||||||
* String-to-key (S2K) specifiers are used to convert passphrase strings
|
|
||||||
* into symmetric-key encryption/decryption keys. They are used in two
|
|
||||||
* places, currently: to encrypt the secret part of private keys in the
|
|
||||||
* private keyring, and to convert passphrases to encryption keys for
|
|
||||||
* symmetrically encrypted messages.
|
|
||||||
* @module type/s2k
|
|
||||||
*/
|
|
||||||
|
|
||||||
import defaultConfig from '../../config';
|
|
||||||
import enums from '../../enums';
|
import enums from '../../enums';
|
||||||
import { UnsupportedError } from '../../packet/packet';
|
import { UnsupportedError } from '../../packet/packet';
|
||||||
import util from '../../util';
|
import util from '../../util';
|
||||||
|
|
||||||
class GnuS2k {
|
class GnuS2K {
|
||||||
algorithm: number;
|
type: 'gnu' = 'gnu';
|
||||||
type: string;
|
gnuType?: 'gnu-dummy' = undefined;
|
||||||
c: number;
|
algorithm: number = enums.hash.sha256
|
||||||
/**
|
|
||||||
* @param {Object} [config] - Full configuration, defaults to openpgp.config
|
|
||||||
*/
|
|
||||||
constructor(config = defaultConfig) {
|
|
||||||
/**
|
|
||||||
* Hash function identifier, or 0 for gnu-dummy keys
|
|
||||||
* @type {module:enums.hash | 0}
|
|
||||||
*/
|
|
||||||
this.algorithm = enums.hash.sha256;
|
|
||||||
/**
|
|
||||||
* enums.s2k identifier or 'gnu-dummy'
|
|
||||||
* @type {String}
|
|
||||||
*/
|
|
||||||
this.type = 'gnu';
|
|
||||||
/**
|
|
||||||
* @type {Integer}
|
|
||||||
*/
|
|
||||||
this.c = config.s2kIterationCountByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parsing function for a string-to-key specifier ({@link https://tools.ietf.org/html/rfc4880#section-3.7|RFC 4880 3.7}).
|
* Parsing function for a string-to-key specifier ({@link https://tools.ietf.org/html/rfc4880#section-3.7|RFC 4880 3.7}).
|
||||||
@ -68,7 +36,7 @@ class GnuS2k {
|
|||||||
i += 3; // GNU
|
i += 3; // GNU
|
||||||
const gnuExtType = 1000 + bytes[i++];
|
const gnuExtType = 1000 + bytes[i++];
|
||||||
if (gnuExtType === 1001) {
|
if (gnuExtType === 1001) {
|
||||||
this.type = 'gnu-dummy';
|
this.gnuType = 'gnu-dummy';
|
||||||
// GnuPG extension mode 1001 -- don't write secret key at all
|
// GnuPG extension mode 1001 -- don't write secret key at all
|
||||||
} else {
|
} else {
|
||||||
throw new UnsupportedError('Unknown s2k gnu protection mode.');
|
throw new UnsupportedError('Unknown s2k gnu protection mode.');
|
||||||
@ -85,7 +53,7 @@ class GnuS2k {
|
|||||||
* @returns {Uint8Array} Binary representation of s2k.
|
* @returns {Uint8Array} Binary representation of s2k.
|
||||||
*/
|
*/
|
||||||
write(): Uint8Array {
|
write(): Uint8Array {
|
||||||
if (this.type === 'gnu-dummy') {
|
if (this.gnuType === 'gnu-dummy') {
|
||||||
return new Uint8Array([101, 0, ...util.stringToUint8Array('GNU'), 1]);
|
return new Uint8Array([101, 0, ...util.stringToUint8Array('GNU'), 1]);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('GNU s2k type not supported.');
|
throw new Error('GNU s2k type not supported.');
|
||||||
@ -101,19 +69,8 @@ class GnuS2k {
|
|||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
async produceKey(passphrase: string, numBytes: number): Promise<Uint8Array> {
|
async produceKey(passphrase: string, numBytes: number): Promise<Uint8Array> {
|
||||||
const arr: number[] = [];
|
throw new Error('Gnu S2K does not support producing keys');
|
||||||
let rlength = 0;
|
|
||||||
|
|
||||||
while (rlength < numBytes) {
|
|
||||||
if (this.type !== 'gnu') {
|
|
||||||
throw new Error('Unknown s2k type.');
|
|
||||||
} else {
|
|
||||||
throw new Error('GNU s2k type not supported.');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.concatUint8Array(arr).subarray(0, numBytes);
|
export default GnuS2K;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default GnuS2k;
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ const allowedS2KTypesForEncryption = new Set([enums.s2k.argon2, enums.s2k.iterat
|
|||||||
export function newS2KFromType (type: number, config = defaultConfig): Argon2S2K | GenericS2K | GnuS2K {
|
export function newS2KFromType (type: number, config = defaultConfig): Argon2S2K | GenericS2K | GnuS2K {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case enums.s2k.gnu:
|
case enums.s2k.gnu:
|
||||||
return new GnuS2K(config);
|
return new GnuS2K();
|
||||||
case enums.s2k.argon2:
|
case enums.s2k.argon2:
|
||||||
return new Argon2S2K(config);
|
return new Argon2S2K(config);
|
||||||
case enums.s2k.iterated:
|
case enums.s2k.iterated:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user