/
kdf_params.js
71 lines (65 loc) · 2.24 KB
/
kdf_params.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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
*
* {@link https://tools.ietf.org/html/rfc6637#section-7|RFC 6637 7}:
* A key derivation function (KDF) is necessary to implement the EC
* encryption. The Concatenation Key Derivation Function (Approved
* Alternative 1) [NIST-SP800-56A] with the KDF hash function that is
* SHA2-256 [FIPS-180-3] or stronger is REQUIRED.
* @requires enums
* @module type/kdf_params
*/
/**
* @constructor
* @param {enums.hash} hash Hash algorithm
* @param {enums.symmetric} cipher Symmetric algorithm
*/
function KDFParams(data) {
if (data && data.length === 2) {
this.hash = data[0];
this.cipher = data[1];
} else {
this.hash = null;
this.cipher = null;
}
}
/**
* Read KDFParams from an Uint8Array
* @param {Uint8Array} input Where to read the KDFParams from
* @returns {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
* @returns {Uint8Array} Array with the KDFParams value
*/
KDFParams.prototype.write = function () {
return new Uint8Array([3, 1, this.hash, this.cipher]);
};
KDFParams.fromClone = function (clone) {
return new KDFParams([clone.hash, clone.cipher]);
};
export default KDFParams;