From 26c6ab9b9705f619e4449a36baf6300ce2f19f71 Mon Sep 17 00:00:00 2001 From: Ismael Bejarano Date: Fri, 12 Aug 2016 00:23:20 -0300 Subject: [PATCH] Parameters for the ECDH key derivation function --- src/index.js | 6 +++++ src/type/kdf_params.js | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/type/kdf_params.js diff --git a/src/index.js b/src/index.js index e701837b..fd0e9de5 100644 --- a/src/index.js +++ b/src/index.js @@ -83,6 +83,12 @@ export { default as Keyid } from './type/keyid'; */ export { default as EcdhSymmetricKey } from './type/ecdh_symkey'; +/** + * @see module:type/kdf_params + * @name module:openpgp.KdfParams + */ +export { default as KdfParams } from './type/kdf_params'; + /** * @see module:type/oid * @name module:openpgp.Oid diff --git a/src/type/kdf_params.js b/src/type/kdf_params.js new file mode 100644 index 00000000..1a6d1371 --- /dev/null +++ b/src/type/kdf_params.js @@ -0,0 +1,61 @@ +// OpenPGP.js - An OpenPGP implementation in javascript +// Copyright (C) 2015-2016 Decentral +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3.0 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +/** + * Implementation of type KDF parameters RFC 6637
+ *
+ * @requires enums + * @module type/kdf_params + */ + +'use strict'; + +import enums from '../enums.js'; + +module.exports = KdfParams; + +/** + * @constructor + * @param {enums.hash} hash Hash algorithm + * @param {enums.symmetric} cipher Symmetric algorithm + */ +function KdfParams(hash, cipher) { + this.hash = hash || enums.hash.sha1; + this.cipher = cipher || enums.symmetric.aes128; +} + +/** + * Read KdfParams from an Uint8Array + * @param {Uint8Array} input Where to read the KdfParams from + * @return {Number} Number of read bytes + */ +KdfParams.prototype.read = function (input) { + if (input.length < 4 || input[0] !== 3 || input[1] !== 1) { + throw new Error('Cannot read KdfParams'); + } + this.hash = input[2]; + this.cipher = input[3]; + return 4; +}; + +/** + * Write KdfParams to an Uint8Array + * @return {Uint8Array} Array with the KdfParams value + */ +KdfParams.prototype.write = function () { + return new Uint8Array([3, 1, this.hash, this.cipher]); +};